This repository has been archived by the owner on Feb 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
buildAssets.py
executable file
·422 lines (315 loc) · 12 KB
/
buildAssets.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#!/usr/bin/env python3
import os
import re
LEN_PREFIX = 4
LEN_SID = 4 # subtype
PREFIX = {"Player_": {"pre": 0, "current": 0},
"Enemy_": {"pre": 1, "current": 0},
"Boss_": {"pre": 2, "current": 0},
"Henchman_": {"pre": 3, "current": 0},
"Obstacle_": {"pre": 4, "current": 0},
"Powerup_": {"pre": 5, "current": 0},
"Bullet": {"pre": 6, "current": 0}}
STATES = {
"idle": 0,
"move": 1,
"shoot": 2,
"hurt": 3,
"die": 4,
"respawn": 5,
"power_up": 6
}
ASSETS_DIR = "include/client/cli/assets"
ASSETS_ID_FILE = "include/assetsID.hpp"
GUI_ID_FILE = "include/client/gui/guiID.hpp"
MAP_FILE_CLI = "src/client/cli/Assets.cpp"
MAP_FILE_GUI = "src/client/gui/Assets.cpp"
PACK_DIR = "static/packs"
DEFAULT_PACK = "StorWors"
BLANK_IMAGE = "static/packs/blank.png"
BLANK_AUDIO = "static/packs/blank.ogg"
# 0001 0001
# type + subtype <-- id assets
# ^
# |
# id server (Entity)
def genId(asset: str) -> int:
prefix = None
for p in PREFIX:
if asset.startswith(p):
prefix = PREFIX[p]
break
if prefix == None:
return None
sid = prefix["current"]
prefix["current"] += 1
return (prefix["pre"] << LEN_SID)+sid
def getAssetSize(path: str) -> [int, int]:
height = 0
width = 0
with open(path) as f:
for line in f.readlines():
w = re.match(
".*_width\s*=\s*(\d+)\s*\n", line)
if w != None:
width = int(w.group(1))
l = re.match(
".*_height\s*=\s*(\d+)\s*\n", line)
if l != None:
height = int(l.group(1))
return [width, height]
class AssetsID:
def __init__(self, file: str):
self._file = file
self._spritesID = []
def add(self, key: str, value: int):
self._spritesID.append((key, value))
def write(self):
with open(self._file, 'w') as f:
f.write(f'#pragma once\n\n')
f.write(
f'constexpr int ASSET_LEN_PREFIX = {LEN_PREFIX};\nconstexpr int ASSET_LEN_SID = {LEN_SID};\n\n')
for p in PREFIX:
f.write(
f'constexpr int ASSET_{p.replace("_", "").upper()}_TYPE = {PREFIX[p]["pre"]};\n')
f.write("\n")
for (key, value) in self._spritesID:
f.write(f'constexpr int ASSET_{key} = {value};\n')
f.close()
class GuiID:
def __init__(self, file: str):
self._file = file
self._spritesID = []
self._audios = 0
def add(self, key: str, value: int):
self._spritesID.append((key, value))
def addAudio(self, key: str, value: int):
self._spritesID.append((key, value))
self._audios += 1
def write(self):
with open(self._file, 'w') as f:
f.write(f'#pragma once\n\n')
f.write(f'constexpr int PACK_AUDIO_CHANNELS = {self._audios};\n\n')
for (key, value) in self._spritesID:
f.write(f'constexpr int PACK_{key} = {value};\n')
f.close()
class CliAssetsMap:
def __init__(self, file: str):
self._file = file
self._sprites = []
def addSprite(self, sprite: str):
self._sprites.append(sprite)
def write(self):
with open(self._file, 'w') as f:
f.write(
'''#include "client/cli/Assets.hpp"
#include "assetsID.hpp"
#include "client/cli/assets/Sprite.hpp"
''')
for s in self._sprites:
f.write(f'#include "client/cli/assets/{s}.hpp"\n')
f.write(
'''
std::map<unsigned, Sprite*> Assets::_assets = {};
void Assets::buildAssets() {
destroyAssets();
''')
for i in range(len(self._sprites)):
s = self._sprites[i]
f.write(
f' _assets.insert({{ASSET_{s.upper()}_ID, new {s}(ASSET_{s.upper()}_HEIGHT, ASSET_{s.upper()}_WIDTH)}});\n')
f.write(
'''}
void Assets::destroyAssets() {
for (const auto& e: _assets) {
delete e.second;
}
_assets.clear();
}
Sprite* Assets::getSpriteById(unsigned id) {
return _assets.at(id);
}
''')
f.close()
class GuiAssetsMap:
def __init__(self, file: str):
self._file = file
self._pack = {}
self._files = {}
self._audios = {}
def addPack(self, pack: str, path: str):
self._pack[pack] = {
"path": path,
"assets": {}
}
def addAsset(self, pack: str, asset: str):
self._pack[pack]["assets"][asset] = [{} for x in range(len(STATES))]
def addStateSteps(self, pack: str, asset: str, state: int, stateStep: list):
self._pack[pack]["assets"][asset][state] = stateStep
def addFile(self, id: int, path: str):
self._files[id] = path
def addAudio(self, id: int, paths: list):
self._audios[id] = paths
def write(self):
with open(self._file, 'w') as f:
f.write(
'''#include "client/gui/Assets.hpp"
#include <unistd.h>
#include "assetsID.hpp"
#include "client/gui/guiID.hpp"
#include "constants.hpp"
std::vector<std::string> Assets::_pack = {
''')
for p in self._pack:
f.write(f' "{self._pack[p]["path"]}",\n')
f.write(
'''};
int Assets::_currentPack = 0;
''')
f.write(
f'std::string Assets::_blankAudio = "{BLANK_AUDIO}";\n\n')
f.write('Assets::AudioMap Assets::_audios = {\n')
for aid in self._audios:
f.write(
f' {{\n PACK_{aid}_ID,\n {{\n')
for a in self._audios[aid]:
f.write(f' "{a}",\n')
f.write(' }\n },\n')
f.write('};\n\n')
f.write('Assets::FileMap Assets::_files = {\n')
for fid in self._files:
f.write(
f' {{\n PACK_{fid}_ID,\n "{self._files[fid]}",\n }},\n')
f.write('};\n\n')
f.write(
f'std::string Assets::_blankImage = "{BLANK_IMAGE}";')
f.write(
'''
Assets::AssetsMap Assets::_assets = {
''')
for p in self._pack:
for a in self._pack[p]["assets"]:
f.write(
f' {{\n ASSET_{a.upper()}_ID,\n {{\n ASSET_{a.upper()}_WIDTH,\n ASSET_{a.upper()}_HEIGHT,\n {{\n')
for s in range(len(self._pack[p]["assets"][a])):
f.write(' {\n')
for ss in range(len(self._pack[p]["assets"][a][s])):
v = ", ".join(
[f'"{x}"' for x in self._pack[p]["assets"][a][s][ss]])
f.write(
f' {{{v}}},\n')
f.write(' },\n')
f.write(' },\n')
f.write(' },\n')
f.write(' },\n')
f.write(
"""};
void Assets::setPack(int pack) {
_currentPack = pack < _pack.size() ? pack : 0;
}
int Assets::getCurrentPack() {
return _currentPack;
}
std::string Assets::getAudio(int id) {
std::string path = _pack[_currentPack] + _audios[id][rand() % _audios[id].size()];
if (access(path.c_str(), F_OK) == -1) return _blankAudio;
return path;
}
std::string Assets::getFile(int id) {
std::string path = _pack[_currentPack] + _files[id];
if (access(path.c_str(), F_OK) == -1) return _blankImage;
return path;
}
std::vector<Sprite>& Assets::getEditableEntities(std::vector<Sprite>& dest) {
for (const auto& e: _assets) {
switch (e.first >> ASSET_LEN_SID) {
case ASSET_OBSTACLE_TYPE:
case ASSET_ENEMY_TYPE:
case ASSET_BOSS_TYPE:
dest.push_back({e.first, std::get<0>(e.second), std::get<1>(e.second), _pack[_currentPack] + "assets/" + std::get<2>(e.second)[IDLE_STATE][0][0]});
break;
}
}
return dest;
}
Sprite Assets::getSprite(unsigned id, int state, int stateStep, int variant) {
int width = std::get<0>(_assets.at(id));
int height = std::get<1>(_assets.at(id));
std::vector<std::vector<std::vector<std::string>>> spriteStates = std::get<2>(_assets.at(id));
if (state >= spriteStates.size()) return {id, width, height, _blankImage};
std::vector<std::vector<std::string>> spriteSateSteps = spriteStates[state];
if (spriteSateSteps.size() == 0) {
spriteSateSteps = spriteStates[0];
}
if (stateStep >= spriteSateSteps.size()) stateStep %= spriteSateSteps.size();
std::vector<std::string> spriteVariant = spriteSateSteps[stateStep];
if (variant >> ASSET_LEN_SID == ASSET_POWERUP_TYPE) variant = (variant % ASSET_LEN_SID) + 1;
if (variant >= spriteVariant.size()) variant = 0;
return {id, width, height, _pack[_currentPack] + "assets/" + spriteVariant[variant]};
}
std::tuple<int, int> Assets::getSpriteSize(unsigned id) {
int width = std::get<0>(_assets.at(id));
int height = std::get<1>(_assets.at(id));
return {width, height};
}""")
f.close()
def main():
assetsID = AssetsID(ASSETS_ID_FILE)
cliAssetsMap = CliAssetsMap(MAP_FILE_CLI)
for f in sorted(os.listdir(ASSETS_DIR)):
if not os.path.isdir(os.path.join(ASSETS_DIR, f)):
filename = f.rsplit('.', 1)[0]
id = genId(filename)
if id != None:
[width, height] = getAssetSize(os.path.join(ASSETS_DIR, f))
assetsID.add(f'{filename.upper()}_ID', id)
assetsID.add(f'{filename.upper()}_WIDTH', width)
assetsID.add(f'{filename.upper()}_HEIGHT', height)
cliAssetsMap.addSprite(filename)
assetsID.write()
cliAssetsMap.write()
guiID = GuiID(GUI_ID_FILE)
guiAssetsMap = GuiAssetsMap(MAP_FILE_GUI)
packs = os.listdir(PACK_DIR)
defaultPack = packs.index(DEFAULT_PACK)
packs[defaultPack], packs[0] = packs[0], packs[defaultPack]
for pack in packs:
packPath = os.path.join(PACK_DIR, f'{pack}/')
if os.path.isdir(packPath):
guiAssetsMap.addPack(pack, packPath)
gamePath = os.path.join(packPath, "assets")
for asset in sorted(os.listdir(gamePath)):
guiAssetsMap.addAsset(pack, asset)
assetPath = os.path.join(gamePath, asset)
for state in os.listdir(assetPath):
statePath = os.path.join(assetPath, state)
stateSteps = []
statePath = os.path.join(assetPath, state)
for stateStep in sorted(os.listdir(statePath)):
stateStepPath = os.path.join(statePath, stateStep)
stateSteps.append([os.path.relpath(os.path.join(
stateStepPath, variant), gamePath) for variant in sorted(os.listdir(stateStepPath))])
guiAssetsMap.addStateSteps(
pack, asset, STATES[state], stateSteps)
defaultPackPath = os.path.join(PACK_DIR, DEFAULT_PACK)
i = 0
for a in sorted(os.listdir(os.path.join(defaultPackPath, "audios"))):
guiID.addAudio(f'AUDIO_{a.upper()}_ID', i)
guiAssetsMap.addAudio(
f'AUDIO_{a.upper()}', [os.path.join("audios", a, f) for f in sorted(os.listdir(os.path.join(defaultPackPath, "audios", a)))])
i += 1
for b in sorted(os.listdir(os.path.join(defaultPackPath, "background"))):
filename = b.rsplit('.', 1)[0]
guiID.add(f'BACKGROUND_{filename.upper()}_ID', i)
guiAssetsMap.addFile(
f'BACKGROUND_{filename.upper()}', os.path.join("background", b))
i += 1
for h in sorted(os.listdir(os.path.join(defaultPackPath, "hud"))):
filename = h.rsplit('.', 1)[0]
guiID.add(f'HUD_{filename.upper()}_ID', i)
guiAssetsMap.addFile(
f'HUD_{filename.upper()}', os.path.join("hud", h))
i += 1
guiAssetsMap.write()
guiID.write()
main()