[Assets] Handle streaming resources properly

This commit is contained in:
SweetId 2024-03-11 17:14:56 -04:00
parent 841778fe2f
commit cc91b8d3b9
1 changed files with 24 additions and 2 deletions

View File

@ -4,6 +4,12 @@
namespace Nz
{
template <typename TResource>
concept IsStreamingResource = requires(TResource)
{
{ TResource::OpenFromFile({}) } -> std::same_as<std::shared_ptr<TResource>>;
};
template <typename TResource>
Asset<TResource> AssetCatalog::LoadFromFile(const std::filesystem::path& path)
{
@ -20,11 +26,27 @@ namespace Nz
if (fs) // first try using a FileSystem component to load the asset
{
std::string filepath = asset.m_descriptor.path.string();
asset.m_resource = fs->Load<TResource>(std::string_view(filepath), asset.m_descriptor.parameters);
if constexpr (IsStreamingResource<TResource>)
{
asset.m_resource = fs->Open<TResource>(std::string_view(filepath), asset.m_descriptor.parameters);
}
else
{
asset.m_resource = fs->Load<TResource>(std::string_view(filepath), asset.m_descriptor.parameters);
}
}
if (!asset) // if it fails, use the default loader
asset.m_resource = TResource::LoadFromFile(asset.m_descriptor.path, asset.m_descriptor.parameters);
{
if constexpr (IsStreamingResource<TResource>)
{
asset.m_resource = TResource::OpenFromFile(asset.m_descriptor.path, asset.m_descriptor.parameters);
}
else
{
asset.m_resource = TResource::LoadFromFile(asset.m_descriptor.path, asset.m_descriptor.parameters);
}
}
return asset;
}