Path: blob/master/modules/mono/class_db_api_json.cpp
10277 views
/**************************************************************************/1/* class_db_api_json.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "class_db_api_json.h"3132#ifdef DEBUG_ENABLED3334#include "core/config/project_settings.h"35#include "core/io/file_access.h"36#include "core/io/json.h"37#include "core/version.h"3839void class_db_api_to_json(const String &p_output_file, ClassDB::APIType p_api) {40Dictionary classes_dict;4142List<StringName> class_list;43ClassDB::get_class_list(&class_list);44// Must be alphabetically sorted for hash to compute.45class_list.sort_custom<StringName::AlphCompare>();4647for (const StringName &E : class_list) {48ClassDB::ClassInfo *t = ClassDB::classes.getptr(E);49ERR_FAIL_NULL(t);50if (t->api != p_api || !t->exposed) {51continue;52}5354Dictionary class_dict;55classes_dict[t->name] = class_dict;5657class_dict["inherits"] = t->inherits;5859{ //methods6061List<StringName> snames;6263for (const KeyValue<StringName, MethodBind *> &F : t->method_map) {64String name = F.key.operator String();6566ERR_CONTINUE(name.is_empty());6768if (name[0] == '_') {69continue; // Ignore non-virtual methods that start with an underscore70}7172snames.push_back(F.key);73}7475snames.sort_custom<StringName::AlphCompare>();7677Array methods;7879for (const StringName &F : snames) {80Dictionary method_dict;81methods.push_back(method_dict);8283MethodBind *mb = t->method_map[F];84method_dict["name"] = mb->get_name();85method_dict["argument_count"] = mb->get_argument_count();86method_dict["return_type"] = mb->get_argument_type(-1);8788Array arguments;89method_dict["arguments"] = arguments;9091for (int i = 0; i < mb->get_argument_count(); i++) {92Dictionary argument_dict;93arguments.push_back(argument_dict);94const PropertyInfo info = mb->get_argument_info(i);95argument_dict["type"] = info.type;96argument_dict["name"] = info.name;97argument_dict["hint"] = info.hint;98argument_dict["hint_string"] = info.hint_string;99}100101method_dict["default_argument_count"] = mb->get_default_argument_count();102103Array default_arguments;104method_dict["default_arguments"] = default_arguments;105106for (int i = 0; i < mb->get_default_argument_count(); i++) {107Dictionary default_argument_dict;108default_arguments.push_back(default_argument_dict);109//hash should not change, i hope for tis110Variant da = mb->get_default_argument(i);111default_argument_dict["value"] = da;112}113114method_dict["hint_flags"] = mb->get_hint_flags();115}116117if (!methods.is_empty()) {118class_dict["methods"] = methods;119}120}121122{ //constants123124List<StringName> snames;125126for (const KeyValue<StringName, int64_t> &F : t->constant_map) {127snames.push_back(F.key);128}129130snames.sort_custom<StringName::AlphCompare>();131132Array constants;133134for (const StringName &F : snames) {135Dictionary constant_dict;136constants.push_back(constant_dict);137138constant_dict["name"] = F;139constant_dict["value"] = t->constant_map[F];140}141142if (!constants.is_empty()) {143class_dict["constants"] = constants;144}145}146147{ //signals148149List<StringName> snames;150151for (const KeyValue<StringName, MethodInfo> &F : t->signal_map) {152snames.push_back(F.key);153}154155snames.sort_custom<StringName::AlphCompare>();156157Array signals;158159for (const StringName &F : snames) {160Dictionary signal_dict;161signals.push_back(signal_dict);162163MethodInfo &mi = t->signal_map[F];164signal_dict["name"] = F;165166Array arguments;167signal_dict["arguments"] = arguments;168for (const PropertyInfo &pi : mi.arguments) {169Dictionary argument_dict;170arguments.push_back(argument_dict);171argument_dict["type"] = pi.type;172}173}174175if (!signals.is_empty()) {176class_dict["signals"] = signals;177}178}179180{ //properties181182List<StringName> snames;183184for (const KeyValue<StringName, ClassDB::PropertySetGet> &F : t->property_setget) {185snames.push_back(F.key);186}187188snames.sort_custom<StringName::AlphCompare>();189190Array properties;191192for (const StringName &F : snames) {193Dictionary property_dict;194properties.push_back(property_dict);195196ClassDB::PropertySetGet *psg = t->property_setget.getptr(F);197198property_dict["name"] = F;199property_dict["setter"] = psg->setter;200property_dict["getter"] = psg->getter;201}202203if (!properties.is_empty()) {204class_dict["property_setget"] = properties;205}206}207208Array property_list;209210//property list211for (const PropertyInfo &F : t->property_list) {212Dictionary property_dict;213property_list.push_back(property_dict);214215property_dict["name"] = F.name;216property_dict["type"] = F.type;217property_dict["hint"] = F.hint;218property_dict["hint_string"] = F.hint_string;219property_dict["usage"] = F.usage;220}221222if (!property_list.is_empty()) {223class_dict["property_list"] = property_list;224}225}226227Ref<FileAccess> f = FileAccess::open(p_output_file, FileAccess::WRITE);228ERR_FAIL_COND_MSG(f.is_null(), "Cannot open file '" + p_output_file + "'.");229f->store_string(JSON::stringify(classes_dict, "\t"));230231print_line(String() + "ClassDB API JSON written to: " + ProjectSettings::get_singleton()->globalize_path(p_output_file));232}233234#endif // DEBUG_ENABLED235236237