resource_manager.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright (c) 2012-2020 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/dbartolini/crown/blob/master/LICENSE
  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.h"
  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 };
  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. {
  56. HASH_MAP_SKIP_HOLE(_rm, cur);
  57. const StringId64 type = cur->first.type;
  58. const StringId64 name = cur->first.name;
  59. on_offline(type, name);
  60. on_unload(type, cur->second.data);
  61. }
  62. }
  63. void ResourceManager::load(StringId64 type, StringId64 name)
  64. {
  65. ResourcePair id = { type, name };
  66. ResourceEntry& entry = hash_map::get(_rm, id, ResourceEntry::NOT_FOUND);
  67. if (entry == ResourceEntry::NOT_FOUND)
  68. {
  69. ResourceTypeData rtd;
  70. rtd.version = UINT32_MAX;
  71. rtd.load = NULL;
  72. rtd.online = NULL;
  73. rtd.offline = NULL;
  74. rtd.unload = NULL;
  75. rtd = hash_map::get(_type_data, type, rtd);
  76. ResourceRequest rr;
  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. _loader->add_request(rr);
  84. return;
  85. }
  86. entry.references++;
  87. }
  88. void ResourceManager::unload(StringId64 type, StringId64 name)
  89. {
  90. flush();
  91. ResourcePair id = { type, name };
  92. ResourceEntry& entry = hash_map::get(_rm, id, ResourceEntry::NOT_FOUND);
  93. if (--entry.references == 0)
  94. {
  95. on_offline(type, name);
  96. on_unload(type, entry.data);
  97. hash_map::remove(_rm, id);
  98. }
  99. }
  100. void ResourceManager::reload(StringId64 type, StringId64 name)
  101. {
  102. const ResourcePair id = { type, name };
  103. const ResourceEntry& entry = hash_map::get(_rm, id, ResourceEntry::NOT_FOUND);
  104. const u32 old_refs = entry.references;
  105. unload(type, name);
  106. load(type, name);
  107. flush();
  108. ResourceEntry& new_entry = hash_map::get(_rm, id, ResourceEntry::NOT_FOUND);
  109. new_entry.references = old_refs;
  110. }
  111. bool ResourceManager::can_get(StringId64 type, StringId64 name)
  112. {
  113. const ResourcePair id = { type, name };
  114. return _autoload ? true : hash_map::has(_rm, id);
  115. }
  116. const void* ResourceManager::get(StringId64 type, StringId64 name)
  117. {
  118. const ResourcePair id = { type, name };
  119. const ResourceId res_id = resource_id(type, name);
  120. CE_ASSERT(can_get(type, name), "Resource not loaded: " RESOURCE_ID_FMT, res_id._id);
  121. if (_autoload && !hash_map::has(_rm, id))
  122. {
  123. load(type, name);
  124. flush();
  125. }
  126. const ResourceEntry& entry = hash_map::get(_rm, id, ResourceEntry::NOT_FOUND);
  127. return entry.data;
  128. }
  129. void ResourceManager::enable_autoload(bool enable)
  130. {
  131. _autoload = enable;
  132. }
  133. void ResourceManager::flush()
  134. {
  135. _loader->flush();
  136. complete_requests();
  137. }
  138. void ResourceManager::complete_requests()
  139. {
  140. TempAllocator1024 ta;
  141. Array<ResourceRequest> loaded(ta);
  142. _loader->get_loaded(loaded);
  143. for (u32 i = 0; i < array::size(loaded); ++i)
  144. complete_request(loaded[i].type, loaded[i].name, loaded[i].data);
  145. }
  146. void ResourceManager::complete_request(StringId64 type, StringId64 name, void* data)
  147. {
  148. ResourceEntry entry;
  149. entry.references = 1;
  150. entry.data = data;
  151. ResourcePair id = { type, name };
  152. hash_map::set(_rm, id, entry);
  153. on_online(type, name);
  154. }
  155. void ResourceManager::register_type(StringId64 type, u32 version, LoadFunction load, UnloadFunction unload, OnlineFunction online, OfflineFunction offline)
  156. {
  157. ResourceTypeData rtd;
  158. rtd.version = version;
  159. rtd.load = load;
  160. rtd.online = online;
  161. rtd.offline = offline;
  162. rtd.unload = unload;
  163. hash_map::set(_type_data, type, rtd);
  164. }
  165. void ResourceManager::on_online(StringId64 type, StringId64 name)
  166. {
  167. OnlineFunction func = hash_map::get(_type_data, type, ResourceTypeData()).online;
  168. if (func)
  169. func(name, *this);
  170. }
  171. void ResourceManager::on_offline(StringId64 type, StringId64 name)
  172. {
  173. OfflineFunction func = hash_map::get(_type_data, type, ResourceTypeData()).offline;
  174. if (func)
  175. func(name, *this);
  176. }
  177. void ResourceManager::on_unload(StringId64 type, void* data)
  178. {
  179. UnloadFunction func = hash_map::get(_type_data, type, ResourceTypeData()).unload;
  180. if (func)
  181. func(_resource_heap, data);
  182. else
  183. _resource_heap.deallocate(data);
  184. }
  185. } // namespace crown