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
|
package assets_gen
import os "core:os/os2"
import "core:fmt"
import "core:path/filepath"
import "core:strings"
import "core:sort"
COMPILED_DIR :: ".compiled-res/"
HELP_STR :: `USAGE: %v [input dir] [output file].odin`
Asset_Loader :: #type proc(string, ^os.File, ^os.File)
file_content :=
`#+feature dynamic-literals
package demonchime
// DO NOT EDIT
//
// This file is autogenerated by tools/compile_assets
// All resource types are defined in 'src/resources.odin'.
Image_Id :: enum {
<image-enum>}
Animation_Id :: enum {
<anim-enum>}
Room_Id :: enum {
<room-enum>}
Tileset_Id :: enum {
<tileset-enum>}
Object_Type :: enum {
<object-types>}
images: [Image_Id]Image_Resource = {
<image-load>}
animations: [Animation_Id]Animation_Resource = {
<anim-load>}
rooms: [Room_Id]Room_Resource = {
<room-load>}
tilesets: [Tileset_Id]Tileset_Resource = {
<tileset-load>}
tiles: []Tile_Resource = {
<tiles-load>}
world: []Room_Position_Resource = {
<room-positions>}
Resource_Id :: union {
Image_Id,
Animation_Id,
Room_Id,
Tileset_Id,
}
path_to_resource_id: map[string]Resource_Id = {
<resource-paths>}
`
images: map[string]string
animations: map[string]string
rooms: map[string]string
tiles: [dynamic]string
tilesets: map[string]string
world: [dynamic]string
// Don't want to add types multiple times, so it's a set
object_type_names: map[string]struct{}
output_dir: string
paths_to_res_type: map[string]string
die :: proc(msg: string, args: ..any, exit_code := 1) {
fmt.eprintfln(msg, ..args)
os.exit(exit_code)
}
print_help :: proc(exit_code := 1) {
die(HELP_STR, os.args[0], exit_code = exit_code)
}
recursive_read_directory :: proc(
dir: string,
allocator := context.allocator,
) -> ([]os.File_Info, os.Error) {
context.allocator = allocator
full_files: [dynamic]os.File_Info
files, read_dir_err := os.read_all_directory_by_path(
dir,
allocator = allocator,
)
if read_dir_err != nil {
return nil, read_dir_err
}
defer delete(files)
for file in files {
fmt.printfln("Found %-15v '%v'", file.type, file.fullpath)
#partial switch file.type {
case .Directory:
subdir_files, sub_err := recursive_read_directory(file.fullpath)
if sub_err != nil {
delete(full_files)
return nil, sub_err
}
defer delete(subdir_files)
append(&full_files, ..subdir_files)
os.file_info_delete(file, allocator)
case .Regular:
append(&full_files, file)
case:
delete(full_files)
die("Invalid file type %v", file.type)
}
}
return full_files[:], nil
}
sort_keys :: proc(
m: map[string]$V,
allocator := context.allocator,
) -> []string {
arr := make([]string, len(m), allocator)
i := 0
for k, _ in m {
arr[i] = k
i += 1
}
sort.merge_sort(arr)
return arr
}
create_enum :: proc(
content: string,
replace: string,
elements: map[string]string
) -> string {
ids := ""
keys := sort_keys(elements, context.temp_allocator)
for key in keys {
ids = strings.concatenate({
ids,
" ",
strings.to_ada_case(key, context.temp_allocator),
",\n"
}, allocator = context.temp_allocator)
}
replaced, _ := strings.replace_all(
content,
replace,
ids,
allocator = context.temp_allocator,
)
return replaced
}
create_loads :: proc(
content: string,
map_name: string,
placeholder: string,
elements: map[string]string
) -> string {
load := ""
keys := sort_keys(elements, context.temp_allocator)
for key in keys {
load = strings.concatenate({
load,
" .",
strings.to_ada_case(key, context.temp_allocator),
" = ",
elements[key],
",\n",
}, allocator = context.temp_allocator)
}
return set_placeholder(content, placeholder, load)
}
set_placeholder :: proc(
content: string,
placeholder: string,
with: string
) -> string {
new_content, _ := strings.replace_all(
content,
placeholder,
with,
allocator = context.temp_allocator,
)
return new_content
}
main :: proc() {
if len(os.args) != 3 {
print_help()
}
// add dummy
append(&tiles, "")
// Need to copy, cause you can't just free executable memory, you fool
no_anim_line := "{frame_count=1, frame_durations={100}, tags={}}"
no_anim_line_owned := make([]u8, len(no_anim_line))
copy_from_string(no_anim_line_owned, no_anim_line)
animations["NONE"] = string(no_anim_line_owned)
input_dir := os.args[1]
output_file_path := os.args[2]
abs_output_path, found_abs := filepath.abs(
output_file_path,
context.temp_allocator
)
if !found_abs {
die("Could not find absolute path to output file")
}
output_dir = filepath.dir(abs_output_path)
defer delete(output_dir)
output_file, open_err := os.open(output_file_path, {.Write, .Create, .Trunc})
if open_err != nil {
die("Could not create output file '%v' (%v)", output_file_path, open_err)
}
defer os.close(output_file)
dir_info, stat_err := os.stat(input_dir, context.temp_allocator)
if stat_err != nil {
die("Error reading directory %v", stat_err)
}
if dir_info.type != .Directory {
die("Expected directory (Got %v)", dir_info.type)
}
files, dir_read_err := recursive_read_directory(input_dir)
if dir_read_err != nil {
die("Could not iterate directory %v", dir_read_err)
}
defer {
for file in files {
os.file_info_delete(file, context.allocator)
}
delete(files)
}
free_all(context.temp_allocator)
loaders: map[string]Asset_Loader
defer delete(loaders)
loaders["tmj"] = load_room
loaders["world"] = load_world
loaders["tsj"] = load_tileset
loaders["qoi"] = load_qoi
loaders["png"] = load_png
loaders["ase"] = load_ase
os.make_directory_all(COMPILED_DIR)
fmt.println("Generating assets file...")
for file in files {
ext := filepath.ext(file.fullpath)[1:]
loader, has := &loaders[ext]
if !has {
fmt.printfln(
"%-25v Skipped\tNo loader for '.%v'",
file.name,
ext,
)
continue
}
f, open_err := os.open(file.fullpath)
if open_err != nil {
fmt.printfln("%-25v Skipped\tCould not load file", file.name)
continue
}
defer os.close(f)
loader^(file.fullpath, f, output_file)
fmt.printfln("%-25v Loaded", file.name)
free_all(context.temp_allocator)
}
content := file_content
images_to_enum: map[string]string
defer delete(images_to_enum)
content = create_enum(content, "<image-enum>", images)
content = create_enum(content, "<anim-enum>", animations)
content = create_enum(content, "<room-enum>", rooms)
content = create_enum(content, "<tileset-enum>", tilesets)
content = create_loads(content, "images", "<image-load>", images)
content = create_loads(content, "animations", "<anim-load>", animations)
content = create_loads(content, "rooms", "<room-load>", rooms)
content = create_loads(content, "tilesets", "<tileset-load>", tilesets)
tile_loads := ""
for tile, i in tiles {
if i == 0 {
continue
}
tile_loads = strings.concatenate({
tile_loads,
" ",
tile,
",\n",
}, allocator = context.temp_allocator)
}
content = set_placeholder(content, "<tiles-load>", tile_loads)
room_pos_loads := ""
for room_pos, i in world {
room_pos_loads = strings.concatenate({
room_pos_loads,
" ",
room_pos,
",\n",
}, allocator = context.temp_allocator)
}
content = set_placeholder(content, "<room-positions>", room_pos_loads)
res_paths := ""
cwd, _ := os.get_working_directory(context.temp_allocator)
for file in files {
res_type, is_res := paths_to_res_type[file.fullpath]
if !is_res {
continue
}
res_name := strings.to_ada_case(
filepath.stem(file.fullpath),
context.temp_allocator,
)
rel_path, _ := filepath.rel(cwd, file.fullpath, context.temp_allocator)
res_paths = strings.concatenate({
res_paths,
" \"",
rel_path,
"\" = ",
res_type,
".",
res_name,
",\n",
}, allocator = context.temp_allocator)
}
content = set_placeholder(content, "<resource-paths>", res_paths)
object_type_enum := ""
for object_type in sort_keys(
object_type_names,
allocator = context.temp_allocator,
) {
object_type_enum = strings.concatenate({
object_type_enum,
" ",
object_type,
",\n"
}, allocator = context.temp_allocator)
delete(object_type)
}
content, _ = strings.replace_all(
content,
"<object-types>",
object_type_enum,
allocator = context.temp_allocator,
)
os.write_string(output_file, content)
for image in images {
delete(images[image])
}
for anim in animations {
delete(animations[anim])
}
for room in rooms {
delete(rooms[room])
}
free_all(context.temp_allocator)
}
|