合并 main 的合并前复核修复到镜牢选队分支

Browse Source
分支从 8a9495b 分出,缺 main 上 a74fda0 的急停与驱动复核修复。
两边都保留,不取单边:

- 急停:沿用分支的 GetAsyncKeyState 轮询与 End 键(pynput 钩子在该
  机器收不到键,F12 是 Steam 截图键);移植 main 的 on_stop 释放驱动
  按住的鼠标键、250 毫秒强退兜底、listening() 管理监听生命周期。
  按键状态改为独立绑定(SHORT 返回值),接口不可用时启动即报告。
- 驱动输入:同时保留句柄级前台校验、press_ms/settle 与 main 的
  停止标志、输入锁和按键记录;点击经 down()/up() 以便急停释放。
  前台校验改用本模块的类型化 _api 绑定。
- 代理每次点击按当前句柄重新 bind_window:代理复用时不再拿安装时
  的旧句柄拒绝游戏重启后的全部点击。
- aalc_task:listening() 包住整个任务,内部安装两个选队补丁。
- 分支新增测试改为替换模块接口,不再直接 patch ctypes.windll
  (Linux 上 9 项报错);README 急停键改为 End。

验证:Linux / Python 3.12 全套 474 项通过;git diff --check 通过。
Windows 热键、驱动和选队仍需实机回归。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
yws
2026-11-14 03:19:53 +00:00
co-authored by Claude Opus 5.5
14 changed files with 490 additions and 182 deletions
+71 -39
View File
@@ -301,8 +301,8 @@ class DriverWiringTest(unittest.TestCase):
def test_actual_task_entry_enables_driver_for_all_and_daily(self): def test_actual_task_entry_enables_driver_for_all_and_daily(self):
from lc_mcp import aalc_task from lc_mcp import aalc_task
for task in ('all', 'daily'): for task, problems in [('all', []), ('daily', []), ('all', ['invalid config'])]:
with self.subTest(task=task): with self.subTest(task=task, problems=problems):
auto, handler_type = vendor_auto() auto, handler_type = vendor_auto()
auto.clear_img_cache = mock.Mock() auto.clear_img_cache = mock.Mock()
auto.click_element = mock.Mock(return_value=False) auto.click_element = mock.Mock(return_value=False)
@@ -329,14 +329,21 @@ class DriverWiringTest(unittest.TestCase):
mock.patch.object(aalc_task, 'install_exact_team_match'), \ mock.patch.object(aalc_task, 'install_exact_team_match'), \
mock.patch.object(aalc_task, 'install_robust_set_foreground'), \ mock.patch.object(aalc_task, 'install_robust_set_foreground'), \
mock.patch.object(aalc_task, 'WindowGuard'), \ mock.patch.object(aalc_task, 'WindowGuard'), \
mock.patch.object(aalc_task.killswitch, 'install') as install_hotkey, \
mock.patch.object(aalc_task, 'log'), \ mock.patch.object(aalc_task, 'log'), \
mock.patch.object(aalc_task.preflight, 'collect', return_value={}), \ mock.patch.object(aalc_task.preflight, 'collect', return_value={}), \
mock.patch.object(aalc_task.preflight, 'desktop_size', return_value=(1920, 1080)), \ mock.patch.object(aalc_task.preflight, 'desktop_size', return_value=(1920, 1080)), \
mock.patch.object(aalc_task.preflight, 'check', return_value=[]), \ mock.patch.object(aalc_task.preflight, 'check', return_value=problems), \
mock.patch.object(di, 'DriverInput', return_value=drv), \ mock.patch.object(di, 'DriverInput', return_value=drv), \
mock.patch.object(ap.DriverClickProxy, '_screen_pos', return_value=(10, 20)): mock.patch.object(ap.DriverClickProxy, '_screen_pos', return_value=(10, 20)):
self.assertEqual(aalc_task.run(task, {}), 0) self.assertEqual(aalc_task.run(task, {}), 3 if problems else 0)
drv.click.assert_called_once_with(10, 20, times=1) install_hotkey.return_value.stop.assert_called_once()
if problems:
drv.click.assert_not_called()
else:
drv.click.assert_called_once_with(10, 20, times=1)
install_hotkey.call_args.kwargs['on_stop']()
drv.emergency_stop.assert_called_once()
def test_disabled_and_simulator_leave_initialization_unchanged(self): def test_disabled_and_simulator_leave_initialization_unchanged(self):
for enabled, simulator in [('0', False), ('1', True)]: for enabled, simulator in [('0', False), ('1', True)]:
@@ -350,6 +357,23 @@ class DriverWiringTest(unittest.TestCase):
class DriverFailureTest(unittest.TestCase): class DriverFailureTest(unittest.TestCase):
def test_emergency_stop_releases_press_and_blocks_subsequent_clicks(self):
drv = di.DriverInput()
drv._open, drv._dll = True, mock.Mock()
with mock.patch.object(drv, 'move_to', return_value=True), \
mock.patch.object(di, 'click_point_is_on_foreground', lambda *a, **k: (True, 'ok')), \
mock.patch.object(di.time, 'sleep', side_effect=lambda _: drv.emergency_stop()):
# settle=0: 第一次 sleep 是按下后的按住时长, 急停发生在按住期间
self.assertFalse(drv.click(10, 20, times=3, settle=0))
drv._dll.mouse_down.assert_called_once_with(1)
drv._dll.mouse_up.assert_called_with(1)
self.assertFalse(drv.ready)
self.assertFalse(drv.click(30, 40))
self.assertFalse(drv.move_relative(20, 30))
self.assertFalse(drv.down())
drv._dll.moveR.assert_not_called()
self.assertEqual(drv._pressed, set())
def test_release_even_if_sleep_is_interrupted(self): def test_release_even_if_sleep_is_interrupted(self):
drv = di.DriverInput() drv = di.DriverInput()
drv._open, drv._dll = True, mock.Mock() drv._open, drv._dll = True, mock.Mock()
@@ -437,8 +461,18 @@ class ForegroundFallbackTest(unittest.TestCase):
[mock.call(33, 11, True), mock.call(33, 11, False)]) [mock.call(33, 11, True), mock.call(33, 11, False)])
if __name__ == '__main__':
unittest.main() def win_api(**functions):
"""按 API 名替换 driver_input._api 返回的函数。"""
return mock.patch.object(di, '_api', side_effect=lambda library, name, result, args: functions[name])
def window_rect(left, top, right, bottom):
def get_window_rect(hwnd, ptr):
r = ptr._obj
r.left, r.top, r.right, r.bottom = left, top, right, bottom
return 1
return get_window_rect
class ForegroundCheckTest(unittest.TestCase): class ForegroundCheckTest(unittest.TestCase):
@@ -455,37 +489,21 @@ class ForegroundCheckTest(unittest.TestCase):
return d return d
def test_rejects_when_no_foreground_window(self): def test_rejects_when_no_foreground_window(self):
with mock.patch.object(di.ctypes.windll.user32, 'GetForegroundWindow', with mock.patch.object(di, 'foreground_window', return_value=None):
return_value=0):
ok, why = di.click_point_is_on_foreground(100, 100) ok, why = di.click_point_is_on_foreground(100, 100)
self.assertFalse(ok) self.assertFalse(ok)
self.assertIn('前台窗口', why) self.assertIn('前台窗口', why)
def test_rejects_when_point_outside_foreground_window(self): def test_rejects_when_point_outside_foreground_window(self):
def fake_get_window_rect(hwnd, ptr): with mock.patch.object(di, 'foreground_window', return_value=12345), \
r = ptr._obj win_api(GetWindowRect=window_rect(0, 0, 50, 50)):
r.left, r.top, r.right, r.bottom = 0, 0, 50, 50
return 1
with mock.patch.object(di.ctypes.windll.user32, 'GetForegroundWindow',
return_value=12345), \
mock.patch.object(di.ctypes.windll.user32, 'GetWindowRect',
fake_get_window_rect), \
mock.patch.object(di.ctypes.windll.user32, 'GetWindowTextW'):
ok, why = di.click_point_is_on_foreground(999, 999) ok, why = di.click_point_is_on_foreground(999, 999)
self.assertFalse(ok) self.assertFalse(ok)
self.assertIn('不在前台窗口内', why) self.assertIn('不在前台窗口内', why)
def test_accepts_when_point_inside_foreground_window(self): def test_accepts_when_point_inside_foreground_window(self):
def fake_get_window_rect(hwnd, ptr): with mock.patch.object(di, 'foreground_window', return_value=12345), \
r = ptr._obj win_api(GetWindowRect=window_rect(0, 0, 1920, 1080)):
r.left, r.top, r.right, r.bottom = 0, 0, 1920, 1080
return 1
with mock.patch.object(di.ctypes.windll.user32, 'GetForegroundWindow',
return_value=12345), \
mock.patch.object(di.ctypes.windll.user32, 'GetWindowRect',
fake_get_window_rect):
ok, why = di.click_point_is_on_foreground(500, 500) ok, why = di.click_point_is_on_foreground(500, 500)
self.assertTrue(ok) self.assertTrue(ok)
self.assertEqual(why, 'ok') self.assertEqual(why, 'ok')
@@ -553,30 +571,29 @@ class HwndForegroundCheckTest(unittest.TestCase):
""" """
def test_accepts_when_foreground_is_the_target(self): def test_accepts_when_foreground_is_the_target(self):
with mock.patch.object(di.ctypes.windll.user32, 'GetForegroundWindow', with mock.patch.object(di, 'foreground_window', return_value=999):
return_value=999):
ok, why = di.click_point_is_on_foreground(100, 100, 999) ok, why = di.click_point_is_on_foreground(100, 100, 999)
self.assertTrue(ok) self.assertTrue(ok)
self.assertEqual(why, 'ok') self.assertEqual(why, 'ok')
def test_accepts_child_window_of_target(self): def test_accepts_child_window_of_target(self):
"""游戏可能有子窗口拿到焦点, 也算在目标窗口上。""" """游戏可能有子窗口拿到焦点, 也算在目标窗口上。"""
with mock.patch.object(di.ctypes.windll.user32, 'GetForegroundWindow', is_child = mock.Mock(return_value=True)
return_value=1000), \ with mock.patch.object(di, 'foreground_window', return_value=1000), \
mock.patch.object(di.ctypes.windll.user32, 'IsChild', win_api(IsChild=is_child):
return_value=True):
ok, _ = di.click_point_is_on_foreground(100, 100, 999) ok, _ = di.click_point_is_on_foreground(100, 100, 999)
is_child.assert_called_once_with(999, 1000)
self.assertTrue(ok) self.assertTrue(ok)
def test_rejects_when_foreground_is_another_window(self): def test_rejects_when_foreground_is_another_window(self):
with mock.patch.object(di.ctypes.windll.user32, 'GetForegroundWindow', titles = {1000: 'Browser', 999: 'LimbusCompany'}
return_value=1000), \ with mock.patch.object(di, 'foreground_window', return_value=1000), \
mock.patch.object(di.ctypes.windll.user32, 'IsChild', win_api(IsChild=mock.Mock(return_value=False)), \
return_value=False), \ mock.patch.object(di, 'window_title', side_effect=titles.get):
mock.patch.object(di.ctypes.windll.user32, 'GetWindowTextW'):
ok, why = di.click_point_is_on_foreground(100, 100, 999) ok, why = di.click_point_is_on_foreground(100, 100, 999)
self.assertFalse(ok) self.assertFalse(ok)
self.assertIn('前台不是目标窗口', why) self.assertIn('前台不是目标窗口', why)
self.assertIn('Browser', why)
def test_bind_window_is_used_by_click(self): def test_bind_window_is_used_by_click(self):
"""绑定窗口后, click() 必须走句柄级校验。""" """绑定窗口后, click() 必须走句柄级校验。"""
@@ -597,3 +614,18 @@ class HwndForegroundCheckTest(unittest.TestCase):
self.assertFalse(d.click(10, 10)) self.assertFalse(d.click(10, 10))
self.assertEqual(seen['hwnd'], 4242, 'click 没把绑定的句柄传下去') self.assertEqual(seen['hwnd'], 4242, 'click 没把绑定的句柄传下去')
d._dll.mouse_down.assert_not_called() d._dll.mouse_down.assert_not_called()
def test_proxy_rebinds_current_window_before_each_click(self):
"""游戏重启后句柄会变; 代理复用时若仍拿安装时的旧句柄, 前台校验会拒绝所有点击。"""
drv = di.DriverInput(dll_path='X.dll', log=lambda m: None).bind_window(111)
handle = SimpleNamespace(hwnd=222, isMinimized=False, setForeground=mock.Mock())
proxy = ap.DriverClickProxy(mock.Mock(), drv, SimpleNamespace(handle=handle), log=lambda _: None)
with mock.patch.object(di, 'is_console_session', return_value=True), \
mock.patch.object(di, 'foreground_window', return_value=222), \
mock.patch.object(di, 'client_to_screen', return_value=(5, 6)):
self.assertEqual(proxy._screen_pos(1, 2), (5, 6))
self.assertEqual(drv.hwnd, 222)
if __name__ == '__main__':
unittest.main()