一、为什么"我用脚本点击时画面一直抽搐, AALC 点击不抽搐"
链条查清了:
* lc_mcp/aalc_engine.py 的 click 每次先 set_window_transparent(True)
* 而这个方法的实现是 SetLayeredWindowAttributes(hwnd, 0, 0, LWA_ALPHA)
—— alpha=0, 整个游戏窗口**完全透明(看不见)**
* 它原来**只有 background_screenshot 会恢复成 False**(screenshot.py:194)
* 我的脚本是"点击 -> 截图 -> 点击 -> 截图"高频交替
=> 窗口在不可见/可见之间反复切换 => 肉眼看就是一直抽搐
AALC 自己的 WindowMoveInput.mouse_click 只搬窗口、**根本不碰透明度**, 所以它最多
"跳一下", 不会闪 —— 这就是两边观感不同的原因。
改法: 透明度在**本次点击/拖动结束时立刻还原**, 并且放在嵌套 try/finally 里 ——
还原位置那步也可能抛错(任务被停), 不能让它把透明度还原跳过去, 否则窗口会一直隐形。
(第一版就是把还原排在 finally 里的 sleep 之后, 测试里"异常路径"那条立刻抓到了。)
二、render_ok() 改成测 AALC 真正走的那条路
原来只测屏幕 BitBlt(GetDC(0))。2026-10-22 实测它在 RDP 会话下会瞬时失败, 而 AALC
走的是 background_screenshot(窗口 DC + PrintWindow), 那条路照样能截到图 —— 我的自检
把本来能干活的轮次判成"截不到图"跳过了(10-17 02:23 就白丢过一轮)。
现在优先测窗口能否采集, 窗口不在或不可采才回退到屏幕。
测试: 新增 3 个(点击内还原/异常也还原/前台模式不碰透明度) + 4 个 render_ok 分支。
全套 384/384 通过。
378 lines
17 KiB
Python
378 lines
17 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 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)
|
|
|
|
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
|
|
handler = self.auto.input_handler
|
|
self.auto.wait_pause()
|
|
for _ in range(int(times)):
|
|
position = None
|
|
transparent = False
|
|
if self.cfg.win_input_type == 'window_move':
|
|
# 搬到光标下面时设成透明/点击穿透, 免得挡住用户的光标。
|
|
# ⛔ 必须在本次点击结束时就还原: 以前只有 background_screenshot
|
|
# 会把它恢复成 False, 于是"点击 -> 截图 -> 点击 -> 截图"这种
|
|
# 高频交替会让游戏窗口在不可见/可见之间反复切换 —— 肉眼看就是
|
|
# 画面一直抽搐。AALC 自己的 WindowMoveInput.mouse_click 根本不碰
|
|
# 透明度, 所以它只是"跳一下", 不会闪。
|
|
self.screen.handle.set_window_transparent(True)
|
|
transparent = True
|
|
position = handler._set_window_pos(int(x), int(y))
|
|
handler.set_active()
|
|
self._background_mouse(win32con.WM_MOUSEMOVE, x, y)
|
|
time.sleep(0.12)
|
|
self._background_mouse(win32con.WM_LBUTTONDOWN, x, y, held=True)
|
|
try:
|
|
time.sleep(0.12)
|
|
finally:
|
|
# 嵌套 try/finally: 还原位置那步也可能抛错(比如任务被停),
|
|
# 但透明度**无论如何**都要还原, 否则窗口会一直隐形。
|
|
try:
|
|
self._background_mouse(win32con.WM_LBUTTONUP, x, y)
|
|
if position is not None:
|
|
time.sleep(0.12)
|
|
self._restore_background_position(position)
|
|
finally:
|
|
if transparent:
|
|
self.screen.handle.set_window_transparent(False)
|
|
# 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':
|
|
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 in ('background', 'window_move'):
|
|
import win32con
|
|
self.auto.wait_pause()
|
|
position = None
|
|
transparent = False
|
|
if self.cfg.win_input_type == 'window_move':
|
|
# 同 click(): 透明必须在本次拖动结束时就还原, 不能留给下一次截图
|
|
self.screen.handle.set_window_transparent(True)
|
|
transparent = True
|
|
position = self.auto.input_handler._set_window_pos(int(from_x), int(from_y))
|
|
self.auto.input_handler.set_active()
|
|
self._background_mouse(win32con.WM_MOUSEMOVE, from_x, from_y)
|
|
time.sleep(0.12)
|
|
self._background_mouse(win32con.WM_LBUTTONDOWN, from_x, from_y, held=True)
|
|
last_x, last_y = from_x, from_y
|
|
try:
|
|
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:
|
|
# 同 click(): 透明度必须在嵌套的 finally 里还原, 不能被还原位置那步的
|
|
# 异常跳过, 否则窗口会一直隐形。
|
|
try:
|
|
self._background_mouse(win32con.WM_LBUTTONUP, last_x, last_y)
|
|
if position is not None:
|
|
time.sleep(0.12)
|
|
self._restore_background_position(position)
|
|
finally:
|
|
if transparent:
|
|
self.screen.handle.set_window_transparent(False)
|
|
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))
|