resource_format_text.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. bool translation_remapped = false;
  39. String local_path;
  40. String res_path;
  41. String error_text;
  42. Ref<FileAccess> f;
  43. VariantParser::StreamFile stream;
  44. struct ExtResource {
  45. Ref<ResourceLoader::LoadToken> load_token;
  46. String path;
  47. String type;
  48. };
  49. bool is_scene = false;
  50. String res_type;
  51. bool ignore_resource_parsing = false;
  52. HashMap<String, ExtResource> ext_resources;
  53. HashMap<String, Ref<Resource>> int_resources;
  54. int resources_total = 0;
  55. int resource_current = 0;
  56. String resource_type;
  57. String script_class;
  58. VariantParser::Tag next_tag;
  59. ResourceFormatLoader::CacheMode cache_mode = ResourceFormatLoader::CACHE_MODE_REUSE;
  60. bool use_sub_threads = false;
  61. float *progress = nullptr;
  62. mutable int lines = 0;
  63. ResourceUID::ID res_uid = ResourceUID::INVALID_ID;
  64. HashMap<String, String> remaps;
  65. 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); }
  66. 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); }
  67. Error _parse_sub_resource(VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str);
  68. Error _parse_ext_resource(VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str);
  69. // for converter
  70. class DummyResource : public Resource {
  71. public:
  72. };
  73. struct DummyReadData {
  74. bool no_placeholders = false;
  75. HashMap<Ref<Resource>, int> external_resources;
  76. HashMap<String, Ref<Resource>> rev_external_resources;
  77. HashMap<Ref<Resource>, int> resource_index_map;
  78. HashMap<String, Ref<Resource>> resource_map;
  79. };
  80. 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); }
  81. 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); }
  82. static Error _parse_sub_resource_dummy(DummyReadData *p_data, VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str);
  83. static Error _parse_ext_resource_dummy(DummyReadData *p_data, VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str);
  84. VariantParser::ResourceParser rp;
  85. friend class ResourceFormatLoaderText;
  86. friend class ResourceFormatSaverText;
  87. Error error = OK;
  88. Ref<Resource> resource;
  89. Ref<PackedScene> _parse_node_tag(VariantParser::ResourceParser &parser);
  90. public:
  91. Ref<Resource> get_resource();
  92. Error load();
  93. Error set_uid(Ref<FileAccess> p_f, ResourceUID::ID p_uid);
  94. int get_stage() const;
  95. int get_stage_count() const;
  96. void set_translation_remapped(bool p_remapped);
  97. void open(Ref<FileAccess> p_f, bool p_skip_first_tag = false);
  98. String recognize(Ref<FileAccess> p_f);
  99. String recognize_script_class(Ref<FileAccess> p_f);
  100. ResourceUID::ID get_uid(Ref<FileAccess> p_f);
  101. void get_dependencies(Ref<FileAccess> p_f, List<String> *p_dependencies, bool p_add_types);
  102. Error rename_dependencies(Ref<FileAccess> p_f, const String &p_path, const HashMap<String, String> &p_map);
  103. Error get_classes_used(HashSet<StringName> *r_classes);
  104. Error save_as_binary(const String &p_path);
  105. ResourceLoaderText();
  106. };
  107. class ResourceFormatLoaderText : public ResourceFormatLoader {
  108. public:
  109. static ResourceFormatLoaderText *singleton;
  110. 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);
  111. virtual void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const;
  112. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  113. virtual bool handles_type(const String &p_type) const;
  114. virtual void get_classes_used(const String &p_path, HashSet<StringName> *r_classes);
  115. virtual String get_resource_type(const String &p_path) const;
  116. virtual String get_resource_script_class(const String &p_path) const;
  117. virtual ResourceUID::ID get_resource_uid(const String &p_path) const;
  118. virtual void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false);
  119. virtual Error rename_dependencies(const String &p_path, const HashMap<String, String> &p_map);
  120. static Error convert_file_to_binary(const String &p_src_path, const String &p_dst_path);
  121. ResourceFormatLoaderText() { singleton = this; }
  122. };
  123. class ResourceFormatSaverTextInstance {
  124. String local_path;
  125. Ref<PackedScene> packed_scene;
  126. bool takeover_paths = false;
  127. bool relative_paths = false;
  128. bool bundle_resources = false;
  129. bool skip_editor = false;
  130. struct NonPersistentKey { //for resource properties generated on the fly
  131. Ref<Resource> base;
  132. StringName property;
  133. bool operator<(const NonPersistentKey &p_key) const { return base == p_key.base ? property < p_key.property : base < p_key.base; }
  134. };
  135. RBMap<NonPersistentKey, Variant> non_persistent_map;
  136. HashSet<Ref<Resource>> resource_set;
  137. List<Ref<Resource>> saved_resources;
  138. HashMap<Ref<Resource>, String> external_resources;
  139. HashMap<Ref<Resource>, String> internal_resources;
  140. struct ResourceSort {
  141. Ref<Resource> resource;
  142. String id;
  143. bool operator<(const ResourceSort &p_right) const {
  144. return id.naturalnocasecmp_to(p_right.id) < 0;
  145. }
  146. };
  147. void _find_resources(const Variant &p_variant, bool p_main = false);
  148. static String _write_resources(void *ud, const Ref<Resource> &p_resource);
  149. String _write_resource(const Ref<Resource> &res);
  150. public:
  151. Error save(const String &p_path, const Ref<Resource> &p_resource, uint32_t p_flags = 0);
  152. };
  153. class ResourceFormatSaverText : public ResourceFormatSaver {
  154. public:
  155. static ResourceFormatSaverText *singleton;
  156. virtual Error save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags = 0);
  157. virtual Error set_uid(const String &p_path, ResourceUID::ID p_uid);
  158. virtual bool recognize(const Ref<Resource> &p_resource) const;
  159. virtual void get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const;
  160. ResourceFormatSaverText();
  161. };
  162. #endif // RESOURCE_FORMAT_TEXT_H