way more reliable DragonAPI patch

This commit is contained in:
FalsePattern committed 2022-07-03 19:17:16 +02:00
1 parent 76a9345665
commit 6e66066f65
8 files changed
+203 -81

No files matched your search

@@ -0,0 +1,48 @@
package com.falsepattern.endlessids.asm;
import Reika.DragonAPI.Libraries.Java.ReikaJavaLibrary;
import com.falsepattern.endlessids.asm.stubpackage.Reika.DragonAPI.ModList;
import lombok.SneakyThrows;
import lombok.val;
import cpw.mods.fml.common.Loader;
public class ASMHooks {
@SneakyThrows
public static void DragonAPIModListConstructorTweak(ModList list) {
list.successful = false;
DragonAPIModListTryLoad(list);
}
public static boolean DragonAPIModListTryLoad(ModList list) {
if (!list.successful) {
try {
list.condition = Loader.isModLoaded(list.modLabel);
list.successful = true;
if (list.condition) {
ReikaJavaLibrary.pConsole(
"DRAGONAPI: " + list + " detected in the MC installation. Adjusting behavior accordingly.");
} else {
ReikaJavaLibrary.pConsole(
"DRAGONAPI: " + list + " not detected in the MC installation. No special action taken.");
}
if (list.condition) {
ReikaJavaLibrary.pConsole("DRAGONAPI: Attempting to load data from " + list);
if (list.blockClasses == null) {
ReikaJavaLibrary.pConsole("DRAGONAPI: Could not block class for " + list +
": Specified class was null. This may not be an error.");
}
if (list.itemClasses == null) {
ReikaJavaLibrary.pConsole("DRAGONAPI: Could not item class for " + list +
": Specified class was null. This may not be an error.");
}
}
} catch (NullPointerException e) {
list.condition = list.successful = false;
}
}
return list.condition;
}
public static void DragonAPIModListSuppressLogging(Object obj) {}
}
@@ -1,5 +1,6 @@
package com.falsepattern.endlessids.asm;
import com.falsepattern.endlessids.asm.transformer.DragonAPIModList;
import com.falsepattern.endlessids.asm.transformer.FmlRegistry;
import java.util.HashMap;
@@ -8,6 +9,7 @@ import java.util.Map;
public enum ClassEdit {
FmlRegistry(new FmlRegistry(), new String[]{"cpw.mods.fml.common.registry.GameData",
"cpw.mods.fml.common.registry.FMLControlledNamespacedRegistry"}),
DragonAPIModList(new DragonAPIModList(), new String[]{"Reika.DragonAPI.ModList"})
;
private static final Map<String, ClassEdit> editMap;
@@ -0,0 +1,9 @@
package com.falsepattern.endlessids.asm.stubpackage.Reika.DragonAPI;
public class ModList {
public String modLabel;
public boolean condition;
public boolean successful;
public String[] itemClasses;
public String[] blockClasses;
}
@@ -0,0 +1,141 @@
package com.falsepattern.endlessids.asm.transformer;
import com.falsepattern.endlessids.asm.ASMHooks;
import com.falsepattern.endlessids.asm.IClassNodeTransformer;
import com.falsepattern.endlessids.mixin.plugin.MixinPlugin;
import lombok.val;
import lombok.var;
import org.objectweb.asm.Label;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.FieldInsnNode;
import org.objectweb.asm.tree.FieldNode;
import org.objectweb.asm.tree.FrameNode;
import org.objectweb.asm.tree.InsnNode;
import org.objectweb.asm.tree.LabelNode;
import org.objectweb.asm.tree.LineNumberNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.MethodNode;
import org.objectweb.asm.tree.VarInsnNode;
import java.util.Arrays;
import java.util.List;
public class DragonAPIModList implements IClassNodeTransformer {
private static final List<String> fields = Arrays.asList("condition", "itemClasses", "blockClasses");
private void crash() {
throw new IllegalStateException("Tweaking failed!");
}
@Override
public void transform(final ClassNode cn, final boolean obfuscated) {
int state = 0;
for (val field: cn.fields) {
if (field.name.equals("__MARKER__FALSEPATTERN__MIXINPATCH__")) {
return;
}
}
for (val field: cn.fields) {
if (field.name.equals("condition") && field.desc.equals("Z")) {
field.access &= ~Opcodes.ACC_FINAL;
state++;
}
if (fields.contains(field.name)) {
field.access &= ~Opcodes.ACC_PRIVATE;
field.access &= ~Opcodes.ACC_PROTECTED;
field.access |= Opcodes.ACC_PUBLIC;
state++;
}
}
if (state != 4) {
crash();
}
cn.fields.add(new FieldNode(Opcodes.ACC_PUBLIC, "successful", "Z", null, 0));
cn.fields.add(new FieldNode(Opcodes.ACC_PRIVATE, "__MARKER__FALSEPATTERN__MIXINPATCH__", "Z", null, 0));
int state2 = 0;
for (val method : cn.methods) {
if ((state2 & 1) == 0 &&
method.name.equals("<init>") &&
method.desc.equals("(Ljava/lang/String;ILjava/lang/String;[Ljava/lang/String;[Ljava/lang/String;)V")) {
tweakInit(method);
state2 |= 1;
}
if ((state2 & 2) == 0 &&
method.name.equals("isLoaded") &&
method.desc.equals("()Z")) {
tweakIsLoaded(method);
state2 |= 2;
}
}
if (state2 != 3) {
crash();
}
}
private static void tweakIsLoaded(MethodNode method) {
val iterator = method.instructions.iterator();
iterator.next();
iterator.next();
iterator.next();
iterator.next();
iterator.set(new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(ASMHooks.class), "DragonAPIModListTryLoad", "(LReika/DragonAPI/ModList;)Z", false));
}
private static void tweakInit(MethodNode method) {
val iterator = method.instructions.iterator();
while (iterator.hasNext()) {
var insn = iterator.next();
if (insn instanceof LineNumberNode && ((LineNumberNode)insn).line == 154) {
//SRC:
// L3
// LINENUMBER 154 L3
// ALOAD 0
// ALOAD 0
// GETFIELD Reika/DragonAPI/ModList.modLabel : Ljava/lang/String;
// INVOKESTATIC cpw/mods/fml/common/Loader.isModLoaded (Ljava/lang/String;)Z
// PUTFIELD Reika/DragonAPI/ModList.condition : Z
//TGT:
// L3
// LINENUMBER 154 L3
// ALOAD 0
// ALOAD 5
// PUTFIELD Reika/DragonAPI/ModList.itemClasses : [Ljava/lang/String;
// ALOAD 0
// ALOAD 4
// PUTFIELD Reika/DragonAPI/ModList.blockClasses : [Ljava/lang/String;
// ALOAD 0
// INVOKESTATIC ASMHooks.isModLoaded (LReika/DragonAPI/ModList;)V
// FRAME FULL [Reika/DragonAPI/ModList java/lang/String I java/lang/String [Ljava/lang/String; [Ljava/lang/String;] []
// RETURN
iterator.add(new VarInsnNode(Opcodes.ALOAD, 0));
iterator.add(new VarInsnNode(Opcodes.ALOAD, 5));
iterator.add(new FieldInsnNode(Opcodes.PUTFIELD,
"Reika/DragonAPI/ModList",
"itemClasses",
"[Ljava/lang/String;"));
iterator.add(new VarInsnNode(Opcodes.ALOAD, 0));
iterator.add(new VarInsnNode(Opcodes.ALOAD, 4));
iterator.add(new FieldInsnNode(Opcodes.PUTFIELD,
"Reika/DragonAPI/ModList",
"blockClasses",
"[Ljava/lang/String;"));
iterator.next();
iterator.remove();
iterator.next();
iterator.next();
iterator.remove();
var methodInsn = (MethodInsnNode) iterator.next();
methodInsn.owner = Type.getInternalName(ASMHooks.class);
methodInsn.name = "DragonAPIModListConstructorTweak";
methodInsn.desc = "(LReika/DragonAPI/ModList;)V";
iterator.add(new FrameNode(Opcodes.F_FULL, 6, new Object[] {"Reika/DragonAPI/ModList", "java/lang/String", Opcodes.INTEGER, "java/lang/String", "[Ljava/lang/String;", "[Ljava/lang/String;"}, 0, new Object[] {}));
iterator.add(new InsnNode(Opcodes.RETURN));
iterator.add(new FrameNode(Opcodes.F_FULL, 6, new Object[] {"Reika/DragonAPI/ModList", "java/lang/String", Opcodes.INTEGER, "java/lang/String", "[Ljava/lang/String;", "[Ljava/lang/String;"}, 0, new Object[] {}));
iterator.next();
iterator.remove();
break;
}
}
}
}
@@ -12,7 +12,7 @@ import net.minecraft.world.gen.layer.GenLayerRiverMix;
public abstract class GenLayerRiverMixMixin {
@ModifyConstant(method = "getInts",
constant = @Constant(intValue = VanillaConstants.biomeIDMask),
expect = 0)
expect = -1)
private int extendTheID(int constant) {
return ExtendedConstants.biomeIDMask;
}
@@ -1,6 +1,5 @@
package com.falsepattern.endlessids.mixin.plugin;
import com.falsepattern.endlessids.mixin.util.DragonAPICompat;
import com.falsepattern.lib.mixin.ITargetedMod;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
@@ -16,10 +15,7 @@ public enum TargetedMod implements ITargetedMod {
ATG("Alternate Terrain Generation", true, startsWith("atg")),
BOP("Biomes O' Plenty", true, startsWith("biomesoplenty-")),
COFHLIB("CoFH Lib", true, startsWith("cofhlib-")),
DRAGONAPI("DragonAPI", false, startsWith("dragonapi").and((x) -> {
DragonAPICompat.activate();
return true;
})),
DRAGONAPI("DragonAPI", false, startsWith("dragonapi")),
GALACTICRAFTCORE("GalactiCraftCore", false, startsWith("galacticraftcore")),
MFQM("More Fun Quicksand Mod", false, startsWith("morefunquicksandmod-")),
UBC("Underground Biomes Constructs", false, startsWith("undergroundbiomesconstructs-")),
@@ -1,74 +0,0 @@
package com.falsepattern.endlessids.mixin.util;
import com.falsepattern.endlessids.mixin.plugin.MixinPlugin;
import lombok.SneakyThrows;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import cpw.mods.fml.common.Loader;
import java.lang.reflect.Field;
import java.util.Collections;
public class DragonAPICompat {
private static boolean activated = false;
public static void activate() {
if (activated) return;
activated = true;
Logger LOG = LogManager.getLogger(MixinPlugin.LOG.getName() + " DragonAPI Compat Patcher");
LOG.info("DragonAPI detected. Activating very early compatibility patches.");
int total = 1;
int complete = 0;
try {
patchFML(LOG);
complete++;
} catch (Throwable t) {
LOG.fatal("Failed to load early DragonAPI compat patches! Crash imminent! Successful: " + complete + ", Total: " + total, t);
}
LOG.info("DragonAPI early compat patching finished! Resuming normal loading.");
}
@SneakyThrows
private static void patchFML(Logger baseLog) {
Logger LOG = LogManager.getLogger(baseLog.getName() + " FML Tweaker");
LOG.info("Patching FML loader class...");
try {
LOG.info("Attempting to get a reference to " + Loader.class.getName() +
".namedMods with reflection...");
Field f;
try {
f = Loader.class.getDeclaredField("namedMods");
LOG.info("[OK] Reference retrieved!");
} catch (Throwable t) {
LOG.error("[FAIL] Failed to retrieve the field!");
throw t;
}
LOG.info("[INFO] Making the reference accessible...");
try {
f.setAccessible(true);
LOG.info("[OK] Reference is now accessible!");
} catch (Throwable t) {
LOG.error("[FAIL] Failed to make the reference accessible!");
throw t;
}
LOG.info("[INFO] Inserting placeholder Collections.emptyMap() object into the field...");
try {
if (f.get(Loader.instance()) != null) {
LOG.info("[OK] Field already had a value, not replacing.");
} else {
f.set(Loader.instance(), Collections.emptyMap());
LOG.info("[OK] Successfully inserted placeholder object!");
}
} catch (Throwable t) {
LOG.error("[FAIL] Failed to insert placeholder object!");
throw t;
}
LOG.info("FML Loader class patched successfully!");
} catch (Throwable t) {
LOG.error("Failed to patch FML Loader class! Check the log file for debug info!");
throw t;
}
}
}