Fix background Unity input and shop boundaries; record login failure
This commit is contained in:
+73
-5
@@ -79,6 +79,19 @@ class AalcEngine(Engine):
|
|||||||
self.images_root = os.path.join(root, 'assets', 'images')
|
self.images_root = os.path.join(root, 'assets', 'images')
|
||||||
|
|
||||||
# ---- 看 ----
|
# ---- 看 ----
|
||||||
|
def show_game_background(self):
|
||||||
|
if self.cfg.simulator:
|
||||||
|
raise RuntimeError('后台恢复窗口仅支持 Windows 游戏客户端')
|
||||||
|
hwnd = self.screen.handle.hwnd
|
||||||
|
if not hwnd:
|
||||||
|
raise RuntimeError('未找到游戏窗口,未修改其他窗口')
|
||||||
|
import win32con
|
||||||
|
import win32gui
|
||||||
|
foreground = win32gui.GetForegroundWindow()
|
||||||
|
win32gui.ShowWindow(hwnd, win32con.SW_SHOWNOACTIVATE)
|
||||||
|
return {'visible': bool(win32gui.IsWindowVisible(hwnd)),
|
||||||
|
'foreground_changed': win32gui.GetForegroundWindow() != foreground}
|
||||||
|
|
||||||
def _grab(self, gray):
|
def _grab(self, gray):
|
||||||
if not self.cfg.simulator and not self.screen.handle.hwnd:
|
if not self.cfg.simulator and not self.screen.handle.hwnd:
|
||||||
raise RuntimeError('未找到游戏窗口 %r: 先启动游戏(run_task 会自动启动)' % self.cfg.game_title_name)
|
raise RuntimeError('未找到游戏窗口 %r: 先启动游戏(run_task 会自动启动)' % self.cfg.game_title_name)
|
||||||
@@ -198,19 +211,45 @@ class AalcEngine(Engine):
|
|||||||
if not handle.isActive:
|
if not handle.isActive:
|
||||||
raise RuntimeError('无法激活游戏窗口,未发送输入')
|
raise RuntimeError('无法激活游戏窗口,未发送输入')
|
||||||
|
|
||||||
|
def _background_mouse(self, message, x, y, held=False):
|
||||||
|
"""Deliver client coordinates without moving the user's system pointer."""
|
||||||
|
import win32api
|
||||||
|
import win32con
|
||||||
|
hwnd = self.screen.handle.hwnd
|
||||||
|
if not hwnd:
|
||||||
|
raise RuntimeError('未找到游戏窗口,未发送鼠标消息')
|
||||||
|
win32api.PostMessage(hwnd, message, win32con.MK_LBUTTON if held else 0,
|
||||||
|
win32api.MAKELONG(int(x), int(y)))
|
||||||
|
|
||||||
|
def _restore_background_position(self, position):
|
||||||
|
import win32con
|
||||||
|
import win32gui
|
||||||
|
win32gui.SetWindowPos(self.screen.handle.hwnd, 0, *position, 0, 0,
|
||||||
|
win32con.SWP_NOSIZE | win32con.SWP_NOZORDER |
|
||||||
|
win32con.SWP_NOACTIVATE)
|
||||||
|
|
||||||
def click(self, x, y, times=1):
|
def click(self, x, y, times=1):
|
||||||
self._prepare_input()
|
self._prepare_input()
|
||||||
if not self.cfg.simulator and self.cfg.win_input_type == 'background':
|
if not self.cfg.simulator and self.cfg.win_input_type in ('background', 'window_move'):
|
||||||
|
import win32con
|
||||||
handler = self.auto.input_handler
|
handler = self.auto.input_handler
|
||||||
self.auto.wait_pause()
|
self.auto.wait_pause()
|
||||||
for _ in range(int(times)):
|
for _ in range(int(times)):
|
||||||
handler.set_mouse_pos(int(x), int(y))
|
position = None
|
||||||
|
if self.cfg.win_input_type == 'window_move':
|
||||||
|
self.screen.handle.set_window_transparent(True)
|
||||||
|
position = handler._set_window_pos(int(x), int(y))
|
||||||
handler.set_active()
|
handler.set_active()
|
||||||
handler.mouse_down(int(x), int(y))
|
self._background_mouse(win32con.WM_MOUSEMOVE, x, y)
|
||||||
|
time.sleep(0.12)
|
||||||
|
self._background_mouse(win32con.WM_LBUTTONDOWN, x, y, held=True)
|
||||||
try:
|
try:
|
||||||
time.sleep(0.12)
|
time.sleep(0.12)
|
||||||
finally:
|
finally:
|
||||||
handler.mouse_up(int(x), int(y))
|
self._background_mouse(win32con.WM_LBUTTONUP, x, y)
|
||||||
|
if position is not None:
|
||||||
|
time.sleep(0.12)
|
||||||
|
self._restore_background_position(position)
|
||||||
# Let Unity consume the release before another target is selected.
|
# Let Unity consume the release before another target is selected.
|
||||||
time.sleep(0.12)
|
time.sleep(0.12)
|
||||||
elif not self.cfg.simulator and self.cfg.win_input_type == 'foreground':
|
elif not self.cfg.simulator and self.cfg.win_input_type == 'foreground':
|
||||||
@@ -236,7 +275,7 @@ class AalcEngine(Engine):
|
|||||||
if k not in self.key_names:
|
if k not in self.key_names:
|
||||||
raise ValueError('AALC 不支持按键 %r, 可用: %s' % (key, ' '.join(self.key_names)))
|
raise ValueError('AALC 不支持按键 %r, 可用: %s' % (key, ' '.join(self.key_names)))
|
||||||
self._prepare_input()
|
self._prepare_input()
|
||||||
if not self.cfg.simulator and self.cfg.win_input_type == 'background':
|
if not self.cfg.simulator and self.cfg.win_input_type in ('background', 'window_move'):
|
||||||
# Unity 可能在同一帧收到 down/up,导致快捷键被吞掉。
|
# Unity 可能在同一帧收到 down/up,导致快捷键被吞掉。
|
||||||
handler = self.auto.input_handler
|
handler = self.auto.input_handler
|
||||||
self.auto.wait_pause()
|
self.auto.wait_pause()
|
||||||
@@ -262,6 +301,35 @@ class AalcEngine(Engine):
|
|||||||
if not (0 <= x < img.width and 0 <= y < img.height):
|
if not (0 <= x < img.width and 0 <= y < img.height):
|
||||||
raise ValueError('拖动坐标超出游戏客户区 %sx%s' % (img.width, img.height))
|
raise ValueError('拖动坐标超出游戏客户区 %sx%s' % (img.width, img.height))
|
||||||
self._prepare_input()
|
self._prepare_input()
|
||||||
|
if not self.cfg.simulator and self.cfg.win_input_type in ('background', 'window_move'):
|
||||||
|
import win32con
|
||||||
|
self.auto.wait_pause()
|
||||||
|
position = None
|
||||||
|
if self.cfg.win_input_type == 'window_move':
|
||||||
|
self.screen.handle.set_window_transparent(True)
|
||||||
|
position = self.auto.input_handler._set_window_pos(int(from_x), int(from_y))
|
||||||
|
self.auto.input_handler.set_active()
|
||||||
|
self._background_mouse(win32con.WM_MOUSEMOVE, from_x, from_y)
|
||||||
|
time.sleep(0.12)
|
||||||
|
self._background_mouse(win32con.WM_LBUTTONDOWN, from_x, from_y, held=True)
|
||||||
|
last_x, last_y = from_x, from_y
|
||||||
|
try:
|
||||||
|
time.sleep(0.12)
|
||||||
|
steps = max(1, int(float(duration) / 0.02))
|
||||||
|
for step in range(1, steps + 1):
|
||||||
|
last_x = round(from_x + (to_x - from_x) * step / steps)
|
||||||
|
last_y = round(from_y + (to_y - from_y) * step / steps)
|
||||||
|
if position is not None:
|
||||||
|
self.auto.input_handler._set_window_pos(last_x, last_y)
|
||||||
|
self._background_mouse(win32con.WM_MOUSEMOVE, last_x, last_y, held=True)
|
||||||
|
time.sleep(float(duration) / steps)
|
||||||
|
time.sleep(0.12)
|
||||||
|
finally:
|
||||||
|
self._background_mouse(win32con.WM_LBUTTONUP, last_x, last_y)
|
||||||
|
if position is not None:
|
||||||
|
time.sleep(0.12)
|
||||||
|
self._restore_background_position(position)
|
||||||
|
return
|
||||||
if not self.cfg.simulator and self.cfg.win_input_type == 'foreground':
|
if not self.cfg.simulator and self.cfg.win_input_type == 'foreground':
|
||||||
import pyautogui
|
import pyautogui
|
||||||
start = self.auto.input_handler.pos_offset(int(from_x), int(from_y))
|
start = self.auto.input_handler.pos_offset(int(from_x), int(from_y))
|
||||||
|
|||||||
@@ -36,6 +36,10 @@ class Engine:
|
|||||||
"""截图并落盘。返回 {'path': str, 'w': int, 'h': int}。"""
|
"""截图并落盘。返回 {'path': str, 'w': int, 'h': int}。"""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def show_game_background(self):
|
||||||
|
"""Show the game window without activating it or sending input."""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
def ocr(self):
|
def ocr(self):
|
||||||
"""截图 + OCR。返回 (shot, items); items = [{text, x, y, w, h, score}]。"""
|
"""截图 + OCR。返回 (shot, items); items = [{text, x, y, w, h, score}]。"""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|||||||
@@ -16,6 +16,10 @@ class FakeEngine(Engine):
|
|||||||
self.shots += 1
|
self.shots += 1
|
||||||
return {'path': 'fake/shot_%d.png' % self.shots, 'w': 1920, 'h': 1080}
|
return {'path': 'fake/shot_%d.png' % self.shots, 'w': 1920, 'h': 1080}
|
||||||
|
|
||||||
|
def show_game_background(self):
|
||||||
|
self.actions.append(('show_game_background',))
|
||||||
|
return {'visible': True, 'foreground_changed': False}
|
||||||
|
|
||||||
def ocr(self):
|
def ocr(self):
|
||||||
shot = self.screenshot()
|
shot = self.screenshot()
|
||||||
items = self.frames.pop(0) if len(self.frames) > 1 else self.frames[0]
|
items = self.frames.pop(0) if len(self.frames) > 1 else self.frames[0]
|
||||||
|
|||||||
+6
-1
@@ -11,7 +11,7 @@ from .engine import find_hits
|
|||||||
|
|
||||||
MAX_WAIT = 300.0
|
MAX_WAIT = 300.0
|
||||||
WAIT_INTERVAL = 1.0
|
WAIT_INTERVAL = 1.0
|
||||||
INPUT_TOOLS = {'click', 'click_text', 'click_image', 'keypress', 'type_text', 'drag'}
|
INPUT_TOOLS = {'click', 'click_text', 'click_image', 'keypress', 'type_text', 'drag', 'show_game_background'}
|
||||||
TASK_TOOLS = {'run_task', 'task_status', 'stop_task'}
|
TASK_TOOLS = {'run_task', 'task_status', 'stop_task'}
|
||||||
|
|
||||||
|
|
||||||
@@ -31,6 +31,8 @@ _IMG = {'name': {'type': 'string', 'description': 'AALC 模板图相对路径,
|
|||||||
'description': 'clam=只在模板原位置找, normal=扩大范围, aggressive=全屏'}}
|
'description': 'clam=只在模板原位置找, normal=扩大范围, aggressive=全屏'}}
|
||||||
|
|
||||||
TOOLS = [
|
TOOLS = [
|
||||||
|
{'name': 'show_game_background', 'description': '后台显示/恢复游戏窗口,使隐藏窗口继续渲染;不激活前台、不发送鼠标或键盘输入。',
|
||||||
|
'inputSchema': _obj()},
|
||||||
{'name': 'get_state', 'description': '截图 + OCR, 返回游戏窗口内全部文字(含坐标/置信度)。决策前先调这个, 不要猜坐标',
|
{'name': 'get_state', 'description': '截图 + OCR, 返回游戏窗口内全部文字(含坐标/置信度)。决策前先调这个, 不要猜坐标',
|
||||||
'inputSchema': _obj()},
|
'inputSchema': _obj()},
|
||||||
{'name': 'click_text', 'description': '按文字点击: OCR 定位后点中心。子串匹配、忽略空格, 完全相同的优先; index 选第几个命中',
|
{'name': 'click_text', 'description': '按文字点击: OCR 定位后点中心。子串匹配、忽略空格, 完全相同的优先; index 选第几个命中',
|
||||||
@@ -146,6 +148,9 @@ class Server:
|
|||||||
return {'ok': False, 'error': 'AALC 任务 %s 正在运行, 手动输入会打乱它; 先 stop_task' % self.tasks.info['task']}
|
return {'ok': False, 'error': 'AALC 任务 %s 正在运行, 手动输入会打乱它; 先 stop_task' % self.tasks.info['task']}
|
||||||
e = self.engine
|
e = self.engine
|
||||||
|
|
||||||
|
if name == 'show_game_background':
|
||||||
|
return dict(e.show_game_background(), ok=True)
|
||||||
|
|
||||||
if name == 'screenshot':
|
if name == 'screenshot':
|
||||||
return dict(e.screenshot(), ok=True)
|
return dict(e.screenshot(), ok=True)
|
||||||
if name == 'get_state':
|
if name == 'get_state':
|
||||||
|
|||||||
Reference in New Issue
Block a user