"""Assemble canonical ore code with each hash-pinned retained planet archive.""" import argparse import hashlib import json from pathlib import Path import subprocess import tempfile from zipfile import ZIP_DEFLATED, ZipFile PLANETS = { 'arrakis': '8ae685ebe3272f9a6be2928ecf2421b1892b9c4d3a40ed8efe1c22a57d1bd7b8', 'caladan': 'de18104b28bd5aadeec139484b8627b961325a095d512e96561a87f9c9c54619', 'giedi_prime': 'e853921b730a45edd32899fa614d0b60e09b41e478d7610520473b5b402e72f9', 'kaitain': '263b9a704245dc810b239264315c3d90813f4fd5daac2437052580916021b1ac', 'sigma_draconis_iv': '73424945f1206a7bfee4308ef1a91deff0fa8356166ded6f32dd6ce49179ec16', } PACKAGE = 'com/example/duneterrain/' GENERATOR = PACKAGE + 'DuneTerrain$PlanetChunkGenerator.class' SAMPLER = {PACKAGE + suffix for suffix in ('OreHeights.class', 'OreHeights$Band.class')} SIGMA_PACK = '37544b795b053b3388f5335ad18db39a54fad0f484a8a4a76bbc850c689a7bfa' SIGMA_BIOME = 'data/dunerealms/worldgen/biome/frozen_desert.json' NATIVE_ORES = { 'coal_upper', 'coal_lower', 'iron_upper', 'iron_middle', 'iron_small', 'gold', 'gold_lower', 'redstone', 'redstone_lower', 'diamond', 'diamond_medium', 'diamond_large', 'diamond_buried', 'lapis', 'lapis_buried', 'copper', } def sha(path): return hashlib.sha256(path.read_bytes()).hexdigest() def archive_names(archive): entries = archive.namelist() if len(set(entries)) != len(entries): raise ValueError('Duplicate ZIP entry') return set(entries) def extract(compiled, output): with ZipFile(compiled) as jar, ZipFile(output, 'x', ZIP_DEFLATED) as patch: archive_names(jar) for name in sorted(SAMPLER): patch.writestr(name, jar.read(name)) def assemble(planet, baseline, bundle, output, java): if sha(baseline) != PLANETS[planet]: raise ValueError('Unexpected retained terrain archive for ' + planet) with ZipFile(baseline) as original, ZipFile(bundle / 'ore-heights.zip') as patch: if SAMPLER & archive_names(original) or archive_names(patch) != SAMPLER: raise ValueError('Unexpected ore sampler entries') with tempfile.TemporaryDirectory() as tmp: previous, candidate = Path(tmp) / 'previous.class', Path(tmp) / 'candidate.class' previous.write_bytes(original.read(GENERATOR)) subprocess.run([java, str(bundle / 'PlanetOreTransition.java'), str(previous), str(candidate)], check=True) generator = candidate.read_bytes() with ZipFile(output, 'x', ZIP_DEFLATED) as result: for entry in original.infolist(): result.writestr(entry, generator if entry.filename == GENERATOR else original.read(entry)) for name in sorted(SAMPLER): result.writestr(name, patch.read(name)) def verify_terrain(baseline, candidate, bundle): with ZipFile(baseline) as original, ZipFile(candidate) as incoming, ZipFile(bundle / 'ore-heights.zip') as patch: names = archive_names(original) if archive_names(patch) != SAMPLER or archive_names(incoming) != names | SAMPLER: raise ValueError('Unexpected assembled Terrain archive entries') if incoming.read(GENERATOR) == original.read(GENERATOR): raise ValueError('Planet ore method was not updated') for name in names - {GENERATOR}: if incoming.read(name) != original.read(name): raise ValueError(f'Unrelated Terrain entry changed: {name}') for name in SAMPLER: if incoming.read(name) != patch.read(name): raise ValueError(f'Ore class differs from the canonical build: {name}') def sigma_pack(baseline, reviewed, output): if sha(baseline) != SIGMA_PACK: raise ValueError('Unexpected retained Sigma biome pack') with ZipFile(baseline) as original: archive_names(original) expected = json.loads(original.read(SIGMA_BIOME)) features = {f'minecraft:ore_{name}' for name in NATIVE_ORES} removed = set() for layer in expected['features']: removed.update(features.intersection(layer)) layer[:] = [name for name in layer if name not in features] if removed != features or expected != json.loads(reviewed.read_bytes()): raise ValueError('Sigma biome patch changes more than the duplicated ore list') with ZipFile(output, 'x', ZIP_DEFLATED) as result: for entry in original.infolist(): result.writestr(entry, reviewed.read_bytes() if entry.filename == SIGMA_BIOME else original.read(entry)) if __name__ == '__main__': parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('mode', choices=['extract', 'sigma-pack', *PLANETS]) parser.add_argument('input', type=Path) parser.add_argument('output', type=Path) parser.add_argument('--bundle', type=Path) parser.add_argument('--java', default='java') args = parser.parse_args() if args.mode == 'extract': extract(args.input, args.output) elif args.mode == 'sigma-pack': sigma_pack(args.input, args.bundle / 'frozen_desert.json', args.output) else: assemble(args.mode, args.input, args.bundle, args.output, args.java)