From 8d87440fafcb76ae78eac2609742fb5e87a122fa Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Sat, 1 Oct 2016 23:40:16 +0100 Subject: [PATCH] Refactor AP to support different mapping environments, closes #141 --- build.gradle | 8 + checkstyle.xml | 4 +- gradle.properties | 2 +- .../org/spongepowered/tools/MirrorUtils.java | 10 + .../tools/obfuscation/AnnotatedMixin.java | 77 +--- .../AnnotatedMixinElementHandler.java | 78 +++- .../AnnotatedMixinInjectorHandler.java | 87 ++-- .../AnnotatedMixinOverwriteHandler.java | 48 ++- .../AnnotatedMixinShadowHandler.java | 152 +++++-- .../tools/obfuscation/AnnotatedMixins.java | 25 +- .../InjectionObfuscationProcessor.java | 4 +- .../tools/obfuscation/Mappings.java | 112 ++++++ .../tools/obfuscation/MixinProcessor.java | 2 +- .../tools/obfuscation/ObfuscationData.java | 6 +- .../obfuscation/ObfuscationDataProvider.java | 266 +++++++++++++ ...nment.java => ObfuscationEnvironment.java} | 253 +++++------- .../tools/obfuscation/ObfuscationManager.java | 376 +++--------------- .../tools/obfuscation/ObfuscationType.java | 130 +++--- .../tools/obfuscation/ReferenceManager.java | 197 +++++++++ .../tools/obfuscation/SupportedOptions.java | 43 +- .../TargetObfuscationProcessor.java | 4 +- .../interfaces/IObfuscationDataProvider.java | 121 ++++++ .../interfaces/IObfuscationManager.java | 126 +----- .../interfaces/IReferenceManager.java | 91 +++++ .../obfuscation/mapping/IMappingConsumer.java | 137 +++++++ .../obfuscation/mapping/IMappingProvider.java | 94 +++++ .../obfuscation/mapping/IMappingWriter.java | 47 +++ .../mapping/common/MappingProvider.java | 88 ++++ .../mapping/common/MappingWriter.java | 69 ++++ .../mapping/mcp/MappingProviderSrg.java | 104 +++++ .../mapping/mcp/MappingWriterSrg.java | 94 +++++ .../mcp/ObfuscationEnvironmentMCP.java | 55 +++ .../mcp/ObfuscationServiceMCP.java | 85 ++++ .../service/IObfuscationService.java | 50 +++ .../service/ObfuscationServices.java | 124 ++++++ .../service/ObfuscationTypeDescriptor.java | 109 +++++ ...ls.obfuscation.service.IObfuscationService | 1 + .../obfuscation/json/MappingWriterJson.java | 96 +++++ .../json/ObfuscationEnvironmentJson.java | 55 +++ .../json/ObfuscationServiceJson.java | 68 ++++ ...ls.obfuscation.service.IObfuscationService | 1 + .../asm/launch/MixinBootstrap.java | 2 +- .../mixin/injection/struct/MemberInfo.java | 81 ++-- .../asm/obfuscation/SrgContainer.java | 90 ----- .../asm/obfuscation/mapping/IMapping.java | 64 +++ .../mapping/common/MappingField.java | 123 ++++++ .../common/MappingMethod.java} | 54 ++- .../mcp/MappingFieldSrg.java} | 78 ++-- 48 files changed, 2988 insertions(+), 1003 deletions(-) create mode 100644 src/ap/java/org/spongepowered/tools/obfuscation/Mappings.java create mode 100644 src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationDataProvider.java rename src/ap/java/org/spongepowered/tools/obfuscation/{TargetObfuscationEnvironment.java => ObfuscationEnvironment.java} (52%) create mode 100644 src/ap/java/org/spongepowered/tools/obfuscation/ReferenceManager.java create mode 100644 src/ap/java/org/spongepowered/tools/obfuscation/interfaces/IObfuscationDataProvider.java create mode 100644 src/ap/java/org/spongepowered/tools/obfuscation/interfaces/IReferenceManager.java create mode 100644 src/ap/java/org/spongepowered/tools/obfuscation/mapping/IMappingConsumer.java create mode 100644 src/ap/java/org/spongepowered/tools/obfuscation/mapping/IMappingProvider.java create mode 100644 src/ap/java/org/spongepowered/tools/obfuscation/mapping/IMappingWriter.java create mode 100644 src/ap/java/org/spongepowered/tools/obfuscation/mapping/common/MappingProvider.java create mode 100644 src/ap/java/org/spongepowered/tools/obfuscation/mapping/common/MappingWriter.java create mode 100644 src/ap/java/org/spongepowered/tools/obfuscation/mapping/mcp/MappingProviderSrg.java create mode 100644 src/ap/java/org/spongepowered/tools/obfuscation/mapping/mcp/MappingWriterSrg.java create mode 100644 src/ap/java/org/spongepowered/tools/obfuscation/mcp/ObfuscationEnvironmentMCP.java create mode 100644 src/ap/java/org/spongepowered/tools/obfuscation/mcp/ObfuscationServiceMCP.java create mode 100644 src/ap/java/org/spongepowered/tools/obfuscation/service/IObfuscationService.java create mode 100644 src/ap/java/org/spongepowered/tools/obfuscation/service/ObfuscationServices.java create mode 100644 src/ap/java/org/spongepowered/tools/obfuscation/service/ObfuscationTypeDescriptor.java create mode 100644 src/ap/resources/META-INF/services/org.spongepowered.tools.obfuscation.service.IObfuscationService create mode 100644 src/example/java/org/spongepowered/tools/obfuscation/json/MappingWriterJson.java create mode 100644 src/example/java/org/spongepowered/tools/obfuscation/json/ObfuscationEnvironmentJson.java create mode 100644 src/example/java/org/spongepowered/tools/obfuscation/json/ObfuscationServiceJson.java create mode 100644 src/example/resources/META-INF/services/org.spongepowered.tools.obfuscation.service.IObfuscationService delete mode 100644 src/main/java/org/spongepowered/asm/obfuscation/SrgContainer.java create mode 100644 src/main/java/org/spongepowered/asm/obfuscation/mapping/IMapping.java create mode 100644 src/main/java/org/spongepowered/asm/obfuscation/mapping/common/MappingField.java rename src/main/java/org/spongepowered/asm/obfuscation/{SrgMethod.java => mapping/common/MappingMethod.java} (63%) rename src/main/java/org/spongepowered/asm/obfuscation/{SrgField.java => mapping/mcp/MappingFieldSrg.java} (50%) diff --git a/build.gradle b/build.gradle index 07518b4a..2b06dc15 100644 --- a/build.gradle +++ b/build.gradle @@ -88,6 +88,10 @@ sourceSets { bridge { compileClasspath += main.output } + example { + compileClasspath += main.output + compileClasspath += ap.output + } } // Dependencies for renamed ASM @@ -130,9 +134,11 @@ dependencies { compile 'org.slf4j:slf4j-api:1.7.7' compile 'com.google.guava:guava:17.0' compile 'com.google.code.gson:gson:2.2.4' + exampleCompile 'com.google.code.gson:gson:2.2.4' compile 'commons-io:commons-io:2.4' compile 'commons-codec:commons-codec:1.9' apCompile 'com.google.guava:guava:17.0' + exampleCompile 'com.google.guava:guava:17.0' testCompile 'junit:junit:4.11' testCompile 'org.hamcrest:hamcrest-library:1.3' testCompile 'org.mockito:mockito-core:1.9.0' @@ -276,6 +282,8 @@ task sourceJar(type: Jar) { from sourceSets.agent.resources from sourceSets.bridge.java from sourceSets.bridge.resources + from sourceSets.example.java + from sourceSets.example.resources classifier = "sources" } diff --git a/checkstyle.xml b/checkstyle.xml index b10493a1..f11b7e93 100644 --- a/checkstyle.xml +++ b/checkstyle.xml @@ -5,7 +5,7 @@ This configuration file was written by the eclipse-cs plugin configuration editor --> @@ -72,7 +72,7 @@ - + diff --git a/gradle.properties b/gradle.properties index 9bfdf2e5..515d3f5c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,5 +4,5 @@ packaging=jar description=Mixin url=https://www.spongepowered.org organization=SpongePowered -buildVersion=0.5.14 +buildVersion=0.5.15 buildType=SNAPSHOT diff --git a/src/ap/java/org/spongepowered/tools/MirrorUtils.java b/src/ap/java/org/spongepowered/tools/MirrorUtils.java index 3a683e0f..752b1438 100644 --- a/src/ap/java/org/spongepowered/tools/MirrorUtils.java +++ b/src/ap/java/org/spongepowered/tools/MirrorUtils.java @@ -194,6 +194,16 @@ public abstract class MirrorUtils { } return sb.toString(); } + + public static String getDescriptor(Element elem) { + if (elem instanceof ExecutableElement) { + return MirrorUtils.generateSignature((ExecutableElement)elem); + } else if (elem instanceof VariableElement) { + return MirrorUtils.getInternalName((VariableElement)elem); + } + + return MirrorUtils.getInternalName(elem.asType()); + } public static String getTypeName(TypeMirror type) { switch (type.getKind()) { diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java index 2714c7c6..4775084c 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java @@ -27,11 +27,7 @@ package org.spongepowered.tools.obfuscation; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -import java.util.HashMap; -import java.util.LinkedHashSet; import java.util.List; -import java.util.Map; -import java.util.Set; import javax.annotation.processing.Messager; import javax.lang.model.element.AnnotationMirror; @@ -46,9 +42,13 @@ import javax.tools.Diagnostic.Kind; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.tools.MirrorUtils; +import org.spongepowered.tools.obfuscation.AnnotatedMixinInjectorHandler.AnnotatedElementInjectionPoint; +import org.spongepowered.tools.obfuscation.AnnotatedMixinInjectorHandler.AnnotatedElementInjector; +import org.spongepowered.tools.obfuscation.AnnotatedMixinOverwriteHandler.AnnotatedElementOverwrite; import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor; import org.spongepowered.tools.obfuscation.interfaces.IMixinValidator; import org.spongepowered.tools.obfuscation.interfaces.IMixinValidator.ValidationPass; +import org.spongepowered.tools.obfuscation.mapping.IMappingConsumer; import org.spongepowered.tools.obfuscation.interfaces.IObfuscationManager; import org.spongepowered.tools.obfuscation.interfaces.ITypeHandleProvider; import org.spongepowered.tools.obfuscation.struct.Message; @@ -78,6 +78,11 @@ class AnnotatedMixin { */ private final IObfuscationManager obf; + /** + * Generated mappings + */ + private final IMappingConsumer mappings; + /** * Mixin class */ @@ -112,17 +117,7 @@ class AnnotatedMixin { * True if we will actually process remappings for this mixin */ private final boolean remap; - - /** - * Stored (ordered) field mappings - */ - private final Map> fieldMappings = new HashMap>(); - - /** - * Stored (ordered) method mappings - */ - private final Map> methodMappings = new HashMap>(); - + /** * Overwrite handler */ @@ -142,6 +137,7 @@ class AnnotatedMixin { this.annotation = MirrorUtils.getAnnotation(type, Mixin.class); this.typeProvider = ap.getTypeProvider(); this.obf = ap.getObfuscationManager(); + this.mappings = this.obf.createMappingConsumer(); this.messager = ap; this.mixin = type; this.handle = new TypeHandle(type); @@ -158,11 +154,6 @@ 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()); - } - this.overwrites = new AnnotatedMixinOverwriteHandler(ap, this); this.shadows = new AnnotatedMixinShadowHandler(ap, this); this.injectors = new AnnotatedMixinInjectorHandler(ap, this); @@ -239,9 +230,9 @@ class AnnotatedMixin { } private void addSoftTarget(TypeHandle type, String reference) { - ObfuscationData obfClassData = this.obf.getObfClass(type); + ObfuscationData obfClassData = this.obf.getDataProvider().getObfClass(type); if (!obfClassData.isEmpty()) { - this.obf.addClassMapping(this.classRef, reference, obfClassData); + this.obf.getReferenceManager().addClassMapping(this.classRef, reference, obfClassData); } this.addTarget(type); @@ -316,54 +307,28 @@ class AnnotatedMixin { return this.remap; } - /** - * Get stored field mappings - */ - public Set getFieldMappings(ObfuscationType type) { - return this.fieldMappings.get(type); + public IMappingConsumer getMappings() { + return this.mappings; } - /** - * Get stored method mappings - */ - public Set getMethodMappings(ObfuscationType type) { - return this.methodMappings.get(type); - } - - /** - * Clear all stored mappings - */ - public void clear() { - this.fieldMappings.clear(); - this.methodMappings.clear(); - } - public void registerOverwrite(ExecutableElement method, AnnotationMirror overwrite) { - this.overwrites.registerOverwrite(method, overwrite); + this.overwrites.registerOverwrite(new AnnotatedElementOverwrite(method, overwrite)); } public void registerShadow(VariableElement field, AnnotationMirror shadow, boolean shouldRemap) { - this.shadows.registerShadow(field, shadow, shouldRemap); + this.shadows.registerShadow(this.shadows.new AnnotatedElementShadowField(field, shadow, shouldRemap)); } public void registerShadow(ExecutableElement method, AnnotationMirror shadow, boolean shouldRemap) { - this.shadows.registerShadow(method, shadow, shouldRemap); + this.shadows.registerShadow(this.shadows.new AnnotatedElementShadowMethod(method, shadow, shouldRemap)); } - public Message registerInjector(ExecutableElement method, AnnotationMirror inject, boolean remap) { - return this.injectors.registerInjector(method, inject, remap); + public Message registerInjector(ExecutableElement method, AnnotationMirror inject, boolean shouldRemap) { + return this.injectors.registerInjector(new AnnotatedElementInjector(method, inject, shouldRemap)); } public int registerInjectionPoint(ExecutableElement element, AnnotationMirror inject, AnnotationMirror at) { - return this.injectors.registerInjectionPoint(element, inject, at); - } - - void addFieldMapping(ObfuscationType type, String mapping) { - this.fieldMappings.get(type).add(mapping); - } - - void addMethodMapping(ObfuscationType type, String mapping) { - this.methodMappings.get(type).add(mapping); + return this.injectors.registerInjectionPoint(new AnnotatedElementInjectionPoint(element, inject, at)); } } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandler.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandler.java index 41ed260e..f0e79d5d 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandler.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandler.java @@ -35,7 +35,9 @@ import javax.tools.Diagnostic.Kind; import org.spongepowered.asm.mixin.injection.struct.InvalidMemberDescriptorException; import org.spongepowered.asm.mixin.injection.struct.MemberInfo; -import org.spongepowered.asm.obfuscation.SrgMethod; +import org.spongepowered.asm.obfuscation.mapping.IMapping; +import org.spongepowered.asm.obfuscation.mapping.common.MappingField; +import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod; import org.spongepowered.asm.util.ConstraintParser; import org.spongepowered.asm.util.ConstraintParser.Constraint; import org.spongepowered.asm.util.throwables.ConstraintViolationException; @@ -50,6 +52,32 @@ import org.spongepowered.tools.obfuscation.interfaces.IObfuscationManager; */ abstract class AnnotatedMixinElementHandler { + /** + * An annotated element to be processed by this element handler + * + * @param type of inner element + */ + abstract static class AnnotatedElement { + + private final E element; + + private final AnnotationMirror annotation; + + public AnnotatedElement(E element, AnnotationMirror annotation) { + this.element = element; + this.annotation = annotation; + } + + public E getElement() { + return this.element; + } + + public AnnotationMirror getAnnotation() { + return this.annotation; + } + + } + /** * A name of an element which may have aliases */ @@ -153,6 +181,17 @@ abstract class AnnotatedMixinElementHandler { public String baseName() { return this.baseName; } + + /** + * Sets the obfuscated name for this element + * + * @param name Mapping containing new name + * @return fluent interface + */ + public ShadowElementName setObfuscatedName(IMapping name) { + this.obfuscated = name.getName(); + return this; + } /** * Sets the obfuscated name for this element @@ -161,7 +200,7 @@ abstract class AnnotatedMixinElementHandler { * @return fluent interface */ public ShadowElementName setObfuscatedName(String name) { - this.obfuscated = name.substring(name.lastIndexOf('/') + 1); + this.obfuscated = name; return this; } @@ -241,20 +280,20 @@ abstract class AnnotatedMixinElementHandler { } if (targetMember.isField()) { - ObfuscationData obfFieldData = this.obf.getObfFieldRecursive(targetMember); + ObfuscationData obfFieldData = this.obf.getDataProvider().getObfFieldRecursive(targetMember); if (obfFieldData.isEmpty()) { this.ap.printMessage(Kind.WARNING, "Cannot find field mapping for @At(" + key + ") '" + target + "'", element, inject); return false; } - this.obf.addFieldMapping(this.classRef, target, targetMember, obfFieldData); + this.obf.getReferenceManager().addFieldMapping(this.classRef, target, targetMember, obfFieldData); } else { - ObfuscationData obfMethodData = this.obf.getObfMethodRecursive(targetMember); + ObfuscationData obfMethodData = this.obf.getDataProvider().getObfMethodRecursive(targetMember); if (obfMethodData.isEmpty()) { if (targetMember.owner == null || !targetMember.owner.startsWith("java/lang/")) { this.ap.printMessage(Kind.WARNING, "Cannot find method mapping for @At(" + key + ") '" + target + "'", element, inject); } } - this.obf.addMethodMapping(this.classRef, target, targetMember, obfMethodData); + this.obf.getReferenceManager().addMethodMapping(this.classRef, target, targetMember, obfMethodData); } return true; } @@ -262,9 +301,17 @@ abstract class AnnotatedMixinElementHandler { /** * Add a field mapping to the local table */ - protected final void addFieldMapping(ObfuscationType type, ShadowElementName name) { - String mapping = String.format("FD: %s %s", this.classRef + "/" + name.name(), this.classRef + "/" + name.obfuscated()); - this.mixin.addFieldMapping(type, mapping); + protected final void addFieldMapping(ObfuscationType type, ShadowElementName name, String mcpSignature, String obfSignature) { + this.addFieldMapping(type, name.name(), name.obfuscated(), mcpSignature, obfSignature); + } + + /** + * Add a field mapping to the local table + */ + protected final void addFieldMapping(ObfuscationType type, String mcpName, String obfName, String mcpSignature, String obfSignature) { + MappingField from = new MappingField(this.classRef, mcpName, mcpSignature); + MappingField to = new MappingField(this.classRef, obfName, obfSignature); + this.mixin.getMappings().addFieldMapping(type, from, to); } /** @@ -278,8 +325,9 @@ abstract class AnnotatedMixinElementHandler { * Add a method mapping to the local table */ protected final 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.mixin.addMethodMapping(type, mapping); + MappingMethod from = new MappingMethod(this.classRef, mcpName, mcpSignature); + MappingMethod to = new MappingMethod(this.classRef, obfName, obfSignature); + this.mixin.getMappings().addMethodMapping(type, from, to); } /** @@ -317,6 +365,14 @@ abstract class AnnotatedMixinElementHandler { } } + protected final void validateTarget(Element element, AnnotationMirror annotation, AliasedElementName name, String type) { + if (element instanceof ExecutableElement) { + this.validateTargetMethod((ExecutableElement)element, annotation, name, type); + } else if (element instanceof VariableElement) { + this.validateTargetField((VariableElement)element, annotation, name, type); + } + } + /** * Checks whether the specified method exists in all targets and raises * warnings where appropriate diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinInjectorHandler.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinInjectorHandler.java index 075c34e0..b0a73b0e 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinInjectorHandler.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinInjectorHandler.java @@ -30,13 +30,12 @@ import java.util.Map; import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.AnnotationValue; -import javax.lang.model.element.Element; import javax.lang.model.element.ExecutableElement; import javax.tools.Diagnostic.Kind; import org.spongepowered.asm.mixin.injection.struct.InvalidMemberDescriptorException; import org.spongepowered.asm.mixin.injection.struct.MemberInfo; -import org.spongepowered.asm.obfuscation.SrgMethod; +import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod; import org.spongepowered.asm.util.Constants; import org.spongepowered.tools.MirrorUtils; import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor; @@ -47,24 +46,52 @@ import org.spongepowered.tools.obfuscation.struct.Message; */ class AnnotatedMixinInjectorHandler extends AnnotatedMixinElementHandler { + /** + * Injector element + */ + static class AnnotatedElementInjector extends AnnotatedElement { + + private final boolean shouldRemap; + + public AnnotatedElementInjector(ExecutableElement element, AnnotationMirror annotation, boolean shouldRemap) { + super(element, annotation); + this.shouldRemap = shouldRemap; + } + + public boolean shouldRemap() { + return this.shouldRemap; + } + + } + + /** + * Injection point element + */ + static class AnnotatedElementInjectionPoint extends AnnotatedElement { + + private final AnnotationMirror at; + + public AnnotatedElementInjectionPoint(ExecutableElement element, AnnotationMirror inject, AnnotationMirror at) { + super(element, inject); + this.at = at; + } + + public AnnotationMirror getAt() { + return this.at; + } + + } + AnnotatedMixinInjectorHandler(IMixinAnnotationProcessor ap, AnnotatedMixin mixin) { super(ap, mixin); } - /** - * Register a {@link org.spongepowered.asm.mixin.injection.Inject} method - * - * @param method Callback method - * @param inject Inject annotation - * @param remap - * @return pending error message or null if no error condition - */ - public Message registerInjector(ExecutableElement method, AnnotationMirror inject, boolean remap) { + public Message registerInjector(AnnotatedElementInjector elem) { if (this.mixin.isInterface()) { - this.ap.printMessage(Kind.ERROR, "Injector in interface is unsupported", method); + this.ap.printMessage(Kind.ERROR, "Injector in interface is unsupported", elem.getElement()); } - String originalReference = MirrorUtils.getAnnotationValue(inject, "method"); + String originalReference = MirrorUtils.getAnnotationValue(elem.getAnnotation(), "method"); MemberInfo targetMember = MemberInfo.parse(originalReference); if (targetMember.name == null) { return null; @@ -73,15 +100,15 @@ class AnnotatedMixinInjectorHandler extends AnnotatedMixinElementHandler { try { targetMember.validate(); } catch (InvalidMemberDescriptorException ex) { - this.ap.printMessage(Kind.ERROR, ex.getMessage(), method, inject); + this.ap.printMessage(Kind.ERROR, ex.getMessage(), elem.getElement(), elem.getAnnotation()); } - String type = "@" + inject.getAnnotationType().asElement().getSimpleName() + ""; + String type = "@" + elem.getAnnotation().getAnnotationType().asElement().getSimpleName() + ""; if (targetMember.desc != null) { - this.validateReferencedTarget(method, inject, targetMember, type); + this.validateReferencedTarget(elem.getElement(), elem.getAnnotation(), targetMember, type); } - if (!remap || !this.validateSingleTarget(type, method)) { + if (!elem.shouldRemap() || !this.validateSingleTarget(type, elem.getElement())) { return null; } @@ -89,20 +116,22 @@ class AnnotatedMixinInjectorHandler extends AnnotatedMixinElementHandler { if (desc == null) { if (this.mixin.getPrimaryTarget().isImaginary()) { this.ap.printMessage(Kind.WARNING, type + " target requires method signature because enclosing type information is unavailable", - method, inject); + elem.getElement(), elem.getAnnotation()); } else if (!Constants.CTOR.equals(targetMember.name)) { - this.ap.printMessage(Kind.WARNING, "Unable to determine signature for " + type + " target method", method, inject); + this.ap.printMessage(Kind.WARNING, "Unable to determine signature for " + type + " target method", + elem.getElement(), elem.getAnnotation()); } return null; } - ObfuscationData obfData = this.obf.getObfMethod(new SrgMethod(this.mixin.getPrimaryTargetRef() + "/" + targetMember.name, desc)); + MappingMethod targetMethod = new MappingMethod(this.mixin.getPrimaryTargetRef(), targetMember.name, desc); + ObfuscationData obfData = this.obf.getDataProvider().getObfMethod(targetMethod); if (obfData.isEmpty()) { Kind error = Constants.CTOR.equals(targetMember.name) ? Kind.WARNING : Kind.ERROR; - return new Message(error, "No obfuscation mapping for " + type + " target " + targetMember.name, method, inject); + return new Message(error, "No obfuscation mapping for " + type + " target " + targetMember.name, elem.getElement(), elem.getAnnotation()); } - this.obf.addMethodMapping(this.classRef, originalReference, obfData); + this.obf.getReferenceManager().addMethodMapping(this.classRef, originalReference, obfData); return null; } @@ -110,22 +139,22 @@ class AnnotatedMixinInjectorHandler extends AnnotatedMixinElementHandler { * Register a {@link org.spongepowered.asm.mixin.injection.At} annotation * and process the references */ - public int registerInjectionPoint(Element element, AnnotationMirror inject, AnnotationMirror at) { + public int registerInjectionPoint(AnnotatedElementInjectionPoint elem) { if (this.mixin.isInterface()) { - this.ap.printMessage(Kind.ERROR, "Injector in interface is unsupported", element); + this.ap.printMessage(Kind.ERROR, "Injector in interface is unsupported", elem.getElement()); } - if (!AnnotatedMixins.getRemapValue(at)) { + if (!AnnotatedMixins.getRemapValue(elem.getAt())) { return 0; } - String type = MirrorUtils.getAnnotationValue(at, "value"); - String target = MirrorUtils.getAnnotationValue(at, "target"); - int remapped = this.remapReference(type + ".", target, element, inject, at) ? 1 : 0; + String type = MirrorUtils.getAnnotationValue(elem.getAt(), "value"); + String target = MirrorUtils.getAnnotationValue(elem.getAt(), "target"); + int remapped = this.remapReference(type + ".", target, elem.getElement(), elem.getAnnotation(), elem.getAt()) ? 1 : 0; // Pattern for replacing references in args, not used yet // if ("SOMETYPE".equals(type)) { -// Map args = InjectorHandler.getAtArgs(at); +// Map args = InjectorHandler.getAtArgs(e.getAt()); // this.remapReference(type + ".args[target]", args.get("target")); // } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinOverwriteHandler.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinOverwriteHandler.java index 1c3ebf5e..69dcfaf8 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinOverwriteHandler.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinOverwriteHandler.java @@ -30,7 +30,7 @@ import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.ExecutableElement; import javax.tools.Diagnostic.Kind; -import org.spongepowered.asm.obfuscation.SrgMethod; +import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod; import org.spongepowered.tools.MirrorUtils; import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor; @@ -39,42 +39,54 @@ import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor; */ class AnnotatedMixinOverwriteHandler extends AnnotatedMixinElementHandler { + /** + * Overwrite element + */ + static class AnnotatedElementOverwrite extends AnnotatedElement { + + public AnnotatedElementOverwrite(ExecutableElement element, AnnotationMirror annotation) { + super(element, annotation); + } + + } + AnnotatedMixinOverwriteHandler(IMixinAnnotationProcessor ap, AnnotatedMixin mixin) { super(ap, mixin); } - void registerOverwrite(ExecutableElement method, AnnotationMirror overwrite) { - AliasedElementName name = new AliasedElementName(method, overwrite); - this.validateTargetMethod(method, overwrite, name, "@Overwrite"); - this.checkConstraints(method, overwrite); + public void registerOverwrite(AnnotatedElementOverwrite elem) { + AliasedElementName name = new AliasedElementName(elem.getElement(), elem.getAnnotation()); + this.validateTargetMethod(elem.getElement(), elem.getAnnotation(), name, "@Overwrite"); + this.checkConstraints(elem.getElement(), elem.getAnnotation()); - if (!this.mixin.remap() || !this.validateSingleTarget("@Overwrite", method)) { + if (!this.mixin.remap() || !this.validateSingleTarget("@Overwrite", elem.getElement())) { return; } - String mcpName = method.getSimpleName().toString(); - String mcpSignature = MirrorUtils.generateSignature(method); - ObfuscationData obfData = this.obf.getObfMethod(new SrgMethod(this.mixin.getPrimaryTargetRef() + "/" + mcpName, mcpSignature)); + String mcpName = elem.getElement().getSimpleName().toString(); + String mcpSignature = MirrorUtils.generateSignature(elem.getElement()); + MappingMethod targetMethod = new MappingMethod(this.mixin.getPrimaryTargetRef(), mcpName, mcpSignature); + ObfuscationData obfData = this.obf.getDataProvider().getObfMethod(targetMethod); if (obfData.isEmpty()) { Kind error = Kind.ERROR; try { // Try to access isStatic from com.sun.tools.javac.code.Symbol - Method md = method.getClass().getMethod("isStatic"); - if (((Boolean)md.invoke(method)).booleanValue()) { + Method md = elem.getElement().getClass().getMethod("isStatic"); + if (((Boolean)md.invoke(elem.getElement())).booleanValue()) { error = Kind.WARNING; } } catch (Exception ex) { // well, we tried } - this.ap.printMessage(error, "No obfuscation mapping for @Overwrite method", method); + this.ap.printMessage(error, "No obfuscation mapping for @Overwrite method", elem.getElement()); return; } for (ObfuscationType type : obfData) { - SrgMethod obfMethod = obfData.get(type); + MappingMethod obfMethod = obfData.get(type); this.addMethodMapping(type, mcpName, obfMethod.getSimpleName(), mcpSignature, obfMethod.getDesc()); } @@ -82,20 +94,20 @@ class AnnotatedMixinOverwriteHandler extends AnnotatedMixinElementHandler { Kind overwriteErrorKind = "error".equalsIgnoreCase(this.ap.getOption(SupportedOptions.OVERWRITE_ERROR_LEVEL)) ? Kind.ERROR : Kind.WARNING; - String javadoc = this.ap.getJavadocProvider().getJavadoc(method); + String javadoc = this.ap.getJavadocProvider().getJavadoc(elem.getElement()); if (javadoc == null) { - this.ap.printMessage(overwriteErrorKind, "@Overwrite is missing javadoc comment", method); + this.ap.printMessage(overwriteErrorKind, "@Overwrite is missing javadoc comment", elem.getElement()); return; } if (!javadoc.toLowerCase().contains("@author")) { - this.ap.printMessage(overwriteErrorKind, "@Overwrite is missing an @author tag", method); + this.ap.printMessage(overwriteErrorKind, "@Overwrite is missing an @author tag", elem.getElement()); } if (!javadoc.toLowerCase().contains("@reason")) { - this.ap.printMessage(overwriteErrorKind, "@Overwrite is missing an @reason tag", method); + this.ap.printMessage(overwriteErrorKind, "@Overwrite is missing an @reason tag", elem.getElement()); } } } - + } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinShadowHandler.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinShadowHandler.java index e470d299..1527cada 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinShadowHandler.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinShadowHandler.java @@ -25,18 +25,127 @@ package org.spongepowered.tools.obfuscation; import javax.lang.model.element.AnnotationMirror; +import javax.lang.model.element.Element; import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.VariableElement; import javax.tools.Diagnostic.Kind; -import org.spongepowered.asm.obfuscation.SrgMethod; +import org.spongepowered.asm.obfuscation.mapping.IMapping; +import org.spongepowered.asm.obfuscation.mapping.IMapping.Type; +import org.spongepowered.asm.obfuscation.mapping.common.MappingField; +import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod; import org.spongepowered.tools.MirrorUtils; import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor; +import org.spongepowered.tools.obfuscation.interfaces.IObfuscationDataProvider; /** * A module for {@link AnnotatedMixin} which handles shadowed fields and methods */ class AnnotatedMixinShadowHandler extends AnnotatedMixinElementHandler { + + /** + * A shadow element to be processed + * + * @param Type of inner element + * @param Mapping type of mapping used by this element + */ + abstract static class AnnotatedElementShadow> extends AnnotatedElement { + + private final boolean shouldRemap; + + private final ShadowElementName name; + + private final String desc; + + private final Type type; + + protected AnnotatedElementShadow(E element, AnnotationMirror annotation, boolean shouldRemap, Type type) { + super(element, annotation); + this.shouldRemap = shouldRemap; + this.name = new ShadowElementName(element, annotation); + this.desc = MirrorUtils.getDescriptor(element); + this.type = type; + } + + public boolean shouldRemap() { + return this.shouldRemap; + } + + public ShadowElementName getName() { + return this.name; + } + + public String getDesc() { + return this.desc; + } + + public Type getElementType() { + return this.type; + } + + @Override + public String toString() { + return this.getElementType().name().toLowerCase(); + } + + public ShadowElementName setObfuscatedName(IMapping name) { + return this.setObfuscatedName(name.getName()); + } + + public ShadowElementName setObfuscatedName(String name) { + return this.getName().setObfuscatedName(name); + } + + public ObfuscationData getObfuscationData(IObfuscationDataProvider provider, String owner) { + return provider.getObfEntry(this.getMapping(owner, this.getName().toString(), this.getDesc())); + } + + public abstract M getMapping(String owner, String name, String desc); + + public abstract void addMapping(ObfuscationType type, IMapping remapped); + + } + + /** + * Shadow field element + */ + class AnnotatedElementShadowField extends AnnotatedElementShadow { + + public AnnotatedElementShadowField(VariableElement element, AnnotationMirror annotation, boolean shouldRemap) { + super(element, annotation, shouldRemap, Type.FIELD); + } + + @Override + public MappingField getMapping(String owner, String name, String desc) { + return new MappingField(owner, name, desc); + } + + @Override + public void addMapping(ObfuscationType type, IMapping remapped) { + AnnotatedMixinShadowHandler.this.addFieldMapping(type, this.setObfuscatedName(remapped), this.getDesc(), remapped.getDesc()); + } + } + + /** + * Shadow method element + */ + class AnnotatedElementShadowMethod extends AnnotatedElementShadow { + + public AnnotatedElementShadowMethod(ExecutableElement element, AnnotationMirror annotation, boolean shouldRemap) { + super(element, annotation, shouldRemap, Type.METHOD); + } + + @Override + public MappingMethod getMapping(String owner, String name, String desc) { + return new MappingMethod(owner, name, desc); + } + + @Override + public void addMapping(ObfuscationType type, IMapping remapped) { + AnnotatedMixinShadowHandler.this.addMethodMapping(type, this.setObfuscatedName(remapped), this.getDesc(), remapped.getDesc()); + } + + } AnnotatedMixinShadowHandler(IMixinAnnotationProcessor ap, AnnotatedMixin mixin) { super(ap, mixin); @@ -45,49 +154,22 @@ class AnnotatedMixinShadowHandler extends AnnotatedMixinElementHandler { /** * Register a {@link org.spongepowered.asm.mixin.Shadow} field */ - public void registerShadow(VariableElement field, AnnotationMirror shadow, boolean remap) { - ShadowElementName name = new ShadowElementName(field, shadow); - this.validateTargetField(field, shadow, name, "@Shadow"); + public void registerShadow(AnnotatedElementShadow elem) { + this.validateTarget(elem.getElement(), elem.getAnnotation(), elem.getName(), "@Shadow"); - if (!remap || !this.validateSingleTarget("@Shadow", field)) { + if (!elem.shouldRemap() || !this.validateSingleTarget("@Shadow", elem.getElement())) { return; } - ObfuscationData obfFieldData = this.obf.getObfField(this.mixin.getPrimaryTargetRef() + "/" + name); - - if (obfFieldData.isEmpty()) { - this.ap.printMessage(Kind.WARNING, "Unable to locate obfuscation mapping for @Shadow field", field, shadow); - return; - } - - for (ObfuscationType type : obfFieldData) { - String fieldName = obfFieldData.get(type); - this.addFieldMapping(type, name.setObfuscatedName(fieldName)); - } - } - - /** - * Register a {@link org.spongepowered.asm.mixin.Shadow} method - */ - public void registerShadow(ExecutableElement method, AnnotationMirror shadow, boolean remap) { - ShadowElementName name = new ShadowElementName(method, shadow); - this.validateTargetMethod(method, shadow, name, "@Shadow"); - - if (!remap || !this.validateSingleTarget("@Shadow", method)) { - return; - } - - String mcpSignature = MirrorUtils.generateSignature(method); - ObfuscationData obfData = this.obf.getObfMethod(new SrgMethod(this.mixin.getPrimaryTargetRef() + "/" + name, mcpSignature)); + ObfuscationData> obfData = elem.getObfuscationData(this.obf.getDataProvider(), this.mixin.getPrimaryTargetRef()); if (obfData.isEmpty()) { - this.ap.printMessage(Kind.WARNING, "Unable to locate obfuscation mapping for @Shadow method", method, shadow); + this.ap.printMessage(Kind.WARNING, "Unable to locate obfuscation mapping for @Shadow " + elem, elem.getElement(), elem.getAnnotation()); return; } - + for (ObfuscationType type : obfData) { - SrgMethod obfMethod = obfData.get(type); - this.addMethodMapping(type, name.setObfuscatedName(obfMethod.getName()), mcpSignature, obfMethod.getDesc()); + elem.addMapping(type, obfData.get(type)); } } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java index c07715aa..598ef4d3 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java @@ -71,7 +71,7 @@ import com.google.common.collect.ImmutableList; /** * Mixin info manager, stores all of the mixin info during processing and also - * manages access to the srgs + * manages access to the mappings */ class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider, ITypeHandleProvider, IJavadocProvider { @@ -118,7 +118,7 @@ class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider, ITyp /** * Obfuscation manager */ - private final ObfuscationManager obf; + private final IObfuscationManager obf; /** * Rule validators @@ -145,12 +145,13 @@ class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider, ITyp * Private constructor, get instances using {@link #getMixinsForEnvironment} */ private AnnotatedMixins(ProcessingEnvironment processingEnv) { + this.printMessage(Kind.NOTE, "SpongePowered Mixin Annotation Processor v" + MixinBootstrap.VERSION); + this.env = this.detectEnvironment(processingEnv); this.processingEnv = processingEnv; this.targets = this.initTargetMap(); this.obf = new ObfuscationManager(this); - - this.printMessage(Kind.NOTE, "SpongePowered Mixin Annotation Processor v" + MixinBootstrap.VERSION); + this.obf.init(); this.validators = ImmutableList.of( new ParentValidator(this), @@ -233,6 +234,10 @@ class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider, ITyp @Override public String getOption(String option) { + if (option == null) { + return null; + } + String value = this.processingEnv.getOptions().get(option); if (value != null) { return value; @@ -270,17 +275,17 @@ class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider, ITyp } /** - * Write out generated srgs + * Write out generated mappings */ - public void writeSrgs() { - this.obf.writeSrgs(this.mixins); + public void writeMappings() { + this.obf.writeMappings(); } /** - * Write out stored mappings + * Write out stored references */ - public void writeRefs() { - this.obf.writeRefs(); + public void writeReferences() { + this.obf.writeReferences(); } /** diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/InjectionObfuscationProcessor.java b/src/ap/java/org/spongepowered/tools/obfuscation/InjectionObfuscationProcessor.java index 832b5600..38a1167a 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/InjectionObfuscationProcessor.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/InjectionObfuscationProcessor.java @@ -46,7 +46,7 @@ import org.spongepowered.tools.MirrorUtils; /** * Annotation processor which finds {@link Inject} and {@link At} annotations in - * mixin classes and generates SRG mappings + * mixin classes and generates obfuscation mappings */ @SupportedAnnotationTypes({ "org.spongepowered.asm.mixin.injection.Inject", @@ -83,7 +83,7 @@ public class InjectionObfuscationProcessor extends MixinProcessor { super.postProcess(roundEnv); try { - this.mixins.writeRefs(); + this.mixins.writeReferences(); } catch (Exception ex) { ex.printStackTrace(); } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/Mappings.java b/src/ap/java/org/spongepowered/tools/obfuscation/Mappings.java new file mode 100644 index 00000000..b0a3c7fb --- /dev/null +++ b/src/ap/java/org/spongepowered/tools/obfuscation/Mappings.java @@ -0,0 +1,112 @@ +/* + * 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.Map; + +import org.spongepowered.asm.obfuscation.mapping.common.MappingField; +import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod; +import org.spongepowered.tools.obfuscation.mapping.IMappingConsumer; +import org.spongepowered.tools.obfuscation.mapping.IMappingConsumer.MappingSet.Pair; + +/** + * Reference implementation of mapping consumer + */ +class Mappings implements IMappingConsumer { + + /** + * Stored (ordered) field mappings + */ + private final Map> fieldMappings = + new HashMap>(); + + /** + * Stored (ordered) method mappings + */ + private final Map> methodMappings = + new HashMap>(); + + + public Mappings() { + this.init(); + } + + private void init() { + for (ObfuscationType obfType : ObfuscationType.types()) { + this.fieldMappings.put(obfType, new MappingSet()); + this.methodMappings.put(obfType, new MappingSet()); + } + } + + /** + * Get stored field mappings + */ + @Override + public MappingSet getFieldMappings(ObfuscationType type) { + MappingSet mappings = this.fieldMappings.get(type); + return mappings != null ? mappings : new MappingSet(); + } + + /** + * Get stored method mappings + */ + @Override + public MappingSet getMethodMappings(ObfuscationType type) { + MappingSet mappings = this.methodMappings.get(type); + return mappings != null ? mappings : new MappingSet(); + } + + /** + * Clear all stored mappings + */ + @Override + public void clear() { + this.fieldMappings.clear(); + this.methodMappings.clear(); + this.init(); + } + + @Override + public void addFieldMapping(ObfuscationType type, MappingField from, MappingField to) { + MappingSet mappings = this.fieldMappings.get(type); + if (mappings == null) { + mappings = new MappingSet(); + this.fieldMappings.put(type, mappings); + } + mappings.add(new Pair(from, to)); + } + + @Override + public void addMethodMapping(ObfuscationType type, MappingMethod from, MappingMethod to) { + MappingSet mappings = this.methodMappings.get(type); + if (mappings == null) { + mappings = new MappingSet(); + this.methodMappings.put(type, mappings); + } + mappings.add(new Pair(from, to)); + } + +} diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/MixinProcessor.java b/src/ap/java/org/spongepowered/tools/obfuscation/MixinProcessor.java index 028eec49..fba47d11 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/MixinProcessor.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/MixinProcessor.java @@ -89,7 +89,7 @@ public abstract class MixinProcessor extends AbstractProcessor { @Override public Set getSupportedOptions() { - return SupportedOptions.all; + return SupportedOptions.getAllOptions(); } } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationData.java b/src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationData.java index 232bbca2..3c664c2b 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationData.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationData.java @@ -28,7 +28,7 @@ import java.util.HashMap; import java.util.Iterator; import java.util.Map; -import org.spongepowered.asm.obfuscation.SrgMethod; +import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod; /** * Return value struct for various obfuscation queries performed by the mixin @@ -43,7 +43,7 @@ import org.spongepowered.asm.obfuscation.SrgMethod; * {@link #get} to retrieve the remapped entries for each key.

