97 lines
3.0 KiB
C++
97 lines
3.0 KiB
C++
|
#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;
|
||
|
}
|
||
|
}
|
||
|
}
|