Skip to content

Commit

Permalink
Mono scripts are now serializable, but not deserializable
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunkTreasure1 committed Sep 21, 2023
1 parent 814eb89 commit 1e35e8f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
69 changes: 66 additions & 3 deletions Volt/Volt/src/Volt/Asset/Importers/SceneImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1427,6 +1427,8 @@
#include "Volt/Scene/Reflection/ComponentReflection.h"
#include "Volt/Scene/Reflection/ComponentRegistry.h"

#include "Volt/Scripting/Mono/MonoScriptClass.h"

namespace Volt
{
template<typename T>
Expand Down Expand Up @@ -1682,7 +1684,7 @@ namespace Volt
{
continue;
}

const uint8_t* componentPtr = reinterpret_cast<const uint8_t*>(storage.get(id));
SerializeClass(componentPtr, 0, componentDesc, streamWriter, false);
}
Expand Down Expand Up @@ -1833,8 +1835,22 @@ namespace Volt
for (const auto& [name, value] : fieldMap)
{
streamWriter.BeginMap();
streamWriter.SetKey("name", name);

if (value->field.type.IsString())
{
auto cStr = value->data.As<const char>();
std::string str(cStr);

// #TODO_Ivar: Finish implementation
streamWriter.SetKey("data", str);
}
else
{
if (s_typeSerializers.contains(value->field.type.typeIndex))
{
s_typeSerializers.at(value->field.type.typeIndex)(streamWriter, value->data.As<const uint8_t>(), 0);
}
}

streamWriter.EndMap();
}
Expand Down Expand Up @@ -1892,6 +1908,11 @@ namespace Volt

});

if (scene->GetRegistry().any_of<MonoScriptComponent>(entityId))
{
DeserializeMono(entityId, scene, streamReader);
}

streamReader.ExitScope();
}

Expand Down Expand Up @@ -1958,9 +1979,10 @@ namespace Volt
return;
}

streamReader.ForEach("values", [&]()
streamReader.ForEach("values", [&]()
{
uint8_t* tempDataStorage = new uint8_t[arrayDesc->GetElementTypeSize()];
memset(tempDataStorage, 0, arrayDesc->GetElementTypeSize());

if (isNonDefaultType)
{
Expand Down Expand Up @@ -1997,4 +2019,45 @@ namespace Volt
delete[] tempDataStorage;
});
}

void SceneImporter::DeserializeMono(entt::entity id, const Ref<Scene>& scene, YAMLStreamReader& streamReader) const
{
streamReader.ForEach("MonoScripts", [&]()
{
streamReader.EnterScope("ScriptEntry");

const std::string scriptName = streamReader.ReadKey("name", std::string(""));
const UUID scriptId = streamReader.ReadKey("id", UUID(0));

auto& fieldCache = scene->GetScriptFieldCache().GetCache()[scriptId];

streamReader.ForEach("members", [&]()
{
const std::string memberName = streamReader.ReadKey("name", std::string(""));

if (!fieldCache.contains(memberName))
{
return;
}

auto& field = fieldCache.at(memberName);

if (field->field.type.IsString())
{
const std::string str = streamReader.ReadKey("data", std::string(""));
field->SetValue(str, str.size());
}
else
{
if (s_typeDeserializers.contains(field->field.type.typeIndex))
{
field->data.Allocate(field->field.type.typeSize);
s_typeDeserializers.at(field->field.type.typeIndex)(streamReader, field->data.As<uint8_t>(), 0);
}
}
});

streamReader.ExitScope();
});
}
}
1 change: 1 addition & 0 deletions Volt/Volt/src/Volt/Asset/Importers/SceneImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ namespace Volt
void DeserializeEntity(const Ref<Scene>& scene, const AssetMetadata& metadata, YAMLStreamReader& streamReader) const;
void DeserializeClass(uint8_t* data, const size_t offset, const IComponentTypeDesc* compDesc, YAMLStreamReader& streamReader) const;
void DeserializeArray(uint8_t* data, const size_t offset, const IArrayTypeDesc* arrayDesc, YAMLStreamReader& streamReader) const;
void DeserializeMono(entt::entity id, const Ref<Scene>& scene, YAMLStreamReader& streamReader) const;

inline static std::unordered_map<std::type_index, std::function<void(YAMLStreamWriter&, const uint8_t*, const size_t)>> s_typeSerializers;
inline static std::unordered_map<std::type_index, std::function<void(YAMLStreamReader&, uint8_t*, const size_t)>> s_typeDeserializers;
Expand Down

0 comments on commit 1e35e8f

Please sign in to comment.