ResourceManager.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. namespace crown
  31. {
  32. //-----------------------------------------------------------------------------
  33. ResourceManager::ResourceManager(Bundle& bundle, uint32_t seed) :
  34. m_resource_heap("resource", default_allocator()),
  35. m_loader(bundle, m_resource_heap),
  36. m_seed(seed),
  37. m_resources(default_allocator())
  38. {
  39. m_loader.start();
  40. }
  41. //-----------------------------------------------------------------------------
  42. ResourceManager::~ResourceManager()
  43. {
  44. m_loader.stop();
  45. }
  46. //-----------------------------------------------------------------------------
  47. ResourceId ResourceManager::load(const char* type, const char* name)
  48. {
  49. uint32_t type_hash = hash::murmur2_32(type, string::strlen(type), 0);
  50. uint32_t name_hash = hash::murmur2_32(name, string::strlen(name), m_seed);
  51. return load(name_hash, type_hash);
  52. }
  53. //-----------------------------------------------------------------------------
  54. void ResourceManager::unload(ResourceId name)
  55. {
  56. CE_ASSERT(has(name), "Resource not loaded: %.8X%.8X", name.name, name.type);
  57. ResourceEntry& entry = m_resources[name.index];
  58. entry.references--;
  59. if (entry.references == 0 && entry.state == RS_LOADED)
  60. {
  61. resource_on_unload(name.type, m_resource_heap, entry.resource);
  62. entry.state = RS_UNLOADED;
  63. entry.resource = NULL;
  64. }
  65. }
  66. //-----------------------------------------------------------------------------
  67. bool ResourceManager::has(ResourceId name) const
  68. {
  69. if (m_resources.size() > name.index)
  70. {
  71. return (m_resources[name.index].id.name == name.name);
  72. }
  73. return false;
  74. }
  75. //-----------------------------------------------------------------------------
  76. const void* ResourceManager::data(ResourceId name) const
  77. {
  78. CE_ASSERT(has(name), "Resource not loaded: %.8X%.8X", name.name, name.type);
  79. return m_resources[name.index].resource;
  80. }
  81. //-----------------------------------------------------------------------------
  82. bool ResourceManager::is_loaded(ResourceId name) const
  83. {
  84. CE_ASSERT(has(name), "Resource not loaded: %.8X%.8X", name.name, name.type);
  85. return m_resources[name.index].state == RS_LOADED;
  86. }
  87. //-----------------------------------------------------------------------------
  88. uint32_t ResourceManager::references(ResourceId name) const
  89. {
  90. CE_ASSERT(has(name), "Resource not loaded: %.8X%.8X", name.name, name.type);
  91. return m_resources[name.index].references;
  92. }
  93. //-----------------------------------------------------------------------------
  94. void ResourceManager::flush()
  95. {
  96. while (m_loader.remaining() > 0) ;
  97. poll_resource_loader();
  98. }
  99. //-----------------------------------------------------------------------------
  100. uint32_t ResourceManager::seed() const
  101. {
  102. return m_seed;
  103. }
  104. //-----------------------------------------------------------------------------
  105. void ResourceManager::poll_resource_loader()
  106. {
  107. if (m_loader.num_loaded() != 0)
  108. {
  109. TempAllocator1024 alloc;
  110. List<LoadedResource> loaded(alloc);
  111. m_loader.get_loaded(loaded);
  112. for (uint32_t i = 0; i < loaded.size(); i++)
  113. {
  114. online(loaded[i].resource, loaded[i].data);
  115. }
  116. }
  117. }
  118. //-----------------------------------------------------------------------------
  119. ResourceId ResourceManager::load(uint32_t name, uint32_t type)
  120. {
  121. ResourceId id;
  122. id.name = name;
  123. id.type = type;
  124. // Search for an already existent resource
  125. ResourceEntry* entry = std::find(m_resources.begin(), m_resources.end(), id);
  126. // If resource not found, create a new one
  127. if (entry == m_resources.end())
  128. {
  129. id.index = m_resources.size();
  130. ResourceEntry entry;
  131. entry.id = id;
  132. entry.state = RS_UNLOADED;
  133. entry.references = 1;
  134. entry.resource = NULL;
  135. m_resources.push_back(entry);
  136. // Issue request to resource loader
  137. m_loader.load(id);
  138. return id;
  139. }
  140. // Else, increment its reference count
  141. entry->references++;
  142. return entry->id;
  143. }
  144. //-----------------------------------------------------------------------------
  145. void ResourceManager::online(ResourceId name, void* resource)
  146. {
  147. resource_on_online(name.type, resource);
  148. ResourceEntry& entry = m_resources[name.index];
  149. entry.resource = resource;
  150. entry.state = RS_LOADED;
  151. }
  152. } // namespace crown