Skip to content

Commit

Permalink
Implemented more serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunkTreasure1 committed Mar 12, 2024
1 parent a28c0cc commit 3658144
Show file tree
Hide file tree
Showing 12 changed files with 436 additions and 8 deletions.
1 change: 1 addition & 0 deletions Volt/Volt/src/Volt/Animation/BlendSpace.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace Volt

private:
friend class BlendSpaceImporter;
friend class BlendSpaceSerializer;

BlendSpaceDimension myDimension = BlendSpaceDimension::OneD;

Expand Down
6 changes: 3 additions & 3 deletions Volt/Volt/src/Volt/Asset/AssetManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ namespace Volt
m_assetImporters.emplace(AssetType::Font, CreateScope<FontImporter>()); // Done
m_assetImporters.emplace(AssetType::PhysicsMaterial, CreateScope<PhysicsMaterialImporter>()); // Done
m_assetImporters.emplace(AssetType::Video, CreateScope<VideoImporter>()); // Done
m_assetImporters.emplace(AssetType::BehaviorGraph, CreateScope<BehaviorTreeImporter>());
m_assetImporters.emplace(AssetType::BlendSpace, CreateScope<BlendSpaceImporter>());
m_assetImporters.emplace(AssetType::PostProcessingStack, CreateScope<PostProcessingStackImporter>());
m_assetImporters.emplace(AssetType::BehaviorGraph, CreateScope<BehaviorTreeImporter>()); // Done
m_assetImporters.emplace(AssetType::BlendSpace, CreateScope<BlendSpaceImporter>()); // Done
m_assetImporters.emplace(AssetType::PostProcessingStack, CreateScope<PostProcessingStackImporter>()); // Done
m_assetImporters.emplace(AssetType::PostProcessingMaterial, CreateScope<PostProcessingMaterialImporter>());
m_assetImporters.emplace(AssetType::NetContract, CreateScope<NetContractImporter>());

Expand Down
237 changes: 237 additions & 0 deletions Volt/Volt/src/Volt/Asset/ImportersNew/BehaviourTreeSerializer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
#include "vtpch.h"
#include "BehaviourTreeSerializer.h"

#include "Volt/Asset/AssetManager.h"
#include "Volt/BehaviorTree/BehaviorTree.hpp"

