[Assets] Making AssetCatalog an AppComponent
This commit is contained in:
parent
172d3ea720
commit
92179df553
|
|
@ -16,6 +16,8 @@ namespace Nz
|
|||
TResource* operator->() { return m_resource.get(); }
|
||||
const TResource* operator->() const { return m_resource.get(); }
|
||||
|
||||
static Asset LoadFromFile(const std::filesystem::path& path);
|
||||
|
||||
protected:
|
||||
AssetDescriptor<TResource> m_descriptor;
|
||||
std::shared_ptr<TResource> m_resource;
|
||||
|
|
@ -23,3 +25,5 @@ namespace Nz
|
|||
friend class AssetCatalog;
|
||||
};
|
||||
}
|
||||
|
||||
#include <Nazara/Core/Asset.inl>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,60 @@
|
|||
#include <Nazara/Core/ApplicationBase.hpp>
|
||||
#include <Nazara/Core/FilesystemAppComponent.hpp>
|
||||
#include <Nazara/Core/JsonSerialization.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
template <typename TResource>
|
||||
concept IsStreamingResource = requires(TResource)
|
||||
{
|
||||
{ TResource::OpenFromFile({}) } -> std::same_as<std::shared_ptr<TResource>>;
|
||||
};
|
||||
|
||||
template <typename TResource>
|
||||
Asset<TResource> Asset<TResource>::LoadFromFile(const std::filesystem::path& path)
|
||||
{
|
||||
Asset<TResource> asset;
|
||||
|
||||
FilesystemAppComponent* fs = ApplicationBase::Instance()->TryGetComponent<FilesystemAppComponent>();
|
||||
if (fs) // first try using a FileSystem component to load the asset
|
||||
{
|
||||
auto json = fs->Load<JsonSerializationContext>(std::string_view(path.string()));
|
||||
if (json)
|
||||
{
|
||||
if (!Unserialize(*json, "", &asset.m_descriptor))
|
||||
return {};
|
||||
|
||||
std::string filepath = asset.m_descriptor.path.string();
|
||||
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
|
||||
{
|
||||
JsonSerializationContext json;
|
||||
if (!json.Load(path))
|
||||
return {};
|
||||
|
||||
if (!Unserialize(json, "", &asset.m_descriptor))
|
||||
return {};
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,16 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <Nazara/Core/Asset.hpp>
|
||||
#include <Nazara/Core/Export.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class NAZARA_CORE_API AssetCatalog
|
||||
{
|
||||
public:
|
||||
template <typename TResource>
|
||||
static Asset<TResource> LoadFromFile(const std::filesystem::path& path);
|
||||
};
|
||||
}
|
||||
|
||||
#include <Nazara/Core/AssetCatalog.inl>
|
||||
|
|
@ -1,59 +0,0 @@
|
|||
#include <Nazara/Core/ApplicationBase.hpp>
|
||||
#include <Nazara/Core/FilesystemAppComponent.hpp>
|
||||
#include <Nazara/Core/JsonSerialization.hpp>
|
||||
|
||||
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)
|
||||
{
|
||||
Asset<TResource> asset;
|
||||
|
||||
FilesystemAppComponent* fs = ApplicationBase::Instance()->TryGetComponent<FilesystemAppComponent>();
|
||||
if (fs) // first try using a FileSystem component to load the asset
|
||||
{
|
||||
auto json = fs->Load<JsonSerializationContext>(std::string_view(path.string()));
|
||||
|
||||
if (!Unserialize(*json, "", &asset.m_descriptor))
|
||||
return {};
|
||||
|
||||
|
||||
std::string filepath = asset.m_descriptor.path.string();
|
||||
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
|
||||
{
|
||||
JsonSerializationContext json;
|
||||
if (!json.Load(path))
|
||||
return {};
|
||||
|
||||
if (!Unserialize(json, "", &asset.m_descriptor))
|
||||
return {};
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
#pragma once
|
||||
|
||||
#include <Nazara/Core/Asset.hpp>
|
||||
#include <Nazara/Core/ApplicationComponent.hpp>
|
||||
#include <Nazara/Core/Export.hpp>
|
||||
|
||||
#include <set>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
struct AssetHeaderLite
|
||||
{
|
||||
std::string name;
|
||||
std::filesystem::path path;
|
||||
|
||||
friend auto operator<=>(const AssetHeaderLite& A, const AssetHeaderLite& B)
|
||||
{
|
||||
return A.name <=> B.name;
|
||||
}
|
||||
};
|
||||
|
||||
NAZARA_CORE_API bool Serialize(struct SerializationContext& context, const AssetHeaderLite& header, TypeTag<AssetHeaderLite>);
|
||||
NAZARA_CORE_API bool Unserialize(struct SerializationContext& context, AssetHeaderLite* header, TypeTag<AssetHeaderLite>);
|
||||
|
||||
|
||||
class NAZARA_CORE_API AssetCatalogAppComponent
|
||||
: public ApplicationComponent
|
||||
{
|
||||
public:
|
||||
inline AssetCatalogAppComponent(ApplicationBase& app);
|
||||
AssetCatalogAppComponent(const AssetCatalogAppComponent&) = delete;
|
||||
AssetCatalogAppComponent(AssetCatalogAppComponent&&) = delete;
|
||||
~AssetCatalogAppComponent() = default;
|
||||
|
||||
void AddFolder(std::string_view folder);
|
||||
|
||||
template<typename TResource>
|
||||
inline Asset<TResource> Load(std::string_view name) const;
|
||||
|
||||
protected:
|
||||
std::set<AssetHeaderLite> m_assets;
|
||||
};
|
||||
}
|
||||
|
||||
#include <Nazara/Core/AssetCatalogAppComponent.inl>
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
|
||||
namespace Nz
|
||||
{
|
||||
inline AssetCatalogAppComponent::AssetCatalogAppComponent(ApplicationBase& app)
|
||||
: ApplicationComponent(app)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename TResource>
|
||||
Asset<TResource> AssetCatalogAppComponent::Load(std::string_view name) const
|
||||
{
|
||||
AssetHeaderLite header{ std::string(name), "" };
|
||||
auto it = m_assets.find(header);
|
||||
if (it == m_assets.end())
|
||||
return {};
|
||||
|
||||
return Asset<TResource>::LoadFromFile(it->path);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
|
||||
#include <Nazara/Core/AssetCatalogAppComponent.hpp>
|
||||
#include <Nazara/Core/Serialization.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
bool Serialize(SerializationContext& context, const AssetHeaderLite& header, TypeTag<AssetHeaderLite>)
|
||||
{
|
||||
Serialize(context, "name", header.name);
|
||||
Serialize(context, "path", header.path);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Unserialize(SerializationContext& context, AssetHeaderLite* header, TypeTag<AssetHeaderLite>)
|
||||
{
|
||||
Unserialize(context, "name", &header->name);
|
||||
Unserialize(context, "path", &header->path);
|
||||
return true;
|
||||
}
|
||||
|
||||
void AddFile(std::set<AssetHeaderLite>& container, Nz::Stream& stream)
|
||||
{
|
||||
AssetHeaderLite header;
|
||||
auto json = JsonSerializationContext::LoadFromStream(stream);
|
||||
if (!json)
|
||||
return;
|
||||
|
||||
if (!Unserialize(*json, "", &header))
|
||||
return;
|
||||
|
||||
header.path = stream.GetPath();
|
||||
container.insert(header);
|
||||
}
|
||||
|
||||
void AssetCatalogAppComponent::AddFolder(std::string_view folder)
|
||||
{
|
||||
auto& fs = GetApp().GetComponent<FilesystemAppComponent>();
|
||||
auto directory = fs.GetDirectory(folder);
|
||||
if (!directory)
|
||||
return;
|
||||
|
||||
directory->Foreach([&](std::string_view, const VirtualDirectory::Entry& entry) {
|
||||
if (std::holds_alternative<VirtualDirectory::FileEntry>(entry))
|
||||
{
|
||||
auto& file = std::get<VirtualDirectory::FileEntry>(entry);
|
||||
|
||||
if (file.stream->GetPath().extension() == ".nzasset")
|
||||
AddFile(m_assets, *file.stream.get());
|
||||
}
|
||||
}, true);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue