// Copyright (C) 2020 Jérôme Leclercq // This file is part of the "Nazara Engine - Core module" // For conditions of distribution and use, see copyright notice in Config.hpp #include #include #include #include namespace Nz { /*! * \ingroup core * \class Nz::ResourceManager * \brief Core class that represents a resource manager */ /*! * \brief Clears the content of the manager */ template ResourceManager::ResourceManager(Loader& loader) : m_loader(loader) { } /*! * \brief Clears the content of the manager */ template void ResourceManager::Clear() { m_resources.clear(); } /*! * \brief Gets a reference to the object loaded from file * \return Reference to the object * * \param filePath Path to the asset that will be loaded */ template std::shared_ptr ResourceManager::Get(const std::filesystem::path& filePath) { std::filesystem::path absolutePath = std::filesystem::canonical(filePath); auto it = m_resources.find(absolutePath); if (it == m_resources.end()) { std::shared_ptr resource = m_loader.LoadFromFile(absolutePath, GetDefaultParameters()); if (!resource) { NazaraError("Failed to load resource from file: " + absolutePath.generic_u8string()); return std::shared_ptr(); } NazaraDebug("Loaded resource from file " + absolutePath.generic_u8string()); it = m_resources.insert(std::make_pair(absolutePath, resource)).first; } return it->second; } /*! * \brief Gets the defaults parameters for the load * \return Default parameters for loading from file */ template const Parameters& ResourceManager::GetDefaultParameters() { return m_defaultParameters; } /*! * \brief Registers the resource under the filePath * * \param filePath Path for the resource * \param resource Object to associate with */ template void ResourceManager::Register(const std::filesystem::path& filePath, std::shared_ptr resource) { std::filesystem::path absolutePath = std::filesystem::canonical(filePath); m_resources[absolutePath] = resource; } /*! * \brief Sets the defaults parameters for the load * * \param params Default parameters for loading from file */ template void ResourceManager::SetDefaultParameters(Parameters params) { m_defaultParameters = std::move(params); } /*! * \brief Unregisters the resource under the filePath * * \param filePath Path for the resource */ template void ResourceManager::Unregister(const std::filesystem::path& filePath) { std::filesystem::path absolutePath = std::filesystem::canonical(filePath); m_resources.erase(absolutePath); } } #include