namespace Volt
{
constexpr uint32_t CURRENT_ASSET_VERSION = 1;

struct SerializedDecorator
{
UUID uuid;
std::string compareFunctionName;
BehaviorTree::eDecoratorType type;

static void Serialize(BinaryStreamWriter& streamWriter, const SerializedDecorator& data)
{
streamWriter.Write(data.uuid);
streamWriter.Write(data.compareFunctionName);
streamWriter.Write(data.type);
}

static void Deserialize(BinaryStreamReader& streamReader, SerializedDecorator& outData)
{
streamReader.Read(outData.uuid);
streamReader.Read(outData.compareFunctionName);
streamReader.Read(outData.type);
}
};

struct SerializedLeaf
{
UUID uuid;
std::string functionName;

static void Serialize(BinaryStreamWriter& streamWriter, const SerializedLeaf& data)
{
streamWriter.Write(data.uuid);
streamWriter.Write(data.functionName);
}

static void Deserialize(BinaryStreamReader& streamReader, SerializedLeaf& outData)
{
streamReader.Read(outData.uuid);
streamReader.Read(outData.functionName);
}
};

struct SerializedLink
{
UUID id;
UUID parentId;
UUID childId;
};

struct SerializedPosition
{
UUID id;
std::string positionString;

static void Serialize(BinaryStreamWriter& streamWriter, const SerializedPosition& data)
{
streamWriter.Write(data.id);
streamWriter.Write(data.positionString);
}

static void Deserialize(BinaryStreamReader& streamReader, SerializedPosition& outData)
{
streamReader.Read(outData.id);
streamReader.Read(outData.positionString);
}
};

struct BehaviourTreeSerializationData
{
std::vector<UUID> sequenceIDs;
std::vector<UUID> selectorIDs;
std::vector<SerializedDecorator> decorators;
std::vector<SerializedLeaf> leafs;
std::vector<SerializedLink> links;
std::vector<SerializedPosition> positions;

static void Serialize(BinaryStreamWriter& streamWriter, const BehaviourTreeSerializationData& data)
{
streamWriter.Write(data.sequenceIDs);
streamWriter.Write(data.selectorIDs);
streamWriter.Write(data.decorators);
streamWriter.Write(data.leafs);
streamWriter.WriteRaw(data.links);
streamWriter.Write(data.positions);
}

static void Deserialize(BinaryStreamReader& streamReader, BehaviourTreeSerializationData& outData)
{
streamReader.Read(outData.sequenceIDs);
streamReader.Read(outData.selectorIDs);
streamReader.Read(outData.decorators);
streamReader.Read(outData.leafs);
streamReader.ReadRaw(outData.links);
streamReader.Read(outData.positions);
}
};

void BehaviourTreeSerializer::Serialize(const AssetMetadata& metadata, const Ref<Asset>& asset) const
{
Ref<BehaviorTree::Tree> tree = std::reinterpret_pointer_cast<BehaviorTree::Tree>(asset);
UUID rootID = tree->GetRoot();

BehaviourTreeSerializationData serializationData{};

for (const auto& [id, node] : tree->GetNodeManager().m_nodes)
{
switch (node->m_kind)
{
case BehaviorTree::eNodeKind::DECORATOR:
{
auto decorator = std::reinterpret_pointer_cast<BehaviorTree::Decorator>(tree->GetNodeManager().GetNodeFromUUID(id));

auto& serDecorator = serializationData.decorators.emplace_back();
serDecorator.uuid = id;
serDecorator.compareFunctionName = decorator->m_if;
serDecorator.type = decorator->m_type;

break;
}

case BehaviorTree::eNodeKind::LEAF:
{
auto leaf = std::reinterpret_pointer_cast<BehaviorTree::Leaf>(tree->GetNodeManager().GetNodeFromUUID(id));

auto& serLeaf = serializationData.leafs.emplace_back();
serLeaf.uuid = leaf->GetUUID();
serLeaf.functionName = leaf->m_monoScriptFunctonName;

break;
}

case BehaviorTree::eNodeKind::SELECTOR: serializationData.selectorIDs.push_back(id); break;
case BehaviorTree::eNodeKind::SEQUENCE: serializationData.sequenceIDs.push_back(id); break;
}
}

for (const auto& [id, links] : tree->GetNodeManager().m_links)
{
for (const auto& link : links)
{
auto& newLink = serializationData.links.emplace_back();
newLink.id = link.m_uuid;
newLink.parentId = link.m_parentID;
newLink.childId = link.m_childID;
}
}

for (const auto& [id, node] : tree->GetNodeManager().m_nodes)
{
auto& pos = serializationData.positions.emplace_back();
pos.id = id;
pos.positionString = node->GetPos();
}

BinaryStreamWriter streamWriter{};
const size_t compressedDataOffset = AssetSerializer::WriteMetadata(metadata, CURRENT_ASSET_VERSION, streamWriter);

streamWriter.Write(serializationData);

const auto filePath = AssetManager::GetFilesystemPath(metadata.filePath);
streamWriter.WriteToDisk(filePath, true, compressedDataOffset);
}

bool BehaviourTreeSerializer::Deserialize(const AssetMetadata& metadata, Ref<Asset> destinationAsset) const
{
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;
}

SerializedAssetMetadata serializedMetadata = AssetSerializer::ReadMetadata(streamReader);

BehaviourTreeSerializationData serializationData{};
streamReader.Read(serializationData);

Ref<BehaviorTree::Tree> behaviorTree = std::reinterpret_pointer_cast<BehaviorTree::Tree>(destinationAsset);

for (const auto& node : serializationData.sequenceIDs)
{
behaviorTree->CreateNode<BehaviorTree::Sequence>(node);
}

for (const auto& node : serializationData.selectorIDs)
{
behaviorTree->CreateNode<BehaviorTree::Selector>(node);
}

for (const auto& node : serializationData.decorators)
{
auto newNodeId = behaviorTree->CreateNode<BehaviorTree::Decorator>(node.uuid);
auto newNode = std::reinterpret_pointer_cast<BehaviorTree::Decorator>(behaviorTree->GetNodeManager().GetNodeFromUUID(newNodeId));

newNode->m_type = node.type;
newNode->m_if = node.compareFunctionName;
}

for (const auto& node : serializationData.leafs)
{
auto newNodeId = behaviorTree->CreateNode<BehaviorTree::Leaf>(node.uuid);
auto newNode = std::reinterpret_pointer_cast<BehaviorTree::Leaf>(behaviorTree->GetNodeManager().GetNodeFromUUID(newNodeId));

newNode->m_monoScriptFunctonName = node.functionName;
}

for (const auto& link : serializationData.links)
{
behaviorTree->GetNodeManager().RegisterLink(link.parentId, link.childId, link.id);
}

for (const auto& node : serializationData.positions)
{
behaviorTree->GetNodeManager().GetNodeFromUUID(node.id)->SetPos(node.positionString);
}

return true;
}
}
13 changes: 13 additions & 0 deletions Volt/Volt/src/Volt/Asset/ImportersNew/BehaviourTreeSerializer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include "Volt/Asset/ImportersNew/AssetSerializer.h"

