resource_format_text.h 9.3 KB

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