From 304d09d3b3622671f44260aa5354240f722c8a8c Mon Sep 17 00:00:00 2001 From: Lassebq Date: Sun, 5 Nov 2023 21:28:38 +0200 Subject: [PATCH] BTA and Indev tests Fix BTA throwing exception when attempting to apply LWJGL frame patch --- .../launchwrapper/tweak/LegacyTweak.java | 59 +++++++++++-------- .../launchwrapper/test/BTATest.java | 30 ++++++++++ .../launchwrapper/test/IndevTest.java | 35 +++++++++++ .../launchwrapper/test/InfdevTest.java | 2 +- 4 files changed, 101 insertions(+), 25 deletions(-) create mode 100644 src/test/java/org/mcphackers/launchwrapper/test/BTATest.java create mode 100644 src/test/java/org/mcphackers/launchwrapper/test/IndevTest.java diff --git a/src/main/java/org/mcphackers/launchwrapper/tweak/LegacyTweak.java b/src/main/java/org/mcphackers/launchwrapper/tweak/LegacyTweak.java index 4f4d0ef..90e4199 100644 --- a/src/main/java/org/mcphackers/launchwrapper/tweak/LegacyTweak.java +++ b/src/main/java/org/mcphackers/launchwrapper/tweak/LegacyTweak.java @@ -50,7 +50,7 @@ public class LegacyTweak extends Tweak { }; public static final String[] MAIN_APPLETS = { "net/minecraft/client/MinecraftApplet", - "com/mojang/minecraft/MinecraftApplet" + "com/mojang/minecraft/MinecraftApplet" }; public static final boolean EXPERIMENTAL_INDEV_SAVING = true; @@ -109,6 +109,7 @@ public class LegacyTweak extends Tweak { if(main == null) { minecraft.methods.add(main = getMain()); } else { + // Try to patch main instead of replacing. Issues with BTA minecraft.methods.remove(main); minecraft.methods.add(main = getMain()); } @@ -376,7 +377,7 @@ public class LegacyTweak extends Tweak { insn1 = nextInsn(insn1); } } - + private void bitDepthFix(MethodNode init) { for(TryCatchBlockNode tryCatch : init.tryCatchBlocks) { if(!"org/lwjgl/LWJGLException".equals(tryCatch.type)) { @@ -1204,7 +1205,12 @@ public class LegacyTweak extends Tweak { insns.add(new VarInsnNode(ALOAD, appletIndex)); insns.add(new FieldInsnNode(GETFIELD, minecraftApplet.name, mcField, mcDesc)); } else { - insns.add(getNewMinecraftImpl(minecraft, null)); + InsnList constructor = getNewMinecraftImpl(minecraft, null); + if(constructor != null) { + insns.add(constructor); + } else { + throw new IllegalStateException("Unexpected constructor!"); + } } insns.add(new VarInsnNode(ASTORE, mcIndex)); if(width != null && height != null) { @@ -1286,27 +1292,30 @@ public class LegacyTweak extends Tweak { AbstractInsnNode[] insns2 = fill(insn, 6); if(compareInsn(insns2[1], PUTFIELD, minecraftApplet.name, mcField, mcDesc) && compareInsn(insns2[0], INVOKESPECIAL, null, "")) { - InsnList insns = new InsnList(); - insns.add(new VarInsnNode(ALOAD, 0)); - insns.add(new FieldInsnNode(GETFIELD, minecraftApplet.name, canvasField, "Ljava/awt/Canvas;")); - insns.add(new TypeInsnNode(NEW, "java/awt/Dimension")); - insns.add(new InsnNode(DUP)); - insns.add(intInsn(launch.width.get())); - insns.add(intInsn(launch.height.get())); - insns.add(new MethodInsnNode(INVOKESPECIAL, "java/awt/Dimension", "", "(II)V")); - insns.add(new MethodInsnNode(INVOKEVIRTUAL, "java/awt/Canvas", "setPreferredSize", "(Ljava/awt/Dimension;)V")); - init.instructions.insert(insns2[1], insns); MethodInsnNode invoke = (MethodInsnNode) insns2[0]; - init.instructions.insertBefore(insns2[1], getNewMinecraftImpl(source.getClass(invoke.owner), canvasField)); - IdentifyCall call = new IdentifyCall(invoke); - AbstractInsnNode newInsn = call.getArgument(0)[0].getPrevious(); - if(newInsn.getOpcode() == NEW) { - init.instructions.remove(newInsn); + InsnList constructor = getNewMinecraftImpl(source.getClass(invoke.owner), canvasField); + if(constructor != null) { + InsnList insns = new InsnList(); + insns.add(new VarInsnNode(ALOAD, 0)); + insns.add(new FieldInsnNode(GETFIELD, minecraftApplet.name, canvasField, "Ljava/awt/Canvas;")); + insns.add(new TypeInsnNode(NEW, "java/awt/Dimension")); + insns.add(new InsnNode(DUP)); + insns.add(intInsn(launch.width.get())); + insns.add(intInsn(launch.height.get())); + insns.add(new MethodInsnNode(INVOKESPECIAL, "java/awt/Dimension", "", "(II)V")); + insns.add(new MethodInsnNode(INVOKEVIRTUAL, "java/awt/Canvas", "setPreferredSize", "(Ljava/awt/Dimension;)V")); + init.instructions.insert(insns2[1], insns); + init.instructions.insertBefore(insns2[1], constructor); + IdentifyCall call = new IdentifyCall(invoke); + AbstractInsnNode newInsn = call.getArgument(0)[0].getPrevious(); + if(newInsn.getOpcode() == NEW) { + init.instructions.remove(newInsn); + } + for(AbstractInsnNode[] arg : call.getArguments()) { + remove(init.instructions, arg); + } + init.instructions.remove(invoke); } - for(AbstractInsnNode[] arg : call.getArguments()) { - remove(init.instructions, arg); - } - init.instructions.remove(invoke); insn = insns2[1]; } if(compareInsn(insns2[0], ALOAD, 0) @@ -1437,13 +1446,15 @@ public class LegacyTweak extends Tweak { } else if(i == 1) { insns.add(intInsn(launch.height.get())); } else { - throw new IllegalStateException("Unexpected constructor: " + init.desc); + return null; + // throw new IllegalStateException("Unexpected constructor: " + init.desc); } i++; } else if(desc.equals("Z")) { insns.add(booleanInsn(launch.fullscreen.get())); } else { - throw new IllegalStateException("Unexpected constructor: " + init.desc); + return null; + // throw new IllegalStateException("Unexpected constructor: " + init.desc); } } insns.add(new MethodInsnNode(INVOKESPECIAL, minecraftImpl.name, "", init.desc)); diff --git a/src/test/java/org/mcphackers/launchwrapper/test/BTATest.java b/src/test/java/org/mcphackers/launchwrapper/test/BTATest.java new file mode 100644 index 0000000..6fe8d7a --- /dev/null +++ b/src/test/java/org/mcphackers/launchwrapper/test/BTATest.java @@ -0,0 +1,30 @@ +package org.mcphackers.launchwrapper.test; + +import org.mcphackers.launchwrapper.LaunchConfig; +import org.mcphackers.launchwrapper.tweak.LegacyTweak; +import org.mcphackers.launchwrapper.tweak.Tweak; +import org.mcphackers.launchwrapper.util.ClassNodeSource; + +public class BTATest extends TweakTest { + + @Override + protected String jarName() { + return "bta.jar"; + } + + @Override + protected String jarUrl() { + return "https://github.com/Better-than-Adventure/bta-download-repo/releases/download/v1.7.7.0_02/bta.jar"; + } + + @Override + public Tweak getTweak(ClassNodeSource source, LaunchConfig config) { + return new LegacyTweak(source, config); + } + + @Override + public TestFeatureBuilder getTests() { + return new TestFeatureBuilder(); + } + +} diff --git a/src/test/java/org/mcphackers/launchwrapper/test/IndevTest.java b/src/test/java/org/mcphackers/launchwrapper/test/IndevTest.java new file mode 100644 index 0000000..c1e35e5 --- /dev/null +++ b/src/test/java/org/mcphackers/launchwrapper/test/IndevTest.java @@ -0,0 +1,35 @@ +package org.mcphackers.launchwrapper.test; + +import org.mcphackers.launchwrapper.LaunchConfig; +import org.mcphackers.launchwrapper.tweak.LegacyTweak; +import org.mcphackers.launchwrapper.tweak.Tweak; +import org.mcphackers.launchwrapper.util.ClassNodeSource; + +public class IndevTest extends TweakTest { + + @Override + protected String jarName() { + return "in-20100223.jar"; + } + + @Override + protected String jarUrl() { + return "http://files.betacraft.uk/launcher/assets/versions/in-20100223.jar"; + } + + @Override + public Tweak getTweak(ClassNodeSource source, LaunchConfig config) { + return new LegacyTweak(source, config); + } + + @Override + public TestFeatureBuilder getTests() { + return new TestFeatureBuilder() + .tweakInfoList("Indev launch", "Indev save patch", "Set default width and height", + "Fullscreen init patch", "Replaced icon", "Replaced fullscreen", "Replaced width", + "Replaced height", "Shutdown patch", "Options load fix", + "Replaced title", "Replaced gameDir", "Removed canvas null check", + "Set fullscreen", "Added main"); + } + +} diff --git a/src/test/java/org/mcphackers/launchwrapper/test/InfdevTest.java b/src/test/java/org/mcphackers/launchwrapper/test/InfdevTest.java index a446c45..1913188 100644 --- a/src/test/java/org/mcphackers/launchwrapper/test/InfdevTest.java +++ b/src/test/java/org/mcphackers/launchwrapper/test/InfdevTest.java @@ -30,6 +30,6 @@ public class InfdevTest extends TweakTest { "Replaced height", "Shutdown patch", "SoundManager shutdown", "Options load fix", "Replaced canvas getWidth", "Replaced canvas getHeight", "Replaced title", "Replaced gameDir", "Removed canvas null check", "Set fullscreen", - "Set default width and height", "Added main"); + "Added main"); } }