resource_manager.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. char buf[64];
  87. CE_ASSERT(can_get(type, name), "Resource not loaded #ID(%s)", id.to_string(buf));
  88. CE_UNUSED(buf);
  89. if (_autoload && !sort_map::has(_rm, id))
  90. {
  91. load(type, name);
  92. flush();
  93. }
  94. const ResourceEntry& entry = sort_map::get(_rm, id, NOT_FOUND);
  95. return entry.data;
  96. }
  97. void ResourceManager::enable_autoload(bool enable)
  98. {
  99. _autoload = enable;
  100. }
  101. void ResourceManager::flush()
  102. {
  103. _loader.flush();
  104. complete_requests();
  105. }
  106. void ResourceManager::complete_requests()
  107. {
  108. TempAllocator1024 ta;
  109. Array<ResourceData> loaded(ta);
  110. _loader.get_loaded(loaded);
  111. for (uint32_t i = 0; i < array::size(loaded); i++)
  112. complete_request(loaded[i].id, loaded[i].data);
  113. }
  114. void ResourceManager::complete_request(ResourceId id, void* data)
  115. {
  116. ResourceEntry entry;
  117. entry.references = 1;
  118. entry.data = data;
  119. sort_map::set(_rm, id, entry);
  120. sort_map::sort(_rm);
  121. resource_on_online(id.type, id.name, *this);
  122. }
  123. } // namespace crown