This website requires JavaScript.
0
Issues
Pull Requests
Milestones
Explore
0
Create…
New Repository
New Migration
New Organization
public
Signed in as
public
Profile
Starred
Subscriptions
Settings
Help
Sign Out
yws
/
lc-mcp
Watch
1
Star
0
Fork
0
Code
Issues
Pull Requests
Actions
Packages
Projects
Releases
Wiki
Activity
Files
lc-mcp
/
lc_mcp
/
or
Cancel
Edit File
Preview
Preview Changes
Spaces
Tabs
2
4
8
Wrap
No wrap
# -*- 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 # 实验: 编队列表滑动补丁, 仅在 LCP_TEAM_SCROLL_PATCH=1 时安装 (见 aalc_input_patch) from . import aalc_input_patch aalc_input_patch.install() 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 _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 click(self, x, y, times=1): self._prepare_input() 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) 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 == '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) elif not self.cfg.simulator and self.cfg.win_input_type == 'foreground': import pyautogui pyautogui.keyDown(k) try: time.sleep(0.12) finally: pyautogui.keyUp(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._prepare_input() 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))
Commit Changes
Add a Signed-off-by trailer by the committer at the end of the commit log message.
Commit directly to the
fix/team-scroll-patch
branch.
Create a
new branch
for this commit and start a pull request.
Commit Changes
Cancel