Files

39 lines
1.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""资产引用校验:game.js 引用的每个音频 id 都必须有 .ogg 和 .m4a 文件。"""
import re, sys, pathlib
root = pathlib.Path('/home/xsl/SoundGame/web/escape')
src = (root / 'game.js').read_text(encoding='utf-8')
ids = set()
# 1) 静态字符串 id:'sfx/xxx' / 'voice/xxx' / 'bgm/xxx'
for m in re.finditer(r"['\"]((bgm|voice|sfx)/[a-z0-9_]+)['\"]", src):
ids.add(m.group(1))
# 2) 动态数字播报 voice/n${...} → n0..n9
if re.search(r"voice/n\$\{", src):
ids.update(f'voice/n{i}' for i in range(10))
missing = []
for aid in sorted(ids):
for ext in ('.ogg', '.m4a'):
if not (root / 'audio' / (aid + ext)).is_file():
missing.append(aid + ext)
print(f'引用的音频 id 数: {len(ids)}')
for aid in sorted(ids):
print(' ', aid)
if missing:
print('\n缺失文件:')
for f in missing:
print(' MISSING', f)
sys.exit(1)
print('\nOK:所有引用的 id 均有 .ogg 和 .m4a')
# 反向检查:目录里是否存在从未被引用的音频(仅提示,不算失败)
import os
on_disk = {str(p.relative_to(root / 'audio'))[:-4]
for p in (root / 'audio').rglob('*.ogg')}
unused = on_disk - ids
if unused:
print('未被代码引用(仅提示):', ', '.join(sorted(unused)))