mirror of
https://github.com/LegacyModdingMC/UniMix.git
synced 2026-11-22 07:22:13 +00:00
Add runtime support for remapping refmaps to current mappings on-the-fly
This commit is contained in:
1 parent
e106a5519c
commit
f1f60c3ee8
11 files changed
+486
-58
No files matched your search
@@ -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) {
|
||||
|
||||
@@ -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 <tt>mixin.env.refMapRemappingFile</tt> and
|
||||
* <tt>mixin.env.refMapRemappingEnv</tt>. Though those properties can be
|
||||
* ignored if starting via <tt>GradleStart</tt> (this property is also
|
||||
* automatically enabled if loading via GradleStart).
|
||||
*/
|
||||
REFMAP_REMAP(Option.ENVIRONMENT, Inherit.INDEPENDENT, "remapRefMap"),
|
||||
|
||||
/**
|
||||
* If <tt>mixin.env.remapRefMap</tt> 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 <tt>searge</tt> and a target type
|
||||
* matching the current development environment. If the source type is
|
||||
* not <tt>searge</tt> then the <tt>mixin.env.refMapRemappingEnv</tt>
|
||||
* should be set to the correct source environment type.
|
||||
*/
|
||||
REFMAP_REMAP_RESOURCE(Option.ENVIRONMENT, Inherit.INDEPENDENT, "refMapRemappingFile", ""),
|
||||
|
||||
/**
|
||||
* When using <tt>mixin.env.refMapRemappingFile</tt>, 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
|
||||
*/
|
||||
|
||||
@@ -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<AbstractInsnNode> nodes, AbstractInsnNode insn) {
|
||||
nodes.add(insn);
|
||||
return true;
|
||||
|
||||
@@ -380,20 +380,31 @@ public abstract class InjectionInfo extends SpecialMethodInfo implements ISliceC
|
||||
private void findMethods(Set<MemberInfo> 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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", "");
|
||||
|
||||
@@ -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 <tt>option</tt> from the environment
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* 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.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);
|
||||
|
||||
}
|
||||
@@ -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<String, Map<String, String>> mappings = this.mappings;
|
||||
if (context != null) {
|
||||
|
||||
@@ -0,0 +1,283 @@
|
||||
/*
|
||||
* 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.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.
|
||||
*
|
||||
* <p>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.</p>
|
||||
*
|
||||
* <p>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.</p>
|
||||
*
|
||||
* <p>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.</p>
|
||||
*
|
||||
* <p>All lookups are straightforward string replacements using <em>all</em>
|
||||
* 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.</p>
|
||||
*/
|
||||
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 <em>from</em>
|
||||
*/
|
||||
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<String, Map<String, String>> srgs = new HashMap<String, Map<String,String>>();
|
||||
|
||||
/**
|
||||
* 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<String, String> 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<String, Map<String, String>> cache = new HashMap<String, Map<String, String>>();
|
||||
|
||||
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<String, String> classCache = this.getCache(className);
|
||||
String remapped = classCache.get(reference);
|
||||
if (remapped == null) {
|
||||
remapped = this.refMap.remap(className, reference);
|
||||
for (Entry<String, String> entry : this.mappings.entrySet()) {
|
||||
remapped = remapped.replace(entry.getKey(), entry.getValue());
|
||||
}
|
||||
classCache.put(reference, remapped);
|
||||
}
|
||||
return remapped;
|
||||
}
|
||||
|
||||
private Map<String, String> getCache(String className) {
|
||||
Map<String, String> classCache = this.cache.get(className);
|
||||
if (classCache == null) {
|
||||
classCache = new HashMap<String, String>();
|
||||
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<String, String> loadSrgs(String fileName) {
|
||||
if (RemappingReferenceMapper.srgs.containsKey(fileName)) {
|
||||
return RemappingReferenceMapper.srgs.get(fileName);
|
||||
}
|
||||
|
||||
final Map<String, String> map = new HashMap<String, String>();
|
||||
RemappingReferenceMapper.srgs.put(fileName, map);
|
||||
|
||||
File file = new File(fileName);
|
||||
if (!file.isFile()) {
|
||||
return map;
|
||||
}
|
||||
|
||||
try {
|
||||
Files.readLines(file, Charsets.UTF_8, new LineProcessor<Object>() {
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<MixinConfig>, 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<MixinConfig>, 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<MixinConfig>, IMixinConfig {
|
||||
/**
|
||||
* Get the reference remapper for injectors
|
||||
*/
|
||||
public ReferenceMapper getReferenceMapper() {
|
||||
public IReferenceMapper getReferenceMapper() {
|
||||
if (this.env.getOption(Option.DISABLE_REFMAP)) {
|
||||
return ReferenceMapper.DEFAULT_MAPPER;
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user