Deserialization WIP

This commit is contained in:
Sri Sham Haran 2022-09-27 21:06:25 +08:00
parent c4ed199165
commit cebb1a2cf8
2 changed files with 94 additions and 15 deletions

View File

@ -4,6 +4,7 @@
#include <yaml-cpp/yaml.h> #include <yaml-cpp/yaml.h>
#include "ECS_Base/Managers/SHEntityManager.h"
#include "Scene/SHSceneManager.h" #include "Scene/SHSceneManager.h"
#include "Tools/SHException.h" #include "Tools/SHException.h"
@ -29,18 +30,60 @@ namespace SHADE
auto root = sceneGraph.GetRoot(); auto root = sceneGraph.GetRoot();
SHASSERT(root != nullptr, "Root is null. Failed to serialize scene to node."); SHASSERT(root != nullptr, "Root is null. Failed to serialize scene to node.");
auto const& children = root->GetChildren(); auto const& children = root->GetChildren();
out << YAML::BeginDoc; out << YAML::BeginDoc;
for (auto child : children)
{ auto pred = [&out](SHSceneNode* node){out << SerializeEntityToNode(node);};
out << YAML::BeginSeq; sceneGraph.Traverse(pred);
out << SerializeEntityToNode(child); //out << SerializeEntityToNode(child);
out << YAML::EndSeq;
}
out << YAML::EndDoc; out << YAML::EndDoc;
} }
static void DeserializeEntity(YAML::iterator& it, YAML::Node const& node, std::vector<EntityID>& createdEntities, EntityID parentEID = MAX_EID)
{
if(!node[EIDNode])
return;
EntityID eid = node[EIDNode].as<EntityID>();
std::string name = "Default";
if(node[EntityNameNode])
name = node[EntityNameNode].as<std::string>();
//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<int>();
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<EntityID> 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() std::string SHSerialization::SerializeEntityToString()
{ {
return std::string(); return std::string();
@ -53,16 +96,19 @@ namespace SHADE
YAML::Node SHSerialization::SerializeEntityToNode(SHSceneNode* sceneNode) YAML::Node SHSerialization::SerializeEntityToNode(SHSceneNode* sceneNode)
{ {
YAML::Node node; YAML::Node node;
if (!sceneNode) auto eid = sceneNode->GetEntityID();
auto entity = SHEntityManager::GetEntityByID(eid);
if (!sceneNode || !entity)
{ {
node = YAML::Null; node = YAML::Null;
return node; return node;
} }
auto eid = sceneNode->GetEntityID();
node["EID"] = eid; node[EIDNode] = eid;
node["isActive"] = sceneNode->IsActive(); node[EntityNameNode] = entity->name;
node[IsActiveNode] = sceneNode->IsActive();
auto const& children = sceneNode->GetChildren(); auto const& children = sceneNode->GetChildren();
node["NumberOfChildren"] = children.size(); node[NumberOfChildrenNode] = children.size();
YAML::Node components; YAML::Node components;
@ -70,7 +116,27 @@ namespace SHADE
{ {
components[rttr::type::get<SHTransformComponent>().get_name().data()] = SHSerializationHelper::SerializeComponentToNode(transform); components[rttr::type::get<SHTransformComponent>().get_name().data()] = SHSerializationHelper::SerializeComponentToNode(transform);
} }
node["Components"] = components; node[ComponentsNode] = components;
return node; return node;
} }
template<typename ComponentType, std::enable_if_t<std::is_base_of_v<SHComponent, ComponentType>, bool> = true>
std::optional<ComponentTypeID> GetComponentID(YAML::Node const & componentNode)
{
if(componentNode[rttr::type::get<ComponentType>().get_name().data()])
return {SHFamilyID<SHComponent>::GetID<ComponentType>()};
else
return std::nullopt;
}
std::vector<ComponentTypeID> SHSerialization::GetComponentIDList(YAML::Node const& componentsNode)
{
std::vector<ComponentTypeID> componentIDList;
auto id = GetComponentID<SHTransformComponent>(componentsNode);
if(id.has_value())
componentIDList.push_back(id.value());
return componentIDList;
}
} }

View File

@ -5,7 +5,7 @@
#include <filesystem> #include <filesystem>
#include <ECS_Base/Components/SHComponent.h> #include <ECS_Base/Components/SHComponent.h>
#include "SH_API.h"
namespace YAML namespace YAML
{ {
class Emitter; class Emitter;
@ -15,14 +15,27 @@ namespace YAML
namespace SHADE namespace SHADE
{ {
class SHSceneNode; 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 struct SH_API SHSerialization
{ {
//TODO: change paths to resource ID
static void SerializeSceneToFile(std::filesystem::path const& path); static void SerializeSceneToFile(std::filesystem::path const& path);
static std::string SerializeSceneToString(); static std::string SerializeSceneToString();
static void SerializeSceneToEmitter(YAML::Emitter& out); static void SerializeSceneToEmitter(YAML::Emitter& out);
static void DeserializeSceneFromFile(std::string const& fileData);
static std::string SerializeEntityToString(); static std::string SerializeEntityToString();
static void SerializeEntityToFile(std::filesystem::path const& path); static void SerializeEntityToFile(std::filesystem::path const& path);
static YAML::Node SerializeEntityToNode(SHSceneNode* sceneNode); static YAML::Node SerializeEntityToNode(SHSceneNode* sceneNode);
static std::vector<ComponentTypeID> GetComponentIDList(YAML::Node const& componentsNode);
private:
}; };
} }