plugins/tools/plugin_pillager_abi.py
unimatrixgit 02e07b87e5
Some checks failed
DuneRealms Resourcepack CI / resourcepack (push) Successful in 1m51s
DuneRealms Plugin CI / plugins (push) Has been cancelled
fix: retain installed native NPC lifecycle and pin economy implementations
2026-11-29 16:36:10 +01:00

104 lines
5.6 KiB
Python

"""Read-only public JVM compatibility checks for the bounded Pillager release."""
import argparse
import hashlib
import json
from pathlib import Path
import sys
import zipfile
sys.path.insert(0,str(Path(__file__).resolve().parent))
from plugin_backend_abi import jar_classes
REVIEWED_TERRAIN_SHA256='b0777941e5c320e16731e447101b4157fb8af917951fc0e3128c4e6b73710b4f'
def verify_contract(path,fixture,require_hash=False):
if require_hash and hashlib.sha256(Path(path).read_bytes()).hexdigest()!=fixture['sha256']:
raise ValueError('Installed vendor runtime is not the reviewed input')
with zipfile.ZipFile(path) as archive:
for name,expected in fixture.get('unchangedClassSha256',{}).items():
if hashlib.sha256(archive.read(name)).hexdigest()!=expected:
raise ValueError('Retained implementation changed: '+name)
classes=jar_classes(path)
count=0
for name,expected in fixture['classes'].items():
actual=classes.get(name)
if actual is None:raise ValueError('Missing retained public class: '+name)
for kind in ('methods','fields'):
for member in expected.get(kind,[]):
if member not in actual[kind]:raise ValueError('Retained public ABI changed: '+name+'.'+member[0]+member[1])
count+=1
return {'classes':len(fixture['classes']),'descriptors':count,'sha256':hashlib.sha256(Path(path).read_bytes()).hexdigest()}
def verify(jarvis,mf,artifacts,worldguard=None):
folder=Path(artifacts)
result={
'jarvis':verify_contract(jarvis,json.loads((folder/'retained-jarvis-public-abi.json').read_text())),
'medievalFactions':verify_contract(mf,json.loads((folder/'medieval-factions-11.1-raid-api.json').read_text()),True),
}
if worldguard is not None:result['worldGuard']=verify_contract(worldguard,json.loads((folder/'worldguard-raid-api.json').read_text()),True)
return result
def verify_menu_resources(incoming,installed):
allowed={
'plugin.yml',
'menu-art/pack/assets/dunerealms/items/pillager/goldmembers_shovel_mk8.json',
'menu-art/pack/assets/dunerealms/models/item/pillager/goldmembers_shovel_mk8.json',
'menu-art/pack/assets/dunerealms/textures/item/pillager/goldmembers_shovel_mk8.png',
}
with zipfile.ZipFile(incoming) as new,zipfile.ZipFile(installed) as old:
before={n:old.read(n) for n in old.namelist() if not n.endswith('/')}
after={n:new.read(n) for n in new.namelist() if not n.endswith('/')}
changed={n for n in set(before)|set(after) if before.get(n)!=after.get(n)}
if changed-allowed or set(before)-set(after):
raise ValueError('Pillager menu update may contain only its approved item-art/version resources: '+', '.join(sorted(changed-allowed)))
classes={n for n in before if n.endswith('.class')}
if not classes or classes!={n for n in after if n.endswith('.class')}:
raise ValueError('Accepted menu runtime classes are missing or changed')
return {'identicalClasses':len(classes),'resourceChanges':sorted(changed)}
def verify_accepted_frontends(menu,gui,fixture):
if hashlib.sha256(Path(gui).read_bytes()).hexdigest()!=fixture['guiSha256']:
raise ValueError('Accepted GUI binary changed')
result=verify_menu_contract(menu,fixture)
return dict(result,guiSha256=fixture['guiSha256'])
def verify_menu_contract(menu,fixture):
with zipfile.ZipFile(menu) as jar:entries={n:jar.read(n) for n in jar.namelist() if not n.endswith('/')}
classes={n:hashlib.sha256(b).hexdigest() for n,b in sorted(entries.items()) if n.endswith('.class')}
resources=[n+'\0'+hashlib.sha256(b).hexdigest() for n,b in sorted(entries.items()) if not n.endswith('.class') and n not in fixture['allowedResourceChanges']]
if classes!=fixture['classes'] or hashlib.sha256('\n'.join(resources).encode()).hexdigest()!=fixture['unchangedResourceTreeSha256']:
raise ValueError('Accepted menu classes or unrelated artwork/config resources changed')
return {'identicalClasses':len(classes),'unchangedResources':len(resources)}
def archive_content_hash(path):
with zipfile.ZipFile(path) as jar:rows=[n+'\0'+hashlib.sha256(jar.read(n)).hexdigest() for n in sorted(jar.namelist()) if not n.endswith('/')]
return hashlib.sha256('\n'.join(rows).encode()).hexdigest()
def verify_terrain(path,fixture):
result=verify_contract(path,fixture)
classes=jar_classes(path)
if ['isGeneratedSpiceDeposit','(Lorg/bukkit/block/Block;)Z'] not in classes['com/example/duneterrain/DuneTerrain']['methods']:
raise ValueError('Terrain does not expose the reviewed provenance API')
with zipfile.ZipFile(path) as archive:
for name,expected in fixture['resources'].items():
payload=archive.read(name)
if isinstance(expected,dict):payload=json.dumps(json.loads(payload),sort_keys=True,separators=(',',':')).encode();expected=expected['canonicalSha256']
if hashlib.sha256(payload).hexdigest()!=expected:raise ValueError('Retained Terrain gameplay resource changed: '+name)
return result
if __name__=='__main__':
parser=argparse.ArgumentParser(description=__doc__)
parser.add_argument('jarvis',type=Path)
parser.add_argument('--mf',type=Path)
parser.add_argument('--worldguard',type=Path)
parser.add_argument('--fixtures',type=Path,default=Path(__file__).resolve().parents[1]/'JarvisConnector/src/test/resources')
args=parser.parse_args()
if args.mf:result=verify(args.jarvis,args.mf,args.fixtures,args.worldguard)
else:result=verify_contract(args.jarvis,json.loads((args.fixtures/'retained-jarvis-public-abi.json').read_text()))
print(json.dumps(result,indent=2))