SHADE_Y3/SHADE_Engine/src/Serialization/SHSerialization.cpp

143 lines
4.1 KiB
C++
Raw Normal View History

2022-09-26 21:08:59 +08:00
#include "SHpch.h"
2022-09-26 23:51:20 +08:00
#include "SHSerializationHelper.hpp"
2022-09-26 21:08:59 +08:00
#include "SHSerialization.h"
#include <yaml-cpp/yaml.h>
2022-09-27 21:06:25 +08:00
#include "ECS_Base/Managers/SHEntityManager.h"
2022-09-26 21:08:59 +08:00
#include "Scene/SHSceneManager.h"
#include "Tools/SHException.h"
#include "Math/Transform/SHTransformComponent.h"
namespace SHADE
{
2022-09-26 23:51:20 +08:00
void SHSerialization::SerializeSceneToFile(std::filesystem::path const& path)
2022-09-26 21:08:59 +08:00
{
2022-09-26 23:51:20 +08:00
}
2022-09-26 21:08:59 +08:00
2022-09-26 23:51:20 +08:00
std::string SHSerialization::SerializeSceneToString()
{
YAML::Emitter out;
SerializeSceneToEmitter(out);
return std::string(out.c_str());
}
2022-09-26 21:08:59 +08:00
2022-09-26 23:51:20 +08:00
void SHSerialization::SerializeSceneToEmitter(YAML::Emitter& out)
{
auto const& sceneGraph = SHSceneManager::GetCurrentSceneGraph();
auto root = sceneGraph.GetRoot();
2022-09-26 21:08:59 +08:00
2022-09-26 23:51:20 +08:00
SHASSERT(root != nullptr, "Root is null. Failed to serialize scene to node.");
2022-09-27 21:06:25 +08:00
2022-09-26 23:51:20 +08:00
auto const& children = root->GetChildren();
out << YAML::BeginDoc;
2022-09-27 21:06:25 +08:00
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<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)
2022-09-26 21:08:59 +08:00
{
2022-09-27 21:06:25 +08:00
DeserializeEntity(it, (*it), createdEntities);
}
//Initialize Entity
for(auto it = entities.begin(); it != entities.end(); ++it)
{
//For each node, Deserialize component
//Recurse through properties
2022-09-26 21:08:59 +08:00
}
2022-09-26 23:51:20 +08:00
}
2022-09-26 21:08:59 +08:00
2022-09-27 21:06:25 +08:00
2022-09-26 23:51:20 +08:00
std::string SHSerialization::SerializeEntityToString()
{
return std::string();
}
2022-09-26 21:08:59 +08:00
2022-09-26 23:51:20 +08:00
void SHSerialization::SerializeEntityToFile(std::filesystem::path const& path)
{
}
YAML::Node SHSerialization::SerializeEntityToNode(SHSceneNode* sceneNode)
{
YAML::Node node;
2022-09-27 21:06:25 +08:00
auto eid = sceneNode->GetEntityID();
auto entity = SHEntityManager::GetEntityByID(eid);
if (!sceneNode || !entity)
2022-09-26 21:08:59 +08:00
{
2022-09-26 23:51:20 +08:00
node = YAML::Null;
return node;
}
2022-09-27 21:06:25 +08:00
node[EIDNode] = eid;
node[EntityNameNode] = entity->name;
node[IsActiveNode] = sceneNode->IsActive();
2022-09-26 23:51:20 +08:00
auto const& children = sceneNode->GetChildren();
2022-09-27 21:06:25 +08:00
node[NumberOfChildrenNode] = children.size();
2022-09-26 21:08:59 +08:00
2022-09-26 23:51:20 +08:00
YAML::Node components;
2022-09-26 21:08:59 +08:00
2022-09-26 23:51:20 +08:00
if (const auto transform = SHComponentManager::GetComponent_s<SHTransformComponent>(eid))
{
components[rttr::type::get<SHTransformComponent>().get_name().data()] = SHSerializationHelper::SerializeComponentToNode(transform);
2022-09-26 21:08:59 +08:00
}
2022-09-27 21:06:25 +08:00
node[ComponentsNode] = components;
2022-09-26 23:51:20 +08:00
return node;
2022-09-26 21:08:59 +08:00
}
2022-09-27 21:06:25 +08:00
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;
}
2022-09-26 21:08:59 +08:00
}