From 498643bfdd8add4f32d629b6a0908ae4a467e7b9 Mon Sep 17 00:00:00 2001 From: Shadl7 Date: Wed, 17 Jul 2024 00:37:55 +0700 Subject: [PATCH] Rework coremod and transformers. Fixes CleanroomMC/Cleanroom#32 --- .../skins/init/forge/asm/ASMTransformer.java | 44 +++-- .../lain/mods/skins/init/forge/asm/Hooks.java | 1 + .../mods/skins/init/forge/asm/ObfHelper.java | 177 +----------------- .../mods/skins/init/forge/asm/Plugin.java | 9 +- .../lain/mods/skins/init/forge/asm/Setup.java | 121 ------------ 5 files changed, 44 insertions(+), 308 deletions(-) delete mode 100644 src/main/java/lain/mods/skins/init/forge/asm/Setup.java diff --git a/src/main/java/lain/mods/skins/init/forge/asm/ASMTransformer.java b/src/main/java/lain/mods/skins/init/forge/asm/ASMTransformer.java index 6a538f3..e321325 100644 --- a/src/main/java/lain/mods/skins/init/forge/asm/ASMTransformer.java +++ b/src/main/java/lain/mods/skins/init/forge/asm/ASMTransformer.java @@ -7,6 +7,7 @@ import org.objectweb.asm.Opcodes; import net.minecraft.launchwrapper.IClassTransformer; +@SuppressWarnings("unused") public class ASMTransformer implements IClassTransformer { @@ -29,7 +30,8 @@ public void visitInsn(int opcode) this.visitVarInsn(Opcodes.ASTORE, 1); this.visitVarInsn(Opcodes.ALOAD, 0); this.visitVarInsn(Opcodes.ALOAD, 1); - this.visitMethodInsn(Opcodes.INVOKESTATIC, "lain/mods/skins/init/forge/asm/Hooks", "getLocationCape", "(Lnet/minecraft/client/entity/AbstractClientPlayer;Lnet/minecraft/util/ResourceLocation;)Lnet/minecraft/util/ResourceLocation;", false); + this.visitMethodInsn(Opcodes.INVOKESTATIC, "lain/mods/skins/init/forge/asm/Hooks", "getLocationCape", + "(Lnet/minecraft/client/entity/AbstractClientPlayer;Lnet/minecraft/util/ResourceLocation;)Lnet/minecraft/util/ResourceLocation;", false); } super.visitInsn(opcode); } @@ -52,7 +54,8 @@ public void visitInsn(int opcode) this.visitVarInsn(Opcodes.ASTORE, 1); this.visitVarInsn(Opcodes.ALOAD, 0); this.visitVarInsn(Opcodes.ALOAD, 1); - this.visitMethodInsn(Opcodes.INVOKESTATIC, "lain/mods/skins/init/forge/asm/Hooks", "getLocationSkin", "(Lnet/minecraft/client/entity/AbstractClientPlayer;Lnet/minecraft/util/ResourceLocation;)Lnet/minecraft/util/ResourceLocation;", false); + this.visitMethodInsn(Opcodes.INVOKESTATIC, "lain/mods/skins/init/forge/asm/Hooks", "getLocationSkin", + "(Lnet/minecraft/client/entity/AbstractClientPlayer;Lnet/minecraft/util/ResourceLocation;)Lnet/minecraft/util/ResourceLocation;", false); } super.visitInsn(opcode); } @@ -75,16 +78,23 @@ public void visitInsn(int opcode) this.visitVarInsn(Opcodes.ASTORE, 1); this.visitVarInsn(Opcodes.ALOAD, 0); this.visitVarInsn(Opcodes.ALOAD, 1); - this.visitMethodInsn(Opcodes.INVOKESTATIC, "lain/mods/skins/init/forge/asm/Hooks", "getSkinType", "(Lnet/minecraft/client/entity/AbstractClientPlayer;Ljava/lang/String;)Ljava/lang/String;", false); + this.visitMethodInsn(Opcodes.INVOKESTATIC, "lain/mods/skins/init/forge/asm/Hooks", "getSkinType", + "(Lnet/minecraft/client/entity/AbstractClientPlayer;Ljava/lang/String;)Ljava/lang/String;", false); } super.visitInsn(opcode); } } - ObfHelper m001 = ObfHelper.newMethod("func_110303_q", "net/minecraft/client/entity/AbstractClientPlayer", "()Lnet/minecraft/util/ResourceLocation;").setDevName("getLocationCape"); - ObfHelper m002 = ObfHelper.newMethod("func_110306_p", "net/minecraft/client/entity/AbstractClientPlayer", "()Lnet/minecraft/util/ResourceLocation;").setDevName("getLocationSkin"); - ObfHelper m003 = ObfHelper.newMethod("func_175154_l", "net/minecraft/client/entity/AbstractClientPlayer", "()Ljava/lang/String;").setDevName("getSkinType"); + ObfHelper m001 = ObfHelper.newMethod("func_110303_q", "getLocationCape", + "net/minecraft/client/entity/AbstractClientPlayer", + "()Lnet/minecraft/util/ResourceLocation;"); + ObfHelper m002 = ObfHelper.newMethod("func_110306_p", "getLocationSkin", + "net/minecraft/client/entity/AbstractClientPlayer", + "()Lnet/minecraft/util/ResourceLocation;"); + ObfHelper m003 = ObfHelper.newMethod("func_175154_l", "getSkinType", + "net/minecraft/client/entity/AbstractClientPlayer", + "()Ljava/lang/String;"); public transformer001(ClassVisitor cv) { @@ -111,7 +121,9 @@ class transformer002 extends ClassVisitor class method001 extends MethodVisitor { - ObfHelper target = ObfHelper.newMethod("func_147499_a", "net/minecraft/client/renderer/tileentity/TileEntitySpecialRenderer", "(Lnet/minecraft/util/ResourceLocation;)V").setDevName("bindTexture"); + ObfHelper target = ObfHelper.newMethod("func_147499_a", "bindTexture", + "net/minecraft/client/renderer/tileentity/TileEntitySpecialRenderer", + "(Lnet/minecraft/util/ResourceLocation;)V"); int lastALOAD = -1; @@ -128,7 +140,8 @@ public void visitMethodInsn(int opcode, String owner, String name, String desc, this.visitInsn(Opcodes.POP); this.visitVarInsn(Opcodes.ALOAD, 7); this.visitVarInsn(Opcodes.ALOAD, 11); - this.visitMethodInsn(Opcodes.INVOKESTATIC, "lain/mods/skins/init/forge/asm/Hooks", "TileEntitySkullRenderer_bindTexture", "(Lcom/mojang/authlib/GameProfile;Lnet/minecraft/util/ResourceLocation;)Lnet/minecraft/util/ResourceLocation;", false); + this.visitMethodInsn(Opcodes.INVOKESTATIC, "lain/mods/skins/init/forge/asm/Hooks", "TileEntitySkullRenderer_bindTexture", + "(Lcom/mojang/authlib/GameProfile;Lnet/minecraft/util/ResourceLocation;)Lnet/minecraft/util/ResourceLocation;", false); this.visitVarInsn(Opcodes.ASTORE, 11); this.visitVarInsn(Opcodes.ALOAD, 11); } @@ -145,7 +158,9 @@ public void visitVarInsn(int opcode, int var) } - ObfHelper m001 = ObfHelper.newMethod("func_188190_a", "net/minecraft/client/renderer/tileentity/TileEntitySkullRenderer", "(FFFLnet/minecraft/util/EnumFacing;FILcom/mojang/authlib/GameProfile;IF)V").setDevName("renderSkull"); + ObfHelper m001 = ObfHelper.newMethod("func_188190_a", "renderSkull", + "net/minecraft/client/renderer/tileentity/TileEntitySkullRenderer", + "(FFFLnet/minecraft/util/EnumFacing;FILcom/mojang/authlib/GameProfile;IF)V"); public transformer002(ClassVisitor cv) { @@ -168,7 +183,9 @@ class transformer003 extends ClassVisitor class method001 extends MethodVisitor { - ObfHelper target = ObfHelper.newMethod("func_110577_a", "net/minecraft/client/renderer/texture/TextureManager", "(Lnet/minecraft/util/ResourceLocation;)V").setDevName("bindTexture"); + ObfHelper target = ObfHelper.newMethod("func_110577_a", "bindTexture", + "net/minecraft/client/renderer/texture/TextureManager", + "(Lnet/minecraft/util/ResourceLocation;)V"); public method001(MethodVisitor mv) { @@ -183,7 +200,8 @@ public void visitMethodInsn(int opcode, String owner, String name, String desc, this.visitVarInsn(Opcodes.ASTORE, 32); this.visitVarInsn(Opcodes.ALOAD, 25); this.visitVarInsn(Opcodes.ALOAD, 32); - this.visitMethodInsn(Opcodes.INVOKESTATIC, "lain/mods/skins/init/forge/asm/Hooks", "GuiPlayerTabOverlay_bindTexture", "(Lcom/mojang/authlib/GameProfile;Lnet/minecraft/util/ResourceLocation;)Lnet/minecraft/util/ResourceLocation;", false); + this.visitMethodInsn(Opcodes.INVOKESTATIC, "lain/mods/skins/init/forge/asm/Hooks", "GuiPlayerTabOverlay_bindTexture", + "(Lcom/mojang/authlib/GameProfile;Lnet/minecraft/util/ResourceLocation;)Lnet/minecraft/util/ResourceLocation;", false); } super.visitMethodInsn(opcode, owner, name, desc, itf); } @@ -201,7 +219,9 @@ public void visitVarInsn(int opcode, int var) } - ObfHelper m001 = ObfHelper.newMethod("func_175249_a", "net/minecraft/client/gui/GuiPlayerTabOverlay", "(ILnet/minecraft/scoreboard/Scoreboard;Lnet/minecraft/scoreboard/ScoreObjective;)V").setDevName("renderPlayerlist"); + ObfHelper m001 = ObfHelper.newMethod("func_175249_a", "renderPlayerlist", + "net/minecraft/client/gui/GuiPlayerTabOverlay", + "(ILnet/minecraft/scoreboard/Scoreboard;Lnet/minecraft/scoreboard/ScoreObjective;)V"); public transformer003(ClassVisitor cv) { diff --git a/src/main/java/lain/mods/skins/init/forge/asm/Hooks.java b/src/main/java/lain/mods/skins/init/forge/asm/Hooks.java index a68140e..12ed269 100644 --- a/src/main/java/lain/mods/skins/init/forge/asm/Hooks.java +++ b/src/main/java/lain/mods/skins/init/forge/asm/Hooks.java @@ -6,6 +6,7 @@ import net.minecraft.util.ResourceLocation; import net.minecraftforge.fml.common.Loader; +@SuppressWarnings("unused") public class Hooks { diff --git a/src/main/java/lain/mods/skins/init/forge/asm/ObfHelper.java b/src/main/java/lain/mods/skins/init/forge/asm/ObfHelper.java index f2c62d6..1d07730 100644 --- a/src/main/java/lain/mods/skins/init/forge/asm/ObfHelper.java +++ b/src/main/java/lain/mods/skins/init/forge/asm/ObfHelper.java @@ -1,189 +1,30 @@ package lain.mods.skins.init.forge.asm; -import java.util.List; -import java.util.Map; import org.apache.commons.lang3.Validate; -import com.google.common.collect.ImmutableList; -public class ObfHelper -{ +public class ObfHelper { - public static ObfHelper newClass(String className) - { - Validate.notNull(className); - ObfHelper result = new ObfHelper(); - result.type = 2; - result.data[0] = className; - result.transform(!Plugin.runtimeDeobfuscationEnabled); - return result; - } - - public static ObfHelper newField(String fieldName, String ownerName, String descriptor) - { - Validate.notNull(fieldName); - Validate.notNull(ownerName); - Validate.notNull(descriptor); - ObfHelper result = new ObfHelper(); - result.type = 3; - result.data[0] = ownerName; - result.data[1] = fieldName; - result.data[2] = descriptor; - result.transform(!Plugin.runtimeDeobfuscationEnabled); - return result; - } - - public static ObfHelper newMethod(String methodName, String ownerName, String descriptor) - { + public static ObfHelper newMethod(String obfuscatedName, String methodName, String ownerName, String descriptor) { Validate.notNull(methodName); Validate.notNull(ownerName); Validate.notNull(descriptor); ObfHelper result = new ObfHelper(); - result.type = 4; result.data[0] = ownerName; result.data[1] = methodName; result.data[2] = descriptor; - result.transform(!Plugin.runtimeDeobfuscationEnabled); - return result; - } - - public static ObfHelper newPackage(String packageName) - { - Validate.notNull(packageName); - ObfHelper result = new ObfHelper(); - result.type = 1; - result.data[0] = packageName; - result.transform(!Plugin.runtimeDeobfuscationEnabled); return result; } - - private int type; private String[] data = new String[3]; - private ObfHelper() - { - } - - public String getData(int index) - { - if (index >= 0 && index <= 2) - return data[index]; - return null; - } - - public int getType() - { - return type; + private ObfHelper() { } - public boolean match(Object... obj) - { - switch (type) - { - case 1: - if (obj.length == 1) - return data[0].equals(obj[0]); - break; - case 2: - if (obj.length == 1) - return data[0].equals(obj[0]); - break; - case 3: - if (obj.length == 1) - return data[1].equals(obj[0]); - else if (obj.length == 2) - return data[0].equals(obj[0]) && data[1].equals(obj[1]); - break; - case 4: - if (obj.length == 2) - return data[1].equals(obj[0]) && data[2].equals(obj[1]); - else if (obj.length == 3) - return data[0].equals(obj[0]) && data[1].equals(obj[1]) && data[2].equals(obj[2]); - break; - } + public boolean match(Object... obj) { + if (obj.length == 2) + return data[1].equals(obj[0]) && data[2].equals(obj[1]); + else if (obj.length == 3) + return data[0].equals(obj[0]) && data[1].equals(obj[1]) && data[2].equals(obj[2]); return false; } - - public ObfHelper setDevName(String name) - { - return setDevName(name, Plugin.isDevelopmentEnvironment); - } - - private ObfHelper setDevName(String name, boolean devEnv) - { - if (devEnv) - { - switch (type) - { - case 1: - data[0] = name; - break; - case 2: - data[0] = name; - break; - case 3: - data[1] = name; - break; - case 4: - data[1] = name; - break; - } - } - return this; - } - - @Override - public String toString() - { - switch (type) - { - case 1: - return String.format("ObfHelper:Package{%s}", data[0]); - case 2: - return String.format("ObfHelper:Class{%s}", data[0]); - case 3: - return String.format("ObfHelper:Field{%s/%s %s}", data[0], data[1], data[2]); - case 4: - return String.format("ObfHelper:Method{%s/%s %s}", data[0], data[1], data[2]); - } - return String.format("ObfHelper:Unknown(%d){%s %s %s}", type, data[0], data[1], data[2]); - } - - private void transform(boolean deobfuscated) - { - switch (type) - { - case 1: - Map map1 = deobfuscated ? Setup.PackageMap : Setup.PackageMap.inverse(); - if (map1.containsKey(data[0])) - data[0] = map1.get(data[0]); - break; - case 2: - Map map2 = deobfuscated ? Setup.ClassMap : Setup.ClassMap.inverse(); - if (map2.containsKey(data[0])) - data[0] = map2.get(data[0]); - break; - case 3: - Map, List> map3 = deobfuscated ? Setup.FieldMap : Setup.FieldMap.inverse(); - List list3 = ImmutableList.of(data[0], data[1]); - if (map3.containsKey(list3)) - { - list3 = map3.get(list3); - data[0] = list3.get(0); - data[1] = list3.get(1); - } - break; - case 4: - Map, List> map4 = deobfuscated ? Setup.MethodMap : Setup.MethodMap.inverse(); - List list4 = ImmutableList.of(data[0], data[1], data[2]); - if (map4.containsKey(list4)) - { - list4 = map4.get(list4); - data[0] = list4.get(0); - data[1] = list4.get(1); - data[2] = list4.get(2); - } - break; - } - } - } + diff --git a/src/main/java/lain/mods/skins/init/forge/asm/Plugin.java b/src/main/java/lain/mods/skins/init/forge/asm/Plugin.java index d56e215..78d310a 100644 --- a/src/main/java/lain/mods/skins/init/forge/asm/Plugin.java +++ b/src/main/java/lain/mods/skins/init/forge/asm/Plugin.java @@ -10,7 +10,6 @@ public class Plugin implements IFMLLoadingPlugin { public static boolean runtimeDeobfuscationEnabled = false; - public static boolean isDevelopmentEnvironment = false; @Override public String getAccessTransformerClass() @@ -33,14 +32,10 @@ public String getModContainerClass() @Override public String getSetupClass() { - return "lain.mods.skins.init.forge.asm.Setup"; + return null; } @Override - public void injectData(Map data) - { - runtimeDeobfuscationEnabled = (Boolean) data.get("runtimeDeobfuscationEnabled"); - isDevelopmentEnvironment = (getClass().getResource("/binpatches.pack.lzma") == null); - } + public void injectData(Map data) {} } diff --git a/src/main/java/lain/mods/skins/init/forge/asm/Setup.java b/src/main/java/lain/mods/skins/init/forge/asm/Setup.java deleted file mode 100644 index 3063d02..0000000 --- a/src/main/java/lain/mods/skins/init/forge/asm/Setup.java +++ /dev/null @@ -1,121 +0,0 @@ -package lain.mods.skins.init.forge.asm; - -import java.io.IOException; -import java.io.InputStream; -import java.util.List; -import java.util.Map; -import com.google.common.base.Charsets; -import com.google.common.collect.BiMap; -import com.google.common.collect.HashBiMap; -import com.google.common.collect.ImmutableBiMap; -import com.google.common.collect.ImmutableList; -import com.google.common.io.ByteSource; -import com.google.common.io.LineProcessor; -import LZMA.LzmaInputStream; -import net.minecraftforge.fml.relauncher.IFMLCallHook; - -public class Setup implements IFMLCallHook -{ - - public static BiMap PackageMap; - public static BiMap ClassMap; - public static BiMap, List> FieldMap; - public static BiMap, List> MethodMap; - - private String deobfuscationFileName; - - @Override - public Void call() throws Exception - { - try - { - PackageMap = HashBiMap.create(); - ClassMap = HashBiMap.create(); - FieldMap = HashBiMap.create(); - MethodMap = HashBiMap.create(); - new ByteSource() - { - - @Override - public InputStream openStream() throws IOException - { - return new LzmaInputStream(Setup.class.getResourceAsStream(deobfuscationFileName)); - } - - }.asCharSource(Charsets.UTF_8).readLines(new LineProcessor() - { - - @Override - public Void getResult() - { - return null; - } - - @Override - public boolean processLine(String line) throws IOException - { - if (line.startsWith("PK: ")) - { - line = line.substring(4); - String[] data = line.split(" "); - if (data.length == 2) - PackageMap.put(data[0], data[1]); - } - else if (line.startsWith("CL: ")) - { - line = line.substring(4); - String[] data = line.split(" "); - if (data.length == 2) - ClassMap.put(data[0], data[1]); - } - else if (line.startsWith("FD: ")) - { - line = line.substring(4); - String[] data = line.split(" "); - if (data.length == 2) - { - int i0 = data[0].lastIndexOf("/"); - int i1 = data[1].lastIndexOf("/"); - if (i0 != -1 && i1 != -1) - FieldMap.put(ImmutableList.of(data[0].substring(0, i0), data[0].substring(i0 + 1, data[0].length())), ImmutableList.of(data[1].substring(0, i1), data[1].substring(i1 + 1, data[1].length()))); - } - } - else if (line.startsWith("MD: ")) - { - line = line.substring(4); - String[] data = line.split(" "); - if (data.length == 4) - { - int i0 = data[0].lastIndexOf("/"); - int i2 = data[2].lastIndexOf("/"); - if (i0 != -1 && i2 != -1) - MethodMap.put(ImmutableList.of(data[0].substring(0, i0), data[0].substring(i0 + 1, data[0].length()), data[1]), ImmutableList.of(data[2].substring(0, i2), data[2].substring(i2 + 1, data[2].length()), data[3])); - } - } - return true; - } - - }); - PackageMap = ImmutableBiMap.copyOf(PackageMap); - ClassMap = ImmutableBiMap.copyOf(ClassMap); - FieldMap = ImmutableBiMap.copyOf(FieldMap); - MethodMap = ImmutableBiMap.copyOf(MethodMap); - } - catch (Exception e) - { - PackageMap = ImmutableBiMap.of(); - ClassMap = ImmutableBiMap.of(); - FieldMap = ImmutableBiMap.of(); - MethodMap = ImmutableBiMap.of(); - throw e; - } - return null; - } - - @Override - public void injectData(Map data) - { - deobfuscationFileName = (String) data.get("deobfuscationFileName"); - } - -}