80 lines
3.4 KiB
Python
80 lines
3.4 KiB
Python
"""Build exact station-origin and Essentials door overlays against retained JARs."""
|
|
import argparse
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
from tempfile import TemporaryDirectory
|
|
|
|
from plugin_wild_update import digest, read_archive, write_archive
|
|
|
|
JARVIS = "com/dunerealms/jarvis/"
|
|
ESS = "com/example/duneess/"
|
|
SCOPES = {
|
|
"station": (
|
|
tuple(JARVIS + name for name in ("WorkstationOrigins", "ItemOrigin", "ArrakisBrewing", "ItemOriginRemainders")),
|
|
(JARVIS + "ComposterOrigins",),
|
|
{
|
|
"86af7e1b6127dbb577cf9223b9b850a613c1a56e42fcfcac53d4dbad367b2703",
|
|
"8facb13e8049a73b8072eb1af5053ec3255c2ae22d28b37419ad5cb34c24b31c",
|
|
"adc66adcfc1db4ead4de1f5b0d4863822158f23c0b612c669b86fcd1f9ac287a",
|
|
},
|
|
),
|
|
"ess": (
|
|
(ESS + "DuneEss",),
|
|
(ESS + "IronDoorInteraction",),
|
|
{
|
|
"35d6bc1946574159defd7e296d1f037c5d0be1ee21bc767801380ed9b7d5a214",
|
|
"2e4de7e769c47a036fa9f6c5f175d966aa38c336f5e537d60b07cdd1c267eb1e",
|
|
"fd9f633b1d0a89a265a6dd91cfd863ca7c1901652030ee521941d40eb0182954",
|
|
},
|
|
),
|
|
}
|
|
|
|
|
|
def donor_archive(role, jar):
|
|
changed, added, _ = SCOPES[role]
|
|
source = read_archive(jar.read_bytes())
|
|
return write_archive({name + ".class": source[name + ".class"] for name in (*changed, *added)})
|
|
|
|
|
|
def assemble(role, baseline, baseline_sha, donor, donor_sha, output, classpath):
|
|
changed, added, approved = SCOPES[role]
|
|
existing = baseline.read_bytes()
|
|
compiled = donor.read_bytes()
|
|
if digest(existing) != baseline_sha or baseline_sha not in approved or digest(compiled) != donor_sha:
|
|
raise ValueError("Unexpected baseline or donor checksum")
|
|
retained = read_archive(existing)
|
|
source = read_archive(compiled)
|
|
if set(source) != {name + ".class" for name in (*changed, *added)}:
|
|
raise ValueError("Unexpected source class scope")
|
|
if any(name + ".class" in retained for name in added):
|
|
raise ValueError("The retained archive already contains the new feature")
|
|
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("StationAndDoorOverlay.java")
|
|
with TemporaryDirectory() as temporary:
|
|
root = Path(temporary)
|
|
for name in changed:
|
|
path = name + ".class"
|
|
old, new, merged = (root / file for file in ("retained.class", "donor.class", "merged.class"))
|
|
old.write_bytes(retained[path])
|
|
new.write_bytes(source[path])
|
|
subprocess.run([java, "--class-path", classpath, str(helper), role, name, str(old), str(new), str(merged)], check=True)
|
|
retained[path] = merged.read_bytes()
|
|
for name in added:
|
|
retained[name + ".class"] = source[name + ".class"]
|
|
with output.open("xb") as candidate:
|
|
candidate.write(write_archive(retained))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("role", choices=SCOPES)
|
|
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()))
|

Staff