Skip to content

Commit d48c963

Browse files
committed
perf:针对agent提供单独的更新方法
1 parent 6775676 commit d48c963

File tree

1 file changed

+57
-11
lines changed

1 file changed

+57
-11
lines changed

app/utils/update.py

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ def download_file(self, url, file_path, progress_signal: pyqtBoundSignal):
5959

6060
def extract_zip(self, zip_file_path, extract_to):
6161
try:
62-
with zipfile.ZipFile(zip_file_path, "r", metadata_encoding='utf-8') as zip_ref:
62+
with zipfile.ZipFile(
63+
zip_file_path, "r", metadata_encoding="utf-8"
64+
) as zip_ref:
6365
all_members = zip_ref.namelist()
6466
actual_main_folder = all_members[0].split("/")[0]
6567
zip_ref.extractall(extract_to)
@@ -345,7 +347,7 @@ def mirror_check(
345347
Returns:
346348
Dict: 资源信息
347349
"""
348-
if maa_config_data.interface_config.get("mirrorchyan_multiplatform",False):
350+
if maa_config_data.interface_config.get("mirrorchyan_multiplatform", False):
349351
version_file_path = os.path.join(os.getcwd(), "config", "version.txt")
350352
logger.info(f"正在读取版本文件: {version_file_path}")
351353
with open(version_file_path, "r", encoding="utf-8") as f:
@@ -608,6 +610,8 @@ def github_download(self, update_dict: Dict):
608610
logger.info(f"正在读取版本文件: {version_file_path}")
609611
with open(version_file_path, "r") as version_file:
610612
version = version_file.read().split()[2][1:]
613+
os_type = version_file.read().split()[0]
614+
arch = version_file.read().split()[1]
611615
logger.debug(f"当前版本: {version}")
612616
except FileNotFoundError:
613617
logger.exception("版本文件未找到")
@@ -625,7 +629,22 @@ def github_download(self, update_dict: Dict):
625629
self.stop_flag = False
626630
try:
627631
# 下载过程
628-
download_url = update_dict["zipball_url"]
632+
if maa_config_data.interface_config.get("agent", False):
633+
for release in update_dict["assets"]:
634+
if os_type in release["name"] and arch in release["name"]:
635+
download_url = release["browser_download_url"]
636+
break
637+
else:
638+
logger.warning("未找到匹配的资源")
639+
signalBus.update_download_finished.emit(
640+
{
641+
"status": "failed",
642+
"msg": self.tr("No matching resource found"),
643+
}
644+
)
645+
646+
else:
647+
download_url = update_dict["zipball_url"]
629648
logger.info(f"开始下载更新包: {download_url}")
630649

631650
hotfix_directory = os.path.join(os.getcwd(), "hotfix")
@@ -658,11 +677,29 @@ def github_download(self, update_dict: Dict):
658677
{"status": "failed", "msg": self.tr("Extraction failed")}
659678
)
660679
return
680+
if maa_config_data.interface_config.get("agent", False):
681+
main_folder = os.path.join(os.getcwd(), "hotfix", main_folder)
682+
files_to_keep = [
683+
"python",
684+
"resource",
685+
"interface.json",
686+
"custom",
687+
"agent",
688+
]
689+
690+
for item in os.listdir(main_folder):
691+
if item not in files_to_keep:
692+
item_path = os.path.join(main_folder, item)
693+
if os.path.isdir(item_path):
694+
shutil.rmtree(item_path)
695+
else:
696+
os.remove(item_path)
661697

662-
folder_to_extract = os.path.join(
663-
os.getcwd(), "hotfix", main_folder, "assets"
664-
)
665-
logger.debug(f"资源解压路径: {folder_to_extract}")
698+
else:
699+
folder_to_extract = os.path.join(
700+
os.getcwd(), "hotfix", main_folder, "assets"
701+
)
702+
logger.debug(f"资源解压路径: {folder_to_extract}")
666703

667704
# 版本兼容性检查
668705
interface_file = os.path.join(folder_to_extract, "interface.json")
@@ -689,15 +726,24 @@ def github_download(self, update_dict: Dict):
689726
if os.path.exists(target_path):
690727
logger.info("开始清理旧资源")
691728
try:
729+
if os.path.exists(os.path.join(target_path, "python")):
730+
shutil.rmtree(os.path.join(target_path, "python"))
731+
logger.debug("成功删除 python 目录")
732+
733+
if os.path.exists(os.path.join(target_path, "custom")):
734+
shutil.rmtree(os.path.join(target_path, "custom"))
735+
logger.debug("成功删除 custom 目录")
736+
737+
if os.path.exists(os.path.join(target_path, "agent")):
738+
shutil.rmtree(os.path.join(target_path, "agent"))
739+
logger.debug("成功删除 agent 目录")
740+
692741
shutil.rmtree(os.path.join(target_path, "resource"))
693742
logger.debug("成功删除 resource 目录")
694743

695744
os.remove(os.path.join(target_path, "interface.json"))
696745
logger.debug("成功删除 interface.json")
697746

698-
if os.path.exists(os.path.join(target_path, "custom")):
699-
shutil.rmtree(os.path.join(target_path, "custom"))
700-
logger.debug("成功删除 custom 目录")
701747
except Exception as e:
702748
logger.error(f"清理旧资源失败: {str(e)}")
703749
signalBus.update_download_finished.emit(
@@ -743,7 +789,7 @@ def github_download(self, update_dict: Dict):
743789
except requests.exceptions.HTTPError as e:
744790
logger.exception(f"HTTP错误: {str(e)}")
745791
signalBus.update_download_finished.emit(
746-
{"status": "failed", "msg": self.tr("HTTP error")+str(e)}
792+
{"status": "failed", "msg": self.tr("HTTP error") + str(e)}
747793
)
748794
except KeyError as e:
749795
logger.exception(f"关键数据缺失: {str(e)}")

0 commit comments

Comments
 (0)