Files
lc-mcp/lc_mcp/aalc_engine.py
T
ywsandClaude Opus 5.5 96a31bc1ae 按台账修镜牢: 饰品弹窗兜底、按键扫描码、卡包按体系加分
按 docs/ISSUES.md 处理本机回归后待修/待数据的条目:

- I-06/R-04 开局「获得E.G.O饰品」弹窗死循环: 确认模板连续第 3 次命中起改点
  按钮中心(勾号 +76/1440), 连续 20 次还在就记 [ERROR]、抛 TaskAbort 结束任务
  (上游 continue 跳过了循环计数)。单测执行上游真实 select_init_ego_gift。
- I-05/R-03 键盘寻路 0 成功: 前台按键改发带扫描码和扩展键标志的 keybd_event,
  各模式按住 0.12 秒(上游注释: Unity 6 校验扫描码/扩展键); 连续 3 个节点进不去
  就本次任务停用键盘寻路(方案 B)。MCP key 工具前台同样处理。LCP_KEY_HOLD=0 关。
- I-07/R-02 事件表 6 -> 9 条: 蜘蛛巢、归乡路途两段(拿「归途」), 按 wiki.gg 核对结果;
  全表按本机装的零协 release 2026111301 重新核对唯一性。
- I-13 复查 5992827: 「进入」先模板确认再 OCR(原来每轮都跑一次整屏 OCR);
  选队页重试重新截图; 入场切队失败记 [ERROR] 且本局不再重翻列表。
- R-01/I-08 卡包按体系加分: wiki.gg 122 个卡包(楼层/专属饰品/Featured 分类) +
  零协中文名生成 lc_mcp/data/theme_pack_systems.json(scripts/build_theme_pack_systems.py);
  灰机 wiki 对云端 403、BWIKI 无卡包条目, 中文社区攻略用 codex CLI 联网检索,
  推荐/避开并入表的 community。选卡包时临时加到 AALC 权重上, 按当前难度只看会出现
  的卡包; 审计记 system_bonus。LCP_THEME_PACK_SYSTEMS=0 关。
- 文档: ISSUES/BACKLOG/README 更新, 新增 docs/feedback/2026-11-15/REGRESSION.md
  (本轮回归清单)和 theme-pack-research.md(检索记录)。

单测 703 个全过(Linux)。实机未验。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 01:24:46 +00:00

