-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.py
260 lines (211 loc) · 8.92 KB
/
runner.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
import re
import os
import sys
import platform
from PySide6 import QtCore, QtWidgets, QtGui, QtWebEngineWidgets
from searchbin import search_loop
PLATFORM = platform.system()
class PromptWidget(QtWidgets.QWidget):
def __init__(self, openGameHandler):
super().__init__()
self.openGame = openGameHandler
layout = QtWidgets.QVBoxLayout()
layout.setAlignment(QtCore.Qt.AlignTop)
self.mainlabel = QtWidgets.QLabel(
"Play a T3 File", alignment=QtCore.Qt.AlignCenter
)
self.mainlabel.setStyleSheet("font-size: 20pt; font-weight: 700;")
self.mainlabel.installEventFilter(self)
layout.addWidget(self.mainlabel)
self.subtitlelabel = QtWidgets.QLabel(
"Drag and drop the file into this window or click the button below",
margin=5,
alignment=QtCore.Qt.AlignCenter,
)
self.subtitlelabel.setStyleSheet(
"font-size: 14pt; color: #ddd; font-weight: 300;"
)
self.subtitlelabel.installEventFilter(self)
layout.addWidget(self.subtitlelabel)
self.button = QtWidgets.QPushButton("Open game")
self.button.clicked.connect(self.openChooseGameDialog)
self.button.setStyleSheet(
"QPushButton { height: 40px; background: #400040 } QPushButton:hover { background: #300030; }"
)
self.button.installEventFilter(self)
layout.addWidget(self.button)
self.setLayout(layout)
def showDropIsValid(self, valid):
if valid:
self.mainlabel.setStyleSheet(
"color: #FF8E19; font-size: 20pt; font-weight: 700;"
)
self.subtitlelabel.setStyleSheet(
"color: #FF8E19; font-size: 14pt; font-weight: 300;"
)
else:
self.mainlabel.setStyleSheet(
"color: white; font-size: 20pt; font-weight: 700;"
)
self.subtitlelabel.setStyleSheet(
"color: #ddd; font-size: 14pt; font-weight: 300;"
)
def openChooseGameDialog(self, event):
dialog = QtWidgets.QFileDialog()
dialog.setAcceptDrops(True)
dialog.setFileMode(QtWidgets.QFileDialog.FileMode.ExistingFile)
dialog.setNameFilter("TADS 3 Game Files (*.t3)")
if dialog.exec():
self.openGame(dialog.selectedUrls()[0].toLocalFile())
class DragAndDropButtonWidget(QtWidgets.QWidget):
def __init__(self, child: QtWidgets.QWidget, openGameHandler):
super().__init__()
self.openGame = openGameHandler
self.setSizePolicy(QtWidgets.QSizePolicy.Maximum, QtWidgets.QSizePolicy.Maximum)
self.setAcceptDrops(True)
self.child = child
self.child.installEventFilter(self)
layout = QtWidgets.QVBoxLayout()
layout.setAlignment(QtCore.Qt.AlignCenter)
layout.addWidget(self.child)
self.setLayout(layout)
def dragEnterEvent(self, event):
urls = event.mimeData().urls()
if len(urls) == 1 and urls[0].toLocalFile().endswith(".t3"):
event.acceptProposedAction()
self.child.showDropIsValid(True)
def dragLeaveEvent(self, event):
self.child.showDropIsValid(False)
def dropEvent(self, event):
path = event.mimeData().urls()[0].toLocalFile()
self.openGame(path)
event.acceptProposedAction()
self.child.showDropIsValid(False)
def findUrl(string):
# findall() has been used
# with valid conditions for urls in string
regex = r"(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))"
url = re.findall(regex, string)
return [x[0] for x in url]
class RunnerWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
title = "TADS 3 Runner"
self.setWindowTitle(title)
self.setStyleSheet("background: #300030; color: white;")
self.openGameWidget = DragAndDropButtonWidget(
PromptWidget(openGameHandler=self.openGame), openGameHandler=self.openGame
)
self.playerWidget = QtWebEngineWidgets.QWebEngineView()
self.playerWidget.load("http://tads.org")
self.stack = QtWidgets.QStackedWidget()
self.stack.addWidget(self.openGameWidget)
self.stack.addWidget(self.playerWidget)
self.stack.setCurrentIndex(0)
self.serverProcess = None
self.foundGame = False
self.isWebUI = False
self.serverFinished = True
self.setCentralWidget(self.stack)
def processFinished(self):
self.serverFinished = True
if self.foundGame:
self.close()
else:
QtWidgets.QMessageBox.critical(
self,
"Uh oh!",
"Failed to start the interpreter process.",
buttons=QtWidgets.QMessageBox.StandardButton.Ok,
)
def closeEvent(self, event, accepted=False):
if self.serverProcess is not None and not self.serverFinished:
button = QtWidgets.QMessageBox.question(
self,
"Close game?",
"Are you sure you want to close your game while it's still running?",
)
if button == QtWidgets.QMessageBox.StandardButton.Yes:
if (
PLATFORM == "Windows"
): # windows doesn't have a proper way to end terminal applications lmfao
self.serverProcess.kill()
else:
self.serverProcess.terminate()
self.serverProcess.waitForFinished()
event.accept()
else:
event.ignore()
else:
event.accept()
def processStdout(self):
data = self.serverProcess.readAllStandardOutput()
line = bytes(data).decode("utf8")
print("Interpreter Stdout:" + line)
if self.isWebUI:
urls = findUrl(line)
if len(urls) > 0:
self.foundGame = True
self.playerWidget.load(urls[0])
self.stack.setCurrentIndex(1)
else:
matches = re.search(r"^WinId: ([0-9]+)$", line)
if matches is not None:
window = QtGui.QWindow.fromWinId(int(matches.group(1)))
window.setFlags(QtCore.Qt.WindowType.FramelessWindowHint)
self.playerWidget = QtWidgets.QWidget.createWindowContainer(window)
self.stack.insertWidget(1, self.playerWidget)
self.stack.setCurrentIndex(1)
def processStderr(self):
data = self.serverProcess.readAllStandardError()
line = bytes(data).decode("utf8")
print("Interpreter Stderr:" + line)
def processTimeout(self):
if not self.foundGame:
QtWidgets.QMessageBox.critical(
self,
"Uh oh!",
"It looks like the frobTADS/qTADS interpreter didn't start in a reasonable amount of time. Something's wrong.",
buttons=QtWidgets.QMessageBox.StandardButton.Ok,
)
self.serverProcess.kill()
def openGame(self, path):
global PLATFORM
self.foundGame = False
self.isWebUI = False
with open(path, "rb") as fh:
if search_loop(["tads-net".encode("utf-8")], fh.name, fh.read, fh.seek):
self.isWebUI = True
self.serverProcess = QtCore.QProcess()
self.serverProcess.finished.connect(self.processFinished)
self.serverProcess.readyReadStandardOutput.connect(self.processStdout)
self.serverProcess.readyReadStandardError.connect(self.processStderr)
if not self.isWebUI:
if PLATFORM == "Linux" or PLATFORM == "Darwin":
self.serverProcess.start("qtads", ["-e", path])
elif PLATFORM == "Windows":
self.serverProcess.start("./qtads.exe", [path])
self.foundGame = True
self.serverFinished = False
else:
if PLATFORM == "Linux" or PLATFORM == "Darwin":
self.serverProcess.start("frob", ["-i", "plain", "-N", "0", path])
elif PLATFORM == "Windows":
self.serverProcess.start(
"./t3run.exe", ["-plain", "-ns0", "-webhost", "localhost", path]
)
self.serverFinished = False
QtCore.QTimer.singleShot(1300, self.processTimeout)
if __name__ == "__main__":
app = QtWidgets.QApplication([])
if PLATFORM not in ["Windows", "Linux", "Darwin"]:
QtWidgets.QMessageBox.critical(
app,
"Uh oh!",
"It looks like you're running this on an unsupported platform. At the moment, we only support Linux, macOS, and Windows.",
)
window = RunnerWindow()
window.show()
if len(sys.argv) > 1:
window.openGame(sys.argv[1], None)
sys.exit(app.exec())