Files
WoW-Hybrid-gateway/ops/hunter_aspect_rows.py

135 lines
5.9 KiB
Python

#!/usr/bin/env python3
"""Generate Db2Bake setrows/addrows inputs for the 70334 Hunter stance bar."""
import argparse
import hashlib
import json
import subprocess
import tempfile
from pathlib import Path
from spell_passive_compat import BUILD, TABLES, db2_header
from turtle_data import DBC, spell_layout
from warrior_stance_rows import TABLE as SHAPESHIFT_TABLE, LAYOUT as SHAPESHIFT_LAYOUT
HUNTER_SPELL_FAMILY = 9
USE_SHAPESHIFT_BAR = 0x10
# Match the project's client presentation row namespace (ops/octo_spell_presentation.py).
NEW_ORDER_ID_BASE = 2_600_000
def hunter_orders(spells, fields):
result = {}
for row in spells.rows:
if row[fields['SpellClassSet']] != HUNTER_SPELL_FAMILY:
continue
if not row[fields['AttributesExB']] & USE_SHAPESHIFT_BAR:
continue
spell = row[fields['ID']]
order = row[fields['StanceBarOrder']]
if order == 0xffffffff:
order = -1
if not -1 <= order <= 127:
raise ValueError(f'Hunter spell {spell} has unsupported stance order {order}')
result[spell] = (order, row[fields['ShapeshiftMask']], row[fields['ShapeshiftExclude']])
if not result:
raise ValueError('Octo Spell.dbc has no Hunter spells marked for the stance bar')
return result
def prepare(orders, misc_rows, shape_rows):
misc = {}
for row in misc_rows:
spell = row['SpellID']
if spell not in orders:
continue
if spell in misc or row['DifficultyID'] != 0:
raise ValueError(f'Expected one base SpellMisc row for Hunter spell {spell}')
misc[spell] = row
shapes = {}
occupied = set()
for row in shape_rows:
occupied.add(row['ID'])
spell = row['SpellID']
if spell in orders:
if spell in shapes:
raise ValueError(f'Duplicate SpellShapeshift relationship for spell {spell}')
shapes[spell] = row
misc_edits, order_edits, additions = [], [], []
for spell, (order, mask, exclude) in sorted(orders.items()):
if spell not in misc:
raise ValueError(f'Missing base SpellMisc row for Hunter spell {spell}')
row = misc[spell]
attributes = list(row['Attributes'])
if len(attributes) != 17:
raise ValueError(f'Unexpected Build 70334 SpellMisc.Attributes width for {spell}')
if not attributes[2] & USE_SHAPESHIFT_BAR:
attributes[2] |= USE_SHAPESHIFT_BAR
misc_edits.append({'ID': row['ID'], 'Attributes': attributes})
if spell in shapes:
shape = shapes[spell]
if shape['StanceBarOrder'] != order:
order_edits.append({'ID': shape['ID'], 'StanceBarOrder': order})
elif mask or exclude:
raise ValueError(f'Missing SpellShapeshift row with form restrictions for {spell}')
elif order != 0:
# Devotion Aura is the native zero-order control with no child.
# Nonzero orders, including the signed -1, require an explicit child.
ident = NEW_ORDER_ID_BASE + spell
if ident in occupied or ident > 0x7fffffff:
raise ValueError(f'Unavailable SpellShapeshift ID {ident} for spell {spell}')
additions.append({'ID': ident, 'SpellID': spell, 'StanceBarOrder': order,
'ShapeshiftExclude': [0, 0], 'ShapeshiftMask': [0, 0]})
return {'SpellMisc.setrows': misc_edits, 'SpellShapeshift.setrows': order_edits,
'SpellShapeshift.addrows': additions}
def generate(source, parent, definitions, reader, out):
spells = DBC(source, fields=173)
orders = hunter_orders(spells, spell_layout(definitions / 'Spell.dbd'))
layouts = {'SpellMisc': TABLES['SpellMisc'][:2],
'SpellShapeshift': (SHAPESHIFT_TABLE, SHAPESHIFT_LAYOUT)}
parents = {}
for table, (table_hash, layout_hash) in layouts.items():
path = parent / f'{table}.db2'
db2_header(path, table_hash, layout_hash)
parents[table] = hashlib.sha256(path.read_bytes()).hexdigest()
with tempfile.TemporaryDirectory() as temp:
def export(table, fields):
output = Path(temp) / f'{table}.json'
subprocess.run(['dotnet', str(reader), str(parent), str(definitions),
table, str(output), *fields], check=True)
return json.loads(output.read_bytes())
where = '--where=SpellID:' + ','.join(map(str, sorted(orders)))
misc = export('SpellMisc', [where, 'SpellID', 'DifficultyID', 'Attributes'])
shapes = export('SpellShapeshift', ['SpellID', 'StanceBarOrder'])
batches = prepare(orders, misc, shapes)
result = {'build': BUILD, 'source_spell_sha256': spells.digest(),
'parent_sha256': parents,
'source_orders': {spell: values[0] for spell, values in sorted(orders.items())},
'counts': {key: len(rows) for key, rows in batches.items()}}
# Each output directory represents one complete, pinned parent.
with tempfile.TemporaryDirectory(dir=out.parent) as temp:
staged = Path(temp) / 'rows'
staged.mkdir()
for name, data in {**batches, 'manifest': result}.items():
(staged / f'{name}.json').write_text(json.dumps(data, indent=2) + '\n')
if out.exists():
raise FileExistsError(out)
staged.rename(out)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('source', type=Path, help='Octo client Spell.dbc')
parser.add_argument('parent', type=Path, help='directory of current enUS parent DB2s')
parser.add_argument('definitions', type=Path, help='WoWDBDefs definitions directory')
parser.add_argument('reader', type=Path, help='project DB2Gameplay.dll')
parser.add_argument('out', type=Path, help='new directory for Db2Bake rows and source hashes')
args = parser.parse_args()
generate(args.source, args.parent, args.definitions, args.reader, args.out)