"""Assemble the Golden Path graft over explicitly pinned retained plugin archives.""" import argparse import json import os from pathlib import Path import subprocess from tempfile import TemporaryDirectory from plugin_wild_update import digest, read_archive, write_archive MODULES = { "gui": ( "com/dunerealms/gui/", ("PolQuestNpc",), ("LetoQuestContacts", "LetoQuestContacts$Contact"), "quest-categories.json", ), "jarvis": ( "com/dunerealms/jarvis/", ("ExchangeInventory", "JarvisTranslator", "QuestObservations", "StarterKitFlow"), ("LetoQuestObservations", "LetoQuestObservations$Contact", "LetoQuestRewards", "LetoQuestRewards$Template"), "leto-item-templates.json", ), } def pinned(path, sha): raw = path.read_bytes() if digest(raw) != sha: raise ValueError(f"Unexpected archive bytes: {path.name}") return read_archive(raw) def assemble(module, baseline, baseline_sha, donor, donor_sha, output, classpath): prefix, retained, additions, resource = MODULES[module] existing = pinned(baseline, baseline_sha) compiled = pinned(donor, donor_sha) if any(f"{prefix}{name}.class" in existing for name in additions): raise ValueError("Golden Path classes already installed") previous = json.loads(existing[resource]) if module == "gui" else None if module == "gui": updated = json.loads(compiled[resource]) new_categories = updated.get("categories", []) additions_rows = [category for category in new_categories if category.get("id") == "golden-path"] if len(additions_rows) != 1 or { **updated, "categories": [category for category in new_categories if category.get("id") != "golden-path"] } != previous: raise ValueError("Only the Golden Path category may be added") elif resource in existing: raise ValueError("Golden Path templates already installed") helper = Path(__file__).with_name("GoldenPathMerge.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) separator = os.pathsep for name in retained: entry = prefix + name + ".class" old, new, result = (root / f"{stem}.class" for stem in ("retained", "donor", "merged")) old.write_bytes(existing[entry]) new.write_bytes(compiled[entry]) subprocess.run( ["java", "-cp", str(classes) + separator + classpath, "GoldenPathMerge", str(old), str(new), str(result)], check=True, ) existing[entry] = result.read_bytes() for name in additions: entry = prefix + name + ".class" existing[entry] = compiled[entry] existing[resource] = compiled[resource] with output.open("xb") as target: target.write(write_archive(existing)) if __name__ == "__main__": parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("module", choices=MODULES) 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()))