From 54fa0f4e768a7d282a1d9eb59d8c06d7c6c44d16 Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Tue, 26 Jan 2016 17:29:48 +0000 Subject: [PATCH] Temp fix for downstream SRGs not remapping members, fixes #99 --- .../tools/obfuscation/ObfuscationManager.java | 5 +- .../TargetObfuscationEnvironment.java | 27 +++++- .../asm/obfuscation/SrgContainer.java | 8 +- .../asm/obfuscation/SrgField.java | 86 +++++++++++++++++++ .../asm/obfuscation/SrgMethod.java | 8 ++ 5 files changed, 125 insertions(+), 9 deletions(-) create mode 100644 src/main/java/org/spongepowered/asm/obfuscation/SrgField.java diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationManager.java b/src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationManager.java index 27962169..19f1f9df 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationManager.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationManager.java @@ -37,6 +37,7 @@ import javax.tools.StandardLocation; import org.spongepowered.asm.mixin.injection.struct.MemberInfo; import org.spongepowered.asm.mixin.injection.struct.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; @@ -281,9 +282,9 @@ public class ObfuscationManager implements IObfuscationManager { ObfuscationData data = new ObfuscationData(); for (TargetObfuscationEnvironment targetEnv : this.targetEnvironments) { - String obfField = targetEnv.getObfField(field); + SrgField obfField = targetEnv.getObfField(field); if (obfField != null) { - data.add(targetEnv.getType(), obfField); + data.add(targetEnv.getType(), obfField.toString()); } } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/TargetObfuscationEnvironment.java b/src/ap/java/org/spongepowered/tools/obfuscation/TargetObfuscationEnvironment.java index 7434e65e..b17d566f 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/TargetObfuscationEnvironment.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/TargetObfuscationEnvironment.java @@ -45,6 +45,7 @@ import org.spongepowered.asm.mixin.injection.struct.MemberInfo; import org.spongepowered.asm.mixin.injection.struct.ReferenceMapper; import org.spongepowered.asm.obfuscation.SrgMethod; import org.spongepowered.asm.obfuscation.SrgContainer; +import org.spongepowered.asm.obfuscation.SrgField; import org.spongepowered.asm.util.ObfuscationUtil; import org.spongepowered.asm.util.ObfuscationUtil.IClassRemapper; import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor; @@ -198,7 +199,17 @@ class TargetObfuscationEnvironment { */ public SrgMethod getObfMethod(SrgMethod method) { if (this.initSrgs()) { - return this.srgs.getMethodMapping(method); + SrgMethod originalMethod = method.copy(); + SrgMethod methodMapping = this.srgs.getMethodMapping(method); + // If no obf mapping, we can attempt to remap the owner class + if (methodMapping == null) { + methodMapping = originalMethod; + } + String remappedOwner = this.getObfClass(methodMapping.getOwner()); + if (remappedOwner == null || remappedOwner.equals(method.getOwner()) || remappedOwner.equals(methodMapping.getOwner())) { + return methodMapping != originalMethod ? methodMapping : null; + } + return methodMapping.move(remappedOwner); } return null; } @@ -236,9 +247,19 @@ class TargetObfuscationEnvironment { /** * Get an obfuscation mapping for a field */ - public String getObfField(String field) { + public SrgField getObfField(String field) { if (this.initSrgs()) { - return this.srgs.getFieldMapping(field); + 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) { + 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); } return null; } diff --git a/src/main/java/org/spongepowered/asm/obfuscation/SrgContainer.java b/src/main/java/org/spongepowered/asm/obfuscation/SrgContainer.java index 24dd72c2..c2a9289f 100644 --- a/src/main/java/org/spongepowered/asm/obfuscation/SrgContainer.java +++ b/src/main/java/org/spongepowered/asm/obfuscation/SrgContainer.java @@ -41,9 +41,9 @@ import joptsimple.internal.Strings; */ public class SrgContainer { - private final BiMap classMap = HashBiMap.create(); - private final BiMap fieldMap = HashBiMap.create(); 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 { @@ -60,7 +60,7 @@ public class SrgContainer { } else if (type.equals("CL")) { this.classMap.forcePut(args[0], args[1]); } else if (type.equals("FD")) { - this.fieldMap.forcePut(args[0], args[1]); + 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 { @@ -73,7 +73,7 @@ public class SrgContainer { return this.methodMap.get(methodName); } - public String getFieldMapping(String fieldName) { + public SrgField getFieldMapping(SrgField fieldName) { return this.fieldMap.get(fieldName); } diff --git a/src/main/java/org/spongepowered/asm/obfuscation/SrgField.java b/src/main/java/org/spongepowered/asm/obfuscation/SrgField.java new file mode 100644 index 00000000..77b747d9 --- /dev/null +++ b/src/main/java/org/spongepowered/asm/obfuscation/SrgField.java @@ -0,0 +1,86 @@ +/* + * 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 com.google.common.base.Objects; + +public final class SrgField { + + private final String mapping; + + public SrgField(String mapping) { + this.mapping = mapping; + } + + public String getName() { + if (this.mapping == null) { + return null; + } + int pos = this.mapping.lastIndexOf('/'); + return pos > -1 ? this.mapping.substring(pos + 1) : this.mapping; + } + + public String getOwner() { + if (this.mapping == null) { + return null; + } + int pos = this.mapping.lastIndexOf('/'); + return pos > -1 ? this.mapping.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 diff --git a/src/main/java/org/spongepowered/asm/obfuscation/SrgMethod.java b/src/main/java/org/spongepowered/asm/obfuscation/SrgMethod.java index d57f28a6..d1b9c91f 100644 --- a/src/main/java/org/spongepowered/asm/obfuscation/SrgMethod.java +++ b/src/main/java/org/spongepowered/asm/obfuscation/SrgMethod.java @@ -60,6 +60,14 @@ public final class SrgMethod { return this.desc; } + public SrgMethod move(String newOwner) { + return new SrgMethod((newOwner != null ? newOwner + "/" : "") + this.getSimpleName(), this.desc); + } + + public SrgMethod copy() { + return new SrgMethod(this.name, this.desc); + } + @Override public int hashCode() { return Objects.hashCode(this.name, this.desc);