2014-02-09 22:10:30 -03:00
/**************************************************************************/
/* resource.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
2018-01-05 00:50:27 +01:00
2014-02-09 22:10:30 -03:00
# pragma once
2021-07-23 16:01:18 -03:00
# include "core/io/resource_uid.h"
2020-11-07 19:33:38 -03:00
# include "core/object/class_db.h"
2022-10-08 14:47:58 +02:00
# include "core/object/gdvirtual.gen.inc"
2021-06-04 18:03:15 +02:00
# include "core/object/ref_counted.h"
2020-11-07 19:33:38 -03:00
# include "core/templates/safe_refcount.h"
# include "core/templates/self_list.h"
2014-02-09 22:10:30 -03:00
2022-01-25 08:37:41 -07:00
class Node ;
2014-02-09 22:10:30 -03:00
# define RES_BASE_EXTENSION(m_ext) \
public : \
2017-01-02 23:03:46 -03:00
static void register_custom_data_to_otdb ( ) { \
ClassDB : : add_resource_base_extension ( m_ext , get_class_static ( ) ) ; \
} \
2020-07-10 11:34:39 +01:00
virtual String get_base_extension ( ) const override { \
return m_ext ; \
} \
2017-03-05 16:44:50 +01:00
\
2014-02-09 22:10:30 -03:00
private :
2021-06-04 18:03:15 +02:00
class Resource : public RefCounted {
GDCLASS ( Resource , RefCounted ) ;
2014-02-09 22:10:30 -03:00
2020-07-10 11:34:39 +01:00
public :
2025-09-22 01:30:57 +02:00
static constexpr AncestralClass static_ancestral_class = AncestralClass : : RESOURCE ;
2020-07-10 11:34:39 +01:00
static void register_custom_data_to_otdb ( ) { ClassDB : : add_resource_base_extension ( " res " , get_class_static ( ) ) ; }
virtual String get_base_extension ( ) const { return " res " ; }
Overhaul `Resource::duplicate()`
Thanks to a refactor, `Resource::duplicate_for_local_scene()` and `Resource::duplicate()` are now both users of the same, parametrized, implementation.
`Resource::duplicate()` now honors deepness in a more consistent and predictable fashion. `Resource::duplicate_deep()` is added (instead of just adding a parameter to the former, for compatibility needs).
The behavior after this change is as follows:
- Deep (`deep=true`, formerly `subresources=true`):
- Previously, only resources found as direct property values of the one to copy would be, recursively, duplicated.
- Now, in addition, arrays and dictionaries are walked so the copy is truly deep, and only local subresources found across are copied.
- Previously, subresources would be duplicated as many times as being referenced throughout the main resource.
- Now, each subresource is only duplicated once and from that point, a referenced to that single copy is used. That's the enhanced behavior that `duplicate_for_local_scene()` already featured.
- The behavior with respect to packed arrays is still duplication.
- Formerly, arrays and dictionaries were recursive duplicated, with resources ignored.
- Now, arrays and dictionaries are recursive duplicated, with resources duplicated.
- When doing it through `duplicate_deep()`, there's a` deep_subresources_mode` parameter, with various possibilites to control if no resources are duplicated (so arrays, etc. are, but keeping referencing the originals), if only the internal ones are (resources with no non-local path, the default), or if all of them are. The default is to copy every subresource, just like `duplicate(true)`.
- Not deep (`deep=false`, formerly `subresources=false`): <a name="resource-shallow"></a>
- Previously, the first level of resources found as direct property values would be duplicated unconditionally. Packed arrays, arrays and dictionaries were non-recursively duplicated.
- Now, no subresource found at any level in any form will be duplicated, but the original reference kept instead. Packed arrays, arrays and dictionaries are referenced, not duplicated at all.
- Now, resources found as values of always-duplicate properties are duplicated, recursively or not matching what was requested for the root call.
This commit also changes what's the virtual method to override to customize the duplication (now it's the protected `_duplicate()` instead of the public `duplicate()`).
2025-01-21 10:25:11 +01:00
protected :
struct DuplicateParams {
bool deep = false ;
ResourceDeepDuplicateMode subres_mode = RESOURCE_DEEP_DUPLICATE_MAX ;
Node * local_scene = nullptr ;
} ;
2020-07-10 11:34:39 +01:00
private :
2014-02-09 22:10:30 -03:00
friend class ResBase ;
2016-03-09 00:00:52 +01:00
friend class ResourceCache ;
2014-02-09 22:10:30 -03:00
String name ;
String path_cache ;
2021-07-20 21:36:56 -03:00
String scene_unique_id ;
2014-02-09 22:10:30 -03:00
# ifdef TOOLS_ENABLED
2020-05-12 17:01:17 +02:00
uint64_t last_modified_time = 0 ;
uint64_t import_last_modified_time = 0 ;
2017-02-01 09:45:45 -03:00
String import_path ;
2014-02-09 22:10:30 -03:00
# endif
2025-02-10 11:49:26 +02:00
enum EmitChangedState {
EMIT_CHANGED_UNBLOCKED ,
EMIT_CHANGED_BLOCKED ,
EMIT_CHANGED_BLOCKED_PENDING_EMIT ,
} ;
EmitChangedState emit_changed_state = EMIT_CHANGED_UNBLOCKED ;
2020-05-12 17:01:17 +02:00
bool local_to_scene = false ;
2017-01-10 01:04:31 -03:00
friend class SceneState ;
2020-05-12 17:01:17 +02:00
Node * local_scene = nullptr ;
2017-01-10 01:04:31 -03:00
2017-06-28 17:00:18 -03:00
SelfList < Resource > remapped_list ;
Overhaul `Variant::duplicate()` for resources
This in the scope of a duplication triggered via any type in the `Variant` realm. that is, the following: `Variant` itself, `Array` and `Dictionary`. That includes invoking `duplicate()` from scripts.
A `duplicate_deep(deep_subresources_mode)` method is added to `Variant`, `Array` and `Dictionary` (for compatibility reasons, simply adding an extra parameter was not possible). The default value for it is `RESOURCE_DEEP_DUPLICATE_NONE`, which is like calling `duplicate(true)`.
Remarks:
- The results of copying resources via those `Variant` types are exactly the same as if the copy were initiated from the `Resource` type at C++.
- In order to keep some separation between `Variant` and the higher-level animal which is `Resource`, `Variant` still contains the original code for that, so it's self-sufficient unless there's a `Resource` involved. Once the deep copy finds a `Resource` that has to be copied according to the duplication parameters, the algorithm invokes the `Resource` duplication machinery. When the stack is unwind back to a nesting level `Variant` can handle, `Variant` duplication logic keeps functioning.
While that is good from a responsibility separation standpoint, that would have a caveat: `Variant` would not be aware of the mapping between original and duplicate subresources and so wouldn't be able to keep preventing multiple duplicates.
To avoid that, this commit also introduces a wormwhole, a sharing mechanism by which `Variant` and `Resource` can collaborate in managing the lifetime of the original-to-duplicates map. The user-visible benefit is that the overduplicate prevention works as broadly as the whole `Variant` entity being copied, including all nesting levels, regardless how disconnected the data members containing resources may be across al the nesting levels. In other words, despite the aforementioned division of duties between `Variant` and `Resource` duplication logic, the duplicates map is shared among them. It's created when first finding a `Resource` and, however how deep the copy was working at that point, the map kept alive unitl the stack is unwind to the root user call, until the first step of the recursion.
Thanks to that common map of duplicates, this commit is able to fix the issue that `Resource::duplicate_for_local_scene()` used to ignore overridden duplicate logic.
2025-01-21 11:55:23 +01:00
using DuplicateRemapCacheT = HashMap < Ref < Resource > , Ref < Resource > > ;
static thread_local inline DuplicateRemapCacheT * thread_duplicate_remap_cache = nullptr ;
Overhaul `Resource::duplicate()`
Thanks to a refactor, `Resource::duplicate_for_local_scene()` and `Resource::duplicate()` are now both users of the same, parametrized, implementation.
`Resource::duplicate()` now honors deepness in a more consistent and predictable fashion. `Resource::duplicate_deep()` is added (instead of just adding a parameter to the former, for compatibility needs).
The behavior after this change is as follows:
- Deep (`deep=true`, formerly `subresources=true`):
- Previously, only resources found as direct property values of the one to copy would be, recursively, duplicated.
- Now, in addition, arrays and dictionaries are walked so the copy is truly deep, and only local subresources found across are copied.
- Previously, subresources would be duplicated as many times as being referenced throughout the main resource.
- Now, each subresource is only duplicated once and from that point, a referenced to that single copy is used. That's the enhanced behavior that `duplicate_for_local_scene()` already featured.
- The behavior with respect to packed arrays is still duplication.
- Formerly, arrays and dictionaries were recursive duplicated, with resources ignored.
- Now, arrays and dictionaries are recursive duplicated, with resources duplicated.
- When doing it through `duplicate_deep()`, there's a` deep_subresources_mode` parameter, with various possibilites to control if no resources are duplicated (so arrays, etc. are, but keeping referencing the originals), if only the internal ones are (resources with no non-local path, the default), or if all of them are. The default is to copy every subresource, just like `duplicate(true)`.
- Not deep (`deep=false`, formerly `subresources=false`): <a name="resource-shallow"></a>
- Previously, the first level of resources found as direct property values would be duplicated unconditionally. Packed arrays, arrays and dictionaries were non-recursively duplicated.
- Now, no subresource found at any level in any form will be duplicated, but the original reference kept instead. Packed arrays, arrays and dictionaries are referenced, not duplicated at all.
- Now, resources found as values of always-duplicate properties are duplicated, recursively or not matching what was requested for the root call.
This commit also changes what's the virtual method to override to customize the duplication (now it's the protected `_duplicate()` instead of the public `duplicate()`).
2025-01-21 10:25:11 +01:00
Variant _duplicate_recursive ( const Variant & p_variant , const DuplicateParams & p_params , uint32_t p_usage = 0 ) const ;
2023-01-17 21:14:19 +01:00
void _find_sub_resources ( const Variant & p_variant , HashSet < Ref < Resource > > & p_resources_found ) ;
2025-06-20 17:55:15 +02:00
// Only for binding the deep duplicate method, so it doesn't need actual members.
enum DeepDuplicateMode : int ;
_ALWAYS_INLINE_ Ref < Resource > _duplicate_deep_bind ( DeepDuplicateMode p_deep_subresources_mode ) const ;
2014-02-09 22:10:30 -03:00
protected :
virtual void _resource_path_changed ( ) ;
static void _bind_methods ( ) ;
2014-06-30 01:28:05 -03:00
2025-02-10 11:49:26 +02:00
void _block_emit_changed ( ) ;
void _unblock_emit_changed ( ) ;
2014-06-30 01:28:05 -03:00
void _set_path ( const String & p_path ) ;
void _take_over_path ( const String & p_path ) ;
2019-07-16 09:46:40 -07:00
2023-07-08 19:38:27 +08:00
virtual void reset_local_to_scene ( ) ;
2022-10-08 14:47:58 +02:00
GDVIRTUAL0 ( _setup_local_to_scene ) ;
2023-07-08 19:38:27 +08:00
2024-01-17 15:03:52 +01:00
GDVIRTUAL0RC ( RID , _get_rid ) ;
2024-04-19 13:26:21 -04:00
GDVIRTUAL1C ( _set_path_cache , String ) ;
GDVIRTUAL0 ( _reset_state ) ;
Overhaul `Resource::duplicate()`
Thanks to a refactor, `Resource::duplicate_for_local_scene()` and `Resource::duplicate()` are now both users of the same, parametrized, implementation.
`Resource::duplicate()` now honors deepness in a more consistent and predictable fashion. `Resource::duplicate_deep()` is added (instead of just adding a parameter to the former, for compatibility needs).
The behavior after this change is as follows:
- Deep (`deep=true`, formerly `subresources=true`):
- Previously, only resources found as direct property values of the one to copy would be, recursively, duplicated.
- Now, in addition, arrays and dictionaries are walked so the copy is truly deep, and only local subresources found across are copied.
- Previously, subresources would be duplicated as many times as being referenced throughout the main resource.
- Now, each subresource is only duplicated once and from that point, a referenced to that single copy is used. That's the enhanced behavior that `duplicate_for_local_scene()` already featured.
- The behavior with respect to packed arrays is still duplication.
- Formerly, arrays and dictionaries were recursive duplicated, with resources ignored.
- Now, arrays and dictionaries are recursive duplicated, with resources duplicated.
- When doing it through `duplicate_deep()`, there's a` deep_subresources_mode` parameter, with various possibilites to control if no resources are duplicated (so arrays, etc. are, but keeping referencing the originals), if only the internal ones are (resources with no non-local path, the default), or if all of them are. The default is to copy every subresource, just like `duplicate(true)`.
- Not deep (`deep=false`, formerly `subresources=false`): <a name="resource-shallow"></a>
- Previously, the first level of resources found as direct property values would be duplicated unconditionally. Packed arrays, arrays and dictionaries were non-recursively duplicated.
- Now, no subresource found at any level in any form will be duplicated, but the original reference kept instead. Packed arrays, arrays and dictionaries are referenced, not duplicated at all.
- Now, resources found as values of always-duplicate properties are duplicated, recursively or not matching what was requested for the root call.
This commit also changes what's the virtual method to override to customize the duplication (now it's the protected `_duplicate()` instead of the public `duplicate()`).
2025-01-21 10:25:11 +01:00
virtual Ref < Resource > _duplicate ( const DuplicateParams & p_params ) const ;
2017-03-05 16:44:50 +01:00
public :
2025-02-06 22:16:18 +02:00
static Node * ( * _get_local_scene_func ) ( ) ; // Used by the editor.
static void ( * _update_configuration_warning ) ( ) ; // Used by the editor.
2017-01-10 01:04:31 -03:00
2021-04-27 12:43:49 -03:00
void update_configuration_warning ( ) ;
2016-06-27 13:17:20 -03:00
virtual bool editor_can_reload_from_file ( ) ;
2025-02-06 22:16:18 +02:00
virtual void reset_state ( ) ; // For resources that store state in non-exposed properties, such as via _validate_property or _get_property_list, this function must be implemented to clear them.
2021-02-11 14:18:45 -03:00
virtual Error copy_from ( const Ref < Resource > & p_resource ) ;
2014-02-09 22:10:30 -03:00
virtual void reload_from_file ( ) ;
2023-07-03 21:29:37 +02:00
void emit_changed ( ) ;
void connect_changed ( const Callable & p_callable , uint32_t p_flags = 0 ) ;
void disconnect_changed ( const Callable & p_callable ) ;
2014-02-09 22:10:30 -03:00
void set_name ( const String & p_name ) ;
String get_name ( ) const ;
2015-10-10 09:09:09 -03:00
virtual void set_path ( const String & p_path , bool p_take_over = false ) ;
2014-02-09 22:10:30 -03:00
String get_path ( ) const ;
2024-02-22 12:53:19 +01:00
virtual void set_path_cache ( const String & p_path ) ; // Set raw path without involving resource cache.
2022-02-03 21:48:38 +05:45
_FORCE_INLINE_ bool is_built_in ( ) const { return path_cache . is_empty ( ) | | path_cache . contains ( " :: " ) | | path_cache . begins_with ( " local:// " ) ; }
2014-02-09 22:10:30 -03:00
2024-09-23 15:07:00 +02:00
static void seed_scene_unique_id ( uint32_t p_seed ) ;
2021-07-20 21:36:56 -03:00
static String generate_scene_unique_id ( ) ;
void set_scene_unique_id ( const String & p_id ) ;
String get_scene_unique_id ( ) const ;
2015-06-22 00:03:19 -03:00
Overhaul `Resource::duplicate()`
Thanks to a refactor, `Resource::duplicate_for_local_scene()` and `Resource::duplicate()` are now both users of the same, parametrized, implementation.
`Resource::duplicate()` now honors deepness in a more consistent and predictable fashion. `Resource::duplicate_deep()` is added (instead of just adding a parameter to the former, for compatibility needs).
The behavior after this change is as follows:
- Deep (`deep=true`, formerly `subresources=true`):
- Previously, only resources found as direct property values of the one to copy would be, recursively, duplicated.
- Now, in addition, arrays and dictionaries are walked so the copy is truly deep, and only local subresources found across are copied.
- Previously, subresources would be duplicated as many times as being referenced throughout the main resource.
- Now, each subresource is only duplicated once and from that point, a referenced to that single copy is used. That's the enhanced behavior that `duplicate_for_local_scene()` already featured.
- The behavior with respect to packed arrays is still duplication.
- Formerly, arrays and dictionaries were recursive duplicated, with resources ignored.
- Now, arrays and dictionaries are recursive duplicated, with resources duplicated.
- When doing it through `duplicate_deep()`, there's a` deep_subresources_mode` parameter, with various possibilites to control if no resources are duplicated (so arrays, etc. are, but keeping referencing the originals), if only the internal ones are (resources with no non-local path, the default), or if all of them are. The default is to copy every subresource, just like `duplicate(true)`.
- Not deep (`deep=false`, formerly `subresources=false`): <a name="resource-shallow"></a>
- Previously, the first level of resources found as direct property values would be duplicated unconditionally. Packed arrays, arrays and dictionaries were non-recursively duplicated.
- Now, no subresource found at any level in any form will be duplicated, but the original reference kept instead. Packed arrays, arrays and dictionaries are referenced, not duplicated at all.
- Now, resources found as values of always-duplicate properties are duplicated, recursively or not matching what was requested for the root call.
This commit also changes what's the virtual method to override to customize the duplication (now it's the protected `_duplicate()` instead of the public `duplicate()`).
2025-01-21 10:25:11 +01:00
Ref < Resource > duplicate ( bool p_deep = false ) const ;
Ref < Resource > duplicate_deep ( ResourceDeepDuplicateMode p_deep_subresources_mode = RESOURCE_DEEP_DUPLICATE_INTERNAL ) const ;
Overhaul `Variant::duplicate()` for resources
This in the scope of a duplication triggered via any type in the `Variant` realm. that is, the following: `Variant` itself, `Array` and `Dictionary`. That includes invoking `duplicate()` from scripts.
A `duplicate_deep(deep_subresources_mode)` method is added to `Variant`, `Array` and `Dictionary` (for compatibility reasons, simply adding an extra parameter was not possible). The default value for it is `RESOURCE_DEEP_DUPLICATE_NONE`, which is like calling `duplicate(true)`.
Remarks:
- The results of copying resources via those `Variant` types are exactly the same as if the copy were initiated from the `Resource` type at C++.
- In order to keep some separation between `Variant` and the higher-level animal which is `Resource`, `Variant` still contains the original code for that, so it's self-sufficient unless there's a `Resource` involved. Once the deep copy finds a `Resource` that has to be copied according to the duplication parameters, the algorithm invokes the `Resource` duplication machinery. When the stack is unwind back to a nesting level `Variant` can handle, `Variant` duplication logic keeps functioning.
While that is good from a responsibility separation standpoint, that would have a caveat: `Variant` would not be aware of the mapping between original and duplicate subresources and so wouldn't be able to keep preventing multiple duplicates.
To avoid that, this commit also introduces a wormwhole, a sharing mechanism by which `Variant` and `Resource` can collaborate in managing the lifetime of the original-to-duplicates map. The user-visible benefit is that the overduplicate prevention works as broadly as the whole `Variant` entity being copied, including all nesting levels, regardless how disconnected the data members containing resources may be across al the nesting levels. In other words, despite the aforementioned division of duties between `Variant` and `Resource` duplication logic, the duplicates map is shared among them. It's created when first finding a `Resource` and, however how deep the copy was working at that point, the map kept alive unitl the stack is unwind to the root user call, until the first step of the recursion.
Thanks to that common map of duplicates, this commit is able to fix the issue that `Resource::duplicate_for_local_scene()` used to ignore overridden duplicate logic.
2025-01-21 11:55:23 +01:00
Ref < Resource > _duplicate_from_variant ( bool p_deep , ResourceDeepDuplicateMode p_deep_subresources_mode , int p_recursion_count ) const ;
static void _teardown_duplicate_from_variant ( ) ;
Overhaul `Resource::duplicate()`
Thanks to a refactor, `Resource::duplicate_for_local_scene()` and `Resource::duplicate()` are now both users of the same, parametrized, implementation.
`Resource::duplicate()` now honors deepness in a more consistent and predictable fashion. `Resource::duplicate_deep()` is added (instead of just adding a parameter to the former, for compatibility needs).
The behavior after this change is as follows:
- Deep (`deep=true`, formerly `subresources=true`):
- Previously, only resources found as direct property values of the one to copy would be, recursively, duplicated.
- Now, in addition, arrays and dictionaries are walked so the copy is truly deep, and only local subresources found across are copied.
- Previously, subresources would be duplicated as many times as being referenced throughout the main resource.
- Now, each subresource is only duplicated once and from that point, a referenced to that single copy is used. That's the enhanced behavior that `duplicate_for_local_scene()` already featured.
- The behavior with respect to packed arrays is still duplication.
- Formerly, arrays and dictionaries were recursive duplicated, with resources ignored.
- Now, arrays and dictionaries are recursive duplicated, with resources duplicated.
- When doing it through `duplicate_deep()`, there's a` deep_subresources_mode` parameter, with various possibilites to control if no resources are duplicated (so arrays, etc. are, but keeping referencing the originals), if only the internal ones are (resources with no non-local path, the default), or if all of them are. The default is to copy every subresource, just like `duplicate(true)`.
- Not deep (`deep=false`, formerly `subresources=false`): <a name="resource-shallow"></a>
- Previously, the first level of resources found as direct property values would be duplicated unconditionally. Packed arrays, arrays and dictionaries were non-recursively duplicated.
- Now, no subresource found at any level in any form will be duplicated, but the original reference kept instead. Packed arrays, arrays and dictionaries are referenced, not duplicated at all.
- Now, resources found as values of always-duplicate properties are duplicated, recursively or not matching what was requested for the root call.
This commit also changes what's the virtual method to override to customize the duplication (now it's the protected `_duplicate()` instead of the public `duplicate()`).
2025-01-21 10:25:11 +01:00
Ref < Resource > duplicate_for_local_scene ( Node * p_for_scene , HashMap < Ref < Resource > , Ref < Resource > > & p_remap_cache ) const ;
2023-01-17 21:14:19 +01:00
void configure_for_local_scene ( Node * p_for_scene , HashMap < Ref < Resource > , Ref < Resource > > & p_remap_cache ) ;
2017-01-10 01:04:31 -03:00
void set_local_to_scene ( bool p_enable ) ;
bool is_local_to_scene ( ) const ;
virtual void setup_local_to_scene ( ) ;
2014-02-09 22:10:30 -03:00
2017-01-10 01:04:31 -03:00
Node * get_local_scene ( ) const ;
2016-05-27 14:18:40 -03:00
2014-02-09 22:10:30 -03:00
# ifdef TOOLS_ENABLED
2024-03-07 17:45:13 +01:00
virtual uint32_t hash_edited_version_for_preview ( ) const ;
2016-05-27 14:18:40 -03:00
2015-12-13 20:39:01 -03:00
virtual void set_last_modified_time ( uint64_t p_time ) { last_modified_time = p_time ; }
2014-02-09 22:10:30 -03:00
uint64_t get_last_modified_time ( ) const { return last_modified_time ; }
2017-02-01 09:45:45 -03:00
virtual void set_import_last_modified_time ( uint64_t p_time ) { import_last_modified_time = p_time ; }
uint64_t get_import_last_modified_time ( ) const { return import_last_modified_time ; }
void set_import_path ( const String & p_path ) { import_path = p_path ; }
String get_import_path ( ) const { return import_path ; }
2014-02-09 22:10:30 -03:00
# endif
2017-06-28 17:00:18 -03:00
void set_as_translation_remapped ( bool p_remapped ) ;
2025-02-06 22:16:18 +02:00
virtual RID get_rid ( ) const ; // Some resources may offer conversion to RID.
2014-02-09 22:10:30 -03:00
2025-02-06 22:16:18 +02:00
// Helps keep IDs the same when loading/saving scenes. An empty ID clears the entry, and an empty ID is returned when not found.
2025-07-30 13:01:37 +02:00
static void set_resource_id_for_path ( const String & p_referrer_path , const String & p_resource_path , const String & p_id ) ;
void set_id_for_path ( const String & p_referrer_path , const String & p_id ) { set_resource_id_for_path ( p_referrer_path , get_path ( ) , p_id ) ; }
String get_id_for_path ( const String & p_referrer_path ) const ;
2019-04-11 14:35:23 -03:00
2016-03-09 00:00:52 +01:00
Resource ( ) ;
2014-02-09 22:10:30 -03:00
~ Resource ( ) ;
} ;
2025-06-20 17:55:15 +02:00
VARIANT_ENUM_CAST ( Resource : : DeepDuplicateMode ) ;
Overhaul `Resource::duplicate()`
Thanks to a refactor, `Resource::duplicate_for_local_scene()` and `Resource::duplicate()` are now both users of the same, parametrized, implementation.
`Resource::duplicate()` now honors deepness in a more consistent and predictable fashion. `Resource::duplicate_deep()` is added (instead of just adding a parameter to the former, for compatibility needs).
The behavior after this change is as follows:
- Deep (`deep=true`, formerly `subresources=true`):
- Previously, only resources found as direct property values of the one to copy would be, recursively, duplicated.
- Now, in addition, arrays and dictionaries are walked so the copy is truly deep, and only local subresources found across are copied.
- Previously, subresources would be duplicated as many times as being referenced throughout the main resource.
- Now, each subresource is only duplicated once and from that point, a referenced to that single copy is used. That's the enhanced behavior that `duplicate_for_local_scene()` already featured.
- The behavior with respect to packed arrays is still duplication.
- Formerly, arrays and dictionaries were recursive duplicated, with resources ignored.
- Now, arrays and dictionaries are recursive duplicated, with resources duplicated.
- When doing it through `duplicate_deep()`, there's a` deep_subresources_mode` parameter, with various possibilites to control if no resources are duplicated (so arrays, etc. are, but keeping referencing the originals), if only the internal ones are (resources with no non-local path, the default), or if all of them are. The default is to copy every subresource, just like `duplicate(true)`.
- Not deep (`deep=false`, formerly `subresources=false`): <a name="resource-shallow"></a>
- Previously, the first level of resources found as direct property values would be duplicated unconditionally. Packed arrays, arrays and dictionaries were non-recursively duplicated.
- Now, no subresource found at any level in any form will be duplicated, but the original reference kept instead. Packed arrays, arrays and dictionaries are referenced, not duplicated at all.
- Now, resources found as values of always-duplicate properties are duplicated, recursively or not matching what was requested for the root call.
This commit also changes what's the virtual method to override to customize the duplication (now it's the protected `_duplicate()` instead of the public `duplicate()`).
2025-01-21 10:25:11 +01:00
2014-02-09 22:10:30 -03:00
class ResourceCache {
2016-03-09 00:00:52 +01:00
friend class Resource ;
2025-02-06 22:16:18 +02:00
friend class ResourceLoader ; // Need the lock.
2022-06-22 13:46:46 +02:00
static Mutex lock ;
2016-03-09 00:00:52 +01:00
static HashMap < String , Resource * > resources ;
2019-07-16 09:46:40 -07:00
# ifdef TOOLS_ENABLED
2021-07-20 21:36:56 -03:00
static HashMap < String , HashMap < String , String > > resource_path_cache ; // Each tscn has a set of resource paths and IDs.
2021-01-18 14:01:38 +01:00
static RWLock path_cache_lock ;
2019-07-16 09:46:40 -07:00
# endif // TOOLS_ENABLED
2014-02-09 22:10:30 -03:00
friend void unregister_core_types ( ) ;
static void clear ( ) ;
2017-01-07 18:25:37 -03:00
friend void register_core_types ( ) ;
2014-02-09 22:10:30 -03:00
2017-03-05 16:44:50 +01:00
public :
2014-02-09 22:10:30 -03:00
static bool has ( const String & p_path ) ;
2022-06-22 13:46:46 +02:00
static Ref < Resource > get_ref ( const String & p_path ) ;
2020-03-17 07:33:00 +01:00
static void get_cached_resources ( List < Ref < Resource > > * p_resources ) ;
2014-02-09 22:10:30 -03:00
static int get_cached_resource_count ( ) ;
} ;