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

109 lines
2.7 KiB

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