[Assets] add base classes and functionnalities

This commit is contained in:
SweetId 2024-03-10 17:05:54 -04:00
parent 6a6ffb6779
commit 467f471390
6 changed files with 128 additions and 0 deletions

View File

@ -0,0 +1,20 @@
#pragma once
#include <Nazara/Core/AssetDescriptor.hpp>
namespace Nz
{
template <typename TResource>
class Asset final
{
public:
const std::shared_ptr<TResource>& Get() const { return m_resource; }
explicit operator bool() const noexcept { return !!m_resource; }
protected:
AssetDescriptor<TResource> m_descriptor;
std::shared_ptr<TResource> m_resource;
friend class AssetCatalog;
};
}

View File

View File

@ -0,0 +1,16 @@
#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>

View File

@ -0,0 +1,31 @@
#include <Nazara/Core/ApplicationBase.hpp>
#include <Nazara/Core/FilesystemAppComponent.hpp>
#include <Nazara/Core/JsonSerialization.hpp>
namespace Nz
{
template <typename TResource>
Asset<TResource> AssetCatalog::LoadFromFile(const std::filesystem::path& path)
{
Asset<TResource> asset;
JsonSerializationContext json;
if (!json.Load(path))
return {};
if (!Unserialize(json, "", &asset.m_descriptor))
return {};
FilesystemAppComponent* fs = ApplicationBase::Instance()->TryGetComponent<FilesystemAppComponent>();
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 (!asset) // if it fails, use the default loader
asset.m_resource = TResource::LoadFromFile(asset.m_descriptor.path, asset.m_descriptor.parameters);
return asset;
}
}

View File

@ -0,0 +1,24 @@
#pragma once
#include <Nazara/Core/Serialization.hpp>
#include <NazaraUtils/TypeName.hpp>
namespace Nz
{
template <typename TResource>
struct AssetDescriptor
{
constexpr static std::string_view Type = TypeName<TResource>();
int version;
std::string type;
std::string name;
std::filesystem::path path;
TResource::Params parameters;
};
template <typename TResource> bool Serialize(SerializationContext& context, const AssetDescriptor<TResource>& descriptor, TypeTag<AssetDescriptor<TResource>>);
template <typename TResource> bool Unserialize(SerializationContext& context, AssetDescriptor<TResource>* descriptor, TypeTag<AssetDescriptor<TResource>>);
}
#include <Nazara/Core/AssetDescriptor.inl>

View File

@ -0,0 +1,37 @@
namespace Nz
{
template <typename TResource>
bool Serialize(SerializationContext& context, const AssetDescriptor<TResource>& descriptor, TypeTag<AssetDescriptor<TResource>>)
{
Serialize(context, "version", descriptor.version);
Serialize(context, "type", descriptor.type);
Serialize(context, "name", descriptor.name);
Serialize(context, "path", descriptor.path);
Serialize(context, "parameters", descriptor.parameters);
return true;
}
template <typename TResource>
bool Unserialize(SerializationContext& context, AssetDescriptor<TResource>* descriptor, TypeTag<AssetDescriptor<TResource>>)
{
if (!Unserialize(context, "version", &descriptor->version))
return false;
if (!Unserialize(context, "type", &descriptor->type))
return false;
if (AssetDescriptor<TResource>::Type != descriptor->type)
return false;
if (!Unserialize(context, "name", &descriptor->name))
return false;
if (!Unserialize(context, "path", &descriptor->path))
return false;
if (!Unserialize(context, "parameters", &descriptor->parameters))
return false;
return true;
}
}