mirror of
https://github.com/godotengine/godot.git
synced 2025-12-07 22:00:10 +00:00
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.
This commit is contained in:
parent
2a03b459b9
commit
342266cfd9
14 changed files with 264 additions and 54 deletions
|
|
@ -37,7 +37,6 @@
|
|||
#include "core/templates/vector.h"
|
||||
#include "core/variant/callable.h"
|
||||
#include "core/variant/dictionary.h"
|
||||
#include "core/variant/variant.h"
|
||||
|
||||
struct ArrayPrivate {
|
||||
SafeRefCount refcount;
|
||||
|
|
@ -518,10 +517,14 @@ const Variant &Array::get(int p_idx) const {
|
|||
}
|
||||
|
||||
Array Array::duplicate(bool p_deep) const {
|
||||
return recursive_duplicate(p_deep, 0);
|
||||
return recursive_duplicate(p_deep, RESOURCE_DEEP_DUPLICATE_NONE, 0);
|
||||
}
|
||||
|
||||
Array Array::recursive_duplicate(bool p_deep, int recursion_count) const {
|
||||
Array Array::duplicate_deep(ResourceDeepDuplicateMode p_deep_subresources_mode) const {
|
||||
return recursive_duplicate(true, p_deep_subresources_mode, 0);
|
||||
}
|
||||
|
||||
Array Array::recursive_duplicate(bool p_deep, ResourceDeepDuplicateMode p_deep_subresources_mode, int recursion_count) const {
|
||||
Array new_arr;
|
||||
new_arr._p->typed = _p->typed;
|
||||
|
||||
|
|
@ -531,12 +534,19 @@ Array Array::recursive_duplicate(bool p_deep, int recursion_count) const {
|
|||
}
|
||||
|
||||
if (p_deep) {
|
||||
bool is_call_chain_end = recursion_count == 0;
|
||||
|
||||
recursion_count++;
|
||||
int element_count = size();
|
||||
new_arr.resize(element_count);
|
||||
Variant *write = new_arr._p->array.ptrw();
|
||||
for (int i = 0; i < element_count; i++) {
|
||||
write[i] = get(i).recursive_duplicate(true, recursion_count);
|
||||
write[i] = get(i).recursive_duplicate(true, p_deep_subresources_mode, recursion_count);
|
||||
}
|
||||
|
||||
// Variant::recursive_duplicate() may have created a remap cache by now.
|
||||
if (is_call_chain_end) {
|
||||
Resource::_teardown_duplicate_from_variant();
|
||||
}
|
||||
} else {
|
||||
new_arr._p->array = _p->array;
|
||||
|
|
|
|||
|
|
@ -31,6 +31,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "core/typedefs.h"
|
||||
#include "core/variant/variant_deep_duplicate.h"
|
||||
|
||||
#include <climits>
|
||||
#include <initializer_list>
|
||||
|
|
@ -164,7 +165,8 @@ public:
|
|||
Variant pop_at(int p_pos);
|
||||
|
||||
Array duplicate(bool p_deep = false) const;
|
||||
Array recursive_duplicate(bool p_deep, int recursion_count) const;
|
||||
Array duplicate_deep(ResourceDeepDuplicateMode p_deep_subresources_mode = RESOURCE_DEEP_DUPLICATE_INTERNAL) const;
|
||||
Array recursive_duplicate(bool p_deep, ResourceDeepDuplicateMode p_deep_subresources_mode, int recursion_count) const;
|
||||
|
||||
Array slice(int p_begin, int p_end = INT_MAX, int p_step = 1, bool p_deep = false) const;
|
||||
Array filter(const Callable &p_callable) const;
|
||||
|
|
|
|||
|
|
@ -569,7 +569,11 @@ const Variant *Dictionary::next(const Variant *p_key) const {
|
|||
}
|
||||
|
||||
Dictionary Dictionary::duplicate(bool p_deep) const {
|
||||
return recursive_duplicate(p_deep, 0);
|
||||
return recursive_duplicate(p_deep, RESOURCE_DEEP_DUPLICATE_NONE, 0);
|
||||
}
|
||||
|
||||
Dictionary Dictionary::duplicate_deep(ResourceDeepDuplicateMode p_deep_subresources_mode) const {
|
||||
return recursive_duplicate(true, p_deep_subresources_mode, 0);
|
||||
}
|
||||
|
||||
void Dictionary::make_read_only() {
|
||||
|
|
@ -581,7 +585,7 @@ bool Dictionary::is_read_only() const {
|
|||
return _p->read_only != nullptr;
|
||||
}
|
||||
|
||||
Dictionary Dictionary::recursive_duplicate(bool p_deep, int recursion_count) const {
|
||||
Dictionary Dictionary::recursive_duplicate(bool p_deep, ResourceDeepDuplicateMode p_deep_subresources_mode, int recursion_count) const {
|
||||
Dictionary n;
|
||||
n._p->typed_key = _p->typed_key;
|
||||
n._p->typed_value = _p->typed_value;
|
||||
|
|
@ -592,9 +596,16 @@ Dictionary Dictionary::recursive_duplicate(bool p_deep, int recursion_count) con
|
|||
}
|
||||
|
||||
if (p_deep) {
|
||||
bool is_call_chain_end = recursion_count == 0;
|
||||
|
||||
recursion_count++;
|
||||
for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
|
||||
n[E.key.recursive_duplicate(true, recursion_count)] = E.value.recursive_duplicate(true, recursion_count);
|
||||
n[E.key.recursive_duplicate(true, p_deep_subresources_mode, recursion_count)] = E.value.recursive_duplicate(true, p_deep_subresources_mode, recursion_count);
|
||||
}
|
||||
|
||||
// Variant::recursive_duplicate() may have created a remap cache by now.
|
||||
if (is_call_chain_end) {
|
||||
Resource::_teardown_duplicate_from_variant();
|
||||
}
|
||||
} else {
|
||||
for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
|
||||
|
|
|
|||
|
|
@ -35,6 +35,7 @@
|
|||
#include "core/templates/local_vector.h"
|
||||
#include "core/templates/pair.h"
|
||||
#include "core/variant/array.h"
|
||||
#include "core/variant/variant_deep_duplicate.h"
|
||||
|
||||
class Variant;
|
||||
|
||||
|
|
@ -98,7 +99,8 @@ public:
|
|||
Array values() const;
|
||||
|
||||
Dictionary duplicate(bool p_deep = false) const;
|
||||
Dictionary recursive_duplicate(bool p_deep, int recursion_count) const;
|
||||
Dictionary duplicate_deep(ResourceDeepDuplicateMode p_deep_subresources_mode = RESOURCE_DEEP_DUPLICATE_INTERNAL) const;
|
||||
Dictionary recursive_duplicate(bool p_deep, ResourceDeepDuplicateMode p_deep_subresources_mode, int recursion_count) const;
|
||||
|
||||
void set_typed(const ContainerType &p_key_type, const ContainerType &p_value_type);
|
||||
void set_typed(uint32_t p_key_type, const StringName &p_key_class_name, const Variant &p_key_script, uint32_t p_value_type, const StringName &p_value_class_name, const Variant &p_value_script);
|
||||
|
|
|
|||
|
|
@ -61,6 +61,7 @@
|
|||
#include "core/variant/array.h"
|
||||
#include "core/variant/callable.h"
|
||||
#include "core/variant/dictionary.h"
|
||||
#include "core/variant/variant_deep_duplicate.h"
|
||||
|
||||
class Object;
|
||||
class RefCounted;
|
||||
|
|
@ -612,7 +613,8 @@ public:
|
|||
|
||||
void zero();
|
||||
Variant duplicate(bool p_deep = false) const;
|
||||
Variant recursive_duplicate(bool p_deep, int recursion_count) const;
|
||||
Variant duplicate_deep(ResourceDeepDuplicateMode p_deep_subresources_mode = RESOURCE_DEEP_DUPLICATE_INTERNAL) const;
|
||||
Variant recursive_duplicate(bool p_deep, ResourceDeepDuplicateMode p_deep_subresources_mode, int recursion_count) const;
|
||||
|
||||
/* Built-In Methods */
|
||||
|
||||
|
|
|
|||
|
|
@ -2398,6 +2398,7 @@ static void _register_variant_builtin_methods_misc() {
|
|||
bind_method(Dictionary, keys, sarray(), varray());
|
||||
bind_method(Dictionary, values, sarray(), varray());
|
||||
bind_method(Dictionary, duplicate, sarray("deep"), varray(false));
|
||||
bind_method(Dictionary, duplicate_deep, sarray("deep_subresources_mode"), varray(RESOURCE_DEEP_DUPLICATE_INTERNAL));
|
||||
bind_method(Dictionary, get, sarray("key", "default"), varray(Variant()));
|
||||
bind_method(Dictionary, get_or_add, sarray("key", "default"), varray(Variant()));
|
||||
bind_method(Dictionary, set, sarray("key", "value"), varray());
|
||||
|
|
@ -2456,6 +2457,7 @@ static void _register_variant_builtin_methods_array() {
|
|||
bind_method(Array, bsearch_custom, sarray("value", "func", "before"), varray(true));
|
||||
bind_method(Array, reverse, sarray(), varray());
|
||||
bind_method(Array, duplicate, sarray("deep"), varray(false));
|
||||
bind_method(Array, duplicate_deep, sarray("deep_subresources_mode"), varray(RESOURCE_DEEP_DUPLICATE_INTERNAL));
|
||||
bind_method(Array, slice, sarray("begin", "end", "step", "deep"), varray(INT_MAX, 1, false));
|
||||
bind_method(Array, filter, sarray("method"), varray());
|
||||
bind_method(Array, map, sarray("method"), varray());
|
||||
|
|
|
|||
41
core/variant/variant_deep_duplicate.h
Normal file
41
core/variant/variant_deep_duplicate.h
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
/**************************************************************************/
|
||||
/* variant_deep_duplicate.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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
// This would be ideally declared nested in Variant, but that would cause circular
|
||||
// includes with Array and Dictionary, for instance.
|
||||
// Also, this enum is be exposed via Resource.
|
||||
enum ResourceDeepDuplicateMode {
|
||||
RESOURCE_DEEP_DUPLICATE_NONE,
|
||||
RESOURCE_DEEP_DUPLICATE_INTERNAL,
|
||||
RESOURCE_DEEP_DUPLICATE_ALL,
|
||||
RESOURCE_DEEP_DUPLICATE_MAX
|
||||
};
|
||||
|
|
@ -29,9 +29,10 @@
|
|||
/**************************************************************************/
|
||||
|
||||
#include "variant_setget.h"
|
||||
|
||||
#include "variant_callable.h"
|
||||
|
||||
#include "core/io/resource.h"
|
||||
|
||||
struct VariantSetterGetterInfo {
|
||||
void (*setter)(Variant *base, const Variant *value, bool &valid);
|
||||
void (*getter)(const Variant *base, Variant *value);
|
||||
|
|
@ -1969,26 +1970,33 @@ Variant Variant::iter_get(const Variant &r_iter, bool &r_valid) const {
|
|||
}
|
||||
|
||||
Variant Variant::duplicate(bool p_deep) const {
|
||||
return recursive_duplicate(p_deep, 0);
|
||||
return recursive_duplicate(p_deep, RESOURCE_DEEP_DUPLICATE_NONE, 0);
|
||||
}
|
||||
|
||||
Variant Variant::recursive_duplicate(bool p_deep, int recursion_count) const {
|
||||
Variant Variant::duplicate_deep(ResourceDeepDuplicateMode p_deep_subresources_mode) const {
|
||||
ERR_FAIL_INDEX_V(p_deep_subresources_mode, RESOURCE_DEEP_DUPLICATE_MAX, Variant());
|
||||
return recursive_duplicate(true, p_deep_subresources_mode, 0);
|
||||
}
|
||||
|
||||
Variant Variant::recursive_duplicate(bool p_deep, ResourceDeepDuplicateMode p_deep_subresources_mode, int recursion_count) const {
|
||||
switch (type) {
|
||||
case OBJECT: {
|
||||
/* breaks stuff :(
|
||||
if (p_deep && !_get_obj().ref.is_null()) {
|
||||
Ref<Resource> resource = _get_obj().ref;
|
||||
if (resource.is_valid()) {
|
||||
return resource->duplicate(true);
|
||||
}
|
||||
// If the root target of duplicate() is a Resource, we can't early-reject because that
|
||||
// resource itself must be duplicated, much as if Resource::duplicate() had been called.
|
||||
if (p_deep_subresources_mode == RESOURCE_DEEP_DUPLICATE_NONE && recursion_count > 0) {
|
||||
return *this;
|
||||
}
|
||||
Resource *res = Object::cast_to<Resource>(_get_obj().obj);
|
||||
if (res) {
|
||||
return res->_duplicate_from_variant(p_deep, p_deep_subresources_mode, recursion_count);
|
||||
} else {
|
||||
return *this;
|
||||
}
|
||||
*/
|
||||
return *this;
|
||||
} break;
|
||||
case DICTIONARY:
|
||||
return operator Dictionary().recursive_duplicate(p_deep, recursion_count);
|
||||
return operator Dictionary().recursive_duplicate(p_deep, p_deep_subresources_mode, recursion_count);
|
||||
case ARRAY:
|
||||
return operator Array().recursive_duplicate(p_deep, recursion_count);
|
||||
return operator Array().recursive_duplicate(p_deep, p_deep_subresources_mode, recursion_count);
|
||||
case PACKED_BYTE_ARRAY:
|
||||
return operator Vector<uint8_t>().duplicate();
|
||||
case PACKED_INT32_ARRAY:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue