-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathqsiproject.py
More file actions
237 lines (206 loc) · 7.16 KB
/
qsiproject.py
File metadata and controls
237 lines (206 loc) · 7.16 KB
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
from enum import Enum
import glob
from io import BufferedReader
from os import path
import re
from PySide6.QtGui import QIcon
from PySide6.QtCore import QDir, QFileInfo
from PySide6.QtWidgets import QMessageBox
from dialogs.errordialog import ErrorDialog
_file_filters = ("*.ssproj", "Cellscript.js", "Cellscript.mjs", "Cellscript.cjs", "game.sgm")
class ProjectType(Enum):
Unknown = 0
SSProject = 1
Cellscript_js = 2
Cellscript_mjs = 3
Cellscript_cjs = 4
SGM = 5
class QSIProject:
name: str
author: str
width: int
height: int
apiLevel: int
version: int
projectType: ProjectType
projectFilePath: str
saveID: str
summary: str
buildDir: str
script: str
projectDir: str
compiler: str
def __init__(self):
self.projectType = ProjectType.Unknown
self.projectFilePath = None
self.name = ""
self.author = ""
self.width = 0
self.height = 0
self.apiLevel = 0
self.version = 0
self.saveID = ""
self.summary = ""
self.buildDir = None
self.script = None
self.projectDir = None
self.compiler = "Vanilla"
def open(self, path: str, printWarnings:bool = False) -> bool:
self.projectDir = path
fileInfo = QFileInfo(path)
if not fileInfo.exists():
ErrorDialog.showError(None, "Project path %s does not exist" % path)
return False
if fileInfo.isDir():
infoList = QDir(path).entryInfoList(_file_filters, QDir.Filter.Files|QDir.Filter.NoDotAndDotDot)
for info in infoList:
self.projectFilePath = info.filePath()
filename = info.fileName()
if info.suffix() == "ssproj":
self.projectType = ProjectType.SSProject
elif filename == "Cellscript.js":
self.projectType = ProjectType.Cellscript_js
self.compiler = "Cell"
elif filename == "Cellscript.cjs":
self.projectType = ProjectType.Cellscript_cjs
self.compiler = "Cell"
elif filename == "Cellscript.mjs":
self.projectType = ProjectType.Cellscript_cjs
self.compiler = "Cell"
elif filename == "game.sgm":
self.compiler = "Vanilla"
self.projectType = ProjectType.SGM
else:
self.projectFilePath = fileInfo.filePath()
self.projectDir = fileInfo.dir().path()
if self.projectFilePath == "" or self.projectFilePath is None:
if printWarnings:
print(f"Unable to get project info for path {path} - skipping")
return False
self.buildDir = self.projectDir
with open(self.projectFilePath, "rb") as projectFile:
return self._prepareProjectFile(projectFile)
def getIcon(self) -> QIcon:
dir = QDir(self.projectDir)
iconInfo = QFileInfo(dir, "icon.png")
if iconInfo.exists():
return QIcon(iconInfo.canonicalFilePath())
else:
icoList = glob.glob("*.ico", root_dir=self.projectDir, recursive=False)
if len(icoList) > 0:
return QIcon(path.join(self.projectDir, icoList[0]))
return QIcon(":/res/neosphere.png")
def getResolutionString(self) -> str:
return "%dx%d" % (self.width, self.height)
def saveSSProj(self):
if self.projectDir is None or self.name == "":
return
with open(path.join(self.projectDir, self.name.replace("/", "-").replace(":", "-") + ".ssproj"), "bw") as file:
file.write("[.ssproj]\r\n" +
f"author={self.author}\r\n" +
"backCompatible=True\r\n" +
f"compiler={self.compiler}\r\n" +
f"description={self.author}\r\n" +
f"mainScript={self.script}\r\n" +
f"name={self.name}\r\n" +
f"screenHeight={self.height}\r\n" +
f"screenWidth={self.width}\r\n\r\n")
def _getCellscriptStringValue(self, cellscriptStr:str, key:str, defaultValue:str = "") -> str:
matches = re.match(r".*" + key + r"\s*:\s*(['\"`]).*", cellscriptStr, re.DOTALL|re.MULTILINE)
if matches is None:
return defaultValue
quoteChar = matches.group(1)[0]
startIndex = matches.start(1)
scriptClipped = cellscriptStr[startIndex+1:]
endIndex = -1
for i in range(len(scriptClipped)):
if scriptClipped[i] == quoteChar and scriptClipped[i-1] != '\\':
endIndex = i
break
return scriptClipped[:endIndex]
def _getCellscriptIntValue(self, cellscriptStr:str, key:str, defaultValue:int = 0) -> int:
matches = re.findall(r".*(" + key + r")\s*:\s*(\d+).*", cellscriptStr, re.DOTALL|re.MULTILINE)
if len(matches) == 0:
return defaultValue
value:str = matches[0][1]
return int(value) if value.isdigit() else defaultValue
def _prepareProjectFile(self, projectFile:BufferedReader) -> bool:
match self.projectType:
case ProjectType.SSProject:
return self._readSSProj(projectFile)
case ProjectType.Cellscript_js | ProjectType.Cellscript_mjs | ProjectType.Cellscript_cjs:
return self._readCellscript(projectFile)
case ProjectType.SGM:
return self._readSGM(projectFile)
def _readSSProj(self, projectFile:BufferedReader) -> bool:
lines = projectFile.readlines()
for line in lines:
line = line.decode()
if line == "" or line.find("=") == -1:
continue
parts = line.split("=")
val = parts[1].strip()
match parts[0]:
case "author":
self.author = val
case "buildDir":
self.buildDir = QDir(self.projectDir).absoluteFilePath(val)
case "compiler":
self.compiler = val
case "description":
self.summary = val
case "mainScript":
self.script = val
case "name":
self.name = val
case "screenHeight":
self.height = int(val)
case "screenWidth":
self.width = int(val)
return True
def _readCellscript(self, projectFile:BufferedReader) -> bool:
scriptStr = projectFile.read().decode()
self.name = self._getCellscriptStringValue(scriptStr, "name")
self.author = self._getCellscriptStringValue(scriptStr, "author")
self.version = self._getCellscriptIntValue(scriptStr, "version", 1)
self.apiLevel = self._getCellscriptIntValue(scriptStr, "apiLevel", 1)
self.saveID = self._getCellscriptStringValue(scriptStr, "saveID")
self.summary = self._getCellscriptStringValue(scriptStr, "summary")
self.script = self._getCellscriptStringValue(scriptStr, "main")
resolution = self._getCellscriptStringValue(scriptStr, "resolution")
resolutionArr = resolution.split("x")
if len(resolutionArr) != 2:
# expects format <width>x<height>, missing x or more than one x
ErrorDialog.showError(None, f"Project {self.name} has an invalid resolution string: {resolution}")
return False
self.width = int(resolutionArr[0]) if resolutionArr[0].isdigit() else -1
self.height = int(resolutionArr[1]) if resolutionArr[1].isdigit() else -1
if self.width < 1 or self.height < 1:
# width or height are an invalid number
ErrorDialog.showError(None, f"Project {self.name} has an invalid resolution string: {resolution}")
return False
self.compiler = "Cell"
self.buildDir = QFileInfo(self.projectFilePath).dir().absoluteFilePath("dist/")
return True
def _readSGM(self, projectFile:BufferedReader) -> bool:
lines = projectFile.readlines()
for line in lines:
if line == "":
continue
parts = line.decode().split("=", 1)
match parts[0]:
case "name":
self.name = parts[1]
case "author":
self.author = parts[1]
case "description":
self.summary = parts[1]
case "screen_width":
self.width = int(parts[1])
case "screen_height":
self.height = int(parts[1])
case "script":
self.script = parts[1]
return True
def __repr__(self) -> str:
return repr((self.name, self.author))