Skip to content

Commit

Permalink
Divided YAMLStream utilities and improved serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunkTreasure1 committed Mar 7, 2024
1 parent b770625 commit 3cad686
Show file tree
Hide file tree
Showing 22 changed files with 204 additions and 112 deletions.
26 changes: 13 additions & 13 deletions Volt/Sandbox/src/Sandbox/Modals/ProjectUpgradeModal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <Volt/Asset/Prefab.h>

#include <Volt/Utility/UIUtility.h>
#include <Volt/Utility/FileIO/YAMLStreamReader.h>
#include <Volt/Utility/FileIO/YAMLFileStreamReader.h>

#include <Volt/Asset/Asset.h>
#include <Volt/Asset/Animation/AnimatedCharacter.h>
Expand Down Expand Up @@ -58,15 +58,15 @@ struct TypeIndexContainer
};

static std::unordered_map<PreV113PropertyType, TypeIndexContainer> s_preV113PropTypeToTypeIndexMap;
static std::unordered_map<std::type_index, std::function<void(Volt::YAMLStreamReader&, uint8_t*, const size_t)>> s_arrayDeserializers;
static std::unordered_map<std::type_index, std::function<void(Volt::YAMLFileStreamReader&, uint8_t*, const size_t)>> s_arrayDeserializers;
static std::unordered_map<VoltGUID, std::unordered_map<std::string, std::string>> s_componentMemberRemap;

static bool s_initialize = false;

