"""Inventory selected local UI resources without touching the running game."""
import concurrent.futures, importlib.util, json, pathlib, re, sys
sys.stdout.reconfigure(encoding='utf-8')
spec=importlib.util.spec_from_file_location('assets','research/galaxy_asset_inventory.py')
saved=sys.argv;sys.argv=['inventory','artifacts/refit-0411-qa/unused.json']
assets=importlib.util.module_from_spec(spec);spec.loader.exec_module(assets);sys.argv=saved
out=pathlib.Path('artifacts/refit-0411-qa');out.mkdir(parents=True,exist_ok=True)
pattern=re.compile(rb'outfitt|module_category|item_.*_icon|module.?icon|weapon.?icon|multicannon|equipment|shield|gfx|gui.?icon',re.I)
def scan(directory):
    found=[]
    for path in directory.iterdir():
        if not path.is_file():continue
        with path.open('rb') as f:header=f.read(262)
        if header[:4]!=b'FREA':continue
        raw=pow(int.from_bytes(header[6:262],'big'),assets.E,assets.N).to_bytes(256,'big');sep=raw.find(b'\0',2)
        if sep<10 or not pattern.search(raw[sep+37:]):continue
        prefix=assets.decode(path,262+8192)
        strings=[m.group().decode('ascii') for m in re.finditer(rb'[A-Za-z_][A-Za-z0-9_.:/ -]{3,}',prefix[:6000])]
        found.append({'path':str(path),'bytes':path.stat().st_size,'strings':strings[:14]})
    return found
rows=[]
with concurrent.futures.ThreadPoolExecutor(max_workers=6) as pool:
    for found in pool.map(scan, sorted(p for p in assets.ROOT.iterdir() if p.is_dir())):
        rows+=found
(out/'local-ui-assets.json').write_text(json.dumps(rows,indent=2),encoding='utf8')
print('Candidates',len(rows),'saved',out/'local-ui-assets.json')
print(json.dumps([x for x in rows if any('outfitt' in s.lower() or 'icon' in s.lower() for s in x['strings'])][:24],ensure_ascii=False))
