resource_saver.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*************************************************************************/
  2. /* resource_saver.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_saver.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/io/file_access.h"
  33. #include "core/io/resource_loader.h"
  34. #include "core/object/script_language.h"
  35. Ref<ResourceFormatSaver> ResourceSaver::saver[MAX_SAVERS];
  36. int ResourceSaver::saver_count = 0;
  37. bool ResourceSaver::timestamp_on_save = false;
  38. ResourceSavedCallback ResourceSaver::save_callback = nullptr;
  39. ResourceSaverGetResourceIDForPath ResourceSaver::save_get_id_for_path = nullptr;
  40. Error ResourceFormatSaver::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  41. int64_t res;
  42. if (GDVIRTUAL_CALL(_save, p_path, p_resource, p_flags, res)) {
  43. return (Error)res;
  44. }
  45. return ERR_METHOD_NOT_FOUND;
  46. }
  47. bool ResourceFormatSaver::recognize(const RES &p_resource) const {
  48. bool success;
  49. if (GDVIRTUAL_CALL(_recognize, p_resource, success)) {
  50. return success;
  51. }
  52. return false;
  53. }
  54. void ResourceFormatSaver::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const {
  55. PackedStringArray exts;
  56. if (GDVIRTUAL_CALL(_get_recognized_extensions, p_resource, exts)) {
  57. const String *r = exts.ptr();
  58. for (int i = 0; i < exts.size(); ++i) {
  59. p_extensions->push_back(r[i]);
  60. }
  61. }
  62. }
  63. void ResourceFormatSaver::_bind_methods() {
  64. GDVIRTUAL_BIND(_save, "path", "resource", "flags");
  65. GDVIRTUAL_BIND(_recognize, "resource");
  66. GDVIRTUAL_BIND(_get_recognized_extensions, "resource");
  67. }
  68. Error ResourceSaver::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  69. String extension = p_path.get_extension();
  70. Error err = ERR_FILE_UNRECOGNIZED;
  71. for (int i = 0; i < saver_count; i++) {
  72. if (!saver[i]->recognize(p_resource)) {
  73. continue;
  74. }
  75. List<String> extensions;
  76. bool recognized = false;
  77. saver[i]->get_recognized_extensions(p_resource, &extensions);
  78. for (const String &E : extensions) {
  79. if (E.nocasecmp_to(extension) == 0) {
  80. recognized = true;
  81. }
  82. }
  83. if (!recognized) {
  84. continue;
  85. }
  86. String old_path = p_resource->get_path();
  87. String local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  88. RES rwcopy = p_resource;
  89. if (p_flags & FLAG_CHANGE_PATH) {
  90. rwcopy->set_path(local_path);
  91. }
  92. err = saver[i]->save(p_path, p_resource, p_flags);
  93. if (err == OK) {
  94. #ifdef TOOLS_ENABLED
  95. ((Resource *)p_resource.ptr())->set_edited(false);
  96. if (timestamp_on_save) {
  97. uint64_t mt = FileAccess::get_modified_time(p_path);
  98. ((Resource *)p_resource.ptr())->set_last_modified_time(mt);
  99. }
  100. #endif
  101. if (p_flags & FLAG_CHANGE_PATH) {
  102. rwcopy->set_path(old_path);
  103. }
  104. if (save_callback && p_path.begins_with("res://")) {
  105. save_callback(p_resource, p_path);
  106. }
  107. return OK;
  108. }
  109. }
  110. return err;
  111. }
  112. void ResourceSaver::set_save_callback(ResourceSavedCallback p_callback) {
  113. save_callback = p_callback;
  114. }
  115. void ResourceSaver::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) {
  116. for (int i = 0; i < saver_count; i++) {
  117. saver[i]->get_recognized_extensions(p_resource, p_extensions);
  118. }
  119. }
  120. void ResourceSaver::add_resource_format_saver(Ref<ResourceFormatSaver> p_format_saver, bool p_at_front) {
  121. ERR_FAIL_COND_MSG(p_format_saver.is_null(), "It's not a reference to a valid ResourceFormatSaver object.");
  122. ERR_FAIL_COND(saver_count >= MAX_SAVERS);
  123. if (p_at_front) {
  124. for (int i = saver_count; i > 0; i--) {
  125. saver[i] = saver[i - 1];
  126. }
  127. saver[0] = p_format_saver;
  128. saver_count++;
  129. } else {
  130. saver[saver_count++] = p_format_saver;
  131. }
  132. }
  133. void ResourceSaver::remove_resource_format_saver(Ref<ResourceFormatSaver> p_format_saver) {
  134. ERR_FAIL_COND_MSG(p_format_saver.is_null(), "It's not a reference to a valid ResourceFormatSaver object.");
  135. // Find saver
  136. int i = 0;
  137. for (; i < saver_count; ++i) {
  138. if (saver[i] == p_format_saver) {
  139. break;
  140. }
  141. }
  142. ERR_FAIL_COND(i >= saver_count); // Not found
  143. // Shift next savers up
  144. for (; i < saver_count - 1; ++i) {
  145. saver[i] = saver[i + 1];
  146. }
  147. saver[saver_count - 1].unref();
  148. --saver_count;
  149. }
  150. Ref<ResourceFormatSaver> ResourceSaver::_find_custom_resource_format_saver(String path) {
  151. for (int i = 0; i < saver_count; ++i) {
  152. if (saver[i]->get_script_instance() && saver[i]->get_script_instance()->get_script()->get_path() == path) {
  153. return saver[i];
  154. }
  155. }
  156. return Ref<ResourceFormatSaver>();
  157. }
  158. bool ResourceSaver::add_custom_resource_format_saver(String script_path) {
  159. if (_find_custom_resource_format_saver(script_path).is_valid()) {
  160. return false;
  161. }
  162. Ref<Resource> res = ResourceLoader::load(script_path);
  163. ERR_FAIL_COND_V(res.is_null(), false);
  164. ERR_FAIL_COND_V(!res->is_class("Script"), false);
  165. Ref<Script> s = res;
  166. StringName ibt = s->get_instance_base_type();
  167. bool valid_type = ClassDB::is_parent_class(ibt, "ResourceFormatSaver");
  168. ERR_FAIL_COND_V_MSG(!valid_type, false, "Script does not inherit a CustomResourceSaver: " + script_path + ".");
  169. Object *obj = ClassDB::instantiate(ibt);
  170. ERR_FAIL_COND_V_MSG(obj == nullptr, false, "Cannot instance script as custom resource saver, expected 'ResourceFormatSaver' inheritance, got: " + String(ibt) + ".");
  171. Ref<ResourceFormatSaver> crl = Object::cast_to<ResourceFormatSaver>(obj);
  172. crl->set_script(s);
  173. ResourceSaver::add_resource_format_saver(crl);
  174. return true;
  175. }
  176. void ResourceSaver::remove_custom_resource_format_saver(String script_path) {
  177. Ref<ResourceFormatSaver> custom_saver = _find_custom_resource_format_saver(script_path);
  178. if (custom_saver.is_valid()) {
  179. remove_resource_format_saver(custom_saver);
  180. }
  181. }
  182. void ResourceSaver::add_custom_savers() {
  183. // Custom resource savers exploits global class names
  184. String custom_saver_base_class = ResourceFormatSaver::get_class_static();
  185. List<StringName> global_classes;
  186. ScriptServer::get_global_class_list(&global_classes);
  187. for (const StringName &class_name : global_classes) {
  188. StringName base_class = ScriptServer::get_global_class_native_base(class_name);
  189. if (base_class == custom_saver_base_class) {
  190. String path = ScriptServer::get_global_class_path(class_name);
  191. add_custom_resource_format_saver(path);
  192. }
  193. }
  194. }
  195. void ResourceSaver::remove_custom_savers() {
  196. Vector<Ref<ResourceFormatSaver>> custom_savers;
  197. for (int i = 0; i < saver_count; ++i) {
  198. if (saver[i]->get_script_instance()) {
  199. custom_savers.push_back(saver[i]);
  200. }
  201. }
  202. for (int i = 0; i < custom_savers.size(); ++i) {
  203. remove_resource_format_saver(custom_savers[i]);
  204. }
  205. }
  206. ResourceUID::ID ResourceSaver::get_resource_id_for_path(const String &p_path, bool p_generate) {
  207. if (save_get_id_for_path) {
  208. return save_get_id_for_path(p_path, p_generate);
  209. }
  210. return ResourceUID::INVALID_ID;
  211. }
  212. void ResourceSaver::set_get_resource_id_for_path(ResourceSaverGetResourceIDForPath p_callback) {
  213. save_get_id_for_path = p_callback;
  214. }