resource_preloader.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*************************************************************************/
  2. /* resource_preloader.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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. void ResourcePreloader::_set_resources(const Array &p_data) {
  32. resources.clear();
  33. ERR_FAIL_COND(p_data.size() != 2);
  34. DVector<String> names = p_data[0];
  35. Array resdata = p_data[1];
  36. ERR_FAIL_COND(names.size() != resdata.size());
  37. for (int i = 0; i < resdata.size(); i++) {
  38. String name = names[i];
  39. RES resource = resdata[i];
  40. ERR_CONTINUE(!resource.is_valid());
  41. resources[name] = resource;
  42. //add_resource(name,resource);
  43. }
  44. }
  45. Array ResourcePreloader::_get_resources() const {
  46. DVector<String> names;
  47. Array arr;
  48. arr.resize(resources.size());
  49. names.resize(resources.size());
  50. Set<String> sorted_names;
  51. for (Map<StringName, RES>::Element *E = resources.front(); E; E = E->next()) {
  52. sorted_names.insert(E->key());
  53. }
  54. int i = 0;
  55. for (Set<String>::Element *E = sorted_names.front(); E; E = E->next()) {
  56. names.set(i, E->get());
  57. arr[i] = resources[E->get()];
  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 RES &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. RES 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. RES ResourcePreloader::get_resource(const StringName &p_name) const {
  97. ERR_FAIL_COND_V(!resources.has(p_name), RES());
  98. return resources[p_name];
  99. }
  100. DVector<String> ResourcePreloader::_get_resource_list() const {
  101. DVector<String> res;
  102. res.resize(resources.size());
  103. int i = 0;
  104. for (Map<StringName, RES>::Element *E = resources.front(); E; E = E->next(), i++) {
  105. res.set(i, E->key());
  106. }
  107. return res;
  108. }
  109. void ResourcePreloader::get_resource_list(List<StringName> *p_list) {
  110. for (Map<StringName, RES>::Element *E = resources.front(); E; E = E->next()) {
  111. p_list->push_back(E->key());
  112. }
  113. }
  114. void ResourcePreloader::_bind_methods() {
  115. ObjectTypeDB::bind_method(_MD("_set_resources"), &ResourcePreloader::_set_resources);
  116. ObjectTypeDB::bind_method(_MD("_get_resources"), &ResourcePreloader::_get_resources);
  117. ObjectTypeDB::bind_method(_MD("add_resource", "name", "resource"), &ResourcePreloader::add_resource);
  118. ObjectTypeDB::bind_method(_MD("remove_resource", "name"), &ResourcePreloader::remove_resource);
  119. ObjectTypeDB::bind_method(_MD("rename_resource", "name", "newname"), &ResourcePreloader::rename_resource);
  120. ObjectTypeDB::bind_method(_MD("has_resource", "name"), &ResourcePreloader::has_resource);
  121. ObjectTypeDB::bind_method(_MD("get_resource", "name"), &ResourcePreloader::get_resource);
  122. ObjectTypeDB::bind_method(_MD("get_resource_list"), &ResourcePreloader::_get_resource_list);
  123. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "resources", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), _SCS("_set_resources"), _SCS("_get_resources"));
  124. }
  125. ResourcePreloader::ResourcePreloader() {
  126. }