-
-
Notifications
You must be signed in to change notification settings - Fork 184
/
MixinTransformer.java
291 lines (252 loc) · 10.2 KB
/
MixinTransformer.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* This file is part of Mixin, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.asm.mixin.transformer;
import java.lang.reflect.Constructor;
import java.util.List;
import org.objectweb.asm.tree.ClassNode;
import org.spongepowered.asm.launch.MixinInitialisationError;
import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.mixin.MixinEnvironment.Option;
import org.spongepowered.asm.mixin.throwables.MixinException;
import org.spongepowered.asm.mixin.transformer.ext.Extensions;
import org.spongepowered.asm.mixin.transformer.ext.IExtensionRegistry;
import org.spongepowered.asm.mixin.transformer.ext.IHotSwap;
import org.spongepowered.asm.transformers.TreeTransformer;
import org.spongepowered.asm.util.Constants;
import org.spongepowered.asm.util.asm.ASM;
/**
* Transformer which manages the mixin configuration and application process
*/
final class MixinTransformer extends TreeTransformer implements IMixinTransformer {
/**
* Impl of mixin transformer factory
*/
static class Factory implements IMixinTransformerFactory {
/* (non-Javadoc)
* @see org.spongepowered.asm.mixin.transformer.IMixinTransformerFactory
* #createTransformer()
*/
@Override
public IMixinTransformer createTransformer() throws MixinInitialisationError {
return new MixinTransformer();
}
}
private static final String MIXIN_AGENT_CLASS = "org.spongepowered.tools.agent.MixinAgent";
/**
* Synthetic class registry
*/
private final SyntheticClassRegistry syntheticClassRegistry;
/**
* Transformer extensions
*/
private final Extensions extensions;
/**
* Hotswap agent, if available
*/
private final IHotSwap hotSwapper;
/**
* Coprocessor which handles merging nest members into nest hosts which may
* or may not be mixin targets themselves
*/
private final MixinCoprocessorNestHost nestHostCoprocessor;
/**
* Mixin processor which actually manages application of mixins
*/
private final MixinProcessor processor;
/**
* Class generator
*/
private final MixinClassGenerator generator;
MixinTransformer() {
MixinEnvironment environment = MixinEnvironment.getCurrentEnvironment();
Object globalMixinTransformer = environment.getActiveTransformer();
if (globalMixinTransformer instanceof IMixinTransformer) {
throw new MixinException("Terminating MixinTransformer instance " + this);
}
// I am a leaf on the wind
environment.setActiveTransformer(this);
this.syntheticClassRegistry = new SyntheticClassRegistry();
this.extensions = new Extensions(this.syntheticClassRegistry);
this.hotSwapper = this.initHotSwapper(environment);
this.nestHostCoprocessor = new MixinCoprocessorNestHost();
this.processor = new MixinProcessor(environment, this.extensions, this.hotSwapper, this.nestHostCoprocessor);
this.generator = new MixinClassGenerator(environment, this.extensions);
DefaultExtensions.create(environment, this.extensions, this.syntheticClassRegistry, this.nestHostCoprocessor);
}
private IHotSwap initHotSwapper(MixinEnvironment environment) {
if (!environment.getOption(Option.HOT_SWAP)) {
return null;
}
try {
MixinProcessor.logger.info("Attempting to load Hot-Swap agent");
@SuppressWarnings("unchecked")
Class<? extends IHotSwap> clazz =
(Class<? extends IHotSwap>)Class.forName(MixinTransformer.MIXIN_AGENT_CLASS);
Constructor<? extends IHotSwap> ctor = clazz.getDeclaredConstructor(IMixinTransformer.class);
return ctor.newInstance(this);
} catch (Throwable th) {
MixinProcessor.logger.info("Hot-swap agent could not be loaded, hot swapping of mixins won't work. {}: {}",
th.getClass().getSimpleName(), th.getMessage());
}
return null;
}
/* (non-Javadoc)
* @see org.spongepowered.asm.mixin.transformer.IMixinTransformer
* #getExtensions()
*/
@Override
public IExtensionRegistry getExtensions() {
return this.extensions;
}
/* (non-Javadoc)
* @see org.spongepowered.asm.service.ITransformer#getName()
*/
@Override
public String getName() {
return this.getClass().getName();
}
/* (non-Javadoc)
* @see org.spongepowered.asm.service.ITransformer#isDelegationExcluded()
*/
@Override
public boolean isDelegationExcluded() {
return true;
}
/**
* Run audit process on current mixin processor
*
* @param environment Environment for audit
*/
@Override
public void audit(MixinEnvironment environment) {
this.processor.audit(environment);
}
/**
* Callback from hotswap agent
*
* @param mixinClass Name of the mixin
* @param classNode New class
* @return List of classes that need to be updated
*/
@Override
public List<String> reload(String mixinClass, ClassNode classNode) {
return this.processor.reload(mixinClass, classNode);
}
/* (non-Javadoc)
* @see org.spongepowered.asm.mixin.transformer.IMixinTransformer
* #transformClassBytes(java.lang.String, java.lang.String, byte[])
*/
@Override
public byte[] transformClassBytes(String name, String transformedName, byte[] basicClass) {
if (transformedName == null) {
return basicClass;
}
MixinEnvironment environment = MixinEnvironment.getCurrentEnvironment();
if (basicClass == null) {
return this.generateClass(environment, transformedName);
}
return this.transformClass(environment, transformedName, basicClass);
}
/**
* Called when the transformation reason is computing_frames. The only
* operation we care about here is adding interfaces to target classes but
* at the moment we don't have sufficient scaffolding to determine that
* without triggering re-entrance. Currently just a no-op in order to not
* cause a re-entrance crash under ModLauncher 7.0+.
*
* @param environment Current environment
* @param name Class transformed name
* @param classNode Class tree
* @return true if the class was transformed
*/
@Override
public boolean computeFramesForClass(MixinEnvironment environment, String name, ClassNode classNode) {
// TODO compute added interfaces
return false;
}
/**
* Apply mixins and postprocessors to the supplied class
*
* @param environment Current environment
* @param name Class transformed name
* @param classBytes Class bytecode
* @return Transformed bytecode
*/
@Override
public byte[] transformClass(MixinEnvironment environment, String name, byte[] classBytes) {
ClassNode classNode = this.readClass(name, classBytes);
if (this.processor.applyMixins(environment, name, classNode)) {
return this.writeClass(classNode);
}
return classBytes;
}
/**
* Apply mixins and postprocessors to the supplied class
*
* @param environment Current environment
* @param name Class transformed name
* @param classNode Class tree
* @return true if the class was transformed
*/
@Override
public boolean transformClass(MixinEnvironment environment, String name, ClassNode classNode) {
return this.processor.applyMixins(environment, name, classNode);
}
/**
* Generate the specified mixin-synthetic class
*
* @param environment Current environment
* @param name Class name to generate
* @return Generated bytecode or <tt>null</tt> if no class was generated
*/
@Override
public byte[] generateClass(MixinEnvironment environment, String name) {
ClassNode classNode = MixinTransformer.createEmptyClass(name);
if (this.generator.generateClass(environment, name, classNode)) {
return this.writeClass(classNode);
}
return null;
}
/**
* @param environment Current environment
* @param name Class transformed name
* @param classNode Empty classnode to populate
* @return True if the class was generated successfully
*/
@Override
public boolean generateClass(MixinEnvironment environment, String name, ClassNode classNode) {
return this.generator.generateClass(environment, name, classNode);
}
/**
* You need to ask yourself why you're reading this comment
*/
private static ClassNode createEmptyClass(String name) {
ClassNode classNode = new ClassNode(ASM.API_VERSION);
classNode.name = name.replace('.', '/');
classNode.version = MixinEnvironment.getCompatibilityLevel().getClassVersion();
classNode.superName = Constants.OBJECT;
return classNode;
}
}