From 03744c86a097daba18b16e9ee5d347f7eb1ad473 Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Fri, 4 Dec 2015 01:45:43 +0000 Subject: [PATCH] AP support for gradle plugin, version bump to 0.4.8 --- gradle.properties | 2 +- .../tools/obfuscation/AnnotatedMixin.java | 87 +++--- .../tools/obfuscation/AnnotatedMixins.java | 269 ++++++----------- .../obfuscation/ITypeHandleProvider.java | 31 ++ .../tools/obfuscation/MixinProcessor.java | 8 +- .../tools/obfuscation/ObfuscationData.java | 52 ++++ .../tools/obfuscation/ObfuscationType.java | 81 +++++ .../tools/obfuscation/SupportedOptions.java | 55 ++++ .../TargetObfuscationEnvironment.java | 279 ++++++++++++++++++ .../validation/TargetValidator.java | 3 +- .../asm/launch/MixinBootstrap.java | 2 +- 11 files changed, 635 insertions(+), 234 deletions(-) create mode 100644 src/ap/java/org/spongepowered/tools/obfuscation/ITypeHandleProvider.java create mode 100644 src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationData.java create mode 100644 src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationType.java create mode 100644 src/ap/java/org/spongepowered/tools/obfuscation/SupportedOptions.java create mode 100644 src/ap/java/org/spongepowered/tools/obfuscation/TargetObfuscationEnvironment.java diff --git a/gradle.properties b/gradle.properties index 699cd8cc..71e0f17d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,5 +4,5 @@ packaging=jar description=Mixin url=https://www.spongepowered.org organization=SpongePowered -buildVersion=0.4.7 +buildVersion=0.4.8 buildType=SNAPSHOT diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java index 3083d2d4..a90eb314 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java @@ -259,12 +259,12 @@ class AnnotatedMixin { /** * Stored (ordered) field mappings */ - private final Set fieldMappings = new LinkedHashSet(); + private final Map> fieldMappings = new HashMap>(); /** * Stored (ordered) method mappings */ - private final Set methodMappings = new LinkedHashSet(); + private final Map> methodMappings = new HashMap>(); public AnnotatedMixin(AnnotatedMixins mixins, TypeElement type) { @@ -284,6 +284,11 @@ class AnnotatedMixin { } this.remap = AnnotatedMixins.getRemapValue(this.annotation) && this.targets.size() > 0; + + for (ObfuscationType obfType : ObfuscationType.values()) { + this.fieldMappings.put(obfType, new LinkedHashSet()); + this.methodMappings.put(obfType, new LinkedHashSet()); + } } AnnotatedMixin runValidators(ValidationPass pass, Collection validators) { @@ -389,15 +394,15 @@ class AnnotatedMixin { /** * Get stored field mappings */ - public Set getFieldMappings() { - return this.fieldMappings; + public Set getFieldMappings(ObfuscationType type) { + return this.fieldMappings.get(type); } /** * Get stored method mappings */ - public Set getMethodMappings() { - return this.methodMappings; + public Set getMethodMappings(ObfuscationType type) { + return this.methodMappings.get(type); } /** @@ -437,9 +442,9 @@ class AnnotatedMixin { String mcpName = method.getSimpleName().toString(); String mcpSignature = MirrorUtils.generateSignature(method); - MethodData obfMethod = this.mixins.getObfMethod(new MethodData(this.targetRef + "/" + mcpName, mcpSignature)); + ObfuscationData obfMethodData = this.mixins.getObfMethod(new MethodData(this.targetRef + "/" + mcpName, mcpSignature)); - if (obfMethod == null) { + if (obfMethodData.isEmpty()) { Kind error = Kind.ERROR; try { @@ -455,9 +460,12 @@ class AnnotatedMixin { this.mixins.printMessage(error, "No obfuscation mapping for @Overwrite method", method); return; } - - String obfName = obfMethod.name.substring(obfMethod.name.lastIndexOf('/') + 1); - this.addMethodMapping(mcpName, obfName, mcpSignature, obfMethod.sig); + + for (ObfuscationType type : obfMethodData) { + MethodData obfMethod = obfMethodData.get(type); + String obfName = obfMethod.name.substring(obfMethod.name.lastIndexOf('/') + 1); + this.addMethodMapping(type, mcpName, obfName, mcpSignature, obfMethod.sig); + } } /** @@ -471,14 +479,17 @@ class AnnotatedMixin { return; } - String obfField = this.mixins.getObfField(this.targetRef + "/" + name); + ObfuscationData obfFieldData = this.mixins.getObfField(this.targetRef + "/" + name); - if (obfField == null) { + if (obfFieldData.isEmpty()) { this.mixins.printMessage(Kind.WARNING, "Unable to locate obfuscation mapping for @Shadow field", field, shadow); return; } - this.addFieldMapping(name.setObfuscatedName(obfField)); + for (ObfuscationType type : obfFieldData) { + String fieldName = obfFieldData.get(type); + this.addFieldMapping(type, name.setObfuscatedName(fieldName)); + } } /** @@ -493,14 +504,17 @@ class AnnotatedMixin { } String mcpSignature = MirrorUtils.generateSignature(method); - MethodData obfMethod = this.mixins.getObfMethod(new MethodData(this.targetRef + "/" + name, mcpSignature)); + ObfuscationData obfMethodData = this.mixins.getObfMethod(new MethodData(this.targetRef + "/" + name, mcpSignature)); - if (obfMethod == null) { + if (obfMethodData.isEmpty()) { this.mixins.printMessage(Kind.WARNING, "Unable to locate obfuscation mapping for @Shadow method", method, shadow); return; } - this.addMethodMapping(name.setObfuscatedName(obfMethod.name), mcpSignature, obfMethod.sig); + for (ObfuscationType type : obfMethodData) { + MethodData obfMethod = obfMethodData.get(type); + this.addMethodMapping(type, name.setObfuscatedName(obfMethod.name), mcpSignature, obfMethod.sig); + } } /** @@ -544,18 +558,13 @@ class AnnotatedMixin { return null; } - MethodData obfMethod = this.mixins.getObfMethod(new MethodData(this.targetRef + "/" + targetMember.name, desc)); - if (obfMethod == null) { + ObfuscationData obfMethod = this.mixins.getObfMethod(new MethodData(this.targetRef + "/" + targetMember.name, desc)); + if (obfMethod.isEmpty()) { Kind error = Constants.INIT.equals(targetMember.name) ? Kind.WARNING : Kind.ERROR; return new Message(error, "No obfuscation mapping for " + type + " target " + targetMember.name, method, inject); } - int slashPos = obfMethod.name.lastIndexOf('/'); - String obfName = obfMethod.name.substring(slashPos + 1); - String obfOwner = obfMethod.name.substring(0, slashPos); - MemberInfo remappedReference = new MemberInfo(obfName, obfOwner, obfMethod.sig, false); - - this.mixins.getReferenceMapper().addMapping(this.classRef, originalReference, remappedReference.toString()); + this.mixins.addMethodMapping(this.classRef, originalReference, obfMethod); return null; } @@ -599,50 +608,46 @@ class AnnotatedMixin { this.mixins.printMessage(Kind.ERROR, ex.getMessage(), element, inject); } - MemberInfo remappedReference = null; if (targetMember.isField()) { - String obfField = this.mixins.getObfField(targetMember.toSrg()); - if (obfField == null) { + ObfuscationData obfFieldData = this.mixins.getObfField(targetMember.toSrg()); + if (obfFieldData.isEmpty()) { this.mixins.printMessage(Kind.WARNING, "Cannot find field mapping for @At(" + key + ") '" + target + "'", element, inject); return false; } - remappedReference = MemberInfo.fromSrgField(obfField, targetMember.desc); + this.mixins.addFieldMapping(this.classRef, target, targetMember, obfFieldData); } else { - MethodData obfMethod = this.mixins.getObfMethod(targetMember); - if (obfMethod == null) { + ObfuscationData obfMethodData = this.mixins.getObfMethod(targetMember); + if (obfMethodData.isEmpty()) { if (targetMember.owner == null || !targetMember.owner.startsWith("java/lang/")) { this.mixins.printMessage(Kind.WARNING, "Cannot find method mapping for @At(" + key + ") '" + target + "'", element, inject); } - return false; } - remappedReference = targetMember.remapUsing(obfMethod, false); + this.mixins.addMethodMapping(this.classRef, target, targetMember, obfMethodData); } - - this.mixins.getReferenceMapper().addMapping(this.classRef, target, remappedReference.toString()); return true; } /** * Add a field mapping to the local table */ - private void addFieldMapping(ShadowElementName name) { + private void addFieldMapping(ObfuscationType type, ShadowElementName name) { String mapping = String.format("FD: %s %s", this.classRef + "/" + name.name(), this.classRef + "/" + name.obfuscated()); - this.fieldMappings.add(mapping); + this.fieldMappings.get(type).add(mapping); } /** * Add a method mapping to the local table */ - private void addMethodMapping(ShadowElementName name, String mcpSignature, String obfSignature) { - this.addMethodMapping(name.name(), name.obfuscated(), mcpSignature, obfSignature); + private void addMethodMapping(ObfuscationType type, ShadowElementName name, String mcpSignature, String obfSignature) { + this.addMethodMapping(type, name.name(), name.obfuscated(), mcpSignature, obfSignature); } /** * Add a method mapping to the local table */ - private void addMethodMapping(String mcpName, String obfName, String mcpSignature, String obfSignature) { + private void addMethodMapping(ObfuscationType type, String mcpName, String obfName, String mcpSignature, String obfSignature) { String mapping = String.format("MD: %s %s %s %s", this.classRef + "/" + mcpName, mcpSignature, this.classRef + "/" + obfName, obfSignature); - this.methodMappings.add(mapping); + this.methodMappings.get(type).add(mapping); } /** diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java index febeab46..156d24f9 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java @@ -24,18 +24,13 @@ */ 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; -import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Properties; -import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -50,7 +45,6 @@ import javax.lang.model.element.PackageElement; import javax.lang.model.element.TypeElement; import javax.lang.model.element.VariableElement; import javax.lang.model.type.DeclaredType; -import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeMirror; import javax.lang.model.util.Elements; import javax.tools.Diagnostic; @@ -61,7 +55,6 @@ 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; @@ -72,14 +65,13 @@ import org.spongepowered.tools.obfuscation.validation.TargetValidator; import com.google.common.collect.ImmutableList; import net.minecraftforge.srg2source.rangeapplier.MethodData; -import net.minecraftforge.srg2source.rangeapplier.SrgContainer; /** * Mixin info manager, stores all of the mixin info during processing and also * manages access to the srgs */ -class AnnotatedMixins implements Messager, ITokenProvider, IOptionProvider { +class AnnotatedMixins implements Messager, ITokenProvider, IOptionProvider, ITypeHandleProvider { private static final String MAPID_SYSTEM_PROPERTY = "mixin.target.mapid"; @@ -118,24 +110,9 @@ class AnnotatedMixins implements Messager, ITokenProvider, IOptionProvider { private final List mixinsForPass = new ArrayList(); /** - * Name of the resource to write generated srgs to + * Target obfuscation environments */ - private final String outSrgFileName; - - /** - * Name of the resource to write remapped refs to - */ - private final String outRefMapFileName; - - /** - * File containing the reobfd srgs - */ - private final String reobfSrgFileName; - - /** - * Reference mapper for reference mapping - */ - private final ReferenceMapper refMapper = new ReferenceMapper(); + private final List targetEnvironments = new ArrayList(); /** * Rule validators @@ -152,17 +129,6 @@ class AnnotatedMixins implements Messager, ITokenProvider, IOptionProvider { */ private final TargetMap targets; - /** - * SRG container for mcp->srg mappings - */ - private SrgContainer srgs; - - /** - * True once we've tried to initialise the srgs, initially false so that we - * can do srg init lazily - */ - private boolean initDone; - /** * Properties file used to specify options when AP options cannot be * configured via the build script (eg. when using AP with MCP) @@ -178,10 +144,12 @@ class AnnotatedMixins implements Messager, ITokenProvider, IOptionProvider { this.targets = this.initTargetMap(); this.printMessage(Kind.NOTE, "SpongePowered Mixin Annotation Processor v" + MixinBootstrap.VERSION); - - this.reobfSrgFileName = this.getOption("reobfSrgFile"); - this.outSrgFileName = this.getOption("outSrgFile"); - this.outRefMapFileName = this.getOption("outRefMapFile"); + + for (ObfuscationType obfType : ObfuscationType.values()) { + if (obfType.isSupported(this)) { + this.targetEnvironments.add(new TargetObfuscationEnvironment(obfType, this, this, this)); + } + } this.validators = ImmutableList.of( new ParentValidator(processingEnv, this, this), @@ -267,123 +235,103 @@ class AnnotatedMixins implements Messager, ITokenProvider, IOptionProvider { return CompilerEnvironment.JAVAC; } - /** - * Lazy initialisation for srgs, so that we only initialise the srgs if - * they're actually required. - */ - private boolean initSrgs() { - if (!this.initDone) { - this.initDone = true; - - if (this.reobfSrgFileName == null) { - this.printMessage(Kind.ERROR, "The reobfSrgFile argument was not supplied, obfuscation processing will not occur"); - return false; - } - - try { - File reobfSrgFile = new File(this.reobfSrgFileName); - this.printMessage(Kind.NOTE, "Loading SRGs from " + reobfSrgFile.getAbsolutePath()); - this.srgs = new SrgContainer().readSrg(reobfSrgFile); - } catch (Exception ex) { - ex.printStackTrace(); - this.printMessage(Kind.ERROR, "The specified SRG file could not be read, processing cannot continue"); - this.srgs = null; - } - } - - return this.srgs != null; - } - /** * Write out generated srgs */ public void writeSrgs() { - if (this.outSrgFileName == null) { - return; + for (TargetObfuscationEnvironment targetEnv : this.targetEnvironments) { + targetEnv.writeSrgs(this.processingEnv.getFiler(), this.mixins); } - - Set fieldMappings = new LinkedHashSet(); - Set methodMappings = new LinkedHashSet(); - - for (AnnotatedMixin mixin : this.mixins.values()) { - fieldMappings.addAll(mixin.getFieldMappings()); - methodMappings.addAll(mixin.getMethodMappings()); - } - - PrintWriter writer = null; - - try { - writer = this.openFileWriter(this.outSrgFileName, "output SRGs"); - for (String fieldMapping : fieldMappings) { - writer.println(fieldMapping); - } - for (String methodMapping : methodMappings) { - writer.println(methodMapping); - } - } 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); - } - - Filer filer = this.processingEnv.getFiler(); - FileObject outSrg = filer.createResource(StandardLocation.CLASS_OUTPUT, "", fileName); - this.printMessage(Kind.NOTE, "Writing " + description + " to " + new File(outSrg.toUri()).getAbsolutePath()); - return new PrintWriter(outSrg.openWriter()); } /** * Write out stored mappings */ public void writeRefs() { - if (this.outRefMapFileName == null) { - return; + for (TargetObfuscationEnvironment targetEnv : this.targetEnvironments) { + targetEnv.writeRefs(this.processingEnv.getFiler()); } + } + + public ObfuscationData getObfMethod(MemberInfo method) { + ObfuscationData data = new ObfuscationData(); - 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 - } + for (TargetObfuscationEnvironment targetEnv : this.targetEnvironments) { + MethodData obfMethod = targetEnv.getObfMethod(method); + if (obfMethod != null) { + data.add(targetEnv.getType(), obfMethod); } } + return data; } /** - * Get the reference mapper + * Get an obfuscation mapping for a method */ - public ReferenceMapper getReferenceMapper() { - return this.refMapper; + public ObfuscationData getObfMethod(MethodData method) { + ObfuscationData data = new ObfuscationData(); + + for (TargetObfuscationEnvironment targetEnv : this.targetEnvironments) { + MethodData obfMethod = targetEnv.getObfMethod(method); + if (obfMethod != null) { + data.add(targetEnv.getType(), obfMethod); + } + } + + return data; + } + + /** + * Get an obfuscation mapping for a field + */ + public ObfuscationData getObfField(String field) { + ObfuscationData data = new ObfuscationData(); + + for (TargetObfuscationEnvironment targetEnv : this.targetEnvironments) { + String obfField = targetEnv.getObfField(field); + if (obfField != null) { + data.add(targetEnv.getType(), obfField); + } + } + + return data; + } + + void addMethodMapping(String className, String reference, ObfuscationData obfMethodData) { + for (TargetObfuscationEnvironment targetEnv : this.targetEnvironments) { + MethodData obfMethod = obfMethodData.get(targetEnv.getType()); + if (obfMethod != null) { + int slashPos = obfMethod.name.lastIndexOf('/'); + 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()); + } + } } + void addMethodMapping(String className, String reference, MemberInfo context, ObfuscationData obfMethodData) { + for (TargetObfuscationEnvironment targetEnv : this.targetEnvironments) { + MethodData obfMethod = obfMethodData.get(targetEnv.getType()); + if (obfMethod != null) { + MemberInfo remappedReference = context.remapUsing(obfMethod, false); + targetEnv.getReferenceMapper().addMapping(className, reference, remappedReference.toString()); + } + } + } + + void addFieldMapping(String className, String reference, MemberInfo context, ObfuscationData obfFieldData) { + for (TargetObfuscationEnvironment targetEnv : this.targetEnvironments) { + String obfField = obfFieldData.get(targetEnv.getType()); + if (obfField != null) { + MemberInfo remappedReference = MemberInfo.fromSrgField(obfField, context.desc); + String remapped = remappedReference.toString(); + targetEnv.getReferenceMapper().addMapping(className, reference, remapped); + } + } + } + /** * Clear all registered mixins */ @@ -553,7 +501,7 @@ class AnnotatedMixins implements Messager, ITokenProvider, IOptionProvider { * Called from each AP when a pass is completed */ public void onPassCompleted() { - if (!"true".equalsIgnoreCase(this.getOption("disableTargetExport"))) { + if (!"true".equalsIgnoreCase(this.getOption(SupportedOptions.DISABLE_TARGET_EXPORT))) { this.targets.write(true); } @@ -618,6 +566,7 @@ class AnnotatedMixins implements Messager, ITokenProvider, IOptionProvider { * Get a TypeHandle representing another type in the current processing * environment */ + @Override public TypeHandle getTypeHandle(String name) { name = name.replace('/', '.'); @@ -642,52 +591,6 @@ class AnnotatedMixins implements Messager, ITokenProvider, IOptionProvider { return null; } - - /** - * Get an obfuscation mapping for a method - */ - public MethodData getObfMethod(MemberInfo method) { - MethodData obfd = this.getObfMethod(method.asMethodData()); - if (obfd != null || !method.isFullyQualified()) { - return obfd; - } - - // Get a type handle for the declared method owner - TypeHandle type = this.getTypeHandle(method.owner); - if (type.isImaginary()) { - return null; - } - - // See if we can get the superclass from the reference - TypeMirror superClass = type.getElement().getSuperclass(); - if (superClass.getKind() != TypeKind.DECLARED) { - return null; - } - - // Well we found it, let's inflect the class name and recurse the search - String superClassName = ((TypeElement)((DeclaredType)superClass).asElement()).getQualifiedName().toString(); - return this.getObfMethod(new MemberInfo(method.name, superClassName.replace('.', '/'), method.desc, method.matchAll)); - } - - /** - * Get an obfuscation mapping for a method - */ - public MethodData getObfMethod(MethodData method) { - if (this.initSrgs()) { - return this.srgs.methodMap.get(method); - } - return null; - } - - /** - * Get an obfuscation mapping for a field - */ - public String getObfField(String field) { - if (this.initSrgs()) { - return this.srgs.fieldMap.get(field); - } - return null; - } /** * Get the mixin manager instance for this environment diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/ITypeHandleProvider.java b/src/ap/java/org/spongepowered/tools/obfuscation/ITypeHandleProvider.java new file mode 100644 index 00000000..f885afb9 --- /dev/null +++ b/src/ap/java/org/spongepowered/tools/obfuscation/ITypeHandleProvider.java @@ -0,0 +1,31 @@ +/* + * 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.tools.obfuscation; + +public interface ITypeHandleProvider { + + public abstract TypeHandle getTypeHandle(String name); + +} diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/MixinProcessor.java b/src/ap/java/org/spongepowered/tools/obfuscation/MixinProcessor.java index 0184fdc8..77bb75d0 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/MixinProcessor.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/MixinProcessor.java @@ -37,14 +37,8 @@ import javax.tools.Diagnostic.Kind; import org.spongepowered.asm.mixin.Mixin; -import com.google.common.collect.ImmutableSet; - public abstract class MixinProcessor extends AbstractProcessor { - private static final Set supportedOptions = ImmutableSet.of( - "reobfSrgFile", "outSrgFile", "outRefMapFile", "disableTargetValidator" - ); - /** * Mixin info manager */ @@ -92,7 +86,7 @@ public abstract class MixinProcessor extends AbstractProcessor { @Override public Set getSupportedOptions() { - return MixinProcessor.supportedOptions; + return SupportedOptions.all; } } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationData.java b/src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationData.java new file mode 100644 index 00000000..370569d4 --- /dev/null +++ b/src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationData.java @@ -0,0 +1,52 @@ +/* + * 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.tools.obfuscation; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +public class ObfuscationData implements Iterable { + + private final Map data = new HashMap(); + + public void add(ObfuscationType type, T method) { + this.data.put(type, method); + } + + public boolean isEmpty() { + return this.data.isEmpty(); + } + + public T get(ObfuscationType type) { + return this.data.get(type); + } + + @Override + public Iterator iterator() { + return this.data.keySet().iterator(); + } + +} diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationType.java b/src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationType.java new file mode 100644 index 00000000..583802d6 --- /dev/null +++ b/src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationType.java @@ -0,0 +1,81 @@ +/* + * 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.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); + + private final String displayName; + 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; + this.srgFileArgName = srgFileArgName; + this.outSrgFileArgName = outSrgFileArgName; + this.refmapArgName = refmapArgName; + } + + @Override + public String toString() { + return this.displayName; + } + + public String getDisplayName() { + return this.displayName; + } + + public String getSrgFileOption() { + return this.srgFileArgName; + } + + public String getOutputSrgFileOption() { + return this.outSrgFileArgName; + } + + public String getRefMapFileOption() { + return this.refmapArgName; + } + + public boolean isSupported(IOptionProvider options) { + return this.getSrgFileName(options) != null; + } + + public String getSrgFileName(IOptionProvider options) { + return options.getOption(this.srgFileArgName); + } + + 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 new file mode 100644 index 00000000..ef63f177 --- /dev/null +++ b/src/ap/java/org/spongepowered/tools/obfuscation/SupportedOptions.java @@ -0,0 +1,55 @@ +/* + * 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.tools.obfuscation; + +import java.util.Set; + +import com.google.common.collect.ImmutableSet; + +public final class SupportedOptions { + + public static final String REOBF_SRG_FILE = "reobfSrgFile"; + 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 DISABLE_TARGET_VALIDATOR = "disableTargetValidator"; + public static final String DISABLE_TARGET_EXPORT = "disableTargetExport"; + + 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.DISABLE_TARGET_VALIDATOR, + SupportedOptions.DISABLE_TARGET_EXPORT + ); + + private SupportedOptions() {} + +} diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/TargetObfuscationEnvironment.java b/src/ap/java/org/spongepowered/tools/obfuscation/TargetObfuscationEnvironment.java new file mode 100644 index 00000000..f9b06fc4 --- /dev/null +++ b/src/ap/java/org/spongepowered/tools/obfuscation/TargetObfuscationEnvironment.java @@ -0,0 +1,279 @@ +/* + * 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.tools.obfuscation; + +import java.io.File; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.LinkedHashSet; +import java.util.Map; +import java.util.Set; + +import javax.annotation.processing.Filer; +import javax.annotation.processing.Messager; +import javax.lang.model.element.TypeElement; +import javax.lang.model.type.DeclaredType; +import javax.lang.model.type.TypeKind; +import javax.lang.model.type.TypeMirror; +import javax.tools.FileObject; +import javax.tools.StandardLocation; +import javax.tools.Diagnostic.Kind; + +import org.spongepowered.asm.mixin.injection.struct.MemberInfo; +import org.spongepowered.asm.mixin.injection.struct.ReferenceMapper; + +import net.minecraftforge.srg2source.rangeapplier.MethodData; +import net.minecraftforge.srg2source.rangeapplier.SrgContainer; + +class TargetObfuscationEnvironment { + + /** + * Type + */ + private final ObfuscationType type; + + /** + * Messager + */ + private final Messager messager; + + /** + * Type handle provider + */ + private final ITypeHandleProvider typeProvider; + + /** + * 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 + */ + private final String reobfSrgFileName; + + /** + * Reference mapper for reference mapping + */ + private final ReferenceMapper refMapper = new ReferenceMapper(); + + /** + * SRG container for mcp->? mappings + */ + private SrgContainer srgs; + + /** + * True once we've tried to initialise the srgs, initially false so that we + * can do srg init lazily + */ + private boolean initDone; + + public TargetObfuscationEnvironment(ObfuscationType type, Messager messager, ITypeHandleProvider typeProvider, IOptionProvider options) { + this.type = type; + this.messager = messager; + this.typeProvider = typeProvider; +// this.options = options; + + this.reobfSrgFileName = type.getSrgFileName(options); + this.outSrgFileName = type.getOutputSrgFileName(options); + this.outRefMapFileName = type.getRefMapFileName(options); + } + + private boolean initSrgs() { + if (!this.initDone) { + this.initDone = true; + + if (this.reobfSrgFileName == null) { + this.messager.printMessage(Kind.ERROR, "The " + this.type.getSrgFileOption() + + " argument was not supplied, obfuscation processing will not occur"); + return false; + } + + try { + File reobfSrgFile = new File(this.reobfSrgFileName); + this.messager.printMessage(Kind.NOTE, "Loading " + this.type + " mappings from " + reobfSrgFile.getAbsolutePath()); + this.srgs = new SrgContainer().readSrg(reobfSrgFile); + } catch (Exception ex) { + ex.printStackTrace(); + this.messager.printMessage(Kind.ERROR, "The specified " + this.type + " SRG file could not be read, processing cannot continue"); + this.srgs = null; + } + } + + return this.srgs != null; + } + + /** + * Get an obfuscation mapping for a method + */ + public MethodData getObfMethod(MemberInfo method) { + MethodData obfd = this.getObfMethod(method.asMethodData()); + if (obfd != null || !method.isFullyQualified()) { + return obfd; + } + + // Get a type handle for the declared method owner + TypeHandle type = this.typeProvider.getTypeHandle(method.owner); + if (type.isImaginary()) { + return null; + } + + // See if we can get the superclass from the reference + TypeMirror superClass = type.getElement().getSuperclass(); + if (superClass.getKind() != TypeKind.DECLARED) { + return null; + } + + // Well we found it, let's inflect the class name and recurse the search + String superClassName = ((TypeElement)((DeclaredType)superClass).asElement()).getQualifiedName().toString(); + return this.getObfMethod(new MemberInfo(method.name, superClassName.replace('.', '/'), method.desc, method.matchAll)); + } + + /** + * Get an obfuscation mapping for a method + */ + public MethodData getObfMethod(MethodData method) { + if (this.initSrgs()) { + return this.srgs.methodMap.get(method); + } + return null; + } + + /** + * Get an obfuscation mapping for a field + */ + public String getObfField(String field) { + if (this.initSrgs()) { + return this.srgs.fieldMap.get(field); + } + return null; + } + + /** + * Write out generated srgs + */ + public void writeSrgs(Filer filer, Map mixins) { + if (this.outSrgFileName == null) { + return; + } + + Set fieldMappings = new LinkedHashSet(); + Set methodMappings = new LinkedHashSet(); + + for (AnnotatedMixin mixin : mixins.values()) { + fieldMappings.addAll(mixin.getFieldMappings(this.type)); + methodMappings.addAll(mixin.getMethodMappings(this.type)); + } + + PrintWriter writer = null; + + try { + writer = this.openFileWriter(filer, this.outSrgFileName, this.type + " output SRGs"); + for (String fieldMapping : fieldMappings) { + writer.println(fieldMapping); + } + for (String methodMapping : methodMappings) { + writer.println(methodMapping); + } + } catch (IOException ex) { + ex.printStackTrace(); + } finally { + if (writer != null) { + try { + writer.close(); + } catch (Exception ex) { + // oh well + } + } + } + } + + /** + * 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 + */ + private PrintWriter openFileWriter(Filer filer, String fileName, String description) throws IOException { + if (fileName.matches("^.*[\\\\/:].*$")) { + File outSrgFile = new File(fileName); + outSrgFile.getParentFile().mkdirs(); + this.messager.printMessage(Kind.NOTE, "Writing " + description + " to " + outSrgFile.getAbsolutePath()); + return new PrintWriter(outSrgFile); + } + + FileObject outSrg = filer.createResource(StandardLocation.CLASS_OUTPUT, "", fileName); + this.messager.printMessage(Kind.NOTE, "Writing " + description + " to " + new File(outSrg.toUri()).getAbsolutePath()); + 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/ap/java/org/spongepowered/tools/obfuscation/validation/TargetValidator.java b/src/ap/java/org/spongepowered/tools/obfuscation/validation/TargetValidator.java index 3fc429fd..08c8bf64 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/validation/TargetValidator.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/validation/TargetValidator.java @@ -37,6 +37,7 @@ import javax.lang.model.type.TypeMirror; import org.spongepowered.tools.MirrorUtils; import org.spongepowered.tools.obfuscation.IOptionProvider; import org.spongepowered.tools.obfuscation.MixinValidator; +import org.spongepowered.tools.obfuscation.SupportedOptions; import org.spongepowered.tools.obfuscation.TypeHandle; @@ -63,7 +64,7 @@ public class TargetValidator extends MixinValidator { */ @Override public boolean validate(TypeElement mixin, AnnotationMirror annotation, Collection targets) { - if ("true".equalsIgnoreCase(this.options.getOption("disableTargetValidator"))) { + if ("true".equalsIgnoreCase(this.options.getOption(SupportedOptions.DISABLE_TARGET_VALIDATOR))) { return true; } diff --git a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java index e516137d..3b619abb 100644 --- a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java +++ b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java @@ -60,7 +60,7 @@ public abstract class MixinBootstrap { /** * Subsystem version */ - public static final String VERSION = "0.4.7"; + public static final String VERSION = "0.4.8"; /** * Blackboard key where the subsystem version will be stored to indicate