67 lines
2.5 KiB
Python
67 lines
2.5 KiB
Python
"""Export the 1.12 CharSections WDBC with source row IDs and all three texture slots."""
|
|
import argparse
|
|
from collections import Counter, defaultdict
|
|
import json
|
|
from pathlib import Path
|
|
|
|
from turtle_data import DBC
|
|
|
|
|
|
def read_sections(path):
|
|
table = DBC(path, fields=10)
|
|
if not table.strings or table.strings[0] != 0:
|
|
raise ValueError('CharSections string block must start with the empty name')
|
|
rows = []
|
|
for ident, race, sex, section, variation, color, first, second, third, flags in table.rows:
|
|
rows.append(dict(id=ident, race=race, sex=sex, section=section, variation=variation,
|
|
color=color, textures=[table.string(offset) for offset in (first, second, third)],
|
|
flags=flags))
|
|
return rows
|
|
|
|
|
|
def selector(row):
|
|
return tuple(row[k] for k in ('race', 'sex', 'section', 'variation', 'color'))
|
|
|
|
|
|
def compare_sections(effective, baseline):
|
|
"""Keep the row-ID delta and the selectable look delta independent of DBC string offsets."""
|
|
by_id = {r['id']: r for r in baseline}
|
|
by_selector = defaultdict(set)
|
|
for row in baseline:
|
|
by_selector[selector(row)].add((row['flags'], tuple(row['textures'])))
|
|
rows = []
|
|
for row in effective:
|
|
prior = by_id.get(row['id'])
|
|
variants = by_selector.get(selector(row))
|
|
if prior is None:
|
|
id_state = 'added'
|
|
else:
|
|
id_state = 'unchanged' if row == prior else 'changed'
|
|
if variants is None:
|
|
selector_state = 'added'
|
|
elif (row['flags'], tuple(row['textures'])) in variants:
|
|
selector_state = 'unchanged'
|
|
else:
|
|
selector_state = 'changed'
|
|
rows.append(dict(row, id_state=id_state, selector_state=selector_state))
|
|
ids = {r['id'] for r in effective}
|
|
return dict(rows=rows, removed=[r for r in baseline if r['id'] not in ids],
|
|
by_id=dict(Counter(r['id_state'] for r in rows)),
|
|
by_selector=dict(Counter(r['selector_state'] for r in rows)))
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('--effective', type=Path, required=True)
|
|
parser.add_argument('--base', type=Path, required=True)
|
|
parser.add_argument('--output', type=Path, required=True)
|
|
args = parser.parse_args()
|
|
result = compare_sections(read_sections(args.effective), read_sections(args.base))
|
|
with args.output.open('x', encoding='utf-8') as output:
|
|
json.dump(result, output, indent=1)
|
|
output.write('\n')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|