plugins/tools/plugin_supplied_item_art.py
unimatrixgit c9944bb832
Some checks failed
DuneRealms Plugin CI / plugins (push) Failing after 8m59s
Supplied Dune Item Art Build / supplied-item-art (push) Successful in 30s
Keep supplied source text in canonical Git line endings
2026-12-03 20:52:03 +01:00

57 lines
2.7 KiB
Python

"""Install only the supplied item sprites into the reviewed live pack variants."""
import argparse
import hashlib
import json
from pathlib import Path
import re
from zipfile import ZIP_DEFLATED, ZipFile, ZipInfo
BASE = {
'arrakis': 'c15a074da8edcf1404f16fd284aa0d12d16104743ce99e5702dec48370b87b4d',
'fleet': '25ae69ae7198002ebff65b1eafbb454be7df235f51cdc46bb6233aa0b3ceec7a',
}
ITEM = re.compile(r'assets/(minecraft|dunerealms)/textures/item/[a-z0-9_./-]+[.]png\Z')
def read_pack(path):
with ZipFile(path) as archive:
files = [entry for entry in archive.namelist() if not entry.endswith('/')]
if len(set(files)) != len(files):
raise ValueError('Duplicate resource path')
return {name: archive.read(name) for name in files}
def assemble(variant, baseline, source, pins_file, output):
if hashlib.sha256(baseline.read_bytes()).hexdigest() != BASE[variant]:
raise ValueError('Unexpected live pack checksum')
pins = json.loads(pins_file.read_bytes())
if not pins or any(not ITEM.fullmatch(name) or '..' in name.split('/') for name in pins):
raise ValueError('Expected only reviewed item PNG paths')
retained = read_pack(baseline)
generated = read_pack(source)
if not pins.keys() <= retained.keys() & generated.keys():
raise ValueError('Reviewed item art is missing')
updates = {key: generated[key] for key in pins}
if any(hashlib.sha256(data).hexdigest() != pins[key] for key, data in updates.items()):
raise ValueError('Supplied item artwork differs from the reviewed inputs')
updated = retained | updates
with ZipFile(output, 'x', ZIP_DEFLATED, compresslevel=9) as archive:
for name, data in sorted(updated.items()):
entry = ZipInfo(name, (2026, 11, 29, 0, 0, 0))
entry.create_system = 0
archive.writestr(entry, data, compress_type=ZIP_DEFLATED, compresslevel=9)
if read_pack(output) != updated:
raise ValueError('Output differs from the reviewed item-only transition')
payload = output.read_bytes()
return {'filename': output.name, 'variant': variant, 'bytes': len(payload),
'sha1': hashlib.sha1(payload).hexdigest(), 'sha256': hashlib.sha256(payload).hexdigest(),
'changed': sum(retained[key] != data for key, data in updates.items())}
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('variant', choices=BASE)
for field in ('baseline', 'source', 'pins', 'output'):
parser.add_argument(field, type=Path)
args = parser.parse_args()
print(json.dumps(assemble(args.variant, args.baseline, args.source, args.pins, args.output), indent=2))