* * @param Contained type of the data, usually {@link String} for field - * and type obfuscations, and {@link SrgMethod} for methods. + * and type obfuscations, and {@link MappingMethod} for methods. */ public class ObfuscationData implements Iterable { @@ -85,7 +85,7 @@ public class ObfuscationData implements Iterable { private String listValues() { StringBuilder sb = new StringBuilder(); for (ObfuscationType type : this.data.keySet()) { - sb.append(type.name()).append('=').append(this.data.get(type)).append(','); + sb.append(type.getKey()).append('=').append(this.data.get(type)).append(','); } return sb.toString(); } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationDataProvider.java b/src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationDataProvider.java new file mode 100644 index 00000000..cf1b8ef0 --- /dev/null +++ b/src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationDataProvider.java @@ -0,0 +1,266 @@ +/* + * 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.List; + +import org.spongepowered.asm.mixin.injection.struct.MemberInfo; +import org.spongepowered.asm.obfuscation.mapping.IMapping; +import org.spongepowered.asm.obfuscation.mapping.IMapping.Type; +import org.spongepowered.asm.obfuscation.mapping.common.MappingField; +import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod; +import org.spongepowered.asm.util.Constants; +import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor; +import org.spongepowered.tools.obfuscation.interfaces.IObfuscationDataProvider; + +/** + * Implementation of obfuscation provider which queries all obfuscation + * environments to return mappings for each source member + */ +public class ObfuscationDataProvider implements IObfuscationDataProvider { + + /** + * Annotation processor + */ + private final IMixinAnnotationProcessor ap; + + /** + * Available obfuscation environments + */ + private final List environments; + + public ObfuscationDataProvider(IMixinAnnotationProcessor ap, List environments) { + this.ap = ap; + this.environments = environments; + } + + /* (non-Javadoc) + * @see org.spongepowered.tools.obfuscation.IObfuscationManager + * #getObfEntryRecursive( + * org.spongepowered.asm.mixin.injection.struct.MemberInfo) + */ + @Override + @SuppressWarnings("unchecked") + public ObfuscationData getObfEntryRecursive(final MemberInfo targetMember) { + MemberInfo currentTarget = targetMember; + ObfuscationData obfTargetNames = this.getObfClass(currentTarget.owner); + ObfuscationData obfData = this.getObfEntry(currentTarget); + try { + while (obfData.isEmpty()) { + TypeHandle targetType = this.ap.getTypeProvider().getTypeHandle(currentTarget.owner); + if (targetType == null) { + return obfData; + } + TypeHandle superClass = targetType.getSuperclass(); + if (superClass == null) { + return obfData; + } + currentTarget = currentTarget.move(superClass.getName()); + obfData = this.getObfEntry(currentTarget); + if (!obfData.isEmpty()) { + for (ObfuscationType type : obfData) { + String obfClass = obfTargetNames.get(type); + T obfMember = obfData.get(type); + obfData.add(type, (T)MemberInfo.fromMapping((IMapping)obfMember).move(obfClass).asMapping()); + } + } + } + } catch (Exception ex) { + ex.printStackTrace(); + return this.getObfEntry(targetMember); + } + return obfData; + } + + /* (non-Javadoc) + * @see org.spongepowered.tools.obfuscation.IObfuscationManager + * #getObfEntry( + * org.spongepowered.asm.mixin.injection.struct.MemberInfo) + */ + @Override + @SuppressWarnings("unchecked") + public ObfuscationData getObfEntry(MemberInfo targetMember) { + if (targetMember.isField()) { + return (ObfuscationData)this.getObfField(targetMember); + } + return (ObfuscationData)this.getObfMethod(targetMember.asMethodMapping()); + } + + @SuppressWarnings("unchecked") + @Override + public ObfuscationData getObfEntry(IMapping mapping) { + if (mapping != null) { + if (mapping.getType() == Type.FIELD) { + return (ObfuscationData) this.getObfField((MappingField)mapping); + } else if (mapping.getType() == Type.METHOD) { + return (ObfuscationData) this.getObfMethod((MappingMethod)mapping); + } + } + + return new ObfuscationData(); + } + + /* (non-Javadoc) + * @see org.spongepowered.tools.obfuscation.IObfuscationManager + * #getObfMethodRecursive( + * org.spongepowered.asm.mixin.injection.struct.MemberInfo) + */ + @Override + public ObfuscationData getObfMethodRecursive(MemberInfo targetMember) { + return this.getObfEntryRecursive(targetMember); + } + + /* (non-Javadoc) + * @see org.spongepowered.tools.obfuscation.IObfuscationManager + * #getObfMethod( + * org.spongepowered.asm.mixin.injection.struct.MemberInfo) + */ + @Override + public ObfuscationData getObfMethod(MemberInfo method) { + ObfuscationData data = new ObfuscationData(); + + for (ObfuscationEnvironment env : this.environments) { + MappingMethod obfMethod = env.getObfMethod(method); + if (obfMethod != null) { + data.add(env.getType(), obfMethod); + } + } + + if (!data.isEmpty() || !Constants.CTOR.equals(method.name)) { + return data; + } + + return this.remapDescriptor(data, method); + } + + /* (non-Javadoc) + * @see org.spongepowered.tools.obfuscation.interfaces.IObfuscationProvider + * #getObfMethod( + * org.spongepowered.asm.obfuscation.mapping.common.MappingMethod) + */ + @Override + public ObfuscationData getObfMethod(MappingMethod method) { + ObfuscationData data = new ObfuscationData(); + + for (ObfuscationEnvironment env : this.environments) { + MappingMethod obfMethod = env.getObfMethod(method); + if (obfMethod != null) { + data.add(env.getType(), obfMethod); + } + } + + if (!data.isEmpty() || !Constants.CTOR.equals(method.getSimpleName())) { + return data; + } + + return this.remapDescriptor(data, new MemberInfo(method)); + } + + /** + * Remap a method owner and descriptor only, used for remapping ctors + * + * @param data Output method data collection + * @param method Method to remap + * @return data + */ + public ObfuscationData remapDescriptor(ObfuscationData data, MemberInfo method) { + for (ObfuscationEnvironment env : this.environments) { + MemberInfo obfMethod = env.remapDescriptor(method); + if (obfMethod != null) { + data.add(env.getType(), obfMethod.asMethodMapping()); + } + } + + return data; + } + + /* (non-Javadoc) + * @see org.spongepowered.tools.obfuscation.IObfuscationManager + * #getObfFieldRecursive( + * org.spongepowered.asm.mixin.injection.struct.MemberInfo) + */ + @Override + public ObfuscationData getObfFieldRecursive(MemberInfo targetMember) { + return this.getObfEntryRecursive(targetMember); + } + + /* (non-Javadoc) + * @see org.spongepowered.tools.obfuscation.IObfuscationManager + * #getObfField(java.lang.String) + */ + @Override + public ObfuscationData getObfField(MemberInfo field) { + return this.getObfField(field.asFieldMapping()); + } + + /* (non-Javadoc) + * @see org.spongepowered.tools.obfuscation.IObfuscationManager + * #getObfField(java.lang.String) + */ + @Override + public ObfuscationData getObfField(MappingField field) { + ObfuscationData data = new ObfuscationData(); + + for (ObfuscationEnvironment env : this.environments) { + MappingField obfField = env.getObfField(field); + if (obfField != null) { + if (obfField.getDesc() == null && field.getDesc() != null) { + obfField = obfField.transform(env.remapDescriptor(field.getDesc())); + } + data.add(env.getType(), obfField); + } + } + + return data; + } + + /* (non-Javadoc) + * @see org.spongepowered.tools.obfuscation.IObfuscationManager + * #getObfClass(org.spongepowered.tools.obfuscation.TypeHandle) + */ + @Override + public ObfuscationData getObfClass(TypeHandle type) { + return this.getObfClass(type.getName()); + } + + /* (non-Javadoc) + * @see org.spongepowered.tools.obfuscation.IObfuscationManager + * #getObfClass(java.lang.String) + */ + @Override + public ObfuscationData getObfClass(String className) { + ObfuscationData data = new ObfuscationData(className); + + for (ObfuscationEnvironment env : this.environments) { + String obfClass = env.getObfClass(className); + if (obfClass != null) { + data.add(env.getType(), obfClass); + } + } + + return data; + } + +} diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/TargetObfuscationEnvironment.java b/src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationEnvironment.java similarity index 52% rename from src/ap/java/org/spongepowered/tools/obfuscation/TargetObfuscationEnvironment.java rename to src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationEnvironment.java index 1ec418d2..1d978e7a 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/TargetObfuscationEnvironment.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationEnvironment.java @@ -25,39 +25,36 @@ package org.spongepowered.tools.obfuscation; import java.io.File; -import java.io.IOException; -import java.io.PrintWriter; -import java.util.LinkedHashSet; +import java.util.Collection; import java.util.List; -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.Diagnostic.Kind; -import javax.tools.FileObject; -import javax.tools.StandardLocation; import org.spongepowered.asm.mixin.injection.struct.MemberInfo; -import org.spongepowered.asm.mixin.refmap.ReferenceMapper; -import org.spongepowered.asm.obfuscation.SrgMethod; -import org.spongepowered.asm.obfuscation.SrgContainer; -import org.spongepowered.asm.obfuscation.SrgField; +import org.spongepowered.asm.obfuscation.mapping.common.MappingField; +import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod; import org.spongepowered.asm.util.ObfuscationUtil; import org.spongepowered.asm.util.ObfuscationUtil.IClassRemapper; +import org.spongepowered.tools.obfuscation.mapping.IMappingConsumer; +import org.spongepowered.tools.obfuscation.mapping.IMappingProvider; +import org.spongepowered.tools.obfuscation.mapping.IMappingWriter; +import org.spongepowered.tools.obfuscation.mapping.IMappingConsumer.MappingSet; import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor; /** - * Stores information relevant to a particular target obfuscation environment. + * Stores information relevant to a particular obfuscation environment. * - *

We classify different types of possible obfuscation (eg. "searge", "notch" - * ) as obfuscation environments and store related information such as - * the input SRG mappings, generated refmap, generated SRGs, here.

+ *

We classify different types of possible obfuscation (eg. "searge", + * "notch") as obfuscation environments and store related information + * such as the input mappings, generated refmap and generated mappings here.

*/ -class TargetObfuscationEnvironment { +public abstract class ObfuscationEnvironment { /** * Remapping proxy for remapping descriptors @@ -66,88 +63,95 @@ class TargetObfuscationEnvironment { @Override public String map(String typeName) { - if (TargetObfuscationEnvironment.this.srgs == null) { + if (ObfuscationEnvironment.this.mappingProvider == null) { return null; } - return TargetObfuscationEnvironment.this.srgs.getClassMapping(typeName); + return ObfuscationEnvironment.this.mappingProvider.getClassMapping(typeName); } @Override public String unmap(String typeName) { - if (TargetObfuscationEnvironment.this.srgs == null) { + if (ObfuscationEnvironment.this.mappingProvider == null) { return null; } - return TargetObfuscationEnvironment.this.srgs.getClassMapping(typeName); + return ObfuscationEnvironment.this.mappingProvider.getClassMapping(typeName); } } - /** - * SRG container for mcp->? mappings - */ - protected SrgContainer srgs; - - private final RemapperProxy remapper = new RemapperProxy(); - /** * Type */ - private final ObfuscationType type; + protected final ObfuscationType type; + + /** + * Mapping provider + */ + protected final IMappingProvider mappingProvider; + + protected final IMappingWriter mappingWriter; + + protected final RemapperProxy remapper = new RemapperProxy(); /** * Annotation processor */ - private final IMixinAnnotationProcessor ap; + protected final IMixinAnnotationProcessor ap; /** - * Name of the resource to write generated srgs to + * Name of the resource to write generated mappings to */ - private final String outSrgFileName; + protected final String outFileName; /** - * File containing the reobfd srgs + * File containing the source mappings */ - private final List reobfSrgFileNames; + protected final List inFileNames; /** - * Reference mapper for reference mapping - */ - private final ReferenceMapper refMapper; - - /** - * True once we've tried to initialise the srgs, initially false so that we - * can do srg init lazily + * True once we've tried to initialise the mappings, initially false so that + * we can do mapping init lazily */ private boolean initDone; - public TargetObfuscationEnvironment(IMixinAnnotationProcessor ap, ObfuscationType type, ReferenceMapper refMapper) { - this.ap = ap; + protected ObfuscationEnvironment(ObfuscationType type) { this.type = type; - this.refMapper = refMapper; + this.ap = type.getAnnotationProcessor(); - this.reobfSrgFileNames = type.getSrgFileNames(ap); - this.outSrgFileName = type.getOutputSrgFileName(ap); + this.inFileNames = type.getInputFileNames(); + this.outFileName = type.getOutputFileName(); + + this.mappingProvider = this.getMappingProvider(this.ap, this.ap.getProcessingEnvironment().getFiler()); + this.mappingWriter = this.getMappingWriter(this.ap, this.ap.getProcessingEnvironment().getFiler()); } - private boolean initSrgs() { + @Override + public String toString() { + return this.type.toString(); + } + + protected abstract IMappingProvider getMappingProvider(Messager messager, Filer filer); + + protected abstract IMappingWriter getMappingWriter(Messager messager, Filer filer); + + private boolean initMappings() { if (!this.initDone) { this.initDone = true; - if (this.reobfSrgFileNames == null) { - this.ap.printMessage(Kind.ERROR, "The " + this.type.getSrgFileOption() + if (this.inFileNames == null) { + this.ap.printMessage(Kind.ERROR, "The " + this.type.getConfig().getInputFileOption() + " argument was not supplied, obfuscation processing will not occur"); return false; } int successCount = 0; - this.srgs = new SrgContainer(); - for (String srgFileName : this.reobfSrgFileNames) { - File reobfSrgFile = new File(srgFileName); + for (String inputFileName : this.inFileNames) { + File inputFile = new File(inputFileName); try { - if (reobfSrgFile.isFile()) { - this.ap.printMessage(Kind.NOTE, "Loading " + this.type + " mappings from " + reobfSrgFile.getAbsolutePath()); - this.srgs.readSrg(reobfSrgFile); + if (inputFile.isFile()) { + this.ap.printMessage(Kind.NOTE, "Loading " + this.type + " mappings from " + inputFile.getAbsolutePath()); + this.mappingProvider.read(inputFile); successCount++; } } catch (Exception ex) { @@ -156,12 +160,12 @@ class TargetObfuscationEnvironment { } if (successCount < 1) { - this.ap.printMessage(Kind.ERROR, "No valid SRG files for " + this.type + " could be read, processing may not be sucessful."); - this.srgs = null; + this.ap.printMessage(Kind.ERROR, "No valid input files for " + this.type + " could be read, processing may not be sucessful."); + this.mappingProvider.clear(); } } - return this.srgs != null; + return !this.mappingProvider.isEmpty(); } /** @@ -171,18 +175,11 @@ class TargetObfuscationEnvironment { 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.ap)) { - this.refMapper.addMapping(null, className, reference, newReference); - } - } - /** * Get an obfuscation mapping for a method */ - public SrgMethod getObfMethod(MemberInfo method) { - SrgMethod obfd = this.getObfMethod(method.asSrgMethod()); + public MappingMethod getObfMethod(MemberInfo method) { + MappingMethod obfd = this.getObfMethod(method.asMethodMapping()); if (obfd != null || !method.isFullyQualified()) { return obfd; } @@ -207,21 +204,21 @@ class TargetObfuscationEnvironment { /** * Get an obfuscation mapping for a method */ - public SrgMethod getObfMethod(SrgMethod method) { + public MappingMethod getObfMethod(MappingMethod method) { return this.getObfMethod(method, true); } /** * Get an obfuscation mapping for a method */ - public SrgMethod getObfMethod(SrgMethod method, boolean srgOnly) { - if (this.initSrgs()) { + public MappingMethod getObfMethod(MappingMethod method, boolean lazyRemap) { + if (this.initMappings()) { boolean remapped = true; - SrgMethod originalMethod = method.copy(); - SrgMethod methodMapping = this.srgs.getMethodMapping(method); + MappingMethod originalMethod = method.copy(); + MappingMethod methodMapping = this.mappingProvider.getMethodMapping(method); // If no obf mapping, we can attempt to remap the owner class if (methodMapping == null) { - if (srgOnly) { + if (lazyRemap) { return null; } methodMapping = originalMethod; @@ -234,7 +231,8 @@ class TargetObfuscationEnvironment { if (remapped) { return methodMapping.move(remappedOwner); } - return new SrgMethod(remappedOwner, methodMapping.getSimpleName(), ObfuscationUtil.mapDescriptor(methodMapping.getDesc(), this.remapper)); + String desc = ObfuscationUtil.mapDescriptor(methodMapping.getDesc(), this.remapper); + return new MappingMethod(remappedOwner, methodMapping.getSimpleName(), desc); } return null; } @@ -283,96 +281,63 @@ class TargetObfuscationEnvironment { /** * Get an obfuscation mapping for a field */ - public SrgField getObfField(String field) { + public MappingField getObfField(MemberInfo field) { + return this.getObfField(field.asFieldMapping(), true); + } + + /** + * Get an obfuscation mapping for a field + */ + public MappingField getObfField(MappingField field) { return this.getObfField(field, true); } /** * Get an obfuscation mapping for a field */ - public SrgField getObfField(String field, boolean srgOnly) { - if (this.initSrgs()) { - SrgField originalField = new SrgField(field); - SrgField fieldMapping = this.srgs.getFieldMapping(originalField); - // If no obf mapping, we can attempt to remap the owner class - if (fieldMapping == null) { - if (srgOnly) { - return null; - } - fieldMapping = originalField; - } - String remappedOwner = this.getObfClass(fieldMapping.getOwner()); - if (remappedOwner == null || remappedOwner.equals(originalField.getOwner()) || remappedOwner.equals(fieldMapping.getOwner())) { - return fieldMapping != originalField ? fieldMapping : null; - } - return fieldMapping.move(remappedOwner); + public MappingField getObfField(MappingField field, boolean lazyRemap) { + if (!this.initMappings()) { + return null; } - return null; + + MappingField fieldMapping = this.mappingProvider.getFieldMapping(field); + // If no obf mapping, we can attempt to remap the owner class + if (fieldMapping == null) { + if (lazyRemap) { + return null; + } + fieldMapping = field; + } + String remappedOwner = this.getObfClass(fieldMapping.getOwner()); + if (remappedOwner == null || remappedOwner.equals(field.getOwner()) || remappedOwner.equals(fieldMapping.getOwner())) { + return fieldMapping != field ? fieldMapping : null; + } + return fieldMapping.move(remappedOwner); } /** * Get an obfuscation mapping for a class */ public String getObfClass(String className) { - if (this.initSrgs()) { - return this.srgs.getClassMapping(className); + if (!this.initMappings()) { + return null; } - return null; + return this.mappingProvider.getClassMapping(className); } /** - * Write out generated srgs + * Write out generated mappings */ - public void writeSrgs(Filer filer, Map mixins) { - if (this.outSrgFileName == null) { - return; + public void writeMappings(Collection consumers) { + MappingSet fields = new MappingSet(); + MappingSet methods = new MappingSet(); + + for (IMappingConsumer mappings : consumers) { + fields.addAll(mappings.getFieldMappings(this.type)); + methods.addAll(mappings.getMethodMappings(this.type)); } - - 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 - } - } - } - } - - /** - * 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.ap.printMessage(Kind.NOTE, "Writing " + description + " to " + outSrgFile.getAbsolutePath()); - return new PrintWriter(outSrgFile); - } - - FileObject outSrg = filer.createResource(StandardLocation.CLASS_OUTPUT, "", fileName); - this.ap.printMessage(Kind.NOTE, "Writing " + description + " to " + new File(outSrg.toUri()).getAbsolutePath()); - return new PrintWriter(outSrg.openWriter()); + + this.mappingWriter.write(this.outFileName, this.type, fields, methods); } } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationManager.java b/src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationManager.java index f4a071e4..adf13be8 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationManager.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationManager.java @@ -24,24 +24,15 @@ */ package org.spongepowered.tools.obfuscation; -import java.io.File; -import java.io.IOException; -import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; -import java.util.Map; -import javax.tools.Diagnostic.Kind; -import javax.tools.FileObject; -import javax.tools.StandardLocation; - -import org.spongepowered.asm.mixin.injection.struct.MemberInfo; -import org.spongepowered.asm.mixin.refmap.ReferenceMapper; -import org.spongepowered.asm.obfuscation.SrgField; -import org.spongepowered.asm.obfuscation.SrgMethod; -import org.spongepowered.asm.util.Constants; import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor; import org.spongepowered.tools.obfuscation.interfaces.IObfuscationManager; +import org.spongepowered.tools.obfuscation.interfaces.IObfuscationDataProvider; +import org.spongepowered.tools.obfuscation.interfaces.IReferenceManager; +import org.spongepowered.tools.obfuscation.mapping.IMappingConsumer; +import org.spongepowered.tools.obfuscation.service.ObfuscationServices; /** * Obfuscation Manager for mixin Annotation Processor @@ -54,336 +45,85 @@ public class ObfuscationManager implements IObfuscationManager { private final IMixinAnnotationProcessor ap; /** - * Name of the resource to write remapped refs to + * Available obfuscation environments */ - private final String outRefMapFileName; + private final List environments = new ArrayList(); /** - * Target obfuscation environments + * Obfuscation provider */ - private final List targetEnvironments = new ArrayList(); + private final IObfuscationDataProvider obfs; /** - * Reference mapper for reference mapping + * Refmap manager */ - private final ReferenceMapper refMapper = new ReferenceMapper(); + private final IReferenceManager refs; + + /** + * Mapping consumers + */ + private final List consumers = new ArrayList(); + + private boolean initDone; public ObfuscationManager(IMixinAnnotationProcessor ap) { this.ap = ap; - this.outRefMapFileName = this.ap.getOption(SupportedOptions.OUT_REFMAP_FILE); - for (ObfuscationType obfType : ObfuscationType.values()) { - if (obfType.isSupported(this.ap)) { - this.targetEnvironments.add(new TargetObfuscationEnvironment(ap, obfType, this.refMapper)); + this.obfs = new ObfuscationDataProvider(ap, this.environments); + this.refs = new ReferenceManager(ap, this.environments); + } + + @Override + public void init() { + if (this.initDone) { + return; + } + this.initDone = true; + ObfuscationServices.getInstance().initProviders(this.ap); + for (ObfuscationType obfType : ObfuscationType.types()) { + if (obfType.isSupported()) { + this.environments.add(obfType.createEnvironment()); } } } - /* (non-Javadoc) - * @see org.spongepowered.tools.obfuscation.IObfuscationManager - * #getReferenceMapper() - */ @Override - public ReferenceMapper getReferenceMapper() { - return this.refMapper; + public IObfuscationDataProvider getDataProvider() { + return this.obfs; + } + + @Override + public IReferenceManager getReferenceManager() { + return this.refs; + } + + @Override + public IMappingConsumer createMappingConsumer() { + Mappings mappings = new Mappings(); + this.consumers.add(mappings); + return mappings; + } + + @Override + public List getEnvironments() { + return this.environments; } /** - * Write out generated srgs + * Write out generated mappings */ - public void writeSrgs(Map mixins) { - for (TargetObfuscationEnvironment targetEnv : this.targetEnvironments) { - targetEnv.writeSrgs(this.ap.getProcessingEnvironment().getFiler(), mixins); + @Override + public void writeMappings() { + for (ObfuscationEnvironment env : this.environments) { + env.writeMappings(this.consumers); } } /** * Write out stored mappings */ - public void writeRefs() { - if (this.outRefMapFileName == null) { - return; - } - - PrintWriter writer = null; - - try { - writer = this.newWriter(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 newWriter(String fileName, String description) throws IOException { - if (fileName.matches("^.*[\\\\/:].*$")) { - File outSrgFile = new File(fileName); - outSrgFile.getParentFile().mkdirs(); - this.ap.printMessage(Kind.NOTE, "Writing " + description + " to " + outSrgFile.getAbsolutePath()); - return new PrintWriter(outSrgFile); - } - - FileObject outSrg = this.ap.getProcessingEnvironment().getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", fileName); - this.ap.printMessage(Kind.NOTE, "Writing " + description + " to " + new File(outSrg.toUri()).getAbsolutePath()); - return new PrintWriter(outSrg.openWriter()); - } - - /* (non-Javadoc) - * @see org.spongepowered.tools.obfuscation.IObfuscationManager - * #getObfEntryRecursive( - * org.spongepowered.asm.mixin.injection.struct.MemberInfo) - */ @Override - @SuppressWarnings("unchecked") - public ObfuscationData getObfEntryRecursive(final MemberInfo targetMember) { - MemberInfo currentTarget = targetMember; - ObfuscationData obfTargetNames = this.getObfClass(currentTarget.owner); - ObfuscationData obfData = this.getObfEntry(currentTarget); - try { - while (obfData.isEmpty()) { - TypeHandle targetType = this.ap.getTypeProvider().getTypeHandle(currentTarget.owner); - if (targetType == null) { - return obfData; - } - TypeHandle superClass = targetType.getSuperclass(); - if (superClass == null) { - return obfData; - } - currentTarget = currentTarget.move(superClass.getName()); - obfData = this.getObfEntry(currentTarget); - if (!obfData.isEmpty()) { - for (ObfuscationType type : obfData) { - String obfClass = obfTargetNames.get(type); - T obfMember = obfData.get(type); - if (currentTarget.isField()) { - obfData.add(type, (T)MemberInfo.fromSrgField(obfMember.toString(), "").move(obfClass).toSrg()); - } else { - obfData.add(type, (T)MemberInfo.fromSrgMethod((SrgMethod)obfMember).move(obfClass).asSrgMethod()); - } - } - } - } - } catch (Exception ex) { - return this.getObfEntry(targetMember); - } - return obfData; + public void writeReferences() { + this.refs.write(); } - /* (non-Javadoc) - * @see org.spongepowered.tools.obfuscation.IObfuscationManager - * #getObfEntry( - * org.spongepowered.asm.mixin.injection.struct.MemberInfo) - */ - @Override - @SuppressWarnings("unchecked") - public ObfuscationData getObfEntry(MemberInfo targetMember) { - if (targetMember.isField()) { - return (ObfuscationData)this.getObfField(targetMember.toSrg()); - } - return (ObfuscationData)this.getObfMethod(targetMember.asSrgMethod()); - } - - /* (non-Javadoc) - * @see org.spongepowered.tools.obfuscation.IObfuscationManager - * #getObfMethodRecursive( - * org.spongepowered.asm.mixin.injection.struct.MemberInfo) - */ - @Override - public ObfuscationData getObfMethodRecursive(MemberInfo targetMember) { - return this.getObfEntryRecursive(targetMember); - } - - /* (non-Javadoc) - * @see org.spongepowered.tools.obfuscation.IObfuscationManager - * #getObfMethod( - * org.spongepowered.asm.mixin.injection.struct.MemberInfo) - */ - @Override - public ObfuscationData getObfMethod(MemberInfo method) { - ObfuscationData data = new ObfuscationData(); - - for (TargetObfuscationEnvironment targetEnv : this.targetEnvironments) { - SrgMethod obfMethod = targetEnv.getObfMethod(method); - if (obfMethod != null) { - data.add(targetEnv.getType(), obfMethod); - } - } - - if (!data.isEmpty() || !Constants.CTOR.equals(method.name)) { - return data; - } - - return this.remapDescriptor(data, method); - } - - /* (non-Javadoc) - * @see org.spongepowered.tools.obfuscation.interfaces.IObfuscationManager - * #getObfMethod(org.spongepowered.asm.obfuscation.SrgMethod) - */ - @Override - public ObfuscationData getObfMethod(SrgMethod method) { - ObfuscationData data = new ObfuscationData(); - - for (TargetObfuscationEnvironment targetEnv : this.targetEnvironments) { - SrgMethod obfMethod = targetEnv.getObfMethod(method); - if (obfMethod != null) { - data.add(targetEnv.getType(), obfMethod); - } - } - - if (!data.isEmpty() || !Constants.CTOR.equals(method.getSimpleName())) { - return data; - } - - return this.remapDescriptor(data, new MemberInfo(method)); - } - - /** - * Remap a method owner and descriptor only, used for remapping ctors - * - * @param data Output method data collection - * @param method Method to remap - * @return data - */ - public ObfuscationData remapDescriptor(ObfuscationData data, MemberInfo method) { - for (TargetObfuscationEnvironment targetEnv : this.targetEnvironments) { - MemberInfo obfMethod = targetEnv.remapDescriptor(method); - if (obfMethod != null) { - data.add(targetEnv.getType(), obfMethod.asSrgMethod()); - } - } - - return data; - } - - /* (non-Javadoc) - * @see org.spongepowered.tools.obfuscation.IObfuscationManager - * #getObfFieldRecursive( - * org.spongepowered.asm.mixin.injection.struct.MemberInfo) - */ - @Override - public ObfuscationData getObfFieldRecursive(MemberInfo targetMember) { - return this.getObfEntryRecursive(targetMember); - } - - /* (non-Javadoc) - * @see org.spongepowered.tools.obfuscation.IObfuscationManager - * #getObfField(java.lang.String) - */ - @Override - public ObfuscationData getObfField(String field) { - ObfuscationData data = new ObfuscationData(); - - for (TargetObfuscationEnvironment targetEnv : this.targetEnvironments) { - SrgField obfField = targetEnv.getObfField(field); - if (obfField != null) { - data.add(targetEnv.getType(), obfField.toString()); - } - } - - return data; - } - - /* (non-Javadoc) - * @see org.spongepowered.tools.obfuscation.IObfuscationManager - * #getObfClass(org.spongepowered.tools.obfuscation.TypeHandle) - */ - @Override - public ObfuscationData getObfClass(TypeHandle type) { - return this.getObfClass(type.getName()); - } - - /* (non-Javadoc) - * @see org.spongepowered.tools.obfuscation.IObfuscationManager - * #getObfClass(java.lang.String) - */ - @Override - public ObfuscationData getObfClass(String className) { - ObfuscationData data = new ObfuscationData(className); - - for (TargetObfuscationEnvironment targetEnv : this.targetEnvironments) { - String obfClass = targetEnv.getObfClass(className); - if (obfClass != null) { - data.add(targetEnv.getType(), obfClass); - } - } - - return data; - } - - /* (non-Javadoc) - * @see org.spongepowered.tools.obfuscation.interfaces.IObfuscationManager - * #addMethodMapping(java.lang.String, java.lang.String, - * org.spongepowered.tools.obfuscation.ObfuscationData) - */ - @Override - public void addMethodMapping(String className, String reference, ObfuscationData obfMethodData) { - for (TargetObfuscationEnvironment targetEnv : this.targetEnvironments) { - SrgMethod obfMethod = obfMethodData.get(targetEnv.getType()); - if (obfMethod != null) { - MemberInfo remappedReference = new MemberInfo(obfMethod); - targetEnv.addMapping(className, reference, remappedReference.toString()); - } - } - } - - /* (non-Javadoc) - * @see org.spongepowered.tools.obfuscation.interfaces.IObfuscationManager - * #addMethodMapping(java.lang.String, java.lang.String, - * org.spongepowered.asm.mixin.injection.struct.MemberInfo, - * org.spongepowered.tools.obfuscation.ObfuscationData) - */ - @Override - public void addMethodMapping(String className, String reference, MemberInfo context, ObfuscationData obfMethodData) { - for (TargetObfuscationEnvironment targetEnv : this.targetEnvironments) { - SrgMethod obfMethod = obfMethodData.get(targetEnv.getType()); - if (obfMethod != null) { - MemberInfo remappedReference = context.remapUsing(obfMethod, true); - targetEnv.addMapping(className, reference, remappedReference.toString()); - } - } - } - - /* (non-Javadoc) - * @see org.spongepowered.tools.obfuscation.interfaces.IObfuscationManager - * #addFieldMapping(java.lang.String, java.lang.String, - * org.spongepowered.asm.mixin.injection.struct.MemberInfo, - * org.spongepowered.tools.obfuscation.ObfuscationData) - */ - @Override - public 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, targetEnv.remapDescriptor(context.desc)); - targetEnv.addMapping(className, reference, remappedReference.toString()); - } - } - } - - /* (non-Javadoc) - * @see org.spongepowered.tools.obfuscation.interfaces.IObfuscationManager - * #addClassMapping(java.lang.String, java.lang.String, - * org.spongepowered.tools.obfuscation.ObfuscationData) - */ - @Override - public void addClassMapping(String className, String reference, ObfuscationData obfClassData) { - for (TargetObfuscationEnvironment targetEnv : this.targetEnvironments) { - String remapped = obfClassData.get(targetEnv.getType()); - if (remapped != null) { - targetEnv.addMapping(className, reference, remapped); - } - } - } - -} +} \ No newline at end of file diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationType.java b/src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationType.java index 679a122b..d9f48444 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationType.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationType.java @@ -24,9 +24,15 @@ */ package org.spongepowered.tools.obfuscation; +import java.lang.reflect.Constructor; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; +import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor; import org.spongepowered.tools.obfuscation.interfaces.IOptionProvider; +import org.spongepowered.tools.obfuscation.mcp.ObfuscationServiceMCP; +import org.spongepowered.tools.obfuscation.service.ObfuscationTypeDescriptor; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList.Builder; @@ -34,47 +40,34 @@ import com.google.common.collect.ImmutableList.Builder; /** * Obfuscation types supported by the annotation processor */ -enum ObfuscationType { +public final class ObfuscationType { - /** - * "SEARGE" mappings, these are unique mappings used by MCP and derived - * projects - */ - SRG("searge", SupportedOptions.REOBF_SRG_FILE, SupportedOptions.REOBF_EXTRA_SRG_FILES, SupportedOptions.OUT_SRG_SRG_FILE), + private static final Map types = new LinkedHashMap(); - /** - * "NOTCH" mappings, internal to minecraft only - */ - NOTCH("notch", SupportedOptions.REOBF_NOTCH_FILE, SupportedOptions.REOBF_EXTRA_NOTCH_FILES, SupportedOptions.OUT_NOTCH_SRG_FILE); - - /** - * Refmap key for this mapping type - */ private final String key; + + private final ObfuscationTypeDescriptor descriptor; - /** - * The name of the Annotation Processor argument which contains the file - * name to read the SRGs for this obfuscation type from - */ - private final String srgFileArgName; + private final IMixinAnnotationProcessor ap; - /** - * The name of the Annotation Processor argument which contains the file - * name to read the extra SRGs for this obfuscation type from - */ - private final String extraSrgFilesArgName; + private final IOptionProvider options; - /** - * The name of the Annotation Processor argument which contains the file - * name to write generated SRG mappings to - */ - private final String outSrgFileArgName; + private ObfuscationType(ObfuscationTypeDescriptor descriptor, IMixinAnnotationProcessor ap) { + this.key = descriptor.getKey(); + this.descriptor = descriptor; + this.ap = ap; + this.options = ap; + } - private ObfuscationType(String displayName, String srgFileArgName, String extraSrgFilesArgName, String outSrgFileArgName) { - this.key = displayName; - this.srgFileArgName = srgFileArgName; - this.extraSrgFilesArgName = extraSrgFilesArgName; - this.outSrgFileArgName = outSrgFileArgName; + public final ObfuscationEnvironment createEnvironment() { + try { + Class cls = this.descriptor.getEnvironmentType(); + Constructor ctor = cls.getDeclaredConstructor(ObfuscationType.class); + ctor.setAccessible(true); + return ctor.newInstance(this); + } catch (Exception ex) { + throw new RuntimeException(ex); + } } @Override @@ -82,51 +75,70 @@ enum ObfuscationType { return this.key; } + public String getKey() { return this.key; } - - public String getSrgFileOption() { - return this.srgFileArgName; + + public ObfuscationTypeDescriptor getConfig() { + return this.descriptor; } - public String getExtraSrgFilesOption() { - return this.extraSrgFilesArgName; + public IMixinAnnotationProcessor getAnnotationProcessor() { + return this.ap; } - public String getOutputSrgFileOption() { - return this.outSrgFileArgName; + public boolean isDefault() { + String defaultEnv = this.options.getOption(SupportedOptions.DEFAULT_OBFUSCATION_ENV); + return (defaultEnv == null && this.key.equals(ObfuscationServiceMCP.SEARGE)) + || (defaultEnv != null && this.key.equals(defaultEnv.toLowerCase())); } - 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() { + return this.getInputFileNames().size() > 0; } - public boolean isSupported(IOptionProvider options) { - return this.getSrgFileNames(options).size() > 0; - } - - public List getSrgFileNames(IOptionProvider options) { + public List getInputFileNames() { Builder builder = ImmutableList.builder(); - String srgFile = options.getOption(this.srgFileArgName); - if (srgFile != null) { - builder.add(srgFile); + String inputFile = this.options.getOption(this.descriptor.getInputFileOption()); + if (inputFile != null) { + builder.add(inputFile); } - String extraSrgFiles = options.getOption(this.extraSrgFilesArgName); - if (extraSrgFiles != null) { - for (String extraSrgFile : extraSrgFiles.split(";")) { - builder.add(extraSrgFile.trim()); + String extraInputFiles = this.options.getOption(this.descriptor.getExtraInputFilesOption()); + if (extraInputFiles != null) { + for (String extraInputFile : extraInputFiles.split(";")) { + builder.add(extraInputFile.trim()); } } return builder.build(); } - public String getOutputSrgFileName(IOptionProvider options) { - return options.getOption(this.outSrgFileArgName); + public String getOutputFileName() { + return this.options.getOption(this.descriptor.getOutputFileOption()); + } + + public static Iterable types() { + return ObfuscationType.types.values(); + } + + public static ObfuscationType create(ObfuscationTypeDescriptor config, IMixinAnnotationProcessor ap) { + String key = config.getKey(); + if (ObfuscationType.types.containsKey(key)) { + throw new IllegalArgumentException("Obfuscation type with key " + key + " was already registered"); + } + ObfuscationType type = new ObfuscationType(config, ap); + ObfuscationType.types.put(key, type); + return type; + } + + public static ObfuscationType get(String key) { + ObfuscationType type = ObfuscationType.types.get(key); + if (type == null) { + throw new IllegalArgumentException("Obfuscation type with key " + key + " was not registered"); + } + return type; } - } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/ReferenceManager.java b/src/ap/java/org/spongepowered/tools/obfuscation/ReferenceManager.java new file mode 100644 index 00000000..859a1efd --- /dev/null +++ b/src/ap/java/org/spongepowered/tools/obfuscation/ReferenceManager.java @@ -0,0 +1,197 @@ +/* + * 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.List; + +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.refmap.ReferenceMapper; +import org.spongepowered.asm.obfuscation.mapping.common.MappingField; +import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod; +import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor; +import org.spongepowered.tools.obfuscation.interfaces.IReferenceManager; + +/** + * Implementation of the reference manager + */ +public class ReferenceManager implements IReferenceManager { + + /** + * Annotation processor + */ + private final IMixinAnnotationProcessor ap; + + /** + * Name of the resource to write remapped refs to + */ + private final String outRefMapFileName; + + /** + * Available obfuscation environments + */ + private final List environments; + + /** + * Reference mapper for reference mapping + */ + private final ReferenceMapper refMapper = new ReferenceMapper(); + + public ReferenceManager(IMixinAnnotationProcessor ap, List environments) { + this.ap = ap; + this.environments = environments; + this.outRefMapFileName = this.ap.getOption(SupportedOptions.OUT_REFMAP_FILE); + } + + /** + * Write out stored mappings + */ + @Override + public void write() { + if (this.outRefMapFileName == null) { + return; + } + + PrintWriter writer = null; + + try { + writer = this.newWriter(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 newWriter(String fileName, String description) throws IOException { + if (fileName.matches("^.*[\\\\/:].*$")) { + File outFile = new File(fileName); + outFile.getParentFile().mkdirs(); + this.ap.printMessage(Kind.NOTE, "Writing " + description + " to " + outFile.getAbsolutePath()); + return new PrintWriter(outFile); + } + + FileObject outResource = this.ap.getProcessingEnvironment().getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", fileName); + this.ap.printMessage(Kind.NOTE, "Writing " + description + " to " + new File(outResource.toUri()).getAbsolutePath()); + return new PrintWriter(outResource.openWriter()); + } + + /* (non-Javadoc) + * @see org.spongepowered.tools.obfuscation.IObfuscationManager + * #getReferenceMapper() + */ + @Override + public ReferenceMapper getMapper() { + return this.refMapper; + } + + /* (non-Javadoc) + * @see org.spongepowered.tools.obfuscation.interfaces.IObfuscationManager + * #addMethodMapping(java.lang.String, java.lang.String, + * org.spongepowered.tools.obfuscation.ObfuscationData) + */ + @Override + public void addMethodMapping(String className, String reference, ObfuscationData obfMethodData) { + for (ObfuscationEnvironment env : this.environments) { + MappingMethod obfMethod = obfMethodData.get(env.getType()); + if (obfMethod != null) { + MemberInfo remappedReference = new MemberInfo(obfMethod); + this.addMapping(env.getType(), className, reference, remappedReference.toString()); + } + } + } + + /* (non-Javadoc) + * @see org.spongepowered.tools.obfuscation.interfaces.IObfuscationManager + * #addMethodMapping(java.lang.String, java.lang.String, + * org.spongepowered.asm.mixin.injection.struct.MemberInfo, + * org.spongepowered.tools.obfuscation.ObfuscationData) + */ + @Override + public void addMethodMapping(String className, String reference, MemberInfo context, ObfuscationData obfMethodData) { + for (ObfuscationEnvironment env : this.environments) { + MappingMethod obfMethod = obfMethodData.get(env.getType()); + if (obfMethod != null) { + MemberInfo remappedReference = context.remapUsing(obfMethod, true); + this.addMapping(env.getType(), className, reference, remappedReference.toString()); + } + } + } + + /* (non-Javadoc) + * @see org.spongepowered.tools.obfuscation.interfaces.IObfuscationManager + * #addFieldMapping(java.lang.String, java.lang.String, + * org.spongepowered.asm.mixin.injection.struct.MemberInfo, + * org.spongepowered.tools.obfuscation.ObfuscationData) + */ + @Override + public void addFieldMapping(String className, String reference, MemberInfo context, ObfuscationData obfFieldData) { + for (ObfuscationEnvironment env : this.environments) { + MappingField obfField = obfFieldData.get(env.getType()); + if (obfField != null) { + MemberInfo remappedReference = MemberInfo.fromMapping(obfField.transform(env.remapDescriptor(context.desc))); + this.addMapping(env.getType(), className, reference, remappedReference.toString()); + } + } + } + + /* (non-Javadoc) + * @see org.spongepowered.tools.obfuscation.interfaces.IObfuscationManager + * #addClassMapping(java.lang.String, java.lang.String, + * org.spongepowered.tools.obfuscation.ObfuscationData) + */ + @Override + public void addClassMapping(String className, String reference, ObfuscationData obfClassData) { + for (ObfuscationEnvironment env : this.environments) { + String remapped = obfClassData.get(env.getType()); + if (remapped != null) { + this.addMapping(env.getType(), className, reference, remapped); + } + } + } + + protected void addMapping(ObfuscationType type, String className, String reference, String newReference) { + this.refMapper.addMapping(type.getKey(), className, reference, newReference); + if (type.isDefault()) { + this.refMapper.addMapping(null, className, reference, newReference); + } + } +} diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/SupportedOptions.java b/src/ap/java/org/spongepowered/tools/obfuscation/SupportedOptions.java index ac1252d0..af172a6d 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/SupportedOptions.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/SupportedOptions.java @@ -26,7 +26,10 @@ package org.spongepowered.tools.obfuscation; import java.util.Set; +import org.spongepowered.tools.obfuscation.service.ObfuscationServices; + import com.google.common.collect.ImmutableSet; +import com.google.common.collect.ImmutableSet.Builder; /** * Supported options for the annotation processor. Not an enum mainly so that @@ -35,12 +38,6 @@ import com.google.common.collect.ImmutableSet; public final class SupportedOptions { public static final String TOKENS = "tokens"; - public static final String REOBF_SRG_FILE = "reobfSrgFile"; - public static final String REOBF_EXTRA_SRG_FILES = "reobfSrgFiles"; - public static final String REOBF_NOTCH_FILE = "reobfNotchSrgFile"; - public static final String REOBF_EXTRA_NOTCH_FILES = "reobfNotchSrgFiles"; - public static final String OUT_SRG_SRG_FILE = "outSrgFile"; - public static final String OUT_NOTCH_SRG_FILE = "outNotchSrgFile"; public static final String OUT_REFMAP_FILE = "outRefMapFile"; public static final String DISABLE_TARGET_VALIDATOR = "disableTargetValidator"; public static final String DISABLE_TARGET_EXPORT = "disableTargetExport"; @@ -48,24 +45,24 @@ public final class SupportedOptions { public static final String OVERWRITE_ERROR_LEVEL = "overwriteErrorLevel"; public static final String DEFAULT_OBFUSCATION_ENV = "defaultObfuscationEnv"; public static final String DEPENDENCY_TARGETS_FILE = "dependencyTargetsFile"; - - public static final Set all = ImmutableSet.of( - SupportedOptions.TOKENS, - SupportedOptions.REOBF_SRG_FILE, - SupportedOptions.REOBF_EXTRA_SRG_FILES, - SupportedOptions.REOBF_NOTCH_FILE, - SupportedOptions.REOBF_EXTRA_NOTCH_FILES, - SupportedOptions.OUT_SRG_SRG_FILE, - SupportedOptions.OUT_NOTCH_SRG_FILE, - SupportedOptions.OUT_REFMAP_FILE, - SupportedOptions.DISABLE_TARGET_VALIDATOR, - SupportedOptions.DISABLE_TARGET_EXPORT, - SupportedOptions.DISABLE_OVERWRITE_CHECKER, - SupportedOptions.OVERWRITE_ERROR_LEVEL, - SupportedOptions.DEFAULT_OBFUSCATION_ENV, - SupportedOptions.DEPENDENCY_TARGETS_FILE - ); private SupportedOptions() {} + public static Set getAllOptions() { + Builder options = ImmutableSet.builder(); + options.add( + SupportedOptions.TOKENS, + SupportedOptions.OUT_REFMAP_FILE, + SupportedOptions.DISABLE_TARGET_VALIDATOR, + SupportedOptions.DISABLE_TARGET_EXPORT, + SupportedOptions.DISABLE_OVERWRITE_CHECKER, + SupportedOptions.OVERWRITE_ERROR_LEVEL, + SupportedOptions.DEFAULT_OBFUSCATION_ENV, + SupportedOptions.DEPENDENCY_TARGETS_FILE + ); + options.addAll( + ObfuscationServices.getInstance().getSupportedOptions() + ); + return options.build(); + } } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/TargetObfuscationProcessor.java b/src/ap/java/org/spongepowered/tools/obfuscation/TargetObfuscationProcessor.java index 53201a87..525d478b 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/TargetObfuscationProcessor.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/TargetObfuscationProcessor.java @@ -42,7 +42,7 @@ import org.spongepowered.tools.MirrorUtils; /** * Annotation processor which finds {@link Shadow} and {@link Overwrite} - * annotations in mixin classes and generates SRG mappings + * annotations in mixin classes and generates new obfuscation mappings */ @SupportedAnnotationTypes({ "org.spongepowered.asm.mixin.Mixin", "org.spongepowered.asm.mixin.Shadow", "org.spongepowered.asm.mixin.Overwrite" }) public class TargetObfuscationProcessor extends MixinProcessor { @@ -71,7 +71,7 @@ public class TargetObfuscationProcessor extends MixinProcessor { super.postProcess(roundEnv); try { - this.mixins.writeSrgs(); + this.mixins.writeMappings(); } catch (Exception ex) { ex.printStackTrace(); } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/interfaces/IObfuscationDataProvider.java b/src/ap/java/org/spongepowered/tools/obfuscation/interfaces/IObfuscationDataProvider.java new file mode 100644 index 00000000..1c478aaa --- /dev/null +++ b/src/ap/java/org/spongepowered/tools/obfuscation/interfaces/IObfuscationDataProvider.java @@ -0,0 +1,121 @@ +/* + * 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.interfaces; + +import org.spongepowered.asm.mixin.injection.struct.MemberInfo; +import org.spongepowered.asm.obfuscation.mapping.IMapping; +import org.spongepowered.asm.obfuscation.mapping.common.MappingField; +import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod; +import org.spongepowered.tools.obfuscation.ObfuscationData; +import org.spongepowered.tools.obfuscation.TypeHandle; + +/** + * Provides obfuscation data to the annotation processor + */ +public interface IObfuscationDataProvider { + + /** + * Attempts to resolve an obfuscation entry by recursively enumerating + * superclasses of the target member until a match is found. If a match is + * found in a superclass then the reference is remapped to the original + * owner class. + * + * @param targetMember Member to search for + * @return ObfuscationData with remapped owner class corresponding to the + * original owner class + */ + public abstract ObfuscationData getObfEntryRecursive(MemberInfo targetMember); + + /** + * Resolves a field or method reference to an ObfuscationData set + * + * @param targetMember member to search for + * @return obfuscation data (by type) for the supplied member + */ + public abstract ObfuscationData getObfEntry(MemberInfo targetMember); + + /** + * Resolves a field or method reference to an ObfuscationData set + * + * @param mapping member to search for + * @return obfuscation data (by type) for the supplied member + */ + public abstract ObfuscationData getObfEntry(IMapping mapping); + + /** + * Attempts to resolve an obfuscated method by recursively enumerating + * superclasses of the target method until a match is found. If a match is + * found in a superclass then the method owner is remapped to the obfuscated + * name of the original owner class. + * + * @param method Method to search for + * @return ObfuscationData with remapped owner class corresponding to the + * original owner class + */ + public abstract ObfuscationData getObfMethodRecursive(MemberInfo method); + + /** + * Get an obfuscation mapping for a method + */ + public abstract ObfuscationData getObfMethod(MemberInfo method); + + /** + * Get an obfuscation mapping for a method + */ + public abstract ObfuscationData getObfMethod(MappingMethod method); + + /** + * Attempts to resolve an obfuscated field by recursively enumerating + * superclasses of the target field until a match is found. If a match is + * found in a superclass then the field owner is remapped to the obfuscated + * name of the original owner class. + * + * @param field Field to search for + * @return ObfuscationData with remapped owner class corresponding to the + * original owner class + */ + public abstract ObfuscationData getObfFieldRecursive(MemberInfo field); + + /** + * Get an obfuscation mapping for a field + */ + public abstract ObfuscationData getObfField(MemberInfo field); + + /** + * Get an obfuscation mapping for a field + */ + public abstract ObfuscationData getObfField(MappingField field); + + /** + * Get an obfuscation mapping for a class + */ + public abstract ObfuscationData getObfClass(TypeHandle type); + + /** + * Get an obfuscation mapping for a class + */ + public abstract ObfuscationData getObfClass(String className); + +} diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/interfaces/IObfuscationManager.java b/src/ap/java/org/spongepowered/tools/obfuscation/interfaces/IObfuscationManager.java index 846233b2..d439522d 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/interfaces/IObfuscationManager.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/interfaces/IObfuscationManager.java @@ -24,11 +24,10 @@ */ package org.spongepowered.tools.obfuscation.interfaces; -import org.spongepowered.asm.mixin.injection.struct.MemberInfo; -import org.spongepowered.asm.mixin.refmap.ReferenceMapper; -import org.spongepowered.asm.obfuscation.SrgMethod; -import org.spongepowered.tools.obfuscation.ObfuscationData; -import org.spongepowered.tools.obfuscation.TypeHandle; +import java.util.List; + +import org.spongepowered.tools.obfuscation.ObfuscationEnvironment; +import org.spongepowered.tools.obfuscation.mapping.IMappingConsumer; /** * Manages obfuscation things @@ -36,119 +35,38 @@ import org.spongepowered.tools.obfuscation.TypeHandle; public interface IObfuscationManager { /** - * Get the underlying reference mapper + * Initialise the obfuscation environments */ - public abstract ReferenceMapper getReferenceMapper(); + public abstract void init(); /** - * Attempts to resolve an obfuscation entry by recursively enumerating - * superclasses of the target member until a match is found. If a match is - * found in a superclass then the reference is remapped to the original - * owner class. - * - * @param targetMember Member to search for - * @return ObfuscationData with remapped owner class corresponding to the - * original owner class + * Get the obfuscation mapping source */ - public abstract ObfuscationData getObfEntryRecursive(MemberInfo targetMember); + public abstract IObfuscationDataProvider getDataProvider(); + + /** + * Get the reference manager + */ + public abstract IReferenceManager getReferenceManager(); /** - * Resolves a field or method reference to an ObfuscationData set - * - * @param targetMember member to search for - * @return obfuscation data (by type) for the supplied member + * Create a new mapping consumer */ - public abstract ObfuscationData getObfEntry(MemberInfo targetMember); + public abstract IMappingConsumer createMappingConsumer(); /** - * Attempts to resolve an obfuscated method by recursively enumerating - * superclasses of the target method until a match is found. If a match is - * found in a superclass then the method owner is remapped to the obfuscated - * name of the original owner class. - * - * @param method Method to search for - * @return ObfuscationData with remapped owner class corresponding to the - * original owner class + * Get available obfuscation environments within this manager */ - public abstract ObfuscationData getObfMethodRecursive(MemberInfo method); + public abstract List getEnvironments(); /** - * Get an obfuscation mapping for a method + * Write out generated mappings to the target environments */ - public abstract ObfuscationData getObfMethod(MemberInfo method); + public abstract void writeMappings(); /** - * Get an obfuscation mapping for a method + * Write out generated refmap */ - public abstract ObfuscationData getObfMethod(SrgMethod method); - - /** - * Attempts to resolve an obfuscated field by recursively enumerating - * superclasses of the target field until a match is found. If a match is - * found in a superclass then the field owner is remapped to the obfuscated - * name of the original owner class. - * - * @param field Field to search for - * @return ObfuscationData with remapped owner class corresponding to the - * original owner class - */ - public abstract ObfuscationData getObfFieldRecursive(MemberInfo field); - - /** - * Get an obfuscation mapping for a field - */ - public abstract ObfuscationData getObfField(String field); - - /** - * Get an obfuscation mapping for a class - */ - public abstract ObfuscationData getObfClass(TypeHandle type); - - /** - * Get an obfuscation mapping for a class - */ - public abstract ObfuscationData getObfClass(String className); - - /** - * Adds a method mapping to the internal refmap - * - * @param className Mixin class name which owns the refmap entry - * @param reference Original reference, as it appears in the annotation - * @param obfMethodData Method data to add for this mapping - */ - public abstract void addMethodMapping(String className, String reference, ObfuscationData obfMethodData); - - /** - * Adds a method mapping to the internal refmap, generates refmap entries - * using the supplied parsed memberinfo as context - * - * @param className Mixin class name which owns the refmap entry - * @param reference Original reference, as it appears in the annotation - * @param context The context for this mapping entry, remapped using the - * supplied obfuscation data - * @param obfMethodData Method data to add for this mapping - */ - public abstract void addMethodMapping(String className, String reference, MemberInfo context, ObfuscationData obfMethodData); - - /** - * Adds a field mapping to the internal refmap, generates refmap entries - * using the supplied parsed memberinfo as context - * - * @param className Mixin class name which owns the refmap entry - * @param reference Original reference, as it appears in the annotation - * @param context The context for this mapping entry, remapped using the - * supplied obfuscation data - * @param obfFieldData Field data to add for this mapping - */ - public abstract void addFieldMapping(String className, String reference, MemberInfo context, ObfuscationData obfFieldData); - - /** - * Adds a class mapping to the internal refmap - * - * @param className Mixin class name which owns the refmap entry - * @param reference Original reference, as it appears in the annotation - * @param obfClassData Class obf names - */ - public abstract void addClassMapping(String className, String reference, ObfuscationData obfClassData); - + void writeReferences(); + } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/interfaces/IReferenceManager.java b/src/ap/java/org/spongepowered/tools/obfuscation/interfaces/IReferenceManager.java new file mode 100644 index 00000000..83477a36 --- /dev/null +++ b/src/ap/java/org/spongepowered/tools/obfuscation/interfaces/IReferenceManager.java @@ -0,0 +1,91 @@ +/* + * 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.interfaces; + +import org.spongepowered.asm.mixin.injection.struct.MemberInfo; +import org.spongepowered.asm.mixin.refmap.ReferenceMapper; +import org.spongepowered.asm.obfuscation.mapping.common.MappingField; +import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod; +import org.spongepowered.tools.obfuscation.ObfuscationData; + +/** + * Consumer for generated references, builds the refmap during an AP pass and + * supports writing the generated refmap to file once the AP run is complete + */ +public interface IReferenceManager { + + /** + * Write the generated refmap to file + */ + public abstract void write(); + + /** + * Get the underlying reference mapper + */ + public abstract ReferenceMapper getMapper(); + + /** + * Adds a method mapping to the internal refmap + * + * @param className Mixin class name which owns the refmap entry + * @param reference Original reference, as it appears in the annotation + * @param obfMethodData Method data to add for this mapping + */ + public abstract void addMethodMapping(String className, String reference, ObfuscationData obfMethodData); + + /** + * Adds a method mapping to the internal refmap, generates refmap entries + * using the supplied parsed memberinfo as context + * + * @param className Mixin class name which owns the refmap entry + * @param reference Original reference, as it appears in the annotation + * @param context The context for this mapping entry, remapped using the + * supplied obfuscation data + * @param obfMethodData Method data to add for this mapping + */ + public abstract void addMethodMapping(String className, String reference, MemberInfo context, ObfuscationData obfMethodData); + + /** + * Adds a field mapping to the internal refmap, generates refmap entries + * using the supplied parsed memberinfo as context + * + * @param className Mixin class name which owns the refmap entry + * @param reference Original reference, as it appears in the annotation + * @param context The context for this mapping entry, remapped using the + * supplied obfuscation data + * @param obfFieldData Field data to add for this mapping + */ + public abstract void addFieldMapping(String className, String reference, MemberInfo context, ObfuscationData obfFieldData); + + /** + * Adds a class mapping to the internal refmap + * + * @param className Mixin class name which owns the refmap entry + * @param reference Original reference, as it appears in the annotation + * @param obfClassData Class obf names + */ + public abstract void addClassMapping(String className, String reference, ObfuscationData obfClassData); + +} diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/mapping/IMappingConsumer.java b/src/ap/java/org/spongepowered/tools/obfuscation/mapping/IMappingConsumer.java new file mode 100644 index 00000000..58da13b8 --- /dev/null +++ b/src/ap/java/org/spongepowered/tools/obfuscation/mapping/IMappingConsumer.java @@ -0,0 +1,137 @@ +/* + * 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.mapping; + +import java.util.LinkedHashSet; + +import org.spongepowered.asm.obfuscation.mapping.IMapping; +import org.spongepowered.asm.obfuscation.mapping.common.MappingField; +import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod; +import org.spongepowered.tools.obfuscation.ObfuscationType; + +import com.google.common.base.Objects; + +/** + * Mapping consumers collect the mappings generated by an AP pass ready for them + * to be written to the output files + */ +public interface IMappingConsumer { + + /** + * A set of mapping pairs + * + * @param type of mappings in the set + */ + public class MappingSet> extends LinkedHashSet> { + + /** + * A pair of mappings + * + * @param The type of mappings being mapped + */ + public static class Pair> { + + /** + * Source mapping + */ + public final TMapping from; + + /** + * Destination mapping + */ + public final TMapping to; + + public Pair(TMapping from, TMapping to) { + this.from = from; + this.to = to; + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof Pair)) { + return false; + } + + @SuppressWarnings("unchecked") + Pair other = (Pair) obj; + return Objects.equal(this.from, other.from) && Objects.equal(this.to, other.to); + } + + @Override + public int hashCode() { + return Objects.hashCode(this.from, this.to); + } + + @Override + public String toString() { + return String.format("%s -> %s", this.from, this.to); + } + + } + + private static final long serialVersionUID = 1L; + + } + + /** + * Clear all collections in the consumer + */ + public abstract void clear(); + + /** + * Add a field mapping to this consumer. + * + * @param type obfuscation type for this entry + * @param from source mapping + * @param to destination mapping + */ + public abstract void addFieldMapping(ObfuscationType type, MappingField from, MappingField to); + + /** + * Add a method mapping to this consumer. + * + * @param type obfuscation type for this entry + * @param from source mapping + * @param to destination mapping + */ + public abstract void addMethodMapping(ObfuscationType type, MappingMethod from, MappingMethod to); + + /** + * Get the stored field mappings for the specified obfuscation type + * + * @param type obfuscation type + * @return stored field mappings + */ + public abstract MappingSet getFieldMappings(ObfuscationType type); + + /** + * Get the stored method mappings for the specified obfuscation type + * + * @param type obfuscation type + * @return stored method mappings + */ + public abstract MappingSet getMethodMappings(ObfuscationType type); + +} diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/mapping/IMappingProvider.java b/src/ap/java/org/spongepowered/tools/obfuscation/mapping/IMappingProvider.java new file mode 100644 index 00000000..1bcf81e7 --- /dev/null +++ b/src/ap/java/org/spongepowered/tools/obfuscation/mapping/IMappingProvider.java @@ -0,0 +1,94 @@ +/* + * 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.mapping; + +import java.io.File; +import java.io.IOException; + +import org.spongepowered.asm.obfuscation.mapping.common.MappingField; +import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod; + +/** + * A mapping provider supplies mapping information to the AP + */ +public interface IMappingProvider { + + /** + * Clear this mapping provider, used to ensure the internal data is cleared + * before beginning a {@link #read} + */ + public abstract void clear(); + + /** + * Returns true if this mapping provider contains no mappings + */ + public abstract boolean isEmpty(); + + /** + * Called multiple times by the environment. This method will be called for + * each input file specified by the user. + * + * @param input + * @throws IOException + */ + public abstract void read(File input) throws IOException; + + /** + * Retrieve a method mapping from this provider. This method should return + * null if no mapping is found + * + * @param method method to find a mapping for + * @return mapped method or null if not found + */ + public abstract MappingMethod getMethodMapping(MappingMethod method); + + /** + * Retrieve a field mapping from this provider. This method should return + * null if no mapping is found + * + * @param field field to find a mapping for + * @return mapped field or null if not found + */ + public abstract MappingField getFieldMapping(MappingField field); + + /** + * Retrieve a class mapping from this provider. This method should return + * null if no mapping is found + * + * @param className name of the class to find a mapping for + * @return mapped class name or null if not found + */ + public abstract String getClassMapping(String className); + + /** + * Retrieve a package mapping from this provider. This method should return + * null if no mapping is found + * + * @param packageName name of the package to find a mapping for + * @return mapped package name or null if not found + */ + public abstract String getPackageMapping(String packageName); + +} diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/mapping/IMappingWriter.java b/src/ap/java/org/spongepowered/tools/obfuscation/mapping/IMappingWriter.java new file mode 100644 index 00000000..6455dc48 --- /dev/null +++ b/src/ap/java/org/spongepowered/tools/obfuscation/mapping/IMappingWriter.java @@ -0,0 +1,47 @@ +/* + * 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.mapping; + +import org.spongepowered.asm.obfuscation.mapping.common.MappingField; +import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod; +import org.spongepowered.tools.obfuscation.ObfuscationType; +import org.spongepowered.tools.obfuscation.mapping.IMappingConsumer.MappingSet; + +/** + * Mapping writer + */ +public interface IMappingWriter { + + /** + * Receives + * + * @param output + * @param type + * @param fields + * @param methods + */ + public abstract void write(String output, ObfuscationType type, MappingSet fields, MappingSet methods); + +} diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/mapping/common/MappingProvider.java b/src/ap/java/org/spongepowered/tools/obfuscation/mapping/common/MappingProvider.java new file mode 100644 index 00000000..a06114e6 --- /dev/null +++ b/src/ap/java/org/spongepowered/tools/obfuscation/mapping/common/MappingProvider.java @@ -0,0 +1,88 @@ +/* + * This file is part of Mixin, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package org.spongepowered.tools.obfuscation.mapping.common; + +import javax.annotation.processing.Filer; +import javax.annotation.processing.Messager; + +import org.spongepowered.asm.obfuscation.mapping.common.MappingField; +import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod; +import org.spongepowered.tools.obfuscation.mapping.IMappingProvider; + +import com.google.common.collect.BiMap; +import com.google.common.collect.HashBiMap; + +/** + * Base class for mapping providers + */ +public abstract class MappingProvider implements IMappingProvider { + + protected final Messager messager; + protected final Filer filer; + + protected final BiMap packageMap = HashBiMap.create(); + protected final BiMap classMap = HashBiMap.create(); + protected final BiMap fieldMap = HashBiMap.create(); + protected final BiMap methodMap = HashBiMap.create(); + + public MappingProvider(Messager messager, Filer filer) { + this.messager = messager; + this.filer = filer; + } + + @Override + public void clear() { + this.packageMap.clear(); + this.classMap.clear(); + this.fieldMap.clear(); + this.methodMap.clear(); + } + + @Override + public boolean isEmpty() { + return this.packageMap.isEmpty() && this.classMap.isEmpty() && this.fieldMap.isEmpty() && this.methodMap.isEmpty(); + } + + @Override + public MappingMethod getMethodMapping(MappingMethod method) { + return this.methodMap.get(method); + } + + @Override + public MappingField getFieldMapping(MappingField field) { + return this.fieldMap.get(field); + } + + @Override + public String getClassMapping(String className) { + return this.classMap.get(className); + } + + @Override + public String getPackageMapping(String packageName) { + return this.packageMap.get(packageName); + } + +} diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/mapping/common/MappingWriter.java b/src/ap/java/org/spongepowered/tools/obfuscation/mapping/common/MappingWriter.java new file mode 100644 index 00000000..5e6099a7 --- /dev/null +++ b/src/ap/java/org/spongepowered/tools/obfuscation/mapping/common/MappingWriter.java @@ -0,0 +1,69 @@ +/* + * 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.mapping.common; + +import java.io.File; +import java.io.IOException; +import java.io.PrintWriter; + +import javax.annotation.processing.Filer; +import javax.annotation.processing.Messager; +import javax.tools.FileObject; +import javax.tools.StandardLocation; +import javax.tools.Diagnostic.Kind; + +import org.spongepowered.tools.obfuscation.mapping.IMappingWriter; + +/** + * Writer for mappings + */ +public abstract class MappingWriter implements IMappingWriter { + + private final Messager messager; + + private final Filer filer; + + public MappingWriter(Messager messager, Filer filer) { + this.messager = messager; + this.filer = filer; + } + + /** + * Open a writer for an output file + */ + protected PrintWriter openFileWriter(String fileName, String description) throws IOException { + if (fileName.matches("^.*[\\\\/:].*$")) { + File outFile = new File(fileName); + outFile.getParentFile().mkdirs(); + this.messager.printMessage(Kind.NOTE, "Writing " + description + " to " + outFile.getAbsolutePath()); + return new PrintWriter(outFile); + } + + FileObject outResource = this.filer.createResource(StandardLocation.CLASS_OUTPUT, "", fileName); + this.messager.printMessage(Kind.NOTE, "Writing " + description + " to " + new File(outResource.toUri()).getAbsolutePath()); + return new PrintWriter(outResource.openWriter()); + } + +} diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/mapping/mcp/MappingProviderSrg.java b/src/ap/java/org/spongepowered/tools/obfuscation/mapping/mcp/MappingProviderSrg.java new file mode 100644 index 00000000..de14d6c0 --- /dev/null +++ b/src/ap/java/org/spongepowered/tools/obfuscation/mapping/mcp/MappingProviderSrg.java @@ -0,0 +1,104 @@ +/* + * 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.mapping.mcp; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.Charset; + +import javax.annotation.processing.Filer; +import javax.annotation.processing.Messager; + +import org.spongepowered.asm.mixin.throwables.MixinException; +import org.spongepowered.asm.obfuscation.mapping.common.MappingField; +import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod; +import org.spongepowered.asm.obfuscation.mapping.mcp.MappingFieldSrg; +import org.spongepowered.tools.obfuscation.mapping.common.MappingProvider; + +import com.google.common.base.Strings; +import com.google.common.collect.BiMap; +import com.google.common.io.Files; +import com.google.common.io.LineProcessor; + +/** + * Ported from Srg2Source ( + * + * github.com/MinecraftForge/Srg2Source). + */ +public class MappingProviderSrg extends MappingProvider { + + public MappingProviderSrg(Messager messager, Filer filer) { + super(messager, filer); + } + + @Override + public void read(final File input) throws IOException { + // Locally scoped to avoid synthetic accessor + final BiMap packageMap = this.packageMap; + final BiMap classMap = this.classMap; + final BiMap fieldMap = this.fieldMap; + final BiMap methodMap = this.methodMap; + + Files.readLines(input, Charset.defaultCharset(), new LineProcessor() { + @Override + public String getResult() { + return null; + } + + @Override + public boolean processLine(String line) throws IOException { + if (Strings.isNullOrEmpty(line) || line.startsWith("#")) { + return true; + } + + String type = line.substring(0, 2); + String[] args = line.substring(4).split(" "); + + if (type.equals("PK")) { + packageMap.forcePut(args[0], args[1]); + } else if (type.equals("CL")) { + classMap.forcePut(args[0], args[1]); + } else if (type.equals("FD")) { + fieldMap.forcePut(new MappingFieldSrg(args[0]).copy(), new MappingFieldSrg(args[1]).copy()); + } else if (type.equals("MD")) { + methodMap.forcePut(new MappingMethod(args[0], args[1]), new MappingMethod(args[2], args[3])); + } else { + throw new MixinException("Invalid SRG file: " + input); + } + + return true; + } + }); + } + + @Override + public MappingField getFieldMapping(MappingField field) { + // SRG fields do not have descriptors so strip the field descriptor before looking up + if (field.getDesc() != null) { + field = new MappingFieldSrg(field); + } + return this.fieldMap.get(field); + } +} diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/mapping/mcp/MappingWriterSrg.java b/src/ap/java/org/spongepowered/tools/obfuscation/mapping/mcp/MappingWriterSrg.java new file mode 100644 index 00000000..b0dde3f3 --- /dev/null +++ b/src/ap/java/org/spongepowered/tools/obfuscation/mapping/mcp/MappingWriterSrg.java @@ -0,0 +1,94 @@ +/* + * 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.mapping.mcp; + +import java.io.IOException; +import java.io.PrintWriter; + +import javax.annotation.processing.Filer; +import javax.annotation.processing.Messager; + +import org.spongepowered.asm.obfuscation.mapping.common.MappingField; +import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod; +import org.spongepowered.tools.obfuscation.ObfuscationType; +import org.spongepowered.tools.obfuscation.mapping.IMappingConsumer.MappingSet; +import org.spongepowered.tools.obfuscation.mapping.IMappingConsumer.MappingSet.Pair; +import org.spongepowered.tools.obfuscation.mapping.common.MappingWriter; + +/** + * Writer for SRG mappings + */ +public class MappingWriterSrg extends MappingWriter { + + public MappingWriterSrg(Messager messager, Filer filer) { + super(messager, filer); + } + + @Override + public void write(final String output, ObfuscationType type, MappingSet fields, MappingSet methods) { + if (output == null) { + return; + } + + PrintWriter writer = null; + + try { + writer = this.openFileWriter(output, type + " output SRGs"); + this.writeFieldMappings(writer, fields); + this.writeMethodMappings(writer, methods); + } catch (IOException ex) { + ex.printStackTrace(); + } finally { + if (writer != null) { + try { + writer.close(); + } catch (Exception ex) { + // oh well + } + } + } + } + + protected void writeFieldMappings(PrintWriter writer, MappingSet fields) { + for (Pair field : fields) { + writer.println(this.formatFieldMapping(field)); + } + } + + protected void writeMethodMappings(PrintWriter writer, MappingSet methods) { + for (Pair method : methods) { + writer.println(this.formatMethodMapping(method)); + } + } + + protected String formatFieldMapping(Pair mapping) { + return String.format("FD: %s/%s %s/%s", mapping.from.getOwner(), mapping.from.getName(), mapping.to.getOwner(), mapping.to.getName()); + } + + protected String formatMethodMapping(Pair mapping) { + return String.format("MD: %s %s %s %s", mapping.from.getName(), mapping.from.getDesc(), mapping.to.getName(), mapping.to.getDesc()); + } + +} diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/mcp/ObfuscationEnvironmentMCP.java b/src/ap/java/org/spongepowered/tools/obfuscation/mcp/ObfuscationEnvironmentMCP.java new file mode 100644 index 00000000..1318b529 --- /dev/null +++ b/src/ap/java/org/spongepowered/tools/obfuscation/mcp/ObfuscationEnvironmentMCP.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.mcp; + +import javax.annotation.processing.Filer; +import javax.annotation.processing.Messager; + +import org.spongepowered.tools.obfuscation.ObfuscationEnvironment; +import org.spongepowered.tools.obfuscation.ObfuscationType; +import org.spongepowered.tools.obfuscation.mapping.IMappingProvider; +import org.spongepowered.tools.obfuscation.mapping.IMappingWriter; +import org.spongepowered.tools.obfuscation.mapping.mcp.MappingProviderSrg; +import org.spongepowered.tools.obfuscation.mapping.mcp.MappingWriterSrg; + +/** + * An MCP obfuscation environment (eg. SEARGE or NOTCH) + */ +public class ObfuscationEnvironmentMCP extends ObfuscationEnvironment { + + protected ObfuscationEnvironmentMCP(ObfuscationType type) { + super(type); + } + + @Override + protected IMappingProvider getMappingProvider(Messager messager, Filer filer) { + return new MappingProviderSrg(messager, filer); + } + + @Override + protected IMappingWriter getMappingWriter(Messager messager, Filer filer) { + return new MappingWriterSrg(messager, filer); + } +} diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/mcp/ObfuscationServiceMCP.java b/src/ap/java/org/spongepowered/tools/obfuscation/mcp/ObfuscationServiceMCP.java new file mode 100644 index 00000000..6c5edbc1 --- /dev/null +++ b/src/ap/java/org/spongepowered/tools/obfuscation/mcp/ObfuscationServiceMCP.java @@ -0,0 +1,85 @@ +/* + * 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.mcp; + +import java.util.Collection; +import java.util.Set; + +import org.spongepowered.tools.obfuscation.service.IObfuscationService; +import org.spongepowered.tools.obfuscation.service.ObfuscationTypeDescriptor; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; + +/** + * Default obfuscation service, provides MCP + */ +public class ObfuscationServiceMCP implements IObfuscationService { + + public static final String SEARGE = "searge"; + public static final String NOTCH = "notch"; + + public static final String REOBF_SRG_FILE = "reobfSrgFile"; + public static final String REOBF_EXTRA_SRG_FILES = "reobfSrgFiles"; + public static final String REOBF_NOTCH_FILE = "reobfNotchSrgFile"; + public static final String REOBF_EXTRA_NOTCH_FILES = "reobfNotchSrgFiles"; + public static final String OUT_SRG_SRG_FILE = "outSrgFile"; + public static final String OUT_NOTCH_SRG_FILE = "outNotchSrgFile"; + public static final String OUT_REFMAP_FILE = "outRefMapFile"; + + @Override + public Set getSupportedOptions() { + return ImmutableSet.of( + ObfuscationServiceMCP.REOBF_SRG_FILE, + ObfuscationServiceMCP.REOBF_EXTRA_SRG_FILES, + ObfuscationServiceMCP.REOBF_NOTCH_FILE, + ObfuscationServiceMCP.REOBF_EXTRA_NOTCH_FILES, + ObfuscationServiceMCP.OUT_SRG_SRG_FILE, + ObfuscationServiceMCP.OUT_NOTCH_SRG_FILE, + ObfuscationServiceMCP.OUT_REFMAP_FILE + ); + } + + @Override + public Collection getObfuscationTypes() { + return ImmutableList.of( + new ObfuscationTypeDescriptor( + ObfuscationServiceMCP.SEARGE, + ObfuscationServiceMCP.REOBF_SRG_FILE, + ObfuscationServiceMCP.REOBF_EXTRA_SRG_FILES, + ObfuscationServiceMCP.OUT_SRG_SRG_FILE, + ObfuscationEnvironmentMCP.class + ), + new ObfuscationTypeDescriptor( + ObfuscationServiceMCP.NOTCH, + ObfuscationServiceMCP.REOBF_NOTCH_FILE, + ObfuscationServiceMCP.REOBF_EXTRA_NOTCH_FILES, + ObfuscationServiceMCP.OUT_NOTCH_SRG_FILE, + ObfuscationEnvironmentMCP.class + ) + ); + } + +} diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/service/IObfuscationService.java b/src/ap/java/org/spongepowered/tools/obfuscation/service/IObfuscationService.java new file mode 100644 index 00000000..2fc38b3b --- /dev/null +++ b/src/ap/java/org/spongepowered/tools/obfuscation/service/IObfuscationService.java @@ -0,0 +1,50 @@ +/* + * 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.service; + +import java.util.Collection; +import java.util.Set; + +/** + * Service interface for obfuscation services + */ +public interface IObfuscationService { + + /** + * Get all options supported by this service + * + * @return supported options as a Set + */ + public abstract Set getSupportedOptions(); + + /** + * This method should return a collection of available obfuscation types + * supported by this service + * + * @return available obfuscation types or null + */ + public abstract Collection getObfuscationTypes(); + +} diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/service/ObfuscationServices.java b/src/ap/java/org/spongepowered/tools/obfuscation/service/ObfuscationServices.java new file mode 100644 index 00000000..d385a0b1 --- /dev/null +++ b/src/ap/java/org/spongepowered/tools/obfuscation/service/ObfuscationServices.java @@ -0,0 +1,124 @@ +/* + * 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.service; + +import java.util.Collection; +import java.util.HashSet; +import java.util.ServiceConfigurationError; +import java.util.ServiceLoader; +import java.util.Set; + +import javax.tools.Diagnostic.Kind; + +import org.spongepowered.tools.obfuscation.ObfuscationType; +import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor; + +/** + * Obfuscation service manager + */ +public class ObfuscationServices { + + /** + * Singleton + */ + private static ObfuscationServices instance; + + /** + * Service loader + */ + private final ServiceLoader serviceLoader; + + /** + * Initialised services + */ + private final Set services = new HashSet(); + + /** + * Singleton pattern + */ + private ObfuscationServices() { + this.serviceLoader = ServiceLoader.load(IObfuscationService.class, this.getClass().getClassLoader()); + } + + /** + * Singleton pattern, get or create the instance + */ + public static ObfuscationServices getInstance() { + if (ObfuscationServices.instance == null) { + ObfuscationServices.instance = new ObfuscationServices(); + } + return ObfuscationServices.instance; + } + + /** + * Initialise services + */ + public void initProviders(IMixinAnnotationProcessor ap) { + try { + for (IObfuscationService service : this.serviceLoader) { + if (!this.services.contains(service)) { + this.services.add(service); + + String serviceName = service.getClass().getSimpleName(); +// ap.printMessage(Kind.NOTE, "Preparing service " + serviceName); + Collection obfTypes = service.getObfuscationTypes(); + if (obfTypes != null) { + for (ObfuscationTypeDescriptor obfType : obfTypes) { + try { + ObfuscationType type = ObfuscationType.create(obfType, ap); + ap.printMessage(Kind.NOTE, serviceName + " supports type: \"" + type + "\""); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + } + } + } + } catch (ServiceConfigurationError serviceError) { + ap.printMessage(Kind.ERROR, serviceError.getClass().getSimpleName() + ": " + serviceError.getMessage()); + serviceError.printStackTrace(); + } + } + + public Set getSupportedOptions() { + Set supportedOptions = new HashSet(); + for (IObfuscationService provider : this.serviceLoader) { + Set options = provider.getSupportedOptions(); + if (options != null) { + supportedOptions.addAll(options); + } + } + return supportedOptions; + } + + public IObfuscationService getService(Class serviceClass) { + for (IObfuscationService service : this.serviceLoader) { + if (serviceClass.getName().equals(service.getClass().getName())) { + return service; + } + } + return null; + } +} diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/service/ObfuscationTypeDescriptor.java b/src/ap/java/org/spongepowered/tools/obfuscation/service/ObfuscationTypeDescriptor.java new file mode 100644 index 00000000..1f8de409 --- /dev/null +++ b/src/ap/java/org/spongepowered/tools/obfuscation/service/ObfuscationTypeDescriptor.java @@ -0,0 +1,109 @@ +/* + * 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.service; + +import org.spongepowered.tools.obfuscation.ObfuscationEnvironment; +import org.spongepowered.tools.obfuscation.ObfuscationType; + +/** + * Describes the arguments for a particular obfuscation type, used as an args + * object for creating {@link ObfuscationType}s + */ +public class ObfuscationTypeDescriptor { + + /** + * Refmap key for this mapping type + */ + private final String key; + + /** + * The name of the Annotation Processor argument which contains the file + * name to read the mappings for this obfuscation type from + */ + private final String inputFileArgName; + + /** + * The name of the Annotation Processor argument which contains the file + * name to read the extra mappings for this obfuscation type from + */ + private final String extraInputFilesArgName; + + /** + * The name of the Annotation Processor argument which contains the file + * name to write generated mappings to + */ + private final String outFileArgName; + + private final Class environmentType; + + public ObfuscationTypeDescriptor(String key, String inputFileArgName, String outFileArgName, + Class environmentType) { + this(key, inputFileArgName, null, outFileArgName, environmentType); + } + + public ObfuscationTypeDescriptor(String key, String inputFileArgName, String extraInputFilesArgName, String outFileArgName, + Class environmentType) { + this.key = key; + this.inputFileArgName = inputFileArgName; + this.extraInputFilesArgName = extraInputFilesArgName; + this.outFileArgName = outFileArgName; + this.environmentType = environmentType; + } + + /** + * Get the key for this type, must be unique + */ + public final String getKey() { + return this.key; + } + + /** + * Get the name of the AP argument used to specify the input file + */ + public String getInputFileOption() { + return this.inputFileArgName; + } + + /** + * Get the name of the AP argument used to specify extra input files + */ + public String getExtraInputFilesOption() { + return this.extraInputFilesArgName; + } + + /** + * Get the name of the AP argument used to specify the output file + */ + public String getOutputFileOption() { + return this.outFileArgName; + } + + /** + * Get the environment class + */ + public Class getEnvironmentType() { + return this.environmentType; + } +} diff --git a/src/ap/resources/META-INF/services/org.spongepowered.tools.obfuscation.service.IObfuscationService b/src/ap/resources/META-INF/services/org.spongepowered.tools.obfuscation.service.IObfuscationService new file mode 100644 index 00000000..96b5eb81 --- /dev/null +++ b/src/ap/resources/META-INF/services/org.spongepowered.tools.obfuscation.service.IObfuscationService @@ -0,0 +1 @@ +org.spongepowered.tools.obfuscation.mcp.ObfuscationServiceMCP \ No newline at end of file diff --git a/src/example/java/org/spongepowered/tools/obfuscation/json/MappingWriterJson.java b/src/example/java/org/spongepowered/tools/obfuscation/json/MappingWriterJson.java new file mode 100644 index 00000000..df4c1594 --- /dev/null +++ b/src/example/java/org/spongepowered/tools/obfuscation/json/MappingWriterJson.java @@ -0,0 +1,96 @@ +/* + * 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.json; + +import java.io.IOException; +import java.io.PrintWriter; +import java.util.LinkedHashMap; +import java.util.Map; + +import javax.annotation.processing.Filer; +import javax.annotation.processing.Messager; + +import org.spongepowered.asm.obfuscation.mapping.common.MappingField; +import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod; +import org.spongepowered.tools.obfuscation.ObfuscationType; +import org.spongepowered.tools.obfuscation.mapping.IMappingConsumer.MappingSet; +import org.spongepowered.tools.obfuscation.mapping.IMappingConsumer.MappingSet.Pair; +import org.spongepowered.tools.obfuscation.mapping.common.MappingWriter; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +/** + * Uses Gson to write mappings to output JSON file + */ +public class MappingWriterJson extends MappingWriter { + + public MappingWriterJson(Messager messager, Filer filer) { + super(messager, filer); + } + + @Override + public void write(String output, ObfuscationType type, MappingSet fields, MappingSet methods) { + if (output == null) { + return; + } + + Map fieldMappings = new LinkedHashMap(); + for (Pair field : fields) { + fieldMappings.put(field.from.serialise(), field.to.serialise()); + } + + Map methodMappings = new LinkedHashMap(); + for (Pair method : methods) { + methodMappings.put(method.from.serialise(), method.to.serialise()); + } + + Map> mappings = new LinkedHashMap>(); + mappings.put("fields", fieldMappings); + mappings.put("methods", methodMappings); + + this.writeJson(output, type, mappings); + } + + protected void writeJson(String output, ObfuscationType type, Map> mappings) { + PrintWriter writer = null; + + try { + writer = this.openFileWriter(output, type + " output"); + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + gson.toJson(mappings, writer); + } catch (IOException ex) { + ex.printStackTrace(); + } finally { + if (writer != null) { + try { + writer.close(); + } catch (Exception ex) { + // oh well + } + } + } + } +} diff --git a/src/example/java/org/spongepowered/tools/obfuscation/json/ObfuscationEnvironmentJson.java b/src/example/java/org/spongepowered/tools/obfuscation/json/ObfuscationEnvironmentJson.java new file mode 100644 index 00000000..94b17269 --- /dev/null +++ b/src/example/java/org/spongepowered/tools/obfuscation/json/ObfuscationEnvironmentJson.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.json; + +import javax.annotation.processing.Filer; +import javax.annotation.processing.Messager; + +import org.spongepowered.tools.obfuscation.ObfuscationEnvironment; +import org.spongepowered.tools.obfuscation.ObfuscationType; +import org.spongepowered.tools.obfuscation.mapping.IMappingProvider; +import org.spongepowered.tools.obfuscation.mapping.IMappingWriter; +import org.spongepowered.tools.obfuscation.mapping.mcp.MappingProviderSrg; + +/** + * Obfuscation environment for the example JSON mapping type + */ +public class ObfuscationEnvironmentJson extends ObfuscationEnvironment { + + public ObfuscationEnvironmentJson(ObfuscationType type) { + super(type); + } + + @Override + protected IMappingProvider getMappingProvider(Messager messager, Filer filer) { + // Use SRG for mapping source for this example + return new MappingProviderSrg(messager, filer); + } + + @Override + protected IMappingWriter getMappingWriter(Messager messager, Filer filer) { + return new MappingWriterJson(messager, filer); + } +} diff --git a/src/example/java/org/spongepowered/tools/obfuscation/json/ObfuscationServiceJson.java b/src/example/java/org/spongepowered/tools/obfuscation/json/ObfuscationServiceJson.java new file mode 100644 index 00000000..7c12c5e9 --- /dev/null +++ b/src/example/java/org/spongepowered/tools/obfuscation/json/ObfuscationServiceJson.java @@ -0,0 +1,68 @@ +/* + * 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.json; + +import java.util.Collection; +import java.util.Set; + +import org.spongepowered.tools.obfuscation.mcp.ObfuscationServiceMCP; +import org.spongepowered.tools.obfuscation.service.IObfuscationService; +import org.spongepowered.tools.obfuscation.service.ObfuscationTypeDescriptor; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; + +/** + * An example obfuscation service which writes mappings to JSON files. For the + * purposes of the example, we actually use an SRG mapping source and a JSON + * mapping writer. However in practice a custom mapping provider would be + * employed as well. + */ +public class ObfuscationServiceJson implements IObfuscationService { + + public static final String KEY = "json"; + public static final String OUTPUT_JSON_FILE = "outputJson"; + + @Override + public Set getSupportedOptions() { + return ImmutableSet.of( + ObfuscationServiceMCP.REOBF_SRG_FILE, // Use SRG file for mapping source + ObfuscationServiceJson.OUTPUT_JSON_FILE + ); + } + + @Override + public Collection getObfuscationTypes() { + return ImmutableList.of( + new ObfuscationTypeDescriptor( + ObfuscationServiceJson.KEY, + ObfuscationServiceMCP.REOBF_SRG_FILE, + ObfuscationServiceJson.OUTPUT_JSON_FILE, + ObfuscationEnvironmentJson.class + ) + ); + } + +} diff --git a/src/example/resources/META-INF/services/org.spongepowered.tools.obfuscation.service.IObfuscationService b/src/example/resources/META-INF/services/org.spongepowered.tools.obfuscation.service.IObfuscationService new file mode 100644 index 00000000..75c8077c --- /dev/null +++ b/src/example/resources/META-INF/services/org.spongepowered.tools.obfuscation.service.IObfuscationService @@ -0,0 +1 @@ +org.spongepowered.tools.obfuscation.json.ObfuscationServiceJson \ No newline at end of file diff --git a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java index d39a8600..3c3fe37d 100644 --- a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java +++ b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java @@ -62,7 +62,7 @@ public abstract class MixinBootstrap { /** * Subsystem version */ - public static final String VERSION = "0.5.14"; + public static final String VERSION = "0.5.15"; // Consts private static final String LAUNCH_PACKAGE = "org.spongepowered.asm.launch."; 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 956f47fc..adc746c9 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 @@ -31,7 +31,9 @@ import org.spongepowered.asm.lib.tree.MethodInsnNode; import org.spongepowered.asm.mixin.refmap.IReferenceMapperContext; import org.spongepowered.asm.mixin.refmap.ReferenceMapper; import org.spongepowered.asm.mixin.throwables.MixinException; -import org.spongepowered.asm.obfuscation.SrgMethod; +import org.spongepowered.asm.obfuscation.mapping.IMapping; +import org.spongepowered.asm.obfuscation.mapping.common.MappingField; +import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod; import org.spongepowered.asm.util.SignaturePrinter; /** @@ -95,6 +97,11 @@ public class MemberInfo { * True to match all matching members, not just the first */ public final boolean matchAll; + + /** + * Force this member to report as a field + */ + private final boolean forceField; /** * ctor @@ -134,10 +141,11 @@ public class MemberInfo { throw new IllegalArgumentException("Attempt to instance a MemberInfo with an invalid owner format"); } - this.owner = owner; - this.name = name; - this.desc = desc; + this.owner = owner; + this.name = name; + this.desc = desc; this.matchAll = matchAll; + this.forceField = false; } /** @@ -148,6 +156,7 @@ public class MemberInfo { */ public MemberInfo(AbstractInsnNode insn) { this.matchAll = false; + this.forceField = false; if (insn instanceof MethodInsnNode) { MethodInsnNode methodNode = (MethodInsnNode) insn; @@ -165,27 +174,29 @@ public class MemberInfo { } /** - * Initialise a MemberInfo using the supplied SrgMethod object + * Initialise a MemberInfo using the supplied mapping object * - * @param srgMethod SrgMethod object to copy values from + * @param mapping Mapping object to copy values from */ - public MemberInfo(SrgMethod srgMethod) { - this.owner = srgMethod.getOwner(); - this.name = srgMethod.getSimpleName(); - this.desc = srgMethod.getDesc(); + public MemberInfo(IMapping mapping) { + this.owner = mapping.getOwner(); + this.name = mapping.getSimpleName(); + this.desc = mapping.getDesc(); this.matchAll = false; + this.forceField = mapping.getType() == IMapping.Type.FIELD; } /** - * Initialise a remapped MemberInfo using the supplied SrgMethod object + * Initialise a remapped MemberInfo using the supplied mapping object * - * @param srgMethod SrgMethod object to copy values from + * @param method mapping method object to copy values from */ - private MemberInfo(MemberInfo remapped, SrgMethod srgMethod, boolean setOwner) { - this.owner = setOwner ? srgMethod.getOwner() : remapped.owner; - this.name = srgMethod.getSimpleName(); - this.desc = srgMethod.getDesc(); + private MemberInfo(MemberInfo remapped, MappingMethod method, boolean setOwner) { + this.owner = setOwner ? method.getOwner() : remapped.owner; + this.name = method.getSimpleName(); + this.desc = method.getDesc(); this.matchAll = remapped.matchAll; + this.forceField = false; } /** @@ -199,6 +210,7 @@ public class MemberInfo { this.name = original.name; this.desc = original.desc; this.matchAll = original.matchAll; + this.forceField = original.forceField; } /* (non-Javadoc) @@ -218,7 +230,9 @@ public class MemberInfo { * Return this MemberInfo as an SRG mapping * * @return SRG representation of this MemberInfo + * @deprecated use m.asMethodMapping().serialise() instead */ + @Deprecated public String toSrg() { if (!this.isFullyQualified()) { throw new MixinException("Cannot convert unqualified reference to SRG mapping"); @@ -239,16 +253,28 @@ public class MemberInfo { return new SignaturePrinter(this).setFullyQualified(true).toDescriptor(); } - public SrgMethod asSrgMethod() { + public IMapping asMapping() { + return this.isField() ? this.asFieldMapping() : this.asMethodMapping(); + } + + public MappingMethod asMethodMapping() { if (!this.isFullyQualified()) { - throw new MixinException("Cannot convert unqualified reference to SrgMethod"); + throw new MixinException("Cannot convert unqualified reference " + this + " to MethodMapping"); } if (this.isField()) { - throw new MixinException("Cannot convert a non-method reference to SrgMethod"); + throw new MixinException("Cannot convert a non-method reference " + this + " to MethodMapping"); } - return new SrgMethod(this.owner + "/" + this.name, this.desc); + return new MappingMethod(this.owner + "/" + this.name, this.desc); + } + + public MappingField asFieldMapping() { + if (!this.isField()) { + throw new MixinException("Cannot convert non-field reference " + this + " to FieldMapping"); + } + + return new MappingField(this.owner, this.name, this.desc); } /** @@ -267,7 +293,7 @@ public class MemberInfo { * @return true if this is definitely a field */ public boolean isField() { - return this.desc != null && !this.desc.startsWith("("); + return this.forceField || (this.desc != null && !this.desc.startsWith("(")); } /** @@ -416,7 +442,7 @@ public class MemberInfo { * @param setOwner True to set the owner as well as the name * @return New MethodInfo with remapped values */ - public MemberInfo remapUsing(SrgMethod srgMethod, boolean setOwner) { + public MemberInfo remapUsing(MappingMethod srgMethod, boolean setOwner) { return new MemberInfo(this, srgMethod, setOwner); } @@ -512,14 +538,7 @@ public class MemberInfo { return new MemberInfo(name, owner, desc, matchAll); } - public static MemberInfo fromSrgField(String srgName, String desc) { - int slashPos = srgName.lastIndexOf('/'); - String owner = srgName.substring(0, slashPos); - String name = srgName.substring(slashPos + 1); - return new MemberInfo(name, owner, desc, false); - } - - public static MemberInfo fromSrgMethod(SrgMethod srgMethod) { - return new MemberInfo(srgMethod); + public static MemberInfo fromMapping(IMapping mapping) { + return new MemberInfo(mapping); } } diff --git a/src/main/java/org/spongepowered/asm/obfuscation/SrgContainer.java b/src/main/java/org/spongepowered/asm/obfuscation/SrgContainer.java deleted file mode 100644 index 86891539..00000000 --- a/src/main/java/org/spongepowered/asm/obfuscation/SrgContainer.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * This file is part of Mixin, licensed under the MIT License (MIT). - * - * Copyright (c) SpongePowered - * Copyright (c) contributors - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -package org.spongepowered.asm.obfuscation; - -import java.io.File; -import java.io.IOException; -import java.nio.charset.Charset; - -import org.spongepowered.asm.mixin.throwables.MixinException; - -import com.google.common.collect.BiMap; -import com.google.common.collect.HashBiMap; -import com.google.common.io.Files; - -import joptsimple.internal.Strings; - -/** - * Ported from Srg2Source ( - * - * github.com/MinecraftForge/Srg2Source). - */ -public class SrgContainer { - - private final BiMap packageMap = HashBiMap.create(); - private final BiMap classMap = HashBiMap.create(); - private final BiMap fieldMap = HashBiMap.create(); - private final BiMap methodMap = HashBiMap.create(); - - public void readSrg(File srg) throws IOException { - for (String line : Files.readLines(srg, Charset.defaultCharset())) { - if (Strings.isNullOrEmpty(line) || line.startsWith("#")) { - continue; - } - - String type = line.substring(0, 2); - String[] args = line.substring(4).split(" "); - - if (type.equals("PK")) { - this.packageMap.forcePut(args[0], args[1]); - } else if (type.equals("CL")) { - this.classMap.forcePut(args[0], args[1]); - } else if (type.equals("FD")) { - this.fieldMap.forcePut(new SrgField(args[0]), new SrgField(args[1])); - } else if (type.equals("MD")) { - this.methodMap.forcePut(new SrgMethod(args[0], args[1]), new SrgMethod(args[2], args[3])); - } else { - throw new MixinException("Invalid SRG file: " + srg); - } - } - } - - public SrgMethod getMethodMapping(SrgMethod methodName) { - return this.methodMap.get(methodName); - } - - public SrgField getFieldMapping(SrgField fieldName) { - return this.fieldMap.get(fieldName); - } - - public String getClassMapping(String className) { - return this.classMap.get(className); - } - - public String getPackageMapping(String packageName) { - return this.packageMap.get(packageName); - } - -} diff --git a/src/main/java/org/spongepowered/asm/obfuscation/mapping/IMapping.java b/src/main/java/org/spongepowered/asm/obfuscation/mapping/IMapping.java new file mode 100644 index 00000000..f4b1b268 --- /dev/null +++ b/src/main/java/org/spongepowered/asm/obfuscation/mapping/IMapping.java @@ -0,0 +1,64 @@ +/* + * This file is part of Mixin, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package org.spongepowered.asm.obfuscation.mapping; + +/** + * Base class for member mapping entries + * + * @param type of this member, for transformation method return types + */ +public interface IMapping { + + /** + * Type of mapping + */ + public enum Type { + FIELD, + METHOD, + CLASS, + PACKAGE + } + + public abstract Type getType(); + + public abstract TMapping move(String newOwner); + + public abstract TMapping remap(String newName); + + public abstract TMapping transform(String newDesc); + + public abstract TMapping copy(); + + public abstract String getName(); + + public abstract String getSimpleName(); + + public abstract String getOwner(); + + public abstract String getDesc(); + + public abstract String serialise(); + +} diff --git a/src/main/java/org/spongepowered/asm/obfuscation/mapping/common/MappingField.java b/src/main/java/org/spongepowered/asm/obfuscation/mapping/common/MappingField.java new file mode 100644 index 00000000..c492e3d6 --- /dev/null +++ b/src/main/java/org/spongepowered/asm/obfuscation/mapping/common/MappingField.java @@ -0,0 +1,123 @@ +/* + * This file is part of Mixin, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package org.spongepowered.asm.obfuscation.mapping.common; + +import org.spongepowered.asm.obfuscation.mapping.IMapping; + +import com.google.common.base.Objects; +import com.google.common.base.Strings; + +/** + * Stores information about a field mapping during AP runs + */ +public class MappingField implements IMapping { + + private final String owner; + + private final String name; + + private final String desc; + + public MappingField(String owner, String name) { + this(owner, name, null); + } + + public MappingField(String owner, String name, String desc) { + this.owner = owner; + this.name = name; + this.desc = desc; + } + + @Override + public Type getType() { + return Type.FIELD; + } + + @Override + public String getName() { + return this.name; + } + + @Override + public final String getSimpleName() { + return this.name; + } + + @Override + public final String getOwner() { + return this.owner; + } + + @Override + public final String getDesc() { + return this.desc; + } + + @Override + public MappingField move(String newOwner) { + return new MappingField(newOwner, this.getName(), this.getDesc()); + } + + @Override + public MappingField remap(String newName) { + return new MappingField(this.getOwner(), newName, this.getDesc()); + } + + @Override + public MappingField transform(String newDesc) { + return new MappingField(this.getOwner(), this.getName(), newDesc); + } + + @Override + public MappingField copy() { + return new MappingField(this.getOwner(), this.getName(), this.getDesc()); + } + + @Override + public int hashCode() { + return Objects.hashCode(this.toString()); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj instanceof MappingField) { + return Objects.equal(this.toString(), ((MappingField)obj).toString()); + } + return false; + } + + @Override + public String serialise() { + return this.toString(); + } + + @Override + public String toString() { + return String.format("L%s;%s:%s", this.getOwner(), this.getName(), Strings.nullToEmpty(this.getDesc())); + } +} \ No newline at end of file diff --git a/src/main/java/org/spongepowered/asm/obfuscation/SrgMethod.java b/src/main/java/org/spongepowered/asm/obfuscation/mapping/common/MappingMethod.java similarity index 63% rename from src/main/java/org/spongepowered/asm/obfuscation/SrgMethod.java rename to src/main/java/org/spongepowered/asm/obfuscation/mapping/common/MappingMethod.java index f0a9d8f6..2525eb57 100644 --- a/src/main/java/org/spongepowered/asm/obfuscation/SrgMethod.java +++ b/src/main/java/org/spongepowered/asm/obfuscation/mapping/common/MappingMethod.java @@ -22,32 +22,41 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -package org.spongepowered.asm.obfuscation; +package org.spongepowered.asm.obfuscation.mapping.common; + +import org.spongepowered.asm.obfuscation.mapping.IMapping; import com.google.common.base.Objects; /** - * Stores information about an SRG method mapping during AP runs + * Stores information about a method mapping during AP runs */ -public final class SrgMethod { +public class MappingMethod implements IMapping { private final String name; private final String desc; - public SrgMethod(String name, String desc) { + public MappingMethod(String name, String desc) { this.name = name; this.desc = desc; } - public SrgMethod(String owner, String simpleName, String desc) { - this.name = SrgMethod.createName(owner, simpleName); + public MappingMethod(String owner, String simpleName, String desc) { + this.name = MappingMethod.createName(owner, simpleName); this.desc = desc; } + @Override + public Type getType() { + return Type.METHOD; + } + + @Override public String getName() { return this.name; } + @Override public String getSimpleName() { if (this.name == null) { return null; @@ -56,6 +65,7 @@ public final class SrgMethod { return pos > -1 ? this.name.substring(pos + 1) : this.name; } + @Override public String getOwner() { if (this.name == null) { return null; @@ -64,21 +74,34 @@ public final class SrgMethod { return pos > -1 ? this.name.substring(0, pos) : null; } + @Override public String getDesc() { return this.desc; } - public SrgMethod move(String newOwner) { - return new SrgMethod(newOwner, this.getSimpleName(), this.desc); + @Override + public MappingMethod move(String newOwner) { + return new MappingMethod(newOwner, this.getSimpleName(), this.getDesc()); + } + + @Override + public MappingMethod remap(String newName) { + return new MappingMethod(this.getOwner(), newName, this.getDesc()); + } + + @Override + public MappingMethod transform(String newDesc) { + return new MappingMethod(this.getName(), newDesc); } - public SrgMethod copy() { - return new SrgMethod(this.name, this.desc); + @Override + public MappingMethod copy() { + return new MappingMethod(this.getName(), this.getDesc()); } @Override public int hashCode() { - return Objects.hashCode(this.name, this.desc); + return Objects.hashCode(this.getName(), this.getDesc()); } @Override @@ -86,11 +109,16 @@ public final class SrgMethod { if (this == obj) { return true; } - if (obj instanceof SrgMethod) { - return Objects.equal(this.name, ((SrgMethod)obj).name) && Objects.equal(this.desc, ((SrgMethod)obj).desc); + if (obj instanceof MappingMethod) { + return Objects.equal(this.name, ((MappingMethod)obj).name) && Objects.equal(this.desc, ((MappingMethod)obj).desc); } return false; } + + @Override + public String serialise() { + return this.toString(); + } @Override public String toString() { diff --git a/src/main/java/org/spongepowered/asm/obfuscation/SrgField.java b/src/main/java/org/spongepowered/asm/obfuscation/mapping/mcp/MappingFieldSrg.java similarity index 50% rename from src/main/java/org/spongepowered/asm/obfuscation/SrgField.java rename to src/main/java/org/spongepowered/asm/obfuscation/mapping/mcp/MappingFieldSrg.java index 30f25b4f..b6c76845 100644 --- a/src/main/java/org/spongepowered/asm/obfuscation/SrgField.java +++ b/src/main/java/org/spongepowered/asm/obfuscation/mapping/mcp/MappingFieldSrg.java @@ -22,68 +22,46 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -package org.spongepowered.asm.obfuscation; +package org.spongepowered.asm.obfuscation.mapping.mcp; -import com.google.common.base.Objects; +import org.spongepowered.asm.obfuscation.mapping.common.MappingField; /** - * Stores information about an SRG field mapping during AP runs + * An SRG field mapping */ -public final class SrgField { +public class MappingFieldSrg extends MappingField { - private final String mapping; + private final String srg; - public SrgField(String mapping) { - this.mapping = mapping; + public MappingFieldSrg(String srg) { + super(MappingFieldSrg.getOwnerFromSrg(srg), MappingFieldSrg.getNameFromSrg(srg), null); + this.srg = srg; } - public String getName() { - if (this.mapping == null) { + public MappingFieldSrg(MappingField field) { + super(field.getOwner(), field.getName(), null); + this.srg = field.getOwner() + "/" + field.getName(); + } + + @Override + public String serialise() { + return this.srg; + } + + private static String getNameFromSrg(String srg) { + if (srg == null) { return null; } - int pos = this.mapping.lastIndexOf('/'); - return pos > -1 ? this.mapping.substring(pos + 1) : this.mapping; + int pos = srg.lastIndexOf('/'); + return pos > -1 ? srg.substring(pos + 1) : srg; } - - public String getOwner() { - if (this.mapping == null) { + + private static String getOwnerFromSrg(String srg) { + if (srg == null) { return null; } - int pos = this.mapping.lastIndexOf('/'); - return pos > -1 ? this.mapping.substring(0, pos) : null; + int pos = srg.lastIndexOf('/'); + return pos > -1 ? srg.substring(0, pos) : null; } - public String getMapping() { - return this.mapping; - } - - public SrgField move(String newOwner) { - return new SrgField((newOwner != null ? newOwner + "/" : "") + this.getName()); - } - - public SrgField copy() { - return new SrgField(this.mapping); - } - - @Override - public int hashCode() { - return Objects.hashCode(this.mapping); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj instanceof SrgField) { - return Objects.equal(this.mapping, ((SrgField)obj).mapping); - } - return false; - } - - @Override - public String toString() { - return this.mapping; - } - -} \ No newline at end of file +}