Core/AppFilesystemComponent: Use hash as key for resource parameters
This commit is contained in:
parent
f2bc1bff7a
commit
cbe055d412
|
|
@ -79,7 +79,6 @@
|
||||||
#include <Nazara/Core/ResourceLoader.hpp>
|
#include <Nazara/Core/ResourceLoader.hpp>
|
||||||
#include <Nazara/Core/ResourceManager.hpp>
|
#include <Nazara/Core/ResourceManager.hpp>
|
||||||
#include <Nazara/Core/ResourceParameters.hpp>
|
#include <Nazara/Core/ResourceParameters.hpp>
|
||||||
#include <Nazara/Core/ResourceRegistry.hpp>
|
|
||||||
#include <Nazara/Core/ResourceSaver.hpp>
|
#include <Nazara/Core/ResourceSaver.hpp>
|
||||||
#include <Nazara/Core/SerializationContext.hpp>
|
#include <Nazara/Core/SerializationContext.hpp>
|
||||||
#include <Nazara/Core/SignalHandlerAppComponent.hpp>
|
#include <Nazara/Core/SignalHandlerAppComponent.hpp>
|
||||||
|
|
|
||||||
|
|
@ -13,20 +13,10 @@
|
||||||
#include <Nazara/Core/ResourceParameters.hpp>
|
#include <Nazara/Core/ResourceParameters.hpp>
|
||||||
#include <Nazara/Core/VirtualDirectory.hpp>
|
#include <Nazara/Core/VirtualDirectory.hpp>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <vector>
|
#include <unordered_map>
|
||||||
|
|
||||||
namespace Nz
|
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
|
class NAZARA_CORE_API AppFilesystemComponent : public ApplicationComponent
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
@ -54,13 +44,11 @@ namespace Nz
|
||||||
AppFilesystemComponent& operator=(const AppFilesystemComponent&) = delete;
|
AppFilesystemComponent& operator=(const AppFilesystemComponent&) = delete;
|
||||||
AppFilesystemComponent& operator=(AppFilesystemComponent&&) = delete;
|
AppFilesystemComponent& operator=(AppFilesystemComponent&&) = delete;
|
||||||
|
|
||||||
static inline void RegisterResourceTypes();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
template<typename T, typename... ExtraArgs> std::shared_ptr<T> LoadImpl(std::string_view assetPath, const typename T::Params& params, ExtraArgs&&... args);
|
template<typename T, typename... ExtraArgs> std::shared_ptr<T> LoadImpl(std::string_view assetPath, const typename T::Params& params, ExtraArgs&&... args);
|
||||||
template<typename T, typename... ExtraArgs> std::shared_ptr<T> OpenImpl(std::string_view assetPath, const typename T::Params& params, ExtraArgs&&... args);
|
template<typename T, typename... ExtraArgs> std::shared_ptr<T> OpenImpl(std::string_view assetPath, const typename T::Params& params, ExtraArgs&&... args);
|
||||||
|
|
||||||
std::vector<std::unique_ptr<ResourceParameters>> m_defaultParameters;
|
std::unordered_map<UInt64 /*typehash*/, std::unique_ptr<ResourceParameters>> m_defaultParameters;
|
||||||
VirtualDirectoryPtr m_rootDirectory;
|
VirtualDirectoryPtr m_rootDirectory;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,8 @@
|
||||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||||
|
|
||||||
#include <Nazara/Core/Error.hpp>
|
#include <Nazara/Core/Error.hpp>
|
||||||
#include <Nazara/Core/ResourceRegistry.hpp>
|
#include <NazaraUtils/Hash.hpp>
|
||||||
|
#include <NazaraUtils/TypeName.hpp>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <Nazara/Core/Debug.hpp>
|
#include <Nazara/Core/Debug.hpp>
|
||||||
|
|
||||||
|
|
@ -21,17 +22,18 @@ namespace Nz
|
||||||
inline AppFilesystemComponent::AppFilesystemComponent(ApplicationBase& app) :
|
inline AppFilesystemComponent::AppFilesystemComponent(ApplicationBase& app) :
|
||||||
ApplicationComponent(app)
|
ApplicationComponent(app)
|
||||||
{
|
{
|
||||||
RegisterResourceTypes();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
const typename T::Params* AppFilesystemComponent::GetDefaultResourceParameters() const
|
const typename T::Params* AppFilesystemComponent::GetDefaultResourceParameters() const
|
||||||
{
|
{
|
||||||
std::size_t resourceIndex = ResourceRegistry<T>::GetResourceId();
|
constexpr UInt64 typeHash = FNV1a64(TypeName<T>());
|
||||||
if (resourceIndex >= m_defaultParameters.size())
|
|
||||||
|
auto it = m_defaultParameters.find(typeHash);
|
||||||
|
if (it == m_defaultParameters.end())
|
||||||
return nullptr;
|
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)
|
VirtualDirectoryPtr AppFilesystemComponent::GetDirectory(std::string_view assetPath)
|
||||||
|
|
@ -84,43 +86,9 @@ namespace Nz
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void AppFilesystemComponent::SetDefaultResourceParameters(typename T::Params params)
|
void AppFilesystemComponent::SetDefaultResourceParameters(typename T::Params params)
|
||||||
{
|
{
|
||||||
std::size_t resourceIndex = ResourceRegistry<T>::GetResourceId();
|
constexpr UInt64 typeHash = FNV1a64(TypeName<T>());
|
||||||
if (resourceIndex >= m_defaultParameters.size())
|
|
||||||
m_defaultParameters.resize(resourceIndex + 1);
|
|
||||||
|
|
||||||
m_defaultParameters[resourceIndex] = std::make_unique<typename T::Params>(std::move(params));
|
m_defaultParameters[typeHash] = 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");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T, typename... ExtraArgs>
|
template<typename T, typename... ExtraArgs>
|
||||||
|
|
|
||||||
|
|
@ -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 <NazaraUtils/Prerequisites.hpp>
|
|
||||||
#include <Nazara/Core/Config.hpp>
|
|
||||||
|
|
||||||
namespace Nz
|
|
||||||
{
|
|
||||||
template<typename T>
|
|
||||||
struct ResourceRegistry
|
|
||||||
{
|
|
||||||
static std::size_t GetResourceId();
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#include <Nazara/Core/ResourceRegistry.inl>
|
|
||||||
|
|
||||||
#endif // NAZARA_CORE_RESOURCEREGISTRY_HPP
|
|
||||||
|
|
@ -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 <Nazara/Core/Debug.hpp>
|
|
||||||
|
|
||||||
namespace Nz
|
|
||||||
{
|
|
||||||
namespace Detail
|
|
||||||
{
|
|
||||||
inline std::size_t ResourceTypeIndexCounter()
|
|
||||||
{
|
|
||||||
static std::size_t counter = 0;
|
|
||||||
return counter++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
std::size_t ResourceRegistry<T>::GetResourceId()
|
|
||||||
{
|
|
||||||
static std::size_t typeId = Detail::ResourceTypeIndexCounter();
|
|
||||||
return typeId;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#include <Nazara/Core/DebugOff.hpp>
|
|
||||||
|
|
@ -177,8 +177,6 @@ namespace Nz
|
||||||
|
|
||||||
void Graphics::RegisterComponent(AppFilesystemComponent& component)
|
void Graphics::RegisterComponent(AppFilesystemComponent& component)
|
||||||
{
|
{
|
||||||
AppFilesystemComponent::RegisterResourceTypes();
|
|
||||||
|
|
||||||
TextureParams defaultTexParams;
|
TextureParams defaultTexParams;
|
||||||
defaultTexParams.renderDevice = m_renderDevice;
|
defaultTexParams.renderDevice = m_renderDevice;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue