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

133 lines
3.7 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. #include "debug/log.hpp"
  21. #include <stdexcept>
  22. resource_manager::resource_manager()
  23. {
  24. // Log PhysicsFS info
  25. // PHYSFS_Version physfs_compiled_version;
  26. // PHYSFS_Version physfs_linked_version;
  27. // PHYSFS_VERSION(&physfs_compiled_version);
  28. // PHYSFS_getLinkedVersion(&physfs_linked_version);
  29. // debug::log::info
  30. // (
  31. // "PhysicsFS compiled version: {}.{}.{}; linked version: {}.{}.{}",
  32. // physfs_compiled_version.major,
  33. // physfs_compiled_version.minor,
  34. // physfs_compiled_version.patch,
  35. // physfs_linked_version.major,
  36. // physfs_linked_version.minor,
  37. // physfs_linked_version.patch
  38. // );
  39. // Init PhysicsFS
  40. debug::log::trace("Initializing PhysicsFS...");
  41. if (!PHYSFS_init(nullptr))
  42. {
  43. debug::log::error("Failed to initialize PhysicsFS: {}", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
  44. throw std::runtime_error("Failed to initialize PhysicsFS");
  45. }
  46. else
  47. {
  48. debug::log::trace("Initialized PhysicsFS");
  49. }
  50. }
  51. resource_manager::~resource_manager()
  52. {
  53. debug::log::trace("Deleting cached resources...");
  54. // Delete cached resources
  55. for (auto it = resource_cache.begin(); it != resource_cache.end(); ++it)
  56. {
  57. delete it->second;
  58. }
  59. debug::log::trace("Deleted cached resources");
  60. // Deinit PhysicsFS
  61. debug::log::trace("Deinitializing PhysicsFS...");
  62. if (!PHYSFS_deinit())
  63. {
  64. debug::log::error("Failed to deinitialize PhysicsFS: {}", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
  65. }
  66. else
  67. {
  68. debug::log::trace("Deinitialized PhysicsFS");
  69. }
  70. }
  71. bool resource_manager::mount(const std::filesystem::path& path)
  72. {
  73. debug::log::trace("Mounting path \"{}\"...", path.string());
  74. if (!PHYSFS_mount(path.string().c_str(), nullptr, 1))
  75. {
  76. debug::log::error("Failed to mount path \"{}\": {}", path.string(), PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
  77. return false;
  78. }
  79. else
  80. {
  81. debug::log::trace("Mounted path \"{}\"", path.string());
  82. return true;
  83. }
  84. }
  85. void resource_manager::set_write_dir(const std::filesystem::path& path)
  86. {
  87. if (!PHYSFS_setWriteDir(path.string().c_str()))
  88. {
  89. debug::log::error("Failed set write directory to \"{}\": {}", path.string(), PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
  90. }
  91. else
  92. {
  93. debug::log::trace("Set write directory to \"{}\"", path.string());
  94. }
  95. }
  96. void resource_manager::unload(const std::filesystem::path& path)
  97. {
  98. // Check if resource is in the cache
  99. auto it = resource_cache.find(path);
  100. if (it != resource_cache.end())
  101. {
  102. // Decrement the resource handle reference count
  103. --it->second->reference_count;
  104. // Free the resource if the resource handle is unreferenced
  105. if (it->second->reference_count <= 0)
  106. {
  107. debug::log::trace("Unloading resource \"{}\"...", path.string());
  108. delete it->second;
  109. debug::log::trace("Unloaded resource \"{}\"", path.string());
  110. }
  111. // Remove resource from the cache
  112. resource_cache.erase(it);
  113. }
  114. }
  115. void resource_manager::include(const std::filesystem::path& search_path)
  116. {
  117. search_paths.push_back(search_path);
  118. }