ResourceManager.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <algorithm>
  24. #include <inttypes.h>
  25. #include "Types.h"
  26. #include "ResourceManager.h"
  27. #include "ResourceRegistry.h"
  28. #include "StringUtils.h"
  29. #include "StringUtils.h"
  30. #include "TempAllocator.h"
  31. #include "DynamicString.h"
  32. #include "Queue.h"
  33. namespace crown
  34. {
  35. ResourceId::ResourceId(const char* type, const char* name)
  36. {
  37. TempAllocator256 alloc;
  38. DynamicString res_name(alloc);
  39. res_name += name;
  40. res_name += '.';
  41. res_name += type;
  42. id = string::murmur2_64(res_name.c_str(), string::strlen(res_name.c_str()), 0);
  43. }
  44. ResourceId::ResourceId(const char* name)
  45. {
  46. id = string::murmur2_64(name, string::strlen(name), 0);
  47. }
  48. //-----------------------------------------------------------------------------
  49. ResourceManager::ResourceManager(Bundle& bundle, uint32_t seed) :
  50. m_resource_heap("resource", default_allocator()),
  51. m_loader(bundle, m_resource_heap),
  52. m_seed(seed),
  53. m_pendings(default_allocator()),
  54. m_resources(default_allocator())
  55. {
  56. m_loader.start();
  57. }
  58. //-----------------------------------------------------------------------------
  59. ResourceManager::~ResourceManager()
  60. {
  61. m_loader.stop();
  62. }
  63. //-----------------------------------------------------------------------------
  64. ResourceId ResourceManager::load(const char* type, const char* name)
  65. {
  66. return load(string::murmur2_32(type, string::strlen(type), 0), ResourceId(type, name));
  67. }
  68. //-----------------------------------------------------------------------------
  69. void ResourceManager::unload(ResourceId name, bool force)
  70. {
  71. CE_ASSERT(has(name), "Resource not loaded: " "%.16"PRIx64"", name.id);
  72. ResourceEntry* entry = find(name);
  73. entry->references--;
  74. if (entry->references == 0 || force)
  75. {
  76. resource_on_offline(entry->type, entry->resource);
  77. resource_on_unload(entry->type, m_resource_heap, entry->resource);
  78. hash::remove(m_resources, name.id);
  79. }
  80. }
  81. //-----------------------------------------------------------------------------
  82. bool ResourceManager::has(ResourceId name) const
  83. {
  84. ResourceEntry* entry = find(name);
  85. return entry != NULL;
  86. }
  87. //-----------------------------------------------------------------------------
  88. const void* ResourceManager::get(const char* type, const char* name) const
  89. {
  90. ResourceEntry* entry = find(ResourceId(type, name));
  91. CE_ASSERT(entry != NULL, "Resource not loaded: type = '%s', name = '%s'", type, name);
  92. CE_ASSERT(entry->resource != NULL, "Resource not loaded: type = '%s', name = '%s'", type, name);
  93. return entry->resource;
  94. }
  95. //-----------------------------------------------------------------------------
  96. const void* ResourceManager::get(ResourceId name) const
  97. {
  98. CE_ASSERT(has(name), "Resource not loaded: " "%.16"PRIx64"", name.id);
  99. return find(name)->resource;
  100. }
  101. //-----------------------------------------------------------------------------
  102. bool ResourceManager::is_loaded(ResourceId name) const
  103. {
  104. CE_ASSERT(has(name), "Resource not loaded: " "%.16"PRIx64"", name.id);
  105. return find(name)->resource != NULL;
  106. }
  107. //-----------------------------------------------------------------------------
  108. uint32_t ResourceManager::references(ResourceId name) const
  109. {
  110. CE_ASSERT(has(name), "Resource not loaded: " "%.16"PRIx64"", name.id);
  111. return find(name)->references;
  112. }
  113. //-----------------------------------------------------------------------------
  114. void ResourceManager::flush()
  115. {
  116. while (!queue::empty(m_pendings))
  117. {
  118. poll_resource_loader();
  119. }
  120. }
  121. //-----------------------------------------------------------------------------
  122. uint32_t ResourceManager::seed() const
  123. {
  124. return m_seed;
  125. }
  126. //-----------------------------------------------------------------------------
  127. ResourceEntry* ResourceManager::find(ResourceId id) const
  128. {
  129. ResourceEntry deff;
  130. deff.type = 0xFFFFFFFFu;
  131. deff.references = 0xFFFFFFFFu;
  132. deff.resource = NULL;
  133. const ResourceEntry& entry = hash::get(m_resources, id.id, deff);
  134. return memcmp(&deff, &entry, sizeof(ResourceEntry)) == 0 ? NULL : const_cast<ResourceEntry*>(&entry);
  135. }
  136. //-----------------------------------------------------------------------------
  137. void ResourceManager::poll_resource_loader()
  138. {
  139. if (!queue::empty(m_pendings))
  140. {
  141. PendingRequest request = queue::front(m_pendings);
  142. if (m_loader.load_resource_status(request.id) == LRS_LOADED)
  143. {
  144. queue::pop_front(m_pendings);
  145. void* data = m_loader.load_resource_data(request.id);
  146. online(request.resource, data);
  147. }
  148. }
  149. }
  150. //-----------------------------------------------------------------------------
  151. ResourceId ResourceManager::load(uint32_t type, ResourceId name)
  152. {
  153. // Search for an already existent resource
  154. ResourceEntry* entry = find(name);
  155. // If resource not found, create a new one
  156. if (entry == NULL)
  157. {
  158. ResourceEntry new_entry;
  159. new_entry.type = type;
  160. new_entry.references = 1;
  161. new_entry.resource = NULL;
  162. hash::set(m_resources, name.id, new_entry);
  163. // Issue request to resource loader
  164. PendingRequest pr;
  165. pr.resource = name;
  166. pr.id = m_loader.load_resource(type, name);
  167. queue::push_back(m_pendings, pr);
  168. return name;
  169. }
  170. // Else, increment its reference count
  171. entry->references++;
  172. return name;
  173. }
  174. //-----------------------------------------------------------------------------
  175. void ResourceManager::online(ResourceId name, void* resource)
  176. {
  177. ResourceEntry* entry = find(name);
  178. resource_on_online(entry->type, resource);
  179. entry->resource = resource;
  180. }
  181. } // namespace crown