From a02c7589c6c9e902c59a632aa650635336fe648c Mon Sep 17 00:00:00 2001 From: Jonathan Bradley Date: Wed, 2 Apr 2025 20:46:41 -0400 Subject: pke: checkpoint: major serialization refactor --- src/serialization.cpp | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 src/serialization.cpp (limited to 'src/serialization.cpp') diff --git a/src/serialization.cpp b/src/serialization.cpp new file mode 100644 index 0000000..d7dd312 --- /dev/null +++ b/src/serialization.cpp @@ -0,0 +1,102 @@ + +#include "serialization.hpp" +#include "serialization-component.hpp" +#include "serialization-camera.hpp" +#include "camera.hpp" +#include "ecs.hpp" + +srlztn_serialize_helper *pke_serialize_init(pk_membucket *bkt) { + srlztn_serialize_helper *helper = pk_new(bkt); + // TODO custom allocator + helper->o = {}; + helper->bkt = bkt; + return helper; +} + +srlztn_deserialize_helper *pke_deserialize_init(pk_membucket *bkt) { + srlztn_deserialize_helper *helper = pk_new(bkt); + helper->bkt = bkt; + helper->read_line = nullptr; + // TODO custom allocator + helper->i = {}; + helper->mapping = {bkt}; + 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_file_project(srlztn_serialize_helper *h) { + (void)h; +} + +void pke_deserialize_file_project(srlztn_deserialize_helper *h) { + (void)h; +} + +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; + + 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; + auto *instances = ECS_GetInstances(b, count); + for (pk_handle_item_index_T i = 0; i < count; ++i) { + const auto &instance = instances[i]; + if (instance.entHandle == EntityHandle_MAX) + continue; + if (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_handle_bucket_index_T cameraBucketCount = PkeCamera_GetBucketCount(); + for (pk_handle_bucket_index_T b = 0; b < cameraBucketCount; ++b) { + pk_handle_item_index_T count; + auto *cameras = PkeCamera_GetCameras(b, count); + for (pk_handle_item_index_T i = 0; i < count; ++i) { + 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; + } + } + + h->o << SRLZTN_FILE_END << std::endl; +} +void pke_deserialize_file_scene(srlztn_deserialize_helper *h) { + h->read_line = pk_new(h->read_line_len, h->bkt); + memset(h->read_line, '\0', h->read_line_len); + + while (h->i.getline(h->read_line, h->read_line_len)) { + if (strcmp(SRLZTN_OBJ_INSTANCE, h->read_line) == 0) { + pke_deserialize_instance(h); + continue; + } + if (strcmp(SRLZTN_OBJ_CAMERA, h->read_line) == 0) { + pke_deserialize_camera(h); + continue; + } + } +} + +void pke_serialize_file_level(srlztn_serialize_helper *h) { + (void)h; +} +void pke_deserialize_file_level(srlztn_deserialize_helper *h) { + (void)h; +} + -- cgit v1.2.3