summaryrefslogtreecommitdiff
path: root/modules/mono/class_db_api_json.cpp
blob: 01d0e135158cc4399666a232f7bc5218670f83f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*************************************************************************/
/*  class_db_api_json.cpp                                                */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                      https://godotengine.org                          */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur.                 */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md).   */
/*                                                                       */
/* 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.                */
/*************************************************************************/

#include "class_db_api_json.h"

#ifdef DEBUG_METHODS_ENABLED

#include "core/config/project_settings.h"
#include "core/io/file_access.h"
#include "core/io/json.h"
#include "core/version.h"

void class_db_api_to_json(const String &p_output_file, ClassDB::APIType p_api) {
	Dictionary classes_dict;

	List<StringName> names;

	const StringName *k = nullptr;

	while ((k = ClassDB::classes.next(k))) {
		names.push_back(*k);
	}
	//must be alphabetically sorted for hash to compute
	names.sort_custom<StringName::AlphCompare>();

	for (StringName &E : names) {
		ClassDB::ClassInfo *t = ClassDB::classes.getptr(E);
		ERR_FAIL_COND(!t);
		if (t->api != p_api || !t->exposed) {
			continue;
		}

		Dictionary class_dict;
		classes_dict[t->name] = class_dict;

		class_dict["inherits"] = t->inherits;

		{ //methods

			List<StringName> snames;

			k = nullptr;

			while ((k = t->method_map.next(k))) {
				String name = k->operator String();

				ERR_CONTINUE(name.is_empty());

				if (name[0] == '_') {
					continue; // Ignore non-virtual methods that start with an underscore
				}

				snames.push_back(*k);
			}

			snames.sort_custom<StringName::AlphCompare>();

			Array methods;

			for (StringName &F : snames) {
				Dictionary method_dict;
				methods.push_back(method_dict);

				MethodBind *mb = t->method_map[F];
				method_dict["name"] = mb->get_name();
				method_dict["argument_count"] = mb->get_argument_count();
				method_dict["return_type"] = mb->get_argument_type(-1);

				Array arguments;
				method_dict["arguments"] = arguments;

				for (int i = 0; i < mb->get_argument_count(); i++) {
					Dictionary argument_dict;
					arguments.push_back(argument_dict);
					const PropertyInfo info = mb->get_argument_info(i);
					argument_dict["type"] = info.type;
					argument_dict["name"] = info.name;
					argument_dict["hint"] = info.hint;
					argument_dict["hint_string"] = info.hint_string;
				}

				method_dict["default_argument_count"] = mb->get_default_argument_count();

				Array default_arguments;
				method_dict["default_arguments"] = default_arguments;

				for (int i = 0; i < mb->get_default_argument_count(); i++) {
					Dictionary default_argument_dict;
					default_arguments.push_back(default_argument_dict);
					//hash should not change, i hope for tis
					Variant da = mb->get_default_argument(i);
					default_argument_dict["value"] = da;
				}

				method_dict["hint_flags"] = mb->get_hint_flags();
			}

			if (!methods.is_empty()) {
				class_dict["methods"] = methods;
			}
		}

		{ //constants

			List<StringName> snames;

			k = nullptr;

			while ((k = t->constant_map.next(k))) {
				snames.push_back(*k);
			}

			snames.sort_custom<StringName::AlphCompare>();

			Array constants;

			for (StringName &F : snames) {
				Dictionary constant_dict;
				constants.push_back(constant_dict);

				constant_dict["name"] = F;
				constant_dict["value"] = t->constant_map[F];
			}

			if (!constants.is_empty()) {
				class_dict["constants"] = constants;
			}
		}

		{ //signals

			List<StringName> snames;

			k = nullptr;

			while ((k = t->signal_map.next(k))) {
				snames.push_back(*k);
			}

			snames.sort_custom<StringName::AlphCompare>();

			Array signals;

			for (StringName &F : snames) {
				Dictionary signal_dict;
				signals.push_back(signal_dict);

				MethodInfo &mi = t->signal_map[F];
				signal_dict["name"] = F;

				Array arguments;
				signal_dict["arguments"] = arguments;
				for (int i = 0; i < mi.arguments.size(); i++) {
					Dictionary argument_dict;
					arguments.push_back(argument_dict);
					argument_dict["type"] = mi.arguments[i].type;
				}
			}

			if (!signals.is_empty()) {
				class_dict["signals"] = signals;
			}
		}

		{ //properties

			List<StringName> snames;

			k = nullptr;

			while ((k = t->property_setget.next(k))) {
				snames.push_back(*k);
			}

			snames.sort_custom<StringName::AlphCompare>();

			Array properties;

			for (StringName &F : snames) {
				Dictionary property_dict;
				properties.push_back(property_dict);

				ClassDB::PropertySetGet *psg = t->property_setget.getptr(F);

				property_dict["name"] = F;
				property_dict["setter"] = psg->setter;
				property_dict["getter"] = psg->getter;
			}

			if (!properties.is_empty()) {
				class_dict["property_setget"] = properties;
			}
		}

		Array property_list;

		//property list
		for (PropertyInfo &F : t->property_list) {
			Dictionary property_dict;
			property_list.push_back(property_dict);

			property_dict["name"] = F.name;
			property_dict["type"] = F.type;
			property_dict["hint"] = F.hint;
			property_dict["hint_string"] = F.hint_string;
			property_dict["usage"] = F.usage;
		}

		if (!property_list.is_empty()) {
			class_dict["property_list"] = property_list;
		}
	}

	FileAccessRef f = FileAccess::open(p_output_file, FileAccess::WRITE);
	ERR_FAIL_COND_MSG(!f, "Cannot open file '" + p_output_file + "'.");
	JSON json;
	f->store_string(json.stringify(classes_dict, "\t"));
	f->close();

	print_line(String() + "ClassDB API JSON written to: " + ProjectSettings::get_singleton()->globalize_path(p_output_file));
}

#endif // DEBUG_METHODS_ENABLED