Skip to content

Commit

Permalink
Fix Forge to support asm 9.7
Browse files Browse the repository at this point in the history
  • Loading branch information
Lassebq committed Oct 30, 2024
1 parent 05b08d7 commit 9e5124a
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 52 deletions.
11 changes: 8 additions & 3 deletions src/main/java/net/minecraft/launchwrapper/Launch.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.mcphackers.launchwrapper.target.LaunchTarget;
import org.mcphackers.launchwrapper.target.MainLaunchTarget;
import org.mcphackers.launchwrapper.tweak.Tweak;
import org.mcphackers.launchwrapper.tweak.injection.forge.ForgeFix;

public final class Launch extends org.mcphackers.launchwrapper.Launch {
public static File minecraftHome;
Expand Down Expand Up @@ -55,8 +56,9 @@ public void launch() {
blackboard.put("ArgumentList", new ArrayList<String>());
minecraftHome = config.gameDir.get();
assetsDir = config.assetsDir.get();
runTweakers();
LaunchClassLoader loader = getLoader();
runPreInitTweaks(loader);
runTweakers(loader);
Tweak mainTweak = getTweak();
if (mainTweak == null) {
if (config.tweakClass.get() == null) {
Expand Down Expand Up @@ -103,9 +105,12 @@ private List<String> getArgs() {
return args;
}

private void runPreInitTweaks(LaunchClassLoader loader) {
new ForgeFix().apply(loader, config);
}

@SuppressWarnings("unchecked")
private void runTweakers() {
LaunchClassLoader loader = getLoader();
private void runTweakers(LaunchClassLoader loader) {
List<String> args = getArgs();
List<String> tweakClassNames = (List<String>)blackboard.get("TweakClasses");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ public void invokeMain(String launchTarget, String... args) {
} catch (InvocationTargetException e1) {
if (e1.getCause() != null) {
e1.getCause().printStackTrace();
} else {
e1.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,26 @@ public static byte[] loadLevel(File levelsDir, int index) throws IOException {
* Used in level save screen
*/
public static void saveLevel(File levelsDir, int index, String levelName, byte[] data) throws IOException {
if (!levelsDir.exists()) {
levelsDir.mkdirs();
}
if (!levelsDir.isDirectory()) {
throw new IOException(levelsDir + " is not a directory");
}
String[] lvlNames = getLevelNames(levelsDir);
lvlNames[index] = levelName;
File level = new File(levelsDir, "level" + index + ".dat");
if (!levelsDir.exists()) {
levelsDir.mkdirs();
}

// Since minecraft doesn't have a delete button on levels, just save a new one with this name and it'll get deleted
if (levelName.equals("---") /* Can't be "-" because save button is active at 3>= characters */) {
level.delete();
if (level.exists()) {
level.delete();
}
lvlNames[index] = EMPTY_LEVEL;
} else {
if (!levelsDir.exists()) {
levelsDir.mkdirs();
}
OutputStream fileOutput = new FileOutputStream(level);
try {
fileOutput.write(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* Compatibility fixes for Java 5.
* Replaces most of Java 6 API used by Minecraft with Java 5 alternative
*/
public class Java5LazyTweaker implements Tweaker {
public class Java5Tweaker implements Tweaker {

public boolean tweakClass(ClassNodeSource source, String name) {
ClassNode node = source.getClass(name);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.mcphackers.launchwrapper.tweak;

import java.util.ArrayList;
import java.util.List;

import org.mcphackers.launchwrapper.LaunchConfig;
import org.mcphackers.launchwrapper.target.LaunchTarget;
import org.mcphackers.launchwrapper.tweak.injection.Injection;
import org.mcphackers.launchwrapper.tweak.injection.forge.ForgeFix;

public class LegacyLauncherTweak extends Tweak {

protected Tweak baseTweak;

public LegacyLauncherTweak(LaunchConfig config, Tweak tweak) {
super(config);
baseTweak = tweak;
}

@Override
public List<Injection> getInjections() {
List<Injection> injections = new ArrayList<Injection>();
injections.addAll(baseTweak.getInjections());
injections.add(new ForgeFix());
return injections;
}

@Override
public List<Tweaker> getTweakers() {
return baseTweak.getTweakers();
}

@Override
public LaunchTarget getLaunchTarget() {
return baseTweak.getLaunchTarget();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public List<Injection> getInjections() {

@Override
public List<Tweaker> getTweakers() {
return Collections.<Tweaker>singletonList(new Java5LazyTweaker());
return Collections.<Tweaker>singletonList(new Java5Tweaker());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public List<Injection> getInjections() {

@Override
public List<Tweaker> getTweakers() {
return Collections.<Tweaker>singletonList(new Java5LazyTweaker());
return Collections.<Tweaker>singletonList(new Java5Tweaker());
}

@Override
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.mcphackers.launchwrapper.tweak.injection.forge;

import static org.mcphackers.launchwrapper.util.asm.InsnHelper.*;
import static org.objectweb.asm.Opcodes.*;

import org.mcphackers.launchwrapper.LaunchConfig;
import org.mcphackers.launchwrapper.tweak.injection.Injection;
import org.mcphackers.launchwrapper.util.ClassNodeSource;
import org.mcphackers.launchwrapper.util.asm.NodeHelper;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.MethodNode;

public class ForgeFix implements Injection {

public String name() {
return "Forge patch";
}

public boolean required() {
return false;
}

public boolean apply(ClassNodeSource source, LaunchConfig config) {
ClassNode eventSubscriptionTransformer = source.getClass("net/minecraftforge/fml/common/asm/transformers/EventSubscriptionTransformer");
if (eventSubscriptionTransformer == null) {
return false;
}
MethodNode buildEvents = NodeHelper.getMethod(eventSubscriptionTransformer, "buildEvents", "(Lorg/objectweb/asm/tree/ClassNode;)Z");
if (buildEvents == null) {
return false;
}
for (AbstractInsnNode insn = getFirst(buildEvents.instructions); insn != null; insn = nextInsn(insn)) {
if (compareInsn(insn, GETFIELD, "org/objectweb/asm/tree/ClassNode", "superName", "Ljava/lang/String;") &&
compareInsn(nextInsn(insn), INVOKESTATIC, "org/objectweb/asm/Type", "getType", "(Ljava/lang/String;)Lorg/objectweb/asm/Type;")) {
// Forge incorrectly uses getType here. getObjectType makes it compatible with asm 9.7+
buildEvents.instructions.set(nextInsn(insn), new MethodInsnNode(INVOKESTATIC, "org/objectweb/asm/Type", "getObjectType", "(Ljava/lang/String;)Lorg/objectweb/asm/Type;"));
break;
}
}
source.overrideClass(eventSubscriptionTransformer);
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ public Tweak getTweak(LaunchConfig config) {
@Override
public TestFeatureBuilder getTests() {
return new TestFeatureBuilder()
.tweakInfoList("LegacyTweak init", "Fix Shutdown", "Indev save patch", "LWJGL Patch", "Replace game directory", "Options load fix", "Add main");
.tweakInfoList("LegacyTweak init", "Classic crash screen", "Fix Shutdown", "Indev save patch", "LWJGL Patch", "Replace game directory", "Options load fix", "Add main");
}
}

0 comments on commit 9e5124a

Please sign in to comment.