ModUtilities.java
9.17 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package com.mumfrey.liteloader.util;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.entity.Render;
import net.minecraft.client.renderer.entity.RenderManager;
import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.entity.Entity;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.launchwrapper.IClassTransformer;
import net.minecraft.launchwrapper.Launch;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ObjectIntIdentityMap;
import net.minecraft.util.RegistryNamespaced;
import net.minecraft.util.RegistrySimple;
import net.minecraft.util.ResourceLocation;
import com.mumfrey.liteloader.client.util.PrivateFields;
import com.mumfrey.liteloader.core.LiteLoader;
import com.mumfrey.liteloader.core.runtime.Obf;
import com.mumfrey.liteloader.util.log.LiteLoaderLogger;
/**
* A small collection of useful functions for mods
*
* @author Adam Mummery-Smith
*/
public abstract class ModUtilities
{
/**
* True if FML is being used, in which case we use searge names instead of raw field/method names
*/
private static boolean fmlDetected = false;
private static boolean seargeNames = false;
static
{
// Check for FML
ModUtilities.fmlDetected = ModUtilities.fmlIsPresent();
try
{
Minecraft.class.getDeclaredField("running");
}
catch (SecurityException ex)
{
}
catch (NoSuchFieldException ex)
{
ModUtilities.seargeNames = true;
}
}
/**
* @return
*/
public static boolean fmlIsPresent()
{
// if (ClientBrandRetriever.getClientModName().contains("fml")) return true;
for (IClassTransformer transformer : Launch.classLoader.getTransformers())
if (transformer.getClass().getName().contains("fml")) return true;
return false;
}
/**
* Add a renderer map entry for the specified entity class
*
* @param entityClass
* @param renderer
*/
@SuppressWarnings("unchecked")
public static void addRenderer(Class<? extends Entity> entityClass, Render renderer)
{
RenderManager renderManager = Minecraft.getMinecraft().getRenderManager();
Map<Class<? extends Entity>, Render> entityRenderMap = PrivateFields.entityRenderMap.get(renderManager);
if (entityRenderMap != null)
{
entityRenderMap.put(entityClass, renderer);
}
else
{
LiteLoaderLogger.warning("Attempted to set renderer %s for entity class %s but the operation failed", renderer.getClass().getSimpleName(), entityClass.getSimpleName());
}
}
@SuppressWarnings("unchecked")
public static void addRenderer(Class<? extends TileEntity> tileEntityClass, TileEntitySpecialRenderer renderer)
{
TileEntityRendererDispatcher tileEntityRenderer = TileEntityRendererDispatcher.instance;
try
{
Map<Class<? extends TileEntity>, TileEntitySpecialRenderer> specialRendererMap = PrivateFields.specialRendererMap.get(tileEntityRenderer);
specialRendererMap.put(tileEntityClass, renderer);
renderer.setRendererDispatcher(tileEntityRenderer);
}
catch (Exception ex)
{
LiteLoaderLogger.warning("Attempted to set renderer %s for tile entity class %s but the operation failed", renderer.getClass().getSimpleName(), tileEntityClass.getSimpleName());
}
}
/**
* Add a block to the blocks registry
*
* @param blockId Block ID to insert
* @param blockName Block identifier
* @param block Block to register
* @param force Force insertion even if the operation is blocked by FMl
*/
public static void addBlock(int blockId, ResourceLocation blockName, Block block, boolean force)
{
Block existingBlock = (Block)Block.blockRegistry.getObject(blockName);
try
{
Block.blockRegistry.register(blockId, blockName, block);
}
catch (IllegalArgumentException ex)
{
if (!force) throw new IllegalArgumentException("Could not register block '" + blockName + "', the operation was blocked by FML.", ex);
ModUtilities.removeObjectFromRegistry(Block.blockRegistry, blockName);
Block.blockRegistry.register(blockId, blockName, block);
}
if (existingBlock != null)
{
try
{
for (Field field : Blocks.class.getDeclaredFields())
{
field.setAccessible(true);
if (field.isAccessible() && Block.class.isAssignableFrom(field.getType()))
{
Block fieldValue = (Block)field.get(null);
if (fieldValue == existingBlock)
{
ModUtilities.setFinalStaticField(field, block);
}
}
}
}
catch (Exception ex) { ex.printStackTrace(); }
}
}
/**
* Add an item to the items registry
*
* @param itemId Item ID to insert
* @param itemName Item identifier
* @param item Item to register
* @param force Force insertion even if the operation is blocked by FMl
*/
public static void addItem(int itemId, ResourceLocation itemName, Item item, boolean force)
{
Item existingItem = (Item)Item.itemRegistry.getObject(itemName);
try
{
Item.itemRegistry.register(itemId, itemName, item);
}
catch (IllegalArgumentException ex)
{
if (!force) throw new IllegalArgumentException("Could not register item '" + itemName + "', the operation was blocked by FML.", ex);
ModUtilities.removeObjectFromRegistry(Block.blockRegistry, itemName);
Item.itemRegistry.register(itemId, itemName, item);
}
if (existingItem != null)
{
try
{
for (Field field : Items.class.getDeclaredFields())
{
field.setAccessible(true);
if (field.isAccessible() && Item.class.isAssignableFrom(field.getType()))
{
Item fieldValue = (Item)field.get(null);
if (fieldValue == existingItem)
{
ModUtilities.setFinalStaticField(field, item);
}
}
}
}
catch (Exception ex) {}
}
}
@SuppressWarnings("unchecked")
public static void addTileEntity(String entityName, Class<? extends TileEntity> tileEntityClass)
{
try
{
Map<String, Class<? extends TileEntity>> nameToClassMap = PrivateFields.tileEntityNameToClassMap.get(null);
Map<Class<? extends TileEntity>, String> classToNameMap = PrivateFields.tileEntityClassToNameMap.get(null);
nameToClassMap.put(entityName, tileEntityClass);
classToNameMap.put(tileEntityClass, entityName);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
/**
* Abstraction helper function
*
* @param fieldName Name of field to get, returned unmodified if in debug mode
* @return Obfuscated field name if present
*/
public static String getObfuscatedFieldName(String fieldName, String obfuscatedFieldName, String seargeFieldName)
{
boolean deobfuscated = Tessellator.class.getSimpleName().equals("Tessellator");
return deobfuscated ? (ModUtilities.seargeNames ? seargeFieldName : fieldName) : (ModUtilities.fmlDetected ? seargeFieldName : obfuscatedFieldName);
}
/**
* Abstraction helper function
*
* @param fieldName Name of field to get, returned unmodified if in debug mode
* @return Obfuscated field name if present
*/
public static String getObfuscatedFieldName(Obf obf)
{
boolean deobfuscated = Tessellator.class.getSimpleName().equals("Tessellator");
return deobfuscated ? (ModUtilities.seargeNames ? obf.srg : obf.name) : (ModUtilities.fmlDetected ? obf.srg : obf.obf);
}
@SuppressWarnings("unchecked")
private static <K, V> V removeObjectFromRegistry(RegistrySimple registry, K key)
{
if (registry == null) return null;
ObjectIntIdentityMap underlyingIntegerMap = null;
if (registry instanceof RegistryNamespaced)
{
RegistryNamespaced rns = (RegistryNamespaced)registry;
underlyingIntegerMap = PrivateFields.underlyingIntegerMap.get(rns);
}
Map<K, V> registryObjects = PrivateFields.registryObjects.get(registry);
if (registryObjects != null)
{
V existingValue = registryObjects.get(key);
if (existingValue != null)
{
registryObjects.remove(key);
if (underlyingIntegerMap != null)
{
IdentityHashMap<V, Integer> identityMap = PrivateFields.identityMap.get(underlyingIntegerMap);
List<V> objectList = PrivateFields.objectList.get(underlyingIntegerMap);
if (identityMap != null) identityMap.remove(existingValue);
if (objectList != null) objectList.remove(existingValue);
}
return existingValue;
}
}
return null;
}
private static void setFinalStaticField(Field field, Object value) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException
{
Field modifiers = Field.class.getDeclaredField("modifiers");
modifiers.setAccessible(true);
modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, value);
}
}