Files
d2r-mobile/dm/ios/tools/gen_assets.py
T
2026-10-12 16:51:08 +08:00

83 lines
7.1 KiB
Python
Raw 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
"""从玩家自己的游戏文件生成 App 用的原版衍生资源(仓库里不放原版的图和字,见 dm/README.md「从你的游戏文件生成资源」)。run.sh build 先跑这里。
输入:Switch 版 3.3.94012 的 Update/RomFS(参数或环境变量 DM_ROMFS_SRC);可选 PC 版 D2R 的解包目录(DM_PC_DATA,底栏出原版 2 倍图用,没有就用 Switch 素材放大)。
产物(不进 git):
Gen/assets/NativeHUD/{widestash, pouch, touch, mapgoal, v4} 加宽储藏箱 / 编年史盖板 / 百宝囊框 / 选角右侧面板 / 小地图终点 / 底部 UI v4
Sources/TouchArt/dm_native_*.png、Sources/dm_touch_native_art.c 原版技能图集与技能索引(生成在原位置,dm/ios/.gitignore 忽略)
宿主的三张表(dm_vault_tab.h、dm_hud_exp.h、dm_bright_levels.inc)由 dm/host/Makefile 生成。
用法:gen_assets.py [<原版 RomFS(Update)>]
输入没变就不重出(指纹 = 游戏文件清单 + dm/ios/tools 下全部文件 + PC 素材开关;写在产物根目录 .stamp);同一棵树里同时只许一个进程在生成(锁目录 .gen_lock)——
accept 里 iOS 构建与功能用例并行,后者读产物、前者原先每次重写产物,读到一半被删过(base.png 找不到)。gen_romfs_overlay.py 开头也调 ensure()。
Generate the assets derived from the original game that the App uses, from the player's own game files (the repository holds none of the original game's images or text, see dm/README.md 「从你的游戏文件生成资源」(Generate assets from your game files)). run.sh build runs this first.
Input: Update/RomFS of Switch version 3.3.94012 (argument or environment variable DM_ROMFS_SRC); optionally the unpacked directory of PC D2R (DM_PC_DATA, used to produce the original game's 2x art for the bottom bar; without it, Switch art is upscaled).
Build output (not in git):
Gen/assets/NativeHUD/{widestash, pouch, touch, mapgoal, v4} widened stash / chronicle cover plate / pouch frame / character select right panel / minimap goal / bottom UI v4
Sources/TouchArt/dm_native_*.png, Sources/dm_touch_native_art.c the original game's skill atlas and skill index (generated in place, ignored by dm/ios/.gitignore)
The host's three tables (dm_vault_tab.h, dm_hud_exp.h, dm_bright_levels.inc) are generated by dm/host/Makefile.
Usage: gen_assets.py [<original game RomFS (Update)>]
Nothing is regenerated when the input is unchanged (fingerprint = game file list + all files under dm/ios/tools + the PC art switch; written to .stamp in the build output root); only one process may generate at a time in the same tree (lock directory .gen_lock) --
in accept the iOS build and the functional cases run in parallel; the latter read the build output while the former used to rewrite it every time, and files were deleted mid-read (base.png not found). The start of gen_romfs_overlay.py also calls ensure()."""
import hashlib, os, shutil, subprocess, sys, time
import gen_paths
DM_ROOT = os.environ.get('DM_ROOT') or os.path.expanduser('~/dm-work') # 工作区根目录(游戏文件、产物、运行现场都在它下面) | Workspace root (game files, build outputs and run artifacts are all under it)
HERE = os.path.dirname(os.path.abspath(__file__))
STEPS = [('加宽储藏箱', ['gen_widestash_art.py', '{R}']), ('编年史盖板', ['gen_vault_cover.py']), ('百宝囊框', ['gen_pouch_art.py', '{R}']),
('选角右侧面板', ['gen_fe_rightpanel.py', '{R}']), ('小地图终点', ['gen_map_goal_art.py', '{R}']), ('佣兵面板底图', ['gen_merc_art.py', '{R}']),
('技能图集与索引', ['native_art/generate.py', '{R}']), ('底部 UI v4', ['gen_hud_v4.py', '{R}'])]
def stamp(R):
h = hashlib.sha256(open(os.path.join(R, 'data', 'build_manifest.psv'), 'rb').read())
for d, ds, fs in sorted(os.walk(HERE)):
ds[:] = sorted(x for x in ds if x != '__pycache__')
for f in sorted(fs): h.update(os.path.relpath(os.path.join(d, f), HERE).encode()); h.update(open(os.path.join(d, f), 'rb').read())
pc = os.environ.get('DM_PC_DATA') or os.path.join(DM_ROOT, 'data/assets-src/raw')
h.update(('%s|%s|%s' % (pc, os.path.isdir(pc), os.environ.get('DM_REQUIRE_PC_ART', ''))).encode())
return h.hexdigest()
def ensure(R):
"""产物齐、指纹没变就直接返回;否则拿锁生成。返回 True = 这次重出了。 | If the build output is complete and the fingerprint unchanged, return directly; otherwise take the lock and generate. Returns True = regenerated this time."""
root = gen_paths.gen_dir(); st = os.path.join(root, '.stamp'); lock = os.path.join(root, '.gen_lock')
outs = [os.path.join(root, 'NativeHUD', 'v4', 'base.png'), os.path.join(HERE, '..', 'Sources', 'TouchArt', 'dm_native_skill_0.png'), os.path.join(HERE, '..', 'Sources', 'dm_touch_native_art.c')]
want = stamp(R); fresh = lambda: all(map(os.path.isfile, outs)) and os.path.isfile(st) and open(st).read() == want
if fresh(): return False
t0 = time.time()
while True:
try: os.mkdir(lock); break
except FileExistsError:
try: pid = int(open(os.path.join(lock, 'pid')).read())
except (OSError, ValueError): pid = 0
if pid:
try: os.kill(pid, 0)
except ProcessLookupError: shutil.rmtree(lock, ignore_errors=True); continue # 持锁进程已经不在:接管 | The lock-holding process is gone: take over
if time.time() - t0 > 600: sys.exit('🔴 gen_assets:等别的进程生成等了 10 分钟(锁 %s,pid %s)' % (lock, pid))
time.sleep(0.5)
try:
open(os.path.join(lock, 'pid'), 'w').write(str(os.getpid()))
if fresh(): return False # 等锁期间别人已经生成好了 | Someone else already generated it while we waited for the lock
if os.path.exists(st): os.remove(st)
run_steps(R); open(st, 'w').write(want); return True
finally: shutil.rmtree(lock, ignore_errors=True)
def run_steps(R):
for name, cmd in STEPS:
t0 = time.time(); r = subprocess.run([sys.executable] + [os.path.join(HERE, c) if i == 0 else c.replace('{R}', R) for i, c in enumerate(cmd)],
capture_output=True, text=True)
if r.returncode: sys.exit('🔴 生成「%s」失败:\n%s%s' % (name, r.stdout[-1500:], r.stderr[-1500:]))
tip = [l for l in r.stdout.splitlines() if l.startswith(('没找到', '底栏素材'))]
print('%s:%.1f 秒%s' % (name, time.time() - t0, ('(' + tip[0] + ')') if tip else ''))
def main():
R = sys.argv[1] if len(sys.argv) > 1 else os.environ.get('DM_ROMFS_SRC') or os.path.join(DM_ROOT, 'data/switch/3.3.94012/Update/RomFS')
if not os.path.isfile(os.path.join(R, 'data', 'build_manifest.psv')):
sys.exit('🔴 没找到游戏文件:%s/data/build_manifest.psv。把 Switch 版 3.3.94012 的 Update/RomFS 放到这里,或用环境变量 DM_ROMFS_SRC 指过去'
'(见 dm/README.md「从你的游戏文件生成资源」)' % R)
if not ensure(R): print('原版衍生资源:输入没变,沿用 %s' % gen_paths.gen_dir())
if __name__ == '__main__':
main()