💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

210 lines
5.5 KiB

  1. /*
  2. * Copyright (C) 2023 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef ANTKEEPER_RESOURCES_RESOURCE_MANAGER_HPP
  20. #define ANTKEEPER_RESOURCES_RESOURCE_MANAGER_HPP
  21. #include "resources/resource-handle.hpp"
  22. #include "resources/resource-loader.hpp"
  23. #include "debug/log.hpp"
  24. #include <filesystem>
  25. #include <list>
  26. #include <map>
  27. #include <physfs.h>
  28. #include <stdexcept>
  29. #include <string>
  30. /**
  31. * Loads resources.
  32. */
  33. class resource_manager
  34. {
  35. public:
  36. /**
  37. * Creates a resource manager.
  38. */
  39. resource_manager();
  40. /**
  41. * Destroys a resource manager and frees all of its resources.
  42. */
  43. ~resource_manager();
  44. bool mount(const std::filesystem::path& path);
  45. void set_write_dir(const std::filesystem::path& path);
  46. /**
  47. * Adds a path to be searched when a resource is requested.
  48. *
  49. * @param path Search path.
  50. */
  51. void include(const std::filesystem::path& path);
  52. /**
  53. * Loads the requested resource. If the resource has already been loaded it will be retrieved from the resource cache and its reference count incremented.
  54. *
  55. * @tparam T Resource type.
  56. * @param path Path to the resource, relative to the search paths.
  57. * @return Pointer to the requested resource, or nullptr if the resource could not be found nor loaded.
  58. */
  59. template <typename T>
  60. T* load(const std::filesystem::path& path);
  61. /**
  62. * Decrements a resource's reference count and unloads the resource if it's unreferenced.
  63. *
  64. * @param path Path to the resource, relative to the search paths.
  65. */
  66. void unload(const std::filesystem::path& path);
  67. /**
  68. * Saves the specified resource.
  69. *
  70. * @tparam T Resource type.
  71. * @param resource Pointer to the resource.
  72. * @param path Path to the resource.
  73. */
  74. template <typename T>
  75. void save(const T* resource, const std::filesystem::path& path);
  76. private:
  77. std::map<std::filesystem::path, resource_handle_base*> resource_cache;
  78. std::list<std::filesystem::path> search_paths;
  79. };
  80. template <typename T>
  81. T* resource_manager::load(const std::filesystem::path& path)
  82. {
  83. // Check if resource is in the cache
  84. auto it = resource_cache.find(path);
  85. if (it != resource_cache.end())
  86. {
  87. //debug::log::trace("Fetched cached resource \"{}\"". path.string());
  88. // Resource found
  89. resource_handle<T>* resource = static_cast<resource_handle<T>*>(it->second);
  90. // Increment resource handle reference count
  91. ++resource->reference_count;
  92. // Return resource data
  93. return resource->data;
  94. }
  95. debug::log::trace("Loading resource \"{}\"...", path.string());
  96. // Resource not cached, look for file in search paths
  97. T* data = nullptr;
  98. bool found = false;
  99. for (const std::filesystem::path& search_path: search_paths)
  100. {
  101. std::filesystem::path full_path = search_path / path;
  102. // Check if file exists
  103. if (!PHYSFS_exists(full_path.string().c_str()))
  104. {
  105. continue;
  106. }
  107. // File found
  108. found = true;
  109. // Open file for reading
  110. PHYSFS_File* file = PHYSFS_openRead(full_path.string().c_str());
  111. if (!file)
  112. {
  113. debug::log::error("Failed to load resource \"{}\": {}", path.string(), PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
  114. break;
  115. }
  116. // Load opened file
  117. try
  118. {
  119. data = resource_loader<T>::load(this, file, full_path);
  120. }
  121. catch (const std::exception& e)
  122. {
  123. debug::log::error("Failed to load resource \"{}\": {}", path.string(), e.what());
  124. }
  125. // Close opened file
  126. if (!PHYSFS_close(file))
  127. {
  128. debug::log::error("Failed to close resource file \"{}\": {}", path.string(), PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
  129. }
  130. break;
  131. }
  132. if (!data)
  133. {
  134. if (!found)
  135. {
  136. debug::log::error("Failed to load resource \"{}\": file not found", path.string());
  137. }
  138. return nullptr;
  139. }
  140. // Create a resource handle for the resource data
  141. resource_handle<T>* resource = new resource_handle<T>();
  142. resource->data = data;
  143. resource->reference_count = 1;
  144. // Add resource to the cache
  145. resource_cache[path] = resource;
  146. debug::log::trace("Loaded resource \"{}\"", path.string());
  147. return resource->data;
  148. }
  149. template <typename T>
  150. void resource_manager::save(const T* resource, const std::filesystem::path& path)
  151. {
  152. debug::log::trace("Saving resource to \"{}\"", path.string());
  153. // Open file for writing
  154. PHYSFS_File* file = PHYSFS_openWrite(path.string().c_str());
  155. if (!file)
  156. {
  157. debug::log::error("Failed to save resource to \"{}\": {}", path.string(), PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
  158. return;
  159. }
  160. // Save to opened file
  161. try
  162. {
  163. resource_loader<T>::save(this, file, path, resource);
  164. debug::log::trace("Saved resource to \"{}\"", path.string());
  165. }
  166. catch (const std::exception& e)
  167. {
  168. debug::log::error("Failed to save resource to \"{}\": {}", e.what());
  169. }
  170. // Close opened file
  171. if (!PHYSFS_close(file))
  172. {
  173. debug::log::error("Failed to close file \"{}\": {}", path.string(), PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
  174. }
  175. }
  176. #endif // ANTKEEPER_RESOURCES_RESOURCE_MANAGER_HPP