-
Notifications
You must be signed in to change notification settings - Fork 44
/
main.py
364 lines (288 loc) · 10.9 KB
/
main.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import logging
import os
import os.path
import sys
import traceback
import PyQt5
from PyQt5 import QtGui, QtCore
from PyQt5.QtWidgets import QMainWindow, QApplication
from autologging import TRACE
from urllib.parse import urlparse
from osr2mp4.Utils.getmods import mod_string_to_enums
from osr2mp4.osrparse.replay import Replay
from HomeComponents.AutoCheckBox import AutoCheckBox
from HomeComponents.Buttons.FolderButton import FolderButton
from HomeComponents.Buttons.MapsetButton import MapsetButton
from HomeComponents.Buttons.Options import Options
from HomeComponents.Buttons.OsrButton import OsrButton
from HomeComponents.Buttons.OsrGrayButton import OsrGrayButton
from HomeComponents.Buttons.OutputButton import OutputButton
from HomeComponents.Buttons.StartButton import StartButton
from HomeComponents.Buttons.UpdateButton import UpdateButton
from HomeComponents.Buttons.osuButton import osuButton
from HomeComponents.Buttons.CancelButton import CancelButton
from HomeComponents.Buttons.osuMapButton import osuMapButton
from HomeComponents.LanguageDropDown import LanguageDropDown
from HomeComponents.Logo import Logo
from HomeComponents.PathImage import OsrPath, MapSetPath
from HomeComponents.PopupWindow import PopupWindow, CustomTextWindow
from HomeComponents.ProgressBar import ProgressBar
from HomeComponents.SkinDropDown import SkinDropDown
from Info import Info
from BaseComponents.Buttons import ButtonBrowse, PopupButton
from SettingComponents.Layouts.SettingsPage import SettingsPage
from abspath import abspath, configpath, Log
from config_data import current_config, current_settings
from helper.helper import kill, cleanupkill, get_latest_replay, get_right_map
from helper.osudatahelper import parse_osr, parse_map
from helper.datahelper import save
class Window(QMainWindow):
def __init__(self, App, execpath):
super().__init__()
logging.basicConfig(level=TRACE, filename=Log.apppath, filemode="w",
format="%(asctime)s:%(levelname)s:%(name)s:%(funcName)s:%(message)s")
apikey = current_settings["api key"]
current_settings["api key"] = None # avoid logging api key
logging.info("Current settings is updated to: {}".format(current_settings))
current_settings["api key"] = apikey
logging.info("Current config is updated to: {}".format(current_config))
self.setFocus()
App.applicationStateChanged.connect(self.applicationStateChanged)
self.setWindowIcon(QtGui.QIcon(os.path.join(abspath, "res/OsrLogo.png")))
self.setWindowTitle("osr2mp4")
self.setStyleSheet("background-color: rgb(30, 30, 33);")
self.setAcceptDrops(True)
window_width, window_height = 832, 469
self.execpath = execpath
self.minimum_resolution = [640, 360]
self.previous_resolution = [0, 0]
self.default_width, self.default_height = window_width, window_height
self.popup_bool = True
self.clicked_inside = False
self.prevreplay = ""
self.osrbutton = OsrButton(self)
self.mapsetbutton = MapsetButton(self)
self.startbutton = StartButton(self)
self.logo = Logo(self)
self.osrpath = OsrPath(self)
self.mapsetpath = MapSetPath(self)
self.skin_dropdown = SkinDropDown(self)
self.options = Options(self)
self.updatebutton = UpdateButton(self)
self.folderbutton = FolderButton(self)
self.osrgraybutton = OsrGrayButton(self)
self.osumapbutton = osuMapButton(self)
self.autocheckbox = AutoCheckBox(self)
self.cancelbutton = CancelButton(self)
logging.info("Loaded Buttons")
self.blurrable_widgets = [self.osrbutton, self.mapsetbutton, self.startbutton, self.logo, self.osrpath,
self.mapsetpath, self.options, self.skin_dropdown, self.cancelbutton, self.folderbutton, self.autocheckbox]
self.langs_dropdown = LanguageDropDown(self)
self.popup_window = PopupWindow(self)
self.output_window = OutputButton(self)
self.osu_window = osuButton(self)
self.customwindow = CustomTextWindow(self)
self.customwindow.hide()
logging.info("Loaded Popupwindow output button and osu button")
self.settingspage = SettingsPage(self)
logging.info("Loaded settings page")
self.popup_widgets = [self.popup_window, self.output_window, self.osu_window]
self.progressbar = ProgressBar(self)
self.progressbar.hide()
current_config[".osr path"] = "brrrrr" # because if .osr path is auto, it won't check for latest play on startup
self.check_osu_path()
# self.check_replay_map()
self.show()
# bind taskbar progress, windows only
if getattr(self.progressbar, 'taskbar_btn', None): # could also import the using_windows variable and use that
self.progressbar.taskbar_btn.setWindow(self.windowHandle()) # maybe do that instead?
self.resize(window_width, window_height)
def toggle_auto(self, enable_auto):
if enable_auto:
self.prevreplay = "auto"
self.setreplay("auto")
self.setmap("")
Info.replay = Replay()
Info.replay.mod_combination = mod_string_to_enums(current_settings["Custom mods"])
Info.map = None
Info.maphash = None
self.osrgraybutton.show()
self.osrbutton.hide()
self.mapsetbutton.hide()
self.osumapbutton.show()
else:
self.setreplay("")
self.setmap("")
current_settings["Custom mods"] = ""
current_config[".osr path"] = ""
current_config["Beatmap path"] = ""
Info.replay = None
Info.real_mod = None
Info.map = None
Info.maphash = None
self.check_replay_map()
self.osrgraybutton.hide()
self.osrbutton.show()
self.mapsetbutton.show()
self.osumapbutton.hide()
def applicationStateChanged(self, state):
if ButtonBrowse.browsing or PopupButton.browsing:
ButtonBrowse.browsing = False
PopupButton.browsing = False
return
if state == 4:
self.check_replay_map()
def resizeEvent(self, event):
height = self.width() * 9 / 16
self.resize(self.width(), height)
if self.width() < self.minimum_resolution[0] and self.height() < self.minimum_resolution[1]:
self.resize(self.previous_resolution[0], self.previous_resolution[1])
self.osrbutton.changesize()
self.mapsetbutton.changesize()
self.startbutton.changesize()
self.logo.changesize()
self.osrpath.changesize()
self.mapsetpath.changesize()
self.output_window.changesize()
self.osu_window.changesize()
self.popup_window.changesize()
self.skin_dropdown.changesize()
self.settingspage.changesize()
self.options.changesize()
self.progressbar.changesize()
self.customwindow.changesize()
self.updatebutton.changesize()
self.cancelbutton.changesize()
self.folderbutton.changesize()
self.autocheckbox.changesize()
self.osrgraybutton.changesize()
self.osumapbutton.changesize()
self.langs_dropdown.changesize()
if self.popup_bool:
self.blur_function(True)
else:
self.blur_function(False)
self.previous_resolution[0] = self.width()
self.previous_resolution[1] = self.height()
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Escape:
self.hidesettings()
def mousePressEvent(self, QMouseEvent):
self.hidesettings()
def hidesettings(self):
if self.settingspage.isVisible():
self.settingspage.hide()
self.settingspage.settingsarea.scrollArea.hide()
save()
if self.customwindow.isVisible():
self.customwindow.hide()
def blur_function(self, blur):
if blur:
for x in self.blurrable_widgets:
x.blur_me(True)
x.clickable = False
else:
for x in self.blurrable_widgets:
x.blur_me(False)
x.clickable = True
def delete_popup(self):
for x in self.popup_widgets:
x.setParent(None)
def check_osu_path(self):
if os.path.isfile(configpath):
self.skin_dropdown.get_skins()
if current_config["Output path"] != "" and current_config["osu! path"] != "":
self.delete_popup()
self.popup_bool = False
if current_config[".osr path"] == "auto":
current_settings["Custom mods"] = ""
save()
if not self.popup_bool:
self.settingspage.load_settings()
else:
self.settingspage.settingsarea.scrollArea.hide()
def setreplay(self, replay_path):
if replay_path is None or replay_path == "":
return
replay_name = os.path.split(replay_path)[-1]
self.osrpath.setText(replay_name)
current_config[".osr path"] = replay_path
parse_osr(current_config, current_settings)
logging.info("Updated replay path to: {}".format(replay_path))
def setmap(self, mapset_path):
if mapset_path is None or mapset_path == "":
return
current_config["Beatmap path"] = mapset_path
map_name = os.path.split(mapset_path)[-1]
self.mapsetpath.setText(map_name)
parse_map(current_config, current_settings)
logging.info("Updated beatmap path to: {}".format(mapset_path))
def check_replay_map(self):
if current_config[".osr path"] == "auto":
return
replay = get_latest_replay()
if self.prevreplay == replay or replay is None:
return
self.prevreplay = replay
self.setreplay(replay)
mapset = get_right_map(replay)
if mapset is None:
return
self.setmap(mapset)
def dragEnterEvent(self, e):
e.accept()
def dropEvent(self, e):
for url in e.mimeData().urls():
p = urlparse(url.url())
final_path = os.path.abspath(os.path.join(p.netloc, p.path))
if final_path.endswith(".osr"):
self.setreplay(final_path)
mapset = get_right_map(final_path)
if mapset is not None:
self.setmap(mapset)
elif os.path.isdir(final_path):
self.setmap(final_path)
elif final_path.endswith(".osu"):
if current_config[".osr path"] != "auto":
final_path = os.path.dirname(final_path)
self.setmap(final_path)
def excepthook(exc_type, exc_value, exc_tb):
tb = traceback.format_exc() + " " + "".join(traceback.format_exception(exc_type, exc_value, exc_tb))
logging.exception(tb)
print(tb)
QApplication.quit()
def main(execpath="."):
sys.excepthook = excepthook
floop = open(os.path.join(execpath, "exit.txt"), "w")
floop.write("0")
floop.close()
execpath = os.path.abspath(execpath)
if not os.path.isdir(os.path.join(execpath, "Logs")):
os.mkdir(os.path.join(execpath, "Logs"))
Log.apppath = os.path.join(execpath, "Logs", Log.apppath)
Log.runosupath = os.path.join(execpath, "Logs", Log.runosupath)
qtpath = os.path.dirname(PyQt5.__file__)
pluginpath = os.path.join(qtpath, "Qt/plugins")
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = pluginpath
App = QApplication(sys.argv)
window = Window(App, execpath)
b = open(os.path.join(abspath, "progress.txt"), "w")
b.close()
watcher = QtCore.QFileSystemWatcher([os.path.join(abspath, 'progress.txt')])
watcher.directoryChanged.connect(window.progressbar.directory_changed)
watcher.fileChanged.connect(window.progressbar.file_changed)
b = open("error.txt", "w") # remove abspath for temporary fix
b.close()
errorwatcher = QtCore.QFileSystemWatcher(['error.txt'])
errorwatcher.directoryChanged.connect(window.customwindow.directory_changed)
errorwatcher.fileChanged.connect(window.customwindow.file_changed)
ret = App.exec_()
if window.startbutton.proc is not None and window.startbutton.proc.poll() is None:
kill(window.startbutton.proc.pid)
cleanupkill()
with open("progress.txt", "w") as file:
file.write("done")
file.close()
sys.exit(ret)
if __name__ == "__main__":
main()