Implement remapper, fixes notch->srg at runtime (eg FML), bump to 0.4.10

This commit is contained in:
Mumfrey committed 2015-12-18 22:56:03 +00:00
1 parent 0f12304abe
commit c04eb1bdd7
21 files changed
+571 -55

No files matched your search

@@ -44,6 +44,13 @@ interface IMixinLaunchAgent {
*/
public abstract void prepare();
/**
* Called from <tt>injectIntoClassLoader</tt> in the parent tweaker but <b>
* only called on the primary tweak container</b>. This is useful if the
* agent needs to perform some environment-specific setup just once.
*/
public abstract void initPrimaryContainer();
/**
* Called from <tt>injectIntoClassLoader</tt> in the parent tweaker
*
@@ -60,7 +60,7 @@ public abstract class MixinBootstrap {
/**
* Subsystem version
*/
public static final String VERSION = "0.4.9";
public static final String VERSION = "0.4.10";
/**
* Blackboard key where the subsystem version will be stored to indicate
@@ -24,6 +24,9 @@
*/
package org.spongepowered.asm.launch;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.File;
import java.net.URI;
@@ -33,6 +36,11 @@ import java.net.URI;
*/
public abstract class MixinLaunchAgentAbstract implements IMixinLaunchAgent {
/**
* Logger
*/
protected final Logger logger = LogManager.getLogger("mixin");
/**
* URI to the container
*/
@@ -64,6 +64,10 @@ public class MixinLaunchAgentDefault extends MixinLaunchAgentAbstract {
}
}
}
@Override
public void initPrimaryContainer() {
}
@Override
public void injectIntoClassLoader(LaunchClassLoader classLoader) {
@@ -33,7 +33,8 @@ import java.util.List;
import net.minecraft.launchwrapper.ITweaker;
import net.minecraft.launchwrapper.Launch;
import net.minecraft.launchwrapper.LaunchClassLoader;
import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.mixin.extensibility.IRemapper;
/**
* Launch agent for use under FML.
@@ -160,6 +161,28 @@ public class MixinLaunchAgentFML extends MixinLaunchAgentAbstract {
@Override
public void prepare() {
}
/* (non-Javadoc)
* @see org.spongepowered.asm.launch.IMixinLaunchAgent
* #initPrimaryContainer()
*/
@Override
public void initPrimaryContainer() {
if (this.clCoreModManager != null) {
this.injectRemapper();
}
}
private void injectRemapper() {
try {
Class<?> clFmlRemapperAdapter = Class.forName("org.spongepowered.asm.bridge.RemapperAdapterFML", true, Launch.classLoader);
Method mdCreate = clFmlRemapperAdapter.getDeclaredMethod("create");
IRemapper remapper = (IRemapper)mdCreate.invoke(null);
MixinEnvironment.getDefaultEnvironment().getRemappers().add(remapper);
} catch (Exception ex) {
this.logger.debug("Failed instancing remapper adapter for FML, things will probably go horribly for notch-obf'd mods!");
}
}
/* (non-Javadoc)
* @see org.spongepowered.asm.launch.IMixinLaunchAgent
@@ -86,6 +86,13 @@ public class MixinTweakContainer {
agent.prepare();
}
}
public void initPrimaryContainer() {
for (IMixinLaunchAgent agent : this.agents) {
this.logger.debug("Processing launch tasks for {}", agent);
agent.initPrimaryContainer();
}
}
/**
* @param classLoader
@@ -63,6 +63,11 @@ public class MixinTweaker implements ITweaker {
*/
private final Map<URI, MixinTweakContainer> containers = new LinkedHashMap<URI, MixinTweakContainer>();
/**
* Container for this tweaker
*/
private MixinTweakContainer primaryContainer;
/**
* Tracks whether {@link #acceptOptions} was called yet, if true, causes new
* launch agents to be <tt>prepare</tt>d immediately
@@ -80,16 +85,17 @@ public class MixinTweaker implements ITweaker {
try {
uri = this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI();
if (uri != null) {
this.addContainer(uri);
this.primaryContainer = this.addContainer(uri);
}
} catch (URISyntaxException ex) {
ex.printStackTrace();
}
}
public final void addContainer(URI uri) {
if (this.containers.containsKey(uri)) {
return;
public final MixinTweakContainer addContainer(URI uri) {
MixinTweakContainer existingContainer = this.containers.get(uri);
if (existingContainer != null) {
return existingContainer;
}
MixinTweaker.logger.debug("Adding mixin launch agents for container {}", uri);
@@ -99,6 +105,7 @@ public class MixinTweaker implements ITweaker {
if (this.prepared) {
container.prepare();
}
return container;
}
/* (non-Javadoc)
@@ -136,6 +143,10 @@ public class MixinTweaker implements ITweaker {
*/
@Override
public final void injectIntoClassLoader(LaunchClassLoader classLoader) {
if (this.primaryContainer != null) {
this.primaryContainer.initPrimaryContainer();
}
this.scanClasspath();
MixinTweaker.logger.debug("injectIntoClassLoader running with {} agents", this.containers.size());
for (MixinTweakContainer container : this.containers.values()) {
@@ -605,6 +605,11 @@ public class MixinEnvironment implements ITokenProvider {
* Internal tokens defined by this environment
*/
private final Map<String, Integer> internalTokens = new HashMap<String, Integer>();
/**
* Remappers for this environment
*/
private final RemapperChain remappers = new RemapperChain();
/**
* Detected side
@@ -904,10 +909,20 @@ public class MixinEnvironment implements ITokenProvider {
this.obfuscationContext = context;
}
/**
* Get the current obfuscation context
*/
public String getObfuscationContext() {
return this.obfuscationContext;
}
/**
* Get the remapper chain for this environment
*/
public RemapperChain getRemappers() {
return this.remappers;
}
/**
* Invoke a mixin environment audit process
*/
@@ -0,0 +1,106 @@
/*
* 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;
import java.util.ArrayList;
import java.util.List;
import org.spongepowered.asm.mixin.extensibility.IRemapper;
public class RemapperChain implements IRemapper {
private final List<IRemapper> remappers = new ArrayList<IRemapper>();
public RemapperChain add(IRemapper remapper) {
this.remappers.add(remapper);
return this;
}
@Override
public String mapMethodName(String owner, String name, String desc) {
for (IRemapper remapper : this.remappers) {
String newName = remapper.mapMethodName(owner, name, desc);
if (newName != null && !newName.equals(name)) {
name = newName;
}
}
return name;
}
@Override
public String mapFieldName(String owner, String name, String desc) {
for (IRemapper remapper : this.remappers) {
String newName = remapper.mapFieldName(owner, name, desc);
if (newName != null && !newName.equals(name)) {
name = newName;
}
}
return name;
}
@Override
public String map(String typeName) {
for (IRemapper remapper : this.remappers) {
String newName = remapper.map(typeName);
if (newName != null && !newName.equals(typeName)) {
typeName = newName;
}
}
return typeName;
}
@Override
public String unmap(String typeName) {
for (IRemapper remapper : this.remappers) {
String newName = remapper.unmap(typeName);
if (newName != null && !newName.equals(typeName)) {
typeName = newName;
}
}
return typeName;
}
@Override
public String mapDesc(String desc) {
for (IRemapper remapper : this.remappers) {
String newDesc = remapper.mapDesc(desc);
if (newDesc != null && !newDesc.equals(desc)) {
desc = newDesc;
}
}
return desc;
}
@Override
public String unmapDesc(String desc) {
for (IRemapper remapper : this.remappers) {
String newDesc = remapper.unmapDesc(desc);
if (newDesc != null && !newDesc.equals(desc)) {
desc = newDesc;
}
}
return desc;
}
}
@@ -0,0 +1,84 @@
/*
* 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.extensibility;
/**
* Interface for remap chain participants
*/
public interface IRemapper {
/**
* Map method name to the new name. Subclasses can override.
*
* @param owner owner of the method.
* @param name name of the method.
* @param desc descriptor of the method.
* @return new name of the method
*/
public abstract String mapMethodName(String owner, String name, String desc);
/**
* Map field name to the new name. Subclasses can override.
*
* @param owner owner of the field.
* @param name name of the field
* @param desc descriptor of the field
* @return new name of the field.
*/
public abstract String mapFieldName(String owner, String name, String desc);
/**
* Map type name to the new name. Subclasses can override.
*
* @param typeName Class name to convert
* @return new name for the class
*/
public abstract String map(String typeName);
/**
* Convert a mapped type name back to the original obfuscated name
*
* @param typeName Class name to convert
* @return old name for the class
*/
public abstract String unmap(String typeName);
/**
* Convert a descriptor to remapped form
*
* @param desc descriptor to convert
* @return new descriptor
*/
public abstract String mapDesc(String desc);
/**
* Convert a descriptor back to the original obfuscated form
*
* @param desc descriptor to convert
* @return old descriptor
*/
public abstract String unmapDesc(String desc);
}
@@ -367,10 +367,10 @@ public class CallbackInjector extends Injector {
// signature for the current locals. This allows silent failover
// if changes to the local variable table are EXPECTED for some
// reason.
MethodNode handler = ASMHelper.findMethod(this.classNode, this.methodNode.name, callback.getDescriptor());
if (handler != null && ASMHelper.getVisibleAnnotation(handler, Surrogate.class) != null) {
MethodNode surrogateHandler = ASMHelper.findMethod(this.classNode, this.methodNode.name, callback.getDescriptor());
if (surrogateHandler != null && ASMHelper.getVisibleAnnotation(surrogateHandler, Surrogate.class) != null) {
// Found a matching method, use it
callbackMethod = handler;
callbackMethod = surrogateHandler;
} else {
// No matching method, generate a message to bitch about it
String message = this.generateBadLVTMessage(callback);
@@ -400,8 +400,14 @@ public class CallbackInjector extends Injector {
throw new InvalidInjectionException(this.info, "Invalid descriptor on callback: CallbackInfoReturnable is required!");
}
throw new InvalidInjectionException(this.info, "Invalid descriptor on callback: expected " + callback.getDescriptor()
+ " but found " + this.methodNode.desc);
MethodNode surrogateHandler = ASMHelper.findMethod(this.classNode, this.methodNode.name, callback.getDescriptor());
if (surrogateHandler != null && ASMHelper.getVisibleAnnotation(surrogateHandler, Surrogate.class) != null) {
// Found a matching surrogate method, use it
callbackMethod = surrogateHandler;
} else {
throw new InvalidInjectionException(this.info, "Invalid descriptor on callback: expected " + callback.getDescriptor()
+ " but found " + this.methodNode.desc);
}
}
}
@@ -393,9 +393,7 @@ public class MemberInfo {
/**
* Create a new version of this member with a different owner
*
* @param methodData Method data to use
* @param setOwner True to set the owner as well as the name
* @return New MethodInfo with remapped values
* @param newOwner New owner for this member
*/
public MemberInfo move(String newOwner) {
if (newOwner.equals(this.owner)) {
@@ -1256,8 +1256,8 @@ public class ClassInfo extends TreeInfo {
* of the existing class. Basically creates a cloned member with this
* ClassInfo as its parent.
*
* @param member
* @return
* @param member member to clone
* @return wrapper member
*/
@SuppressWarnings("unchecked")
private <M extends Member> M cloneMember(M member) {
@@ -1402,8 +1402,8 @@ public class ClassInfo extends TreeInfo {
* from the internal cache. Otherwise a new ClassInfo is created and
* returned.
*
* @param classNode
* @return
* @param classNode classNode to get info for
* @return ClassInfo instance for the supplied classNode
*/
static ClassInfo fromClassNode(ClassNode classNode) {
ClassInfo info = ClassInfo.cache.get(classNode.name);
@@ -518,7 +518,7 @@ public class MixinApplicator {
* Finds a suitable ctor for reading the instance initialiser bytecode
*
* @param mixin mixin to search
* @return
* @return appropriate ctor or null if none found
*/
private MethodNode getConstructor(MixinTargetContext mixin) {
MethodNode ctor = null;
@@ -38,7 +38,9 @@ import org.spongepowered.asm.lib.tree.FieldInsnNode;
import org.spongepowered.asm.lib.tree.FieldNode;
import org.spongepowered.asm.lib.tree.MethodInsnNode;
import org.spongepowered.asm.lib.tree.MethodNode;
import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.RemapperChain;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.transformer.ClassInfo.Field;
import org.spongepowered.asm.mixin.transformer.ClassInfo.Method;
@@ -182,9 +184,13 @@ class MixinPreProcessor {
if (!mustExist) {
return false;
}
throw new InvalidMixinException(this.mixin, annotationType.getSimpleName() + " method " + mixinMethod.name
+ " was not located in the target class");
target = MixinPreProcessor.findRemappedMethod(context.getTargetClass(), mixinMethod);
if (target == null) {
throw new InvalidMixinException(this.mixin, annotationType.getSimpleName() + " method " + mixinMethod.name
+ " was not located in the target class");
}
mixinMethod.name = target.name;
method.renameTo(target.name);
}
if (Constants.INIT.equals(target.name)) {
@@ -215,33 +221,39 @@ class MixinPreProcessor {
context.transformDescriptor(mixinField);
Field field = this.mixin.getClassInfo().findField(mixinField);
FieldNode target = this.findField(context.getTargetClass(), mixinField, shadow);
FieldNode target = MixinPreProcessor.findField(context.getTargetClass(), mixinField, shadow);
if (target == null) {
// If this field is a shadow field but is NOT found in the target class, that's bad, mmkay
if (shadow != null) {
if (shadow == null) {
continue;
}
target = MixinPreProcessor.findRemappedField(context.getTargetClass(), mixinField);
if (target == null) {
// If this field is a shadow field but is NOT found in the target class, that's bad, mmkay
throw new InvalidMixinException(this.mixin, "Shadow field " + mixinField.name + " was not located in the target class");
}
} else {
// Check that the shadow field has a matching descriptor
if (!target.desc.equals(mixinField.desc)) {
throw new InvalidMixinException(this.mixin, "The field " + mixinField.name + " in the target class has a conflicting signature");
mixinField.name = target.name;
field.renameTo(target.name);
}
// Check that the shadow field has a matching descriptor
if (!target.desc.equals(mixinField.desc)) {
throw new InvalidMixinException(this.mixin, "The field " + mixinField.name + " in the target class has a conflicting signature");
}
if (!target.name.equals(mixinField.name)) {
if ((target.access & Opcodes.ACC_PRIVATE) == 0 && (target.access & Opcodes.ACC_SYNTHETIC) == 0) {
throw new InvalidMixinException(this.mixin, "Non-private field cannot be aliased. Found " + target.name);
}
if (!target.name.equals(mixinField.name)) {
if ((target.access & Opcodes.ACC_PRIVATE) == 0 && (target.access & Opcodes.ACC_SYNTHETIC) == 0) {
throw new InvalidMixinException(this.mixin, "Non-private field cannot be aliased. Found " + target.name);
}
mixinField.name = target.name;
field.renameTo(target.name);
}
// Shadow fields get stripped from the mixin class
iter.remove();
if (shadow != null) {
context.addShadowField(mixinField);
}
mixinField.name = target.name;
field.renameTo(target.name);
}
// Shadow fields get stripped from the mixin class
iter.remove();
if (shadow != null) {
context.addShadowField(mixinField);
}
}
}
@@ -314,6 +326,19 @@ class MixinPreProcessor {
return MixinPreProcessor.findMethod(classNode, aliases, method.desc);
}
private static MethodNode findRemappedMethod(ClassNode classNode, MethodNode method) {
RemapperChain remapperChain = MixinEnvironment.getCurrentEnvironment().getRemappers();
String remappedName = remapperChain.mapMethodName(classNode.name, method.name, method.desc);
if (remappedName.equals(method.name)) {
return null;
}
Deque<String> aliases = new LinkedList<String>();
aliases.add(remappedName);
return MixinPreProcessor.findMethod(classNode, aliases, method.desc);
}
private static MethodNode findMethod(ClassNode classNode, Deque<String> aliases, String desc) {
String alias = aliases.poll();
if (alias == null) {
@@ -329,7 +354,7 @@ class MixinPreProcessor {
return MixinPreProcessor.findMethod(classNode, aliases, desc);
}
private FieldNode findField(ClassNode classNode, FieldNode field, AnnotationNode shadow) {
private static FieldNode findField(ClassNode classNode, FieldNode field, AnnotationNode shadow) {
Deque<String> aliases = new LinkedList<String>();
aliases.add(field.name);
if (shadow != null) {
@@ -339,9 +364,21 @@ class MixinPreProcessor {
}
}
return this.findField(classNode, aliases, field.desc);
return MixinPreProcessor.findField(classNode, aliases, field.desc);
}
private static FieldNode findRemappedField(ClassNode classNode, FieldNode field) {
RemapperChain remapperChain = MixinEnvironment.getCurrentEnvironment().getRemappers();
String remappedName = remapperChain.mapFieldName(classNode.name, field.name, field.desc);
if (remappedName.equals(field.name)) {
return null;
}
Deque<String> aliases = new LinkedList<String>();
aliases.add(remappedName);
return MixinPreProcessor.findField(classNode, aliases, field.desc);
}
/**
* Finds a field in the target class
*
@@ -349,7 +386,7 @@ class MixinPreProcessor {
* @param desc
* @return Target field or null if not found
*/
private FieldNode findField(ClassNode classNode, Deque<String> aliases, String desc) {
private static FieldNode findField(ClassNode classNode, Deque<String> aliases, String desc) {
String alias = aliases.poll();
if (alias == null) {
return null;
@@ -361,6 +398,6 @@ class MixinPreProcessor {
}
}
return this.findField(classNode, aliases, desc);
return MixinPreProcessor.findField(classNode, aliases, desc);
}
}