Files
2026-11-30 09:52:33 +08:00

4.3 KiB

AGENTS.md

Notes for coding agents (Claude Code etc.) working on this repo. User-facing docs: README.md. The user writes in Chinese; docs are Chinese, code and comments are English. The UI is bilingual (zh/en). Structure and conventions follow the sibling project F:\C\pbzero (Phantom Blade Zero save editor).

What this is

黑神话:悟空存档编辑 — a Windows desktop editor for Black Myth: Wukong save statistics: Will (灵韵), deaths per chapter, play time. Python backend + one HTML page in a pywebview (WebView2) window, shipped as a single PyInstaller exe.

Layout

src/                      all source; also the resource root (bmkong.ROOT = src/ or sys._MEIPASS)
  run.py                  entry: window, or `--serve` = same API over HTTP on 127.0.0.1:8766
  bmkong/protobuf.py      schema-less protobuf: parse with spans, lossless set/append by field path
  bmkong/container.py     file envelope: header + XOR payload; SaveError
  bmkong/save.py          stats model: summary() for the UI, build()/write() for saving
  bmkong/app.py           Api exposed to JS (pywebview js_api / dev HTTP server)
  bmkong/web/index.html   the entire UI (vanilla JS, no build step)
  assets/icon.*, logo.svg  generated by tools/make_icon.py (logo.svg is inlined in index.html)
  tests/test_roundtrip.py protobuf unit tests + real-save round-trip tests
  tools/build_exe.bat     icon + PyInstaller (BMKONG.spec) -> out/BMKONG.exe
doc/save-format.md        save format notes (Chinese)
doc/screenshots/          README previews (Steam ID masked)
out/                      build output only (gitignored)

Save format

Full notes (Chinese): doc/save-format.md — keep it updated when format knowledge changes. Essentials:

  • File = protobuf {1: header, 2: payload}; payload = protobuf XOR repeating key 7b5cda913efcda37, uncompressed.
  • Header field 1 = lowercase hex md5(encrypted payload + "lhx2tkh6lj1wj8jmrgs3k1xb2brusehx"). The game refuses to load a save whose MD5 doesn't match (seen in game). Source: b1.BGW_GameArchiveMgr::CalcByteArrayMD5.
  • No field names in the save; meanings come from diffing saves. The game's C# assemblies (with real message/field names) are embedded in b1-Win64-Shipping.exe, not in the paks; see the doc for how to carve and inspect them.

Rules that matter

  • Every user-visible string has a zh and an en form. UI: add keys to both I18N.zh and I18N.en in index.html and render through t(key, ...args). Backend: SaveError(zh, en); Api._lang picks the message.
  • Never re-serialize the archive: edit through protobuf.set_int/set_float/append, which splice the value and re-encode only ancestor length prefixes. A test asserts that sibling fields stay byte-identical.
  • Archive.encode() keeps every header byte except the MD5, which it recomputes; write() refuses output whose MD5 does not verify. A save with a wrong MD5 opens with checksum_ok: false; build({}) then repairs it.
  • SaveFile.write() refuses while b1-Win64-Shipping.exe runs, re-checks the file is unchanged since opening, decodes its own output, backs up to %LOCALAPPDATA%\BMKong\backups, then replaces atomically.
  • Test against copies only. Never write the user's real save unless they ask. Set BMKONG_HOME to redirect settings/backups — do NOT override LOCALAPPDATA (the Windows Python launcher then installs a fresh Python there). --serve auto-opens the only save it finds (the real one): open a copy before testing writes.
  • pywebview introspects every public attribute of the js_api object: keep non-API state private (_save, _window).
  • The page must boot on pywebviewready (or when pywebview.api already has methods); calling earlier fails.

Commands (run from src/)

python run.py                     # desktop window
python run.py --serve             # browser UI at http://127.0.0.1:8766
python -m unittest tests.test_roundtrip -v   # set BMKONG_TEST_SAVE (a copy) first
tools\build_exe.bat               # -> ../out/BMKONG.exe

Dev machine: Python 3.14, game at G:\G\Steam\steamapps\common\BlackMythWukong. Deps: pywebview; build also needs pyinstaller and pillow.

Ideas not done

  • Boss counts (6.1.1), guid counts (6.2.1.6), level/exp, inventory: structure known, meaning or names not confirmed. Boss/item names would need the game's data tables from the encrypted paks.