367 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
93 lines
4.2 KiB
Python
93 lines
4.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Build a conservative Source-to-native guild roster area identity profile.
|
|
|
|
Consumes a saved Source AreaTable.dbc and native 70334 AreaTable.db2. Only
|
|
unencrypted native rows with equal nonempty English name, continent and parent
|
|
are admitted. Missing and changed names remain unknown, as do hotfix-only rows.
|
|
"""
|
|
import argparse
|
|
import hashlib
|
|
import struct
|
|
from pathlib import Path
|
|
|
|
from map_light_compat import read_wdc5
|
|
|
|
NATIVE_IDENTITY = (1918102339, 2576725911)
|
|
|
|
|
|
def profile(source, native):
|
|
if source[:4] != b'WDBC':
|
|
raise ValueError('Source AreaTable must be WDBC')
|
|
count, fields, size, strings = struct.unpack_from('<4I', source, 4)
|
|
if fields != 25 or size != 100 or len(source) != 20 + count * size + strings:
|
|
raise ValueError('Unexpected Source AreaTable layout')
|
|
string_start = 20 + count * size
|
|
def string(data, pos, limit):
|
|
if pos < 0 or pos >= limit:
|
|
raise ValueError('Invalid string offset')
|
|
end = data.find(b'\0', pos, limit)
|
|
if end < 0:
|
|
raise ValueError('Unterminated area string')
|
|
return data[pos:end].decode('utf8')
|
|
src = {}
|
|
for i in range(count):
|
|
v = struct.unpack_from('<25I', source, 20 + i * size)
|
|
if v[0] in src:
|
|
raise ValueError('Duplicate Source area')
|
|
src[v[0]] = (v[1], v[2], string(source, string_start + v[11], len(source)))
|
|
t = read_wdc5(native)
|
|
if (t['table'], t['layout']) != NATIVE_IDENTITY:
|
|
raise ValueError('Unexpected native AreaTable identity')
|
|
head = struct.unpack_from('<4I2I2I I 2H 7I', native, 136)
|
|
record_count, field_count, record_size, section_count = head[0], head[1], head[2], head[17]
|
|
secs = [struct.unpack_from('<Q8I', native, 204 + 40 * i) for i in range(section_count)]
|
|
# The analyzed table has one ordinary section, followed by encrypted
|
|
# sections. String offsets count all sections' records before strings.
|
|
if field_count != 24 or record_size != 28 or not secs or secs[0][0] or any(not s[0] for s in secs[1:]):
|
|
raise ValueError('Unexpected native section layout')
|
|
_, offset, n, string_size, end, id_size, rel, sparse, copies = secs[0]
|
|
if end or sparse or copies or id_size != n * 4 or record_count != sum(s[2] for s in secs):
|
|
raise ValueError('Unexpected native AreaTable section')
|
|
strings_at = offset + n * record_size
|
|
ids = struct.unpack_from(f'<{n}I', native, strings_at + string_size)
|
|
shift = (record_count - n) * record_size
|
|
approved = []
|
|
for i, ident in enumerate(ids):
|
|
v = t['rows'][ident]
|
|
x = src.get(ident)
|
|
if not x:
|
|
continue
|
|
row = offset + i * record_size
|
|
str_offset = struct.unpack_from('<I', native, row + 4)[0]
|
|
start = row + 4 + str_offset - shift
|
|
if start < strings_at:
|
|
raise ValueError('Native area name outside string table')
|
|
name = string(native, start, strings_at + string_size)
|
|
if x[2] and x[2].casefold() == name.casefold() and x[:2] == (v[2], v[3]):
|
|
approved.append(ident)
|
|
return sorted(approved)
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('--source', type=Path, required=True)
|
|
parser.add_argument('--native', type=Path, required=True)
|
|
parser.add_argument('--out', type=Path, required=True)
|
|
args = parser.parse_args()
|
|
src, nat = args.source.read_bytes(), args.native.read_bytes()
|
|
ids = profile(src, nat)
|
|
lines = [', '.join(str(x) for x in ids[i:i + 15]) for i in range(0, len(ids), 15)]
|
|
out = '#pragma once\n#include <algorithm>\n#include <array>\n#include <cstdint>\n\nnamespace gateway {\n'
|
|
out += '// Exact name, map and parent matches; unproven IDs remain absent.\n'
|
|
out += '// Source SHA-256: ' + hashlib.sha256(src).hexdigest() + '\n'
|
|
out += '// Native SHA-256: ' + hashlib.sha256(nat).hexdigest() + '\n'
|
|
out += f'inline constexpr std::array<uint32_t, {len(ids)}> guild_area_ids = {{\n '
|
|
out += ',\n '.join(lines) + '\n};\n'
|
|
out += 'inline uint32_t guild_area_id(uint32_t source) {\n return std::binary_search(guild_area_ids.begin(), guild_area_ids.end(), source) ? source : 0;\n}\n}\n'
|
|
args.out.write_text(out)
|
|
print(f'{len(ids)} exact matches')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|