LkEngine 0.1.2
 
Loading...
Searching...
No Matches
YamlSerialization.h
1#pragma once
2
3#include "LkEngine/Asset/Asset.h"
4
5#include <yaml-cpp/yaml.h>
6
7#include <glm/gtx/quaternion.hpp>
8#include <glm/glm.hpp>
9
10
14namespace YAML
15{
16 template<>
17 struct convert<glm::vec2>
18 {
19 static Node encode(const glm::vec2& Rhs)
20 {
21 Node YamlNode;
22 YamlNode.push_back(Rhs.x);
23 YamlNode.push_back(Rhs.y);
24 return YamlNode;
25 }
26
27 static bool decode(const Node& YamlNode, glm::vec2& Rhs)
28 {
29 if (!YamlNode.IsSequence() || YamlNode.size() != 2)
30 {
31 return false;
32 }
33
34 Rhs.x = YamlNode[0].as<float>();
35 Rhs.y = YamlNode[1].as<float>();
36 return true;
37 }
38 };
39
40 template<>
41 struct convert<glm::vec3>
42 {
43 static Node encode(const glm::vec3& Rhs)
44 {
45 Node YamlNode;
46 YamlNode.push_back(Rhs.x);
47 YamlNode.push_back(Rhs.y);
48 YamlNode.push_back(Rhs.z);
49 return YamlNode;
50 }
51
52 static bool decode(const Node& YamlNode, glm::vec3& Rhs)
53 {
54 if (!YamlNode.IsSequence() || YamlNode.size() != 3)
55 {
56 return false;
57 }
58
59 Rhs.x = YamlNode[0].as<float>();
60 Rhs.y = YamlNode[1].as<float>();
61 Rhs.z = YamlNode[2].as<float>();
62 return true;
63 }
64 };
65
66 template<>
67 struct convert<glm::vec4>
68 {
69 static Node encode(const glm::vec4& Rhs)
70 {
71 Node YamlNode;
72 YamlNode.push_back(Rhs.x);
73 YamlNode.push_back(Rhs.y);
74 YamlNode.push_back(Rhs.z);
75 YamlNode.push_back(Rhs.w);
76 return YamlNode;
77 }
78
79 static bool decode(const Node& YamlNode, glm::vec4& Rhs)
80 {
81 if (!YamlNode.IsSequence() || YamlNode.size() != 4)
82 {
83 return false;
84 }
85
86 Rhs.x = YamlNode[0].as<float>();
87 Rhs.y = YamlNode[1].as<float>();
88 Rhs.z = YamlNode[2].as<float>();
89 Rhs.w = YamlNode[3].as<float>();
90 return true;
91 }
92 };
93
94 template<>
95 struct convert<glm::quat>
96 {
97 static Node encode(const glm::quat& Rhs)
98 {
99 Node YamlNode;
100 YamlNode.push_back(Rhs.w);
101 YamlNode.push_back(Rhs.x);
102 YamlNode.push_back(Rhs.y);
103 YamlNode.push_back(Rhs.z);
104 return YamlNode;
105 }
106
107 static bool decode(const Node& YamlNode, glm::quat& Rhs)
108 {
109 if (!YamlNode.IsSequence() || YamlNode.size() != 4)
110 {
111 return false;
112 }
113
114 Rhs.w = YamlNode[0].as<float>();
115 Rhs.x = YamlNode[1].as<float>();
116 Rhs.y = YamlNode[2].as<float>();
117 Rhs.z = YamlNode[3].as<float>();
118 return true;
119 }
120 };
121
122 template<>
123 struct convert<LkEngine::LUUID>
124 {
125 static Node encode(const LkEngine::LUUID& Rhs)
126 {
127 Node YamlNode;
128 YamlNode.push_back(static_cast<uint64_t>(Rhs));
129 return YamlNode;
130 }
131
132 static bool decode(const Node& YamlNode, LkEngine::LUUID& Rhs)
133 {
134 Rhs = YamlNode.as<uint64_t>();
135 return true;
136 }
137 };
138
139 /* LMesh / LMeshSource / LStaticMesh */
140 template<>
141 struct convert<std::vector<uint32_t>>
142 {
143 static Node encode(const std::vector<uint32_t>& Value)
144 {
145 Node YamlNode;
146 for (uint32_t Element : Value)
147 {
148 YamlNode.push_back(Element);
149 }
150
151 return YamlNode;
152 }
153
154 static bool decode(const Node& NodeRef, std::vector<uint32_t>& Result)
155 {
156 if (!NodeRef.IsSequence())
157 {
158 return false;
159 }
160
161 Result.resize(NodeRef.size());
162 for (std::size_t i = 0; i < NodeRef.size(); i++)
163 {
164 Result[i] = NodeRef[i].as<uint32_t>();
165 }
166
167 return true;
168 }
169 };
170
171 template<>
172 struct convert<std::chrono::seconds>
173 {
174 static Node encode(const std::chrono::seconds& Rhs)
175 {
176 Node YamlNode;
177 YamlNode.push_back(Rhs.count());
178 return YamlNode;
179 }
180
181 static bool decode(const Node& InNode, std::chrono::seconds& Rhs)
182 {
183 if (!InNode.IsScalar())
184 {
185 return false;
186 }
187
188 Rhs = std::chrono::seconds(InNode.as<int>());
189 return true;
190 }
191 };
192
193
194}
195
196namespace LkEngine {
197
198 inline YAML::Emitter& operator<<(YAML::Emitter& Out, const glm::vec2& InVec2)
199 {
200 Out << YAML::Flow;
201 Out << YAML::BeginSeq << InVec2.x << InVec2.y << YAML::EndSeq;
202 return Out;
203 }
204
205 inline YAML::Emitter& operator<<(YAML::Emitter& Out, const glm::vec3& InVec3)
206 {
207 Out << YAML::Flow;
208 Out << YAML::BeginSeq << InVec3.x << InVec3.y << InVec3.z << YAML::EndSeq;
209 return Out;
210 }
211
212 inline YAML::Emitter& operator<<(YAML::Emitter& Out, const glm::vec4& InVec4)
213 {
214 Out << YAML::Flow;
215 Out << YAML::BeginSeq << InVec4.x << InVec4.y << InVec4.z << InVec4.w << YAML::EndSeq;
216 return Out;
217 }
218
219 /* LMesh / LMeshSource / LStaticMesh */
220 inline YAML::Emitter& operator<<(YAML::Emitter& Out, const std::vector<uint32_t>& Value)
221 {
222 Out << YAML::Flow;
223 Out << YAML::BeginSeq;
224 for (const uint32_t Element : Value)
225 {
226 Out << Element;
227 }
228 Out << YAML::EndSeq;
229
230 return Out;
231 }
232
233}
Definition Asset.h:11
Definition ImGuiFwd.h:127
Definition UUID.h:33
Definition UUID.h:13