Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/mono/class_db_api_json.cpp
10277 views
1
/**************************************************************************/
2
/* class_db_api_json.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 "class_db_api_json.h"
32
33
#ifdef DEBUG_ENABLED
34
35
#include "core/config/project_settings.h"
36
#include "core/io/file_access.h"
37
#include "core/io/json.h"
38
#include "core/version.h"
39
40
void class_db_api_to_json(const String &p_output_file, ClassDB::APIType p_api) {
41
Dictionary classes_dict;
42
43
List<StringName> class_list;
44
ClassDB::get_class_list(&class_list);
45
// Must be alphabetically sorted for hash to compute.
46
class_list.sort_custom<StringName::AlphCompare>();
47
48
for (const StringName &E : class_list) {
49
ClassDB::ClassInfo *t = ClassDB::classes.getptr(E);
50
ERR_FAIL_NULL(t);
51
if (t->api != p_api || !t->exposed) {
52
continue;
53
}
54
55
Dictionary class_dict;
56
classes_dict[t->name] = class_dict;
57
58
class_dict["inherits"] = t->inherits;
59
60
{ //methods
61
62
List<StringName> snames;
63
64
for (const KeyValue<StringName, MethodBind *> &F : t->method_map) {
65
String name = F.key.operator String();
66
67
ERR_CONTINUE(name.is_empty());
68
69
if (name[0] == '_') {
70
continue; // Ignore non-virtual methods that start with an underscore
71
}
72
73
snames.push_back(F.key);
74
}
75
76
snames.sort_custom<StringName::AlphCompare>();
77
78
Array methods;
79
80
for (const StringName &F : snames) {
81
Dictionary method_dict;
82
methods.push_back(method_dict);
83
84
MethodBind *mb = t->method_map[F];
85
method_dict["name"] = mb->get_name();
86
method_dict["argument_count"] = mb->get_argument_count();
87
method_dict["return_type"] = mb->get_argument_type(-1);
88
89
Array arguments;
90
method_dict["arguments"] = arguments;
91
92
for (int i = 0; i < mb->get_argument_count(); i++) {
93
Dictionary argument_dict;
94
arguments.push_back(argument_dict);
95
const PropertyInfo info = mb->get_argument_info(i);
96
argument_dict["type"] = info.type;
97
argument_dict["name"] = info.name;
98
argument_dict["hint"] = info.hint;
99
argument_dict["hint_string"] = info.hint_string;
100
}
101
102
method_dict["default_argument_count"] = mb->get_default_argument_count();
103
104
Array default_arguments;
105
method_dict["default_arguments"] = default_arguments;
106
107
for (int i = 0; i < mb->get_default_argument_count(); i++) {
108
Dictionary default_argument_dict;
109
default_arguments.push_back(default_argument_dict);
110
//hash should not change, i hope for tis
111
Variant da = mb->get_default_argument(i);
112
default_argument_dict["value"] = da;
113
}
114
115
method_dict["hint_flags"] = mb->get_hint_flags();
116
}
117
118
if (!methods.is_empty()) {
119
class_dict["methods"] = methods;
120
}
121
}
122
123
{ //constants
124
125
List<StringName> snames;
126
127
for (const KeyValue<StringName, int64_t> &F : t->constant_map) {
128
snames.push_back(F.key);
129
}
130
131
snames.sort_custom<StringName::AlphCompare>();
132
133
Array constants;
134
135
for (const StringName &F : snames) {
136
Dictionary constant_dict;
137
constants.push_back(constant_dict);
138
139
constant_dict["name"] = F;
140
constant_dict["value"] = t->constant_map[F];
141
}
142
143
if (!constants.is_empty()) {
144
class_dict["constants"] = constants;
145
}
146
}
147
148
{ //signals
149
150
List<StringName> snames;
151
152
for (const KeyValue<StringName, MethodInfo> &F : t->signal_map) {
153
snames.push_back(F.key);
154
}
155
156
snames.sort_custom<StringName::AlphCompare>();
157
158
Array signals;
159
160
for (const StringName &F : snames) {
161
Dictionary signal_dict;
162
signals.push_back(signal_dict);
163
164
MethodInfo &mi = t->signal_map[F];
165
signal_dict["name"] = F;
166
167
Array arguments;
168
signal_dict["arguments"] = arguments;
169
for (const PropertyInfo &pi : mi.arguments) {
170
Dictionary argument_dict;
171
arguments.push_back(argument_dict);
172
argument_dict["type"] = pi.type;
173
}
174
}
175
176
if (!signals.is_empty()) {
177
class_dict["signals"] = signals;
178
}
179
}
180
181
{ //properties
182
183
List<StringName> snames;
184
185
for (const KeyValue<StringName, ClassDB::PropertySetGet> &F : t->property_setget) {
186
snames.push_back(F.key);
187
}
188
189
snames.sort_custom<StringName::AlphCompare>();
190
191
Array properties;
192
193
for (const StringName &F : snames) {
194
Dictionary property_dict;
195
properties.push_back(property_dict);
196
197
ClassDB::PropertySetGet *psg = t->property_setget.getptr(F);
198
199
property_dict["name"] = F;
200
property_dict["setter"] = psg->setter;
201
property_dict["getter"] = psg->getter;
202
}
203
204
if (!properties.is_empty()) {
205
class_dict["property_setget"] = properties;
206
}
207
}
208
209
Array property_list;
210
211
//property list
212
for (const PropertyInfo &F : t->property_list) {
213
Dictionary property_dict;
214
property_list.push_back(property_dict);
215
216
property_dict["name"] = F.name;
217
property_dict["type"] = F.type;
218
property_dict["hint"] = F.hint;
219
property_dict["hint_string"] = F.hint_string;
220
property_dict["usage"] = F.usage;
221
}
222
223
if (!property_list.is_empty()) {
224
class_dict["property_list"] = property_list;
225
}
226
}
227
228
Ref<FileAccess> f = FileAccess::open(p_output_file, FileAccess::WRITE);
229
ERR_FAIL_COND_MSG(f.is_null(), "Cannot open file '" + p_output_file + "'.");
230
f->store_string(JSON::stringify(classes_dict, "\t"));
231
232
print_line(String() + "ClassDB API JSON written to: " + ProjectSettings::get_singleton()->globalize_path(p_output_file));
233
}
234
235
#endif // DEBUG_ENABLED
236
237