Hack modlauncher a little to load

This commit is contained in:
IzzelAliz 2021-12-03 22:03:13 +08:00
parent 7ed1b5d245
commit 754e668bbd
No known key found for this signature in database
GPG Key ID: EE50E123A11D8338

View File

@ -5,6 +5,11 @@ import io.izzel.arclight.api.Unsafe;
import io.izzel.arclight.boot.AbstractBootstrap;
import io.izzel.arclight.i18n.ArclightConfig;
import io.izzel.arclight.i18n.ArclightLocale;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import java.util.ServiceLoader;
import java.util.function.Consumer;
@ -36,6 +41,7 @@ public class ApplicationBootstrap extends AbstractBootstrap implements Consumer<
try {
this.setupMod();
this.dirtyHacks();
this.hackModlauncher();
ServiceLoader.load(getClass().getModule().getLayer(), Consumer.class).stream()
.filter(it -> !it.type().getName().contains("arclight"))
.findFirst().orElseThrow().get().accept(args);
@ -44,4 +50,31 @@ public class ApplicationBootstrap extends AbstractBootstrap implements Consumer<
System.err.println("Fail to launch Arclight.");
}
}
private void hackModlauncher() throws Exception {
try (var in = getClass().getClassLoader().getResourceAsStream("cpw/mods/modlauncher/TransformerClassWriter$SuperCollectingVisitor.class")) {
var cw = new ClassWriter(0);
var cr = new ClassReader(in);
cr.accept(new ClassVisitor(Opcodes.ASM9, cw) {
@Override
public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
var mv = super.visitMethod(access, name, descriptor, signature, exceptions);
if (name.equals("<init>")) {
return new MethodVisitor(Opcodes.ASM9, mv) {
@Override
public void visitLdcInsn(Object value) {
if (value.equals(Opcodes.ASM7)) {
super.visitLdcInsn(Opcodes.ASM9);
} else {
super.visitLdcInsn(value);
}
}
};
} else return mv;
}
}, 0);
var bytes = cw.toByteArray();
Unsafe.defineClass(cr.getClassName(), bytes, 0, bytes.length, getClass().getClassLoader(), getClass().getProtectionDomain());
}
}
}