修复点击时画面抽搐, 并把截屏自检改成走 AALC 实际用的采集路径

Browse Source
一、为什么"我用脚本点击时画面一直抽搐, 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:
Codex Agent
2026-10-22 09:34:29 +08:00
parent e1ebb09648
commit a6e987e796
4 changed files with 172 additions and 14 deletions
+31 -8
View File
@@ -236,8 +236,16 @@ class AalcEngine(Engine):
self.auto.wait_pause()
for _ in range(int(times)):
position = None
transparent = False
if self.cfg.win_input_type == 'window_move':
# 搬到光标下面时设成透明/点击穿透, 免得挡住用户的光标。
# ⛔ 必须在本次点击结束时就还原: 以前只有 background_screenshot
# 会把它恢复成 False, 于是"点击 -> 截图 -> 点击 -> 截图"这种
# 高频交替会让游戏窗口在不可见/可见之间反复切换 —— 肉眼看就是
# 画面一直抽搐。AALC 自己的 WindowMoveInput.mouse_click 根本不碰
# 透明度, 所以它只是"跳一下", 不会闪。
self.screen.handle.set_window_transparent(True)
transparent = True
position = handler._set_window_pos(int(x), int(y))
handler.set_active()
self._background_mouse(win32con.WM_MOUSEMOVE, x, y)
@@ -246,10 +254,16 @@ class AalcEngine(Engine):
try:
time.sleep(0.12)
finally:
self._background_mouse(win32con.WM_LBUTTONUP, x, y)
if position is not None:
time.sleep(0.12)
self._restore_background_position(position)
# 嵌套 try/finally: 还原位置那步也可能抛错(比如任务被停),
# 但透明度**无论如何**都要还原, 否则窗口会一直隐形。
try:
self._background_mouse(win32con.WM_LBUTTONUP, x, y)
if position is not None:
time.sleep(0.12)
self._restore_background_position(position)
finally:
if transparent:
self.screen.handle.set_window_transparent(False)
# 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':
@@ -305,8 +319,11 @@ class AalcEngine(Engine):
import win32con
self.auto.wait_pause()
position = None
transparent = False
if self.cfg.win_input_type == 'window_move':
# 同 click(): 透明必须在本次拖动结束时就还原, 不能留给下一次截图
self.screen.handle.set_window_transparent(True)
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)
@@ -325,10 +342,16 @@ class AalcEngine(Engine):
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)
# 同 click(): 透明度必须在嵌套的 finally 里还原, 不能被还原位置那步的
# 异常跳过, 否则窗口会一直隐形。
try:
self._background_mouse(win32con.WM_LBUTTONUP, last_x, last_y)
if position is not None:
time.sleep(0.12)
self._restore_background_position(position)
finally:
if transparent:
self.screen.handle.set_window_transparent(False)
return
if not self.cfg.simulator and self.cfg.win_input_type == 'foreground':
import pyautogui
+43 -5
View File
@@ -110,21 +110,59 @@ def ensure_server(timeout_s=90):
# ---------------------------------------------------------------- 桌面渲染
def render_ok():
"""桌面是否在出图(BitBlt 能否成功)。"""
def _blit_ok(hdc):
"""从给定的 DC 拷一小块回来, 成功与否。"""
from ctypes import windll
u, g = windll.user32, windll.gdi32
hdc = u.GetDC(0)
g = windll.gdi32
mem = g.CreateCompatibleDC(hdc)
bmp = g.CreateCompatibleBitmap(hdc, 64, 64)
g.SelectObject(mem, bmp)
r = g.BitBlt(mem, 0, 0, 64, 64, hdc, 0, 0, 0x00CC0020)
g.DeleteObject(bmp)
g.DeleteDC(mem)
u.ReleaseDC(0, hdc)
return bool(r)
def screen_blit_ok():
"""抓**整个屏幕**能不能成功(GetDC(0))。"""
from ctypes import windll
u = windll.user32
hdc = u.GetDC(0)
try:
return _blit_ok(hdc)
finally:
u.ReleaseDC(0, hdc)
def window_blit_ok(hwnd):
"""从**游戏窗口**的 DC 拷一小块回来能不能成功(和 AALC 的采集走同一类路径)。"""
from ctypes import windll
u = windll.user32
hdc = u.GetWindowDC(hwnd)
if not hdc:
return False
try:
return _blit_ok(hdc)
finally:
u.ReleaseDC(hwnd, hdc)
def render_ok():
"""游戏画面是否可采集。
⛔ 原来只测屏幕 BitBlt(GetDC(0))。2026-10-22 实测它在 RDP 会话下会瞬时失败,
而 AALC 走的是 background_screenshot(窗口 DC + PrintWindow), 那条路照样能截到图
—— 结果是我的自检把本来能干活的轮次判成"截不到图"给跳过了(10-17 02:23 就这样
白丢过一轮)。
现在优先按 AALC 的方式测**窗口**能否采集, 窗口不在或不可采时才回退到屏幕。
"""
hwnd = _find_hwnd()
if hwnd and window_blit_ok(hwnd):
return True
return screen_blit_ok()
def parse_session_table(text):
"""解析 `query session` 的输出。
+64 -1
View File
@@ -48,7 +48,70 @@ class BackgroundRestoreTest(unittest.TestCase):
engine.click(100, 200)
engine._restore_background_position.assert_called_once_with((20, 30))
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):
gui = mock.Mock()
+34
View File
@@ -669,6 +669,40 @@ class CliRoutingTest(QuietLogMixin, unittest.TestCase):
self.assertEqual(code, 0)
class RenderCheckTest(QuietLogMixin, unittest.TestCase):
"""⛔ render_ok() 必须测 AALC 真正走的那条采集路径。
原来只测屏幕 BitBlt(GetDC(0))。2026-10-22 实测它在 RDP 会话下会瞬时失败, 而
AALC 走的是 background_screenshot(窗口 DC + PrintWindow), 那条路照样能截到图 ——
结果是我的自检把本来能干活的轮次判成"截不到图"跳过了。
"""
def test_window_capture_alone_is_enough(self):
"""窗口能采到就算能干活 —— 不该再去看屏幕那一路。"""
with mock.patch.object(cm, '_find_hwnd', lambda: 123), \
mock.patch.object(cm, 'window_blit_ok', lambda hwnd: True), \
mock.patch.object(cm, 'screen_blit_ok',
lambda: self.fail('窗口能采就不该再测屏幕')):
self.assertTrue(cm.render_ok())
def test_falls_back_to_screen_when_window_is_missing(self):
with mock.patch.object(cm, '_find_hwnd', lambda: 0), \
mock.patch.object(cm, 'screen_blit_ok', lambda: True):
self.assertTrue(cm.render_ok())
def test_falls_back_to_screen_when_window_blit_fails(self):
with mock.patch.object(cm, '_find_hwnd', lambda: 123), \
mock.patch.object(cm, 'window_blit_ok', lambda hwnd: False), \
mock.patch.object(cm, 'screen_blit_ok', lambda: True):
self.assertTrue(cm.render_ok())
def test_reports_false_only_when_both_paths_fail(self):
with mock.patch.object(cm, '_find_hwnd', lambda: 123), \
mock.patch.object(cm, 'window_blit_ok', lambda hwnd: False), \
mock.patch.object(cm, 'screen_blit_ok', lambda: False):
self.assertFalse(cm.render_ok())
class SessionTableTest(QuietLogMixin, unittest.TestCase):
"""解析 `query session` —— 用来区分"截不到图"到底是哪种情况。