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

95 lines
6.3 KiB
Python

#!/usr/bin/env python3
"""Port Octo's StatCompare gear scanner into the native 70334 character and inspect windows.
Reads the Octo client's AddOns/StatCompare (read-only) and writes two WoWHybridUI files:
OctoStatCompareData.lua - enUS strings and the StatCompare effect/pattern tables
OctoStatCompareScanner.lua - StatScanner.lua (tooltip line scanner, set bonuses) on 70334 APIs
The presentation (Stats-pane "Gear Bonuses" and the Inspect comparison) is hand-written in OctoStatComparePane.lua.
Every rewrite is asserted to apply, so an Octo update that changes the source fails loudly.
"""
import argparse, hashlib
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
DEFAULT_SRC = Path('/home/praxis/Games/octo/drive_c/Octowow/Interface/AddOns/StatCompare')
OUT = ROOT / 'client_addons/WoWHybridUI'
SCANNER_HEADER = '''-- Generated by ops/port_octo_stat_compare.py from Octo StatCompare StatScanner.lua (sha256 %(sha)s).
-- Scan tooltips are private hidden GameTooltips. StatScanner_LinkSource(slot), when set, supplies the item link per
-- slot (used for the inspected player, whose items come from the gateway's approved inspect reply).
SCObjectTooltip = SCObjectTooltip or CreateFrame("GameTooltip", "SCObjectTooltip", UIParent, "GameTooltipTemplate")
SCItemTooltip = SCItemTooltip or CreateFrame("GameTooltip", "SCItemTooltip", UIParent, "GameTooltipTemplate")
SCObjectTooltip:Hide(); SCItemTooltip:Hide()
'''
def sub_exact(text, old, new, count=1, label=''):
n = text.count(old)
if n != count:
raise SystemExit(f'rewrite {label!r}: expected {count} matches of {old!r}, found {n}')
return text.replace(old, new)
def port(src):
loc = (src / 'Localization_enUS.lua').read_text(encoding='utf-8')
main = (src / 'StatCompare.lua').read_text(encoding='utf-8-sig')
scan = (src / 'StatScanner.lua').read_text(encoding='latin-1')
sha = {n: hashlib.sha256((src / n).read_bytes()).hexdigest() for n in ('Localization_enUS.lua', 'StatCompare.lua', 'StatScanner.lua')}
loc = sub_exact(loc, 'GetAddOnMetadata(STATCOMPARE_ADDON_NAME, "Version")', '"1.8.005"', 1, 'version')
lines = main.split('\n')
start = next(i for i, l in enumerate(lines) if l.startswith('STATCOMPARE_SETNAME_PATTERN'))
end = next(i for i, l in enumerate(lines) if l.startswith('STATCOMPARE_CATEGORIES'))
tables = '\n'.join(lines[start:end + 1])
# 70334 links carry empty fields (item:123::::) and may use the |cnIQn: colour form.
tables = sub_exact(tables, 'STATCOMPARE_ITEMLINK_PATTERN = "|cff(%x+)|Hitem:(%d+:%d+:%d+:%d+)|h%[(.-)%]|h|r";',
'STATCOMPARE_ITEMLINK_PATTERN = "|cff(%x+)|Hitem:([^|]+)|h%[(.-)%]|h|r";', 1, 'link pattern')
data = (f'-- Generated by ops/port_octo_stat_compare.py from Octo StatCompare Localization_enUS.lua (sha256 {sha["Localization_enUS.lua"]})\n'
f'-- and the effect tables of StatCompare.lua (sha256 {sha["StatCompare.lua"]}).\n'
+ loc + '\nSC_Localization_enUS()\n\n' + tables + '\n')
s = scan
s = ''.join(ch if ord(ch) < 128 else '' for ch in s) # one Chinese comment, mis-encoded in the source
s = sub_exact(s, 'getn(StatScanner_setsproperty)', '#StatScanner_setsproperty', 2, 'getn')
s = sub_exact(s, 'getglobal("SCItemTooltipTextLeft"..i)', '_G["SCItemTooltipTextLeft"..i]', 1, 'item lines')
s = sub_exact(s, 'getglobal("SCObjectTooltipTextLeft"..j)', '_G["SCObjectTooltipTextLeft"..j]', 1, 'object lines')
s = sub_exact(s, '\tSCItemTooltip:SetOwner(DressUpFrame, "ANCHOR_NONE");\n\tSCItemTooltip:SetPoint("TOPLEFT", DressUpFrame:GetName(), "TOPRIGHT", -30, -12);\n',
'\tSCItemTooltip:SetOwner(UIParent, "ANCHOR_NONE");\n', 1, 'dressup owner')
s = sub_exact(s, '\tSCItemTooltip:Show();\n', '', 1, 'item show')
s = sub_exact(s, 'local link = GetInventoryItemLink(sunit, i);',
'local link; if StatScanner_LinkSource then link = StatScanner_LinkSource(i) else link = GetInventoryItemLink(sunit, i) end', 1, 'link source')
s = sub_exact(s, 'local item = gsub(link, ".*(item:%d+:%d+:%d+:%d+).*", "%1", 1);',
'local item = string.match(link, "|H(item:[^|]+)|h") or link;', 1, 'hyperlink')
s = sub_exact(s, '\t\telse\n\t\t\tSCObjectTooltip:SetInventoryItem(sunit, i);\n',
'\t\telseif not StatScanner_LinkSource then\n\t\t\tSCObjectTooltip:SetInventoryItem(sunit, i);\n', 1, 'inventory fallback')
# An empty slot leaves the previous item's lines in the tooltip; clear them first.
s = sub_exact(s, '\t\tSCObjectTooltip:SetOwner(UIParent, "ANCHOR_NONE");\n',
'\t\tSCObjectTooltip:SetOwner(UIParent, "ANCHOR_NONE");\n\t\tSCObjectTooltip:ClearLines();\n', 1, 'clear lines')
s = sub_exact(s, '\t\titemName = SCObjectTooltipTextLeft1:GetText();\n\t\tlines = SCObjectTooltip:NumLines();\n',
'\t\tlines = SCObjectTooltip:NumLines();\n', 1, 'item name')
# A cached item record can still have an empty or name-only tooltip for this frame.
s = sub_exact(s, 'local sunit = unit and unit or "target";',
'local sunit = unit and unit or "target";\n\tlocal complete = true;', label='scan completeness')
s = sub_exact(s, 'lines = SCObjectTooltip:NumLines();',
'lines = SCObjectTooltip:NumLines();\n\t\tif link and lines < 2 then complete = false end',
label='pending tooltip')
s = sub_exact(s, '\n\tSCObjectTooltip:Hide()\nend', '\n\tSCObjectTooltip:Hide()\n\treturn complete\nend',
label='completed unit scan')
# The 1.12 crit helper reads the Attack spell tooltip; the panes take crit from the BCS engine instead.
a = s.index('function StatCompare_CharStats_GetCritChance()')
b = s.index('function StatScanner_ScanItem(link)')
s = s[:a] + s[b:]
for bad in ('getn(', 'getglobal(', 'DressUpFrame', 'this.', 'arg1'):
if bad in s: raise SystemExit(f'unported 1.12 call left in scanner: {bad}')
return {'OctoStatCompareData.lua': data, 'OctoStatCompareScanner.lua': SCANNER_HEADER % {'sha': sha['StatScanner.lua']} + s}
def main():
p = argparse.ArgumentParser(description=__doc__)
p.add_argument('--source', type=Path, default=DEFAULT_SRC)
p.add_argument('--out', type=Path, default=OUT)
a = p.parse_args()
for name, text in port(a.source).items():
(a.out / name).write_text(text, encoding='utf-8')
print(name, len(text))
if __name__ == '__main__':
main()