Add native trail wear profiles and preserve installed terrain during rollout

This commit is contained in:
Dev 2026-11-24 00:03:20 +01:00
commit 22ab471ce4
2 changed files with 316 additions and 0 deletions

View file

@ -0,0 +1,32 @@
import java.nio.file.*;
import java.security.*;
import java.util.*;
import java.util.zip.*;
import org.objectweb.asm.*;
import org.objectweb.asm.tree.*;
/** Extend exact installed terrain with surface rules while preserving world generation. */
public final class PatchPlanetSurface {
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 plugin or existing output");
var entries=new LinkedHashMap<String,byte[]>();try(var in=new ZipInputStream(Files.newInputStream(input))){for(ZipEntry e;(e=in.getNextEntry())!=null;)if(entries.put(e.getName(),in.readAllBytes())!=null)throw new IllegalStateException("Duplicate entry");}
var before=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(var method:type.methods)if(method.name.equals("onEnable")&&method.desc.equals("()V")){
var existing=new HashSet<String>();for(var instruction:method.instructions)if(instruction instanceof MethodInsnNode call&&call.name.equals("start"))existing.add(call.owner);
if(existing.contains(prefix+"PlanetSoil"))throw new IllegalStateException("Surface rules already installed");
var hook=new InsnList();
for(String feature:List.of("TrailsBridge","PlanetSoil"))if(!existing.contains(prefix+feature)){hook.add(new VarInsnNode(Opcodes.ALOAD,0));hook.add(new MethodInsnNode(Opcodes.INVOKESTATIC,prefix+feature,"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("Missing activation hook");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");
var allowed=new HashSet<String>(Set.of(main,"plugin.yml"));
for(String feature:List.of("TrailsBridge","PlanetSoil")){String name=prefix+feature+".class";byte[] data=Files.readAllBytes(classes.resolve(name));if(entries.containsKey(name)&&!Arrays.equals(entries.get(name),data))throw new IllegalStateException("Existing feature differs: "+feature);entries.put(name,data);allowed.add(name);}
String descriptor=new String(entries.get("plugin.yml"),java.nio.charset.StandardCharsets.UTF_8);if(descriptor.contains("surface1"))throw new IllegalStateException("Already patched");entries.put("plugin.yml",descriptor.replaceAll("(?m)^(version: [^\\r\\n]+)","$1-surface1").getBytes(java.nio.charset.StandardCharsets.UTF_8));
for(var entry:before.entrySet())if(!allowed.contains(entry.getKey())&&!Arrays.equals(entry.getValue(),entries.get(entry.getKey())))throw new IllegalStateException("Unrelated archive entry changed");
try(var out=new ZipOutputStream(Files.newOutputStream(output,StandardOpenOption.CREATE_NEW))){for(var entry:entries.entrySet()){var next=new ZipEntry(entry.getKey());next.setTime(0);out.putNextEntry(next);out.write(entry.getValue());out.closeEntry();}}
System.out.println("Preserved world generator and unrelated methods; SHA256 "+HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(Files.readAllBytes(output))));
}
}

View file

@ -0,0 +1,21 @@
import fs from 'node:fs';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
const root=path.resolve(path.dirname(fileURLToPath(import.meta.url)),'..');
const baseline=fs.readFileSync(path.join(root,'DuneTerrain/trails/arrakis-config.yml'),'utf8');
const common=[['STONE','COBBLESTONE'],['GRANITE','POLISHED_GRANITE'],['DIORITE','POLISHED_DIORITE'],['ANDESITE','POLISHED_ANDESITE'],['TUFF','POLISHED_TUFF'],['STONE_BRICKS','CRACKED_STONE_BRICKS'],['GLASS','TINTED_GLASS']];
const profiles={
caladan:{worlds:['caladan'],links:[['GRASS_BLOCK','DIRT_PATH'],['DIRT','DIRT_PATH'],['PODZOL','DIRT_PATH'],['ROOTED_DIRT','DIRT_PATH'],['MOSS_BLOCK','DIRT_PATH'],['MUD','PACKED_MUD'],['SAND','SANDSTONE']]},
kaitain:{worlds:['kaitain'],links:[['GRASS_BLOCK','DIRT_PATH'],['DIRT','DIRT_PATH'],['COARSE_DIRT','DIRT_PATH'],['SAND','SANDSTONE']]},
giedi_prime:{worlds:['giedi_prime','giedi_prime_nether'],links:[['DIRT','DIRT_PATH'],['COARSE_DIRT','DIRT_PATH'],['DEEPSLATE','COBBLED_DEEPSLATE'],['NETHERRACK','CRACKED_NETHER_BRICKS'],['SOUL_SAND','SOUL_SOIL'],['BASALT','SMOOTH_BASALT'],['BLACKSTONE','POLISHED_BLACKSTONE']]},
sigma_draconis_iv:{worlds:['sigma_draconis_iv','sigma_draconis_iv_the_end'],links:[['SNOW_BLOCK','PACKED_ICE'],['ICE','PACKED_ICE'],['PODZOL','DIRT_PATH'],['DIRT','DIRT_PATH'],['COARSE_DIRT','DIRT_PATH'],['RED_SAND','RED_SANDSTONE'],['END_STONE','END_STONE_BRICKS']]},
lobby:{worlds:['lobby','world','world_the_end'],links:[['END_STONE','END_STONE_BRICKS'],['GRASS_BLOCK','DIRT_PATH'],['DIRT','DIRT_PATH']]}
};
for(const [name,profile] of Object.entries(profiles)){
const links=[...profile.links,...common];
if(new Set(links.map(([from])=>from)).size!==links.length)throw Error('Repeated source material '+name);
const rules=links.map(([from,to],i)=>` Surface${i+1}: '${from}:32:100:1.0 > ${to}:0:100:1.0'`).join('\n');
const output=baseline.replace('enabled-worlds: [arrakis]',`enabled-worlds: [${profile.worlds.join(', ')}]`).replace(/Trails:\r?\n[\s\S]*?Data-Saving:/,`Trails:\n${rules}\nData-Saving:`);
fs.writeFileSync(path.join(root,'DuneTerrain/trails',name+'-config.yml'),output.replace(/\r\n/g,'\n'));
}
console.log('Generated five native Trails profiles; existing Arrakis policy unchanged.');