Skip to content

Commit 3b3486c

Browse files
Merge pull request #4 from sudoriaa/main
feat: 添加管理员权限检查与分辨率检测,并输出警告日志
2 parents c90d054 + 7997959 commit 3b3486c

4 files changed

Lines changed: 132 additions & 4 deletions

File tree

agent/custom/action/auto_make_coffee.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ def __init__(self):
6767
def run(self, context: Context, argv: CustomAction.RunArg) -> CustomAction.RunResult:
6868
print("=== Auto Make Coffee Action Started ===")
6969
controller = context.tasker.controller
70-
7170
make_count = 10
7271
check_freq = 0.5
7372
if argv.custom_action_param:
@@ -92,7 +91,7 @@ def run(self, context: Context, argv: CustomAction.RunArg) -> CustomAction.RunRe
9291
if context.tasker.stopping:
9392
return CustomAction.RunResult(success=False)
9493
print(f"=== Making Coffee {count + 1}/{make_count} ===")
95-
94+
9695
# Step 1: 选择关卡
9796
print("Tapping on select level...")
9897
click_rect(controller, select_level_target)

agent/main.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,59 @@ def check_and_install_dependencies():
393393
logger.info("Pip 依赖安装已禁用,跳过依赖安装")
394394

395395

396+
# -----
397+
# region 环境检测
398+
# -----
399+
400+
401+
def _check_chinese_path():
402+
"""检测当前程序是否运行在含中文字符的目录下"""
403+
import re
404+
405+
cwd = os.getcwd()
406+
if re.search(r"[一-鿿]", cwd):
407+
logger.warning(
408+
f"当前运行目录含中文字符: {cwd}\n请将程序移动至纯英文路径下运行。"
409+
)
410+
411+
412+
def _check_admin_privilege():
413+
"""检查是否以管理员权限运行,若否则输出警告"""
414+
import ctypes
415+
416+
if ctypes.windll.shell32.IsUserAnAdmin():
417+
return
418+
419+
logger.warning(
420+
"未以管理员权限运行,部分输入功能可能无法正常使用。"
421+
"请右键 MaaNTE.exe → 以管理员身份运行。"
422+
)
423+
424+
425+
def _check_game_resolution():
426+
"""连接控制器后检测游戏窗口分辨率"""
427+
from utils.win32_process import find_window_by_process, get_client_size
428+
429+
hwnd = find_window_by_process("HTGame.exe")
430+
if hwnd is None:
431+
logger.warning("分辨率检测: 未找到游戏窗口 (HTGame.exe)")
432+
return
433+
434+
size = get_client_size(hwnd)
435+
if size is None:
436+
logger.warning("分辨率检测: 无法获取窗口尺寸")
437+
return
438+
439+
w, h = size
440+
if (w, h) == (1280, 720):
441+
logger.info(f"当前窗口分辨率: {w}x{h} [正常]")
442+
else:
443+
logger.warning(
444+
f"当前窗口分辨率: {w}x{h},请使用 1280x720 分辨率。"
445+
"请将游戏设置为 1280x720 窗口化模式,否则部分功能可能异常。"
446+
)
447+
448+
396449
# -----
397450
# region 核心业务
398451
# -----
@@ -442,6 +495,7 @@ def agent(is_dev_mode=False):
442495
log_pi_environment()
443496
AgentServer.start_up(socket_id)
444497
logger.info("AgentServer启动")
498+
_check_game_resolution()
445499
AgentServer.join()
446500
AgentServer.shut_down()
447501
logger.info("AgentServer关闭")
@@ -463,6 +517,9 @@ def main():
463517
current_version = read_interface_version()
464518
is_dev_mode = current_version == "DEBUG"
465519

520+
if sys.platform.startswith("win"):
521+
_check_admin_privilege()
522+
466523
# 如果是Linux系统或开发模式,启动虚拟环境
467524
if sys.platform.startswith("linux") or is_dev_mode:
468525
ensure_venv_and_relaunch_if_needed()

