-
Notifications
You must be signed in to change notification settings - Fork 0
/
rjd_resource_loader.h
248 lines (201 loc) · 9.23 KB
/
rjd_resource_loader.h
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
#pragma once
#define RJD_RESOURCE_LOADER_H 1
struct rjd_resource_loader;
typedef void rjd_resource_loader_destroy_func(struct rjd_resource_loader* loader);
typedef struct rjd_result rjd_resource_loader_get_type_func(struct rjd_resource_loader* loader, struct rjd_resource_id id, struct rjd_resource_type_id* out);
typedef struct rjd_result rjd_resource_loader_load_func(struct rjd_resource_loader* loader, struct rjd_resource_id id, struct rjd_mem_allocator* allocator, struct rjd_istream* out);
struct rjd_resource_loader
{
void* impl;
rjd_resource_loader_destroy_func* destroy_func;
rjd_resource_loader_get_type_func* get_type_func;
rjd_resource_loader_load_func* load_func;
};
enum rjd_resource_loader_type
{
RJD_RESOURCE_LOADER_TYPE_FILESYSTEM, // recursively crawls a root directory to find all resource files
//RJD_RESOURCE_LOADER_TYPE_REMOTE, // requests manifest from a given http endpoint
//RJD_RESOURCE_LOADER_TYPE_PACK, // reads manifest embedded inside given pack files
};
struct rjd_resource_extension_to_type_id
{
struct rjd_resource_type_id type;
const char* extension;
};
struct rjd_resource_loader_desc
{
enum rjd_resource_loader_type type;
struct rjd_mem_allocator* allocator;
union
{
struct {
const char* root;
const struct rjd_resource_extension_to_type_id* type_mappings;
uint32_t type_mappings_count;
uint32_t manifest_capacity;
} filesystem;
};
};
struct rjd_result rjd_resource_loader_create(struct rjd_resource_loader* out, struct rjd_resource_loader_desc desc);
static inline void rjd_resource_loader_destroy(struct rjd_resource_loader* loader);
static inline struct rjd_result rjd_resource_loader_get_type(struct rjd_resource_loader* loader, struct rjd_resource_id id, struct rjd_resource_type_id* out);
static inline struct rjd_result rjd_resource_loader_load(struct rjd_resource_loader* loader, struct rjd_resource_id id, struct rjd_mem_allocator* allocator, struct rjd_istream* out);
////////////////////////////////////////////////////////////////////////////////
// Inline implementation
static inline void rjd_resource_loader_destroy(struct rjd_resource_loader* loader)
{
RJD_ASSERT(loader);
RJD_ASSERT(loader->destroy_func);
loader->destroy_func(loader);
}
static inline struct rjd_result rjd_resource_loader_get_type(struct rjd_resource_loader* loader, struct rjd_resource_id id, struct rjd_resource_type_id* out)
{
RJD_ASSERT(loader);
RJD_ASSERT(loader->get_type_func);
return loader->get_type_func(loader, id, out);
}
static inline struct rjd_result rjd_resource_loader_load(struct rjd_resource_loader* loader, struct rjd_resource_id id, struct rjd_mem_allocator* allocator, struct rjd_istream* out)
{
RJD_ASSERT(loader);
RJD_ASSERT(loader->load_func);
return loader->load_func(loader, id, allocator, out);
}
#if RJD_IMPL
struct rjd_resource_manifest_entry_filesystem
{
struct rjd_resource_id id;
struct rjd_resource_type_id type;
struct rjd_strref* path;
};
// private types
struct rjd_resource_loader_filesystem
{
enum rjd_resource_loader_type debug_sentinel;
struct rjd_strpool strpool;
struct rjd_resource_extension_to_type_id* type_mappings;
struct rjd_strref* root;
struct rjd_resource_manifest_entry_filesystem* manifest_entries; // TODO could be sorted by resource id
};
// static helpers
static int32_t RJD_COMPILER_MSVC_ONLY(__cdecl) rjd_resource_loader_manifest_entry_comparer(const void* a, const void* b);
static struct rjd_resource_loader_filesystem* rjd_resource_loader_to_filesystem_loader(struct rjd_resource_loader* loader);
static void rjd_resource_loader_filesystem_destroy(struct rjd_resource_loader* loader);
static struct rjd_result rjd_resource_loader_filesystem_get_type(struct rjd_resource_loader* loader, struct rjd_resource_id id, struct rjd_resource_type_id* out);
static struct rjd_result rjd_resource_loader_filesystem_load(struct rjd_resource_loader* loader, struct rjd_resource_id id, struct rjd_mem_allocator* allocator, struct rjd_istream* out);
// public implementation
struct rjd_result rjd_resource_loader_create(struct rjd_resource_loader* out, struct rjd_resource_loader_desc desc)
{
void* impl_any = NULL;
if (desc.type == RJD_RESOURCE_LOADER_TYPE_FILESYSTEM)
{
uint32_t manifest_capacity = desc.filesystem.manifest_capacity == 0 ? 1024 : desc.filesystem.manifest_capacity;
struct rjd_resource_loader_filesystem* impl = rjd_mem_alloc(struct rjd_resource_loader_filesystem, desc.allocator);
impl->debug_sentinel = RJD_RESOURCE_LOADER_TYPE_FILESYSTEM;
impl->strpool = rjd_strpool_init(desc.allocator, 64);
impl->type_mappings = rjd_array_alloc(struct rjd_resource_extension_to_type_id, desc.filesystem.type_mappings_count, desc.allocator);
impl->root = rjd_strpool_add(&impl->strpool, desc.filesystem.root);
impl->manifest_entries = rjd_array_alloc(struct rjd_resource_manifest_entry_filesystem, manifest_capacity, desc.allocator);
for (uint32_t i = 0; i < desc.filesystem.type_mappings_count; ++i)
{
rjd_array_push(impl->type_mappings, desc.filesystem.type_mappings[i]);
}
const size_t length_root_path = strlen(desc.filesystem.root) + 1; // +1 to skip path separator
struct rjd_path_enumerator_state path_enumerator = rjd_path_enumerate_create(desc.filesystem.root, desc.allocator, RJD_PATH_ENUMERATE_MODE_RECURSIVE);
for (const char* path = rjd_path_enumerate_next(&path_enumerator); path != NULL; path = rjd_path_enumerate_next(&path_enumerator))
{
const char* relative_path = path + length_root_path;
const char* extension = rjd_path_str_extension(relative_path);
if (extension)
{
struct rjd_resource_type_id type = {0};
for (uint32_t i = 0; i < rjd_array_count(impl->type_mappings); ++i)
{
if (strcmp(impl->type_mappings[i].extension, extension) == 0) // TODO case-insensitive compare
{
type = impl->type_mappings[i].type;
}
}
if (type.hash.hash.value != 0)
{
struct rjd_strref* pathref = rjd_strpool_add(&impl->strpool, relative_path);
struct rjd_resource_manifest_entry_filesystem entry = {
.id = rjd_strhash_init(relative_path),
.type = type,
.path = pathref,
};
rjd_array_push(impl->manifest_entries, entry);
}
}
}
rjd_path_enumerate_destroy(&path_enumerator);
// since the enumerator doesn't return entries in a sorted manner, make sure the manifest order
// is deterministic
rjd_array_sort(impl->manifest_entries, rjd_resource_loader_manifest_entry_comparer);
impl_any = impl;
}
else
{
return RJD_RESULT("unimplemented support for this type of loader");
}
struct rjd_resource_loader loader = {
.impl = impl_any,
.destroy_func = rjd_resource_loader_filesystem_destroy,
.get_type_func = rjd_resource_loader_filesystem_get_type,
.load_func = rjd_resource_loader_filesystem_load,
};
*out = loader;
return RJD_RESULT_OK();
}
// private implementation
static int32_t RJD_COMPILER_MSVC_ONLY(__cdecl) rjd_resource_loader_manifest_entry_comparer(const void* a, const void* b)
{
const struct rjd_resource_manifest_entry_filesystem* aa = a;
const struct rjd_resource_manifest_entry_filesystem* bb = b;
const char* path_a = rjd_strref_str(aa->path);
const char* path_b = rjd_strref_str(bb->path);
return strcmp(path_a, path_b);
}
static struct rjd_resource_loader_filesystem* rjd_resource_loader_to_filesystem_loader(struct rjd_resource_loader* loader)
{
RJD_ASSERT(loader);
struct rjd_resource_loader_filesystem* impl = (struct rjd_resource_loader_filesystem*)loader->impl;
RJD_ASSERT(impl->debug_sentinel == RJD_RESOURCE_LOADER_TYPE_FILESYSTEM);
return impl;
}
static void rjd_resource_loader_filesystem_destroy(struct rjd_resource_loader* loader)
{
struct rjd_resource_loader_filesystem* impl = rjd_resource_loader_to_filesystem_loader(loader);
rjd_array_free(impl->manifest_entries);
rjd_array_free(impl->type_mappings);
rjd_strpool_free(&impl->strpool);
rjd_mem_free(impl);
memset(loader, 0, sizeof(*loader));
}
static struct rjd_result rjd_resource_loader_filesystem_get_type(struct rjd_resource_loader* loader, struct rjd_resource_id id, struct rjd_resource_type_id* out)
{
struct rjd_resource_loader_filesystem* impl = rjd_resource_loader_to_filesystem_loader(loader);
for (uint32_t i = 0; i < rjd_array_count(impl->manifest_entries); ++i) {
if (rjd_resource_id_equals(impl->manifest_entries[i].id, id)) {
*out = impl->manifest_entries[i].type;
return RJD_RESULT_OK();
}
}
return RJD_RESULT("Resource id not found in loader manifest.");
}
static struct rjd_result rjd_resource_loader_filesystem_load(struct rjd_resource_loader* loader, struct rjd_resource_id id, struct rjd_mem_allocator* allocator, struct rjd_istream* out)
{
struct rjd_resource_loader_filesystem* impl = rjd_resource_loader_to_filesystem_loader(loader);
for (uint32_t i = 0; i < rjd_array_count(impl->manifest_entries); ++i) {
if (rjd_resource_id_equals(impl->manifest_entries[i].id, id)) {
const char* root = rjd_strref_str(impl->root);
const char* relative_path = rjd_strref_str(impl->manifest_entries[i].path);
struct rjd_path fullpath = rjd_path_init();
rjd_path_join_str(&fullpath, root);
rjd_path_join_str(&fullpath, relative_path);
*out = rjd_istream_from_file(rjd_path_get(&fullpath), allocator, 1024 * 512);
return out->result;
}
}
return RJD_RESULT("Resource id not found in loader manifest.");
}
#endif // RJD_IMPL