Skip to content

Commit 1294dfa

Browse files
authored
Update download_MAA_fw.py
稍微优化一下
1 parent 4de7a00 commit 1294dfa

1 file changed

Lines changed: 81 additions & 61 deletions

File tree

deps/tools/quick_update/download_MAA_fw.py

Lines changed: 81 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111

1212

1313
def detect_os():
14+
"""Detect the current operating system.
15+
16+
Returns:
17+
str: "android" | "linux" | "macos" | "win" | "unknown"
18+
"""
1419
sys_platform = sys.platform
1520
if sys_platform.startswith("linux"):
1621
# 判断是不是安卓(Termux)
@@ -27,6 +32,15 @@ def detect_os():
2732

2833

2934
def detect_arch():
35+
"""
36+
Detect the architecture of the current machine.
37+
38+
Returns:
39+
str: "x86_64" if the machine is 64-bit Intel/AMD architecture,
40+
"aarch64" if the machine is 64-bit ARM architecture,
41+
or "unknown" if the architecture cannot be determined.
42+
"""
43+
3044
machine = platform.machine().lower()
3145
if machine in ("x86_64", "amd64"):
3246
return "x86_64"
@@ -54,7 +68,7 @@ def get_local_version_from_dll(dll_path: str) -> Optional[str]:
5468
lib = ctypes.WinDLL(dll_path)
5569
except OSError as e:
5670
print(f"加载 DLL 失败: {e}", file=sys.stderr)
57-
return None
71+
return "NONE"
5872

5973
# 记录句柄用于后续卸载
6074
dll_handle = lib._handle
@@ -63,7 +77,7 @@ def get_local_version_from_dll(dll_path: str) -> Optional[str]:
6377
# 2. 检查是否存在 MaaVersion 函数
6478
if not hasattr(lib, 'MaaVersion'):
6579
print(f"DLL 缺少 MaaVersion 函数: {dll_path}", file=sys.stderr)
66-
return None
80+
return "NONE"
6781

6882
# 3. 设置函数签名
6983
lib.MaaVersion.restype = ctypes.c_char_p
@@ -75,13 +89,13 @@ def get_local_version_from_dll(dll_path: str) -> Optional[str]:
7589
# 确保返回的是 bytes
7690
if not isinstance(version_bytes, bytes):
7791
print("MaaVersion 返回无效类型", file=sys.stderr)
78-
return None
92+
return "NONE"
7993

8094
return version_bytes.decode('utf-8')
8195

8296
except (AttributeError, ctypes.ArgumentError) as e:
8397
print(f"调用 DLL 函数出错: {e}", file=sys.stderr)
84-
return None
98+
return "NONE"
8599

86100
finally:
87101
# 5. 无论如何都尝试卸载 DLL
@@ -172,61 +186,64 @@ def custum_ver_select(resource_list):
172186

173187
def get_github_download_options():
174188
"""获取Github的下载选项(前5个版本)"""
189+
url = "https://api.github.com/repos/MaaXYZ/MaaFramework/releases"
175190
try:
176-
response = requests.get(
177-
"https://api.github.com/repos/MaaXYZ/MaaFramework/releases",
178-
timeout=10
179-
)
191+
response = requests.get(url, timeout=10)
180192
response.raise_for_status()
181-
182-
releases = response.json()
183-
if not releases:
184-
print("未找到任何发布版本")
185-
return None
186-
187-
# 只取前5个版本
188-
recent_releases = releases[:5]
189-
190-
resource_list = []
191-
print("\n可用的版本:")
192-
for idx, release in enumerate(recent_releases):
193-
version = release['tag_name']
194-
print(f"{idx}. {version}")
195-
# 收集该版本的资源
196-
assets = release.get("assets", [])
197-
asset_list = []
198-
for asset_idx, asset in enumerate(assets):
199-
size_mb = asset['size'] / (1024 * 1024)
200-
asset_list.append({
201-
"index": asset_idx,
202-
"name": asset['name'],
203-
"url": asset['browser_download_url'],
204-
"size": asset['size'],
205-
"size_mb": size_mb
206-
})
207-
resource_list.append({
208-
"version": version,
209-
"assets": asset_list
210-
})
211-
212-
return resource_list
213-
193+
releases = response.json()[:5]
214194
except requests.exceptions.RequestException as e:
215195
print(f"获取发布信息失败: {str(e)}")
216196
return None
217-
except (KeyError, IndexError, TypeError) as e:
218-
print(f"解析数据出错: {str(e)}")
197+
198+
if not releases:
199+
print("未找到任何发布版本")
219200
return None
220201

