scene_format_text.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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-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. #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. String local_path;
  39. String res_path;
  40. String error_text;
  41. FileAccess *f;
  42. VariantParser::StreamFile stream;
  43. struct ExtResource {
  44. String path;
  45. String type;
  46. };
  47. bool is_scene;
  48. String res_type;
  49. bool ignore_resource_parsing;
  50. // Map<String,String> remaps;
  51. Map<int, ExtResource> ext_resources;
  52. int resources_total;
  53. int resource_current;
  54. String resource_type;
  55. VariantParser::Tag next_tag;
  56. mutable int lines;
  57. Map<String, String> remaps;
  58. //void _printerr();
  59. static Error _parse_sub_resources(void *p_self, VariantParser::Stream *p_stream, Variant &r_value_res, int &line, String &r_err_str) { return reinterpret_cast<ResourceInteractiveLoaderText *>(p_self)->_parse_sub_resource(p_stream, r_value_res, line, r_err_str); }
  60. static Error _parse_ext_resources(void *p_self, VariantParser::Stream *p_stream, Variant &r_value_res, int &line, String &r_err_str) { return reinterpret_cast<ResourceInteractiveLoaderText *>(p_self)->_parse_ext_resource(p_stream, r_value_res, line, r_err_str); }
  61. Error _parse_sub_resource(VariantParser::Stream *p_stream, Variant &r_value_res, int &line, String &r_err_str);
  62. Error _parse_ext_resource(VariantParser::Stream *p_stream, Variant &r_value_res, int &line, String &r_err_str);
  63. VariantParser::ResourceParser rp;
  64. static Error _parse_sub_resources_export(void *p_self, VariantParser::Stream *p_stream, Variant &r_value_res, int &line, String &r_err_str) { return reinterpret_cast<ResourceInteractiveLoaderText *>(p_self)->_parse_sub_resource_export(p_stream, r_value_res, line, r_err_str); }
  65. static Error _parse_ext_resources_export(void *p_self, VariantParser::Stream *p_stream, Variant &r_value_res, int &line, String &r_err_str) { return reinterpret_cast<ResourceInteractiveLoaderText *>(p_self)->_parse_ext_resource_export(p_stream, r_value_res, line, r_err_str); }
  66. Error _parse_sub_resource_export(VariantParser::Stream *p_stream, Variant &r_value_res, int &line, String &r_err_str);
  67. Error _parse_ext_resource_export(VariantParser::Stream *p_stream, Variant &r_value_res, int &line, String &r_err_str);
  68. VariantParser::ResourceParser rp_export;
  69. Ref<PackedScene> packed_scene;
  70. friend class ResourceFormatLoaderText;
  71. List<RES> resource_cache;
  72. Error error;
  73. RES resource;
  74. public:
  75. virtual void set_local_path(const String &p_local_path);
  76. virtual Ref<Resource> get_resource();
  77. virtual Error poll();
  78. virtual int get_stage() const;
  79. virtual int get_stage_count() const;
  80. void open(FileAccess *p_f, bool p_skip_first_tag = false);
  81. String recognize(FileAccess *p_f);
  82. void get_dependencies(FileAccess *p_f, List<String> *p_dependencies, bool p_add_types);
  83. Error rename_dependencies(FileAccess *p_f, const String &p_path, const Map<String, String> &p_map);
  84. Error get_export_data(FileAccess *p_f, ExportData &r_export_data);
  85. ~ResourceInteractiveLoaderText();
  86. };
  87. class ResourceFormatLoaderText : public ResourceFormatLoader {
  88. public:
  89. virtual Ref<ResourceInteractiveLoader> load_interactive(const String &p_path, Error *r_error = NULL);
  90. virtual void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const;
  91. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  92. virtual bool handles_type(const String &p_type) const;
  93. virtual String get_resource_type(const String &p_path) const;
  94. virtual void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false);
  95. virtual Error rename_dependencies(const String &p_path, const Map<String, String> &p_map);
  96. virtual Error get_export_data(const String &p_path, ExportData &r_export_data);
  97. static ResourceFormatLoaderText *singleton;
  98. ResourceFormatLoaderText() { singleton = this; }
  99. };
  100. class ResourceFormatSaverTextInstance {
  101. String local_path;
  102. Ref<PackedScene> packed_scene;
  103. bool takeover_paths;
  104. bool relative_paths;
  105. bool bundle_resources;
  106. bool skip_editor;
  107. FileAccess *f;
  108. Set<RES> resource_set;
  109. List<RES> saved_resources;
  110. Map<RES, int> external_resources;
  111. Map<RES, int> internal_resources;
  112. void _find_resources(const Variant &p_variant, bool p_main = false);
  113. static String _write_resources(void *ud, const RES &p_resource);
  114. String _write_resource(const RES &res);
  115. public:
  116. Error save(const String &p_path, const RES &p_resource, uint32_t p_flags = 0);
  117. };
  118. class ResourceFormatSaverText : public ResourceFormatSaver {
  119. public:
  120. static ResourceFormatSaverText *singleton;
  121. virtual Error save(const String &p_path, const RES &p_resource, uint32_t p_flags = 0);
  122. virtual bool recognize(const RES &p_resource) const;
  123. virtual void get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const;
  124. ResourceFormatSaverText();
  125. };
  126. #endif // SCENE_FORMAT_TEXT_H