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
8#include "hellfire/ecs/LightComponent.h"
9#include "hellfire/ecs/RenderableComponent.h"
10#include "hellfire/ecs/TransformComponent.h"
11#include "hellfire/ecs/components/MeshComponent.h"
12#include "hellfire/scene/Scene.h"
13#include "hellfire/utilities/SerializerUtils.h"
14
15namespace hellfire {
16 template<typename T>
17 struct Serializer {
18 static bool serialize(std::ostream &output, const T *obj);
19
20 static bool deserialize(std::istream &input, T *obj);
21 };
22
23 template<>
25 static bool serialize(std::ostream &output, const TransformComponent *obj) {
26 if (obj == nullptr) return false;
27
28 const nlohmann::json j = {
29 {"position", obj->get_position()},
30 {"rotation", obj->get_rotation()},
31 {"scale", obj->get_scale()}
32 };
33
34 output << j.dump(4);
35 return output.good();
36 }
37
38 static bool deserialize(std::istream &input, TransformComponent *obj) {
39 try {
40 if (obj == nullptr) return false;
41
42 nlohmann::json j;
43 input >> j;
44
45 const auto position = json_get_vec3(j, "position");
46 const auto rotation = json_get_vec3(j, "rotation");
47 const auto scale = json_get_vec3(j, "scale");
48
49 if (!position || !rotation || !scale) return false;
50
51 obj->set_position(*position);
52 obj->set_rotation(*rotation);
53 obj->set_scale(*scale);
54
55 return true;
56 } catch (...) {
57 return false;
58 }
59 }
60 };
61
62 template<>
63 struct Serializer<MeshComponent> {
64 static bool serialize(std::ostream &output, const MeshComponent *obj) {
65 if (!obj) return false;
66
67 nlohmann::json j = {
68 {"mesh_asset", obj->get_mesh_asset()},
69 {"is_wireframe", obj->is_wireframe}
70
71 };
72
73 output << j.dump(4);
74 return output.good();
75 }
76
77 static bool deserialize(std::istream &input, MeshComponent *obj) {
78 if (!obj) return false;
79
80 try {
81 nlohmann::json j;
82 input >> j;
83
84 obj->set_mesh_asset(j.value("mesh_asset", INVALID_ASSET_ID));
85 obj->is_wireframe = j.value("is_wireframe", false);
86
87 return true;
88 } catch (...) {
89 return false;
90 }
91 }
92 };
93
94 template<>
95 struct Serializer<RenderableComponent> {
96 static bool serialize(std::ostream &output, const RenderableComponent *obj) {
97 if (!obj) return false;
98
99 nlohmann::json j = {
100 {"material_asset", obj->get_material_asset()},
101 {"cast_shadows", obj->cast_shadows},
102 {"receive_shadows", obj->receive_shadows},
103 {"visible", obj->visible},
104 {"render_layer", obj->render_layer}
105 };
106
107 output << j.dump(4);
108 return output.good();
109 }
110
111 static bool deserialize(std::istream &input, RenderableComponent *obj) {
112 if (!obj) return false;
113
114 try {
115 nlohmann::json j;
116 input >> j;
117
118 obj->set_material_asset(j.value("material_asset", INVALID_ASSET_ID));
119 obj->cast_shadows = j.value("cast_shadows", true);
120 obj->receive_shadows = j.value("receive_shadows", true);
121 obj->visible = j.value("visible", true);
122 obj->render_layer = j.value("render_layer", 0u);
123
124 return true;
125 } catch (...) {
126 return false;
127 }
128 }
129 };
130
131 template<>
133 static bool serialize(std::ostream &output, const LightComponent *obj) {
134 if (!obj) return false;
135
136 nlohmann::json j = {
137 {"light_type", obj->get_light_type()},
138 {"color", obj->get_color()},
139 {"intensity", obj->get_intensity()},
140 {"should_cast_shadows", obj->should_cast_shadows()},
141 };
142
144 j["range"] = obj->get_range();
145 j["attenuation"] = obj->get_attenuation();
146 }
147
148 output << j.dump(2);
149
150 return output.good();
151 }
152
153 static bool deserialize(std::istream &input, LightComponent *obj) {
154 if (!obj) return false;
155
156 try {
157 nlohmann::json j;
158 input >> j;
159
160 obj->set_light_type(j.value("light_type", LightComponent::POINT));
161 obj->set_color(j.value("color", glm::vec3(0)));
162 obj->set_intensity(j.value("intensity", 1.0f));
163 obj->set_cast_shadows(j.value("should_cast_shadows", false));
164
166 obj->set_range(j.value("range", 0.0f));
167 obj->set_attenuation(j.value("attenuation", 0.0f));
168 }
169
170 return true;
171 } catch (...) {
172 return false;
173 }
174 }
175 };
176}
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)