resource_saver.cpp 9.8 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. ERR_FAIL_COND_V_MSG(p_resource.is_null(), ERR_INVALID_PARAMETER, "Can't save empty resource to path '" + p_path + "'.");
  88. String path = p_path;
  89. if (path.is_empty()) {
  90. path = p_resource->get_path();
  91. }
  92. 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.");
  93. String extension = path.get_extension();
  94. Error err = ERR_FILE_UNRECOGNIZED;
  95. for (int i = 0; i < saver_count; i++) {
  96. if (!saver[i]->recognize(p_resource)) {
  97. continue;
  98. }
  99. if (!saver[i]->recognize_path(p_resource, path)) {
  100. continue;
  101. }
  102. String old_path = p_resource->get_path();
  103. String local_path = ProjectSettings::get_singleton()->localize_path(path);
  104. if (p_flags & FLAG_CHANGE_PATH) {
  105. p_resource->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. p_resource->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. ERR_FAIL_COND_MSG(p_resource.is_null(), "It's not a reference to a valid Resource object.");
  144. for (int i = 0; i < saver_count; i++) {
  145. saver[i]->get_recognized_extensions(p_resource, p_extensions);
  146. }
  147. }
  148. void ResourceSaver::add_resource_format_saver(Ref<ResourceFormatSaver> p_format_saver, bool p_at_front) {
  149. ERR_FAIL_COND_MSG(p_format_saver.is_null(), "It's not a reference to a valid ResourceFormatSaver object.");
  150. ERR_FAIL_COND(saver_count >= MAX_SAVERS);
  151. if (p_at_front) {
  152. for (int i = saver_count; i > 0; i--) {
  153. saver[i] = saver[i - 1];
  154. }
  155. saver[0] = p_format_saver;
  156. saver_count++;
  157. } else {
  158. saver[saver_count++] = p_format_saver;
  159. }
  160. }
  161. void ResourceSaver::remove_resource_format_saver(Ref<ResourceFormatSaver> p_format_saver) {
  162. ERR_FAIL_COND_MSG(p_format_saver.is_null(), "It's not a reference to a valid ResourceFormatSaver object.");
  163. // Find saver
  164. int i = 0;
  165. for (; i < saver_count; ++i) {
  166. if (saver[i] == p_format_saver) {
  167. break;
  168. }
  169. }
  170. ERR_FAIL_COND(i >= saver_count); // Not found
  171. // Shift next savers up
  172. for (; i < saver_count - 1; ++i) {
  173. saver[i] = saver[i + 1];
  174. }
  175. saver[saver_count - 1].unref();
  176. --saver_count;
  177. }
  178. Ref<ResourceFormatSaver> ResourceSaver::_find_custom_resource_format_saver(const String &path) {
  179. for (int i = 0; i < saver_count; ++i) {
  180. if (saver[i]->get_script_instance() && saver[i]->get_script_instance()->get_script()->get_path() == path) {
  181. return saver[i];
  182. }
  183. }
  184. return Ref<ResourceFormatSaver>();
  185. }
  186. bool ResourceSaver::add_custom_resource_format_saver(const String &script_path) {
  187. if (_find_custom_resource_format_saver(script_path).is_valid()) {
  188. return false;
  189. }
  190. Ref<Resource> res = ResourceLoader::load(script_path);
  191. ERR_FAIL_COND_V(res.is_null(), false);
  192. ERR_FAIL_COND_V(!res->is_class("Script"), false);
  193. Ref<Script> s = res;
  194. StringName ibt = s->get_instance_base_type();
  195. bool valid_type = ClassDB::is_parent_class(ibt, "ResourceFormatSaver");
  196. ERR_FAIL_COND_V_MSG(!valid_type, false, vformat("Failed to add a custom resource saver, script '%s' does not inherit 'ResourceFormatSaver'.", script_path));
  197. Object *obj = ClassDB::instantiate(ibt);
  198. ERR_FAIL_NULL_V_MSG(obj, false, vformat("Failed to add a custom resource saver, cannot instantiate '%s'.", ibt));
  199. Ref<ResourceFormatSaver> crl = Object::cast_to<ResourceFormatSaver>(obj);
  200. crl->set_script(s);
  201. ResourceSaver::add_resource_format_saver(crl);
  202. return true;
  203. }
  204. void ResourceSaver::add_custom_savers() {
  205. // Custom resource savers exploits global class names
  206. String custom_saver_base_class = ResourceFormatSaver::get_class_static();
  207. List<StringName> global_classes;
  208. ScriptServer::get_global_class_list(&global_classes);
  209. for (const StringName &class_name : global_classes) {
  210. StringName base_class = ScriptServer::get_global_class_native_base(class_name);
  211. if (base_class == custom_saver_base_class) {
  212. String path = ScriptServer::get_global_class_path(class_name);
  213. add_custom_resource_format_saver(path);
  214. }
  215. }
  216. }
  217. void ResourceSaver::remove_custom_savers() {
  218. Vector<Ref<ResourceFormatSaver>> custom_savers;
  219. for (int i = 0; i < saver_count; ++i) {
  220. if (saver[i]->get_script_instance()) {
  221. custom_savers.push_back(saver[i]);
  222. }
  223. }
  224. for (int i = 0; i < custom_savers.size(); ++i) {
  225. remove_resource_format_saver(custom_savers[i]);
  226. }
  227. }
  228. ResourceUID::ID ResourceSaver::get_resource_id_for_path(const String &p_path, bool p_generate) {
  229. if (save_get_id_for_path) {
  230. return save_get_id_for_path(p_path, p_generate);
  231. }
  232. return ResourceUID::INVALID_ID;
  233. }
  234. void ResourceSaver::set_get_resource_id_for_path(ResourceSaverGetResourceIDForPath p_callback) {
  235. save_get_id_for_path = p_callback;
  236. }