1 | /**************************************************************************/ |
2 | /* resource_format_binary.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_BINARY_H |
32 | #define RESOURCE_FORMAT_BINARY_H |
33 | |
34 | #include "core/io/file_access.h" |
35 | #include "core/io/resource_loader.h" |
36 | #include "core/io/resource_saver.h" |
37 | |
38 | class ResourceLoaderBinary { |
39 | bool translation_remapped = false; |
40 | String local_path; |
41 | String res_path; |
42 | String type; |
43 | Ref<Resource> resource; |
44 | uint32_t ver_format = 0; |
45 | |
46 | Ref<FileAccess> f; |
47 | |
48 | uint64_t importmd_ofs = 0; |
49 | |
50 | ResourceUID::ID uid = ResourceUID::INVALID_ID; |
51 | |
52 | Vector<char> str_buf; |
53 | List<Ref<Resource>> resource_cache; |
54 | |
55 | Vector<StringName> string_map; |
56 | |
57 | StringName _get_string(); |
58 | |
59 | struct ExtResource { |
60 | String path; |
61 | String type; |
62 | ResourceUID::ID uid = ResourceUID::INVALID_ID; |
63 | Ref<ResourceLoader::LoadToken> load_token; |
64 | }; |
65 | |
66 | bool using_named_scene_ids = false; |
67 | bool using_uids = false; |
68 | String script_class; |
69 | bool use_sub_threads = false; |
70 | float *progress = nullptr; |
71 | Vector<ExtResource> external_resources; |
72 | |
73 | struct IntResource { |
74 | String path; |
75 | uint64_t offset; |
76 | }; |
77 | |
78 | Vector<IntResource> internal_resources; |
79 | HashMap<String, Ref<Resource>> internal_index_cache; |
80 | |
81 | String get_unicode_string(); |
82 | void _advance_padding(uint32_t p_len); |
83 | |
84 | HashMap<String, String> remaps; |
85 | Error error = OK; |
86 | |
87 | ResourceFormatLoader::CacheMode cache_mode = ResourceFormatLoader::CACHE_MODE_REUSE; |
88 | |
89 | friend class ResourceFormatLoaderBinary; |
90 | |
91 | Error parse_variant(Variant &r_v); |
92 | |
93 | HashMap<String, Ref<Resource>> dependency_cache; |
94 | |
95 | public: |
96 | Ref<Resource> get_resource(); |
97 | Error load(); |
98 | void set_translation_remapped(bool p_remapped); |
99 | |
100 | void set_remaps(const HashMap<String, String> &p_remaps) { remaps = p_remaps; } |
101 | void open(Ref<FileAccess> p_f, bool p_no_resources = false, bool p_keep_uuid_paths = false); |
102 | String recognize(Ref<FileAccess> p_f); |
103 | String recognize_script_class(Ref<FileAccess> p_f); |
104 | void get_dependencies(Ref<FileAccess> p_f, List<String> *p_dependencies, bool p_add_types); |
105 | void get_classes_used(Ref<FileAccess> p_f, HashSet<StringName> *p_classes); |
106 | |
107 | ResourceLoaderBinary() {} |
108 | }; |
109 | |
110 | class ResourceFormatLoaderBinary : public ResourceFormatLoader { |
111 | public: |
112 | 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); |
113 | virtual void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const; |
114 | virtual void get_recognized_extensions(List<String> *p_extensions) const; |
115 | virtual bool handles_type(const String &p_type) const; |
116 | virtual String get_resource_type(const String &p_path) const; |
117 | virtual String get_resource_script_class(const String &p_path) const; |
118 | virtual void get_classes_used(const String &p_path, HashSet<StringName> *r_classes); |
119 | virtual ResourceUID::ID get_resource_uid(const String &p_path) const; |
120 | virtual void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false); |
121 | virtual Error rename_dependencies(const String &p_path, const HashMap<String, String> &p_map); |
122 | }; |
123 | |
124 | class ResourceFormatSaverBinaryInstance { |
125 | String local_path; |
126 | String path; |
127 | |
128 | bool relative_paths; |
129 | bool bundle_resources; |
130 | bool skip_editor; |
131 | bool big_endian; |
132 | bool takeover_paths; |
133 | String magic; |
134 | HashSet<Ref<Resource>> resource_set; |
135 | |
136 | struct NonPersistentKey { //for resource properties generated on the fly |
137 | Ref<Resource> base; |
138 | StringName property; |
139 | bool operator<(const NonPersistentKey &p_key) const { return base == p_key.base ? property < p_key.property : base < p_key.base; } |
140 | }; |
141 | |
142 | RBMap<NonPersistentKey, Variant> non_persistent_map; |
143 | HashMap<StringName, int> string_map; |
144 | Vector<StringName> strings; |
145 | |
146 | HashMap<Ref<Resource>, int> external_resources; |
147 | List<Ref<Resource>> saved_resources; |
148 | |
149 | struct Property { |
150 | int name_idx; |
151 | Variant value; |
152 | PropertyInfo pi; |
153 | }; |
154 | |
155 | struct ResourceData { |
156 | String type; |
157 | List<Property> properties; |
158 | }; |
159 | |
160 | static void _pad_buffer(Ref<FileAccess> f, int p_bytes); |
161 | void _find_resources(const Variant &p_variant, bool p_main = false); |
162 | static void save_unicode_string(Ref<FileAccess> f, const String &p_string, bool p_bit_on_len = false); |
163 | int get_string_index(const String &p_string); |
164 | |
165 | public: |
166 | enum { |
167 | FORMAT_FLAG_NAMED_SCENE_IDS = 1, |
168 | FORMAT_FLAG_UIDS = 2, |
169 | FORMAT_FLAG_REAL_T_IS_DOUBLE = 4, |
170 | FORMAT_FLAG_HAS_SCRIPT_CLASS = 8, |
171 | |
172 | // Amount of reserved 32-bit fields in resource header |
173 | RESERVED_FIELDS = 11 |
174 | }; |
175 | Error save(const String &p_path, const Ref<Resource> &p_resource, uint32_t p_flags = 0); |
176 | Error set_uid(const String &p_path, ResourceUID::ID p_uid); |
177 | static void write_variant(Ref<FileAccess> f, const Variant &p_property, HashMap<Ref<Resource>, int> &resource_map, HashMap<Ref<Resource>, int> &external_resources, HashMap<StringName, int> &string_map, const PropertyInfo &p_hint = PropertyInfo()); |
178 | }; |
179 | |
180 | class ResourceFormatSaverBinary : public ResourceFormatSaver { |
181 | public: |
182 | static ResourceFormatSaverBinary *singleton; |
183 | virtual Error save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags = 0); |
184 | virtual Error set_uid(const String &p_path, ResourceUID::ID p_uid); |
185 | virtual bool recognize(const Ref<Resource> &p_resource) const; |
186 | virtual void get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const; |
187 | |
188 | ResourceFormatSaverBinary(); |
189 | }; |
190 | |
191 | #endif // RESOURCE_FORMAT_BINARY_H |
192 | |