lycheedev 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/lycheedev.js +257 -0
- package/package.json +42 -0
- package/src/addon.js +87 -0
- package/src/config.js +64 -0
- package/src/doctor.js +100 -0
- package/src/install.js +131 -0
- package/src/paths.js +93 -0
- package/src/python.js +107 -0
- package/src/skill.js +48 -0
- package/src/wow.js +110 -0
- package/vendor/addon/Core/Bootstrap.lua +131 -0
- package/vendor/addon/Core/Clients/Mainline.lua +9 -0
- package/vendor/addon/Core/Clients/Mists.lua +9 -0
- package/vendor/addon/Core/Clients/Titan.lua +9 -0
- package/vendor/addon/Core/Compatibility.lua +43 -0
- package/vendor/addon/Core/Database.lua +859 -0
- package/vendor/addon/Core/Inspector.lua +647 -0
- package/vendor/addon/Core/Locale.lua +307 -0
- package/vendor/addon/Core/Locale_enUS.lua +318 -0
- package/vendor/addon/Core/Safety.lua +42 -0
- package/vendor/addon/Core/Serializer.lua +324 -0
- package/vendor/addon/Libs/AutomationQR.lua +1176 -0
- package/vendor/addon/Lychee Dev_Mainline.toc +43 -0
- package/vendor/addon/Lychee Dev_Mists.toc +43 -0
- package/vendor/addon/Lychee Dev_Wrath.toc +43 -0
- package/vendor/addon/Media/GitHub.png +0 -0
- package/vendor/addon/Media/Logo.png +0 -0
- package/vendor/addon/Modules/Automation/Controller.lua +757 -0
- package/vendor/addon/Modules/Automation/Report.lua +409 -0
- package/vendor/addon/Modules/Automation/auto/auto.lua +12 -0
- package/vendor/addon/Modules/Diagnostics.lua +238 -0
- package/vendor/addon/Modules/Events/Catalog.lua +184 -0
- package/vendor/addon/Modules/Events/CatalogData_Mainline.lua +1788 -0
- package/vendor/addon/Modules/Events/CatalogData_Mists.lua +1489 -0
- package/vendor/addon/Modules/Events/CatalogData_Titan.lua +1492 -0
- package/vendor/addon/Modules/Events/Monitor.lua +181 -0
- package/vendor/addon/Modules/FunctionTrace.lua +85 -0
- package/vendor/addon/Modules/ObjectInspector.lua +329 -0
- package/vendor/addon/UI/AutomationOverlay.lua +183 -0
- package/vendor/addon/UI/Export.lua +140 -0
- package/vendor/addon/UI/Features.lua +890 -0
- package/vendor/addon/UI/MainWindow.lua +1145 -0
- package/vendor/addon/UI/Pages/About.lua +223 -0
- package/vendor/addon/UI/Pages/Automation.lua +417 -0
- package/vendor/addon/UI/Pages/Diagnostics.lua +289 -0
- package/vendor/addon/UI/Pages/ExportRecords.lua +387 -0
- package/vendor/addon/UI/Pages/Object.lua +573 -0
- package/vendor/addon/UI/Pages/Trace.lua +165 -0
- package/vendor/skill/SKILL.md +94 -0
- package/vendor/skill/agents/openai.yaml +4 -0
- package/vendor/skill/references/automation.md +327 -0
- package/vendor/skill/references/runtime-investigations.md +97 -0
- package/vendor/skill/scripts/automation/__init__.py +1 -0
- package/vendor/skill/scripts/automation/registry.py +516 -0
- package/vendor/skill/scripts/automation/saved_variables.py +438 -0
- package/vendor/skill/scripts/automation/session.py +207 -0
- package/vendor/skill/scripts/automation/windows.py +573 -0
- package/vendor/skill/scripts/automation.py +865 -0
- package/vendor/skill/scripts/requirements.txt +13 -0
- package/vendor/skill/scripts/selftest_offline.py +981 -0
|
@@ -0,0 +1,573 @@
|
|
|
1
|
+
"""Window identity, capture and controlled command input for Windows.
|
|
2
|
+
|
|
3
|
+
Window identity is never guessed from the title alone: every use confirms the
|
|
4
|
+
HWND against pid, process executable path and (best effort) the process
|
|
5
|
+
creation time. Command input uses the controlled focus-verify-paste-confirm
|
|
6
|
+
sequence; WGC capture and QR decoding are optional heavy imports that fail
|
|
7
|
+
with explicit messages when their packages are missing.
|
|
8
|
+
|
|
9
|
+
The capture session is long lived: a task can run far longer than one frame, so
|
|
10
|
+
``open_notice_capture`` returns a context manager that keeps one WGC session
|
|
11
|
+
open, keeps only the newest ROI, and is polled by the caller until its own
|
|
12
|
+
deadline expires. Every ctypes entry point used here has an explicit prototype,
|
|
13
|
+
because on 64-bit hosts the default ``c_int`` return type truncates handles and
|
|
14
|
+
pointers.
|
|
15
|
+
|
|
16
|
+
Verified behavior gaps (design section 11): SendInput success does not prove
|
|
17
|
+
the game processed the command, and minimization/occlusion/HDR support must be
|
|
18
|
+
measured before being claimed. The host log, not this module, decides when a
|
|
19
|
+
reload may be repeated.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
from __future__ import annotations
|
|
23
|
+
|
|
24
|
+
import ctypes
|
|
25
|
+
import ctypes.wintypes as wt
|
|
26
|
+
import time
|
|
27
|
+
from collections import deque
|
|
28
|
+
from dataclasses import dataclass
|
|
29
|
+
|
|
30
|
+
user32 = ctypes.WinDLL("user32", use_last_error=True)
|
|
31
|
+
kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)
|
|
32
|
+
|
|
33
|
+
INPUT_KEYBOARD = 1
|
|
34
|
+
KEYEVENTF_KEYUP = 0x0002
|
|
35
|
+
VK_CONTROL = 0x11
|
|
36
|
+
VK_RETURN = 0x0D
|
|
37
|
+
VK_V = 0x56
|
|
38
|
+
VK_MENU = 0x12
|
|
39
|
+
WM_CHAR = 0x0102
|
|
40
|
+
WM_KEYDOWN = 0x0100
|
|
41
|
+
WM_KEYUP = 0x0101
|
|
42
|
+
|
|
43
|
+
PROCESS_QUERY_LIMITED_INFORMATION = 0x1000
|
|
44
|
+
|
|
45
|
+
# Top-left notice region as (top, left, bottom, right) fractions of the captured
|
|
46
|
+
# frame. The completion notice is drawn at the top-left corner (see
|
|
47
|
+
# UI/AutomationOverlay.lua), so the scan area covers that corner with room for
|
|
48
|
+
# the QR quiet zone and a few DPI increments. Recalibrate here when the notice
|
|
49
|
+
# anchor, DPI, window size or capture origin changes (design section 6.1).
|
|
50
|
+
NOTICE_ROI_FRACTIONS = (0.0, 0.0, 0.30, 0.30)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class WindowIdentityError(Exception):
|
|
54
|
+
pass
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class InputError(Exception):
|
|
58
|
+
pass
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class CaptureDependencyError(Exception):
|
|
62
|
+
pass
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
@dataclass
|
|
66
|
+
class WindowIdentity:
|
|
67
|
+
hwnd: int
|
|
68
|
+
pid: int
|
|
69
|
+
exe_path: str | None
|
|
70
|
+
title: str
|
|
71
|
+
|
|
72
|
+
def matches(self, *, pid: int | None = None, exe_path: str | None = None) -> bool:
|
|
73
|
+
if pid is not None and self.pid != pid:
|
|
74
|
+
return False
|
|
75
|
+
if exe_path is not None and (self.exe_path or "").lower() != exe_path.lower():
|
|
76
|
+
return False
|
|
77
|
+
return True
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
class KEYBDINPUT(ctypes.Structure):
|
|
81
|
+
_fields_ = [("wVk", wt.WORD), ("wScan", wt.WORD), ("dwFlags", wt.DWORD),
|
|
82
|
+
("time", wt.DWORD), ("dwExtraInfo", ctypes.c_void_p)]
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
class MOUSEINPUT(ctypes.Structure):
|
|
86
|
+
_fields_ = [("dx", wt.LONG), ("dy", wt.LONG), ("mouseData", wt.DWORD),
|
|
87
|
+
("dwFlags", wt.DWORD), ("time", wt.DWORD),
|
|
88
|
+
("dwExtraInfo", ctypes.c_void_p)]
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
class HARDWAREINPUT(ctypes.Structure):
|
|
92
|
+
_fields_ = [("uMsg", wt.DWORD), ("wParamL", wt.WORD), ("wParamH", wt.WORD)]
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
class _INPUTUNION(ctypes.Union):
|
|
96
|
+
# The union size must follow the real largest member: 32 bytes on 64-bit and
|
|
97
|
+
# 24 bytes on 32-bit, which is why the padding shortcut was wrong.
|
|
98
|
+
_fields_ = [("ki", KEYBDINPUT), ("mi", MOUSEINPUT), ("hi", HARDWAREINPUT)]
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
class INPUT(ctypes.Structure):
|
|
102
|
+
_anonymous_ = ("u",)
|
|
103
|
+
_fields_ = [("type", wt.DWORD), ("u", _INPUTUNION)]
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
INPUT_SIZE = ctypes.sizeof(INPUT)
|
|
107
|
+
|
|
108
|
+
WNDENUMPROC = ctypes.WINFUNCTYPE(wt.BOOL, wt.HWND, wt.LPARAM)
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def _prototype(function, restype, argtypes):
|
|
112
|
+
function.restype = restype
|
|
113
|
+
function.argtypes = list(argtypes)
|
|
114
|
+
return function
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
_prototype(kernel32.OpenProcess,
|
|
118
|
+
wt.HANDLE, (wt.DWORD, wt.BOOL, wt.DWORD))
|
|
119
|
+
_prototype(kernel32.QueryFullProcessImageNameW,
|
|
120
|
+
wt.BOOL, (wt.HANDLE, wt.DWORD, wt.LPWSTR, ctypes.POINTER(wt.DWORD)))
|
|
121
|
+
_prototype(kernel32.CloseHandle, wt.BOOL, (wt.HANDLE,))
|
|
122
|
+
_prototype(kernel32.GlobalLock, ctypes.c_void_p, (ctypes.c_void_p,))
|
|
123
|
+
_prototype(kernel32.GlobalUnlock, wt.BOOL, (ctypes.c_void_p,))
|
|
124
|
+
_prototype(kernel32.GlobalAlloc, ctypes.c_void_p, (wt.UINT, ctypes.c_size_t))
|
|
125
|
+
_prototype(kernel32.GlobalFree, ctypes.c_void_p, (ctypes.c_void_p,))
|
|
126
|
+
_prototype(ctypes.memmove, ctypes.c_void_p,
|
|
127
|
+
(ctypes.c_void_p, ctypes.c_void_p, ctypes.c_size_t))
|
|
128
|
+
|
|
129
|
+
_prototype(user32.IsWindow, wt.BOOL, (wt.HWND,))
|
|
130
|
+
_prototype(user32.IsWindowVisible, wt.BOOL, (wt.HWND,))
|
|
131
|
+
_prototype(user32.GetWindowThreadProcessId,
|
|
132
|
+
wt.DWORD, (wt.HWND, ctypes.POINTER(wt.DWORD)))
|
|
133
|
+
_prototype(user32.GetWindowTextLengthW, ctypes.c_int, (wt.HWND,))
|
|
134
|
+
_prototype(user32.GetWindowTextW, ctypes.c_int, (wt.HWND, wt.LPWSTR, ctypes.c_int))
|
|
135
|
+
_prototype(user32.EnumWindows, wt.BOOL, (WNDENUMPROC, wt.LPARAM))
|
|
136
|
+
_prototype(user32.GetForegroundWindow, wt.HWND, ())
|
|
137
|
+
_prototype(user32.SetForegroundWindow, wt.BOOL, (wt.HWND,))
|
|
138
|
+
_prototype(user32.OpenClipboard, wt.BOOL, (wt.HWND,))
|
|
139
|
+
_prototype(user32.CloseClipboard, wt.BOOL, ())
|
|
140
|
+
_prototype(user32.EmptyClipboard, wt.BOOL, ())
|
|
141
|
+
_prototype(user32.IsClipboardFormatAvailable, wt.BOOL, (wt.UINT,))
|
|
142
|
+
_prototype(user32.GetClipboardData, wt.HANDLE, (wt.UINT,))
|
|
143
|
+
_prototype(user32.SetClipboardData, wt.HANDLE, (wt.UINT, wt.HANDLE))
|
|
144
|
+
_prototype(user32.SendInput, wt.UINT, (wt.UINT, ctypes.POINTER(INPUT), ctypes.c_int))
|
|
145
|
+
_prototype(user32.keybd_event, None, (wt.BYTE, wt.BYTE, wt.DWORD, ctypes.c_void_p))
|
|
146
|
+
_prototype(user32.MapVirtualKeyW, wt.UINT, (wt.UINT, wt.UINT))
|
|
147
|
+
_prototype(user32.PostMessageW, wt.BOOL,
|
|
148
|
+
(wt.HWND, wt.UINT, wt.WPARAM, wt.LPARAM))
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def _query_process_image(pid: int) -> str | None:
|
|
152
|
+
handle = kernel32.OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, False, pid)
|
|
153
|
+
if not handle:
|
|
154
|
+
return None
|
|
155
|
+
try:
|
|
156
|
+
size = wt.DWORD(1024)
|
|
157
|
+
buffer = ctypes.create_unicode_buffer(size.value)
|
|
158
|
+
if kernel32.QueryFullProcessImageNameW(handle, 0, buffer, ctypes.byref(size)):
|
|
159
|
+
return buffer.value
|
|
160
|
+
return None
|
|
161
|
+
finally:
|
|
162
|
+
kernel32.CloseHandle(handle)
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def identify_window(hwnd: int) -> WindowIdentity:
|
|
166
|
+
if not user32.IsWindow(hwnd):
|
|
167
|
+
raise WindowIdentityError(f"hwnd {hwnd} is no longer a live window")
|
|
168
|
+
pid = wt.DWORD(0)
|
|
169
|
+
user32.GetWindowThreadProcessId(hwnd, ctypes.byref(pid))
|
|
170
|
+
length = user32.GetWindowTextLengthW(hwnd)
|
|
171
|
+
buffer = ctypes.create_unicode_buffer(max(length + 1, 1))
|
|
172
|
+
user32.GetWindowTextW(hwnd, buffer, length + 1)
|
|
173
|
+
return WindowIdentity(
|
|
174
|
+
hwnd=hwnd,
|
|
175
|
+
pid=pid.value,
|
|
176
|
+
exe_path=_query_process_image(pid.value),
|
|
177
|
+
title=buffer.value,
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def confirm_window(hwnd: int, *, pid: int | None = None, exe_path: str | None = None) -> WindowIdentity:
|
|
182
|
+
"""Re-verify the window identity immediately before any use."""
|
|
183
|
+
|
|
184
|
+
identity = identify_window(hwnd)
|
|
185
|
+
if not identity.matches(pid=pid, exe_path=exe_path):
|
|
186
|
+
raise WindowIdentityError(
|
|
187
|
+
f"window identity changed: hwnd {hwnd} now pid {identity.pid} "
|
|
188
|
+
f"exe {identity.exe_path!r}")
|
|
189
|
+
return identity
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def find_windows_by_pid(pid: int) -> list[WindowIdentity]:
|
|
193
|
+
found: list[WindowIdentity] = []
|
|
194
|
+
|
|
195
|
+
@WNDENUMPROC
|
|
196
|
+
def callback(hwnd, _lparam):
|
|
197
|
+
window_pid = wt.DWORD(0)
|
|
198
|
+
user32.GetWindowThreadProcessId(hwnd, ctypes.byref(window_pid))
|
|
199
|
+
if window_pid.value == pid and user32.IsWindowVisible(hwnd):
|
|
200
|
+
try:
|
|
201
|
+
found.append(identify_window(hwnd))
|
|
202
|
+
except WindowIdentityError:
|
|
203
|
+
pass
|
|
204
|
+
return True
|
|
205
|
+
|
|
206
|
+
user32.EnumWindows(callback, 0)
|
|
207
|
+
return found
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
def is_foreground(hwnd: int) -> bool:
|
|
211
|
+
return user32.GetForegroundWindow() == hwnd
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
def _send_key(vk: int, *, ctrl: bool = False) -> None:
|
|
215
|
+
def make(vkey: int, flags: int) -> INPUT:
|
|
216
|
+
item = INPUT()
|
|
217
|
+
item.type = INPUT_KEYBOARD
|
|
218
|
+
item.ki = KEYBDINPUT(vkey, 0, flags, 0, None)
|
|
219
|
+
return item
|
|
220
|
+
|
|
221
|
+
sequence: list[INPUT] = []
|
|
222
|
+
if ctrl:
|
|
223
|
+
sequence.append(make(VK_CONTROL, 0))
|
|
224
|
+
sequence.append(make(vk, 0))
|
|
225
|
+
sequence.append(make(vk, KEYEVENTF_KEYUP))
|
|
226
|
+
if ctrl:
|
|
227
|
+
sequence.append(make(VK_CONTROL, KEYEVENTF_KEYUP))
|
|
228
|
+
array = (INPUT * len(sequence))(*sequence)
|
|
229
|
+
sent = user32.SendInput(len(sequence), array, ctypes.sizeof(INPUT))
|
|
230
|
+
if sent != len(sequence):
|
|
231
|
+
raise InputError("SendInput did not accept the full key sequence")
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def send_command(hwnd: int, command: str, *, pid: int | None = None,
|
|
235
|
+
exe_path: str | None = None, clipboard=None,
|
|
236
|
+
mode: str = "foreground") -> None:
|
|
237
|
+
"""Deliver one slash command through the game chat box.
|
|
238
|
+
|
|
239
|
+
``mode`` selects the injection strategy:
|
|
240
|
+
|
|
241
|
+
- ``"foreground"`` (default): focus the window, open chat with Return,
|
|
242
|
+
paste from the clipboard, submit with Return. Uses ``SendInput``, which
|
|
243
|
+
only reaches the foreground window, so the window is focused first.
|
|
244
|
+
- ``"messages"``: post the keystrokes straight into the target window's
|
|
245
|
+
message queue. This needs no foreground at all, but the client only
|
|
246
|
+
reacts when its chat edit box is already open and focused, and a client
|
|
247
|
+
that reads raw input may ignore posted messages entirely.
|
|
248
|
+
|
|
249
|
+
Both modes run the game's own command parser, so a slash command that the
|
|
250
|
+
client cannot run (locked UI, unknown command) still fails inside the game.
|
|
251
|
+
|
|
252
|
+
``clipboard`` may be injected for tests; it needs ``copy_text``,
|
|
253
|
+
``read_text`` and ``clear``. The previous clipboard content is restored
|
|
254
|
+
when it still belongs to this operation.
|
|
255
|
+
"""
|
|
256
|
+
|
|
257
|
+
if mode not in ("foreground", "messages"):
|
|
258
|
+
raise InputError(f"unknown delivery mode: {mode}")
|
|
259
|
+
|
|
260
|
+
if mode == "messages":
|
|
261
|
+
confirm_window(hwnd, pid=pid, exe_path=exe_path)
|
|
262
|
+
send_command_messages(hwnd, command)
|
|
263
|
+
return
|
|
264
|
+
|
|
265
|
+
if clipboard is None:
|
|
266
|
+
clipboard = Clipboard()
|
|
267
|
+
confirm_window(hwnd, pid=pid, exe_path=exe_path)
|
|
268
|
+
focus_window(hwnd, pid=pid, exe_path=exe_path)
|
|
269
|
+
|
|
270
|
+
previous = clipboard.read_text()
|
|
271
|
+
clipboard.copy_text(command)
|
|
272
|
+
if clipboard.read_text() != command:
|
|
273
|
+
raise InputError("the clipboard did not accept the command text")
|
|
274
|
+
try:
|
|
275
|
+
_send_key(VK_RETURN)
|
|
276
|
+
time.sleep(0.15)
|
|
277
|
+
_send_key(VK_V, ctrl=True)
|
|
278
|
+
time.sleep(0.15)
|
|
279
|
+
_send_key(VK_RETURN)
|
|
280
|
+
time.sleep(0.05)
|
|
281
|
+
finally:
|
|
282
|
+
if clipboard.read_text() == command:
|
|
283
|
+
if previous is None:
|
|
284
|
+
clipboard.clear()
|
|
285
|
+
else:
|
|
286
|
+
clipboard.copy_text(previous)
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
def focus_window(hwnd: int, *, pid: int | None = None,
|
|
290
|
+
exe_path: str | None = None, attempts: int = 3) -> None:
|
|
291
|
+
"""Bring ``hwnd`` to the foreground, or fail without sending any input.
|
|
292
|
+
|
|
293
|
+
``SetForegroundWindow`` is refused while another process owns the
|
|
294
|
+
foreground, which is the normal state after the user clicks elsewhere.
|
|
295
|
+
Windows releases that restriction when the foreground change follows user
|
|
296
|
+
input, so a one-shot ALT press is used as the documented unlock and the
|
|
297
|
+
result is always re-verified; a window that still is not foreground raises
|
|
298
|
+
instead of typing into whatever else has focus.
|
|
299
|
+
"""
|
|
300
|
+
|
|
301
|
+
for attempt in range(attempts):
|
|
302
|
+
if user32.SetForegroundWindow(hwnd):
|
|
303
|
+
time.sleep(0.10)
|
|
304
|
+
if is_foreground(hwnd):
|
|
305
|
+
return
|
|
306
|
+
if attempt == attempts - 1:
|
|
307
|
+
break
|
|
308
|
+
# A synthetic ALT is delivered by this process, so the next
|
|
309
|
+
# SetForegroundWindow is allowed through the foreground lock.
|
|
310
|
+
user32.keybd_event(VK_MENU, 0, 0, 0)
|
|
311
|
+
time.sleep(0.05)
|
|
312
|
+
user32.keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0)
|
|
313
|
+
time.sleep(0.15)
|
|
314
|
+
|
|
315
|
+
confirm_window(hwnd, pid=pid, exe_path=exe_path)
|
|
316
|
+
if not is_foreground(hwnd):
|
|
317
|
+
raise InputError(
|
|
318
|
+
"could not bring the game window to the foreground; click the game "
|
|
319
|
+
"window once, or use `send --mode messages`")
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
def _key_lparam(vk: int, *, up: bool = False) -> int:
|
|
323
|
+
"""Pack a WM_KEYDOWN/WM_KEYUP lParam for a virtual key."""
|
|
324
|
+
|
|
325
|
+
scan = user32.MapVirtualKeyW(vk, 0)
|
|
326
|
+
lparam = 1 | (scan << 16)
|
|
327
|
+
if up:
|
|
328
|
+
lparam |= 1 << 30 | 1 << 31
|
|
329
|
+
return lparam
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
def send_command_messages(hwnd: int, command: str, *, delay: float = 0.03) -> None:
|
|
333
|
+
"""Type ``command`` into ``hwnd`` by posting messages; no focus needed.
|
|
334
|
+
|
|
335
|
+
The chat edit box must already be open and focused in the game, because the
|
|
336
|
+
client routes posted keys to whatever widget has focus. Characters go out
|
|
337
|
+
as WM_CHAR (the client re-encodes them for IME), and Return submits.
|
|
338
|
+
"""
|
|
339
|
+
|
|
340
|
+
for char in command:
|
|
341
|
+
if not user32.PostMessageW(hwnd, WM_CHAR, ord(char), 0):
|
|
342
|
+
raise InputError("the client window rejected a character message")
|
|
343
|
+
time.sleep(delay)
|
|
344
|
+
if not user32.PostMessageW(hwnd, WM_KEYDOWN, VK_RETURN, _key_lparam(VK_RETURN)):
|
|
345
|
+
raise InputError("the client window rejected the submit key")
|
|
346
|
+
user32.PostMessageW(hwnd, WM_KEYUP, VK_RETURN, _key_lparam(VK_RETURN, up=True))
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
class Clipboard:
|
|
350
|
+
"""Minimal CF_UNICODETEXT clipboard via user32; no third-party dependency."""
|
|
351
|
+
|
|
352
|
+
CF_UNICODETEXT = 13
|
|
353
|
+
GMEM_MOVEABLE = 0x0002
|
|
354
|
+
|
|
355
|
+
def _open(self, retries: int = 8) -> None:
|
|
356
|
+
for attempt in range(retries):
|
|
357
|
+
if user32.OpenClipboard(0):
|
|
358
|
+
return
|
|
359
|
+
time.sleep(0.05 * (attempt + 1))
|
|
360
|
+
raise InputError("could not open the clipboard")
|
|
361
|
+
|
|
362
|
+
def read_text(self) -> str | None:
|
|
363
|
+
self._open()
|
|
364
|
+
try:
|
|
365
|
+
if not user32.IsClipboardFormatAvailable(self.CF_UNICODETEXT):
|
|
366
|
+
return None
|
|
367
|
+
handle = user32.GetClipboardData(self.CF_UNICODETEXT)
|
|
368
|
+
if not handle:
|
|
369
|
+
return None
|
|
370
|
+
pointer = kernel32.GlobalLock(handle)
|
|
371
|
+
if not pointer:
|
|
372
|
+
return None
|
|
373
|
+
try:
|
|
374
|
+
return ctypes.wstring_at(pointer)
|
|
375
|
+
finally:
|
|
376
|
+
kernel32.GlobalUnlock(handle)
|
|
377
|
+
finally:
|
|
378
|
+
user32.CloseClipboard()
|
|
379
|
+
|
|
380
|
+
def copy_text(self, text: str) -> None:
|
|
381
|
+
self._open()
|
|
382
|
+
try:
|
|
383
|
+
user32.EmptyClipboard()
|
|
384
|
+
size = (len(text) + 1) * 2
|
|
385
|
+
handle = kernel32.GlobalAlloc(self.GMEM_MOVEABLE, size)
|
|
386
|
+
if not handle:
|
|
387
|
+
raise InputError("GlobalAlloc failed for clipboard data")
|
|
388
|
+
pointer = kernel32.GlobalLock(handle)
|
|
389
|
+
if not pointer:
|
|
390
|
+
raise InputError("GlobalLock failed for clipboard data")
|
|
391
|
+
try:
|
|
392
|
+
source = ctypes.create_unicode_buffer(text)
|
|
393
|
+
ctypes.memmove(pointer, ctypes.cast(source, ctypes.c_void_p), size)
|
|
394
|
+
finally:
|
|
395
|
+
kernel32.GlobalUnlock(handle)
|
|
396
|
+
if not user32.SetClipboardData(self.CF_UNICODETEXT, handle):
|
|
397
|
+
kernel32.GlobalFree(handle)
|
|
398
|
+
raise InputError("SetClipboardData failed")
|
|
399
|
+
finally:
|
|
400
|
+
user32.CloseClipboard()
|
|
401
|
+
|
|
402
|
+
def clear(self) -> None:
|
|
403
|
+
self._open()
|
|
404
|
+
try:
|
|
405
|
+
user32.EmptyClipboard()
|
|
406
|
+
finally:
|
|
407
|
+
user32.CloseClipboard()
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
# --- capture (optional dependencies) -----------------------------------------
|
|
411
|
+
|
|
412
|
+
def load_decode_module():
|
|
413
|
+
"""Import zxing-cpp, the only QR reader this tooling supports."""
|
|
414
|
+
|
|
415
|
+
try:
|
|
416
|
+
import zxingcpp
|
|
417
|
+
except ImportError as error: # pragma: no cover - depends on host packages
|
|
418
|
+
raise CaptureDependencyError(
|
|
419
|
+
f"QR decoding dependency missing ({error}); install zxing-cpp 3.1.1"
|
|
420
|
+
) from error
|
|
421
|
+
if not callable(getattr(zxingcpp, "read_barcodes", None)):
|
|
422
|
+
raise CaptureDependencyError(
|
|
423
|
+
"installed zxing-cpp has no read_barcodes(); zxing-cpp 3.x is required")
|
|
424
|
+
return zxingcpp
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
def load_capture_modules():
|
|
428
|
+
"""Import windows-capture plus numpy for the live frame path."""
|
|
429
|
+
|
|
430
|
+
try:
|
|
431
|
+
import numpy
|
|
432
|
+
import windows_capture
|
|
433
|
+
except ImportError as error: # pragma: no cover - depends on host packages
|
|
434
|
+
raise CaptureDependencyError(
|
|
435
|
+
f"capture dependencies missing ({error}); install windows-capture "
|
|
436
|
+
"2.0.1 and numpy") from error
|
|
437
|
+
return numpy, windows_capture
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
class NoticeCapture:
|
|
441
|
+
"""One open WGC window capture keeping only the newest notice ROI.
|
|
442
|
+
|
|
443
|
+
The session stays open for as long as the caller needs it: a task can take
|
|
444
|
+
far longer than a single frame. ``latest_roi()`` returns the newest copied
|
|
445
|
+
ROI or ``None``; the frame buffer is copied before the native frame is
|
|
446
|
+
released. Use it as a context manager.
|
|
447
|
+
"""
|
|
448
|
+
|
|
449
|
+
def __init__(self, capture, numpy_module, *, hwnd: int,
|
|
450
|
+
roi_fractions=NOTICE_ROI_FRACTIONS):
|
|
451
|
+
self.capture = capture
|
|
452
|
+
self.hwnd = hwnd
|
|
453
|
+
self.roi_fractions = tuple(roi_fractions)
|
|
454
|
+
self.window_closed = False
|
|
455
|
+
self.last_frame_error: str | None = None
|
|
456
|
+
self._numpy = numpy_module
|
|
457
|
+
self._frames: deque = deque(maxlen=1)
|
|
458
|
+
self._control = None
|
|
459
|
+
self._closed = False
|
|
460
|
+
capture.frame_handler = self._on_frame
|
|
461
|
+
capture.closed_handler = self._on_closed
|
|
462
|
+
|
|
463
|
+
# -- windows-capture callbacks -----------------------------------------
|
|
464
|
+
|
|
465
|
+
def _frame_bgr(self, frame):
|
|
466
|
+
convert = getattr(frame, "convert_to_bgr", None)
|
|
467
|
+
if callable(convert):
|
|
468
|
+
return convert().frame_buffer
|
|
469
|
+
return frame.frame_buffer
|
|
470
|
+
|
|
471
|
+
def _on_frame(self, frame, _control=None) -> None:
|
|
472
|
+
try:
|
|
473
|
+
image = self._frame_bgr(frame)
|
|
474
|
+
height, width = int(image.shape[0]), int(image.shape[1])
|
|
475
|
+
top, left, bottom, right = self.roi_fractions
|
|
476
|
+
roi = image[int(height * top):int(height * bottom),
|
|
477
|
+
int(width * left):int(width * right)]
|
|
478
|
+
self._frames.append(self._numpy.ascontiguousarray(roi))
|
|
479
|
+
except Exception as error: # a bad frame must never stop the thread
|
|
480
|
+
self.last_frame_error = str(error)
|
|
481
|
+
|
|
482
|
+
def _on_closed(self) -> None:
|
|
483
|
+
self.window_closed = True
|
|
484
|
+
|
|
485
|
+
# -- lifecycle ---------------------------------------------------------
|
|
486
|
+
|
|
487
|
+
def start(self) -> NoticeCapture:
|
|
488
|
+
self._control = self.capture.start_free_threaded()
|
|
489
|
+
return self
|
|
490
|
+
|
|
491
|
+
def latest_roi(self):
|
|
492
|
+
return self._frames[-1] if self._frames else None
|
|
493
|
+
|
|
494
|
+
def close(self, timeout: float = 2.0) -> None:
|
|
495
|
+
if self._closed:
|
|
496
|
+
return
|
|
497
|
+
self._closed = True
|
|
498
|
+
control = self._control
|
|
499
|
+
if control is None:
|
|
500
|
+
return
|
|
501
|
+
try:
|
|
502
|
+
control.stop()
|
|
503
|
+
except Exception:
|
|
504
|
+
return
|
|
505
|
+
deadline = time.monotonic() + timeout
|
|
506
|
+
while time.monotonic() < deadline and not control.is_finished():
|
|
507
|
+
time.sleep(0.02)
|
|
508
|
+
|
|
509
|
+
def __enter__(self) -> NoticeCapture:
|
|
510
|
+
return self.start()
|
|
511
|
+
|
|
512
|
+
def __exit__(self, *_exc) -> bool:
|
|
513
|
+
self.close()
|
|
514
|
+
return False
|
|
515
|
+
|
|
516
|
+
|
|
517
|
+
def open_notice_capture(hwnd: int, *, pid: int | None = None,
|
|
518
|
+
exe_path: str | None = None,
|
|
519
|
+
interval: float = 0.1,
|
|
520
|
+
minimum_update_interval_ms: int | None = None,
|
|
521
|
+
roi_fractions=NOTICE_ROI_FRACTIONS) -> NoticeCapture:
|
|
522
|
+
"""Confirm the window identity, then open one WGC session for the notice.
|
|
523
|
+
|
|
524
|
+
The caller owns the returned session and must close it (the context manager
|
|
525
|
+
does that). Raises WindowIdentityError for a stale/reused hwnd and
|
|
526
|
+
CaptureDependencyError when the optional packages are missing.
|
|
527
|
+
"""
|
|
528
|
+
|
|
529
|
+
numpy_module, windows_capture = load_capture_modules()
|
|
530
|
+
confirm_window(hwnd, pid=pid, exe_path=exe_path)
|
|
531
|
+
if minimum_update_interval_ms is None:
|
|
532
|
+
minimum_update_interval_ms = max(int(round(interval * 1000)), 0)
|
|
533
|
+
kwargs = {"window_hwnd": hwnd}
|
|
534
|
+
if minimum_update_interval_ms:
|
|
535
|
+
kwargs["minimum_update_interval"] = minimum_update_interval_ms
|
|
536
|
+
try:
|
|
537
|
+
capture = windows_capture.WindowsCapture(**kwargs)
|
|
538
|
+
except TypeError:
|
|
539
|
+
# Older releases may not accept the update-interval keyword.
|
|
540
|
+
capture = windows_capture.WindowsCapture(window_hwnd=hwnd)
|
|
541
|
+
return NoticeCapture(capture, numpy_module, hwnd=hwnd,
|
|
542
|
+
roi_fractions=roi_fractions)
|
|
543
|
+
|
|
544
|
+
|
|
545
|
+
def decode_qr(image, zxingcpp_module=None) -> list[str]:
|
|
546
|
+
"""Decode QR payloads from one image; returns the raw strings found.
|
|
547
|
+
|
|
548
|
+
Only ``read_barcodes`` exists in zxing-cpp 3.x; it is restricted to QR codes
|
|
549
|
+
so the notice scan stays cheap, invalid results are dropped, and a
|
|
550
|
+
dependency without that entry point fails explicitly instead of raising a
|
|
551
|
+
bare AttributeError.
|
|
552
|
+
"""
|
|
553
|
+
|
|
554
|
+
module = zxingcpp_module if zxingcpp_module is not None else load_decode_module()
|
|
555
|
+
reader = getattr(module, "read_barcodes", None)
|
|
556
|
+
if not callable(reader):
|
|
557
|
+
raise CaptureDependencyError(
|
|
558
|
+
"installed zxing-cpp has no read_barcodes(); zxing-cpp 3.x is required")
|
|
559
|
+
formats = getattr(getattr(module, "BarcodeFormat", None), "QRCode", None)
|
|
560
|
+
try:
|
|
561
|
+
results = reader(image, formats=formats) if formats is not None else reader(image)
|
|
562
|
+
except TypeError:
|
|
563
|
+
results = reader(image)
|
|
564
|
+
payloads: list[str] = []
|
|
565
|
+
for result in results or ():
|
|
566
|
+
if getattr(result, "valid", True) is False:
|
|
567
|
+
continue
|
|
568
|
+
text = getattr(result, "text", None)
|
|
569
|
+
if isinstance(text, str) and text:
|
|
570
|
+
payloads.append(text)
|
|
571
|
+
return payloads
|
|
572
|
+
|
|
573
|
+
|