namespace Volt
{
class BehaviourTreeSerializer : public AssetSerializer
{
public:
void Serialize(const AssetMetadata& metadata, const Ref<Asset>& asset) const override;
bool Deserialize(const AssetMetadata& metadata, Ref<Asset> destinationAsset) const override;
};
}
71 changes: 71 additions & 0 deletions Volt/Volt/src/Volt/Asset/ImportersNew/BlendSpaceSerializer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include "vtpch.h"
#include "BlendSpaceSerializer.h"

#include "Volt/Asset/AssetManager.h"
#include "Volt/Animation/BlendSpace.h"

namespace Volt
{
constexpr uint32_t CURRENT_ASSET_VERSION = 1;

struct SerializedAnimation
{
AssetHandle handle;
glm::vec2 value;
};

struct BlendSpaceSerializationData
{
BlendSpaceDimension dimension;
glm::vec2 horizontalValues;
glm::vec2 verticalValues;

std::vector<SerializedAnimation> animations;

static void Serialize(BinaryStreamWriter& streamWriter, const BlendSpaceSerializationData& data)
{
streamWriter.Write(data.dimension);
streamWriter.Write(data.horizontalValues);
streamWriter.Write(data.verticalValues);
streamWriter.WriteRaw(data.animations);
}

static void Deserialize(BinaryStreamReader& streamReader, BlendSpaceSerializationData& outData)
{
streamReader.Read(outData.dimension);
streamReader.Read(outData.horizontalValues);
streamReader.Read(outData.verticalValues);
streamReader.ReadRaw(outData.animations);
}
};

void BlendSpaceSerializer::Serialize(const AssetMetadata& metadata, const Ref<Asset>& asset) const
{
Ref<BlendSpace> blendSpace = std::reinterpret_pointer_cast<BlendSpace>(asset);

BlendSpaceSerializationData serializationData{};
serializationData.dimension = blendSpace->myDimension;
serializationData.horizontalValues = blendSpace->myHorizontalValues;
serializationData.verticalValues = blendSpace->myVerticalValues;

for (const auto& anim : blendSpace->myAnimations)
{
auto& serAnim = serializationData.animations.emplace_back();
serAnim.handle = anim.second;
serAnim.value = anim.first;
}

BinaryStreamWriter streamWriter{};
const size_t compressedDataOffset = AssetSerializer::WriteMetadata(metadata, CURRENT_ASSET_VERSION, streamWriter);

streamWriter.Write(serializationData);

const auto filePath = AssetManager::GetFilesystemPath(metadata.filePath);
streamWriter.WriteToDisk(filePath, true, compressedDataOffset);
}

bool BlendSpaceSerializer::Deserialize(const AssetMetadata& metadata, Ref<Asset> destinationAsset) const
{
return false;
}
}
13 changes: 13 additions & 0 deletions Volt/Volt/src/Volt/Asset/ImportersNew/BlendSpaceSerializer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include "Volt/Asset/ImportersNew/AssetSerializer.h"

namespace Volt
{
class BlendSpaceSerializer : public AssetSerializer
{
public:
void Serialize(const AssetMetadata& metadata, const Ref<Asset>& asset) const override;
bool Deserialize(const AssetMetadata& metadata, Ref<Asset> destinationAsset) const override;
};
}
3 changes: 1 addition & 2 deletions Volt/Volt/src/Volt/Asset/ImportersNew/MeshSerializer.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#include "vtpch.h"
#include "MeshSerializer.h"

#include "Volt/Asset/AssetManager.h"
#include "Volt/Asset/Mesh/Mesh.h"
#include "Volt/Asset/Mesh/Material.h"
#include "Volt/Asset/Serialization/AssetSerializationCommon.h"

#include "Volt/Asset/AssetManager.h"

namespace Volt
{
constexpr uint32_t CURRENT_ASSET_VERSION = 1;
Expand Down
Loading

0 comments on commit 3658144

Please sign in to comment.