resource_manager.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "resource_manager.h"
  6. #include "resource_registry.h"
  7. #include "temp_allocator.h"
  8. #include "sort_map.h"
  9. namespace crown
  10. {
  11. const ResourceManager::ResourceEntry ResourceManager::ResourceEntry::NOT_FOUND = { 0xffffffffu, NULL };
  12. ResourceManager::ResourceManager(Filesystem& fs)
  13. : _resource_heap("resource", default_allocator())
  14. , _loader(fs, _resource_heap)
  15. , _rm(default_allocator())
  16. , _autoload(false)
  17. {
  18. }
  19. ResourceManager::~ResourceManager()
  20. {
  21. const ResourceMap::Entry* begin = sort_map::begin(_rm);
  22. const ResourceMap::Entry* end = sort_map::end(_rm);
  23. for (; begin != end; begin++)
  24. {
  25. resource_on_offline(begin->key.type, begin->key.name, *this);
  26. resource_on_unload(begin->key.type, _resource_heap, begin->value.data);
  27. }
  28. }
  29. void ResourceManager::load(StringId64 type, StringId64 name)
  30. {
  31. ResourceId id(type, name);
  32. ResourceEntry& entry = sort_map::get(_rm, id, ResourceEntry::NOT_FOUND);
  33. if (entry == ResourceEntry::NOT_FOUND)
  34. {
  35. _loader.load(id);
  36. return;
  37. }
  38. entry.references++;
  39. }
  40. void ResourceManager::unload(StringId64 type, StringId64 name)
  41. {
  42. flush();
  43. ResourceId id(type, name);
  44. ResourceEntry& entry = sort_map::get(_rm, id, ResourceEntry::NOT_FOUND);
  45. if (--entry.references == 0)
  46. {
  47. resource_on_offline(type, name, *this);
  48. resource_on_unload(type, _resource_heap, entry.data);
  49. sort_map::remove(_rm, id);
  50. sort_map::sort(_rm);
  51. }
  52. }
  53. void ResourceManager::reload(StringId64 type, StringId64 name)
  54. {
  55. const ResourceId id(type, name);
  56. const ResourceEntry& entry = sort_map::get(_rm, id, ResourceEntry::NOT_FOUND);
  57. const uint32_t old_refs = entry.references;
  58. unload(type, name);
  59. load(type, name);
  60. flush();
  61. ResourceEntry& new_entry = sort_map::get(_rm, id, ResourceEntry::NOT_FOUND);
  62. new_entry.references = old_refs;
  63. }
  64. bool ResourceManager::can_get(StringId64 type, StringId64 name)
  65. {
  66. return _autoload ? true : sort_map::has(_rm, ResourceId(type, name));
  67. }
  68. const void* ResourceManager::get(StringId64 type, StringId64 name)
  69. {
  70. ResourceId id(type, name);
  71. char buf[64];
  72. CE_ASSERT(can_get(type, name), "Resource not loaded #ID(%s)", id.to_string(buf));
  73. CE_UNUSED(buf);
  74. if (_autoload && !sort_map::has(_rm, id))
  75. {
  76. load(type, name);
  77. flush();
  78. }
  79. const ResourceEntry& entry = sort_map::get(_rm, id, ResourceEntry::NOT_FOUND);
  80. return entry.data;
  81. }
  82. void ResourceManager::enable_autoload(bool enable)
  83. {
  84. _autoload = enable;
  85. }
  86. void ResourceManager::flush()
  87. {
  88. _loader.flush();
  89. complete_requests();
  90. }
  91. void ResourceManager::complete_requests()
  92. {
  93. TempAllocator1024 ta;
  94. Array<ResourceData> loaded(ta);
  95. _loader.get_loaded(loaded);
  96. for (uint32_t i = 0; i < array::size(loaded); i++)
  97. complete_request(loaded[i].id, loaded[i].data);
  98. }
  99. void ResourceManager::complete_request(ResourceId id, void* data)
  100. {
  101. ResourceEntry entry;
  102. entry.references = 1;
  103. entry.data = data;
  104. sort_map::set(_rm, id, entry);
  105. sort_map::sort(_rm);
  106. resource_on_online(id.type, id.name, *this);
  107. }
  108. } // namespace crown