This repository has been archived by the owner on Oct 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maaupdater.py
327 lines (270 loc) · 12.9 KB
/
maaupdater.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import os
import sys
import configparser
import urllib.request
import zipfile
import tempfile
from PySide6.QtWidgets import (QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget,
QProgressBar, QLabel, QFileDialog, QComboBox,
QSystemTrayIcon, QMenu, QMessageBox, QSpacerItem, QSizePolicy)
from PySide6.QtCore import QThread, Signal, Qt, QTimer, QSharedMemory
from PySide6.QtGui import QIcon, QAction
CONFIG_FILE = 'update_config.ini'
def load_config():
config = configparser.ConfigParser()
if os.path.exists(CONFIG_FILE):
config.read(CONFIG_FILE)
return config.get('Settings', 'maa_directory', fallback=None)
return None
def save_config(maa_directory):
config = configparser.ConfigParser()
config['Settings'] = {'maa_directory': maa_directory}
with open(CONFIG_FILE, 'w') as configfile:
config.write(configfile)
class UpdateThread(QThread):
update_progress = Signal(str, int, int)
update_status = Signal(str)
update_finished = Signal(bool, str)
def __init__(self, maa_directory):
super().__init__()
self.maa_directory = maa_directory
def run(self):
download_url = "https://github.com/MaaAssistantArknights/MaaResource/archive/refs/heads/main.zip"
try:
self.download_and_extract(download_url, self.maa_directory)
self.update_finished.emit(True, "更新完成!")
except Exception as e:
self.update_finished.emit(False, f"更新失败: {str(e)}")
def download_and_extract(self, url, maa_path):
with tempfile.TemporaryDirectory() as temp_dir:
self.update_status.emit("正在下载...")
temp_file_path = os.path.join(temp_dir, 'download.zip')
def update_progress(count, block_size, total_size):
if total_size > 0:
self.update_progress.emit("download", count * block_size, total_size)
urllib.request.urlretrieve(url, temp_file_path, reporthook=update_progress)
self.update_status.emit("正在解压...")
with zipfile.ZipFile(temp_file_path, 'r') as zip_ref:
zip_ref.extractall(temp_dir)
self.update_status.emit("正在更新文件...")
self.total_size = sum(os.path.getsize(os.path.join(dirpath, filename))
for dirpath, dirnames, filenames in os.walk(os.path.join(temp_dir, 'MaaResource-main'))
for filename in filenames)
self.copied_size = 0
self.update_local_folders(temp_dir, maa_path)
def update_local_folders(self, temp_dir, maa_path):
for folder in ['resource', 'cache']:
repo_folder = os.path.join(temp_dir, 'MaaResource-main', folder)
local_folder = os.path.join(maa_path, folder)
if os.path.exists(repo_folder):
self.copy_folder(repo_folder, local_folder)
def copy_folder(self, src, dst):
if not os.path.exists(dst):
os.makedirs(dst)
for item in os.listdir(src):
s = os.path.join(src, item)
d = os.path.join(dst, item)
if os.path.isdir(s):
self.copy_folder(s, d)
else:
with open(s, 'rb') as f_src, open(d, 'wb') as f_dst:
f_dst.write(f_src.read())
self.copied_size += os.path.getsize(s)
self.update_progress.emit("copy", self.copied_size, self.total_size)
class UpdateApp(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("MAA资源更新器")
self.setGeometry(100, 100, 400, 300) # 增加窗口高度
if getattr(sys, 'frozen', False):
application_path = sys._MEIPASS
else:
application_path = os.path.dirname(os.path.abspath(__file__))
icon_path = os.path.join(application_path, 'maa.png')
self.setWindowIcon(QIcon(icon_path))
self.init_ui()
self.load_settings()
self.setup_auto_check()
self.setup_tray()
if not os.path.exists(CONFIG_FILE):
self.first_time_setup()
QTimer.singleShot(0, self.check_update)
def init_ui(self):
layout = QVBoxLayout()
layout.setSpacing(10) # 设置控件之间的间距
self.download_progress = QProgressBar()
layout.addWidget(self.download_progress)
self.copy_progress = QProgressBar()
layout.addWidget(self.copy_progress)
self.status_label = QLabel("准备更新...")
self.status_label.setAlignment(Qt.AlignCenter)
layout.addWidget(self.status_label)
# 添加一个弹性空间
layout.addItem(QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding))
self.check_update_button = QPushButton("检查更新")
self.check_update_button.clicked.connect(self.check_update)
self.check_update_button.setMinimumHeight(40) # 设置按钮最小高度
layout.addWidget(self.check_update_button)
self.start_button = QPushButton("开始更新")
self.start_button.clicked.connect(self.start_update)
self.start_button.setMinimumHeight(40) # 设置按钮最小高度
layout.addWidget(self.start_button)
self.check_interval_combo = QComboBox()
self.check_interval_combo.addItems(["不自动检查更新", "每小时检测更新", "每天检测更新", "每周检测更新"])
self.check_interval_combo.currentIndexChanged.connect(self.set_check_interval)
self.check_interval_combo.setMinimumHeight(30) # 设置下拉框最小高度
layout.addWidget(self.check_interval_combo)
# 添加另一个弹性空间
layout.addItem(QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding))
central_widget = QWidget()
central_widget.setLayout(layout)
self.setCentralWidget(central_widget)
def first_time_setup(self):
QMessageBox.information(self, "首次启动", "欢迎使用MAA资源更新器!请选择MAA的安装路径。")
maa_directory = QFileDialog.getExistingDirectory(self, "选择MAA安装路径")
if not maa_directory:
QMessageBox.critical(self, "错误", "未选择MAA安装路径,程序将退出。")
sys.exit()
save_config(maa_directory)
def load_settings(self):
config = configparser.ConfigParser()
if os.path.exists(CONFIG_FILE):
config.read(CONFIG_FILE)
check_interval = config.get('Settings', 'check_interval', fallback="不自动检查")
self.check_interval_combo.setCurrentText(check_interval)
else:
self.check_interval_combo.setCurrentText("不自动检查")
def save_settings(self):
config = configparser.ConfigParser()
config['Settings'] = {
'maa_directory': str(load_config() or ''),
'check_interval': str(self.check_interval_combo.currentText())
}
with open(CONFIG_FILE, 'w') as configfile:
config.write(configfile)
def set_check_interval(self, index):
self.save_settings()
self.setup_auto_check()
def setup_auto_check(self):
if hasattr(self, 'auto_check_timer'):
self.auto_check_timer.stop()
interval = self.check_interval_combo.currentText()
if interval == "每小时":
self.auto_check_timer = QTimer(self)
self.auto_check_timer.timeout.connect(self.check_update)
self.auto_check_timer.start(3600000) # 3600000 毫秒 = 1 小时
elif interval == "每天":
self.auto_check_timer = QTimer(self)
self.auto_check_timer.timeout.connect(self.check_update)
self.auto_check_timer.start(86400000) # 86400000 毫秒 = 24 小时
elif interval == "每周":
self.auto_check_timer = QTimer(self)
self.auto_check_timer.timeout.connect(self.check_update)
self.auto_check_timer.start(604800000) # 604800000 毫秒 = 7 天
def check_update(self):
self.status_label.setText("正在检查更新...")
try:
maa_directory = load_config()
if maa_directory is None:
self.status_label.setText("错误: 未设置MAA安装路径")
return
local_version_file = os.path.join(maa_directory, 'resource', 'version.json')
remote_version_url = "https://raw.githubusercontent.com/MaaAssistantArknights/MaaResource/main/resource/version.json"
if not os.path.exists(local_version_file):
self.status_label.setText("本地版本文件不存在,需要更新")
return
local_file_size = os.path.getsize(local_version_file)
with tempfile.NamedTemporaryFile(delete=False, suffix='.json') as temp_file:
urllib.request.urlretrieve(remote_version_url, temp_file.name)
remote_file_size = os.path.getsize(temp_file.name)
if local_file_size != remote_file_size:
self.status_label.setText("检测到新版本,可以更新")
else:
self.status_label.setText("已是最新版本")
except Exception as e:
self.status_label.setText(f"检查更新时出错: {str(e)}")
def start_update(self):
self.start_button.setEnabled(False)
maa_directory = load_config()
if maa_directory is None:
maa_directory = QFileDialog.getExistingDirectory(self, "选择MAA安装路径")
if not maa_directory:
self.status_label.setText("错误: 未选择MAA安装路径,程序退出。")
self.start_button.setEnabled(True)
return
save_config(maa_directory)
self.update_thread = UpdateThread(maa_directory)
self.update_thread.update_progress.connect(self.update_progress)
self.update_thread.update_status.connect(self.update_status)
self.update_thread.update_finished.connect(self.update_finished)
self.update_thread.start()
def update_progress(self, progress_type, current, total):
progress = int((current / total) * 100)
if progress_type == "download":
self.download_progress.setValue(progress)
elif progress_type == "copy":
self.copy_progress.setValue(progress)
def update_status(self, status):
self.status_label.setText(status)
def update_finished(self, success, message):
self.status_label.setText(message)
if success:
self.download_progress.setValue(100)
self.copy_progress.setValue(100)
self.start_button.setText("完成")
self.start_button.clicked.disconnect()
self.start_button.clicked.connect(self.close)
self.start_button.setEnabled(True)
def setup_tray(self):
if getattr(sys, 'frozen', False):
base_path = sys._MEIPASS
else:
base_path = os.path.dirname(os.path.abspath(__file__))
icon_path = os.path.join(base_path, 'maa.png')
self.tray_icon = QSystemTrayIcon(self)
self.tray_icon.setIcon(QIcon(icon_path))
tray_menu = QMenu()
show_action = QAction("显示UI", self)
quit_action = QAction("退出", self)
show_action.triggered.connect(self.show)
quit_action.triggered.connect(QApplication.instance().quit)
tray_menu.addAction(show_action)
tray_menu.addAction(quit_action)
self.tray_icon.setContextMenu(tray_menu)
self.tray_icon.show()
def closeEvent(self, event):
event.ignore()
self.hide()
self.tray_icon.showMessage(
"MAA资源更新器",
"程序已最小化到系统托盘,双击图标可以重新打开主窗口。",
QSystemTrayIcon.Information,
2000
)
class SingleApplication(QApplication):
def __init__(self, id, *args, **kwargs):
super().__init__(*args, **kwargs)
self._id = id
self._activation_window = None
self._memory = QSharedMemory(self)
self._memory.setKey(self._id)
if self._memory.attach():
self._running = True
else:
self._running = False
if not self._memory.create(1):
raise RuntimeError(self._memory.errorString())
def is_running(self):
return self._running
if __name__ == "__main__":
app = SingleApplication('MAAResourceUpdater', sys.argv)
if app.is_running():
QMessageBox.warning(None, "警告", "MAA资源更新器已经在运行中。")
sys.exit(1)
if not QSystemTrayIcon.isSystemTrayAvailable():
QMessageBox.critical(None, "系统托盘", "无法检测到系统托盘。")
sys.exit(1)
QApplication.setQuitOnLastWindowClosed(False)
window = UpdateApp()
window.show()
sys.exit(app.exec())