scene_format_text.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*************************************************************************/
  2. /* scene_format_text.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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. #ifndef SCENE_FORMAT_TEXT_H
  31. #define SCENE_FORMAT_TEXT_H
  32. #include "io/resource_loader.h"
  33. #include "io/resource_saver.h"
  34. #include "os/file_access.h"
  35. #include "scene/resources/packed_scene.h"
  36. #include "variant_parser.h"
  37. class ResourceInteractiveLoaderText : public ResourceInteractiveLoader {
  38. bool translation_remapped;
  39. String local_path;
  40. String res_path;
  41. String error_text;
  42. FileAccess *f;
  43. VariantParser::StreamFile stream;
  44. struct ExtResource {
  45. String path;
  46. String type;
  47. };
  48. bool is_scene;
  49. String res_type;
  50. bool ignore_resource_parsing;
  51. //Map<String,String> remaps;
  52. Map<int, ExtResource> ext_resources;
  53. int resources_total;
  54. int resource_current;
  55. String resource_type;
  56. VariantParser::Tag next_tag;
  57. mutable int lines;
  58. Map<String, String> remaps;
  59. //void _printerr();
  60. static Error _parse_sub_resources(void *p_self, VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str) { return reinterpret_cast<ResourceInteractiveLoaderText *>(p_self)->_parse_sub_resource(p_stream, r_res, line, r_err_str); }
  61. static Error _parse_ext_resources(void *p_self, VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str) { return reinterpret_cast<ResourceInteractiveLoaderText *>(p_self)->_parse_ext_resource(p_stream, r_res, line, r_err_str); }
  62. Error _parse_sub_resource(VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str);
  63. Error _parse_ext_resource(VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str);
  64. VariantParser::ResourceParser rp;
  65. Ref<PackedScene> packed_scene;
  66. friend class ResourceFormatLoaderText;
  67. List<RES> resource_cache;
  68. Error error;
  69. RES resource;
  70. public:
  71. virtual void set_local_path(const String &p_local_path);
  72. virtual Ref<Resource> get_resource();
  73. virtual Error poll();
  74. virtual int get_stage() const;
  75. virtual int get_stage_count() const;
  76. virtual void set_translation_remapped(bool p_remapped);
  77. void open(FileAccess *p_f, bool p_skip_first_tag = false);
  78. String recognize(FileAccess *p_f);
  79. void get_dependencies(FileAccess *p_f, List<String> *p_dependencies, bool p_add_types);
  80. Error rename_dependencies(FileAccess *p_f, const String &p_path, const Map<String, String> &p_map);
  81. ResourceInteractiveLoaderText();
  82. ~ResourceInteractiveLoaderText();
  83. };
  84. class ResourceFormatLoaderText : public ResourceFormatLoader {
  85. public:
  86. virtual Ref<ResourceInteractiveLoader> load_interactive(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
  87. virtual void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const;
  88. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  89. virtual bool handles_type(const String &p_type) const;
  90. virtual String get_resource_type(const String &p_path) const;
  91. virtual void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false);
  92. virtual Error rename_dependencies(const String &p_path, const Map<String, String> &p_map);
  93. };
  94. class ResourceFormatSaverTextInstance {
  95. String local_path;
  96. Ref<PackedScene> packed_scene;
  97. bool takeover_paths;
  98. bool relative_paths;
  99. bool bundle_resources;
  100. bool skip_editor;
  101. FileAccess *f;
  102. Set<RES> resource_set;
  103. List<RES> saved_resources;
  104. Map<RES, int> external_resources;
  105. Map<RES, int> internal_resources;
  106. void _find_resources(const Variant &p_variant, bool p_main = false);
  107. static String _write_resources(void *ud, const RES &p_resource);
  108. String _write_resource(const RES &res);
  109. public:
  110. Error save(const String &p_path, const RES &p_resource, uint32_t p_flags = 0);
  111. };
  112. class ResourceFormatSaverText : public ResourceFormatSaver {
  113. public:
  114. static ResourceFormatSaverText *singleton;
  115. virtual Error save(const String &p_path, const RES &p_resource, uint32_t p_flags = 0);
  116. virtual bool recognize(const RES &p_resource) const;
  117. virtual void get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const;
  118. ResourceFormatSaverText();
  119. };
  120. #endif // SCENE_FORMAT_TEXT_H