resource_saver.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /**************************************************************************/
  2. /* resource_saver.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_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 Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags) {
  41. Error err = ERR_METHOD_NOT_FOUND;
  42. GDVIRTUAL_CALL(_save, p_resource, p_path, p_flags, err);
  43. return err;
  44. }
  45. Error ResourceFormatSaver::set_uid(const String &p_path, ResourceUID::ID p_uid) {
  46. Error err = ERR_FILE_UNRECOGNIZED;
  47. GDVIRTUAL_CALL(_set_uid, p_path, p_uid, err);
  48. return err;
  49. }
  50. bool ResourceFormatSaver::recognize(const Ref<Resource> &p_resource) const {
  51. bool success = false;
  52. GDVIRTUAL_CALL(_recognize, p_resource, success);
  53. return success;
  54. }
  55. void ResourceFormatSaver::get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const {
  56. PackedStringArray exts;
  57. if (GDVIRTUAL_CALL(_get_recognized_extensions, p_resource, exts)) {
  58. const String *r = exts.ptr();
  59. for (int i = 0; i < exts.size(); ++i) {
  60. p_extensions->push_back(r[i]);
  61. }
  62. }
  63. }
  64. bool ResourceFormatSaver::recognize_path(const Ref<Resource> &p_resource, const String &p_path) const {
  65. bool ret = false;
  66. if (GDVIRTUAL_CALL(_recognize_path, p_resource, p_path, ret)) {
  67. return ret;
  68. }
  69. String extension = p_path.get_extension();
  70. List<String> extensions;
  71. get_recognized_extensions(p_resource, &extensions);
  72. for (const String &E : extensions) {
  73. if (E.nocasecmp_to(extension) == 0) {
  74. return true;
  75. }
  76. }
  77. return false;
  78. }
  79. void ResourceFormatSaver::_bind_methods() {
  80. GDVIRTUAL_BIND(_save, "resource", "path", "flags");
  81. GDVIRTUAL_BIND(_set_uid, "path", "uid");
  82. GDVIRTUAL_BIND(_recognize, "resource");
  83. GDVIRTUAL_BIND(_get_recognized_extensions, "resource");
  84. GDVIRTUAL_BIND(_recognize_path, "resource", "path");
  85. }
  86. Error ResourceSaver::save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags) {
  87. String path = p_path;
  88. if (path.is_empty()) {
  89. path = p_resource->get_path();
  90. }
  91. ERR_FAIL_COND_V_MSG(path.is_empty(), ERR_INVALID_PARAMETER, "Can't save resource to empty path. Provide non-empty path or a Resource with non-empty resource_path.");
  92. String extension = path.get_extension();
  93. Error err = ERR_FILE_UNRECOGNIZED;
  94. for (int i = 0; i < saver_count; i++) {
  95. if (!saver[i]->recognize(p_resource)) {
  96. continue;
  97. }
  98. if (!saver[i]->recognize_path(p_resource, path)) {
  99. continue;
  100. }
  101. String old_path = p_resource->get_path();
  102. String local_path = ProjectSettings::get_singleton()->localize_path(path);
  103. Ref<Resource> rwcopy = p_resource;
  104. if (p_flags & FLAG_CHANGE_PATH) {
  105. rwcopy->set_path(local_path);
  106. }
  107. err = saver[i]->save(p_resource, path, p_flags);
  108. if (err == OK) {
  109. #ifdef TOOLS_ENABLED
  110. ((Resource *)p_resource.ptr())->set_edited(false);
  111. if (timestamp_on_save) {
  112. uint64_t mt = FileAccess::get_modified_time(path);
  113. ((Resource *)p_resource.ptr())->set_last_modified_time(mt);
  114. }
  115. #endif
  116. if (p_flags & FLAG_CHANGE_PATH) {
  117. rwcopy->set_path(old_path);
  118. }
  119. if (save_callback && path.begins_with("res://")) {
  120. save_callback(p_resource, path);
  121. }
  122. return OK;
  123. }
  124. }
  125. return err;
  126. }
  127. Error ResourceSaver::set_uid(const String &p_path, ResourceUID::ID p_uid) {
  128. String path = p_path;
  129. ERR_FAIL_COND_V_MSG(path.is_empty(), ERR_INVALID_PARAMETER, "Can't update UID to empty path. Provide non-empty path.");
  130. Error err = ERR_FILE_UNRECOGNIZED;
  131. for (int i = 0; i < saver_count; i++) {
  132. err = saver[i]->set_uid(path, p_uid);
  133. if (err == OK) {
  134. break;
  135. }
  136. }
  137. return err;
  138. }
  139. void ResourceSaver::set_save_callback(ResourceSavedCallback p_callback) {
  140. save_callback = p_callback;
  141. }
  142. void ResourceSaver::get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) {
  143. for (int i = 0; i < saver_count; i++) {
  144. saver[i]->get_recognized_extensions(p_resource, p_extensions);
  145. }
  146. }
  147. void ResourceSaver::add_resource_format_saver(Ref<ResourceFormatSaver> p_format_saver, bool p_at_front) {
  148. ERR_FAIL_COND_MSG(p_format_saver.is_null(), "It's not a reference to a valid ResourceFormatSaver object.");
  149. ERR_FAIL_COND(saver_count >= MAX_SAVERS);
  150. if (p_at_front) {
  151. for (int i = saver_count; i > 0; i--) {
  152. saver[i] = saver[i - 1];
  153. }
  154. saver[0] = p_format_saver;
  155. saver_count++;
  156. } else {
  157. saver[saver_count++] = p_format_saver;
  158. }
  159. }
  160. void ResourceSaver::remove_resource_format_saver(Ref<ResourceFormatSaver> p_format_saver) {
  161. ERR_FAIL_COND_MSG(p_format_saver.is_null(), "It's not a reference to a valid ResourceFormatSaver object.");
  162. // Find saver
  163. int i = 0;
  164. for (; i < saver_count; ++i) {
  165. if (saver[i] == p_format_saver) {
  166. break;
  167. }
  168. }
  169. ERR_FAIL_COND(i >= saver_count); // Not found
  170. // Shift next savers up
  171. for (; i < saver_count - 1; ++i) {
  172. saver[i] = saver[i + 1];
  173. }
  174. saver[saver_count - 1].unref();
  175. --saver_count;
  176. }
  177. Ref<ResourceFormatSaver> ResourceSaver::_find_custom_resource_format_saver(String path) {
  178. for (int i = 0; i < saver_count; ++i) {
  179. if (saver[i]->get_script_instance() && saver[i]->get_script_instance()->get_script()->get_path() == path) {
  180. return saver[i];
  181. }
  182. }
  183. return Ref<ResourceFormatSaver>();
  184. }
  185. bool ResourceSaver::add_custom_resource_format_saver(String script_path) {
  186. if (_find_custom_resource_format_saver(script_path).is_valid()) {
  187. return false;
  188. }
  189. Ref<Resource> res = ResourceLoader::load(script_path);
  190. ERR_FAIL_COND_V(res.is_null(), false);
  191. ERR_FAIL_COND_V(!res->is_class("Script"), false);
  192. Ref<Script> s = res;
  193. StringName ibt = s->get_instance_base_type();
  194. bool valid_type = ClassDB::is_parent_class(ibt, "ResourceFormatSaver");
  195. ERR_FAIL_COND_V_MSG(!valid_type, false, "Script does not inherit a CustomResourceSaver: " + script_path + ".");
  196. Object *obj = ClassDB::instantiate(ibt);
  197. ERR_FAIL_COND_V_MSG(obj == nullptr, false, "Cannot instance script as custom resource saver, expected 'ResourceFormatSaver' inheritance, got: " + String(ibt) + ".");
  198. Ref<ResourceFormatSaver> crl = Object::cast_to<ResourceFormatSaver>(obj);
  199. crl->set_script(s);
  200. ResourceSaver::add_resource_format_saver(crl);
  201. return true;
  202. }
  203. void ResourceSaver::add_custom_savers() {
  204. // Custom resource savers exploits global class names
  205. String custom_saver_base_class = ResourceFormatSaver::get_class_static();
  206. List<StringName> global_classes;
  207. ScriptServer::get_global_class_list(&global_classes);
  208. for (const StringName &class_name : global_classes) {
  209. StringName base_class = ScriptServer::get_global_class_native_base(class_name);
  210. if (base_class == custom_saver_base_class) {
  211. String path = ScriptServer::get_global_class_path(class_name);
  212. add_custom_resource_format_saver(path);
  213. }
  214. }
  215. }
  216. void ResourceSaver::remove_custom_savers() {
  217. Vector<Ref<ResourceFormatSaver>> custom_savers;
  218. for (int i = 0; i < saver_count; ++i) {
  219. if (saver[i]->get_script_instance()) {
  220. custom_savers.push_back(saver[i]);
  221. }
  222. }
  223. for (int i = 0; i < custom_savers.size(); ++i) {
  224. remove_resource_format_saver(custom_savers[i]);
  225. }
  226. }
  227. ResourceUID::ID ResourceSaver::get_resource_id_for_path(const String &p_path, bool p_generate) {
  228. if (save_get_id_for_path) {
  229. return save_get_id_for_path(p_path, p_generate);
  230. }
  231. return ResourceUID::INVALID_ID;
  232. }
  233. void ResourceSaver::set_get_resource_id_for_path(ResourceSaverGetResourceIDForPath p_callback) {
  234. save_get_id_for_path = p_callback;
  235. }