202+
resource_list = []
203+
print("\n可用的版本:")
204+
for idx, release in enumerate(releases):
205+
version = release['tag_name']
206+
print(f"{idx}. {version}")
207+
assets = release.get("assets", [])
208+
asset_list = [
209+
{
210+
"index": i,
211+
"name": asset['name'],
212+
"url": asset['browser_download_url'],
213+
"size": asset['size'],
214+
"size_mb": asset['size'] / (1024 * 1024)
215+
} for i, asset in enumerate(assets)
216+
]
217+
resource_list.append({"version": version, "assets": asset_list})
218+
219+
return resource_list
220+
221221

222222
def select_download_resource(resource_list, auto=False):
223-
"""选择下载资源(先选版本,再选资源)"""
223+
"""
224+
选择下载资源
225+
226+
1. 如果 `auto` 是 True,我们将自动选择最新的版本
227+
2. 如果 `auto` 是 False,我们将手动选择版本
228+
1. 显示所有可用的版本
229+
2. 用户选择一个版本
230+
3. 显示该版本的所有可用的资源
231+
4. 用户选择一个资源
232+
233+
Args:
234+
resource_list (list): 一个包含 Release 信息的列表
235+
auto (bool, optional): 是否自动选择最新的版本. Defaults to False.
236+
237+
Returns:
238+
str: 下载资源的 URL,如果用户取消,将返回 False
239+
"""
224240
if not resource_list:
225241
print("没有可用的下载资源")
226242
return None
227243

228244
# 自动模式:使用最新版本
229245
if auto:
246+
# resource_list[0] 是最新的版本
230247
latest_release = resource_list[0]
231248
assets = latest_release['assets']
232249
combo_key = get_local_platform()
@@ -241,7 +258,7 @@ def select_download_resource(resource_list, auto=False):
241258
try:
242259
choice = input("\n请输入版本编号 (输入 'q' 退出): ")
243260
if choice.lower() == 'q':
244-
return False
261+
return None
245262

246263
version_idx = int(choice)
247264
if 0 <= version_idx < len(resource_list):
@@ -250,10 +267,9 @@ def select_download_resource(resource_list, auto=False):
250267

251268
# 显示该版本的资源
252269
assets = selected_release['assets']
253-
# print(assets)
254270
if not assets:
255271
print("该版本没有可用的资源")
256-
return False
272+
return None
257273

258274
print("\n可用的资源:")
259275
for idx, asset in enumerate(assets):
@@ -374,33 +390,31 @@ def delete_file(filename):
374390

375391

376392
def main(is_debug=False, is_delete=True):
377-
if is_debug:
378-
print("处于调试模式,将无视版本进行下载")
379-
380393
print("正在获取最新版本信息", end=":\t")
381394
download_options = get_github_download_options()
382395
if download_options is None:
383396
print("网络错误或者手动退出,无法更新")
384397
return None
385398

386-
auto_update = input("\n输入n或者N并且按回车\n关闭自动选择更新包功能\n")
387-
if auto_update in ["N", "n"]:
388-
auto_update = False
389-
else:
390-
auto_update = True
399+
auto_update = auto_update_check()
391400

392401
print("正在获取本地版本信息", end=":\n")
393402
file_ver = get_local_version_from_dll(dll_path)
394-
print(f"本地版本:{file_ver}")
403+
print(f"当前版本:{file_ver}")
395404

396405
url_ver = select_download_resource(download_options, auto_update)
406+
if url_ver is None:
407+
print("无法找到最新版本,无法更新")
408+
return None
409+
print(f"本地版本:{file_ver}")
410+
print(f"网络最新版本:{url_ver}")
397411

398-
print("网络正常,与最新版本信息比较中")
399-
if check_version(file_ver, url_ver) is False or is_debug or is_delete is False:
400-
print(f"\n当前版本{check_version(file_ver, url_ver)}最新版本\n")
401-
url = url_ver
412+
# 反转检查结果,为是否最新版本做准备
413+
checked_version = not check_version(file_ver, url_ver)
414+
if checked_version or is_debug or not is_delete:
415+
print("\n当前版本与选中版本不符,需要更新\n")
402416
print("正在下载文件MaaFramework.zip")
403-
download_file(url)
417+
download_file(url_ver)
404418
print("正在解压文件")
405419
unzip("MaaFramework.zip")
406420
print("解压完成")
@@ -410,9 +424,15 @@ def main(is_debug=False, is_delete=True):
410424
if is_debug:
411425
pass
412426
elif is_delete:
427+
print("正在删除文件MaaFramework.zip")
413428
delete_file("MaaFramework.zip")
414429

415430

431+
def auto_update_check():
432+
auto_update = input("\n输入n或者N并且按回车\n关闭自动选择更新包功能\n") in ["N", "n"]
433+
return not auto_update
434+
435+
416436
print("正在切换工作路径至exe所在路径")
417437
os.chdir(os.path.dirname(os.path.realpath(sys.argv[0])))
418438

0 commit comments

Comments
 (0)