WindowGuard 补上窗口位置纠正(窗口出界会让抓图整体错位)
现象: 任务日志一直在动、进程正常,但几乎不点击 —— 实测 30 秒只点了 1 次。
同时图像匹配相似度全在 0.2~0.5(AALC 阈值通常 0.8)。
排查: 游戏窗口位置是 (-8, 39) —— 左边 8 像素在屏幕外。
原因: 前台模式(background_click=false)下 AALC 从**屏幕**抓图(BitBlt 屏幕 DC),
窗口一出界, 抓到的画面就整体偏移, 模板匹配自然全部失败; 而 AALC 的
set_win_position=free 表示它不碰窗口位置, 所以没人管这件事。
手动把窗口挪到 (0,0) 后, 30 秒内点击立刻恢复到 5 次 —— 确认因果。
修复: WindowGuard 原来只纠正客户区尺寸, 现在先纠正位置:
用 monitor_info["Work"](工作区, 已扣任务栏)判断窗口是否完整落在里面,
不在就用 SetWindowPos 夹回边界内(保留尺寸、不夺焦点)。
位置放在尺寸检查之前 —— 出界时尺寸检查本身也没意义。
游戏每次启动都会恢复自己保存的位置, 所以这个纠正必须常驻, 不能只在启动时做一次。
This commit is contained in:
+85
-74
@@ -7,10 +7,10 @@
|
|||||||
目标点。所以任务跑起来时鼠标是"拔河"状态, 用户没法用鼠标去点停止按钮。键盘不受
|
目标点。所以任务跑起来时鼠标是"拔河"状态, 用户没法用鼠标去点停止按钮。键盘不受
|
||||||
影响, 所以急停必须走键盘。
|
影响, 所以急停必须走键盘。
|
||||||
|
|
||||||
AALC 自带的 pause_hotkey 帮不上忙: 那个配置项只在 GUI(app/farming_interface.py)
|
实现上踩过一个坑: 先用了 pynput 的 GlobalHotKeys, 静态注册成功、线程也活着, 但
|
||||||
里注册, lc_mcp 走命令行子进程, 那条路径上没有热键监听。这是本模块存在的原因。
|
keybd_event 和 SendInput 两种注入都触发不了它 —— 钩子在当前会话下收不到键。
|
||||||
|
改成 GetAsyncKeyState 轮询后实测可用。这里的测试守住轮询方案的几个关键性质。
|
||||||
"""
|
"""
|
||||||
import os
|
|
||||||
import sys
|
import sys
|
||||||
import unittest
|
import unittest
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
@@ -20,110 +20,121 @@ sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|||||||
from lc_mcp import killswitch # noqa: E402
|
from lc_mcp import killswitch # noqa: E402
|
||||||
|
|
||||||
|
|
||||||
class KeySpecTest(unittest.TestCase):
|
class ResolveVkTest(unittest.TestCase):
|
||||||
|
"""键名解析必须严格: 认不出来要返回 None, 不能悄悄用默认值 ——
|
||||||
|
否则用户以为设成了 F11, 实际还在监听 F12。"""
|
||||||
|
|
||||||
def test_wraps_plain_name(self):
|
def test_common_keys(self):
|
||||||
self.assertEqual(killswitch._key_spec('f12'), '<f12>')
|
self.assertEqual(killswitch.resolve_vk('f12'), 0x7B)
|
||||||
|
self.assertEqual(killswitch.resolve_vk('F11'), 0x7A)
|
||||||
|
self.assertEqual(killswitch.resolve_vk('esc'), 0x1B)
|
||||||
|
self.assertEqual(killswitch.resolve_vk('pause'), 0x13)
|
||||||
|
|
||||||
def test_keeps_already_wrapped(self):
|
def test_accepts_angle_brackets(self):
|
||||||
self.assertEqual(killswitch._key_spec('<f11>'), '<f11>')
|
self.assertEqual(killswitch.resolve_vk('<f12>'), 0x7B)
|
||||||
|
|
||||||
def test_lowercases_and_trims(self):
|
def test_trims_whitespace(self):
|
||||||
self.assertEqual(killswitch._key_spec(' F12 '), '<f12>')
|
self.assertEqual(killswitch.resolve_vk(' F12 '), 0x7B)
|
||||||
|
|
||||||
def test_empty_falls_back_to_default(self):
|
def test_empty_uses_default(self):
|
||||||
self.assertEqual(killswitch._key_spec(''), '<f12>')
|
self.assertEqual(killswitch.resolve_vk(''), killswitch.VK[killswitch.DEFAULT_KEY])
|
||||||
self.assertEqual(killswitch._key_spec(None), '<f12>')
|
self.assertEqual(killswitch.resolve_vk(None), killswitch.VK[killswitch.DEFAULT_KEY])
|
||||||
|
|
||||||
|
def test_unknown_returns_none(self):
|
||||||
|
self.assertIsNone(killswitch.resolve_vk('nosuchkey'))
|
||||||
|
|
||||||
|
|
||||||
class InstallTest(unittest.TestCase):
|
class InstallTest(unittest.TestCase):
|
||||||
"""装不上不能把任务带崩,但必须说清楚怎么应急。"""
|
|
||||||
|
|
||||||
def test_reports_when_pynput_is_missing(self):
|
def test_reports_unknown_key_and_suggests_fallback(self):
|
||||||
lines = []
|
lines = []
|
||||||
with mock.patch.dict(sys.modules, {'pynput': None}):
|
got = killswitch.install(log=lines.append, key='nosuchkey')
|
||||||
got = killswitch.install(log=lines.append, key='f12')
|
|
||||||
self.assertIsNone(got)
|
self.assertIsNone(got)
|
||||||
joined = '\n'.join(lines)
|
joined = '\n'.join(lines)
|
||||||
self.assertIn('急停热键不可用', joined)
|
self.assertIn('认不出键名', joined)
|
||||||
self.assertIn('Ctrl+Alt+Del', joined, '必须给出应急手段')
|
self.assertIn('Ctrl+Alt+Del', joined, '必须给出应急手段')
|
||||||
|
|
||||||
def test_registers_the_configured_key(self):
|
def test_installs_and_logs_the_key(self):
|
||||||
listener = mock.Mock()
|
lines = []
|
||||||
listener.daemon = False
|
with mock.patch.object(killswitch.KillSwitch, 'start', lambda self: self):
|
||||||
fake_keyboard = mock.Mock()
|
switch = killswitch.install(log=lines.append, key='f11')
|
||||||
fake_keyboard.GlobalHotKeys.return_value = listener
|
self.assertIsInstance(switch, killswitch.KillSwitch)
|
||||||
with mock.patch.dict(sys.modules, {'pynput': mock.Mock(keyboard=fake_keyboard),
|
self.assertEqual(switch.vk, 0x7A)
|
||||||
'pynput.keyboard': fake_keyboard}):
|
self.assertIn('F11', '\n'.join(lines))
|
||||||
got = killswitch.install(log=lambda m: None, key='f11')
|
|
||||||
self.assertIs(got, listener)
|
|
||||||
fake_keyboard.GlobalHotKeys.assert_called_once()
|
|
||||||
self.assertIn('<f11>', fake_keyboard.GlobalHotKeys.call_args[0][0])
|
|
||||||
listener.start.assert_called_once()
|
|
||||||
self.assertTrue(listener.daemon)
|
|
||||||
|
|
||||||
def test_env_var_overrides_default_key(self):
|
def test_env_var_overrides_default(self):
|
||||||
listener = mock.Mock()
|
with mock.patch.dict(killswitch.os.environ, {'LCP_KILL_KEY': 'esc'}), \
|
||||||
fake_keyboard = mock.Mock()
|
mock.patch.object(killswitch.KillSwitch, 'start', lambda self: self):
|
||||||
fake_keyboard.GlobalHotKeys.return_value = listener
|
switch = killswitch.install(log=lambda m: None)
|
||||||
with mock.patch.dict(os.environ, {'LCP_KILL_KEY': 'pause'}), \
|
self.assertEqual(switch.vk, 0x1B)
|
||||||
mock.patch.dict(sys.modules, {'pynput': mock.Mock(keyboard=fake_keyboard),
|
|
||||||
'pynput.keyboard': fake_keyboard}):
|
def test_start_failure_is_reported_not_raised(self):
|
||||||
killswitch.install(log=lambda m: None)
|
lines = []
|
||||||
self.assertIn('<pause>', fake_keyboard.GlobalHotKeys.call_args[0][0])
|
with mock.patch.object(killswitch.KillSwitch, 'start',
|
||||||
|
side_effect=RuntimeError('boom')):
|
||||||
|
got = killswitch.install(log=lines.append)
|
||||||
|
self.assertIsNone(got)
|
||||||
|
self.assertIn('启动失败', '\n'.join(lines))
|
||||||
|
|
||||||
|
|
||||||
class ActivateTest(unittest.TestCase):
|
class PollingTest(unittest.TestCase):
|
||||||
"""按下热键必须真的退出进程,而且只退一次。"""
|
"""轮询判断的核心:0x8000 位。测错位会导致"一直触发"或"永不触发"。"""
|
||||||
|
|
||||||
def _grab_handler(self):
|
def test_high_bit_means_pressed(self):
|
||||||
listener = mock.Mock()
|
with mock.patch.object(killswitch.ctypes.windll.user32, 'GetAsyncKeyState',
|
||||||
fake_keyboard = mock.Mock()
|
return_value=0x8000):
|
||||||
fake_keyboard.GlobalHotKeys.return_value = listener
|
self.assertTrue(killswitch._pressed(0x7B))
|
||||||
with mock.patch.dict(sys.modules, {'pynput': mock.Mock(keyboard=fake_keyboard),
|
|
||||||
'pynput.keyboard': fake_keyboard}):
|
def test_low_bits_are_not_pressed(self):
|
||||||
killswitch.install(log=lambda m: None)
|
# 0x0001 表示"上次调用后被按过", 不代表当前按住 —— 不能当成按下
|
||||||
bindings = fake_keyboard.GlobalHotKeys.call_args[0][0]
|
with mock.patch.object(killswitch.ctypes.windll.user32, 'GetAsyncKeyState',
|
||||||
return list(bindings.values())[0], listener
|
return_value=0x0001):
|
||||||
|
self.assertFalse(killswitch._pressed(0x7B))
|
||||||
|
|
||||||
|
def test_zero_is_not_pressed(self):
|
||||||
|
with mock.patch.object(killswitch.ctypes.windll.user32, 'GetAsyncKeyState',
|
||||||
|
return_value=0):
|
||||||
|
self.assertFalse(killswitch._pressed(0x7B))
|
||||||
|
|
||||||
|
|
||||||
|
class FireTest(unittest.TestCase):
|
||||||
|
|
||||||
|
def _switch(self):
|
||||||
|
return killswitch.KillSwitch(0x7B, log=lambda m: None, key_name='f12')
|
||||||
|
|
||||||
def test_exits_with_user_abort_code(self):
|
def test_exits_with_user_abort_code(self):
|
||||||
handler, _ = self._grab_handler()
|
sw = self._switch()
|
||||||
with mock.patch.object(killswitch.os, '_exit') as fake_exit:
|
with mock.patch.object(killswitch.os, '_exit') as fake_exit:
|
||||||
handler()
|
sw._fire()
|
||||||
fake_exit.assert_called_once_with(killswitch.EXIT_CODE)
|
fake_exit.assert_called_once_with(killswitch.EXIT_CODE)
|
||||||
|
|
||||||
def test_only_fires_once(self):
|
def test_only_fires_once(self):
|
||||||
handler, _ = self._grab_handler()
|
sw = self._switch()
|
||||||
with mock.patch.object(killswitch.os, '_exit') as fake_exit:
|
with mock.patch.object(killswitch.os, '_exit') as fake_exit:
|
||||||
handler()
|
sw._fire()
|
||||||
handler()
|
sw._fire()
|
||||||
handler()
|
sw._fire()
|
||||||
self.assertEqual(fake_exit.call_count, 1, '重复按下不该重复退出')
|
self.assertEqual(fake_exit.call_count, 1, '重复触发不该重复退出')
|
||||||
|
|
||||||
def test_logs_that_game_stays_alive(self):
|
def test_logs_that_game_is_kept(self):
|
||||||
handler, _ = self._grab_handler()
|
|
||||||
lines = []
|
lines = []
|
||||||
|
sw = killswitch.KillSwitch(0x7B, log=lines.append, key_name='f12')
|
||||||
with mock.patch.object(killswitch.os, '_exit'):
|
with mock.patch.object(killswitch.os, '_exit'):
|
||||||
handler.__closure__ # 确认是闭包
|
sw._fire()
|
||||||
killswitch.install(log=lines.append)
|
joined = '\n'.join(lines)
|
||||||
# 用新的 logger 重新装一次才拿得到日志,这里只验证消息文案存在
|
self.assertIn('F12', joined)
|
||||||
self.assertTrue(callable(handler))
|
self.assertIn('重新开始', joined)
|
||||||
|
|
||||||
|
|
||||||
class StopTest(unittest.TestCase):
|
class StopTest(unittest.TestCase):
|
||||||
|
|
||||||
def test_stop_is_safe_on_none(self):
|
def test_stop_is_safe_on_none(self):
|
||||||
killswitch.stop(None) # 不该抛
|
killswitch.stop(None)
|
||||||
|
|
||||||
def test_stop_calls_listener(self):
|
def test_stop_swallows_errors(self):
|
||||||
listener = mock.Mock()
|
sw = mock.Mock()
|
||||||
killswitch.stop(listener)
|
sw.stop.side_effect = RuntimeError('boom')
|
||||||
listener.stop.assert_called_once()
|
killswitch.stop(sw)
|
||||||
|
|
||||||
def test_stop_swallows_listener_errors(self):
|
|
||||||
listener = mock.Mock()
|
|
||||||
listener.stop.side_effect = RuntimeError('boom')
|
|
||||||
killswitch.stop(listener) # 不该抛
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
Reference in New Issue
Block a user