#!/usr/bin/env python3
"""gvr-map-extract 결과(<코드>.glb + <코드>.json)를 사이트용으로 압축한다.
pack.py <추출 폴더> <출력 폴더> <버전(예: VALORANT_13.08)>
출력: <출력>/
/<코드>.glb.gz (gltfpack -cc 로 meshopt 압축 · 위치 양자화 → gzip) + <출력>/index.json
사이트 서버 data/maps3d 에 그대로 올리면 된다 (site/server/src/maps3d.ts)."""
import datetime, gzip, json, os, re, shutil, subprocess, sys
src, out, version = sys.argv[1], sys.argv[2], sys.argv[3]
stamp = datetime.datetime.now(datetime.timezone.utc).strftime('%Y%m%d%H%M')
d = re.sub(r'[^\w.-]', '_', f'{version}-{stamp}')
os.makedirs(os.path.join(out, d), exist_ok=True)
maps = {}
for name in sorted(os.listdir(src)):
if not name.endswith('.json'):
continue
info = json.load(open(os.path.join(src, name)))
code = info['code']
glb = os.path.join(src, code + '.glb')
if not os.path.exists(glb) or info.get('triangles', 0) == 0:
continue
packed = os.path.join(out, d, code + '.glb')
subprocess.run(['npx', '-y', 'gltfpack@1.3.0', '-i', glb, '-o', packed, '-cc'], check=True, stdout=subprocess.DEVNULL)
with open(packed, 'rb') as f, gzip.open(packed + '.gz', 'wb', 9) as g:
shutil.copyfileobj(f, g)
os.remove(packed)
size = os.path.getsize(packed + '.gz')
maps[info['mapUrl']] = {'code': code, 'file': code + '.glb', 'bytes': size, 'triangles': info['triangles']}
print(f"{code}: {info['triangles']} 삼각형 → {size / 1e6:.1f}MB")
idx = {'version': version, 'dir': d, 'updatedAt': datetime.datetime.now(datetime.timezone.utc).isoformat(), 'maps': maps}
tmp = os.path.join(out, 'index.json.tmp')
json.dump(idx, open(tmp, 'w'), ensure_ascii=False, indent=1)
os.replace(tmp, os.path.join(out, 'index.json'))
print(f'index.json: 맵 {len(maps)}개 · 폴더 {d}')