Added Serialization/Deserialization #96

Merged
srishamharan merged 12 commits from SP3-8-serialization into main 2022-10-19 08:48:18 +08:00
4 changed files with 205 additions and 1 deletions
Showing only changes of commit 7247faee73 - Show all commits

View File

@ -44,7 +44,7 @@ namespace SHADE
{ {
if (!component) if (!component)
return; return;
auto componentType = rttr::type::get(*component); auto componentType = rttr::type::get<T>();
CheckBox("##IsActive", [component]() {return component->isActive; }, [component](bool const& active) {component->isActive = active; }); CheckBox("##IsActive", [component]() {return component->isActive; }, [component](bool const& active) {component->isActive = active; });
ImGui::SameLine(); ImGui::SameLine();
if (ImGui::CollapsingHeader(componentType.get_name().data())) if (ImGui::CollapsingHeader(componentType.get_name().data()))

View File

@ -0,0 +1,79 @@
#include "SHpch.h"
#include "SHSerialization.h"
#include <yaml-cpp/yaml.h>
#include "Scene/SHSceneManager.h"
#include "Tools/SHException.h"
#include "SHSerializationHelper.hpp"
#include "Math/Transform/SHTransformComponent.h"
namespace SHADE
{
namespace SHSerialization
{
void SerializeSceneToFile(std::filesystem::path const& path)
{
}
std::string SerializeSceneToString()
{
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;
return node;
}
}
}

View File

@ -0,0 +1,28 @@
#pragma once
#include "SH_API.h"
#include <string>
#include <filesystem>
#include <ECS_Base/Components/SHComponent.h>
namespace YAML
{
class Emitter;
class Node;
}
namespace SHADE
{
class SHSceneNode;
namespace SHSerialization
{
static void SerializeSceneToFile(std::filesystem::path const& path);
static std::string SerializeSceneToString();
static void SerializeSceneToEmitter(YAML::Emitter& out);
static std::string SerializeEntityToString();
static void SerializeEntityToFile(std::filesystem::path const& path);
static YAML::Node SerializeEntityToNode(SHSceneNode* sceneNode);
}
}

View File

@ -0,0 +1,97 @@
#pragma once
#include "ECS_Base/Components/SHComponent.h"
#include <yaml-cpp/yaml.h>
#include <rttr/registration>
namespace SHADE
{
namespace SHSerialization
{
template <typename ComponentType, std::enable_if_t<std::is_base_of_v<SHComponent, ComponentType>, bool> = true>
static std::string SerializeComponentToString(ComponentType* component)
{
return std::string();
}
template <typename ComponentType, std::enable_if_t<std::is_base_of_v<SHComponent, ComponentType>, bool> = true>
static void SerializeComponentToFile(ComponentType* component, std::filesystem::path const& path)
{
}
static YAML::Node RTTRToNode(const rttr::variant& var)
{
YAML::Node node;
auto varType = var.get_type();
if(varType.is_arithmetic())
{
bool ok = false;
if (varType == rttr::type::get<bool>())
node = var.to_bool();
else if (varType == rttr::type::get<int8_t>())
node = var.to_int8(&ok);
else if (varType == rttr::type::get<int16_t>())
node = var.to_int16(&ok);
else if (varType == rttr::type::get<int32_t>())
node = var.to_int32(&ok);
else if (varType == rttr::type::get<int64_t>())
node = var.to_int64(&ok);
else if (varType == rttr::type::get<uint8_t>())
node = var.to_uint8(&ok);
else if (varType == rttr::type::get<uint16_t>())
node = var.to_uint16(&ok);
else if (varType == rttr::type::get<uint32_t>())
node = var.to_uint32(&ok);
else if (varType == rttr::type::get<uint64_t>())
node = var.to_uint64(&ok);
else if (varType == rttr::type::get<float>())
node = var.to_float(&ok);
else if (varType == rttr::type::get<double>())
node = var.to_double(&ok);
//else if (varType == rttr::type::get<char>()) //same as uint8_t
// node = var.to_uint8();
}
else if (varType.is_enumeration())
{
bool ok = false;
auto result = var.to_string(&ok);
if (ok)
{
node = var.to_string();
}
else
{
ok = false;
auto value = var.to_uint64(&ok);
if (ok)
node = value;
else
node = YAML::Null;
}
}
else
{
auto properties = var.get_type().get_properties();
for(auto property : properties)
{
node[property.get_name().data()] = RTTRToNode(property.get_value(var));
}
}
return node;
}
template <typename ComponentType, std::enable_if_t<std::is_base_of_v<SHComponent, ComponentType>, bool> = true>
static YAML::Node SerializeComponentToNode(ComponentType* component)
{
YAML::Node node{};
if(!component)
return node;
auto componentType = rttr::type::get<ComponentType>();
node[componentType.get_name().data()] = RTTRToNode(*component);
return node;
}
}
}