From cbe055d412b8785a6d12a7d40d7d346d64b639f1 Mon Sep 17 00:00:00 2001 From: SirLynix Date: Mon, 17 Jul 2023 19:07:45 +0200 Subject: [PATCH] Core/AppFilesystemComponent: Use hash as key for resource parameters --- include/Nazara/Core.hpp | 1 - .../Nazara/Core/AppFilesystemComponent.hpp | 16 +----- .../Nazara/Core/AppFilesystemComponent.inl | 50 ++++--------------- include/Nazara/Core/ResourceRegistry.hpp | 24 --------- include/Nazara/Core/ResourceRegistry.inl | 26 ---------- src/Nazara/Graphics/Graphics.cpp | 2 - 6 files changed, 11 insertions(+), 108 deletions(-) delete mode 100644 include/Nazara/Core/ResourceRegistry.hpp delete mode 100644 include/Nazara/Core/ResourceRegistry.inl diff --git a/include/Nazara/Core.hpp b/include/Nazara/Core.hpp index fee5793f1..df01ced8b 100644 --- a/include/Nazara/Core.hpp +++ b/include/Nazara/Core.hpp @@ -79,7 +79,6 @@ #include #include #include -#include #include #include #include diff --git a/include/Nazara/Core/AppFilesystemComponent.hpp b/include/Nazara/Core/AppFilesystemComponent.hpp index f4daaf84f..c53d98c9b 100644 --- a/include/Nazara/Core/AppFilesystemComponent.hpp +++ b/include/Nazara/Core/AppFilesystemComponent.hpp @@ -13,20 +13,10 @@ #include #include #include -#include +#include namespace Nz { - class Font; - class Image; - class ImageStream; - class Material; - class MaterialInstance; - class Mesh; - class SoundBuffer; - class SoundStream; - class Texture; - class NAZARA_CORE_API AppFilesystemComponent : public ApplicationComponent { public: @@ -54,13 +44,11 @@ namespace Nz AppFilesystemComponent& operator=(const AppFilesystemComponent&) = delete; AppFilesystemComponent& operator=(AppFilesystemComponent&&) = delete; - static inline void RegisterResourceTypes(); - private: template std::shared_ptr LoadImpl(std::string_view assetPath, const typename T::Params& params, ExtraArgs&&... args); template std::shared_ptr OpenImpl(std::string_view assetPath, const typename T::Params& params, ExtraArgs&&... args); - std::vector> m_defaultParameters; + std::unordered_map> m_defaultParameters; VirtualDirectoryPtr m_rootDirectory; }; } diff --git a/include/Nazara/Core/AppFilesystemComponent.inl b/include/Nazara/Core/AppFilesystemComponent.inl index 319d18523..febea9d9b 100644 --- a/include/Nazara/Core/AppFilesystemComponent.inl +++ b/include/Nazara/Core/AppFilesystemComponent.inl @@ -3,7 +3,8 @@ // For conditions of distribution and use, see copyright notice in Config.hpp #include -#include +#include +#include #include #include @@ -21,17 +22,18 @@ namespace Nz inline AppFilesystemComponent::AppFilesystemComponent(ApplicationBase& app) : ApplicationComponent(app) { - RegisterResourceTypes(); } template const typename T::Params* AppFilesystemComponent::GetDefaultResourceParameters() const { - std::size_t resourceIndex = ResourceRegistry::GetResourceId(); - if (resourceIndex >= m_defaultParameters.size()) + constexpr UInt64 typeHash = FNV1a64(TypeName()); + + auto it = m_defaultParameters.find(typeHash); + if (it == m_defaultParameters.end()) return nullptr; - return static_cast(m_defaultParameters[resourceIndex].get()); + return static_cast(it->second.get()); } VirtualDirectoryPtr AppFilesystemComponent::GetDirectory(std::string_view assetPath) @@ -84,43 +86,9 @@ namespace Nz template void AppFilesystemComponent::SetDefaultResourceParameters(typename T::Params params) { - std::size_t resourceIndex = ResourceRegistry::GetResourceId(); - if (resourceIndex >= m_defaultParameters.size()) - m_defaultParameters.resize(resourceIndex + 1); + constexpr UInt64 typeHash = FNV1a64(TypeName()); - m_defaultParameters[resourceIndex] = std::make_unique(std::move(params)); - } - - inline void AppFilesystemComponent::RegisterResourceTypes() - { - // TODO: Switch to hash-based approach like entt? - - if (ResourceRegistry::GetResourceId() != 0) - throw std::runtime_error("Font has wrong resource index, please initialize AppFilesystemComponent before using ResourceRegistry"); - - if (ResourceRegistry::GetResourceId() != 1) - throw std::runtime_error("Image has wrong resource index, please initialize AppFilesystemComponent before using ResourceRegistry"); - - if (ResourceRegistry::GetResourceId() != 2) - throw std::runtime_error("ImageStream has wrong resource index, please initialize AppFilesystemComponent before using ResourceRegistry"); - - if (ResourceRegistry::GetResourceId() != 3) - throw std::runtime_error("Material has wrong resource index, please initialize AppFilesystemComponent before using ResourceRegistry"); - - if (ResourceRegistry::GetResourceId() != 4) - throw std::runtime_error("MaterialInstance has wrong resource index, please initialize AppFilesystemComponent before using ResourceRegistry"); - - if (ResourceRegistry::GetResourceId() != 5) - throw std::runtime_error("Mesh has wrong resource index, please initialize AppFilesystemComponent before using ResourceRegistry"); - - if (ResourceRegistry::GetResourceId() != 6) - throw std::runtime_error("SoundBuffer has wrong resource index, please initialize AppFilesystemComponent before using ResourceRegistry"); - - if (ResourceRegistry::GetResourceId() != 7) - throw std::runtime_error("SoundStream has wrong resource index, please initialize AppFilesystemComponent before using ResourceRegistry"); - - if (ResourceRegistry::GetResourceId() != 8) - throw std::runtime_error("Texture has wrong resource index, please initialize AppFilesystemComponent before using ResourceRegistry"); + m_defaultParameters[typeHash] = std::make_unique(std::move(params)); } template diff --git a/include/Nazara/Core/ResourceRegistry.hpp b/include/Nazara/Core/ResourceRegistry.hpp deleted file mode 100644 index 74c522a7c..000000000 --- a/include/Nazara/Core/ResourceRegistry.hpp +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright (C) 2023 Jérôme "Lynix" Leclercq (lynix680@gmail.com) -// This file is part of the "Nazara Engine - Core module" -// For conditions of distribution and use, see copyright notice in Config.hpp - -#pragma once - -#ifndef NAZARA_CORE_RESOURCEREGISTRY_HPP -#define NAZARA_CORE_RESOURCEREGISTRY_HPP - -#include -#include - -namespace Nz -{ - template - struct ResourceRegistry - { - static std::size_t GetResourceId(); - }; -} - -#include - -#endif // NAZARA_CORE_RESOURCEREGISTRY_HPP diff --git a/include/Nazara/Core/ResourceRegistry.inl b/include/Nazara/Core/ResourceRegistry.inl deleted file mode 100644 index f49e2a6e8..000000000 --- a/include/Nazara/Core/ResourceRegistry.inl +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright (C) 2023 Jérôme "Lynix" Leclercq (lynix680@gmail.com) -// This file is part of the "Nazara Engine - Core module" -// For conditions of distribution and use, see copyright notice in Config.hpp - -#include - -namespace Nz -{ - namespace Detail - { - inline std::size_t ResourceTypeIndexCounter() - { - static std::size_t counter = 0; - return counter++; - } - } - - template - std::size_t ResourceRegistry::GetResourceId() - { - static std::size_t typeId = Detail::ResourceTypeIndexCounter(); - return typeId; - } -} - -#include diff --git a/src/Nazara/Graphics/Graphics.cpp b/src/Nazara/Graphics/Graphics.cpp index 3d71b1ec7..ed73dd4b8 100644 --- a/src/Nazara/Graphics/Graphics.cpp +++ b/src/Nazara/Graphics/Graphics.cpp @@ -177,8 +177,6 @@ namespace Nz void Graphics::RegisterComponent(AppFilesystemComponent& component) { - AppFilesystemComponent::RegisterResourceTypes(); - TextureParams defaultTexParams; defaultTexParams.renderDevice = m_renderDevice;