plugins/tools/plugin_builder_multiverse.py

88 lines
4 KiB
Python

"""Build the paired Lobby Core and standalone Builder updates from exact inputs."""
import argparse
import os
from pathlib import Path
import subprocess
from tempfile import TemporaryDirectory
from plugin_wild_update import digest, read_archive, write_archive
CORE_SHA = "3cc3b9a918f1691503c374a035e51ed295fedf879d2fc083ea8b1dd0b89dea0a"
BUILDER_SHA = "18f8878b18f249ff97728542a63effedb1d19f473168b9ab7ec8ef8f11c25538"
CORE = "com/example/dunerealms/"
BUILDER = "com/example/dunelobby/"
CORE_CLASSES = ["SharedDatabase.class", "DuneCore.class"]
LEASE = "SharedDatabase$BuilderLease.class"
BUILDER_CLASSES = [
"BuilderTransfer.class", "BuilderTransfer$1.class", "BuilderTransfer$VoidTerrain.class",
"BuilderTransfer$SharedBuild.class", "BuilderTransfer$PendingTransfer.class",
"BuilderSharedProfiles.class", "BuilderMultiverse.class", "LegacyBuilderPlugin.class",
]
def checked_archive(file, sha):
data = file.read_bytes()
if digest(data) != sha:
raise ValueError(f"Unexpected checksum for {file.name}")
return read_archive(data)
def assemble(core, builder, core_donor, core_donor_sha, staff_donor, staff_donor_sha, output, classpath):
retained_core = checked_archive(core, CORE_SHA)
retained_builder = checked_archive(builder, BUILDER_SHA)
compiled_core = checked_archive(core_donor, core_donor_sha)
compiled_staff = checked_archive(staff_donor, staff_donor_sha)
if CORE + LEASE in retained_core:
raise ValueError("Retained Core already contains a Builder lease")
if set(name for name in retained_builder if name.startswith(BUILDER) and name.endswith(".class")) != {
BUILDER + "BuilderTransfer.class", BUILDER + "BuilderTransfer$VoidTerrain.class"
}:
raise ValueError("Unexpected classes in retained Builder")
keys = [CORE + name for name in CORE_CLASSES]
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("BuilderProfileCoreOverlay.java")
with TemporaryDirectory() as temporary:
root = Path(temporary)
files = []
for source, prefix in ((retained_core, "retained-"), (compiled_core, "donor-")):
for name in keys:
file = root / (prefix + Path(name).name)
file.write_bytes(source[name])
files.append(str(file))
subprocess.run([java, "--class-path", classpath, str(helper), *files, str(root)], check=True)
for name in keys:
retained_core[name] = (root / Path(name).name).read_bytes()
retained_core[CORE + LEASE] = compiled_core[CORE + LEASE]
descriptor = retained_builder["plugin.yml"]
main = b"main: com.example.dunelobby.BuilderTransfer"
dependency = b"depend: [WorldEdit]"
if descriptor.count(main) != 1 or descriptor.count(dependency) != 1 or b"softdepend:" in descriptor:
raise ValueError("Unexpected retained Builder command descriptor")
line = b"\r\n" if b"\r\n" in descriptor else b"\n"
retained_builder["plugin.yml"] = descriptor.replace(main, b"main: com.example.dunelobby.LegacyBuilderPlugin").replace(
dependency, dependency + line + b"softdepend: [DuneCore, Multiverse-Core]"
)
for name in BUILDER_CLASSES:
retained_builder[BUILDER + name] = compiled_staff[BUILDER + name]
output.mkdir()
(output / core.name).write_bytes(write_archive(retained_core))
(output / builder.name).write_bytes(write_archive(retained_builder))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("core", type=Path)
parser.add_argument("builder", type=Path)
parser.add_argument("core_donor", type=Path)
parser.add_argument("core_donor_sha")
parser.add_argument("staff_donor", type=Path)
parser.add_argument("staff_donor_sha")
parser.add_argument("output", type=Path)
parser.add_argument("classpath", help="ASM core and tree JARs")
assemble(**vars(parser.parse_args()))