Skip to content

Commit baadca7

Browse files
committed
Implement to/from dict helpers for PropertyInfo/MethodInfo
1 parent ad307e4 commit baadca7

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

include/godot_cpp/core/property_info.hpp

+34
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,40 @@ struct PropertyInfo {
7272
PropertyInfo(const GDExtensionPropertyInfo *p_info) :
7373
PropertyInfo(p_info->type, *reinterpret_cast<StringName *>(p_info->name), (PropertyHint)p_info->hint, *reinterpret_cast<String *>(p_info->hint_string), p_info->usage, *reinterpret_cast<StringName *>(p_info->class_name)) {}
7474

75+
operator Dictionary() const {
76+
Dictionary dict;
77+
dict["name"] = name;
78+
dict["class_name"] = class_name;
79+
dict["type"] = type;
80+
dict["hint"] = hint;
81+
dict["hint_string"] = hint_string;
82+
dict["usage"] = usage;
83+
return dict;
84+
}
85+
86+
static PropertyInfo from_dict(const Dictionary &p_dict) {
87+
PropertyInfo pi;
88+
if (p_dict.has("name")) {
89+
pi.name = p_dict["name"];
90+
}
91+
if (p_dict.has("name")) {
92+
pi.name = p_dict["name"];
93+
}
94+
if (p_dict.has("class_name")) {
95+
pi.class_name = p_dict["class_name"];
96+
}
97+
if (p_dict.has("hint")) {
98+
pi.hint = PropertyHint(int(p_dict["hint"]));
99+
}
100+
if (p_dict.has("hint_string")) {
101+
pi.hint_string = p_dict["hint_string"];
102+
}
103+
if (p_dict.has("usage")) {
104+
pi.usage = p_dict["usage"];
105+
}
106+
return pi;
107+
}
108+
75109
void _update(GDExtensionPropertyInfo *p_info) {
76110
p_info->type = (GDExtensionVariantType)type;
77111
*(reinterpret_cast<StringName *>(p_info->name)) = name;

src/core/object.cpp

+58
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,66 @@ Object *get_object_instance_binding(GodotObject *p_engine_object) {
6060
return reinterpret_cast<Object *>(gdextension_interface_object_get_instance_binding(p_engine_object, token, binding_callbacks));
6161
}
6262

63+
TypedArray<Dictionary> convert_property_list(const std::vector<PropertyInfo> &p_list) {
64+
TypedArray<Dictionary> va;
65+
for (const PropertyInfo &pi : p_list) {
66+
va.push_back(Dictionary(pi));
67+
}
68+
return va;
69+
}
70+
6371
} // namespace internal
6472

73+
MethodInfo::operator Dictionary() const {
74+
Dictionary dict;
75+
dict["name"] = name;
76+
dict["args"] = internal::convert_property_list(arguments);
77+
Array da;
78+
for (int i = 0; i < default_arguments.size(); i++) {
79+
da.push_back(default_arguments[i]);
80+
}
81+
dict["default_args"] = da;
82+
dict["flags"] = flags;
83+
dict["id"] = id;
84+
Dictionary r = return_val;
85+
dict["return"] = r;
86+
return dict;
87+
}
88+
89+
MethodInfo MethodInfo::from_dict(const Dictionary& p_dict) {
90+
MethodInfo mi;
91+
92+
if (p_dict.has("name")) {
93+
mi.name = p_dict["name"];
94+
}
95+
Array args;
96+
if (p_dict.has("args")) {
97+
args = p_dict["args"];
98+
}
99+
100+
for (int i = 0; i < args.size(); i++) {
101+
Dictionary d = args[i];
102+
mi.arguments.push_back(PropertyInfo::from_dict(d));
103+
}
104+
Array defargs;
105+
if (p_dict.has("default_args")) {
106+
defargs = p_dict["default_args"];
107+
}
108+
for (int i = 0; i < defargs.size(); i++) {
109+
mi.default_arguments.push_back(defargs[i]);
110+
}
111+
112+
if (p_dict.has("return")) {
113+
mi.return_val = PropertyInfo::from_dict(p_dict["return"]);
114+
}
115+
116+
if (p_dict.has("flags")) {
117+
mi.flags = p_dict["flags"];
118+
}
119+
120+
return mi;
121+
}
122+
65123
MethodInfo::MethodInfo() :
66124
flags(GDEXTENSION_METHOD_FLAG_NORMAL) {}
67125

0 commit comments

Comments
 (0)