#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()) core::SerializeProperty(json, name, *prop->Get(*this)); else if (propType == core::reflection::GetType()) core::SerializeProperty(json, name, *prop->Get(*this)); else if (propType == core::reflection::GetType()) core::SerializeProperty(json, name, *prop->Get(*this)); else if (prop->isContainer) { auto& containerJson = json[name]; if (*prop->containerElementType == core::reflection::GetType()) { for (auto it = prop->Begin(*this); it != prop->End(*this); ++it) { core::Json vecJson; vecJson.push_back(it.Get()->x); vecJson.push_back(it.Get()->y); vecJson.push_back(it.Get()->z); vecJson.push_back(it.Get()->w); containerJson.push_back(std::move(vecJson)); } } else if (*prop->containerElementType == core::reflection::GetType()) { for (auto it = prop->Begin(*this); it != prop->End(*this); ++it) { core::Json vecJson; vecJson.push_back(it.Get()->x); vecJson.push_back(it.Get()->y); vecJson.push_back(it.Get()->z); containerJson.push_back(std::move(vecJson)); } } else if (*prop->containerElementType == core::reflection::GetType()) { for (auto it = prop->Begin(*this); it != prop->End(*this); ++it) { core::Json vecJson; vecJson.push_back(it.Get()->x); vecJson.push_back(it.Get()->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() && compJson[nameStr].size() == 4) { if (core::DeserializeProperty(compJson, nameStr, *prop->Get(*this))) OnPropertyChanged(*prop.get()); } else if (propType == core::reflection::GetType() && compJson[nameStr].size() == 3) { if (core::DeserializeProperty(compJson, nameStr, *prop->Get(*this))) OnPropertyChanged(*prop.get()); } else if (propType == core::reflection::GetType() && compJson[nameStr].size() == 2) { if (core::DeserializeProperty(compJson, nameStr, *prop->Get(*this))) OnPropertyChanged(*prop.get()); } else if (prop->isContainer) { if (!compJson.contains(nameStr)) continue; const auto& containerJson = compJson[nameStr]; if (*prop->containerElementType == core::reflection::GetType()) { 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()) { 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()) { prop->ClearContainer(*this); for (auto& vecJson : containerJson) { if (vecJson.size() == 2) prop->InsertToContainer(*this, Vec3{ vecJson[0], vecJson[1] }); } } } } type = type->super; } } }//namespace