From cebb1a2cf85aa2c779b0ceb4a9372a23cde095f6 Mon Sep 17 00:00:00 2001 From: Sri Sham Haran Date: Tue, 27 Sep 2022 21:06:25 +0800 Subject: [PATCH] Deserialization WIP --- .../src/Serialization/SHSerialization.cpp | 92 ++++++++++++++++--- .../src/Serialization/SHSerialization.h | 17 +++- 2 files changed, 94 insertions(+), 15 deletions(-) diff --git a/SHADE_Engine/src/Serialization/SHSerialization.cpp b/SHADE_Engine/src/Serialization/SHSerialization.cpp index 25358805..926e4ff5 100644 --- a/SHADE_Engine/src/Serialization/SHSerialization.cpp +++ b/SHADE_Engine/src/Serialization/SHSerialization.cpp @@ -4,6 +4,7 @@ #include +#include "ECS_Base/Managers/SHEntityManager.h" #include "Scene/SHSceneManager.h" #include "Tools/SHException.h" @@ -29,18 +30,60 @@ namespace SHADE auto root = sceneGraph.GetRoot(); SHASSERT(root != nullptr, "Root is null. Failed to serialize scene to node."); - + auto const& children = root->GetChildren(); out << YAML::BeginDoc; - for (auto child : children) - { - out << YAML::BeginSeq; - out << SerializeEntityToNode(child); - out << YAML::EndSeq; - } + + auto pred = [&out](SHSceneNode* node){out << SerializeEntityToNode(node);}; + sceneGraph.Traverse(pred); + //out << SerializeEntityToNode(child); + out << YAML::EndDoc; } + static void DeserializeEntity(YAML::iterator& it, YAML::Node const& node, std::vector& createdEntities, EntityID parentEID = MAX_EID) + { + if(!node[EIDNode]) + return; + EntityID eid = node[EIDNode].as(); + std::string name = "Default"; + if(node[EntityNameNode]) + name = node[EntityNameNode].as(); + //Compile component IDs + const auto componentIDList = SHSerialization::GetComponentIDList(node[ComponentsNode]); + eid = SHEntityManager::CreateEntity(componentIDList, eid, name, parentEID); + createdEntities.push_back(eid); + if(node[NumberOfChildrenNode]) + { int numOfChildren = node[NumberOfChildrenNode].as(); + for(int i = 0; i < numOfChildren; ++i) + { + DeserializeEntity(it, *it, createdEntities, eid); + ++it; + } + } + } + + void SHSerialization::DeserializeSceneFromFile(std::string const& fileData) + { + YAML::Node entities = YAML::Load(fileData.c_str()); + std::vector createdEntities{}; + + //Create Entities + for(auto it = entities.begin(); it != entities.end(); ++it) + { + DeserializeEntity(it, (*it), createdEntities); + } + + //Initialize Entity + for(auto it = entities.begin(); it != entities.end(); ++it) + { + //For each node, Deserialize component + //Recurse through properties + } + } + + + std::string SHSerialization::SerializeEntityToString() { return std::string(); @@ -53,16 +96,19 @@ namespace SHADE YAML::Node SHSerialization::SerializeEntityToNode(SHSceneNode* sceneNode) { YAML::Node node; - if (!sceneNode) + auto eid = sceneNode->GetEntityID(); + auto entity = SHEntityManager::GetEntityByID(eid); + if (!sceneNode || !entity) { node = YAML::Null; return node; } - auto eid = sceneNode->GetEntityID(); - node["EID"] = eid; - node["isActive"] = sceneNode->IsActive(); + + node[EIDNode] = eid; + node[EntityNameNode] = entity->name; + node[IsActiveNode] = sceneNode->IsActive(); auto const& children = sceneNode->GetChildren(); - node["NumberOfChildren"] = children.size(); + node[NumberOfChildrenNode] = children.size(); YAML::Node components; @@ -70,7 +116,27 @@ namespace SHADE { components[rttr::type::get().get_name().data()] = SHSerializationHelper::SerializeComponentToNode(transform); } - node["Components"] = components; + node[ComponentsNode] = components; return node; } + + template, bool> = true> + std::optional GetComponentID(YAML::Node const & componentNode) + { + if(componentNode[rttr::type::get().get_name().data()]) + return {SHFamilyID::GetID()}; + else + return std::nullopt; + } + + std::vector SHSerialization::GetComponentIDList(YAML::Node const& componentsNode) + { + std::vector componentIDList; + + auto id = GetComponentID(componentsNode); + if(id.has_value()) + componentIDList.push_back(id.value()); + + return componentIDList; + } } diff --git a/SHADE_Engine/src/Serialization/SHSerialization.h b/SHADE_Engine/src/Serialization/SHSerialization.h index 0611e4d2..022a01c4 100644 --- a/SHADE_Engine/src/Serialization/SHSerialization.h +++ b/SHADE_Engine/src/Serialization/SHSerialization.h @@ -5,7 +5,7 @@ #include #include -#include "SH_API.h" + namespace YAML { class Emitter; @@ -15,14 +15,27 @@ namespace YAML namespace SHADE { class SHSceneNode; + + constexpr const char* ComponentsNode = "Components"; + constexpr const char* EntityNameNode = "Name"; + constexpr const char* EIDNode = "EID"; + constexpr const char* IsActiveNode = "IsActive"; + constexpr const char* NumberOfChildrenNode = "NumberOfChildren"; + struct SH_API SHSerialization { + //TODO: change paths to resource ID static void SerializeSceneToFile(std::filesystem::path const& path); static std::string SerializeSceneToString(); static void SerializeSceneToEmitter(YAML::Emitter& out); - + static void DeserializeSceneFromFile(std::string const& fileData); + + static std::string SerializeEntityToString(); static void SerializeEntityToFile(std::filesystem::path const& path); static YAML::Node SerializeEntityToNode(SHSceneNode* sceneNode); + + static std::vector GetComponentIDList(YAML::Node const& componentsNode); + private: }; } \ No newline at end of file