plugins/tools/plugin_menu_pack.py

55 lines
2.6 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')
name = 'DuneRealms-Menu-' + source + '.zip'
root = Path(artifacts)
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 data.get('stacked'):
raise ValueError('Menu pack source/filename/stacking 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 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):
return 'https://www.dunerealms.com/downloads/DuneRealms-Menu-' + source + '.zip'
def verify_public_menu(url, data):
if url != menu_url(data['sourceCommit']):
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'])
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'])))
return {'MCGUI/config.yml': engine, 'DunerealmsGUI/menu-pack.yml': pin}