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>
|
|
|
|
|
|
|
|
#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-26 21:08:59 +08:00
|
|
|
|
2022-09-26 23:51:20 +08:00
|
|
|
auto const& children = root->GetChildren();
|
|
|
|
out << YAML::BeginDoc;
|
|
|
|
for (auto child : children)
|
2022-09-26 21:08:59 +08:00
|
|
|
{
|
2022-09-26 23:51:20 +08:00
|
|
|
out << YAML::BeginSeq;
|
|
|
|
out << SerializeEntityToNode(child);
|
|
|
|
out << YAML::EndSeq;
|
2022-09-26 21:08:59 +08:00
|
|
|
}
|
2022-09-26 23:51:20 +08:00
|
|
|
out << YAML::EndDoc;
|
|
|
|
}
|
2022-09-26 21:08:59 +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;
|
|
|
|
if (!sceneNode)
|
2022-09-26 21:08:59 +08:00
|
|
|
{
|
2022-09-26 23:51:20 +08:00
|
|
|
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();
|
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-26 23:51:20 +08:00
|
|
|
node["Components"] = components;
|
|
|
|
return node;
|
2022-09-26 21:08:59 +08:00
|
|
|
}
|
|
|
|
}
|