template<typename T>
void RegisterArrayDeserializationFunction()
{
s_arrayDeserializers[std::type_index{ typeid(T) }] = [](Volt::YAMLStreamReader& streamReader, uint8_t* data, const size_t offset)
s_arrayDeserializers[std::type_index{ typeid(T) }] = [](Volt::YAMLFileStreamReader& streamReader, uint8_t* data, const size_t offset)
{
*reinterpret_cast<T*>(&data[offset]) = streamReader.ReadKey("value", T());
};
Expand Down Expand Up @@ -291,7 +291,7 @@ void ProjectUpgradeModal::ConvertAnimationGraphsToV0_1_2()
{
for (const auto& characterPath : characterAssets)
{
Volt::YAMLStreamReader metaStreamReader{};
Volt::YAMLFileStreamReader metaStreamReader{};

if (!metaStreamReader.OpenFile(characterPath.string() + ".vtmeta"))
{
Expand All @@ -306,7 +306,7 @@ void ProjectUpgradeModal::ConvertAnimationGraphsToV0_1_2()

if (characterAssetHandle == handle)
{
Volt::YAMLStreamReader charStreamReader{};
Volt::YAMLFileStreamReader charStreamReader{};

if (!charStreamReader.OpenFile(characterPath))
{
Expand Down Expand Up @@ -337,7 +337,7 @@ void ProjectUpgradeModal::ConvertAnimationGraphsToV0_1_2()
AnimGraphDescriptor& descriptor = animGraphDescriptors.emplace_back();
{ // convert the animgraph
{// parse meta
Volt::YAMLStreamReader metaStreamReader{};
Volt::YAMLFileStreamReader metaStreamReader{};

if (!metaStreamReader.OpenFile(animGraphPath.string() + ".vtmeta"))
{
Expand Down Expand Up @@ -677,7 +677,7 @@ void ProjectUpgradeModal::ConvertScenesToV113()

// Load scene name
{
Volt::YAMLStreamReader streamReader{};
Volt::YAMLFileStreamReader streamReader{};
if (!streamReader.OpenFile(sceneFilePath))
{
VT_CORE_ERROR("[Project Upgrade]: Unable to open scene file! Skipping!");
Expand Down Expand Up @@ -717,7 +717,7 @@ void ProjectUpgradeModal::ConvertScenesToV113()

void ProjectUpgradeModal::ConvertPreV113Prefab(const std::filesystem::path& filePath)
{
Volt::YAMLStreamReader streamReader{};
Volt::YAMLFileStreamReader streamReader{};
if (!streamReader.OpenFile(filePath))
{
return;
Expand Down Expand Up @@ -768,7 +768,7 @@ void ProjectUpgradeModal::ConvertPreV113Prefab(const std::filesystem::path& file

void ProjectUpgradeModal::DeserializePreV113SceneLayer(Ref<Volt::Scene> scene, Volt::SceneLayer& sceneLayer, const std::filesystem::path& layerPath, std::map<Volt::EntityID, Volt::EntityID>& entityRemapping)
{
Volt::YAMLStreamReader streamReader{};
Volt::YAMLFileStreamReader streamReader{};

if (!streamReader.OpenFile(layerPath))
{
Expand All @@ -790,7 +790,7 @@ void ProjectUpgradeModal::DeserializePreV113SceneLayer(Ref<Volt::Scene> scene, V
streamReader.ExitScope();
}

void ProjectUpgradeModal::DeserializePreV113Entity(Ref<Volt::Scene> scene, Volt::YAMLStreamReader& streamReader, std::map<Volt::EntityID, Volt::EntityID>& entityRemapping, bool isPrefabEntity)
void ProjectUpgradeModal::DeserializePreV113Entity(Ref<Volt::Scene> scene, Volt::YAMLFileStreamReader& streamReader, std::map<Volt::EntityID, Volt::EntityID>& entityRemapping, bool isPrefabEntity)
{
if (!isPrefabEntity)
{
Expand Down Expand Up @@ -927,7 +927,7 @@ void ProjectUpgradeModal::DeserializePreV113Entity(Ref<Volt::Scene> scene, Volt:
}
}

void ProjectUpgradeModal::DeserializePreV113Component(uint8_t* componentData, const Volt::IComponentTypeDesc* componentDesc, Volt::YAMLStreamReader& streamReader)
void ProjectUpgradeModal::DeserializePreV113Component(uint8_t* componentData, const Volt::IComponentTypeDesc* componentDesc, Volt::YAMLFileStreamReader& streamReader)
{
const auto& typeDeserializers = Volt::SceneImporter::GetTypeDeserializers();

Expand Down Expand Up @@ -1009,7 +1009,7 @@ void ProjectUpgradeModal::DeserializePreV113Component(uint8_t* componentData, co
});
}

void ProjectUpgradeModal::DeserializePreV113MonoScripts(Ref<Volt::Scene> scene, const Volt::EntityID entityId, Volt::YAMLStreamReader& streamReader)
void ProjectUpgradeModal::DeserializePreV113MonoScripts(Ref<Volt::Scene> scene, const Volt::EntityID entityId, Volt::YAMLFileStreamReader& streamReader)
{
Volt::Entity entity = scene->GetEntityFromUUID(entityId);
const auto& typeDeserializers = Volt::SceneImporter::GetTypeDeserializers();
Expand Down Expand Up @@ -1288,7 +1288,7 @@ const Volt::ComponentMember* ProjectUpgradeModal::TryGetComponentMemberFromName(

std::pair<std::filesystem::path, Volt::AssetHandle> ProjectUpgradeModal::DeserializeV0MetaFile(const std::filesystem::path& metaPath)
{
Volt::YAMLStreamReader streamReader{};
Volt::YAMLFileStreamReader streamReader{};

if (!streamReader.OpenFile(metaPath))
{
Expand Down
8 changes: 4 additions & 4 deletions Volt/Sandbox/src/Sandbox/Modals/ProjectUpgradeModal.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Volt

class Scene;
struct SceneLayer;
class YAMLStreamReader;
class YAMLFileStreamReader;
}

class ProjectUpgradeModal : public Modal
Expand All @@ -40,9 +40,9 @@ class ProjectUpgradeModal : public Modal
void ConvertPreV113Prefab(const std::filesystem::path& filePath);

void DeserializePreV113SceneLayer(Ref<Volt::Scene> scene, Volt::SceneLayer& sceneLayer, const std::filesystem::path& layerPath, std::map<Volt::EntityID, Volt::EntityID>& entityRemapping);
void DeserializePreV113Entity(Ref<Volt::Scene> scene, Volt::YAMLStreamReader& streamReader, std::map<Volt::EntityID, Volt::EntityID>& entityRemapping, bool isPrefabEntity);
void DeserializePreV113Component(uint8_t* componentData, const Volt::IComponentTypeDesc* componentDesc, Volt::YAMLStreamReader& streamReader);
void DeserializePreV113MonoScripts(Ref<Volt::Scene> scene, const Volt::EntityID entityId, Volt::YAMLStreamReader& streamReader);
void DeserializePreV113Entity(Ref<Volt::Scene> scene, Volt::YAMLFileStreamReader& streamReader, std::map<Volt::EntityID, Volt::EntityID>& entityRemapping, bool isPrefabEntity);
void DeserializePreV113Component(uint8_t* componentData, const Volt::IComponentTypeDesc* componentDesc, Volt::YAMLFileStreamReader& streamReader);
void DeserializePreV113MonoScripts(Ref<Volt::Scene> scene, const Volt::EntityID entityId, Volt::YAMLFileStreamReader& streamReader);

void HandleEntityRemapping(Ref<Volt::Scene> scene, const std::map<Volt::EntityID, Volt::EntityID>& entityRemapping);
void HandleEntityArrayRemapping(Ref<Volt::Scene> scene, const std::map<Volt::EntityID, Volt::EntityID>& entityRemapping, const Volt::ComponentMember& componentMember, uint8_t* componentData);
Expand Down
4 changes: 2 additions & 2 deletions Volt/Volt/src/Volt/Asset/Importers/PrefabImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "Volt/Asset/Importers/SceneImporter.h"

#include "Volt/Utility/FileIO/YAMLFileStreamWriter.h"
#include "Volt/Utility/FileIO/YAMLStreamReader.h"
#include "Volt/Utility/FileIO/YAMLFileStreamReader.h"

namespace Volt
{
Expand All @@ -30,7 +30,7 @@ namespace Volt

Ref<Scene> prefabScene = CreateRef<Scene>();

YAMLStreamReader streamReader{};
YAMLFileStreamReader streamReader{};
if (!streamReader.OpenFile(filePath))
{
VT_CORE_ERROR("Failed to open file {0}!", metadata.filePath);
Expand Down
24 changes: 12 additions & 12 deletions Volt/Volt/src/Volt/Asset/Importers/SceneImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ namespace Volt
}

template<typename T>
void RegisterDeserializationFunction(std::unordered_map<std::type_index, std::function<void(YAMLStreamReader&, uint8_t*, const size_t)>>& outTypes)
void RegisterDeserializationFunction(std::unordered_map<std::type_index, std::function<void(YAMLFileStreamReader&, uint8_t*, const size_t)>>& outTypes)
{
VT_PROFILE_FUNCTION();

outTypes[std::type_index{ typeid(T) }] = [](YAMLStreamReader& streamReader, uint8_t* data, const size_t offset)
outTypes[std::type_index{ typeid(T) }] = [](YAMLFileStreamReader& streamReader, uint8_t* data, const size_t offset)
{
*reinterpret_cast<T*>(&data[offset]) = streamReader.ReadKey("data", T());
};
Expand Down Expand Up @@ -136,7 +136,7 @@ namespace Volt
return false;
}

YAMLStreamReader streamReader{};
YAMLFileStreamReader streamReader{};

if (!streamReader.OpenFile(filePath))
{
Expand Down Expand Up @@ -244,7 +244,7 @@ namespace Volt

auto& sceneLayer = sceneLayers.emplace_back();

YAMLStreamReader streamReader{};
YAMLFileStreamReader streamReader{};
if (!streamReader.OpenFile(layerPath))
{
continue;
Expand Down Expand Up @@ -399,7 +399,7 @@ namespace Volt

const auto& path = entityPaths.at(i);

YAMLStreamReader streamReader{};
YAMLFileStreamReader streamReader{};
if (!streamReader.OpenFile(path))
{
return;
Expand Down Expand Up @@ -435,7 +435,7 @@ namespace Volt
streamWriter.EndMap();
}

void SceneImporter::DeserializeWorldEngine(const Ref<Scene>& scene, YAMLStreamReader& streamReader) const
void SceneImporter::DeserializeWorldEngine(const Ref<Scene>& scene, YAMLFileStreamReader& streamReader) const
{
auto& worldEngine = scene->m_worldEngine;

Expand Down Expand Up @@ -482,7 +482,7 @@ namespace Volt
{
const auto& path = entityPaths.at(i);

YAMLStreamReader streamReader{};
YAMLFileStreamReader streamReader{};
if (!streamReader.OpenFile(path))
{
return;
Expand Down Expand Up @@ -762,7 +762,7 @@ namespace Volt
streamWriter.EndSequence();
}

void SceneImporter::DeserializeEntity(const Ref<Scene>& scene, const AssetMetadata& metadata, YAMLStreamReader& streamReader) const
void SceneImporter::DeserializeEntity(const Ref<Scene>& scene, const AssetMetadata& metadata, YAMLFileStreamReader& streamReader) const
{
VT_PROFILE_FUNCTION();

Expand Down Expand Up @@ -856,7 +856,7 @@ namespace Volt
streamReader.ExitScope();
}

void SceneImporter::DeserializeClass(uint8_t* data, const size_t offset, const IComponentTypeDesc* compDesc, YAMLStreamReader& streamReader) const
void SceneImporter::DeserializeClass(uint8_t* data, const size_t offset, const IComponentTypeDesc* compDesc, YAMLFileStreamReader& streamReader) const
{
VT_PROFILE_FUNCTION();

Expand Down Expand Up @@ -909,7 +909,7 @@ namespace Volt
});
}

void SceneImporter::DeserializeArray(uint8_t* data, const size_t offset, const IArrayTypeDesc* arrayDesc, YAMLStreamReader& streamReader) const
void SceneImporter::DeserializeArray(uint8_t* data, const size_t offset, const IArrayTypeDesc* arrayDesc, YAMLFileStreamReader& streamReader) const
{
VT_PROFILE_FUNCTION();

Expand Down Expand Up @@ -966,7 +966,7 @@ namespace Volt
});
}

void SceneImporter::DeserializeMono(entt::entity id, const Ref<Scene>& scene, YAMLStreamReader& streamReader) const
void SceneImporter::DeserializeMono(entt::entity id, const Ref<Scene>& scene, YAMLFileStreamReader& streamReader) const
{
VT_PROFILE_FUNCTION();

Expand Down Expand Up @@ -1097,7 +1097,7 @@ namespace Volt

const auto& path = entityPaths.at(i);

YAMLStreamReader streamReader{};
YAMLFileStreamReader streamReader{};
if (!streamReader.OpenFile(path))
{
return;
Expand Down
14 changes: 7 additions & 7 deletions Volt/Volt/src/Volt/Asset/Importers/SceneImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "Volt/Asset/Importers/AssetImporter.h"

#include "Volt/Utility/FileIO/YAMLFileStreamWriter.h"
#include "Volt/Utility/FileIO/YAMLStreamReader.h"
#include "Volt/Utility/FileIO/YAMLFileStreamReader.h"

#include "Volt/Scene/Entity.h"

Expand Down Expand Up @@ -33,8 +33,8 @@ namespace Volt
void Save(const AssetMetadata& metadata, const Ref<Asset>& asset) const override;

void SerializeEntity(entt::entity id, const AssetMetadata& metadata, const Ref<Scene>& scene, YAMLFileStreamWriter& streamWriter) const;
void DeserializeEntity(const Ref<Scene>& scene, const AssetMetadata& metadata, YAMLStreamReader& streamReader) const;
void DeserializeMono(entt::entity id, const Ref<Scene>& scene, YAMLStreamReader& streamReader) const;
void DeserializeEntity(const Ref<Scene>& scene, const AssetMetadata& metadata, YAMLFileStreamReader& streamReader) const;
void DeserializeMono(entt::entity id, const Ref<Scene>& scene, YAMLFileStreamReader& streamReader) const;

void LoadWorldCell(const Ref<Scene>& scene, const WorldCell& worldCell) const;

Expand All @@ -51,7 +51,7 @@ namespace Volt
void LoadEntities(const AssetMetadata& metadata, const Ref<Scene>& scene, const std::filesystem::path& sceneDirectory) const;

void SerializeWorldEngine(const Ref<Scene>& scene, YAMLFileStreamWriter& streamWriter) const;
void DeserializeWorldEngine(const Ref<Scene>& scene, YAMLStreamReader& streamReader) const;
void DeserializeWorldEngine(const Ref<Scene>& scene, YAMLFileStreamReader& streamReader) const;

void LoadCellEntities(const AssetMetadata& metadata, const Ref<Scene>& scene, const std::filesystem::path& sceneDirectory) const;

Expand All @@ -61,10 +61,10 @@ namespace Volt
void SerializeArray(const uint8_t* data, const size_t offset, const IArrayTypeDesc* arrayDesc, YAMLFileStreamWriter& streamWriter) const;
void SerializeMono(entt::entity id, const Ref<Scene>& scene, YAMLFileStreamWriter& streamWriter) 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 DeserializeClass(uint8_t* data, const size_t offset, const IComponentTypeDesc* compDesc, YAMLFileStreamReader& streamReader) const;
void DeserializeArray(uint8_t* data, const size_t offset, const IArrayTypeDesc* arrayDesc, YAMLFileStreamReader& streamReader) const;

inline static std::unordered_map<std::type_index, std::function<void(YAMLFileStreamWriter&, 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;
inline static std::unordered_map<std::type_index, std::function<void(YAMLFileStreamReader&, uint8_t*, const size_t)>> s_typeDeserializers;
};
}
1 change: 1 addition & 0 deletions Volt/Volt/src/Volt/Asset/ImportersNew/AssetSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Volt
serializedMetadata.handle = metadata.handle;
serializedMetadata.type = metadata.type;
serializedMetadata.version = version;
serializedMetadata.dependencies = metadata.dependencies;

return streamWriter.Write(serializedMetadata);
}
Expand Down
40 changes: 35 additions & 5 deletions Volt/Volt/src/Volt/Asset/ImportersNew/SceneSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "Volt/Utility/FileSystem.h"

#include "Volt/Utility/FileIO/YAMLMemoryStreamWriter.h"
#include "Volt/Utility/FileIO/YAMLStreamReader.h"
#include "Volt/Utility/FileIO/YAMLFileStreamReader.h"

namespace Volt
{
Expand All @@ -40,11 +40,11 @@ namespace Volt
}

template<typename T>
void RegisterDeserializationFunction(std::unordered_map<std::type_index, std::function<void(YAMLStreamReader&, uint8_t*, const size_t)>>& outTypes)
void RegisterDeserializationFunction(std::unordered_map<std::type_index, std::function<void(YAMLFileStreamReader&, uint8_t*, const size_t)>>& outTypes)
{
VT_PROFILE_FUNCTION();

outTypes[std::type_index{ typeid(T) }] = [](YAMLStreamReader& streamReader, uint8_t* data, const size_t offset)
outTypes[std::type_index{ typeid(T) }] = [](YAMLFileStreamReader& streamReader, uint8_t* data, const size_t offset)
{
*reinterpret_cast<T*>(&data[offset]) = streamReader.ReadKey("data", T());
};
Expand Down Expand Up @@ -136,6 +136,11 @@ namespace Volt
directoryPath = directoryPath.parent_path();
}

if (!std::filesystem::exists(directoryPath))
{
std::filesystem::create_directories(directoryPath);
}

std::filesystem::path scenePath = directoryPath / (metadata.filePath.stem().string() + ".vtasset");

// Serialize scene file
Expand Down Expand Up @@ -164,13 +169,38 @@ namespace Volt
sceneFileWriter.Write(buffer);
buffer.Release();

sceneFileWriter.WriteToDisk(scenePath, true, compressedDataOffset); // #TODO_Ivar: Fix compressed offset
sceneFileWriter.WriteToDisk(scenePath, true, compressedDataOffset);
}

if (scene->m_sceneSettings.useWorldEngine)
{
SerializeEntities(metadata, scene, directoryPath);
}
}

bool SceneSerializer::Deserialize(const AssetMetadata& metadata, Ref<Asset> destinationAsset) const
{
return false;
Ref<Scene> scene = reinterpret_pointer_cast<Scene>(destinationAsset);

const auto filePath = AssetManager::GetFilesystemPath(metadata.filePath);

if (!std::filesystem::exists(filePath))
{
VT_CORE_ERROR("File {0} not found!", metadata.filePath);
destinationAsset->SetFlag(AssetFlag::Missing, true);
return false;
}

BinaryStreamReader streamReader{ filePath };
if (!streamReader.IsStreamValid())
{
VT_CORE_ERROR("Failed to open file {0}!", metadata.filePath);
destinationAsset->SetFlag(AssetFlag::Invalid, true);
return false;
}


return true;
}

void SceneSerializer::SerializeEntity(entt::entity id, const AssetMetadata& metadata, const Ref<Scene>& scene, YAMLMemoryStreamWriter& streamWriter) const
Expand Down
Loading

0 comments on commit 3cad686

Please sign in to comment.