resource_format_text.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /**************************************************************************/
  2. /* resource_format_text.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/file_access.h"
  33. #include "core/io/resource_loader.h"
  34. #include "core/io/resource_saver.h"
  35. #include "core/variant/variant_parser.h"
  36. #include "scene/resources/packed_scene.h"
  37. class ResourceLoaderText {
  38. public:
  39. enum {
  40. // Version 2: Changed names for Basis, AABB, Vectors, etc.
  41. // Version 3: New string ID for ext/subresources, breaks forward compat.
  42. // Version 4: PackedByteArray can be base64 encoded, and PackedVector4Array was added.
  43. FORMAT_VERSION = 4,
  44. // For compat, save as version 3 if not using PackedVector4Array or no big PackedByteArray.
  45. FORMAT_VERSION_COMPAT = 3,
  46. };
  47. private:
  48. bool translation_remapped = false;
  49. String local_path;
  50. String res_path;
  51. String error_text;
  52. Ref<FileAccess> f;
  53. VariantParser::StreamFile stream;
  54. struct ExtResource {
  55. Ref<ResourceLoader::LoadToken> load_token;
  56. String path;
  57. String type;
  58. };
  59. bool is_scene = false;
  60. int format_version;
  61. String res_type;
  62. bool ignore_resource_parsing = false;
  63. HashMap<String, ExtResource> ext_resources;
  64. HashMap<String, Ref<Resource>> int_resources;
  65. int resources_total = 0;
  66. int resource_current = 0;
  67. String resource_type;
  68. String script_class;
  69. VariantParser::Tag next_tag;
  70. ResourceFormatLoader::CacheMode cache_mode = ResourceFormatLoader::CACHE_MODE_REUSE;
  71. ResourceFormatLoader::CacheMode cache_mode_for_external = ResourceFormatLoader::CACHE_MODE_REUSE;
  72. bool use_sub_threads = false;
  73. float *progress = nullptr;
  74. mutable int lines = 0;
  75. ResourceUID::ID res_uid = ResourceUID::INVALID_ID;
  76. HashMap<String, String> remaps;
  77. 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<ResourceLoaderText *>(p_self)->_parse_sub_resource(p_stream, r_res, line, r_err_str); }
  78. 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<ResourceLoaderText *>(p_self)->_parse_ext_resource(p_stream, r_res, line, r_err_str); }
  79. Error _parse_sub_resource(VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str);
  80. Error _parse_ext_resource(VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str);
  81. struct DummyReadData {
  82. bool no_placeholders = false;
  83. HashMap<Ref<Resource>, int> external_resources;
  84. HashMap<String, Ref<Resource>> rev_external_resources;
  85. HashMap<Ref<Resource>, int> resource_index_map;
  86. HashMap<String, Ref<Resource>> resource_map;
  87. };
  88. 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(static_cast<DummyReadData *>(p_self), p_stream, r_res, line, r_err_str); }
  89. 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(static_cast<DummyReadData *>(p_self), p_stream, r_res, line, r_err_str); }
  90. static Error _parse_sub_resource_dummy(DummyReadData *p_data, VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str);
  91. static Error _parse_ext_resource_dummy(DummyReadData *p_data, VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str);
  92. void _printerr();
  93. VariantParser::ResourceParser rp;
  94. friend class ResourceFormatLoaderText;
  95. friend class ResourceFormatSaverText;
  96. Error error = OK;
  97. Ref<Resource> resource;
  98. Ref<PackedScene> _parse_node_tag(VariantParser::ResourceParser &parser);
  99. public:
  100. Ref<Resource> get_resource();
  101. Error load();
  102. Error set_uid(Ref<FileAccess> p_f, ResourceUID::ID p_uid);
  103. int get_stage() const;
  104. int get_stage_count() const;
  105. void set_translation_remapped(bool p_remapped);
  106. void open(Ref<FileAccess> p_f, bool p_skip_first_tag = false);
  107. String recognize(Ref<FileAccess> p_f);
  108. String recognize_script_class(Ref<FileAccess> p_f);
  109. ResourceUID::ID get_uid(Ref<FileAccess> p_f);
  110. void get_dependencies(Ref<FileAccess> p_f, List<String> *p_dependencies, bool p_add_types);
  111. Error rename_dependencies(Ref<FileAccess> p_f, const String &p_path, const HashMap<String, String> &p_map);
  112. Error get_classes_used(HashSet<StringName> *r_classes);
  113. ResourceLoaderText();
  114. };
  115. class ResourceFormatLoaderText : public ResourceFormatLoader {
  116. public:
  117. static ResourceFormatLoaderText *singleton;
  118. virtual Ref<Resource> load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override;
  119. virtual void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const override;
  120. virtual void get_recognized_extensions(List<String> *p_extensions) const override;
  121. virtual bool handles_type(const String &p_type) const override;
  122. virtual void get_classes_used(const String &p_path, HashSet<StringName> *r_classes) override;
  123. virtual String get_resource_type(const String &p_path) const override;
  124. virtual String get_resource_script_class(const String &p_path) const override;
  125. virtual ResourceUID::ID get_resource_uid(const String &p_path) const override;
  126. virtual bool has_custom_uid_support() const override;
  127. virtual void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false) override;
  128. virtual Error rename_dependencies(const String &p_path, const HashMap<String, String> &p_map) override;
  129. ResourceFormatLoaderText() { singleton = this; }
  130. };
  131. class ResourceFormatSaverTextInstance {
  132. String local_path;
  133. Ref<PackedScene> packed_scene;
  134. bool takeover_paths = false;
  135. bool relative_paths = false;
  136. bool bundle_resources = false;
  137. bool skip_editor = false;
  138. struct NonPersistentKey { //for resource properties generated on the fly
  139. Ref<Resource> base;
  140. StringName property;
  141. bool operator<(const NonPersistentKey &p_key) const { return base == p_key.base ? property < p_key.property : base < p_key.base; }
  142. };
  143. RBMap<NonPersistentKey, Variant> non_persistent_map;
  144. HashSet<Ref<Resource>> resource_set;
  145. List<Ref<Resource>> saved_resources;
  146. HashMap<Ref<Resource>, String> external_resources;
  147. HashMap<Ref<Resource>, String> internal_resources;
  148. bool use_compat = true;
  149. struct ResourceSort {
  150. Ref<Resource> resource;
  151. String id;
  152. bool operator<(const ResourceSort &p_right) const {
  153. return id.naturalnocasecmp_to(p_right.id) < 0;
  154. }
  155. };
  156. void _find_resources(const Variant &p_variant, bool p_main = false);
  157. static String _write_resources(void *ud, const Ref<Resource> &p_resource);
  158. String _write_resource(const Ref<Resource> &res);
  159. public:
  160. Error save(const String &p_path, const Ref<Resource> &p_resource, uint32_t p_flags = 0);
  161. };
  162. class ResourceFormatSaverText : public ResourceFormatSaver {
  163. public:
  164. static ResourceFormatSaverText *singleton;
  165. virtual Error save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags = 0) override;
  166. virtual Error set_uid(const String &p_path, ResourceUID::ID p_uid) override;
  167. virtual bool recognize(const Ref<Resource> &p_resource) const override;
  168. virtual void get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const override;
  169. ResourceFormatSaverText();
  170. };
  171. #endif // RESOURCE_FORMAT_TEXT_H