fix: 修复更新时主程序退出不彻底与ADB路径与模拟器路径相关问题

This commit is contained in:
DLmaster361
2025-04-20 16:40:14 +08:00
parent ba7299e20c
commit f3af6ddbbc
5 changed files with 94 additions and 17 deletions

View File

@@ -606,7 +606,7 @@ class MaaUserConfig(QConfig):
class AppConfig(GlobalConfig):
VERSION = "4.3.4.3"
VERSION = "4.3.4.4"
gameid_refreshed = Signal()
PASSWORD_refreshed = Signal()

View File

@@ -287,6 +287,11 @@ class MaaManager(QObject):
self.ADB_path = Path(
set["Configurations"]["Default"]["Connect.AdbPath"]
)
self.ADB_path = (
self.ADB_path
if self.ADB_path.is_absolute()
else self.maa_root_path / self.ADB_path
)
self.ADB_address = set["Configurations"]["Default"][
"Connect.Address"
]
@@ -300,8 +305,6 @@ class MaaManager(QObject):
]
== "True"
)
# 添加静默进程标记
Config.silence_list.append(self.emulator_path)
# 增强任务任务开始前强杀ADB
if "ADB" in self.set["RunSet"]["EnhanceTask"]:
@@ -315,12 +318,34 @@ class MaaManager(QObject):
except subprocess.CalledProcessError as e:
# 忽略错误,因为可能本来就没有连接
logger.warning(f"{self.name} | 释放ADB时出现异常{e}")
except Exception as e:
logger.error(f"{self.name} | 释放ADB时出现异常{e}")
self.push_info_bar.emit(
"error",
"释放ADB时出现异常",
"请检查MAA中ADB路径设置",
-1,
)
if self.if_open_emulator_process:
self.emulator_process = subprocess.Popen(
[self.emulator_path, *self.emulator_arguments],
creationflags=subprocess.CREATE_NO_WINDOW,
)
try:
self.emulator_process = subprocess.Popen(
[self.emulator_path, *self.emulator_arguments],
creationflags=subprocess.CREATE_NO_WINDOW,
)
except Exception as e:
logger.error(f"{self.name} | 启动模拟器时出现异常:{e}")
self.push_info_bar.emit(
"error",
"启动模拟器时出现异常",
"请检查MAA中模拟器路径设置",
-1,
)
self.if_open_emulator = True
break
# 添加静默进程标记
Config.silence_list.append(self.emulator_path)
# 创建MAA任务
maa = subprocess.Popen(
@@ -388,6 +413,14 @@ class MaaManager(QObject):
except subprocess.CalledProcessError as e:
# 忽略错误,因为可能本来就没有连接
logger.warning(f"{self.name} | 释放ADB时出现异常{e}")
except Exception as e:
logger.error(f"{self.name} | 释放ADB时出现异常{e}")
self.push_info_bar.emit(
"error",
"释放ADB时出现异常",
"请检查MAA中ADB路径设置",
-1,
)
if self.if_kill_emulator:
if "Emulator" in self.set["RunSet"]["EnhanceTask"]:
System.kill_process(self.emulator_path)

View File

@@ -26,13 +26,12 @@ v4.3
"""
from loguru import logger
from PySide6.QtWidgets import QWidget, QVBoxLayout
from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout
from PySide6.QtCore import Qt
from qfluentwidgets import (
ScrollArea,
FluentIcon,
MessageBox,
Dialog,
HyperlinkCard,
HeaderCardWidget,
ExpandGroupSettingCard,
@@ -42,13 +41,12 @@ from qfluentwidgets import (
import os
import re
import json
import time
import shutil
import subprocess
from datetime import datetime
from packaging import version
from pathlib import Path
from typing import Dict, List, Union
from typing import Dict, Union
from app.core import Config, MainInfoBar, Network
from app.services import Crypto, System, Notify
@@ -405,9 +403,12 @@ class Setting(QWidget):
subprocess.Popen(
[Config.app_path / "AUTO_Updater.active.exe"],
creationflags=subprocess.CREATE_NO_WINDOW,
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP
| subprocess.DETACHED_PROCESS
| subprocess.CREATE_NO_WINDOW,
)
self.window().close()
QApplication.quit()
# 无版本更新
else:

View File

@@ -31,6 +31,7 @@ import zipfile
import requests
import subprocess
import time
import psutil
import win32crypt
import base64
from packaging import version
@@ -180,7 +181,11 @@ class ZipExtractProcess(QThread):
self.accomplish.emit()
break
except PermissionError:
self.info.emit(f"解压出错:{self.name}正在运行,正在等待其关闭")
if self.name == "AUTO_MAA":
self.info.emit(f"解压出错AUTO_MAA正在运行正在尝试将其关闭")
self.kill_process(self.app_path / "AUTO_MAA.exe")
else:
self.info.emit(f"解压出错:{self.name}正在运行,正在等待其关闭")
time.sleep(1)
except Exception as e:
@@ -190,6 +195,30 @@ class ZipExtractProcess(QThread):
self.info.emit(f"解压更新时出错:\n{e}")
return None
def kill_process(self, path: Path) -> None:
"""根据路径中止进程"""
for pid in self.search_pids(path):
killprocess = subprocess.Popen(
f"taskkill /F /PID {pid}",
shell=True,
creationflags=subprocess.CREATE_NO_WINDOW,
)
killprocess.wait()
def search_pids(self, path: Path) -> list:
"""根据路径查找进程PID"""
pids = []
for proc in psutil.process_iter(["pid", "exe"]):
try:
if proc.info["exe"] and proc.info["exe"].lower() == str(path).lower():
pids.append(proc.info["pid"])
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
# 进程可能在此期间已结束或无法访问,忽略这些异常
pass
return pids
class DownloadManager(QDialog):
"""下载管理器"""
@@ -518,7 +547,8 @@ class DownloadManager(QDialog):
if "deleted" in info:
for file_path in info["deleted"]:
(self.app_path / file_path).unlink()
if (self.app_path / file_path).exists():
(self.app_path / file_path).unlink()
(self.app_path / "changes.json").unlink()
@@ -531,12 +561,16 @@ class DownloadManager(QDialog):
if not self.isInterruptionRequested and self.name == "AUTO_MAA":
subprocess.Popen(
[self.app_path / "AUTO_MAA.exe"],
creationflags=subprocess.CREATE_NO_WINDOW,
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP
| subprocess.DETACHED_PROCESS
| subprocess.CREATE_NO_WINDOW,
)
elif not self.isInterruptionRequested and self.name == "MAA":
subprocess.Popen(
[self.app_path / "MAA.exe"],
creationflags=subprocess.CREATE_NO_WINDOW,
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP
| subprocess.DETACHED_PROCESS
| subprocess.CREATE_NO_WINDOW,
)
self.update_info(f"{self.name}更新成功!")