resource_saver.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*************************************************************************/
  2. /* resource_saver.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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/io/resource_loader.h"
  32. #include "core/os/file_access.h"
  33. #include "core/project_settings.h"
  34. #include "core/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 = 0;
  39. Error ResourceFormatSaver::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  40. if (get_script_instance() && get_script_instance()->has_method("save")) {
  41. return (Error)get_script_instance()->call("save", p_path, p_resource, p_flags).operator int64_t();
  42. }
  43. return ERR_METHOD_NOT_FOUND;
  44. }
  45. bool ResourceFormatSaver::recognize(const RES &p_resource) const {
  46. if (get_script_instance() && get_script_instance()->has_method("recognize")) {
  47. return get_script_instance()->call("recognize", p_resource);
  48. }
  49. return false;
  50. }
  51. void ResourceFormatSaver::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const {
  52. if (get_script_instance() && get_script_instance()->has_method("get_recognized_extensions")) {
  53. PoolStringArray exts = get_script_instance()->call("get_recognized_extensions", p_resource);
  54. {
  55. PoolStringArray::Read r = exts.read();
  56. for (int i = 0; i < exts.size(); ++i) {
  57. p_extensions->push_back(r[i]);
  58. }
  59. }
  60. }
  61. }
  62. void ResourceFormatSaver::_bind_methods() {
  63. {
  64. PropertyInfo arg0 = PropertyInfo(Variant::STRING, "path");
  65. PropertyInfo arg1 = PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource");
  66. PropertyInfo arg2 = PropertyInfo(Variant::INT, "flags");
  67. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::INT, "save", arg0, arg1, arg2));
  68. }
  69. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::POOL_STRING_ARRAY, "get_recognized_extensions", PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource")));
  70. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::BOOL, "recognize", PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource")));
  71. }
  72. Error ResourceSaver::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  73. String extension = p_path.get_extension();
  74. Error err = ERR_FILE_UNRECOGNIZED;
  75. for (int i = 0; i < saver_count; i++) {
  76. if (!saver[i]->recognize(p_resource))
  77. continue;
  78. List<String> extensions;
  79. bool recognized = false;
  80. saver[i]->get_recognized_extensions(p_resource, &extensions);
  81. for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
  82. if (E->get().nocasecmp_to(extension) == 0)
  83. recognized = true;
  84. }
  85. if (!recognized)
  86. continue;
  87. String old_path = p_resource->get_path();
  88. String local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  89. RES rwcopy = p_resource;
  90. if (p_flags & FLAG_CHANGE_PATH)
  91. rwcopy->set_path(local_path);
  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. if (save_callback && p_path.begins_with("res://"))
  104. save_callback(p_resource, p_path);
  105. return OK;
  106. } else {
  107. }
  108. }
  109. return err;
  110. }
  111. void ResourceSaver::set_save_callback(ResourceSavedCallback p_callback) {
  112. save_callback = p_callback;
  113. }
  114. void ResourceSaver::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) {
  115. for (int i = 0; i < saver_count; i++) {
  116. saver[i]->get_recognized_extensions(p_resource, p_extensions);
  117. }
  118. }
  119. void ResourceSaver::add_resource_format_saver(Ref<ResourceFormatSaver> p_format_saver, bool p_at_front) {
  120. ERR_FAIL_COND(p_format_saver.is_null());
  121. ERR_FAIL_COND(saver_count >= MAX_SAVERS);
  122. if (p_at_front) {
  123. for (int i = saver_count; i > 0; i--) {
  124. saver[i] = saver[i - 1];
  125. }
  126. saver[0] = p_format_saver;
  127. saver_count++;
  128. } else {
  129. saver[saver_count++] = p_format_saver;
  130. }
  131. }
  132. void ResourceSaver::remove_resource_format_saver(Ref<ResourceFormatSaver> p_format_saver) {
  133. ERR_FAIL_COND(p_format_saver.is_null());
  134. // Find saver
  135. int i = 0;
  136. for (; i < saver_count; ++i) {
  137. if (saver[i] == p_format_saver)
  138. break;
  139. }
  140. ERR_FAIL_COND(i >= saver_count); // Not found
  141. // Shift next savers up
  142. for (; i < saver_count - 1; ++i) {
  143. saver[i] = saver[i + 1];
  144. }
  145. saver[saver_count - 1].unref();
  146. --saver_count;
  147. }
  148. Ref<ResourceFormatSaver> ResourceSaver::_find_custom_resource_format_saver(String path) {
  149. for (int i = 0; i < saver_count; ++i) {
  150. if (saver[i]->get_script_instance() && saver[i]->get_script_instance()->get_script()->get_path() == path) {
  151. return saver[i];
  152. }
  153. }
  154. return Ref<ResourceFormatSaver>();
  155. }
  156. bool ResourceSaver::add_custom_resource_format_saver(String script_path) {
  157. if (_find_custom_resource_format_saver(script_path).is_valid())
  158. return false;
  159. Ref<Resource> res = ResourceLoader::load(script_path);
  160. ERR_FAIL_COND_V(res.is_null(), false);
  161. ERR_FAIL_COND_V(!res->is_class("Script"), false);
  162. Ref<Script> s = res;
  163. StringName ibt = s->get_instance_base_type();
  164. bool valid_type = ClassDB::is_parent_class(ibt, "ResourceFormatSaver");
  165. ERR_EXPLAIN("Script does not inherit a CustomResourceSaver: " + script_path);
  166. ERR_FAIL_COND_V(!valid_type, false);
  167. Object *obj = ClassDB::instance(ibt);
  168. ERR_EXPLAIN("Cannot instance script as custom resource saver, expected 'ResourceFormatSaver' inheritance, got: " + String(ibt));
  169. ERR_FAIL_COND_V(obj == NULL, false);
  170. ResourceFormatSaver *crl = NULL;
  171. crl = Object::cast_to<ResourceFormatSaver>(obj);
  172. crl->set_script(s.get_ref_ptr());
  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> saver = _find_custom_resource_format_saver(script_path);
  178. if (saver.is_valid())
  179. remove_resource_format_saver(saver);
  180. }
  181. void ResourceSaver::add_custom_savers() {
  182. // Custom resource savers exploits global class names
  183. String custom_saver_base_class = ResourceFormatSaver::get_class_static();
  184. List<StringName> global_classes;
  185. ScriptServer::get_global_class_list(&global_classes);
  186. for (List<StringName>::Element *E = global_classes.front(); E; E = E->next()) {
  187. StringName class_name = E->get();
  188. StringName base_class = ScriptServer::get_global_class_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. }