Core: Add AppFilesystemComponent
This commit is contained in:
parent
f98cbcd00a
commit
ca0c4a5db7
|
|
@ -45,6 +45,9 @@ int main()
|
|||
|
||||
auto& ecs = app.AddComponent<Nz::AppEntitySystemComponent>();
|
||||
|
||||
auto& fs = app.AddComponent<Nz::AppFilesystemComponent>();
|
||||
fs.RegisterPath(resourceDir);
|
||||
|
||||
Nz::RenderSystem& renderSystem = ecs.AddSystem<Nz::RenderSystem>();
|
||||
auto& windowSwapchain = renderSystem.CreateSwapchain(mainWindow);
|
||||
|
||||
|
|
@ -76,7 +79,7 @@ int main()
|
|||
texParams.loadFormat = Nz::PixelFormat::RGBA8_SRGB;
|
||||
|
||||
std::shared_ptr<Nz::MaterialInstance> materialInstance = material->Instantiate();
|
||||
materialInstance->SetTextureProperty("BaseColorMap", Nz::Texture::LoadFromFile(resourceDir / "Spaceship/Texture/diffuse.png", texParams));
|
||||
materialInstance->SetTextureProperty("BaseColorMap", fs.GetOrLoad<Nz::Texture>("Spaceship/Texture/diffuse.png", texParams));
|
||||
|
||||
Nz::ImageWidget* imageWidget = canvas2D.Add<Nz::ImageWidget>(materialInstance);
|
||||
imageWidget->SetPosition(1200.f, 200.f);
|
||||
|
|
|
|||
|
|
@ -32,6 +32,7 @@
|
|||
#include <Nazara/Core/AbstractHash.hpp>
|
||||
#include <Nazara/Core/AbstractLogger.hpp>
|
||||
#include <Nazara/Core/Algorithm.hpp>
|
||||
#include <Nazara/Core/AppFilesystemComponent.hpp>
|
||||
#include <Nazara/Core/Application.hpp>
|
||||
#include <Nazara/Core/ApplicationBase.hpp>
|
||||
#include <Nazara/Core/ApplicationComponent.hpp>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,44 @@
|
|||
// 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_APPFILESYSTEMCOMPONENT_HPP
|
||||
#define NAZARA_CORE_APPFILESYSTEMCOMPONENT_HPP
|
||||
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Core/ApplicationComponent.hpp>
|
||||
#include <Nazara/Core/Config.hpp>
|
||||
#include <Nazara/Core/VirtualDirectory.hpp>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class NAZARA_CORE_API AppFilesystemComponent : public ApplicationComponent
|
||||
{
|
||||
public:
|
||||
using ApplicationComponent::ApplicationComponent;
|
||||
AppFilesystemComponent(const AppFilesystemComponent&) = delete;
|
||||
AppFilesystemComponent(AppFilesystemComponent&&) = delete;
|
||||
~AppFilesystemComponent() = default;
|
||||
|
||||
template<typename T, typename... Args> std::shared_ptr<T> GetOrLoad(std::string_view assetPath, Args&&... args);
|
||||
|
||||
inline const VirtualDirectoryPtr& RegisterPath(std::filesystem::path filepath);
|
||||
inline const VirtualDirectoryPtr& RegisterVirtualDirectory(VirtualDirectoryPtr directory);
|
||||
|
||||
inline void UnregisterVirtualDirectory(const VirtualDirectoryPtr& directory);
|
||||
|
||||
AppFilesystemComponent& operator=(const AppFilesystemComponent&) = delete;
|
||||
AppFilesystemComponent& operator=(AppFilesystemComponent&&) = delete;
|
||||
|
||||
private:
|
||||
std::vector<VirtualDirectoryPtr> m_virtualDirectories;
|
||||
};
|
||||
}
|
||||
|
||||
#include <Nazara/Core/AppFilesystemComponent.inl>
|
||||
|
||||
#endif // NAZARA_CORE_APPFILESYSTEMCOMPONENT_HPP
|
||||
|
|
@ -0,0 +1,72 @@
|
|||
// 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/AppFilesystemComponent.hpp>
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
template<typename T, typename... Args>
|
||||
std::shared_ptr<T> AppFilesystemComponent::GetOrLoad(std::string_view assetPath, Args&&... args)
|
||||
{
|
||||
std::shared_ptr<T> resource;
|
||||
for (const VirtualDirectoryPtr& virtualDir : m_virtualDirectories)
|
||||
{
|
||||
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::LoadFromMemory(arg.data, arg.size, std::forward<Args>(args)...);
|
||||
return true;
|
||||
}
|
||||
else if constexpr (std::is_same_v<Param, VirtualDirectory::FileContentEntry>)
|
||||
{
|
||||
resource = T::LoadFromMemory(&arg.data[0], arg.data.size(), std::forward<Args>(args)...);
|
||||
return true;
|
||||
}
|
||||
else if constexpr (std::is_same_v<Param, VirtualDirectory::PhysicalFileEntry>)
|
||||
{
|
||||
resource = T::LoadFromFile(arg.filePath, std::forward<Args>(args)...);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
static_assert(AlwaysFalse<Param>(), "unhandled case");
|
||||
}, entry);
|
||||
};
|
||||
|
||||
if (virtualDir->GetEntry(assetPath, callback) && resource)
|
||||
return resource;
|
||||
}
|
||||
|
||||
return resource;
|
||||
}
|
||||
|
||||
inline const VirtualDirectoryPtr& AppFilesystemComponent::RegisterPath(std::filesystem::path filepath)
|
||||
{
|
||||
return RegisterVirtualDirectory(std::make_shared<VirtualDirectory>(std::move(filepath)));
|
||||
}
|
||||
|
||||
inline const VirtualDirectoryPtr& AppFilesystemComponent::RegisterVirtualDirectory(VirtualDirectoryPtr directory)
|
||||
{
|
||||
return m_virtualDirectories.emplace_back(std::move(directory));
|
||||
}
|
||||
|
||||
inline void AppFilesystemComponent::UnregisterVirtualDirectory(const VirtualDirectoryPtr& directory)
|
||||
{
|
||||
auto it = std::find(m_virtualDirectories.begin(), m_virtualDirectories.end(), directory);
|
||||
if (it == m_virtualDirectories.end())
|
||||
m_virtualDirectories.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
#include <Nazara/Core/DebugOff.hpp>
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
// 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/AppFilesystemComponent.hpp>
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
}
|
||||
Loading…
Reference in New Issue