-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScriptableObject.cpp
More file actions
148 lines (137 loc) · 4.48 KB
/
Copy pathScriptableObject.cpp
File metadata and controls
148 lines (137 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "ScriptableObject.h"
#include "Vector.h"
namespace sh::game
{
SH_GAME_API auto ScriptableObject::Serialize() const -> core::Json
{
core::Json mainJson = Super::Serialize();
const core::reflection::STypeInfo* type = &GetType();
while (type != nullptr)
{
core::Json& json = mainJson[type->name.ToString()];
for (auto& prop : type->GetProperties())
{
if (prop->bNoSaveProperty)
continue;
const core::reflection::TypeInfo& propType = prop->type;
const core::Name& name = prop->GetName();
if (propType == core::reflection::GetType<Vec4>())
core::SerializeProperty(json, name, *prop->Get<Vec4>(*this));
else if (propType == core::reflection::GetType<Vec3>())
core::SerializeProperty(json, name, *prop->Get<Vec3>(*this));
else if (propType == core::reflection::GetType<Vec2>())
core::SerializeProperty(json, name, *prop->Get<Vec2>(*this));
else if (prop->isContainer)
{
auto& containerJson = json[name];
if (*prop->containerElementType == core::reflection::GetType<Vec4>())
{
for (auto it = prop->Begin(*this); it != prop->End(*this); ++it)
{
core::Json vecJson;
vecJson.push_back(it.Get<Vec4>()->x);
vecJson.push_back(it.Get<Vec4>()->y);
vecJson.push_back(it.Get<Vec4>()->z);
vecJson.push_back(it.Get<Vec4>()->w);
containerJson.push_back(std::move(vecJson));
}
}
else if (*prop->containerElementType == core::reflection::GetType<Vec3>())
{
for (auto it = prop->Begin(*this); it != prop->End(*this); ++it)
{
core::Json vecJson;
vecJson.push_back(it.Get<Vec3>()->x);
vecJson.push_back(it.Get<Vec3>()->y);
vecJson.push_back(it.Get<Vec3>()->z);
containerJson.push_back(std::move(vecJson));
}
}
else if (*prop->containerElementType == core::reflection::GetType<Vec2>())
{
for (auto it = prop->Begin(*this); it != prop->End(*this); ++it)
{
core::Json vecJson;
vecJson.push_back(it.Get<Vec2>()->x);
vecJson.push_back(it.Get<Vec2>()->y);
containerJson.push_back(std::move(vecJson));
}
}
}
}
type = type->super;
}
mainJson["fullType"] = GetType().type.name;
return mainJson;
}
SH_GAME_API void ScriptableObject::Deserialize(const core::Json& json)
{
Super::Deserialize(json);
const core::reflection::STypeInfo* type = &GetType();
while (type)
{
if (!json.contains(type->name))
{
type = type->super;
continue;
}
const core::Json& compJson = json[type->name.ToString()];
for (auto& prop : type->GetProperties())
{
if (prop->bNoSaveProperty)
continue;
const core::reflection::TypeInfo& propType = prop->type;
const std::string& nameStr = prop->GetName().ToString();
if (propType == core::reflection::GetType<Vec4>() && compJson[nameStr].size() == 4)
{
if (core::DeserializeProperty(compJson, nameStr, *prop->Get<Vec4>(*this)))
OnPropertyChanged(*prop.get());
}
else if (propType == core::reflection::GetType<Vec3>() && compJson[nameStr].size() == 3)
{
if (core::DeserializeProperty(compJson, nameStr, *prop->Get<Vec3>(*this)))
OnPropertyChanged(*prop.get());
}
else if (propType == core::reflection::GetType<Vec2>() && compJson[nameStr].size() == 2)
{
if (core::DeserializeProperty(compJson, nameStr, *prop->Get<Vec2>(*this)))
OnPropertyChanged(*prop.get());
}
else if (prop->isContainer)
{
if (!compJson.contains(nameStr))
continue;
const auto& containerJson = compJson[nameStr];
if (*prop->containerElementType == core::reflection::GetType<Vec4>())
{
prop->ClearContainer(*this);
for (auto& vecJson : containerJson)
{
if (vecJson.size() == 4)
prop->InsertToContainer(*this, Vec4{ vecJson[0], vecJson[1], vecJson[2], vecJson[3] });
}
}
else if (*prop->containerElementType == core::reflection::GetType<Vec3>())
{
prop->ClearContainer(*this);
for (auto& vecJson : containerJson)
{
if (vecJson.size() == 3)
prop->InsertToContainer(*this, Vec3{ vecJson[0], vecJson[1], vecJson[2] });
}
}
else if (*prop->containerElementType == core::reflection::GetType<Vec2>())
{
prop->ClearContainer(*this);
for (auto& vecJson : containerJson)
{
if (vecJson.size() == 2)
prop->InsertToContainer(*this, Vec3{ vecJson[0], vecJson[1] });
}
}
}
}
type = type->super;
}
}
}//namespace