-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathTextureMetadataHandler.java
More file actions
204 lines (189 loc) · 9.38 KB
/
TextureMetadataHandler.java
File metadata and controls
204 lines (189 loc) · 9.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package team.chisel.ctm.client.util;
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import net.minecraft.client.resources.model.ModelResourceLocation;
import org.apache.logging.log4j.message.ParameterizedMessage;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import it.unimi.dsi.fastutil.objects.Object2BooleanLinkedOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2BooleanMap;
import lombok.SneakyThrows;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.client.resources.model.BlockModelRotation;
import net.minecraft.client.resources.model.Material;
import net.minecraft.client.resources.model.ModelBakery;
import net.minecraft.client.resources.model.UnbakedModel;
import net.minecraft.resources.ResourceLocation;
import net.neoforged.neoforge.client.event.ModelEvent;
import net.neoforged.bus.api.EventPriority;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.util.ObfuscationReflectionHelper;
import org.jetbrains.annotations.NotNull;
import team.chisel.ctm.CTM;
import team.chisel.ctm.client.model.AbstractCTMBakedModel;
import team.chisel.ctm.client.model.ModelBakedCTM;
import team.chisel.ctm.client.model.ModelCTM;
public enum TextureMetadataHandler {
INSTANCE;
private final Object2BooleanMap<ModelResourceLocation> wrappedModels = new Object2BooleanLinkedOpenHashMap<>();
private final Multimap<ResourceLocation, Material> scrapedTextures = HashMultimap.create();
public void textureScraped(ResourceLocation modelLocation, Material material) {
scrapedTextures.put(modelLocation, material);
}
/*
* Handle stitching metadata additional textures
*/
// @SubscribeEvent(priority = EventPriority.LOWEST)
// public void onTextureStitch(TextureStitchEvent.Pre event) {
// Set<ResourceLocation> sprites = new HashSet<>(ObfuscationReflectionHelper.getPrivateValue(TextureStitchEvent.Pre.class, event, "sprites"));
// for (ResourceLocation rel : sprites) {
// try {
// rel = new ResourceLocation(rel.getNamespace(), "textures/" + rel.getPath() + ".png");
// Optional<IMetadataSectionCTM> metadata = ResourceUtil.getMetadata(rel);
// var proxy = metadata.map(IMetadataSectionCTM::getProxy);
// if (proxy.isPresent()) {
// ResourceLocation proxysprite = new ResourceLocation(proxy.get());
// Optional<IMetadataSectionCTM> proxymeta = ResourceUtil.getMetadata(ResourceUtil.spriteToAbsolute(proxysprite));
// // Load proxy's base sprite
// event.addSprite(proxysprite);
// proxymeta.ifPresent(m -> {
// // Load proxy's additional textures
// for (ResourceLocation r : m.getAdditionalTextures()) {
// if (registeredTextures.add(r)) {
// event.addSprite(r);
// }
// }
// });
// }
//
// metadata.map(IMetadataSectionCTM::getAdditionalTextures)
// .ifPresent(textures -> {
// // Load additional textures
// for (ResourceLocation r : textures) {
// if (registeredTextures.add(r)) {
// event.addSprite(r);
// }
// }
// });
// }
// catch (FileNotFoundException e) {} // Ignore these, they are reported by vanilla
// catch (IOException e) {
// e.printStackTrace();
// }
// }
// }
/*
* Handle wrapping models that use CTM textures
*/
// private static final Class<?> multipartModelClass;
// private static final Class<?> vanillaModelWrapperClass;
// private static final Field multipartPartModels;
// private static final Field modelWrapperModel;
// static {
// try {
// multipartModelClass = Class.forName("net.minecraftforge.client.model.ModelLoader$MultipartModel");
// multipartPartModels = multipartModelClass.getDeclaredField("partModels");
// multipartPartModels.setAccessible(true);
// vanillaModelWrapperClass = Class.forName("net.minecraftforge.client.model.ModelLoader$VanillaModelWrapper");
// modelWrapperModel = vanillaModelWrapperClass.getDeclaredField("model");
// modelWrapperModel.setAccessible(true);
// } catch (ClassNotFoundException | NoSuchFieldException | SecurityException e) {
// throw Throwables.propagate(e);
// }
// }
@SubscribeEvent(priority = EventPriority.LOWEST) // low priority to capture all event-registered models
@SneakyThrows
public void onModelBake(ModelEvent.ModifyBakingResult event) {
ModelBakery modelBakery = event.getModelBakery();
for (Map.Entry<ModelResourceLocation, BakedModel> entry : event.getModels().entrySet()) {
ModelResourceLocation mrl = entry.getKey();
UnbakedModel rootModel = modelBakery.topLevelModels.get(mrl);
if (rootModel != null) {
BakedModel baked = entry.getValue();
if (baked == null || baked instanceof AbstractCTMBakedModel) {
continue;
}
if (baked.isCustomRenderer()) { // Nothing we can add to builtin models
continue;
}
Deque<ResourceLocation> dependencies = new ArrayDeque<>();
Set<ResourceLocation> seenModels = new HashSet<>();
ResourceLocation rl = mrl.id();
dependencies.push(rl);
seenModels.add(rl);
boolean shouldWrap = wrappedModels.getOrDefault(mrl, false);
// Breadth-first loop through dependencies, exiting as soon as a CTM texture is found, and skipping duplicates/cycles
while (!shouldWrap && !dependencies.isEmpty()) {
ResourceLocation dep = dependencies.pop();
UnbakedModel model;
try {
model = dep == rl ? rootModel : modelBakery.getModel(dep);
} catch (Exception e) {
continue;
}
try {
Set<Material> textures = new HashSet<>(scrapedTextures.get(dep));
for (Material tex : textures) {
// Cache all dependent texture metadata
try {
// At least one texture has CTM metadata, so we should wrap this model
if (ResourceUtil.getMetadata(ResourceUtil.spriteToAbsolute(tex.texture())).isPresent()) { // TODO lazy
shouldWrap = true;
break;
}
} catch (IOException e) {} // Fallthrough
}
if (!shouldWrap) {
for (ResourceLocation newDep : model.getDependencies()) {
if (seenModels.add(newDep)) {
dependencies.push(newDep);
}
}
}
} catch (Exception e) {
CTM.logger.error(new ParameterizedMessage("Error loading model dependency {} for model {}. Skipping...", dep, rl), e);
}
}
wrappedModels.put(mrl, shouldWrap);
if (shouldWrap) {
try {
entry.setValue(wrap(rootModel, baked));
} catch (IOException e) {
CTM.logger.error("Could not wrap model {}. Aborting...", rl, e);
}
}
}
}
}
private @NotNull BakedModel wrap(UnbakedModel model, BakedModel object) throws IOException {
ModelCTM modelchisel = new ModelCTM(model);
return new ModelBakedCTM(modelchisel, object, null);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@SubscribeEvent
public void onModelBake(ModelEvent.BakingCompleted event) {
var cache = ObfuscationReflectionHelper.<Map, ModelBakery>getPrivateValue(ModelBakery.class, event.getModelBakery(), "bakedCache");
var cacheCopy = Map.copyOf(cache);
cache.clear();
for (var e : event.getModels().entrySet()) {
if (e.getValue() instanceof AbstractCTMBakedModel baked &&
baked.getModel() instanceof ModelCTM ctmModel &&
!ctmModel.isInitialized()) {
var baker = event.getModelBakery().new ModelBakerImpl((rl, m) -> m.sprite(), e.getKey());
ctmModel.bake(baker, Material::sprite, BlockModelRotation.X0_Y0);
//Note: We have to clear the cache after baking each model to ensure that we can initialize and capture any textures
// that might be done by parent models
cache.clear();
}
}
cache.putAll(cacheCopy);
}
public void invalidateCaches() {
wrappedModels.clear();
scrapedTextures.clear();
}
}