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