33import tomlkit
44import dataclasses
55import zipfile
6+ import json
67import typing as t
8+ import scratchattach .editor # this is slow but easier
79from pathlib import Path
10+ from slugify import slugify
811from datetime import datetime
912
1013
1114@dataclasses .dataclass
1215class Asset :
1316 md5 : str = ""
14- # names: list [str] = dataclasses.field(default_factory=list )
17+ names : set [str ] = dataclasses .field (default_factory = set )
1518 ext : str = ""
1619 content_written : bool = False
1720 # content: bytes = dataclasses.field(default=b"", repr=False)
@@ -100,13 +103,25 @@ def sortfunc(f: Path):
100103 assets : dict [str , Asset ] = {}
101104 for file in files :
102105 with zipfile .ZipFile (file ) as archive :
106+ if file .name .endswith (".sb3" ):
107+ body = scratchattach .editor .Project .from_json (
108+ json .loads (archive .read ("project.json" ))
109+ ) # using from json so assets aren't loaded and no extra zipfiling
110+ else :
111+ assert file .name .endswith (".sprite3" )
112+ body = scratchattach .editor .Sprite .from_json (
113+ json .loads (archive .read ("sprite.json" ))
114+ )
103115 for file in archive .filelist :
104116 if file .filename .endswith (".json" ):
105117 continue
106118 asset = assets .get (file .filename , Asset ())
107119 asset .ext = file .filename .split ("." )[- 1 ]
108120 asset .md5 = file .filename .removesuffix (f".{ asset .ext } " )
109- # asset.names.append() # TODO: read sprite.json or project.json
121+
122+ for aobj in body .assets :
123+ if aobj .file_name == file .filename :
124+ asset .names .add (aobj .name )
110125
111126 if not asset .content_written :
112127 (assetpath / file .filename ).write_bytes (archive .read (file .filename ))
@@ -120,6 +135,10 @@ def sortfunc(f: Path):
120135 print (asset )
121136 table .add ("md5" , asset .md5 )
122137 table .add ("ext" , asset .ext )
138+ table .add ("names" , list (asset .names ))
139+ table .add (
140+ "chosen_name" , slugify (next (iter (asset .names ))) if asset .names else "foo"
141+ )
123142
124143 asset_arr .append (table )
125144 # store file toml
@@ -135,7 +154,7 @@ def sortfunc(f: Path):
135154 file_arr .append (table )
136155
137156 content = {"files" : file_arr , "assets" : asset_arr }
138- with outpath .open ("w" ) as f :
157+ with outpath .open ("w" , encoding = "utf-8" ) as f :
139158 tomlkit .dump (content , f )
140159
141160
0 commit comments