Obf changes, remove srg2source, add ctors to refmap, fixes #96 fixes #97

This commit is contained in:
Mumfrey committed 2016-01-26 13:41:16 +00:00
1 parent 2c9e3d1d39
commit 5afccbd4dd
15 files changed
+382 -85

No files matched your search

+2 -14
View File
@@ -43,16 +43,6 @@ targetCompatibility = '1.6'
// Project repositories
repositories {
mavenCentral()
maven {
// for srg2source
name = "forge"
url = "http://files.minecraftforge.net/maven"
}
maven {
// because Srg2Source needs an eclipse dependency.
name = "eclipse"
url = "https://repo.eclipse.org/content/groups/eclipse/"
}
maven {
name = 'minecraft'
url = 'https://libraries.minecraft.net/'
@@ -142,6 +132,7 @@ dependencies {
compile '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'
testCompile 'junit:junit:4.11'
testCompile 'org.hamcrest:hamcrest-library:1.3'
testCompile 'org.mockito:mockito-core:1.9.0'
@@ -149,10 +140,6 @@ dependencies {
// maven
deployerJars 'org.apache.maven.wagon:wagon-ftp:2.7'
// for annotation processor
compile 'net.minecraftforge.srg2source:Srg2Source:3.2-SNAPSHOT'
apCompile 'net.minecraftforge.srg2source:Srg2Source:3.2-SNAPSHOT'
// remapped ASM
compile tasks.renamedASM.outputs.files
apCompile tasks.renamedASM.outputs.files
@@ -169,6 +156,7 @@ dependencies {
agentCompile 'net.minecraft:launchwrapper:1.11'
// asm bridge
compile 'org.ow2.asm:asm-commons:5.0.3'
bridgeCompile 'org.ow2.asm:asm-commons:5.0.3'
bridgeCompile 'org.apache.logging.log4j:log4j-core:2.0-beta9'
}
+1 -1
View File
@@ -4,5 +4,5 @@ packaging=jar
description=Mixin
url=https://www.spongepowered.org
organization=SpongePowered
buildVersion=0.4.16
buildVersion=0.4.17
buildType=SNAPSHOT
@@ -35,6 +35,7 @@ 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.MethodData;
import org.spongepowered.asm.util.ConstraintParser;
import org.spongepowered.asm.util.ConstraintParser.Constraint;
import org.spongepowered.asm.util.ConstraintViolationException;
@@ -43,8 +44,6 @@ import org.spongepowered.tools.MirrorUtils;
import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor;
import org.spongepowered.tools.obfuscation.interfaces.IObfuscationManager;
import net.minecraftforge.srg2source.rangeapplier.MethodData;
abstract class AnnotatedMixinElementHandler {
/**
@@ -36,13 +36,12 @@ 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.MethodData;
import org.spongepowered.asm.util.Constants;
import org.spongepowered.tools.MirrorUtils;
import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor;
import org.spongepowered.tools.obfuscation.struct.Message;
import net.minecraftforge.srg2source.rangeapplier.MethodData;
class AnnotatedMixinInjectorHandler extends AnnotatedMixinElementHandler {
AnnotatedMixinInjectorHandler(IMixinAnnotationProcessor ap, AnnotatedMixin mixin) {
@@ -30,11 +30,10 @@ import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.ExecutableElement;
import javax.tools.Diagnostic.Kind;
import org.spongepowered.asm.obfuscation.MethodData;
import org.spongepowered.tools.MirrorUtils;
import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor;
import net.minecraftforge.srg2source.rangeapplier.MethodData;
class AnnotatedMixinOverwriteHandler extends AnnotatedMixinElementHandler {
AnnotatedMixinOverwriteHandler(IMixinAnnotationProcessor ap, AnnotatedMixin mixin) {
@@ -73,8 +72,7 @@ class AnnotatedMixinOverwriteHandler extends AnnotatedMixinElementHandler {
for (ObfuscationType type : obfData) {
MethodData obfMethod = obfData.get(type);
String obfName = obfMethod.name.substring(obfMethod.name.lastIndexOf('/') + 1);
this.addMethodMapping(type, mcpName, obfName, mcpSignature, obfMethod.sig);
this.addMethodMapping(type, mcpName, obfMethod.getSimpleName(), mcpSignature, obfMethod.getDesc());
}
if (!"true".equalsIgnoreCase(this.ap.getOption(SupportedOptions.DISABLE_OVERWRITE_CHECKER))) {
@@ -29,11 +29,10 @@ import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.VariableElement;
import javax.tools.Diagnostic.Kind;
import org.spongepowered.asm.obfuscation.MethodData;
import org.spongepowered.tools.MirrorUtils;
import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor;
import net.minecraftforge.srg2source.rangeapplier.MethodData;
class AnnotatedMixinShadowHandler extends AnnotatedMixinElementHandler {
AnnotatedMixinShadowHandler(IMixinAnnotationProcessor ap, AnnotatedMixin mixin) {
@@ -85,7 +84,7 @@ class AnnotatedMixinShadowHandler extends AnnotatedMixinElementHandler {
for (ObfuscationType type : obfData) {
MethodData obfMethod = obfData.get(type);
this.addMethodMapping(type, name.setObfuscatedName(obfMethod.name), mcpSignature, obfMethod.sig);
this.addMethodMapping(type, name.setObfuscatedName(obfMethod.getName()), mcpSignature, obfMethod.getDesc());
}
}
@@ -37,11 +37,11 @@ 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.MethodData;
import org.spongepowered.asm.util.Constants;
import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor;
import org.spongepowered.tools.obfuscation.interfaces.IObfuscationManager;
import net.minecraftforge.srg2source.rangeapplier.MethodData;
public class ObfuscationManager implements IObfuscationManager {
/**
@@ -215,7 +215,11 @@ public class ObfuscationManager implements IObfuscationManager {
}
}
return data;
if (!data.isEmpty() || !Constants.INIT.equals(method.name)) {
return data;
}
return this.remapDescriptor(data, method);
}
/* (non-Javadoc)
@@ -233,6 +237,28 @@ public class ObfuscationManager implements IObfuscationManager {
}
}
if (!data.isEmpty() || !Constants.INIT.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<MethodData> remapDescriptor(ObfuscationData<MethodData> data, MemberInfo method) {
for (TargetObfuscationEnvironment targetEnv : this.targetEnvironments) {
MemberInfo obfMethod = targetEnv.remapDescriptor(method);
if (obfMethod != null) {
data.add(targetEnv.getType(), obfMethod.asMethodData());
}
}
return data;
}
@@ -301,10 +327,7 @@ public class ObfuscationManager implements IObfuscationManager {
for (TargetObfuscationEnvironment targetEnv : this.targetEnvironments) {
MethodData obfMethod = obfMethodData.get(targetEnv.getType());
if (obfMethod != null) {
int slashPos = obfMethod.name.lastIndexOf('/');
String obfName = obfMethod.name.substring(slashPos + 1);
String obfOwner = obfMethod.name.substring(0, slashPos);
MemberInfo remappedReference = new MemberInfo(obfName, obfOwner, obfMethod.sig, false);
MemberInfo remappedReference = new MemberInfo(obfMethod);
targetEnv.addMapping(className, reference, remappedReference.toString());
}
}
@@ -43,13 +43,41 @@ 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.MethodData;
import org.spongepowered.asm.obfuscation.SrgContainer;
import org.spongepowered.asm.util.ObfuscationUtil;
import org.spongepowered.asm.util.ObfuscationUtil.IClassRemapper;
import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor;
import net.minecraftforge.srg2source.rangeapplier.MethodData;
import net.minecraftforge.srg2source.rangeapplier.SrgContainer;
class TargetObfuscationEnvironment {
final class RemapperProxy implements IClassRemapper {
@Override
public String map(String typeName) {
if (TargetObfuscationEnvironment.this.srgs == null) {
return null;
}
return TargetObfuscationEnvironment.this.srgs.getClassMapping(typeName);
}
@Override
public String unmap(String typeName) {
if (TargetObfuscationEnvironment.this.srgs == null) {
return null;
}
return TargetObfuscationEnvironment.this.srgs.getClassMapping(typeName);
}
}
/**
* SRG container for mcp->? mappings
*/
protected SrgContainer srgs;
private final RemapperProxy remapper = new RemapperProxy();
/**
* Type
*/
@@ -75,11 +103,6 @@ class TargetObfuscationEnvironment {
*/
private final ReferenceMapper refMapper;
/**
* SRG container for mcp->? mappings
*/
private SrgContainer srgs;
/**
* True once we've tried to initialise the srgs, initially false so that we
* can do srg init lazily
@@ -175,17 +198,47 @@ class TargetObfuscationEnvironment {
*/
public MethodData getObfMethod(MethodData method) {
if (this.initSrgs()) {
return this.srgs.methodMap.get(method);
return this.srgs.getMethodMapping(method);
}
return null;
}
/**
* Remap only the owner and descriptor of the specified method
*
* @param method method to remap
* @return remapped method or null if no remapping occurred
*/
public MemberInfo remapDescriptor(MemberInfo method) {
boolean transformed = false;
String owner = method.owner;
if (owner != null) {
String newOwner = this.remapper.map(owner);
if (newOwner != null) {
owner = newOwner;
transformed = true;
}
}
String desc = method.desc;
if (desc != null) {
String newDesc = ObfuscationUtil.mapDescriptor(method.desc, this.remapper);
if (!newDesc.equals(method.desc)) {
desc = newDesc;
transformed = true;
}
}
return transformed ? new MemberInfo(method.name, owner, desc, method.matchAll) : null;
}
/**
* Get an obfuscation mapping for a field
*/
public String getObfField(String field) {
if (this.initSrgs()) {
return this.srgs.fieldMap.get(field);
return this.srgs.getFieldMapping(field);
}
return null;
}
@@ -195,7 +248,7 @@ class TargetObfuscationEnvironment {
*/
public String getObfClass(String className) {
if (this.initSrgs()) {
return this.srgs.classMap.get(className);
return this.srgs.getClassMapping(className);
}
return null;
}
@@ -26,11 +26,10 @@ package org.spongepowered.tools.obfuscation.interfaces;
import org.spongepowered.asm.mixin.injection.struct.MemberInfo;
import org.spongepowered.asm.mixin.injection.struct.ReferenceMapper;
import org.spongepowered.asm.obfuscation.MethodData;
import org.spongepowered.tools.obfuscation.ObfuscationData;
import org.spongepowered.tools.obfuscation.TypeHandle;
import net.minecraftforge.srg2source.rangeapplier.MethodData;
/**
* Manages obfuscation things
*/
@@ -28,8 +28,10 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.objectweb.asm.commons.Remapper;
import org.spongepowered.asm.mixin.extensibility.IRemapper;
import org.spongepowered.asm.util.ObfuscationUtil;
import org.spongepowered.asm.util.ObfuscationUtil.IClassRemapper;
public abstract class RemapperAdapter implements IRemapper {
public abstract class RemapperAdapter implements IRemapper, IClassRemapper {
protected final Logger logger = LogManager.getLogger("mixin");
protected final Remapper remapper;
@@ -87,32 +89,7 @@ public abstract class RemapperAdapter implements IRemapper {
@Override
public String unmapDesc(String desc) {
StringBuilder sb = new StringBuilder();
StringBuilder token = null;
for (int pos = 0; pos < desc.length(); pos++) {
char c = desc.charAt(pos);
if (token != null) {
if (c == ';') {
sb.append('L').append(this.unmap(token.toString())).append(';');
token = null;
} else {
token.append(c);
}
continue;
}
if (c == 'L') {
token = new StringBuilder();
} else {
sb.append(c);
}
}
if (token != null) {
throw new IllegalArgumentException("Invalid descriptor '" + desc + "', missing ';'");
}
return sb.toString();
return ObfuscationUtil.unmapDescriptor(desc, this);
}
}
@@ -60,7 +60,7 @@ public abstract class MixinBootstrap {
/**
* Subsystem version
*/
public static final String VERSION = "0.4.16";
public static final String VERSION = "0.4.17";
/**
* Blackboard key where the subsystem version will be stored to indicate
@@ -29,10 +29,9 @@ import org.spongepowered.asm.lib.tree.AbstractInsnNode;
import org.spongepowered.asm.lib.tree.FieldInsnNode;
import org.spongepowered.asm.lib.tree.MethodInsnNode;
import org.spongepowered.asm.mixin.transformer.IReferenceMapperContext;
import org.spongepowered.asm.obfuscation.MethodData;
import org.spongepowered.asm.util.SignaturePrinter;
import net.minecraftforge.srg2source.rangeapplier.MethodData;
/**
* <p>Information bundle about a member (method or field) parsed from a String
* token in another annotation, this is used where target members need to be
@@ -168,11 +167,10 @@ public class MemberInfo {
*
* @param methodData MethodData object to copy values from
*/
private MemberInfo(MethodData methodData) {
int slashPos = methodData.name.lastIndexOf('/');
this.owner = methodData.name.substring(0, slashPos);
this.name = methodData.name.substring(slashPos + 1);
this.desc = methodData.sig;
public MemberInfo(MethodData methodData) {
this.owner = methodData.getOwner();
this.name = methodData.getSimpleName();
this.desc = methodData.getDesc();
this.matchAll = false;
}
@@ -182,10 +180,9 @@ public class MemberInfo {
* @param methodData MethodData object to copy values from
*/
private MemberInfo(MemberInfo remapped, MethodData methodData, boolean setOwner) {
int slashPos = methodData.name.lastIndexOf('/');
this.owner = setOwner ? methodData.name.substring(0, slashPos) : remapped.owner;
this.name = methodData.name.substring(slashPos + 1);
this.desc = methodData.sig;
this.owner = setOwner ? methodData.getOwner() : remapped.owner;
this.name = methodData.getSimpleName();
this.desc = methodData.getDesc();
this.matchAll = remapped.matchAll;
}
@@ -0,0 +1,84 @@
/*
* This file is part of Mixin, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.asm.obfuscation;
import com.google.common.base.Objects;
public final class MethodData {
private final String name;
private final String desc;
public MethodData(String name, String sig) {
this.name = name;
this.desc = sig;
}
public String getName() {
return this.name;
}
public String getSimpleName() {
if (this.name == null) {
return null;
}
int pos = this.name.lastIndexOf('/');
return pos > -1 ? this.name.substring(pos + 1) : this.name;
}
public String getOwner() {
if (this.name == null) {
return null;
}
int pos = this.name.lastIndexOf('/');
return pos > -1 ? this.name.substring(0, pos) : null;
}
public String getDesc() {
return this.desc;
}
@Override
public int hashCode() {
return Objects.hashCode(this.name, this.desc);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof MethodData) {
return Objects.equal(this.name, ((MethodData)obj).name) && Objects.equal(this.desc, ((MethodData)obj).desc);
}
return false;
}
@Override
public String toString() {
return String.format("%s %s", this.name, this.desc);
}
}
@@ -0,0 +1,88 @@
/*
* This file is part of Mixin, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.asm.obfuscation;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.io.Files;
import joptsimple.internal.Strings;
/**
* Ported from <strong>Srg2Source</strong> (
* <a href=\"https://github.com/MinecraftForge/Srg2Source\">
* github.com/MinecraftForge/Srg2Source</a>).
*/
public class SrgContainer {
private final BiMap<String, String> classMap = HashBiMap.create();
private final BiMap<String, String> fieldMap = HashBiMap.create();
private final BiMap<String, String> packageMap = HashBiMap.create();
private final BiMap<MethodData, MethodData> 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(args[0], args[1]);
} else if (type.equals("MD")) {
this.methodMap.forcePut(new MethodData(args[0], args[1]), new MethodData(args[2], args[3]));
} else {
throw new RuntimeException("Invalid SRG file: " + srg);
}
}
}
public MethodData getMethodMapping(MethodData methodName) {
return this.methodMap.get(methodName);
}
public String getFieldMapping(String 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);
}
}
@@ -0,0 +1,93 @@
/*
* This file is part of Mixin, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.asm.util;
public abstract class ObfuscationUtil {
public interface IClassRemapper {
/**
* Map type name to the new name. Subclasses can override.
*
* @param typeName Class name to convert
* @return new name for the class
*/
public abstract String map(String typeName);
/**
* Convert a mapped type name back to the original obfuscated name
*
* @param typeName Class name to convert
* @return old name for the class
*/
public abstract String unmap(String typeName);
}
private ObfuscationUtil() {}
public static String mapDescriptor(String desc, IClassRemapper remapper) {
return ObfuscationUtil.remapDescriptor(desc, remapper, false);
}
public static String unmapDescriptor(String desc, IClassRemapper remapper) {
return ObfuscationUtil.remapDescriptor(desc, remapper, true);
}
private static String remapDescriptor(String desc, IClassRemapper remapper, boolean unmap) {
StringBuilder sb = new StringBuilder();
StringBuilder token = null;
for (int pos = 0; pos < desc.length(); pos++) {
char c = desc.charAt(pos);
if (token != null) {
if (c == ';') {
sb.append('L').append(ObfuscationUtil.remap(token.toString(), remapper, unmap)).append(';');
token = null;
} else {
token.append(c);
}
continue;
}
if (c == 'L') {
token = new StringBuilder();
} else {
sb.append(c);
}
}
if (token != null) {
throw new IllegalArgumentException("Invalid descriptor '" + desc + "', missing ';'");
}
return sb.toString();
}
private static Object remap(String typeName, IClassRemapper remapper, boolean unmap) {
String result = unmap ? remapper.unmap(typeName) : remapper.map(typeName);
return result != null ? result : typeName;
}
}