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
|
#include "serialization.hpp"
#include "font.hpp"
#include "pk.h"
#include "player-input.hpp"
#include "serialization-component.hpp"
#include "serialization-camera.hpp"
#include "camera.hpp"
#include "ecs.hpp"
#include "serialization-font.hpp"
#include "serialization-input.hpp"
#include "serialization-static-ui.hpp"
#include "static-ui.hpp"
bool srlztn_mapping_find_first_handle_by_uuid(void *handle, void *mapping) {
srlztn_ecs_mapping *inst_mapping = reinterpret_cast<srlztn_ecs_mapping *>(mapping);
return inst_mapping->serialized_uuid == *reinterpret_cast<pk_uuid *>(handle);
}
bool srlztn_kvec_find_first_by_handle(void *handle, void *container) {
pke_kve_container *kvec = reinterpret_cast<pke_kve_container*>(container);
return kvec->srlztn_handle == *reinterpret_cast<pk_handle*>(handle);
}
srlztn_serialize_helper *pke_serialize_init(pk_membucket *bkt) {
srlztn_serialize_helper *helper = pk_new<srlztn_serialize_helper>(bkt);
helper->bkt = bkt;
helper->kvp_containers.bkt = bkt;
pk_arr_reserve(&helper->kvp_containers, 1);
pk_arr_reset(&helper->kvp_containers);
return helper;
}
srlztn_deserialize_helper *pke_deserialize_init(pke_level *level, pk_membucket *bkt) {
srlztn_deserialize_helper *helper = pk_new<srlztn_deserialize_helper>(bkt);
helper->bkt = bkt;
helper->kvp_containers = {};
helper->kvp_containers.bkt = bkt;
helper->mapping = {};
helper->mapping.bkt = bkt;
helper->level = level;
pk_arr_reserve(&helper->kvp_containers, 1);
pk_arr_reserve(&helper->mapping, 1);
return helper;
}
void pke_serialize_teardown(srlztn_serialize_helper *helper) {
(void)helper;
}
void pke_deserialize_teardown(srlztn_deserialize_helper *helper) {
(void)helper;
}
void pke_serialize_project(srlztn_serialize_helper *h) {
(void)h;
}
void pke_deserialize_project(srlztn_deserialize_helper *h) {
(void)h;
}
void pke_serialize_project_to_stream(std::ostream &o, srlztn_serialize_helper *helper) {
(void)o;
(void)helper;
}
void pke_deserialize_project_from_stream(std::istream &i, srlztn_deserialize_helper *helper) {
(void)i;
(void)helper;
}
void pke_serialize_scene(srlztn_serialize_helper *h) {
bool b;
pk_bkt_arr *bkt_arr_instance;
pk_iter_t<CompInstance> iter_instance{};
pk_iter_t<PkeCamera> iter_cam{};
pk_arr_t<pke_input_set> &sets = pke_input_get_input_sets();
for (uint32_t i = 0; i < sets.next; ++i) {
if (PK_HAS_FLAG(sets[i].flags, PKE_INPUT_ACTION_SET_FLAG_DO_NOT_SERIALIZE)) {
continue;
}
pke_serialize_input_set(h, &sets[i]);
}
uint64_t idx_unused;
FontType *fonts = FontType_GetFonts(idx_unused);
for (FontTypeIndex_T b = 0; b < PKE_FONT_MAX_FONT_TYPES; ++b) {
if ((idx_unused & (1llu << b)) != 0) continue;
FontType *ft = &fonts[b];
pk_bkt_arr_t<FontRender>::FN_Iter iter_fn{};
iter_fn.func = [&h](FontRender *fr) {
pke_serialize_font_render(h, fr);
};
pk_bkt_arr_iterate(&ft->renders, iter_fn.invoke, &iter_fn);
}
pke_ui_box_count_T box_count;
pke_ui_box **ui_boxes = pke_ui_get_root_boxes(&box_count);
for (pke_ui_box_count_T i = 0; i < box_count; ++i) {
if (PK_HAS_FLAG(ui_boxes[i]->entity_flags, ENTITY_FLAG_DO_NOT_SERIALIZE)) {
continue;
}
pke_serialize_ui_box(h, ui_boxes[i]);
}
bkt_arr_instance = ECS_GetInstances();
b = pk_bkt_arr_iter_begin(bkt_arr_instance, &iter_instance);
while (b == true) {
if (PK_HAS_FLAG(iter_instance->comp_instance_flags, COMPONENT_INSTANCE_FLAG_DO_NOT_SERIALIZE)) {
b = pk_bkt_arr_iter_increment(bkt_arr_instance, &iter_instance);
continue;
}
pke_serialize_instance(h, iter_instance);
b = pk_bkt_arr_iter_increment(bkt_arr_instance, &iter_instance);
}
pk_bkt_arr &bkt_arr_cams = PkeCamera_GetPkBktArr();
b = pk_bkt_arr_iter_begin(&bkt_arr_cams, &iter_cam);
while (b == true) {
if (iter_cam->camHandle == CameraHandle_MAX) {
b = pk_bkt_arr_iter_increment(&bkt_arr_cams, &iter_cam);
continue;
}
pke_serialize_camera(h, iter_cam);
b = pk_bkt_arr_iter_increment(&bkt_arr_cams, &iter_cam);
}
}
void pke_deserialize_scene(srlztn_deserialize_helper *h) {
uint32_t i;
pke_kve_container *kvec;
for (i = 0; i < h->kvp_containers.next; ++i) {
kvec = &h->kvp_containers[i];
if (strcmp(kvec->type_code.val, SRLZTN_OBJ_INPUT_SET) == 0) {
pke_deserialize_input_set(h, kvec);
continue;
}
if (strcmp(kvec->type_code.val, SRLZTN_OBJ_FONT_RENDER) == 0) {
pke_deserialize_font_render(h, kvec);
continue;
}
if (strcmp(kvec->type_code.val, SRLZTN_OBJ_UI_BOX) == 0) {
pke_deserialize_ui_box(h, kvec);
continue;
}
if (strcmp(kvec->type_code.val, SRLZTN_OBJ_INSTANCE) == 0) {
pke_deserialize_instance(h, kvec);
continue;
}
if (strcmp(kvec->type_code.val, SRLZTN_OBJ_CAMERA) == 0) {
pke_deserialize_camera(h, kvec);
continue;
}
}
}
void pke_serialize_scene_to_stream(std::ostream &o, srlztn_serialize_helper *h) {
uint32_t i, k;
pke_kve_container *kvec;
pke_kve *kve;
o << SRLZTN_FILE_BEGIN << std::endl;
o << SRLZTN_FILE_VERSION << std::endl;
o << "" << std::endl;
for (i = 0; i < h->kvp_containers.next; ++i) {
kvec = &h->kvp_containers[i];
o << kvec->type_code.val << ":";
o << std::setw(8) << std::setfill('0') << std::setbase(16) << kvec->srlztn_handle.bucketIndex;
o << SRLZTN_HANDLE_SEPARATOR;
o << std::setw(8) << std::setfill('0') << std::setbase(16) << kvec->srlztn_handle.itemIndex;
o << std::setbase(0) << std::setw(0) << std::endl;
for (k = 0; k < kvec->child_handles.next; ++k) {
o << SRLZTN_CHILD_ID;
o << std::setw(8) << std::setfill('0') << std::setbase(16) << kvec->child_handles[k].bucketIndex;
o << SRLZTN_HANDLE_SEPARATOR;
o << std::setw(8) << std::setfill('0') << std::setbase(16) << kvec->child_handles[k].itemIndex;
o << std::setbase(0) << std::setw(0) << std::endl;
}
for (k = 0; k < kvec->arr.next; ++k) {
kve = &kvec->arr[k];
o << kve->key << kve->val << kve->end << std::endl;
}
o << SRLZTN_OBJ_END << std::endl;
}
o << SRLZTN_FILE_END;
}
void pke_deserialize_scene_from_stream(std::istream &i, srlztn_deserialize_helper *h) {
// 0: new object
// 1: new kve
// 2: multiline kve start
// 3: multiline kve in progress
uint8_t state = 0;
size_t read_line_len = 128;
size_t offset = 0;
PK_STN_RES res;
char *read_line = pk_new_arr<char>(read_line_len, h->bkt);
memset(read_line, '\0', read_line_len);
char *s, *rl;
pk_handle handle;
pke_kve_container kvec;
pke_kve kve;
kvec.bkt = h->bkt;
kvec.arr.bkt = h->bkt;
kvec.children.bkt = h->bkt;
kvec.child_handles.bkt = h->bkt;
i.getline(read_line, read_line_len);
assert(strstr(read_line, SRLZTN_FILE_BEGIN) != 0);
i.getline(read_line, read_line_len);
assert(strstr(read_line, SRLZTN_FILE_VERSION) != 0);
while (i.getline(read_line, read_line_len)) {
if (strlen(read_line) == 0 && (state != 1 && state != 2)) continue;
if (strstr(read_line, SRLZTN_FILE_END) == read_line) continue;
// new object
if (state == 0) {
// obj_type:FFFFFFFF!FFFFFFFF
read_line[strchr(read_line, ':') - read_line] = '\0';
offset = strlen(read_line) + 1;
read_line[strchr(read_line + offset, '!') - read_line] = '\0';
kvec.type_code.length = strlen(read_line);
kvec.type_code.reserved = kvec.type_code.length + 1;
s = pk_new_arr<char>(kvec.type_code.reserved, h->bkt);
sprintf(s, "%s", read_line);
kvec.type_code.val = s;
rl = read_line + strlen(read_line) + 1;
res = pk_stn(&handle.bucketIndex, rl, NULL);
assert(res == PK_STN_RES_SUCCESS);
rl += 9;
res = pk_stn(&handle.itemIndex, rl, &s, 16);
assert(res == PK_STN_RES_SUCCESS);
state = 1;
kvec.srlztn_handle = handle;
continue;
}
if (state == 1 && strstr(read_line, SRLZTN_CHILD_ID)) {
rl = read_line + strlen(SRLZTN_CHILD_ID) + 1;
res = pk_stn(&handle.bucketIndex, rl, NULL);
assert(res == PK_STN_RES_SUCCESS);
rl += 9;
res = pk_stn(&handle.itemIndex, rl, &s, 16);
assert(res == PK_STN_RES_SUCCESS);
state = 1;
pk_arr_append_t(&kvec.child_handles, handle);
continue;
} else if (state == 1 && strlen(read_line) > 0) { // kve start
offset = strchr(read_line, ':') - read_line;
s = pk_new_arr<char>(strlen(read_line) + 1);
if (strstr(read_line, "::")) {
offset += 1;
state = 2;
}
sprintf(s, "%.*s", (int)offset+1, read_line);
kve.key = s;
if (state == 2) {
continue;
}
read_line[offset] = '\0';
rl = read_line + offset + 1;
s = pk_new_arr<char>(strlen(rl) + 1);
sprintf(s, "%s", rl);
kve.val = s;
kve.end = SRLZTN_KVE_END;
pk_arr_append_t(&kvec.arr, kve);
continue;
}
// multiline
if (state == 2) {
s = pk_new_arr<char>(strlen(read_line) + 1, h->bkt);
sprintf(s, "%s", read_line);
state = 3;
continue;
}
// multiline end
if (strstr(read_line, SRLZTN_MULTILINE_END)) {
assert(state >= 2);
kve.val = s;
kve.end = SRLZTN_MULTILINE_END;
pk_arr_append_t(&kvec.arr, kve);
state = 1;
continue;
}
if (state == 3) {
int sz = snprintf(NULL, 0, "%s%s", s, read_line);
rl = pk_new_arr<char>(sz+1, h->bkt);
sprintf(rl, "%s%s", s, read_line);
pk_delete_arr<char>(s, strlen(s) + 1, h->bkt);
s = rl;
continue;
}
// end of object
if (state != 2) {
state = 0;
if (kvec.type_code.val != nullptr) {
pk_arr_append_t(&h->kvp_containers, kvec);
}
kvec.type_code.val = nullptr;
kvec.type_code.length = 0;
kvec.type_code.reserved = 0;
kvec.children.next = 0;
kvec.children.reserved = 0;
kvec.children.data = nullptr;
kvec.arr.next = 0;
kvec.arr.reserved = 0;
kvec.arr.data = nullptr;
kvec.child_handles.next = 0;
kvec.child_handles.reserved = 0;
kvec.child_handles.data = nullptr;
}
}
for (uint32_t c = 0; c < h->kvp_containers.next; ++c) {
pke_kve_container *parent = &h->kvp_containers[c];
if (parent->child_handles.next == 0 || parent->child_handles.next == parent->children.next) {
continue;
}
for (uint32_t cc = 0; cc < c; ++cc) {
pke_kve_container *child = &h->kvp_containers[cc];
for (uint32_t ch = 0; ch < parent->child_handles.next; ++ch) {
if (child->srlztn_handle == parent->child_handles[ch]) {
pk_arr_append_t(&parent->children, child);
break;
}
}
}
}
kvec.arr.data = nullptr;
kvec.children.data = nullptr;
kvec.child_handles.data = nullptr;
pk_delete_arr<char>(read_line, read_line_len, h->bkt);
}
void pke_serialize_level(srlztn_serialize_helper *h) {
(void)h;
}
void pke_deserialize_level(srlztn_deserialize_helper *h) {
(void)h;
}
void pke_serialize_level_to_stream(std::ostream &o, srlztn_serialize_helper *helper) {
(void)o;
(void)helper;
}
void pke_deserialize_level_from_stream(std::istream &i, srlztn_deserialize_helper *helper) {
(void)i;
(void)helper;
}
|