问题: 日志里能看到 "鼠标点击已切到罗技驱动级输入", 但驱动点击一次都没发生 (追踪文件只有 2 行 install 记录, [driver] 日志 0 条, 而 automation.py:152 的 点击日志有 58 次)。 原因: 安装在 scheme.init_game() **之前**。init_game() 会重建 auto.input_handler (automation.py:46/65/72/77/84/89/94/98 多处赋值), 把装好的代理覆盖掉。所以 patch 日志打了, 但真正点击时 getattr(self.input_handler, 'mouse_click') 取到的 是原来的 handler。 修复: 把安装挪到 init_game() 之后。 另外加了 LCP_DRIVER_TRACE 文件追踪(install 时记录原 handler 与 auto.mouse_click 的原绑定, mouse_click 每次调用都记一行) —— 普通日志会被 AALC 的 logger 吞掉, 文件追踪是这次能定位到根因的关键。 这个问题说明测试覆盖不足: 单元测试用 Mock 造 auto, 没有复现 automation.py 真实的 别名/gate 接线, 所以没能发现"装完被覆盖"。后续要补一个贴近真实接线的测试。
144 lines
5.1 KiB
Python
144 lines
5.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""子进程入口: 跑 AALC 现成的任务流程。
|
|
|
|
用法: python -m lc_mcp.aalc_task <task> [json_params]
|
|
python -m lc_mcp.aalc_task --restore <file> 写回被强杀任务临时覆盖的配置
|
|
放在子进程里跑, 停止 = 杀进程树; 不会像 QThread.terminate 那样留下被占用的锁。
|
|
"""
|
|
import json
|
|
import os
|
|
import sys
|
|
|
|
from . import preflight
|
|
from .aalc_engine import bootstrap
|
|
from .aalc_patches import WindowGuard, install_driver_click, install_exact_team_match
|
|
|
|
# 任务 -> {参数名: AALC 配置项}; 参数只在本次运行内生效
|
|
TASKS = {
|
|
'all': {}, # 完全按 config.yaml 里勾选的任务跑, 等价于 AALC 界面点"开始"
|
|
'daily': {'exp': 'set_EXP_count', 'thread': 'set_thread_count'},
|
|
'reward': {},
|
|
'enkephalin': {'times': 'set_lunacy_to_enkephalin'},
|
|
'mirror': {'count': 'set_mirror_count'},
|
|
}
|
|
RESTORE_ENV = 'LCP_RESTORE_FILE'
|
|
PREFLIGHT_EXIT = 3
|
|
|
|
|
|
def log(msg):
|
|
print(msg, flush=True)
|
|
|
|
|
|
def apply_overrides(cfg, task, params, restore_path=None):
|
|
"""临时覆盖本次运行的次数, 返回原值。
|
|
|
|
AALC 运行中会整份保存配置(如镜牢后 rotate_team_queue), 覆盖值会被写进 config.yaml。
|
|
所以先把原值落盘: 进程被 stop_task 强杀、finally 来不及执行时, TaskRunner 据此写回。
|
|
"""
|
|
originals = {TASKS[task][name]: cfg.get_value(TASKS[task][name]) for name in params}
|
|
if originals and restore_path:
|
|
tmp = restore_path + '.tmp'
|
|
with open(tmp, 'w', encoding='utf-8') as f:
|
|
json.dump(originals, f, ensure_ascii=False)
|
|
os.replace(tmp, restore_path)
|
|
for name, value in params.items():
|
|
cfg.unsaved_set_value(TASKS[task][name], int(value))
|
|
return originals
|
|
|
|
|
|
def restore(cfg, originals, restore_path=None):
|
|
for key, value in originals.items():
|
|
cfg.set_value(key, value)
|
|
if originals:
|
|
cfg.flush()
|
|
if restore_path and os.path.exists(restore_path):
|
|
os.remove(restore_path)
|
|
|
|
|
|
def restore_file(path, cfg=None):
|
|
with open(path, encoding='utf-8') as f:
|
|
originals = json.load(f)
|
|
if cfg is None:
|
|
bootstrap()
|
|
from module.config import cfg
|
|
restore(cfg, originals, path)
|
|
|
|
|
|
def run(task, params):
|
|
bootstrap()
|
|
from module.automation import auto
|
|
from module.config import cfg
|
|
from module.game_and_screen import screen
|
|
from tasks.base import script_task_scheme as scheme
|
|
from tasks.base.retry_monitor import retry_monitor
|
|
from tasks.battle import battle
|
|
from utils.path_manager import path_manager
|
|
|
|
problems = preflight.check(preflight.collect(cfg), task,
|
|
desktop=None if cfg.simulator else preflight.desktop_size())
|
|
if problems:
|
|
for p in problems:
|
|
log('[ERROR] [lc_mcp] preflight: %s' % p)
|
|
return PREFLIGHT_EXIT
|
|
|
|
install_exact_team_match(auto, log)
|
|
guard = WindowGuard(screen, cfg, log=log)
|
|
guard.start()
|
|
restore_path = os.environ.get(RESTORE_ENV)
|
|
originals = apply_overrides(cfg, task, params, restore_path)
|
|
try:
|
|
if task == 'all':
|
|
scheme.script_task()
|
|
return 0
|
|
# 与 script_task() 相同的前置步骤
|
|
scheme.init_game()
|
|
# ⚠ 必须放在 init_game() 之后: 它会把 auto.input_handler 重建一次,
|
|
# 放在前面装好的代理会被覆盖掉(2026-10-22 实测: 日志有 patch 记录但代理从未被调用)。
|
|
if os.environ.get('LCP_DRIVER_INPUT', '').strip() == '1':
|
|
install_driver_click(auto, screen, log=log)
|
|
path_manager.initialize_paths()
|
|
auto.clear_img_cache()
|
|
retry_monitor.start()
|
|
get_reward = None
|
|
if auto.click_element('battle/turn_assets.png', take_screenshot=True):
|
|
print('[lc_mcp] 检测到正在战斗, 先打完', flush=True)
|
|
get_reward = battle.fight()
|
|
{
|
|
'daily': scheme.Daily_task_wrapper(get_reward=get_reward),
|
|
'reward': scheme.to_get_reward,
|
|
'enkephalin': scheme.Buy_enkephalin,
|
|
'mirror': scheme.Mirror_task,
|
|
}[task]()
|
|
return 0
|
|
finally:
|
|
guard.stop()
|
|
retry_monitor.stop()
|
|
restore(cfg, originals, restore_path)
|
|
|
|
|
|
def main(argv):
|
|
if len(argv) == 3 and argv[1] == '--restore':
|
|
restore_file(argv[2])
|
|
print('[lc_mcp] restored config from %s' % argv[2], flush=True)
|
|
return 0
|
|
if len(argv) < 2 or argv[1] not in TASKS:
|
|
print('usage: python -m lc_mcp.aalc_task <%s> [json]' % '|'.join(TASKS), file=sys.stderr)
|
|
return 2
|
|
task = argv[1]
|
|
params = json.loads(argv[2]) if len(argv) > 2 else {}
|
|
unknown = set(params) - set(TASKS[task])
|
|
if unknown:
|
|
print('unknown params for %s: %s' % (task, ', '.join(sorted(unknown))), file=sys.stderr)
|
|
return 2
|
|
print('[lc_mcp] task %s start %s' % (task, json.dumps(params)), flush=True)
|
|
code = run(task, params)
|
|
if code:
|
|
print('[lc_mcp] task %s aborted (exit %s)' % (task, code), flush=True)
|
|
return code
|
|
print('[lc_mcp] task %s done' % task, flush=True)
|
|
return 0
|
|
|
|
|
|
if __name__ == '__main__':
|
|
sys.exit(main(sys.argv))
|