mirror of
https://github.com/LegacyModdingMC/UniMix.git
synced 2026-11-27 19:02:56 +00:00
[Fabric] Provide a Loader-hookable RemappingReferenceMapper
This commit is contained in:
1 parent
3e80843c23
commit
59db546c22
3 files changed
+107
-149
No files matched your search
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
public interface IClassReferenceMapper {
|
||||
public abstract String remapClassName(String className, String inputClassName);
|
||||
public abstract String remapClassNameWithContext(String context, String className, String inputClassName);
|
||||
}
|
||||
@@ -24,73 +24,29 @@
|
||||
*/
|
||||
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.objectweb.asm.Type;
|
||||
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;
|
||||
import org.spongepowered.asm.mixin.extensibility.IRemapper;
|
||||
import org.spongepowered.asm.mixin.injection.struct.MemberInfo;
|
||||
import org.spongepowered.asm.mixin.transformer.ClassInfo;
|
||||
import org.spongepowered.asm.util.ObfuscationUtil;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 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>
|
||||
* This adapter is designed to apply the same remapping used elsewhere in
|
||||
* the development chain (RemapperChain) to reference maps.
|
||||
*/
|
||||
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";
|
||||
|
||||
public final class RemappingReferenceMapper implements IClassReferenceMapper, IReferenceMapper {
|
||||
/**
|
||||
* 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
|
||||
@@ -98,24 +54,20 @@ public final class RemappingReferenceMapper implements IReferenceMapper {
|
||||
private final IReferenceMapper refMap;
|
||||
|
||||
/**
|
||||
* The loaded mappings, retrieved from {@link #srgs} by filename
|
||||
* The remapper in use.
|
||||
*/
|
||||
private final Map<String, String> mappings;
|
||||
|
||||
private final IRemapper remapper;
|
||||
|
||||
/**
|
||||
* Cache of transformed mappings. Each transformation takes about 10ms so
|
||||
* this saves us time when the same mapping is used repeatedly
|
||||
* A map between reference mapper values and their remapped equivalents.
|
||||
*/
|
||||
private final Map<String, Map<String, String>> cache = new HashMap<String, Map<String, String>>();
|
||||
|
||||
private final Map<String, String> mappedReferenceCache = new HashMap<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);
|
||||
this.remapper = env.getRemappers();
|
||||
|
||||
RemappingReferenceMapper.logger.info("Remapping refMap {} using {}", refMap.getResourceName(), resource);
|
||||
RemappingReferenceMapper.logger.info("Remapping refMap {} using remapper chain", refMap.getResourceName());
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -157,7 +109,7 @@ public final class RemappingReferenceMapper implements IReferenceMapper {
|
||||
*/
|
||||
@Override
|
||||
public void setContext(String context) {
|
||||
// Nope
|
||||
this.refMap.setContext(context);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -166,25 +118,16 @@ public final class RemappingReferenceMapper implements IReferenceMapper {
|
||||
*/
|
||||
@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;
|
||||
return this.remapWithContext(getContext(), className, reference);
|
||||
}
|
||||
|
||||
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);
|
||||
private static String remapMethodDescriptor(IRemapper remapper, String desc) {
|
||||
StringBuilder newDesc = new StringBuilder();
|
||||
newDesc.append('(');
|
||||
for (Type arg : Type.getArgumentTypes(desc)) {
|
||||
newDesc.append(remapper.mapDesc(arg.getDescriptor()));
|
||||
}
|
||||
return classCache;
|
||||
return newDesc.append(')').append(remapper.mapDesc(Type.getReturnType(desc).getDescriptor())).toString();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -194,60 +137,39 @@ public final class RemappingReferenceMapper implements IReferenceMapper {
|
||||
*/
|
||||
@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);
|
||||
if (reference.isEmpty()) {
|
||||
return reference;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
String origInfoString = this.refMap.remapWithContext(context, className, reference);
|
||||
String remappedCached = mappedReferenceCache.get(origInfoString);
|
||||
if (remappedCached != null) {
|
||||
return remappedCached;
|
||||
} else {
|
||||
String remapped = origInfoString;
|
||||
|
||||
@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);
|
||||
// To handle propagation, find super/itf-class (for IRemapper)
|
||||
// but pass the requested class in the MemberInfo
|
||||
MemberInfo info = MemberInfo.parse(remapped, null, null);
|
||||
if (info.getName() == null && info.getDesc() == null) {
|
||||
return info.getOwner() != null ? new MemberInfo(remapper.map(info.getOwner()), null, null).toString() : info.toString();
|
||||
} else if (info.isField()) {
|
||||
remapped = new MemberInfo(
|
||||
remapper.mapFieldName(info.getOwner(), info.getName(), info.getDesc()),
|
||||
info.getOwner() == null ? null : remapper.map(info.getOwner()),
|
||||
info.getDesc() == null ? null : remapper.mapDesc(info.getDesc())
|
||||
).toString();
|
||||
} else {
|
||||
remapped = new MemberInfo(
|
||||
remapper.mapMethodName(info.getOwner(), info.getName(), info.getDesc()),
|
||||
info.getOwner() == null ? null : remapper.map(info.getOwner()),
|
||||
info.getDesc() == null ? null : remapMethodDescriptor(remapper, info.getDesc())
|
||||
).toString();
|
||||
}
|
||||
|
||||
mappedReferenceCache.put(origInfoString, remapped);
|
||||
return remapped;
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -259,25 +181,25 @@ public final class RemappingReferenceMapper implements IReferenceMapper {
|
||||
* @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)) {
|
||||
if (!refMap.isDefault()) {
|
||||
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();
|
||||
@Override
|
||||
public String remapClassName(String className, String inputClassName) {
|
||||
return remapClassNameWithContext(getContext(), className, inputClassName);
|
||||
}
|
||||
|
||||
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;
|
||||
@Override
|
||||
public String remapClassNameWithContext(String context, String className, String remapped) {
|
||||
String origInfoString;
|
||||
if (this.refMap instanceof IClassReferenceMapper) {
|
||||
origInfoString = ((IClassReferenceMapper) this.refMap).remapClassNameWithContext(context, className, remapped);
|
||||
} else {
|
||||
origInfoString = this.refMap.remapWithContext(context, className, remapped);
|
||||
}
|
||||
return remapper.map(origInfoString.replace('.', '/'));
|
||||
}
|
||||
|
||||
private static String getMappingEnv(MixinEnvironment env) {
|
||||
String resource = env.getOptionValue(Option.REFMAP_REMAP_SOURCE_ENV);
|
||||
return Strings.isNullOrEmpty(resource) ? RemappingReferenceMapper.DEFAULT_MAPPING_ENV : resource;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -45,6 +45,7 @@ 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.IClassReferenceMapper;
|
||||
import org.spongepowered.asm.mixin.refmap.IReferenceMapper;
|
||||
import org.spongepowered.asm.mixin.refmap.ReferenceMapper;
|
||||
import org.spongepowered.asm.mixin.refmap.RemappingReferenceMapper;
|
||||
@@ -888,7 +889,12 @@ final class MixinConfig implements Comparable<MixinConfig>, IMixinConfig {
|
||||
// if (remapped != null) {
|
||||
// return remapped;
|
||||
// }
|
||||
return this.getReferenceMapper().remap(className, reference);
|
||||
IReferenceMapper mapper = this.getReferenceMapper();
|
||||
if (mapper instanceof IClassReferenceMapper) {
|
||||
return ((IClassReferenceMapper) mapper).remapClassName(className, reference);
|
||||
} else {
|
||||
return mapper.remap(className, reference);
|
||||
}
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
|
||||
Reference in new issue
Block a user