76 lines
3.2 KiB
Python
76 lines
3.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Client item IDs whose default appearance resolves to an ItemDisplayInfo row.
|
|
|
|
Q108: UnitData.VirtualItems / PlayerData.VisibleItems carrying an item the client
|
|
cannot resolve (no Item row, no modifier-0 ItemModifiedAppearance, or an
|
|
ItemAppearance whose display is missing) resolve to display 0. When such a
|
|
weapon slot replaces a drawn weapon, CUnitDisplay::SheatheHandItem asserts
|
|
(UnitDisplay.cpp 2329, "Invalid display ID 0"). Writers gate every emitted item
|
|
on this set.
|
|
|
|
Inputs: a directory with the client's Item, ItemAppearance,
|
|
ItemModifiedAppearance and ItemDisplayInfo DB2s (extracted from the installed
|
|
client, baked hotfixes included) and the generated item_render_profile.hpp,
|
|
whose ItemAppearances rows are served by the codec itself.
|
|
|
|
usage: client_renderable_items.py <db2-dir> <item_render_profile.hpp> <output.hpp>
|
|
"""
|
|
import re
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent / 'claude_recipe_parity'))
|
|
import dbd
|
|
|
|
|
|
def renderable(db2):
|
|
db2 = Path(db2)
|
|
items = set(dbd.table('Item', str(db2 / 'Item.db2'))['rows'])
|
|
displays = set(dbd.table('ItemDisplayInfo', str(db2 / 'ItemDisplayInfo.db2'))['rows'])
|
|
appearance = {i: r['ItemDisplayInfoID'] for i, r in
|
|
dbd.table('ItemAppearance', str(db2 / 'ItemAppearance.db2'))['rows'].items()}
|
|
out = set()
|
|
for r in dbd.table('ItemModifiedAppearance', str(db2 / 'ItemModifiedAppearance.db2'))['rows'].values():
|
|
if r.get('ItemAppearanceModifierID', 0) == 0 and r['ItemID'] in items and appearance.get(r['ItemAppearanceID']) in displays:
|
|
out.add(r['ItemID'])
|
|
return out
|
|
|
|
|
|
def profile_items(path):
|
|
text = Path(path).read_text()
|
|
block = re.search(r'ItemAppearances\[\]=\{(.*?)\};', text, re.S)
|
|
if not block:
|
|
raise SystemExit('ItemAppearances table not found')
|
|
return {int(a) for a, _ in re.findall(r'\{(\d+),(\d+)\}', block.group(1))}
|
|
|
|
|
|
def ranges(ids):
|
|
out = []
|
|
for i in sorted(ids):
|
|
if out and out[-1][1] + 1 == i:
|
|
out[-1][1] = i
|
|
else:
|
|
out.append([i, i])
|
|
return out
|
|
|
|
|
|
def main(db2, profile, output):
|
|
ids = renderable(db2) | profile_items(profile)
|
|
rows = ranges(ids)
|
|
body = ',\n'.join(','.join('{%d,%d}' % tuple(r) for r in rows[i:i + 10]) for i in range(0, len(rows), 10))
|
|
Path(output).write_text(
|
|
'// Generated by ops/client_renderable_items.py: client items whose default appearance has an ItemDisplayInfo row.\n'
|
|
'#pragma once\n#include <algorithm>\n#include <cstdint>\n#include <iterator>\n'
|
|
'namespace gateway::client_renderable_items {\n'
|
|
'struct Range {uint32_t first,last;};\ninline constexpr Range Ranges[]={\n' + body + '};\n'
|
|
'inline bool has(uint32_t item){if(!item)return false;'
|
|
'auto it=std::upper_bound(std::begin(Ranges),std::end(Ranges),item,[](uint32_t i,const Range& r){return i<r.first;});'
|
|
'return it!=std::begin(Ranges)&&item<=std::prev(it)->last;}\n'
|
|
'// Emitted visible/virtual item: unresolvable items render nothing instead of display 0.\n'
|
|
'inline uint32_t or_zero(uint32_t item){return has(item)?item:0;}\n}\n')
|
|
print('items', len(ids), 'ranges', len(rows))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main(*sys.argv[1:4])
|