按"应该在输入层接管"的思路做的: 不是只改 lc_mcp 自己的 click, 而是把 AALC 任务流里
所有鼠标点击都转到罗技驱动上 —— 这样镜牢/日常的点击也走物理输入。
一、lc_mcp/driver_input.py(新)
DriverInput 封装 logitech.driver.dll(device_open/moveR/mouse_down/mouse_up)。
定位用**闭环**: 发相对位移 -> 读回实际坐标 -> 不够再发, 收敛到 <=1px 才认。
不依赖任何标定常数, 也不怕偶发坐标跳变。
preflight() 有三条硬性拒绝, 每条都对应一个实测过的失败条件:
* device_open() != 1 -> 罗技驱动没就绪
* GetForegroundWindow() == 0 -> 当前不是物理交互会话(RDP 独立会话里驱动级无效;
实测那时 22 次采样光标纹丝不动, 切到 console 立刻正常)
* 指针加速开着 -> counts 与像素不成比例(实测 +100/+200/+400 对应
-45/-3/-3 像素), 关掉后严格 1:1
二、aalc_patches.py: DriverClickProxy + install_driver_click
只接管 mouse_click / mouse_down / mouse_up, 其余(拖拽/滑动/滚轮/等待)一律透传。
拖拽类不接管: 那些行为差异大, 用相对位移复现更容易出错。
⛔ 关键细节: automation.py 初始化时把方法绑成了别名
(self.mouse_click = self.input_handler.mouse_click), 所以**换 handler 不够**,
必须把别名一起重绑, 否则任务流里调的还是老方法。测试里专门守这一条。
条件不满足时**一个都不改** —— 宁可继续用原来的注入方式, 也不要半吊子接管。
三、aalc_task.py: 环境变量 LCP_DRIVER_INPUT=1 时才安装; 默认关闭。
验证(真机, 物理 console 会话):
preflight: True | ok 前台窗口存在 True 指针加速 False
move_to (400,300) / (1200,700) / (800,500) -> 误差全部 (0,0)
click(600,400) -> True
测试 15 个, 重点在"各种条件不满足时必须拒绝"。全套 399/399 通过。
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)
|
|
# 驱动级鼠标点击: 默认关闭, 设 LCP_DRIVER_INPUT=1 才启用。
|
|
# 条件不满足(非物理会话 / 指针加速没关 / 驱动没装)时什么都不改, 保持原样。
|
|
if os.environ.get('LCP_DRIVER_INPUT', '').strip() == '1':
|
|
install_driver_click(auto, screen, log=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()
|
|
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))
|