"""Build a self-contained, network-free Relic Hollow HTML from the editable folder."""
from pathlib import Path
import base64,json,re
ROOT=Path(__file__).resolve().parents[1]
paths={p.stem:'assets/'+p.name for p in sorted((ROOT/'assets').glob('*.webp'))}
paths.update({p.stem+'Music':'audio/'+p.name for p in sorted((ROOT/'audio').glob('*.mp3'))})
(ROOT/'src/assets.js').write_text('window.RH_ASSETS='+json.dumps(paths)+';',encoding='utf-8')
assets={name:'data:'+('audio/mpeg' if path.endswith('.mp3') else 'image/webp')+';base64,'+base64.b64encode((ROOT/path).read_bytes()).decode('ascii') for name,path in paths.items()}
css=(ROOT/'src/style.css').read_text()
assert css.count('{')==css.count('}'), 'Unbalanced CSS blocks'
html=(ROOT/'index.html').read_text()
html=html.replace('<link rel="stylesheet" href="src/style.css">','<style>\n'+(ROOT/'src/style.css').read_text()+'\n</style>')
for name in ['assets','engine','audio','game']:
 code='window.RH_ASSETS='+json.dumps(assets)+';' if name=='assets' else (ROOT/'src'/f'{name}.js').read_text()
 html=html.replace(f'<script src="src/{name}.js"></script>','<script>\n'+code+'\n</script>')
out=ROOT.parent/'Relic-Hollow.html';out.write_text(html,encoding='utf-8');print(out,round(out.stat().st_size/1048576,2),'MiB')
