resource_manager.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright (c) 2012-2023 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: MIT
  4. */
  5. #include "core/containers/array.inl"
  6. #include "core/containers/hash_map.inl"
  7. #include "core/memory/temp_allocator.inl"
  8. #include "core/strings/dynamic_string.inl"
  9. #include "core/strings/string_id.inl"
  10. #include "resource/resource_id.inl"
  11. #include "resource/resource_loader.h"
  12. #include "resource/resource_manager.h"
  13. namespace crown
  14. {
  15. bool operator<(const ResourceManager::ResourcePair &a, const ResourceManager::ResourcePair &b)
  16. {
  17. return a.type < b.type
  18. || (a.type == b.type && a.name < b.name)
  19. ;
  20. }
  21. bool operator==(const ResourceManager::ResourcePair &a, const ResourceManager::ResourcePair &b)
  22. {
  23. return a.type == b.type
  24. && a.name == b.name
  25. ;
  26. }
  27. bool operator==(const ResourceManager::ResourceEntry &a, const ResourceManager::ResourceEntry &b)
  28. {
  29. return a.references == b.references
  30. && a.data == b.data
  31. ;
  32. }
  33. const ResourceManager::ResourceEntry ResourceManager::ResourceEntry::NOT_FOUND = { 0xffffffffu, NULL, NULL };
  34. template<>
  35. struct hash<ResourceManager::ResourcePair>
  36. {
  37. u32 operator()(const ResourceManager::ResourcePair &val) const
  38. {
  39. return u32(resource_id(val.type, val.name)._id);
  40. }
  41. };
  42. ResourceManager::ResourceManager(ResourceLoader &rl)
  43. : _resource_heap(default_allocator(), "resource")
  44. , _loader(&rl)
  45. , _type_data(default_allocator())
  46. , _rm(default_allocator())
  47. , _autoload(false)
  48. {
  49. }
  50. ResourceManager::~ResourceManager()
  51. {
  52. auto cur = hash_map::begin(_rm);
  53. auto end = hash_map::end(_rm);
  54. for (; cur != end; ++cur) {
  55. HASH_MAP_SKIP_HOLE(_rm, cur);
  56. const StringId64 type = cur->first.type;
  57. const StringId64 name = cur->first.name;
  58. on_offline(type, name);
  59. on_unload(type, cur->second.allocator, cur->second.data);
  60. }
  61. }
  62. bool ResourceManager::try_load(StringId64 package_name, StringId64 type, StringId64 name)
  63. {
  64. ResourcePair id = { type, name };
  65. ResourceEntry &entry = hash_map::get(_rm, id, ResourceEntry::NOT_FOUND);
  66. if (entry == ResourceEntry::NOT_FOUND) {
  67. ResourceTypeData rtd;
  68. rtd.version = UINT32_MAX;
  69. rtd.load = NULL;
  70. rtd.online = NULL;
  71. rtd.offline = NULL;
  72. rtd.unload = NULL;
  73. rtd = hash_map::get(_type_data, type, rtd);
  74. ResourceRequest rr;
  75. rr.resource_manager = this;
  76. rr.package_name = package_name;
  77. rr.type = type;
  78. rr.name = name;
  79. rr.version = rtd.version;
  80. rr.load_function = rtd.load;
  81. rr.allocator = &_resource_heap;
  82. rr.data = NULL;
  83. return _loader->add_request(rr);
  84. }
  85. entry.references++;
  86. return true;
  87. }
  88. void ResourceManager::unload(StringId64 type, StringId64 name)
  89. {
  90. ResourcePair id = { type, name };
  91. ResourceEntry &entry = hash_map::get(_rm, id, ResourceEntry::NOT_FOUND);
  92. if (--entry.references == 0) {
  93. on_offline(type, name);
  94. on_unload(type, entry.allocator, entry.data);
  95. hash_map::remove(_rm, id);
  96. }
  97. }
  98. void ResourceManager::reload(StringId64 type, StringId64 name)
  99. {
  100. const ResourcePair id = { type, name };
  101. const ResourceEntry &entry = hash_map::get(_rm, id, ResourceEntry::NOT_FOUND);
  102. const u32 old_refs = entry.references;
  103. if (entry == ResourceEntry::NOT_FOUND)
  104. return;
  105. unload(type, name);
  106. while (!try_load(PACKAGE_RESOURCE_NONE, type, name)) {
  107. complete_requests();
  108. }
  109. ResourceEntry &new_entry = hash_map::get(_rm, id, ResourceEntry::NOT_FOUND);
  110. new_entry.references = old_refs;
  111. }
  112. bool ResourceManager::can_get(StringId64 type, StringId64 name)
  113. {
  114. const ResourcePair id = { type, name };
  115. return _autoload ? true : hash_map::has(_rm, id);
  116. }
  117. const void *ResourceManager::get(StringId64 type, StringId64 name)
  118. {
  119. const ResourcePair id = { type, name };
  120. CE_ASSERT(can_get(type, name), "Resource not loaded: " RESOURCE_ID_FMT, resource_id(type, name)._id);
  121. if (_autoload && !hash_map::has(_rm, id)) {
  122. while (!try_load(PACKAGE_RESOURCE_NONE, type, name)) {
  123. complete_requests();
  124. }
  125. while (!hash_map::has(_rm, id)) {
  126. complete_requests();
  127. }
  128. }
  129. const ResourceEntry &entry = hash_map::get(_rm, id, ResourceEntry::NOT_FOUND);
  130. return entry.data;
  131. }
  132. void ResourceManager::enable_autoload(bool enable)
  133. {
  134. _autoload = enable;
  135. }
  136. void ResourceManager::complete_requests()
  137. {
  138. ResourceRequest rr;
  139. while (_loader->_loaded.pop(rr)) {
  140. ResourceEntry entry;
  141. entry.references = 1;
  142. entry.data = rr.data;
  143. entry.allocator = rr.allocator;
  144. ResourcePair id = { rr.type, rr.name };
  145. hash_map::set(_rm, id, entry);
  146. on_online(rr.type, rr.name);
  147. }
  148. }
  149. void ResourceManager::register_type(StringId64 type, u32 version, LoadFunction load, UnloadFunction unload, OnlineFunction online, OfflineFunction offline)
  150. {
  151. ResourceTypeData rtd;
  152. rtd.version = version;
  153. rtd.load = load;
  154. rtd.online = online;
  155. rtd.offline = offline;
  156. rtd.unload = unload;
  157. hash_map::set(_type_data, type, rtd);
  158. }
  159. void ResourceManager::on_online(StringId64 type, StringId64 name)
  160. {
  161. OnlineFunction func = hash_map::get(_type_data, type, ResourceTypeData()).online;
  162. if (func)
  163. func(name, *this);
  164. }
  165. void ResourceManager::on_offline(StringId64 type, StringId64 name)
  166. {
  167. OfflineFunction func = hash_map::get(_type_data, type, ResourceTypeData()).offline;
  168. if (func)
  169. func(name, *this);
  170. }
  171. void ResourceManager::on_unload(StringId64 type, Allocator *allocator, void *data)
  172. {
  173. if (allocator == NULL)
  174. return;
  175. UnloadFunction func = hash_map::get(_type_data, type, ResourceTypeData()).unload;
  176. if (func)
  177. func(*allocator, data);
  178. else
  179. allocator->deallocate(data);
  180. }
  181. } // namespace crown