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