mirror of
https://github.com/hyb-oyqq/FRAISEMOE-Addons-Installer-NEXT.git
synced 2026-01-15 16:10:43 +00:00
chore: 项目文件结构重构
删除多个不再使用的源文件,包括动画、下载、配置、UI 相关文件及图标,清理代码库以提高可维护性。
This commit is contained in:
14
source/workers/__init__.py
Normal file
14
source/workers/__init__.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from .hash_thread import HashThread
|
||||
from .extraction_thread import ExtractionThread
|
||||
from .config_fetch_thread import ConfigFetchThread
|
||||
from .ip_optimizer import IpOptimizerThread
|
||||
from .download import DownloadThread, ProgressWindow
|
||||
|
||||
__all__ = [
|
||||
'IpOptimizerThread',
|
||||
'HashThread',
|
||||
'ExtractionThread',
|
||||
'ConfigFetchThread',
|
||||
'DownloadThread',
|
||||
'ProgressWindow'
|
||||
]
|
||||
52
source/workers/config_fetch_thread.py
Normal file
52
source/workers/config_fetch_thread.py
Normal file
@@ -0,0 +1,52 @@
|
||||
import json
|
||||
import requests
|
||||
from PySide6.QtCore import QThread, Signal
|
||||
|
||||
class ConfigFetchThread(QThread):
|
||||
finished = Signal(object, str) # data, error_message
|
||||
|
||||
def __init__(self, url, headers, debug_mode=False, parent=None):
|
||||
super().__init__(parent)
|
||||
self.url = url
|
||||
self.headers = headers
|
||||
self.debug_mode = debug_mode
|
||||
|
||||
def run(self):
|
||||
try:
|
||||
if self.debug_mode:
|
||||
print("--- Starting to fetch cloud config ---")
|
||||
print(f"DEBUG: Requesting URL: {self.url}")
|
||||
print(f"DEBUG: Using Headers: {self.headers}")
|
||||
|
||||
response = requests.get(self.url, headers=self.headers, timeout=10)
|
||||
|
||||
if self.debug_mode:
|
||||
print(f"DEBUG: Response Status Code: {response.status_code}")
|
||||
print(f"DEBUG: Response Headers: {response.headers}")
|
||||
print(f"DEBUG: Response Text: {response.text}")
|
||||
|
||||
response.raise_for_status()
|
||||
|
||||
# 首先,总是尝试解析JSON
|
||||
config_data = response.json()
|
||||
|
||||
# 检查是否是要求更新的错误信息
|
||||
if config_data.get("message") == "请使用最新版本的FRAISEMOE Addons Installer NEXT进行下载安装":
|
||||
self.finished.emit(None, "update_required")
|
||||
return
|
||||
|
||||
# 检查是否是有效的配置文件
|
||||
required_keys = [f"vol.{i+1}.data" for i in range(4)] + ["after.data"]
|
||||
missing_keys = [key for key in required_keys if key not in config_data]
|
||||
if missing_keys:
|
||||
self.finished.emit(None, f"missing_keys:{','.join(missing_keys)}")
|
||||
return
|
||||
|
||||
self.finished.emit(config_data, "")
|
||||
except requests.exceptions.RequestException as e:
|
||||
self.finished.emit(None, f"网络请求失败: {e}")
|
||||
except (ValueError, json.JSONDecodeError) as e:
|
||||
self.finished.emit(None, f"JSON解析失败: {e}")
|
||||
finally:
|
||||
if self.debug_mode:
|
||||
print("--- Finished fetching cloud config ---")
|
||||
186
source/workers/download.py
Normal file
186
source/workers/download.py
Normal file
@@ -0,0 +1,186 @@
|
||||
import os
|
||||
import sys
|
||||
import subprocess
|
||||
import re
|
||||
from urllib.parse import urlparse
|
||||
from PySide6 import QtCore, QtWidgets
|
||||
from PySide6.QtCore import (Qt, Signal, QThread, QTimer)
|
||||
from PySide6.QtWidgets import (QLabel, QProgressBar, QVBoxLayout, QDialog)
|
||||
from utils import resource_path
|
||||
from data.config import APP_NAME, UA
|
||||
|
||||
# 下载线程类
|
||||
class DownloadThread(QThread):
|
||||
progress = Signal(dict)
|
||||
finished = Signal(bool, str)
|
||||
|
||||
def __init__(self, url, _7z_path, game_version, parent=None):
|
||||
super().__init__(parent)
|
||||
self.url = url
|
||||
self._7z_path = _7z_path
|
||||
self.game_version = game_version
|
||||
self.process = None
|
||||
self._is_running = True
|
||||
|
||||
def stop(self):
|
||||
if self.process and self.process.poll() is None:
|
||||
self._is_running = False
|
||||
try:
|
||||
# 使用 taskkill 强制终止进程及其子进程,并隐藏窗口
|
||||
subprocess.run(['taskkill', '/F', '/T', '/PID', str(self.process.pid)], check=True, creationflags=subprocess.CREATE_NO_WINDOW)
|
||||
except (subprocess.CalledProcessError, FileNotFoundError) as e:
|
||||
print(f"停止下载进程时出错: {e}")
|
||||
|
||||
|
||||
def run(self):
|
||||
try:
|
||||
if not self._is_running:
|
||||
self.finished.emit(False, "下载已手动停止。")
|
||||
return
|
||||
|
||||
aria2c_path = resource_path("aria2c.exe")
|
||||
download_dir = os.path.dirname(self._7z_path)
|
||||
file_name = os.path.basename(self._7z_path)
|
||||
|
||||
parsed_url = urlparse(self.url)
|
||||
referer = f"{parsed_url.scheme}://{parsed_url.netloc}/"
|
||||
|
||||
command = [
|
||||
aria2c_path,
|
||||
]
|
||||
|
||||
command.extend([
|
||||
'--dir', download_dir,
|
||||
'--out', file_name,
|
||||
'--user-agent', UA,
|
||||
'--referer', referer,
|
||||
'--header', f'Origin: {referer.rstrip("/")}',
|
||||
'--header', 'Accept: */*',
|
||||
'--header', 'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8',
|
||||
'--header', 'Accept-Encoding: gzip, deflate, br',
|
||||
'--header', 'Cache-Control: no-cache',
|
||||
'--header', 'Pragma: no-cache',
|
||||
'--header', 'DNT: 1',
|
||||
'--header', 'Sec-Fetch-Dest: empty',
|
||||
'--header', 'Sec-Fetch-Mode: cors',
|
||||
'--header', 'Sec-Fetch-Site: same-origin',
|
||||
'--http-accept-gzip=true',
|
||||
'--console-log-level=info',
|
||||
'--summary-interval=1',
|
||||
'--log-level=info',
|
||||
'--max-tries=3',
|
||||
'--retry-wait=2',
|
||||
'--connect-timeout=60',
|
||||
'--timeout=60',
|
||||
'--auto-file-renaming=false',
|
||||
'--allow-overwrite=true',
|
||||
'--split=16',
|
||||
'--max-connection-per-server=16'
|
||||
])
|
||||
|
||||
# 证书验证现在总是需要,因为我们依赖hosts文件
|
||||
command.append('--check-certificate=false')
|
||||
|
||||
command.append(self.url)
|
||||
|
||||
# 打印将要执行的命令,用于调试
|
||||
print(f"即将执行的 Aria2c 命令: {' '.join(command)}")
|
||||
|
||||
creation_flags = subprocess.CREATE_NO_WINDOW if sys.platform == 'win32' else 0
|
||||
self.process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, encoding='utf-8', errors='replace', creationflags=creation_flags)
|
||||
|
||||
# 正则表达式用于解析aria2c的输出
|
||||
# 例如: #1 GID[...]( 5%) CN:1 DL:10.5MiB/s ETA:1m30s
|
||||
progress_pattern = re.compile(r'\((\d{1,3})%\).*?CN:(\d+).*?DL:\s*([^\s]+).*?ETA:\s*([^\s]+)')
|
||||
|
||||
full_output = []
|
||||
while self._is_running and self.process.poll() is None:
|
||||
if self.process.stdout:
|
||||
line = self.process.stdout.readline()
|
||||
if not line:
|
||||
break
|
||||
else:
|
||||
break
|
||||
|
||||
full_output.append(line)
|
||||
print(line.strip()) # 在控制台输出实时日志
|
||||
|
||||
match = progress_pattern.search(line)
|
||||
if match:
|
||||
percent = int(match.group(1))
|
||||
threads = match.group(2)
|
||||
speed = match.group(3)
|
||||
eta = match.group(4)
|
||||
self.progress.emit({
|
||||
"game": self.game_version,
|
||||
"percent": percent,
|
||||
"threads": threads,
|
||||
"speed": speed,
|
||||
"eta": eta
|
||||
})
|
||||
|
||||
return_code = self.process.wait()
|
||||
|
||||
if not self._is_running: # 如果是手动停止的
|
||||
self.finished.emit(False, "下载已手动停止。")
|
||||
return
|
||||
|
||||
if return_code == 0:
|
||||
self.progress.emit({
|
||||
"game": self.game_version,
|
||||
"percent": 100,
|
||||
"threads": "N/A",
|
||||
"speed": "N/A",
|
||||
"eta": "完成"
|
||||
})
|
||||
self.finished.emit(True, "")
|
||||
else:
|
||||
error_message = f"\nAria2c下载失败,退出码: {return_code}\n\n--- Aria2c 输出 ---\n{''.join(full_output)}\n---------------------\n"
|
||||
self.finished.emit(False, error_message)
|
||||
|
||||
except Exception as e:
|
||||
if self._is_running:
|
||||
self.finished.emit(False, f"\n下载时发生未知错误\n\n【错误信息】: {e}\n")
|
||||
|
||||
# 下载进度窗口类
|
||||
class ProgressWindow(QDialog):
|
||||
def __init__(self, parent=None):
|
||||
super(ProgressWindow, self).__init__(parent)
|
||||
self.setWindowTitle(f"下载进度 - {APP_NAME}")
|
||||
self.resize(450, 180)
|
||||
self.setWindowFlags(self.windowFlags() & ~Qt.WindowType.WindowCloseButtonHint)
|
||||
self.setWindowFlags(self.windowFlags() & ~Qt.WindowType.WindowSystemMenuHint)
|
||||
|
||||
layout = QVBoxLayout()
|
||||
self.game_label = QLabel("正在启动下载,请稍后...")
|
||||
self.progress_bar = QProgressBar()
|
||||
self.progress_bar.setValue(0)
|
||||
self.stats_label = QLabel("速度: - | 线程: - | 剩余时间: -")
|
||||
self.stop_button = QtWidgets.QPushButton("停止下载")
|
||||
|
||||
layout.addWidget(self.game_label)
|
||||
layout.addWidget(self.progress_bar)
|
||||
layout.addWidget(self.stats_label)
|
||||
layout.addWidget(self.stop_button)
|
||||
self.setLayout(layout)
|
||||
|
||||
def update_progress(self, data):
|
||||
game_version = data.get("game", "未知游戏")
|
||||
percent = data.get("percent", 0)
|
||||
speed = data.get("speed", "-")
|
||||
threads = data.get("threads", "-")
|
||||
eta = data.get("eta", "-")
|
||||
|
||||
self.game_label.setText(f"正在下载: {game_version}")
|
||||
self.progress_bar.setValue(int(percent))
|
||||
self.stats_label.setText(f"速度: {speed} | 线程: {threads} | 剩余时间: {eta}")
|
||||
|
||||
if percent == 100:
|
||||
self.stop_button.setEnabled(False)
|
||||
self.stop_button.setText("下载完成")
|
||||
QTimer.singleShot(1500, self.accept)
|
||||
|
||||
def closeEvent(self, event):
|
||||
# 覆盖默认的关闭事件,防止用户通过其他方式关闭窗口
|
||||
# 如果需要,可以在这里添加逻辑,例如询问用户是否要停止下载
|
||||
event.ignore()
|
||||
31
source/workers/extraction_thread.py
Normal file
31
source/workers/extraction_thread.py
Normal file
@@ -0,0 +1,31 @@
|
||||
import os
|
||||
import shutil
|
||||
import py7zr
|
||||
from PySide6.QtCore import QThread, Signal
|
||||
from data.config import PLUGIN, GAME_INFO
|
||||
|
||||
class ExtractionThread(QThread):
|
||||
finished = Signal(bool, str, str) # success, error_message, game_version
|
||||
|
||||
def __init__(self, _7z_path, game_folder, plugin_path, game_version, parent=None):
|
||||
super().__init__(parent)
|
||||
self._7z_path = _7z_path
|
||||
self.game_folder = game_folder
|
||||
self.plugin_path = plugin_path
|
||||
self.game_version = game_version
|
||||
|
||||
def run(self):
|
||||
try:
|
||||
with py7zr.SevenZipFile(self._7z_path, mode="r") as archive:
|
||||
archive.extractall(path=PLUGIN)
|
||||
|
||||
os.makedirs(self.game_folder, exist_ok=True)
|
||||
shutil.copy(self.plugin_path, self.game_folder)
|
||||
|
||||
if self.game_version == "NEKOPARA After":
|
||||
sig_path = os.path.join(PLUGIN, GAME_INFO[self.game_version]["sig_path"])
|
||||
shutil.copy(sig_path, self.game_folder)
|
||||
|
||||
self.finished.emit(True, "", self.game_version)
|
||||
except (py7zr.Bad7zFile, FileNotFoundError, Exception) as e:
|
||||
self.finished.emit(False, f"\n文件操作失败,请重试\n\n【错误信息】:{e}\n", self.game_version)
|
||||
28
source/workers/hash_thread.py
Normal file
28
source/workers/hash_thread.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from PySide6.QtCore import QThread, Signal
|
||||
from utils import HashManager
|
||||
from data.config import BLOCK_SIZE
|
||||
|
||||
class HashThread(QThread):
|
||||
pre_finished = Signal(dict)
|
||||
after_finished = Signal(dict)
|
||||
|
||||
def __init__(self, mode, install_paths, plugin_hash, installed_status, parent=None):
|
||||
super().__init__(parent)
|
||||
self.mode = mode
|
||||
self.install_paths = install_paths
|
||||
self.plugin_hash = plugin_hash
|
||||
self.installed_status = installed_status
|
||||
# 每个线程都应该有自己的HashManager实例
|
||||
self.hash_manager = HashManager(BLOCK_SIZE)
|
||||
|
||||
def run(self):
|
||||
if self.mode == "pre":
|
||||
updated_status = self.hash_manager.cfg_pre_hash_compare(
|
||||
self.install_paths, self.plugin_hash, self.installed_status
|
||||
)
|
||||
self.pre_finished.emit(updated_status)
|
||||
elif self.mode == "after":
|
||||
result = self.hash_manager.cfg_after_hash_compare(
|
||||
self.install_paths, self.plugin_hash, self.installed_status
|
||||
)
|
||||
self.after_finished.emit(result)
|
||||
194
source/workers/ip_optimizer.py
Normal file
194
source/workers/ip_optimizer.py
Normal file
@@ -0,0 +1,194 @@
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
from urllib.parse import urlparse
|
||||
|
||||
from PySide6.QtCore import QThread, Signal
|
||||
from utils import resource_path
|
||||
|
||||
class IpOptimizer:
|
||||
def __init__(self):
|
||||
self.process = None
|
||||
|
||||
def get_optimal_ip(self, url: str) -> str | None:
|
||||
"""
|
||||
使用 CloudflareSpeedTest 工具获取给定 URL 的最优 Cloudflare IP。
|
||||
|
||||
Args:
|
||||
url: 需要进行优选的下载链接。
|
||||
|
||||
Returns:
|
||||
最优的 IP 地址字符串,如果找不到则返回 None。
|
||||
"""
|
||||
try:
|
||||
cst_path = resource_path("cfst.exe")
|
||||
if not os.path.exists(cst_path):
|
||||
print(f"错误: cfst.exe 未在资源路径中找到。")
|
||||
return None
|
||||
|
||||
ip_txt_path = resource_path("ip.txt")
|
||||
|
||||
# 正确的参数设置,根据cfst帮助文档
|
||||
command = [
|
||||
cst_path,
|
||||
"-n", "500", # 延迟测速线程数 (默认200)
|
||||
"-p", "1", # 显示结果数量 (默认10个)
|
||||
"-url", url, # 指定测速地址
|
||||
"-f", ip_txt_path, # IP文件
|
||||
"-dd", # 禁用下载测速,按延迟排序
|
||||
]
|
||||
|
||||
creation_flags = subprocess.CREATE_NO_WINDOW if sys.platform == 'win32' else 0
|
||||
|
||||
print("--- CloudflareSpeedTest 开始执行 ---")
|
||||
|
||||
self.process = subprocess.Popen(
|
||||
command,
|
||||
stdin=subprocess.PIPE,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT,
|
||||
text=True,
|
||||
encoding='utf-8',
|
||||
errors='replace',
|
||||
creationflags=creation_flags,
|
||||
bufsize=0
|
||||
)
|
||||
|
||||
# 更新正则表达式以匹配cfst输出中的IP格式
|
||||
# 匹配格式: IP地址在行首,后面跟着一些数字和文本
|
||||
ip_pattern = re.compile(r'^(\d+\.\d+\.\d+\.\d+)\s+.*')
|
||||
|
||||
# 标记是否已经找到结果表头和完成标记
|
||||
found_header = False
|
||||
found_completion = False
|
||||
|
||||
stdout = self.process.stdout
|
||||
if not stdout:
|
||||
print("错误: 无法获取子进程的输出流。")
|
||||
return None
|
||||
|
||||
optimal_ip = None
|
||||
timeout_counter = 0
|
||||
max_timeout = 300 # 增加超时时间到5分钟
|
||||
|
||||
while True:
|
||||
if self.process.poll() is not None:
|
||||
break
|
||||
try:
|
||||
ready = True
|
||||
try:
|
||||
line = stdout.readline()
|
||||
except:
|
||||
ready = False
|
||||
|
||||
if not ready or not line:
|
||||
timeout_counter += 1
|
||||
if timeout_counter > max_timeout:
|
||||
print("超时: CloudflareSpeedTest 响应超时")
|
||||
break
|
||||
time.sleep(1)
|
||||
continue
|
||||
|
||||
timeout_counter = 0
|
||||
|
||||
cleaned_line = line.strip()
|
||||
if cleaned_line:
|
||||
print(cleaned_line)
|
||||
|
||||
# 检测结果表头
|
||||
if "IP 地址" in cleaned_line and "平均延迟" in cleaned_line:
|
||||
print("检测到IP结果表头,准备获取IP地址...")
|
||||
found_header = True
|
||||
continue
|
||||
|
||||
# 检测完成标记
|
||||
if "完整测速结果已写入" in cleaned_line or "按下 回车键 或 Ctrl+C 退出" in cleaned_line:
|
||||
print("检测到测速完成信息")
|
||||
found_completion = True
|
||||
|
||||
# 如果已经找到了IP,可以退出了
|
||||
if optimal_ip:
|
||||
break
|
||||
|
||||
# 已找到表头后,尝试匹配IP地址行
|
||||
if found_header:
|
||||
match = ip_pattern.search(cleaned_line)
|
||||
if match and not optimal_ip: # 只保存第一个匹配的IP(最优IP)
|
||||
optimal_ip = match.group(1)
|
||||
print(f"找到最优 IP: {optimal_ip}")
|
||||
|
||||
# 如果已经看到完成标记,可以退出了
|
||||
if found_completion:
|
||||
break
|
||||
|
||||
except Exception as e:
|
||||
print(f"读取输出时发生错误: {e}")
|
||||
break
|
||||
|
||||
# 确保完全读取输出后再发送退出信号
|
||||
if self.process and self.process.poll() is None:
|
||||
try:
|
||||
if self.process.stdin and not self.process.stdin.closed:
|
||||
print("发送退出信号...")
|
||||
self.process.stdin.write('\n')
|
||||
self.process.stdin.flush()
|
||||
except:
|
||||
pass
|
||||
|
||||
self.stop()
|
||||
|
||||
print("--- CloudflareSpeedTest 执行结束 ---")
|
||||
return optimal_ip
|
||||
|
||||
except Exception as e:
|
||||
print(f"执行 CloudflareSpeedTest 时发生错误: {e}")
|
||||
return None
|
||||
|
||||
def stop(self):
|
||||
if self.process and self.process.poll() is None:
|
||||
print("正在终止 CloudflareSpeedTest 进程...")
|
||||
try:
|
||||
if self.process.stdin and not self.process.stdin.closed:
|
||||
self.process.stdin.write('\n')
|
||||
self.process.stdin.flush()
|
||||
self.process.stdin.close()
|
||||
except:
|
||||
pass
|
||||
|
||||
try:
|
||||
self.process.terminate()
|
||||
self.process.wait(timeout=5)
|
||||
except subprocess.TimeoutExpired:
|
||||
self.process.kill()
|
||||
self.process.wait()
|
||||
print("CloudflareSpeedTest 进程已终止。")
|
||||
|
||||
|
||||
class IpOptimizerThread(QThread):
|
||||
"""用于在后台线程中运行IP优化的类"""
|
||||
finished = Signal(str)
|
||||
|
||||
def __init__(self, url, parent=None):
|
||||
super().__init__(parent)
|
||||
self.url = url
|
||||
self.optimizer = IpOptimizer()
|
||||
|
||||
def run(self):
|
||||
optimal_ip = self.optimizer.get_optimal_ip(self.url)
|
||||
self.finished.emit(optimal_ip if optimal_ip else "")
|
||||
|
||||
def stop(self):
|
||||
self.optimizer.stop()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# 用于直接测试此模块
|
||||
test_url = "https://speed.cloudflare.com/__down?during=download&bytes=104857600"
|
||||
optimizer = IpOptimizer()
|
||||
ip = optimizer.get_optimal_ip(test_url)
|
||||
if ip:
|
||||
print(f"为 {test_url} 找到的最优 IP 是: {ip}")
|
||||
else:
|
||||
print(f"未能为 {test_url} 找到最优 IP。")
|
||||
Reference in New Issue
Block a user