2019-03-26 23:51:51 +00:00
|
|
|
#!/usr/bin/env python
|
2020-02-02 19:47:45 +00:00
|
|
|
from __future__ import print_function
|
2017-05-12 19:53:07 +00:00
|
|
|
import json
|
2020-02-02 19:47:45 +00:00
|
|
|
import os
|
|
|
|
import errno
|
2021-05-15 20:57:05 +00:00
|
|
|
from pathlib import Path
|
2017-05-12 19:53:07 +00:00
|
|
|
|
2020-06-17 19:12:57 +00:00
|
|
|
# Convenience function for using template get_node
|
2020-08-23 17:30:27 +00:00
|
|
|
def correct_method_name(method_list):
|
|
|
|
for method in method_list:
|
|
|
|
if method["name"] == "get_node":
|
|
|
|
method["name"] = "get_node_internal"
|
2020-06-17 19:12:57 +00:00
|
|
|
|
|
|
|
|
2017-06-20 22:42:29 +00:00
|
|
|
classes = []
|
|
|
|
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2020-02-02 19:47:45 +00:00
|
|
|
def print_file_list(api_filepath, output_dir, headers=False, sources=False):
|
|
|
|
global classes
|
2022-02-26 20:40:06 +00:00
|
|
|
end = ";"
|
2020-02-02 19:47:45 +00:00
|
|
|
with open(api_filepath) as api_file:
|
|
|
|
classes = json.load(api_file)
|
2022-02-26 20:40:06 +00:00
|
|
|
include_gen_folder = Path(output_dir) / "include" / "gen"
|
|
|
|
source_gen_folder = Path(output_dir) / "src" / "gen"
|
2020-02-02 19:47:45 +00:00
|
|
|
for _class in classes:
|
2021-05-15 20:57:05 +00:00
|
|
|
header_filename = include_gen_folder / (strip_name(_class["name"]) + ".hpp")
|
|
|
|
source_filename = source_gen_folder / (strip_name(_class["name"]) + ".cpp")
|
2020-02-02 19:47:45 +00:00
|
|
|
if headers:
|
2021-05-15 20:57:05 +00:00
|
|
|
print(str(header_filename.as_posix()), end=end)
|
2020-02-02 19:47:45 +00:00
|
|
|
if sources:
|
2021-05-15 20:57:05 +00:00
|
|
|
print(str(source_filename.as_posix()), end=end)
|
2022-02-26 20:40:06 +00:00
|
|
|
icall_header_filename = include_gen_folder / "__icalls.hpp"
|
|
|
|
register_types_filename = source_gen_folder / "__register_types.cpp"
|
|
|
|
init_method_bindings_filename = source_gen_folder / "__init_method_bindings.cpp"
|
2020-02-02 19:47:45 +00:00
|
|
|
if headers:
|
2021-05-15 20:57:05 +00:00
|
|
|
print(str(icall_header_filename.as_posix()), end=end)
|
2020-02-02 19:47:45 +00:00
|
|
|
if sources:
|
2021-05-15 20:57:05 +00:00
|
|
|
print(str(register_types_filename.as_posix()), end=end)
|
|
|
|
print(str(init_method_bindings_filename.as_posix()), end=end)
|
2020-02-02 19:47:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
def generate_bindings(api_filepath, use_template_get_node, output_dir="."):
|
2017-06-20 22:42:29 +00:00
|
|
|
global classes
|
2020-02-02 19:47:45 +00:00
|
|
|
with open(api_filepath) as api_file:
|
|
|
|
classes = json.load(api_file)
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
icalls = set()
|
2022-02-26 20:40:06 +00:00
|
|
|
include_gen_folder = Path(output_dir) / "include" / "gen"
|
|
|
|
source_gen_folder = Path(output_dir) / "src" / "gen"
|
2021-05-15 20:57:05 +00:00
|
|
|
|
2020-02-02 19:47:45 +00:00
|
|
|
try:
|
2021-05-15 20:57:05 +00:00
|
|
|
include_gen_folder.mkdir(parents=True)
|
2020-02-02 19:47:45 +00:00
|
|
|
except os.error as e:
|
|
|
|
if e.errno == errno.EEXIST:
|
2021-05-15 20:57:05 +00:00
|
|
|
print(str(source_gen_folder) + ": " + os.strerror(e.errno))
|
2020-02-02 19:47:45 +00:00
|
|
|
else:
|
|
|
|
exit(1)
|
2021-05-15 20:57:05 +00:00
|
|
|
|
2020-02-02 19:47:45 +00:00
|
|
|
try:
|
2021-05-15 20:57:05 +00:00
|
|
|
source_gen_folder.mkdir(parents=True)
|
2020-02-02 19:47:45 +00:00
|
|
|
except os.error as e:
|
|
|
|
if e.errno == errno.EEXIST:
|
2021-05-15 20:57:05 +00:00
|
|
|
print(str(source_gen_folder) + ": " + os.strerror(e.errno))
|
2020-02-02 19:47:45 +00:00
|
|
|
else:
|
|
|
|
exit(1)
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
for c in classes:
|
2020-02-02 19:47:45 +00:00
|
|
|
# print(c['name'])
|
2017-05-12 19:53:07 +00:00
|
|
|
used_classes = get_used_classes(c)
|
2020-08-23 17:30:27 +00:00
|
|
|
if use_template_get_node and c["name"] == "Node":
|
|
|
|
correct_method_name(c["methods"])
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2020-06-17 19:12:57 +00:00
|
|
|
header = generate_class_header(used_classes, c, use_template_get_node)
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2020-06-17 19:12:57 +00:00
|
|
|
impl = generate_class_implementation(icalls, used_classes, c, use_template_get_node)
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-05-15 20:57:05 +00:00
|
|
|
header_filename = include_gen_folder / (strip_name(c["name"]) + ".hpp")
|
|
|
|
with header_filename.open("w+") as header_file:
|
2020-02-02 19:47:45 +00:00
|
|
|
header_file.write(header)
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-05-15 20:57:05 +00:00
|
|
|
source_filename = source_gen_folder / (strip_name(c["name"]) + ".cpp")
|
|
|
|
with source_filename.open("w+") as source_file:
|
2020-02-02 19:47:45 +00:00
|
|
|
source_file.write(impl)
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2022-02-26 20:40:06 +00:00
|
|
|
icall_header_filename = include_gen_folder / "__icalls.hpp"
|
2021-05-15 20:57:05 +00:00
|
|
|
with icall_header_filename.open("w+") as icall_header_file:
|
2020-02-02 19:47:45 +00:00
|
|
|
icall_header_file.write(generate_icall_header(icalls))
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2022-02-26 20:40:06 +00:00
|
|
|
register_types_filename = source_gen_folder / "__register_types.cpp"
|
2021-05-15 20:57:05 +00:00
|
|
|
with register_types_filename.open("w+") as register_types_file:
|
2020-02-02 19:47:45 +00:00
|
|
|
register_types_file.write(generate_type_registry(classes))
|
2018-02-11 14:50:01 +00:00
|
|
|
|
2022-02-26 20:40:06 +00:00
|
|
|
init_method_bindings_filename = source_gen_folder / "__init_method_bindings.cpp"
|
2021-05-15 20:57:05 +00:00
|
|
|
with init_method_bindings_filename.open("w+") as init_method_bindings_file:
|
2020-02-02 19:47:45 +00:00
|
|
|
init_method_bindings_file.write(generate_init_method_bindings(classes))
|
2019-04-07 22:45:35 +00:00
|
|
|
|
2017-06-20 22:42:29 +00:00
|
|
|
|
|
|
|
def is_reference_type(t):
|
|
|
|
for c in classes:
|
2022-02-26 20:40:06 +00:00
|
|
|
if c["name"] != t:
|
2017-06-20 22:42:29 +00:00
|
|
|
continue
|
2022-02-26 20:40:06 +00:00
|
|
|
if c["is_reference"]:
|
2017-06-20 22:42:29 +00:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2022-02-26 20:40:06 +00:00
|
|
|
|
2020-07-28 17:46:01 +00:00
|
|
|
def make_gdnative_type(t, ref_allowed):
|
2017-09-11 19:34:20 +00:00
|
|
|
if is_enum(t):
|
|
|
|
return remove_enum_prefix(t) + " "
|
|
|
|
elif is_class_type(t):
|
2020-07-28 17:46:01 +00:00
|
|
|
if is_reference_type(t) and ref_allowed:
|
2017-06-20 22:42:29 +00:00
|
|
|
return "Ref<" + strip_name(t) + "> "
|
|
|
|
else:
|
|
|
|
return strip_name(t) + " *"
|
|
|
|
else:
|
2017-07-23 15:14:50 +00:00
|
|
|
if t == "int":
|
|
|
|
return "int64_t "
|
|
|
|
if t == "float" or t == "real":
|
2019-04-07 22:45:35 +00:00
|
|
|
return "real_t "
|
2017-06-20 22:42:29 +00:00
|
|
|
return strip_name(t) + " "
|
2017-05-12 19:53:07 +00:00
|
|
|
|
2019-04-07 22:45:35 +00:00
|
|
|
|
2020-06-17 19:12:57 +00:00
|
|
|
def generate_class_header(used_classes, c, use_template_get_node):
|
2017-05-12 19:53:07 +00:00
|
|
|
|
|
|
|
source = []
|
|
|
|
source.append("#ifndef GODOT_CPP_" + strip_name(c["name"]).upper() + "_HPP")
|
|
|
|
source.append("#define GODOT_CPP_" + strip_name(c["name"]).upper() + "_HPP")
|
|
|
|
source.append("")
|
|
|
|
source.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-10-20 23:42:10 +00:00
|
|
|
source.append("#include <gdnative_api_struct.gen.h>")
|
2020-02-02 19:47:45 +00:00
|
|
|
source.append("#include <cstdint>")
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-12-11 15:36:03 +00:00
|
|
|
source.append("#include <core/CoreTypes.hpp>")
|
2017-09-11 19:34:20 +00:00
|
|
|
|
|
|
|
class_name = strip_name(c["name"])
|
|
|
|
|
2018-01-22 21:27:10 +00:00
|
|
|
# Ref<T> is not included in object.h in Godot either,
|
|
|
|
# so don't include it here because it's not needed
|
2018-01-24 09:48:30 +00:00
|
|
|
if class_name != "Object" and class_name != "Reference":
|
2018-01-22 21:27:10 +00:00
|
|
|
source.append("#include <core/Ref.hpp>")
|
2020-07-28 17:46:01 +00:00
|
|
|
ref_allowed = True
|
2018-02-11 14:50:01 +00:00
|
|
|
else:
|
|
|
|
source.append("#include <core/TagDB.hpp>")
|
2020-07-28 17:46:01 +00:00
|
|
|
ref_allowed = False
|
2018-01-22 21:27:10 +00:00
|
|
|
|
2017-09-11 19:34:20 +00:00
|
|
|
included = []
|
|
|
|
|
|
|
|
for used_class in used_classes:
|
|
|
|
if is_enum(used_class) and is_nested_type(used_class):
|
|
|
|
used_class_name = remove_enum_prefix(extract_nested_type(used_class))
|
|
|
|
if used_class_name not in included:
|
|
|
|
included.append(used_class_name)
|
2022-02-26 20:40:06 +00:00
|
|
|
source.append('#include "' + used_class_name + '.hpp"')
|
2017-09-11 19:34:20 +00:00
|
|
|
elif is_enum(used_class) and is_nested_type(used_class) and not is_nested_type(used_class, class_name):
|
|
|
|
used_class_name = remove_enum_prefix(used_class)
|
|
|
|
if used_class_name not in included:
|
|
|
|
included.append(used_class_name)
|
2022-02-26 20:40:06 +00:00
|
|
|
source.append('#include "' + used_class_name + '.hpp"')
|
2017-09-11 19:34:20 +00:00
|
|
|
|
|
|
|
source.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
if c["base_class"] != "":
|
2022-02-26 20:40:06 +00:00
|
|
|
source.append('#include "' + strip_name(c["base_class"]) + '.hpp"')
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append("namespace godot {")
|
|
|
|
source.append("")
|
2017-09-11 19:34:20 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
for used_type in used_classes:
|
2017-09-11 19:34:20 +00:00
|
|
|
if is_enum(used_type) or is_nested_type(used_type, class_name):
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
source.append("class " + strip_name(used_type) + ";")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2018-03-06 10:26:07 +00:00
|
|
|
vararg_templates = ""
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
# generate the class definition here
|
2022-02-26 20:40:06 +00:00
|
|
|
source.append(
|
|
|
|
"class "
|
|
|
|
+ class_name
|
|
|
|
+ (" : public _Wrapped" if c["base_class"] == "" else (" : public " + strip_name(c["base_class"])))
|
|
|
|
+ " {"
|
|
|
|
)
|
2018-02-11 14:50:01 +00:00
|
|
|
|
|
|
|
if c["base_class"] == "":
|
|
|
|
source.append("public: enum { ___CLASS_IS_SCRIPT = 0, };")
|
|
|
|
source.append("")
|
2019-04-07 22:45:35 +00:00
|
|
|
source.append("private:")
|
2018-02-11 14:50:01 +00:00
|
|
|
|
|
|
|
if c["singleton"]:
|
|
|
|
source.append("\tstatic " + class_name + " *_singleton;")
|
|
|
|
source.append("")
|
|
|
|
source.append("\t" + class_name + "();")
|
|
|
|
source.append("")
|
|
|
|
|
2019-04-07 22:45:35 +00:00
|
|
|
# Generate method table
|
|
|
|
source.append("\tstruct ___method_bindings {")
|
2018-02-11 14:50:01 +00:00
|
|
|
|
2019-04-07 22:45:35 +00:00
|
|
|
for method in c["methods"]:
|
|
|
|
source.append("\t\tgodot_method_bind *mb_" + method["name"] + ";")
|
|
|
|
|
|
|
|
source.append("\t};")
|
|
|
|
source.append("\tstatic ___method_bindings ___mb;")
|
2019-10-21 16:08:03 +00:00
|
|
|
source.append("\tstatic void *_detail_class_tag;")
|
2019-04-07 22:45:35 +00:00
|
|
|
source.append("")
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append("public:")
|
2019-04-07 22:45:35 +00:00
|
|
|
source.append("\tstatic void ___init_method_bindings();")
|
|
|
|
|
2019-10-21 16:08:03 +00:00
|
|
|
# class id from core engine for casting
|
|
|
|
source.append("\tinline static size_t ___get_id() { return (size_t)_detail_class_tag; }")
|
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append("")
|
2017-09-11 19:34:20 +00:00
|
|
|
|
2018-02-11 14:50:01 +00:00
|
|
|
if c["singleton"]:
|
|
|
|
source.append("\tstatic inline " + class_name + " *get_singleton()")
|
|
|
|
source.append("\t{")
|
|
|
|
source.append("\t\tif (!" + class_name + "::_singleton) {")
|
|
|
|
source.append("\t\t\t" + class_name + "::_singleton = new " + class_name + ";")
|
|
|
|
source.append("\t\t}")
|
|
|
|
source.append("\t\treturn " + class_name + "::_singleton;")
|
|
|
|
source.append("\t}")
|
|
|
|
source.append("")
|
|
|
|
|
|
|
|
# godot::api->godot_global_get_singleton((char *) \"" + strip_name(c["name"]) + "\");"
|
|
|
|
|
2020-09-12 16:38:46 +00:00
|
|
|
# class name:
|
|
|
|
# Two versions needed needed because when the user implements a custom class,
|
|
|
|
# we want to override `___get_class_name` while `___get_godot_class_name` can keep returning the base name
|
2022-02-26 20:40:06 +00:00
|
|
|
source.append(
|
|
|
|
'\tstatic inline const char *___get_class_name() { return (const char *) "' + strip_name(c["name"]) + '"; }'
|
|
|
|
)
|
|
|
|
source.append(
|
|
|
|
'\tstatic inline const char *___get_godot_class_name() { return (const char *) "'
|
|
|
|
+ strip_name(c["name"])
|
|
|
|
+ '"; }'
|
|
|
|
)
|
|
|
|
|
|
|
|
source.append(
|
|
|
|
"\tstatic inline Object *___get_from_variant(Variant a) { godot_object *o = (godot_object*) a; return (o) ? (Object *) godot::nativescript_1_1_api->godot_nativescript_get_instance_binding_data(godot::_RegisterState::language_index, o) : nullptr; }"
|
|
|
|
)
|
2018-01-14 22:03:57 +00:00
|
|
|
|
2017-09-11 19:34:20 +00:00
|
|
|
enum_values = []
|
|
|
|
|
|
|
|
source.append("\n\t// enums")
|
|
|
|
for enum in c["enums"]:
|
2018-04-25 08:39:18 +00:00
|
|
|
source.append("\tenum " + strip_name(enum["name"]) + " {")
|
2017-09-11 19:34:20 +00:00
|
|
|
for value in enum["values"]:
|
|
|
|
source.append("\t\t" + remove_nested_type_prefix(value) + " = " + str(enum["values"][value]) + ",")
|
|
|
|
enum_values.append(value)
|
|
|
|
source.append("\t};")
|
|
|
|
|
|
|
|
source.append("\n\t// constants")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
for name in c["constants"]:
|
2017-09-11 19:34:20 +00:00
|
|
|
if name not in enum_values:
|
|
|
|
source.append("\tconst static int " + name + " = " + str(c["constants"][name]) + ";")
|
2017-06-19 00:03:59 +00:00
|
|
|
|
|
|
|
if c["instanciable"]:
|
2018-02-11 14:50:01 +00:00
|
|
|
source.append("")
|
|
|
|
source.append("")
|
|
|
|
source.append("\tstatic " + class_name + " *_new();")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append("\n\t// methods")
|
2018-02-11 14:50:01 +00:00
|
|
|
|
|
|
|
if class_name == "Object":
|
|
|
|
source.append("#ifndef GODOT_CPP_NO_OBJECT_CAST")
|
|
|
|
source.append("\ttemplate<class T>")
|
|
|
|
source.append("\tstatic T *cast_to(const Object *obj);")
|
|
|
|
source.append("#endif")
|
|
|
|
source.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
for method in c["methods"]:
|
|
|
|
method_signature = ""
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2018-02-11 14:50:01 +00:00
|
|
|
# TODO decide what to do about virtual methods
|
|
|
|
# method_signature += "virtual " if method["is_virtual"] else ""
|
2020-07-28 17:46:01 +00:00
|
|
|
method_signature += make_gdnative_type(method["return_type"], ref_allowed)
|
2018-03-06 10:26:07 +00:00
|
|
|
method_name = escape_cpp(method["name"])
|
2022-02-26 20:40:06 +00:00
|
|
|
method_signature += method_name + "("
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
has_default_argument = False
|
2018-03-06 10:26:07 +00:00
|
|
|
method_arguments = ""
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
for i, argument in enumerate(method["arguments"]):
|
2020-07-28 17:46:01 +00:00
|
|
|
method_signature += "const " + make_gdnative_type(argument["type"], ref_allowed)
|
2018-03-06 10:26:07 +00:00
|
|
|
argument_name = escape_cpp(argument["name"])
|
|
|
|
method_signature += argument_name
|
|
|
|
method_arguments += argument_name
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
# default arguments
|
|
|
|
def escape_default_arg(_type, default_value):
|
|
|
|
if _type == "Color":
|
|
|
|
return "Color(" + default_value + ")"
|
|
|
|
if _type == "bool" or _type == "int":
|
|
|
|
return default_value.lower()
|
|
|
|
if _type == "Array":
|
|
|
|
return "Array()"
|
2022-02-26 20:40:06 +00:00
|
|
|
if _type in [
|
|
|
|
"PoolVector2Array",
|
|
|
|
"PoolStringArray",
|
|
|
|
"PoolVector3Array",
|
|
|
|
"PoolColorArray",
|
|
|
|
"PoolIntArray",
|
|
|
|
"PoolRealArray",
|
|
|
|
"PoolByteArray",
|
|
|
|
]:
|
2017-05-12 19:53:07 +00:00
|
|
|
return _type + "()"
|
|
|
|
if _type == "Vector2":
|
|
|
|
return "Vector2" + default_value
|
|
|
|
if _type == "Vector3":
|
|
|
|
return "Vector3" + default_value
|
|
|
|
if _type == "Transform":
|
|
|
|
return "Transform()"
|
|
|
|
if _type == "Transform2D":
|
|
|
|
return "Transform2D()"
|
|
|
|
if _type == "Rect2":
|
|
|
|
return "Rect2" + default_value
|
|
|
|
if _type == "Variant":
|
|
|
|
return "Variant()" if default_value == "Null" else default_value
|
2021-09-27 10:41:46 +00:00
|
|
|
if _type == "String" or _type == "NodePath":
|
2022-02-26 20:40:06 +00:00
|
|
|
return '"' + default_value + '"'
|
2017-05-12 19:53:07 +00:00
|
|
|
if _type == "RID":
|
|
|
|
return "RID()"
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
if default_value == "Null" or default_value == "[Object:null]":
|
|
|
|
return "nullptr"
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
return default_value
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
if argument["has_default_value"] or has_default_argument:
|
|
|
|
method_signature += " = " + escape_default_arg(argument["type"], argument["default_value"])
|
|
|
|
has_default_argument = True
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
if i != len(method["arguments"]) - 1:
|
|
|
|
method_signature += ", "
|
2018-03-06 10:26:07 +00:00
|
|
|
method_arguments += ","
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
if method["has_varargs"]:
|
|
|
|
if len(method["arguments"]) > 0:
|
|
|
|
method_signature += ", "
|
2018-03-06 10:26:07 +00:00
|
|
|
method_arguments += ", "
|
2022-02-26 20:40:06 +00:00
|
|
|
vararg_templates += (
|
|
|
|
"\ttemplate <class... Args> "
|
|
|
|
+ method_signature
|
|
|
|
+ "Args... args){\n\t\treturn "
|
|
|
|
+ method_name
|
|
|
|
+ "("
|
|
|
|
+ method_arguments
|
|
|
|
+ "Array::make(args...));\n\t}\n"
|
|
|
|
""
|
|
|
|
)
|
2017-05-12 19:53:07 +00:00
|
|
|
method_signature += "const Array& __var_args = Array()"
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2018-02-11 14:50:01 +00:00
|
|
|
method_signature += ")" + (" const" if method["is_const"] else "")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append("\t" + method_signature + ";")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2018-03-06 10:26:07 +00:00
|
|
|
source.append(vararg_templates)
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2020-08-23 16:01:23 +00:00
|
|
|
if use_template_get_node and class_name == "Node":
|
2020-06-17 19:12:57 +00:00
|
|
|
# Extra definition for template get_node that calls the renamed get_node_internal; has a default template parameter for backwards compatibility.
|
2020-08-23 16:01:23 +00:00
|
|
|
source.append("\ttemplate <class T = Node>")
|
|
|
|
source.append("\tT *get_node(const NodePath path) const {")
|
|
|
|
source.append("\t\treturn Object::cast_to<T>(get_node_internal(path));")
|
|
|
|
source.append("\t}")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2020-06-17 19:12:57 +00:00
|
|
|
source.append("};")
|
|
|
|
source.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2020-06-17 19:12:57 +00:00
|
|
|
# ...And a specialized version so we don't unnecessarily cast when using the default.
|
2020-08-23 16:01:23 +00:00
|
|
|
source.append("template <>")
|
|
|
|
source.append("inline Node *Node::get_node<Node>(const NodePath path) const {")
|
|
|
|
source.append("\treturn get_node_internal(path);")
|
|
|
|
source.append("}")
|
|
|
|
source.append("")
|
2020-06-17 19:12:57 +00:00
|
|
|
else:
|
|
|
|
source.append("};")
|
|
|
|
source.append("")
|
2018-02-11 14:50:01 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append("}")
|
|
|
|
source.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append("#endif")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
return "\n".join(source)
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
|
2020-06-17 19:12:57 +00:00
|
|
|
def generate_class_implementation(icalls, used_classes, c, use_template_get_node):
|
2017-09-11 19:34:20 +00:00
|
|
|
class_name = strip_name(c["name"])
|
2020-07-28 17:46:01 +00:00
|
|
|
|
|
|
|
ref_allowed = class_name != "Object" and class_name != "Reference"
|
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
source = []
|
2022-02-26 20:40:06 +00:00
|
|
|
source.append('#include "' + class_name + '.hpp"')
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append("")
|
|
|
|
source.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-12-11 15:36:03 +00:00
|
|
|
source.append("#include <core/GodotGlobal.hpp>")
|
|
|
|
source.append("#include <core/CoreTypes.hpp>")
|
|
|
|
source.append("#include <core/Ref.hpp>")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-12-11 15:36:03 +00:00
|
|
|
source.append("#include <core/Godot.hpp>")
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2022-02-26 20:40:06 +00:00
|
|
|
source.append('#include "__icalls.hpp"')
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append("")
|
|
|
|
source.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
for used_class in used_classes:
|
2017-09-11 19:34:20 +00:00
|
|
|
if is_enum(used_class):
|
|
|
|
continue
|
|
|
|
else:
|
2022-02-26 20:40:06 +00:00
|
|
|
source.append('#include "' + strip_name(used_class) + '.hpp"')
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append("")
|
|
|
|
source.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append("namespace godot {")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2018-02-11 14:50:01 +00:00
|
|
|
core_object_name = "this"
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append("")
|
|
|
|
source.append("")
|
2018-02-11 14:50:01 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
if c["singleton"]:
|
2018-02-11 14:50:01 +00:00
|
|
|
source.append("" + class_name + " *" + class_name + "::_singleton = NULL;")
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append("")
|
|
|
|
source.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
# FIXME Test if inlining has a huge impact on binary size
|
2018-02-11 14:50:01 +00:00
|
|
|
source.append(class_name + "::" + class_name + "() {")
|
2022-02-26 20:40:06 +00:00
|
|
|
source.append('\t_owner = godot::api->godot_global_get_singleton((char *) "' + strip_name(c["name"]) + '");')
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append("}")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append("")
|
|
|
|
source.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2019-04-07 22:45:35 +00:00
|
|
|
# Method table initialization
|
|
|
|
source.append(class_name + "::___method_bindings " + class_name + "::___mb = {};")
|
|
|
|
source.append("")
|
|
|
|
|
2019-10-21 16:08:03 +00:00
|
|
|
source.append("void *" + class_name + "::_detail_class_tag = nullptr;")
|
|
|
|
source.append("")
|
|
|
|
|
2019-04-07 22:45:35 +00:00
|
|
|
source.append("void " + class_name + "::___init_method_bindings() {")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2019-04-07 22:45:35 +00:00
|
|
|
for method in c["methods"]:
|
2022-02-26 20:40:06 +00:00
|
|
|
source.append(
|
|
|
|
"\t___mb.mb_"
|
|
|
|
+ method["name"]
|
|
|
|
+ ' = godot::api->godot_method_bind_get_method("'
|
|
|
|
+ c["name"]
|
|
|
|
+ '", "'
|
|
|
|
+ ("get_node" if use_template_get_node and method["name"] == "get_node_internal" else method["name"])
|
|
|
|
+ '");'
|
|
|
|
)
|
2019-04-07 22:45:35 +00:00
|
|
|
|
2019-10-21 16:08:03 +00:00
|
|
|
source.append("\tgodot_string_name class_name;")
|
2022-02-26 20:40:06 +00:00
|
|
|
source.append('\tgodot::api->godot_string_name_new_data(&class_name, "' + c["name"] + '");')
|
2019-10-21 16:08:03 +00:00
|
|
|
source.append("\t_detail_class_tag = godot::core_1_2_api->godot_get_class_tag(&class_name);")
|
|
|
|
source.append("\tgodot::api->godot_string_name_destroy(&class_name);")
|
|
|
|
|
2019-04-07 22:45:35 +00:00
|
|
|
source.append("}")
|
|
|
|
source.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-06-19 00:03:59 +00:00
|
|
|
if c["instanciable"]:
|
2018-02-11 14:50:01 +00:00
|
|
|
source.append(class_name + " *" + strip_name(c["name"]) + "::_new()")
|
2017-06-19 00:03:59 +00:00
|
|
|
source.append("{")
|
2022-02-26 20:40:06 +00:00
|
|
|
source.append(
|
|
|
|
"\treturn ("
|
|
|
|
+ class_name
|
|
|
|
+ ' *) godot::nativescript_1_1_api->godot_nativescript_get_instance_binding_data(godot::_RegisterState::language_index, godot::api->godot_get_class_constructor((char *)"'
|
|
|
|
+ c["name"]
|
|
|
|
+ '")());'
|
|
|
|
)
|
2017-06-19 00:03:59 +00:00
|
|
|
source.append("}")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
for method in c["methods"]:
|
|
|
|
method_signature = ""
|
2018-02-11 14:50:01 +00:00
|
|
|
|
2020-07-28 17:46:01 +00:00
|
|
|
method_signature += make_gdnative_type(method["return_type"], ref_allowed)
|
2017-05-12 19:53:07 +00:00
|
|
|
method_signature += strip_name(c["name"]) + "::" + escape_cpp(method["name"]) + "("
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
for i, argument in enumerate(method["arguments"]):
|
2020-07-28 17:46:01 +00:00
|
|
|
method_signature += "const " + make_gdnative_type(argument["type"], ref_allowed)
|
2017-05-12 19:53:07 +00:00
|
|
|
method_signature += escape_cpp(argument["name"])
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
if i != len(method["arguments"]) - 1:
|
|
|
|
method_signature += ", "
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
if method["has_varargs"]:
|
|
|
|
if len(method["arguments"]) > 0:
|
|
|
|
method_signature += ", "
|
|
|
|
method_signature += "const Array& __var_args"
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2018-02-11 14:50:01 +00:00
|
|
|
method_signature += ")" + (" const" if method["is_const"] else "")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append(method_signature + " {")
|
2018-02-11 14:50:01 +00:00
|
|
|
|
|
|
|
if method["name"] == "free":
|
|
|
|
# dirty hack because Object::free is marked virtual but doesn't actually exist...
|
|
|
|
source.append("\tgodot::api->godot_object_destroy(_owner);")
|
|
|
|
source.append("}")
|
|
|
|
source.append("")
|
|
|
|
continue
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
return_statement = ""
|
2020-07-28 17:46:01 +00:00
|
|
|
return_type_is_ref = is_reference_type(method["return_type"]) and ref_allowed
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
if method["return_type"] != "void":
|
2017-06-20 22:42:29 +00:00
|
|
|
if is_class_type(method["return_type"]):
|
2017-09-11 19:34:20 +00:00
|
|
|
if is_enum(method["return_type"]):
|
|
|
|
return_statement += "return (" + remove_enum_prefix(method["return_type"]) + ") "
|
2020-07-28 17:46:01 +00:00
|
|
|
elif return_type_is_ref:
|
2020-09-12 16:38:46 +00:00
|
|
|
return_statement += "return Ref<" + strip_name(method["return_type"]) + ">::__internal_constructor("
|
2017-06-20 22:42:29 +00:00
|
|
|
else:
|
2022-02-26 20:40:06 +00:00
|
|
|
return_statement += "return " + (
|
|
|
|
"(" + strip_name(method["return_type"]) + " *) " if is_class_type(method["return_type"]) else ""
|
|
|
|
)
|
2017-06-20 22:42:29 +00:00
|
|
|
else:
|
|
|
|
return_statement += "return "
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
def get_icall_type_name(name):
|
2017-09-11 19:34:20 +00:00
|
|
|
if is_enum(name):
|
|
|
|
return "int"
|
2017-05-12 19:53:07 +00:00
|
|
|
if is_class_type(name):
|
|
|
|
return "Object"
|
|
|
|
return name
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-06-20 22:42:29 +00:00
|
|
|
if method["has_varargs"]:
|
2017-05-16 07:00:17 +00:00
|
|
|
|
|
|
|
if len(method["arguments"]) != 0:
|
|
|
|
source.append("\tVariant __given_args[" + str(len(method["arguments"])) + "];")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
for i, argument in enumerate(method["arguments"]):
|
2017-10-20 23:42:10 +00:00
|
|
|
source.append("\tgodot::api->godot_variant_new_nil((godot_variant *) &__given_args[" + str(i) + "]);")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
for i, argument in enumerate(method["arguments"]):
|
|
|
|
source.append("\t__given_args[" + str(i) + "] = " + escape_cpp(argument["name"]) + ";")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
size = ""
|
|
|
|
if method["has_varargs"]:
|
|
|
|
size = "(__var_args.size() + " + str(len(method["arguments"])) + ")"
|
|
|
|
else:
|
|
|
|
size = "(" + str(len(method["arguments"])) + ")"
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2022-02-26 20:40:06 +00:00
|
|
|
source.append(
|
|
|
|
"\tgodot_variant **__args = (godot_variant **) alloca(sizeof(godot_variant *) * " + size + ");"
|
|
|
|
)
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
for i, argument in enumerate(method["arguments"]):
|
|
|
|
source.append("\t__args[" + str(i) + "] = (godot_variant *) &__given_args[" + str(i) + "];")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
if method["has_varargs"]:
|
|
|
|
source.append("\tfor (int i = 0; i < __var_args.size(); i++) {")
|
2022-02-26 20:40:06 +00:00
|
|
|
source.append(
|
|
|
|
"\t\t__args[i + "
|
|
|
|
+ str(len(method["arguments"]))
|
|
|
|
+ "] = (godot_variant *) &((Array &) __var_args)[i];"
|
|
|
|
)
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append("\t}")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append("\tVariant __result;")
|
2022-02-26 20:40:06 +00:00
|
|
|
source.append(
|
|
|
|
"\t*(godot_variant *) &__result = godot::api->godot_method_bind_call(___mb.mb_"
|
|
|
|
+ method["name"]
|
|
|
|
+ ", ((const Object *) "
|
|
|
|
+ core_object_name
|
|
|
|
+ ")->_owner, (const godot_variant **) __args, "
|
|
|
|
+ size
|
|
|
|
+ ", nullptr);"
|
|
|
|
)
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2018-02-11 14:50:01 +00:00
|
|
|
if is_class_type(method["return_type"]):
|
|
|
|
source.append("\tObject *obj = Object::___get_from_variant(__result);")
|
2022-02-26 20:40:06 +00:00
|
|
|
source.append('\tif (obj->has_method("reference"))')
|
|
|
|
source.append('\t\tobj->callv("reference", Array());')
|
2018-02-11 14:50:01 +00:00
|
|
|
|
|
|
|
source.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
for i, argument in enumerate(method["arguments"]):
|
2017-10-20 23:42:10 +00:00
|
|
|
source.append("\tgodot::api->godot_variant_destroy((godot_variant *) &__given_args[" + str(i) + "]);")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
if method["return_type"] != "void":
|
|
|
|
cast = ""
|
|
|
|
if is_class_type(method["return_type"]):
|
2020-07-28 17:46:01 +00:00
|
|
|
if return_type_is_ref:
|
2018-01-24 22:16:00 +00:00
|
|
|
cast += "Ref<" + strip_name(method["return_type"]) + ">::__internal_constructor(__result);"
|
2017-06-20 22:42:29 +00:00
|
|
|
else:
|
2022-02-26 20:40:06 +00:00
|
|
|
cast += (
|
|
|
|
"("
|
|
|
|
+ strip_name(method["return_type"])
|
|
|
|
+ " *) "
|
|
|
|
+ strip_name(method["return_type"] + "::___get_from_variant(")
|
|
|
|
+ "__result);"
|
|
|
|
)
|
2017-06-20 22:42:29 +00:00
|
|
|
else:
|
|
|
|
cast += "__result;"
|
|
|
|
source.append("\treturn " + cast)
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
else:
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
args = []
|
|
|
|
for arg in method["arguments"]:
|
|
|
|
args.append(get_icall_type_name(arg["type"]))
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
icall_ret_type = get_icall_type_name(method["return_type"])
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
icall_sig = tuple((icall_ret_type, tuple(args)))
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
icalls.add(icall_sig)
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
icall_name = get_icall_name(icall_sig)
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2019-04-07 22:45:35 +00:00
|
|
|
return_statement += icall_name + "(___mb.mb_" + method["name"] + ", (const Object *) " + core_object_name
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
for arg in method["arguments"]:
|
2020-07-28 17:46:01 +00:00
|
|
|
arg_is_ref = is_reference_type(arg["type"]) and ref_allowed
|
|
|
|
return_statement += ", " + escape_cpp(arg["name"]) + (".ptr()" if arg_is_ref else "")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
return_statement += ")"
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2020-07-28 17:46:01 +00:00
|
|
|
if return_type_is_ref:
|
|
|
|
return_statement += ")"
|
|
|
|
|
|
|
|
source.append("\t" + return_statement + ";")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append("}")
|
|
|
|
source.append("")
|
|
|
|
|
|
|
|
source.append("}")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
return "\n".join(source)
|
|
|
|
|
|
|
|
|
|
|
|
def generate_icall_header(icalls):
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
source = []
|
|
|
|
source.append("#ifndef GODOT_CPP__ICALLS_HPP")
|
|
|
|
source.append("#define GODOT_CPP__ICALLS_HPP")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-10-20 23:42:10 +00:00
|
|
|
source.append("#include <gdnative_api_struct.gen.h>")
|
2017-07-23 15:14:50 +00:00
|
|
|
source.append("#include <stdint.h>")
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-12-11 15:36:03 +00:00
|
|
|
source.append("#include <core/GodotGlobal.hpp>")
|
|
|
|
source.append("#include <core/CoreTypes.hpp>")
|
2022-02-26 20:40:06 +00:00
|
|
|
source.append('#include "Object.hpp"')
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append("")
|
|
|
|
source.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-13 11:55:04 +00:00
|
|
|
source.append("namespace godot {")
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
for icall in icalls:
|
|
|
|
ret_type = icall[0]
|
|
|
|
args = icall[1]
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2019-04-07 22:45:35 +00:00
|
|
|
method_signature = "static inline "
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2022-02-26 20:40:06 +00:00
|
|
|
method_signature += (
|
|
|
|
get_icall_return_type(ret_type) + get_icall_name(icall) + "(godot_method_bind *mb, const Object *inst"
|
|
|
|
)
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
for i, arg in enumerate(args):
|
|
|
|
method_signature += ", const "
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
if is_core_type(arg):
|
2019-04-07 22:45:35 +00:00
|
|
|
method_signature += arg + "&"
|
2018-11-17 16:23:52 +00:00
|
|
|
elif arg == "int":
|
|
|
|
method_signature += "int64_t "
|
2018-05-09 00:58:48 +00:00
|
|
|
elif arg == "float":
|
|
|
|
method_signature += "double "
|
2017-05-12 19:53:07 +00:00
|
|
|
elif is_primitive(arg):
|
|
|
|
method_signature += arg + " "
|
|
|
|
else:
|
|
|
|
method_signature += "Object *"
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
method_signature += "arg" + str(i)
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
method_signature += ")"
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append(method_signature + " {")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
if ret_type != "void":
|
2022-02-26 20:40:06 +00:00
|
|
|
source.append(
|
|
|
|
"\t" + ("godot_object *" if is_class_type(ret_type) else get_icall_return_type(ret_type)) + "ret;"
|
|
|
|
)
|
2017-05-12 19:53:07 +00:00
|
|
|
if is_class_type(ret_type):
|
|
|
|
source.append("\tret = nullptr;")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append("\tconst void *args[" + ("1" if len(args) == 0 else "") + "] = {")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
for i, arg in enumerate(args):
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
wrapped_argument = "\t\t"
|
|
|
|
if is_primitive(arg) or is_core_type(arg):
|
|
|
|
wrapped_argument += "(void *) &arg" + str(i)
|
|
|
|
else:
|
2018-11-19 20:40:31 +00:00
|
|
|
wrapped_argument += "(void *) (arg" + str(i) + ") ? arg" + str(i) + "->_owner : nullptr"
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
wrapped_argument += ","
|
|
|
|
source.append(wrapped_argument)
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append("\t};")
|
|
|
|
source.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2022-02-26 20:40:06 +00:00
|
|
|
source.append(
|
|
|
|
"\tgodot::api->godot_method_bind_ptrcall(mb, inst->_owner, args, "
|
|
|
|
+ ("nullptr" if ret_type == "void" else "&ret")
|
|
|
|
+ ");"
|
|
|
|
)
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
if ret_type != "void":
|
2018-02-11 14:50:01 +00:00
|
|
|
if is_class_type(ret_type):
|
|
|
|
source.append("\tif (ret) {")
|
2022-02-26 20:40:06 +00:00
|
|
|
source.append(
|
|
|
|
"\t\treturn (Object *) godot::nativescript_1_1_api->godot_nativescript_get_instance_binding_data(godot::_RegisterState::language_index, ret);"
|
|
|
|
)
|
2018-02-11 14:50:01 +00:00
|
|
|
source.append("\t}")
|
|
|
|
source.append("")
|
|
|
|
source.append("\treturn (Object *) ret;")
|
|
|
|
else:
|
|
|
|
source.append("\treturn ret;")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append("}")
|
2019-04-07 22:45:35 +00:00
|
|
|
source.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-13 11:55:04 +00:00
|
|
|
source.append("}")
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2019-04-07 22:45:35 +00:00
|
|
|
source.append("#endif")
|
2017-05-12 19:53:07 +00:00
|
|
|
|
2019-04-07 22:45:35 +00:00
|
|
|
return "\n".join(source)
|
2017-05-12 19:53:07 +00:00
|
|
|
|
|
|
|
|
2018-02-11 14:50:01 +00:00
|
|
|
def generate_type_registry(classes):
|
|
|
|
source = []
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2022-02-26 20:40:06 +00:00
|
|
|
source.append('#include "TagDB.hpp"')
|
2018-02-11 14:50:01 +00:00
|
|
|
source.append("#include <typeinfo>")
|
|
|
|
source.append("\n")
|
|
|
|
|
|
|
|
for c in classes:
|
|
|
|
source.append("#include <" + strip_name(c["name"]) + ".hpp>")
|
|
|
|
|
|
|
|
source.append("")
|
|
|
|
source.append("")
|
|
|
|
|
|
|
|
source.append("namespace godot {")
|
|
|
|
|
|
|
|
source.append("void ___register_types()")
|
|
|
|
source.append("{")
|
|
|
|
|
|
|
|
for c in classes:
|
|
|
|
class_name = strip_name(c["name"])
|
|
|
|
base_class_name = strip_name(c["base_class"])
|
|
|
|
|
|
|
|
class_type_hash = "typeid(" + class_name + ").hash_code()"
|
|
|
|
|
|
|
|
base_class_type_hash = "typeid(" + base_class_name + ").hash_code()"
|
|
|
|
|
|
|
|
if base_class_name == "":
|
|
|
|
base_class_type_hash = "0"
|
2017-05-12 19:53:07 +00:00
|
|
|
|
2022-02-26 20:40:06 +00:00
|
|
|
source.append(
|
|
|
|
'\tgodot::_TagDB::register_global_type("'
|
|
|
|
+ c["name"]
|
|
|
|
+ '", '
|
|
|
|
+ class_type_hash
|
|
|
|
+ ", "
|
|
|
|
+ base_class_type_hash
|
|
|
|
+ ");"
|
|
|
|
)
|
2017-05-12 19:53:07 +00:00
|
|
|
|
2018-02-11 14:50:01 +00:00
|
|
|
source.append("}")
|
|
|
|
|
|
|
|
source.append("")
|
|
|
|
source.append("}")
|
|
|
|
|
|
|
|
return "\n".join(source)
|
2017-05-12 19:53:07 +00:00
|
|
|
|
|
|
|
|
2019-04-07 22:45:35 +00:00
|
|
|
def generate_init_method_bindings(classes):
|
|
|
|
source = []
|
2017-05-12 19:53:07 +00:00
|
|
|
|
2019-04-07 22:45:35 +00:00
|
|
|
for c in classes:
|
|
|
|
source.append("#include <" + strip_name(c["name"]) + ".hpp>")
|
2017-05-12 19:53:07 +00:00
|
|
|
|
2019-04-07 22:45:35 +00:00
|
|
|
source.append("")
|
|
|
|
source.append("")
|
2017-05-12 19:53:07 +00:00
|
|
|
|
2019-04-07 22:45:35 +00:00
|
|
|
source.append("namespace godot {")
|
2017-05-12 19:53:07 +00:00
|
|
|
|
2019-04-07 22:45:35 +00:00
|
|
|
source.append("void ___init_method_bindings()")
|
|
|
|
source.append("{")
|
2017-05-12 19:53:07 +00:00
|
|
|
|
2019-04-07 22:45:35 +00:00
|
|
|
for c in classes:
|
|
|
|
source.append("\t" + strip_name(c["name"]) + "::___init_method_bindings();")
|
2017-05-12 19:53:07 +00:00
|
|
|
|
2019-04-07 22:45:35 +00:00
|
|
|
source.append("}")
|
2017-05-12 19:53:07 +00:00
|
|
|
|
2019-04-07 22:45:35 +00:00
|
|
|
source.append("")
|
|
|
|
source.append("}")
|
2017-05-12 19:53:07 +00:00
|
|
|
|
2019-04-07 22:45:35 +00:00
|
|
|
return "\n".join(source)
|
2017-05-12 19:53:07 +00:00
|
|
|
|
|
|
|
|
2019-04-07 22:45:35 +00:00
|
|
|
def get_icall_return_type(t):
|
2017-05-12 19:53:07 +00:00
|
|
|
if is_class_type(t):
|
|
|
|
return "Object *"
|
2017-07-23 15:14:50 +00:00
|
|
|
if t == "int":
|
|
|
|
return "int64_t "
|
|
|
|
if t == "float" or t == "real":
|
|
|
|
return "double "
|
2017-05-12 19:53:07 +00:00
|
|
|
return t + " "
|
|
|
|
|
|
|
|
|
|
|
|
def get_icall_name(sig):
|
|
|
|
ret_type = sig[0]
|
|
|
|
args = sig[1]
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
name = "___godot_icall_"
|
|
|
|
name += strip_name(ret_type)
|
|
|
|
for arg in args:
|
|
|
|
name += "_" + strip_name(arg)
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
return name
|
|
|
|
|
|
|
|
|
|
|
|
def get_used_classes(c):
|
|
|
|
classes = []
|
|
|
|
for method in c["methods"]:
|
|
|
|
if is_class_type(method["return_type"]) and not (method["return_type"] in classes):
|
|
|
|
classes.append(method["return_type"])
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
for arg in method["arguments"]:
|
|
|
|
if is_class_type(arg["type"]) and not (arg["type"] in classes):
|
|
|
|
classes.append(arg["type"])
|
|
|
|
return classes
|
|
|
|
|
|
|
|
|
|
|
|
def strip_name(name):
|
2018-02-11 14:50:01 +00:00
|
|
|
if len(name) == 0:
|
|
|
|
return name
|
2022-02-26 20:40:06 +00:00
|
|
|
if name[0] == "_":
|
2017-05-12 19:53:07 +00:00
|
|
|
return name[1:]
|
|
|
|
return name
|
|
|
|
|
2022-02-26 20:40:06 +00:00
|
|
|
|
2017-09-11 19:34:20 +00:00
|
|
|
def extract_nested_type(nested_type):
|
2022-02-26 20:40:06 +00:00
|
|
|
return strip_name(nested_type[: nested_type.find("::")])
|
|
|
|
|
2017-09-11 19:34:20 +00:00
|
|
|
|
|
|
|
def remove_nested_type_prefix(name):
|
2022-02-26 20:40:06 +00:00
|
|
|
return name if name.find("::") == -1 else strip_name(name[name.find("::") + 2 :])
|
|
|
|
|
2017-09-11 19:34:20 +00:00
|
|
|
|
|
|
|
def remove_enum_prefix(name):
|
2022-02-26 20:40:06 +00:00
|
|
|
return strip_name(name[name.find("enum.") + 5 :])
|
|
|
|
|
2017-09-11 19:34:20 +00:00
|
|
|
|
2022-02-26 20:40:06 +00:00
|
|
|
def is_nested_type(name, type=""):
|
2017-09-11 19:34:20 +00:00
|
|
|
return name.find(type + "::") != -1
|
|
|
|
|
2022-02-26 20:40:06 +00:00
|
|
|
|
2017-09-11 19:34:20 +00:00
|
|
|
def is_enum(name):
|
|
|
|
return name.find("enum.") == 0
|
2017-05-12 19:53:07 +00:00
|
|
|
|
2022-02-26 20:40:06 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
def is_class_type(name):
|
|
|
|
return not is_core_type(name) and not is_primitive(name)
|
|
|
|
|
2022-02-26 20:40:06 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
def is_core_type(name):
|
2022-02-26 20:40:06 +00:00
|
|
|
core_types = [
|
|
|
|
"Array",
|
|
|
|
"Basis",
|
|
|
|
"Color",
|
|
|
|
"Dictionary",
|
|
|
|
"Error",
|
|
|
|
"NodePath",
|
|
|
|
"Plane",
|
|
|
|
"PoolByteArray",
|
|
|
|
"PoolIntArray",
|
|
|
|
"PoolRealArray",
|
|
|
|
"PoolStringArray",
|
|
|
|
"PoolVector2Array",
|
|
|
|
"PoolVector3Array",
|
|
|
|
"PoolColorArray",
|
|
|
|
"PoolIntArray",
|
|
|
|
"PoolRealArray",
|
|
|
|
"Quat",
|
|
|
|
"Rect2",
|
|
|
|
"AABB",
|
|
|
|
"RID",
|
|
|
|
"String",
|
|
|
|
"Transform",
|
|
|
|
"Transform2D",
|
|
|
|
"Variant",
|
|
|
|
"Vector2",
|
|
|
|
"Vector3",
|
|
|
|
]
|
2017-05-12 19:53:07 +00:00
|
|
|
return name in core_types
|
|
|
|
|
|
|
|
|
|
|
|
def is_primitive(name):
|
|
|
|
core_types = ["int", "bool", "real", "float", "void"]
|
|
|
|
return name in core_types
|
|
|
|
|
2022-02-26 20:40:06 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
def escape_cpp(name):
|
|
|
|
escapes = {
|
2022-02-26 20:40:06 +00:00
|
|
|
"class": "_class",
|
|
|
|
"enum": "_enum",
|
|
|
|
"char": "_char",
|
|
|
|
"short": "_short",
|
|
|
|
"bool": "_bool",
|
|
|
|
"int": "_int",
|
|
|
|
"default": "_default",
|
|
|
|
"case": "_case",
|
|
|
|
"switch": "_switch",
|
|
|
|
"export": "_export",
|
2017-05-12 19:53:07 +00:00
|
|
|
"template": "_template",
|
2022-02-26 20:40:06 +00:00
|
|
|
"new": "new_",
|
2019-02-04 01:55:16 +00:00
|
|
|
"operator": "_operator",
|
2022-02-26 20:40:06 +00:00
|
|
|
"typename": "_typename",
|
2017-05-12 19:53:07 +00:00
|
|
|
}
|
|
|
|
if name in escapes:
|
|
|
|
return escapes[name]
|
|
|
|
return name
|