💿🐜 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.

102 lines
2.8 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. #include "resources/resource-manager.hpp"
  20. resource_manager::resource_manager()
  21. {
  22. // Init PhysicsFS
  23. debug::log::trace("Initializing PhysicsFS...");
  24. if (!PHYSFS_init(nullptr))
  25. {
  26. debug::log::error("Failed to initialize PhysicsFS: {}", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
  27. }
  28. else
  29. {
  30. debug::log::trace("Initialized PhysicsFS");
  31. }
  32. }
  33. resource_manager::~resource_manager()
  34. {
  35. debug::log::trace("Deleting cached resources...");
  36. // Delete cached resources
  37. for (auto it = resource_cache.begin(); it != resource_cache.end(); ++it)
  38. {
  39. delete it->second;
  40. }
  41. debug::log::trace("Deleted cached resources");
  42. // Deinit PhysicsFS
  43. debug::log::trace("Deinitializing PhysicsFS...");
  44. if (!PHYSFS_deinit())
  45. {
  46. debug::log::error("Failed to deinitialize PhysicsFS: {}", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
  47. }
  48. else
  49. {
  50. debug::log::trace("Deinitialized PhysicsFS");
  51. }
  52. }
  53. bool resource_manager::mount(const std::filesystem::path& path)
  54. {
  55. debug::log::trace("Mounting path \"{}\"...", path.string());
  56. if (!PHYSFS_mount(path.string().c_str(), nullptr, 1))
  57. {
  58. debug::log::error("Failed to mount path \"{}\": {}", path.string(), PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
  59. return false;
  60. }
  61. else
  62. {
  63. debug::log::trace("Mounted path \"{}\"", path.string());
  64. return true;
  65. }
  66. }
  67. void resource_manager::unload(const std::filesystem::path& path)
  68. {
  69. // Check if resource is in the cache
  70. auto it = resource_cache.find(path);
  71. if (it != resource_cache.end())
  72. {
  73. // Decrement the resource handle reference count
  74. --it->second->reference_count;
  75. // Free the resource if the resource handle is unreferenced
  76. if (it->second->reference_count <= 0)
  77. {
  78. debug::log::trace("Unloading resource \"{}\"...", path.string());
  79. delete it->second;
  80. debug::log::trace("Unloaded resource \"{}\"", path.string());
  81. }
  82. // Remove resource from the cache
  83. resource_cache.erase(it);
  84. }
  85. }
  86. void resource_manager::include(const std::filesystem::path& search_path)
  87. {
  88. search_paths.push_back(search_path);
  89. }