summaryrefslogtreecommitdiff
path: root/src/serialization.cpp
diff options
context:
space:
mode:
authorJonathan Bradley <jcb@pikum.xyz>2025-05-06 13:12:24 -0400
committerJonathan Bradley <jcb@pikum.xyz>2025-05-06 13:12:24 -0400
commit32968050f0b34fdabfcc2a4fb5601d4be361bbd2 (patch)
treeacef384a2156a16d4d506c37f13f79d454a4a6e9 /src/serialization.cpp
parentef37d054dfe5812efa9eefb4b9b18621fdabac25 (diff)
pke: major serialization refactor, first-pass
Diffstat (limited to 'src/serialization.cpp')
-rw-r--r--src/serialization.cpp235
1 files changed, 209 insertions, 26 deletions
diff --git a/src/serialization.cpp b/src/serialization.cpp
index b41c70d..bc5fb44 100644
--- a/src/serialization.cpp
+++ b/src/serialization.cpp
@@ -1,5 +1,6 @@
#include "serialization.hpp"
+#include "pk.h"
#include "serialization-component.hpp"
#include "serialization-camera.hpp"
#include "camera.hpp"
@@ -9,21 +10,27 @@ bool srlztn_mapping_find_first_handle_by_uuid(void *handle, void *mapping) {
srlztn_instance_mapping *inst_mapping = reinterpret_cast<srlztn_instance_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);
// TODO custom allocator
- helper->o = {};
+ // helper->o = {};
helper->bkt = bkt;
+ helper->kvp_containers.bkt = bkt;
return helper;
}
srlztn_deserialize_helper *pke_deserialize_init(pk_membucket *bkt) {
srlztn_deserialize_helper *helper = pk_new<srlztn_deserialize_helper>(bkt);
helper->bkt = bkt;
- helper->read_line = nullptr;
+ // helper->read_line = nullptr;
// TODO custom allocator
- helper->mapping = {bkt};
+ // helper->mapping = {bkt};
+ helper->kvp_containers.bkt = bkt;
return helper;
}
@@ -35,19 +42,27 @@ void pke_deserialize_teardown(srlztn_deserialize_helper *helper) {
(void)helper;
}
-void pke_serialize_file_project(srlztn_serialize_helper *h) {
+void pke_serialize_project(srlztn_serialize_helper *h) {
(void)h;
}
-
-void pke_deserialize_file_project(srlztn_deserialize_helper *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_file_scene(srlztn_serialize_helper *h) {
- h->o << SRLZTN_FILE_BEGIN << std::endl;
- h->o << SRLZTN_FILE_VERSION << std::endl;
- h->o << "" << std::endl;
-
+void pke_serialize_scene(srlztn_serialize_helper *h) {
+ pke_kve_container kvec = {};
+ kvec.bkt = h->bkt;
+ kvec.arr.bkt = h->bkt;
+ kvec.children.bkt = h->bkt;
+ kvec.type_code = cstring_to_pk_cstr(SRLZTN_OBJ_INSTANCE);
pk_handle_bucket_index_T instanceBucketCount = ECS_GetInstances_BucketCount();
for (pk_handle_bucket_index_T b = 0; b < instanceBucketCount; ++b) {
pk_handle_item_index_T count;
@@ -59,12 +74,12 @@ void pke_serialize_file_scene(srlztn_serialize_helper *h) {
if (PK_HAS_FLAG(instance.comp_instance_flags, COMPONENT_INSTANCE_FLAG_DO_NOT_SERIALIZE)) {
continue;
}
- h->o << SRLZTN_OBJ_INSTANCE << std::endl;
pke_serialize_instance(h, &instance);
- h->o << SRLZTN_OBJ_END << std::endl;
+ pk_arr_append_t(&h->kvp_containers, kvec);
}
}
+ kvec.type_code = cstring_to_pk_cstr(SRLZTN_OBJ_CAMERA);
pk_handle_bucket_index_T cameraBucketCount = PkeCamera_GetBucketCount();
for (pk_handle_bucket_index_T b = 0; b < cameraBucketCount; ++b) {
pk_handle_item_index_T count;
@@ -73,34 +88,202 @@ void pke_serialize_file_scene(srlztn_serialize_helper *h) {
const auto &cam = cameras[i];
if (cam.handle == CameraHandle_MAX)
continue;
- h->o << SRLZTN_OBJ_CAMERA << std::endl;
pke_serialize_camera(h, &cam);
- h->o << SRLZTN_OBJ_END << std::endl;
+ pk_arr_append_t(&h->kvp_containers, kvec);
+ }
+ }
+}
+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(SRLZTN_OBJ_INSTANCE, kvec->type_code.val) == 0) {
+ pke_deserialize_instance(h, kvec);
+ continue;
+ }
+ if (strcmp(SRLZTN_OBJ_CAMERA, kvec->type_code.val) == 0) {
+ pke_deserialize_camera(h, kvec);
+ continue;
}
}
- h->o << SRLZTN_FILE_END << std::endl;
}
-void pke_deserialize_file_scene(srlztn_deserialize_helper *h) {
- h->read_line = pk_new<char>(h->read_line_len, h->bkt);
- memset(h->read_line, '\0', h->read_line_len);
+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;
- while (h->i->getline(h->read_line, h->read_line_len)) {
- if (strcmp(SRLZTN_OBJ_INSTANCE, h->read_line) == 0) {
- pke_deserialize_instance(h);
+ 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->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->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 << std::endl;
+}
+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<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;
+
+ 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) 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<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 (strcmp(SRLZTN_OBJ_CAMERA, h->read_line) == 0) {
- pke_deserialize_camera(h);
+ 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<char>(strlen(read_line) + 1);
+ sprintf(s, "%.*s", (int)offset+1, read_line);
+ kve.key = s;
+ if (strstr(read_line, "::")) {
+ state = 2;
+ continue;
+ }
+ read_line[offset] = '\0';
+ rl = read_line + offset + 1;
+ s = pk_new<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<char>(strlen(read_line) + 1, h->bkt);
+ sprintf(s, "%s", read_line);
+ state = 3;
+ continue;
+ }
+ if (state == 3) {
+ rl = pk_new<char>(strlen(s) + strlen(read_line) + 1, h->bkt);
+ sprintf(rl, "%s%s", s, read_line);
+ pk_delete<char>(s, strlen(s) + 1, h->bkt);
+ s = rl;
continue;
}
+ // multiline end
+ if (strstr(read_line, SRLZTN_MULTILINE_END)) {
+ assert(state >= 2);
+ kve.end = SRLZTN_MULTILINE_END;
+ pk_arr_append_t(&kvec.arr, kve);
+ state = 1;
+ 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;
+ pk_arr_reset(&kvec.arr);
+ pk_arr_reset(&kvec.children);
+ }
}
+
+ 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;
+ }
+ }
+ }
+ }
+
+ pk_delete<char>(read_line, read_line_len, h->bkt);
}
-void pke_serialize_file_level(srlztn_serialize_helper *h) {
+void pke_serialize_level(srlztn_serialize_helper *h) {
(void)h;
}
-void pke_deserialize_file_level(srlztn_deserialize_helper *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;
+}