resource_preloader.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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_valid());
  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;
  61. res.push_back(names);
  62. res.push_back(arr);
  63. return res;
  64. }
  65. void ResourcePreloader::add_resource(const StringName &p_name, const Ref<Resource> &p_resource) {
  66. ERR_FAIL_COND(p_resource.is_null());
  67. if (resources.has(p_name)) {
  68. StringName new_name;
  69. int idx = 2;
  70. while (true) {
  71. new_name = p_name.operator String() + " " + itos(idx);
  72. if (resources.has(new_name)) {
  73. idx++;
  74. continue;
  75. }
  76. break;
  77. }
  78. add_resource(new_name, p_resource);
  79. } else {
  80. resources[p_name] = p_resource;
  81. }
  82. }
  83. void ResourcePreloader::remove_resource(const StringName &p_name) {
  84. ERR_FAIL_COND(!resources.has(p_name));
  85. resources.erase(p_name);
  86. }
  87. void ResourcePreloader::rename_resource(const StringName &p_from_name, const StringName &p_to_name) {
  88. ERR_FAIL_COND(!resources.has(p_from_name));
  89. Ref<Resource> res = resources[p_from_name];
  90. resources.erase(p_from_name);
  91. add_resource(p_to_name, res);
  92. }
  93. bool ResourcePreloader::has_resource(const StringName &p_name) const {
  94. return resources.has(p_name);
  95. }
  96. Ref<Resource> ResourcePreloader::get_resource(const StringName &p_name) const {
  97. ERR_FAIL_COND_V(!resources.has(p_name), Ref<Resource>());
  98. return resources[p_name];
  99. }
  100. Vector<String> ResourcePreloader::_get_resource_list() const {
  101. Vector<String> res;
  102. res.resize(resources.size());
  103. int i = 0;
  104. for (const KeyValue<StringName, Ref<Resource>> &E : resources) {
  105. res.set(i, E.key);
  106. i++;
  107. }
  108. return res;
  109. }
  110. void ResourcePreloader::get_resource_list(List<StringName> *p_list) {
  111. for (const KeyValue<StringName, Ref<Resource>> &E : resources) {
  112. p_list->push_back(E.key);
  113. }
  114. }
  115. void ResourcePreloader::_bind_methods() {
  116. ClassDB::bind_method(D_METHOD("_set_resources", "resources"), &ResourcePreloader::_set_resources);
  117. ClassDB::bind_method(D_METHOD("_get_resources"), &ResourcePreloader::_get_resources);
  118. ClassDB::bind_method(D_METHOD("add_resource", "name", "resource"), &ResourcePreloader::add_resource);
  119. ClassDB::bind_method(D_METHOD("remove_resource", "name"), &ResourcePreloader::remove_resource);
  120. ClassDB::bind_method(D_METHOD("rename_resource", "name", "newname"), &ResourcePreloader::rename_resource);
  121. ClassDB::bind_method(D_METHOD("has_resource", "name"), &ResourcePreloader::has_resource);
  122. ClassDB::bind_method(D_METHOD("get_resource", "name"), &ResourcePreloader::get_resource);
  123. ClassDB::bind_method(D_METHOD("get_resource_list"), &ResourcePreloader::_get_resource_list);
  124. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "resources", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_resources", "_get_resources");
  125. }
  126. ResourcePreloader::ResourcePreloader() {
  127. }