agent/utils/win32_process.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import ctypes
2+
from ctypes import wintypes
3+
4+
user32 = ctypes.windll.user32
5+
kernel32 = ctypes.windll.kernel32
6+
7+
# 使进程感知 DPI,避免 GetClientRect 返回缩放后的虚拟坐标
8+
# 150% 缩放时未设置此项会导致返回值只有实际分辨率的 2/3
9+
user32.SetProcessDPIAware()
10+
11+
WNDENUMPROC = ctypes.WINFUNCTYPE(wintypes.BOOL, wintypes.HWND, wintypes.LPARAM)
12+
13+
TH32CS_SNAPPROCESS = 0x00000002
14+
15+
16+
class PROCESSENTRY32W(ctypes.Structure):
17+
_fields_ = [
18+
("dwSize", wintypes.DWORD),
19+
("cntUsage", wintypes.DWORD),
20+
("th32ProcessID", wintypes.DWORD),
21+
("th32DefaultHeapID", ctypes.POINTER(wintypes.ULONG)),
22+
("th32ModuleID", wintypes.DWORD),
23+
("cntThreads", wintypes.DWORD),
24+
("th32ParentProcessID", wintypes.DWORD),
25+
("pcPriClassBase", wintypes.LONG),
26+
("dwFlags", wintypes.DWORD),
27+
("szExeFile", ctypes.c_wchar * 260),
28+
]
29+
30+
31+
def get_pids_by_name(process_name):
32+
snapshot = kernel32.CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
33+
if snapshot == -1:
34+
return []
35+
entry = PROCESSENTRY32W()
36+
entry.dwSize = ctypes.sizeof(PROCESSENTRY32W)
37+
pids = []
38+
if kernel32.Process32FirstW(snapshot, ctypes.byref(entry)):
39+
while True:
40+
if entry.szExeFile.lower() == process_name.lower():
41+
pids.append(entry.th32ProcessID)
42+
if not kernel32.Process32NextW(snapshot, ctypes.byref(entry)):
43+
break
44+
kernel32.CloseHandle(snapshot)
45+
return pids
46+
47+
48+
def find_window_by_process(process_name):
49+
pids = get_pids_by_name(process_name)
50+
if not pids:
51+
return None
52+
pid_set = set(pids)
53+
results = []
54+
55+
def callback(hwnd, _lparam):
56+
if not user32.IsWindowVisible(hwnd):
57+
return True
58+
pid = wintypes.DWORD()
59+
user32.GetWindowThreadProcessId(hwnd, ctypes.byref(pid))
60+
if pid.value in pid_set:
61+
results.append(hwnd)
62+
return True
63+
64+
user32.EnumWindows(WNDENUMPROC(callback), 0)
65+
return results[0] if results else None
66+
67+
68+
def get_client_size(hwnd):
69+
rect = wintypes.RECT()
70+
if not user32.GetClientRect(hwnd, ctypes.byref(rect)):
71+
return None
72+
return rect.right - rect.left, rect.bottom - rect.top

assets/interface.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "异环小助手,由MAAFramework强力驱动!",
55
"contact": "QQ群: 1103323319",
66
"license": "MIT",
7-
"welcome": "首次使用!欢迎使用 MAANTE!",
7+
"welcome": "首次使用!欢迎使用 MaaNTE!",
88
"github": "https://github.com/1bananachicken/MaaNTE",
99
"version": "0.0.1",
1010
"controller": [
@@ -13,7 +13,7 @@
1313
"type": "Win32",
1414
"win32": {
1515
"class_regex": ".*",
16-
"window_regex": "异环",
16+
"window_regex": "^异环.*$",
1717
"screencap": "FramePool",
1818
"mouse": "SendMessageWithWindowPos",
1919
"keyboard": "SendMessageWithWindowPos"

0 commit comments

Comments
 (0)