1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
#!/usr/bin/env python3
"""
Deterministic audit for FXID "absolute parity" checklist.
What this script produces:
1) strict parsing stats across all FXID payloads in NRes archives,
2) opcode histogram and rare-branch counters (op6, op1 tail usage),
3) reference vectors for RNG core (sub_10002220 semantics).
"""
from __future__ import annotations
import argparse
import json
import struct
from collections import Counter
from pathlib import Path
from typing import Any
import archive_roundtrip_validator as arv
TYPE_FXID = 0x44495846
FX_CMD_SIZE = {1: 224, 2: 148, 3: 200, 4: 204, 5: 112, 6: 4, 7: 208, 8: 248, 9: 208, 10: 208}
def _entry_payload(blob: bytes, entry: dict[str, Any]) -> bytes:
start = int(entry["data_offset"])
end = start + int(entry["size"])
return blob[start:end]
def _cstr32(raw: bytes) -> str:
return raw.split(b"\x00", 1)[0].decode("latin1", errors="replace")
def _rng_step_sub_10002220(state32: int) -> tuple[int, int]:
"""
sub_10002220 semantics in 32-bit packed state form:
lo = state[15:0], hi = state[31:16]
new_lo = hi ^ (lo << 1)
new_hi = (hi >> 1) ^ new_lo
return new_hi (u16), update state=(new_hi<<16)|new_lo
"""
lo = state32 & 0xFFFF
hi = (state32 >> 16) & 0xFFFF
new_lo = (hi ^ ((lo << 1) & 0xFFFF)) & 0xFFFF
new_hi = ((hi >> 1) ^ new_lo) & 0xFFFF
return ((new_hi << 16) | new_lo), new_hi
def _rng_vectors() -> dict[str, Any]:
seeds = [0x00000000, 0x00000001, 0x12345678, 0x89ABCDEF, 0xFFFFFFFF]
out: list[dict[str, Any]] = []
for seed in seeds:
state = seed
outputs: list[int] = []
states: list[int] = []
for _ in range(16):
state, value = _rng_step_sub_10002220(state)
outputs.append(value)
states.append(state)
out.append(
{
"seed_hex": f"0x{seed:08X}",
"outputs_u16_hex": [f"0x{x:04X}" for x in outputs],
"states_u32_hex": [f"0x{x:08X}" for x in states],
}
)
return {"generator": "sub_10002220", "vectors": out}
def run_audit(root: Path) -> dict[str, Any]:
counters: Counter[str] = Counter()
opcode_hist: Counter[int] = Counter()
issues: list[dict[str, Any]] = []
op1_tail6_samples: list[dict[str, Any]] = []
op1_optref_samples: list[dict[str, Any]] = []
for item in arv.scan_archives(root):
if item["type"] != "nres":
continue
archive_path = root / item["relative_path"]
counters["archives_total"] += 1
data = archive_path.read_bytes()
try:
parsed = arv.parse_nres(data, source=str(archive_path))
except Exception as exc: # pylint: disable=broad-except
issues.append(
{
"severity": "error",
"archive": str(archive_path),
"entry": None,
"message": f"cannot parse NRes: {exc}",
}
)
continue
for entry in parsed["entries"]:
if int(entry["type_id"]) != TYPE_FXID:
continue
counters["fxid_total"] += 1
payload = _entry_payload(data, entry)
entry_name = str(entry["name"])
if len(payload) < 60:
issues.append(
{
"severity": "error",
"archive": str(archive_path),
"entry": entry_name,
"message": f"payload too small: {len(payload)}",
}
)
continue
cmd_count = struct.unpack_from("<I", payload, 0)[0]
ptr = 0x3C
ok = True
for idx in range(cmd_count):
if ptr + 4 > len(payload):
issues.append(
{
"severity": "error",
"archive": str(archive_path),
"entry": entry_name,
"message": f"command {idx}: missing header at offset={ptr}",
}
)
ok = False
break
word = struct.unpack_from("<I", payload, ptr)[0]
opcode = word & 0xFF
size = FX_CMD_SIZE.get(opcode)
if size is None:
issues.append(
{
"severity": "error",
"archive": str(archive_path),
"entry": entry_name,
"message": f"command {idx}: unknown opcode={opcode} at offset={ptr}",
}
)
ok = False
break
if ptr + size > len(payload):
issues.append(
{
"severity": "error",
"archive": str(archive_path),
"entry": entry_name,
"message": f"command {idx}: truncated end={ptr + size}, payload={len(payload)}",
}
)
ok = False
break
opcode_hist[opcode] += 1
if opcode == 6:
counters["op6_commands"] += 1
if opcode == 1:
tail6 = payload[ptr + 136 : ptr + 160]
if any(tail6):
counters["op1_tail6_nonzero"] += 1
if len(op1_tail6_samples) < 16:
dwords = list(struct.unpack("<6I", tail6))
op1_tail6_samples.append(
{
"archive": str(archive_path),
"entry": entry_name,
"cmd_index": idx,
"tail6_u32_hex": [f"0x{x:08X}" for x in dwords],
}
)
archive_s = _cstr32(payload[ptr + 160 : ptr + 192])
name_s = _cstr32(payload[ptr + 192 : ptr + 224])
if archive_s or name_s:
counters["op1_optref_nonempty"] += 1
if len(op1_optref_samples) < 16:
op1_optref_samples.append(
{
"archive": str(archive_path),
"entry": entry_name,
"cmd_index": idx,
"opt_archive": archive_s,
"opt_name": name_s,
}
)
ptr += size
if ok and ptr != len(payload):
issues.append(
{
"severity": "error",
"archive": str(archive_path),
"entry": entry_name,
"message": f"tail bytes after command stream: parsed_end={ptr}, payload={len(payload)}",
}
)
ok = False
if ok:
counters["fxid_ok"] += 1
return {
"input_root": str(root),
"summary": {
"archives_total": counters["archives_total"],
"fxid_total": counters["fxid_total"],
"fxid_ok": counters["fxid_ok"],
"issues_total": len(issues),
"op6_commands": counters["op6_commands"],
"op1_tail6_nonzero": counters["op1_tail6_nonzero"],
"op1_optref_nonempty": counters["op1_optref_nonempty"],
},
"opcode_histogram": {str(k): opcode_hist[k] for k in sorted(opcode_hist)},
"op1_tail6_samples": op1_tail6_samples,
"op1_optref_samples": op1_optref_samples,
"rng_reference": _rng_vectors(),
"rng_states_fx_path": [
{"state": "dword_10023688", "seed_init": "sub_10002660", "used_by": ["sub_10001720", "sub_10001A40"]},
{"state": "dword_100238C0", "seed_init": "sub_10003A50", "used_by": ["sub_10002BE0"]},
{"state": "dword_10024110", "seed_init": "sub_10009180", "used_by": ["sub_10008120", "sub_10007D10"]},
{"state": "dword_10024810", "seed_init": "sub_1000D370", "used_by": ["sub_1000BF30", "sub_1000C1A0"]},
{"state": "dword_10024A48", "seed_init": "sub_1000F420", "used_by": ["sub_1000EC50"]},
{"state": "dword_10024C80", "seed_init": "sub_10010370", "used_by": ["sub_1000F6E0"]},
{"state": "dword_100250F0", "seed_init": "sub_10012C70", "used_by": ["sub_10011230", "sub_100115C0"]},
],
"issues": issues,
}
def main() -> int:
parser = argparse.ArgumentParser(description="FXID absolute parity audit.")
parser.add_argument("--input", required=True, help="Root directory with game/test archives.")
parser.add_argument("--report", required=True, help="Output JSON report path.")
args = parser.parse_args()
root = Path(args.input).resolve()
report_path = Path(args.report).resolve()
payload = run_audit(root)
report_path.parent.mkdir(parents=True, exist_ok=True)
report_path.write_text(json.dumps(payload, indent=2, ensure_ascii=False) + "\n", encoding="utf-8")
summary = payload["summary"]
print(f"Input root : {root}")
print(f"NRes archives : {summary['archives_total']}")
print(f"FXID payloads : {summary['fxid_ok']}/{summary['fxid_total']} valid")
print(f"Issues : {summary['issues_total']}")
print(f"Opcode6 commands : {summary['op6_commands']}")
print(f"Op1 tail6 nonzero : {summary['op1_tail6_nonzero']}")
print(f"Op1 optref non-empty : {summary['op1_optref_nonempty']}")
print(f"Report : {report_path}")
return 0
if __name__ == "__main__":
raise SystemExit(main())
|