修复点击时画面抽搐, 并把截屏自检改成走 AALC 实际用的采集路径
一、为什么"我用脚本点击时画面一直抽搐, AALC 点击不抽搐"
链条查清了:
* lc_mcp/aalc_engine.py 的 click 每次先 set_window_transparent(True)
* 而这个方法的实现是 SetLayeredWindowAttributes(hwnd, 0, 0, LWA_ALPHA)
—— alpha=0, 整个游戏窗口**完全透明(看不见)**
* 它原来**只有 background_screenshot 会恢复成 False**(screenshot.py:194)
* 我的脚本是"点击 -> 截图 -> 点击 -> 截图"高频交替
=> 窗口在不可见/可见之间反复切换 => 肉眼看就是一直抽搐
AALC 自己的 WindowMoveInput.mouse_click 只搬窗口、**根本不碰透明度**, 所以它最多
"跳一下", 不会闪 —— 这就是两边观感不同的原因。
改法: 透明度在**本次点击/拖动结束时立刻还原**, 并且放在嵌套 try/finally 里 ——
还原位置那步也可能抛错(任务被停), 不能让它把透明度还原跳过去, 否则窗口会一直隐形。
(第一版就是把还原排在 finally 里的 sleep 之后, 测试里"异常路径"那条立刻抓到了。)
二、render_ok() 改成测 AALC 真正走的那条路
原来只测屏幕 BitBlt(GetDC(0))。2026-10-22 实测它在 RDP 会话下会瞬时失败, 而 AALC
走的是 background_screenshot(窗口 DC + PrintWindow), 那条路照样能截到图 —— 我的自检
把本来能干活的轮次判成"截不到图"跳过了(10-17 02:23 就白丢过一轮)。
现在优先测窗口能否采集, 窗口不在或不可采才回退到屏幕。
测试: 新增 3 个(点击内还原/异常也还原/前台模式不碰透明度) + 4 个 render_ok 分支。
全套 384/384 通过。
This commit is contained in:
@@ -48,7 +48,70 @@ class BackgroundRestoreTest(unittest.TestCase):
|
|||||||
engine.click(100, 200)
|
engine.click(100, 200)
|
||||||
engine._restore_background_position.assert_called_once_with((20, 30))
|
engine._restore_background_position.assert_called_once_with((20, 30))
|
||||||
engine.auto.input_handler.set_mouse_pos.assert_not_called()
|
engine.auto.input_handler.set_mouse_pos.assert_not_called()
|
||||||
engine.screen.handle.set_window_transparent.assert_called_once_with(True)
|
|
||||||
|
def test_click_restores_transparency_within_the_same_click(self):
|
||||||
|
"""⛔ 透明度必须在本次点击内还原, 不能留给下一次截图。
|
||||||
|
|
||||||
|
以前只在 background_screenshot 里恢复, 于是"点击 -> 截图 -> 点击 -> 截图"
|
||||||
|
的高频交替会让游戏窗口在不可见/可见之间反复切换 —— 肉眼看就是画面一直抽搐。
|
||||||
|
AALC 自己的 WindowMoveInput.mouse_click 根本不碰透明度, 所以它只"跳一下"。
|
||||||
|
"""
|
||||||
|
engine = AalcEngine.__new__(AalcEngine)
|
||||||
|
engine.cfg = SimpleNamespace(simulator=False, win_input_type='window_move')
|
||||||
|
engine.auto = mock.Mock()
|
||||||
|
engine.screen = mock.Mock()
|
||||||
|
engine.auto.input_handler._set_window_pos.return_value = (20, 30)
|
||||||
|
engine._background_mouse = mock.Mock()
|
||||||
|
engine._restore_background_position = mock.Mock()
|
||||||
|
engine._prepare_input = mock.Mock()
|
||||||
|
constants = SimpleNamespace(WM_MOUSEMOVE=512, WM_LBUTTONDOWN=513, WM_LBUTTONUP=514)
|
||||||
|
with mock.patch.dict(sys.modules, win32con=constants), \
|
||||||
|
mock.patch('lc_mcp.aalc_engine.time.sleep'):
|
||||||
|
engine.click(100, 200)
|
||||||
|
calls = [c.args[0] for c in engine.screen.handle.set_window_transparent.call_args_list]
|
||||||
|
self.assertEqual(calls, [True, False], '应当先设透明再立刻还原, 实际: %s' % calls)
|
||||||
|
|
||||||
|
def test_click_leaves_no_transparency_when_it_raises(self):
|
||||||
|
"""中途抛异常也必须把透明度还原, 否则窗口会一直隐形。"""
|
||||||
|
engine = AalcEngine.__new__(AalcEngine)
|
||||||
|
engine.cfg = SimpleNamespace(simulator=False, win_input_type='window_move')
|
||||||
|
engine.auto = mock.Mock()
|
||||||
|
engine.screen = mock.Mock()
|
||||||
|
engine.auto.input_handler._set_window_pos.return_value = (20, 30)
|
||||||
|
engine._background_mouse = mock.Mock()
|
||||||
|
engine._restore_background_position = mock.Mock()
|
||||||
|
engine._prepare_input = mock.Mock()
|
||||||
|
constants = SimpleNamespace(WM_MOUSEMOVE=512, WM_LBUTTONDOWN=513, WM_LBUTTONUP=514)
|
||||||
|
|
||||||
|
# 第二次 sleep 时抛错 —— 那一步在按下之后的 try 里, finally 必须照常还原
|
||||||
|
calls = {'n': 0}
|
||||||
|
|
||||||
|
def sleep_then_fail(_):
|
||||||
|
calls['n'] += 1
|
||||||
|
if calls['n'] >= 2:
|
||||||
|
raise RuntimeError('stopped')
|
||||||
|
|
||||||
|
with mock.patch.dict(sys.modules, win32con=constants), \
|
||||||
|
mock.patch('lc_mcp.aalc_engine.time.sleep', sleep_then_fail):
|
||||||
|
with self.assertRaises(RuntimeError):
|
||||||
|
engine.click(100, 200)
|
||||||
|
seen = [c.args[0] for c in engine.screen.handle.set_window_transparent.call_args_list]
|
||||||
|
self.assertIn(True, seen, '点击前应当设过透明')
|
||||||
|
self.assertEqual(seen[-1], False, '异常路径也要还原透明度, 实际: %s' % seen)
|
||||||
|
|
||||||
|
def test_foreground_mode_never_touches_transparency(self):
|
||||||
|
"""前台模式不需要搬窗口, 也就不该动透明度。"""
|
||||||
|
engine = AalcEngine.__new__(AalcEngine)
|
||||||
|
engine.cfg = SimpleNamespace(simulator=False, win_input_type='foreground')
|
||||||
|
engine.auto = mock.Mock()
|
||||||
|
engine.screen = mock.Mock()
|
||||||
|
engine.auto.input_handler.pos_offset.return_value = (100, 200)
|
||||||
|
engine._prepare_input = mock.Mock()
|
||||||
|
with mock.patch('pyautogui.moveTo'), mock.patch('pyautogui.mouseDown'), \
|
||||||
|
mock.patch('pyautogui.mouseUp'), \
|
||||||
|
mock.patch('lc_mcp.aalc_engine.time.sleep'):
|
||||||
|
engine.click(100, 200)
|
||||||
|
engine.screen.handle.set_window_transparent.assert_not_called()
|
||||||
|
|
||||||
def test_restore_uses_noactivate_and_preserves_foreground(self):
|
def test_restore_uses_noactivate_and_preserves_foreground(self):
|
||||||
gui = mock.Mock()
|
gui = mock.Mock()
|
||||||
|
|||||||
Reference in New Issue
Block a user