web/tools/PatchOrdosCaves.java

46 lines
3 KiB
Java

import java.nio.file.*;
import java.security.MessageDigest;
import java.util.HexFormat;
import org.objectweb.asm.*;
/** Build tool: retain the installed planet generator and enable only Ordos cave carving. */
public final class PatchOrdosCaves {
public static void main(String[] args)throws Exception{
if(args.length!=3)throw new IllegalArgumentException("input.class output.class expectedSha256");
Path input=Path.of(args[0]),output=Path.of(args[1]);byte[] original=Files.readAllBytes(input);
if(Files.exists(output)||!HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(original)).equals(args[2]))throw new IllegalStateException("Input changed or output exists");
ClassReader reader=new ClassReader(original);String owner="com/example/duneterrain/DuneTerrain$PlanetChunkGenerator";
if(!reader.getClassName().equals(owner))throw new IllegalStateException("Unexpected generator class");
ClassWriter writer=new ClassWriter(reader,ClassWriter.COMPUTE_MAXS);int[] changed={0,0};
reader.accept(new ClassVisitor(Opcodes.ASM9,writer){
@Override public MethodVisitor visitMethod(int access,String name,String descriptor,String signature,String[] exceptions){
MethodVisitor method=super.visitMethod(access,name,descriptor,signature,exceptions);
if(name.equals("generateCaves")&&descriptor.equals("(Lorg/bukkit/generator/WorldInfo;Ljava/util/Random;IILorg/bukkit/generator/ChunkGenerator$ChunkData;)V")){
changed[1]++;
return new MethodVisitor(Opcodes.ASM9,method){
@Override public void visitInsn(int opcode){
if(opcode==Opcodes.RETURN){
super.visitVarInsn(Opcodes.ALOAD,1);super.visitVarInsn(Opcodes.ILOAD,3);super.visitVarInsn(Opcodes.ILOAD,4);super.visitVarInsn(Opcodes.ALOAD,5);
super.visitMethodInsn(Opcodes.INVOKESTATIC,"com/example/duneterrain/OrdosSulfurSurfaces","apply","(Lorg/bukkit/generator/WorldInfo;IILorg/bukkit/generator/ChunkGenerator$ChunkData;)V",false);
}
super.visitInsn(opcode);
}
};
}
if(!name.equals("shouldGenerateCaves")||!descriptor.equals("(Lorg/bukkit/generator/WorldInfo;Ljava/util/Random;II)Z"))return method;
changed[0]++;
return new MethodVisitor(Opcodes.ASM9,method){
@Override public void visitCode(){
super.visitCode();Label originalPath=new Label();
super.visitVarInsn(Opcodes.ALOAD,0);super.visitFieldInsn(Opcodes.GETFIELD,owner,"planet","Ljava/lang/String;");
super.visitLdcInsn("sigma_draconis_iv");super.visitMethodInsn(Opcodes.INVOKEVIRTUAL,"java/lang/String","equals","(Ljava/lang/Object;)Z",false);
super.visitJumpInsn(Opcodes.IFEQ,originalPath);super.visitInsn(Opcodes.ICONST_1);super.visitInsn(Opcodes.IRETURN);
super.visitLabel(originalPath);super.visitFrame(Opcodes.F_SAME,0,null,0,null);
}
};
}
},0);
if(changed[0]!=1||changed[1]!=1)throw new IllegalStateException("Expected exactly one cave policy and generation hook");
Files.write(output,writer.toByteArray());System.out.println("Patched Ordos cave policy; original paths for other planets retained");
}
}