plugins/tools/plugin_menu_pack.py

59 lines
3.2 KiB
Python

"""Shared immutable client-pack verification for built publisher/installer tools."""
import hashlib
import json
import re
from pathlib import Path
import urllib.request
import zipfile
def sha(path, algorithm='sha256'):
return hashlib.new(algorithm, Path(path).read_bytes()).hexdigest()
def load_menu(artifacts, source):
if not re.fullmatch(r'[a-f0-9]{40}', source):
raise ValueError('Menu source must be an exact commit SHA')
root = Path(artifacts)
unified=(root / ('DuneRealms-Resources-'+source+'.manifest.json')).is_file()
name = ('DuneRealms-Resources-' if unified else 'DuneRealms-Menu-') + source + '.zip'
data = json.loads((root / name.replace('.zip', '.manifest.json')).read_text())
path = root / name
if data.get('sourceCommit') != source or data.get('filename') != name or (not unified and not data.get('stacked')):
raise ValueError('Menu pack source/filename/stacking contract differs')
if unified and (not data.get('unified') or data.get('delivery')!='server' or data.get('packId')!='c24c7cf5-a292-539d-971f-a96d8b838162'):
raise ValueError('Unified pack identity/delivery contract differs')
if sha(path) != data['sha256'] or sha(path, 'sha1') != data['sha1']:
raise ValueError('Built menu pack checksum differs')
with zipfile.ZipFile(path) as archive:
names = archive.namelist()
if 'pack.mcmeta' not in names or not any(n.startswith('uiengine_26_2/assets/minecraft/shaders/') for n in names):
raise ValueError('Menu pack lacks client root/26.2 transport')
if not unified and any('/textures/block/' in n or '/textures/entity/' in n for n in names):
raise ValueError('Menu pack would replace world textures')
return path, data
def menu_url(source, unified=False):
return 'https://www.dunerealms.com/downloads/' + ('DuneRealms-Resources-' if unified else 'DuneRealms-Menu-') + source + '.zip'
def verify_public_menu(url, data):
if url != menu_url(data['sourceCommit'],data.get('unified',False)):
raise ValueError('Menu URL must be the exact immutable built release URL')
with urllib.request.urlopen(url, timeout=30) as response:
if response.geturl() != url:
raise ValueError('Menu publication must not redirect')
payload = response.read(data['bytes'] + 1)
if len(payload) != data['bytes'] or hashlib.sha256(payload).hexdigest() != data['sha256']:
raise ValueError('Published menu pack bytes differ from CI build')
def config_payloads(data):
url = menu_url(data['sourceCommit'],data.get('unified',False))
marker = '# Managed by DuneRealms plugin CI; immutable menu overlay.\n'
engine = marker + 'telemetry: false\nupdate-check: false\nallow-web: false\nresource-pack:\n mode: external\n public-url: ""\n bind: 127.0.0.1\n port: 0\n'
pin = marker + ''.join(key + ': ' + json.dumps(value) + '\n' for key, value in (
('url', url), ('sha1', data['sha1']), ('sha256', data['sha256']), ('source-commit', data['sourceCommit'])))
if data.get('unified'):pin+='delivery: server\npack-id: '+json.dumps(data['packId'])+'\n'
return {'MCGUI/config.yml': engine, 'DunerealmsGUI/menu-pack.yml': pin}