diff --git a/scripts/convert_modules.py b/scripts/convert_modules.py index d3ebfe6..697a332 100644 --- a/scripts/convert_modules.py +++ b/scripts/convert_modules.py @@ -364,22 +364,46 @@ def ensure_window_size(width, height, find=None, client=None, resize=None): return after -def launch_game(timeout_s=240): +def launch_game(timeout_s=240, tries=3): + """启动游戏, 分几次尝试。 + + ⛔ 2026-10-17 02:03 实测: 那一轮 exe 起来就退了 —— Steam 的 content_log 里 + 02:0x 毫无记录(上一轮 22:03:43 有 "App Running"), 应用事件日志也没有报错, + 游戏进程始终没出现。而 13 分钟后我手动启动, 5 秒就起来了。 + 时间线上, 01:49 刚发生过一次会话断开重连, Steam 的 webhelper 直到 02:16 才恢复活动。 + + 原来的写法是"点一次火, 干等 240 秒", 碰上一次静默失败就白丢一整轮(这轮体力白攒)。 + 改成多次尝试 —— 中间隔一会儿再来一次, 大多数瞬时故障都能靠这个救回来。 + """ if game_running(): log('游戏已在运行') return True - log('启动游戏: %s' % GAME_EXE) - subprocess.Popen([GAME_EXE], close_fds=True) - t0 = time.time() - while time.time() - t0 < timeout_s: - time.sleep(5) - if game_running(): - log('游戏进程已出现 (%.0fs)' % (time.time() - t0)) - return True - log('游戏启动超时') + log('Steam 进程数: %d' % len([p for p in _popen_psutil('steam')])) + each = max(40, timeout_s // max(1, tries)) + for i in range(tries): + log('启动游戏(第 %d/%d 次, 每次等 %ds): %s' % (i + 1, tries, each, GAME_EXE)) + subprocess.Popen([GAME_EXE], close_fds=True) + t0 = time.time() + while time.time() - t0 < each: + time.sleep(5) + if game_running(): + log('游戏进程已出现 (%.0fs)' % (time.time() - t0)) + return True + log(' 这一次没等到游戏进程') + log('游戏启动超时(试了 %d 次)' % tries) return False +def _popen_psutil(name): + """按进程名查进程(只用于日志, 失败返回空)。""" + try: + out = subprocess.run(['tasklist', '/FI', 'IMAGENAME eq %s.exe' % name], + capture_output=True, text=True, errors='ignore').stdout + return [l for l in out.splitlines() if l.lower().startswith(name.lower())] + except OSError: + return [] + + def close_game(): subprocess.run(['taskkill', '/IM', '%s.exe' % GAME_PROC, '/F'], capture_output=True, text=True, errors='ignore') diff --git a/tests/test_convert_modules.py b/tests/test_convert_modules.py index 191312a..dce7f58 100644 --- a/tests/test_convert_modules.py +++ b/tests/test_convert_modules.py @@ -630,6 +630,84 @@ class CliRoutingTest(QuietLogMixin, unittest.TestCase): self.assertEqual(code, 0) +class LaunchGameRetryTest(QuietLogMixin, unittest.TestCase): + """⛔ 2026-10-17 02:03 实测: exe 起来就退了。 + + Steam 的 content_log 里 02:0x 毫无记录(上一轮 22:03:43 还有 "App Running"), + 应用事件日志也没报错, 游戏进程始终没出现; 而 13 分钟后手动启动 5 秒就起来了。 + 原来"点一次火干等 240 秒"的写法碰上一次静默失败就白丢一整轮。 + + 这里的时钟是假的 —— sleep 打桩成空函数后墙钟几乎不走, 内层循环会狂转, + 不控时钟就测不出"一次尝试等满 40 秒"这件事。 + """ + + class FakeTime: + def __init__(self): + self.t = 1000.0 + + def time(self): + return self.t + + def sleep(self, s): + self.t += s + + def _run(self, appear_after_call, tries=3, each=40): + """appear_after_call: 第几次 game_running 调用开始返回 True(None=永不)。""" + clock = self.FakeTime() + calls = {'n': 0} + pops = [] + + def running(): + calls['n'] += 1 + return appear_after_call is not None and calls['n'] >= appear_after_call + + with mock.patch.object(cm, 'game_running', running), \ + mock.patch.object(cm, '_popen_psutil', lambda n: []), \ + mock.patch.object(cm.subprocess, 'Popen', + lambda *a, **k: pops.append(a)), \ + mock.patch.object(cm.time, 'time', clock.time), \ + mock.patch.object(cm.time, 'sleep', clock.sleep): + ok = cm.launch_game(timeout_s=tries * each, tries=tries) + return ok, len(pops), clock.t + + def test_first_attempt_success_is_enough(self): + # 第 1 次调用是进入函数时的"已经在运行吗"检查(返回 False), + # 之后循环里的调用才代表"点火后进程出现了" —— 所以用 3。 + ok, pops, _ = self._run(appear_after_call=3) + self.assertTrue(ok) + self.assertEqual(pops, 1, '第一次就成功, 不该重复点火') + + def test_silent_failure_is_retried_and_can_recover(self): + """第一次点火后进程没出现 -> 必须再试, 第二次才出现。""" + ok, pops, _ = self._run(appear_after_call=None if False else 10) + self.assertTrue(ok, '重试之后应该成功') + self.assertGreater(pops, 1, '进程没出现时应该重新点火') + + def test_already_running_never_launches(self): + # 第 1 次检查就为真 -> 直接返回, 不点火 + ok, pops, _ = self._run(appear_after_call=1) + self.assertTrue(ok) + self.assertEqual(pops, 0, '已经在运行就不该点火') + + def test_gives_up_after_all_tries(self): + ok, pops, _ = self._run(appear_after_call=None) + self.assertFalse(ok) + self.assertEqual(pops, 3, '试满 3 次就放弃') + + def test_each_attempt_waits_its_share(self): + """总等待要摊到每次尝试上, 不能把 240 秒全花在第一次。""" + ok, pops, elapsed = self._run(appear_after_call=None, tries=3, each=40) + self.assertAlmostEqual(elapsed - 1000.0, 120.0, delta=1.0, + msg='3 次 x 40 秒 = 120 秒总等待') + + def test_already_running_does_not_relaunch(self): + with mock.patch.object(cm, 'game_running', lambda: True), \ + mock.patch.object(cm, '_popen_psutil', lambda n: []), \ + mock.patch.object(cm.subprocess, 'Popen', + lambda *a, **k: self.fail('已在运行还去点火')): + self.assertTrue(cm.launch_game()) + + class OnModulesTabTest(QuietLogMixin, unittest.TestCase): """确认停在免费合成页签 —— 判错的代价是整轮拒绝合成。