resource_manager.cpp 3.2 KB

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