Build the scoped cooking artwork overlay with exact asset validation
This commit is contained in:
parent
0aa09e3853
commit
625deac85a
2 changed files with 85 additions and 0 deletions
54
tools/cooking_beta_artwork.py
Normal file
54
tools/cooking_beta_artwork.py
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
"""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()}))
|
||||
31
tools/test_cooking_beta_artwork.py
Normal file
31
tools/test_cooking_beta_artwork.py
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import json,sys,tempfile,unittest,shutil
|
||||
from pathlib import Path
|
||||
sys.path.insert(0,str(Path(__file__).resolve().parent))
|
||||
import cooking_beta_artwork as art
|
||||
ROOT=Path(__file__).resolve().parents[1]
|
||||
|
||||
class CookingArtworkTest(unittest.TestCase):
|
||||
def test_all99_authored_icons_have_closed_models_and_source_hashes(self):
|
||||
files=art.artwork(ROOT)
|
||||
self.assertEqual(len(files),297)
|
||||
self.assertEqual(sum(n.endswith('.png')for n in files),99)
|
||||
self.assertEqual(len({b for n,b in files.items()if n.endswith('.png')}),99)
|
||||
self.assertTrue(all(n.startswith('assets/dunerealms/')and'/cooking/'in n for n in files))
|
||||
def test_unknown_preimage_cannot_be_overwritten(self):
|
||||
with tempfile.TemporaryDirectory()as tmp:
|
||||
base=Path(tmp)/'old';base.write_bytes(b'wrong pack');out=Path(tmp)/'new'
|
||||
with self.assertRaisesRegex(ValueError,'preimage'):art.assemble(ROOT,base,out,'network')
|
||||
self.assertEqual(base.read_bytes(),b'wrong pack');self.assertFalse(out.exists())
|
||||
def test_missing_or_unreviewed_texture_is_rejected(self):
|
||||
with tempfile.TemporaryDirectory()as tmp:
|
||||
root=Path(tmp);artRoot=root/'network/resource-pack/cooking-beta';source=ROOT/'network/resource-pack/cooking-beta'
|
||||
shutil.copytree(source,artRoot)
|
||||
dst=root/'network/content-assets/cookbook';dst.mkdir(parents=True)
|
||||
shutil.copyfile(ROOT/'network/content-assets/cookbook/regional-cooking.json',dst/'regional-cooking.json')
|
||||
png=artRoot/'overlay/assets/dunerealms/textures/item/cooking/pork_mince.png'
|
||||
data=png.read_bytes();png.write_bytes(data+b'unreviewed')
|
||||
with self.assertRaisesRegex(ValueError,'hash mismatch'):art.artwork(root)
|
||||
png.unlink()
|
||||
with self.assertRaises(FileNotFoundError):art.artwork(root)
|
||||
|
||||
if __name__=='__main__':unittest.main()
|
||||
Loading…
Reference in a new issue

Staff