"""从游戏存档重新解密全部槽位,刷新 save_snapshot/。 只读游戏存档,不会写入任何游戏文件。 游戏位置查找顺序: 1. 环境变量 PBZ_GAME 2. 命令行 --game 3. Steam 注册表 + libraryfolders.vdf 自动扫描 4. 常见安装路径 """ from __future__ import annotations import ctypes import json import os import re import struct import sys from pathlib import Path from cryptography.hazmat.primitives.ciphers.aead import AESGCM ROOT = Path(__file__).resolve().parent SNAP = ROOT / "save_snapshot" ANCHOR = "sealed but block is PLAINTEXT".encode("utf-16-le") + b"\0\0" KNOWN_OFFSET = 203505944 # 参考版本(build 26266538)的密钥偏移,仅作首选尝试 # ---------------------------------------------------------------- 找游戏 def _steam_libraries() -> list[Path]: """从 Steam 注册表与 libraryfolders.vdf 里找出所有库目录。""" libs: list[Path] = [] steam: Path | None = None try: import winreg for hive, key in ((winreg.HKEY_CURRENT_USER, r"Software\Valve\Steam"), (winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\WOW6432Node\Valve\Steam")): try: with winreg.OpenKey(hive, key) as k: for name in ("SteamPath", "InstallPath"): try: v = winreg.QueryValueEx(k, name)[0] except OSError: continue if v and Path(v).is_dir(): steam = Path(v) break except OSError: continue if steam: break except ImportError: pass if steam is None: for guess in (r"C:\Program Files (x86)\Steam", r"C:\Program Files\Steam"): if Path(guess).is_dir(): steam = Path(guess) break if steam: libs.append(steam) vdf = steam / "steamapps" / "libraryfolders.vdf" if vdf.is_file(): try: text = vdf.read_text(encoding="utf8", errors="ignore") for m in re.finditer(r'"path"\s+"([^"]+)"', text): p = Path(m.group(1).replace("\\\\", "\\")) if p.is_dir(): libs.append(p) except OSError: pass return libs def find_game() -> Path: # 1) 环境变量 env = os.environ.get("PBZ_GAME") if env and (Path(env) / "platform/steam/PBZ.exe").is_file(): return Path(env) # 2) 命令行 if "--game" in sys.argv: p = Path(sys.argv[sys.argv.index("--game") + 1]) if (p / "platform/steam/PBZ.exe").is_file(): return p # 3) Steam 库 for lib in _steam_libraries(): cand = lib / "steamapps" / "common" / "PBZ" if (cand / "platform/steam/PBZ.exe").is_file(): return cand # 4) 常见路径兜底 for drive in "CDEFGH": for sub in ("game/steam", "Steam", "SteamLibrary", "Games/Steam", "Program Files (x86)/Steam", "Program Files/Steam"): cand = Path(f"{drive}:/") / sub / "steamapps" / "common" / "PBZ" if (cand / "platform/steam/PBZ.exe").is_file(): return cand raise SystemExit( "找不到游戏安装目录。请用 --game 指定,或设置环境变量 PBZ_GAME,例如:\n" ' set PBZ_GAME=D:\\SteamLibrary\\steamapps\\common\\PBZ\n' " python refresh.py --game \"D:\\SteamLibrary\\steamapps\\common\\PBZ\"" ) def find_save(game: Path) -> Path: base = game / "ProjectPBZ" / "Saved" / "SaveGames" / "Steam" if not base.is_dir(): raise SystemExit(f"找不到存档目录:{base}") # 每个子目录是一个 SteamID64,取最近修改的那个 cands = [d / "PBZSaveGame.sav" for d in base.iterdir() if d.is_dir()] cands = [c for c in cands if c.is_file()] if not cands: raise SystemExit(f"{base} 下没有 PBZSaveGame.sav") return max(cands, key=lambda p: p.stat().st_mtime) # ---------------------------------------------------------------- 密钥 def pe_sections(data) -> list[tuple[int, int]]: pe = struct.unpack_from(" tuple[bytes, int]: """锚点搜索 + 目录块 GCM 认证。返回 (key, offset)。""" dsize = struct.unpack_from(" bool: try: d = AESGCM(key).decrypt(block[8:20], block[36:] + block[20:36], None) except Exception: return False return d[:12] == src[:12] import mmap with exe.open("rb") as f, mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) as data: ranges = pe_sections(data) def try_at(off: int): if not any(s <= off and off + 64 <= e for s, e in ranges): return None mat = data[off:off + 64] key = bytes(a ^ b for a, b in zip(mat[:32], mat[32:])) return key if ok(key) else None k = try_at(KNOWN_OFFSET) if k: return k, KNOWN_OFFSET pos = 0 while True: m = data.find(ANCHOR, pos) if m < 0: break pos = m + 1 end = m + len(ANCHOR) for pad in range(64): k = try_at(end + pad) if k: return k, end + pad raise SystemExit("无法恢复存档密钥(游戏可能已更新加密方式)") # ---------------------------------------------------------------- 解码器 CODEC_HINT = """缺少 Oodle Kraken 解码器 pbz_codec.dll。 它来自开源项目 PBZ-Editor(MIT 授权)的发行包,本仓库不直接分发二进制: 1. 打开 https://github.com/qry6868/PBZ-Editor/releases 2. 下载 *-windows-x64.zip 3. 取出 PBZ-Editor/_internal/native/pbz_codec.dll 4. 放到本目录,或用环境变量 PBZ_CODEC 指向它的绝对路径 也可以运行 python fetch_codec.py 自动下载并抽取。 """ def get_codec() -> Path: env = os.environ.get("PBZ_CODEC") if env and Path(env).is_file(): return Path(env) for p in (ROOT / "pbz_codec.dll", SNAP / ".pbz_codec.dll"): if p.is_file(): return p raise SystemExit(CODEC_HINT) # ---------------------------------------------------------------- 主流程 def main(): game = find_game() save = find_save(game) print(f"游戏: {game}") print(f"存档: {save}") src = save.read_bytes() key, off = recover_key(game / "platform/steam/PBZ.exe", src) print(f"密钥已恢复 (offset {off})") codec_path = get_codec() lib = ctypes.CDLL(str(codec_path)) lib.pbz_codec_version.restype = ctypes.c_uint32 fn = lib.pbz_decode fn.restype = ctypes.c_ssize_t fn.argtypes = [ctypes.c_void_p, ctypes.c_size_t, ctypes.c_void_p, ctypes.c_size_t] dsize = struct.unpack_from(" {SNAP}") if __name__ == "__main__": main()