diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java index 156d24f9..e569bdd7 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java @@ -24,7 +24,10 @@ */ package org.spongepowered.tools.obfuscation; +import java.io.File; +import java.io.IOException; import java.io.InputStream; +import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -55,6 +58,7 @@ import javax.tools.StandardLocation; import org.spongepowered.asm.launch.MixinBootstrap; import org.spongepowered.asm.mixin.Overwrite; import org.spongepowered.asm.mixin.injection.struct.MemberInfo; +import org.spongepowered.asm.mixin.injection.struct.ReferenceMapper; import org.spongepowered.asm.util.ITokenProvider; import org.spongepowered.tools.MirrorUtils; import org.spongepowered.tools.obfuscation.IMixinValidator.ValidationPass; @@ -66,7 +70,6 @@ import com.google.common.collect.ImmutableList; import net.minecraftforge.srg2source.rangeapplier.MethodData; - /** * Mixin info manager, stores all of the mixin info during processing and also * manages access to the srgs @@ -109,11 +112,21 @@ class AnnotatedMixins implements Messager, ITokenProvider, IOptionProvider, ITyp */ private final List mixinsForPass = new ArrayList(); + /** + * Name of the resource to write remapped refs to + */ + private final String outRefMapFileName; + /** * Target obfuscation environments */ private final List targetEnvironments = new ArrayList(); + /** + * Reference mapper for reference mapping + */ + private final ReferenceMapper refMapper = new ReferenceMapper(); + /** * Rule validators */ @@ -142,12 +155,13 @@ class AnnotatedMixins implements Messager, ITokenProvider, IOptionProvider, ITyp this.env = this.detectEnvironment(processingEnv); this.processingEnv = processingEnv; this.targets = this.initTargetMap(); + this.outRefMapFileName = this.getOption(SupportedOptions.OUT_REFMAP_FILE); this.printMessage(Kind.NOTE, "SpongePowered Mixin Annotation Processor v" + MixinBootstrap.VERSION); for (ObfuscationType obfType : ObfuscationType.values()) { if (obfType.isSupported(this)) { - this.targetEnvironments.add(new TargetObfuscationEnvironment(obfType, this, this, this)); + this.targetEnvironments.add(new TargetObfuscationEnvironment(obfType, this, this, this, this.refMapper)); } } @@ -248,11 +262,44 @@ class AnnotatedMixins implements Messager, ITokenProvider, IOptionProvider, ITyp * Write out stored mappings */ public void writeRefs() { - for (TargetObfuscationEnvironment targetEnv : this.targetEnvironments) { - targetEnv.writeRefs(this.processingEnv.getFiler()); + if (this.outRefMapFileName == null) { + return; + } + + PrintWriter writer = null; + + try { + writer = this.openFileWriter(this.outRefMapFileName, "refmap"); + this.refMapper.write(writer); + } catch (IOException ex) { + ex.printStackTrace(); + } finally { + if (writer != null) { + try { + writer.close(); + } catch (Exception ex) { + // oh well + } + } } } - + + /** + * Open a writer for an output file + */ + private PrintWriter openFileWriter(String fileName, String description) throws IOException { + if (fileName.matches("^.*[\\\\/:].*$")) { + File outSrgFile = new File(fileName); + outSrgFile.getParentFile().mkdirs(); + this.printMessage(Kind.NOTE, "Writing " + description + " to " + outSrgFile.getAbsolutePath()); + return new PrintWriter(outSrgFile); + } + + FileObject outSrg = this.processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", fileName); + this.printMessage(Kind.NOTE, "Writing " + description + " to " + new File(outSrg.toUri()).getAbsolutePath()); + return new PrintWriter(outSrg.openWriter()); + } + public ObfuscationData getObfMethod(MemberInfo method) { ObfuscationData data = new ObfuscationData(); @@ -306,7 +353,7 @@ class AnnotatedMixins implements Messager, ITokenProvider, IOptionProvider, ITyp String obfName = obfMethod.name.substring(slashPos + 1); String obfOwner = obfMethod.name.substring(0, slashPos); MemberInfo remappedReference = new MemberInfo(obfName, obfOwner, obfMethod.sig, false); - targetEnv.getReferenceMapper().addMapping(className, reference, remappedReference.toString()); + targetEnv.addMapping(className, reference, remappedReference.toString()); } } } @@ -316,7 +363,7 @@ class AnnotatedMixins implements Messager, ITokenProvider, IOptionProvider, ITyp MethodData obfMethod = obfMethodData.get(targetEnv.getType()); if (obfMethod != null) { MemberInfo remappedReference = context.remapUsing(obfMethod, false); - targetEnv.getReferenceMapper().addMapping(className, reference, remappedReference.toString()); + targetEnv.addMapping(className, reference, remappedReference.toString()); } } } @@ -327,11 +374,18 @@ class AnnotatedMixins implements Messager, ITokenProvider, IOptionProvider, ITyp if (obfField != null) { MemberInfo remappedReference = MemberInfo.fromSrgField(obfField, context.desc); String remapped = remappedReference.toString(); - targetEnv.getReferenceMapper().addMapping(className, reference, remapped); + targetEnv.addMapping(className, reference, remapped); } } } - + + /** + * Get the reference mapper + */ + public ReferenceMapper getReferenceMapper() { + return this.refMapper; + } + /** * Clear all registered mixins */ diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationType.java b/src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationType.java index 583802d6..19b2d494 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationType.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationType.java @@ -26,28 +26,26 @@ package org.spongepowered.tools.obfuscation; enum ObfuscationType { - SRG("searge", SupportedOptions.REOBF_SRG_FILE, SupportedOptions.OUT_SRG_SRG_FILE, SupportedOptions.OUT_SRG_REFMAP_FILE), - NOTCH("notch", SupportedOptions.REOBF_NOTCH_FILE, SupportedOptions.OUT_NOTCH_SRG_FILE, SupportedOptions.OUT_NOTCH_REFMAP_FILE); + SRG("searge", SupportedOptions.REOBF_SRG_FILE, SupportedOptions.OUT_SRG_SRG_FILE), + NOTCH("notch", SupportedOptions.REOBF_NOTCH_FILE, SupportedOptions.OUT_NOTCH_SRG_FILE); - private final String displayName; + private final String key; private final String srgFileArgName; private final String outSrgFileArgName; - private final String refmapArgName; - private ObfuscationType(String displayName, String srgFileArgName, String outSrgFileArgName, String refmapArgName) { - this.displayName = displayName; + private ObfuscationType(String displayName, String srgFileArgName, String outSrgFileArgName) { + this.key = displayName; this.srgFileArgName = srgFileArgName; this.outSrgFileArgName = outSrgFileArgName; - this.refmapArgName = refmapArgName; } @Override public String toString() { - return this.displayName; + return this.key; } - public String getDisplayName() { - return this.displayName; + public String getKey() { + return this.key; } public String getSrgFileOption() { @@ -58,8 +56,9 @@ enum ObfuscationType { return this.outSrgFileArgName; } - public String getRefMapFileOption() { - return this.refmapArgName; + public boolean isDefault(IOptionProvider options) { + String defaultEnv = options.getOption(SupportedOptions.DEFAULT_OBFUSCATION_ENV); + return (defaultEnv == null && this == ObfuscationType.SRG) || (defaultEnv != null && this.key.equals(defaultEnv.toLowerCase())); } public boolean isSupported(IOptionProvider options) { @@ -73,9 +72,5 @@ enum ObfuscationType { public String getOutputSrgFileName(IOptionProvider options) { return options.getOption(this.outSrgFileArgName); } - - public String getRefMapFileName(IOptionProvider options) { - return options.getOption(this.refmapArgName); - } - + } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/SupportedOptions.java b/src/ap/java/org/spongepowered/tools/obfuscation/SupportedOptions.java index ef63f177..7ca77842 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/SupportedOptions.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/SupportedOptions.java @@ -34,20 +34,20 @@ public final class SupportedOptions { public static final String REOBF_NOTCH_FILE = "reobfNotchSrgFile"; public static final String OUT_SRG_SRG_FILE = "outSrgFile"; public static final String OUT_NOTCH_SRG_FILE = "outNotchSrgFile"; - public static final String OUT_SRG_REFMAP_FILE = "outRefMapFile"; - public static final String OUT_NOTCH_REFMAP_FILE = "outNotchRefMapFile"; + public static final String OUT_REFMAP_FILE = "outRefMapFile"; public static final String DISABLE_TARGET_VALIDATOR = "disableTargetValidator"; public static final String DISABLE_TARGET_EXPORT = "disableTargetExport"; + public static final String DEFAULT_OBFUSCATION_ENV = "defaultObfuscationEnv"; public static final Set all = ImmutableSet.of( SupportedOptions.REOBF_SRG_FILE, SupportedOptions.REOBF_NOTCH_FILE, SupportedOptions.OUT_SRG_SRG_FILE, SupportedOptions.OUT_NOTCH_SRG_FILE, - SupportedOptions.OUT_SRG_REFMAP_FILE, - SupportedOptions.OUT_NOTCH_REFMAP_FILE, + SupportedOptions.OUT_REFMAP_FILE, SupportedOptions.DISABLE_TARGET_VALIDATOR, - SupportedOptions.DISABLE_TARGET_EXPORT + SupportedOptions.DISABLE_TARGET_EXPORT, + SupportedOptions.DEFAULT_OBFUSCATION_ENV ); private SupportedOptions() {} diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/TargetObfuscationEnvironment.java b/src/ap/java/org/spongepowered/tools/obfuscation/TargetObfuscationEnvironment.java index f9b06fc4..231bb344 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/TargetObfuscationEnvironment.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/TargetObfuscationEnvironment.java @@ -67,18 +67,13 @@ class TargetObfuscationEnvironment { /** * Options */ -// private final IOptionProvider options; + private final IOptionProvider options; /** * Name of the resource to write generated srgs to */ private final String outSrgFileName; - /** - * Name of the resource to write remapped refs to - */ - private final String outRefMapFileName; - /** * File containing the reobfd srgs */ @@ -87,7 +82,7 @@ class TargetObfuscationEnvironment { /** * Reference mapper for reference mapping */ - private final ReferenceMapper refMapper = new ReferenceMapper(); + private final ReferenceMapper refMapper; /** * SRG container for mcp->? mappings @@ -100,15 +95,16 @@ class TargetObfuscationEnvironment { */ private boolean initDone; - public TargetObfuscationEnvironment(ObfuscationType type, Messager messager, ITypeHandleProvider typeProvider, IOptionProvider options) { + public TargetObfuscationEnvironment(ObfuscationType type, Messager messager, ITypeHandleProvider typeProvider, IOptionProvider options, + ReferenceMapper refMapper) { this.type = type; this.messager = messager; this.typeProvider = typeProvider; -// this.options = options; + this.options = options; + this.refMapper = refMapper; this.reobfSrgFileName = type.getSrgFileName(options); this.outSrgFileName = type.getOutputSrgFileName(options); - this.outRefMapFileName = type.getRefMapFileName(options); } private boolean initSrgs() { @@ -134,7 +130,21 @@ class TargetObfuscationEnvironment { return this.srgs != null; } - + + /** + * Get the type + */ + public ObfuscationType getType() { + return this.type; + } + + public void addMapping(String className, String reference, String newReference) { + this.refMapper.addMapping(this.type.getKey(), className, reference, newReference); + if (this.type.isDefault(this.options)) { + this.refMapper.addMapping(null, className, reference, newReference); + } + } + /** * Get an obfuscation mapping for a method */ @@ -219,33 +229,7 @@ class TargetObfuscationEnvironment { } } } - - /** - * Write out stored mappings - */ - public void writeRefs(Filer filer) { - if (this.outRefMapFileName == null) { - return; - } - - PrintWriter writer = null; - - try { - writer = this.openFileWriter(filer, this.outRefMapFileName, this.type + "refmap"); - this.refMapper.write(writer); - } catch (IOException ex) { - ex.printStackTrace(); - } finally { - if (writer != null) { - try { - writer.close(); - } catch (Exception ex) { - // oh well - } - } - } - } - + /** * Open a writer for an output file */ @@ -262,18 +246,4 @@ class TargetObfuscationEnvironment { return new PrintWriter(outSrg.openWriter()); } - /** - * Get the type - */ - public ObfuscationType getType() { - return this.type; - } - - /** - * Get the reference mapper - */ - public ReferenceMapper getReferenceMapper() { - return this.refMapper; - } - } diff --git a/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java b/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java index 6973e578..71600fbc 100644 --- a/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java +++ b/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java @@ -585,6 +585,11 @@ public class MixinEnvironment implements ITokenProvider { */ private IClassNameTransformer nameTransformer; + /** + * Obfuscation context (refmap key to use in this environment) + */ + private String obfuscationContext = null; + MixinEnvironment(Phase phase) { this.phase = phase; this.configsKey = MixinEnvironment.CONFIGS_KEY + "." + this.phase.name.toLowerCase(); @@ -847,6 +852,19 @@ public class MixinEnvironment implements ITokenProvider { return option.getStringValue(); } + /** + * Set the obfuscation context + * + * @param context + */ + public void setObfuscationContext(String context) { + this.obfuscationContext = context; + } + + public String getObfuscationContext() { + return this.obfuscationContext; + } + /** * Invoke a mixin environment audit process */ diff --git a/src/main/java/org/spongepowered/asm/mixin/extensibility/IMixinConfig.java b/src/main/java/org/spongepowered/asm/mixin/extensibility/IMixinConfig.java index d14f49ad..0e39783c 100644 --- a/src/main/java/org/spongepowered/asm/mixin/extensibility/IMixinConfig.java +++ b/src/main/java/org/spongepowered/asm/mixin/extensibility/IMixinConfig.java @@ -26,6 +26,8 @@ package org.spongepowered.asm.mixin.extensibility; import java.util.Set; +import org.spongepowered.asm.mixin.MixinEnvironment; + /** * Interface for loaded mixin configurations */ @@ -35,6 +37,13 @@ public interface IMixinConfig { * Default priority for mixin configs and mixins */ public static final int DEFAULT_PRIORITY = 1000; + + /** + * Get the parent environment of this config + * + * @return the parent environment + */ + public abstract MixinEnvironment getEnvironment(); /** * Get the name of the file from which this configuration object was 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 d5c3f40e..206aa467 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 @@ -433,7 +433,7 @@ public class MemberInfo { * @param mixinClass Mixin class to use for remapping * @return parsed MemberInfo */ - public static MemberInfo parse(String name, ReferenceMapper refMapper, String mixinClass) { + private static MemberInfo parse(String name, ReferenceMapper refMapper, String mixinClass) { String desc = null; String owner = null; diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/struct/ReferenceMapper.java b/src/main/java/org/spongepowered/asm/mixin/injection/struct/ReferenceMapper.java index f2c5122a..47c5e3ad 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/struct/ReferenceMapper.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/struct/ReferenceMapper.java @@ -33,35 +33,58 @@ import java.util.Map; import net.minecraft.launchwrapper.Launch; +import com.google.common.collect.Maps; import com.google.gson.Gson; import com.google.gson.GsonBuilder; public final class ReferenceMapper implements Serializable { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 2L; public static final String DEFAULT_RESOURCE = "mixin.refmap.json"; - - private final Map> mappings; + + private final Map> mappings = Maps.newHashMap(); + + private final Map>> data = Maps.newHashMap(); + + private transient String context = null; public ReferenceMapper() { - this(new HashMap>()); } - - public ReferenceMapper(Map> mappings) { - this.mappings = mappings; + + public String getContext() { + return this.context; + } + + public void setContext(String context) { + this.context = context; } public String remap(String className, String reference) { + return this.remapWithContext(this.context, className, reference); + } + + public String remapWithContext(String context, String className, String reference) { + Map> mappings = this.mappings; + if (context != null) { + mappings = this.data.get(context); + if (mappings == null) { + mappings = this.mappings; + } + } + return this.remap(mappings, className, reference); + } + + private String remap(Map> mappings, String className, String reference) { if (className == null) { - for (Map mappings : this.mappings.values()) { - if (mappings.containsKey(reference)) { - return mappings.get(reference); + for (Map mapping : mappings.values()) { + if (mapping.containsKey(reference)) { + return mapping.get(reference); } } } - Map classMappings = this.mappings.get(className); + Map classMappings = mappings.get(className); if (classMappings == null) { return reference; } @@ -69,11 +92,19 @@ public final class ReferenceMapper implements Serializable { return remappedReference != null ? remappedReference : reference; } - public void addMapping(String className, String reference, String newReference) { - Map classMappings = this.mappings.get(className); + public void addMapping(String context, String className, String reference, String newReference) { + Map> mappings = this.mappings; + if (context != null) { + mappings = this.data.get(context); + if (mappings == null) { + mappings = Maps.newHashMap(); + this.data.put(context, mappings); + } + } + Map classMappings = mappings.get(className); if (classMappings == null) { classMappings = new HashMap(); - this.mappings.put(className, classMappings); + mappings.put(className, classMappings); } classMappings.put(reference, newReference); } 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 2138bf48..f4a833fa 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java @@ -173,6 +173,11 @@ class MixinConfig implements Comparable, IMixinConfig { */ private final transient int order = MixinConfig.configOrder++; + /** + * Parent environment + */ + private transient MixinEnvironment env; + /** * Name of the file this config was initialised from */ @@ -198,7 +203,7 @@ class MixinConfig implements Comparable, IMixinConfig { * Keep track of initialisation state */ private transient boolean initialised = false; - + /** * Spawn via GSON, no public ctor for you */ @@ -207,16 +212,18 @@ class MixinConfig implements Comparable, IMixinConfig { /** * Called immediately after deserialisation * + * @param env Parent environment * @param name Mixin config name * @return true if the config was successfully initialised and should be * returned, or false if initialisation failed and the config should * be discarded */ - private boolean onLoad(String name) { + private boolean onLoad(MixinEnvironment env, String name) { + this.env = env; this.name = name; VersionNumber minVersion = VersionNumber.parse(this.version); - VersionNumber curVersion = VersionNumber.parse(MixinEnvironment.getCurrentEnvironment().getVersion()); + VersionNumber curVersion = VersionNumber.parse(env.getVersion()); if (minVersion.compareTo(curVersion) > 0) { this.logger.warn("Mixin config {} requires mixin subsystem version {} but {} was found. The mixin config will not be applied.", this.name, minVersion, curVersion); @@ -257,7 +264,7 @@ class MixinConfig implements Comparable, IMixinConfig { } this.refMapper = ReferenceMapper.read(this.refMapperConfig); - this.verboseLogging |= MixinEnvironment.getCurrentEnvironment().getOption(Option.DEBUG_VERBOSE); + this.verboseLogging |= env.getOption(Option.DEBUG_VERBOSE); return true; } @@ -283,7 +290,7 @@ class MixinConfig implements Comparable, IMixinConfig { this.initialiseMixins(this.mixinClasses, false, hotSwapper); - switch (MixinEnvironment.getCurrentEnvironment().getSide()) { + switch (this.env.getSide()) { case CLIENT: this.initialiseMixins(this.mixinClassesClient, false, hotSwapper); break; @@ -372,7 +379,16 @@ class MixinConfig implements Comparable, IMixinConfig { public boolean isRequired() { return this.required; } - + + /* (non-Javadoc) + * @see org.spongepowered.asm.mixin.extensibility.IMixinConfig + * #getEnvironment() + */ + @Override + public MixinEnvironment getEnvironment() { + return this.env; + } + /* (non-Javadoc) * @see org.spongepowered.asm.mixin.transformer.IMixinConfig#getName() */ @@ -424,6 +440,7 @@ class MixinConfig implements Comparable, IMixinConfig { * Get the reference remapper for injectors */ public ReferenceMapper getReferenceMapper() { + this.refMapper.setContext(this.env.getObfuscationContext()); return this.refMapper; } @@ -551,12 +568,13 @@ class MixinConfig implements Comparable, IMixinConfig { * specified configFile, which must be accessible on the classpath * * @param configFile + * @param env parent environment * @return */ - static MixinConfig create(String configFile) { + static MixinConfig create(String configFile, MixinEnvironment env) { try { MixinConfig config = new Gson().fromJson(new InputStreamReader(Launch.classLoader.getResourceAsStream(configFile)), MixinConfig.class); - if (config.onLoad(configFile)) { + if (config.onLoad(env, configFile)) { return config; } return null; diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java index 14bdbf6b..874e45ac 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java @@ -492,7 +492,7 @@ public class MixinTransformer extends TreeTransformer { if (configs != null) { for (String configFile : configs) { try { - MixinConfig config = MixinConfig.create(configFile); + MixinConfig config = MixinConfig.create(configFile, environment); if (config != null) { this.logger.log(this.verboseLoggingLevel, "Adding mixin config {}", config); this.pendingConfigs.add(config);