| 1 | /**************************************************************************/ |
| 2 | /* resource.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.h" |
| 32 | |
| 33 | #include "core/io/resource_loader.h" |
| 34 | #include "core/math/math_funcs.h" |
| 35 | #include "core/math/random_pcg.h" |
| 36 | #include "core/os/os.h" |
| 37 | #include "core/variant/container_type_validate.h" |
| 38 | #include "scene/main/node.h" //only so casting works |
| 39 | |
| 40 | void Resource::emit_changed() { |
| 41 | if (emit_changed_state != EMIT_CHANGED_UNBLOCKED) { |
| 42 | emit_changed_state = EMIT_CHANGED_BLOCKED_PENDING_EMIT; |
| 43 | return; |
| 44 | } |
| 45 | if (ResourceLoader::is_within_load() && !Thread::is_main_thread()) { |
| 46 | ResourceLoader::resource_changed_emit(this); |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | emit_signal(CoreStringName(changed)); |
| 51 | } |
| 52 | |
| 53 | void Resource::_block_emit_changed() { |
| 54 | if (emit_changed_state == EMIT_CHANGED_UNBLOCKED) { |
| 55 | emit_changed_state = EMIT_CHANGED_BLOCKED; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | void Resource::_unblock_emit_changed() { |
| 60 | bool emit = (emit_changed_state == EMIT_CHANGED_BLOCKED_PENDING_EMIT); |
| 61 | emit_changed_state = EMIT_CHANGED_UNBLOCKED; |
| 62 | if (emit) { |
| 63 | emit_changed(); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | void Resource::_resource_path_changed() { |
| 68 | } |
| 69 | |
| 70 | void Resource::set_path(const String &p_path, bool p_take_over) { |
| 71 | if (path_cache == p_path) { |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | if (p_path.is_empty()) { |
| 76 | p_take_over = false; // Can't take over an empty path |
| 77 | } |
| 78 | |
| 79 | { |
| 80 | MutexLock lock(ResourceCache::lock); |
| 81 | |
| 82 | if (!path_cache.is_empty()) { |
| 83 | ResourceCache::resources.erase(path_cache); |
| 84 | } |
| 85 | |
| 86 | path_cache = ""; |
| 87 | |
| 88 | Ref<Resource> existing = ResourceCache::get_ref(p_path); |
| 89 | |
| 90 | if (existing.is_valid()) { |
| 91 | if (p_take_over) { |
| 92 | existing->path_cache = String(); |
| 93 | ResourceCache::resources.erase(p_path); |
| 94 | } else { |
| 95 | ERR_FAIL_MSG(vformat("Another resource is loaded from path '%s' (possible cyclic resource inclusion).", p_path)); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | path_cache = p_path; |
| 100 | |
| 101 | if (!path_cache.is_empty()) { |
| 102 | ResourceCache::resources[path_cache] = this; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | _resource_path_changed(); |
| 107 | } |
| 108 | |
| 109 | String Resource::get_path() const { |
| 110 | return path_cache; |
| 111 | } |
| 112 | |
| 113 | void Resource::set_path_cache(const String &p_path) { |
| 114 | path_cache = p_path; |
| 115 | GDVIRTUAL_CALL(_set_path_cache, p_path); |
| 116 | } |
| 117 | |
| 118 | static thread_local RandomPCG unique_id_gen = RandomPCG(0); |
| 119 | |
| 120 | void Resource::seed_scene_unique_id(uint32_t p_seed) { |
| 121 | unique_id_gen.seed(p_seed); |
| 122 | } |
| 123 | |
| 124 | String Resource::generate_scene_unique_id() { |
| 125 | // Generate a unique enough hash, but still user-readable. |
| 126 | // If it's not unique it does not matter because the saver will try again. |
| 127 | if (unique_id_gen.get_seed() == 0) { |
| 128 | OS::DateTime dt = OS::get_singleton()->get_datetime(); |
| 129 | uint32_t hash = hash_murmur3_one_32(OS::get_singleton()->get_ticks_usec()); |
| 130 | hash = hash_murmur3_one_32(dt.year, hash); |
| 131 | hash = hash_murmur3_one_32(dt.month, hash); |
| 132 | hash = hash_murmur3_one_32(dt.day, hash); |
| 133 | hash = hash_murmur3_one_32(dt.hour, hash); |
| 134 | hash = hash_murmur3_one_32(dt.minute, hash); |
| 135 | hash = hash_murmur3_one_32(dt.second, hash); |
| 136 | hash = hash_murmur3_one_32(Math::rand(), hash); |
| 137 | unique_id_gen.seed(hash); |
| 138 | } |
| 139 | |
| 140 | uint32_t random_num = unique_id_gen.rand(); |
| 141 | |
| 142 | static constexpr uint32_t characters = 5; |
| 143 | static constexpr uint32_t char_count = ('z' - 'a'); |
| 144 | static constexpr uint32_t base = char_count + ('9' - '0'); |
| 145 | String id; |
| 146 | id.resize_uninitialized(characters + 1); |
| 147 | char32_t *ptr = id.ptrw(); |
| 148 | for (uint32_t i = 0; i < characters; i++) { |
| 149 | uint32_t c = random_num % base; |
| 150 | if (c < char_count) { |
| 151 | ptr[i] = ('a' + c); |
| 152 | } else { |
| 153 | ptr[i] = ('0' + (c - char_count)); |
| 154 | } |
| 155 | random_num /= base; |
| 156 | } |
| 157 | ptr[characters] = '\0'; |
| 158 | |
| 159 | return id; |
| 160 | } |
| 161 | |
| 162 | void Resource::set_scene_unique_id(const String &p_id) { |
| 163 | bool is_valid = true; |
| 164 | for (int i = 0; i < p_id.length(); i++) { |
| 165 | if (!is_ascii_identifier_char(p_id[i])) { |
| 166 | is_valid = false; |
| 167 | scene_unique_id = Resource::generate_scene_unique_id(); |
| 168 | break; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | ERR_FAIL_COND_MSG(!is_valid, "The scene unique ID must contain only letters, numbers, and underscores."); |
| 173 | scene_unique_id = p_id; |
| 174 | } |
| 175 | |
| 176 | String Resource::get_scene_unique_id() const { |
| 177 | return scene_unique_id; |
| 178 | } |
| 179 | |
| 180 | void Resource::set_name(const String &p_name) { |
| 181 | name = p_name; |
| 182 | emit_changed(); |
| 183 | } |
| 184 | |
| 185 | String Resource::get_name() const { |
| 186 | return name; |
| 187 | } |
| 188 | |
| 189 | void Resource::update_configuration_warning() { |
| 190 | if (_update_configuration_warning) { |
| 191 | _update_configuration_warning(); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | bool Resource::editor_can_reload_from_file() { |
| 196 | return true; //by default yes |
| 197 | } |
| 198 | |
| 199 | void Resource::connect_changed(const Callable &p_callable, uint32_t p_flags) { |
| 200 | if (ResourceLoader::is_within_load() && !Thread::is_main_thread()) { |
| 201 | ResourceLoader::resource_changed_connect(this, p_callable, p_flags); |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | if (!is_connected(CoreStringName(changed), p_callable) || p_flags & CONNECT_REFERENCE_COUNTED) { |
| 206 | connect(CoreStringName(changed), p_callable, p_flags); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | void Resource::disconnect_changed(const Callable &p_callable) { |
| 211 | if (ResourceLoader::is_within_load() && !Thread::is_main_thread()) { |
| 212 | ResourceLoader::resource_changed_disconnect(this, p_callable); |
| 213 | return; |
| 214 | } |
| 215 | |
| 216 | if (is_connected(CoreStringName(changed), p_callable)) { |
| 217 | disconnect(CoreStringName(changed), p_callable); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | void Resource::reset_state() { |
| 222 | GDVIRTUAL_CALL(_reset_state); |
| 223 | } |
| 224 | |
| 225 | Error Resource::copy_from(const Ref<Resource> &p_resource) { |
| 226 | ERR_FAIL_COND_V(p_resource.is_null(), ERR_INVALID_PARAMETER); |
| 227 | if (get_class() != p_resource->get_class()) { |
| 228 | return ERR_INVALID_PARAMETER; |
| 229 | } |
| 230 | |
| 231 | _block_emit_changed(); |
| 232 | |
| 233 | reset_state(); // May want to reset state. |
| 234 | |
| 235 | List<PropertyInfo> pi; |
| 236 | p_resource->get_property_list(&pi); |
| 237 | |
| 238 | for (const PropertyInfo &E : pi) { |
| 239 | if (!(E.usage & PROPERTY_USAGE_STORAGE)) { |
| 240 | continue; |
| 241 | } |
| 242 | if (E.name == "resource_path") { |
| 243 | continue; //do not change path |
| 244 | } |
| 245 | |
| 246 | set(E.name, p_resource->get(E.name)); |
| 247 | } |
| 248 | |
| 249 | _unblock_emit_changed(); |
| 250 | |
| 251 | return OK; |
| 252 | } |
| 253 | |
| 254 | void Resource::reload_from_file() { |
| 255 | String path = get_path(); |
| 256 | if (!path.is_resource_file()) { |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | Ref<Resource> s = ResourceLoader::load(ResourceLoader::path_remap(path), get_class(), ResourceFormatLoader::CACHE_MODE_IGNORE); |
| 261 | |
| 262 | if (s.is_null()) { |
| 263 | return; |
| 264 | } |
| 265 | |
| 266 | copy_from(s); |
| 267 | } |
| 268 | |
| 269 | Variant Resource::_duplicate_recursive(const Variant &p_variant, const DuplicateParams &p_params, uint32_t p_usage) const { |
| 270 | // Anything other than object can be simply skipped in case of a shallow copy. |
| 271 | if (!p_params.deep && p_variant.get_type() != Variant::OBJECT) { |
| 272 | return p_variant; |
| 273 | } |
| 274 | |
| 275 | switch (p_variant.get_type()) { |
| 276 | case Variant::OBJECT: { |
| 277 | const Ref<Resource> &sr = p_variant; |
| 278 | bool should_duplicate = false; |
| 279 | if (sr.is_valid()) { |
| 280 | if ((p_usage & PROPERTY_USAGE_ALWAYS_DUPLICATE)) { |
| 281 | should_duplicate = true; |
| 282 | } else if ((p_usage & PROPERTY_USAGE_NEVER_DUPLICATE)) { |
| 283 | should_duplicate = false; |
| 284 | } else if (p_params.local_scene) { |
| 285 | should_duplicate = sr->is_local_to_scene(); |
| 286 | } else { |
| 287 | switch (p_params.subres_mode) { |
| 288 | case RESOURCE_DEEP_DUPLICATE_NONE: { |
| 289 | should_duplicate = false; |
| 290 | } break; |
| 291 | case RESOURCE_DEEP_DUPLICATE_INTERNAL: { |
| 292 | should_duplicate = p_params.deep && sr->is_built_in(); |
| 293 | } break; |
| 294 | case RESOURCE_DEEP_DUPLICATE_ALL: { |
| 295 | should_duplicate = p_params.deep; |
| 296 | } break; |
| 297 | default: { |
| 298 | DEV_ASSERT(false); |
| 299 | } |
| 300 | } |
| 301 | if (should_duplicate) { |
| 302 | Ref<Script> scr = sr; |
| 303 | if (scr.is_valid()) { |
| 304 | should_duplicate = false; |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | if (should_duplicate) { |
| 310 | if (thread_duplicate_remap_cache->has(sr)) { |
| 311 | return thread_duplicate_remap_cache->get(sr); |
| 312 | } else { |
| 313 | const Ref<Resource> &dupe = p_params.local_scene |
| 314 | ? sr->duplicate_for_local_scene(p_params.local_scene, *thread_duplicate_remap_cache) |
| 315 | : sr->_duplicate(p_params); |
| 316 | thread_duplicate_remap_cache->insert(sr, dupe); |
| 317 | return dupe; |
| 318 | } |
| 319 | } else { |
| 320 | return p_variant; |
| 321 | } |
| 322 | } break; |
| 323 | case Variant::ARRAY: { |
| 324 | const Array &src = p_variant; |
| 325 | Array dst; |
| 326 | if (src.is_typed()) { |
| 327 | dst.set_typed(src.get_element_type()); |
| 328 | } |
| 329 | dst.resize(src.size()); |
| 330 | for (int i = 0; i < src.size(); i++) { |
| 331 | dst[i] = _duplicate_recursive(src[i], p_params); |
| 332 | } |
| 333 | return dst; |
| 334 | } break; |
| 335 | case Variant::DICTIONARY: { |
| 336 | const Dictionary &src = p_variant; |
| 337 | Dictionary dst; |
| 338 | if (src.is_typed()) { |
| 339 | dst.set_typed(src.get_typed_key_builtin(), src.get_typed_key_class_name(), src.get_typed_key_script(), src.get_typed_value_builtin(), src.get_typed_value_class_name(), src.get_typed_value_script()); |
| 340 | } |
| 341 | for (const Variant &k : src.get_key_list()) { |
| 342 | const Variant &v = src[k]; |
| 343 | dst.set( |
| 344 | _duplicate_recursive(k, p_params), |
| 345 | _duplicate_recursive(v, p_params)); |
| 346 | } |
| 347 | return dst; |
| 348 | } break; |
| 349 | case Variant::PACKED_BYTE_ARRAY: |
| 350 | case Variant::PACKED_INT32_ARRAY: |
| 351 | case Variant::PACKED_INT64_ARRAY: |
| 352 | case Variant::PACKED_FLOAT32_ARRAY: |
| 353 | case Variant::PACKED_FLOAT64_ARRAY: |
| 354 | case Variant::PACKED_STRING_ARRAY: |
| 355 | case Variant::PACKED_VECTOR2_ARRAY: |
| 356 | case Variant::PACKED_VECTOR3_ARRAY: |
| 357 | case Variant::PACKED_COLOR_ARRAY: |
| 358 | case Variant::PACKED_VECTOR4_ARRAY: { |
| 359 | return p_variant.duplicate(); |
| 360 | } break; |
| 361 | default: { |
| 362 | return p_variant; |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | Ref<Resource> Resource::_duplicate(const DuplicateParams &p_params) const { |
| 368 | ERR_FAIL_COND_V_MSG(p_params.local_scene && p_params.subres_mode != RESOURCE_DEEP_DUPLICATE_MAX, Ref<Resource>(), "Duplication for local-to-scene can't specify a deep duplicate mode."); |
| 369 | |
| 370 | DuplicateRemapCacheT *remap_cache_backup = thread_duplicate_remap_cache; |
| 371 | bool remap_cache_needs_deallocation_backup = thread_duplicate_remap_cache_needs_deallocation; |
| 372 | |
| 373 | // These are for avoiding potential duplicates that can happen in custom code |
| 374 | // from participating in the same duplication session (remap cache). |
| 375 | #define BEFORE_USER_CODE thread_duplicate_remap_cache = nullptr; |
| 376 | #define AFTER_USER_CODE \ |
| 377 | thread_duplicate_remap_cache = remap_cache_backup; \ |
| 378 | thread_duplicate_remap_cache_needs_deallocation = remap_cache_needs_deallocation_backup; |
| 379 | |
| 380 | List<PropertyInfo> plist; |
| 381 | get_property_list(&plist); |
| 382 | |
| 383 | BEFORE_USER_CODE |
| 384 | Ref<Resource> r = Object::cast_to<Resource>(ClassDB::instantiate(get_class())); |
| 385 | AFTER_USER_CODE |
| 386 | ERR_FAIL_COND_V(r.is_null(), Ref<Resource>()); |
| 387 | |
| 388 | thread_duplicate_remap_cache->insert(Ref<Resource>(this), r); |
| 389 | |
| 390 | if (p_params.local_scene) { |
| 391 | r->local_scene = p_params.local_scene; |
| 392 | } |
| 393 | |
| 394 | // Duplicate script first, so the scripted properties are considered. |
| 395 | BEFORE_USER_CODE |
| 396 | r->set_script(get_script()); |
| 397 | AFTER_USER_CODE |
| 398 | |
| 399 | for (const PropertyInfo &E : plist) { |
| 400 | if (!(E.usage & PROPERTY_USAGE_STORAGE)) { |
| 401 | continue; |
| 402 | } |
| 403 | if (E.name == "script") { |
| 404 | continue; |
| 405 | } |
| 406 | |
| 407 | BEFORE_USER_CODE |
| 408 | Variant p = get(E.name); |
| 409 | AFTER_USER_CODE |
| 410 | |
| 411 | p = _duplicate_recursive(p, p_params, E.usage); |
| 412 | |
| 413 | BEFORE_USER_CODE |
| 414 | r->set(E.name, p); |
| 415 | AFTER_USER_CODE |
| 416 | } |
| 417 | |
| 418 | return r; |
| 419 | |
| 420 | #undef BEFORE_USER_CODE |
| 421 | #undef AFTER_USER_CODE |
| 422 | } |
| 423 | |
| 424 | Ref<Resource> Resource::duplicate_for_local_scene(Node *p_for_scene, DuplicateRemapCacheT &p_remap_cache) const { |
| 425 | #ifdef DEBUG_ENABLED |
| 426 | // The only possibilities for the remap cache passed being valid are these: |
| 427 | // a) It's the same already used as the one of the thread. That happens when this function |
| 428 | // is called within some recursion level within a duplication. |
| 429 | // b) There's no current thread remap cache, which means this function is acting as an entry point. |
| 430 | // This check failing means that this function is being called as an entry point during an ongoing |
| 431 | // duplication, likely due to custom instantiation or setter code. It would be an engine bug because |
| 432 | // code starting or joining a duplicate session must ensure to exit it temporarily when making calls |
| 433 | // that may in turn invoke such custom code. |
| 434 | if (thread_duplicate_remap_cache && &p_remap_cache != thread_duplicate_remap_cache) { |
| 435 | ERR_PRINT("Resource::duplicate_for_local_scene() called during an ongoing duplication session. This is an engine bug."); |
| 436 | } |
| 437 | #endif |
| 438 | |
| 439 | DuplicateRemapCacheT *remap_cache_backup = thread_duplicate_remap_cache; |
| 440 | bool remap_cache_needs_deallocation_backup = thread_duplicate_remap_cache_needs_deallocation; |
| 441 | thread_duplicate_remap_cache = &p_remap_cache; |
| 442 | thread_duplicate_remap_cache_needs_deallocation = false; |
| 443 | |
| 444 | DuplicateParams params; |
| 445 | params.deep = true; |
| 446 | params.local_scene = p_for_scene; |
| 447 | const Ref<Resource> &dupe = _duplicate(params); |
| 448 | |
| 449 | thread_duplicate_remap_cache = remap_cache_backup; |
| 450 | thread_duplicate_remap_cache_needs_deallocation = remap_cache_needs_deallocation_backup; |
| 451 | |
| 452 | return dupe; |
| 453 | } |
| 454 | |
| 455 | void Resource::_find_sub_resources(const Variant &p_variant, HashSet<Ref<Resource>> &p_resources_found) { |
| 456 | switch (p_variant.get_type()) { |
| 457 | case Variant::ARRAY: { |
| 458 | Array a = p_variant; |
| 459 | for (int i = 0; i < a.size(); i++) { |
| 460 | _find_sub_resources(a[i], p_resources_found); |
| 461 | } |
| 462 | } break; |
| 463 | case Variant::DICTIONARY: { |
| 464 | Dictionary d = p_variant; |
| 465 | for (const KeyValue<Variant, Variant> &kv : d) { |
| 466 | _find_sub_resources(kv.key, p_resources_found); |
| 467 | _find_sub_resources(kv.value, p_resources_found); |
| 468 | } |
| 469 | } break; |
| 470 | case Variant::OBJECT: { |
| 471 | Ref<Resource> r = p_variant; |
| 472 | if (r.is_valid()) { |
| 473 | p_resources_found.insert(r); |
| 474 | } |
| 475 | } break; |
| 476 | default: { |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | void Resource::configure_for_local_scene(Node *p_for_scene, DuplicateRemapCacheT &p_remap_cache) { |
| 482 | List<PropertyInfo> plist; |
| 483 | get_property_list(&plist); |
| 484 | |
| 485 | reset_local_to_scene(); |
| 486 | local_scene = p_for_scene; |
| 487 | |
| 488 | for (const PropertyInfo &E : plist) { |
| 489 | if (!(E.usage & PROPERTY_USAGE_STORAGE)) { |
| 490 | continue; |
| 491 | } |
| 492 | Variant p = get(E.name); |
| 493 | |
| 494 | HashSet<Ref<Resource>> sub_resources; |
| 495 | _find_sub_resources(p, sub_resources); |
| 496 | |
| 497 | for (Ref<Resource> sr : sub_resources) { |
| 498 | if (sr->is_local_to_scene()) { |
| 499 | if (!p_remap_cache.has(sr)) { |
| 500 | sr->configure_for_local_scene(p_for_scene, p_remap_cache); |
| 501 | p_remap_cache[sr] = sr; |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | Ref<Resource> Resource::duplicate(bool p_deep) const { |
| 509 | DuplicateRemapCacheT remap_cache; |
| 510 | bool started_session = false; |
| 511 | if (!thread_duplicate_remap_cache) { |
| 512 | thread_duplicate_remap_cache = &remap_cache; |
| 513 | thread_duplicate_remap_cache_needs_deallocation = false; |
| 514 | started_session = true; |
| 515 | } |
| 516 | |
| 517 | DuplicateParams params; |
| 518 | params.deep = p_deep; |
| 519 | params.subres_mode = RESOURCE_DEEP_DUPLICATE_INTERNAL; |
| 520 | const Ref<Resource> &dupe = _duplicate(params); |
| 521 | |
| 522 | if (started_session) { |
| 523 | thread_duplicate_remap_cache = nullptr; |
| 524 | } |
| 525 | |
| 526 | return dupe; |
| 527 | } |
| 528 | |
| 529 | Ref<Resource> Resource::duplicate_deep(ResourceDeepDuplicateMode p_deep_subresources_mode) const { |
| 530 | ERR_FAIL_INDEX_V(p_deep_subresources_mode, RESOURCE_DEEP_DUPLICATE_MAX, Ref<Resource>()); |
| 531 | |
| 532 | DuplicateRemapCacheT remap_cache; |
| 533 | bool started_session = false; |
| 534 | if (!thread_duplicate_remap_cache) { |
| 535 | thread_duplicate_remap_cache = &remap_cache; |
| 536 | thread_duplicate_remap_cache_needs_deallocation = false; |
| 537 | started_session = true; |
| 538 | } |
| 539 | |
| 540 | DuplicateParams params; |
| 541 | params.deep = true; |
| 542 | params.subres_mode = p_deep_subresources_mode; |
| 543 | const Ref<Resource> &dupe = _duplicate(params); |
| 544 | |
| 545 | if (started_session) { |
| 546 | thread_duplicate_remap_cache = nullptr; |
| 547 | } |
| 548 | |
| 549 | return dupe; |
| 550 | } |
| 551 | |
| 552 | Ref<Resource> Resource::_duplicate_deep_bind(DeepDuplicateMode p_deep_subresources_mode) const { |
| 553 | return _duplicate_from_variant(true, (ResourceDeepDuplicateMode)p_deep_subresources_mode, 0); |
| 554 | } |
| 555 | |
| 556 | Ref<Resource> Resource::_duplicate_from_variant(bool p_deep, ResourceDeepDuplicateMode p_deep_subresources_mode, int p_recursion_count) const { |
| 557 | // A call without deep duplication would have been early-rejected at Variant::duplicate() unless it's the root call. |
| 558 | DEV_ASSERT(!(p_recursion_count > 0 && p_deep_subresources_mode == RESOURCE_DEEP_DUPLICATE_NONE)); |
| 559 | |
| 560 | // When duplicating from Variant, this function may be called multiple times from |
| 561 | // different parts of the data structure being copied. Therefore, we need to create |
| 562 | // a remap cache instance in a way that can be shared among all of the calls. |
| 563 | // Whatever Variant, Array or Dictionary that initiated the call chain will eventually |
| 564 | // claim it, when the stack unwinds up to the root call. |
| 565 | // One exception is that this is the root call. |
| 566 | |
| 567 | if (p_recursion_count == 0) { |
| 568 | if (p_deep) { |
| 569 | return duplicate_deep(p_deep_subresources_mode); |
| 570 | } else { |
| 571 | return duplicate(false); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | if (thread_duplicate_remap_cache) { |
| 576 | Resource::DuplicateRemapCacheT::Iterator E = thread_duplicate_remap_cache->find(Ref<Resource>(this)); |
| 577 | if (E) { |
| 578 | return E->value; |
| 579 | } |
| 580 | } else { |
| 581 | thread_duplicate_remap_cache = memnew(DuplicateRemapCacheT); |
| 582 | thread_duplicate_remap_cache_needs_deallocation = true; |
| 583 | } |
| 584 | |
| 585 | DuplicateParams params; |
| 586 | params.deep = p_deep; |
| 587 | params.subres_mode = p_deep_subresources_mode; |
| 588 | |
| 589 | const Ref<Resource> dupe = _duplicate(params); |
| 590 | |
| 591 | return dupe; |
| 592 | } |
| 593 | |
| 594 | void Resource::_teardown_duplicate_from_variant() { |
| 595 | if (thread_duplicate_remap_cache && thread_duplicate_remap_cache_needs_deallocation) { |
| 596 | memdelete(thread_duplicate_remap_cache); |
| 597 | thread_duplicate_remap_cache = nullptr; |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | void Resource::_set_path(const String &p_path) { |
| 602 | set_path(p_path, false); |
| 603 | } |
| 604 | |
| 605 | void Resource::_take_over_path(const String &p_path) { |
| 606 | set_path(p_path, true); |
| 607 | } |
| 608 | |
| 609 | RID Resource::get_rid() const { |
| 610 | RID ret; |
| 611 | if (!GDVIRTUAL_CALL(_get_rid, ret)) { |
| 612 | #ifndef DISABLE_DEPRECATED |
| 613 | if (_get_extension() && _get_extension()->get_rid) { |
| 614 | ret = RID::from_uint64(_get_extension()->get_rid(_get_extension_instance())); |
| 615 | } |
| 616 | #endif |
| 617 | } |
| 618 | return ret; |
| 619 | } |
| 620 | |
| 621 | #ifdef TOOLS_ENABLED |
| 622 | |
| 623 | uint32_t Resource::hash_edited_version_for_preview() const { |
| 624 | uint32_t hash = hash_murmur3_one_32(get_edited_version()); |
| 625 | |
| 626 | List<PropertyInfo> plist; |
| 627 | get_property_list(&plist); |
| 628 | |
| 629 | for (const PropertyInfo &E : plist) { |
| 630 | if (E.usage & PROPERTY_USAGE_STORAGE && E.type == Variant::OBJECT && E.hint == PROPERTY_HINT_RESOURCE_TYPE) { |
| 631 | Ref<Resource> res = get(E.name); |
| 632 | if (res.is_valid()) { |
| 633 | hash = hash_murmur3_one_32(res->hash_edited_version_for_preview(), hash); |
| 634 | } |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | return hash; |
| 639 | } |
| 640 | |
| 641 | #endif |
| 642 | |
| 643 | void Resource::set_local_to_scene(bool p_enable) { |
| 644 | local_to_scene = p_enable; |
| 645 | } |
| 646 | |
| 647 | bool Resource::is_local_to_scene() const { |
| 648 | return local_to_scene; |
| 649 | } |
| 650 | |
| 651 | Node *Resource::get_local_scene() const { |
| 652 | if (local_scene) { |
| 653 | return local_scene; |
| 654 | } |
| 655 | |
| 656 | if (_get_local_scene_func) { |
| 657 | return _get_local_scene_func(); |
| 658 | } |
| 659 | |
| 660 | return nullptr; |
| 661 | } |
| 662 | |
| 663 | void Resource::setup_local_to_scene() { |
| 664 | emit_signal(SNAME("setup_local_to_scene_requested")); |
| 665 | GDVIRTUAL_CALL(_setup_local_to_scene); |
| 666 | } |
| 667 | |
| 668 | void Resource::reset_local_to_scene() { |
| 669 | // Restores the state as if setup_local_to_scene() hadn't been called. |
| 670 | } |
| 671 | |
| 672 | String Resource::_to_string() { |
| 673 | return (name.is_empty() ? "" : String(name) + " ") + "(" + path_cache + "):" + Object::_to_string(); |
| 674 | } |
| 675 | |
| 676 | Node *(*Resource::_get_local_scene_func)() = nullptr; |
| 677 | void (*Resource::_update_configuration_warning)() = nullptr; |
| 678 | |
| 679 | void Resource::set_as_translation_remapped(bool p_remapped) { |
| 680 | if (remapped_list.in_list() == p_remapped) { |
| 681 | return; |
| 682 | } |
| 683 | |
| 684 | MutexLock lock(ResourceCache::lock); |
| 685 | |
| 686 | if (p_remapped) { |
| 687 | ResourceLoader::remapped_list.add(&remapped_list); |
| 688 | } else { |
| 689 | ResourceLoader::remapped_list.remove(&remapped_list); |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | // Helps keep IDs the same when loading/saving scenes. An empty ID clears the entry, and an empty ID is returned when not found. |
| 694 | void Resource::set_resource_id_for_path(const String &p_referrer_path, const String &p_resource_path, const String &p_id) { |
| 695 | #ifdef TOOLS_ENABLED |
| 696 | if (p_id.is_empty()) { |
| 697 | ResourceCache::path_cache_lock.write_lock(); |
| 698 | ResourceCache::resource_path_cache[p_referrer_path].erase(p_resource_path); |
| 699 | ResourceCache::path_cache_lock.write_unlock(); |
| 700 | } else { |
| 701 | ResourceCache::path_cache_lock.write_lock(); |
| 702 | ResourceCache::resource_path_cache[p_referrer_path][p_resource_path] = p_id; |
| 703 | ResourceCache::path_cache_lock.write_unlock(); |
| 704 | } |
| 705 | #endif |
| 706 | } |
| 707 | |
| 708 | String Resource::get_id_for_path(const String &p_referrer_path) const { |
| 709 | #ifdef TOOLS_ENABLED |
| 710 | ResourceCache::path_cache_lock.read_lock(); |
| 711 | if (ResourceCache::resource_path_cache[p_referrer_path].has(get_path())) { |
| 712 | String result = ResourceCache::resource_path_cache[p_referrer_path][get_path()]; |
| 713 | ResourceCache::path_cache_lock.read_unlock(); |
| 714 | return result; |
| 715 | } else { |
| 716 | ResourceCache::path_cache_lock.read_unlock(); |
| 717 | return ""; |
| 718 | } |
| 719 | #else |
| 720 | return ""; |
| 721 | #endif |
| 722 | } |
| 723 | |
| 724 | void Resource::_bind_methods() { |
| 725 | ClassDB::bind_method(D_METHOD("set_path", "path"), &Resource::_set_path); |
| 726 | ClassDB::bind_method(D_METHOD("take_over_path", "path"), &Resource::_take_over_path); |
| 727 | ClassDB::bind_method(D_METHOD("get_path"), &Resource::get_path); |
| 728 | ClassDB::bind_method(D_METHOD("set_path_cache", "path"), &Resource::set_path_cache); |
| 729 | ClassDB::bind_method(D_METHOD("set_name", "name"), &Resource::set_name); |
| 730 | ClassDB::bind_method(D_METHOD("get_name"), &Resource::get_name); |
| 731 | ClassDB::bind_method(D_METHOD("get_rid"), &Resource::get_rid); |
| 732 | ClassDB::bind_method(D_METHOD("set_local_to_scene", "enable"), &Resource::set_local_to_scene); |
| 733 | ClassDB::bind_method(D_METHOD("is_local_to_scene"), &Resource::is_local_to_scene); |
| 734 | ClassDB::bind_method(D_METHOD("get_local_scene"), &Resource::get_local_scene); |
| 735 | ClassDB::bind_method(D_METHOD("setup_local_to_scene"), &Resource::setup_local_to_scene); |
| 736 | ClassDB::bind_method(D_METHOD("reset_state"), &Resource::reset_state); |
| 737 | |
| 738 | ClassDB::bind_method(D_METHOD("set_id_for_path", "path", "id"), &Resource::set_id_for_path); |
| 739 | ClassDB::bind_method(D_METHOD("get_id_for_path", "path"), &Resource::get_id_for_path); |
| 740 | |
| 741 | ClassDB::bind_method(D_METHOD("is_built_in"), &Resource::is_built_in); |
| 742 | |
| 743 | ClassDB::bind_static_method("Resource", D_METHOD("generate_scene_unique_id"), &Resource::generate_scene_unique_id); |
| 744 | ClassDB::bind_method(D_METHOD("set_scene_unique_id", "id"), &Resource::set_scene_unique_id); |
| 745 | ClassDB::bind_method(D_METHOD("get_scene_unique_id"), &Resource::get_scene_unique_id); |
| 746 | |
| 747 | ClassDB::bind_method(D_METHOD("emit_changed"), &Resource::emit_changed); |
| 748 | |
| 749 | ClassDB::bind_method(D_METHOD("duplicate", "deep"), &Resource::duplicate, DEFVAL(false)); |
| 750 | ClassDB::bind_method(D_METHOD("duplicate_deep", "deep_subresources_mode"), &Resource::_duplicate_deep_bind, DEFVAL(RESOURCE_DEEP_DUPLICATE_INTERNAL)); |
| 751 | |
| 752 | // For the bindings, it's much more natural to expose this enum from the Variant realm via Resource. |
| 753 | // Therefore, we can't use BIND_ENUM_CONSTANT here because we need some customization. |
| 754 | ClassDB::bind_integer_constant(get_class_static(), StringName("DeepDuplicateMode"), "DEEP_DUPLICATE_NONE", RESOURCE_DEEP_DUPLICATE_NONE); |
| 755 | ClassDB::bind_integer_constant(get_class_static(), StringName("DeepDuplicateMode"), "DEEP_DUPLICATE_INTERNAL", RESOURCE_DEEP_DUPLICATE_INTERNAL); |
| 756 | ClassDB::bind_integer_constant(get_class_static(), StringName("DeepDuplicateMode"), "DEEP_DUPLICATE_ALL", RESOURCE_DEEP_DUPLICATE_ALL); |
| 757 | |
| 758 | ADD_SIGNAL(MethodInfo("changed")); |
| 759 | ADD_SIGNAL(MethodInfo("setup_local_to_scene_requested")); |
| 760 | |
| 761 | ADD_GROUP("Resource", "resource_"); |
| 762 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "resource_local_to_scene"), "set_local_to_scene", "is_local_to_scene"); |
| 763 | ADD_PROPERTY(PropertyInfo(Variant::STRING, "resource_path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_path", "get_path"); |
| 764 | ADD_PROPERTY(PropertyInfo(Variant::STRING, "resource_name"), "set_name", "get_name"); |
| 765 | ADD_PROPERTY(PropertyInfo(Variant::STRING, "resource_scene_unique_id", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_scene_unique_id", "get_scene_unique_id"); |
| 766 | |
| 767 | GDVIRTUAL_BIND(_setup_local_to_scene); |
| 768 | GDVIRTUAL_BIND(_get_rid); |
| 769 | GDVIRTUAL_BIND(_reset_state); |
| 770 | GDVIRTUAL_BIND(_set_path_cache, "path"); |
| 771 | } |
| 772 | |
| 773 | Resource::Resource() : |
| 774 | remapped_list(this) { |
| 775 | _define_ancestry(AncestralClass::RESOURCE); |
| 776 | } |
| 777 | |
| 778 | Resource::~Resource() { |
| 779 | if (unlikely(path_cache.is_empty())) { |
| 780 | return; |
| 781 | } |
| 782 | |
| 783 | MutexLock lock(ResourceCache::lock); |
| 784 | // Only unregister from the cache if this is the actual resource listed there. |
| 785 | // (Other resources can have the same value in `path_cache` if loaded with `CACHE_IGNORE`.) |
| 786 | HashMap<String, Resource *>::Iterator E = ResourceCache::resources.find(path_cache); |
| 787 | if (likely(E && E->value == this)) { |
| 788 | ResourceCache::resources.remove(E); |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | HashMap<String, Resource *> ResourceCache::resources; |
| 793 | #ifdef TOOLS_ENABLED |
| 794 | HashMap<String, HashMap<String, String>> ResourceCache::resource_path_cache; |
| 795 | #endif |
| 796 | |
| 797 | Mutex ResourceCache::lock; |
| 798 | #ifdef TOOLS_ENABLED |
| 799 | RWLock ResourceCache::path_cache_lock; |
| 800 | #endif |
| 801 | |
| 802 | void ResourceCache::clear() { |
| 803 | if (!resources.is_empty()) { |
| 804 | if (OS::get_singleton()->is_stdout_verbose()) { |
| 805 | ERR_PRINT(vformat("%d resources still in use at exit.", resources.size())); |
| 806 | for (const KeyValue<String, Resource *> &E : resources) { |
| 807 | print_line(vformat("Resource still in use: %s (%s)", E.key, E.value->get_class())); |
| 808 | } |
| 809 | } else { |
| 810 | ERR_PRINT(vformat("%d resources still in use at exit (run with --verbose for details).", resources.size())); |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | resources.clear(); |
| 815 | } |
| 816 | |
| 817 | bool ResourceCache::has(const String &p_path) { |
| 818 | Resource **res = nullptr; |
| 819 | |
| 820 | { |
| 821 | MutexLock mutex_lock(lock); |
| 822 | |
| 823 | res = resources.getptr(p_path); |
| 824 | |
| 825 | if (res && (*res)->get_reference_count() == 0) { |
| 826 | // This resource is in the process of being deleted, ignore its existence. |
| 827 | (*res)->path_cache = String(); |
| 828 | resources.erase(p_path); |
| 829 | res = nullptr; |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | if (!res) { |
| 834 | return false; |
| 835 | } |
| 836 | |
| 837 | return true; |
| 838 | } |
| 839 | |
| 840 | Ref<Resource> ResourceCache::get_ref(const String &p_path) { |
| 841 | Ref<Resource> ref; |
| 842 | { |
| 843 | MutexLock mutex_lock(lock); |
| 844 | Resource **res = resources.getptr(p_path); |
| 845 | |
| 846 | if (res) { |
| 847 | ref = Ref<Resource>(*res); |
| 848 | } |
| 849 | |
| 850 | if (res && ref.is_null()) { |
| 851 | // This resource is in the process of being deleted, ignore its existence |
| 852 | (*res)->path_cache = String(); |
| 853 | resources.erase(p_path); |
| 854 | res = nullptr; |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | return ref; |
| 859 | } |
| 860 | |
| 861 | void ResourceCache::get_cached_resources(List<Ref<Resource>> *p_resources) { |
| 862 | MutexLock mutex_lock(lock); |
| 863 | |
| 864 | LocalVector<String> to_remove; |
| 865 | |
| 866 | for (KeyValue<String, Resource *> &E : resources) { |
| 867 | Ref<Resource> ref = Ref<Resource>(E.value); |
| 868 | |
| 869 | if (ref.is_null()) { |
| 870 | // This resource is in the process of being deleted, ignore its existence |
| 871 | E.value->path_cache = String(); |
| 872 | to_remove.push_back(E.key); |
| 873 | continue; |
| 874 | } |
| 875 | |
| 876 | p_resources->push_back(ref); |
| 877 | } |
| 878 | |
| 879 | for (const String &E : to_remove) { |
| 880 | resources.erase(E); |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | int ResourceCache::get_cached_resource_count() { |
| 885 | MutexLock mutex_lock(lock); |
| 886 | return resources.size(); |
| 887 | } |