Commit 885da345c63da9d1cd7a462b1b10dee4f652b423

Authored by Mumfrey
1 parent fba0e0ff

allow accessor injections specified in JSON to use obfuscation entries from the JSON file

java/common/com/mumfrey/liteloader/transformers/ObfProvider.java 0 → 100644
  1 +package com.mumfrey.liteloader.transformers;
  2 +
  3 +import com.mumfrey.liteloader.core.runtime.Obf;
  4 +
  5 +/**
  6 + * Interface for dynamic (context-specific) obfuscation provider, used internally by ModEventInjectionTransformer to
  7 + * provide obf entries for the AccessorTransformer from JSON
  8 + *
  9 + * @author Adam Mummery-Smith
  10 + */
  11 +public interface ObfProvider
  12 +{
  13 + /**
  14 + * Try to locate an obfuscation table entry by name (id), returns null if no entry was found
  15 + *
  16 + * @param name
  17 + * @return
  18 + */
  19 + public abstract Obf getByName(String name);
  20 +}
... ...
java/common/com/mumfrey/liteloader/transformers/access/AccessorTransformer.java
... ... @@ -22,6 +22,7 @@ import org.objectweb.asm.tree.VarInsnNode;
22 22 import com.mumfrey.liteloader.core.runtime.Obf;
23 23 import com.mumfrey.liteloader.transformers.ByteCodeUtilities;
24 24 import com.mumfrey.liteloader.transformers.ClassTransformer;
  25 +import com.mumfrey.liteloader.transformers.ObfProvider;
25 26 import com.mumfrey.liteloader.util.log.LiteLoaderLogger;
26 27  
27 28 /**
... ... @@ -49,18 +50,29 @@ public abstract class AccessorTransformer extends ClassTransformer
49 50 private final Class<? extends Obf> table;
50 51  
51 52 /**
  53 + * Obfuscation provider for this context
  54 + */
  55 + private final ObfProvider obfProvider;
  56 +
  57 + /**
52 58 * Target class to inject into
53 59 */
54 60 private final Obf target;
55 61  
56 62 protected AccessorInjection(String iface) throws IOException
57 63 {
  64 + this(iface, null);
  65 + }
  66 +
  67 + protected AccessorInjection(String iface, ObfProvider obfProvider) throws IOException
  68 + {
58 69 ClassNode ifaceNode = this.loadClass(iface);
59 70 this.table = this.setupTable(ifaceNode);
60 71 this.target = this.setupTarget(ifaceNode);
61 72 this.iface = iface;
  73 + this.obfProvider = obfProvider;
62 74 }
63   -
  75 +
64 76 private ClassNode loadClass(String iface) throws IOException
65 77 {
66 78 byte[] bytes = this.getClassBytes(iface);
... ... @@ -77,7 +89,22 @@ public abstract class AccessorTransformer extends ClassTransformer
77 89  
78 90 private Obf getObf(String name)
79 91 {
80   - return Obf.getByName(this.table, name);
  92 + if (this.obfProvider != null)
  93 + {
  94 + Obf obf = this.obfProvider.getByName(name);
  95 + if (obf != null)
  96 + {
  97 + return obf;
  98 + }
  99 + }
  100 +
  101 + Obf obf = Obf.getByName(this.table, name);
  102 + if (obf != null)
  103 + {
  104 + return obf;
  105 + }
  106 +
  107 + throw new RuntimeException("No obfuscation table entry could be found for '" + name + "'");
81 108 }
82 109  
83 110 protected Obf getTarget()
... ... @@ -318,7 +345,7 @@ public abstract class AccessorTransformer extends ClassTransformer
318 345 }
319 346 }
320 347  
321   - private List<AccessorInjection> accessors = new ArrayList<AccessorInjection>();
  348 + private final List<AccessorInjection> accessors = new ArrayList<AccessorInjection>();
