plugins/tools/plugin_workstation_origin.py
unimatrixgit a85bd084a8
Some checks failed
Workstation Origin Overlay / workstation-origin (push) Successful in 1m21s
DuneRealms Plugin CI / plugins (push) Successful in 10m5s
DuneRealms Resourcepack CI / resourcepack (push) Failing after 17s
Preserve retained item metadata behavior in workstation overlay
2026-12-04 19:55:57 +01:00

60 lines
2.5 KiB
Python

"""Build and apply the scoped workstation provenance class overlay."""
import argparse
import os
import subprocess
from tempfile import TemporaryDirectory
from pathlib import Path
from plugin_wild_update import digest, read_archive, write_archive
ENTRY = "com/dunerealms/jarvis/ItemMetadataOverlay.class"
def extract(built, output):
compiled = read_archive(built.read_bytes())
output.write_bytes(write_archive({ENTRY: compiled[ENTRY]}))
def assemble(baseline, baseline_sha, patch, patch_sha, output):
baseline_bytes = baseline.read_bytes()
patch_bytes = patch.read_bytes()
if digest(baseline_bytes) != baseline_sha or digest(patch_bytes) != patch_sha:
raise ValueError("Unexpected baseline or overlay checksum")
retained = read_archive(baseline_bytes)
overlay = read_archive(patch_bytes)
if set(overlay) != {ENTRY} or ENTRY not in retained or retained[ENTRY] == overlay[ENTRY]:
raise ValueError("Expected one replacement class in an existing JarvisConnector JAR")
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
with TemporaryDirectory() as temporary:
current, donor, merged = (Path(temporary) / name for name in ("current.class", "donor.class", "merged.class"))
current.write_bytes(retained[ENTRY])
donor.write_bytes(overlay[ENTRY])
subprocess.run([
java,
"--add-exports=java.base/jdk.internal.org.objectweb.asm=ALL-UNNAMED",
"--add-exports=java.base/jdk.internal.org.objectweb.asm.tree=ALL-UNNAMED",
str(Path(__file__).with_name("WorkstationOriginMerge.java")),
str(current), str(donor), 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__)
sub = parser.add_subparsers(dest="action", required=True)
pack = sub.add_parser("extract")
pack.add_argument("built", type=Path)
pack.add_argument("output", type=Path)
merge = sub.add_parser("assemble")
merge.add_argument("baseline", type=Path)
merge.add_argument("baseline_sha")
merge.add_argument("patch", type=Path)
merge.add_argument("patch_sha")
merge.add_argument("output", type=Path)
args = vars(parser.parse_args())
(extract if args.pop("action") == "extract" else assemble)(**args)