import java.io.*; import java.nio.file.*; import java.security.*; import java.util.*; import java.util.zip.*; import org.objectweb.asm.*; import org.objectweb.asm.tree.*; /** Extend the exact live terrain build; never deploy a rebuild of stale terrain sources. */ public final class PatchArrakisEcology { static final String MAIN="com/example/duneterrain/DuneTerrain.class",ADDON="com/example/duneterrain/ArrakisEcology"; static final String ORIGINAL="f6f8547049bfd022bc68b57f37c5a259f268c2ad54a2aa08790ac3f011920bcf"; static byte[] methodBytes(MethodNode method){var out=new ClassWriter(0);out.visit(65,Opcodes.ACC_PUBLIC,"MethodCheck",null,"java/lang/Object",null);method.accept(out.visitMethod(method.access,method.name,method.desc,method.signature,method.exceptions.toArray(String[]::new)));out.visitEnd();return out.toByteArray();} static byte[] patch(byte[] original){ var node=new ClassNode();new ClassReader(original).accept(node,0);var before=new HashMap();var touched=new HashSet(); for(var method:node.methods){ for(var instruction:method.instructions)if(instruction instanceof MethodInsnNode call&&call.owner.equals(ADDON))throw new IllegalStateException("Ecology is already installed"); String key=method.name+method.desc;before.put(key,methodBytes(method)); boolean start=key.equals("onEnable()V"),bloom=key.equals("announceSpiceBloom(Lorg/bukkit/Location;)V");if(!start&&!bloom)continue; var hook=new InsnList();hook.add(new VarInsnNode(Opcodes.ALOAD,0));if(bloom)hook.add(new VarInsnNode(Opcodes.ALOAD,1));hook.add(new MethodInsnNode(Opcodes.INVOKESTATIC,ADDON,start?"start":"bloom",start?"(Lorg/bukkit/plugin/Plugin;)V":"(Lorg/bukkit/plugin/Plugin;Lorg/bukkit/Location;)V",false));method.instructions.insert(hook);method.maxStack=Math.max(method.maxStack,bloom?2:1);touched.add(key); } if(!touched.equals(Set.of("onEnable()V","announceSpiceBloom(Lorg/bukkit/Location;)V")))throw new IllegalStateException("Live terrain hooks changed"); var writer=new ClassWriter(0);node.accept(writer);byte[] result=writer.toByteArray();var check=new ClassNode();new ClassReader(result).accept(check,0);for(var method:check.methods){String key=method.name+method.desc;if(!touched.contains(key)&&!Arrays.equals(before.get(key),methodBytes(method)))throw new IllegalStateException("Unrelated terrain method changed: "+key);}return result; } public static void main(String[] args)throws Exception{ if(args.length!=3)throw new IllegalArgumentException("original.jar classes-folder output.jar");var source=Path.of(args[0]);var classes=Path.of(args[1]);var target=Path.of(args[2]);if(Files.exists(target)||source.toRealPath().equals(target.toAbsolutePath()))throw new IllegalStateException("Use a new output file");if(!HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(Files.readAllBytes(source))).equals(ORIGINAL))throw new IllegalStateException("Live terrain identity changed"); var entries=new LinkedHashMap();try(var zip=new ZipInputStream(Files.newInputStream(source))){for(ZipEntry entry;(entry=zip.getNextEntry())!=null;){if(entries.put(entry.getName(),zip.readAllBytes())!=null)throw new IllegalStateException("Duplicate JAR entry");}} var baseline=new LinkedHashMap<>(entries);entries.put(MAIN,patch(Objects.requireNonNull(entries.get(MAIN))));String descriptor=new String(entries.get("plugin.yml"),java.nio.charset.StandardCharsets.UTF_8);if(!descriptor.contains("version: 1.0.12-arrakeen1"))throw new IllegalStateException("Unexpected live version");entries.put("plugin.yml",descriptor.replace("version: 1.0.12-arrakeen1","version: 1.0.12-arrakeen1-ecology1").getBytes(java.nio.charset.StandardCharsets.UTF_8)); var additions=new ArrayList();try(var paths=Files.list(classes.resolve("com/example/duneterrain"))){for(var p:paths.filter(p->p.getFileName().toString().matches("ArrakisEcology(?:\\$[A-Za-z0-9]+)?\\.class")).sorted().toList()){String name="com/example/duneterrain/"+p.getFileName();if(entries.containsKey(name))throw new IllegalStateException("Duplicate ecology class");entries.put(name,Files.readAllBytes(p));additions.add(name);}} if(!additions.contains(ADDON+".class")||!additions.contains(ADDON+"$Items.class"))throw new IllegalStateException("Compiled ecology classes missing"); for(var e:baseline.entrySet())if(!Set.of(MAIN,"plugin.yml").contains(e.getKey())&&!Arrays.equals(e.getValue(),entries.get(e.getKey())))throw new IllegalStateException("Unrelated JAR entry changed"); Files.createDirectories(target.toAbsolutePath().getParent());try(var out=new ZipOutputStream(Files.newOutputStream(target,StandardOpenOption.CREATE_NEW))){for(var e:entries.entrySet()){var entry=new ZipEntry(e.getKey());entry.setTime(0);out.putNextEntry(entry);out.write(e.getValue());out.closeEntry();}} System.out.println("Preserved "+(baseline.size()-2)+" existing JAR entries; changed only terrain hooks and version; added "+additions.size()+" ecology classes.");System.out.println("SHA256 "+HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(Files.readAllBytes(target)))); } }