Debian Codegraph @

scene/resources/resource_format_text.h

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