resource_preloader.cpp 5.6 KB

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