Merge pull request #49600 from vnen/gdnative-api-generator-var-writer

GDNative: Use VariantWriter for the API JSON generator
This commit is contained in:
Rémi Verschelde 2021-06-14 18:43:54 +02:00 committed by GitHub
commit f7fa5a0e3a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 5 deletions

View file

@ -38,6 +38,7 @@
#include "core/object/class_db.h" #include "core/object/class_db.h"
#include "core/string/string_builder.h" #include "core/string/string_builder.h"
#include "core/templates/pair.h" #include "core/templates/pair.h"
#include "core/variant/variant_parser.h"
// helper stuff // helper stuff
@ -638,6 +639,7 @@ static List<String> generate_c_api_json(const List<ClassAPI> &p_api) {
// I'm sorry for the \t mess // I'm sorry for the \t mess
List<String> source; List<String> source;
VariantWriter writer;
source.push_back("[\n"); source.push_back("[\n");
@ -682,7 +684,12 @@ static List<String> generate_c_api_json(const List<ClassAPI> &p_api) {
source.push_back("\t\t\t\t\t\t\"name\": \"" + e->get().argument_names[i] + "\",\n"); source.push_back("\t\t\t\t\t\t\"name\": \"" + e->get().argument_names[i] + "\",\n");
source.push_back("\t\t\t\t\t\t\"type\": \"" + e->get().argument_types[i] + "\",\n"); source.push_back("\t\t\t\t\t\t\"type\": \"" + e->get().argument_types[i] + "\",\n");
source.push_back(String("\t\t\t\t\t\t\"has_default_value\": ") + (e->get().default_arguments.has(i) ? "true" : "false") + ",\n"); source.push_back(String("\t\t\t\t\t\t\"has_default_value\": ") + (e->get().default_arguments.has(i) ? "true" : "false") + ",\n");
source.push_back("\t\t\t\t\t\t\"default_value\": \"" + (e->get().default_arguments.has(i) ? (String)e->get().default_arguments[i] : "") + "\"\n"); String default_value;
if (e->get().default_arguments.has(i)) {
writer.write_to_string(e->get().default_arguments[i], default_value);
default_value = default_value.replace("\n", "").json_escape();
}
source.push_back("\t\t\t\t\t\t\"default_value\": \"" + default_value + "\"\n");
source.push_back(String("\t\t\t\t\t}") + ((i < e->get().argument_names.size() - 1) ? "," : "") + "\n"); source.push_back(String("\t\t\t\t\t}") + ((i < e->get().argument_names.size() - 1) ? "," : "") + "\n");
} }
source.push_back("\t\t\t\t]\n"); source.push_back("\t\t\t\t]\n");
@ -708,7 +715,12 @@ static List<String> generate_c_api_json(const List<ClassAPI> &p_api) {
source.push_back("\t\t\t\t\t\t\"name\": \"" + e->get().argument_names[i] + "\",\n"); source.push_back("\t\t\t\t\t\t\"name\": \"" + e->get().argument_names[i] + "\",\n");
source.push_back("\t\t\t\t\t\t\"type\": \"" + e->get().argument_types[i] + "\",\n"); source.push_back("\t\t\t\t\t\t\"type\": \"" + e->get().argument_types[i] + "\",\n");
source.push_back(String("\t\t\t\t\t\t\"has_default_value\": ") + (e->get().default_arguments.has(i) ? "true" : "false") + ",\n"); source.push_back(String("\t\t\t\t\t\t\"has_default_value\": ") + (e->get().default_arguments.has(i) ? "true" : "false") + ",\n");
source.push_back("\t\t\t\t\t\t\"default_value\": \"" + (e->get().default_arguments.has(i) ? (String)e->get().default_arguments[i] : "") + "\"\n"); String default_value;
if (e->get().default_arguments.has(i)) {
writer.write_to_string(e->get().default_arguments[i], default_value);
default_value = default_value.replace("\n", "").json_escape();
}
source.push_back("\t\t\t\t\t\t\"default_value\": \"" + default_value + "\"\n");
source.push_back(String("\t\t\t\t\t}") + ((i < e->get().argument_names.size() - 1) ? "," : "") + "\n"); source.push_back(String("\t\t\t\t\t}") + ((i < e->get().argument_names.size() - 1) ? "," : "") + "\n");
} }
source.push_back("\t\t\t\t]\n"); source.push_back("\t\t\t\t]\n");
@ -756,6 +768,8 @@ static void append_indented(StringBuilder &p_source, const char *p_text) {
} }
static void write_builtin_method(StringBuilder &p_source, const MethodAPI &p_method) { static void write_builtin_method(StringBuilder &p_source, const MethodAPI &p_method) {
VariantWriter writer;
append_indented(p_source, vformat(R"("name": "%s",)", p_method.method_name)); append_indented(p_source, vformat(R"("name": "%s",)", p_method.method_name));
append_indented(p_source, vformat(R"("return_type": "%s",)", p_method.return_type)); append_indented(p_source, vformat(R"("return_type": "%s",)", p_method.return_type));
append_indented(p_source, vformat(R"("is_const": %s,)", p_method.is_const ? "true" : "false")); append_indented(p_source, vformat(R"("is_const": %s,)", p_method.is_const ? "true" : "false"));
@ -771,7 +785,12 @@ static void write_builtin_method(StringBuilder &p_source, const MethodAPI &p_met
append_indented(p_source, vformat(R"("name": "%s",)", p_method.argument_names[i])); append_indented(p_source, vformat(R"("name": "%s",)", p_method.argument_names[i]));
append_indented(p_source, vformat(R"("type": "%s",)", p_method.argument_types[i])); append_indented(p_source, vformat(R"("type": "%s",)", p_method.argument_types[i]));
append_indented(p_source, vformat(R"("has_default_value": %s,)", p_method.default_arguments.has(i) ? "true" : "false")); append_indented(p_source, vformat(R"("has_default_value": %s,)", p_method.default_arguments.has(i) ? "true" : "false"));
append_indented(p_source, vformat(R"("default_value": "%s")", p_method.default_arguments.has(i) ? p_method.default_arguments[i].operator String() : "")); String default_value;
if (p_method.default_arguments.has(i)) {
writer.write_to_string(p_method.default_arguments[i], default_value);
default_value = default_value.replace("\n", "").json_escape();
}
append_indented(p_source, vformat(R"("default_value": "%s")", default_value));
indent_level--; indent_level--;
append_indented(p_source, i < p_method.argument_count - 1 ? "}," : "}"); append_indented(p_source, i < p_method.argument_count - 1 ? "}," : "}");

View file

@ -28,7 +28,7 @@
<method name="resume"> <method name="resume">
<return type="Variant"> <return type="Variant">
</return> </return>
<argument index="0" name="args" type="Array" default="null"> <argument index="0" name="args" type="Array" default="[ ]">
</argument> </argument>
<description> <description>
</description> </description>

View file

@ -2204,7 +2204,7 @@ Variant VisualScriptFunctionState::resume(Array p_args) {
void VisualScriptFunctionState::_bind_methods() { void VisualScriptFunctionState::_bind_methods() {
ClassDB::bind_method(D_METHOD("connect_to_signal", "obj", "signals", "args"), &VisualScriptFunctionState::connect_to_signal); ClassDB::bind_method(D_METHOD("connect_to_signal", "obj", "signals", "args"), &VisualScriptFunctionState::connect_to_signal);
ClassDB::bind_method(D_METHOD("resume", "args"), &VisualScriptFunctionState::resume, DEFVAL(Variant())); ClassDB::bind_method(D_METHOD("resume", "args"), &VisualScriptFunctionState::resume, DEFVAL(Array()));
ClassDB::bind_method(D_METHOD("is_valid"), &VisualScriptFunctionState::is_valid); ClassDB::bind_method(D_METHOD("is_valid"), &VisualScriptFunctionState::is_valid);
ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "_signal_callback", &VisualScriptFunctionState::_signal_callback, MethodInfo("_signal_callback")); ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "_signal_callback", &VisualScriptFunctionState::_signal_callback, MethodInfo("_signal_callback"));
} }