ResourceManager.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 "Types.h"
  25. #include "ResourceManager.h"
  26. #include "ResourceRegistry.h"
  27. #include "StringUtils.h"
  28. #include "Hash.h"
  29. #include "TempAllocator.h"
  30. #include "DynamicString.h"
  31. namespace crown
  32. {
  33. //-----------------------------------------------------------------------------
  34. ResourceManager::ResourceManager(Bundle& bundle, uint32_t seed) :
  35. m_resource_heap("resource", default_allocator()),
  36. m_loader(bundle, m_resource_heap),
  37. m_seed(seed),
  38. m_pendings(default_allocator()),
  39. m_resources(default_allocator())
  40. {
  41. m_loader.start();
  42. }
  43. //-----------------------------------------------------------------------------
  44. ResourceManager::~ResourceManager()
  45. {
  46. m_loader.stop();
  47. }
  48. //-----------------------------------------------------------------------------
  49. ResourceId ResourceManager::load(const char* type, const char* name)
  50. {
  51. return load(hash::murmur2_32(type, string::strlen(type), 0), resource_id(type, name));
  52. }
  53. //-----------------------------------------------------------------------------
  54. void ResourceManager::unload(ResourceId name)
  55. {
  56. CE_ASSERT(has(name), "Resource not loaded: %.16llx", name.id);
  57. ResourceEntry* entry = find(name);
  58. entry->references--;
  59. if (entry->references == 0)
  60. {
  61. resource_on_unload(entry->type, m_resource_heap, entry->resource);
  62. entry->resource = NULL;
  63. }
  64. }
  65. //-----------------------------------------------------------------------------
  66. bool ResourceManager::has(ResourceId name) const
  67. {
  68. ResourceEntry* entry = find(name);
  69. return entry != NULL;
  70. }
  71. //-----------------------------------------------------------------------------
  72. const void* ResourceManager::data(ResourceId name) const
  73. {
  74. CE_ASSERT(has(name), "Resource not loaded: %.16llx", name.id);
  75. return find(name)->resource;
  76. }
  77. //-----------------------------------------------------------------------------
  78. bool ResourceManager::is_loaded(ResourceId name) const
  79. {
  80. CE_ASSERT(has(name), "Resource not loaded: %.16llx", name.id);
  81. return find(name)->resource != NULL;
  82. }
  83. //-----------------------------------------------------------------------------
  84. uint32_t ResourceManager::references(ResourceId name) const
  85. {
  86. CE_ASSERT(has(name), "Resource not loaded: %.16llx", name.id);
  87. return find(name)->references;
  88. }
  89. //-----------------------------------------------------------------------------
  90. void ResourceManager::flush()
  91. {
  92. while (!m_pendings.empty())
  93. {
  94. poll_resource_loader();
  95. }
  96. }
  97. //-----------------------------------------------------------------------------
  98. uint32_t ResourceManager::seed() const
  99. {
  100. return m_seed;
  101. }
  102. //-----------------------------------------------------------------------------
  103. ResourceId ResourceManager::resource_id(const char* type, const char* name) const
  104. {
  105. TempAllocator256 alloc;
  106. DynamicString res_name(alloc);
  107. res_name += name;
  108. res_name += '.';
  109. res_name += type;
  110. ResourceId res_id;
  111. res_id.id = hash::murmur2_64(res_name.c_str(), string::strlen(res_name.c_str()), m_seed);
  112. return res_id;
  113. }
  114. //-----------------------------------------------------------------------------
  115. ResourceEntry* ResourceManager::find(ResourceId id) const
  116. {
  117. const ResourceEntry* entry = std::find(m_resources.begin(), m_resources.end(), id);
  118. return entry != m_resources.end() ? const_cast<ResourceEntry*>(entry) : NULL;
  119. }
  120. //-----------------------------------------------------------------------------
  121. void ResourceManager::poll_resource_loader()
  122. {
  123. if (!m_pendings.empty())
  124. {
  125. PendingRequest request = m_pendings.front();
  126. if (m_loader.load_resource_status(request.id) == LRS_LOADED)
  127. {
  128. m_pendings.pop_front();
  129. void* data = m_loader.load_resource_data(request.id);
  130. online(request.resource, data);
  131. }
  132. }
  133. }
  134. //-----------------------------------------------------------------------------
  135. ResourceId ResourceManager::load(uint32_t type, ResourceId name)
  136. {
  137. // Search for an already existent resource
  138. ResourceEntry* entry = find(name);
  139. // If resource not found, create a new one
  140. if (entry == NULL)
  141. {
  142. ResourceEntry entry;
  143. entry.id = name;
  144. entry.type = type;
  145. entry.references = 1;
  146. entry.resource = NULL;
  147. m_resources.push_back(entry);
  148. // Issue request to resource loader
  149. PendingRequest pr;
  150. pr.resource = name;
  151. pr.id = m_loader.load_resource(type, name);
  152. m_pendings.push_back(pr);
  153. return name;
  154. }
  155. // Else, increment its reference count
  156. entry->references++;
  157. return entry->id;
  158. }
  159. //-----------------------------------------------------------------------------
  160. void ResourceManager::online(ResourceId name, void* resource)
  161. {
  162. ResourceEntry* entry = find(name);
  163. resource_on_online(entry->type, resource);
  164. entry->resource = resource;
  165. }
  166. } // namespace crown