322 349  
323 350 public AccessorTransformer()
324 351 {
... ... @@ -327,9 +354,14 @@ public abstract class AccessorTransformer extends ClassTransformer
327 354  
328 355 public void addAccessor(String interfaceName)
329 356 {
  357 + this.addAccessor(interfaceName, null);
  358 + }
  359 +
  360 + public void addAccessor(String interfaceName, ObfProvider obfProvider)
  361 + {
330 362 try
331 363 {
332   - this.accessors.add(new AccessorInjection(interfaceName));
  364 + this.accessors.add(new AccessorInjection(interfaceName, obfProvider));
333 365 }
334 366 catch (Exception ex)
335 367 {
... ...
java/common/com/mumfrey/liteloader/transformers/event/EventInjectionTransformer.java
1 1 package com.mumfrey.liteloader.transformers.event;
2 2  
  3 +import com.mumfrey.liteloader.transformers.ObfProvider;
  4 +
3 5 import net.minecraft.launchwrapper.IClassTransformer;
4 6  
5 7 public abstract class EventInjectionTransformer implements IClassTransformer
... ... @@ -85,4 +87,15 @@ public abstract class EventInjectionTransformer implements IClassTransformer
85 87 {
86 88 EventTransformer.addAccessor(interfaceName);
87 89 }
  90 +
  91 + /**
  92 + * Register an access injection interface and provide a contextual obfuscation provider
  93 + *
  94 + * @param interfaceName
  95 + * @param obfProvider
  96 + */
  97 + protected final void addAccessor(String interfaceName, ObfProvider obfProvider)
  98 + {
  99 + EventTransformer.addAccessor(interfaceName, obfProvider);
  100 + }
88 101 }
... ...
java/common/com/mumfrey/liteloader/transformers/event/EventTransformer.java
... ... @@ -19,6 +19,7 @@ import org.objectweb.asm.util.CheckClassAdapter;
19 19  
20 20 import com.mumfrey.liteloader.transformers.ByteCodeUtilities;
21 21 import com.mumfrey.liteloader.transformers.ClassTransformer;
  22 +import com.mumfrey.liteloader.transformers.ObfProvider;
22 23 import com.mumfrey.liteloader.transformers.access.AccessorTransformer;
23 24 import com.mumfrey.liteloader.util.log.LiteLoaderLogger;
24 25  
... ... @@ -191,6 +192,11 @@ public final class EventTransformer extends ClassTransformer
191 192  
192 193 static void addAccessor(String interfaceName)
193 194 {
  195 + EventTransformer.addAccessor(interfaceName, null);
  196 + }
  197 +
  198 + static void addAccessor(String interfaceName, ObfProvider obfProvider)
  199 + {
194 200 if (EventTransformer.accessorTransformer == null)
195 201 {
196 202 EventTransformer.accessorTransformer = new AccessorTransformer()
... ... @@ -200,7 +206,7 @@ public final class EventTransformer extends ClassTransformer
200 206 };
201 207 }
202 208  
203   - EventTransformer.accessorTransformer.addAccessor(interfaceName);
  209 + EventTransformer.accessorTransformer.addAccessor(interfaceName, obfProvider);
204 210 }
205 211  
206 212 @Override
... ...
java/common/com/mumfrey/liteloader/transformers/event/json/JsonEvents.java
... ... @@ -10,6 +10,7 @@ import com.google.gson.Gson;
10 10 import com.google.gson.GsonBuilder;
11 11 import com.google.gson.annotations.SerializedName;
12 12 import com.mumfrey.liteloader.core.runtime.Obf;
  13 +import com.mumfrey.liteloader.transformers.ObfProvider;
13 14  
14 15 /**
15 16 * Serialisable class which represents a set of event injection definitions. Instances of this class are
... ... @@ -21,7 +22,7 @@ import com.mumfrey.liteloader.core.runtime.Obf;
21 22 *
22 23 * @author Adam Mummery-Smith
23 24 */
24   -public class JsonEvents implements Serializable
  25 +public class JsonEvents implements Serializable, ObfProvider
25 26 {
26 27 private static final long serialVersionUID = 1L;
27 28  
... ... @@ -71,9 +72,9 @@ public class JsonEvents implements Serializable
71 72 */
72 73 private void parse()
73 74 {
74   - if (this.events == null || this.events.isEmpty())
  75 + if (this.obfuscation == null)
75 76 {
76   - throw new InvalidEventJsonException("No events were defined in the supplied JSON");
  77 + this.obfuscation = new JsonObfuscationTable();
77 78 }
78 79  
79 80 try
... ... @@ -84,10 +85,13 @@ public class JsonEvents implements Serializable
84 85 // Parse the descriptor list
85 86 this.methods = new JsonMethods(this.obfuscation, this.descriptors);
86 87  
87   - // Parse the events
88   - for (JsonEvent event : this.events)
  88 + if (this.events != null)
89 89 {
90   - event.parse(this.methods);
  90 + // Parse the events
  91 + for (JsonEvent event : this.events)
  92 + {
  93 + event.parse(this.methods);
  94 + }
91 95 }
92 96  
93 97 if (this.accessors != null)
... ... @@ -144,9 +148,18 @@ public class JsonEvents implements Serializable
144 148  
145 149 for (String interfaceName : this.accessorInterfaces)
146 150 {
147   - transformer.registerAccessor(interfaceName);
  151 + transformer.registerAccessor(interfaceName, this);
148 152 }
149 153 }
  154 +
  155 + /* (non-Javadoc)
  156 + * @see com.mumfrey.liteloader.transformers.ObfProvider#getByName(java.lang.String)
  157 + */
  158 + @Override
  159 + public Obf getByName(String name)
  160 + {
  161 + return this.obfuscation.getByName(name);
  162 + }
150 163  
151 164 // public String toJson()
152 165 // {
... ...
java/common/com/mumfrey/liteloader/transformers/event/json/JsonObfuscationTable.java
... ... @@ -88,13 +88,41 @@ public class JsonObfuscationTable implements Serializable
88 88  
89 89 return this.parseClass(token);
90 90 }
  91 +
  92 + /**
  93 + * Find an obf entry of any type by name
  94 + *
  95 + * @param name
  96 + */
  97 + public Obf getByName(String name)
  98 + {
  99 + Obf classObf = this.classObfs.get(name);
  100 + if (classObf != null)
  101 + {
  102 + return classObf;
  103 + }
  104 +
  105 + Obf methodObf = this.methodObfs.get(name);
  106 + if (methodObf != null)
  107 + {
  108 + return methodObf;
  109 + }
  110 +
  111 + Obf fieldObf = this.fieldObfs.get(name);
  112 + if (fieldObf != null)
  113 + {
  114 + return fieldObf;
  115 + }
  116 +
  117 + return null;
  118 + }
91 119  
92 120 /**
93 121 * @param token
94 122 */
95 123 public Obf parseClass(String token)
96 124 {
97   - return this.parseObf(token, this.classObfs);
  125 + return this.parseObf(token, this.classObfs, false);
98 126 }
99 127  
100 128 /**
... ... @@ -102,7 +130,7 @@ public class JsonObfuscationTable implements Serializable
102 130 */
103 131 public Obf parseMethod(String token)
104 132 {
105   - return this.parseObf(token, this.methodObfs);
  133 + return this.parseObf(token, this.methodObfs, false);
106 134 }
107 135  
108 136 /**
... ... @@ -110,14 +138,15 @@ public class JsonObfuscationTable implements Serializable
110 138 */
111 139 public Obf parseField(String token)
112 140 {
113   - return this.parseObf(token, this.fieldObfs);
  141 + return this.parseObf(token, this.fieldObfs, false);
114 142 }
115 143  
116 144 /**
117 145 * @param token
118 146 * @param obfs
  147 + * @param returnNullOnFailure return null instead of throwing an exception
119 148 */
120   - private Obf parseObf(String token, Map<String, Obf> obfs)
  149 + private Obf parseObf(String token, Map<String, Obf> obfs, boolean returnNullOnFailure)
121 150 {
122 151 String key = JsonEvents.parseToken(token);
123 152  
... ... @@ -140,6 +169,11 @@ public class JsonObfuscationTable implements Serializable
140 169 return packet;
141 170 }
142 171  
  172 + if (returnNullOnFailure)
  173 + {
  174 + return null;
  175 + }
  176 +
143 177 throw new InvalidEventJsonException("The token " + token + " could not be resolved to a type");
144 178 }
145 179  
... ...
java/common/com/mumfrey/liteloader/transformers/event/json/ModEventInjectionTransformer.java
... ... @@ -3,6 +3,7 @@ package com.mumfrey.liteloader.transformers.event.json;
3 3 import java.util.Map.Entry;
4 4  
5 5 import com.mumfrey.liteloader.transformers.ClassTransformer;
  6 +import com.mumfrey.liteloader.transformers.ObfProvider;
6 7 import com.mumfrey.liteloader.transformers.event.Event;
7 8 import com.mumfrey.liteloader.transformers.event.EventInjectionTransformer;
8 9 import com.mumfrey.liteloader.transformers.event.InjectionPoint;
... ... @@ -75,8 +76,8 @@ public class ModEventInjectionTransformer extends EventInjectionTransformer
75 76 return super.addEvent(event, targetMethod, injectionPoint);
76 77 }
77 78  
78   - protected void registerAccessor(String interfaceName)
  79 + protected void registerAccessor(String interfaceName, ObfProvider obfProvider)
79 80 {
80   - super.addAccessor(interfaceName);
  81 + super.addAccessor(interfaceName, obfProvider);
81 82 }
82 83 }
... ...