-
Notifications
You must be signed in to change notification settings - Fork 19
/
ClassTinkerers.java
276 lines (246 loc) · 11.9 KB
/
ClassTinkerers.java
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
/*
* Copyright 2019 Chocohead
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package com.chocohead.mm.api;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Predicate;
import org.apache.commons.lang3.ArrayUtils;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.ClassNode;
/**
* A collection of helper methods to allow adding and changing the definition of classes.
*
* <p>Class transformations via {@link #addTransformation(String, Consumer)} and additional enum entries via
* {@link #enumBuilder(String, Class...)} should be done via an Early Riser.
* Additional class definitions via {@link #define(String, byte[])} can be done at any time.
*
* <p><b>Non-static methods are to be treated as non-API and thus should be left alone.</b>
*
* @author Chocohead
*/
public enum ClassTinkerers {
INSTANCE;
private Predicate<URL> urlers = url -> false;
private Map<String, byte[]> clazzes = new HashMap<>();
private Map<String, Consumer<ClassNode>> replacers = new HashMap<>();
private Map<String, Set<Consumer<ClassNode>>> tinkerers = new HashMap<>();
private Set<EnumAdder> enumExtensions = new HashSet<>();
public void hookUp(Consumer<URL> liveURL, Map<String, byte[]> liveClassMap, Map<String, Consumer<ClassNode>> liveReplacers, Map<String, Set<Consumer<ClassNode>>> liveTinkerers, Set<EnumAdder> liveEnums) {
urlers = url -> {
liveURL.accept(url);
return true;
};
liveClassMap.putAll(clazzes);
clazzes = liveClassMap;
liveReplacers.putAll(replacers);
replacers = liveReplacers;
liveTinkerers.putAll(tinkerers);
tinkerers = liveTinkerers;
liveEnums.addAll(enumExtensions);
enumExtensions = liveEnums;
}
/**
* Adds the given {@link URL} to the mod {@link URLClassLoader}'s list used to search for mod classes and resources
*
* <p>A {@code false} return value means this has been invoked too early in the loading process and the addition failed
* <p>If the given {@link URL} is {@code null} or is already in the list, this will have no effect
* but will still return {@link true} if it would have otherwise succeeded.
*
* @param url The URL to be added to the search path of URLs
* @return Whether the URL has been given to the classloader
*
* @since 1.6
*/
public static boolean addURL(URL url) {
return INSTANCE.urlers.test(url);
}
/**
* Define a class with the given {@link name} by the given {@link contents} if it doesn't already exist
* <p><b>Behaviour is undefined if the target class name is already class loaded</b>
*
* @param name The name of the class to define
* @param contents The bytecode for the class
* @return Whether the definition was successful (ie another definition with the same name is not already present)
*
* @throws NullPointerException If name is {@code null}
* @throws IllegalArgumentException If contents is {@code null}
*/
public static boolean define(String name, byte[] contents) {
name = '/' + name.replace('.', '/') + ".class";
if (INSTANCE.clazzes.containsKey(name)) return false;
if (contents == null) throw new IllegalArgumentException("Tried to define null class named " + name);
INSTANCE.clazzes.put(name, contents);
return true;
}
/**
* Add a class replacer for the given class {@link target} to allow totally replacing bytecode during definition.
* <p><b>Does nothing if the target class is already defined</b>
*
* <p>This method is designed when the bulk or entirety of the target class is going to be replaced by the
* {@code replacer}. For more modest changes {@link #addTransformation(String, Consumer)} is strongly recommended.
*
* <p>No Mixins or {@link #addTransformation(String, Consumer) transformations} will have applied when the
* {@code replacer} is given the {@link ClassNode}. Subsequently only one replacement for a given class can be registered,
* attempting to register more will result in an {@link IllegalStateException}.
*
* @param target The name of the class to be replaced
* @param transformer A {@link Consumer} to take the target class's unmodified {@link ClassNode} replace the contents
*
* @throws NullPointerException If target is {@code null}
* @throws IllegalArgumentException If replacer is {@code null}
* @throws IllegalStateException If replacement for the target has already been registered
*
* @since 1.9
*/
public static void addReplacement(String target, Consumer<ClassNode> replacer) {
if (replacer == null) throw new IllegalArgumentException("Tried to set null replacer for " + target);
String name = target.replace('.', '/');
Consumer<ClassNode> existing = INSTANCE.replacers.get(name);
if (existing != null) {
throw new IllegalStateException("Multiple attempts to replace " + name + ": " + existing + " and " + replacer);
}
INSTANCE.replacers.put(name, replacer);
}
/**
* Add a class transformer for the given class {@link target} to allow modifying the bytecode during definition.
* <p><b>Does nothing if the target class is already defined</b>
*
* <p>This method is designed when certain elements of the target class are changed by the {@code transformer}.
* For more drastic changes {@link #addReplacement(String, Consumer)} might prove beneficial.
*
* <p>Any {@link #addReplacement(String, Consumer) replacement} will have applied before the {@code transformer}
* is given the {@link ClassNode}. Any number of other Mixins or transformations could have applied also so care
* should be taken that the target of the transformation is as expected.
*
* @param target The name of the class to be transformed
* @param transformer A {@link Consumer} to take the target class's {@link ClassNode} to be tinkered with
*
* @throws NullPointerException If target is {@code null}
* @throws IllegalArgumentException If transformer is {@code null}
*/
public static void addTransformation(String target, Consumer<ClassNode> transformer) {
if (transformer == null) throw new IllegalArgumentException("Tried to add null transformer for " + target);
INSTANCE.tinkerers.computeIfAbsent(target.replace('.', '/'), k -> new HashSet<>()).add(transformer);
}
/**
* Create a new {@link EnumAdder} in order to add additional Enum entries to the given type name.
* <p>Nothing will be done if the given Enum type has already been loaded.</p>
*
* @param type The name of the enum to be extended
* @return A builder for which additional entries can be defined
*
* @throws NullPointerException If type is {@code null}
*
* @since 2.2
*/
public static EnumAdder enumBuilder(String type) {
if (type == null) throw new NullPointerException("Tried to add onto a null type!");
return new EnumAdder(type.replace('.', '/'), ArrayUtils.EMPTY_CLASS_ARRAY);
}
/**
* Create a new {@link EnumAdder} in order to add additional Enum entries to the given type name.
* <p>Nothing will be done if the given Enum type has already been loaded.</p>
* <p><b>Will crash if any of the parameter types are from Minecraft to avoid early class loading</b>
*
* @param type The name of the enum to be extended
* @param parameterTypes The type of parameters the constructor to be used takes
* @return A builder for which additional entries can be defined
*
* @throws NullPointerException If type is null
* @throws IllegalArgumentException If parameterTypes is or contains null, or is a Minecraft class
*/
public static EnumAdder enumBuilder(String type, Class<?>... parameterTypes) {
if (type == null) throw new NullPointerException("Tried to add onto a null type!");
if (parameterTypes == null || ArrayUtils.contains(parameterTypes, null))
throw new IllegalArgumentException("Invalid parameter array: " + Arrays.toString(parameterTypes));
for (Class<?> param : parameterTypes) {
if (param.getName().startsWith("net.minecraft.")) {
throw new IllegalArgumentException("Early loaded " + param.getName());
}
}
return new EnumAdder(type.replace('.', '/'), parameterTypes);
}
/**
* Create a new {@link EnumAdder} in order to add additional Enum entries to the given type name.
* <p>Nothing will be done if the given Enum type has already been loaded.</p>
*
* @param type The name of the enum to be extended
* @param parameterTypes The <b>internal names</b> of the parameter types the constructor to be used takes
* @return A builder for which additional entries can be defined
*
* @throws NullPointerException If type is {@code null}
* @throws IllegalArgumentException If parameterTypes is or contains {@code null}, or is invalidly specified
*/
public static EnumAdder enumBuilder(String type, String... parameterTypes) {
if (type == null) throw new NullPointerException("Tried to add onto a null type!");
if (parameterTypes == null || ArrayUtils.contains(parameterTypes, null))
throw new IllegalArgumentException("Invalid parameter array: " + Arrays.toString(parameterTypes));
return new EnumAdder(type.replace('.', '/'), Arrays.stream(parameterTypes).map(param -> param.replace('.', '/')).toArray(String[]::new));
}
/**
* Create a new {@link EnumAdder} in order to add additional Enum entries to the given type name
*
* <p>The given parameter types can be any mix of
* <ul>
* <li>{@link Class} - <b>Will crash if a Minecraft class to avoid early class loading</b>
* <li>{@link String} - Given as <b>internal names</b> (ie <code>Lmy/package/class;</code> or <code>I</code>)
* <li>{@link Type} - Any {@link Type#getSort() sorts} aside from {@link Type#METHOD} or {@link Type#VOID}
* </ul>
* So that it matches the constructor that is wanted to be used.</p>
*
* <p>Nothing will be done if the given Enum type has already been loaded.</p>
*
* @param type The name of the enum to be extended
* @param parameterTypes The type or internal names of the parameter types the constructor to be used takes
* @return A builder for which additional entries can be defined
*
* @throws NullPointerException If type is {@code null}
* @throws IllegalArgumentException If parameterTypes is or contains {@code null}, or is invalidly specified
*/
public static EnumAdder enumBuilder(String type, Object... parameterTypes) {
if (type == null) throw new NullPointerException("Tried to add onto a null type!");
if (parameterTypes == null || ArrayUtils.contains(parameterTypes, null))
throw new IllegalArgumentException("Invalid parameter array: " + Arrays.toString(parameterTypes));
return new EnumAdder(type.replace('.', '/'), parameterTypes);
}
/**
* Register the given {@link EnumAdder} as finished and ready to be used
*
* @param builder The finished EnumAdder to store
*/
static void addEnum(EnumAdder builder) {
if (ArrayUtils.contains(builder.parameterTypes, null)) //Individual array entries could be swapped out naughtily, guard against it
throw new IllegalArgumentException("Builder for " + builder.type + " has an invalid parameter array: " + Arrays.toString(builder.parameterTypes));
//Only bother adding it if changes are actually made
if (!builder.getAdditions().isEmpty()) INSTANCE.enumExtensions.add(builder);
}
/**
* Gets the Enum entry with the given name from the given enum type
*
* @param type The type of Enum for which to search in
* @param name The name of the entry to return
* @return The entry within type that has {@link Enum#name()} equal to name
*
* @throws NullPointerException If type is {@code null}
* @throws IllegalArgumentException If no entry with the given name can be found in type
*/
public static <E extends Enum<E>> E getEnum(Class<E> type, String name) {
for (E constant : type.getEnumConstants()) {
if (constant.name().equals(name)) {
return constant;
}
}
throw new IllegalArgumentException("Unable to find " + name + " in " + type);
}
}