resource_manager.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 "resource_manager.h"
  27. #include "resource_registry.h"
  28. #include "string_utils.h"
  29. #include "string_utils.h"
  30. #include "temp_allocator.h"
  31. #include "dynamic_string.h"
  32. #include "queue.h"
  33. #include "sort_map.h"
  34. namespace crown
  35. {
  36. ResourceId::ResourceId(const char* type, const char* name)
  37. : type(murmur2_64(type, strlen(type), 0))
  38. , name(murmur2_64(name, strlen(name), 0))
  39. {
  40. }
  41. ResourceManager::ResourceManager(Bundle& bundle)
  42. : _resource_heap("resource", default_allocator())
  43. , _loader(bundle, _resource_heap)
  44. , _rm(default_allocator())
  45. , _autoload(false)
  46. {
  47. }
  48. const ResourceManager::ResourceEntry ResourceManager::NOT_FOUND = { 0xffffffffu, NULL };
  49. ResourceManager::~ResourceManager()
  50. {
  51. const ResourceMap::Entry* begin = sort_map::begin(_rm);
  52. const ResourceMap::Entry* end = sort_map::end(_rm);
  53. for (; begin != end; begin++)
  54. {
  55. resource_on_offline(begin->key.type, begin->key.name, *this);
  56. resource_on_unload(begin->key.type, _resource_heap, begin->value.data);
  57. }
  58. }
  59. void ResourceManager::load(StringId64 type, StringId64 name)
  60. {
  61. ResourceId id(type, name);
  62. ResourceEntry& entry = sort_map::get(_rm, id, NOT_FOUND);
  63. if (entry == NOT_FOUND)
  64. {
  65. _loader.load(id);
  66. return;
  67. }
  68. entry.references++;
  69. }
  70. void ResourceManager::unload(StringId64 type, StringId64 name)
  71. {
  72. flush();
  73. ResourceId id(type, name);
  74. ResourceEntry& entry = sort_map::get(_rm, id, NOT_FOUND);
  75. if (--entry.references == 0)
  76. {
  77. resource_on_offline(type, name, *this);
  78. resource_on_unload(type, _resource_heap, entry.data);
  79. sort_map::remove(_rm, id);
  80. sort_map::sort(_rm);
  81. }
  82. }
  83. void ResourceManager::reload(StringId64 type, StringId64 name)
  84. {
  85. const ResourceId id(type, name);
  86. const ResourceEntry& entry = sort_map::get(_rm, id, NOT_FOUND);
  87. const uint32_t old_refs = entry.references;
  88. unload(type, name);
  89. load(type, name);
  90. flush();
  91. ResourceEntry& new_entry = sort_map::get(_rm, id, NOT_FOUND);
  92. new_entry.references = old_refs;
  93. }
  94. bool ResourceManager::can_get(const char* type, const char* name)
  95. {
  96. ResourceId id(type, name);
  97. return can_get(id.type, id.name);
  98. }
  99. bool ResourceManager::can_get(StringId64 type, StringId64 name)
  100. {
  101. return _autoload ? true : sort_map::has(_rm, ResourceId(type, name));
  102. }
  103. const void* ResourceManager::get(const char* type, const char* name)
  104. {
  105. ResourceId id(type, name);
  106. return get(id.type, id.name);
  107. }
  108. const void* ResourceManager::get(StringId64 type, StringId64 name)
  109. {
  110. ResourceId id(type, name);
  111. if (_autoload && !sort_map::has(_rm, id))
  112. {
  113. load(type, name);
  114. flush();
  115. }
  116. const ResourceEntry& entry = sort_map::get(_rm, id, NOT_FOUND);
  117. return entry.data;
  118. }
  119. void ResourceManager::enable_autoload(bool enable)
  120. {
  121. _autoload = enable;
  122. }
  123. void ResourceManager::flush()
  124. {
  125. _loader.flush();
  126. complete_requests();
  127. }
  128. void ResourceManager::complete_requests()
  129. {
  130. TempAllocator1024 ta;
  131. Array<ResourceData> loaded(ta);
  132. _loader.get_loaded(loaded);
  133. for (uint32_t i = 0; i < array::size(loaded); i++)
  134. complete_request(loaded[i].id, loaded[i].data);
  135. }
  136. void ResourceManager::complete_request(ResourceId id, void* data)
  137. {
  138. ResourceEntry entry;
  139. entry.references = 1;
  140. entry.data = data;
  141. sort_map::set(_rm, id, entry);
  142. sort_map::sort(_rm);
  143. resource_on_online(id.type, id.name, *this);
  144. }
  145. } // namespace crown