| 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/missing_resource.h" |
| 37 | #include "core/object/script_language.h" |
| 38 | #include "core/version.h" |
| 39 | #include "scene/property_utils.h" |
| 40 | #include "scene/resources/packed_scene.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 | VARIANT_PACKED_VECTOR4_ARRAY = 53, |
| 89 | OBJECT_EMPTY = 0, |
| 90 | OBJECT_EXTERNAL_RESOURCE = 1, |
| 91 | OBJECT_INTERNAL_RESOURCE = 2, |
| 92 | OBJECT_EXTERNAL_RESOURCE_INDEX = 3, |
| 93 | // Version 2: Added 64-bit support for float and int. |
| 94 | // Version 3: Changed NodePath encoding. |
| 95 | // Version 4: New string ID for ext/subresources, breaks forward compat. |
| 96 | // Version 5: Ability to store script class in the header. |
| 97 | // Version 6: Added PackedVector4Array Variant type. |
| 98 | FORMAT_VERSION = 6, |
| 99 | FORMAT_VERSION_CAN_RENAME_DEPS = 1, |
| 100 | FORMAT_VERSION_NO_NODEPATH_PROPERTY = 3, |
| 101 | }; |
| 102 | |
| 103 | void ResourceLoaderBinary::_advance_padding(uint32_t p_len) { |
| 104 | uint32_t extra = 4 - (p_len % 4); |
| 105 | if (extra < 4) { |
| 106 | for (uint32_t i = 0; i < extra; i++) { |
| 107 | f->get_8(); //pad to 32 |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | static Error read_reals(real_t *dst, Ref<FileAccess> &f, size_t count) { |
| 113 | if (f->real_is_double) { |
| 114 | if constexpr (sizeof(real_t) == 8) { |
| 115 | // Ideal case with double-precision |
| 116 | f->get_buffer((uint8_t *)dst, count * sizeof(double)); |
| 117 | #ifdef BIG_ENDIAN_ENABLED |
| 118 | { |
| 119 | uint64_t *dst = (uint64_t *)dst; |
| 120 | for (size_t i = 0; i < count; i++) { |
| 121 | dst[i] = BSWAP64(dst[i]); |
| 122 | } |
| 123 | } |
| 124 | #endif |
| 125 | } else if constexpr (sizeof(real_t) == 4) { |
| 126 | // May be slower, but this is for compatibility. Eventually the data should be converted. |
| 127 | for (size_t i = 0; i < count; ++i) { |
| 128 | dst[i] = f->get_double(); |
| 129 | } |
| 130 | } else { |
| 131 | ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "real_t size is neither 4 nor 8!"); |
| 132 | } |
| 133 | } else { |
| 134 | if constexpr (sizeof(real_t) == 4) { |
| 135 | // Ideal case with float-precision |
| 136 | f->get_buffer((uint8_t *)dst, count * sizeof(float)); |
| 137 | #ifdef BIG_ENDIAN_ENABLED |
| 138 | { |
| 139 | uint32_t *dst = (uint32_t *)dst; |
| 140 | for (size_t i = 0; i < count; i++) { |
| 141 | dst[i] = BSWAP32(dst[i]); |
| 142 | } |
| 143 | } |
| 144 | #endif |
| 145 | } else if constexpr (sizeof(real_t) == 8) { |
| 146 | for (size_t i = 0; i < count; ++i) { |
| 147 | dst[i] = f->get_float(); |
| 148 | } |
| 149 | } else { |
| 150 | ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "real_t size is neither 4 nor 8!"); |
| 151 | } |
| 152 | } |
| 153 | return OK; |
| 154 | } |
| 155 | |
| 156 | StringName ResourceLoaderBinary::_get_string() { |
| 157 | uint32_t id = f->get_32(); |
| 158 | if (id & 0x80000000) { |
| 159 | uint32_t len = id & 0x7FFFFFFF; |
| 160 | if ((int)len > str_buf.size()) { |
| 161 | str_buf.resize(len); |
| 162 | } |
| 163 | if (len == 0) { |
| 164 | return StringName(); |
| 165 | } |
| 166 | f->get_buffer((uint8_t *)&str_buf[0], len); |
| 167 | return String::utf8(&str_buf[0], len); |
| 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(vformat("Couldn't load resource (no cache): %s.", path)); |
| 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, cache_mode_for_external); |
| 434 | |
| 435 | if (res.is_null()) { |
| 436 | WARN_PRINT(vformat("Couldn't load resource: %s.", path)); |
| 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, vformat("Can't load dependency: '%s'.", 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 | case VARIANT_PACKED_VECTOR4_ARRAY: { |
| 657 | uint32_t len = f->get_32(); |
| 658 | |
| 659 | Vector<Vector4> array; |
| 660 | array.resize(len); |
| 661 | Vector4 *w = array.ptrw(); |
| 662 | static_assert(sizeof(Vector4) == 4 * sizeof(real_t)); |
| 663 | const Error err = read_reals(reinterpret_cast<real_t *>(w), f, len * 4); |
| 664 | ERR_FAIL_COND_V(err != OK, err); |
| 665 | |
| 666 | r_v = array; |
| 667 | |
| 668 | } break; |
| 669 | default: { |
| 670 | ERR_FAIL_V(ERR_FILE_CORRUPT); |
| 671 | } break; |
| 672 | } |
| 673 | |
| 674 | return OK; //never reach anyway |
| 675 | } |
| 676 | |
| 677 | Ref<Resource> ResourceLoaderBinary::get_resource() { |
| 678 | return resource; |
| 679 | } |
| 680 | |
| 681 | Error ResourceLoaderBinary::load() { |
| 682 | if (error != OK) { |
| 683 | return error; |
| 684 | } |
| 685 | |
| 686 | for (int i = 0; i < external_resources.size(); i++) { |
| 687 | String path = external_resources[i].path; |
| 688 | |
| 689 | if (remaps.has(path)) { |
| 690 | path = remaps[path]; |
| 691 | } |
| 692 | |
| 693 | if (!path.contains("://") && path.is_relative_path()) { |
| 694 | // path is relative to file being loaded, so convert to a resource path |
| 695 | path = ProjectSettings::get_singleton()->localize_path(path.get_base_dir().path_join(external_resources[i].path)); |
| 696 | } |
| 697 | |
| 698 | 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 |
| 699 | 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, cache_mode_for_external); |
| 700 | if (external_resources[i].load_token.is_null()) { |
| 701 | if (!ResourceLoader::get_abort_on_missing_resources()) { |
| 702 | ResourceLoader::notify_dependency_error(local_path, path, external_resources[i].type); |
| 703 | } else { |
| 704 | error = ERR_FILE_MISSING_DEPENDENCIES; |
| 705 | ERR_FAIL_V_MSG(error, vformat("Can't load dependency: '%s'.", path)); |
| 706 | } |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | for (int i = 0; i < internal_resources.size(); i++) { |
| 711 | bool main = i == (internal_resources.size() - 1); |
| 712 | |
| 713 | //maybe it is loaded already |
| 714 | String path; |
| 715 | String id; |
| 716 | |
| 717 | if (!main) { |
| 718 | path = internal_resources[i].path; |
| 719 | |
| 720 | if (path.begins_with("local://")) { |
| 721 | path = path.replace_first("local://", ""); |
| 722 | id = path; |
| 723 | path = res_path + "::" + path; |
| 724 | |
| 725 | internal_resources.write[i].path = path; // Update path. |
| 726 | } |
| 727 | |
| 728 | if (cache_mode == ResourceFormatLoader::CACHE_MODE_REUSE && ResourceCache::has(path)) { |
| 729 | Ref<Resource> cached = ResourceCache::get_ref(path); |
| 730 | if (cached.is_valid()) { |
| 731 | //already loaded, don't do anything |
| 732 | error = OK; |
| 733 | internal_index_cache[path] = cached; |
| 734 | continue; |
| 735 | } |
| 736 | } |
| 737 | } else { |
| 738 | if (cache_mode != ResourceFormatLoader::CACHE_MODE_IGNORE && !ResourceCache::has(res_path)) { |
| 739 | path = res_path; |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | uint64_t offset = internal_resources[i].offset; |
| 744 | |
| 745 | f->seek(offset); |
| 746 | |
| 747 | String t = get_unicode_string(); |
| 748 | |
| 749 | Ref<Resource> res; |
| 750 | Resource *r = nullptr; |
| 751 | |
| 752 | MissingResource *missing_resource = nullptr; |
| 753 | |
| 754 | if (main) { |
| 755 | res = ResourceLoader::get_resource_ref_override(local_path); |
| 756 | r = res.ptr(); |
| 757 | } |
| 758 | if (!r) { |
| 759 | if (cache_mode == ResourceFormatLoader::CACHE_MODE_REPLACE && ResourceCache::has(path)) { |
| 760 | //use the existing one |
| 761 | Ref<Resource> cached = ResourceCache::get_ref(path); |
| 762 | if (cached->get_class() == t) { |
| 763 | cached->reset_state(); |
| 764 | res = cached; |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | if (res.is_null()) { |
| 769 | //did not replace |
| 770 | |
| 771 | Object *obj = ClassDB::instantiate(t); |
| 772 | if (!obj) { |
| 773 | if (ResourceLoader::is_creating_missing_resources_if_class_unavailable_enabled()) { |
| 774 | //create a missing resource |
| 775 | missing_resource = memnew(MissingResource); |
| 776 | missing_resource->set_original_class(t); |
| 777 | missing_resource->set_recording_properties(true); |
| 778 | obj = missing_resource; |
| 779 | } else { |
| 780 | error = ERR_FILE_CORRUPT; |
| 781 | ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, vformat("'%s': Resource of unrecognized type in file: '%s'.", local_path, t)); |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | r = Object::cast_to<Resource>(obj); |
| 786 | if (!r) { |
| 787 | String obj_class = obj->get_class(); |
| 788 | error = ERR_FILE_CORRUPT; |
| 789 | memdelete(obj); //bye |
| 790 | ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, vformat("'%s': Resource type in resource field not a resource, type is: %s.", local_path, obj_class)); |
| 791 | } |
| 792 | |
| 793 | res = Ref<Resource>(r); |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | if (r) { |
| 798 | if (!path.is_empty()) { |
| 799 | if (cache_mode != ResourceFormatLoader::CACHE_MODE_IGNORE) { |
| 800 | r->set_path(path, cache_mode == ResourceFormatLoader::CACHE_MODE_REPLACE); // If got here because the resource with same path has different type, replace it. |
| 801 | } else { |
| 802 | r->set_path_cache(path); |
| 803 | } |
| 804 | } |
| 805 | r->set_scene_unique_id(id); |
| 806 | } |
| 807 | |
| 808 | if (!main) { |
| 809 | internal_index_cache[path] = res; |
| 810 | } |
| 811 | |
| 812 | int pc = f->get_32(); |
| 813 | |
| 814 | //set properties |
| 815 | |
| 816 | Dictionary missing_resource_properties; |
| 817 | |
| 818 | for (int j = 0; j < pc; j++) { |
| 819 | StringName name = _get_string(); |
| 820 | |
| 821 | if (name == StringName()) { |
| 822 | error = ERR_FILE_CORRUPT; |
| 823 | ERR_FAIL_V(ERR_FILE_CORRUPT); |
| 824 | } |
| 825 | |
| 826 | Variant value; |
| 827 | |
| 828 | error = parse_variant(value); |
| 829 | if (error) { |
| 830 | return error; |
| 831 | } |
| 832 | |
| 833 | bool set_valid = true; |
| 834 | if (value.get_type() == Variant::OBJECT && missing_resource == nullptr && ResourceLoader::is_creating_missing_resources_if_class_unavailable_enabled()) { |
| 835 | // If the property being set is a missing resource (and the parent is not), |
| 836 | // then setting it will most likely not work. |
| 837 | // Instead, save it as metadata. |
| 838 | |
| 839 | Ref<MissingResource> mr = value; |
| 840 | if (mr.is_valid()) { |
| 841 | missing_resource_properties[name] = mr; |
| 842 | set_valid = false; |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | if (value.get_type() == Variant::ARRAY) { |
| 847 | Array set_array = value; |
| 848 | bool is_get_valid = false; |
| 849 | Variant get_value = res->get(name, &is_get_valid); |
| 850 | if (is_get_valid && get_value.get_type() == Variant::ARRAY) { |
| 851 | Array get_array = get_value; |
| 852 | if (!set_array.is_same_typed(get_array)) { |
| 853 | value = Array(set_array, get_array.get_typed_builtin(), get_array.get_typed_class_name(), get_array.get_typed_script()); |
| 854 | } |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | if (value.get_type() == Variant::DICTIONARY) { |
| 859 | Dictionary set_dict = value; |
| 860 | bool is_get_valid = false; |
| 861 | Variant get_value = res->get(name, &is_get_valid); |
| 862 | if (is_get_valid && get_value.get_type() == Variant::DICTIONARY) { |
| 863 | Dictionary get_dict = get_value; |
| 864 | if (!set_dict.is_same_typed(get_dict)) { |
| 865 | value = Dictionary(set_dict, get_dict.get_typed_key_builtin(), get_dict.get_typed_key_class_name(), get_dict.get_typed_key_script(), |
| 866 | get_dict.get_typed_value_builtin(), get_dict.get_typed_value_class_name(), get_dict.get_typed_value_script()); |
| 867 | } |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | if (set_valid) { |
| 872 | res->set(name, value); |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | if (missing_resource) { |
| 877 | missing_resource->set_recording_properties(false); |
| 878 | } |
| 879 | |
| 880 | if (!missing_resource_properties.is_empty()) { |
| 881 | res->set_meta(META_MISSING_RESOURCES, missing_resource_properties); |
| 882 | } |
| 883 | |
| 884 | #ifdef TOOLS_ENABLED |
| 885 | res->set_edited(false); |
| 886 | #endif |
| 887 | |
| 888 | if (progress) { |
| 889 | *progress = (i + 1) / float(internal_resources.size()); |
| 890 | } |
| 891 | |
| 892 | resource_cache.push_back(res); |
| 893 | |
| 894 | if (main) { |
| 895 | f.unref(); |
| 896 | resource = res; |
| 897 | resource->set_as_translation_remapped(translation_remapped); |
| 898 | error = OK; |
| 899 | return OK; |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | return ERR_FILE_EOF; |
| 904 | } |
| 905 | |
| 906 | void ResourceLoaderBinary::set_translation_remapped(bool p_remapped) { |
| 907 | translation_remapped = p_remapped; |
| 908 | } |
| 909 | |
| 910 | static void save_ustring(Ref<FileAccess> f, const String &p_string) { |
| 911 | CharString utf8 = p_string.utf8(); |
| 912 | f->store_32(uint32_t(utf8.length() + 1)); |
| 913 | f->store_buffer((const uint8_t *)utf8.get_data(), utf8.length() + 1); |
| 914 | } |
| 915 | |
| 916 | static String get_ustring(Ref<FileAccess> f) { |
| 917 | int len = f->get_32(); |
| 918 | Vector<char> str_buf; |
| 919 | str_buf.resize(len); |
| 920 | f->get_buffer((uint8_t *)&str_buf[0], len); |
| 921 | return String::utf8(&str_buf[0], len); |
| 922 | } |
| 923 | |
| 924 | String ResourceLoaderBinary::get_unicode_string() { |
| 925 | int len = f->get_32(); |
| 926 | if (len > str_buf.size()) { |
| 927 | str_buf.resize(len); |
| 928 | } |
| 929 | if (len == 0) { |
| 930 | return String(); |
| 931 | } |
| 932 | f->get_buffer((uint8_t *)&str_buf[0], len); |
| 933 | return String::utf8(&str_buf[0], len); |
| 934 | } |
| 935 | |
| 936 | void ResourceLoaderBinary::get_classes_used(Ref<FileAccess> p_f, HashSet<StringName> *p_classes) { |
| 937 | open(p_f, false, true); |
| 938 | if (error) { |
| 939 | return; |
| 940 | } |
| 941 | |
| 942 | for (const IntResource &res : internal_resources) { |
| 943 | p_f->seek(res.offset); |
| 944 | String t = get_unicode_string(); |
| 945 | if (!p_f->get_error() && t != String() && ClassDB::class_exists(t)) { |
| 946 | p_classes->insert(t); |
| 947 | } |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | void ResourceLoaderBinary::get_dependencies(Ref<FileAccess> p_f, List<String> *p_dependencies, bool p_add_types) { |
| 952 | open(p_f, false, true); |
| 953 | if (error) { |
| 954 | return; |
| 955 | } |
| 956 | |
| 957 | for (int i = 0; i < external_resources.size(); i++) { |
| 958 | String dep; |
| 959 | String fallback_path; |
| 960 | |
| 961 | if (external_resources[i].uid != ResourceUID::INVALID_ID) { |
| 962 | dep = ResourceUID::get_singleton()->id_to_text(external_resources[i].uid); |
| 963 | fallback_path = external_resources[i].path; // Used by Dependency Editor, in case uid path fails. |
| 964 | } else { |
| 965 | dep = external_resources[i].path; |
| 966 | } |
| 967 | |
| 968 | if (p_add_types && !external_resources[i].type.is_empty()) { |
| 969 | dep += "::" + external_resources[i].type; |
| 970 | } |
| 971 | if (!fallback_path.is_empty()) { |
| 972 | if (!p_add_types) { |
| 973 | dep += "::"; // Ensure that path comes third, even if there is no type. |
| 974 | } |
| 975 | dep += "::" + fallback_path; |
| 976 | } |
| 977 | |
| 978 | p_dependencies->push_back(dep); |
| 979 | } |
| 980 | } |
| 981 | |
| 982 | void ResourceLoaderBinary::open(Ref<FileAccess> p_f, bool p_no_resources, bool p_keep_uuid_paths) { |
| 983 | error = OK; |
| 984 | |
| 985 | f = p_f; |
| 986 | uint8_t header[4]; |
| 987 | f->get_buffer(header, 4); |
| 988 | if (header[0] == 'R' && header[1] == 'S' && header[2] == 'C' && header[3] == 'C') { |
| 989 | // Compressed. |
| 990 | Ref<FileAccessCompressed> fac; |
| 991 | fac.instantiate(); |
| 992 | error = fac->open_after_magic(f); |
| 993 | if (error != OK) { |
| 994 | f.unref(); |
| 995 | ERR_FAIL_MSG(vformat("Failed to open binary resource file: '%s'.", local_path)); |
| 996 | } |
| 997 | f = fac; |
| 998 | |
| 999 | } else if (header[0] != 'R' || header[1] != 'S' || header[2] != 'R' || header[3] != 'C') { |
| 1000 | // Not normal. |
| 1001 | error = ERR_FILE_UNRECOGNIZED; |
| 1002 | f.unref(); |
| 1003 | ERR_FAIL_MSG(vformat("Unrecognized binary resource file: '%s'.", local_path)); |
| 1004 | } |
| 1005 | |
| 1006 | bool big_endian = f->get_32(); |
| 1007 | bool use_real64 = f->get_32(); |
| 1008 | |
| 1009 | f->set_big_endian(big_endian != 0); //read big endian if saved as big endian |
| 1010 | |
| 1011 | uint32_t ver_major = f->get_32(); |
| 1012 | uint32_t ver_minor = f->get_32(); |
| 1013 | ver_format = f->get_32(); |
| 1014 | |
| 1015 | print_bl("big endian: " + itos(big_endian)); |
| 1016 | #ifdef BIG_ENDIAN_ENABLED |
| 1017 | print_bl("endian swap: " + itos(!big_endian)); |
| 1018 | #else |
| 1019 | print_bl("endian swap: " + itos(big_endian)); |
| 1020 | #endif |
| 1021 | print_bl("real64: " + itos(use_real64)); |
| 1022 | print_bl("major: " + itos(ver_major)); |
| 1023 | print_bl("minor: " + itos(ver_minor)); |
| 1024 | print_bl("format: " + itos(ver_format)); |
| 1025 | |
| 1026 | if (ver_format > FORMAT_VERSION || ver_major > GODOT_VERSION_MAJOR) { |
| 1027 | f.unref(); |
| 1028 | 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).", |
| 1029 | local_path, ver_format, ver_major, ver_minor, GODOT_VERSION_BRANCH)); |
| 1030 | } |
| 1031 | |
| 1032 | type = get_unicode_string(); |
| 1033 | |
| 1034 | print_bl("type: " + type); |
| 1035 | |
| 1036 | importmd_ofs = f->get_64(); |
| 1037 | uint32_t flags = f->get_32(); |
| 1038 | if (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_NAMED_SCENE_IDS) { |
| 1039 | using_named_scene_ids = true; |
| 1040 | } |
| 1041 | if (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_UIDS) { |
| 1042 | using_uids = true; |
| 1043 | } |
| 1044 | f->real_is_double = (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_REAL_T_IS_DOUBLE) != 0; |
| 1045 | |
| 1046 | if (using_uids) { |
| 1047 | uid = ResourceUID::ID(f->get_64()); |
| 1048 | } else { |
| 1049 | f->get_64(); // skip over uid field |
| 1050 | uid = ResourceUID::INVALID_ID; |
| 1051 | } |
| 1052 | |
| 1053 | if (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_HAS_SCRIPT_CLASS) { |
| 1054 | script_class = get_unicode_string(); |
| 1055 | } |
| 1056 | |
| 1057 | for (int i = 0; i < ResourceFormatSaverBinaryInstance::RESERVED_FIELDS; i++) { |
| 1058 | f->get_32(); //skip a few reserved fields |
| 1059 | } |
| 1060 | |
| 1061 | if (p_no_resources) { |
| 1062 | return; |
| 1063 | } |
| 1064 | |
| 1065 | uint32_t string_table_size = f->get_32(); |
| 1066 | string_map.resize(string_table_size); |
| 1067 | for (uint32_t i = 0; i < string_table_size; i++) { |
| 1068 | StringName s = get_unicode_string(); |
| 1069 | string_map.write[i] = s; |
| 1070 | } |
| 1071 | |
| 1072 | print_bl("strings: " + itos(string_table_size)); |
| 1073 | |
| 1074 | uint32_t ext_resources_size = f->get_32(); |
| 1075 | for (uint32_t i = 0; i < ext_resources_size; i++) { |
| 1076 | ExtResource er; |
| 1077 | er.type = get_unicode_string(); |
| 1078 | er.path = get_unicode_string(); |
| 1079 | if (using_uids) { |
| 1080 | er.uid = ResourceUID::ID(f->get_64()); |
| 1081 | if (!p_keep_uuid_paths && er.uid != ResourceUID::INVALID_ID) { |
| 1082 | if (ResourceUID::get_singleton()->has_id(er.uid)) { |
| 1083 | // If a UID is found and the path is valid, it will be used, otherwise, it falls back to the path. |
| 1084 | er.path = ResourceUID::get_singleton()->get_id_path(er.uid); |
| 1085 | } else { |
| 1086 | #ifdef TOOLS_ENABLED |
| 1087 | // Silence a warning that can happen during the initial filesystem scan due to cache being regenerated. |
| 1088 | if (ResourceLoader::get_resource_uid(res_path) != er.uid) { |
| 1089 | WARN_PRINT(vformat("'%s': In external resource #%d, invalid UID: '%s' - using text path instead: '%s'.", res_path, i, ResourceUID::get_singleton()->id_to_text(er.uid), er.path)); |
| 1090 | } |
| 1091 | #else |
| 1092 | WARN_PRINT(vformat("'%s': In external resource #%d, invalid UID: '%s' - using text path instead: '%s'.", res_path, i, ResourceUID::get_singleton()->id_to_text(er.uid), er.path)); |
| 1093 | #endif |
| 1094 | } |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | external_resources.push_back(er); |
| 1099 | } |
| 1100 | |
| 1101 | print_bl("ext resources: " + itos(ext_resources_size)); |
| 1102 | uint32_t int_resources_size = f->get_32(); |
| 1103 | |
| 1104 | for (uint32_t i = 0; i < int_resources_size; i++) { |
| 1105 | IntResource ir; |
| 1106 | ir.path = get_unicode_string(); |
| 1107 | ir.offset = f->get_64(); |
| 1108 | internal_resources.push_back(ir); |
| 1109 | } |
| 1110 | |
| 1111 | print_bl("int resources: " + itos(int_resources_size)); |
| 1112 | |
| 1113 | if (f->eof_reached()) { |
| 1114 | error = ERR_FILE_CORRUPT; |
| 1115 | f.unref(); |
| 1116 | ERR_FAIL_MSG(vformat("Premature end of file (EOF): '%s'.", local_path)); |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | String ResourceLoaderBinary::recognize(Ref<FileAccess> p_f) { |
| 1121 | error = OK; |
| 1122 | |
| 1123 | f = p_f; |
| 1124 | uint8_t header[4]; |
| 1125 | f->get_buffer(header, 4); |
| 1126 | if (header[0] == 'R' && header[1] == 'S' && header[2] == 'C' && header[3] == 'C') { |
| 1127 | // Compressed. |
| 1128 | Ref<FileAccessCompressed> fac; |
| 1129 | fac.instantiate(); |
| 1130 | error = fac->open_after_magic(f); |
| 1131 | if (error != OK) { |
| 1132 | f.unref(); |
| 1133 | return ""; |
| 1134 | } |
| 1135 | f = fac; |
| 1136 | |
| 1137 | } else if (header[0] != 'R' || header[1] != 'S' || header[2] != 'R' || header[3] != 'C') { |
| 1138 | // Not normal. |
| 1139 | error = ERR_FILE_UNRECOGNIZED; |
| 1140 | f.unref(); |
| 1141 | return ""; |
| 1142 | } |
| 1143 | |
| 1144 | bool big_endian = f->get_32(); |
| 1145 | f->get_32(); // use_real64 |
| 1146 | |
| 1147 | f->set_big_endian(big_endian != 0); //read big endian if saved as big endian |
| 1148 | |
| 1149 | uint32_t ver_major = f->get_32(); |
| 1150 | f->get_32(); // ver_minor |
| 1151 | uint32_t ver_fmt = f->get_32(); |
| 1152 | |
| 1153 | if (ver_fmt > FORMAT_VERSION || ver_major > GODOT_VERSION_MAJOR) { |
| 1154 | f.unref(); |
| 1155 | return ""; |
| 1156 | } |
| 1157 | |
| 1158 | return get_unicode_string(); |
| 1159 | } |
| 1160 | |
| 1161 | String ResourceLoaderBinary::recognize_script_class(Ref<FileAccess> p_f) { |
| 1162 | error = OK; |
| 1163 | |
| 1164 | f = p_f; |
| 1165 | uint8_t header[4]; |
| 1166 | f->get_buffer(header, 4); |
| 1167 | if (header[0] == 'R' && header[1] == 'S' && header[2] == 'C' && header[3] == 'C') { |
| 1168 | // Compressed. |
| 1169 | Ref<FileAccessCompressed> fac; |
| 1170 | fac.instantiate(); |
| 1171 | error = fac->open_after_magic(f); |
| 1172 | if (error != OK) { |
| 1173 | f.unref(); |
| 1174 | return ""; |
| 1175 | } |
| 1176 | f = fac; |
| 1177 | |
| 1178 | } else if (header[0] != 'R' || header[1] != 'S' || header[2] != 'R' || header[3] != 'C') { |
| 1179 | // Not normal. |
| 1180 | error = ERR_FILE_UNRECOGNIZED; |
| 1181 | f.unref(); |
| 1182 | return ""; |
| 1183 | } |
| 1184 | |
| 1185 | bool big_endian = f->get_32(); |
| 1186 | f->get_32(); // use_real64 |
| 1187 | |
| 1188 | f->set_big_endian(big_endian != 0); //read big endian if saved as big endian |
| 1189 | |
| 1190 | uint32_t ver_major = f->get_32(); |
| 1191 | f->get_32(); // ver_minor |
| 1192 | uint32_t ver_fmt = f->get_32(); |
| 1193 | |
| 1194 | if (ver_fmt > FORMAT_VERSION || ver_major > GODOT_VERSION_MAJOR) { |
| 1195 | f.unref(); |
| 1196 | return ""; |
| 1197 | } |
| 1198 | |
| 1199 | _ALLOW_DISCARD_ get_unicode_string(); // type |
| 1200 | |
| 1201 | f->get_64(); // Metadata offset |
| 1202 | uint32_t flags = f->get_32(); |
| 1203 | f->get_64(); // UID |
| 1204 | |
| 1205 | if (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_HAS_SCRIPT_CLASS) { |
| 1206 | return get_unicode_string(); |
| 1207 | } else { |
| 1208 | return String(); |
| 1209 | } |
| 1210 | } |
| 1211 | |
| 1212 | 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) { |
| 1213 | if (r_error) { |
| 1214 | *r_error = ERR_FILE_CANT_OPEN; |
| 1215 | } |
| 1216 | |
| 1217 | Error err; |
| 1218 | Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ, &err); |
| 1219 | |
| 1220 | ERR_FAIL_COND_V_MSG(err != OK, Ref<Resource>(), vformat("Cannot open file '%s'.", p_path)); |
| 1221 | |
| 1222 | ResourceLoaderBinary loader; |
| 1223 | switch (p_cache_mode) { |
| 1224 | case CACHE_MODE_IGNORE: |
| 1225 | case CACHE_MODE_REUSE: |
| 1226 | case CACHE_MODE_REPLACE: |
| 1227 | loader.cache_mode = p_cache_mode; |
| 1228 | loader.cache_mode_for_external = CACHE_MODE_REUSE; |
| 1229 | break; |
| 1230 | case CACHE_MODE_IGNORE_DEEP: |
| 1231 | loader.cache_mode = CACHE_MODE_IGNORE; |
| 1232 | loader.cache_mode_for_external = p_cache_mode; |
| 1233 | break; |
| 1234 | case CACHE_MODE_REPLACE_DEEP: |
| 1235 | loader.cache_mode = CACHE_MODE_REPLACE; |
| 1236 | loader.cache_mode_for_external = p_cache_mode; |
| 1237 | break; |
| 1238 | } |
| 1239 | loader.use_sub_threads = p_use_sub_threads; |
| 1240 | loader.progress = r_progress; |
| 1241 | String path = !p_original_path.is_empty() ? p_original_path : p_path; |
| 1242 | loader.local_path = ProjectSettings::get_singleton()->localize_path(path); |
| 1243 | loader.res_path = loader.local_path; |
| 1244 | loader.open(f); |
| 1245 | |
| 1246 | err = loader.load(); |
| 1247 | |
| 1248 | if (r_error) { |
| 1249 | *r_error = err; |
| 1250 | } |
| 1251 | |
| 1252 | if (err) { |
| 1253 | return Ref<Resource>(); |
| 1254 | } |
| 1255 | return loader.resource; |
| 1256 | } |
| 1257 | |
| 1258 | void ResourceFormatLoaderBinary::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const { |
| 1259 | if (p_type.is_empty()) { |
| 1260 | get_recognized_extensions(p_extensions); |
| 1261 | return; |
| 1262 | } |
| 1263 | |
| 1264 | // res files not supported for GDExtension. |
| 1265 | if (p_type == "GDExtension") { |
| 1266 | return; |
| 1267 | } |
| 1268 | |
| 1269 | List<String> extensions; |
| 1270 | ClassDB::get_extensions_for_type(p_type, &extensions); |
| 1271 | |
| 1272 | extensions.sort(); |
| 1273 | |
| 1274 | for (const String &E : extensions) { |
| 1275 | String ext = E.to_lower(); |
| 1276 | p_extensions->push_back(ext); |
| 1277 | } |
| 1278 | } |
| 1279 | |
| 1280 | void ResourceFormatLoaderBinary::get_recognized_extensions(List<String> *p_extensions) const { |
| 1281 | List<String> extensions; |
| 1282 | ClassDB::get_resource_base_extensions(&extensions); |
| 1283 | extensions.sort(); |
| 1284 | |
| 1285 | for (const String &E : extensions) { |
| 1286 | String ext = E.to_lower(); |
| 1287 | p_extensions->push_back(ext); |
| 1288 | } |
| 1289 | } |
| 1290 | |
| 1291 | bool ResourceFormatLoaderBinary::handles_type(const String &p_type) const { |
| 1292 | return true; //handles all |
| 1293 | } |
| 1294 | |
| 1295 | void ResourceFormatLoaderBinary::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) { |
| 1296 | Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ); |
| 1297 | ERR_FAIL_COND_MSG(f.is_null(), vformat("Cannot open file '%s'.", p_path)); |
| 1298 | |
| 1299 | ResourceLoaderBinary loader; |
| 1300 | loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path); |
| 1301 | loader.res_path = loader.local_path; |
| 1302 | loader.get_dependencies(f, p_dependencies, p_add_types); |
| 1303 | } |
| 1304 | |
| 1305 | Error ResourceFormatLoaderBinary::rename_dependencies(const String &p_path, const HashMap<String, String> &p_map) { |
| 1306 | Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ); |
| 1307 | ERR_FAIL_COND_V_MSG(f.is_null(), ERR_CANT_OPEN, vformat("Cannot open file '%s'.", p_path)); |
| 1308 | |
| 1309 | Ref<FileAccess> fw; |
| 1310 | |
| 1311 | String local_path = p_path.get_base_dir(); |
| 1312 | |
| 1313 | uint8_t header[4]; |
| 1314 | f->get_buffer(header, 4); |
| 1315 | if (header[0] == 'R' && header[1] == 'S' && header[2] == 'C' && header[3] == 'C') { |
| 1316 | // Compressed. |
| 1317 | Ref<FileAccessCompressed> fac; |
| 1318 | fac.instantiate(); |
| 1319 | Error err = fac->open_after_magic(f); |
| 1320 | ERR_FAIL_COND_V_MSG(err != OK, err, vformat("Cannot open file '%s'.", p_path)); |
| 1321 | f = fac; |
| 1322 | |
| 1323 | Ref<FileAccessCompressed> facw; |
| 1324 | facw.instantiate(); |
| 1325 | facw->configure("RSCC"); |
| 1326 | err = facw->open_internal(p_path + ".depren", FileAccess::WRITE); |
| 1327 | ERR_FAIL_COND_V_MSG(err, ERR_FILE_CORRUPT, vformat("Cannot create file '%s.depren'.", p_path)); |
| 1328 | |
| 1329 | fw = facw; |
| 1330 | |
| 1331 | } else if (header[0] != 'R' || header[1] != 'S' || header[2] != 'R' || header[3] != 'C') { |
| 1332 | // Not normal. |
| 1333 | ERR_FAIL_V_MSG(ERR_FILE_UNRECOGNIZED, vformat("Unrecognized binary resource file '%s'.", local_path)); |
| 1334 | } else { |
| 1335 | fw = FileAccess::open(p_path + ".depren", FileAccess::WRITE); |
| 1336 | ERR_FAIL_COND_V_MSG(fw.is_null(), ERR_CANT_CREATE, vformat("Cannot create file '%s.depren'.", p_path)); |
| 1337 | |
| 1338 | uint8_t magic[4] = { 'R', 'S', 'R', 'C' }; |
| 1339 | fw->store_buffer(magic, 4); |
| 1340 | } |
| 1341 | |
| 1342 | bool big_endian = f->get_32(); |
| 1343 | bool use_real64 = f->get_32(); |
| 1344 | |
| 1345 | f->set_big_endian(big_endian != 0); //read big endian if saved as big endian |
| 1346 | |
| 1347 | fw->store_32(big_endian); |
| 1348 | fw->store_32(use_real64); //use real64 |
| 1349 | fw->set_big_endian(big_endian != 0); |
| 1350 | |
| 1351 | uint32_t ver_major = f->get_32(); |
| 1352 | uint32_t ver_minor = f->get_32(); |
| 1353 | uint32_t ver_format = f->get_32(); |
| 1354 | |
| 1355 | if (ver_format < FORMAT_VERSION_CAN_RENAME_DEPS) { |
| 1356 | fw.unref(); |
| 1357 | |
| 1358 | { |
| 1359 | Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); |
| 1360 | da->remove(p_path + ".depren"); |
| 1361 | } |
| 1362 | |
| 1363 | // Use the old approach. |
| 1364 | |
| 1365 | WARN_PRINT(vformat("This file is old, so it can't refactor dependencies, opening and resaving '%s'.", p_path)); |
| 1366 | |
| 1367 | Error err; |
| 1368 | f = FileAccess::open(p_path, FileAccess::READ, &err); |
| 1369 | |
| 1370 | ERR_FAIL_COND_V_MSG(err != OK, ERR_FILE_CANT_OPEN, vformat("Cannot open file '%s'.", p_path)); |
| 1371 | |
| 1372 | ResourceLoaderBinary loader; |
| 1373 | loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path); |
| 1374 | loader.res_path = loader.local_path; |
| 1375 | loader.remaps = p_map; |
| 1376 | loader.open(f); |
| 1377 | |
| 1378 | err = loader.load(); |
| 1379 | |
| 1380 | ERR_FAIL_COND_V(err != ERR_FILE_EOF, ERR_FILE_CORRUPT); |
| 1381 | Ref<Resource> res = loader.get_resource(); |
| 1382 | ERR_FAIL_COND_V(res.is_null(), ERR_FILE_CORRUPT); |
| 1383 | |
| 1384 | return ResourceFormatSaverBinary::singleton->save(res, p_path); |
| 1385 | } |
| 1386 | |
| 1387 | if (ver_format > FORMAT_VERSION || ver_major > GODOT_VERSION_MAJOR) { |
| 1388 | ERR_FAIL_V_MSG(ERR_FILE_UNRECOGNIZED, |
| 1389 | 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).", |
| 1390 | local_path, ver_format, ver_major, ver_minor, GODOT_VERSION_BRANCH)); |
| 1391 | } |
| 1392 | |
| 1393 | // Since we're not actually converting the file contents, leave the version |
| 1394 | // numbers in the file untouched. |
| 1395 | fw->store_32(ver_major); |
| 1396 | fw->store_32(ver_minor); |
| 1397 | fw->store_32(ver_format); |
| 1398 | |
| 1399 | save_ustring(fw, get_ustring(f)); //type |
| 1400 | |
| 1401 | uint64_t md_ofs = f->get_position(); |
| 1402 | uint64_t importmd_ofs = f->get_64(); |
| 1403 | fw->store_64(0); //metadata offset |
| 1404 | |
| 1405 | uint32_t flags = f->get_32(); |
| 1406 | bool using_uids = (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_UIDS); |
| 1407 | uint64_t uid_data = f->get_64(); |
| 1408 | |
| 1409 | fw->store_32(flags); |
| 1410 | fw->store_64(uid_data); |
| 1411 | if (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_HAS_SCRIPT_CLASS) { |
| 1412 | save_ustring(fw, get_ustring(f)); |
| 1413 | } |
| 1414 | |
| 1415 | for (int i = 0; i < ResourceFormatSaverBinaryInstance::RESERVED_FIELDS; i++) { |
| 1416 | fw->store_32(0); // reserved |
| 1417 | f->get_32(); |
| 1418 | } |
| 1419 | |
| 1420 | //string table |
| 1421 | uint32_t string_table_size = f->get_32(); |
| 1422 | |
| 1423 | fw->store_32(string_table_size); |
| 1424 | |
| 1425 | for (uint32_t i = 0; i < string_table_size; i++) { |
| 1426 | String s = get_ustring(f); |
| 1427 | save_ustring(fw, s); |
| 1428 | } |
| 1429 | |
| 1430 | //external resources |
| 1431 | uint32_t ext_resources_size = f->get_32(); |
| 1432 | fw->store_32(ext_resources_size); |
| 1433 | for (uint32_t i = 0; i < ext_resources_size; i++) { |
| 1434 | String type = get_ustring(f); |
| 1435 | String path = get_ustring(f); |
| 1436 | |
| 1437 | if (using_uids) { |
| 1438 | ResourceUID::ID uid = f->get_64(); |
| 1439 | if (uid != ResourceUID::INVALID_ID) { |
| 1440 | if (ResourceUID::get_singleton()->has_id(uid)) { |
| 1441 | // If a UID is found and the path is valid, it will be used, otherwise, it falls back to the path. |
| 1442 | path = ResourceUID::get_singleton()->get_id_path(uid); |
| 1443 | } |
| 1444 | } |
| 1445 | } |
| 1446 | |
| 1447 | bool relative = false; |
| 1448 | if (!path.begins_with("res://")) { |
| 1449 | path = local_path.path_join(path).simplify_path(); |
| 1450 | relative = true; |
| 1451 | } |
| 1452 | |
| 1453 | if (p_map.has(path)) { |
| 1454 | String np = p_map[path]; |
| 1455 | path = np; |
| 1456 | } |
| 1457 | |
| 1458 | String full_path = path; |
| 1459 | |
| 1460 | if (relative) { |
| 1461 | //restore relative |
| 1462 | path = local_path.path_to_file(path); |
| 1463 | } |
| 1464 | |
| 1465 | save_ustring(fw, type); |
| 1466 | save_ustring(fw, path); |
| 1467 | |
| 1468 | if (using_uids) { |
| 1469 | ResourceUID::ID uid = ResourceSaver::get_resource_id_for_path(full_path); |
| 1470 | fw->store_64(uint64_t(uid)); |
| 1471 | } |
| 1472 | } |
| 1473 | |
| 1474 | int64_t size_diff = (int64_t)fw->get_position() - (int64_t)f->get_position(); |
| 1475 | |
| 1476 | //internal resources |
| 1477 | uint32_t int_resources_size = f->get_32(); |
| 1478 | fw->store_32(int_resources_size); |
| 1479 | |
| 1480 | for (uint32_t i = 0; i < int_resources_size; i++) { |
| 1481 | String path = get_ustring(f); |
| 1482 | uint64_t offset = f->get_64(); |
| 1483 | save_ustring(fw, path); |
| 1484 | fw->store_64(offset + size_diff); |
| 1485 | } |
| 1486 | |
| 1487 | //rest of file |
| 1488 | uint8_t b = f->get_8(); |
| 1489 | while (!f->eof_reached()) { |
| 1490 | fw->store_8(b); |
| 1491 | b = f->get_8(); |
| 1492 | } |
| 1493 | f.unref(); |
| 1494 | |
| 1495 | bool all_ok = fw->get_error() == OK; |
| 1496 | |
| 1497 | fw->seek(md_ofs); |
| 1498 | fw->store_64(importmd_ofs + size_diff); |
| 1499 | |
| 1500 | if (!all_ok) { |
| 1501 | return ERR_CANT_CREATE; |
| 1502 | } |
| 1503 | |
| 1504 | fw.unref(); |
| 1505 | |
| 1506 | Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES); |
| 1507 | if (da->exists(p_path + ".depren")) { |
| 1508 | da->remove(p_path); |
| 1509 | da->rename(p_path + ".depren", p_path); |
| 1510 | } |
| 1511 | return OK; |
| 1512 | } |
| 1513 | |
| 1514 | void ResourceFormatLoaderBinary::get_classes_used(const String &p_path, HashSet<StringName> *r_classes) { |
| 1515 | Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ); |
| 1516 | ERR_FAIL_COND_MSG(f.is_null(), vformat("Cannot open file '%s'.", p_path)); |
| 1517 | |
| 1518 | ResourceLoaderBinary loader; |
| 1519 | loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path); |
| 1520 | loader.res_path = loader.local_path; |
| 1521 | loader.get_classes_used(f, r_classes); |
| 1522 | |
| 1523 | if (loader.type != "PackedScene") { |
| 1524 | return; |
| 1525 | } |
| 1526 | |
| 1527 | // Fetch the nodes inside scene files. |
| 1528 | |
| 1529 | // Reopening is necessary, or errors will occur. |
| 1530 | f->reopen(p_path, FileAccess::READ); |
| 1531 | loader.open(f); |
| 1532 | ERR_FAIL_COND(loader.load() != OK); |
| 1533 | |
| 1534 | Ref<SceneState> state = Ref<PackedScene>(loader.get_resource())->get_state(); |
| 1535 | for (int i = 0; i < state->get_node_count(); i++) { |
| 1536 | const StringName node_name = state->get_node_type(i); |
| 1537 | if (ClassDB::class_exists(node_name)) { |
| 1538 | r_classes->insert(node_name); |
| 1539 | } |
| 1540 | |
| 1541 | // Fetch the values of properties in the node. |
| 1542 | for (int j = 0; j < state->get_node_property_count(i); j++) { |
| 1543 | const Variant var = state->get_node_property_value(i, j); |
| 1544 | if (var.get_type() != Variant::OBJECT) { |
| 1545 | continue; |
| 1546 | } |
| 1547 | |
| 1548 | const Object *obj = var.get_validated_object(); |
| 1549 | if (obj == nullptr) { |
| 1550 | continue; |
| 1551 | } |
| 1552 | |
| 1553 | const StringName obj_name = obj->get_class_name(); |
| 1554 | if (ClassDB::class_exists(obj_name)) { |
| 1555 | r_classes->insert(obj_name); |
| 1556 | } |
| 1557 | } |
| 1558 | } |
| 1559 | } |
| 1560 | |
| 1561 | String ResourceFormatLoaderBinary::get_resource_type(const String &p_path) const { |
| 1562 | Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ); |
| 1563 | if (f.is_null()) { |
| 1564 | return ""; //could not read |
| 1565 | } |
| 1566 | |
| 1567 | ResourceLoaderBinary loader; |
| 1568 | loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path); |
| 1569 | loader.res_path = loader.local_path; |
| 1570 | String r = loader.recognize(f); |
| 1571 | return ClassDB::get_compatibility_remapped_class(r); |
| 1572 | } |
| 1573 | |
| 1574 | String ResourceFormatLoaderBinary::get_resource_script_class(const String &p_path) const { |
| 1575 | Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ); |
| 1576 | if (f.is_null()) { |
| 1577 | return ""; //could not read |
| 1578 | } |
| 1579 | |
| 1580 | ResourceLoaderBinary loader; |
| 1581 | loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path); |
| 1582 | loader.res_path = loader.local_path; |
| 1583 | return loader.recognize_script_class(f); |
| 1584 | } |
| 1585 | |
| 1586 | ResourceUID::ID ResourceFormatLoaderBinary::get_resource_uid(const String &p_path) const { |
| 1587 | String ext = p_path.get_extension().to_lower(); |
| 1588 | if (!ClassDB::is_resource_extension(ext)) { |
| 1589 | return ResourceUID::INVALID_ID; |
| 1590 | } |
| 1591 | |
| 1592 | Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ); |
| 1593 | if (f.is_null()) { |
| 1594 | return ResourceUID::INVALID_ID; //could not read |
| 1595 | } |
| 1596 | |
| 1597 | ResourceLoaderBinary loader; |
| 1598 | loader.local_path = ProjectSettings::get_singleton()->localize_path(p_path); |
| 1599 | loader.res_path = loader.local_path; |
| 1600 | loader.open(f, true); |
| 1601 | if (loader.error != OK) { |
| 1602 | return ResourceUID::INVALID_ID; //could not read |
| 1603 | } |
| 1604 | return loader.uid; |
| 1605 | } |
| 1606 | |
| 1607 | bool ResourceFormatLoaderBinary::has_custom_uid_support() const { |
| 1608 | return true; |
| 1609 | } |
| 1610 | |
| 1611 | /////////////////////////////////////////////////////////// |
| 1612 | /////////////////////////////////////////////////////////// |
| 1613 | /////////////////////////////////////////////////////////// |
| 1614 | |
| 1615 | void ResourceFormatSaverBinaryInstance::_pad_buffer(Ref<FileAccess> f, int p_bytes) { |
| 1616 | int extra = 4 - (p_bytes % 4); |
| 1617 | if (extra < 4) { |
| 1618 | for (int i = 0; i < extra; i++) { |
| 1619 | f->store_8(0); //pad to 32 |
| 1620 | } |
| 1621 | } |
| 1622 | } |
| 1623 | |
| 1624 | 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) { |
| 1625 | switch (p_property.get_type()) { |
| 1626 | case Variant::NIL: { |
| 1627 | f->store_32(VARIANT_NIL); |
| 1628 | // don't store anything |
| 1629 | } break; |
| 1630 | case Variant::BOOL: { |
| 1631 | f->store_32(VARIANT_BOOL); |
| 1632 | bool val = p_property; |
| 1633 | f->store_32(val); |
| 1634 | } break; |
| 1635 | case Variant::INT: { |
| 1636 | int64_t val = p_property; |
| 1637 | if (val > 0x7FFFFFFF || val < -(int64_t)0x80000000) { |
| 1638 | f->store_32(VARIANT_INT64); |
| 1639 | f->store_64(uint64_t(val)); |
| 1640 | |
| 1641 | } else { |
| 1642 | f->store_32(VARIANT_INT); |
| 1643 | f->store_32(uint32_t(p_property)); |
| 1644 | } |
| 1645 | |
| 1646 | } break; |
| 1647 | case Variant::FLOAT: { |
| 1648 | double d = p_property; |
| 1649 | float fl = d; |
| 1650 | if (double(fl) != d) { |
| 1651 | f->store_32(VARIANT_DOUBLE); |
| 1652 | f->store_double(d); |
| 1653 | } else { |
| 1654 | f->store_32(VARIANT_FLOAT); |
| 1655 | f->store_real(fl); |
| 1656 | } |
| 1657 | |
| 1658 | } break; |
| 1659 | case Variant::STRING: { |
| 1660 | f->store_32(VARIANT_STRING); |
| 1661 | String val = p_property; |
| 1662 | save_unicode_string(f, val); |
| 1663 | |
| 1664 | } break; |
| 1665 | case Variant::VECTOR2: { |
| 1666 | f->store_32(VARIANT_VECTOR2); |
| 1667 | Vector2 val = p_property; |
| 1668 | f->store_real(val.x); |
| 1669 | f->store_real(val.y); |
| 1670 | |
| 1671 | } break; |
| 1672 | case Variant::VECTOR2I: { |
| 1673 | f->store_32(VARIANT_VECTOR2I); |
| 1674 | Vector2i val = p_property; |
| 1675 | f->store_32(uint32_t(val.x)); |
| 1676 | f->store_32(uint32_t(val.y)); |
| 1677 | |
| 1678 | } break; |
| 1679 | case Variant::RECT2: { |
| 1680 | f->store_32(VARIANT_RECT2); |
| 1681 | Rect2 val = p_property; |
| 1682 | f->store_real(val.position.x); |
| 1683 | f->store_real(val.position.y); |
| 1684 | f->store_real(val.size.x); |
| 1685 | f->store_real(val.size.y); |
| 1686 | |
| 1687 | } break; |
| 1688 | case Variant::RECT2I: { |
| 1689 | f->store_32(VARIANT_RECT2I); |
| 1690 | Rect2i val = p_property; |
| 1691 | f->store_32(uint32_t(val.position.x)); |
| 1692 | f->store_32(uint32_t(val.position.y)); |
| 1693 | f->store_32(uint32_t(val.size.x)); |
| 1694 | f->store_32(uint32_t(val.size.y)); |
| 1695 | |
| 1696 | } break; |
| 1697 | case Variant::VECTOR3: { |
| 1698 | f->store_32(VARIANT_VECTOR3); |
| 1699 | Vector3 val = p_property; |
| 1700 | f->store_real(val.x); |
| 1701 | f->store_real(val.y); |
| 1702 | f->store_real(val.z); |
| 1703 | |
| 1704 | } break; |
| 1705 | case Variant::VECTOR3I: { |
| 1706 | f->store_32(VARIANT_VECTOR3I); |
| 1707 | Vector3i val = p_property; |
| 1708 | f->store_32(uint32_t(val.x)); |
| 1709 | f->store_32(uint32_t(val.y)); |
| 1710 | f->store_32(uint32_t(val.z)); |
| 1711 | |
| 1712 | } break; |
| 1713 | case Variant::VECTOR4: { |
| 1714 | f->store_32(VARIANT_VECTOR4); |
| 1715 | Vector4 val = p_property; |
| 1716 | f->store_real(val.x); |
| 1717 | f->store_real(val.y); |
| 1718 | f->store_real(val.z); |
| 1719 | f->store_real(val.w); |
| 1720 | |
| 1721 | } break; |
| 1722 | case Variant::VECTOR4I: { |
| 1723 | f->store_32(VARIANT_VECTOR4I); |
| 1724 | Vector4i val = p_property; |
| 1725 | f->store_32(uint32_t(val.x)); |
| 1726 | f->store_32(uint32_t(val.y)); |
| 1727 | f->store_32(uint32_t(val.z)); |
| 1728 | f->store_32(uint32_t(val.w)); |
| 1729 | |
| 1730 | } break; |
| 1731 | case Variant::PLANE: { |
| 1732 | f->store_32(VARIANT_PLANE); |
| 1733 | Plane val = p_property; |
| 1734 | f->store_real(val.normal.x); |
| 1735 | f->store_real(val.normal.y); |
| 1736 | f->store_real(val.normal.z); |
| 1737 | f->store_real(val.d); |
| 1738 | |
| 1739 | } break; |
| 1740 | case Variant::QUATERNION: { |
| 1741 | f->store_32(VARIANT_QUATERNION); |
| 1742 | Quaternion val = p_property; |
| 1743 | f->store_real(val.x); |
| 1744 | f->store_real(val.y); |
| 1745 | f->store_real(val.z); |
| 1746 | f->store_real(val.w); |
| 1747 | |
| 1748 | } break; |
| 1749 | case Variant::AABB: { |
| 1750 | f->store_32(VARIANT_AABB); |
| 1751 | AABB val = p_property; |
| 1752 | f->store_real(val.position.x); |
| 1753 | f->store_real(val.position.y); |
| 1754 | f->store_real(val.position.z); |
| 1755 | f->store_real(val.size.x); |
| 1756 | f->store_real(val.size.y); |
| 1757 | f->store_real(val.size.z); |
| 1758 | |
| 1759 | } break; |
| 1760 | case Variant::TRANSFORM2D: { |
| 1761 | f->store_32(VARIANT_TRANSFORM2D); |
| 1762 | Transform2D val = p_property; |
| 1763 | f->store_real(val.columns[0].x); |
| 1764 | f->store_real(val.columns[0].y); |
| 1765 | f->store_real(val.columns[1].x); |
| 1766 | f->store_real(val.columns[1].y); |
| 1767 | f->store_real(val.columns[2].x); |
| 1768 | f->store_real(val.columns[2].y); |
| 1769 | |
| 1770 | } break; |
| 1771 | case Variant::BASIS: { |
| 1772 | f->store_32(VARIANT_BASIS); |
| 1773 | Basis val = p_property; |
| 1774 | f->store_real(val.rows[0].x); |
| 1775 | f->store_real(val.rows[0].y); |
| 1776 | f->store_real(val.rows[0].z); |
| 1777 | f->store_real(val.rows[1].x); |
| 1778 | f->store_real(val.rows[1].y); |
| 1779 | f->store_real(val.rows[1].z); |
| 1780 | f->store_real(val.rows[2].x); |
| 1781 | f->store_real(val.rows[2].y); |
| 1782 | f->store_real(val.rows[2].z); |
| 1783 | |
| 1784 | } break; |
| 1785 | case Variant::TRANSFORM3D: { |
| 1786 | f->store_32(VARIANT_TRANSFORM3D); |
| 1787 | Transform3D val = p_property; |
| 1788 | f->store_real(val.basis.rows[0].x); |
| 1789 | f->store_real(val.basis.rows[0].y); |
| 1790 | f->store_real(val.basis.rows[0].z); |
| 1791 | f->store_real(val.basis.rows[1].x); |
| 1792 | f->store_real(val.basis.rows[1].y); |
| 1793 | f->store_real(val.basis.rows[1].z); |
| 1794 | f->store_real(val.basis.rows[2].x); |
| 1795 | f->store_real(val.basis.rows[2].y); |
| 1796 | f->store_real(val.basis.rows[2].z); |
| 1797 | f->store_real(val.origin.x); |
| 1798 | f->store_real(val.origin.y); |
| 1799 | f->store_real(val.origin.z); |
| 1800 | |
| 1801 | } break; |
| 1802 | case Variant::PROJECTION: { |
| 1803 | f->store_32(VARIANT_PROJECTION); |
| 1804 | Projection val = p_property; |
| 1805 | f->store_real(val.columns[0].x); |
| 1806 | f->store_real(val.columns[0].y); |
| 1807 | f->store_real(val.columns[0].z); |
| 1808 | f->store_real(val.columns[0].w); |
| 1809 | f->store_real(val.columns[1].x); |
| 1810 | f->store_real(val.columns[1].y); |
| 1811 | f->store_real(val.columns[1].z); |
| 1812 | f->store_real(val.columns[1].w); |
| 1813 | f->store_real(val.columns[2].x); |
| 1814 | f->store_real(val.columns[2].y); |
| 1815 | f->store_real(val.columns[2].z); |
| 1816 | f->store_real(val.columns[2].w); |
| 1817 | f->store_real(val.columns[3].x); |
| 1818 | f->store_real(val.columns[3].y); |
| 1819 | f->store_real(val.columns[3].z); |
| 1820 | f->store_real(val.columns[3].w); |
| 1821 | |
| 1822 | } break; |
| 1823 | case Variant::COLOR: { |
| 1824 | f->store_32(VARIANT_COLOR); |
| 1825 | Color val = p_property; |
| 1826 | // Color are always floats |
| 1827 | f->store_float(val.r); |
| 1828 | f->store_float(val.g); |
| 1829 | f->store_float(val.b); |
| 1830 | f->store_float(val.a); |
| 1831 | |
| 1832 | } break; |
| 1833 | case Variant::STRING_NAME: { |
| 1834 | f->store_32(VARIANT_STRING_NAME); |
| 1835 | String val = p_property; |
| 1836 | save_unicode_string(f, val); |
| 1837 | |
| 1838 | } break; |
| 1839 | |
| 1840 | case Variant::NODE_PATH: { |
| 1841 | f->store_32(VARIANT_NODE_PATH); |
| 1842 | NodePath np = p_property; |
| 1843 | f->store_16(np.get_name_count()); |
| 1844 | uint16_t snc = np.get_subname_count(); |
| 1845 | if (np.is_absolute()) { |
| 1846 | snc |= 0x8000; |
| 1847 | } |
| 1848 | f->store_16(snc); |
| 1849 | for (int i = 0; i < np.get_name_count(); i++) { |
| 1850 | if (string_map.has(np.get_name(i))) { |
| 1851 | f->store_32(uint32_t(string_map[np.get_name(i)])); |
| 1852 | } else { |
| 1853 | save_unicode_string(f, np.get_name(i), true); |
| 1854 | } |
| 1855 | } |
| 1856 | for (int i = 0; i < np.get_subname_count(); i++) { |
| 1857 | if (string_map.has(np.get_subname(i))) { |
| 1858 | f->store_32(uint32_t(string_map[np.get_subname(i)])); |
| 1859 | } else { |
| 1860 | save_unicode_string(f, np.get_subname(i), true); |
| 1861 | } |
| 1862 | } |
| 1863 | |
| 1864 | } break; |
| 1865 | case Variant::RID: { |
| 1866 | f->store_32(VARIANT_RID); |
| 1867 | WARN_PRINT("Can't save RIDs."); |
| 1868 | RID val = p_property; |
| 1869 | f->store_32(uint32_t(val.get_id())); |
| 1870 | } break; |
| 1871 | case Variant::OBJECT: { |
| 1872 | f->store_32(VARIANT_OBJECT); |
| 1873 | Ref<Resource> res = p_property; |
| 1874 | if (res.is_null() || res->get_meta(SNAME("_skip_save_"), false)) { |
| 1875 | f->store_32(OBJECT_EMPTY); |
| 1876 | return; // Don't save it. |
| 1877 | } |
| 1878 | |
| 1879 | if (!res->is_built_in()) { |
| 1880 | f->store_32(OBJECT_EXTERNAL_RESOURCE_INDEX); |
| 1881 | f->store_32(uint32_t(external_resources[res])); |
| 1882 | } else { |
| 1883 | if (!resource_map.has(res)) { |
| 1884 | f->store_32(OBJECT_EMPTY); |
| 1885 | ERR_FAIL_MSG("Resource was not pre cached for the resource section, most likely due to circular reference."); |
| 1886 | } |
| 1887 | |
| 1888 | f->store_32(OBJECT_INTERNAL_RESOURCE); |
| 1889 | f->store_32(uint32_t(resource_map[res])); |
| 1890 | //internal resource |
| 1891 | } |
| 1892 | |
| 1893 | } break; |
| 1894 | case Variant::CALLABLE: { |
| 1895 | f->store_32(VARIANT_CALLABLE); |
| 1896 | WARN_PRINT("Can't save Callables."); |
| 1897 | } break; |
| 1898 | case Variant::SIGNAL: { |
| 1899 | f->store_32(VARIANT_SIGNAL); |
| 1900 | WARN_PRINT("Can't save Signals."); |
| 1901 | } break; |
| 1902 | |
| 1903 | case Variant::DICTIONARY: { |
| 1904 | f->store_32(VARIANT_DICTIONARY); |
| 1905 | Dictionary d = p_property; |
| 1906 | f->store_32(uint32_t(d.size())); |
| 1907 | |
| 1908 | for (const KeyValue<Variant, Variant> &kv : d) { |
| 1909 | write_variant(f, kv.key, resource_map, external_resources, string_map); |
| 1910 | write_variant(f, kv.value, resource_map, external_resources, string_map); |
| 1911 | } |
| 1912 | |
| 1913 | } break; |
| 1914 | case Variant::ARRAY: { |
| 1915 | f->store_32(VARIANT_ARRAY); |
| 1916 | Array a = p_property; |
| 1917 | f->store_32(uint32_t(a.size())); |
| 1918 | for (const Variant &var : a) { |
| 1919 | write_variant(f, var, resource_map, external_resources, string_map); |
| 1920 | } |
| 1921 | |
| 1922 | } break; |
| 1923 | case Variant::PACKED_BYTE_ARRAY: { |
| 1924 | f->store_32(VARIANT_PACKED_BYTE_ARRAY); |
| 1925 | Vector<uint8_t> arr = p_property; |
| 1926 | int len = arr.size(); |
| 1927 | f->store_32(uint32_t(len)); |
| 1928 | const uint8_t *r = arr.ptr(); |
| 1929 | f->store_buffer(r, len); |
| 1930 | _pad_buffer(f, len); |
| 1931 | |
| 1932 | } break; |
| 1933 | case Variant::PACKED_INT32_ARRAY: { |
| 1934 | f->store_32(VARIANT_PACKED_INT32_ARRAY); |
| 1935 | Vector<int32_t> arr = p_property; |
| 1936 | int len = arr.size(); |
| 1937 | f->store_32(uint32_t(len)); |
| 1938 | const int32_t *r = arr.ptr(); |
| 1939 | for (int i = 0; i < len; i++) { |
| 1940 | f->store_32(uint32_t(r[i])); |
| 1941 | } |
| 1942 | |
| 1943 | } break; |
| 1944 | case Variant::PACKED_INT64_ARRAY: { |
| 1945 | f->store_32(VARIANT_PACKED_INT64_ARRAY); |
| 1946 | Vector<int64_t> arr = p_property; |
| 1947 | int len = arr.size(); |
| 1948 | f->store_32(uint32_t(len)); |
| 1949 | const int64_t *r = arr.ptr(); |
| 1950 | for (int i = 0; i < len; i++) { |
| 1951 | f->store_64(uint64_t(r[i])); |
| 1952 | } |
| 1953 | |
| 1954 | } break; |
| 1955 | case Variant::PACKED_FLOAT32_ARRAY: { |
| 1956 | f->store_32(VARIANT_PACKED_FLOAT32_ARRAY); |
| 1957 | Vector<float> arr = p_property; |
| 1958 | int len = arr.size(); |
| 1959 | f->store_32(uint32_t(len)); |
| 1960 | const float *r = arr.ptr(); |
| 1961 | for (int i = 0; i < len; i++) { |
| 1962 | f->store_float(r[i]); |
| 1963 | } |
| 1964 | |
| 1965 | } break; |
| 1966 | case Variant::PACKED_FLOAT64_ARRAY: { |
| 1967 | f->store_32(VARIANT_PACKED_FLOAT64_ARRAY); |
| 1968 | Vector<double> arr = p_property; |
| 1969 | int len = arr.size(); |
| 1970 | f->store_32(uint32_t(len)); |
| 1971 | const double *r = arr.ptr(); |
| 1972 | for (int i = 0; i < len; i++) { |
| 1973 | f->store_double(r[i]); |
| 1974 | } |
| 1975 | |
| 1976 | } break; |
| 1977 | case Variant::PACKED_STRING_ARRAY: { |
| 1978 | f->store_32(VARIANT_PACKED_STRING_ARRAY); |
| 1979 | Vector<String> arr = p_property; |
| 1980 | int len = arr.size(); |
| 1981 | f->store_32(uint32_t(len)); |
| 1982 | const String *r = arr.ptr(); |
| 1983 | for (int i = 0; i < len; i++) { |
| 1984 | save_unicode_string(f, r[i]); |
| 1985 | } |
| 1986 | } break; |
| 1987 | |
| 1988 | case Variant::PACKED_VECTOR2_ARRAY: { |
| 1989 | f->store_32(VARIANT_PACKED_VECTOR2_ARRAY); |
| 1990 | Vector<Vector2> arr = p_property; |
| 1991 | int len = arr.size(); |
| 1992 | f->store_32(uint32_t(len)); |
| 1993 | const Vector2 *r = arr.ptr(); |
| 1994 | for (int i = 0; i < len; i++) { |
| 1995 | f->store_real(r[i].x); |
| 1996 | f->store_real(r[i].y); |
| 1997 | } |
| 1998 | } break; |
| 1999 | |
| 2000 | case Variant::PACKED_VECTOR3_ARRAY: { |
| 2001 | f->store_32(VARIANT_PACKED_VECTOR3_ARRAY); |
| 2002 | Vector<Vector3> arr = p_property; |
| 2003 | int len = arr.size(); |
| 2004 | f->store_32(uint32_t(len)); |
| 2005 | const Vector3 *r = arr.ptr(); |
| 2006 | for (int i = 0; i < len; i++) { |
| 2007 | f->store_real(r[i].x); |
| 2008 | f->store_real(r[i].y); |
| 2009 | f->store_real(r[i].z); |
| 2010 | } |
| 2011 | } break; |
| 2012 | |
| 2013 | case Variant::PACKED_COLOR_ARRAY: { |
| 2014 | f->store_32(VARIANT_PACKED_COLOR_ARRAY); |
| 2015 | Vector<Color> arr = p_property; |
| 2016 | int len = arr.size(); |
| 2017 | f->store_32(uint32_t(len)); |
| 2018 | const Color *r = arr.ptr(); |
| 2019 | for (int i = 0; i < len; i++) { |
| 2020 | f->store_float(r[i].r); |
| 2021 | f->store_float(r[i].g); |
| 2022 | f->store_float(r[i].b); |
| 2023 | f->store_float(r[i].a); |
| 2024 | } |
| 2025 | |
| 2026 | } break; |
| 2027 | case Variant::PACKED_VECTOR4_ARRAY: { |
| 2028 | f->store_32(VARIANT_PACKED_VECTOR4_ARRAY); |
| 2029 | Vector<Vector4> arr = p_property; |
| 2030 | int len = arr.size(); |
| 2031 | f->store_32(uint32_t(len)); |
| 2032 | const Vector4 *r = arr.ptr(); |
| 2033 | for (int i = 0; i < len; i++) { |
| 2034 | f->store_real(r[i].x); |
| 2035 | f->store_real(r[i].y); |
| 2036 | f->store_real(r[i].z); |
| 2037 | f->store_real(r[i].w); |
| 2038 | } |
| 2039 | |
| 2040 | } break; |
| 2041 | default: { |
| 2042 | ERR_FAIL_MSG("Invalid variant."); |
| 2043 | } |
| 2044 | } |
| 2045 | } |
| 2046 | |
| 2047 | void ResourceFormatSaverBinaryInstance::_find_resources(const Variant &p_variant, bool p_main) { |
| 2048 | switch (p_variant.get_type()) { |
| 2049 | case Variant::OBJECT: { |
| 2050 | Ref<Resource> res = p_variant; |
| 2051 | |
| 2052 | if (res.is_null() || external_resources.has(res) || res->get_meta(SNAME("_skip_save_"), false)) { |
| 2053 | return; |
| 2054 | } |
| 2055 | |
| 2056 | if (!p_main && (!bundle_resources) && !res->is_built_in()) { |
| 2057 | if (res->get_path() == path) { |
| 2058 | ERR_PRINT(vformat("Circular reference to resource being saved found: '%s' will be null next time it's loaded.", local_path)); |
| 2059 | return; |
| 2060 | } |
| 2061 | int idx = external_resources.size(); |
| 2062 | external_resources[res] = idx; |
| 2063 | return; |
| 2064 | } |
| 2065 | |
| 2066 | if (resource_set.has(res)) { |
| 2067 | return; |
| 2068 | } |
| 2069 | |
| 2070 | resource_set.insert(res); |
| 2071 | |
| 2072 | List<PropertyInfo> property_list; |
| 2073 | |
| 2074 | res->get_property_list(&property_list); |
| 2075 | |
| 2076 | for (const PropertyInfo &E : property_list) { |
| 2077 | if (E.usage & PROPERTY_USAGE_STORAGE) { |
| 2078 | Variant value = res->get(E.name); |
| 2079 | if (E.usage & PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT) { |
| 2080 | NonPersistentKey npk; |
| 2081 | npk.base = res; |
| 2082 | npk.property = E.name; |
| 2083 | non_persistent_map[npk] = value; |
| 2084 | |
| 2085 | Ref<Resource> sres = value; |
| 2086 | if (sres.is_valid()) { |
| 2087 | resource_set.insert(sres); |
| 2088 | saved_resources.push_back(sres); |
| 2089 | } else { |
| 2090 | _find_resources(value); |
| 2091 | } |
| 2092 | } else { |
| 2093 | _find_resources(value); |
| 2094 | } |
| 2095 | } |
| 2096 | } |
| 2097 | |
| 2098 | saved_resources.push_back(res); |
| 2099 | |
| 2100 | } break; |
| 2101 | |
| 2102 | case Variant::ARRAY: { |
| 2103 | Array varray = p_variant; |
| 2104 | _find_resources(varray.get_typed_script()); |
| 2105 | for (const Variant &v : varray) { |
| 2106 | _find_resources(v); |
| 2107 | } |
| 2108 | |
| 2109 | } break; |
| 2110 | |
| 2111 | case Variant::DICTIONARY: { |
| 2112 | Dictionary d = p_variant; |
| 2113 | _find_resources(d.get_typed_key_script()); |
| 2114 | _find_resources(d.get_typed_value_script()); |
| 2115 | for (const KeyValue<Variant, Variant> &kv : d) { |
| 2116 | _find_resources(kv.key); |
| 2117 | _find_resources(kv.value); |
| 2118 | } |
| 2119 | } break; |
| 2120 | case Variant::NODE_PATH: { |
| 2121 | //take the chance and save node path strings |
| 2122 | NodePath np = p_variant; |
| 2123 | for (int i = 0; i < np.get_name_count(); i++) { |
| 2124 | get_string_index(np.get_name(i)); |
| 2125 | } |
| 2126 | for (int i = 0; i < np.get_subname_count(); i++) { |
| 2127 | get_string_index(np.get_subname(i)); |
| 2128 | } |
| 2129 | |
| 2130 | } break; |
| 2131 | default: { |
| 2132 | } |
| 2133 | } |
| 2134 | } |
| 2135 | |
| 2136 | void ResourceFormatSaverBinaryInstance::save_unicode_string(Ref<FileAccess> p_f, const String &p_string, bool p_bit_on_len) { |
| 2137 | CharString utf8 = p_string.utf8(); |
| 2138 | if (p_bit_on_len) { |
| 2139 | p_f->store_32(uint32_t((utf8.length() + 1) | 0x80000000)); |
| 2140 | } else { |
| 2141 | p_f->store_32(uint32_t(utf8.length() + 1)); |
| 2142 | } |
| 2143 | p_f->store_buffer((const uint8_t *)utf8.get_data(), utf8.length() + 1); |
| 2144 | } |
| 2145 | |
| 2146 | int ResourceFormatSaverBinaryInstance::get_string_index(const String &p_string) { |
| 2147 | StringName s = p_string; |
| 2148 | if (string_map.has(s)) { |
| 2149 | return string_map[s]; |
| 2150 | } |
| 2151 | |
| 2152 | string_map[s] = strings.size(); |
| 2153 | strings.push_back(s); |
| 2154 | return strings.size() - 1; |
| 2155 | } |
| 2156 | |
| 2157 | static String _resource_get_class(Ref<Resource> p_resource) { |
| 2158 | Ref<MissingResource> missing_resource = p_resource; |
| 2159 | if (missing_resource.is_valid()) { |
| 2160 | return missing_resource->get_original_class(); |
| 2161 | } else { |
| 2162 | return p_resource->get_class(); |
| 2163 | } |
| 2164 | } |
| 2165 | |
| 2166 | Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const Ref<Resource> &p_resource, uint32_t p_flags) { |
| 2167 | Resource::seed_scene_unique_id(p_path.hash()); |
| 2168 | |
| 2169 | Error err; |
| 2170 | Ref<FileAccess> f; |
| 2171 | if (p_flags & ResourceSaver::FLAG_COMPRESS) { |
| 2172 | Ref<FileAccessCompressed> fac; |
| 2173 | fac.instantiate(); |
| 2174 | fac->configure("RSCC"); |
| 2175 | f = fac; |
| 2176 | err = fac->open_internal(p_path, FileAccess::WRITE); |
| 2177 | } else { |
| 2178 | f = FileAccess::open(p_path, FileAccess::WRITE, &err); |
| 2179 | } |
| 2180 | |
| 2181 | ERR_FAIL_COND_V_MSG(err != OK, err, vformat("Cannot create file '%s'.", p_path)); |
| 2182 | |
| 2183 | relative_paths = p_flags & ResourceSaver::FLAG_RELATIVE_PATHS; |
| 2184 | skip_editor = p_flags & ResourceSaver::FLAG_OMIT_EDITOR_PROPERTIES; |
| 2185 | bundle_resources = p_flags & ResourceSaver::FLAG_BUNDLE_RESOURCES; |
| 2186 | big_endian = p_flags & ResourceSaver::FLAG_SAVE_BIG_ENDIAN; |
| 2187 | takeover_paths = p_flags & ResourceSaver::FLAG_REPLACE_SUBRESOURCE_PATHS; |
| 2188 | |
| 2189 | if (!p_path.begins_with("res://")) { |
| 2190 | takeover_paths = false; |
| 2191 | } |
| 2192 | |
| 2193 | local_path = p_path.get_base_dir(); |
| 2194 | path = ProjectSettings::get_singleton()->localize_path(p_path); |
| 2195 | |
| 2196 | _find_resources(p_resource, true); |
| 2197 | |
| 2198 | if (!(p_flags & ResourceSaver::FLAG_COMPRESS)) { |
| 2199 | //save header compressed |
| 2200 | static const uint8_t header[4] = { 'R', 'S', 'R', 'C' }; |
| 2201 | f->store_buffer(header, 4); |
| 2202 | } |
| 2203 | |
| 2204 | if (big_endian) { |
| 2205 | f->store_32(1); |
| 2206 | } else { |
| 2207 | f->store_32(0); |
| 2208 | } |
| 2209 | f->store_32(0); //64 bits file, false for now |
| 2210 | f->set_big_endian(big_endian); |
| 2211 | |
| 2212 | f->store_32(GODOT_VERSION_MAJOR); |
| 2213 | f->store_32(GODOT_VERSION_MINOR); |
| 2214 | f->store_32(FORMAT_VERSION); |
| 2215 | |
| 2216 | if (f->get_error() != OK && f->get_error() != ERR_FILE_EOF) { |
| 2217 | return ERR_CANT_CREATE; |
| 2218 | } |
| 2219 | |
| 2220 | save_unicode_string(f, _resource_get_class(p_resource)); |
| 2221 | f->store_64(0); //offset to import metadata |
| 2222 | |
| 2223 | String script_class; |
| 2224 | { |
| 2225 | uint32_t format_flags = FORMAT_FLAG_NAMED_SCENE_IDS | FORMAT_FLAG_UIDS; |
| 2226 | #ifdef REAL_T_IS_DOUBLE |
| 2227 | format_flags |= FORMAT_FLAG_REAL_T_IS_DOUBLE; |
| 2228 | #endif |
| 2229 | if (!p_resource->is_class("PackedScene")) { |
| 2230 | Ref<Script> s = p_resource->get_script(); |
| 2231 | if (s.is_valid()) { |
| 2232 | script_class = s->get_global_name(); |
| 2233 | if (!script_class.is_empty()) { |
| 2234 | format_flags |= ResourceFormatSaverBinaryInstance::FORMAT_FLAG_HAS_SCRIPT_CLASS; |
| 2235 | } |
| 2236 | } |
| 2237 | } |
| 2238 | |
| 2239 | f->store_32(format_flags); |
| 2240 | } |
| 2241 | ResourceUID::ID uid = ResourceSaver::get_resource_id_for_path(p_path, true); |
| 2242 | f->store_64(uint64_t(uid)); |
| 2243 | if (!script_class.is_empty()) { |
| 2244 | save_unicode_string(f, script_class); |
| 2245 | } |
| 2246 | |
| 2247 | for (int i = 0; i < ResourceFormatSaverBinaryInstance::RESERVED_FIELDS; i++) { |
| 2248 | f->store_32(0); // reserved |
| 2249 | } |
| 2250 | |
| 2251 | List<ResourceData> resources; |
| 2252 | |
| 2253 | { |
| 2254 | for (const Ref<Resource> &E : saved_resources) { |
| 2255 | Dictionary missing_resource_properties = E->get_meta(META_MISSING_RESOURCES, Dictionary()); |
| 2256 | |
| 2257 | ResourceData &rd = resources.push_back(ResourceData())->get(); |
| 2258 | rd.type = _resource_get_class(E); |
| 2259 | |
| 2260 | List<PropertyInfo> property_list; |
| 2261 | E->get_property_list(&property_list); |
| 2262 | |
| 2263 | for (const PropertyInfo &F : property_list) { |
| 2264 | if (skip_editor && F.name.begins_with("__editor")) { |
| 2265 | continue; |
| 2266 | } |
| 2267 | if (F.name == META_PROPERTY_MISSING_RESOURCES) { |
| 2268 | continue; |
| 2269 | } |
| 2270 | |
| 2271 | if ((F.usage & PROPERTY_USAGE_STORAGE) || missing_resource_properties.has(F.name)) { |
| 2272 | Property p; |
| 2273 | p.name_idx = get_string_index(F.name); |
| 2274 | |
| 2275 | if (F.usage & PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT) { |
| 2276 | NonPersistentKey npk; |
| 2277 | npk.base = E; |
| 2278 | npk.property = F.name; |
| 2279 | if (non_persistent_map.has(npk)) { |
| 2280 | p.value = non_persistent_map[npk]; |
| 2281 | } |
| 2282 | } else { |
| 2283 | p.value = E->get(F.name); |
| 2284 | } |
| 2285 | |
| 2286 | if (F.type == Variant::OBJECT && missing_resource_properties.has(F.name)) { |
| 2287 | // Was this missing resource overridden? If so do not save the old value. |
| 2288 | Ref<Resource> res = p.value; |
| 2289 | if (res.is_null()) { |
| 2290 | p.value = missing_resource_properties[F.name]; |
| 2291 | } |
| 2292 | } |
| 2293 | |
| 2294 | bool is_script = F.name == CoreStringName(script); |
| 2295 | Variant default_value = is_script ? Variant() : PropertyUtils::get_property_default_value(E.ptr(), F.name); |
| 2296 | |
| 2297 | if (default_value.get_type() != Variant::NIL && bool(Variant::evaluate(Variant::OP_EQUAL, p.value, default_value))) { |
| 2298 | continue; |
| 2299 | } |
| 2300 | |
| 2301 | p.pi = F; |
| 2302 | |
| 2303 | rd.properties.push_back(p); |
| 2304 | } |
| 2305 | } |
| 2306 | } |
| 2307 | } |
| 2308 | |
| 2309 | f->store_32(uint32_t(strings.size())); //string table size |
| 2310 | for (int i = 0; i < strings.size(); i++) { |
| 2311 | save_unicode_string(f, strings[i]); |
| 2312 | } |
| 2313 | |
| 2314 | // save external resource table |
| 2315 | f->store_32(external_resources.size()); //amount of external resources |
| 2316 | Vector<Ref<Resource>> save_order; |
| 2317 | save_order.resize(external_resources.size()); |
| 2318 | |
| 2319 | for (const KeyValue<Ref<Resource>, int> &E : external_resources) { |
| 2320 | save_order.write[E.value] = E.key; |
| 2321 | } |
| 2322 | |
| 2323 | for (int i = 0; i < save_order.size(); i++) { |
| 2324 | save_unicode_string(f, save_order[i]->get_save_class()); |
| 2325 | String res_path = save_order[i]->get_path(); |
| 2326 | res_path = relative_paths ? local_path.path_to_file(res_path) : res_path; |
| 2327 | save_unicode_string(f, res_path); |
| 2328 | ResourceUID::ID ruid = ResourceSaver::get_resource_id_for_path(save_order[i]->get_path(), false); |
| 2329 | f->store_64(uint64_t(ruid)); |
| 2330 | } |
| 2331 | // save internal resource table |
| 2332 | f->store_32(uint32_t(saved_resources.size())); //amount of internal resources |
| 2333 | Vector<uint64_t> ofs_pos; |
| 2334 | HashSet<String> used_unique_ids; |
| 2335 | |
| 2336 | for (Ref<Resource> &r : saved_resources) { |
| 2337 | if (r->is_built_in()) { |
| 2338 | if (!r->get_scene_unique_id().is_empty()) { |
| 2339 | if (used_unique_ids.has(r->get_scene_unique_id())) { |
| 2340 | r->set_scene_unique_id(""); |
| 2341 | } else { |
| 2342 | used_unique_ids.insert(r->get_scene_unique_id()); |
| 2343 | } |
| 2344 | } |
| 2345 | } |
| 2346 | } |
| 2347 | |
| 2348 | HashMap<Ref<Resource>, int> resource_map; |
| 2349 | int res_index = 0; |
| 2350 | for (Ref<Resource> &r : saved_resources) { |
| 2351 | if (r->is_built_in()) { |
| 2352 | if (r->get_scene_unique_id().is_empty()) { |
| 2353 | String new_id; |
| 2354 | |
| 2355 | while (true) { |
| 2356 | new_id = _resource_get_class(r) + "_" + Resource::generate_scene_unique_id(); |
| 2357 | if (!used_unique_ids.has(new_id)) { |
| 2358 | break; |
| 2359 | } |
| 2360 | } |
| 2361 | |
| 2362 | r->set_scene_unique_id(new_id); |
| 2363 | used_unique_ids.insert(new_id); |
| 2364 | } |
| 2365 | |
| 2366 | save_unicode_string(f, "local://" + r->get_scene_unique_id()); |
| 2367 | if (takeover_paths) { |
| 2368 | r->set_path(p_path + "::" + r->get_scene_unique_id(), true); |
| 2369 | } |
| 2370 | #ifdef TOOLS_ENABLED |
| 2371 | r->set_edited(false); |
| 2372 | #endif |
| 2373 | } else { |
| 2374 | save_unicode_string(f, r->get_path()); //actual external |
| 2375 | } |
| 2376 | ofs_pos.push_back(f->get_position()); |
| 2377 | f->store_64(0); //offset in 64 bits |
| 2378 | resource_map[r] = res_index++; |
| 2379 | } |
| 2380 | |
| 2381 | Vector<uint64_t> ofs_table; |
| 2382 | |
| 2383 | //now actually save the resources |
| 2384 | for (const ResourceData &rd : resources) { |
| 2385 | ofs_table.push_back(f->get_position()); |
| 2386 | save_unicode_string(f, rd.type); |
| 2387 | f->store_32(uint32_t(rd.properties.size())); |
| 2388 | |
| 2389 | for (const Property &p : rd.properties) { |
| 2390 | f->store_32(uint32_t(p.name_idx)); |
| 2391 | write_variant(f, p.value, resource_map, external_resources, string_map, p.pi); |
| 2392 | } |
| 2393 | } |
| 2394 | |
| 2395 | for (int i = 0; i < ofs_table.size(); i++) { |
| 2396 | f->seek(ofs_pos[i]); |
| 2397 | f->store_64(ofs_table[i]); |
| 2398 | } |
| 2399 | |
| 2400 | f->seek_end(); |
| 2401 | |
| 2402 | f->store_buffer((const uint8_t *)"RSRC", 4); //magic at end |
| 2403 | |
| 2404 | if (f->get_error() != OK && f->get_error() != ERR_FILE_EOF) { |
| 2405 | return ERR_CANT_CREATE; |
| 2406 | } |
| 2407 | |
| 2408 | return OK; |
| 2409 | } |
| 2410 | |
| 2411 | Error ResourceFormatSaverBinaryInstance::set_uid(const String &p_path, ResourceUID::ID p_uid) { |
| 2412 | Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ); |
| 2413 | ERR_FAIL_COND_V_MSG(f.is_null(), ERR_CANT_OPEN, vformat("Cannot open file '%s'.", p_path)); |
| 2414 | |
| 2415 | Ref<FileAccess> fw; |
| 2416 | |
| 2417 | local_path = p_path.get_base_dir(); |
| 2418 | |
| 2419 | uint8_t header[4]; |
| 2420 | f->get_buffer(header, 4); |
| 2421 | if (header[0] == 'R' && header[1] == 'S' && header[2] == 'C' && header[3] == 'C') { |
| 2422 | // Compressed. |
| 2423 | Ref<FileAccessCompressed> fac; |
| 2424 | fac.instantiate(); |
| 2425 | Error err = fac->open_after_magic(f); |
| 2426 | ERR_FAIL_COND_V_MSG(err != OK, err, vformat("Cannot open file '%s'.", p_path)); |
| 2427 | f = fac; |
| 2428 | |
| 2429 | Ref<FileAccessCompressed> facw; |
| 2430 | facw.instantiate(); |
| 2431 | facw->configure("RSCC"); |
| 2432 | err = facw->open_internal(p_path + ".uidren", FileAccess::WRITE); |
| 2433 | ERR_FAIL_COND_V_MSG(err, ERR_FILE_CORRUPT, vformat("Cannot create file '%s.uidren'.", p_path)); |
| 2434 | |
| 2435 | fw = facw; |
| 2436 | |
| 2437 | } else if (header[0] != 'R' || header[1] != 'S' || header[2] != 'R' || header[3] != 'C') { |
| 2438 | // Not a binary resource. |
| 2439 | return ERR_FILE_UNRECOGNIZED; |
| 2440 | } else { |
| 2441 | fw = FileAccess::open(p_path + ".uidren", FileAccess::WRITE); |
| 2442 | ERR_FAIL_COND_V_MSG(fw.is_null(), ERR_CANT_CREATE, vformat("Cannot create file '%s.uidren'.", p_path)); |
| 2443 | |
| 2444 | uint8_t magich[4] = { 'R', 'S', 'R', 'C' }; |
| 2445 | fw->store_buffer(magich, 4); |
| 2446 | } |
| 2447 | |
| 2448 | big_endian = f->get_32(); |
| 2449 | bool use_real64 = f->get_32(); |
| 2450 | f->set_big_endian(big_endian != 0); //read big endian if saved as big endian |
| 2451 | |
| 2452 | fw->store_32(big_endian); |
| 2453 | fw->store_32(use_real64); //use real64 |
| 2454 | fw->set_big_endian(big_endian != 0); |
| 2455 | |
| 2456 | uint32_t ver_major = f->get_32(); |
| 2457 | uint32_t ver_minor = f->get_32(); |
| 2458 | uint32_t ver_format = f->get_32(); |
| 2459 | |
| 2460 | if (ver_format < FORMAT_VERSION_CAN_RENAME_DEPS) { |
| 2461 | fw.unref(); |
| 2462 | |
| 2463 | { |
| 2464 | Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); |
| 2465 | da->remove(p_path + ".uidren"); |
| 2466 | } |
| 2467 | |
| 2468 | // Use the old approach. |
| 2469 | |
| 2470 | WARN_PRINT(vformat("This file is old, so it does not support UIDs, opening and resaving '%s'.", p_path)); |
| 2471 | return ERR_UNAVAILABLE; |
| 2472 | } |
| 2473 | |
| 2474 | if (ver_format > FORMAT_VERSION || ver_major > GODOT_VERSION_MAJOR) { |
| 2475 | ERR_FAIL_V_MSG(ERR_FILE_UNRECOGNIZED, |
| 2476 | 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).", |
| 2477 | local_path, ver_format, ver_major, ver_minor, GODOT_VERSION_BRANCH)); |
| 2478 | } |
| 2479 | |
| 2480 | // Since we're not actually converting the file contents, leave the version |
| 2481 | // numbers in the file untouched. |
| 2482 | fw->store_32(ver_major); |
| 2483 | fw->store_32(ver_minor); |
| 2484 | fw->store_32(ver_format); |
| 2485 | |
| 2486 | save_ustring(fw, get_ustring(f)); //type |
| 2487 | |
| 2488 | fw->store_64(f->get_64()); //metadata offset |
| 2489 | |
| 2490 | uint32_t flags = f->get_32(); |
| 2491 | flags |= ResourceFormatSaverBinaryInstance::FORMAT_FLAG_UIDS; |
| 2492 | f->get_64(); // Skip previous UID |
| 2493 | |
| 2494 | fw->store_32(flags); |
| 2495 | fw->store_64(uint64_t(p_uid)); |
| 2496 | |
| 2497 | if (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_HAS_SCRIPT_CLASS) { |
| 2498 | save_ustring(fw, get_ustring(f)); |
| 2499 | } |
| 2500 | |
| 2501 | //rest of file |
| 2502 | uint8_t b = f->get_8(); |
| 2503 | while (!f->eof_reached()) { |
| 2504 | fw->store_8(b); |
| 2505 | b = f->get_8(); |
| 2506 | } |
| 2507 | |
| 2508 | f.unref(); |
| 2509 | |
| 2510 | bool all_ok = fw->get_error() == OK; |
| 2511 | |
| 2512 | if (!all_ok) { |
| 2513 | return ERR_CANT_CREATE; |
| 2514 | } |
| 2515 | |
| 2516 | fw.unref(); |
| 2517 | |
| 2518 | Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES); |
| 2519 | da->remove(p_path); |
| 2520 | da->rename(p_path + ".uidren", p_path); |
| 2521 | return OK; |
| 2522 | } |
| 2523 | |
| 2524 | Error ResourceFormatSaverBinary::save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags) { |
| 2525 | String local_path = ProjectSettings::get_singleton()->localize_path(p_path); |
| 2526 | ResourceFormatSaverBinaryInstance saver; |
| 2527 | return saver.save(local_path, p_resource, p_flags); |
| 2528 | } |
| 2529 | |
| 2530 | Error ResourceFormatSaverBinary::set_uid(const String &p_path, ResourceUID::ID p_uid) { |
| 2531 | String local_path = ProjectSettings::get_singleton()->localize_path(p_path); |
| 2532 | ResourceFormatSaverBinaryInstance saver; |
| 2533 | return saver.set_uid(local_path, p_uid); |
| 2534 | } |
| 2535 | |
| 2536 | bool ResourceFormatSaverBinary::recognize(const Ref<Resource> &p_resource) const { |
| 2537 | return true; //all recognized |
| 2538 | } |
| 2539 | |
| 2540 | void ResourceFormatSaverBinary::get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const { |
| 2541 | String base = p_resource->get_base_extension().to_lower(); |
| 2542 | p_extensions->push_back(base); |
| 2543 | if (base != "res") { |
| 2544 | p_extensions->push_back("res"); |
| 2545 | } |
| 2546 | } |
| 2547 | |
| 2548 | ResourceFormatSaverBinary::ResourceFormatSaverBinary() { |
| 2549 | singleton = this; |
| 2550 | } |