plugins/tools/plugin_lobby_food.py
unimatrixgit ac64d0d5f2
Some checks failed
Lobby Food Startup Build / lobby-food (push) Successful in 44s
DuneRealms Plugin CI / plugins (push) Failing after 2m11s
Keep Arrakis-only food registration out of Lobby startup
2026-12-01 18:48:04 +01:00

52 lines
2 KiB
Python

import argparse
import hashlib
from pathlib import Path
from zipfile import ZIP_DEFLATED, ZipFile
BASELINE = '5c632e3db85fc58e55c1917b29916ef1865b3c9eca3e3e70b9131ef44dad1aaf'
CLASSES = tuple(f'com/dunerealms/jarvis/{name}.class'
for name in ('ArrakisBrewing', 'SurvivalFoodRecipes'))
def unique_names(archive):
names = archive.namelist()
if len(names) != len(set(names)):
raise ValueError('Duplicate archive entry')
return set(names)
def extract(compiled, output):
with ZipFile(compiled) as jar, ZipFile(output, 'x', ZIP_DEFLATED) as patch:
unique_names(jar)
for name in CLASSES:
patch.writestr(name, jar.read(name))
def assemble(baseline, patch, output):
if hashlib.sha256(baseline.read_bytes()).hexdigest() != BASELINE:
raise ValueError('Expected the exact retained Lobby JARVIS baseline')
with ZipFile(baseline) as original, ZipFile(patch) as updates:
names = unique_names(original)
if unique_names(updates) != set(CLASSES) or not set(CLASSES) <= names:
raise ValueError('Expected only the two reviewed Lobby recipe listeners')
replacements = {name: updates.read(name) for name in CLASSES}
if any(not data.startswith(b'\xca\xfe\xba\xbe') or data == original.read(name)
for name, data in replacements.items()):
raise ValueError('Invalid or unchanged Lobby listener class')
with ZipFile(output, 'x', ZIP_DEFLATED) as candidate:
for entry in original.infolist():
data = replacements[entry.filename] if entry.filename in replacements else original.read(entry)
candidate.writestr(entry, data)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('input', type=Path)
parser.add_argument('output', type=Path)
parser.add_argument('--baseline', type=Path)
args = parser.parse_args()
if args.baseline is None:
extract(args.input, args.output)
else:
assemble(args.baseline, args.input, args.output)