resource_format_text.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*************************************************************************/
  2. /* resource_format_text.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 RESOURCE_FORMAT_TEXT_H
  31. #define RESOURCE_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. public:
  101. static ResourceFormatLoaderText *singleton;
  102. virtual Ref<ResourceInteractiveLoader> load_interactive(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
  103. virtual void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const;
  104. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  105. virtual bool handles_type(const String &p_type) const;
  106. virtual String get_resource_type(const String &p_path) const;
  107. virtual void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false);
  108. virtual Error rename_dependencies(const String &p_path, const Map<String, String> &p_map);
  109. static Error convert_file_to_binary(const String &p_src_path, const String &p_dst_path);
  110. ResourceFormatLoaderText() { singleton = this; }
  111. };
  112. class ResourceFormatSaverTextInstance {
  113. String local_path;
  114. Ref<PackedScene> packed_scene;
  115. bool takeover_paths;
  116. bool relative_paths;
  117. bool bundle_resources;
  118. bool skip_editor;
  119. FileAccess *f;
  120. struct NonPersistentKey { //for resource properties generated on the fly
  121. RES base;
  122. StringName property;
  123. bool operator<(const NonPersistentKey &p_key) const { return base == p_key.base ? property < p_key.property : base < p_key.base; }
  124. };
  125. Map<NonPersistentKey, RES> non_persistent_map;
  126. Set<RES> resource_set;
  127. List<RES> saved_resources;
  128. Map<RES, int> external_resources;
  129. Map<RES, int> internal_resources;
  130. struct ResourceSort {
  131. RES resource;
  132. int index;
  133. bool operator<(const ResourceSort &p_right) const {
  134. return index < p_right.index;
  135. }
  136. };
  137. void _find_resources(const Variant &p_variant, bool p_main = false);
  138. static String _write_resources(void *ud, const RES &p_resource);
  139. String _write_resource(const RES &res);
  140. public:
  141. Error save(const String &p_path, const RES &p_resource, uint32_t p_flags = 0);
  142. };
  143. class ResourceFormatSaverText : public ResourceFormatSaver {
  144. public:
  145. static ResourceFormatSaverText *singleton;
  146. virtual Error save(const String &p_path, const RES &p_resource, uint32_t p_flags = 0);
  147. virtual bool recognize(const RES &p_resource) const;
  148. virtual void get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const;
  149. ResourceFormatSaverText();
  150. };
  151. #endif // RESOURCE_FORMAT_TEXT_H