Loading...
Searching...
No Matches
Serializer.h
Go to the documentation of this file.
1//
2// Created by denzel on 03/12/2025.
3//
4#pragma once
5
6#include <iosfwd>
7
9#include "hellfire/ecs/LightComponent.h"
10#include "hellfire/ecs/RenderableComponent.h"
11#include "hellfire/ecs/TransformComponent.h"
12#include "hellfire/ecs/components/MeshComponent.h"
13#include "hellfire/scene/Scene.h"
14#include "hellfire/utilities/SerializerUtils.h"
15
16namespace hellfire {
17 template<typename T>
18 struct Serializer {
19 static bool serialize(std::ostream &output, const T *obj);
20
21 static bool deserialize(std::istream &input, T *obj);
22 };
23
24 template<>
26 static bool serialize(std::ostream &output, const TransformComponent *obj) {
27 if (obj == nullptr) return false;
28
29 const nlohmann::json j = {
30 {"position", obj->get_position()},
31 {"rotation", obj->get_rotation()},
32 {"scale", obj->get_scale()}
33 };
34
35 output << j.dump(4);
36 return output.good();
37 }
38
39 static bool deserialize(std::istream &input, TransformComponent *obj) {
40 try {
41 if (obj == nullptr) return false;
42
43 nlohmann::json j;
44 input >> j;
45
46 const auto position = json_get_vec3(j, "position");
47 const auto rotation = json_get_vec3(j, "rotation");
48 const auto scale = json_get_vec3(j, "scale");
49
50 if (!position || !rotation || !scale) return false;
51
52 obj->set_position(*position);
53 obj->set_rotation(*rotation);
54 obj->set_scale(*scale);
55
56 return true;
57 } catch (...) {
58 return false;
59 }
60 }
61 };
62
63 template<>
64 struct Serializer<MeshComponent> {
65 static bool serialize(std::ostream &output, const MeshComponent *obj) {
66 if (!obj) return false;
67
68 nlohmann::json j = {
69 {"mesh_asset", obj->get_mesh_asset()},
70 {"is_wireframe", obj->is_wireframe}
71
72 };
73
74 output << j.dump(4);
75 return output.good();
76 }
77
78 static bool deserialize(std::istream &input, MeshComponent *obj) {
79 if (!obj) return false;
80
81 try {
82 nlohmann::json j;
83 input >> j;
84
85 obj->set_mesh_asset(j.value("mesh_asset", INVALID_ASSET_ID));
86 obj->is_wireframe = j.value("is_wireframe", false);
87
88 return true;
89 } catch (...) {
90 return false;
91 }
92 }
93 };
94
95 template<>
96 struct Serializer<RenderableComponent> {
97 static bool serialize(std::ostream &output, const RenderableComponent *obj) {
98 if (!obj) return false;
99
100 nlohmann::json j = {
101 {"material_asset", obj->get_material_asset()},
102 {"cast_shadows", obj->cast_shadows},
103 {"receive_shadows", obj->receive_shadows},
104 {"visible", obj->visible},
105 {"render_layer", obj->render_layer}
106 };
107
108 output << j.dump(4);
109 return output.good();
110 }
111
112 static bool deserialize(std::istream &input, RenderableComponent *obj) {
113 if (!obj) return false;
114
115 try {
116 nlohmann::json j;
117 input >> j;
118
119 obj->set_material_asset(j.value("material_asset", INVALID_ASSET_ID));
120 obj->cast_shadows = j.value("cast_shadows", true);
121 obj->receive_shadows = j.value("receive_shadows", true);
122 obj->visible = j.value("visible", true);
123 obj->render_layer = j.value("render_layer", 0u);
124
125 return true;
126 } catch (...) {
127 return false;
128 }
129 }
130 };
131
132 template<>
134 static bool serialize(std::ostream &output, const LightComponent *obj) {
135 if (!obj) return false;
136
137 nlohmann::json j = {
138 {"light_type", obj->get_light_type()},
139 {"color", obj->get_color()},
140 {"intensity", obj->get_intensity()},
141 {"should_cast_shadows", obj->should_cast_shadows()},
142 };
143
145 j["range"] = obj->get_range();
146 j["attenuation"] = obj->get_attenuation();
147 }
148
149 output << j.dump(2);
150
151 return output.good();
152 }
153
154 static bool deserialize(std::istream &input, LightComponent *obj) {
155 if (!obj) return false;
156
157 try {
158 nlohmann::json j;
159 input >> j;
160
161 obj->set_light_type(j.value("light_type", LightComponent::POINT));
162 obj->set_color(j.value("color", glm::vec3(0)));
163 obj->set_intensity(j.value("intensity", 1.0f));
164 obj->set_cast_shadows(j.value("should_cast_shadows", false));
165
167 obj->set_range(j.value("range", 0.0f));
168 obj->set_attenuation(j.value("attenuation", 0.0f));
169 }
170
171 return true;
172 } catch (...) {
173 return false;
174 }
175 }
176 };
177}
void import_all_pending()
Import all unprocessed assets in registry.
Registry for storing assets.
void set_intensity(const float intensity)
LightType get_light_type() const
void set_range(const float range)
void set_attenuation(const float attenuation)
void set_cast_shadows(const bool cast_shadows)
void set_light_type(const LightType type)
void set_mesh_asset(AssetID id)
const std::string & get_name() const
Definition Project.h:50
std::unique_ptr< AssetManager > asset_manager_
Definition Project.h:62
const ProjectMetadata & get_metadata() const
Definition Project.h:47
ProjectMetadata & get_metadata()
Definition Project.h:48
std::filesystem::path get_assets_path() const
Definition Project.cpp:136
std::filesystem::path get_settings_path() const
Definition Project.cpp:144
AssetRegistry * get_asset_registry() const
Definition Project.h:54
void cleanup_managers()
Definition Project.cpp:196
std::unique_ptr< SceneManager > scene_manager_
Definition Project.h:60
std::vector< std::filesystem::path > recent_scenes_
Definition Project.h:65
void initialize_default_assets()
Definition Project.cpp:157
static std::unique_ptr< Project > create(const std::string &name, const std::filesystem::path &location)
Definition Project.cpp:37
const std::string & get_version() const
Definition Project.h:51
static std::unique_ptr< Project > load_data(const std::filesystem::path &project_file)
Definition Project.cpp:61
std::unique_ptr< Renderer > scene_renderer_
Definition Project.h:63
std::unique_ptr< AssetRegistry > asset_registry_
Definition Project.h:61
Project(const ProjectMetadata &metadata)
Definition Project.cpp:31
SceneManager * get_scene_manager() const
Definition Project.h:53
ProjectMetadata metadata_
Definition Project.h:56
void create_directory_structure() const
Definition Project.cpp:148
std::filesystem::path project_root_path_
Definition Project.h:57
std::filesystem::path project_file_path_
Definition Project.h:58
Project(const std::string &name)
Definition Project.cpp:21
void initialize_managers()
Definition Project.cpp:161
std::filesystem::path get_scenes_path() const
Definition Project.cpp:140
std::filesystem::path get_project_root() const
Definition Project.cpp:132
static std::string get_current_timestamp()
Definition Project.cpp:204
void set_position(float x, float y, float z)
void set_rotation(float x, float y, float z)
constexpr AssetID INVALID_ASSET_ID
std::filesystem::path renderer_settings
Definition Project.h:23
std::optional< AssetID > last_scene
Definition Project.h:21
std::string created_at
Definition Project.h:19
std::string last_opened
Definition Project.h:20
std::string engine_version
Definition Project.h:18
std::filesystem::path default_scene
Definition Project.h:22
static bool serialize(std::ostream &output, const ProjectMetadata *obj)
static bool deserialize(std::istream &input, ProjectMetadata *obj)