Core/AppFilesystemComponent: Add Open method
This commit is contained in:
parent
3d2aa94ac4
commit
304bf35c08
|
|
@ -43,6 +43,9 @@ namespace Nz
|
|||
inline const VirtualDirectoryPtr& Mount(std::string_view name, std::filesystem::path filepath);
|
||||
inline const VirtualDirectoryPtr& Mount(std::string_view name, VirtualDirectoryPtr directory);
|
||||
|
||||
template<typename T> std::shared_ptr<T> Open(std::string_view assetPath);
|
||||
template<typename T> std::shared_ptr<T> Open(std::string_view assetPath, typename T::Params params);
|
||||
|
||||
template<typename T> void SetDefaultResourceParameters(typename T::Params params);
|
||||
|
||||
AppFilesystemComponent& operator=(const AppFilesystemComponent&) = delete;
|
||||
|
|
@ -52,6 +55,7 @@ namespace Nz
|
|||
|
||||
private:
|
||||
template<typename T> std::shared_ptr<T> LoadImpl(std::string_view assetPath, const typename T::Params& params);
|
||||
template<typename T> std::shared_ptr<T> OpenImpl(std::string_view assetPath, const typename T::Params& params);
|
||||
|
||||
std::vector<std::unique_ptr<ResourceParameters>> m_defaultParameters;
|
||||
VirtualDirectoryPtr m_rootDirectory;
|
||||
|
|
|
|||
|
|
@ -72,6 +72,24 @@ namespace Nz
|
|||
return m_rootDirectory->StoreDirectory(name, std::move(directory)).directory;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::shared_ptr<T> AppFilesystemComponent::Open(std::string_view assetPath)
|
||||
{
|
||||
return Open<T>(assetPath, typename T::Params{});
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::shared_ptr<T> AppFilesystemComponent::Open(std::string_view assetPath, typename T::Params params)
|
||||
{
|
||||
if constexpr (Detail::ResourceParameterHasMerge<typename T::Params>::value)
|
||||
{
|
||||
if (const auto* defaultParams = GetDefaultResourceParameters<T>())
|
||||
params.Merge(*defaultParams);
|
||||
}
|
||||
|
||||
return OpenImpl<T>(assetPath, params);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void AppFilesystemComponent::SetDefaultResourceParameters(typename T::Params params)
|
||||
{
|
||||
|
|
@ -154,6 +172,47 @@ namespace Nz
|
|||
m_rootDirectory->GetEntry(assetPath, callback);
|
||||
return resource;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::shared_ptr<T> AppFilesystemComponent::OpenImpl(std::string_view assetPath, const typename T::Params& params)
|
||||
{
|
||||
std::shared_ptr<T> resource;
|
||||
if (!m_rootDirectory)
|
||||
return resource;
|
||||
|
||||
auto callback = [&](const VirtualDirectory::Entry& entry)
|
||||
{
|
||||
return std::visit([&](auto&& arg)
|
||||
{
|
||||
using Param = std::decay_t<decltype(arg)>;
|
||||
if constexpr (std::is_base_of_v<VirtualDirectory::DirectoryEntry, Param>)
|
||||
{
|
||||
NazaraError(std::string(assetPath) + " is a directory");
|
||||
return false;
|
||||
}
|
||||
else if constexpr (std::is_same_v<Param, VirtualDirectory::DataPointerEntry>)
|
||||
{
|
||||
resource = T::OpenFromMemory(arg.data, arg.size, params);
|
||||
return true;
|
||||
}
|
||||
else if constexpr (std::is_same_v<Param, VirtualDirectory::FileContentEntry>)
|
||||
{
|
||||
resource = T::OpenFromMemory(&arg.data[0], arg.data.size(), params);
|
||||
return true;
|
||||
}
|
||||
else if constexpr (std::is_same_v<Param, VirtualDirectory::PhysicalFileEntry>)
|
||||
{
|
||||
resource = T::OpenFromFile(arg.filePath, params);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
static_assert(AlwaysFalse<Param>(), "unhandled case");
|
||||
}, entry);
|
||||
};
|
||||
|
||||
m_rootDirectory->GetEntry(assetPath, callback);
|
||||
return resource;
|
||||
}
|
||||
}
|
||||
|
||||
#include <Nazara/Core/DebugOff.hpp>
|
||||
|
|
|
|||
Loading…
Reference in New Issue