plugins/tools/plugin_regional_cooking.py
2026-12-06 14:56:25 +01:00

68 lines
No EOL
3.1 KiB
Python

"""Build add-only and exact-method overlays for retained regional cooking plugins."""
import argparse
import os
from pathlib import Path
import subprocess
from tempfile import TemporaryDirectory
from plugin_golden_path import pinned
from plugin_wild_update import write_archive
JARVIS = "com/dunerealms/jarvis/"
GUI = "com/dunerealms/gui/"
TERRAIN = "com/example/duneterrain/"
CATALOG = "regional-cooking.json"
ROLES = {
"jarvis": ((JARVIS + "JarvisTranslator", JARVIS + "SurvivalFoodRecipes"), (
JARVIS + "RegionalCooking", JARVIS + "RegionalCraftRemainders",
JARVIS + "RegionalFoodAcquisition", JARVIS + "RegionalFoodItem",
JARVIS + "RegionalFoodStations", JARVIS + "NativeLeafDecay"), (CATALOG,)),
"gui": ((GUI + "OrganisationMenus",), (GUI + "RegionalCookbook",), (CATALOG,)),
"arrakis": ((TERRAIN + "DuneTerrain$PlanetChunkGenerator",), (), ()),
"giedi": ((TERRAIN + "PlanetMobs", TERRAIN + "NativePlants"), (), ()),
"kaitain": ((TERRAIN + "PlanetMobs",), (TERRAIN + "KaitainLivestock",), ()),
}
def slice_donor(archive, role):
changed, families, resources = ROLES[role]
selected = {name + ".class" for name in changed} | set(resources)
for family in families:
selected.add(family + ".class")
selected.update(entry for entry in archive if entry.startswith(family + "$") and entry.endswith(".class"))
return {entry: archive[entry] for entry in sorted(selected)}
def assemble(role, baseline, baseline_sha, donor, donor_sha, output, classpath):
existing = pinned(baseline, baseline_sha)
compiled = pinned(donor, donor_sha)
selection = slice_donor(compiled, role)
changed = {name + ".class" for name in ROLES[role][0]}
additions = selection.keys() - changed
if existing.keys() & additions:
raise ValueError("An additive cooking entry already exists in the retained archive")
helper = Path(__file__).with_name("RegionalCookingMerge.java")
with TemporaryDirectory() as temporary:
root = Path(temporary)
classes = root / "helper"
classes.mkdir()
subprocess.run(["javac", "-cp", classpath, "-d", str(classes), str(helper)], check=True)
for entry in sorted(changed):
old, new, result = root / "old.class", root / "donor.class", root / f"changed-{role}.class"
old.write_bytes(existing[entry])
new.write_bytes(selection[entry])
subprocess.run(["java", "-cp", f"{classes}{os.pathsep}{classpath}", "RegionalCookingMerge",
str(old), str(new), str(result)], check=True)
existing[entry] = result.read_bytes()
existing.update({entry: selection[entry] for entry in additions})
with output.open("xb") as target:
target.write(write_archive(existing))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("role", choices=ROLES)
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")
assemble(**vars(parser.parse_args()))