scene_format_text.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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-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. #ifndef SCENE_FORMAT_TEXT_H
  31. #define SCENE_FORMAT_TEXT_H
  32. #include "core/io/resource_loader.h"
  33. #include "core/io/resource_saver.h"
  34. #include "core/os/file_access.h"
  35. #include "core/variant_parser.h"
  36. #include "scene/resources/packed_scene.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. // for converter
  65. class DummyResource : public Resource {
  66. public:
  67. };
  68. struct DummyReadData {
  69. Map<RES, int> external_resources;
  70. Map<int, RES> rev_external_resources;
  71. Set<RES> resource_set;
  72. Map<int, RES> resource_map;
  73. };
  74. static Error _parse_sub_resource_dummys(void *p_self, VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str) { return _parse_sub_resource_dummy((DummyReadData *)(p_self), p_stream, r_res, line, r_err_str); }
  75. static Error _parse_ext_resource_dummys(void *p_self, VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str) { return _parse_ext_resource_dummy((DummyReadData *)(p_self), p_stream, r_res, line, r_err_str); }
  76. static Error _parse_sub_resource_dummy(DummyReadData *p_data, VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str);
  77. static Error _parse_ext_resource_dummy(DummyReadData *p_data, VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str);
  78. VariantParser::ResourceParser rp;
  79. friend class ResourceFormatLoaderText;
  80. List<RES> resource_cache;
  81. Error error;
  82. RES resource;
  83. Ref<PackedScene> _parse_node_tag(VariantParser::ResourceParser &parser);
  84. public:
  85. virtual void set_local_path(const String &p_local_path);
  86. virtual Ref<Resource> get_resource();
  87. virtual Error poll();
  88. virtual int get_stage() const;
  89. virtual int get_stage_count() const;
  90. virtual void set_translation_remapped(bool p_remapped);
  91. void open(FileAccess *p_f, bool p_skip_first_tag = false);
  92. String recognize(FileAccess *p_f);
  93. void get_dependencies(FileAccess *p_f, List<String> *p_dependencies, bool p_add_types);
  94. Error rename_dependencies(FileAccess *p_f, const String &p_path, const Map<String, String> &p_map);
  95. Error save_as_binary(FileAccess *p_f, const String &p_path);
  96. ResourceInteractiveLoaderText();
  97. ~ResourceInteractiveLoaderText();
  98. };
  99. class ResourceFormatLoaderText : public ResourceFormatLoader {
  100. GDCLASS(ResourceFormatLoaderText, ResourceFormatLoader)
  101. public:
  102. static ResourceFormatLoaderText *singleton;
  103. virtual Ref<ResourceInteractiveLoader> load_interactive(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
  104. virtual void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const;
  105. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  106. virtual bool handles_type(const String &p_type) const;
  107. virtual String get_resource_type(const String &p_path) const;
  108. virtual void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false);
  109. virtual Error rename_dependencies(const String &p_path, const Map<String, String> &p_map);
  110. static Error convert_file_to_binary(const String &p_src_path, const String &p_dst_path);
  111. ResourceFormatLoaderText() { singleton = this; }
  112. };
  113. class ResourceFormatSaverTextInstance {
  114. String local_path;
  115. Ref<PackedScene> packed_scene;
  116. bool takeover_paths;
  117. bool relative_paths;
  118. bool bundle_resources;
  119. bool skip_editor;
  120. FileAccess *f;
  121. Set<RES> resource_set;
  122. List<RES> saved_resources;
  123. Map<RES, int> external_resources;
  124. Map<RES, int> internal_resources;
  125. void _find_resources(const Variant &p_variant, bool p_main = false);
  126. static String _write_resources(void *ud, const RES &p_resource);
  127. String _write_resource(const RES &res);
  128. public:
  129. Error save(const String &p_path, const RES &p_resource, uint32_t p_flags = 0);
  130. };
  131. class ResourceFormatSaverText : public ResourceFormatSaver {
  132. GDCLASS(ResourceFormatSaverText, ResourceFormatSaver)
  133. public:
  134. static ResourceFormatSaverText *singleton;
  135. virtual Error save(const String &p_path, const RES &p_resource, uint32_t p_flags = 0);
  136. virtual bool recognize(const RES &p_resource) const;
  137. virtual void get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const;
  138. ResourceFormatSaverText();
  139. };
  140. #endif // SCENE_FORMAT_TEXT_H