// 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 void ResourceManager::Clear() { Type::s_managerMap.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 ObjectRef ResourceManager::Get(const std::filesystem::path& filePath) { std::filesystem::path absolutePath = std::filesystem::canonical(filePath); auto it = Type::s_managerMap.find(absolutePath); if (it == Type::s_managerMap.end()) { ObjectRef resource = Type::LoadFromFile(absolutePath, GetDefaultParameters()); if (!resource) { NazaraError("Failed to load resource from file: " + absolutePath.generic_u8string()); return ObjectRef(); } NazaraDebug("Loaded resource from file " + absolutePath.generic_u8string()); it = Type::s_managerMap.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 Type::s_managerParameters; } /*! * \brief Purges the resource manager from every asset whose it is the only owner */ template void ResourceManager::Purge() { auto it = Type::s_managerMap.begin(); while (it != Type::s_managerMap.end()) { const ObjectRef& ref = it->second; if (ref->GetReferenceCount() == 1) // Are we the only ones to own the resource ? { NazaraDebug("Purging resource from file " + ref->GetFilePath().generic_u8string()); Type::s_managerMap.erase(it++); // Then we erase it } else ++it; } } /*! * \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, ObjectRef resource) { std::filesystem::path absolutePath = std::filesystem::canonical(filePath); Type::s_managerMap[absolutePath] = resource; } /*! * \brief Sets the defaults parameters for the load * * \param params Default parameters for loading from file */ template void ResourceManager::SetDefaultParameters(const Parameters& params) { Type::s_managerParameters = 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); Type::s_managerMap.erase(absolutePath); } /*! * \brief Initializes the resource manager * \return true */ template bool ResourceManager::Initialize() { return true; } /*! * \brief Uninitialize the resource manager */ template void ResourceManager::Uninitialize() { Clear(); } } #include