记录 4-48 实测失败与耗时复盘,修复前台输入及 Jev 配置
This commit is contained in:
@@ -155,7 +155,51 @@ class AalcEngine(Engine):
|
||||
return (int(pos[0]), int(pos[1])) if pos else None
|
||||
|
||||
# ---- 动手 (AALC 输入后端,由 config.yaml 决定后台消息/前台输入) ----
|
||||
def _focus_game(self):
|
||||
handle = self.screen.handle
|
||||
try:
|
||||
handle.setForeground()
|
||||
except Exception:
|
||||
pass # Windows 可拒绝后台服务直接切换前台;下面通过输入线程重试。
|
||||
if handle.isActive:
|
||||
return
|
||||
import win32api
|
||||
import win32gui
|
||||
import win32process
|
||||
current = win32api.GetCurrentThreadId()
|
||||
foreground = win32gui.GetForegroundWindow()
|
||||
if not foreground:
|
||||
return
|
||||
threads = {win32process.GetWindowThreadProcessId(foreground)[0],
|
||||
win32process.GetWindowThreadProcessId(handle.hwnd)[0]} - {current}
|
||||
attached = []
|
||||
try:
|
||||
for target in sorted(threads):
|
||||
win32process.AttachThreadInput(current, target, True)
|
||||
attached.append(target)
|
||||
win32gui.BringWindowToTop(handle.hwnd)
|
||||
win32gui.SetActiveWindow(handle.hwnd)
|
||||
win32gui.SetFocus(handle.hwnd)
|
||||
handle.setForeground()
|
||||
finally:
|
||||
for target in reversed(attached):
|
||||
win32process.AttachThreadInput(current, target, False)
|
||||
|
||||
def _prepare_input(self):
|
||||
# 前台后端将输入发给系统当前窗口;服务启动后不一定仍是游戏。
|
||||
# 先确认焦点,获取失败时停止,避免把点击或按键发送到其他应用。
|
||||
if not self.cfg.simulator and self.cfg.win_input_type == 'foreground':
|
||||
handle = self.screen.handle
|
||||
if not handle.hwnd or handle.isMinimized:
|
||||
raise RuntimeError('前台输入需要可见的游戏窗口')
|
||||
if not handle.isActive:
|
||||
self._focus_game()
|
||||
time.sleep(0.15)
|
||||
if not handle.isActive:
|
||||
raise RuntimeError('无法激活游戏窗口,未发送输入')
|
||||
|
||||
def click(self, x, y, times=1):
|
||||
self._prepare_input()
|
||||
if not self.cfg.simulator and self.cfg.win_input_type == 'background':
|
||||
handler = self.auto.input_handler
|
||||
self.auto.wait_pause()
|
||||
@@ -169,6 +213,21 @@ class AalcEngine(Engine):
|
||||
handler.mouse_up(int(x), int(y))
|
||||
# Let Unity consume the release before another target is selected.
|
||||
time.sleep(0.12)
|
||||
elif not self.cfg.simulator and self.cfg.win_input_type == 'foreground':
|
||||
import pyautogui
|
||||
px, py = self.auto.input_handler.pos_offset(int(x), int(y))
|
||||
pyautogui.moveTo(px, py)
|
||||
# Give Unity a frame to update the pointer target before pressing.
|
||||
time.sleep(0.12)
|
||||
for _ in range(int(times)):
|
||||
pyautogui.mouseDown(px, py)
|
||||
try:
|
||||
# Slow Unity frames can miss a shorter foreground press.
|
||||
# Keep this below the game's long-press detail threshold.
|
||||
time.sleep(0.35)
|
||||
finally:
|
||||
pyautogui.mouseUp(px, py)
|
||||
time.sleep(0.12)
|
||||
else:
|
||||
self.auto.mouse_click(int(x), int(y), times=int(times))
|
||||
|
||||
@@ -176,6 +235,7 @@ class AalcEngine(Engine):
|
||||
k = str(key).lower()
|
||||
if k not in self.key_names:
|
||||
raise ValueError('AALC 不支持按键 %r, 可用: %s' % (key, ' '.join(self.key_names)))
|
||||
self._prepare_input()
|
||||
if not self.cfg.simulator and self.cfg.win_input_type == 'background':
|
||||
# Unity 可能在同一帧收到 down/up,导致快捷键被吞掉。
|
||||
handler = self.auto.input_handler
|
||||
@@ -186,6 +246,13 @@ class AalcEngine(Engine):
|
||||
time.sleep(0.12)
|
||||
finally:
|
||||
handler.key_up(k)
|
||||
elif not self.cfg.simulator and self.cfg.win_input_type == 'foreground':
|
||||
import pyautogui
|
||||
pyautogui.keyDown(k)
|
||||
try:
|
||||
time.sleep(0.12)
|
||||
finally:
|
||||
pyautogui.keyUp(k)
|
||||
else:
|
||||
self.auto.key_press(k)
|
||||
|
||||
@@ -194,8 +261,26 @@ class AalcEngine(Engine):
|
||||
for x, y in ((from_x, from_y), (to_x, to_y)):
|
||||
if not (0 <= x < img.width and 0 <= y < img.height):
|
||||
raise ValueError('拖动坐标超出游戏客户区 %sx%s' % (img.width, img.height))
|
||||
self._prepare_input()
|
||||
if not self.cfg.simulator and self.cfg.win_input_type == 'foreground':
|
||||
import pyautogui
|
||||
start = self.auto.input_handler.pos_offset(int(from_x), int(from_y))
|
||||
end = self.auto.input_handler.pos_offset(int(to_x), int(to_y))
|
||||
pyautogui.moveTo(*start)
|
||||
time.sleep(0.12)
|
||||
pyautogui.mouseDown(*start)
|
||||
try:
|
||||
if start == end:
|
||||
time.sleep(float(duration))
|
||||
else:
|
||||
pyautogui.moveTo(*end, duration=float(duration))
|
||||
time.sleep(0.12)
|
||||
finally:
|
||||
pyautogui.mouseUp(*end)
|
||||
return
|
||||
self.auto.mouse_drag(int(from_x), int(from_y), drag_time=float(duration),
|
||||
dx=int(to_x) - int(from_x), dy=int(to_y) - int(from_y))
|
||||
|
||||
def type_text(self, text):
|
||||
self._prepare_input()
|
||||
self.auto.input_text(str(text))
|
||||
|
||||
Reference in New Issue
Block a user