113 lines
5.6 KiB
Python
113 lines
5.6 KiB
Python
"""Add client-only tracking lock 4582 to the reviewed 70334 enUS Lock.db2."""
|
|
import argparse
|
|
import copy
|
|
import hashlib
|
|
import json
|
|
import os
|
|
import struct
|
|
import subprocess
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
PARENT_SHA256 = "df5e98a60e7da210e6776ab7d5525f686ea6b8cb29c396204b13a64ac3aef81f"
|
|
SOURCE_ID = 259
|
|
CLIENT_ID = 4582
|
|
SLOTS = ("Index", "Skill", "Type", "Action")
|
|
|
|
def sha(data):
|
|
return hashlib.sha256(data).hexdigest()
|
|
|
|
def require(condition, message):
|
|
if not condition:
|
|
raise ValueError(message)
|
|
|
|
def dump(tool, definitions, directory):
|
|
command = [tool, "dump", directory, definitions, "Lock", "1.60.1.70334"]
|
|
result = subprocess.run(command, capture_output=True, text=True, check=True)
|
|
rows = {}
|
|
for line in result.stdout.splitlines():
|
|
key, *column_text = line.split("\t")
|
|
columns = dict(field.split("=", 1) for field in column_text)
|
|
require(set(columns) == {"ID", "Flags", *SLOTS}, "Unexpected Lock field layout")
|
|
row = {name: [int(v) for v in columns[name].split(",")] for name in SLOTS}
|
|
row.update(ID=int(columns["ID"]), Flags=int(columns["Flags"]))
|
|
require(all(len(row[name]) == 8 for name in SLOTS), "Unexpected Lock slot count")
|
|
require(row["ID"] == int(key) and int(key) not in rows, "Duplicate or mismatched Lock ID")
|
|
rows[int(key)] = row
|
|
return rows
|
|
|
|
def updated_row(source):
|
|
require(source == dict(ID=SOURCE_ID, Flags=0, Index=[0,13,2,0,0,0,0,0],
|
|
Skill=[0]*8, Type=[0,2,2,0,0,0,0,0], Action=[0]*8),
|
|
"Unexpected source lock requirements")
|
|
row = copy.deepcopy(source)
|
|
row["ID"] = CLIENT_ID
|
|
for name in SLOTS:
|
|
row[name][0], row[name][2] = row[name][2], row[name][0]
|
|
return row
|
|
|
|
def certify_physical(before, after, added):
|
|
# Pinned WDC5 table. Compare the entire native member with one append to the
|
|
# direct-record block and one append to its noninline ID list. The full
|
|
# original copy table, strings, row order and field descriptors stay exact.
|
|
require(before.startswith(b"WDC5"), "Expected WDC5 native member")
|
|
require(struct.unpack_from("<I", before, 200) == (1,), "Expected one native section")
|
|
header = struct.unpack_from("<Q8I", before, 204)
|
|
require(header == (0,384,304,2,0,1216,0,0,26), "Unexpected native section layout")
|
|
require(struct.unpack_from("<I", before, 144) == (68,), "Unexpected Lock record width")
|
|
start, records, strings, id_bytes = header[1], header[2], header[3], header[5]
|
|
records_end = start + 68 * records
|
|
ids_start = records_end + strings
|
|
ids_end = ids_start + id_bytes
|
|
header_bytes = bytearray(before[:start])
|
|
for offset, value in ((136, records+1), (164, CLIENT_ID), (216, records+1), (228, id_bytes+4)):
|
|
struct.pack_into("<I", header_bytes, offset, value)
|
|
record = struct.pack("<I8I8H8B8B", added["Flags"], *added["Index"], *added["Skill"],
|
|
*added["Type"], *added["Action"])
|
|
expected = b"".join((header_bytes, before[start:records_end], record, before[records_end:ids_end],
|
|
struct.pack("<I", CLIENT_ID), before[ids_end:]))
|
|
require(after == expected, "DBCD output changed data outside the reviewed insertions")
|
|
|
|
def build(before_path, output, tool, definitions):
|
|
require(before_path.name == "Lock.db2", "DBCD requires an input named Lock.db2")
|
|
require(output.resolve() != before_path.resolve(), "Output must be separate from the release input")
|
|
original = before_path.read_bytes()
|
|
require(sha(original) == PARENT_SHA256, "Unreviewed parent Lock.db2")
|
|
before = dump(tool, definitions, before_path.parent)
|
|
require(len(before) == 330 and max(before) == 4581 and CLIENT_ID not in before,
|
|
"Unexpected parent row inventory")
|
|
new = updated_row(before[SOURCE_ID])
|
|
output.parent.mkdir(parents=True, exist_ok=True)
|
|
with tempfile.TemporaryDirectory(dir=output.parent) as scratch:
|
|
scratch = Path(scratch)
|
|
rows_file = scratch / "rows.json"
|
|
rows_file.write_text(json.dumps([new]) + "\n")
|
|
generated = scratch / "Lock.db2"
|
|
process = subprocess.run([tool, "addrows", before_path.parent, definitions, "Lock", rows_file, generated],
|
|
capture_output=True, text=True, check=True)
|
|
require(dump(tool, definitions, scratch) == {**before, CLIENT_ID:new},
|
|
"Independent DBCD reload differs from the original rows and one requested addition")
|
|
result = generated.read_bytes()
|
|
certify_physical(original, result, new)
|
|
os.replace(generated, output)
|
|
return dict(file_data_id=1343608, asset_path="dbfilesclient/lock.db2",
|
|
parent_build="7cf7aeb8c4a36c5ab4238694ae0f00aa",
|
|
before_sha256=sha(original), after_sha256=sha(result),
|
|
before_ckey=hashlib.md5(original).hexdigest(), after_ckey=hashlib.md5(result).hexdigest(),
|
|
before_bytes=len(original), after_bytes=len(result), original_logical_rows=len(before),
|
|
final_logical_rows=len(before)+1, original_copy_rows=26, final_copy_rows=26,
|
|
stock_row=before[SOURCE_ID], added_row=new, dbcd_result=process.stdout.strip())
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--before", type=Path, required=True)
|
|
parser.add_argument("--out", type=Path, required=True)
|
|
parser.add_argument("--db2bake", type=Path, required=True)
|
|
parser.add_argument("--definitions", type=Path, required=True)
|
|
args = parser.parse_args()
|
|
receipt = build(args.before, args.out, args.db2bake, args.definitions)
|
|
print(json.dumps(receipt, indent=2))
|
|
|
|
if __name__ == "__main__":
|
|
main()
|