Path: blob/master/modules/mono/class_db_api_json.cpp
11351 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;4142LocalVector<StringName> class_list;43ClassDB::get_class_list(class_list);4445for (const StringName &class_name : class_list) {46ClassDB::ClassInfo *t = ClassDB::classes.getptr(class_name);47ERR_FAIL_NULL(t);48if (t->api != p_api || !t->exposed) {49continue;50}5152Dictionary class_dict;53classes_dict[t->name] = class_dict;5455class_dict["inherits"] = t->inherits;5657{ //methods5859List<StringName> snames;6061for (const KeyValue<StringName, MethodBind *> &F : t->method_map) {62String name = F.key.operator String();6364ERR_CONTINUE(name.is_empty());6566if (name[0] == '_') {67continue; // Ignore non-virtual methods that start with an underscore68}6970snames.push_back(F.key);71}7273snames.sort_custom<StringName::AlphCompare>();7475Array methods;7677for (const StringName &F : snames) {78Dictionary method_dict;79methods.push_back(method_dict);8081MethodBind *mb = t->method_map[F];82method_dict["name"] = mb->get_name();83method_dict["argument_count"] = mb->get_argument_count();84method_dict["return_type"] = mb->get_argument_type(-1);8586Array arguments;87method_dict["arguments"] = arguments;8889for (int i = 0; i < mb->get_argument_count(); i++) {90Dictionary argument_dict;91arguments.push_back(argument_dict);92const PropertyInfo info = mb->get_argument_info(i);93argument_dict["type"] = info.type;94argument_dict["name"] = info.name;95argument_dict["hint"] = info.hint;96argument_dict["hint_string"] = info.hint_string;97}9899method_dict["default_argument_count"] = mb->get_default_argument_count();100101Array default_arguments;102method_dict["default_arguments"] = default_arguments;103104for (int i = 0; i < mb->get_default_argument_count(); i++) {105Dictionary default_argument_dict;106default_arguments.push_back(default_argument_dict);107//hash should not change, i hope for tis108Variant da = mb->get_default_argument(i);109default_argument_dict["value"] = da;110}111112method_dict["hint_flags"] = mb->get_hint_flags();113}114115if (!methods.is_empty()) {116class_dict["methods"] = methods;117}118}119120{ //constants121122List<StringName> snames;123124for (const KeyValue<StringName, int64_t> &F : t->constant_map) {125snames.push_back(F.key);126}127128snames.sort_custom<StringName::AlphCompare>();129130Array constants;131132for (const StringName &F : snames) {133Dictionary constant_dict;134constants.push_back(constant_dict);135136constant_dict["name"] = F;137constant_dict["value"] = t->constant_map[F];138}139140if (!constants.is_empty()) {141class_dict["constants"] = constants;142}143}144145{ //signals146147List<StringName> snames;148149for (const KeyValue<StringName, MethodInfo> &F : t->signal_map) {150snames.push_back(F.key);151}152153snames.sort_custom<StringName::AlphCompare>();154155Array signals;156157for (const StringName &F : snames) {158Dictionary signal_dict;159signals.push_back(signal_dict);160161MethodInfo &mi = t->signal_map[F];162signal_dict["name"] = F;163164Array arguments;165signal_dict["arguments"] = arguments;166for (const PropertyInfo &pi : mi.arguments) {167Dictionary argument_dict;168arguments.push_back(argument_dict);169argument_dict["type"] = pi.type;170}171}172173if (!signals.is_empty()) {174class_dict["signals"] = signals;175}176}177178{ //properties179180List<StringName> snames;181182for (const KeyValue<StringName, ClassDB::PropertySetGet> &F : t->property_setget) {183snames.push_back(F.key);184}185186snames.sort_custom<StringName::AlphCompare>();187188Array properties;189190for (const StringName &F : snames) {191Dictionary property_dict;192properties.push_back(property_dict);193194ClassDB::PropertySetGet *psg = t->property_setget.getptr(F);195196property_dict["name"] = F;197property_dict["setter"] = psg->setter;198property_dict["getter"] = psg->getter;199}200201if (!properties.is_empty()) {202class_dict["property_setget"] = properties;203}204}205206Array property_list;207208//property list209for (const PropertyInfo &F : t->property_list) {210Dictionary property_dict;211property_list.push_back(property_dict);212213property_dict["name"] = F.name;214property_dict["type"] = F.type;215property_dict["hint"] = F.hint;216property_dict["hint_string"] = F.hint_string;217property_dict["usage"] = F.usage;218}219220if (!property_list.is_empty()) {221class_dict["property_list"] = property_list;222}223}224225Ref<FileAccess> f = FileAccess::open(p_output_file, FileAccess::WRITE);226ERR_FAIL_COND_MSG(f.is_null(), "Cannot open file '" + p_output_file + "'.");227f->store_string(JSON::stringify(classes_dict, "\t"));228229print_line(String() + "ClassDB API JSON written to: " + ProjectSettings::get_singleton()->globalize_path(p_output_file));230}231232#endif // DEBUG_ENABLED233234235