Rewrite gregtech.asm to clean ASM and removed javassist

This commit is contained in:
Dragon2488 2016-09-12 22:27:35 +07:00
parent 461c02e69f
commit 235b6310f8
2 changed files with 38 additions and 38 deletions

View file

@ -86,7 +86,6 @@ dependencies {
//provided "codechicken:CodeChickenLib:${config.minecraft.version}-${config.codechickenlib.version}:dev" //provided "codechicken:CodeChickenLib:${config.minecraft.version}-${config.codechickenlib.version}:dev"
//provided "codechicken:CodeChickenCore:${config.minecraft.version}-${config.codechickencore.version}:dev" //provided "codechicken:CodeChickenCore:${config.minecraft.version}-${config.codechickencore.version}:dev"
//provided "codechicken:NotEnoughItems:${config.minecraft.version}-${config.nei.version}:dev" //provided "codechicken:NotEnoughItems:${config.minecraft.version}-${config.nei.version}:dev"
compile "org.javassist:javassist:3.20.0-GA"
provided "net.industrial-craft:industrialcraft-2:${config.ic2.version}:dev" provided "net.industrial-craft:industrialcraft-2:${config.ic2.version}:dev"
deobfCompile "net.sengir.forestry:forestry_${config.minecraft.version}:${config.forestry.version}" deobfCompile "net.sengir.forestry:forestry_${config.minecraft.version}:${config.forestry.version}"
deobfCompile "mezz.jei:jei_${config.minecraft.version}:+" deobfCompile "mezz.jei:jei_${config.minecraft.version}:+"
@ -142,13 +141,6 @@ jar {
attributes 'FMLCorePlugin': 'gregtech.asm.ASM' attributes 'FMLCorePlugin': 'gregtech.asm.ASM'
attributes 'FMLCorePluginContainsFMLMod': 'true' attributes 'FMLCorePluginContainsFMLMod': 'true'
} }
doFirst {
for(file in configurations.compile) {
if(file.name.contains("javassist")) {
from zipTree(file)
}
}
}
} }
task dev(type: Jar) { task dev(type: Jar) {
@ -158,13 +150,6 @@ task dev(type: Jar) {
attributes 'FMLCorePlugin': 'gregtech.asm.ASM' attributes 'FMLCorePlugin': 'gregtech.asm.ASM'
attributes 'FMLCorePluginContainsFMLMod': 'true' attributes 'FMLCorePluginContainsFMLMod': 'true'
} }
doFirst {
for(file in configurations.compile) {
if(file.name.contains("javassist")) {
from zipTree(file)
}
}
}
} }
artifacts { artifacts {

View file

@ -1,8 +1,11 @@
package gregtech.asm; package gregtech.asm;
import javassist.*;
import net.minecraft.launchwrapper.IClassTransformer; import net.minecraft.launchwrapper.IClassTransformer;
import net.minecraftforge.fml.relauncher.IFMLLoadingPlugin; import net.minecraftforge.fml.relauncher.IFMLLoadingPlugin;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.*;
import java.util.Map; import java.util.Map;
@ -10,31 +13,45 @@ import java.util.Map;
@IFMLLoadingPlugin.SortingIndex(10001) @IFMLLoadingPlugin.SortingIndex(10001)
public class ASM implements IFMLLoadingPlugin, IClassTransformer { public class ASM implements IFMLLoadingPlugin, IClassTransformer {
private static ClassPool pool = ClassPool.getDefault();
private static final String OBF_METHOD_NAME = "func_178125_b"; private static final String OBF_METHOD_NAME = "func_178125_b";
private static boolean runtimeDeobfuscation = false; private static final String BLOCK_RENDERER = "gregtech/common/render/newblocks/BlockRenderer";
private static final String SHOULD_HOOK_DESC = "(Lnet/minecraft/block/state/IBlockState;)Z";
static { private static final String HOOK_DESC = "(Lnet/minecraft/block/state/IBlockState;)Lnet/minecraft/client/renderer/block/model/IBakedModel;";
pool.appendClassPath(new LoaderClassPath(Thread.currentThread().getContextClassLoader()));
pool.appendClassPath(new LoaderClassPath(ClassLoader.getSystemClassLoader()));
}
@Override @Override
public byte[] transform(String s, String s1, byte[] bytes) { public byte[] transform(String s, String s1, byte[] bytes) {
if(s1.equals("net.minecraft.client.renderer.BlockModelShapes")) { if(s1.equals("net.minecraft.client.renderer.BlockModelShapes")) {
pool.appendClassPath(new ByteArrayClassPath(s1, bytes)); ClassReader classReader = new ClassReader(bytes);
CtClass ctClass = pool.getOrNull(s1); ClassNode classNode = new ClassNode();
System.out.println(s1); classReader.accept(classNode, 0);
try {
CtMethod method = ctClass.getDeclaredMethod(runtimeDeobfuscation ? OBF_METHOD_NAME : "getModelForState"); for(MethodNode method : classNode.methods) {
method.insertBefore( if(method.name.equals("getModelForState") || method.name.equals(OBF_METHOD_NAME)) {
"if(gregtech.common.render.newblocks.BlockRenderer.shouldHook($1)) {" + InsnList insnList = new InsnList();
"return gregtech.common.render.newblocks.BlockRenderer.hook($1); }"); // load block state argument
return ctClass.toBytecode(); insnList.add(new VarInsnNode(Opcodes.ALOAD, 1));
} catch (Exception exception) { //shouldHook: pop IBlockState -> push boolean
exception.printStackTrace(); insnList.add(new MethodInsnNode(Opcodes.INVOKESTATIC, BLOCK_RENDERER, "shouldHook", SHOULD_HOOK_DESC, false));
LabelNode labelNode = new LabelNode();
//pop boolean and jump if 0
insnList.add(new JumpInsnNode(Opcodes.IFEQ, labelNode));
// load block state argument
insnList.add(new VarInsnNode(Opcodes.ALOAD, 1));
//hook: pop IBlockState -> push IBakedModel
insnList.add(new MethodInsnNode(Opcodes.INVOKESTATIC, BLOCK_RENDERER, "hook", HOOK_DESC, false));
//returnL pop IBakedModel
insnList.add(new InsnNode(Opcodes.ARETURN));
//normal execution
insnList.add(labelNode);
//inject before method code
method.instructions.insert(insnList);
}
} }
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
classNode.accept(classWriter);
return classWriter.toByteArray();
} }
return bytes; return bytes;
} }
@ -55,9 +72,7 @@ public class ASM implements IFMLLoadingPlugin, IClassTransformer {
} }
@Override @Override
public void injectData(Map<String, Object> data) { public void injectData(Map<String, Object> data) {}
runtimeDeobfuscation = (Boolean) data.get("runtimeDeobfuscationEnabled");
}
@Override @Override
public String getAccessTransformerClass() { public String getAccessTransformerClass() {