resource_saver.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*************************************************************************/
  2. /* resource_saver.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "resource_saver.h"
  30. #include "globals.h"
  31. #include "os/file_access.h"
  32. #include "script_language.h"
  33. #include "resource_loader.h"
  34. ResourceFormatSaver *ResourceSaver::saver[MAX_SAVERS];
  35. int ResourceSaver::saver_count=0;
  36. bool ResourceSaver::timestamp_on_save=false;
  37. ResourceSavedCallback ResourceSaver::save_callback=0;
  38. Error ResourceSaver::save(const String &p_path,const RES& p_resource,uint32_t p_flags) {
  39. String extension=p_path.extension();
  40. Error err=ERR_FILE_UNRECOGNIZED;
  41. for (int i=0;i<saver_count;i++) {
  42. if (!saver[i]->recognize(p_resource))
  43. continue;
  44. List<String> extensions;
  45. bool recognized=false;
  46. saver[i]->get_recognized_extensions(p_resource,&extensions);
  47. for (List<String>::Element *E=extensions.front();E;E=E->next()) {
  48. if (E->get().nocasecmp_to(extension.extension())==0)
  49. recognized=true;
  50. }
  51. if (!recognized)
  52. continue;
  53. String old_path=p_resource->get_path();
  54. String local_path=Globals::get_singleton()->localize_path(p_path);
  55. RES rwcopy = p_resource;
  56. if (p_flags&FLAG_CHANGE_PATH)
  57. rwcopy->set_path(local_path);
  58. err = saver[i]->save(p_path,p_resource,p_flags);
  59. if (err == OK ) {
  60. #ifdef TOOLS_ENABLED
  61. ((Resource*)p_resource.ptr())->set_edited(false);
  62. if (timestamp_on_save) {
  63. uint64_t mt = FileAccess::get_modified_time(p_path);
  64. ((Resource*)p_resource.ptr())->set_last_modified_time(mt);
  65. }
  66. #endif
  67. if (p_flags&FLAG_CHANGE_PATH)
  68. rwcopy->set_path(old_path);
  69. if (save_callback && p_path.begins_with("res://"))
  70. save_callback(p_path);
  71. return OK;
  72. } else {
  73. }
  74. }
  75. return err;
  76. }
  77. void ResourceSaver::set_save_callback(ResourceSavedCallback p_callback) {
  78. save_callback=p_callback;
  79. }
  80. void ResourceSaver::get_recognized_extensions(const RES& p_resource,List<String> *p_extensions) {
  81. for (int i=0;i<saver_count;i++) {
  82. saver[i]->get_recognized_extensions(p_resource,p_extensions);
  83. }
  84. }
  85. void ResourceSaver::add_resource_format_saver(ResourceFormatSaver *p_format_saver) {
  86. ERR_FAIL_COND( saver_count >= MAX_SAVERS );
  87. saver[saver_count++]=p_format_saver;
  88. }