-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a28c0cc
commit 3658144
Showing
12 changed files
with
436 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
237 changes: 237 additions & 0 deletions
237
Volt/Volt/src/Volt/Asset/ImportersNew/BehaviourTreeSerializer.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
Volt/Volt/src/Volt/Asset/ImportersNew/BehaviourTreeSerializer.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
71
Volt/Volt/src/Volt/Asset/ImportersNew/BlendSpaceSerializer.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
Volt/Volt/src/Volt/Asset/ImportersNew/BlendSpaceSerializer.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.