forked from unimatrix/web
28 lines
3.7 KiB
Java
28 lines
3.7 KiB
Java
import java.nio.file.*;
|
|
import java.security.*;
|
|
import java.util.*;
|
|
import java.util.zip.*;
|
|
import org.objectweb.asm.*;
|
|
import org.objectweb.asm.tree.*;
|
|
|
|
/** Add fauna/Trails to an exact current terrain JAR without replacing generation. */
|
|
public final class PatchFaunaTerrain {
|
|
public static void main(String[] args)throws Exception{
|
|
Path input=Path.of(args[0]),classes=Path.of(args[1]),output=Path.of(args[2]);String expected=args[3],prefix="com/example/duneterrain/";
|
|
if(Files.exists(output)||!HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(Files.readAllBytes(input))).equals(expected))throw new IllegalStateException("Unexpected live terrain or output");
|
|
var entries=new LinkedHashMap<String,byte[]>();try(var zip=new ZipInputStream(Files.newInputStream(input))){for(ZipEntry e;(e=zip.getNextEntry())!=null;)if(entries.put(e.getName(),zip.readAllBytes())!=null)throw new IllegalStateException("Duplicate archive entry");}
|
|
var original=new LinkedHashMap<>(entries);String main=prefix+"DuneTerrain.class";var type=new ClassNode();new ClassReader(entries.get(main)).accept(type,0);var methods=new HashMap<String,byte[]>();for(var method:type.methods)methods.put(method.name+method.desc,PatchArrakisEcology.methodBytes(method));
|
|
int edits=0;for(MethodNode method:type.methods)if(method.name.equals("onEnable")&&method.desc.equals("()V")){
|
|
for(var instruction:method.instructions)if(instruction instanceof MethodInsnNode call&&call.owner.equals(prefix+"TrailsBridge"))throw new IllegalStateException("Trails bridge already present");
|
|
var hook=new InsnList();hook.add(new VarInsnNode(Opcodes.ALOAD,0));hook.add(new MethodInsnNode(Opcodes.INVOKESTATIC,prefix+"TrailsBridge","start","(Lorg/bukkit/plugin/Plugin;)V",false));method.instructions.insert(hook);method.maxStack=Math.max(method.maxStack,1);edits++;
|
|
}
|
|
if(edits!=1)throw new IllegalStateException("Native activation hook changed");var writer=new ClassWriter(0);type.accept(writer);entries.put(main,writer.toByteArray());var verify=new ClassNode();new ClassReader(entries.get(main)).accept(verify,0);for(var method:verify.methods)if(!method.name.equals("onEnable")&&!Arrays.equals(methods.get(method.name+method.desc),PatchArrakisEcology.methodBytes(method)))throw new IllegalStateException("Unrelated terrain method changed");
|
|
var allowed=new HashSet<String>(Set.of(main,"plugin.yml"));var inserted=new HashSet<String>();
|
|
try(var paths=Files.list(classes.resolve(prefix))){for(Path file:paths.filter(p->p.getFileName().toString().matches("(?:ArrakisEcology|JerboaMobs|FaunaModels|TrailsBridge)(?:\\$[A-Za-z0-9]+)?\\.class")).toList()){String name=prefix+file.getFileName();entries.put(name,Files.readAllBytes(file));allowed.add(name);inserted.add(name);}}
|
|
for(String name:List.of("ArrakisEcology","JerboaMobs","FaunaModels","TrailsBridge"))if(!inserted.contains(prefix+name+".class"))throw new IllegalStateException("Missing compiled feature");
|
|
String descriptor=new String(entries.get("plugin.yml"),java.nio.charset.StandardCharsets.UTF_8);entries.put("plugin.yml",descriptor.replaceAll("(?m)^version:.*$","version: 1.0.12-arrakeen1-fauna2-trails1").getBytes(java.nio.charset.StandardCharsets.UTF_8));
|
|
for(var e:original.entrySet())if(!allowed.contains(e.getKey())&&!Arrays.equals(e.getValue(),entries.get(e.getKey())))throw new IllegalStateException("Unrelated archive entry changed");
|
|
try(var zip=new ZipOutputStream(Files.newOutputStream(output,StandardOpenOption.CREATE_NEW))){for(var e:entries.entrySet()){var next=new ZipEntry(e.getKey());next.setTime(0);zip.putNextEntry(next);zip.write(e.getValue());zip.closeEntry();}}
|
|
System.out.println("Preserved original generation and unrelated terrain methods. SHA256 "+HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(Files.readAllBytes(output))));
|
|
}
|
|
}
|

Staff