"""Split all remapped variants into version payloads and one shared library JAR.""" import contextlib import shutil import tempfile import zipfile from pathlib import Path ROOT = Path(__file__).resolve().parents[1] VERSIONS = tuple(f"1.21.{patch}" for patch in range(1, 12)) def pack(inputs, destination): """Keep game classes version-local; share only byte-identical libraries/assets.""" with contextlib.ExitStack() as stack: jars = {version: stack.enter_context(zipfile.ZipFile(path)) for version, path in inputs.items()} for version, jar in jars.items(): actual = jar.read("prestige-engine/minecraft-version.txt").decode().strip() if actual != version: raise ValueError(f"{version}: payload targets {actual}") first = next(iter(jars.values())) candidates = {name for name in first.namelist() if not name.endswith('/') and not name.startswith(( 'dev/zprestige/', 'prestige-engine/', 'META-INF/')) and name != 'fabric.mod.json' and not name.endswith(( '.mixins.json', '.accesswidener'))} shared = {name: first.read(name) for name in sorted(candidates) if all(name in jar.namelist() and jar.read(name) == first.read(name) for jar in jars.values())} destination.mkdir(parents=True, exist_ok=True) with zipfile.ZipFile(destination / 'common.jar', 'w', zipfile.ZIP_DEFLATED) as common: for name, data in shared.items(): common.writestr(name, data) for version, jar in jars.items(): with zipfile.ZipFile(destination / f'{version}.jar', 'w', zipfile.ZIP_DEFLATED) as payload: for name in sorted(jar.namelist()): if name not in shared and not name.endswith('/'): payload.writestr(name, jar.read(name)) (destination / 'versions.txt').write_text('\n'.join(inputs) + '\n', encoding='utf-8') def main(): inputs = {} for version in VERSIONS: candidates = [p for p in (ROOT / 'versions' / version / 'build/libs').glob('prestige-*.jar') if not p.name.endswith(('-sources.jar', '-dev.jar'))] if len(candidates) != 1: raise ValueError(f'{version}: expected one remapped JAR, found {len(candidates)}; build all variants first') inputs[version] = candidates[0] # Stage only after every input is present. Never publish an incomplete version matrix. (ROOT / 'build').mkdir(parents=True, exist_ok=True) with tempfile.TemporaryDirectory(dir=ROOT / 'build') as staging: pack(inputs, Path(staging)) output = ROOT / 'build/bundle' output.mkdir(parents=True, exist_ok=True) for path in Path(staging).iterdir(): shutil.copy2(path, output / path.name) print(f'Packed {len(inputs)} versions with common.jar in {output}') if __name__ == '__main__': main()