From f1f60c3ee857d591c349db12ced94cf1f620af1a Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Tue, 8 Aug 2017 14:19:03 +0100 Subject: [PATCH] Add runtime support for remapping refmaps to current mappings on-the-fly --- .../asm/launch/MixinBootstrap.java | 2 +- .../asm/mixin/MixinEnvironment.java | 27 ++ .../mixin/injection/points/BeforeInvoke.java | 14 +- .../mixin/injection/struct/InjectionInfo.java | 33 +- .../mixin/injection/struct/MemberInfo.java | 16 +- .../asm/mixin/refmap/IMixinContext.java | 2 +- .../asm/mixin/refmap/IReferenceMapper.java | 88 ++++++ .../asm/mixin/refmap/ReferenceMapper.java | 65 ++-- .../refmap/RemappingReferenceMapper.java | 283 ++++++++++++++++++ .../asm/mixin/transformer/MixinConfig.java | 10 +- .../mixin/transformer/MixinTargetContext.java | 4 +- 11 files changed, 486 insertions(+), 58 deletions(-) create mode 100644 src/main/java/org/spongepowered/asm/mixin/refmap/IReferenceMapper.java create mode 100644 src/main/java/org/spongepowered/asm/mixin/refmap/RemappingReferenceMapper.java diff --git a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java index 40ab7765..7ce2cad8 100644 --- a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java +++ b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java @@ -145,7 +145,7 @@ public abstract class MixinBootstrap { String command = System.getProperty("sun.java.command"); if (command != null && command.contains("GradleStart")) { - System.setProperty("mixin.env.disableRefMap", "true"); + System.setProperty("mixin.env.remapRefMap", "true"); } if (MixinBootstrap.findInStackTrace(Launch.class.getName(), "launch") > 132) { diff --git a/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java b/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java index e28d674a..75312045 100644 --- a/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java +++ b/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java @@ -371,6 +371,33 @@ public final class MixinEnvironment implements ITokenProvider { */ DISABLE_REFMAP(Option.ENVIRONMENT, Inherit.INDEPENDENT, "disableRefMap"), + /** + * Rather than disabling the refMap, you may wish to remap existing + * refMaps at runtime. This can be achieved by setting this property and + * supplying values for mixin.env.refMapRemappingFile and + * mixin.env.refMapRemappingEnv. Though those properties can be + * ignored if starting via GradleStart (this property is also + * automatically enabled if loading via GradleStart). + */ + REFMAP_REMAP(Option.ENVIRONMENT, Inherit.INDEPENDENT, "remapRefMap"), + + /** + * If mixin.env.remapRefMap is enabled, this setting can be + * used to override the name of the SRG file to read mappings from. The + * mappings must have a source type of searge and a target type + * matching the current development environment. If the source type is + * not searge then the mixin.env.refMapRemappingEnv + * should be set to the correct source environment type. + */ + REFMAP_REMAP_RESOURCE(Option.ENVIRONMENT, Inherit.INDEPENDENT, "refMapRemappingFile", ""), + + /** + * When using mixin.env.refMapRemappingFile, this setting + * overrides the default source environment (searge). However note that + * the specified environment type must exist in the orignal refmap. + */ + REFMAP_REMAP_SOURCE_ENV(Option.ENVIRONMENT, Inherit.INDEPENDENT, "refMapRemappingEnv", "searge"), + /** * Globally ignore the "required" attribute of all configurations */ diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/points/BeforeInvoke.java b/src/main/java/org/spongepowered/asm/mixin/injection/points/BeforeInvoke.java index 5cbf77db..8dcde60e 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/points/BeforeInvoke.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/points/BeforeInvoke.java @@ -32,6 +32,7 @@ import org.apache.logging.log4j.Logger; import org.spongepowered.asm.lib.tree.AbstractInsnNode; import org.spongepowered.asm.lib.tree.InsnList; import org.spongepowered.asm.lib.tree.MethodInsnNode; +import org.spongepowered.asm.mixin.MixinEnvironment.Option; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.InjectionPoint; import org.spongepowered.asm.mixin.injection.InjectionPoint.AtCode; @@ -67,7 +68,7 @@ import org.spongepowered.asm.mixin.injection.struct.MemberInfo; @AtCode("INVOKE") public class BeforeInvoke extends InjectionPoint { - protected final MemberInfo target; + protected final MemberInfo target, permissiveTarget; /** * This strategy can be used to identify a particular invocation if the same @@ -95,6 +96,7 @@ public class BeforeInvoke extends InjectionPoint { this.ordinal = data.getOrdinal(); this.log = data.get("log", false); this.className = this.getClassName(); + this.permissiveTarget = data.getContext().getOption(Option.REFMAP_REMAP) ? this.target.transform(null) : null; } private String getClassName() { @@ -133,7 +135,7 @@ public class BeforeInvoke extends InjectionPoint { MemberInfo nodeInfo = new MemberInfo(insn); this.log("{} is considering insn {}", this.className, nodeInfo); - if (this.target.matches(nodeInfo.owner, nodeInfo.name, nodeInfo.desc)) { + if (this.matches(nodeInfo)) { this.log("{} > found a matching insn, checking preconditions...", this.className); if (this.matchesInsn(nodeInfo, ordinal)) { @@ -156,6 +158,14 @@ public class BeforeInvoke extends InjectionPoint { return found; } + private boolean matches(MemberInfo nodeInfo) { + if (this.target.matches(nodeInfo.owner, nodeInfo.name, nodeInfo.desc)) { + return true; + } + + return this.permissiveTarget != null && this.permissiveTarget.matches(nodeInfo.owner, nodeInfo.name, nodeInfo.desc); + } + protected boolean addInsn(InsnList insns, Collection nodes, AbstractInsnNode insn) { nodes.add(insn); return true; diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionInfo.java b/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionInfo.java index 1df288bd..deb31236 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/struct/InjectionInfo.java @@ -380,20 +380,31 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC private void findMethods(Set searchFor, String type) { this.targets.clear(); + // When remapping refmap is enabled this implies we are in a development + // environment. In certain circumstances including the descriptor for + // the method may actually fail, so we will do a second pass without the + // descriptor if this happens. + int passes = this.mixin.getEnvironment().getOption(Option.REFMAP_REMAP) ? 2 : 1; + for (MemberInfo member : searchFor) { - int ordinal = 0; - for (MethodNode target : this.classNode.methods) { - if (member.matches(target.name, target.desc, ordinal)) { - boolean isMixinMethod = Annotations.getVisible(target, MixinMerged.class) != null; - if (member.matchAll && (Bytecode.methodIsStatic(target) != this.isStatic || target == this.method || isMixinMethod)) { - continue; + for (int count = 0, pass = 0; pass < passes && count < 1; pass++) { + int ordinal = 0; + for (MethodNode target : this.classNode.methods) { + if (member.matches(target.name, target.desc, ordinal)) { + boolean isMixinMethod = Annotations.getVisible(target, MixinMerged.class) != null; + if (member.matchAll && (Bytecode.methodIsStatic(target) != this.isStatic || target == this.method || isMixinMethod)) { + continue; + } + + this.checkTarget(target); + this.targets.add(target); + ordinal++; + count++; } - - this.checkTarget(target); - - this.targets.add(target); - ordinal++; } + + // Second pass ignores descriptor + member = member.transform(null); } } 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 88397ddd..a7730704 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 @@ -29,7 +29,7 @@ import org.spongepowered.asm.lib.tree.AbstractInsnNode; import org.spongepowered.asm.lib.tree.FieldInsnNode; import org.spongepowered.asm.lib.tree.MethodInsnNode; import org.spongepowered.asm.mixin.refmap.IMixinContext; -import org.spongepowered.asm.mixin.refmap.ReferenceMapper; +import org.spongepowered.asm.mixin.refmap.IReferenceMapper; import org.spongepowered.asm.mixin.throwables.MixinException; import org.spongepowered.asm.obfuscation.mapping.IMapping; import org.spongepowered.asm.obfuscation.mapping.common.MappingField; @@ -592,6 +592,18 @@ public final class MemberInfo { return new MemberInfo(this, newOwner); } + /** + * Create a new version of this member with a different descriptor + * + * @param newDesc New descriptor for this member + */ + public MemberInfo transform(String newDesc) { + if ((newDesc == null && this.desc == null) || (newDesc != null && newDesc.equals(this.desc))) { + return this; + } + return new MemberInfo(this.name, this.owner, newDesc, this.matchAll); + } + /** * Create a remapped version of this member using the supplied method data * @@ -653,7 +665,7 @@ public final class MemberInfo { * @param mixinClass Mixin class to use for remapping * @return parsed MemberInfo */ - private static MemberInfo parse(String input, ReferenceMapper refMapper, String mixinClass) { + private static MemberInfo parse(String input, IReferenceMapper refMapper, String mixinClass) { String desc = null; String owner = null; String name = Strings.nullToEmpty(input).replaceAll("\\s", ""); diff --git a/src/main/java/org/spongepowered/asm/mixin/refmap/IMixinContext.java b/src/main/java/org/spongepowered/asm/mixin/refmap/IMixinContext.java index 90566920..1456cf5c 100644 --- a/src/main/java/org/spongepowered/asm/mixin/refmap/IMixinContext.java +++ b/src/main/java/org/spongepowered/asm/mixin/refmap/IMixinContext.java @@ -58,7 +58,7 @@ public interface IMixinContext { * * @return ReferenceMapper instance (can be null) */ - public abstract ReferenceMapper getReferenceMapper(); + public abstract IReferenceMapper getReferenceMapper(); /** * Retrieve the value of the specified option from the environment diff --git a/src/main/java/org/spongepowered/asm/mixin/refmap/IReferenceMapper.java b/src/main/java/org/spongepowered/asm/mixin/refmap/IReferenceMapper.java new file mode 100644 index 00000000..61f939e3 --- /dev/null +++ b/src/main/java/org/spongepowered/asm/mixin/refmap/IReferenceMapper.java @@ -0,0 +1,88 @@ +/* + * 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.refmap; + +/** + * Interface for reference mapper objects + */ +public interface IReferenceMapper { + + /** + * Get whether this mapper is defaulted. Use this flag rather than reference + * comparison to {@link #DEFAULT_MAPPER} because of classloader shenanigans + * + * @return true if this mapper is a defaulted mapper + */ + public abstract boolean isDefault(); + + /** + * Get the resource name this refmap was loaded from (if available). + * + * @return name of the resource + */ + public abstract String getResourceName(); + + /** + * Get a user-readable "status" string for this refmap for use in error + * messages + * + * @return status message + */ + public abstract String getStatus(); + + /** + * Get the current context + * + * @return current context key, can be null + */ + public abstract String getContext(); + + /** + * Set the current remap context, can be null + * + * @param context remap context + */ + public abstract void setContext(String context); + + /** + * Remap a reference for the specified owning class in the current context + * + * @param className Owner class + * @param reference Reference to remap + * @return remapped reference, returns original reference if not remapped + */ + public abstract String remap(String className, String reference); + + /** + * Remap a reference for the specified owning class in the specified context + * + * @param context Remap context to use + * @param className Owner class + * @param reference Reference to remap + * @return remapped reference, returns original reference if not remapped + */ + public abstract String remapWithContext(String context, String className, String reference); + +} diff --git a/src/main/java/org/spongepowered/asm/mixin/refmap/ReferenceMapper.java b/src/main/java/org/spongepowered/asm/mixin/refmap/ReferenceMapper.java index 4127067f..8fe8bded 100644 --- a/src/main/java/org/spongepowered/asm/mixin/refmap/ReferenceMapper.java +++ b/src/main/java/org/spongepowered/asm/mixin/refmap/ReferenceMapper.java @@ -52,7 +52,7 @@ import net.minecraft.launchwrapper.Launch; * refmap is absent the environment is assumed to be deobfuscated (eg. dev-time) * and injections and other transformations will fail if this is not the case. */ -public final class ReferenceMapper implements Serializable { +public final class ReferenceMapper implements IReferenceMapper, Serializable { private static final long serialVersionUID = 2L; @@ -83,7 +83,7 @@ public final class ReferenceMapper implements Serializable { * True if this refmap cannot be written. Only true for the * {@link #DEFAULT_MAPPER} */ - private final transient boolean readOnly; + private final transient boolean readOnly; /** * Current remapping context, used as the key into {@link data} @@ -112,12 +112,10 @@ public final class ReferenceMapper implements Serializable { this.resource = resource; } - /** - * Get whether this mapper is defaulted. Use this flag rather than reference - * comparison to {@link #DEFAULT_MAPPER} because of classloader shenanigans - * - * @return true if this mapper is a defaulted mapper + /* (non-Javadoc) + * @see org.spongepowered.asm.mixin.refmap.IReferenceMapper#isDefault() */ + @Override public boolean isDefault() { return this.readOnly; } @@ -128,62 +126,55 @@ public final class ReferenceMapper implements Serializable { } } - /** - * Get the resource name this refmap was loaded from (if available). - * - * @return name of the resource + /* (non-Javadoc) + * @see org.spongepowered.asm.mixin.refmap.IReferenceMapper + * #getResourceName() */ + @Override public String getResourceName() { return this.resource; } - /** - * Get a user-readable "status" string for this refmap for use in error - * messages - * - * @return status message + /* (non-Javadoc) + * @see org.spongepowered.asm.mixin.refmap.IReferenceMapper#getStatus() */ + @Override public String getStatus() { return this.isDefault() ? "No refMap loaded." : "Using refmap " + this.getResourceName(); } - /** - * Get the current context - * - * @return current context key, can be null + /* (non-Javadoc) + * @see org.spongepowered.asm.mixin.refmap.IReferenceMapper#getContext() */ + @Override public String getContext() { return this.context; } - /** - * Set the current remap context, can be null - * - * @param context remap context + /* (non-Javadoc) + * @see org.spongepowered.asm.mixin.refmap.IReferenceMapper#setContext( + * java.lang.String) */ + @Override public void setContext(String context) { this.context = context; } - /** - * Remap a reference for the specified owning class in the current context - * - * @param className Owner class - * @param reference Reference to remap - * @return remapped reference, returns original reference if not remapped + /* (non-Javadoc) + * @see org.spongepowered.asm.mixin.refmap.IReferenceMapper#remap( + * java.lang.String, java.lang.String) */ + @Override public String remap(String className, String reference) { return this.remapWithContext(this.context, className, reference); } - /** - * Remap a reference for the specified owning class in the specified context - * - * @param context Remap context to use - * @param className Owner class - * @param reference Reference to remap - * @return remapped reference, returns original reference if not remapped + /* (non-Javadoc) + * @see org.spongepowered.asm.mixin.refmap.IReferenceMapper + * #remapWithContext(java.lang.String, java.lang.String, + * java.lang.String) */ + @Override public String remapWithContext(String context, String className, String reference) { Map> mappings = this.mappings; if (context != null) { diff --git a/src/main/java/org/spongepowered/asm/mixin/refmap/RemappingReferenceMapper.java b/src/main/java/org/spongepowered/asm/mixin/refmap/RemappingReferenceMapper.java new file mode 100644 index 00000000..850bb18f --- /dev/null +++ b/src/main/java/org/spongepowered/asm/mixin/refmap/RemappingReferenceMapper.java @@ -0,0 +1,283 @@ +/* + * 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.refmap; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.spongepowered.asm.mixin.MixinEnvironment; +import org.spongepowered.asm.mixin.MixinEnvironment.Option; + +import com.google.common.base.Charsets; +import com.google.common.base.Strings; +import com.google.common.io.Files; +import com.google.common.io.LineProcessor; + +/** + * This adapter is designed to address a problem with mixins when "deobfCompile" + * dependencies are used in a project which is using newer MCP mappings than the + * ones the imported dependency was compiled with. + * + *

Before now, refMaps in deobfCompile dependencies had to be disabled + * because the obfuscated mappings were no use in a development environment. + * However there existed no "mcp-to-different-version-of-mcp" mappings to use + * instead.

+ * + *

This class leverages the fact that mappings are provided into the + * environment by GradleStart and consumes SRG mappings in the imported refMaps + * and converts them on-the-fly using srg-to-mcp mappings. This allows refMaps + * to be normalised to the current MCP environment.

+ * + *

Note that this class takes a naïve approach to remapping on the basis that + * searge names are unique and can thus be remapped with a straightforward dumb + * string replacement. Whilst the input environment and mappings are + * customisable via the appropriate environment vars, this fact should be taken + * into account if a different mapping environment is to be used.

+ * + *

All lookups are straightforward string replacements using all + * values in the map, this basically means this is probably pretty slow, but I + * don't care because the number of mappings processed is usually pretty small + * (few hundred at most) and this is only used in dev where we don't actually + * care about speed. Some performance is gained (approx 10ms per lookup) by + * caching the transformed descriptors.

+ */ +public final class RemappingReferenceMapper implements IReferenceMapper { + + /** + * This is the default GradleStart-injected system property which tells us + * the location of the srg-to-mcp mappings + */ + private static final String DEFAULT_RESOURCE_PATH_PROPERTY = "net.minecraftforge.gradle.GradleStart.srg.srg-mcp"; + + /** + * The default environment name to map from + */ + private static final String DEFAULT_MAPPING_ENV = "searge"; + + /** + * Logger + */ + private static final Logger logger = LogManager.getLogger("mixin"); + + /** + * Loaded srgs, stored as a mapping of filename to mappings. Global cache so + * that we only need to load each mapping file once. + */ + private static final Map> srgs = new HashMap>(); + + /** + * The "inner" refmap, this is the original refmap specified in the config + */ + private final IReferenceMapper refMap; + + /** + * The loaded mappings, retrieved from {@link #srgs} by filename + */ + private final Map mappings; + + /** + * Cache of transformed mappings. Each transformation takes about 10ms so + * this saves us time when the same mapping is used repeatedly + */ + private final Map> cache = new HashMap>(); + + private RemappingReferenceMapper(MixinEnvironment env, IReferenceMapper refMap) { + this.refMap = refMap; + this.refMap.setContext(RemappingReferenceMapper.getMappingEnv(env)); + + String resource = RemappingReferenceMapper.getResource(env); + this.mappings = RemappingReferenceMapper.loadSrgs(resource); + + RemappingReferenceMapper.logger.info("Remapping refMap {} using {}", refMap.getResourceName(), resource); + } + + /* (non-Javadoc) + * @see org.spongepowered.asm.mixin.refmap.IReferenceMapper#isDefault() + */ + @Override + public boolean isDefault() { + return this.refMap.isDefault(); + } + + /* (non-Javadoc) + * @see org.spongepowered.asm.mixin.refmap.IReferenceMapper + * #getResourceName() + */ + @Override + public String getResourceName() { + return this.refMap.getResourceName(); + } + + /* (non-Javadoc) + * @see org.spongepowered.asm.mixin.refmap.IReferenceMapper#getStatus() + */ + @Override + public String getStatus() { + return this.refMap.getStatus(); + } + + /* (non-Javadoc) + * @see org.spongepowered.asm.mixin.refmap.IReferenceMapper#getContext() + */ + @Override + public String getContext() { + return this.refMap.getContext(); + } + + /* (non-Javadoc) + * @see org.spongepowered.asm.mixin.refmap.IReferenceMapper#setContext( + * java.lang.String) + */ + @Override + public void setContext(String context) { + // Nope + } + + /* (non-Javadoc) + * @see org.spongepowered.asm.mixin.refmap.IReferenceMapper#remap( + * java.lang.String, java.lang.String) + */ + @Override + public String remap(String className, String reference) { + Map classCache = this.getCache(className); + String remapped = classCache.get(reference); + if (remapped == null) { + remapped = this.refMap.remap(className, reference); + for (Entry entry : this.mappings.entrySet()) { + remapped = remapped.replace(entry.getKey(), entry.getValue()); + } + classCache.put(reference, remapped); + } + return remapped; + } + + private Map getCache(String className) { + Map classCache = this.cache.get(className); + if (classCache == null) { + classCache = new HashMap(); + this.cache.put(className, classCache); + } + return classCache; + } + + /* (non-Javadoc) + * @see org.spongepowered.asm.mixin.refmap.IReferenceMapper + * #remapWithContext(java.lang.String, java.lang.String, + * java.lang.String) + */ + @Override + public String remapWithContext(String context, String className, String reference) { + return this.refMap.remapWithContext(context, className, reference); + } + + /** + * Read srgs from the specified file resource. The mappings are cached + * internally so this will only read the file the first time it is called + * with a particulare filename. + * + * @param fileName srg file to read + * @return srgs read from file or empty map if the file could not be read + */ + private static Map loadSrgs(String fileName) { + if (RemappingReferenceMapper.srgs.containsKey(fileName)) { + return RemappingReferenceMapper.srgs.get(fileName); + } + + final Map map = new HashMap(); + RemappingReferenceMapper.srgs.put(fileName, map); + + File file = new File(fileName); + if (!file.isFile()) { + return map; + } + + try { + Files.readLines(file, Charsets.UTF_8, new LineProcessor() { + + @Override + public Object getResult() { + return null; + } + + @Override + public boolean processLine(String line) throws IOException { + if (Strings.isNullOrEmpty(line) || line.startsWith("#")) { + return true; + } + int fromPos = 0, toPos = 0; + if ((toPos = line.startsWith("MD: ") ? 2 : line.startsWith("FD: ") ? 1 : 0) > 0) { + String[] entries = line.substring(4).split(" ", 4); + map.put( + entries[fromPos].substring(entries[fromPos].lastIndexOf('/') + 1), + entries[toPos].substring(entries[toPos].lastIndexOf('/') + 1) + ); + } + return true; + } + }); + } catch (IOException ex) { + RemappingReferenceMapper.logger.warn("Could not read input SRG file: {}", fileName); + RemappingReferenceMapper.logger.catching(ex); + } + + return map; + } + + /** + * Wrap the specified refmap in a remapping adapter using settings in the + * supplied environment + * + * @param env environment to read configuration from + * @param refMap refmap to wrap + * @return wrapped refmap or original refmap is srg data is not available + */ + public static IReferenceMapper of(MixinEnvironment env, IReferenceMapper refMap) { + if (!refMap.isDefault() && RemappingReferenceMapper.hasData(env)) { + return new RemappingReferenceMapper(env, refMap); + } + return refMap; + } + + private static boolean hasData(MixinEnvironment env) { + String fileName = RemappingReferenceMapper.getResource(env); + return fileName != null && new File(fileName).exists(); + } + + private static String getResource(MixinEnvironment env) { + String resource = env.getOptionValue(Option.REFMAP_REMAP_RESOURCE); + return Strings.isNullOrEmpty(resource) ? System.getProperty(RemappingReferenceMapper.DEFAULT_RESOURCE_PATH_PROPERTY) : resource; + } + + private static String getMappingEnv(MixinEnvironment env) { + String resource = env.getOptionValue(Option.REFMAP_REMAP_SOURCE_ENV); + return Strings.isNullOrEmpty(resource) ? RemappingReferenceMapper.DEFAULT_MAPPING_ENV : resource; + } + +} diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java index e09b24ad..c7438313 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java @@ -52,7 +52,9 @@ import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.InjectionPoint; +import org.spongepowered.asm.mixin.refmap.IReferenceMapper; import org.spongepowered.asm.mixin.refmap.ReferenceMapper; +import org.spongepowered.asm.mixin.refmap.RemappingReferenceMapper; import org.spongepowered.asm.mixin.transformer.throwables.InvalidMixinException; import org.spongepowered.asm.util.VersionNumber; @@ -290,7 +292,7 @@ final class MixinConfig implements Comparable, IMixinConfig { /** * Reference mapper for injectors */ - private transient ReferenceMapper refMapper; + private transient IReferenceMapper refMapper; /** * Keep track of initialisation state @@ -469,6 +471,10 @@ final class MixinConfig implements Comparable, IMixinConfig { this.logger.warn("Reference map '{}' for {} could not be read. If this is a development environment you can ignore this message", this.refMapperConfig, this); } + + if (this.env.getOption(Option.REFMAP_REMAP)) { + this.refMapper = RemappingReferenceMapper.of(this.env, this.refMapper); + } } /** @@ -746,7 +752,7 @@ final class MixinConfig implements Comparable, IMixinConfig { /** * Get the reference remapper for injectors */ - public ReferenceMapper getReferenceMapper() { + public IReferenceMapper getReferenceMapper() { if (this.env.getOption(Option.DISABLE_REFMAP)) { return ReferenceMapper.DEFAULT_MAPPER; } diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java index 2c717496..b8516c64 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTargetContext.java @@ -57,7 +57,7 @@ import org.spongepowered.asm.mixin.injection.struct.Target; import org.spongepowered.asm.mixin.injection.throwables.InjectionError; import org.spongepowered.asm.mixin.injection.throwables.InjectionValidationException; import org.spongepowered.asm.mixin.refmap.IMixinContext; -import org.spongepowered.asm.mixin.refmap.ReferenceMapper; +import org.spongepowered.asm.mixin.refmap.IReferenceMapper; import org.spongepowered.asm.mixin.transformer.ClassInfo.Field; import org.spongepowered.asm.mixin.transformer.ClassInfo.Method; import org.spongepowered.asm.mixin.transformer.ClassInfo.SearchType; @@ -1102,7 +1102,7 @@ public class MixinTargetContext extends ClassContext implements IMixinContext { * #getReferenceMapper() */ @Override - public ReferenceMapper getReferenceMapper() { + public IReferenceMapper getReferenceMapper() { return this.mixin.getParent().getReferenceMapper(); }