Files
WoW-Hybrid-gateway/ops/owned_action_button.py
2026-11-23 03:28:32 +02:00

75 lines
3.8 KiB
Python

"""Click the fixture's displayed Consecration button through the original UI."""
import io
import os
import re
import secrets
import subprocess
import time
from PIL import Image, ImageChops, ImageOps
def probe_commands(mark):
# The first report converts UI units with Blizzard PixelUtil's 768 px reference.
# The second is evaluated with the pointer over the reported center.
position = ("/run local b=ActionButton12 local x,y=b:GetCenter() local w,h=GetPhysicalScreenSize() "
"local k=b:GetEffectiveScale()*h/768 UIErrorsFrame:AddMessage(format("
f"'{mark}-%d-%d-%d-%d',x*k+.5,h-y*k+.5,w,h),1,0,1)")
state = ("/run local b=ActionButton12 local a=b.action local t,i=GetActionInfo(a)"
"local s,d=GetActionCooldown(a)UIErrorsFrame:AddMessage(format("
f"'{mark}-%d-%d-%d',a,t=='spell' and i or 0,GetMouseFoci()[1]==b and "
"IsUsableAction(a) and s+d<=GetTime() and 1 or 0),1,0,1)")
if max(len(position), len(state)) > 255:
raise ValueError("Button observation exceeds the client edit-box limit")
return position, state
def read_marker(pixels, mark, count):
if pixels.size != (1920, 1080):
raise RuntimeError("Unrecognized test window layout")
# UIErrorsFrame is top-center. Read only the magenta numeric line for this probe.
r, g, b = pixels.crop((430, 45, 1490, 320)).convert("RGB").split()
mask = ImageChops.subtract(ImageChops.darker(r, b), g).point(lambda v: 255 if v > 72 else 0)
box = mask.getbbox()
if box is None:
return None
mask = mask.crop(box)
img = ImageOps.expand(ImageOps.invert(mask).resize((mask.width*4, mask.height*4),
Image.Resampling.LANCZOS), 28, fill=255)
encoded = io.BytesIO()
img.save(encoded, format="PNG")
result = subprocess.run(["tesseract", "stdin", "stdout", "--psm", "6",
"-c", "tessedit_char_whitelist=0123456789-"], input=encoded.getvalue(),
capture_output=True, timeout=4, check=True, env=dict(os.environ, OMP_THREAD_LIMIT="1"))
rows = [r for r in result.stdout.decode().splitlines() if r.strip().startswith(mark + "-")]
if len(rows) != 1 or not re.fullmatch(rf"{mark}(?:-\d+){{{count}}}", rows[0].strip()):
return None
return [int(s) for s in rows[0].strip().split("-")[1:]]
def click_consecration(command, *, geometry, inspect, hover, click, clock=time.monotonic_ns):
if (command.get("frame"), command.get("slot"), command.get("spell_id")) != ("ActionButton12", 12, 26573):
raise ValueError("Expected the designated paladin's saved first-page Consecration slot")
# Different nonces ensure late geometry text cannot be accepted as action state.
mark = str(secrets.randbelow(899999) + 100000)
position, _ = probe_commands(mark)
x, y, width, height = inspect(position, mark, 4)
if geometry() != (width, height) or not (0 <= x < width and 0 <= y < height):
raise RuntimeError("Named button geometry and owned window do not agree")
hover(x, y)
state_mark = str(int(mark) + 1)
_, state = probe_commands(state_mark)
slot, spell, ready = inspect(state, state_mark, 3)
if slot != 12 or spell not in (26573, 20923):
raise RuntimeError("Named button contains another action, spell or rank")
if ready != 1:
raise RuntimeError("Consecration button failed hit-test, usability or cooldown check")
if geometry() != (width, height):
raise RuntimeError("Owned window resized after the action-button observation")
timestamp = clock()
click(x, y)
return dict(frame="ActionButton12", slot=slot, spell_id=26573, resolved_spell_id=spell,
visible=True, usable=True, cooldown_ready=True, input="left_click", x=x, y=y,
click_monotonic_ns=timestamp)