diff --git a/build.gradle b/build.gradle index edb8ed8f..40018b25 100644 --- a/build.gradle +++ b/build.gradle @@ -95,6 +95,9 @@ sourceSets { agent { compileClasspath += main.output } + bridge { + compileClasspath += main.output + } } // Dependencies for renamed ASM @@ -130,6 +133,7 @@ task renamedASM { } } } +eclipseClasspath.dependsOn(renamedASM) // Project dependencies dependencies { @@ -163,6 +167,11 @@ dependencies { compile 'net.minecraft:launchwrapper:1.11' agentCompile 'net.minecraft:launchwrapper:1.11' + + // asm bridge + compile 'org.ow2.asm:asm-commons:5.0.3' + bridgeCompile 'org.ow2.asm:asm-commons:5.0.3' + bridgeCompile 'org.apache.logging.log4j:log4j-core:2.0-beta9' } // Filter, process, and include resources @@ -221,7 +230,10 @@ jar { // Include hotswap agent from sourceSets.agent.output - + + // Bridge + from sourceSets.bridge.output + // Renamed ASM from zipTree(tasks.renamedASM.outputs.files[0]) @@ -275,6 +287,8 @@ task sourceJar(type: Jar) { from sourceSets.fernflower.resources from sourceSets.agent.java from sourceSets.agent.resources + from sourceSets.bridge.java + from sourceSets.bridge.resources classifier = "sources" } @@ -333,7 +347,7 @@ uploadArchives { } whenConfigured { dependencies = dependencies.findAll { - !it.artifactId.contains('fernflower') + !it.artifactId.matches('^(fernflower|asm-commons|Srg2Source|jarjar|junit|mockito-core|hamcrest-library)$') } } } @@ -344,7 +358,7 @@ uploadArchives { install.repositories.mavenInstaller.pom { whenConfigured { dependencies = dependencies.findAll { - !it.artifactId.contains('fernflower') + !it.artifactId.matches('^(fernflower|asm-commons|Srg2Source|jarjar|junit|mockito-core|hamcrest-library)$') } } } diff --git a/gradle.properties b/gradle.properties index 27db9610..3ffa47fd 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,5 +4,5 @@ packaging=jar description=Mixin url=https://www.spongepowered.org organization=SpongePowered -buildVersion=0.4.9 +buildVersion=0.4.10 buildType=SNAPSHOT diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 3abfba7f..e7e56722 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.2-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-2.8-bin.zip diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java index 02ff43c2..11807aaa 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java @@ -648,9 +648,10 @@ class AnnotatedMixin { } } } - } finally { - return obfFieldData; + } catch (Exception ex) { + // ??? } + return obfFieldData; } /** diff --git a/src/bridge/java/org/spongepowered/asm/bridge/RemapperAdapter.java b/src/bridge/java/org/spongepowered/asm/bridge/RemapperAdapter.java new file mode 100644 index 00000000..08234ccd --- /dev/null +++ b/src/bridge/java/org/spongepowered/asm/bridge/RemapperAdapter.java @@ -0,0 +1,118 @@ +/* + * This file is part of Sponge, licensed under the MIT License (MIT). + * + * Copyright (c) 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.bridge; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.objectweb.asm.commons.Remapper; +import org.spongepowered.asm.mixin.extensibility.IRemapper; + +public abstract class RemapperAdapter implements IRemapper { + + protected final Logger logger = LogManager.getLogger("mixin"); + protected final Remapper remapper; + + public RemapperAdapter(Remapper remapper) { + this.remapper = remapper; + } + + @Override + public String toString() { + return this.getClass().getSimpleName(); + } + + @Override + public String mapMethodName(String owner, String name, String desc) { + this.logger.debug("{} is remapping method {}{} for {}", this, name, desc, owner); + String newName = this.remapper.mapMethodName(owner, name, desc); + if (!newName.equals(name)) { + return newName; + } + String obfOwner = this.unmap(owner); + String obfDesc = this.unmapDesc(desc); + this.logger.debug("{} is remapping obfuscated method {}{} for {}", this, name, obfDesc, obfOwner); + return this.remapper.mapMethodName(obfOwner, name, obfDesc); + } + + @Override + public String mapFieldName(String owner, String name, String desc) { + this.logger.debug("{} is remapping field {}{} for {}", this, name, desc, owner); + String newName = this.remapper.mapFieldName(owner, name, desc); + if (!newName.equals(name)) { + return newName; + } + String obfOwner = this.unmap(owner); + String obfDesc = this.unmapDesc(desc); + this.logger.debug("{} is remapping obfuscated field {}{} for {}", this, name, obfDesc, obfOwner); + return this.remapper.mapFieldName(obfOwner, name, obfDesc); + } + + @Override + public String map(String typeName) { + this.logger.debug("{} is remapping class {}", this, typeName); + return this.remapper.map(typeName); + } + + @Override + public String unmap(String typeName) { + return typeName; + } + + @Override + public String mapDesc(String desc) { + return this.remapper.mapDesc(desc); + } + + @Override + public String unmapDesc(String desc) { + StringBuilder sb = new StringBuilder(); + StringBuilder token = null; + + for (int pos = 0; pos < desc.length(); pos++) { + char c = desc.charAt(pos); + if (token != null) { + if (c == ';') { + sb.append('L').append(this.unmap(token.toString())).append(';'); + token = null; + } else { + token.append(c); + } + continue; + } + if (c == 'L') { + token = new StringBuilder(); + } else { + sb.append(c); + } + } + + if (token != null) { + throw new IllegalArgumentException("Invalid descriptor '" + desc + "', missing ';'"); + } + + return sb.toString(); + } + +} diff --git a/src/bridge/java/org/spongepowered/asm/bridge/RemapperAdapterFML.java b/src/bridge/java/org/spongepowered/asm/bridge/RemapperAdapterFML.java new file mode 100644 index 00000000..206ec1c5 --- /dev/null +++ b/src/bridge/java/org/spongepowered/asm/bridge/RemapperAdapterFML.java @@ -0,0 +1,77 @@ +/* + * This file is part of Mixin, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * 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.bridge; + +import org.objectweb.asm.commons.Remapper; +import org.spongepowered.asm.mixin.extensibility.IRemapper; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +public class RemapperAdapterFML extends RemapperAdapter { + + private final Method mdUnmap; + + private RemapperAdapterFML(Remapper remapper, Method mdUnmap) { + super(remapper); + this.logger.info("Initialised Mixin FML Remapper Adapter with {}", remapper); + this.mdUnmap = mdUnmap; + } + + @Override + public String unmap(String typeName) { + try { + return this.mdUnmap.invoke(this.remapper, typeName).toString(); + } catch (Exception ex) { + return typeName; + } + } + + public static IRemapper create() { + try { + Class clDeobfRemapper = RemapperAdapterFML.getFMLDeobfuscatingRemapper(); + Field singletonField = clDeobfRemapper.getDeclaredField("INSTANCE"); + Method mdUnmap = clDeobfRemapper.getDeclaredMethod("unmap", String.class); + Remapper remapper = (Remapper)singletonField.get(null); + return new RemapperAdapterFML(remapper, mdUnmap); + } catch (Exception ex) { + ex.printStackTrace(); + return null; + } + } + + /** + * Attempt to get the FML Deobfuscating Remapper, tries the post-1.8 + * namespace first and falls back to 1.7.10 if class lookup fails + */ + private static Class getFMLDeobfuscatingRemapper() throws ClassNotFoundException { + try { + return Class.forName("net.minecraftforge.fml.common.asm.transformers.deobf.FMLDeobfuscatingRemapper"); + } catch (ClassNotFoundException ex) { + return Class.forName("cpw.mods.fml.common.asm.transformers.deobf.FMLDeobfuscatingRemapper"); + } + } + +} diff --git a/src/main/java/org/spongepowered/asm/launch/IMixinLaunchAgent.java b/src/main/java/org/spongepowered/asm/launch/IMixinLaunchAgent.java index 4bf108b7..f9a08085 100644 --- a/src/main/java/org/spongepowered/asm/launch/IMixinLaunchAgent.java +++ b/src/main/java/org/spongepowered/asm/launch/IMixinLaunchAgent.java @@ -44,6 +44,13 @@ interface IMixinLaunchAgent { */ public abstract void prepare(); + /** + * Called from injectIntoClassLoader in the parent tweaker but + * only called on the primary tweak container. This is useful if the + * agent needs to perform some environment-specific setup just once. + */ + public abstract void initPrimaryContainer(); + /** * Called from injectIntoClassLoader in the parent tweaker * diff --git a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java index be9b76d2..1a0dbeb7 100644 --- a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java +++ b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java @@ -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 diff --git a/src/main/java/org/spongepowered/asm/launch/MixinLaunchAgentAbstract.java b/src/main/java/org/spongepowered/asm/launch/MixinLaunchAgentAbstract.java index da7833a6..30747daa 100644 --- a/src/main/java/org/spongepowered/asm/launch/MixinLaunchAgentAbstract.java +++ b/src/main/java/org/spongepowered/asm/launch/MixinLaunchAgentAbstract.java @@ -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 */ diff --git a/src/main/java/org/spongepowered/asm/launch/MixinLaunchAgentDefault.java b/src/main/java/org/spongepowered/asm/launch/MixinLaunchAgentDefault.java index 05afe077..c503c3e4 100644 --- a/src/main/java/org/spongepowered/asm/launch/MixinLaunchAgentDefault.java +++ b/src/main/java/org/spongepowered/asm/launch/MixinLaunchAgentDefault.java @@ -64,6 +64,10 @@ public class MixinLaunchAgentDefault extends MixinLaunchAgentAbstract { } } } + + @Override + public void initPrimaryContainer() { + } @Override public void injectIntoClassLoader(LaunchClassLoader classLoader) { diff --git a/src/main/java/org/spongepowered/asm/launch/MixinLaunchAgentFML.java b/src/main/java/org/spongepowered/asm/launch/MixinLaunchAgentFML.java index c39d1460..bdd86623 100644 --- a/src/main/java/org/spongepowered/asm/launch/MixinLaunchAgentFML.java +++ b/src/main/java/org/spongepowered/asm/launch/MixinLaunchAgentFML.java @@ -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 diff --git a/src/main/java/org/spongepowered/asm/launch/MixinTweakContainer.java b/src/main/java/org/spongepowered/asm/launch/MixinTweakContainer.java index 2bf79fa4..cf9eef3a 100644 --- a/src/main/java/org/spongepowered/asm/launch/MixinTweakContainer.java +++ b/src/main/java/org/spongepowered/asm/launch/MixinTweakContainer.java @@ -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 diff --git a/src/main/java/org/spongepowered/asm/launch/MixinTweaker.java b/src/main/java/org/spongepowered/asm/launch/MixinTweaker.java index 66677eaf..daba5586 100644 --- a/src/main/java/org/spongepowered/asm/launch/MixinTweaker.java +++ b/src/main/java/org/spongepowered/asm/launch/MixinTweaker.java @@ -63,6 +63,11 @@ public class MixinTweaker implements ITweaker { */ private final Map containers = new LinkedHashMap(); + /** + * Container for this tweaker + */ + private MixinTweakContainer primaryContainer; + /** * Tracks whether {@link #acceptOptions} was called yet, if true, causes new * launch agents to be prepared 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()) { diff --git a/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java b/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java index 20b7c325..de80faff 100644 --- a/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java +++ b/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java @@ -605,6 +605,11 @@ public class MixinEnvironment implements ITokenProvider { * Internal tokens defined by this environment */ private final Map internalTokens = new HashMap(); + + /** + * 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 */ diff --git a/src/main/java/org/spongepowered/asm/mixin/RemapperChain.java b/src/main/java/org/spongepowered/asm/mixin/RemapperChain.java new file mode 100644 index 00000000..117eed0b --- /dev/null +++ b/src/main/java/org/spongepowered/asm/mixin/RemapperChain.java @@ -0,0 +1,106 @@ +/* + * This file is part of Mixin, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * 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 remappers = new ArrayList(); + + 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; + } +} diff --git a/src/main/java/org/spongepowered/asm/mixin/extensibility/IRemapper.java b/src/main/java/org/spongepowered/asm/mixin/extensibility/IRemapper.java new file mode 100644 index 00000000..69a412b0 --- /dev/null +++ b/src/main/java/org/spongepowered/asm/mixin/extensibility/IRemapper.java @@ -0,0 +1,84 @@ +/* + * This file is part of Mixin, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * 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); + +} diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java b/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java index 0810d129..aa462e63 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java @@ -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); + } } } diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/struct/MemberInfo.java b/src/main/java/org/spongepowered/asm/mixin/injection/struct/MemberInfo.java index 1b1cb758..5d4bf62c 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/struct/MemberInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/struct/MemberInfo.java @@ -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)) { diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java b/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java index 3ac64fa6..ac3e1dcd 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/ClassInfo.java @@ -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 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); diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicator.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicator.java index 1efe2b3f..65bb13fc 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicator.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinApplicator.java @@ -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; diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessor.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessor.java index 769da82d..8fe20f02 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessor.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinPreProcessor.java @@ -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 aliases = new LinkedList(); + aliases.add(remappedName); + + return MixinPreProcessor.findMethod(classNode, aliases, method.desc); + } + private static MethodNode findMethod(ClassNode classNode, Deque 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 aliases = new LinkedList(); 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 aliases = new LinkedList(); + 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 aliases, String desc) { + private static FieldNode findField(ClassNode classNode, Deque 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); } }