Files
lc-mcp/lc_mcp/aalc_engine.py
T
ywsandClaude Opus 5.5 030527f5de 审查修复: 任务配置恢复、执行器容错与续局状态、识别性能
正确性
- stop_task 强杀后 run_task 的临时次数会残留在 config.yaml (AALC 中途整份存盘);
  任务启动时先落盘原值, 强停/异常退出后由 TaskRunner 用 --restore 写回,
  写回失败时拒绝启动新任务并在 task_status 显示 pending_restore
- Jev 执行器: 工具失败不再抛异常导致整局退出 (Session raise_on_error=False),
  原有的"截图暂不可用"等降级分支终于生效; 刷新计数/开战标记只在动作成功后更新
- 一局完成后归档 jev_shop_context.json, 否则 claim_requested 让下一局一启动就判定完成
- 通用按钮直接点已核对的坐标, 不再 click_text 重新搜索 (可能点到背景同名按钮)
- 认证行按字节比较, 非 ASCII 输入不再让 compare_digest 抛 TypeError
- 单实例互斥改用 use_last_error 读取 ERROR_ALREADY_EXISTS

性能
- find_image 只在窗口尺寸变化时清模板缓存 (原来每次两次全量 gc); 模板列表只扫描一次
- 截图 PNG 压缩级别 1: 1080p 存图 0.66s -> 0.19s
- 执行器每帧截图只解码一次 (button_enabled 75ms -> 0.3ms); 地图模板与 ORB 描述子按比例缓存
- 箭头模板用 imdecode 读取, 支持中文路径 (输出与 imread 逐像素一致)

测试 73 -> 84, 修改前的识别输出与修改后逐项比对一致。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-03 00:31:21 +00:00

202 lines
8.6 KiB
Python

