diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2025-05-06 13:12:24 -0400 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2025-05-06 13:12:24 -0400 |
| commit | 32968050f0b34fdabfcc2a4fb5601d4be361bbd2 (patch) | |
| tree | acef384a2156a16d4d506c37f13f79d454a4a6e9 /src/serialization-component.cpp | |
| parent | ef37d054dfe5812efa9eefb4b9b18621fdabac25 (diff) | |
pke: major serialization refactor, first-pass
Diffstat (limited to 'src/serialization-component.cpp')
| -rw-r--r-- | src/serialization-component.cpp | 282 |
1 files changed, 170 insertions, 112 deletions
diff --git a/src/serialization-component.cpp b/src/serialization-component.cpp index 8867339..7514a14 100644 --- a/src/serialization-component.cpp +++ b/src/serialization-component.cpp @@ -10,89 +10,99 @@ #include "pk.h" -#define SRLZTN_FLOAT_WIDTH 10 - -bool pke_serialize_inst_pos(srlztn_serialize_helper *h, const glm::vec3 pos, const glm::quat quat_rot, const glm::vec3 scale) { +pk_handle pke_serialize_inst_pos(srlztn_serialize_helper *h, const glm::vec3 pos, const glm::quat quat_rot, const glm::vec3 scale) { + char *s; + int len; + pke_kve kve{}; + pke_kve_container kvec{}; + kvec.srlztn_handle = h->handle_head; + kvec.type_code = cstring_to_pk_cstr(SRLZTN_OBJ_INSTANCE_POSITION); + kvec.bkt = h->bkt; + kvec.arr.bkt = h->bkt; + kvec.children.bkt = h->bkt; + kvec.child_handles.bkt = h->bkt; + h->handle_head.itemIndex++; if (pos != glm::vec3(0)) { - h->o << SRLZTN_POSROT_POS << "[" - << std::setw(SRLZTN_FLOAT_WIDTH) << pos[0] << "," - << std::setw(SRLZTN_FLOAT_WIDTH) << pos[1] << "," - << std::setw(SRLZTN_FLOAT_WIDTH) << pos[2] << "]" << std::endl; + kve.key = SRLZTN_POSROT_POS; + len = snprintf(NULL, 0, "%f%s%f%s%f", pos[0], SRLZTN_NUM_SEPARATOR, pos[1], SRLZTN_NUM_SEPARATOR, pos[2]); + s = pk_new<char>(len+1, h->bkt); + sprintf(s, "%f%s%f%s%f", pos[0], SRLZTN_NUM_SEPARATOR, pos[1], SRLZTN_NUM_SEPARATOR, pos[2]); + kve.val = s; + kve.end = SRLZTN_KVE_END; + pk_arr_append_t(&kvec.arr, kve); } if (quat_rot != glm::quat{}) { - h->o << SRLZTN_POSROT_ROT << "[" - << std::setw(SRLZTN_FLOAT_WIDTH) << quat_rot[0] << "," - << std::setw(SRLZTN_FLOAT_WIDTH) << quat_rot[1] << "," - << std::setw(SRLZTN_FLOAT_WIDTH) << quat_rot[2] << "," - << std::setw(SRLZTN_FLOAT_WIDTH) << quat_rot[3] << "]" << std::endl; + kve.key = SRLZTN_POSROT_ROT; + len = snprintf(NULL, 0, "%f%s%f%s%f%s%f", quat_rot[0], SRLZTN_NUM_SEPARATOR, quat_rot[1], SRLZTN_NUM_SEPARATOR, quat_rot[2], SRLZTN_NUM_SEPARATOR, quat_rot[3]); + s = pk_new<char>(len+1, h->bkt); + sprintf(s, "%f%s%f%s%f%s%f", quat_rot[0], SRLZTN_NUM_SEPARATOR, quat_rot[1], SRLZTN_NUM_SEPARATOR, quat_rot[2], SRLZTN_NUM_SEPARATOR, quat_rot[3]); + kve.val = s; + kve.end = SRLZTN_KVE_END; + pk_arr_append_t(&kvec.arr, kve); } if (scale != glm::vec3(1)) { - h->o << SRLZTN_POSROT_SCALE << "[" - << std::setw(SRLZTN_FLOAT_WIDTH) << scale[0] << "," - << std::setw(SRLZTN_FLOAT_WIDTH) << scale[1] << "," - << std::setw(SRLZTN_FLOAT_WIDTH) << scale[2] << "]" << std::endl; + kve.key = SRLZTN_POSROT_SCALE; + len = snprintf(NULL, 0, "%f%s%f%s%f", scale[0], SRLZTN_NUM_SEPARATOR, scale[1], SRLZTN_NUM_SEPARATOR, scale[2]); + s = pk_new<char>(len+1, h->bkt); + sprintf(s, "%f%s%f%s%f", scale[0], SRLZTN_NUM_SEPARATOR, scale[1], SRLZTN_NUM_SEPARATOR, scale[2]); + kve.val = s; + kve.end = SRLZTN_KVE_END; + pk_arr_append_t(&kvec.arr, kve); } - return true; + pk_arr_append_t(&h->kvp_containers, kvec); + return kvec.srlztn_handle; } -bool pke_deserialize_inst_pos(srlztn_deserialize_helper *h, glm::vec3 &pos, glm::quat &quat_rot, glm::vec3 &scale) { +void pke_deserialize_inst_pos(srlztn_deserialize_helper *h, pke_kve_container *kvec, glm::vec3 &pos, glm::quat &quat_rot, glm::vec3 &scale) { + (void)h; + char *pEnd = nullptr; + uint32_t i, index; const char *starting_char; - uint64_t prefix_len; PK_STN_RES stn_res; - if (strstr(h->read_line, SRLZTN_POSROT_POS)) { - prefix_len = strlen(SRLZTN_POSROT_POS); - starting_char = strchr(h->read_line + prefix_len, '[') + 1; - assert(starting_char != nullptr); - char *pEnd = nullptr; - long index = 0; - do { - assert(index < 3); - stn_res = pk_stn(&pos[index], starting_char, &pEnd); - if (stn_res != PK_STN_RES_SUCCESS) return false; - starting_char = pEnd + 1; - ++index; - } while (*pEnd != ']'); - return true; - } - if (strstr(h->read_line, SRLZTN_POSROT_ROT)) { - prefix_len = strlen(SRLZTN_POSROT_ROT); - starting_char = strchr(h->read_line + prefix_len, '[') + 1; - assert(starting_char != nullptr); - char *pEnd = nullptr; - long index = 0; - do { - assert(index < 4); - stn_res = pk_stn(&quat_rot[index], starting_char, &pEnd); - if (stn_res != PK_STN_RES_SUCCESS) return false; - starting_char = pEnd + 1; - ++index; - } while (*pEnd != ']'); - return true; - } - if (strstr(h->read_line, SRLZTN_POSROT_SCALE)) { - prefix_len = strlen(SRLZTN_POSROT_SCALE); - starting_char = strchr(h->read_line + prefix_len, '[') + 1; - assert(starting_char != nullptr); - char *pEnd = nullptr; - long index = 0; - do { - assert(index < 3); - stn_res = pk_stn(&scale[index], starting_char, &pEnd); - if (stn_res != PK_STN_RES_SUCCESS) return false; - starting_char = pEnd + 1; - ++index; - } while (*pEnd != ']'); - return true; + for (i = 0; i < kvec->arr.next; ++i) { + if (strstr(kvec->arr[i].key, SRLZTN_POSROT_POS)) { + starting_char = kvec->arr[i].val; + index = 0; + do { + assert(index < 3); + stn_res = pk_stn(&pos[index], starting_char, &pEnd); + if (stn_res != PK_STN_RES_SUCCESS) break; + starting_char = pEnd + 1; + ++index; + } while (*pEnd != '\0'); + } + if (strstr(kvec->arr[i].key, SRLZTN_POSROT_ROT)) { + starting_char = kvec->arr[i].val; + index = 0; + do { + assert(index < 4); + stn_res = pk_stn(&quat_rot[index], starting_char, &pEnd); + if (stn_res != PK_STN_RES_SUCCESS) break; + starting_char = pEnd + 1; + ++index; + } while (*pEnd != '\0'); + } + if (strstr(kvec->arr[i].key, SRLZTN_POSROT_SCALE)) { + starting_char = kvec->arr[i].val; + index = 0; + do { + assert(index < 3); + stn_res = pk_stn(&scale[index], starting_char, &pEnd); + if (stn_res != PK_STN_RES_SUCCESS) break; + starting_char = pEnd + 1; + ++index; + } while (*pEnd != '\0'); + } } - return false; } -bool pke_serialize_instance(srlztn_serialize_helper *h, const CompInstance *comp) { +pk_handle pke_serialize_instance(srlztn_serialize_helper *h, const CompInstance *comp) { EntityType *et = nullptr; - if (comp->grBindsHandle != GrBindsHandle_MAX) { - et = EntityType_FindByEntityHandle(ECS_GetGrBinds(comp->grBindsHandle)->entHandle); - } - + char *s; + int len; + pk_handle inst_pos_handle; + pke_kve kve{}; + pke_kve_container kvec{}; CompInstance c{}; glm::vec3 pos; glm::quat quat_rot; @@ -113,44 +123,98 @@ bool pke_serialize_instance(srlztn_serialize_helper *h, const CompInstance *comp BulletToGlm(comp->bt.rigidBody->getCollisionShape()->getLocalScaling(), scale); } + inst_pos_handle = pke_serialize_inst_pos(h, pos, quat_rot, scale); + + kvec.srlztn_handle = h->handle_head; + kvec.type_code = cstring_to_pk_cstr(SRLZTN_OBJ_INSTANCE); + kvec.bkt = h->bkt; + kvec.arr.bkt = h->bkt; + kvec.children.bkt = h->bkt; + kvec.child_handles.bkt = h->bkt; + h->handle_head.itemIndex++; + pk_arr_append_t(&kvec.child_handles, inst_pos_handle); + + if (comp->grBindsHandle != GrBindsHandle_MAX) { + et = EntityType_FindByEntityHandle(ECS_GetGrBinds(comp->grBindsHandle)->entHandle); + } + if (comp->uuid != pk_uuid_zed && comp->uuid != pk_uuid_max) { - h->o << SRLZTN_INSTANCE_COMPONENT_UUID << comp->uuid << std::endl; + kve.key = SRLZTN_INSTANCE_COMPONENT_UUID; + s = pk_new<char>(37, h->bkt); + sprintf(s, pk_uuid_printf_format, pk_uuid_printf_var(comp->uuid)); + kve.val = s; + kve.end = SRLZTN_KVE_END; + pk_arr_append_t(&kvec.arr, kve); } if (et != nullptr) { - h->o << SRLZTN_INSTANCE_COMPONENT_ENTITY_TYPE_CODE << et->entityTypeCode.val << std::endl; + kve.key = SRLZTN_INSTANCE_COMPONENT_ENTITY_TYPE_CODE; + s = pk_new<char>(et->entityTypeCode.reserved, h->bkt); + sprintf(s, "%s", et->entityTypeCode.val); + kve.val = s; + kve.end = SRLZTN_KVE_END; + pk_arr_append_t(&kvec.arr, kve); } - - pke_serialize_inst_pos(h, pos, quat_rot, scale); - if (mass != 1) { - h->o << SRLZTN_INSTANCE_COMPONENT_MASS << mass << std::endl; + kve.key = SRLZTN_INSTANCE_COMPONENT_MASS; + len = snprintf(NULL, 0, "%f", mass); + s = pk_new<char>(len+1, h->bkt); + sprintf(s, "%f", mass); + kve.val = s; + kve.end = SRLZTN_KVE_END; + pk_arr_append_t(&kvec.arr, kve); } if (collisionLayer != c.physicsLayer) { - h->o << SRLZTN_INSTANCE_COMPONENT_COLLISION_LAYER << static_cast<PhysicsCollision_T>(collisionLayer) << std::endl; + kve.key = SRLZTN_INSTANCE_COMPONENT_COLLISION_LAYER; + len = snprintf(NULL, 0, "%lu", static_cast<PhysicsCollision_T>(collisionLayer)); + s = pk_new<char>(len+1, h->bkt); + sprintf(s, "%lu", static_cast<PhysicsCollision_T>(collisionLayer)); + kve.val = s; + kve.end = SRLZTN_KVE_END; + pk_arr_append_t(&kvec.arr, kve); } if (collisionMask != c.physicsMask) { - h->o << SRLZTN_INSTANCE_COMPONENT_COLLISION_MASK << static_cast<PhysicsCollision_T>(collisionMask) << std::endl; + kve.key = SRLZTN_INSTANCE_COMPONENT_COLLISION_MASK; + len = snprintf(NULL, 0, "%lu", static_cast<PhysicsCollision_T>(collisionMask)); + s = pk_new<char>(len+1, h->bkt); + sprintf(s, "%lu", static_cast<PhysicsCollision_T>(collisionMask)); + kve.val = s; + kve.end = SRLZTN_KVE_END; + pk_arr_append_t(&kvec.arr, kve); } if (comp->collisionCallback.name[0] != '\0') { - h->o << SRLZTN_INSTANCE_COMPONENT_COLLISION_CB_SIGNATURE << comp->collisionCallback.name << std::endl; + kve.key = SRLZTN_INSTANCE_COMPONENT_COLLISION_CB_SIGNATURE; + s = pk_new<char>(CallbackSignatureLength + 1, h->bkt); + sprintf(s, "%s", comp->collisionCallback.name); + kve.val = s; + kve.end = SRLZTN_KVE_END; + pk_arr_append_t(&kvec.arr, kve); } - return true; + pk_arr_append_t(&h->kvp_containers, kvec); + return kvec.srlztn_handle; } -bool pke_deserialize_instance(srlztn_deserialize_helper *h) { - uint64_t prefix_len; +void pke_deserialize_instance(srlztn_deserialize_helper *h, pke_kve_container *kvec) { + uint32_t i; PK_STN_RES stn_res; EntityType *et_ptr = nullptr; - float mass; + float mass = 1; InstPos inst_pos; CompInstance comp{}; glm::vec3 pos = glm::vec3(0); glm::quat quat_rot = glm::quat(0, 0, 0, 1); glm::vec3 scale = glm::vec3(1); comp.collisionCallback.name[0] = '\0'; - while (h->i->getline(h->read_line, h->read_line_len)) { - if (strstr(SRLZTN_OBJ_END, h->read_line)) { + + for (i = 0; i < kvec->children.next; ++i) { + pke_kve_container *child_kvec = kvec->children[i]; + if (strncmp(child_kvec->type_code.val, SRLZTN_OBJ_INSTANCE_POSITION, strlen(SRLZTN_OBJ_INSTANCE_POSITION)) == 0) { + pke_deserialize_inst_pos(h, child_kvec, pos, quat_rot, scale); + } + } + + for (i = 0; i < kvec->arr.next; ++i) { + if (strstr(SRLZTN_OBJ_END, kvec->arr[i].key)) { if (et_ptr == nullptr) { fprintf(stdout, "[Game::DeserializeInstance] Unknown EntityTypeCode, skipping instance.\n"); break; @@ -196,46 +260,40 @@ bool pke_deserialize_instance(srlztn_deserialize_helper *h) { pk_arr_append(&h->mapping, &map); } } - return true; - } - if (strstr(h->read_line, SRLZTN_INSTANCE_COMPONENT_UUID)) { - prefix_len = strlen(SRLZTN_INSTANCE_COMPONENT_UUID); - (h->read_line + prefix_len) >> comp.uuid ; - continue; } - if (strstr(h->read_line, SRLZTN_INSTANCE_COMPONENT_ENTITY_TYPE_CODE)) { - prefix_len = strlen(SRLZTN_INSTANCE_COMPONENT_ENTITY_TYPE_CODE); - if (strlen(h->read_line + prefix_len) > 1) { - et_ptr = EntityType_FindByTypeCode(h->read_line + prefix_len); - } + if (strstr(kvec->arr[i].key, SRLZTN_INSTANCE_COMPONENT_UUID)) { + kvec->arr[i].val >> comp.uuid ; continue; } - if (strstr(h->read_line, SRLZTN_INSTANCE_COMPONENT_MASS)) { - prefix_len = strlen(SRLZTN_INSTANCE_COMPONENT_MASS); - stn_res = pk_stn(&mass, h->read_line + prefix_len, nullptr); - if (stn_res != PK_STN_RES_SUCCESS) return false; + if (strstr(kvec->arr[i].key, SRLZTN_INSTANCE_COMPONENT_ENTITY_TYPE_CODE)) { + et_ptr = EntityType_FindByTypeCode(kvec->arr[i].val); continue; } - if (strstr(h->read_line, SRLZTN_INSTANCE_COMPONENT_COLLISION_LAYER)) { - prefix_len = strlen(SRLZTN_INSTANCE_COMPONENT_COLLISION_LAYER); - stn_res = pk_stn(&comp.physicsLayer, h->read_line + prefix_len, nullptr, 10); - if (stn_res != PK_STN_RES_SUCCESS) return false; + if (strstr(kvec->arr[i].key, SRLZTN_INSTANCE_COMPONENT_MASS)) { + stn_res = pk_stn(&mass, kvec->arr[i].val, nullptr); + if (stn_res != PK_STN_RES_SUCCESS) { + fprintf(stderr, "[pke_deserialize_instance] Failed to parse %s", SRLZTN_INSTANCE_COMPONENT_MASS); + } continue; } - if (strstr(h->read_line, SRLZTN_INSTANCE_COMPONENT_COLLISION_MASK)) { - prefix_len = strlen(SRLZTN_INSTANCE_COMPONENT_COLLISION_MASK); - stn_res = pk_stn(&comp.physicsMask, h->read_line + prefix_len, nullptr, 10); - if (stn_res != PK_STN_RES_SUCCESS) return false; + if (strstr(kvec->arr[i].key, SRLZTN_INSTANCE_COMPONENT_COLLISION_LAYER)) { + stn_res = pk_stn(&comp.physicsLayer, kvec->arr[i].val, nullptr, 10); + if (stn_res != PK_STN_RES_SUCCESS) { + fprintf(stderr, "[pke_deserialize_instance] Failed to parse %s", SRLZTN_INSTANCE_COMPONENT_COLLISION_LAYER); + } continue; } - if (strstr(h->read_line, SRLZTN_INSTANCE_COMPONENT_COLLISION_CB_SIGNATURE)) { - prefix_len = strlen(SRLZTN_INSTANCE_COMPONENT_COLLISION_CB_SIGNATURE); - strncpy(comp.collisionCallback.name, h->read_line + prefix_len, 16); + if (strstr(kvec->arr[i].key, SRLZTN_INSTANCE_COMPONENT_COLLISION_MASK)) { + stn_res = pk_stn(&comp.physicsMask, kvec->arr[i].val, nullptr, 10); + if (stn_res != PK_STN_RES_SUCCESS) { + fprintf(stderr, "[pke_deserialize_instance] Failed to parse %s", SRLZTN_INSTANCE_COMPONENT_COLLISION_MASK); + } continue; } - if (pke_deserialize_inst_pos(h, pos, quat_rot, scale)) { + if (strstr(kvec->arr[i].key, SRLZTN_INSTANCE_COMPONENT_COLLISION_CB_SIGNATURE)) { + strncpy(comp.collisionCallback.name, kvec->arr[i].val, 16); continue; } } - return false; + return; } |
