From a21354b4b4bec22f8797c3ee6d84589803967780 Mon Sep 17 00:00:00 2001 From: Romain BOULLARD Date: Fri, 13 Feb 2026 18:43:21 +0100 Subject: [PATCH] AssetContainer semantic --- .../Engine/BigFile/Asset/AssetContainer.hpp | 74 ++++++++++++++++++- Bigfoot/Tests/Engine/BigFile/BigFile.cpp | 46 +++++++----- .../EngineTests/BigFile/Asset/AssetA.hpp | 35 ++++++++- 3 files changed, 130 insertions(+), 25 deletions(-) diff --git a/Bigfoot/Sources/Engine/Include/Engine/BigFile/Asset/AssetContainer.hpp b/Bigfoot/Sources/Engine/Include/Engine/BigFile/Asset/AssetContainer.hpp index 039479d..667aa22 100644 --- a/Bigfoot/Sources/Engine/Include/Engine/BigFile/Asset/AssetContainer.hpp +++ b/Bigfoot/Sources/Engine/Include/Engine/BigFile/Asset/AssetContainer.hpp @@ -7,25 +7,91 @@ #ifndef BIGFOOT_ENGINE_ASSETCONTAINER_HPP #define BIGFOOT_ENGINE_ASSETCONTAINER_HPP #include - #include -#include +#include namespace Bigfoot { template -concept BigfootAssetConcept = requires(ASSET p_asset) { +concept BigfootAssetConcept = requires(ASSET p_asset, const ASSET p_constAsset) { requires FlatAssetConcept && - std::constructible_from>; + std::constructible_from*>; }; template class AssetContainer { public: + AssetContainer() = default; + + AssetContainer(const AssetContainer& p_container) = delete; + AssetContainer(AssetContainer&& p_container) = delete; + + [[nodiscard]] + bool Add(const UUID& p_uuid, const eastl::string_view p_name) + { + FlatAssetWrapper flatAsset {}; + flatAsset.Asset().asset_header->uuid = p_uuid; + flatAsset.Asset().asset_header->name = p_name; + + return Add(p_uuid, flatAsset.Pack()); + } + + [[nodiscard]] + bool Add(const UUID& p_uuid, const eastl::span p_flatBuffer) + { + const auto& added = m_assets.emplace(p_uuid, eastl::make_unique(p_flatBuffer)); + + CRITICAL_ASSERT(EngineAssertHandler, + !added.second || added.first->second->m_flatAsset.Asset().asset_header->uuid == p_uuid, + "Added ASSET UUID does not match !"); + + return added.second; + } + + [[nodiscard]] + ASSET* Get(const UUID& p_uuid) + { + return m_assets.contains(p_uuid) ? &m_assets.at_key(p_uuid)->m_asset : nullptr; + } + + [[nodiscard]] + const ASSET* Get(const UUID& p_uuid) const + { + return m_assets.contains(p_uuid) ? &m_assets.at_key(p_uuid)->m_asset : nullptr; + } + + void Remove(const UUID& p_uuid) + { + m_assets.erase(p_uuid); + } + + [[nodiscard]] + eastl::vector Pack(const UUID& p_uuid) const + { + return m_assets.contains(p_uuid) ? m_assets.at_key(p_uuid)->m_flatAsset.Pack() : eastl::vector {}; + } + + ~AssetContainer() = default; + + AssetContainer& operator=(const AssetContainer& p_container) = delete; + AssetContainer& operator=(AssetContainer&& p_container) = delete; private: + struct AssetFlatAssetWrapperPair + { + AssetFlatAssetWrapperPair(const eastl::span p_flatbuffer): + m_flatAsset(p_flatbuffer), + m_asset(&m_flatAsset) + { + } + + FlatAssetWrapper m_flatAsset; + ASSET m_asset; + }; + + eastl::vector_map> m_assets; }; } // namespace Bigfoot diff --git a/Bigfoot/Tests/Engine/BigFile/BigFile.cpp b/Bigfoot/Tests/Engine/BigFile/BigFile.cpp index 39f3efc..0604aae 100644 --- a/Bigfoot/Tests/Engine/BigFile/BigFile.cpp +++ b/Bigfoot/Tests/Engine/BigFile/BigFile.cpp @@ -15,6 +15,7 @@ #include #include +#include #include #include @@ -36,7 +37,7 @@ class BigFileFixture: public ::testing::Test m_bigFile.CommitTransaction(); } - FlatAssetWrapper test; + AssetAContainer m_assetAContainer; BIGFOOT_NOT_OPTIMIZED_ONLY(Singleton::Lifetime m_loggerLifetime;) @@ -48,31 +49,36 @@ class BigFileFixture: public ::testing::Test TEST_F(BigFileFixture, Lol) { { - UUID uuid; - - test.Asset().health = 100; - test.Asset().mana = 42; - - test.Asset().asset_header->name = "Instance"; - test.Asset().asset_header->uuid = uuid; - test.Asset().asset_header->version = 2; - - const eastl::vector flatbuffer = test.Pack(); - EXPECT_STREQ(FlatAssetWrapper::TypeName().data(), "Bigfoot.Flat.AssetA"); EXPECT_EQ(FlatAssetWrapper::TypeID(), rapidhash(FlatAssetWrapper::TypeName().data(), FlatAssetWrapper::TypeName().size())); - FlatAssetWrapper test2 {flatbuffer}; - EXPECT_EQ(test2.Asset().health, 100); - EXPECT_EQ(test2.Asset().mana, 42); + UUID uuid; + std::ignore = m_assetAContainer.Add(uuid, "Instance"); - EXPECT_STREQ(test2.Asset().asset_header->name.c_str(), "Instance"); - EXPECT_EQ(test2.Asset().asset_header->type_id, FlatAssetWrapper::TypeID()); - EXPECT_STREQ(test2.Asset().asset_header->type_name.c_str(), FlatAssetWrapper::TypeName().data()); - EXPECT_EQ(test2.Asset().asset_header->uuid, uuid); - EXPECT_EQ(test2.Asset().asset_header->version, 2); + AssetA* test = m_assetAContainer.Get(uuid); + + test->Health() = 100; + test->Mana() = 42; + + const eastl::vector flatbuffer = m_assetAContainer.Pack(uuid); + m_assetAContainer.Remove(uuid); + + std::ignore = m_assetAContainer.Add(uuid, flatbuffer); + std::ignore = m_assetAContainer.Add(UUID {}, "Instance2"); + std::ignore = m_assetAContainer.Add(UUID {}, "Instance3"); + + AssetA* test2 = m_assetAContainer.Get(uuid); + + EXPECT_EQ(test2->Health(), 100); + EXPECT_EQ(test2->Mana(), 42); + + EXPECT_STREQ(test2->AssetHeader()->name.c_str(), "Instance"); + EXPECT_EQ(test2->AssetHeader()->type_id, FlatAssetWrapper::TypeID()); + EXPECT_STREQ(test2->AssetHeader()->type_name.c_str(), FlatAssetWrapper::TypeName().data()); + EXPECT_EQ(test2->AssetHeader()->uuid, uuid); + EXPECT_EQ(test2->AssetHeader()->version, 2); } UUID uuid; diff --git a/Bigfoot/Tests/Engine/Include/EngineTests/BigFile/Asset/AssetA.hpp b/Bigfoot/Tests/Engine/Include/EngineTests/BigFile/Asset/AssetA.hpp index 3be16c3..1ed37e2 100644 --- a/Bigfoot/Tests/Engine/Include/EngineTests/BigFile/Asset/AssetA.hpp +++ b/Bigfoot/Tests/Engine/Include/EngineTests/BigFile/Asset/AssetA.hpp @@ -17,17 +17,50 @@ class AssetA public: using FLAT_ASSET = Flat::AssetA; - AssetA(const FlatAssetWrapper& p_flatAsset); + AssetA(FlatAssetWrapper* p_flatAsset): + m_flatAsset(p_flatAsset) + { + // deprecation... + if (m_flatAsset->Asset().asset_header->version < 2) + { + m_flatAsset->Asset().asset_header->version = 2; + } + } AssetA(const AssetA& p_assetA) = default; AssetA(AssetA&& p_assetA) = default; + [[nodiscard]] + std::uint32_t& Health() + { + return m_flatAsset->Asset().health; + } + + [[nodiscard]] + std::uint32_t& Mana() + { + return m_flatAsset->Asset().mana; + } + + [[nodiscard]] + Flat::AssetHeaderT* AssetHeader() + { + return m_flatAsset->Asset().asset_header.get(); + } + + [[nodiscard]] + const Flat::AssetHeaderT* AssetHeader() const + { + return m_flatAsset->Asset().asset_header.get(); + } + ~AssetA() = default; AssetA& operator=(const AssetA& p_assetA) = default; AssetA& operator=(AssetA&& p_assetA) = default; private: + FlatAssetWrapper* m_flatAsset; }; using AssetAContainer = AssetContainer;