# -*- coding: utf-8 -*-
"""AALC 引擎: 截图/OCR/模板匹配/输入全部走 AALC 的 module.automation。
只能在 Windows 的交互桌面会话里运行(找窗口句柄、后台截图、PostMessage 都依赖它)。
AALC 代码按相对路径读 assets/ 和 config.yaml, 所以 bootstrap() 会 chdir 到 AALC 目录。
"""
import glob
import os
import sys
import time
from .engine import Engine
REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEFAULT_AALC_DIR = os.path.join(REPO, 'vendor', 'aalc')
_booted = None
def aalc_dir():
return os.path.abspath(os.environ.get('AALC_DIR') or DEFAULT_AALC_DIR)
def bootstrap():
"""进程内初始化 AALC (幂等)。必须在 import 任何 AALC 模块之前调用。"""
global _booted
if _booted:
return _booted
d = aalc_dir()
if not os.path.isfile(os.path.join(d, 'main.py')):
raise RuntimeError('AALC 目录无效: %s (git submodule update --init, 或设置 AALC_DIR)' % d)
os.chdir(d)
if d not in sys.path:
sys.path.insert(0, d)
# 与 AALC main.py 相同的 DPI 设置, 否则截图/点击坐标在缩放下错位
from ctypes import c_void_p, windll
try:
windll.user32.SetProcessDpiAwarenessContext(c_void_p(-4))
except (AttributeError, OSError):
try:
windll.shcore.SetProcessDpiAwareness(2)
except (AttributeError, OSError):
windll.user32.SetProcessDPIAware()
from module.logger.my_log import Logger
Logger()
_booted = d
return d
def prune(directory, pattern, keep):
files = sorted(glob.glob(os.path.join(directory, pattern)), key=os.path.getmtime)
for p in files[:-keep] if keep > 0 else []:
try:
os.remove(p)
except OSError:
pass
class AalcEngine(Engine):
_templates = None # 模板相对路径集合; assets 运行中不变, 只扫描一次磁盘
_template_size = None # 当前 AALC 模板缓存对应的缩放尺寸
def __init__(self, shot_dir, keep_shots=30):
self.shot_dir = os.path.abspath(shot_dir)
os.makedirs(self.shot_dir, exist_ok=True)
self.keep_shots = keep_shots
root = bootstrap()
from module.automation import auto
from module.automation.input_handlers.input import key_list
from module.automation.screenshot import ScreenShot
from module.config import cfg
from module.game_and_screen import screen
from module.ocr import ocr
from utils.path_manager import path_manager
path_manager.initialize_paths()
self.auto, self.cfg, self.screen, self.ocr_engine = auto, cfg, screen, ocr
self.ScreenShot = ScreenShot
self.key_names = sorted(key_list)
self.images_root = os.path.join(root, 'assets', 'images')
# ---- 看 ----
def _grab(self, gray):
if not self.cfg.simulator and not self.screen.handle.hwnd:
raise RuntimeError('未找到游戏窗口 %r: 先启动游戏(run_task 会自动启动)' % self.cfg.game_title_name)
# 不用 auto.take_screenshot: 它连续失败 60 秒会直接 taskkill 游戏
with self.auto._screenshot_lock:
img = self.ScreenShot.take_screenshot(gray)
if img is None:
raise RuntimeError('AALC 截图失败(窗口最小化/被遮挡? 见 AALC logs)')
self.auto.screenshot = img # find_image_element 读这个
return img
def _save(self, img):
stamp = time.strftime('%Y%m%d_%H%M%S') + '_%03d' % (int(time.time() * 1000) % 1000)
path = os.path.join(self.shot_dir, 'shot_%s.png' % stamp)
# 每次 get_state 都存图: 1080p 下默认压缩级别 6 约 0.66s, 级别 1 约 0.19s, 文件只大 ~17%
img.save(path, compress_level=1)
prune(self.shot_dir, 'shot_*.png', self.keep_shots)
return {'path': path.replace('\\', '/'), 'w': img.width, 'h': img.height}
def screenshot(self):
return self._save(self._grab(gray=False))
def ocr(self):
img = self._grab(gray=False)
shot = self._save(img)
res = self.ocr_engine.run(img)
items = []
boxes = res.boxes if res.boxes is not None else []
scores = res.scores if res.scores is not None else [1.0] * len(boxes)
for box, text, score in zip(boxes, res.txts or (), scores):
xs = [p[0] for p in box]
ys = [p[1] for p in box]
x0, y0 = int(min(xs)), int(min(ys))
items.append({'text': text, 'x': x0, 'y': y0,
'w': int(max(xs)) - x0, 'h': int(max(ys)) - y0,
'score': round(float(score), 3)})
return shot, items
def list_images(self, prefix=''):
if self._templates is None:
names = set()
for theme in ('dark', 'default'):
for lang in ('zh_cn', 'en', 'share'):
base = os.path.join(self.images_root, theme, lang)
for p in glob.glob(os.path.join(base, '**', '*.png'), recursive=True):
names.add(os.path.relpath(p, base).replace('\\', '/'))
self._templates = frozenset(names)
return sorted(n for n in self._templates if n.startswith(prefix))
def find_image(self, name, threshold=0.8, model='normal'):
if name not in self.list_images(name):
raise ValueError('没有模板图 %r (用 list_images 查)' % name)
img = self._grab(gray=True)
# 模板按 2560x1440 制作。配置中的目标窗口尺寸不一定等于
# 当前客户区;临时按实际 16:9 画面缩放,不调整窗口或保存配置。
old_size = self.cfg.set_win_size
# 保留亚像素比例:例如 1278 宽对应 718.875 高,取整会改变
# 缩放采样,使小图标的匹配置信度明显下降。
match_size = max(1, min(img.height, img.width * 9 / 16))
# AALC 的模板缓存按加载时的 set_win_size 缩放, 键里不含尺寸; 尺寸变了才清空。
# 本进程只有这里用模板, 缓存可以一直保持当前客户区的比例
# (clear_img_cache 每次都会全量 gc, 不能每次调用都清)。
if match_size != self._template_size:
self.auto.clear_img_cache()
self._template_size = match_size
changed = match_size != old_size
try:
if changed:
self.cfg.unsaved_set_value('set_win_size', match_size)
pos = self.auto.find_image_element(name, threshold, model=model)
finally:
if changed:
self.cfg.unsaved_set_value('set_win_size', old_size)
return (int(pos[0]), int(pos[1])) if pos else None
# ---- 动手 (AALC 输入后端,由 config.yaml 决定后台消息/前台输入) ----
def click(self, x, y, times=1):
if not self.cfg.simulator and self.cfg.win_input_type == 'background':
handler = self.auto.input_handler
self.auto.wait_pause()
for _ in range(int(times)):
handler.set_mouse_pos(int(x), int(y))
handler.set_active()
handler.mouse_down(int(x), int(y))
try:
time.sleep(0.12)
finally:
handler.mouse_up(int(x), int(y))
# Let Unity consume the release before another target is selected.
time.sleep(0.12)
else:
self.auto.mouse_click(int(x), int(y), times=int(times))
def keypress(self, key):
k = str(key).lower()
if k not in self.key_names:
raise ValueError('AALC 不支持按键 %r, 可用: %s' % (key, ' '.join(self.key_names)))
if not self.cfg.simulator and self.cfg.win_input_type == 'background':
# Unity 可能在同一帧收到 down/up,导致快捷键被吞掉。
handler = self.auto.input_handler
self.auto.wait_pause()
handler.set_active()
handler.key_down(k)
try:
time.sleep(0.12)
finally:
handler.key_up(k)
else:
self.auto.key_press(k)
def drag(self, from_x, from_y, to_x, to_y, duration=1.0):
img = self._grab(gray=False)
for x, y in ((from_x, from_y), (to_x, to_y)):
if not (0 <= x < img.width and 0 <= y < img.height):
raise ValueError('拖动坐标超出游戏客户区 %sx%s' % (img.width, img.height))
self.auto.mouse_drag(int(from_x), int(from_y), drag_time=float(duration),
dx=int(to_x) - int(from_x), dy=int(to_y) - int(from_y))
def type_text(self, text):
self.auto.input_text(str(text))