"""Assemble reviewed compact-menu 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/MenuDocument.class", "com/dunerealms/gui/DuneGUI.class", "com/dunerealms/gui/QuestMenus.class", "com/dunerealms/gui/SuppliedMenus.class"), "menus": ("com/dunerealms/menus/SuppliedDocument.class", "com/dunerealms/menus/DuneMenusPlugin.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("CompactMenuMerge.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() if module in ("gui", "menus"): entry = "com/dunerealms/" + module + "/MenuPolish.class" if entry in retained: raise ValueError("Menu styling already installed") retained[entry] = compiled[entry] 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()))