# -*- 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): 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) img.save(path) 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=''): 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('\\', '/')) return sorted(n for n in names 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) self._grab(gray=True) pos = self.auto.find_image_element(name, threshold, model=model) return (int(pos[0]), int(pos[1])) if pos else None # ---- 动手 (AALC 输入后端, 由 config.yaml win_input_type 决定: 默认后台 PostMessage) ---- def click(self, x, y, times=1): 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.auto.key_press(k) def type_text(self, text): self.auto.input_text(str(text))