PrivateFields.java
4.32 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
package com.mumfrey.liteloader.util;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Map;
import net.minecraft.client.Minecraft;
import net.minecraft.src.*;
/**
* Wrapper for obf/mcp reflection-accessed private fields, mainly added to centralise the locations I have to update the obfuscated field names
*
* @author Adam Mummery-Smith
*
* @param <P> Parent class type, the type of the class that owns the field
* @param <T> Field type, the type of the field value
*/
@SuppressWarnings("rawtypes")
public class PrivateFields<P, T>
{
/**
* Class to which this field belongs
*/
public final Class<P> parentClass;
/**
* MCP name for this field
*/
public final String mcpName;
/**
* Real (obfuscated) name for this field
*/
public final String name;
public final String seargeName;
/**
* Name used to access the field, determined at init
*/
private final String fieldName;
/**
* Creates a new private field entry
*
* @param owner
* @param mcpName
* @param name
*/
private PrivateFields(Class<P> owner, String mcpName, String name, String seargeName)
{
this.parentClass = owner;
this.mcpName = mcpName;
this.name = name;
this.seargeName = seargeName;
this.fieldName = ModUtilities.getObfuscatedFieldName(mcpName, name, seargeName);
}
/**
* Get the current value of this field on the instance class supplied
*
* @param instance Class to get the value of
* @return field value or null if errors occur
*/
@SuppressWarnings("unchecked")
public T get(P instance)
{
try
{
Field field = parentClass.getDeclaredField(fieldName);
field.setAccessible(true);
return (T)field.get(instance);
}
catch (Exception ex)
{
return null;
}
}
/**
* Set the value of this field on the instance class supplied
*
* @param instance Object to set the value of the field on
* @param value value to set
* @return value
*/
public T set(P instance, T value)
{
try
{
Field field = parentClass.getDeclaredField(fieldName);
field.setAccessible(true);
field.set(instance, value);
}
catch (Exception ex) {}
return value;
}
/**
* Set the value of this FINAL field on the instance class supplied
*
* @param instance Object to set the value of the field on
* @param value value to set
* @return value
*/
public T setFinal(P instance, T value)
{
try
{
Field modifiers = Field.class.getDeclaredField("modifiers");
modifiers.setAccessible(true);
Field field = parentClass.getDeclaredField(fieldName);
modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.setAccessible(true);
field.set(instance, value);
}
catch (Exception ex) {}
return value;
}
/**
* Static private fields
*
* @param <P> Parent class type, the type of the class that owns the field
* @param <T> Field type, the type of the field value
*/
public static final class StaticFields<P, T> extends PrivateFields<P, T>
{
public StaticFields(Class<P> owner, String mcpName, String name, String fmlName) { super(owner, mcpName, name, fmlName); }
public T get() { return get(null); }
public void set(T value) { set(null, value); }
public static final StaticFields<Packet, Map> packetClassToIdMap = new StaticFields<Packet, Map> (Packet.class, "packetClassToIdMap", "a", "field_73291_a"); // Packet/packetClassToIdMap
public static final StaticFields<TileEntity, Map> tileEntityNameToClassMap = new StaticFields<TileEntity, Map> (TileEntity.class, "nameToClassMap", "a", "field_70326_a"); // TileEntity/nameToClassMap
}
public static final PrivateFields<Minecraft, Timer> minecraftTimer = new PrivateFields<Minecraft, Timer> (Minecraft.class, "timer", "V", "field_71428_T"); // Minecraft/timer
public static final PrivateFields<Minecraft, Profiler> minecraftProfiler = new PrivateFields<Minecraft, Profiler> (Minecraft.class, "mcProfiler", "J", "field_71424_I"); // Minecraft/mcProfiler
public static final PrivateFields<RenderManager, Map> entityRenderMap = new PrivateFields<RenderManager, Map> (RenderManager.class, "entityRenderMap", "q", "field_78729_o"); // RenderManager/entityRenderMap
}