Skip to content

Commit 9853a69

Browse files
committed
Implement typed dictionaries
1 parent 906a4e9 commit 9853a69

File tree

86 files changed

+3072
-194
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+3072
-194
lines changed

core/core_constants.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,7 @@ void register_global_constants() {
671671
BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_INT_IS_OBJECTID);
672672
BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_INT_IS_POINTER);
673673
BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_ARRAY_TYPE);
674+
BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_DICTIONARY_TYPE);
674675
BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_LOCALE_ID);
675676
BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_LOCALIZABLE_STRING);
676677
BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_NODE_TYPE);

core/doc_data.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
String DocData::get_default_value_string(const Variant &p_value) {
3434
if (p_value.get_type() == Variant::ARRAY) {
3535
return Variant(Array(p_value, 0, StringName(), Variant())).get_construct_string().replace("\n", " ");
36+
} else if (p_value.get_type() == Variant::DICTIONARY) {
37+
return Variant(Dictionary(p_value, 0, StringName(), Variant(), 0, StringName(), Variant())).get_construct_string().replace("\n", " ");
3638
} else {
3739
return p_value.get_construct_string().replace("\n", " ");
3840
}
@@ -57,6 +59,8 @@ void DocData::return_doc_from_retinfo(DocData::MethodDoc &p_method, const Proper
5759
p_method.return_type = p_retinfo.class_name;
5860
} else if (p_retinfo.type == Variant::ARRAY && p_retinfo.hint == PROPERTY_HINT_ARRAY_TYPE) {
5961
p_method.return_type = p_retinfo.hint_string + "[]";
62+
} else if (p_retinfo.type == Variant::DICTIONARY && p_retinfo.hint == PROPERTY_HINT_DICTIONARY_TYPE) {
63+
p_method.return_type = "Dictionary[" + p_retinfo.hint_string.replace(";", ", ") + "]";
6064
} else if (p_retinfo.hint == PROPERTY_HINT_RESOURCE_TYPE) {
6165
p_method.return_type = p_retinfo.hint_string;
6266
} else if (p_retinfo.type == Variant::NIL && p_retinfo.usage & PROPERTY_USAGE_NIL_IS_VARIANT) {
@@ -89,6 +93,8 @@ void DocData::argument_doc_from_arginfo(DocData::ArgumentDoc &p_argument, const
8993
p_argument.type = p_arginfo.class_name;
9094
} else if (p_arginfo.type == Variant::ARRAY && p_arginfo.hint == PROPERTY_HINT_ARRAY_TYPE) {
9195
p_argument.type = p_arginfo.hint_string + "[]";
96+
} else if (p_arginfo.type == Variant::DICTIONARY && p_arginfo.hint == PROPERTY_HINT_DICTIONARY_TYPE) {
97+
p_argument.type = "Dictionary[" + p_arginfo.hint_string.replace(";", ", ") + "]";
9298
} else if (p_arginfo.hint == PROPERTY_HINT_RESOURCE_TYPE) {
9399
p_argument.type = p_arginfo.hint_string;
94100
} else if (p_arginfo.type == Variant::NIL) {

core/extension/extension_api_dump.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ static String get_property_info_type_name(const PropertyInfo &p_info) {
6060
if (p_info.type == Variant::ARRAY && (p_info.hint == PROPERTY_HINT_ARRAY_TYPE)) {
6161
return String("typedarray::") + p_info.hint_string;
6262
}
63+
if (p_info.type == Variant::DICTIONARY && (p_info.hint == PROPERTY_HINT_DICTIONARY_TYPE)) {
64+
return String("typeddictionary::") + p_info.hint_string;
65+
}
6366
if (p_info.type == Variant::INT && (p_info.usage & (PROPERTY_USAGE_CLASS_IS_ENUM))) {
6467
return String("enum::") + String(p_info.class_name);
6568
}

core/extension/gdextension_interface.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,15 @@ static GDExtensionVariantPtr gdextension_dictionary_operator_index_const(GDExten
11991199
return (GDExtensionVariantPtr)&self->operator[](*(const Variant *)p_key);
12001200
}
12011201

1202+
void gdextension_dictionary_set_typed(GDExtensionTypePtr p_self, GDExtensionVariantType p_key_type, GDExtensionConstStringNamePtr p_key_class_name, GDExtensionConstVariantPtr p_key_script, GDExtensionVariantType p_value_type, GDExtensionConstStringNamePtr p_value_class_name, GDExtensionConstVariantPtr p_value_script) {
1203+
Dictionary *self = reinterpret_cast<Dictionary *>(p_self);
1204+
const StringName *key_class_name = reinterpret_cast<const StringName *>(p_key_class_name);
1205+
const Variant *key_script = reinterpret_cast<const Variant *>(p_key_script);
1206+
const StringName *value_class_name = reinterpret_cast<const StringName *>(p_value_class_name);
1207+
const Variant *value_script = reinterpret_cast<const Variant *>(p_value_script);
1208+
self->set_typed((uint32_t)p_key_type, *key_class_name, *key_script, (uint32_t)p_value_type, *value_class_name, *value_script);
1209+
}
1210+
12021211
/* OBJECT API */
12031212

12041213
static void gdextension_object_method_bind_call(GDExtensionMethodBindPtr p_method_bind, GDExtensionObjectPtr p_instance, const GDExtensionConstVariantPtr *p_args, GDExtensionInt p_arg_count, GDExtensionUninitializedVariantPtr r_return, GDExtensionCallError *r_error) {
@@ -1679,6 +1688,7 @@ void gdextension_setup_interface() {
16791688
REGISTER_INTERFACE_FUNC(array_set_typed);
16801689
REGISTER_INTERFACE_FUNC(dictionary_operator_index);
16811690
REGISTER_INTERFACE_FUNC(dictionary_operator_index_const);
1691+
REGISTER_INTERFACE_FUNC(dictionary_set_typed);
16821692
REGISTER_INTERFACE_FUNC(object_method_bind_call);
16831693
REGISTER_INTERFACE_FUNC(object_method_bind_ptrcall);
16841694
REGISTER_INTERFACE_FUNC(object_destroy);

core/extension/gdextension_interface.h

+16
Original file line numberDiff line numberDiff line change
@@ -2372,6 +2372,22 @@ typedef GDExtensionVariantPtr (*GDExtensionInterfaceDictionaryOperatorIndex)(GDE
23722372
*/
23732373
typedef GDExtensionVariantPtr (*GDExtensionInterfaceDictionaryOperatorIndexConst)(GDExtensionConstTypePtr p_self, GDExtensionConstVariantPtr p_key);
23742374

2375+
/**
2376+
* @name dictionary_set_typed
2377+
* @since 4.4
2378+
*
2379+
* Makes a Dictionary into a typed Dictionary.
2380+
*
2381+
* @param p_self A pointer to the Dictionary.
2382+
* @param p_key_type The type of Variant the Dictionary key will store.
2383+
* @param p_key_class_name A pointer to a StringName with the name of the object (if p_key_type is GDEXTENSION_VARIANT_TYPE_OBJECT).
2384+
* @param p_key_script A pointer to a Script object (if p_key_type is GDEXTENSION_VARIANT_TYPE_OBJECT and the base class is extended by a script).
2385+
* @param p_value_type The type of Variant the Dictionary value will store.
2386+
* @param p_value_class_name A pointer to a StringName with the name of the object (if p_value_type is GDEXTENSION_VARIANT_TYPE_OBJECT).
2387+
* @param p_value_script A pointer to a Script object (if p_value_type is GDEXTENSION_VARIANT_TYPE_OBJECT and the base class is extended by a script).
2388+
*/
2389+
typedef void (*GDExtensionInterfaceDictionarySetTyped)(GDExtensionTypePtr p_self, GDExtensionVariantType p_key_type, GDExtensionConstStringNamePtr p_key_class_name, GDExtensionConstVariantPtr p_key_script, GDExtensionVariantType p_value_type, GDExtensionConstStringNamePtr p_value_class_name, GDExtensionConstVariantPtr p_value_script);
2390+
23752391
/* INTERFACE: Object */
23762392

23772393
/**

core/io/resource_format_binary.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,19 @@ Error ResourceLoaderBinary::load() {
857857
}
858858
}
859859

860+
if (value.get_type() == Variant::DICTIONARY) {
861+
Dictionary set_dict = value;
862+
bool is_get_valid = false;
863+
Variant get_value = res->get(name, &is_get_valid);
864+
if (is_get_valid && get_value.get_type() == Variant::DICTIONARY) {
865+
Dictionary get_dict = get_value;
866+
if (!set_dict.is_same_typed(get_dict)) {
867+
value = Dictionary(set_dict, get_dict.get_typed_key_builtin(), get_dict.get_typed_key_class_name(), get_dict.get_typed_key_script(),
868+
get_dict.get_typed_value_builtin(), get_dict.get_typed_value_class_name(), get_dict.get_typed_value_script());
869+
}
870+
}
871+
}
872+
860873
if (set_valid) {
861874
res->set(name, value);
862875
}
@@ -2064,6 +2077,8 @@ void ResourceFormatSaverBinaryInstance::_find_resources(const Variant &p_variant
20642077

20652078
case Variant::DICTIONARY: {
20662079
Dictionary d = p_variant;
2080+
_find_resources(d.get_typed_key_script());
2081+
_find_resources(d.get_typed_value_script());
20672082
List<Variant> keys;
20682083
d.get_key_list(&keys);
20692084
for (const Variant &E : keys) {

core/object/object.h

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ enum PropertyHint {
8686
PROPERTY_HINT_HIDE_QUATERNION_EDIT, /// Only Node3D::transform should hide the quaternion editor.
8787
PROPERTY_HINT_PASSWORD,
8888
PROPERTY_HINT_LAYERS_AVOIDANCE,
89+
PROPERTY_HINT_DICTIONARY_TYPE,
8990
PROPERTY_HINT_MAX,
9091
};
9192

0 commit comments

Comments
 (0)