Forge 1.12.2 核心改装:java.lang.StringIndexOutOfBoundsException
Forge 1.12.2 Coremodding: java.lang.StringIndexOutOfBoundsException
我尝试在 1.12.2 Forge 上制作一个核心mod 以修补失落之城 mod 中一些丢失的东西。 (来源:https://github.com/McJtyMods/LostCities/blob/1.12/src/main/java/mcjty/lostcities/dimensions/world/lost/BuildingInfo.java)
我和一个朋友写了这个LostCitiesClassTransformer.java:(完整来源:https://github.com/Nick1st/LCPatches)
package seemdmax.lcpatches;
import static org.objectweb.asm.Opcodes.ALOAD;
import static org.objectweb.asm.Opcodes.IFGT;
import static org.objectweb.asm.Opcodes.IFLE;
import static org.objectweb.asm.Opcodes.IFNE;
import static org.objectweb.asm.Opcodes.ILOAD;
import static org.objectweb.asm.Opcodes.INVOKEINTERFACE;
import static org.objectweb.asm.Opcodes.INVOKESTATIC;
import static org.objectweb.asm.Opcodes.INVOKEVIRTUAL;
import static org.objectweb.asm.Opcodes.GETFIELD;
import java.util.Arrays;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.InsnList;
import org.objectweb.asm.tree.JumpInsnNode;
import org.objectweb.asm.tree.LabelNode;
import org.objectweb.asm.tree.LdcInsnNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.MethodNode;
import org.objectweb.asm.tree.VarInsnNode;
import mcjty.lostcities.api.ILostCityBuilding;
import net.minecraft.launchwrapper.IClassTransformer;
public class LostCitiesClassTransformer implements IClassTransformer {
private static final String[] classesBeingTransformed = { "mcjty.lostcities.dimensions.world.lost.BuildingInfo", "mcjty.lostcities.dimensions.world.terraingen.LostCitiesTerrainGenerator"};
@Override
public byte[] transform(String name, String transformedName, byte[] classBeingTransformed) {
boolean isObfuscated = !name.equals(transformedName);
int index = Arrays.asList(classesBeingTransformed).indexOf(transformedName);
return index != -1 ? transform(index, classBeingTransformed, isObfuscated) : classBeingTransformed;
}
private static byte[] transform(int index, byte[] classBeingTransformed, boolean isObfuscated) {
System.out.println("Transform " + classesBeingTransformed[index] + " got called!");
try {
ClassNode classNode = new ClassNode();
ClassReader classReader = new ClassReader(classBeingTransformed);
classReader.accept(classNode, 2);
System.out.println("Transforming " + classesBeingTransformed[index] + " Is Obf: " + isObfuscated);
switch (index) {
case 0:
transformLCCellars(classNode, isObfuscated);
break;
case 1:
transformBuildingBorders(classNode, isObfuscated);
break;
}
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS /*| ClassWriter.COMPUTE_FRAMES*/);
classNode.accept(classWriter);
return classWriter.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
return classBeingTransformed;
}
private static void transformBuildingBorders(ClassNode terrainGenClass, boolean isObfuscated) {
final String METHOD = isObfuscated ? "generateBuilding" : "generateBuilding";
final String METHOD_DESC = isObfuscated
? "(Lmcjty/lostcities/dimensions/world/lost/BuildingInfo;Lmcjty/lostcities/dimensions/world/ChunkHeightmap;)V"
: "(Lmcjty/lostcities/dimensions/world/lost/BuildingInfo;Lmcjty/lostcities/dimensions/world/ChunkHeightmap;)V";
for (MethodNode method : terrainGenClass.methods) {
if (method.name.equals(METHOD) && method.desc.equals(METHOD_DESC)) {
System.out.println("Found method in BuildingInfo to transform");
AbstractInsnNode targetNode = null;
for (AbstractInsnNode instruction : method.instructions.toArray()) {
if (instruction.getOpcode() == ALOAD) {
if (((VarInsnNode) instruction).var == 1 & instruction.getNext().getOpcode() == GETFIELD & instruction.getNext().getNext().getOpcode() == IFLE) {
System.out.println("Matched");
targetNode = instruction.getNext().getNext();
break;
}
}
}
if (targetNode != null) {
System.out.println("Target Node valid");
InsnList toInsert = new InsnList();
toInsert.add(new VarInsnNode(ALOAD, 1));
toInsert.add(
new MethodInsnNode(INVOKEVIRTUAL, "mcjty/lostcities/dimensions/world/lost/BuildingInfo",
"floorsBelowGround", "I", false));
toInsert.add(
new MethodInsnNode(INVOKEVIRTUAL, "seemdmax/lcpatches/Constants",
"getKey", "()Ljava/lang/String;", false));
toInsert.add(
new MethodInsnNode(INVOKEVIRTUAL, "java/lang/String", "endsWith", "(Ljava/lang/String;)Z", false));
toInsert.add(new JumpInsnNode(IFNE, ((JumpInsnNode) targetNode).label));
method.instructions.insert(targetNode, toInsert);
System.out.println("Transform done!");
} else {
System.out.println("Something went wrong transforming LostCitiesTerrainGenerator!");
}
}
}
}
private static void transformLCCellars(ClassNode buildingInfoClass, boolean isObfuscated) {
//This is already working
}
}
我要打补丁的原代码(字节码大纲):
L55
LINENUMBER 2338 L55
FRAME CHOP 3
ALOAD 1
GETFIELD mcjty/lostcities/dimensions/world/lost/BuildingInfo.floorsBelowGround : I
IFLE L68
包含我的更改的字节码大纲:
L55
LINENUMBER 2338 L55
FRAME CHOP 3
ALOAD 1
GETFIELD mcjty/lostcities/dimensions/world/lost/BuildingInfo.floorsBelowGround : I
IFLE L68
ALOAD 1
INVOKEVIRTUAL mcjty/lostcities/dimensions/world/lost/BuildingInfo.getBuildingType ()Ljava/lang/String;
LDC "#NOBORDER"
INVOKEVIRTUAL java/lang/String.endsWith (Ljava/lang/String;)Z
IFNE L68
我得到的堆栈跟踪:
java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.charAt(String.java:658)
at org.objectweb.asm.Type.getArgumentsAndReturnSizes(Type.java:420)
at org.objectweb.asm.MethodWriter.visitMethodInsn(MethodWriter.java:931)
at org.objectweb.asm.tree.MethodInsnNode.accept(MethodInsnNode.java:133)
at org.objectweb.asm.tree.InsnList.accept(InsnList.java:162)
at org.objectweb.asm.tree.MethodNode.accept(MethodNode.java:817)
at org.objectweb.asm.tree.MethodNode.accept(MethodNode.java:727)
at org.objectweb.asm.tree.ClassNode.accept(ClassNode.java:412)
at seemdmax.lcpatches.LostCitiesClassTransformer.transform(LostCitiesClassTransformer.java:59)
at seemdmax.lcpatches.LostCitiesClassTransformer.transform(LostCitiesClassTransformer.java:38)
at net.minecraftforge.fml.common.asm.ASMTransformerWrapper$TransformerWrapper.transform(ASMTransformerWrapper.java:256)
at net.minecraft.launchwrapper.LaunchClassLoader.runTransformers(LaunchClassLoader.java:279)
at net.minecraft.launchwrapper.LaunchClassLoader.findClass(LaunchClassLoader.java:176)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
at mcjty.lostcities.dimensions.world.LostCityChunkGenerator.<init>(LostCityChunkGenerator.java:170)
at mcjty.lostcities.dimensions.world.LostWorldType.getChunkGenerator(LostWorldType.java:55)
at net.minecraft.world.WorldProvider.createChunkGenerator(WorldProvider.java:86)
at net.minecraft.world.WorldServer.createChunkProvider(WorldServer.java:890)
at net.minecraft.world.WorldServer.<init>(WorldServer.java:124)
at net.minecraft.server.integrated.IntegratedServer.loadAllWorlds(IntegratedServer.java:122)
at net.minecraft.server.integrated.IntegratedServer.init(IntegratedServer.java:160)
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:552)
at java.lang.Thread.run(Thread.java:748)
为什么我要对开源 mod 进行 monkey-patch?
我需要它作为 mod 包装; Curseforge 不允许在 mod 包中使用 mod 化的罐子。 McJty 说,他不想要第二个 LC 项目,但他可以使用 coremods/mixins 等
修补 LC
如有任何帮助,我们将不胜感激。如果您需要了解更多详细信息,请随时询问。提前致谢。
这是问题所在:
toInsert.add(
new MethodInsnNode(INVOKEVIRTUAL, "mcjty/lostcities/dimensions/world/lost/BuildingInfo",
"floorsBelowGround", "I", false));
你试图调用 floorsBelowGround
就像它是一个方法,但它实际上是一个字段。您需要制作 GETFIELD
FieldInsnNode
而不是 INVOKEVIRTUAL
MethodInsnNode
.
我尝试在 1.12.2 Forge 上制作一个核心mod 以修补失落之城 mod 中一些丢失的东西。 (来源:https://github.com/McJtyMods/LostCities/blob/1.12/src/main/java/mcjty/lostcities/dimensions/world/lost/BuildingInfo.java)
我和一个朋友写了这个LostCitiesClassTransformer.java:(完整来源:https://github.com/Nick1st/LCPatches)
package seemdmax.lcpatches;
import static org.objectweb.asm.Opcodes.ALOAD;
import static org.objectweb.asm.Opcodes.IFGT;
import static org.objectweb.asm.Opcodes.IFLE;
import static org.objectweb.asm.Opcodes.IFNE;
import static org.objectweb.asm.Opcodes.ILOAD;
import static org.objectweb.asm.Opcodes.INVOKEINTERFACE;
import static org.objectweb.asm.Opcodes.INVOKESTATIC;
import static org.objectweb.asm.Opcodes.INVOKEVIRTUAL;
import static org.objectweb.asm.Opcodes.GETFIELD;
import java.util.Arrays;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.InsnList;
import org.objectweb.asm.tree.JumpInsnNode;
import org.objectweb.asm.tree.LabelNode;
import org.objectweb.asm.tree.LdcInsnNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.MethodNode;
import org.objectweb.asm.tree.VarInsnNode;
import mcjty.lostcities.api.ILostCityBuilding;
import net.minecraft.launchwrapper.IClassTransformer;
public class LostCitiesClassTransformer implements IClassTransformer {
private static final String[] classesBeingTransformed = { "mcjty.lostcities.dimensions.world.lost.BuildingInfo", "mcjty.lostcities.dimensions.world.terraingen.LostCitiesTerrainGenerator"};
@Override
public byte[] transform(String name, String transformedName, byte[] classBeingTransformed) {
boolean isObfuscated = !name.equals(transformedName);
int index = Arrays.asList(classesBeingTransformed).indexOf(transformedName);
return index != -1 ? transform(index, classBeingTransformed, isObfuscated) : classBeingTransformed;
}
private static byte[] transform(int index, byte[] classBeingTransformed, boolean isObfuscated) {
System.out.println("Transform " + classesBeingTransformed[index] + " got called!");
try {
ClassNode classNode = new ClassNode();
ClassReader classReader = new ClassReader(classBeingTransformed);
classReader.accept(classNode, 2);
System.out.println("Transforming " + classesBeingTransformed[index] + " Is Obf: " + isObfuscated);
switch (index) {
case 0:
transformLCCellars(classNode, isObfuscated);
break;
case 1:
transformBuildingBorders(classNode, isObfuscated);
break;
}
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS /*| ClassWriter.COMPUTE_FRAMES*/);
classNode.accept(classWriter);
return classWriter.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
return classBeingTransformed;
}
private static void transformBuildingBorders(ClassNode terrainGenClass, boolean isObfuscated) {
final String METHOD = isObfuscated ? "generateBuilding" : "generateBuilding";
final String METHOD_DESC = isObfuscated
? "(Lmcjty/lostcities/dimensions/world/lost/BuildingInfo;Lmcjty/lostcities/dimensions/world/ChunkHeightmap;)V"
: "(Lmcjty/lostcities/dimensions/world/lost/BuildingInfo;Lmcjty/lostcities/dimensions/world/ChunkHeightmap;)V";
for (MethodNode method : terrainGenClass.methods) {
if (method.name.equals(METHOD) && method.desc.equals(METHOD_DESC)) {
System.out.println("Found method in BuildingInfo to transform");
AbstractInsnNode targetNode = null;
for (AbstractInsnNode instruction : method.instructions.toArray()) {
if (instruction.getOpcode() == ALOAD) {
if (((VarInsnNode) instruction).var == 1 & instruction.getNext().getOpcode() == GETFIELD & instruction.getNext().getNext().getOpcode() == IFLE) {
System.out.println("Matched");
targetNode = instruction.getNext().getNext();
break;
}
}
}
if (targetNode != null) {
System.out.println("Target Node valid");
InsnList toInsert = new InsnList();
toInsert.add(new VarInsnNode(ALOAD, 1));
toInsert.add(
new MethodInsnNode(INVOKEVIRTUAL, "mcjty/lostcities/dimensions/world/lost/BuildingInfo",
"floorsBelowGround", "I", false));
toInsert.add(
new MethodInsnNode(INVOKEVIRTUAL, "seemdmax/lcpatches/Constants",
"getKey", "()Ljava/lang/String;", false));
toInsert.add(
new MethodInsnNode(INVOKEVIRTUAL, "java/lang/String", "endsWith", "(Ljava/lang/String;)Z", false));
toInsert.add(new JumpInsnNode(IFNE, ((JumpInsnNode) targetNode).label));
method.instructions.insert(targetNode, toInsert);
System.out.println("Transform done!");
} else {
System.out.println("Something went wrong transforming LostCitiesTerrainGenerator!");
}
}
}
}
private static void transformLCCellars(ClassNode buildingInfoClass, boolean isObfuscated) {
//This is already working
}
}
我要打补丁的原代码(字节码大纲):
L55
LINENUMBER 2338 L55
FRAME CHOP 3
ALOAD 1
GETFIELD mcjty/lostcities/dimensions/world/lost/BuildingInfo.floorsBelowGround : I
IFLE L68
包含我的更改的字节码大纲:
L55
LINENUMBER 2338 L55
FRAME CHOP 3
ALOAD 1
GETFIELD mcjty/lostcities/dimensions/world/lost/BuildingInfo.floorsBelowGround : I
IFLE L68
ALOAD 1
INVOKEVIRTUAL mcjty/lostcities/dimensions/world/lost/BuildingInfo.getBuildingType ()Ljava/lang/String;
LDC "#NOBORDER"
INVOKEVIRTUAL java/lang/String.endsWith (Ljava/lang/String;)Z
IFNE L68
我得到的堆栈跟踪:
java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.charAt(String.java:658)
at org.objectweb.asm.Type.getArgumentsAndReturnSizes(Type.java:420)
at org.objectweb.asm.MethodWriter.visitMethodInsn(MethodWriter.java:931)
at org.objectweb.asm.tree.MethodInsnNode.accept(MethodInsnNode.java:133)
at org.objectweb.asm.tree.InsnList.accept(InsnList.java:162)
at org.objectweb.asm.tree.MethodNode.accept(MethodNode.java:817)
at org.objectweb.asm.tree.MethodNode.accept(MethodNode.java:727)
at org.objectweb.asm.tree.ClassNode.accept(ClassNode.java:412)
at seemdmax.lcpatches.LostCitiesClassTransformer.transform(LostCitiesClassTransformer.java:59)
at seemdmax.lcpatches.LostCitiesClassTransformer.transform(LostCitiesClassTransformer.java:38)
at net.minecraftforge.fml.common.asm.ASMTransformerWrapper$TransformerWrapper.transform(ASMTransformerWrapper.java:256)
at net.minecraft.launchwrapper.LaunchClassLoader.runTransformers(LaunchClassLoader.java:279)
at net.minecraft.launchwrapper.LaunchClassLoader.findClass(LaunchClassLoader.java:176)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
at mcjty.lostcities.dimensions.world.LostCityChunkGenerator.<init>(LostCityChunkGenerator.java:170)
at mcjty.lostcities.dimensions.world.LostWorldType.getChunkGenerator(LostWorldType.java:55)
at net.minecraft.world.WorldProvider.createChunkGenerator(WorldProvider.java:86)
at net.minecraft.world.WorldServer.createChunkProvider(WorldServer.java:890)
at net.minecraft.world.WorldServer.<init>(WorldServer.java:124)
at net.minecraft.server.integrated.IntegratedServer.loadAllWorlds(IntegratedServer.java:122)
at net.minecraft.server.integrated.IntegratedServer.init(IntegratedServer.java:160)
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:552)
at java.lang.Thread.run(Thread.java:748)
为什么我要对开源 mod 进行 monkey-patch? 我需要它作为 mod 包装; Curseforge 不允许在 mod 包中使用 mod 化的罐子。 McJty 说,他不想要第二个 LC 项目,但他可以使用 coremods/mixins 等
修补 LC如有任何帮助,我们将不胜感激。如果您需要了解更多详细信息,请随时询问。提前致谢。
这是问题所在:
toInsert.add(
new MethodInsnNode(INVOKEVIRTUAL, "mcjty/lostcities/dimensions/world/lost/BuildingInfo",
"floorsBelowGround", "I", false));
你试图调用 floorsBelowGround
就像它是一个方法,但它实际上是一个字段。您需要制作 GETFIELD
FieldInsnNode
而不是 INVOKEVIRTUAL
MethodInsnNode
.