"""Preserve the existing Atelier menus/quests; adapt only receipt metadata rendering.""" from pathlib import Path import hashlib import copy import os import sys import zipfile BASELINE = 'b0e640901419eb3e544ae0e60b52867f927341246990a2f5cc67bc1a4dc86bf8' CLASSES = {'com/dunerealms/atelier/' + name + '.class' for name in ['DeliveryStore', 'DeliveryStore$Design', 'DeliveryStore$Pending', 'KitFactory']} def install(artifact, apply=False): target = Path('/opt/dunerealms/lobby/plugins/DuneAtelier-1.0.0.jar') if hashlib.sha256(target.read_bytes()).hexdigest() != BASELINE: raise RuntimeError('Existing Atelier changed; no files replaced') with zipfile.ZipFile(artifact) as patch: entries = {n: patch.read(n) for n in patch.namelist() if not n.endswith('/') and n != 'META-INF/MANIFEST.MF'} if set(entries) != CLASSES or any(not v.startswith(b'\xca\xfe\xba\xbe') for v in entries.values()): raise RuntimeError('Unexpected compatibility artifact') with zipfile.ZipFile(target) as original: descriptor = original.read('plugin.yml').decode() if 'softdepend:' in descriptor or 'version: 1.0.0\n' not in descriptor: raise RuntimeError('Unexpected Atelier descriptor') descriptor = descriptor.replace('version: 1.0.0\n', 'version: 1.0.0-kit3\n') + '\nsoftdepend: [DuneKits]\n' if not apply: print('Plan: four compatibility classes and load order; existing menus, quests and transaction code retained') return temporary = target.with_suffix('.kit-v3-new') try: with zipfile.ZipFile(temporary, 'w', zipfile.ZIP_DEFLATED) as output: for item in original.infolist(): output.writestr(copy.copy(item), descriptor.encode() if item.filename == 'plugin.yml' else entries.get(item.filename, original.read(item.filename))) with zipfile.ZipFile(temporary) as check: assert check.testzip() is None assert all(check.read(name) == original.read(name) for name in original.namelist() if name not in CLASSES | {'plugin.yml'}) status = target.stat() os.chmod(temporary, status.st_mode & 0o777) os.chown(temporary, status.st_uid, status.st_gid) os.replace(temporary, target) finally: temporary.unlink(missing_ok=True) print('Installed Atelier metadata compatibility; all other classes are unchanged') if __name__ == '__main__': install(Path(sys.argv[1]), '--apply' in sys.argv)