Merge commit '5bb36a4eb2' into fix/woodcutter-origin-20261204
Some checks failed
DuneRealms Plugin CI / plugins (push) Has been cancelled

This commit is contained in:
unimatrixgit 2026-12-04 20:48:05 +01:00
commit 1a1f6c8d09
2 changed files with 276 additions and 12 deletions

View file

@ -0,0 +1,81 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Handle;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.InvokeDynamicInsnNode;
import org.objectweb.asm.tree.MethodNode;
class GuildCookbookMerge {
private static final String GUI = "com/dunerealms/gui/";
private static final Map<String, Set<String>> TARGETS = Map.of(
GUI + "DuneGUI", Set.of("openGuild", "guildReturnMenu"),
GUI + "OrganisationMenus", Set.of("cookbookRecipe", "recipeIngredient"),
GUI + "CookbookCatalog$Recipe", Set.of("key"),
"com/example/dunelobby/DuneLobby", Set.of("entityInteract"),
"com/example/dunenpc/DuneNPC", Set.of("navigator"));
public static void main(String[] args) throws Exception {
if (args.length != 3) throw new IllegalArgumentException("Expected retained, donor and output classes");
var retainedReader = new ClassReader(Files.readAllBytes(Path.of(args[0])));
var retained = parse(retainedReader);
var donor = parse(new ClassReader(Files.readAllBytes(Path.of(args[1]))));
var targets = TARGETS.get(retained.name);
if (targets == null || !retained.name.equals(donor.name)) throw new IllegalArgumentException("Unexpected overlay class");
var additions = new LinkedHashMap<String, MethodNode>();
var renamed = new HashMap<String, String>();
for (String target : targets.stream().sorted().toList()) {
var method = donor.methods.stream().filter(m -> m.name.equals(target)).toList();
if (method.size() != 1) throw new IllegalArgumentException("Expected one donor method " + target);
prepare(donor, method.getFirst(), additions, renamed);
}
for (var entry : additions.entrySet()) {
var method = entry.getValue();
if (!targets.contains(method.name) &&
retained.methods.stream().anyMatch(old -> old.name.equals(method.name) && old.desc.equals(method.desc))) {
throw new IllegalArgumentException("Unexpected existing helper " + method.name);
}
retained.methods.removeIf(old -> old.name.equals(method.name) && old.desc.equals(method.desc));
retained.methods.add(method);
}
var writer = new ClassWriter(retainedReader, 0);
retained.accept(writer);
Files.write(Path.of(args[2]), writer.toByteArray());
}
private static void prepare(ClassNode donor, MethodNode method, Map<String, MethodNode> copies,
Map<String, String> renamed) {
String key = method.name + method.desc;
if (copies.putIfAbsent(key, method) != null) return;
for (var instruction : method.instructions) {
if (!(instruction instanceof InvokeDynamicInsnNode dynamic)) continue;
for (int i = 0; i < dynamic.bsmArgs.length; i++) {
if (!(dynamic.bsmArgs[i] instanceof Handle handle) || !handle.getOwner().equals(donor.name) ||
!handle.getName().startsWith("lambda$")) continue;
String original = handle.getName() + handle.getDesc();
String replacement = renamed.get(original);
if (replacement == null) {
var helper = donor.methods.stream().filter(m -> m.name.equals(handle.getName()) &&
m.desc.equals(handle.getDesc())).findFirst().orElseThrow();
replacement = "guildCookbook$" + handle.getName();
renamed.put(original, replacement);
helper.name = replacement;
prepare(donor, helper, copies, renamed);
}
dynamic.bsmArgs[i] = new Handle(handle.getTag(), handle.getOwner(), replacement,
handle.getDesc(), handle.isInterface());
}
}
}
private static ClassNode parse(ClassReader reader) {
var node = new ClassNode();
reader.accept(node, 0);
return node;
}
}

View file

@ -0,0 +1,53 @@
"""Assemble reviewed Guild and Cookbook methods onto the installed plugin variants."""
import argparse
import os
from pathlib import Path
import subprocess
from tempfile import TemporaryDirectory
from plugin_wild_update import digest, read_archive, write_archive
CLASSES = {
"gui": (
"com/dunerealms/gui/DuneGUI.class",
"com/dunerealms/gui/OrganisationMenus.class",
"com/dunerealms/gui/CookbookCatalog$Recipe.class",
),
"npc": ("com/example/dunenpc/DuneNPC.class",),
"lobby": ("com/example/dunelobby/DuneLobby.class",),
}
def assemble(module, baseline, baseline_sha, donor, donor_sha, output, classpath):
baseline_bytes = baseline.read_bytes()
donor_bytes = donor.read_bytes()
if digest(baseline_bytes) != baseline_sha or digest(donor_bytes) != donor_sha:
raise ValueError("Unexpected baseline or donor checksum")
retained = read_archive(baseline_bytes)
compiled = read_archive(donor_bytes)
java_home = os.environ.get("JAVA_HOME")
executable = "java.exe" if os.name == "nt" else "java"
java = str(Path(java_home) / "bin" / executable) if java_home else executable
helper = Path(__file__).with_name("GuildCookbookMerge.java")
with TemporaryDirectory() as temporary:
temp = Path(temporary)
for index, entry in enumerate(CLASSES[module]):
old, new, merged = (temp / f"{index}-{name}.class" for name in ("retained", "donor", "merged"))
old.write_bytes(retained[entry])
new.write_bytes(compiled[entry])
subprocess.run([java, "--class-path", classpath, str(helper), str(old), str(new), str(merged)], check=True)
retained[entry] = merged.read_bytes()
with output.open("xb") as candidate:
candidate.write(write_archive(retained))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("module", choices=CLASSES)
parser.add_argument("baseline", type=Path)
parser.add_argument("baseline_sha")
parser.add_argument("donor", type=Path)
parser.add_argument("donor_sha")
parser.add_argument("output", type=Path)
parser.add_argument("classpath", help="ASM core and tree JARs")
assemble(**vars(parser.parse_args()))