diff --git a/scripts/convert_modules.py b/scripts/convert_modules.py index f1a0e93..728f256 100644 --- a/scripts/convert_modules.py +++ b/scripts/convert_modules.py @@ -294,21 +294,9 @@ def _client_size(hwnd): return r.right - r.left, r.bottom - r.top -def ensure_window_size(width, height): - """把游戏客户区调成 width x height, 返回调完后的 (w, h)。 - - 为什么要管这个: RDP 断线重连会把窗口改小(实测 1600x900 -> 1278x719), 之后 - OCR 质量明显下降 —— 连底栏的 LUNACY 标签都读不出来, 狂气校验直接变成 - "读不到, 跳过"。AALC 自己的 WindowGuard 只在跑任务时才生效, 而这个脚本 - 不跑任务, 所以得自己调。 - """ +def _move_window(hwnd, width, height, cur): + """把窗口调到客户区正好 width x height。""" from ctypes import windll, wintypes, byref - hwnd = _find_hwnd() - if not hwnd: - return None - cur = _client_size(hwnd) - if cur == (width, height): - return cur wr = wintypes.RECT() windll.user32.GetWindowRect(hwnd, byref(wr)) # 边框/标题栏占掉的差值 = 窗口尺寸 - 客户区尺寸 @@ -316,8 +304,30 @@ def ensure_window_size(width, height): frame_h = (wr.bottom - wr.top) - cur[1] windll.user32.MoveWindow(hwnd, wr.left, wr.top, width + frame_w, height + frame_h, True) + + +def ensure_window_size(width, height, find=None, client=None, resize=None): + """把游戏客户区调成 width x height, 返回调完后的 (w, h)。 + + 为什么要管这个: RDP 断线重连会把窗口改小(实测 1600x900 -> 1278x719), 之后 + OCR 质量明显下降 —— 连底栏的 LUNACY 标签都读不出来, 狂气校验直接变成 + "读不到, 跳过"。AALC 自己的 WindowGuard 只在跑任务时才生效, 而这个脚本 + 不跑任务, 所以得自己调。 + + find/client/resize 可注入, 便于测试 —— 默认走 ctypes 真实实现。 + """ + find = find or _find_hwnd + client = client or _client_size + resize = resize or _move_window + hwnd = find() + if not hwnd: + return None + cur = client(hwnd) + if cur == (width, height): + return cur # 已经对了就别动它 + resize(hwnd, width, height, cur) time.sleep(2) - after = _client_size(hwnd) + after = client(hwnd) log('窗口客户区 %sx%s -> %sx%s (目标 %sx%s)' % (cur + after + (width, height))) return after diff --git a/tests/test_convert_modules.py b/tests/test_convert_modules.py index b883258..52c9f80 100644 --- a/tests/test_convert_modules.py +++ b/tests/test_convert_modules.py @@ -211,6 +211,48 @@ class EnkephalinGateTest(QuietLogMixin, unittest.TestCase): self.assertTrue(called, '重试后读到了就该正常合成') +class WindowSizeTest(QuietLogMixin, unittest.TestCase): + """RDP 断线重连会把客户区改小(实测 1600x900 -> 1278x719), 之后 OCR 明显变差。 + + AALC 自己的 WindowGuard 只在跑任务时才生效, 而这个脚本不跑任务, 所以要自己调。 + """ + + def test_missing_window_returns_none(self): + self.assertIsNone(cm.ensure_window_size(1600, 900, find=lambda: 0)) + + def test_correct_size_is_left_alone(self): + moved = [] + r = cm.ensure_window_size( + 1600, 900, + find=lambda: 123, + client=lambda h: (1600, 900), + resize=lambda *a: moved.append(a)) + self.assertEqual(r, (1600, 900)) + self.assertEqual(moved, [], '尺寸已经对了就不该去动窗口') + + def test_wrong_size_is_fixed(self): + sizes = iter([(1278, 719), (1600, 900)]) + moved = [] + r = cm.ensure_window_size( + 1600, 900, + find=lambda: 123, + client=lambda h: next(sizes), + resize=lambda *a: moved.append(a)) + self.assertEqual(r, (1600, 900)) + self.assertEqual(len(moved), 1, '尺寸不对时应该调一次') + # 传下去的是目标客户区尺寸和"调整前"的客户区 + self.assertEqual(moved[0][1:], (1600, 900, (1278, 719))) + + def test_reports_actual_size_even_if_resize_fails(self): + """调不动时返回真实值, 让调用方自己能告警 —— 不要假装调好了。""" + r = cm.ensure_window_size( + 1600, 900, + find=lambda: 123, + client=lambda h: (1278, 719), + resize=lambda *a: None) + self.assertEqual(r, (1278, 719)) + + class LunacyFalseAlarmTest(QuietLogMixin, unittest.TestCase): """⛔ 报"狂气被消费"之前必须复读确认。