Serialization fix

This commit is contained in:
Sri Sham Haran 2022-09-26 23:51:20 +08:00
parent 7247faee73
commit 233e7a0e8f
3 changed files with 61 additions and 64 deletions

View File

@ -1,4 +1,5 @@
#include "SHpch.h"
#include "SHSerializationHelper.hpp"
#include "SHSerialization.h"
#include <yaml-cpp/yaml.h>
@ -6,74 +7,70 @@
#include "Scene/SHSceneManager.h"
#include "Tools/SHException.h"
#include "SHSerializationHelper.hpp"
#include "Math/Transform/SHTransformComponent.h"
namespace SHADE
{
namespace SHSerialization
void SHSerialization::SerializeSceneToFile(std::filesystem::path const& path)
{
void SerializeSceneToFile(std::filesystem::path const& path)
{
}
std::string SHSerialization::SerializeSceneToString()
{
YAML::Emitter out;
SerializeSceneToEmitter(out);
return std::string(out.c_str());
}
void SHSerialization::SerializeSceneToEmitter(YAML::Emitter& out)
{
auto const& sceneGraph = SHSceneManager::GetCurrentSceneGraph();
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;
}
out << YAML::EndDoc;
}
std::string SerializeSceneToString()
std::string SHSerialization::SerializeEntityToString()
{
return std::string();
}
void SHSerialization::SerializeEntityToFile(std::filesystem::path const& path)
{
}
YAML::Node SHSerialization::SerializeEntityToNode(SHSceneNode* sceneNode)
{
YAML::Node node;
if (!sceneNode)
{
YAML::Emitter out;
SerializeSceneToEmitter(out);
return std::string(out.c_str());
}
void SerializeSceneToEmitter(YAML::Emitter& out)
{
auto const& sceneGraph = SHSceneManager::GetCurrentSceneGraph();
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;
}
out << YAML::EndDoc;
}
std::string SerializeEntityToString()
{
return std::string();
}
void SerializeEntityToFile(std::filesystem::path const& path)
{
}
YAML::Node SerializeEntityToNode(SHSceneNode* sceneNode)
{
YAML::Node node;
if(!sceneNode)
{
node = YAML::Null;
return node;
}
auto eid = sceneNode->GetEntityID();
node["EID"] = eid;
node["isActive"] = sceneNode->IsActive();
auto const& children = sceneNode->GetChildren();
node["NumberOfChildren"] = children.size();
YAML::Node components;
if(const auto transform = SHComponentManager::GetComponent_s<SHTransformComponent>(eid))
{
components[rttr::type::get<SHTransformComponent>().get_name().data()] = SerializeComponentToNode(transform);
}
node["Components"] = components;
node = YAML::Null;
return node;
}
auto eid = sceneNode->GetEntityID();
node["EID"] = eid;
node["isActive"] = sceneNode->IsActive();
auto const& children = sceneNode->GetChildren();
node["NumberOfChildren"] = children.size();
YAML::Node components;
if (const auto transform = SHComponentManager::GetComponent_s<SHTransformComponent>(eid))
{
components[rttr::type::get<SHTransformComponent>().get_name().data()] = SHSerializationHelper::SerializeComponentToNode(transform);
}
node["Components"] = components;
return node;
}
}

View File

@ -5,7 +5,7 @@
#include <filesystem>
#include <ECS_Base/Components/SHComponent.h>
#include "SH_API.h"
namespace YAML
{
class Emitter;
@ -15,7 +15,7 @@ namespace YAML
namespace SHADE
{
class SHSceneNode;
namespace SHSerialization
struct SH_API SHSerialization
{
static void SerializeSceneToFile(std::filesystem::path const& path);
static std::string SerializeSceneToString();
@ -24,5 +24,5 @@ namespace SHADE
static std::string SerializeEntityToString();
static void SerializeEntityToFile(std::filesystem::path const& path);
static YAML::Node SerializeEntityToNode(SHSceneNode* sceneNode);
}
};
}

View File

@ -7,7 +7,7 @@
namespace SHADE
{
namespace SHSerialization
struct SHSerializationHelper
{
template <typename ComponentType, std::enable_if_t<std::is_base_of_v<SHComponent, ComponentType>, bool> = true>
static std::string SerializeComponentToString(ComponentType* component)
@ -89,9 +89,9 @@ namespace SHADE
return node;
auto componentType = rttr::type::get<ComponentType>();
node[componentType.get_name().data()] = RTTRToNode(*component);
node = RTTRToNode(*component);
return node;
}
}
};
}