Skip to content

Commit a0f9f5d

Browse files
committed
Merge pull request #107770 from RandomShaper/fix_res_dupe_bindings
Enhance bindings of deep resource duplication
2 parents e557456 + 7dc37bd commit a0f9f5d

File tree

6 files changed

+25
-13
lines changed

6 files changed

+25
-13
lines changed

core/io/resource.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,10 @@ Ref<Resource> Resource::duplicate_deep(ResourceDeepDuplicateMode p_deep_subresou
535535
return dupe;
536536
}
537537

538+
Ref<Resource> Resource::_duplicate_deep_bind(DeepDuplicateMode p_deep_subresources_mode) const {
539+
return _duplicate_from_variant(true, (ResourceDeepDuplicateMode)p_deep_subresources_mode, 0);
540+
}
541+
538542
Ref<Resource> Resource::_duplicate_from_variant(bool p_deep, ResourceDeepDuplicateMode p_deep_subresources_mode, int p_recursion_count) const {
539543
// A call without deep duplication would have been early-rejected at Variant::duplicate() unless it's the root call.
540544
DEV_ASSERT(!(p_recursion_count > 0 && p_deep_subresources_mode == RESOURCE_DEEP_DUPLICATE_NONE));
@@ -724,12 +728,13 @@ void Resource::_bind_methods() {
724728
ClassDB::bind_method(D_METHOD("emit_changed"), &Resource::emit_changed);
725729

726730
ClassDB::bind_method(D_METHOD("duplicate", "deep"), &Resource::duplicate, DEFVAL(false));
727-
ClassDB::bind_method(D_METHOD("duplicate_deep", "deep_subresources_mode"), &Resource::duplicate_deep, DEFVAL(RESOURCE_DEEP_DUPLICATE_INTERNAL));
731+
ClassDB::bind_method(D_METHOD("duplicate_deep", "deep_subresources_mode"), &Resource::_duplicate_deep_bind, DEFVAL(RESOURCE_DEEP_DUPLICATE_INTERNAL));
728732

729733
// For the bindings, it's much more natural to expose this enum from the Variant realm via Resource.
730-
ClassDB::bind_integer_constant(get_class_static(), StringName("ResourceDeepDuplicateMode"), "RESOURCE_DEEP_DUPLICATE_NONE", RESOURCE_DEEP_DUPLICATE_NONE);
731-
ClassDB::bind_integer_constant(get_class_static(), StringName("ResourceDeepDuplicateMode"), "RESOURCE_DEEP_DUPLICATE_INTERNAL", RESOURCE_DEEP_DUPLICATE_INTERNAL);
732-
ClassDB::bind_integer_constant(get_class_static(), StringName("ResourceDeepDuplicateMode"), "RESOURCE_DEEP_DUPLICATE_ALL", RESOURCE_DEEP_DUPLICATE_ALL);
734+
// Therefore, we can't use BIND_ENUM_CONSTANT here because we need some customization.
735+
ClassDB::bind_integer_constant(get_class_static(), StringName("DeepDuplicateMode"), "DEEP_DUPLICATE_NONE", RESOURCE_DEEP_DUPLICATE_NONE);
736+
ClassDB::bind_integer_constant(get_class_static(), StringName("DeepDuplicateMode"), "DEEP_DUPLICATE_INTERNAL", RESOURCE_DEEP_DUPLICATE_INTERNAL);
737+
ClassDB::bind_integer_constant(get_class_static(), StringName("DeepDuplicateMode"), "DEEP_DUPLICATE_ALL", RESOURCE_DEEP_DUPLICATE_ALL);
733738

734739
ADD_SIGNAL(MethodInfo("changed"));
735740
ADD_SIGNAL(MethodInfo("setup_local_to_scene_requested"));

core/io/resource.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ class Resource : public RefCounted {
9696
Variant _duplicate_recursive(const Variant &p_variant, const DuplicateParams &p_params, uint32_t p_usage = 0) const;
9797
void _find_sub_resources(const Variant &p_variant, HashSet<Ref<Resource>> &p_resources_found);
9898

99+
// Only for binding the deep duplicate method, so it doesn't need actual members.
100+
enum DeepDuplicateMode : int;
101+
102+
_ALWAYS_INLINE_ Ref<Resource> _duplicate_deep_bind(DeepDuplicateMode p_deep_subresources_mode) const;
103+
99104
protected:
100105
virtual void _resource_path_changed();
101106
static void _bind_methods();
@@ -183,7 +188,7 @@ class Resource : public RefCounted {
183188
~Resource();
184189
};
185190

186-
VARIANT_ENUM_CAST(ResourceDeepDuplicateMode);
191+
VARIANT_ENUM_CAST(Resource::DeepDuplicateMode);
187192

188193
class ResourceCache {
189194
friend class Resource;

core/variant/variant_call.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2262,6 +2262,8 @@ static void _register_variant_builtin_methods_math() {
22622262
bind_static_method(Color, from_rgba8, sarray("r8", "g8", "b8", "a8"), varray(255));
22632263
}
22642264

2265+
VARIANT_ENUM_CAST(ResourceDeepDuplicateMode);
2266+
22652267
static void _register_variant_builtin_methods_misc() {
22662268
/* RID */
22672269

doc/classes/Array.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@
341341
<param index="0" name="deep_subresources_mode" type="int" default="1" />
342342
<description>
343343
Duplicates this array, deeply, like [method duplicate][code](true)[/code], with extra control over how subresources are handled.
344-
[param deep_subresources_mode] must be one of the values from [enum Resource.ResourceDeepDuplicateMode]. By default, only internal resources will be duplicated (recursively).
344+
[param deep_subresources_mode] must be one of the values from [enum Resource.DeepDuplicateMode]. By default, only internal resources will be duplicated (recursively).
345345
</description>
346346
</method>
347347
<method name="erase">

doc/classes/Dictionary.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@
197197
<param index="0" name="deep_subresources_mode" type="int" default="1" />
198198
<description>
199199
Duplicates this dictionary, deeply, like [method duplicate][code](true)[/code], with extra control over how subresources are handled.
200-
[param deep_subresources_mode] must be one of the values from [enum Resource.ResourceDeepDuplicateMode]. By default, only internal resources will be duplicated (recursively).
200+
[param deep_subresources_mode] must be one of the values from [enum Resource.DeepDuplicateMode]. By default, only internal resources will be duplicated (recursively).
201201
</description>
202202
</method>
203203
<method name="erase">

doc/classes/Resource.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
<description>
5555
Duplicates this resource, returning a new resource with its [code]export[/code]ed or [constant PROPERTY_USAGE_STORAGE] properties copied from the original.
5656
If [param deep] is [code]false[/code], a [b]shallow[/b] copy is returned: nested [Array], [Dictionary], and [Resource] properties are not duplicated and are shared with the original resource.
57-
If [param deep] is [code]true[/code], a [b]deep[/b] copy is returned: all nested arrays, dictionaries, and packed arrays are also duplicated (recursively). Any [Resource] found inside will only be duplicated if it's local, like [constant RESOURCE_DEEP_DUPLICATE_INTERNAL] used with [method duplicate_deep].
57+
If [param deep] is [code]true[/code], a [b]deep[/b] copy is returned: all nested arrays, dictionaries, and packed arrays are also duplicated (recursively). Any [Resource] found inside will only be duplicated if it's local, like [constant DEEP_DUPLICATE_INTERNAL] used with [method duplicate_deep].
5858
The following exceptions apply:
5959
- Subresource properties with the [constant PROPERTY_USAGE_ALWAYS_DUPLICATE] flag are always duplicated (recursively or not, depending on [param deep]).
6060
- Subresource properties with the [constant PROPERTY_USAGE_NEVER_DUPLICATE] flag are never duplicated.
@@ -64,10 +64,10 @@
6464
</method>
6565
<method name="duplicate_deep" qualifiers="const">
6666
<return type="Resource" />
67-
<param index="0" name="deep_subresources_mode" type="int" enum="ResourceDeepDuplicateMode" default="1" />
67+
<param index="0" name="deep_subresources_mode" type="int" enum="Resource.DeepDuplicateMode" default="1" />
6868
<description>
6969
Duplicates this resource, deeply, like [method duplicate][code](true)[/code], with extra control over how subresources are handled.
70-
[param deep_subresources_mode] must be one of the values from [enum ResourceDeepDuplicateMode].
70+
[param deep_subresources_mode] must be one of the values from [enum DeepDuplicateMode].
7171
</description>
7272
</method>
7373
<method name="emit_changed">
@@ -186,13 +186,13 @@
186186
</signal>
187187
</signals>
188188
<constants>
189-
<constant name="RESOURCE_DEEP_DUPLICATE_NONE" value="0" enum="ResourceDeepDuplicateMode">
189+
<constant name="DEEP_DUPLICATE_NONE" value="0" enum="DeepDuplicateMode">
190190
No subresorces at all are duplicated. This is useful even in a deep duplication to have all the arrays and dictionaries duplicated but still pointing to the original resources.
191191
</constant>
192-
<constant name="RESOURCE_DEEP_DUPLICATE_INTERNAL" value="1" enum="ResourceDeepDuplicateMode">
192+
<constant name="DEEP_DUPLICATE_INTERNAL" value="1" enum="DeepDuplicateMode">
193193
Only subresources without a path or with a scene-local path will be duplicated.
194194
</constant>
195-
<constant name="RESOURCE_DEEP_DUPLICATE_ALL" value="2" enum="ResourceDeepDuplicateMode">
195+
<constant name="DEEP_DUPLICATE_ALL" value="2" enum="DeepDuplicateMode">
196196
Every subresource found will be duplicated, even if it has a non-local path. In other words, even potentially big resources stored separately will be duplicated.
197197
</constant>
198198
</constants>

0 commit comments

Comments
 (0)