resource_manager.cpp 3.2 KB

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