1 | /**************************************************************************/ |
2 | /* resource_format_binary.cpp */ |
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 | #include "resource_format_binary.h" |
32 | |
33 | #include "core/config/project_settings.h" |
34 | #include "core/io/dir_access.h" |
35 | #include "core/io/file_access_compressed.h" |
36 | #include "core/io/image.h" |
37 | #include "core/io/marshalls.h" |
38 | #include "core/io/missing_resource.h" |
39 | #include "core/object/script_language.h" |
40 | #include "core/version.h" |
41 | |
42 | //#define print_bl(m_what) print_line(m_what) |
43 | #define print_bl(m_what) (void)(m_what) |
44 | |
45 | enum { |
46 | //numbering must be different from variant, in case new variant types are added (variant must be always contiguous for jumptable optimization) |
47 | VARIANT_NIL = 1, |
48 | VARIANT_BOOL = 2, |
49 | VARIANT_INT = 3, |
50 | VARIANT_FLOAT = 4, |
51 | VARIANT_STRING = 5, |
52 | VARIANT_VECTOR2 = 10, |
53 | VARIANT_RECT2 = 11, |
54 | VARIANT_VECTOR3 = 12, |
55 | VARIANT_PLANE = 13, |
56 | VARIANT_QUATERNION = 14, |
57 | VARIANT_AABB = 15, |
58 | VARIANT_BASIS = 16, |
59 | VARIANT_TRANSFORM3D = 17, |
60 | VARIANT_TRANSFORM2D = 18, |
61 | VARIANT_COLOR = 20, |
62 | VARIANT_NODE_PATH = 22, |
63 | VARIANT_RID = 23, |
64 | VARIANT_OBJECT = 24, |
65 | VARIANT_INPUT_EVENT = 25, |
66 | VARIANT_DICTIONARY = 26, |
67 | VARIANT_ARRAY = 30, |
68 | VARIANT_PACKED_BYTE_ARRAY = 31, |
69 | VARIANT_PACKED_INT32_ARRAY = 32, |
70 | VARIANT_PACKED_FLOAT32_ARRAY = 33, |
71 | VARIANT_PACKED_STRING_ARRAY = 34, |
72 | VARIANT_PACKED_VECTOR3_ARRAY = 35, |
73 | VARIANT_PACKED_COLOR_ARRAY = 36, |
74 | VARIANT_PACKED_VECTOR2_ARRAY = 37, |
75 | VARIANT_INT64 = 40, |
76 | VARIANT_DOUBLE = 41, |
77 | VARIANT_CALLABLE = 42, |
78 | VARIANT_SIGNAL = 43, |
79 | VARIANT_STRING_NAME = 44, |
80 | VARIANT_VECTOR2I = 45, |
81 | VARIANT_RECT2I = 46, |
82 | VARIANT_VECTOR3I = 47, |
83 | VARIANT_PACKED_INT64_ARRAY = 48, |
84 | VARIANT_PACKED_FLOAT64_ARRAY = 49, |
85 | VARIANT_VECTOR4 = 50, |
86 | VARIANT_VECTOR4I = 51, |
87 | VARIANT_PROJECTION = 52, |
88 | OBJECT_EMPTY = 0, |
89 | OBJECT_EXTERNAL_RESOURCE = 1, |
90 | OBJECT_INTERNAL_RESOURCE = 2, |
91 | OBJECT_EXTERNAL_RESOURCE_INDEX = 3, |
92 | // Version 2: added 64 bits support for float and int. |
93 | // Version 3: changed nodepath encoding. |
94 | // Version 4: new string ID for ext/subresources, breaks forward compat. |
95 | // Version 5: Ability to store script class in the header. |
96 | FORMAT_VERSION = 5, |
97 | FORMAT_VERSION_CAN_RENAME_DEPS = 1, |
98 | FORMAT_VERSION_NO_NODEPATH_PROPERTY = 3, |
99 | }; |
100 | |
101 | void ResourceLoaderBinary::_advance_padding(uint32_t p_len) { |
102 | uint32_t = 4 - (p_len % 4); |
103 | if (extra < 4) { |
104 | for (uint32_t i = 0; i < extra; i++) { |
105 | f->get_8(); //pad to 32 |
106 | } |
107 | } |
108 | } |
109 | |
110 | static Error read_reals(real_t *dst, Ref<FileAccess> &f, size_t count) { |
111 | if (f->real_is_double) { |
112 | if constexpr (sizeof(real_t) == 8) { |
113 | // Ideal case with double-precision |
114 | f->get_buffer((uint8_t *)dst, count * sizeof(double)); |
115 | #ifdef BIG_ENDIAN_ENABLED |
116 | { |
117 | uint64_t *dst = (uint64_t *)dst; |
118 | for (size_t i = 0; i < count; i++) { |
119 | dst[i] = BSWAP64(dst[i]); |
120 | } |
121 | } |
122 | #endif |
123 | } else if constexpr (sizeof(real_t) == 4) { |
124 | // May be slower, but this is for compatibility. Eventually the data should be converted. |
125 | for (size_t i = 0; i < count; ++i) { |
126 | dst[i] = f->get_double(); |
127 | } |
128 | } else { |
129 | ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "real_t size is neither 4 nor 8!" ); |
130 | } |
131 | } else { |
132 | if constexpr (sizeof(real_t) == 4) { |
133 | // Ideal case with float-precision |
134 | f->get_buffer((uint8_t *)dst, count * sizeof(float)); |
135 | #ifdef BIG_ENDIAN_ENABLED |
136 | { |
137 | uint32_t *dst = (uint32_t *)dst; |
138 | for (size_t i = 0; i < count; i++) { |
139 | dst[i] = BSWAP32(dst[i]); |
140 | } |
141 | } |
142 | #endif |
143 | } else if constexpr (sizeof(real_t) == 8) { |
144 | for (size_t i = 0; i < count; ++i) { |
145 | dst[i] = f->get_float(); |
146 | } |
147 | } else { |
148 | ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "real_t size is neither 4 nor 8!" ); |
149 | } |
150 | } |
151 | return OK; |
152 | } |
153 | |
154 | StringName ResourceLoaderBinary::_get_string() { |
155 | uint32_t id = f->get_32(); |
156 | if (id & 0x80000000) { |
157 | uint32_t len = id & 0x7FFFFFFF; |
158 | if ((int)len > str_buf.size()) { |
159 | str_buf.resize(len); |
160 | } |
161 | if (len == 0) { |
162 | return StringName(); |
163 | } |
164 | f->get_buffer((uint8_t *)&str_buf[0], len); |
165 | String s; |
166 | s.parse_utf8(&str_buf[0]); |
167 | return s; |
168 | } |
169 | |
170 | return string_map[id]; |
171 | } |
172 | |
173 | Error ResourceLoaderBinary::parse_variant(Variant &r_v) { |
174 | uint32_t prop_type = f->get_32(); |
175 | print_bl("find property of type: " + itos(prop_type)); |
176 | |
177 | switch (prop_type) { |
178 | case VARIANT_NIL: { |
179 | r_v = Variant(); |
180 | } break; |
181 | case VARIANT_BOOL: { |
182 | r_v = bool(f->get_32()); |
183 | } break; |
184 | case VARIANT_INT: { |
185 | r_v = int(f->get_32()); |
186 | } break; |
187 | case VARIANT_INT64: { |
188 | r_v = int64_t(f->get_64()); |
189 | } break; |
190 | case VARIANT_FLOAT: { |
191 | r_v = f->get_real(); |
192 | } break; |
193 | case VARIANT_DOUBLE: { |
194 | r_v = f->get_double(); |
195 | } break; |
196 | case VARIANT_STRING: { |
197 | r_v = get_unicode_string(); |
198 | } break; |
199 | case VARIANT_VECTOR2: { |
200 | Vector2 v; |
201 | v.x = f->get_real(); |
202 | v.y = f->get_real(); |
203 | r_v = v; |
204 | |
205 | } break; |
206 | case VARIANT_VECTOR2I: { |
207 | Vector2i v; |
208 | v.x = f->get_32(); |
209 | v.y = f->get_32(); |
210 | r_v = v; |
211 | |
212 | } break; |
213 | case VARIANT_RECT2: { |
214 | Rect2 v; |
215 | v.position.x = f->get_real(); |
216 | v.position.y = f->get_real(); |
217 | v.size.x = f->get_real(); |
218 | v.size.y = f->get_real(); |
219 | r_v = v; |
220 | |
221 | } break; |
222 | case VARIANT_RECT2I: { |
223 | Rect2i v; |
224 | v.position.x = f->get_32(); |
225 | v.position.y = f->get_32(); |
226 | v.size.x = f->get_32(); |
227 | v.size.y = f->get_32(); |
228 | r_v = v; |
229 | |
230 | } break; |
231 | case VARIANT_VECTOR3: { |
232 | Vector3 v; |
233 | v.x = f->get_real(); |
234 | v.y = f->get_real(); |
235 | v.z = f->get_real(); |
236 | r_v = v; |
237 | } break; |
238 | case VARIANT_VECTOR3I: { |
239 | Vector3i v; |
240 | v.x = f->get_32(); |
241 | v.y = f->get_32(); |
242 | v.z = f->get_32(); |
243 | r_v = v; |
244 | } break; |
245 | case VARIANT_VECTOR4: { |
246 | Vector4 v; |
247 | v.x = f->get_real(); |
248 | v.y = f->get_real(); |
249 | v.z = f->get_real(); |
250 | v.w = f->get_real(); |
251 | r_v = v; |
252 | } break; |
253 | case VARIANT_VECTOR4I: { |
254 | Vector4i v; |
255 | v.x = f->get_32(); |
256 | v.y = f->get_32(); |
257 | v.z = f->get_32(); |
258 | v.w = f->get_32(); |
259 | r_v = v; |
260 | } break; |
261 | case VARIANT_PLANE: { |
262 | Plane v; |
263 | v.normal.x = f->get_real(); |
264 | v.normal.y = f->get_real(); |
265 | v.normal.z = f->get_real(); |
266 | v.d = f->get_real(); |
267 | r_v = v; |
268 | } break; |
269 | case VARIANT_QUATERNION: { |
270 | Quaternion v; |
271 | v.x = f->get_real(); |
272 | v.y = f->get_real(); |
273 | v.z = f->get_real(); |
274 | v.w = f->get_real(); |
275 | r_v = v; |
276 | |
277 | } break; |
278 | case VARIANT_AABB: { |
279 | AABB v; |
280 | v.position.x = f->get_real(); |
281 | v.position.y = f->get_real(); |
282 | v.position.z = f->get_real(); |
283 | v.size.x = f->get_real(); |
284 | v.size.y = f->get_real(); |
285 | v.size.z = f->get_real(); |
286 | r_v = v; |
287 | |
288 | } break; |
289 | case VARIANT_TRANSFORM2D: { |
290 | Transform2D v; |
291 | v.columns[0].x = f->get_real(); |
292 | v.columns[0].y = f->get_real(); |
293 | v.columns[1].x = f->get_real(); |
294 | v.columns[1].y = f->get_real(); |
295 | v.columns[2].x = f->get_real(); |
296 | v.columns[2].y = f->get_real(); |
297 | r_v = v; |
298 | |
299 | } break; |
300 | case VARIANT_BASIS: { |
301 | Basis v; |
302 | v.rows[0].x = f->get_real(); |
303 | v.rows[0].y = f->get_real(); |
304 | v.rows[0].z = f->get_real(); |
305 | v.rows[1].x = f->get_real(); |
306 | v.rows[1].y = f->get_real(); |
307 | v.rows[1].z = f->get_real(); |
308 | v.rows[2].x = f->get_real(); |
309 | v.rows[2].y = f->get_real(); |
310 | v.rows[2].z = f->get_real(); |
311 | r_v = v; |
312 | |
313 | } break; |
314 | case VARIANT_TRANSFORM3D: { |
315 | Transform3D v; |
316 | v.basis.rows[0].x = f->get_real(); |
317 | v.basis.rows[0].y = f->get_real(); |
318 | v.basis.rows[0].z = f->get_real(); |
319 | v.basis.rows[1].x = f->get_real(); |
320 | v.basis.rows[1].y = f->get_real(); |
321 | v.basis.rows[1].z = f->get_real(); |
322 | v.basis.rows[2].x = f->get_real(); |
323 | v.basis.rows[2].y = f->get_real(); |
324 | v.basis.rows[2].z = f->get_real(); |
325 | v.origin.x = f->get_real(); |
326 | v.origin.y = f->get_real(); |
327 | v.origin.z = f->get_real(); |
328 | r_v = v; |
329 | } break; |
330 | case VARIANT_PROJECTION: { |
331 | Projection v; |
332 | v.columns[0].x = f->get_real(); |
333 | v.columns[0].y = f->get_real(); |
334 | v.columns[0].z = f->get_real(); |
335 | v.columns[0].w = f->get_real(); |
336 | v.columns[1].x = f->get_real(); |
337 | v.columns[1].y = f->get_real(); |
338 | v.columns[1].z = f->get_real(); |
339 | v.columns[1].w = f->get_real(); |
340 | v.columns[2].x = f->get_real(); |
341 | v.columns[2].y = f->get_real(); |
342 | v.columns[2].z = f->get_real(); |
343 | v.columns[2].w = f->get_real(); |
344 | v.columns[3].x = f->get_real(); |
345 | v.columns[3].y = f->get_real(); |
346 | v.columns[3].z = f->get_real(); |
347 | v.columns[3].w = f->get_real(); |
348 | r_v = v; |
349 | } break; |
350 | case VARIANT_COLOR: { |
351 | Color v; // Colors should always be in single-precision. |
352 | v.r = f->get_float(); |
353 | v.g = f->get_float(); |
354 | v.b = f->get_float(); |
355 | v.a = f->get_float(); |
356 | r_v = v; |
357 | |
358 | } break; |
359 | case VARIANT_STRING_NAME: { |
360 | r_v = StringName(get_unicode_string()); |
361 | } break; |
362 | |
363 | case VARIANT_NODE_PATH: { |
364 | Vector<StringName> names; |
365 | Vector<StringName> subnames; |
366 | bool absolute; |
367 | |
368 | int name_count = f->get_16(); |
369 | uint32_t subname_count = f->get_16(); |
370 | absolute = subname_count & 0x8000; |
371 | subname_count &= 0x7FFF; |
372 | if (ver_format < FORMAT_VERSION_NO_NODEPATH_PROPERTY) { |
373 | subname_count += 1; // has a property field, so we should count it as well |
374 | } |
375 | |
376 | for (int i = 0; i < name_count; i++) { |
377 | names.push_back(_get_string()); |
378 | } |
379 | for (uint32_t i = 0; i < subname_count; i++) { |
380 | subnames.push_back(_get_string()); |
381 | } |
382 | |
383 | NodePath np = NodePath(names, subnames, absolute); |
384 | |
385 | r_v = np; |
386 | |
387 | } break; |
388 | case VARIANT_RID: { |
389 | r_v = f->get_32(); |
390 | } break; |
391 | case VARIANT_OBJECT: { |
392 | uint32_t objtype = f->get_32(); |
393 | |
394 | switch (objtype) { |
395 | case OBJECT_EMPTY: { |
396 | //do none |
397 | |
398 | } break; |
399 | case OBJECT_INTERNAL_RESOURCE: { |
400 | uint32_t index = f->get_32(); |
401 | String path; |
402 | |
403 | if (using_named_scene_ids) { // New format. |
404 | ERR_FAIL_INDEX_V((int)index, internal_resources.size(), ERR_PARSE_ERROR); |
405 | path = internal_resources[index].path; |
406 | } else { |
407 | path += res_path + "::" + itos(index); |
408 | } |
409 | |
410 | //always use internal cache for loading internal resources |
411 | if (!internal_index_cache.has(path)) { |
412 | WARN_PRINT(String("Couldn't load resource (no cache): " + path).utf8().get_data()); |
413 | r_v = Variant(); |
414 | } else { |
415 | r_v = internal_index_cache[path]; |
416 | } |
417 | } break; |
418 | case OBJECT_EXTERNAL_RESOURCE: { |
419 | //old file format, still around for compatibility |
420 | |
421 | String exttype = get_unicode_string(); |
422 | String path = get_unicode_string(); |
423 | |
424 | if (!path.contains("://" ) && path.is_relative_path()) { |
425 | // path is relative to file being loaded, so convert to a resource path |
426 | path = ProjectSettings::get_singleton()->localize_path(res_path.get_base_dir().path_join(path)); |
427 | } |
428 | |
429 | if (remaps.find(path)) { |
430 | path = remaps[path]; |
431 | } |
432 | |
433 | Ref<Resource> res = ResourceLoader::load(path, exttype); |
434 | |
435 | if (res.is_null()) { |
436 | WARN_PRINT(String("Couldn't load resource: " + path).utf8().get_data()); |
437 | } |
438 | r_v = res; |
439 | |
440 | } break; |
441 | case OBJECT_EXTERNAL_RESOURCE_INDEX: { |
442 | //new file format, just refers to an index in the external list |
443 | int erindex = f->get_32(); |
444 | |
445 | if (erindex < 0 || erindex >= external_resources.size()) { |
446 | WARN_PRINT("Broken external resource! (index out of size)" ); |
447 | r_v = Variant(); |
448 | } else { |
449 | Ref<ResourceLoader::LoadToken> &load_token = external_resources.write[erindex].load_token; |
450 | if (load_token.is_valid()) { // If not valid, it's OK since then we know this load accepts broken dependencies. |
451 | Error err; |
452 | Ref<Resource> res = ResourceLoader::_load_complete(*load_token.ptr(), &err); |
453 | if (res.is_null()) { |
454 | if (!ResourceLoader::is_cleaning_tasks()) { |
455 | if (!ResourceLoader::get_abort_on_missing_resources()) { |
456 | ResourceLoader::notify_dependency_error(local_path, external_resources[erindex].path, external_resources[erindex].type); |
457 | } else { |
458 | error = ERR_FILE_MISSING_DEPENDENCIES; |
459 | ERR_FAIL_V_MSG(error, "Can't load dependency: " + external_resources[erindex].path + "." ); |
460 | } |
461 | } |
462 | } else { |
463 | r_v = res; |
464 | } |
465 | } |
466 | } |
467 | } break; |
468 | default: { |
469 | ERR_FAIL_V(ERR_FILE_CORRUPT); |
470 | } break; |
471 | } |
472 | } break; |
473 | case VARIANT_CALLABLE: { |
474 | r_v = Callable(); |
475 | } break; |
476 | case VARIANT_SIGNAL: { |
477 | r_v = Signal(); |
478 | } break; |
479 | |
480 | case VARIANT_DICTIONARY: { |
481 | uint32_t len = f->get_32(); |
482 | Dictionary d; //last bit means shared |
483 | len &= 0x7FFFFFFF; |
484 | for (uint32_t i = 0; i < len; i++) { |
485 | Variant key; |
486 | Error err = parse_variant(key); |
487 | ERR_FAIL_COND_V_MSG(err, ERR_FILE_CORRUPT, "Error when trying to parse Variant." ); |
488 | Variant value; |
489 | err = parse_variant(value); |
490 | ERR_FAIL_COND_V_MSG(err, ERR_FILE_CORRUPT, "Error when trying to parse Variant." ); |
491 | d[key] = value; |
492 | } |
493 | r_v = d; |
494 | } break; |
495 | case VARIANT_ARRAY: { |
496 | uint32_t len = f->get_32(); |
497 | Array a; //last bit means shared |
498 | len &= 0x7FFFFFFF; |
499 | a.resize(len); |
500 | for (uint32_t i = 0; i < len; i++) { |
501 | Variant val; |
502 | Error err = parse_variant(val); |
503 | ERR_FAIL_COND_V_MSG(err, ERR_FILE_CORRUPT, "Error when trying to parse Variant." ); |
504 | a[i] = val; |
505 | } |
506 | r_v = a; |
507 | |
508 | } break; |
509 | case VARIANT_PACKED_BYTE_ARRAY: { |
510 | uint32_t len = f->get_32(); |
511 | |
512 | Vector<uint8_t> array; |
513 | array.resize(len); |
514 | uint8_t *w = array.ptrw(); |
515 | f->get_buffer(w, len); |
516 | _advance_padding(len); |
517 | |
518 | r_v = array; |
519 | |
520 | } break; |
521 | case VARIANT_PACKED_INT32_ARRAY: { |
522 | uint32_t len = f->get_32(); |
523 | |
524 | Vector<int32_t> array; |
525 | array.resize(len); |
526 | int32_t *w = array.ptrw(); |
527 | f->get_buffer((uint8_t *)w, len * sizeof(int32_t)); |
528 | #ifdef BIG_ENDIAN_ENABLED |
529 | { |
530 | uint32_t *ptr = (uint32_t *)w.ptr(); |
531 | for (int i = 0; i < len; i++) { |
532 | ptr[i] = BSWAP32(ptr[i]); |
533 | } |
534 | } |
535 | |
536 | #endif |
537 | |
538 | r_v = array; |
539 | } break; |
540 | case VARIANT_PACKED_INT64_ARRAY: { |
541 | uint32_t len = f->get_32(); |
542 | |
543 | Vector<int64_t> array; |
544 | array.resize(len); |
545 | int64_t *w = array.ptrw(); |
546 | f->get_buffer((uint8_t *)w, len * sizeof(int64_t)); |
547 | #ifdef BIG_ENDIAN_ENABLED |
548 | { |
549 | uint64_t *ptr = (uint64_t *)w.ptr(); |
550 | for (int i = 0; i < len; i++) { |
551 | ptr[i] = BSWAP64(ptr[i]); |
552 | } |
553 | } |
554 | |
555 | #endif |
556 | |
557 | r_v = array; |
558 | } break; |
559 | case VARIANT_PACKED_FLOAT32_ARRAY: { |
560 | uint32_t len = f->get_32(); |
561 | |
562 | Vector<float> array; |
563 | array.resize(len); |
564 | float *w = array.ptrw(); |
565 | f->get_buffer((uint8_t *)w, len * sizeof(float)); |
566 | #ifdef BIG_ENDIAN_ENABLED |
567 | { |
568 | uint32_t *ptr = (uint32_t *)w.ptr(); |
569 | for (int i = 0; i < len; i++) { |
570 | ptr[i] = BSWAP32(ptr[i]); |
571 | } |
572 | } |
573 | |
574 | #endif |
575 | |
576 | r_v = array; |
577 | } break; |
578 | case VARIANT_PACKED_FLOAT64_ARRAY: { |
579 | uint32_t len = f->get_32(); |
580 | |
581 | Vector<double> array; |
582 | array.resize(len); |
583 | double *w = array.ptrw(); |
584 | f->get_buffer((uint8_t *)w, len * sizeof(double)); |
585 | #ifdef BIG_ENDIAN_ENABLED |
586 | { |
587 | uint64_t *ptr = (uint64_t *)w.ptr(); |
588 | for (int i = 0; i < len; i++) { |
589 | ptr[i] = BSWAP64(ptr[i]); |
590 | } |
591 | } |
592 | |
593 | #endif |
594 | |
595 | r_v = array; |
596 | } break; |
597 | case VARIANT_PACKED_STRING_ARRAY: { |
598 | uint32_t len = f->get_32(); |
599 | Vector<String> array; |
600 | array.resize(len); |
601 | String *w = array.ptrw(); |
602 | for (uint32_t i = 0; i < len; i++) { |
603 | w[i] = get_unicode_string(); |
604 | } |
605 | |
606 | r_v = array; |
607 | |
608 | } break; |
609 | case VARIANT_PACKED_VECTOR2_ARRAY: { |
610 | uint32_t len = f->get_32(); |
611 | |
612 | Vector<Vector2> array; |
613 | array.resize(len); |
614 | Vector2 *w = array.ptrw(); |
615 | static_assert(sizeof(Vector2) == 2 * sizeof(real_t)); |
616 | const Error err = read_reals(reinterpret_cast<real_t *>(w), f, len * 2); |
617 | ERR_FAIL_COND_V(err != OK, err); |
618 | |
619 | r_v = array; |
620 | |
621 | } break; |
622 | case VARIANT_PACKED_VECTOR3_ARRAY: { |
623 | uint32_t len = f->get_32(); |
624 | |
625 | Vector<Vector3> array; |
626 | array.resize(len); |
627 | Vector3 *w = array.ptrw(); |
628 | static_assert(sizeof(Vector3) == 3 * sizeof(real_t)); |
629 | const Error err = read_reals(reinterpret_cast<real_t *>(w), f, len * 3); |
630 | ERR_FAIL_COND_V(err != OK, err); |
631 | |
632 | r_v = array; |
633 | |
634 | } break; |
635 | case VARIANT_PACKED_COLOR_ARRAY: { |
636 | uint32_t len = f->get_32(); |
637 | |
638 | Vector<Color> array; |
639 | array.resize(len); |
640 | Color *w = array.ptrw(); |
641 | // Colors always use `float` even with double-precision support enabled |
642 | static_assert(sizeof(Color) == 4 * sizeof(float)); |
643 | f->get_buffer((uint8_t *)w, len * sizeof(float) * 4); |
644 | #ifdef BIG_ENDIAN_ENABLED |
645 | { |
646 | uint32_t *ptr = (uint32_t *)w.ptr(); |
647 | for (int i = 0; i < len * 4; i++) { |
648 | ptr[i] = BSWAP32(ptr[i]); |
649 | } |
650 | } |
651 | |
652 | #endif |
653 | |
654 | r_v = array; |
655 | } break; |
656 | default: { |
657 | ERR_FAIL_V(ERR_FILE_CORRUPT); |
658 | } break; |
659 | } |
660 | |
661 | return OK; //never reach anyway |
662 | } |
663 | |
664 | Ref<Resource> ResourceLoaderBinary::get_resource() { |
665 | return resource; |
666 | } |
667 | |
668 | Error ResourceLoaderBinary::load() { |
669 | if (error != OK) { |
670 | return error; |
671 | } |
672 | |
673 | for (int i = 0; i < external_resources.size(); i++) { |
674 | String path = external_resources[i].path; |
675 | |
676 | if (remaps.has(path)) { |
677 | path = remaps[path]; |
678 | } |
679 | |
680 | if (!path.contains("://" ) && path.is_relative_path()) { |
681 | // path is relative to file being loaded, so convert to a resource path |
682 | path = ProjectSettings::get_singleton()->localize_path(path.get_base_dir().path_join(external_resources[i].path)); |
683 | } |
684 | |
685 | external_resources.write[i].path = path; //remap happens here, not on load because on load it can actually be used for filesystem dock resource remap |
686 | external_resources.write[i].load_token = ResourceLoader::_load_start(path, external_resources[i].type, use_sub_threads ? ResourceLoader::LOAD_THREAD_DISTRIBUTE : ResourceLoader::LOAD_THREAD_FROM_CURRENT, ResourceFormatLoader::CACHE_MODE_REUSE); |
687 | if (!external_resources[i].load_token.is_valid()) { |
688 | if (!ResourceLoader::get_abort_on_missing_resources()) { |
689 | ResourceLoader::notify_dependency_error(local_path, path, external_resources[i].type); |
690 | } else { |
691 | error = ERR_FILE_MISSING_DEPENDENCIES; |
692 | ERR_FAIL_V_MSG(error, "Can't load dependency: " + path + "." ); |
693 | } |
694 | } |
695 | } |
696 | |
697 | for (int i = 0; i < internal_resources.size(); i++) { |
698 | bool main = i == (internal_resources.size() - 1); |
699 | |
700 | //maybe it is loaded already |
701 | String path; |
702 | String id; |
703 | |
704 | if (!main) { |
705 | path = internal_resources[i].path; |
706 | |
707 | if (path.begins_with("local://" )) { |
708 | path = path.replace_first("local://" , "" ); |
709 | id = path; |
710 | path = res_path + "::" + path; |
711 | |
712 | internal_resources.write[i].path = path; // Update path. |
713 | } |
714 | |
715 | if (cache_mode == ResourceFormatLoader::CACHE_MODE_REUSE && ResourceCache::has(path)) { |
716 | Ref<Resource> cached = ResourceCache::get_ref(path); |
717 | if (cached.is_valid()) { |
718 | //already loaded, don't do anything |
719 | error = OK; |
720 | internal_index_cache[path] = cached; |
721 | continue; |
722 | } |
723 | } |
724 | } else { |
725 | if (cache_mode != ResourceFormatLoader::CACHE_MODE_IGNORE && !ResourceCache::has(res_path)) { |
726 | path = res_path; |
727 | } |
728 | } |
729 | |
730 | uint64_t offset = internal_resources[i].offset; |
731 | |
732 | f->seek(offset); |
733 | |
734 | String t = get_unicode_string(); |
735 | |
736 | Ref<Resource> res; |
737 | |
738 | if (cache_mode == ResourceFormatLoader::CACHE_MODE_REPLACE && ResourceCache::has(path)) { |
739 | //use the existing one |
740 | Ref<Resource> cached = ResourceCache::get_ref(path); |
741 | if (cached->get_class() == t) { |
742 | cached->reset_state(); |
743 | res = cached; |
744 | } |
745 | } |
746 | |
747 | MissingResource *missing_resource = nullptr; |
748 | |
749 | if (res.is_null()) { |
750 | //did not replace |
751 | |
752 | Object *obj = ClassDB::instantiate(t); |
753 | if (!obj) { |
754 | if (ResourceLoader::is_creating_missing_resources_if_class_unavailable_enabled()) { |
755 | //create a missing resource |
756 | missing_resource = memnew(MissingResource); |
757 | missing_resource->set_original_class(t); |
758 | missing_resource->set_recording_properties(true); |
759 | obj = missing_resource; |
760 | } else { |
761 | error = ERR_FILE_CORRUPT; |
762 | ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, local_path + ":Resource of unrecognized type in file: " + t + "." ); |
763 | } |
764 | } |
765 | |
766 | Resource *r = Object::cast_to<Resource>(obj); |
767 | if (!r) { |
768 | String obj_class = obj->get_class(); |
769 | error = ERR_FILE_CORRUPT; |
770 | memdelete(obj); //bye |
771 | ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, local_path + ":Resource type in resource field not a resource, type is: " + obj_class + "." ); |
772 | } |
773 | |
774 | res = Ref<Resource>(r); |
775 | if (!path.is_empty() && cache_mode != ResourceFormatLoader::CACHE_MODE_IGNORE) { |
776 | r->set_path(path, cache_mode == ResourceFormatLoader::CACHE_MODE_REPLACE); //if got here because the resource with same path has different type, replace it |
777 | } |
778 | r->set_scene_unique_id(id); |
779 | } |
780 | |
781 | if (!main) { |
782 | internal_index_cache[path] = res; |
783 | } |
784 | |
785 | int pc = f->get_32(); |
786 | |
787 | //set properties |
788 | |
789 | Dictionary missing_resource_properties; |
790 | |
791 | for (int j = 0; j < pc; j++) { |
792 | StringName name = _get_string(); |
793 | |
794 | if (name == StringName()) { |
795 | error = ERR_FILE_CORRUPT; |
796 | ERR_FAIL_V(ERR_FILE_CORRUPT); |
797 | } |
798 | |
799 | Variant value; |
800 | |
801 | error = parse_variant(value); |
802 | if (error) { |
803 | return error; |
804 | } |
805 | |
806 | bool set_valid = true; |
807 | if (value.get_type() == Variant::OBJECT && missing_resource != nullptr) { |
808 | // If the property being set is a missing resource (and the parent is not), |
809 | // then setting it will most likely not work. |
810 | // Instead, save it as metadata. |
811 | |
812 | Ref<MissingResource> mr = value; |
813 | if (mr.is_valid()) { |
814 | missing_resource_properties[name] = mr; |
815 | set_valid = false; |
816 | } |
817 | } |
818 | |
819 | if (value.get_type() == Variant::ARRAY) { |
820 | Array set_array = value; |
821 | bool is_get_valid = false; |
822 | Variant get_value = res->get(name, &is_get_valid); |
823 | if (is_get_valid && get_value.get_type() == Variant::ARRAY) { |
824 | Array get_array = get_value; |
825 | if (!set_array.is_same_typed(get_array)) { |
826 | value = Array(set_array, get_array.get_typed_builtin(), get_array.get_typed_class_name(), get_array.get_typed_script()); |
827 | } |
828 | } |
829 | } |
830 | |
831 | if (set_valid) { |
832 | res->set(name, value); |
833 | } |
834 | } |
835 | |
836 | if (missing_resource) { |
837 | missing_resource->set_recording_properties(false); |
838 | } |
839 | |
840 | if (!missing_resource_properties.is_empty()) { |
841 | res->set_meta(META_MISSING_RESOURCES, missing_resource_properties); |
842 | } |
843 | |
844 | #ifdef TOOLS_ENABLED |
845 | res->set_edited(false); |
846 | #endif |
847 | |
848 | if (progress) { |
849 | *progress = (i + 1) / float(internal_resources.size()); |
850 | } |
851 | |
852 | resource_cache.push_back(res); |
853 | |
854 | if (main) { |
855 | f.unref(); |
856 | resource = res; |
857 | resource->set_as_translation_remapped(translation_remapped); |
858 | error = OK; |
859 | return OK; |
860 | } |
861 | } |
862 | |
863 | return ERR_FILE_EOF; |
864 | } |
865 | |
866 | void ResourceLoaderBinary::set_translation_remapped(bool p_remapped) { |
867 | translation_remapped = p_remapped; |
868 | } |
869 | |
870 | static void save_ustring(Ref<FileAccess> f, const String &p_string) { |
871 | CharString utf8 = p_string.utf8(); |
872 | f->store_32(utf8.length() + 1); |
873 | f->store_buffer((const uint8_t *)utf8.get_data(), utf8.length() + 1); |
874 | } |
875 | |
876 | static String get_ustring(Ref<FileAccess> f) { |
877 | int len = f->get_32(); |
878 | Vector<char> str_buf; |
879 | str_buf.resize(len); |
880 | f->get_buffer((uint8_t *)&str_buf[0], len); |
881 | String s; |
882 | s.parse_utf8(&str_buf[0]); |
883 | return s; |
884 | } |
885 | |
886 | String ResourceLoaderBinary::get_unicode_string() { |
887 | int len = f->get_32(); |
888 | if (len > str_buf.size()) { |
889 | str_buf.resize(len); |
890 | } |
891 | if (len == 0) { |
892 | return String(); |
893 | } |
894 | f->get_buffer((uint8_t *)&str_buf[0], len); |
895 | String s; |
896 | s.parse_utf8(&str_buf[0]); |
897 | return s; |
898 | } |
899 | |
900 | void ResourceLoaderBinary::get_classes_used(Ref<FileAccess> p_f, HashSet<StringName> *p_classes) { |
901 | open(p_f, false, true); |
902 | if (error) { |
903 | return; |
904 | } |
905 | |
906 | for (int i = 0; i < internal_resources.size(); i++) { |
907 | p_f->seek(internal_resources[i].offset); |
908 | String t = get_unicode_string(); |
909 | ERR_FAIL_COND(p_f->get_error() != OK); |
910 | if (t != String()) { |
911 | p_classes->insert(t); |
912 | } |
913 | } |
914 | } |
915 | |
916 | void ResourceLoaderBinary::get_dependencies(Ref<FileAccess> p_f, List<String> *p_dependencies, bool p_add_types) { |
917 | open(p_f, false, true); |
918 | if (error) { |
919 | return; |
920 | } |
921 | |
922 | for (int i = 0; i < external_resources.size(); i++) { |
923 | String dep; |
924 | String fallback_path; |
925 | |
926 | if (external_resources[i].uid != ResourceUID::INVALID_ID) { |
927 | dep = ResourceUID::get_singleton()->id_to_text(external_resources[i].uid); |
928 | fallback_path = external_resources[i].path; // Used by Dependency Editor, in case uid path fails. |
929 | } else { |
930 | dep = external_resources[i].path; |
931 | } |
932 | |
933 | if (p_add_types && !external_resources[i].type.is_empty()) { |
934 | dep += "::" + external_resources[i].type; |
935 | } |
936 | if (!fallback_path.is_empty()) { |
937 | if (!p_add_types) { |
938 | dep += "::" ; // Ensure that path comes third, even if there is no type. |
939 | } |
940 | dep += "::" + fallback_path; |
941 | } |
942 | |
943 | p_dependencies->push_back(dep); |
944 | } |
945 | } |
946 | |
947 | void ResourceLoaderBinary::open(Ref<FileAccess> p_f, bool p_no_resources, bool p_keep_uuid_paths) { |
948 | error = OK; |
949 | |
950 | f = p_f; |
951 | uint8_t [4]; |
952 | f->get_buffer(header, 4); |
953 | if (header[0] == 'R' && header[1] == 'S' && header[2] == 'C' && header[3] == 'C') { |
954 | // Compressed. |
955 | Ref<FileAccessCompressed> fac; |
956 | fac.instantiate(); |
957 | error = fac->open_after_magic(f); |
958 | if (error != OK) { |
959 | f.unref(); |
960 | ERR_FAIL_MSG("Failed to open binary resource file: " + local_path + "." ); |
961 | } |
962 | f = fac; |
963 | |
964 | } else if (header[0] != 'R' || header[1] != 'S' || header[2] != 'R' || header[3] != 'C') { |
965 | // Not normal. |
966 | error = ERR_FILE_UNRECOGNIZED; |
967 | f.unref(); |
968 | ERR_FAIL_MSG("Unrecognized binary resource file: " + local_path + "." ); |
969 | } |
970 | |
971 | bool big_endian = f->get_32(); |
972 | bool use_real64 = f->get_32(); |
973 | |
974 | f->set_big_endian(big_endian != 0); //read big endian if saved as big endian |
975 | |
976 | uint32_t ver_major = f->get_32(); |
977 | uint32_t ver_minor = f->get_32(); |
978 | ver_format = f->get_32(); |
979 | |
980 | print_bl("big endian: " + itos(big_endian)); |
981 | #ifdef BIG_ENDIAN_ENABLED |
982 | print_bl("endian swap: " + itos(!big_endian)); |
983 | #else |
984 | print_bl("endian swap: " + itos(big_endian)); |
985 | #endif |
986 | print_bl("real64: " + itos(use_real64)); |
987 | print_bl("major: " + itos(ver_major)); |
988 | print_bl("minor: " + itos(ver_minor)); |
989 | print_bl("format: " + itos(ver_format)); |
990 | |
991 | if (ver_format > FORMAT_VERSION || ver_major > VERSION_MAJOR) { |
992 | f.unref(); |
993 | ERR_FAIL_MSG(vformat("File '%s' can't be loaded, as it uses a format version (%d) or engine version (%d.%d) which are not supported by your engine version (%s)." , |
994 | local_path, ver_format, ver_major, ver_minor, VERSION_BRANCH)); |
995 | } |
996 | |
997 | type = get_unicode_string(); |
998 | |
999 | print_bl("type: " + type); |
1000 | |
1001 | importmd_ofs = f->get_64(); |
1002 | uint32_t flags = f->get_32(); |
1003 | if (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_NAMED_SCENE_IDS) { |
1004 | using_named_scene_ids = true; |
1005 | } |
1006 | if (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_UIDS) { |
1007 | using_uids = true; |
1008 | } |
1009 | f->real_is_double = (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_REAL_T_IS_DOUBLE) != 0; |
1010 | |
1011 | if (using_uids) { |
1012 | uid = f->get_64(); |
1013 | } else { |
1014 | f->get_64(); // skip over uid field |
1015 | uid = ResourceUID::INVALID_ID; |
1016 | } |
1017 | |
1018 | if (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_HAS_SCRIPT_CLASS) { |
1019 | script_class = get_unicode_string(); |
1020 | } |
1021 | |
1022 | for (int i = 0; i < ResourceFormatSaverBinaryInstance::RESERVED_FIELDS; i++) { |
1023 | f->get_32(); //skip a few reserved fields |
1024 | } |
1025 | |
1026 | if (p_no_resources) { |
1027 | return; |
1028 | } |
1029 | |
1030 | uint32_t string_table_size = f->get_32(); |
1031 | string_map.resize(string_table_size); |
1032 | for (uint32_t i = 0; i < string_table_size; i++) { |
1033 | StringName s = get_unicode_string(); |
1034 | string_map.write[i] = s; |
1035 | } |
1036 | |
1037 | print_bl("strings: " + itos(string_table_size)); |
1038 | |
1039 | uint32_t ext_resources_size = f->get_32(); |
1040 | for (uint32_t i = 0; i < ext_resources_size; i++) { |
1041 | ExtResource er; |
1042 | er.type = get_unicode_string(); |
1043 | er.path = get_unicode_string(); |
1044 | if (using_uids) { |
1045 | er.uid = f->get_64(); |
1046 | if (!p_keep_uuid_paths && er.uid != ResourceUID::INVALID_ID) { |
1047 | if (ResourceUID::get_singleton()->has_id(er.uid)) { |
1048 | // If a UID is found and the path is valid, it will be used, otherwise, it falls back to the path. |
1049 | er.path = ResourceUID::get_singleton()->get_id_path(er.uid); |
1050 | } else { |
1051 | #ifdef TOOLS_ENABLED |
1052 | // Silence a warning that can happen during the initial filesystem scan due to cache being regenerated. |
1053 | if (ResourceLoader::get_resource_uid(res_path) != er.uid) { |
1054 | WARN_PRINT(String(res_path + ": In external resource #" + itos(i) + ", invalid UID: " + ResourceUID::get_singleton()->id_to_text(er.uid) + " - using text path instead: " + er.path).utf8().get_data()); |
1055 | } |
1056 | #else |
1057 | WARN_PRINT(String(res_path + ": In external resource #" + itos(i) + ", invalid UID: " + ResourceUID::get_singleton()->id_to_text(er.uid) + " - using text path instead: " + er.path).utf8().get_data()); |
1058 | #endif |
1059 | } |
1060 | } |
1061 | } |
1062 | |
1063 | external_resources.push_back(er); |
1064 | } |
1065 | |
1066 | print_bl("ext resources: " + itos(ext_resources_size)); |
1067 | uint32_t int_resources_size = f->get_32(); |
1068 | |
1069 | for (uint32_t i = 0; i < int_resources_size; i++) { |
1070 | IntResource ir; |
1071 | ir.path = get_unicode_string(); |
1072 | ir.offset = f->get_64(); |
1073 | internal_resources.push_back(ir); |
1074 | } |
1075 | |
1076 | print_bl("int resources: " + itos(int_resources_size)); |
1077 | |
1078 | if (f->eof_reached()) { |
1079 | error = ERR_FILE_CORRUPT; |
1080 | f.unref(); |
1081 | ERR_FAIL_MSG("Premature end of file (EOF): " + local_path + "." ); |
1082 | } |
1083 | } |
1084 | |
1085 | String ResourceLoaderBinary::recognize(Ref<FileAccess> p_f) { |
1086 | error = OK; |
1087 | |
1088 | f = p_f; |
1089 | uint8_t [4]; |
1090 | f->get_buffer(header, 4); |
1091 | if (header[0] == 'R' && header[1] == 'S' && header[2] == 'C' && header[3] == 'C') { |
1092 | // Compressed. |
1093 | Ref<FileAccessCompressed> fac; |
1094 | fac.instantiate(); |
1095 | error = fac->open_after_magic(f); |
1096 | if (error != OK) { |
1097 | f.unref(); |
1098 | return "" ; |
1099 | } |
1100 | f = fac; |
1101 | |
1102 | } else if (header[0] != 'R' || header[1] != 'S' || header[2] != 'R' || header[3] != 'C') { |
1103 | // Not normal. |
1104 | error = ERR_FILE_UNRECOGNIZED; |
1105 | f.unref(); |
1106 | return "" ; |
1107 | } |
1108 | |
1109 | bool big_endian = f->get_32(); |
1110 | f->get_32(); // use_real64 |
1111 | |
1112 | f->set_big_endian(big_endian != 0); //read big endian if saved as big endian |
1113 | |
1114 | uint32_t ver_major = f->get_32(); |
1115 | f->get_32(); // ver_minor |
1116 | uint32_t ver_fmt = f->get_32(); |
1117 | |
1118 | if (ver_fmt > FORMAT_VERSION || ver_major > VERSION_MAJOR) { |
1119 | f.unref(); |
1120 | return "" ; |
1121 | } |
1122 | |
1123 | return get_unicode_string(); |
1124 | } |
1125 | |
1126 | String ResourceLoaderBinary::recognize_script_class(Ref<FileAccess> p_f) { |
1127 | error = OK; |
1128 | |
1129 | f = p_f; |
1130 | uint8_t [4]; |
1131 | f->get_buffer(header, 4); |
1132 | if (header[0] == 'R' && header[1] == 'S' && header[2] == 'C' && header[3] == 'C') { |
1133 | // Compressed. |
1134 | Ref<FileAccessCompressed> fac; |
1135 | fac.instantiate(); |
1136 | error = fac->open_after_magic(f); |
1137 | if (error != OK) { |
1138 | f.unref(); |
1139 | return "" ; |
1140 | } |
1141 | f = fac; |
1142 | |
1143 | } else if (header[0] != 'R' || header[1] != 'S' || header[2] != 'R' || header[3] != 'C') { |
1144 | // Not normal. |
1145 | error = ERR_FILE_UNRECOGNIZED; |
1146 | f.unref(); |
1147 | return "" ; |
1148 | } |
1149 | |
1150 | bool big_endian = f->get_32(); |
1151 | f->get_32(); // use_real64 |
1152 | |
1153 | f->set_big_endian(big_endian != 0); //read big endian if saved as big endian |
1154 | |
1155 | uint32_t ver_major = f->get_32(); |
1156 | f->get_32(); // ver_minor |
1157 | uint32_t ver_fmt = f->get_32(); |
1158 | |
1159 | if (ver_fmt > FORMAT_VERSION || ver_major > VERSION_MAJOR) { |
1160 | f.unref(); |
1161 | return "" ; |
1162 | } |
1163 | |
1164 | get_unicode_string(); // type |
1165 | |
1166 | f->get_64(); // Metadata offset |
1167 | uint32_t flags = f->get_32(); |
1168 | f->get_64(); // UID |
1169 | |
1170 | if (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_HAS_SCRIPT_CLASS) { |
1171 | return get_unicode_string(); |
1172 | } else { |
1173 | return String(); |
1174 | } |
1175 | } |
1176 | |
1177 | Ref<Resource> ResourceFormatLoaderBinary::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) { |
1178 | if (r_error) { |
1179 | *r_error = ERR_FILE_CANT_OPEN; |
1180 | } |
1181 | |
1182 | Error err; |
1183 | Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ, &err); |
1184 | |
1185 | ERR_FAIL_COND_V_MSG(err != OK, Ref<Resource>(), "Cannot open file '" + p_path + "'." ); |
1186 | |
1187 | ResourceLoaderBinary loader; |
1188 | loader.cache_mode = p_cache_mode; |
1189 | loader.use_sub_threads = p_use_sub_threads; |
1190 | loader.progress = r_progress; |
1191 | String path = !p_original_path.is_empty() ? p_original_path : p_path; |
1192 | loader.local_path = ProjectSettings::get_singleton()->localize_path(path); |
1193 | loader.res_path = loader.local_path; |
1194 | loader.open(f); |
1195 | |
1196 | err = loader.load(); |
1197 | |
1198 | if (r_error) { |
1199 | *r_error = err; |
1200 | } |
1201 | |
1202 | if (err) { |
1203 | return Ref<Resource>(); |
1204 | } |
1205 | return loader.resource; |
1206 | } |
1207 | |
1208 | void ResourceFormatLoaderBinary::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const { |
1209 | if (p_type.is_empty()) { |
1210 | get_recognized_extensions(p_extensions); |
1211 | return; |
1212 | } |
1213 | |
1214 | List<String> extensions; |
1215 | ClassDB::get_extensions_for_type(p_type, &extensions); |
1216 | |
1217 | extensions.sort(); |
1218 | |
1219 | for (const String &E : extensions) { |
1220 | String ext = E.to_lower(); |
1221 | p_extensions->push_back(ext); |
1222 | } |
1223 | } |
1224 | |
1225 | void ResourceFormatLoaderBinary::get_recognized_extensions(List<String> *p_extensions) const { |
1226 | List<String> extensions; |
1227 | ClassDB::get_resource_base_extensions(&extensions); |
1228 | extensions.sort(); |
1229 | |
1230 | for (const String &E : extensions) { |
1231 | String ext = E.to_lower(); |
1232 | p_extensions->push_back(ext); |
1233 | } |
1234 | } |
1235 | |
1236 | bool ResourceFormatLoaderBinary::handles_type(const String &p_type) const { |
1237 | return true; //handles all |
1238 | } |
1239 | |
1240 | void ResourceFormatLoaderBinary::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) { |
1241 | Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ); |
1242 | ERR_FAIL_COND_MSG(f.is_null(), "Cannot open file '" + p_path + "'." ); |
1243 | |
1244 | ResourceLoaderBinary loader; |
1245 | loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path); |
1246 | loader.res_path = loader.local_path; |
1247 | loader.get_dependencies(f, p_dependencies, p_add_types); |
1248 | } |
1249 | |
1250 | Error ResourceFormatLoaderBinary::rename_dependencies(const String &p_path, const HashMap<String, String> &p_map) { |
1251 | Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ); |
1252 | ERR_FAIL_COND_V_MSG(f.is_null(), ERR_CANT_OPEN, "Cannot open file '" + p_path + "'." ); |
1253 | |
1254 | Ref<FileAccess> fw; |
1255 | |
1256 | String local_path = p_path.get_base_dir(); |
1257 | |
1258 | uint8_t [4]; |
1259 | f->get_buffer(header, 4); |
1260 | if (header[0] == 'R' && header[1] == 'S' && header[2] == 'C' && header[3] == 'C') { |
1261 | // Compressed. |
1262 | Ref<FileAccessCompressed> fac; |
1263 | fac.instantiate(); |
1264 | Error err = fac->open_after_magic(f); |
1265 | ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot open file '" + p_path + "'." ); |
1266 | f = fac; |
1267 | |
1268 | Ref<FileAccessCompressed> facw; |
1269 | facw.instantiate(); |
1270 | facw->configure("RSCC" ); |
1271 | err = facw->open_internal(p_path + ".depren" , FileAccess::WRITE); |
1272 | ERR_FAIL_COND_V_MSG(err, ERR_FILE_CORRUPT, "Cannot create file '" + p_path + ".depren'." ); |
1273 | |
1274 | fw = facw; |
1275 | |
1276 | } else if (header[0] != 'R' || header[1] != 'S' || header[2] != 'R' || header[3] != 'C') { |
1277 | // Not normal. |
1278 | ERR_FAIL_V_MSG(ERR_FILE_UNRECOGNIZED, "Unrecognized binary resource file '" + local_path + "'." ); |
1279 | } else { |
1280 | fw = FileAccess::open(p_path + ".depren" , FileAccess::WRITE); |
1281 | ERR_FAIL_COND_V_MSG(fw.is_null(), ERR_CANT_CREATE, "Cannot create file '" + p_path + ".depren'." ); |
1282 | |
1283 | uint8_t magic[4] = { 'R', 'S', 'R', 'C' }; |
1284 | fw->store_buffer(magic, 4); |
1285 | } |
1286 | |
1287 | bool big_endian = f->get_32(); |
1288 | bool use_real64 = f->get_32(); |
1289 | |
1290 | f->set_big_endian(big_endian != 0); //read big endian if saved as big endian |
1291 | #ifdef BIG_ENDIAN_ENABLED |
1292 | fw->store_32(!big_endian); |
1293 | #else |
1294 | fw->store_32(big_endian); |
1295 | #endif |
1296 | fw->set_big_endian(big_endian != 0); |
1297 | fw->store_32(use_real64); //use real64 |
1298 | |
1299 | uint32_t ver_major = f->get_32(); |
1300 | uint32_t ver_minor = f->get_32(); |
1301 | uint32_t ver_format = f->get_32(); |
1302 | |
1303 | if (ver_format < FORMAT_VERSION_CAN_RENAME_DEPS) { |
1304 | fw.unref(); |
1305 | |
1306 | { |
1307 | Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); |
1308 | da->remove(p_path + ".depren" ); |
1309 | } |
1310 | |
1311 | // Use the old approach. |
1312 | |
1313 | WARN_PRINT("This file is old, so it can't refactor dependencies, opening and resaving '" + p_path + "'." ); |
1314 | |
1315 | Error err; |
1316 | f = FileAccess::open(p_path, FileAccess::READ, &err); |
1317 | |
1318 | ERR_FAIL_COND_V_MSG(err != OK, ERR_FILE_CANT_OPEN, "Cannot open file '" + p_path + "'." ); |
1319 | |
1320 | ResourceLoaderBinary loader; |
1321 | loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path); |
1322 | loader.res_path = loader.local_path; |
1323 | loader.remaps = p_map; |
1324 | loader.open(f); |
1325 | |
1326 | err = loader.load(); |
1327 | |
1328 | ERR_FAIL_COND_V(err != ERR_FILE_EOF, ERR_FILE_CORRUPT); |
1329 | Ref<Resource> res = loader.get_resource(); |
1330 | ERR_FAIL_COND_V(!res.is_valid(), ERR_FILE_CORRUPT); |
1331 | |
1332 | return ResourceFormatSaverBinary::singleton->save(res, p_path); |
1333 | } |
1334 | |
1335 | if (ver_format > FORMAT_VERSION || ver_major > VERSION_MAJOR) { |
1336 | ERR_FAIL_V_MSG(ERR_FILE_UNRECOGNIZED, |
1337 | vformat("File '%s' can't be loaded, as it uses a format version (%d) or engine version (%d.%d) which are not supported by your engine version (%s)." , |
1338 | local_path, ver_format, ver_major, ver_minor, VERSION_BRANCH)); |
1339 | } |
1340 | |
1341 | // Since we're not actually converting the file contents, leave the version |
1342 | // numbers in the file untouched. |
1343 | fw->store_32(ver_major); |
1344 | fw->store_32(ver_minor); |
1345 | fw->store_32(ver_format); |
1346 | |
1347 | save_ustring(fw, get_ustring(f)); //type |
1348 | |
1349 | uint64_t md_ofs = f->get_position(); |
1350 | uint64_t importmd_ofs = f->get_64(); |
1351 | fw->store_64(0); //metadata offset |
1352 | |
1353 | uint32_t flags = f->get_32(); |
1354 | bool using_uids = (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_UIDS); |
1355 | uint64_t uid_data = f->get_64(); |
1356 | |
1357 | fw->store_32(flags); |
1358 | fw->store_64(uid_data); |
1359 | if (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_HAS_SCRIPT_CLASS) { |
1360 | save_ustring(fw, get_ustring(f)); |
1361 | } |
1362 | |
1363 | for (int i = 0; i < ResourceFormatSaverBinaryInstance::RESERVED_FIELDS; i++) { |
1364 | fw->store_32(0); // reserved |
1365 | f->get_32(); |
1366 | } |
1367 | |
1368 | //string table |
1369 | uint32_t string_table_size = f->get_32(); |
1370 | |
1371 | fw->store_32(string_table_size); |
1372 | |
1373 | for (uint32_t i = 0; i < string_table_size; i++) { |
1374 | String s = get_ustring(f); |
1375 | save_ustring(fw, s); |
1376 | } |
1377 | |
1378 | //external resources |
1379 | uint32_t ext_resources_size = f->get_32(); |
1380 | fw->store_32(ext_resources_size); |
1381 | for (uint32_t i = 0; i < ext_resources_size; i++) { |
1382 | String type = get_ustring(f); |
1383 | String path = get_ustring(f); |
1384 | |
1385 | if (using_uids) { |
1386 | ResourceUID::ID uid = f->get_64(); |
1387 | if (uid != ResourceUID::INVALID_ID) { |
1388 | if (ResourceUID::get_singleton()->has_id(uid)) { |
1389 | // If a UID is found and the path is valid, it will be used, otherwise, it falls back to the path. |
1390 | path = ResourceUID::get_singleton()->get_id_path(uid); |
1391 | } |
1392 | } |
1393 | } |
1394 | |
1395 | bool relative = false; |
1396 | if (!path.begins_with("res://" )) { |
1397 | path = local_path.path_join(path).simplify_path(); |
1398 | relative = true; |
1399 | } |
1400 | |
1401 | if (p_map.has(path)) { |
1402 | String np = p_map[path]; |
1403 | path = np; |
1404 | } |
1405 | |
1406 | String full_path = path; |
1407 | |
1408 | if (relative) { |
1409 | //restore relative |
1410 | path = local_path.path_to_file(path); |
1411 | } |
1412 | |
1413 | save_ustring(fw, type); |
1414 | save_ustring(fw, path); |
1415 | |
1416 | if (using_uids) { |
1417 | ResourceUID::ID uid = ResourceSaver::get_resource_id_for_path(full_path); |
1418 | fw->store_64(uid); |
1419 | } |
1420 | } |
1421 | |
1422 | int64_t size_diff = (int64_t)fw->get_position() - (int64_t)f->get_position(); |
1423 | |
1424 | //internal resources |
1425 | uint32_t int_resources_size = f->get_32(); |
1426 | fw->store_32(int_resources_size); |
1427 | |
1428 | for (uint32_t i = 0; i < int_resources_size; i++) { |
1429 | String path = get_ustring(f); |
1430 | uint64_t offset = f->get_64(); |
1431 | save_ustring(fw, path); |
1432 | fw->store_64(offset + size_diff); |
1433 | } |
1434 | |
1435 | //rest of file |
1436 | uint8_t b = f->get_8(); |
1437 | while (!f->eof_reached()) { |
1438 | fw->store_8(b); |
1439 | b = f->get_8(); |
1440 | } |
1441 | f.unref(); |
1442 | |
1443 | bool all_ok = fw->get_error() == OK; |
1444 | |
1445 | fw->seek(md_ofs); |
1446 | fw->store_64(importmd_ofs + size_diff); |
1447 | |
1448 | if (!all_ok) { |
1449 | return ERR_CANT_CREATE; |
1450 | } |
1451 | |
1452 | fw.unref(); |
1453 | |
1454 | Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES); |
1455 | da->remove(p_path); |
1456 | da->rename(p_path + ".depren" , p_path); |
1457 | return OK; |
1458 | } |
1459 | |
1460 | void ResourceFormatLoaderBinary::get_classes_used(const String &p_path, HashSet<StringName> *r_classes) { |
1461 | Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ); |
1462 | ERR_FAIL_COND_MSG(f.is_null(), "Cannot open file '" + p_path + "'." ); |
1463 | |
1464 | ResourceLoaderBinary loader; |
1465 | loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path); |
1466 | loader.res_path = loader.local_path; |
1467 | loader.get_classes_used(f, r_classes); |
1468 | } |
1469 | |
1470 | String ResourceFormatLoaderBinary::get_resource_type(const String &p_path) const { |
1471 | Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ); |
1472 | if (f.is_null()) { |
1473 | return "" ; //could not read |
1474 | } |
1475 | |
1476 | ResourceLoaderBinary loader; |
1477 | loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path); |
1478 | loader.res_path = loader.local_path; |
1479 | String r = loader.recognize(f); |
1480 | return ClassDB::get_compatibility_remapped_class(r); |
1481 | } |
1482 | |
1483 | String ResourceFormatLoaderBinary::get_resource_script_class(const String &p_path) const { |
1484 | Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ); |
1485 | if (f.is_null()) { |
1486 | return "" ; //could not read |
1487 | } |
1488 | |
1489 | ResourceLoaderBinary loader; |
1490 | loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path); |
1491 | loader.res_path = loader.local_path; |
1492 | return loader.recognize_script_class(f); |
1493 | } |
1494 | |
1495 | ResourceUID::ID ResourceFormatLoaderBinary::get_resource_uid(const String &p_path) const { |
1496 | String ext = p_path.get_extension().to_lower(); |
1497 | if (!ClassDB::is_resource_extension(ext)) { |
1498 | return ResourceUID::INVALID_ID; |
1499 | } |
1500 | |
1501 | Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ); |
1502 | if (f.is_null()) { |
1503 | return ResourceUID::INVALID_ID; //could not read |
1504 | } |
1505 | |
1506 | ResourceLoaderBinary loader; |
1507 | loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path); |
1508 | loader.res_path = loader.local_path; |
1509 | loader.open(f, true); |
1510 | if (loader.error != OK) { |
1511 | return ResourceUID::INVALID_ID; //could not read |
1512 | } |
1513 | return loader.uid; |
1514 | } |
1515 | |
1516 | /////////////////////////////////////////////////////////// |
1517 | /////////////////////////////////////////////////////////// |
1518 | /////////////////////////////////////////////////////////// |
1519 | |
1520 | void ResourceFormatSaverBinaryInstance::_pad_buffer(Ref<FileAccess> f, int p_bytes) { |
1521 | int = 4 - (p_bytes % 4); |
1522 | if (extra < 4) { |
1523 | for (int i = 0; i < extra; i++) { |
1524 | f->store_8(0); //pad to 32 |
1525 | } |
1526 | } |
1527 | } |
1528 | |
1529 | void ResourceFormatSaverBinaryInstance::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) { |
1530 | switch (p_property.get_type()) { |
1531 | case Variant::NIL: { |
1532 | f->store_32(VARIANT_NIL); |
1533 | // don't store anything |
1534 | } break; |
1535 | case Variant::BOOL: { |
1536 | f->store_32(VARIANT_BOOL); |
1537 | bool val = p_property; |
1538 | f->store_32(val); |
1539 | } break; |
1540 | case Variant::INT: { |
1541 | int64_t val = p_property; |
1542 | if (val > 0x7FFFFFFF || val < -(int64_t)0x80000000) { |
1543 | f->store_32(VARIANT_INT64); |
1544 | f->store_64(val); |
1545 | |
1546 | } else { |
1547 | f->store_32(VARIANT_INT); |
1548 | f->store_32(int32_t(p_property)); |
1549 | } |
1550 | |
1551 | } break; |
1552 | case Variant::FLOAT: { |
1553 | double d = p_property; |
1554 | float fl = d; |
1555 | if (double(fl) != d) { |
1556 | f->store_32(VARIANT_DOUBLE); |
1557 | f->store_double(d); |
1558 | } else { |
1559 | f->store_32(VARIANT_FLOAT); |
1560 | f->store_real(fl); |
1561 | } |
1562 | |
1563 | } break; |
1564 | case Variant::STRING: { |
1565 | f->store_32(VARIANT_STRING); |
1566 | String val = p_property; |
1567 | save_unicode_string(f, val); |
1568 | |
1569 | } break; |
1570 | case Variant::VECTOR2: { |
1571 | f->store_32(VARIANT_VECTOR2); |
1572 | Vector2 val = p_property; |
1573 | f->store_real(val.x); |
1574 | f->store_real(val.y); |
1575 | |
1576 | } break; |
1577 | case Variant::VECTOR2I: { |
1578 | f->store_32(VARIANT_VECTOR2I); |
1579 | Vector2i val = p_property; |
1580 | f->store_32(val.x); |
1581 | f->store_32(val.y); |
1582 | |
1583 | } break; |
1584 | case Variant::RECT2: { |
1585 | f->store_32(VARIANT_RECT2); |
1586 | Rect2 val = p_property; |
1587 | f->store_real(val.position.x); |
1588 | f->store_real(val.position.y); |
1589 | f->store_real(val.size.x); |
1590 | f->store_real(val.size.y); |
1591 | |
1592 | } break; |
1593 | case Variant::RECT2I: { |
1594 | f->store_32(VARIANT_RECT2I); |
1595 | Rect2i val = p_property; |
1596 | f->store_32(val.position.x); |
1597 | f->store_32(val.position.y); |
1598 | f->store_32(val.size.x); |
1599 | f->store_32(val.size.y); |
1600 | |
1601 | } break; |
1602 | case Variant::VECTOR3: { |
1603 | f->store_32(VARIANT_VECTOR3); |
1604 | Vector3 val = p_property; |
1605 | f->store_real(val.x); |
1606 | f->store_real(val.y); |
1607 | f->store_real(val.z); |
1608 | |
1609 | } break; |
1610 | case Variant::VECTOR3I: { |
1611 | f->store_32(VARIANT_VECTOR3I); |
1612 | Vector3i val = p_property; |
1613 | f->store_32(val.x); |
1614 | f->store_32(val.y); |
1615 | f->store_32(val.z); |
1616 | |
1617 | } break; |
1618 | case Variant::VECTOR4: { |
1619 | f->store_32(VARIANT_VECTOR4); |
1620 | Vector4 val = p_property; |
1621 | f->store_real(val.x); |
1622 | f->store_real(val.y); |
1623 | f->store_real(val.z); |
1624 | f->store_real(val.w); |
1625 | |
1626 | } break; |
1627 | case Variant::VECTOR4I: { |
1628 | f->store_32(VARIANT_VECTOR4I); |
1629 | Vector4i val = p_property; |
1630 | f->store_32(val.x); |
1631 | f->store_32(val.y); |
1632 | f->store_32(val.z); |
1633 | f->store_32(val.w); |
1634 | |
1635 | } break; |
1636 | case Variant::PLANE: { |
1637 | f->store_32(VARIANT_PLANE); |
1638 | Plane val = p_property; |
1639 | f->store_real(val.normal.x); |
1640 | f->store_real(val.normal.y); |
1641 | f->store_real(val.normal.z); |
1642 | f->store_real(val.d); |
1643 | |
1644 | } break; |
1645 | case Variant::QUATERNION: { |
1646 | f->store_32(VARIANT_QUATERNION); |
1647 | Quaternion val = p_property; |
1648 | f->store_real(val.x); |
1649 | f->store_real(val.y); |
1650 | f->store_real(val.z); |
1651 | f->store_real(val.w); |
1652 | |
1653 | } break; |
1654 | case Variant::AABB: { |
1655 | f->store_32(VARIANT_AABB); |
1656 | AABB val = p_property; |
1657 | f->store_real(val.position.x); |
1658 | f->store_real(val.position.y); |
1659 | f->store_real(val.position.z); |
1660 | f->store_real(val.size.x); |
1661 | f->store_real(val.size.y); |
1662 | f->store_real(val.size.z); |
1663 | |
1664 | } break; |
1665 | case Variant::TRANSFORM2D: { |
1666 | f->store_32(VARIANT_TRANSFORM2D); |
1667 | Transform2D val = p_property; |
1668 | f->store_real(val.columns[0].x); |
1669 | f->store_real(val.columns[0].y); |
1670 | f->store_real(val.columns[1].x); |
1671 | f->store_real(val.columns[1].y); |
1672 | f->store_real(val.columns[2].x); |
1673 | f->store_real(val.columns[2].y); |
1674 | |
1675 | } break; |
1676 | case Variant::BASIS: { |
1677 | f->store_32(VARIANT_BASIS); |
1678 | Basis val = p_property; |
1679 | f->store_real(val.rows[0].x); |
1680 | f->store_real(val.rows[0].y); |
1681 | f->store_real(val.rows[0].z); |
1682 | f->store_real(val.rows[1].x); |
1683 | f->store_real(val.rows[1].y); |
1684 | f->store_real(val.rows[1].z); |
1685 | f->store_real(val.rows[2].x); |
1686 | f->store_real(val.rows[2].y); |
1687 | f->store_real(val.rows[2].z); |
1688 | |
1689 | } break; |
1690 | case Variant::TRANSFORM3D: { |
1691 | f->store_32(VARIANT_TRANSFORM3D); |
1692 | Transform3D val = p_property; |
1693 | f->store_real(val.basis.rows[0].x); |
1694 | f->store_real(val.basis.rows[0].y); |
1695 | f->store_real(val.basis.rows[0].z); |
1696 | f->store_real(val.basis.rows[1].x); |
1697 | f->store_real(val.basis.rows[1].y); |
1698 | f->store_real(val.basis.rows[1].z); |
1699 | f->store_real(val.basis.rows[2].x); |
1700 | f->store_real(val.basis.rows[2].y); |
1701 | f->store_real(val.basis.rows[2].z); |
1702 | f->store_real(val.origin.x); |
1703 | f->store_real(val.origin.y); |
1704 | f->store_real(val.origin.z); |
1705 | |
1706 | } break; |
1707 | case Variant::PROJECTION: { |
1708 | f->store_32(VARIANT_PROJECTION); |
1709 | Projection val = p_property; |
1710 | f->store_real(val.columns[0].x); |
1711 | f->store_real(val.columns[0].y); |
1712 | f->store_real(val.columns[0].z); |
1713 | f->store_real(val.columns[0].w); |
1714 | f->store_real(val.columns[1].x); |
1715 | f->store_real(val.columns[1].y); |
1716 | f->store_real(val.columns[1].z); |
1717 | f->store_real(val.columns[1].w); |
1718 | f->store_real(val.columns[2].x); |
1719 | f->store_real(val.columns[2].y); |
1720 | f->store_real(val.columns[2].z); |
1721 | f->store_real(val.columns[2].w); |
1722 | f->store_real(val.columns[3].x); |
1723 | f->store_real(val.columns[3].y); |
1724 | f->store_real(val.columns[3].z); |
1725 | f->store_real(val.columns[3].w); |
1726 | |
1727 | } break; |
1728 | case Variant::COLOR: { |
1729 | f->store_32(VARIANT_COLOR); |
1730 | Color val = p_property; |
1731 | // Color are always floats |
1732 | f->store_float(val.r); |
1733 | f->store_float(val.g); |
1734 | f->store_float(val.b); |
1735 | f->store_float(val.a); |
1736 | |
1737 | } break; |
1738 | case Variant::STRING_NAME: { |
1739 | f->store_32(VARIANT_STRING_NAME); |
1740 | String val = p_property; |
1741 | save_unicode_string(f, val); |
1742 | |
1743 | } break; |
1744 | |
1745 | case Variant::NODE_PATH: { |
1746 | f->store_32(VARIANT_NODE_PATH); |
1747 | NodePath np = p_property; |
1748 | f->store_16(np.get_name_count()); |
1749 | uint16_t snc = np.get_subname_count(); |
1750 | if (np.is_absolute()) { |
1751 | snc |= 0x8000; |
1752 | } |
1753 | f->store_16(snc); |
1754 | for (int i = 0; i < np.get_name_count(); i++) { |
1755 | if (string_map.has(np.get_name(i))) { |
1756 | f->store_32(string_map[np.get_name(i)]); |
1757 | } else { |
1758 | save_unicode_string(f, np.get_name(i), true); |
1759 | } |
1760 | } |
1761 | for (int i = 0; i < np.get_subname_count(); i++) { |
1762 | if (string_map.has(np.get_subname(i))) { |
1763 | f->store_32(string_map[np.get_subname(i)]); |
1764 | } else { |
1765 | save_unicode_string(f, np.get_subname(i), true); |
1766 | } |
1767 | } |
1768 | |
1769 | } break; |
1770 | case Variant::RID: { |
1771 | f->store_32(VARIANT_RID); |
1772 | WARN_PRINT("Can't save RIDs." ); |
1773 | RID val = p_property; |
1774 | f->store_32(val.get_id()); |
1775 | } break; |
1776 | case Variant::OBJECT: { |
1777 | f->store_32(VARIANT_OBJECT); |
1778 | Ref<Resource> res = p_property; |
1779 | if (res.is_null() || res->get_meta(SNAME("_skip_save_" ), false)) { |
1780 | f->store_32(OBJECT_EMPTY); |
1781 | return; // Don't save it. |
1782 | } |
1783 | |
1784 | if (!res->is_built_in()) { |
1785 | f->store_32(OBJECT_EXTERNAL_RESOURCE_INDEX); |
1786 | f->store_32(external_resources[res]); |
1787 | } else { |
1788 | if (!resource_map.has(res)) { |
1789 | f->store_32(OBJECT_EMPTY); |
1790 | ERR_FAIL_MSG("Resource was not pre cached for the resource section, most likely due to circular reference." ); |
1791 | } |
1792 | |
1793 | f->store_32(OBJECT_INTERNAL_RESOURCE); |
1794 | f->store_32(resource_map[res]); |
1795 | //internal resource |
1796 | } |
1797 | |
1798 | } break; |
1799 | case Variant::CALLABLE: { |
1800 | f->store_32(VARIANT_CALLABLE); |
1801 | WARN_PRINT("Can't save Callables." ); |
1802 | } break; |
1803 | case Variant::SIGNAL: { |
1804 | f->store_32(VARIANT_SIGNAL); |
1805 | WARN_PRINT("Can't save Signals." ); |
1806 | } break; |
1807 | |
1808 | case Variant::DICTIONARY: { |
1809 | f->store_32(VARIANT_DICTIONARY); |
1810 | Dictionary d = p_property; |
1811 | f->store_32(uint32_t(d.size())); |
1812 | |
1813 | List<Variant> keys; |
1814 | d.get_key_list(&keys); |
1815 | |
1816 | for (const Variant &E : keys) { |
1817 | write_variant(f, E, resource_map, external_resources, string_map); |
1818 | write_variant(f, d[E], resource_map, external_resources, string_map); |
1819 | } |
1820 | |
1821 | } break; |
1822 | case Variant::ARRAY: { |
1823 | f->store_32(VARIANT_ARRAY); |
1824 | Array a = p_property; |
1825 | f->store_32(uint32_t(a.size())); |
1826 | for (int i = 0; i < a.size(); i++) { |
1827 | write_variant(f, a[i], resource_map, external_resources, string_map); |
1828 | } |
1829 | |
1830 | } break; |
1831 | case Variant::PACKED_BYTE_ARRAY: { |
1832 | f->store_32(VARIANT_PACKED_BYTE_ARRAY); |
1833 | Vector<uint8_t> arr = p_property; |
1834 | int len = arr.size(); |
1835 | f->store_32(len); |
1836 | const uint8_t *r = arr.ptr(); |
1837 | f->store_buffer(r, len); |
1838 | _pad_buffer(f, len); |
1839 | |
1840 | } break; |
1841 | case Variant::PACKED_INT32_ARRAY: { |
1842 | f->store_32(VARIANT_PACKED_INT32_ARRAY); |
1843 | Vector<int32_t> arr = p_property; |
1844 | int len = arr.size(); |
1845 | f->store_32(len); |
1846 | const int32_t *r = arr.ptr(); |
1847 | for (int i = 0; i < len; i++) { |
1848 | f->store_32(r[i]); |
1849 | } |
1850 | |
1851 | } break; |
1852 | case Variant::PACKED_INT64_ARRAY: { |
1853 | f->store_32(VARIANT_PACKED_INT64_ARRAY); |
1854 | Vector<int64_t> arr = p_property; |
1855 | int len = arr.size(); |
1856 | f->store_32(len); |
1857 | const int64_t *r = arr.ptr(); |
1858 | for (int i = 0; i < len; i++) { |
1859 | f->store_64(r[i]); |
1860 | } |
1861 | |
1862 | } break; |
1863 | case Variant::PACKED_FLOAT32_ARRAY: { |
1864 | f->store_32(VARIANT_PACKED_FLOAT32_ARRAY); |
1865 | Vector<float> arr = p_property; |
1866 | int len = arr.size(); |
1867 | f->store_32(len); |
1868 | const float *r = arr.ptr(); |
1869 | for (int i = 0; i < len; i++) { |
1870 | f->store_float(r[i]); |
1871 | } |
1872 | |
1873 | } break; |
1874 | case Variant::PACKED_FLOAT64_ARRAY: { |
1875 | f->store_32(VARIANT_PACKED_FLOAT64_ARRAY); |
1876 | Vector<double> arr = p_property; |
1877 | int len = arr.size(); |
1878 | f->store_32(len); |
1879 | const double *r = arr.ptr(); |
1880 | for (int i = 0; i < len; i++) { |
1881 | f->store_double(r[i]); |
1882 | } |
1883 | |
1884 | } break; |
1885 | case Variant::PACKED_STRING_ARRAY: { |
1886 | f->store_32(VARIANT_PACKED_STRING_ARRAY); |
1887 | Vector<String> arr = p_property; |
1888 | int len = arr.size(); |
1889 | f->store_32(len); |
1890 | const String *r = arr.ptr(); |
1891 | for (int i = 0; i < len; i++) { |
1892 | save_unicode_string(f, r[i]); |
1893 | } |
1894 | |
1895 | } break; |
1896 | case Variant::PACKED_VECTOR3_ARRAY: { |
1897 | f->store_32(VARIANT_PACKED_VECTOR3_ARRAY); |
1898 | Vector<Vector3> arr = p_property; |
1899 | int len = arr.size(); |
1900 | f->store_32(len); |
1901 | const Vector3 *r = arr.ptr(); |
1902 | for (int i = 0; i < len; i++) { |
1903 | f->store_real(r[i].x); |
1904 | f->store_real(r[i].y); |
1905 | f->store_real(r[i].z); |
1906 | } |
1907 | |
1908 | } break; |
1909 | case Variant::PACKED_VECTOR2_ARRAY: { |
1910 | f->store_32(VARIANT_PACKED_VECTOR2_ARRAY); |
1911 | Vector<Vector2> arr = p_property; |
1912 | int len = arr.size(); |
1913 | f->store_32(len); |
1914 | const Vector2 *r = arr.ptr(); |
1915 | for (int i = 0; i < len; i++) { |
1916 | f->store_real(r[i].x); |
1917 | f->store_real(r[i].y); |
1918 | } |
1919 | |
1920 | } break; |
1921 | case Variant::PACKED_COLOR_ARRAY: { |
1922 | f->store_32(VARIANT_PACKED_COLOR_ARRAY); |
1923 | Vector<Color> arr = p_property; |
1924 | int len = arr.size(); |
1925 | f->store_32(len); |
1926 | const Color *r = arr.ptr(); |
1927 | for (int i = 0; i < len; i++) { |
1928 | f->store_float(r[i].r); |
1929 | f->store_float(r[i].g); |
1930 | f->store_float(r[i].b); |
1931 | f->store_float(r[i].a); |
1932 | } |
1933 | |
1934 | } break; |
1935 | default: { |
1936 | ERR_FAIL_MSG("Invalid variant." ); |
1937 | } |
1938 | } |
1939 | } |
1940 | |
1941 | void ResourceFormatSaverBinaryInstance::_find_resources(const Variant &p_variant, bool p_main) { |
1942 | switch (p_variant.get_type()) { |
1943 | case Variant::OBJECT: { |
1944 | Ref<Resource> res = p_variant; |
1945 | |
1946 | if (res.is_null() || external_resources.has(res) || res->get_meta(SNAME("_skip_save_" ), false)) { |
1947 | return; |
1948 | } |
1949 | |
1950 | if (!p_main && (!bundle_resources) && !res->is_built_in()) { |
1951 | if (res->get_path() == path) { |
1952 | ERR_PRINT("Circular reference to resource being saved found: '" + local_path + "' will be null next time it's loaded." ); |
1953 | return; |
1954 | } |
1955 | int idx = external_resources.size(); |
1956 | external_resources[res] = idx; |
1957 | return; |
1958 | } |
1959 | |
1960 | if (resource_set.has(res)) { |
1961 | return; |
1962 | } |
1963 | |
1964 | resource_set.insert(res); |
1965 | |
1966 | List<PropertyInfo> property_list; |
1967 | |
1968 | res->get_property_list(&property_list); |
1969 | |
1970 | for (const PropertyInfo &E : property_list) { |
1971 | if (E.usage & PROPERTY_USAGE_STORAGE) { |
1972 | Variant value = res->get(E.name); |
1973 | if (E.usage & PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT) { |
1974 | NonPersistentKey npk; |
1975 | npk.base = res; |
1976 | npk.property = E.name; |
1977 | non_persistent_map[npk] = value; |
1978 | |
1979 | Ref<Resource> sres = value; |
1980 | if (sres.is_valid()) { |
1981 | resource_set.insert(sres); |
1982 | saved_resources.push_back(sres); |
1983 | } else { |
1984 | _find_resources(value); |
1985 | } |
1986 | } else { |
1987 | _find_resources(value); |
1988 | } |
1989 | } |
1990 | } |
1991 | |
1992 | saved_resources.push_back(res); |
1993 | |
1994 | } break; |
1995 | |
1996 | case Variant::ARRAY: { |
1997 | Array varray = p_variant; |
1998 | int len = varray.size(); |
1999 | for (int i = 0; i < len; i++) { |
2000 | const Variant &v = varray.get(i); |
2001 | _find_resources(v); |
2002 | } |
2003 | |
2004 | } break; |
2005 | |
2006 | case Variant::DICTIONARY: { |
2007 | Dictionary d = p_variant; |
2008 | List<Variant> keys; |
2009 | d.get_key_list(&keys); |
2010 | for (const Variant &E : keys) { |
2011 | _find_resources(E); |
2012 | Variant v = d[E]; |
2013 | _find_resources(v); |
2014 | } |
2015 | } break; |
2016 | case Variant::NODE_PATH: { |
2017 | //take the chance and save node path strings |
2018 | NodePath np = p_variant; |
2019 | for (int i = 0; i < np.get_name_count(); i++) { |
2020 | get_string_index(np.get_name(i)); |
2021 | } |
2022 | for (int i = 0; i < np.get_subname_count(); i++) { |
2023 | get_string_index(np.get_subname(i)); |
2024 | } |
2025 | |
2026 | } break; |
2027 | default: { |
2028 | } |
2029 | } |
2030 | } |
2031 | |
2032 | void ResourceFormatSaverBinaryInstance::save_unicode_string(Ref<FileAccess> p_f, const String &p_string, bool p_bit_on_len) { |
2033 | CharString utf8 = p_string.utf8(); |
2034 | if (p_bit_on_len) { |
2035 | p_f->store_32((utf8.length() + 1) | 0x80000000); |
2036 | } else { |
2037 | p_f->store_32(utf8.length() + 1); |
2038 | } |
2039 | p_f->store_buffer((const uint8_t *)utf8.get_data(), utf8.length() + 1); |
2040 | } |
2041 | |
2042 | int ResourceFormatSaverBinaryInstance::get_string_index(const String &p_string) { |
2043 | StringName s = p_string; |
2044 | if (string_map.has(s)) { |
2045 | return string_map[s]; |
2046 | } |
2047 | |
2048 | string_map[s] = strings.size(); |
2049 | strings.push_back(s); |
2050 | return strings.size() - 1; |
2051 | } |
2052 | |
2053 | static String _resource_get_class(Ref<Resource> p_resource) { |
2054 | Ref<MissingResource> missing_resource = p_resource; |
2055 | if (missing_resource.is_valid()) { |
2056 | return missing_resource->get_original_class(); |
2057 | } else { |
2058 | return p_resource->get_class(); |
2059 | } |
2060 | } |
2061 | |
2062 | Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const Ref<Resource> &p_resource, uint32_t p_flags) { |
2063 | Error err; |
2064 | Ref<FileAccess> f; |
2065 | if (p_flags & ResourceSaver::FLAG_COMPRESS) { |
2066 | Ref<FileAccessCompressed> fac; |
2067 | fac.instantiate(); |
2068 | fac->configure("RSCC" ); |
2069 | f = fac; |
2070 | err = fac->open_internal(p_path, FileAccess::WRITE); |
2071 | } else { |
2072 | f = FileAccess::open(p_path, FileAccess::WRITE, &err); |
2073 | } |
2074 | |
2075 | ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot create file '" + p_path + "'." ); |
2076 | |
2077 | relative_paths = p_flags & ResourceSaver::FLAG_RELATIVE_PATHS; |
2078 | skip_editor = p_flags & ResourceSaver::FLAG_OMIT_EDITOR_PROPERTIES; |
2079 | bundle_resources = p_flags & ResourceSaver::FLAG_BUNDLE_RESOURCES; |
2080 | big_endian = p_flags & ResourceSaver::FLAG_SAVE_BIG_ENDIAN; |
2081 | takeover_paths = p_flags & ResourceSaver::FLAG_REPLACE_SUBRESOURCE_PATHS; |
2082 | |
2083 | if (!p_path.begins_with("res://" )) { |
2084 | takeover_paths = false; |
2085 | } |
2086 | |
2087 | local_path = p_path.get_base_dir(); |
2088 | path = ProjectSettings::get_singleton()->localize_path(p_path); |
2089 | |
2090 | _find_resources(p_resource, true); |
2091 | |
2092 | if (!(p_flags & ResourceSaver::FLAG_COMPRESS)) { |
2093 | //save header compressed |
2094 | static const uint8_t [4] = { 'R', 'S', 'R', 'C' }; |
2095 | f->store_buffer(header, 4); |
2096 | } |
2097 | |
2098 | if (big_endian) { |
2099 | f->store_32(1); |
2100 | f->set_big_endian(true); |
2101 | } else { |
2102 | f->store_32(0); |
2103 | } |
2104 | |
2105 | f->store_32(0); //64 bits file, false for now |
2106 | f->store_32(VERSION_MAJOR); |
2107 | f->store_32(VERSION_MINOR); |
2108 | f->store_32(FORMAT_VERSION); |
2109 | |
2110 | if (f->get_error() != OK && f->get_error() != ERR_FILE_EOF) { |
2111 | return ERR_CANT_CREATE; |
2112 | } |
2113 | |
2114 | save_unicode_string(f, _resource_get_class(p_resource)); |
2115 | f->store_64(0); //offset to import metadata |
2116 | |
2117 | String script_class; |
2118 | { |
2119 | uint32_t format_flags = FORMAT_FLAG_NAMED_SCENE_IDS | FORMAT_FLAG_UIDS; |
2120 | #ifdef REAL_T_IS_DOUBLE |
2121 | format_flags |= FORMAT_FLAG_REAL_T_IS_DOUBLE; |
2122 | #endif |
2123 | if (!p_resource->is_class("PackedScene" )) { |
2124 | Ref<Script> s = p_resource->get_script(); |
2125 | if (s.is_valid()) { |
2126 | script_class = s->get_global_name(); |
2127 | if (!script_class.is_empty()) { |
2128 | format_flags |= ResourceFormatSaverBinaryInstance::FORMAT_FLAG_HAS_SCRIPT_CLASS; |
2129 | } |
2130 | } |
2131 | } |
2132 | |
2133 | f->store_32(format_flags); |
2134 | } |
2135 | ResourceUID::ID uid = ResourceSaver::get_resource_id_for_path(p_path, true); |
2136 | f->store_64(uid); |
2137 | if (!script_class.is_empty()) { |
2138 | save_unicode_string(f, script_class); |
2139 | } |
2140 | |
2141 | for (int i = 0; i < ResourceFormatSaverBinaryInstance::RESERVED_FIELDS; i++) { |
2142 | f->store_32(0); // reserved |
2143 | } |
2144 | |
2145 | List<ResourceData> resources; |
2146 | |
2147 | Dictionary missing_resource_properties = p_resource->get_meta(META_MISSING_RESOURCES, Dictionary()); |
2148 | |
2149 | { |
2150 | for (const Ref<Resource> &E : saved_resources) { |
2151 | ResourceData &rd = resources.push_back(ResourceData())->get(); |
2152 | rd.type = _resource_get_class(E); |
2153 | |
2154 | List<PropertyInfo> property_list; |
2155 | E->get_property_list(&property_list); |
2156 | |
2157 | for (const PropertyInfo &F : property_list) { |
2158 | if (skip_editor && F.name.begins_with("__editor" )) { |
2159 | continue; |
2160 | } |
2161 | if (F.name == META_PROPERTY_MISSING_RESOURCES) { |
2162 | continue; |
2163 | } |
2164 | |
2165 | if ((F.usage & PROPERTY_USAGE_STORAGE)) { |
2166 | Property p; |
2167 | p.name_idx = get_string_index(F.name); |
2168 | |
2169 | if (F.usage & PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT) { |
2170 | NonPersistentKey npk; |
2171 | npk.base = E; |
2172 | npk.property = F.name; |
2173 | if (non_persistent_map.has(npk)) { |
2174 | p.value = non_persistent_map[npk]; |
2175 | } |
2176 | } else { |
2177 | p.value = E->get(F.name); |
2178 | } |
2179 | |
2180 | if (p.pi.type == Variant::OBJECT && missing_resource_properties.has(F.name)) { |
2181 | // Was this missing resource overridden? If so do not save the old value. |
2182 | Ref<Resource> res = p.value; |
2183 | if (res.is_null()) { |
2184 | p.value = missing_resource_properties[F.name]; |
2185 | } |
2186 | } |
2187 | |
2188 | Variant default_value = ClassDB::class_get_default_property_value(E->get_class(), F.name); |
2189 | |
2190 | if (default_value.get_type() != Variant::NIL && bool(Variant::evaluate(Variant::OP_EQUAL, p.value, default_value))) { |
2191 | continue; |
2192 | } |
2193 | |
2194 | p.pi = F; |
2195 | |
2196 | rd.properties.push_back(p); |
2197 | } |
2198 | } |
2199 | } |
2200 | } |
2201 | |
2202 | f->store_32(strings.size()); //string table size |
2203 | for (int i = 0; i < strings.size(); i++) { |
2204 | save_unicode_string(f, strings[i]); |
2205 | } |
2206 | |
2207 | // save external resource table |
2208 | f->store_32(external_resources.size()); //amount of external resources |
2209 | Vector<Ref<Resource>> save_order; |
2210 | save_order.resize(external_resources.size()); |
2211 | |
2212 | for (const KeyValue<Ref<Resource>, int> &E : external_resources) { |
2213 | save_order.write[E.value] = E.key; |
2214 | } |
2215 | |
2216 | for (int i = 0; i < save_order.size(); i++) { |
2217 | save_unicode_string(f, save_order[i]->get_save_class()); |
2218 | String res_path = save_order[i]->get_path(); |
2219 | res_path = relative_paths ? local_path.path_to_file(res_path) : res_path; |
2220 | save_unicode_string(f, res_path); |
2221 | ResourceUID::ID ruid = ResourceSaver::get_resource_id_for_path(save_order[i]->get_path(), false); |
2222 | f->store_64(ruid); |
2223 | } |
2224 | // save internal resource table |
2225 | f->store_32(saved_resources.size()); //amount of internal resources |
2226 | Vector<uint64_t> ofs_pos; |
2227 | HashSet<String> used_unique_ids; |
2228 | |
2229 | for (Ref<Resource> &r : saved_resources) { |
2230 | if (r->is_built_in()) { |
2231 | if (!r->get_scene_unique_id().is_empty()) { |
2232 | if (used_unique_ids.has(r->get_scene_unique_id())) { |
2233 | r->set_scene_unique_id("" ); |
2234 | } else { |
2235 | used_unique_ids.insert(r->get_scene_unique_id()); |
2236 | } |
2237 | } |
2238 | } |
2239 | } |
2240 | |
2241 | HashMap<Ref<Resource>, int> resource_map; |
2242 | int res_index = 0; |
2243 | for (Ref<Resource> &r : saved_resources) { |
2244 | if (r->is_built_in()) { |
2245 | if (r->get_scene_unique_id().is_empty()) { |
2246 | String new_id; |
2247 | |
2248 | while (true) { |
2249 | new_id = _resource_get_class(r) + "_" + Resource::generate_scene_unique_id(); |
2250 | if (!used_unique_ids.has(new_id)) { |
2251 | break; |
2252 | } |
2253 | } |
2254 | |
2255 | r->set_scene_unique_id(new_id); |
2256 | used_unique_ids.insert(new_id); |
2257 | } |
2258 | |
2259 | save_unicode_string(f, "local://" + r->get_scene_unique_id()); |
2260 | if (takeover_paths) { |
2261 | r->set_path(p_path + "::" + r->get_scene_unique_id(), true); |
2262 | } |
2263 | #ifdef TOOLS_ENABLED |
2264 | r->set_edited(false); |
2265 | #endif |
2266 | } else { |
2267 | save_unicode_string(f, r->get_path()); //actual external |
2268 | } |
2269 | ofs_pos.push_back(f->get_position()); |
2270 | f->store_64(0); //offset in 64 bits |
2271 | resource_map[r] = res_index++; |
2272 | } |
2273 | |
2274 | Vector<uint64_t> ofs_table; |
2275 | |
2276 | //now actually save the resources |
2277 | for (const ResourceData &rd : resources) { |
2278 | ofs_table.push_back(f->get_position()); |
2279 | save_unicode_string(f, rd.type); |
2280 | f->store_32(rd.properties.size()); |
2281 | |
2282 | for (const Property &p : rd.properties) { |
2283 | f->store_32(p.name_idx); |
2284 | write_variant(f, p.value, resource_map, external_resources, string_map, p.pi); |
2285 | } |
2286 | } |
2287 | |
2288 | for (int i = 0; i < ofs_table.size(); i++) { |
2289 | f->seek(ofs_pos[i]); |
2290 | f->store_64(ofs_table[i]); |
2291 | } |
2292 | |
2293 | f->seek_end(); |
2294 | |
2295 | f->store_buffer((const uint8_t *)"RSRC" , 4); //magic at end |
2296 | |
2297 | if (f->get_error() != OK && f->get_error() != ERR_FILE_EOF) { |
2298 | return ERR_CANT_CREATE; |
2299 | } |
2300 | |
2301 | return OK; |
2302 | } |
2303 | |
2304 | Error ResourceFormatSaverBinaryInstance::set_uid(const String &p_path, ResourceUID::ID p_uid) { |
2305 | Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ); |
2306 | ERR_FAIL_COND_V_MSG(f.is_null(), ERR_CANT_OPEN, "Cannot open file '" + p_path + "'." ); |
2307 | |
2308 | Ref<FileAccess> fw; |
2309 | |
2310 | local_path = p_path.get_base_dir(); |
2311 | |
2312 | uint8_t [4]; |
2313 | f->get_buffer(header, 4); |
2314 | if (header[0] == 'R' && header[1] == 'S' && header[2] == 'C' && header[3] == 'C') { |
2315 | // Compressed. |
2316 | Ref<FileAccessCompressed> fac; |
2317 | fac.instantiate(); |
2318 | Error err = fac->open_after_magic(f); |
2319 | ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot open file '" + p_path + "'." ); |
2320 | f = fac; |
2321 | |
2322 | Ref<FileAccessCompressed> facw; |
2323 | facw.instantiate(); |
2324 | facw->configure("RSCC" ); |
2325 | err = facw->open_internal(p_path + ".uidren" , FileAccess::WRITE); |
2326 | ERR_FAIL_COND_V_MSG(err, ERR_FILE_CORRUPT, "Cannot create file '" + p_path + ".uidren'." ); |
2327 | |
2328 | fw = facw; |
2329 | |
2330 | } else if (header[0] != 'R' || header[1] != 'S' || header[2] != 'R' || header[3] != 'C') { |
2331 | // Not a binary resource. |
2332 | return ERR_FILE_UNRECOGNIZED; |
2333 | } else { |
2334 | fw = FileAccess::open(p_path + ".uidren" , FileAccess::WRITE); |
2335 | ERR_FAIL_COND_V_MSG(fw.is_null(), ERR_CANT_CREATE, "Cannot create file '" + p_path + ".uidren'." ); |
2336 | |
2337 | uint8_t magich[4] = { 'R', 'S', 'R', 'C' }; |
2338 | fw->store_buffer(magich, 4); |
2339 | } |
2340 | |
2341 | big_endian = f->get_32(); |
2342 | bool use_real64 = f->get_32(); |
2343 | f->set_big_endian(big_endian != 0); //read big endian if saved as big endian |
2344 | #ifdef BIG_ENDIAN_ENABLED |
2345 | fw->store_32(!big_endian); |
2346 | #else |
2347 | fw->store_32(big_endian); |
2348 | #endif |
2349 | fw->set_big_endian(big_endian != 0); |
2350 | fw->store_32(use_real64); //use real64 |
2351 | |
2352 | uint32_t ver_major = f->get_32(); |
2353 | uint32_t ver_minor = f->get_32(); |
2354 | uint32_t ver_format = f->get_32(); |
2355 | |
2356 | if (ver_format < FORMAT_VERSION_CAN_RENAME_DEPS) { |
2357 | fw.unref(); |
2358 | |
2359 | { |
2360 | Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); |
2361 | da->remove(p_path + ".uidren" ); |
2362 | } |
2363 | |
2364 | // Use the old approach. |
2365 | |
2366 | WARN_PRINT("This file is old, so it does not support UIDs, opening and resaving '" + p_path + "'." ); |
2367 | return ERR_UNAVAILABLE; |
2368 | } |
2369 | |
2370 | if (ver_format > FORMAT_VERSION || ver_major > VERSION_MAJOR) { |
2371 | ERR_FAIL_V_MSG(ERR_FILE_UNRECOGNIZED, |
2372 | vformat("File '%s' can't be loaded, as it uses a format version (%d) or engine version (%d.%d) which are not supported by your engine version (%s)." , |
2373 | local_path, ver_format, ver_major, ver_minor, VERSION_BRANCH)); |
2374 | } |
2375 | |
2376 | // Since we're not actually converting the file contents, leave the version |
2377 | // numbers in the file untouched. |
2378 | fw->store_32(ver_major); |
2379 | fw->store_32(ver_minor); |
2380 | fw->store_32(ver_format); |
2381 | |
2382 | save_ustring(fw, get_ustring(f)); //type |
2383 | |
2384 | fw->store_64(f->get_64()); //metadata offset |
2385 | |
2386 | uint32_t flags = f->get_32(); |
2387 | flags |= ResourceFormatSaverBinaryInstance::FORMAT_FLAG_UIDS; |
2388 | f->get_64(); // Skip previous UID |
2389 | |
2390 | fw->store_32(flags); |
2391 | fw->store_64(p_uid); |
2392 | |
2393 | if (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_HAS_SCRIPT_CLASS) { |
2394 | save_ustring(fw, get_ustring(f)); |
2395 | } |
2396 | |
2397 | //rest of file |
2398 | uint8_t b = f->get_8(); |
2399 | while (!f->eof_reached()) { |
2400 | fw->store_8(b); |
2401 | b = f->get_8(); |
2402 | } |
2403 | |
2404 | f.unref(); |
2405 | |
2406 | bool all_ok = fw->get_error() == OK; |
2407 | |
2408 | if (!all_ok) { |
2409 | return ERR_CANT_CREATE; |
2410 | } |
2411 | |
2412 | fw.unref(); |
2413 | |
2414 | Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES); |
2415 | da->remove(p_path); |
2416 | da->rename(p_path + ".uidren" , p_path); |
2417 | return OK; |
2418 | } |
2419 | |
2420 | Error ResourceFormatSaverBinary::save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags) { |
2421 | String local_path = ProjectSettings::get_singleton()->localize_path(p_path); |
2422 | ResourceFormatSaverBinaryInstance saver; |
2423 | return saver.save(local_path, p_resource, p_flags); |
2424 | } |
2425 | |
2426 | Error ResourceFormatSaverBinary::set_uid(const String &p_path, ResourceUID::ID p_uid) { |
2427 | String local_path = ProjectSettings::get_singleton()->localize_path(p_path); |
2428 | ResourceFormatSaverBinaryInstance saver; |
2429 | return saver.set_uid(local_path, p_uid); |
2430 | } |
2431 | |
2432 | bool ResourceFormatSaverBinary::recognize(const Ref<Resource> &p_resource) const { |
2433 | return true; //all recognized |
2434 | } |
2435 | |
2436 | void ResourceFormatSaverBinary::get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const { |
2437 | String base = p_resource->get_base_extension().to_lower(); |
2438 | p_extensions->push_back(base); |
2439 | if (base != "res" ) { |
2440 | p_extensions->push_back("res" ); |
2441 | } |
2442 | } |
2443 | |
2444 | ResourceFormatSaverBinary *ResourceFormatSaverBinary::singleton = nullptr; |
2445 | |
2446 | ResourceFormatSaverBinary::ResourceFormatSaverBinary() { |
2447 | singleton = this; |
2448 | } |
2449 | |