Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/object/method_info.cpp
45997 views
1
/**************************************************************************/
2
/* method_info.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "method_info.h"
32
33
#include "core/variant/array.h"
34
#include "core/variant/dictionary.h"
35
#include "core/variant/typed_array.h" // IWYU pragma: keep. `convert_property_list` return type.
36
37
MethodInfo::operator Dictionary() const {
38
Dictionary d;
39
d["name"] = name;
40
d["args"] = convert_property_list(arguments);
41
Array da;
42
for (int i = 0; i < default_arguments.size(); i++) {
43
da.push_back(default_arguments[i]);
44
}
45
d["default_args"] = da;
46
d["flags"] = flags;
47
d["id"] = id;
48
Dictionary r = return_val;
49
d["return"] = r;
50
return d;
51
}
52
53
MethodInfo MethodInfo::from_dict(const Dictionary &p_dict) {
54
MethodInfo mi;
55
56
if (p_dict.has("name")) {
57
mi.name = p_dict["name"];
58
}
59
Array args;
60
if (p_dict.has("args")) {
61
args = p_dict["args"];
62
}
63
64
for (const Variant &arg : args) {
65
Dictionary d = arg;
66
mi.arguments.push_back(PropertyInfo::from_dict(d));
67
}
68
Array defargs;
69
if (p_dict.has("default_args")) {
70
defargs = p_dict["default_args"];
71
}
72
for (const Variant &defarg : defargs) {
73
mi.default_arguments.push_back(defarg);
74
}
75
76
if (p_dict.has("return")) {
77
mi.return_val = PropertyInfo::from_dict(p_dict["return"]);
78
}
79
80
if (p_dict.has("flags")) {
81
mi.flags = p_dict["flags"];
82
}
83
84
return mi;
85
}
86
87
uint32_t MethodInfo::get_compatibility_hash() const {
88
bool has_return = (return_val.type != Variant::NIL) || (return_val.usage & PROPERTY_USAGE_NIL_IS_VARIANT);
89
90
uint32_t hash = hash_murmur3_one_32(has_return);
91
hash = hash_murmur3_one_32(arguments.size(), hash);
92
93
if (has_return) {
94
hash = hash_murmur3_one_32(return_val.type, hash);
95
if (return_val.class_name != StringName()) {
96
hash = hash_murmur3_one_32(return_val.class_name.hash(), hash);
97
}
98
}
99
100
for (const PropertyInfo &arg : arguments) {
101
hash = hash_murmur3_one_32(arg.type, hash);
102
if (arg.class_name != StringName()) {
103
hash = hash_murmur3_one_32(arg.class_name.hash(), hash);
104
}
105
}
106
107
hash = hash_murmur3_one_32(default_arguments.size(), hash);
108
for (const Variant &v : default_arguments) {
109
hash = hash_murmur3_one_32(v.hash(), hash);
110
}
111
112
hash = hash_murmur3_one_32(flags & METHOD_FLAG_CONST ? 1 : 0, hash);
113
hash = hash_murmur3_one_32(flags & METHOD_FLAG_VARARG ? 1 : 0, hash);
114
115
return hash_fmix32(hash);
116
}
117
118