Core/AppFilesystemComponent: Use hash as key for resource parameters

This commit is contained in:
SirLynix
2023-07-17 19:07:45 +02:00
parent f2bc1bff7a
commit cbe055d412
6 changed files with 11 additions and 108 deletions

View File

@@ -3,7 +3,8 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/ResourceRegistry.hpp>
#include <NazaraUtils/Hash.hpp>
#include <NazaraUtils/TypeName.hpp>
#include <stdexcept>
#include <Nazara/Core/Debug.hpp>
@@ -21,17 +22,18 @@ namespace Nz
inline AppFilesystemComponent::AppFilesystemComponent(ApplicationBase& app) :
ApplicationComponent(app)
{
RegisterResourceTypes();
}
template<typename T>
const typename T::Params* AppFilesystemComponent::GetDefaultResourceParameters() const
{
std::size_t resourceIndex = ResourceRegistry<T>::GetResourceId();
if (resourceIndex >= m_defaultParameters.size())
constexpr UInt64 typeHash = FNV1a64(TypeName<T>());
auto it = m_defaultParameters.find(typeHash);
if (it == m_defaultParameters.end())
return nullptr;
return static_cast<const typename T::Params*>(m_defaultParameters[resourceIndex].get());
return static_cast<const typename T::Params*>(it->second.get());
}
VirtualDirectoryPtr AppFilesystemComponent::GetDirectory(std::string_view assetPath)
@@ -84,43 +86,9 @@ namespace Nz
template<typename T>
void AppFilesystemComponent::SetDefaultResourceParameters(typename T::Params params)
{
std::size_t resourceIndex = ResourceRegistry<T>::GetResourceId();
if (resourceIndex >= m_defaultParameters.size())
m_defaultParameters.resize(resourceIndex + 1);
constexpr UInt64 typeHash = FNV1a64(TypeName<T>());
m_defaultParameters[resourceIndex] = std::make_unique<typename T::Params>(std::move(params));
}
inline void AppFilesystemComponent::RegisterResourceTypes()
{
// TODO: Switch to hash-based approach like entt?
if (ResourceRegistry<Font>::GetResourceId() != 0)
throw std::runtime_error("Font has wrong resource index, please initialize AppFilesystemComponent before using ResourceRegistry");
if (ResourceRegistry<Image>::GetResourceId() != 1)
throw std::runtime_error("Image has wrong resource index, please initialize AppFilesystemComponent before using ResourceRegistry");
if (ResourceRegistry<ImageStream>::GetResourceId() != 2)
throw std::runtime_error("ImageStream has wrong resource index, please initialize AppFilesystemComponent before using ResourceRegistry");
if (ResourceRegistry<Material>::GetResourceId() != 3)
throw std::runtime_error("Material has wrong resource index, please initialize AppFilesystemComponent before using ResourceRegistry");
if (ResourceRegistry<MaterialInstance>::GetResourceId() != 4)
throw std::runtime_error("MaterialInstance has wrong resource index, please initialize AppFilesystemComponent before using ResourceRegistry");
if (ResourceRegistry<Mesh>::GetResourceId() != 5)
throw std::runtime_error("Mesh has wrong resource index, please initialize AppFilesystemComponent before using ResourceRegistry");
if (ResourceRegistry<SoundBuffer>::GetResourceId() != 6)
throw std::runtime_error("SoundBuffer has wrong resource index, please initialize AppFilesystemComponent before using ResourceRegistry");
if (ResourceRegistry<SoundStream>::GetResourceId() != 7)
throw std::runtime_error("SoundStream has wrong resource index, please initialize AppFilesystemComponent before using ResourceRegistry");
if (ResourceRegistry<Texture>::GetResourceId() != 8)
throw std::runtime_error("Texture has wrong resource index, please initialize AppFilesystemComponent before using ResourceRegistry");
m_defaultParameters[typeHash] = std::make_unique<typename T::Params>(std::move(params));
}
template<typename T, typename... ExtraArgs>