-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassets.cpp
516 lines (407 loc) · 15.7 KB
/
assets.cpp
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
#include "assets.h"
#include "file.h"
#include "lexer.h"
#include "map.h"
#include "core.h"
#include "stb/stb_image.h"
i32 next_asset_type_id = 0;
struct {
DynamicArray<String> folders;
DynamicArray<Asset> loaded;
DynamicArray<AssetHandle> removed;
DynamicArray<AssetHandle> free_slots;
DynamicMap<String, i32> types;
DynamicMap<String, asset_load_t> load_procs;
DynamicMap<String, asset_save_t> save_procs;
} assets{};
void init_assets(Array<String> folders, const AssetTypesDesc &desc) EXPORT
{
SArena scratch = tl_scratch_arena();
assets.folders = { .alloc = mem_dynamic };
array_reserve(&assets.folders, folders.count);
for (auto it : folders) {
String path = absolute_path(it, scratch);
if (!path) continue;
if (array_contains(assets.folders, path)) continue;
LOG_INFO("adding asset folder: %.*s", STRFMT(path));
array_add(&assets.folders, duplicate_string(path, mem_dynamic));
}
assets.load_procs.alloc = mem_dynamic;
assets.save_procs.alloc = mem_dynamic;
for (i32 i = 0; i < ARRAY_COUNT(desc.types); i++) {
if (desc.types[i].ext.length == 0) break;
map_set(&assets.types, desc.types[i].ext, desc.types[i].type_id);
map_set(&assets.load_procs, desc.types[i].ext, desc.types[i].load_proc);
map_set(&assets.save_procs, desc.types[i].ext, desc.types[i].save_proc);
}
}
bool is_asset_loaded(String path) EXPORT
{
for (auto it : assets.loaded) if (it.path == path) return true;
return false;
}
AssetHandle find_loaded_asset(String path) EXPORT
{
for (i32 i = 0; i < assets.loaded.count; i++) {
if (assets.loaded[i].path == path) {
return AssetHandle{ .index = i, .gen = assets.loaded[i].gen };
}
}
return ASSET_HANDLE_INVALID;
}
AssetHandle find_asset_handle(String path) EXPORT
{
SArena scratch = tl_scratch_arena();
String apath = resolve_asset_path(path, scratch);
if (!apath.length) return ASSET_HANDLE_INVALID;
for (i32 i = 0; i < assets.loaded.count; i++) {
if (path_equals(assets.loaded[i].path, apath)) {
return AssetHandle{ .index = i, .gen = assets.loaded[i].gen };
}
}
String ext = extension_of(apath);
i32 *type = map_find(&assets.types, ext);
if (!type) LOG_ERROR("unknown asset type for asset '%.*s' with ext: '%.*s'", STRFMT(apath), STRFMT(ext));
return create_asset(apath, *type, nullptr);
}
Asset* find_asset_by_path(String path) EXPORT
{
AssetHandle handle = find_asset_handle(path);
if (handle == ASSET_HANDLE_INVALID) return nullptr;
return get_asset(handle);
}
i32 get_asset_type(AssetHandle handle) EXPORT
{
ASSERT(handle != ASSET_HANDLE_INVALID);
return assets.loaded[handle.index].type_id;
}
Asset* get_asset(AssetHandle handle) EXPORT
{
SArena scratch = tl_scratch_arena();
ASSERT(handle != ASSET_HANDLE_INVALID);
Asset asset = assets.loaded[handle.index];
if (asset.gen != handle.gen) return nullptr;
if (!asset.data) {
FileInfo fi = read_file(asset.path, scratch);
if (!fi.data) {
LOG_ERROR("unable to load asset '%.*s'", STRFMT(asset.path));
return nullptr;
}
if (!load_asset(handle, fi.data, fi.size)) return nullptr;
}
return &assets.loaded[handle.index];
}
String get_asset_path(AssetHandle handle) EXPORT
{
ASSERT(handle != ASSET_HANDLE_INVALID);
Asset &asset = assets.loaded[handle.index];
if (asset.gen != handle.gen) return {};
return asset.path;
}
AssetHandle gen_asset_handle() EXPORT
{
if (assets.free_slots.count > 0) return array_pop(&assets.free_slots);
i32 i = array_add(&assets.loaded, { .gen = 1 });
AssetHandle handle{ .index = i, .gen = 1 };
//LOG_INFO("generated asset handle: { %d %d }", handle.index, handle.gen);
return handle;
}
AssetHandle create_asset(AssetHandle handle, Asset asset) EXPORT
{
//LOG_INFO("creating asset '%.*s', handle: { %d %d }", STRFMT(asset.path), handle.index, handle.gen);
asset.identifier = filename_of(asset.path);
if (assets.loaded.count <= handle.index) {
ASSERT(handle.gen == 1);
asset.gen = 1;
array_add(&assets.loaded, asset);
} else {
assets.loaded[handle.index] = asset;
assets.loaded[handle.index].gen = handle.gen;
}
return handle;
}
AssetHandle create_asset(Asset asset) EXPORT
{
AssetHandle handle = gen_asset_handle();
return create_asset(handle, asset);
}
AssetHandle create_asset(AssetHandle handle, String path, i32 type_id, void *data) EXPORT
{
Asset asset{
.path = duplicate_string(path, mem_dynamic),
.type_id = type_id,
.data = data,
};
if (file_exists(path)) asset.last_saved = file_modified_timestamp(path);
else asset.last_modified = wall_timestamp();
return create_asset(handle, asset);
}
AssetHandle create_asset(String path, i32 type_id, void *data) EXPORT
{
AssetHandle handle = gen_asset_handle();
return create_asset(handle, path, type_id, data);
}
AssetHandle restore_removed_asset(String path) EXPORT
{
for (i32 i = 0; i < assets.removed.count; i++) {
auto &it = assets.loaded[assets.removed[i].index];
if (it.path == path) {
AssetHandle handle = assets.removed[i];
array_remove_unsorted(&assets.removed, i);
return handle;
}
}
return ASSET_HANDLE_INVALID;
}
void restore_removed_asset(AssetHandle handle) EXPORT
{
for (i32 i = 0; i < assets.removed.count; i++) {
if (assets.removed[i] == handle) {
array_remove_unsorted(&assets.removed, i);
return;
}
}
}
void remove_asset(AssetHandle handle) EXPORT
{
if (handle.gen != assets.loaded[handle.index].gen) {
LOG_ERROR("asset generation id mismatch between handle and loaded asset");
return;
}
// NOTE(jesper): actual removal and reuse of assets and asset handles is currently disabled because we rely on stable asset handles in the editor undo/redo history stack. This needs to be either re-thoughts, or perhaps turned into a flag that is disabled/enabled depending on whether the editor is enabled
//array_add(&assets.free_slots, { handle.index, handle.gen+1 });
array_add(&assets.removed, handle);
//assets.loaded[handle.index].gen++;
}
bool asset_path_used(String path) EXPORT
{
for (auto it : assets.loaded) if (it.path == path) return true;
return false;
}
bool load_asset(AssetHandle handle, u8 *contents, i32 size) EXPORT
{
String path = assets.loaded[handle.index].path;
if (assets.loaded[handle.index].gen == handle.gen &&
assets.loaded[handle.index].data &&
assets.loaded[handle.index].last_saved == file_modified_timestamp(path))
{
LOG_INFO("asset '%.*s' is up to date", STRFMT(path));
return true;
}
String ext = extension_of(path);
asset_load_t *load_proc = map_find(&assets.load_procs, ext);
if (load_proc == nullptr) {
LOG_ERROR("unknown load proc for asset '%.*s' with ext: '%.*s'", STRFMT(path), STRFMT(ext));
return false;
}
LOG_INFO("loading asset '%.*s', handle: { %d %d }", STRFMT(path), handle.index, handle.gen);
Asset asset = assets.loaded[handle.index];
if (void *data = (*load_proc)(handle, asset.data, asset.identifier, contents, size); data) {
Asset *asset = &assets.loaded[handle.index];
asset->data = data;
asset->last_saved = file_modified_timestamp(path);
asset->last_modified = 0;
return true;
}
return false;
}
AssetHandle load_asset(String path) EXPORT
{
SArena scratch = tl_scratch_arena();
String ext = extension_of(path);
asset_load_t *load_proc = map_find(&assets.load_procs, ext);
if (load_proc == nullptr || *load_proc == nullptr) {
LOG_ERROR("unknown load proc for asset '%.*s' with ext: '%.*s'", STRFMT(path), STRFMT(ext));
return ASSET_HANDLE_INVALID;
}
String apath = resolve_asset_path(path, scratch);
if (!apath) {
LOG_ERROR("unable to resolve asset path: %.*s", STRFMT(path));
return ASSET_HANDLE_INVALID;
}
FileInfo file = read_file(apath, mem_dynamic);
return load_asset(apath, file.data, file.size);
}
AssetHandle load_asset(String path, u8 *contents, i32 size) EXPORT
{
String ext = extension_of(path);
asset_load_t *load_proc = map_find(&assets.load_procs, ext);
if (load_proc == nullptr || *load_proc == nullptr) {
LOG_ERROR("unknown load proc for asset '%.*s' with ext: '%.*s'", STRFMT(path), STRFMT(ext));
return ASSET_HANDLE_INVALID;
}
AssetHandle handle = find_loaded_asset(path);
if (handle == ASSET_HANDLE_INVALID) {
i32 *type = map_find(&assets.types, ext);
if (!type) LOG_ERROR("unknown asset type for asset '%.*s' with ext: '%.*s'", STRFMT(path), STRFMT(ext));
handle = create_asset(path, *type, nullptr);
}
if (!load_asset(handle, contents, size)) return ASSET_HANDLE_INVALID;
return handle;
}
bool ensure_loaded(AssetHandle handle) EXPORT
{
if (handle.gen != assets.loaded[handle.index].gen) {
LOG_ERROR("asset generation id mismatch between handle and loaded asset");
return false;
}
if (!assets.loaded[handle.index].data) {
SArena scratch = tl_scratch_arena();
String path = assets.loaded[handle.index].path;
FileInfo fi = read_file(path, scratch);
if (!fi.data) {
LOG_ERROR("unable to load asset '%.*s'", STRFMT(path));
return false;
}
if (!load_asset(handle, fi.data, fi.size)) return false;
}
return true;
}
void dirty_asset(AssetHandle handle) EXPORT
{
ASSERT(handle.gen == assets.loaded[handle.index].gen);
assets.loaded[handle.index].last_modified = wall_timestamp();
}
void save_dirty_assets() EXPORT
{
SArena scratch = tl_scratch_arena();
for (auto handle : assets.removed) {
auto &it = assets.loaded[handle.index];
if (it.last_modified > it.last_saved) {
LOG_INFO("removing file for deleted asset: %.*s", STRFMT(it.path));
remove_file(it.path);
it.last_saved = wall_timestamp();
it.last_modified = 0;
}
}
StringBuilder stream{ .alloc = scratch };
for (auto it : iterator(assets.loaded)) {
if (it->last_modified > it->last_saved) {
reset_string_builder(&stream);
asset_save_t *save_proc = map_find(&assets.save_procs, extension_of(it->path));
if (save_proc == nullptr || *save_proc == nullptr) {
LOG_ERROR("unable to save asset '%.*s', unknown save procedure", STRFMT(it->path));
continue;
}
LOG_INFO("saving modified asset: %.*s", STRFMT(it->path));
if ((*save_proc)(AssetHandle{ it.index, it->gen }, &stream, it->data)) {
write_file(it->path, &stream);
it->last_saved = file_modified_timestamp(it->path);
it->last_modified = 0;
}
}
}
}
Array<AssetHandle> get_unsaved_assets(Allocator mem) EXPORT
{
DynamicArray<AssetHandle> result{ .alloc = mem };
for (i32 i = 0; i < assets.loaded.count; i++) {
if (assets.loaded[i].last_modified > assets.loaded[i].last_saved) {
array_add(&result, { .index = i, .gen = assets.loaded[i].gen });
}
}
return result;
}
void save_asset(AssetHandle handle) EXPORT
{
SArena scratch = tl_scratch_arena();
StringBuilder stream{ .alloc = scratch };
Asset *it = get_asset(handle);
asset_save_t *save_proc = map_find(&assets.save_procs, extension_of(it->path));
if (save_proc == nullptr || *save_proc == nullptr) {
LOG_ERROR("unable to save asset '%.*s', unknown save procedure", STRFMT(it->path));
return;
}
if ((*save_proc)(handle, &stream, it->data)) {
write_file(it->path, &stream);
it->last_saved = file_modified_timestamp(it->path);
it->last_modified = 0;
}
}
String resolve_asset_path(String path, Allocator mem) EXPORT
{
SArena scratch = tl_scratch_arena(mem);
if (file_exists(path) && !is_directory(path)) {
return absolute_path(path, mem);
}
for (auto f : assets.folders) {
String s = join_path(f, path, scratch);
if (file_exists(s) && !is_directory(s)) return absolute_path(s, mem);
}
return "";
}
String normalise_asset_path(String path, Allocator mem) EXPORT
{
String short_path = resolve_asset_path(path, mem);
for (auto f : assets.folders) {
if (starts_with(path, f)) {
String s = slice(path, f.length+1);
if (s.length < short_path.length) short_path = s;
}
}
for (i32 i = 0; i < short_path.length; i++) {
if (short_path[i] == '\\') short_path[i] = '/';
}
return short_path;
}
Array<String> list_asset_files(Allocator mem) EXPORT
{
DynamicArray<String> files{ .alloc = mem };
for (auto f : assets.folders) {
i32 c = files.count;
list_files(&files, f, mem, FILE_LIST_ABSOLUTE | FILE_LIST_RECURSIVE);
for (i32 i = c; i < files.count; i++) {
if (map_find(&assets.types, extension_of(files[i]))) continue;
array_remove_unsorted(&files, i--);
}
// NOTE(jesper): handle the case of an asset folder being a subfolder to another. This should probably be handled in a much better way to avoid a lot of re-iteration of the filesystem, nevermind the allocation of the resolved file paths
for (i32 i = 0; i < c; i++) {
for (i32 j = c; j < files.count; j++) {
if (files[i] == files[j]) array_remove_unsorted(&files, j--);
}
}
}
return files;
}
Array<String> list_asset_files(i32 type, Allocator mem) EXPORT
{
DynamicArray<String> files{ .alloc = mem };
for (auto f : assets.folders) {
i32 c = files.count;
list_files(&files, f, mem, FILE_LIST_ABSOLUTE | FILE_LIST_RECURSIVE);
// NOTE(jesper): dumb type filtering. The asset system probably ought to keep track of the filesystem better on its own, so these kinds of queries can be O(1), or close to it
for (i32 i = c; i < files.count; i++) {
String ext = extension_of(files[i]);
i32 *t = map_find(&assets.types, ext);
if (t && *t == type) continue;
array_remove_unsorted(&files, i--);
}
// NOTE(jesper): handle the case of an asset folder being a subfolder to another. This should probably be handled in a much better way to avoid a lot of re-iteration of the filesystem, nevermind the allocation of the resolved file paths
for (i32 i = 0; i < c; i++) {
for (i32 j = c; j < files.count; j++) {
if (files[i] == files[j]) array_remove_unsorted(&files, j--);
}
}
}
return files;
}
Array<String> list_asset_files(String ext, Allocator mem) EXPORT
{
DynamicArray<String> files{ .alloc = mem };
for (auto dir : assets.folders) {
i32 c = files.count;
list_files(&files, dir, ext, mem, FILE_LIST_ABSOLUTE | FILE_LIST_RECURSIVE);
// NOTE(jesper): handle the case of an asset folder being a subfolder to another. This should probably be handled in a much better way to avoid a lot of re-iteration of the filesystem, nevermind the allocation of the resolved file paths
for (i32 i = 0; i < c; i++) {
for (i32 j = c; j < files.count; j++) {
if (files[i] == files[j]) array_remove_unsorted(&files, j--);
}
}
}
return files;
}
void* load_string_asset(AssetHandle /*handle*/, void *existing, String /*identifier*/, u8 *data, i32 size) EXPORT
{
if (existing) FREE(mem_dynamic, existing);
return ALLOC_T(mem_dynamic, String) { (char*)data, size };
}