ResourceManager.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. #include "Log.h"
  34. namespace crown
  35. {
  36. ResourceId::ResourceId(const char* type, const char* name)
  37. : type(string::murmur2_64(type, string::strlen(type), 0))
  38. , name(string::murmur2_64(name, string::strlen(name), 0))
  39. {
  40. }
  41. //-----------------------------------------------------------------------------
  42. ResourceManager::ResourceManager(Bundle& bundle)
  43. : m_resource_heap("resource", default_allocator())
  44. , m_loader(bundle, m_resource_heap)
  45. , m_resources(default_allocator())
  46. {
  47. }
  48. //-----------------------------------------------------------------------------
  49. void ResourceManager::load(ResourceId id)
  50. {
  51. // Search for an already existent resource
  52. ResourceEntry* entry = find(id);
  53. // If resource not found, post load request
  54. if (entry == NULL)
  55. {
  56. m_loader.load(id);
  57. return;
  58. }
  59. // Else, increment its reference count
  60. entry->references++;
  61. }
  62. //-----------------------------------------------------------------------------
  63. void ResourceManager::unload(ResourceId id, bool force)
  64. {
  65. CE_ASSERT(has(id), "Resource not loaded: %.16lx-%16lx", id.type, id.name);
  66. flush();
  67. ResourceEntry* entry = find(id);
  68. entry->references--;
  69. if (entry->references == 0 || force)
  70. {
  71. resource_on_offline(id.type, entry->resource);
  72. resource_on_unload(id.type, m_resource_heap, entry->resource);
  73. // Swap with last
  74. ResourceEntry temp = m_resources[array::size(m_resources) - 1];
  75. (*entry) = temp;
  76. array::pop_back(m_resources);
  77. }
  78. }
  79. //-----------------------------------------------------------------------------
  80. bool ResourceManager::has(ResourceId id) const
  81. {
  82. ResourceEntry* entry = find(id);
  83. return entry != NULL;
  84. }
  85. //-----------------------------------------------------------------------------
  86. const void* ResourceManager::get(const char* type, const char* name) const
  87. {
  88. ResourceEntry* entry = find(ResourceId(type, name));
  89. CE_ASSERT(entry != NULL, "Resource not loaded: type = '%s', name = '%s'", type, name);
  90. CE_ASSERT(entry->resource != NULL, "Resource not loaded: type = '%s', name = '%s'", type, name);
  91. return entry->resource;
  92. }
  93. //-----------------------------------------------------------------------------
  94. const void* ResourceManager::get(ResourceId id) const
  95. {
  96. CE_ASSERT(has(id), "Resource not loaded: %.16lx-%16lx", id.type, id.name);
  97. return find(id)->resource;
  98. }
  99. //-----------------------------------------------------------------------------
  100. bool ResourceManager::is_loaded(ResourceId id) const
  101. {
  102. CE_ASSERT(has(id), "Resource not loaded: %.16lx-%16lx", id.type, id.name);
  103. return find(id)->resource != NULL;
  104. }
  105. //-----------------------------------------------------------------------------
  106. uint32_t ResourceManager::references(ResourceId id) const
  107. {
  108. CE_ASSERT(has(id), "Resource not loaded: %.16lx-%16lx", id.type, id.name);
  109. return find(id)->references;
  110. }
  111. //-----------------------------------------------------------------------------
  112. void ResourceManager::flush()
  113. {
  114. m_loader.flush();
  115. complete_requests();
  116. }
  117. //-----------------------------------------------------------------------------
  118. ResourceEntry* ResourceManager::find(ResourceId id) const
  119. {
  120. const ResourceEntry* entry = std::find(array::begin(m_resources), array::end(m_resources), id);
  121. return entry != array::end(m_resources) ? const_cast<ResourceEntry*>(entry) : NULL;
  122. }
  123. //-----------------------------------------------------------------------------
  124. void ResourceManager::complete_requests()
  125. {
  126. TempAllocator1024 ta;
  127. Array<ResourceData> loaded(ta);
  128. m_loader.get_loaded(loaded);
  129. for (uint32_t i = 0; i < array::size(loaded); i++)
  130. complete_request(loaded[i].id, loaded[i].data);
  131. }
  132. //-----------------------------------------------------------------------------
  133. void ResourceManager::complete_request(ResourceId id, void* data)
  134. {
  135. resource_on_online(id.type, data);
  136. ResourceEntry entry;
  137. entry.id = id;
  138. entry.references = 1;
  139. entry.resource = data;
  140. array::push_back(m_resources, entry);
  141. }
  142. } // namespace crown