resource_saver.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*************************************************************************/
  2. /* resource_saver.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "globals.h"
  32. #include "os/file_access.h"
  33. #include "resource_loader.h"
  34. #include "script_language.h"
  35. 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 ResourceSaver::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  40. String extension = p_path.extension();
  41. Error err = ERR_FILE_UNRECOGNIZED;
  42. for (int i = 0; i < saver_count; i++) {
  43. if (!saver[i]->recognize(p_resource))
  44. continue;
  45. List<String> extensions;
  46. bool recognized = false;
  47. saver[i]->get_recognized_extensions(p_resource, &extensions);
  48. for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
  49. if (E->get().nocasecmp_to(extension.extension()) == 0)
  50. recognized = true;
  51. }
  52. if (!recognized)
  53. continue;
  54. String old_path = p_resource->get_path();
  55. String local_path = Globals::get_singleton()->localize_path(p_path);
  56. RES rwcopy = p_resource;
  57. if (p_flags & FLAG_CHANGE_PATH)
  58. rwcopy->set_path(local_path);
  59. err = saver[i]->save(p_path, p_resource, p_flags);
  60. if (err == OK) {
  61. #ifdef TOOLS_ENABLED
  62. ((Resource *)p_resource.ptr())->set_edited(false);
  63. if (timestamp_on_save) {
  64. uint64_t mt = FileAccess::get_modified_time(p_path);
  65. ((Resource *)p_resource.ptr())->set_last_modified_time(mt);
  66. }
  67. #endif
  68. if (p_flags & FLAG_CHANGE_PATH)
  69. rwcopy->set_path(old_path);
  70. if (save_callback && p_path.begins_with("res://"))
  71. save_callback(p_path);
  72. return OK;
  73. } else {
  74. }
  75. }
  76. return err;
  77. }
  78. void ResourceSaver::set_save_callback(ResourceSavedCallback p_callback) {
  79. save_callback = p_callback;
  80. }
  81. void ResourceSaver::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) {
  82. for (int i = 0; i < saver_count; i++) {
  83. saver[i]->get_recognized_extensions(p_resource, p_extensions);
  84. }
  85. }
  86. void ResourceSaver::add_resource_format_saver(ResourceFormatSaver *p_format_saver, bool p_at_front) {
  87. ERR_FAIL_COND(saver_count >= MAX_SAVERS);
  88. if (p_at_front) {
  89. for (int i = saver_count; i > 0; i--) {
  90. saver[i] = saver[i - 1];
  91. }
  92. saver[0] = p_format_saver;
  93. saver_count++;
  94. } else {
  95. saver[saver_count++] = p_format_saver;
  96. }
  97. }