363 lines
16 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 contextlib import contextmanager
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 show_game_background(self):
if self.cfg.simulator:
raise RuntimeError('后台恢复窗口仅支持 Windows 游戏客户端')
hwnd = self.screen.handle.hwnd
if not hwnd:
raise RuntimeError('未找到游戏窗口,未修改其他窗口')
import win32con
import win32gui
foreground = win32gui.GetForegroundWindow()
win32gui.ShowWindow(hwnd, win32con.SW_SHOWNOACTIVATE)
return {'visible': bool(win32gui.IsWindowVisible(hwnd)),
'foreground_changed': win32gui.GetForegroundWindow() != foreground}
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 _focus_game(self):
handle = self.screen.handle
try:
handle.setForeground()
except Exception:
pass # Windows 可拒绝后台服务直接切换前台;下面通过输入线程重试。
if handle.isActive:
return
import win32api
import win32gui
import win32process
current = win32api.GetCurrentThreadId()
foreground = win32gui.GetForegroundWindow()
if not foreground:
return
threads = {win32process.GetWindowThreadProcessId(foreground)[0],
win32process.GetWindowThreadProcessId(handle.hwnd)[0]} - {current}
attached = []
try:
for target in sorted(threads):
win32process.AttachThreadInput(current, target, True)
attached.append(target)
win32gui.BringWindowToTop(handle.hwnd)
win32gui.SetActiveWindow(handle.hwnd)
win32gui.SetFocus(handle.hwnd)
handle.setForeground()
finally:
for target in reversed(attached):
win32process.AttachThreadInput(current, target, False)
def _prepare_input(self):
# 前台后端将输入发给系统当前窗口;服务启动后不一定仍是游戏。
# 先确认焦点,获取失败时停止,避免把点击或按键发送到其他应用。
if not self.cfg.simulator and self.cfg.win_input_type == 'foreground':
handle = self.screen.handle
if not handle.hwnd or handle.isMinimized:
raise RuntimeError('前台输入需要可见的游戏窗口')
if not handle.isActive:
self._focus_game()
time.sleep(0.15)
if not handle.isActive:
raise RuntimeError('无法激活游戏窗口,未发送输入')
def _background_mouse(self, message, x, y, held=False):
"""Deliver client coordinates without moving the user's system pointer."""
import win32api
import win32con
hwnd = self.screen.handle.hwnd
if not hwnd:
raise RuntimeError('未找到游戏窗口,未发送鼠标消息')
win32api.PostMessage(hwnd, message, win32con.MK_LBUTTON if held else 0,
win32api.MAKELONG(int(x), int(y)))
def _restore_background_position(self, position):
import win32con
import win32gui
win32gui.SetWindowPos(self.screen.handle.hwnd, 0, *position, 0, 0,
win32con.SWP_NOSIZE | win32con.SWP_NOZORDER |
win32con.SWP_NOACTIVATE)
@contextmanager
def _background_target(self, x, y):
position = None
transparent = self.cfg.win_input_type == 'window_move'
try:
if transparent:
self.screen.handle.set_window_transparent(True)
position = self.auto.input_handler._set_window_pos(int(x), int(y))
self.auto.input_handler.set_active()
yield position
finally:
try:
if position is not None:
try:
time.sleep(0.12)
finally:
self._restore_background_position(position)
finally:
if transparent:
self.screen.handle.set_window_transparent(False)
def click(self, x, y, times=1):
self._prepare_input()
if not self.cfg.simulator and self.cfg.win_input_type in ('background', 'window_move'):
import win32con
self.auto.wait_pause()
for _ in range(int(times)):
with self._background_target(x, y):
self._background_mouse(win32con.WM_MOUSEMOVE, x, y)
time.sleep(0.12)
try:
self._background_mouse(win32con.WM_LBUTTONDOWN, x, y, held=True)
time.sleep(0.12)
finally:
self._background_mouse(win32con.WM_LBUTTONUP, x, y)
# Let Unity consume the release before another target is selected.
time.sleep(0.12)
elif not self.cfg.simulator and self.cfg.win_input_type == 'foreground':
import pyautogui
px, py = self.auto.input_handler.pos_offset(int(x), int(y))
pyautogui.moveTo(px, py)
# Give Unity a frame to update the pointer target before pressing.
time.sleep(0.12)
for _ in range(int(times)):
pyautogui.mouseDown(px, py)
try:
# Slow Unity frames can miss a shorter foreground press.
# Keep this below the game's long-press detail threshold.
time.sleep(0.35)
finally:
pyautogui.mouseUp(px, py)
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)))
self._prepare_input()
if not self.cfg.simulator and self.cfg.win_input_type in ('background', 'window_move'):
# 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)
elif not self.cfg.simulator and self.cfg.win_input_type == 'foreground':
# pyautogui 发的按键扫描码是 0、方向键不带扩展键标志, Unity 6 会忽略(ISSUES I-05);
# 与任务子进程的按键补丁用同一个 press_vk, 同样按住 0.12 秒。
import ctypes
from module.automation.input_handlers.input import EXTENDED_KEY_VKS, key_list
from .aalc_patches import press_vk
vk = key_list[k]
press_vk(ctypes.windll.user32, vk, vk in EXTENDED_KEY_VKS, hold=0.12)
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._prepare_input()
if not self.cfg.simulator and self.cfg.win_input_type in ('background', 'window_move'):
import win32con
self.auto.wait_pause()
with self._background_target(from_x, from_y) as position:
self._background_mouse(win32con.WM_MOUSEMOVE, from_x, from_y)
time.sleep(0.12)
last_x, last_y = from_x, from_y
try:
self._background_mouse(win32con.WM_LBUTTONDOWN, from_x, from_y, held=True)
time.sleep(0.12)
steps = max(1, int(float(duration) / 0.02))
for step in range(1, steps + 1):
last_x = round(from_x + (to_x - from_x) * step / steps)
last_y = round(from_y + (to_y - from_y) * step / steps)
if position is not None:
self.auto.input_handler._set_window_pos(last_x, last_y)
self._background_mouse(win32con.WM_MOUSEMOVE, last_x, last_y, held=True)
time.sleep(float(duration) / steps)
time.sleep(0.12)
finally:
self._background_mouse(win32con.WM_LBUTTONUP, last_x, last_y)
return
if not self.cfg.simulator and self.cfg.win_input_type == 'foreground':
import pyautogui
start = self.auto.input_handler.pos_offset(int(from_x), int(from_y))
end = self.auto.input_handler.pos_offset(int(to_x), int(to_y))
pyautogui.moveTo(*start)
time.sleep(0.12)
pyautogui.mouseDown(*start)
try:
if start == end:
time.sleep(float(duration))
else:
pyautogui.moveTo(*end, duration=float(duration))
time.sleep(0.12)
finally:
pyautogui.mouseUp(*end)
return
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._prepare_input()
self.auto.input_text(str(text))