Bigfoot / Sonarqube (push) Skipped
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang (Unity Build: OFF, Variant: game) (push) Successful in 1m4s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang (Unity Build: ON, Variant: game) (push) Failing after 18s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang (Unity Build: OFF, Variant: toolmode) (push) Successful in 42s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang (Unity Build: ON, Variant: toolmode) (push) Failing after 17s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang_asan (Unity Build: OFF, Variant: game) (push) Successful in 1m3s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang_asan (Unity Build: ON, Variant: game) (push) Failing after 20s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang_asan (Unity Build: OFF, Variant: toolmode) (push) Successful in 1m11s
Bigfoot / Build & Test Debug with ./ConanProfiles/host/clang_asan (Unity Build: ON, Variant: toolmode) (push) Failing after 22s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang (Unity Build: OFF, Variant: game) (push) Successful in 41s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang (Unity Build: ON, Variant: game) (push) Failing after 22s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang (Unity Build: OFF, Variant: toolmode) (push) Successful in 48s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang (Unity Build: ON, Variant: toolmode) (push) Failing after 25s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang_asan (Unity Build: OFF, Variant: game) (push) Successful in 1m8s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang_asan (Unity Build: ON, Variant: game) (push) Failing after 29s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang_asan (Unity Build: OFF, Variant: toolmode) (push) Successful in 1m14s
Bigfoot / Build & Test RelWithDebInfo with ./ConanProfiles/host/clang_asan (Unity Build: ON, Variant: toolmode) (push) Failing after 33s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang (Unity Build: OFF, Variant: game) (push) Successful in 32s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang (Unity Build: ON, Variant: game) (push) Successful in 26s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang (Unity Build: OFF, Variant: toolmode) (push) Successful in 52s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang (Unity Build: ON, Variant: toolmode) (push) Successful in 39s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang_asan (Unity Build: OFF, Variant: game) (push) Successful in 48s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang_asan (Unity Build: ON, Variant: game) (push) Successful in 40s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang_asan (Unity Build: OFF, Variant: toolmode) (push) Successful in 53s
Bigfoot / Build & Test Release with ./ConanProfiles/host/clang_asan (Unity Build: ON, Variant: toolmode) (push) Successful in 45s
Bigfoot / Clang Format Checks (push) Successful in 9s
280 lines
10 KiB
C++
280 lines
10 KiB
C++
/*********************************************************************
|
|
* \file SerializationManager.cpp
|
|
*
|
|
* \author Romain BOULLARD
|
|
* \date August 2026
|
|
*********************************************************************/
|
|
#include <Engine/SerializationManager/SerializationManager.hpp>
|
|
|
|
#include <Engine/Asset/AssetLibrary.hpp>
|
|
#include <Engine/DataAccessor/DataAccessor.hpp>
|
|
|
|
#include <Utils/Algorithm/Graph.hpp>
|
|
|
|
#include <ankerl/unordered_dense.h>
|
|
|
|
namespace Bigfoot::SerializationManager
|
|
{
|
|
/*
|
|
* Build the transitive HardReference graph reachable from p_roots
|
|
*
|
|
* \param p_roots The assets to start the discovery from
|
|
* \param p_discovered Every discovered asset that isn't already loaded, including p_roots
|
|
* \return Every asset's HardReferences
|
|
*/
|
|
[[nodiscard]]
|
|
ankerl::unordered_dense::map<UUID, eastl::vector<UUID>> BuildDependencyGraph(
|
|
const eastl::span<const UUID> p_roots,
|
|
ankerl::unordered_dense::set<UUID>& p_discovered)
|
|
{
|
|
ankerl::unordered_dense::map<UUID, eastl::vector<UUID>> graph;
|
|
|
|
eastl::vector<UUID> toQuery;
|
|
toQuery.reserve(p_roots.size());
|
|
for (const UUID& uuid: p_roots)
|
|
{
|
|
if (!g_activeAssetLibrary->IsLoaded(uuid) && p_discovered.insert(uuid).second)
|
|
{
|
|
toQuery.push_back(uuid);
|
|
}
|
|
}
|
|
|
|
while (!toQuery.empty())
|
|
{
|
|
eastl::vector<eastl::vector<UUID>> hardReferencesBatch = g_dataAccessor->GetAssetsHardReferences(toQuery);
|
|
|
|
eastl::vector<UUID> nextToQuery;
|
|
for (std::size_t i = 0; i < toQuery.size(); ++i)
|
|
{
|
|
eastl::vector<UUID>& hardReferences = graph[toQuery[i]];
|
|
hardReferences = std::move(hardReferencesBatch[i]);
|
|
|
|
for (const UUID& hardReference: hardReferences)
|
|
{
|
|
if (!g_activeAssetLibrary->IsLoaded(hardReference) && p_discovered.insert(hardReference).second)
|
|
{
|
|
nextToQuery.push_back(hardReference);
|
|
}
|
|
}
|
|
}
|
|
|
|
toQuery = std::move(nextToQuery);
|
|
}
|
|
|
|
return graph;
|
|
}
|
|
|
|
/****************************************************************************************/
|
|
|
|
void LoadAssets(const eastl::span<const UUID> p_uuids)
|
|
{
|
|
ankerl::unordered_dense::set<UUID> discovered;
|
|
const ankerl::unordered_dense::map<UUID, eastl::vector<UUID>> graph = BuildDependencyGraph(p_uuids, discovered);
|
|
|
|
// Assets that are part of a cyclic HardReference dependency are left out of waves and simply never loaded.
|
|
eastl::vector<eastl::vector<UUID>> waves;
|
|
eastl::vector<UUID> cyclicUuids;
|
|
std::ignore = LayeredTopologicalSort(graph, discovered, waves, cyclicUuids);
|
|
BIGFOOT_LOG_ERROR_IF(!cyclicUuids.empty(),
|
|
ENGINE_LOGGER,
|
|
"Cyclic HardReference dependency detected while loading assets, involved UUIDs: {}",
|
|
cyclicUuids);
|
|
|
|
// Waves are loaded one after another since a wave's assets may HardReference the previous wave's; within a
|
|
// wave, every asset is independent of the others, so this is where loading could later be parallelized.
|
|
for (const eastl::vector<UUID>& wave: waves)
|
|
{
|
|
const eastl::vector<eastl::vector<std::byte>> assetFlatbuffers = g_dataAccessor->GetAssetFlatbuffers(wave);
|
|
for (const eastl::vector<std::byte>& flatbuffer: assetFlatbuffers)
|
|
{
|
|
if (flatbuffer.empty())
|
|
{
|
|
continue;
|
|
}
|
|
|
|
#ifdef BIGFOOT_NOT_OPTIMIZED
|
|
flatbuffers::Verifier verifier {std::bit_cast<std::uint8_t*>(flatbuffer.data()), flatbuffer.size()};
|
|
const bool assetVerified = ::Flat::Bigfoot::VerifyAssetBuffer(verifier);
|
|
CRITICAL_ASSERT(EngineAssertHandler, assetVerified, "Given asset could not be verified!");
|
|
#endif
|
|
|
|
const ::Flat::Bigfoot::Asset* asset = flatbuffers::GetRoot<::Flat::Bigfoot::Asset>(flatbuffer.data());
|
|
|
|
AssetContainerBase* container = g_activeAssetLibrary->GetAssetContainer(
|
|
flatbuffers::UnPack(*asset->header()->type_id()));
|
|
std::ignore = container->Insert(flatbuffer);
|
|
}
|
|
}
|
|
}
|
|
|
|
/****************************************************************************************/
|
|
|
|
void LoadAsset(const UUID& p_uuid)
|
|
{
|
|
ankerl::unordered_dense::set<UUID> discovered;
|
|
const ankerl::unordered_dense::map<UUID, eastl::vector<UUID>> graph = BuildDependencyGraph(
|
|
eastl::span<const UUID> {&p_uuid, 1},
|
|
discovered);
|
|
|
|
// Assets that are part of a cyclic HardReference dependency are left out of waves and simply never loaded.
|
|
eastl::vector<eastl::vector<UUID>> waves;
|
|
eastl::vector<UUID> cyclicUuids;
|
|
std::ignore = LayeredTopologicalSort(graph, discovered, waves, cyclicUuids);
|
|
BIGFOOT_LOG_ERROR_IF(!cyclicUuids.empty(),
|
|
ENGINE_LOGGER,
|
|
"Cyclic HardReference dependency detected while loading asset {}, involved UUIDs: {}",
|
|
p_uuid,
|
|
cyclicUuids);
|
|
|
|
// Waves are loaded one after another since a wave's assets may HardReference the previous wave's; within a
|
|
// wave, every asset is independent of the others, so this is where loading could later be parallelized.
|
|
for (const eastl::vector<UUID>& wave: waves)
|
|
{
|
|
for (const UUID& uuid: wave)
|
|
{
|
|
const eastl::vector<std::byte> flatbuffer = g_dataAccessor->GetAssetFlatbuffer(uuid);
|
|
if (flatbuffer.empty())
|
|
{
|
|
continue;
|
|
}
|
|
|
|
#ifdef BIGFOOT_NOT_OPTIMIZED
|
|
flatbuffers::Verifier verifier {std::bit_cast<std::uint8_t*>(flatbuffer.data()), flatbuffer.size()};
|
|
const bool assetVerified = ::Flat::Bigfoot::VerifyAssetBuffer(verifier);
|
|
CRITICAL_ASSERT(EngineAssertHandler, assetVerified, "Given asset could not be verified!");
|
|
#endif
|
|
|
|
const ::Flat::Bigfoot::Asset* asset = flatbuffers::GetRoot<::Flat::Bigfoot::Asset>(flatbuffer.data());
|
|
|
|
AssetContainerBase* container = g_activeAssetLibrary->GetAssetContainer(
|
|
flatbuffers::UnPack(*asset->header()->type_id()));
|
|
std::ignore = container->Insert(flatbuffer);
|
|
}
|
|
}
|
|
}
|
|
|
|
/****************************************************************************************/
|
|
|
|
#if defined BIGFOOT_TOOLMODE
|
|
/*
|
|
* Finds the cyclic dependencies that may exist in Assets
|
|
*
|
|
* \param p_uuids The assets we want to explore
|
|
* \param p_hardReferences The HardReferences of p_uuids, matching by index
|
|
* \return Every UUID caught in a cyclic dependency
|
|
*/
|
|
[[nodiscard]]
|
|
eastl::vector<UUID> FindCyclicDependencies(const eastl::span<const UUID> p_uuids,
|
|
const eastl::span<const eastl::vector<UUID>> p_hardReferences)
|
|
{
|
|
ankerl::unordered_dense::map<UUID, eastl::vector<UUID>> graph;
|
|
ankerl::unordered_dense::set<UUID> discovered;
|
|
|
|
eastl::vector<UUID> toQuery;
|
|
for (std::size_t i = 0; i < p_uuids.size(); ++i)
|
|
{
|
|
discovered.insert(p_uuids[i]);
|
|
graph[p_uuids[i]] = p_hardReferences[i];
|
|
}
|
|
for (const eastl::vector<UUID>& hardReferences: p_hardReferences)
|
|
{
|
|
for (const UUID& hardReference: hardReferences)
|
|
{
|
|
if (discovered.insert(hardReference).second)
|
|
{
|
|
toQuery.push_back(hardReference);
|
|
}
|
|
}
|
|
}
|
|
|
|
while (!toQuery.empty())
|
|
{
|
|
eastl::vector<eastl::vector<UUID>> hardReferencesBatch = g_dataAccessor->GetAssetsHardReferences(toQuery);
|
|
|
|
eastl::vector<UUID> nextToQuery;
|
|
for (std::size_t i = 0; i < toQuery.size(); ++i)
|
|
{
|
|
eastl::vector<UUID>& hardReferences = graph[toQuery[i]];
|
|
hardReferences = std::move(hardReferencesBatch[i]);
|
|
|
|
for (const UUID& hardReference: hardReferences)
|
|
{
|
|
if (discovered.insert(hardReference).second)
|
|
{
|
|
nextToQuery.push_back(hardReference);
|
|
}
|
|
}
|
|
}
|
|
|
|
toQuery = std::move(nextToQuery);
|
|
}
|
|
|
|
eastl::vector<eastl::vector<UUID>> waves;
|
|
eastl::vector<UUID> cyclicUuids;
|
|
std::ignore = LayeredTopologicalSort(graph, discovered, waves, cyclicUuids);
|
|
return cyclicUuids;
|
|
}
|
|
|
|
/****************************************************************************************/
|
|
|
|
bool SaveAsset(const UUID& p_uuid)
|
|
{
|
|
AssetContainerBase* container = g_activeAssetLibrary->GetAssetContainer(p_uuid);
|
|
const AssetBase* asset = container->AcquireContainerHandle(p_uuid)->GetAsset();
|
|
|
|
const eastl::vector<std::byte> flatbuffer = asset->Pack();
|
|
const eastl::vector<UUID> hardReferences = asset->GetHardReferences(flatbuffer);
|
|
|
|
const eastl::vector<UUID> cyclicUuids = FindCyclicDependencies(
|
|
eastl::span<const UUID> {&p_uuid, 1},
|
|
eastl::span<const eastl::vector<UUID>> {&hardReferences, 1});
|
|
BIGFOOT_LOG_ERROR_IF(!cyclicUuids.empty(),
|
|
ENGINE_LOGGER,
|
|
"Cyclic HardReference dependency detected while saving asset {}, involved UUIDs: {}",
|
|
p_uuid,
|
|
cyclicUuids);
|
|
if (!cyclicUuids.empty())
|
|
{
|
|
return false;
|
|
}
|
|
|
|
g_dataAccessor->SaveAssetFlatbuffer(p_uuid, flatbuffer, hardReferences);
|
|
return true;
|
|
}
|
|
|
|
/****************************************************************************************/
|
|
|
|
bool SaveAssets(const eastl::span<const UUID> p_uuids)
|
|
{
|
|
eastl::vector<eastl::vector<std::byte>> flatbuffers;
|
|
eastl::vector<eastl::vector<UUID>> hardReferences;
|
|
|
|
flatbuffers.reserve(p_uuids.size());
|
|
hardReferences.reserve(p_uuids.size());
|
|
for (const UUID& uuid: p_uuids)
|
|
{
|
|
AssetContainerBase* container = g_activeAssetLibrary->GetAssetContainer(uuid);
|
|
const AssetBase* asset = container->AcquireContainerHandle(uuid)->GetAsset();
|
|
|
|
const eastl::vector<std::byte> flatbuffer = asset->Pack();
|
|
|
|
flatbuffers.push_back(flatbuffer);
|
|
hardReferences.push_back(asset->GetHardReferences(flatbuffer));
|
|
}
|
|
|
|
const eastl::vector<UUID> cyclicUuids = FindCyclicDependencies(p_uuids, hardReferences);
|
|
BIGFOOT_LOG_ERROR_IF(!cyclicUuids.empty(),
|
|
ENGINE_LOGGER,
|
|
"Cyclic HardReference dependency detected while saving assets, involved UUIDs: {}",
|
|
cyclicUuids);
|
|
if (!cyclicUuids.empty())
|
|
{
|
|
return false;
|
|
}
|
|
|
|
g_dataAccessor->SaveAssetFlatbuffers(p_uuids, flatbuffers, hardReferences);
|
|
return true;
|
|
}
|
|
#endif
|
|
} // namespace Bigfoot::SerializationManager
|