Skip to content

Commit

Permalink
更新器测试版
Browse files Browse the repository at this point in the history
  • Loading branch information
DLmaster361 committed Nov 2, 2024
1 parent c490c3b commit af1d62c
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 44 deletions.
33 changes: 27 additions & 6 deletions AUTO_MAA.py
Original file line number Diff line number Diff line change
Expand Up @@ -2344,10 +2344,20 @@ def check_version(self):
updater_version_current = list(
map(int, version_current["updater_version"].split("."))
)
if not os.path.exists(self.app_path + "/Updater.exe"):
updater_version_current = [0, 0, 0, 0]
# 从远程服务器获取最新版本信息
response = requests.get(
"https://ghp.ci/https://github.com/DLmaster361/AUTO_MAA/blob/Updater/res/version.json"
)
try:
response = requests.get(
"https://ghp.ci/https://github.com/DLmaster361/AUTO_MAA/blob/Updater/res/version.json"
)
except Exception as e:
QMessageBox.critical(
self.ui,
"错误",
f"获取版本信息时出错: {e}",
)
return None
version_remote = response.json()
main_version_remote = list(map(int, version_remote["main_version"].split(".")))
updater_version_remote = list(
Expand All @@ -2360,25 +2370,36 @@ def check_version(self):
choice = QMessageBox.question(
self.ui,
"版本更新",
f"发现新版本:\n 主程序:{main_version_current} --> {main_version_remote}\n 更新器:{updater_version_current} --> {updater_version_remote}\n 更新说明:\n{version_remote['announcement'].replace("\n"," \n")}是否开始更新?",
f"发现新版本:\n 主程序:{self.version_text(main_version_current)} --> {self.version_text(main_version_remote)}\n 更新器:{self.version_text(updater_version_current)} --> {self.version_text(updater_version_remote)}\n 更新说明:\n{version_remote['announcement'].replace("\n"," \n")}\n是否开始更新?",
)
if choice == QMessageBox.No:
return None

if updater_version_remote > updater_version_current:
updater = Updater.AUTO_MAA_Updater(
self.updater = Updater.Updater(
self.app_path,
"AUTO_MAA更新器",
version_remote["updater_download_url"],
None,
)
updater.main.run()
self.updater.ui.show()
if main_version_remote > main_version_current:
with open(self.config_path, "r", encoding="utf-8") as f:
config = json.load(f)
config["Default"]["Self.IfKill"] = "True"
with open(self.config_path, "w", encoding="utf-8") as f:
json.dump(config, f, indent=4)
subprocess.Popen([self.app_path + "/Updater.exe"])
else:
self.push_notification("已经是最新版本!", " ", " ", 10)

def version_text(self, version_numb):
"""将版本号列表转为可读的文本信息"""
if version_numb[3] == 0:
version = f"v{'.'.join(str(_) for _ in version_numb[0:3])}"
elif version_numb[3] == 1:
version = f"v{'.'.join(str(_) for _ in version_numb[0:3])}_beta"
return version

def push_notification(self, title, message, ticker, t):
"""推送系统通知"""
Expand Down
84 changes: 50 additions & 34 deletions Updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,37 +44,24 @@
uiLoader = QUiLoader()


class MaaRunner(QtCore.QThread):
def __init__(self):
super(MaaRunner, self).__init__()
class UpdateProcess(QtCore.QThread):


class Updater(QtCore.QObject):
info = QtCore.Signal(str)
progress = QtCore.Signal(int, int, int)

def __init__(self, app_path, name, download_url, main_uid):
super().__init__()

self.ui = uiLoader.load(self.app_path + "/gui/ui/updater.ui")
self.ui.setWindowTitle("AUTO_MAA_Updater")
self.ui.setWindowIcon(QIcon(self.app_path + "/res/AUTO_MAA.ico"))
super(UpdateProcess, self).__init__()

self.app_path = app_path
self.name = name
self.download_url = download_url
self.main_uid = main_uid
self.download_path = app_path + "/AUTO_MAA_Update.zip" # 临时下载文件的路径
self.version_path = app_path + "/res/version.json"

self.info = self.ui.findChild(QLabel, "label")

self.progress = self.ui.findChild(QProgressBar, "progressBar")
self.progress.setRange(0, 100)

def run(self):
result = self.download()
if result:
self.unzip()

def download(self):
# 下载
try:
response = requests.get(self.download_url, stream=True)
file_size = response.headers.get("Content-Length")
Expand All @@ -87,38 +74,67 @@ def download(self):
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
downloaded_size += len(chunk)
self.info.setText(
self.info.emit(
f"正在下载:{self.name} 已下载: {downloaded_size / 1048576:.2f}/{file_size / 1048576:.2f} MB ({downloaded_size / file_size * 100:.2f}%)"
)
self.progress.setValue(int(downloaded_size / file_size * 100))
return True
self.progress.emit(0, 100, int(downloaded_size / file_size * 100))
except Exception as e:
self.info.setText(f"下载{self.name}时出错: {e}")
return False

def unzip(self):
self.info.emit(f"下载{self.name}时出错: {e}")
return None
# 结束主程序
if self.main_uid:
killprocess = subprocess.Popen(
"taskkill /F /T /PID " + str(self.main_uid),
shell=True,
creationflags=subprocess.CREATE_NO_WINDOW,
)
killprocess.wait()
# 解压
try:
self.info.setText("正在解压更新文件")
self.progress.setRange(0, 0)
self.info.emit("正在解压更新文件")
self.progress.emit(0, 0, 0)
with zipfile.ZipFile(self.download_path, "r") as zip_ref:
zip_ref.extractall(self.app_path)

self.info.setText("正在删除临时文件")
self.progress.setRange(0, 0)
self.info.emit("正在删除临时文件")
self.progress.emit(0, 0, 0)
os.remove(self.download_path)

self.info.setText(f"{self.name}更新成功")
self.progress.setRange(100, 100)
self.info.emit(f"{self.name}更新成功")
self.progress.emit(0, 100, 100)

except Exception as e:
self.info.setText(f"解压更新时出错: {e}")
self.info.emit(f"解压更新时出错: {e}")


class Updater(QtCore.QObject):

def __init__(self, app_path, name, download_url, main_uid):
super().__init__()

self.ui = uiLoader.load(app_path + "/gui/ui/updater.ui")
self.ui.setWindowTitle("AUTO_MAA更新器")
self.ui.setWindowIcon(QIcon(app_path + "/res/AUTO_MAA.ico"))

self.info = self.ui.findChild(QLabel, "label")
self.info.setText("正在初始化")

self.progress = self.ui.findChild(QProgressBar, "progressBar")
self.progress.setRange(0, 0)

self.update_process = UpdateProcess(app_path, name, download_url, main_uid)

self.update_process.info.connect(self.update_info)
self.update_process.progress.connect(self.update_progress)

self.update_process.start()

def update_info(self, text):
self.info.setText(text)

def update_progress(self, begin, end, current):
self.progress.setRange(begin, end)
self.progress.setValue(current)


class AUTO_MAA_Updater(QApplication):
Expand Down Expand Up @@ -158,4 +174,4 @@ def __init__(self, app_path, name, download_url):
app = AUTO_MAA_Updater(
app_path, "AUTO_MAA主程序", version_remote["main_download_url"]
)
app.main.run()
sys.exit(app.exec())
8 changes: 4 additions & 4 deletions res/Updater_info.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ VSVersionInfo(
ffi=FixedFileInfo(
# filevers and prodvers should be always a tuple with four items: (1, 2, 3, 4)
# Set not needed items to zero 0.
filevers=(0, 0, 0, 1),
filevers=(0, 1, 0, 1),
prodvers=(0, 0, 0, 0),
# Contains a bitmask that specifies the valid bits 'flags'r
mask=0x3f,
Expand All @@ -31,13 +31,13 @@ VSVersionInfo(
[StringStruct('Comments', 'https://github.com/DLmaster361/AUTO_MAA/'),
StringStruct('CompanyName', 'AUTO_MAA Team'),
StringStruct('FileDescription', 'AUTO_MAA Component'),
StringStruct('FileVersion', '0.0.0.1'),
StringStruct('FileVersion', '0.1.0.1'),
StringStruct('InternalName', 'AUTO_MAA_Updater'),
StringStruct('LegalCopyright', 'Copyright © 2024 DLmaster361'),
StringStruct('OriginalFilename', 'Updater.py'),
StringStruct('ProductName', 'AUTO_MAA_Updater'),
StringStruct('ProductVersion', 'v0.0.0.1'),
StringStruct('Assembly Version', 'v0.0.0.1')])
StringStruct('ProductVersion', 'v0.1.0.1'),
StringStruct('Assembly Version', 'v0.1.0.1')])
])
]
)

0 comments on commit af1d62c

Please sign in to comment.