93 lines
4.6 KiB
Python
93 lines
4.6 KiB
Python
"""Complete the Sigma primary overworld ore palette without replacing Terrain."""
|
|
|
|
import argparse
|
|
import importlib.util
|
|
import json
|
|
from pathlib import Path
|
|
import re
|
|
import tempfile
|
|
from zipfile import ZIP_DEFLATED, ZipFile, ZipInfo
|
|
|
|
from planet_ore_release import NATIVE_ORES, archive_names, sha
|
|
|
|
|
|
spec = importlib.util.spec_from_file_location('reset_planet_ores', Path(__file__).with_name('reset-planet-ores.py'))
|
|
reset = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(reset)
|
|
|
|
PLANET = 'sigma_draconis_iv'
|
|
UNIT = f'dune-{PLANET}.service'
|
|
TERRAIN = 'c1a564402a9cfea8be942d4945cdc55af36b7ffb441707ce683a177ab397494e'
|
|
PACK = 'e44c703a480916190c9ce9c22d8c2a4bb03822e95cfb02cf85bc88e825e2f3a5'
|
|
BIOMES = ('badlands.json', 'eroded_badlands.json', 'sulfur_caves.json')
|
|
PATHS = {name: 'data/minecraft/worldgen/biome/' + name for name in BIOMES}
|
|
ASSETS = {*BIOMES, 'planet_ore_release.py', 'reset-planet-ores.py', 'reset-sigma-ore-pack.py', 'SOURCE_COMMIT'}
|
|
|
|
|
|
def build_pack(previous, bundle, output):
|
|
if sha(previous) != PACK:
|
|
raise ValueError('Expected the exact previously installed Sigma biome pack')
|
|
features = {f'minecraft:ore_{name}' for name in NATIVE_ORES | {'gold_extra'}}
|
|
with ZipFile(previous) as source:
|
|
names = archive_names(source)
|
|
if names.intersection(PATHS.values()):
|
|
raise ValueError('Expected these three biome definitions to be new')
|
|
with ZipFile(output, 'x', ZIP_DEFLATED) as result:
|
|
for entry in source.infolist():
|
|
result.writestr(entry, source.read(entry))
|
|
for name, internal in PATHS.items():
|
|
data = (bundle / name).read_bytes()
|
|
biome = json.loads(data)
|
|
if features.intersection(feature for layer in biome['features'] for feature in layer):
|
|
raise ValueError(f'Native mineral feature remains in {name}')
|
|
info = ZipInfo(internal)
|
|
info.compress_type = ZIP_DEFLATED
|
|
result.writestr(info, data)
|
|
|
|
|
|
def run(bundle, source, apply, receipt):
|
|
reset.verify_bundle(reset.checked(bundle), source, ASSETS)
|
|
root = reset.checked(reset.ROOT / PLANET)
|
|
if (reset.state(UNIT) != 'active' or
|
|
reset.service('show', UNIT, '--property=WorkingDirectory', '--value') != str(root)):
|
|
raise ValueError('Unexpected Sigma service or working directory')
|
|
before = reset.snapshot_server(root)
|
|
if (before['settings'].get('level-name') != PLANET or
|
|
sum(details[0] == TERRAIN for details in before['jars'].values()) != 1):
|
|
raise ValueError('Expected Sigma primary world and exact installed ore Terrain')
|
|
if not re.search(rf'^\s+generator:\s*[\'\"]?DuneTerrain:{PLANET}[\'\"]?\s*$', (root / 'bukkit.yml').read_text(), re.M):
|
|
raise ValueError('Expected the current Sigma DuneTerrain generator')
|
|
world = reset.checked(root / PLANET)
|
|
dimension = reset.checked(world / reset.DIMENSION)
|
|
identity = {name: reset.fingerprint(dimension / name) for name in reset.IDENTITY}
|
|
target = world / reset.PACK
|
|
previous = {target: reset.fingerprint(target)}
|
|
with tempfile.TemporaryDirectory() as temp:
|
|
candidate = Path(temp) / 'sigma-biomes.zip'
|
|
build_pack(target, bundle, candidate)
|
|
report = {'source': source, 'planet': PLANET, 'world': str(world), 'unit': UNIT,
|
|
'identity': identity, 'incoming': {str(target): sha(candidate)},
|
|
'chunkFileCount': len(reset.chunk_files(dimension)), 'status': 'planned'}
|
|
if not apply:
|
|
return report
|
|
return reset.apply_transition(root, world, dimension, UNIT, before, identity, previous,
|
|
{target: candidate}, report, receipt)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('--bundle', required=True, type=Path)
|
|
parser.add_argument('--expected-source', required=True)
|
|
parser.add_argument('--apply-reset-no-backup', action='store_true')
|
|
parser.add_argument('--receipt', type=Path)
|
|
args = parser.parse_args()
|
|
if args.apply_reset_no_backup != (args.receipt is not None):
|
|
parser.error('An apply requires a new private receipt path')
|
|
if args.apply_reset_no_backup:
|
|
import fcntl
|
|
with Path('/run/lock/dunerealms-planet-ore-reset.lock').open('a') as lock:
|
|
fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
|
result = run(args.bundle, args.expected_source, True, args.receipt)
|
|
else:
|
|
result = run(args.bundle, args.expected_source, False, None)
|
|
print(json.dumps(result, indent=2))
|

Staff