2
0

resource_saver.cpp 9.0 KB

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