-
Notifications
You must be signed in to change notification settings - Fork 58
/
GenerateResources.py
362 lines (307 loc) · 8.76 KB
/
GenerateResources.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
#!/usr/bin/env python3
###################################################################################
# This script will generate all Resources.cpp and Resources.h files. #
# Run this script every time files are added to the Squally/Resources/ directory. #
###################################################################################
from os import listdir
from os import path
from os.path import isfile, join, splitext, abspath, realpath, basename, relpath
import os
import re
import sys
def main():
continueStr = "n"
print("This will generate all resource paths and will result in a large build time. Continue (y/*)?")
if (sys.version_info > (3, 0)):
continueStr = input()
else:
continueStr = raw_input()
if (continueStr != "y"):
print("Script canceled by user")
return False
print("Generating resource paths...")
# BackgroundResources
createResourceFile("BackgroundResources", (
'.scml',
'.png',
'.jpg',
'.bmp',
'.tif',
), "Platformer/Backgrounds", False)
# CutsceneResources
createResourceFile("CutsceneResources", (
'.scml',
'.png',
'.jpg',
'.bmp',
'.tif',
), "Platformer/Cutscenes", False)
# DecorResources
createResourceFile("DecorResources", (
'.scml',
'.png',
'.jpg',
'.bmp',
'.tif',
), "Platformer/Decor", False)
# EntityResources
createResourceFile("EntityResources", (
'.scml',
'.png',
'.jpg',
'.bmp',
'.tif',
), "Platformer/Entities", False)
# IsometricEntityResources
createResourceFile("IsometricEntityResources", (
'.scml',
'.png',
'.jpg',
'.bmp',
'.tif',
), "Isometric/Entities", False)
# HexusResources
createResourceFile("HexusResources", (
'.scml',
'.png',
'.jpg',
'.bmp',
'.tif',
), "Hexus", False)
# PointerTraceResources
createResourceFile("PointerTraceResources", (
'.scml',
'.png',
'.jpg',
'.bmp',
'.tif',
), "PointerTrace", False)
# CipherResources
createResourceFile("CipherResources", (
'.scml',
'.png',
'.jpg',
'.bmp',
'.tif',
), "Cipher", False)
# ObjectResources
createResourceFile("ObjectResources", (
'.scml',
'.png',
'.jpg',
'.bmp',
'.tif',
), "Platformer/Objects", False)
# ItemResources
createResourceFile("ItemResources", (
'.scml',
'.png',
'.jpg',
'.bmp',
'.tif',
), "Platformer/Items", False)
# FXResources
createResourceFile("FXResources", (
'.scml',
'.png',
'.jpg',
'.bmp',
'.tif',
), "Platformer/FX", False)
# IsometricObjectResources
createResourceFile("IsometricObjectResources", (
'.scml',
'.png',
'.jpg',
'.bmp',
'.tif',
), "Isometric/Objects", False)
# TerrainResources
createResourceFile("TerrainResources", (
'.png',
'.jpg',
'.bmp',
'.tif',
), "Platformer/Terrain", False)
# TextureResources
createResourceFile("TextureResources", (
'.png',
'.jpg',
'.bmp',
'.tif',
), "Platformer/Textures", False)
# CutsceneResources
createResourceFile("CutsceneResources", (
'.scml',
'.png',
'.jpg',
'.bmp',
'.tif',
), "Cutscenes", False)
# UIResources
createResourceFile("UIResources", (
'.scml',
'.png',
'.jpg',
'.bmp',
'.tif',
), "UI", False)
# ShaderResources
createResourceFile("ShaderResources", (
'.frag',
'.vert',
), "Shaders", False)
# MusicResources
createResourceFile("MusicResources", (
'.ogg',
'.wav',
), "Music", False)
# SoundResources
createResourceFile("SoundResources", (
'.ogg',
'.wav',
), "Sounds", False)
# MapResources
createResourceFile("MapResources", (
'.tmx',
'.png',
), "Platformer/Maps", False)
# IsometricMapResources
createResourceFile("IsometricMapResources", (
'.tmx',
'.png',
), "Isometric/Maps", False)
# ParticleResources
createResourceFile("ParticleResources", (
'.plist',
), "Particles", False)
# FontResources
createResourceFile("FontResources", (
'.ttf',
'.png',
'.jpg',
'.bmp',
'.tif',
), "UI/Fonts", False)
# StringResources
createResourceFile("StringResources", (
'.json',
), "Strings", False)
print("Resource generation complete.")
return True
# TODO: Maybe use this, but this needs some debugging
def natural_sort(l):
convert = lambda text: int(text) if text.isdigit() else text.lower()
alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
return sorted(l, key=alphanum_key)
def createResourceFile(outputFileBase, extensions, searchPath, asBytes):
projectRoot = abspath(join(realpath(__file__), ".."))
searchPathPublic = join(projectRoot, "Resources/Public/" + searchPath)
searchPathPrivate = join(projectRoot, "Resources/Private/" + searchPath)
generatedClassFolder = "Source/Resources/"
outputHeader = outputFileBase + ".h"
outputClass = outputFileBase + ".cpp"
resourcePath = abspath(join(join(realpath(__file__), ".."), "Resources"))
files = []
for extension in extensions:
for root, dirnames, filenames in os.walk(searchPathPublic):
for filename in filenames:
if filename.lower().endswith(extension):
files.append((join(root, filename), searchPathPublic))
continue
for root, dirnames, filenames in os.walk(searchPathPrivate):
for filename in filenames:
if filename.lower().endswith(extension):
files.append((join(root, filename), searchPathPrivate))
continue
# TODO: Windows/OSX prioritize underscores vs alphanumeric differently. Should probably do a custom sort for consistency.
files.sort()
with open(generatedClassFolder + outputHeader,'w+') as h, open(generatedClassFolder + outputClass,'w+') as cpp:
warning = ( "////////////////////////////////////////////////////////////////////////////////////////////" + "\n" +
"// THIS C++ FILE IS GENERATED DO NOT EDIT. RUN GenerateResources.py TO GENERATE THIS FILE //" + "\n" +
"////////////////////////////////////////////////////////////////////////////////////////////" + "\n")
h.write(warning)
h.write("#pragma once" + "\n")
h.write("#include <string>" + "\n")
if asBytes:
h.write("#include <vector>" + "\n")
h.write("\n")
#h.write("class " + outputFileBase + "\n");
h.write("namespace " + outputFileBase + "\n")
h.write("{" + "\n")
# h.write("public:" + "\n");
#h.write("\ttemplate<typename... Ts>\n")
#h.write("\tstd::vector<std::byte> make_bytes(Ts&&... args) noexcept\n");
#h.write("\t{\n")
#h.write("\t\treturn{std::byte(std::forward<Ts>(args))...};\n")
#h.write("\t}\n\n")
cpp.write(warning)
cpp.write("\n")
cpp.write("#include \"" + outputHeader + "\"" + "\n")
cpp.write("#include <string>" + "\n")
cpp.write("\n")
for file in files:
resourceRelativeFilePath = relpath(file[0], resourcePath).replace("\\", "/")
searchRelativeFilePath = relpath(file[0], file[1]).replace("\\", "/")
variableName = searchRelativeFilePath \
.replace("/", "_") \
.replace(" ", "_") \
.replace("+", "_") \
.replace("@", "_") \
.replace("-", "_") \
.replace("(", "_") \
.replace(")", "_")
variableNameNoExtension = splitext(variableName)[0]
if asBytes:
print("Embedding " + variableNameNoExtension + "...")
fullPath = path.abspath(path.join(resourcePath, resourceRelativeFilePath))
h.write("\tstatic const std::vector<unsigned char> " + variableNameNoExtension + " = {")
readBytes(fullPath, h)
h.write("};\n")
# fileLength = getFileLength(fullPath)
#h.write("\textern const unsigned char " + variableNameNoExtension + "[" + str(fileLength) + "];" + "\n")
#cpp.write("const unsigned char " + outputFileBase + "::" + variableNameNoExtension + "[" + str(fileLength) + "] = {\n\t")
#readBytes(fullPath, cpp)
#cpp.write("};\n\n")
else:
h.write("\textern const std::string " + variableNameNoExtension + ";" + "\n");
cpp.write("const std::string " + outputFileBase + "::" + variableNameNoExtension + " = \"" + resourceRelativeFilePath + "\";" + "\n")
h.write("};" + "\n")
def getFileLength(fullPath):
if (sys.version_info > (3, 0)):
return os.stat(fullPath).st_size
else:
# Not sure if different in Py2
return os.stat(fullPath).st_size
def readBytes(fullPath, file):
grouping = { 'v': 0 }
def writeByte(byte, file):
file.write("," + hex(byte[0]))
grouping['v'] = grouping['v'] + 1
if grouping['v'] > 16:
# file.write("\n\t")
grouping['v'] = 0
if (sys.version_info > (3, 0)):
with open(fullPath, "rb") as f:
byte = f.read(1)
if byte != b"":
file.write(hex(byte[0]))
byte = f.read(1)
while byte != b"":
writeByte(byte, file)
byte = f.read(1)
else:
with open(fullPath, "rb") as f:
byte = f.read(1)
if byte != "":
file.write(hex(byte[0]))
byte = f.read(1)
while byte != "":
writeByte(byte, file)
byte = f.read(1)
if __name__ == '__main__':
if main():
# Upon successful regen, call the CopyResources script.
# We defer the import to CopyResourcces, so that if a nested import is missing, it will not effect this script
import CopyResources
CopyResources.main()