resource_manager.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (c) 2012-2014 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include <algorithm>
  6. #include <inttypes.h>
  7. #include "types.h"
  8. #include "resource_manager.h"
  9. #include "resource_registry.h"
  10. #include "string_utils.h"
  11. #include "string_utils.h"
  12. #include "temp_allocator.h"
  13. #include "dynamic_string.h"
  14. #include "queue.h"
  15. #include "sort_map.h"
  16. namespace crown
  17. {
  18. ResourceManager::ResourceManager(Filesystem& fs)
  19. : _resource_heap("resource", default_allocator())
  20. , _loader(fs, _resource_heap)
  21. , _rm(default_allocator())
  22. , _autoload(false)
  23. {
  24. }
  25. const ResourceManager::ResourceEntry ResourceManager::NOT_FOUND = { 0xffffffffu, NULL };
  26. ResourceManager::~ResourceManager()
  27. {
  28. const ResourceMap::Entry* begin = sort_map::begin(_rm);
  29. const ResourceMap::Entry* end = sort_map::end(_rm);
  30. for (; begin != end; begin++)
  31. {
  32. resource_on_offline(begin->key.type, begin->key.name, *this);
  33. resource_on_unload(begin->key.type, _resource_heap, begin->value.data);
  34. }
  35. }
  36. void ResourceManager::load(StringId64 type, StringId64 name)
  37. {
  38. ResourceId id(type, name);
  39. ResourceEntry& entry = sort_map::get(_rm, id, NOT_FOUND);
  40. if (entry == NOT_FOUND)
  41. {
  42. _loader.load(id);
  43. return;
  44. }
  45. entry.references++;
  46. }
  47. void ResourceManager::unload(StringId64 type, StringId64 name)
  48. {
  49. flush();
  50. ResourceId id(type, name);
  51. ResourceEntry& entry = sort_map::get(_rm, id, NOT_FOUND);
  52. if (--entry.references == 0)
  53. {
  54. resource_on_offline(type, name, *this);
  55. resource_on_unload(type, _resource_heap, entry.data);
  56. sort_map::remove(_rm, id);
  57. sort_map::sort(_rm);
  58. }
  59. }
  60. void ResourceManager::reload(StringId64 type, StringId64 name)
  61. {
  62. const ResourceId id(type, name);
  63. const ResourceEntry& entry = sort_map::get(_rm, id, NOT_FOUND);
  64. const uint32_t old_refs = entry.references;
  65. unload(type, name);
  66. load(type, name);
  67. flush();
  68. ResourceEntry& new_entry = sort_map::get(_rm, id, NOT_FOUND);
  69. new_entry.references = old_refs;
  70. }
  71. bool ResourceManager::can_get(const char* type, const char* name)
  72. {
  73. ResourceId id(type, name);
  74. return can_get(id.type, id.name);
  75. }
  76. bool ResourceManager::can_get(StringId64 type, StringId64 name)
  77. {
  78. return _autoload ? true : sort_map::has(_rm, ResourceId(type, name));
  79. }
  80. const void* ResourceManager::get(const char* type, const char* name)
  81. {
  82. ResourceId id(type, name);
  83. return get(id.type, id.name);
  84. }
  85. const void* ResourceManager::get(StringId64 type, StringId64 name)
  86. {
  87. ResourceId id(type, name);
  88. if (_autoload && !sort_map::has(_rm, id))
  89. {
  90. load(type, name);
  91. flush();
  92. }
  93. const ResourceEntry& entry = sort_map::get(_rm, id, NOT_FOUND);
  94. return entry.data;
  95. }
  96. void ResourceManager::enable_autoload(bool enable)
  97. {
  98. _autoload = enable;
  99. }
  100. void ResourceManager::flush()
  101. {
  102. _loader.flush();
  103. complete_requests();
  104. }
  105. void ResourceManager::complete_requests()
  106. {
  107. TempAllocator1024 ta;
  108. Array<ResourceData> loaded(ta);
  109. _loader.get_loaded(loaded);
  110. for (uint32_t i = 0; i < array::size(loaded); i++)
  111. complete_request(loaded[i].id, loaded[i].data);
  112. }
  113. void ResourceManager::complete_request(ResourceId id, void* data)
  114. {
  115. ResourceEntry entry;
  116. entry.references = 1;
  117. entry.data = data;
  118. sort_map::set(_rm, id, entry);
  119. sort_map::sort(_rm);
  120. resource_on_online(id.type, id.name, *this);
  121. }
  122. } // namespace crown