plugins/tools/cooking_beta_artwork.py

54 lines
3.8 KiB
Python

"""Append the 99 reviewed cooking sprites; replace only the requested matte stone texture."""
import argparse,hashlib,json,struct
from pathlib import Path
from plugin_wild_update import read_archive,write_archive,digest
NETWORK='cb13b75c44d8158f1ec772c76beff14dfe14645ebaa4c30f7b6dba929be946b8'
MENUS={'02ffea4ec37f0e1c6d08581b47ac0b0d7ae2ccc9d4166b72c2e4e04ba03aff7e','149047b7be7068d8dbdc3cc917a8599867acd373d63414f5c47463fd27f26381'}
STONE='assets/minecraft/textures/block/stone.png'
STONE_SHA='7b137ffe5074b7e9ce137cc1a435f157f82dcbbe902c5a491227ee67f9a9845b'
def artwork(source):
root=source/'network/resource-pack/cooking-beta';overlay=root/'overlay'
rows=json.loads((source/'network/content-assets/cookbook/regional-cooking.json').read_text(encoding='utf-8'))['items']
hashes=json.loads((root/'artwork-manifest.json').read_text(encoding='utf-8'))
if len(rows)!=99 or len({r['id']for r in rows})!=99:raise ValueError('Expected99 unique authored items')
payload={}
for row in rows:
ident=row['id'];key='cooking/'+ident
if row['model']!='dunerealms:'+key:raise ValueError('Catalogue model mismatch')
item='assets/dunerealms/items/'+key+'.json';model='assets/dunerealms/models/item/'+key+'.json';texture='assets/dunerealms/textures/item/'+key+'.png'
entry={n:(overlay/n).read_bytes()for n in [item,model,texture]}
if json.loads(entry[item])!={'model':{'type':'minecraft:model','model':'dunerealms:item/'+key}}:raise ValueError('Invalid item model '+ident)
if json.loads(entry[model])!={'parent':'minecraft:item/generated','textures':{'layer0':'dunerealms:item/'+key}}:raise ValueError('Invalid texture binding '+ident)
png=entry[texture]
if png[:16]!=b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR' or struct.unpack('>II',png[16:24])!=(32,32)or png[25]!=6:raise ValueError('Expected32px RGBA sprite '+ident)
if digest(png)!=hashes[ident+'.png']['sha256']:raise ValueError('Artwork hash mismatch '+ident)
payload.update(entry)
if {p.relative_to(overlay).as_posix()for p in overlay.rglob('*')if p.is_file()}!=set(payload):raise ValueError('Unreviewed artwork path')
return payload
def assemble(source,baseline,output,mode):
old=baseline.read_bytes();sha=digest(old)
if mode=='network'and sha!=NETWORK or mode=='menus'and sha not in MENUS:raise ValueError('Unexpected current pack/cache preimage')
base=read_archive(old);prefix='menu-art/pack/'if mode=='menus'else''
added={prefix+n:b for n,b in artwork(source).items()}
if set(added)&set(base):raise ValueError('Cooking art already present or collision')
stone=(source/'network/resource-pack/realistic-stone-family'/STONE).read_bytes()
if digest(stone)!=STONE_SHA:raise ValueError('Unreviewed stone texture')
key=prefix+STONE
if mode=='network'and key not in base:raise ValueError('Existing stone missing')
final=base|added|{key:stone}
data=write_archive(final)
if output.resolve()==baseline.resolve():raise ValueError('Never overwrite source archive')
output.write_bytes(data)
actual=read_archive(data)
changed={n for n in base if actual[n]!=base[n]}
if changed-{key}or set(base)-set(actual)or set(actual)-set(base)!=set(added)|({key}-set(base)):raise ValueError('Unexpected archive delta')
proof={'baselineSha256':sha,'sha256':digest(data),'sha1':hashlib.sha1(data).hexdigest(),'bytes':len(data),'added':sorted(set(actual)-set(base)),'changed':sorted(changed),'items':99}
output.with_suffix(output.suffix+'.proof.json').write_text(json.dumps(proof,indent=2)+'\n',encoding='utf-8')
return proof
if __name__=='__main__':
p=argparse.ArgumentParser(description=__doc__);p.add_argument('source',type=Path);p.add_argument('baseline',type=Path);p.add_argument('output',type=Path);p.add_argument('mode',choices=['network','menus']);a=p.parse_args()
r=assemble(a.source,a.baseline,a.output,a.mode);print(json.dumps({k:v if not isinstance(v,list)else len(v)for k,v in r.items()}))