111 lines
6.8 KiB
Python
111 lines
6.8 KiB
Python
import binascii
|
|
import copy
|
|
import hashlib
|
|
import json
|
|
import shutil
|
|
from pathlib import Path
|
|
import struct
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
import zlib
|
|
sys.path.insert(0,str(Path(__file__).resolve().parent))
|
|
import plugin_vanilla_art as art
|
|
import plugin_resource_pack as pack
|
|
|
|
def fixture(mode,depth,payload,extra=()):
|
|
def chunk(kind,data):return struct.pack('>I',len(data))+kind+data+struct.pack('>I',binascii.crc32(kind+data)&0xffffffff)
|
|
return b'\x89PNG\r\n\x1a\n'+chunk(b'IHDR',struct.pack('>IIBBBBB',2,1,depth,mode,0,0,0))+b''.join(chunk(k,v) for k,v in extra)+chunk(b'IDAT',zlib.compress(b'\0'+payload))+chunk(b'IEND',b'')
|
|
|
|
class PNGTests(unittest.TestCase):
|
|
def test_rgba_palette_and_packed_grayscale_transparency(self):
|
|
self.assertEqual(art.decode_png(fixture(6,8,bytes((10,20,30,255,40,50,60,80))))[2],bytes((10,20,30,255,40,50,60,80)))
|
|
palette=((b'PLTE',bytes((20,30,40,70,80,90))),(b'tRNS',bytes((255,60))))
|
|
self.assertEqual(art.decode_png(fixture(3,4,b'\x01',palette))[2],bytes((20,30,40,255,70,80,90,60)))
|
|
self.assertEqual(art.decode_png(fixture(0,1,b'\x40',((b'tRNS',b'\0\0'),)))[2],bytes((0,0,0,0,255,255,255,255)))
|
|
def test_bad_crc_and_invalid_layout_fail_closed(self):
|
|
data=bytearray(fixture(6,8,bytes(range(8))));data[-1]^=1
|
|
with self.assertRaisesRegex(ValueError,'CRC'):art.decode_png(bytes(data))
|
|
with self.assertRaises(ValueError):art.decode_png(b'not a PNG')
|
|
|
|
class VanillaArtworkTests(unittest.TestCase):
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
import os
|
|
cls.vanilla=pack.client_assets(pack.ROOT/'build/resourcepack-cache',os.environ.get('DUNE_PACK_CLIENT'))
|
|
cls.files,cls.report=art.load(cls.vanilla)
|
|
def image(self,name,folder='block'):
|
|
return art.decode_png(self.files[art.PREFIX+folder+'/'+name+'.png'])
|
|
def stage_metadata(self,root,manifest):
|
|
(root/'material-manifest.json').write_text(json.dumps(manifest),encoding='utf-8')
|
|
shutil.copyfile(art.ART/'compatibility.json',root/'compatibility.json')
|
|
shutil.copytree(art.ART/'compatibility-inputs',root/'compatibility-inputs')
|
|
def test_every_static_item_block_and_selected_special_uv_is_accounted_for(self):
|
|
self.assertEqual(self.report['textures'],2289);self.assertEqual(self.report['protectedSpiceTextures'],7)
|
|
self.assertEqual(self.report['authoredItemTextures'],7);self.assertEqual(self.report['animationSheets'],56)
|
|
for key in self.vanilla:
|
|
if key.startswith((art.PREFIX+'block/',art.PREFIX+'item/')) and key.endswith('.png'):
|
|
self.assertTrue(key in self.files or key in art.PROTECTED,key)
|
|
for key in ('entity/chest/normal.png','entity/shulker/shulker_blue.png','entity/shield/shield_base.png'):
|
|
self.assertTrue(art.PREFIX+key in self.files,key)
|
|
def test_missing_or_duplicate_coverage_is_rejected(self):
|
|
original=json.loads((art.ART/'material-manifest.json').read_text(encoding='utf-8'))
|
|
for duplicate in (False,True):
|
|
manifest=copy.deepcopy(original)
|
|
if duplicate:manifest['entries'].append(manifest['entries'][0])
|
|
else:manifest['entries'].pop()
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
root=Path(tmp);self.stage_metadata(root,manifest);(root/'overlay').mkdir()
|
|
with self.assertRaisesRegex(ValueError,'coverage'):art.load(self.vanilla,root)
|
|
def test_existing_spice_carrier_art_cannot_be_replaced_by_this_layer(self):
|
|
manifest=json.loads((art.ART/'material-manifest.json').read_text(encoding='utf-8'))
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
root=Path(tmp);self.stage_metadata(root,manifest)
|
|
target=root/'overlay'/sorted(art.PROTECTED)[0];target.parent.mkdir(parents=True);target.write_bytes(b'bad replacement')
|
|
with self.assertRaisesRegex(ValueError,'spice'):art.load(self.vanilla,root)
|
|
def test_older_client_only_item_assets_are_inside_same_pack(self):
|
|
self.assertEqual(self.report['compatibilityItemPaths'],2)
|
|
for name in ('light','filled_map_markings'):self.assertTrue(art.PREFIX+'item/'+name+'.png' in self.files)
|
|
def test_worn_armour_and_general_entity_vehicle_skins_are_not_replaced(self):
|
|
excluded=('entity/equipment/','entity/copper_golem/','entity/boat/','entity/chest_boat/','entity/minecart/','entity/projectiles/','entity/armorstand/')
|
|
self.assertFalse(any(key.startswith(tuple(art.PREFIX+p for p in excluded)) for key in self.files))
|
|
self.assertNotIn(art.PREFIX+'entity/shulker/shulker_bullet.png',set(self.files))
|
|
def test_native_trim_templates_are_not_replaced(self):
|
|
self.assertEqual(self.report['preservedNativeTrimTemplates'],4)
|
|
for key in art.TRIMS:self.assertFalse(key in self.files,key)
|
|
|
|
def test_familiar_armour_silhouettes_and_leather_dye_masks_are_retained(self):
|
|
for kind in ('helmet','chestplate','leggings','boots'):
|
|
for material in ('iron','copper','golden','diamond','netherite','chainmail','leather'):
|
|
key=art.PREFIX+'item/'+material+'_'+kind+'.png';w,h,body=art.decode_png(self.files[key]);sw,sh,source=art.decode_png(self.vanilla[key])
|
|
self.assertEqual((w,h),(sw*2,sh*2));self.assertTrue(body[3::4]==art.upsample_alpha(source,sw,sh,2),key)
|
|
_,_,base=self.image('leather_'+kind,'item')
|
|
self.assertTrue(all(base[i]==base[i+1]==base[i+2] for i in range(0,len(base),4)))
|
|
def test_biome_and_redstone_tint_masks_remain_neutral(self):
|
|
for name in ('grass_block_top','oak_leaves','redstone_dust_dot','water_still','water_flow'):
|
|
_,_,pixels=self.image(name)
|
|
self.assertTrue(all(pixels[i]==pixels[i+1]==pixels[i+2] for i in range(0,len(pixels),4) if pixels[i+3]),name)
|
|
def test_active_furnace_and_dyed_building_colours_remain_distinct(self):
|
|
_,_,on=self.image('furnace_front_on');_,_,off=self.image('furnace_front')
|
|
hot=lambda pixels:sum(pixels[i]>190 and pixels[i+1]>100 and pixels[i+2]<100 for i in range(0,len(pixels),4))
|
|
self.assertGreater(hot(on),20);self.assertEqual(hot(off),0)
|
|
for name in ('nether_gold_ore','nether_quartz_ore'):
|
|
_,_,p=self.image(name)
|
|
self.assertGreater(sum(p[0::4]),sum(p[1::4])*1.2,name)
|
|
averages=[]
|
|
for color in ('white','orange','magenta','light_blue','yellow','lime','pink','gray','light_gray','cyan','purple','blue','brown','green','red','black'):
|
|
_,_,p=self.image(color+'_wool');averages.append(tuple(sum(p[c::4])/len(p[3::4]) for c in range(3)))
|
|
for i,a in enumerate(averages):
|
|
for b in averages[i+1:]:self.assertGreater(sum((x-y)**2 for x,y in zip(a,b))**.5,16)
|
|
def test_clock_compass_and_bow_pose_sequences_do_not_collapse(self):
|
|
for stem,count in (('clock',64),('compass',32),('recovery_compass',32)):
|
|
prior=None;new_prior=None
|
|
for i in range(count):
|
|
key=art.PREFIX+f'item/{stem}_{i:02d}.png';original=art.decode_png(self.vanilla[key])[2];current=art.decode_png(self.files[key])[2]
|
|
if prior is not None and prior!=original:self.assertNotEqual(current,new_prior,key)
|
|
prior,new_prior=original,current
|
|
for stem in ('bow_pulling','crossbow_pulling'):
|
|
poses=[self.image(stem+'_'+str(i),'item')[2] for i in range(3)]
|
|
self.assertEqual(len(set(poses)),3)
|
|
|
|
if __name__=='__main__':unittest.main()
|

Staff