Wire native trail restoration into the actual Guild worlds
This commit is contained in:
parent
d2b081b1a3
commit
80f773bd8a
1 changed files with 27 additions and 2 deletions
25
tools/PatchGuildTrails.java
Normal file
25
tools/PatchGuildTrails.java
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import java.nio.file.*;
|
||||
import java.security.*;
|
||||
import java.util.*;
|
||||
import java.util.zip.*;
|
||||
import org.objectweb.asm.*;
|
||||
import org.objectweb.asm.tree.*;
|
||||
|
||||
/** Install the same native Trails provenance bridge in the existing Guild plugin. */
|
||||
public final class PatchGuildTrails {
|
||||
public static void main(String[] args)throws Exception{
|
||||
Path input=Path.of(args[0]),classes=Path.of(args[1]),output=Path.of(args[2]);
|
||||
if(Files.exists(output)||!HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(Files.readAllBytes(input))).equals("a1fe48e2ab4f7963287b2fb2b1b1db8c83d8de3338d793c29b66814fcd1e631f"))throw new IllegalStateException("Unexpected Guild plugin");
|
||||
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 entry");}
|
||||
var original=new LinkedHashMap<>(entries);String main="com/example/dunelobby/DuneLobby.class",bridge="com/example/duneterrain/TrailsBridge";
|
||||
if(entries.containsKey(bridge+".class"))throw new IllegalStateException("Already installed");
|
||||
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 hooks=0;for(var method:type.methods)if(method.name.equals("onEnable")&&method.desc.equals("()V")){var hook=new InsnList();hook.add(new VarInsnNode(Opcodes.ALOAD,0));hook.add(new MethodInsnNode(Opcodes.INVOKESTATIC,bridge,"start","(Lorg/bukkit/plugin/Plugin;)V",false));method.instructions.insert(hook);method.maxStack=Math.max(method.maxStack,1);hooks++;}
|
||||
if(hooks!=1)throw new IllegalStateException("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 method changed");
|
||||
entries.put(bridge+".class",Files.readAllBytes(classes.resolve(bridge+".class")));entries.put("plugin.yml",new String(entries.get("plugin.yml"),java.nio.charset.StandardCharsets.UTF_8).replace("version: 1.0.1-exchange","version: 1.0.1-exchange-trails1").getBytes(java.nio.charset.StandardCharsets.UTF_8));
|
||||
for(var entry:original.entrySet())if(!Set.of(main,"plugin.yml").contains(entry.getKey())&&!Arrays.equals(entry.getValue(),entries.get(entry.getKey())))throw new IllegalStateException("Unrelated archive entry changed");
|
||||
try(var zip=new ZipOutputStream(Files.newOutputStream(output,StandardOpenOption.CREATE_NEW))){for(var entry:entries.entrySet()){var next=new ZipEntry(entry.getKey());next.setTime(0);zip.putNextEntry(next);zip.write(entry.getValue());zip.closeEntry();}}
|
||||
System.out.println("Guild plugin preserved; SHA256 "+HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(Files.readAllBytes(output))));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue

Staff