-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathspherelauncher.py
131 lines (97 loc) · 3.98 KB
/
spherelauncher.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
import os
import os.path as path
from PySide6.QtCore import QProcess, QSettings, Slot
from PySide6.QtWidgets import QWidget
from dialogs.errordialog import ErrorDialog
from qsiproject import QSIProject, ProjectType
class SphereLauncher:
settings: QSettings
process:QProcess
output:str
parent:QWidget
sphereName:str
@property
def winePath(self):
return path.join(self.settings.value("wineDir", ""), "wine")
@property
def neospherePath(self):
return path.join(self.settings.value("neosphereDir", ""), "neosphere")
@property
def ssjPath(self):
return path.join(self.settings.value("neosphereDir", ""), "ssj")
@property
def cellPath(self):
return path.join(self.settings.value("neosphereDir", ""), "cell")
@property
def legacySphereEnginePath(self):
return path.join(self.settings.value("legacySphereDir", ""), "engine.exe")
@property
def legacySphereConfigPath(self):
return path.join(self.settings.value("legacySphereDir", ""), "config.exe")
def __init__(self, parent:QWidget = None) -> None:
self.parent = parent
self.output = ""
self.settings = QSettings()
self.process = QProcess(parent)
self.process.readyReadStandardOutput.connect(self.onProcessStdout)
self.process.readyReadStandardError.connect(self.onProcessStderr)
self.process.errorOccurred.connect(lambda e: self.showErrorWithOutput(f"An error occured with the Sphere process: {e}"))
self.process.finished.connect(self.onProcessFinished)
self.sphereName = "Sphere"
def _runSphereProgram(self, isLegacy:bool, program:str, args:list[str], workingDir:str = os.curdir):
if isLegacy and self.settings.value("legacySphereDir", "") == "":
raise Exception("Legacy Sphere directory not set")
self.process.setWorkingDirectory(workingDir)
if os.name == "nt":
self.process.setProgram(program)
self.process.setArguments(args)
else:
if isLegacy:
self.process.setProgram("wine")
self.process.setArguments([program] + args)
else:
self.process.setProgram(program)
self.process.setArguments(args)
print("Starting process: ", self.process.program())
print("Process Arguments: ", self.process.arguments())
self.process.start(self.process.program(), self.process.arguments())
def runLegacyConfig(self):
self.sphereName = "Sphere 1.x config"
self._runSphereProgram(True, self.legacySphereConfigPath, [], self.settings.value("legacySphereDir"))
def runLegacyEngine(self, gameDir:str):
self.sphereName = "Sphere 1.x"
self._runSphereProgram(True, self.legacySphereEnginePath, ["-game", gameDir], gameDir)
def runNeosphere(self, gameDir:str):
self.sphereName = "neoSphere"
self._runSphereProgram(False, self.neospherePath, [gameDir], gameDir)
def launchGame(self, project:QSIProject):
if self.settings.value("defaultEngine", "neosphere") == "neosphere":
self.runNeosphere(project.buildDir)
else:
self.runLegacyEngine(project.buildDir)
def showErrorWithOutput(self, error:str, title="Unable to launch game"):
informativeText = \
f"Command: {self.process.program()}<br>" + \
f"Arguments: {self.process.arguments()}"
if self.output != "":
informativeText += f"<br>{self.sphereName} output:<br>{self.output}"
ErrorDialog.showError(self.parent, error + "<br>" + informativeText, title)
@Slot(QProcess.ProcessError)
def onProcessErrorOccured(self, error:QProcess.ProcessError):
self.showErrorWithOutput(f"An error occured while running {self.sphereName}: {error}", "Error")
@Slot()
def onProcessStdout(self):
data = self.process.readAllStandardOutput().data().decode()
# print("stdout: ", data)
self.output += data
@Slot()
def onProcessStderr(self):
data = self.process.readAllStandardError().data().decode()
# print("stderr: ", data)
self.output += data
@Slot(QProcess.ExitStatus)
def onProcessFinished(self, exitCode: QProcess.ExitStatus|int):
if exitCode != 0 and exitCode != QProcess.ExitStatus.NormalExit:
self.showErrorWithOutput(f"{self.sphereName} process finished with exit code {exitCode}")
else:
print("Process exited normally")