resource_preloader.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**************************************************************************/
  2. /* resource_preloader.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "resource_preloader.h"
  31. #include "core/templates/rb_set.h"
  32. void ResourcePreloader::_set_resources(const Array &p_data) {
  33. resources.clear();
  34. ERR_FAIL_COND(p_data.size() != 2);
  35. Vector<String> names = p_data[0];
  36. Array resdata = p_data[1];
  37. ERR_FAIL_COND(names.size() != resdata.size());
  38. for (int i = 0; i < resdata.size(); i++) {
  39. Ref<Resource> resource = resdata[i];
  40. ERR_CONTINUE(resource.is_null());
  41. resources[names[i]] = resource;
  42. //add_resource(names[i],resource);
  43. }
  44. }
  45. Array ResourcePreloader::_get_resources() const {
  46. Vector<String> names;
  47. Array arr;
  48. arr.resize(resources.size());
  49. names.resize(resources.size());
  50. RBSet<String> sorted_names;
  51. for (const KeyValue<StringName, Ref<Resource>> &E : resources) {
  52. sorted_names.insert(E.key);
  53. }
  54. int i = 0;
  55. for (const String &E : sorted_names) {
  56. names.set(i, E);
  57. arr[i] = resources[E];
  58. i++;
  59. }
  60. Array res = { names, arr };
  61. return res;
  62. }
  63. void ResourcePreloader::add_resource(const StringName &p_name, const Ref<Resource> &p_resource) {
  64. ERR_FAIL_COND(p_resource.is_null());
  65. if (resources.has(p_name)) {
  66. StringName new_name;
  67. int idx = 2;
  68. while (true) {
  69. new_name = p_name.operator String() + " " + itos(idx);
  70. if (resources.has(new_name)) {
  71. idx++;
  72. continue;
  73. }
  74. break;
  75. }
  76. add_resource(new_name, p_resource);
  77. } else {
  78. resources[p_name] = p_resource;
  79. }
  80. }
  81. void ResourcePreloader::remove_resource(const StringName &p_name) {
  82. ERR_FAIL_COND(!resources.has(p_name));
  83. resources.erase(p_name);
  84. }
  85. void ResourcePreloader::rename_resource(const StringName &p_from_name, const StringName &p_to_name) {
  86. ERR_FAIL_COND(!resources.has(p_from_name));
  87. Ref<Resource> res = resources[p_from_name];
  88. resources.erase(p_from_name);
  89. add_resource(p_to_name, res);
  90. }
  91. bool ResourcePreloader::has_resource(const StringName &p_name) const {
  92. return resources.has(p_name);
  93. }
  94. Ref<Resource> ResourcePreloader::get_resource(const StringName &p_name) const {
  95. ERR_FAIL_COND_V(!resources.has(p_name), Ref<Resource>());
  96. return resources[p_name];
  97. }
  98. Vector<String> ResourcePreloader::_get_resource_list() const {
  99. Vector<String> res;
  100. res.resize(resources.size());
  101. int i = 0;
  102. for (const KeyValue<StringName, Ref<Resource>> &E : resources) {
  103. res.set(i, E.key);
  104. i++;
  105. }
  106. return res;
  107. }
  108. void ResourcePreloader::get_resource_list(List<StringName> *p_list) {
  109. for (const KeyValue<StringName, Ref<Resource>> &E : resources) {
  110. p_list->push_back(E.key);
  111. }
  112. }
  113. void ResourcePreloader::_bind_methods() {
  114. ClassDB::bind_method(D_METHOD("_set_resources", "resources"), &ResourcePreloader::_set_resources);
  115. ClassDB::bind_method(D_METHOD("_get_resources"), &ResourcePreloader::_get_resources);
  116. ClassDB::bind_method(D_METHOD("add_resource", "name", "resource"), &ResourcePreloader::add_resource);
  117. ClassDB::bind_method(D_METHOD("remove_resource", "name"), &ResourcePreloader::remove_resource);
  118. ClassDB::bind_method(D_METHOD("rename_resource", "name", "newname"), &ResourcePreloader::rename_resource);
  119. ClassDB::bind_method(D_METHOD("has_resource", "name"), &ResourcePreloader::has_resource);
  120. ClassDB::bind_method(D_METHOD("get_resource", "name"), &ResourcePreloader::get_resource);
  121. ClassDB::bind_method(D_METHOD("get_resource_list"), &ResourcePreloader::_get_resource_list);
  122. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "resources", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_resources", "_get_resources");
  123. }
  124. ResourcePreloader::ResourcePreloader() {
  125. }