2019-03-26 23:51:51 +00:00
|
|
|
#!/usr/bin/env python
|
2021-08-18 14:03:52 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
import json
|
2021-08-18 14:03:52 +00:00
|
|
|
import re
|
|
|
|
import shutil
|
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
|
|
|
|
2022-09-27 10:46:18 +00:00
|
|
|
def generate_mod_version(argcount, const=False, returns=False):
|
|
|
|
s = """
|
|
|
|
#define MODBIND$VER($RETTYPE m_name$ARG) \\
|
|
|
|
virtual $RETVAL _##m_name($FUNCARGS) $CONST override; \\
|
|
|
|
"""
|
|
|
|
sproto = str(argcount)
|
|
|
|
if returns:
|
|
|
|
sproto += "R"
|
|
|
|
s = s.replace("$RETTYPE", "m_ret, ")
|
|
|
|
s = s.replace("$RETVAL", "m_ret")
|
|
|
|
|
|
|
|
else:
|
|
|
|
s = s.replace("$RETTYPE", "")
|
|
|
|
s = s.replace("$RETVAL", "void")
|
|
|
|
|
|
|
|
if const:
|
|
|
|
sproto += "C"
|
|
|
|
s = s.replace("$CONST", "const")
|
|
|
|
else:
|
|
|
|
s = s.replace("$CONST", "")
|
|
|
|
|
|
|
|
s = s.replace("$VER", sproto)
|
|
|
|
argtext = ""
|
|
|
|
funcargs = ""
|
|
|
|
|
|
|
|
for i in range(argcount):
|
|
|
|
if i > 0:
|
|
|
|
funcargs += ", "
|
|
|
|
|
|
|
|
argtext += ", m_type" + str(i + 1)
|
|
|
|
funcargs += "m_type" + str(i + 1) + " arg" + str(i + 1)
|
|
|
|
|
|
|
|
if argcount:
|
|
|
|
s = s.replace("$ARG", argtext)
|
|
|
|
s = s.replace("$FUNCARGS", funcargs)
|
|
|
|
else:
|
|
|
|
s = s.replace("$ARG", "")
|
|
|
|
s = s.replace("$FUNCARGS", funcargs)
|
|
|
|
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
|
|
|
def generate_wrappers(target):
|
|
|
|
max_versions = 12
|
|
|
|
|
|
|
|
txt = """
|
|
|
|
#ifndef GDEXTENSION_WRAPPERS_GEN_H
|
|
|
|
#define GDEXTENSION_WRAPPERS_GEN_H
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
for i in range(max_versions + 1):
|
|
|
|
txt += "\n/* Module Wrapper " + str(i) + " Arguments */\n"
|
|
|
|
txt += generate_mod_version(i, False, False)
|
|
|
|
txt += generate_mod_version(i, False, True)
|
|
|
|
txt += generate_mod_version(i, True, False)
|
|
|
|
txt += generate_mod_version(i, True, True)
|
|
|
|
|
|
|
|
txt += "\n#endif\n"
|
|
|
|
|
2023-06-16 07:38:01 +00:00
|
|
|
with open(target, "w", encoding="utf-8") as f:
|
2022-09-27 10:46:18 +00:00
|
|
|
f.write(txt)
|
|
|
|
|
|
|
|
|
2023-07-07 19:49:56 +00:00
|
|
|
def get_file_list(api_filepath, output_dir, headers=False, sources=False, profile_filepath=""):
|
2021-08-18 14:03:52 +00:00
|
|
|
api = {}
|
2022-04-30 13:49:51 +00:00
|
|
|
files = []
|
2023-06-16 07:38:01 +00:00
|
|
|
with open(api_filepath, encoding="utf-8") as api_file:
|
2021-08-18 14:03:52 +00:00
|
|
|
api = json.load(api_file)
|
2020-06-17 19:12:57 +00:00
|
|
|
|
2023-07-07 19:49:56 +00:00
|
|
|
build_profile = parse_build_profile(profile_filepath, api)
|
|
|
|
|
2022-09-27 10:46:18 +00:00
|
|
|
core_gen_folder = Path(output_dir) / "gen" / "include" / "godot_cpp" / "core"
|
2021-08-18 14:03:52 +00:00
|
|
|
include_gen_folder = Path(output_dir) / "gen" / "include" / "godot_cpp"
|
|
|
|
source_gen_folder = Path(output_dir) / "gen" / "src"
|
2017-06-20 22:42:29 +00:00
|
|
|
|
2022-09-27 10:46:18 +00:00
|
|
|
files.append(str((core_gen_folder / "ext_wrappers.gen.inc").as_posix()))
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
for builtin_class in api["builtin_classes"]:
|
|
|
|
if is_pod_type(builtin_class["name"]):
|
|
|
|
continue
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-09-01 03:11:10 +00:00
|
|
|
if is_included_type(builtin_class["name"]):
|
|
|
|
continue
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
header_filename = include_gen_folder / "variant" / (camel_to_snake(builtin_class["name"]) + ".hpp")
|
|
|
|
source_filename = source_gen_folder / "variant" / (camel_to_snake(builtin_class["name"]) + ".cpp")
|
|
|
|
if headers:
|
2022-04-30 13:49:51 +00:00
|
|
|
files.append(str(header_filename.as_posix()))
|
2021-08-18 14:03:52 +00:00
|
|
|
if sources:
|
2022-04-30 13:49:51 +00:00
|
|
|
files.append(str(source_filename.as_posix()))
|
2021-08-18 14:03:52 +00:00
|
|
|
|
|
|
|
for engine_class in api["classes"]:
|
2023-07-07 02:16:27 +00:00
|
|
|
# Generate code for the ClassDB singleton under a different name.
|
2021-08-31 15:29:13 +00:00
|
|
|
if engine_class["name"] == "ClassDB":
|
2023-07-07 02:16:27 +00:00
|
|
|
engine_class["name"] = "ClassDBSingleton"
|
|
|
|
engine_class["alias_for"] = "ClassDB"
|
2021-08-18 14:03:52 +00:00
|
|
|
header_filename = include_gen_folder / "classes" / (camel_to_snake(engine_class["name"]) + ".hpp")
|
|
|
|
source_filename = source_gen_folder / "classes" / (camel_to_snake(engine_class["name"]) + ".cpp")
|
2020-02-02 19:47:45 +00:00
|
|
|
if headers:
|
2022-04-30 13:49:51 +00:00
|
|
|
files.append(str(header_filename.as_posix()))
|
2023-07-07 19:49:56 +00:00
|
|
|
if sources and is_class_included(engine_class["name"], build_profile):
|
2022-04-30 13:49:51 +00:00
|
|
|
files.append(str(source_filename.as_posix()))
|
2021-08-18 14:03:52 +00:00
|
|
|
|
2022-09-11 19:35:09 +00:00
|
|
|
for native_struct in api["native_structures"]:
|
|
|
|
struct_name = native_struct["name"]
|
2023-10-21 23:32:45 +00:00
|
|
|
if struct_name == "ObjectID":
|
|
|
|
continue
|
2022-09-11 19:35:09 +00:00
|
|
|
snake_struct_name = camel_to_snake(struct_name)
|
|
|
|
|
|
|
|
header_filename = include_gen_folder / "classes" / (snake_struct_name + ".hpp")
|
|
|
|
if headers:
|
|
|
|
files.append(str(header_filename.as_posix()))
|
|
|
|
|
2020-02-02 19:47:45 +00:00
|
|
|
if headers:
|
2022-09-11 19:35:09 +00:00
|
|
|
for path in [
|
|
|
|
include_gen_folder / "variant" / "builtin_types.hpp",
|
2022-10-06 08:32:48 +00:00
|
|
|
include_gen_folder / "variant" / "builtin_binds.hpp",
|
2022-09-11 19:35:09 +00:00
|
|
|
include_gen_folder / "variant" / "utility_functions.hpp",
|
|
|
|
include_gen_folder / "variant" / "variant_size.hpp",
|
2023-04-14 07:33:49 +00:00
|
|
|
include_gen_folder / "variant" / "builtin_vararg_methods.hpp",
|
2022-09-11 19:35:09 +00:00
|
|
|
include_gen_folder / "classes" / "global_constants.hpp",
|
|
|
|
include_gen_folder / "classes" / "global_constants_binds.hpp",
|
2023-07-26 22:16:46 +00:00
|
|
|
include_gen_folder / "core" / "version.hpp",
|
2022-09-11 19:35:09 +00:00
|
|
|
]:
|
|
|
|
files.append(str(path.as_posix()))
|
2020-02-02 19:47:45 +00:00
|
|
|
if sources:
|
2022-09-11 19:35:09 +00:00
|
|
|
utility_functions_source_path = source_gen_folder / "variant" / "utility_functions.cpp"
|
2022-04-30 13:49:51 +00:00
|
|
|
files.append(str(utility_functions_source_path.as_posix()))
|
2022-09-11 19:35:09 +00:00
|
|
|
|
2022-04-30 13:49:51 +00:00
|
|
|
return files
|
|
|
|
|
|
|
|
|
2023-07-07 19:49:56 +00:00
|
|
|
def print_file_list(api_filepath, output_dir, headers=False, sources=False, profile_filepath=""):
|
|
|
|
print(*get_file_list(api_filepath, output_dir, headers, sources, profile_filepath), sep=";", end=None)
|
|
|
|
|
|
|
|
|
|
|
|
def parse_build_profile(profile_filepath, api):
|
|
|
|
if profile_filepath == "":
|
|
|
|
return {}
|
|
|
|
print("Using feature build profile: " + profile_filepath)
|
|
|
|
|
|
|
|
with open(profile_filepath, encoding="utf-8") as profile_file:
|
|
|
|
profile = json.load(profile_file)
|
|
|
|
|
|
|
|
api_dict = {}
|
|
|
|
parents = {}
|
|
|
|
children = {}
|
|
|
|
for engine_class in api["classes"]:
|
|
|
|
api_dict[engine_class["name"]] = engine_class
|
|
|
|
parent = engine_class.get("inherits", "")
|
|
|
|
child = engine_class["name"]
|
|
|
|
parents[child] = parent
|
|
|
|
if parent == "":
|
|
|
|
continue
|
|
|
|
children[parent] = children.get(parent, [])
|
|
|
|
children[parent].append(child)
|
|
|
|
|
|
|
|
# Parse methods dependencies
|
|
|
|
deps = {}
|
|
|
|
reverse_deps = {}
|
|
|
|
for name, engine_class in api_dict.items():
|
|
|
|
ref_cls = set()
|
|
|
|
for method in engine_class.get("methods", []):
|
|
|
|
rtype = method.get("return_value", {}).get("type", "")
|
|
|
|
args = [a["type"] for a in method.get("arguments", [])]
|
|
|
|
if rtype in api_dict:
|
|
|
|
ref_cls.add(rtype)
|
|
|
|
elif is_enum(rtype) and get_enum_class(rtype) in api_dict:
|
|
|
|
ref_cls.add(get_enum_class(rtype))
|
|
|
|
for arg in args:
|
|
|
|
if arg in api_dict:
|
|
|
|
ref_cls.add(arg)
|
|
|
|
elif is_enum(arg) and get_enum_class(arg) in api_dict:
|
|
|
|
ref_cls.add(get_enum_class(arg))
|
|
|
|
deps[engine_class["name"]] = set(filter(lambda x: x != name, ref_cls))
|
|
|
|
for acls in ref_cls:
|
|
|
|
if acls == name:
|
|
|
|
continue
|
|
|
|
reverse_deps[acls] = reverse_deps.get(acls, set())
|
|
|
|
reverse_deps[acls].add(name)
|
|
|
|
|
|
|
|
included = []
|
|
|
|
front = list(profile.get("enabled_classes", []))
|
|
|
|
if front:
|
|
|
|
# These must always be included
|
|
|
|
front.append("WorkerThreadPool")
|
|
|
|
front.append("ClassDB")
|
|
|
|
front.append("ClassDBSingleton")
|
|
|
|
while front:
|
|
|
|
cls = front.pop()
|
|
|
|
if cls in included:
|
|
|
|
continue
|
|
|
|
included.append(cls)
|
|
|
|
parent = parents.get(cls, "")
|
|
|
|
if parent:
|
|
|
|
front.append(parent)
|
|
|
|
for rcls in deps.get(cls, set()):
|
|
|
|
if rcls in included or rcls in front:
|
|
|
|
continue
|
|
|
|
front.append(rcls)
|
|
|
|
|
|
|
|
excluded = []
|
|
|
|
front = list(profile.get("disabled_classes", []))
|
|
|
|
while front:
|
|
|
|
cls = front.pop()
|
|
|
|
if cls in excluded:
|
|
|
|
continue
|
|
|
|
excluded.append(cls)
|
|
|
|
front += children.get(cls, [])
|
|
|
|
for rcls in reverse_deps.get(cls, set()):
|
|
|
|
if rcls in excluded or rcls in front:
|
|
|
|
continue
|
|
|
|
front.append(rcls)
|
|
|
|
|
|
|
|
if included and excluded:
|
|
|
|
print(
|
|
|
|
"WARNING: Cannot specify both 'enabled_classes' and 'disabled_classes' in build profile. 'disabled_classes' will be ignored."
|
|
|
|
)
|
|
|
|
|
|
|
|
return {
|
|
|
|
"enabled_classes": included,
|
|
|
|
"disabled_classes": excluded,
|
|
|
|
}
|
2022-04-30 13:49:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
def scons_emit_files(target, source, env):
|
2023-07-07 19:49:56 +00:00
|
|
|
profile_filepath = env.get("build_profile", "")
|
|
|
|
if profile_filepath and not Path(profile_filepath).is_absolute():
|
|
|
|
profile_filepath = str((Path(env.Dir("#").abspath) / profile_filepath).as_posix())
|
|
|
|
|
|
|
|
files = [env.File(f) for f in get_file_list(str(source[0]), target[0].abspath, True, True, profile_filepath)]
|
2022-10-31 09:24:24 +00:00
|
|
|
env.Clean(target, files)
|
|
|
|
env["godot_cpp_gen_dir"] = target[0].abspath
|
|
|
|
return files, source
|
2022-04-30 13:49:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
def scons_generate_bindings(target, source, env):
|
2022-03-20 16:19:27 +00:00
|
|
|
generate_bindings(
|
|
|
|
str(source[0]),
|
|
|
|
env["generate_template_get_node"],
|
2022-05-29 08:51:33 +00:00
|
|
|
"32" if "32" in env["arch"] else "64",
|
2023-01-09 10:03:07 +00:00
|
|
|
env["precision"],
|
2022-10-31 09:24:24 +00:00
|
|
|
env["godot_cpp_gen_dir"],
|
2022-03-20 16:19:27 +00:00
|
|
|
)
|
2022-04-30 13:49:51 +00:00
|
|
|
return None
|
2020-02-02 19:47:45 +00:00
|
|
|
|
|
|
|
|
2023-01-09 10:03:07 +00:00
|
|
|
def generate_bindings(api_filepath, use_template_get_node, bits="64", precision="single", output_dir="."):
|
2021-08-18 14:03:52 +00:00
|
|
|
api = None
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
target_dir = Path(output_dir) / "gen"
|
2021-05-15 20:57:05 +00:00
|
|
|
|
2023-06-16 07:38:01 +00:00
|
|
|
with open(api_filepath, encoding="utf-8") as api_file:
|
2021-08-18 14:03:52 +00:00
|
|
|
api = json.load(api_file)
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
shutil.rmtree(target_dir, ignore_errors=True)
|
|
|
|
target_dir.mkdir(parents=True)
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2023-01-09 10:03:07 +00:00
|
|
|
real_t = "double" if precision == "double" else "float"
|
|
|
|
print("Built-in type config: " + real_t + "_" + bits)
|
2022-03-20 16:19:27 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
generate_global_constants(api, target_dir)
|
2023-07-26 22:16:46 +00:00
|
|
|
generate_version_header(api, target_dir)
|
2022-02-15 08:47:12 +00:00
|
|
|
generate_global_constant_binds(api, target_dir)
|
2023-01-09 10:03:07 +00:00
|
|
|
generate_builtin_bindings(api, target_dir, real_t + "_" + bits)
|
2021-08-18 14:03:52 +00:00
|
|
|
generate_engine_classes_bindings(api, target_dir, use_template_get_node)
|
|
|
|
generate_utility_functions(api, target_dir)
|
2019-03-26 23:51:51 +00:00
|
|
|
|
|
|
|
|
2024-04-23 18:37:43 +00:00
|
|
|
CLASS_ALIASES = {
|
|
|
|
"ClassDB": "ClassDBSingleton",
|
|
|
|
}
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
builtin_classes = []
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
# Key is class name, value is boolean where True means the class is refcounted.
|
|
|
|
engine_classes = {}
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2022-02-16 09:29:01 +00:00
|
|
|
# Type names of native structures
|
|
|
|
native_structures = []
|
|
|
|
|
2021-08-25 00:50:39 +00:00
|
|
|
singletons = []
|
|
|
|
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
def generate_builtin_bindings(api, output_dir, build_config):
|
|
|
|
global builtin_classes
|
2018-02-11 14:50:01 +00:00
|
|
|
|
2022-09-27 10:46:18 +00:00
|
|
|
core_gen_folder = Path(output_dir) / "include" / "godot_cpp" / "core"
|
2021-08-18 14:03:52 +00:00
|
|
|
include_gen_folder = Path(output_dir) / "include" / "godot_cpp" / "variant"
|
|
|
|
source_gen_folder = Path(output_dir) / "src" / "variant"
|
2019-04-07 22:45:35 +00:00
|
|
|
|
2022-09-27 10:46:18 +00:00
|
|
|
core_gen_folder.mkdir(parents=True, exist_ok=True)
|
2021-08-18 14:03:52 +00:00
|
|
|
include_gen_folder.mkdir(parents=True, exist_ok=True)
|
|
|
|
source_gen_folder.mkdir(parents=True, exist_ok=True)
|
2017-06-20 22:42:29 +00:00
|
|
|
|
2022-09-27 10:46:18 +00:00
|
|
|
generate_wrappers(core_gen_folder / "ext_wrappers.gen.inc")
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
# Store types beforehand.
|
|
|
|
for builtin_api in api["builtin_classes"]:
|
|
|
|
if is_pod_type(builtin_api["name"]):
|
2017-06-20 22:42:29 +00:00
|
|
|
continue
|
2021-08-18 14:03:52 +00:00
|
|
|
builtin_classes.append(builtin_api["name"])
|
2017-05-12 19:53:07 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
builtin_sizes = {}
|
2019-04-07 22:45:35 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
for size_list in api["builtin_class_sizes"]:
|
|
|
|
if size_list["build_configuration"] == build_config:
|
|
|
|
for size in size_list["sizes"]:
|
|
|
|
builtin_sizes[size["name"]] = size["size"]
|
|
|
|
break
|
2017-05-12 19:53:07 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
# Create a file for Variant size, since that class isn't generated.
|
|
|
|
variant_size_filename = include_gen_folder / "variant_size.hpp"
|
2023-06-16 07:38:01 +00:00
|
|
|
with variant_size_filename.open("+w", encoding="utf-8") as variant_size_file:
|
2021-08-18 14:03:52 +00:00
|
|
|
variant_size_source = []
|
|
|
|
add_header("variant_size.hpp", variant_size_source)
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
header_guard = "GODOT_CPP_VARIANT_SIZE_HPP"
|
|
|
|
variant_size_source.append(f"#ifndef {header_guard}")
|
|
|
|
variant_size_source.append(f"#define {header_guard}")
|
|
|
|
variant_size_source.append(f'#define GODOT_CPP_VARIANT_SIZE {builtin_sizes["Variant"]}')
|
|
|
|
variant_size_source.append(f"#endif // ! {header_guard}")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
variant_size_file.write("\n".join(variant_size_source))
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
for builtin_api in api["builtin_classes"]:
|
|
|
|
if is_pod_type(builtin_api["name"]):
|
|
|
|
continue
|
2021-09-01 03:11:10 +00:00
|
|
|
if is_included_type(builtin_api["name"]):
|
|
|
|
continue
|
2017-09-11 19:34:20 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
size = builtin_sizes[builtin_api["name"]]
|
|
|
|
|
|
|
|
header_filename = include_gen_folder / (camel_to_snake(builtin_api["name"]) + ".hpp")
|
|
|
|
source_filename = source_gen_folder / (camel_to_snake(builtin_api["name"]) + ".cpp")
|
|
|
|
|
|
|
|
# Check used classes for header include
|
|
|
|
used_classes = set()
|
|
|
|
fully_used_classes = set()
|
|
|
|
|
|
|
|
class_name = builtin_api["name"]
|
|
|
|
|
|
|
|
if "constructors" in builtin_api:
|
|
|
|
for constructor in builtin_api["constructors"]:
|
|
|
|
if "arguments" in constructor:
|
|
|
|
for argument in constructor["arguments"]:
|
|
|
|
if is_included(argument["type"], class_name):
|
|
|
|
if "default_value" in argument and argument["type"] != "Variant":
|
|
|
|
fully_used_classes.add(argument["type"])
|
|
|
|
else:
|
|
|
|
used_classes.add(argument["type"])
|
|
|
|
|
|
|
|
if "methods" in builtin_api:
|
|
|
|
for method in builtin_api["methods"]:
|
|
|
|
if "arguments" in method:
|
|
|
|
for argument in method["arguments"]:
|
|
|
|
if is_included(argument["type"], class_name):
|
|
|
|
if "default_value" in argument and argument["type"] != "Variant":
|
|
|
|
fully_used_classes.add(argument["type"])
|
|
|
|
else:
|
|
|
|
used_classes.add(argument["type"])
|
|
|
|
if "return_type" in method:
|
|
|
|
if is_included(method["return_type"], class_name):
|
|
|
|
used_classes.add(method["return_type"])
|
|
|
|
|
|
|
|
if "members" in builtin_api:
|
|
|
|
for member in builtin_api["members"]:
|
|
|
|
if is_included(member["type"], class_name):
|
|
|
|
used_classes.add(member["type"])
|
|
|
|
|
|
|
|
if "indexing_return_type" in builtin_api:
|
|
|
|
if is_included(builtin_api["indexing_return_type"], class_name):
|
|
|
|
used_classes.add(builtin_api["indexing_return_type"])
|
|
|
|
|
|
|
|
if "operators" in builtin_api:
|
|
|
|
for operator in builtin_api["operators"]:
|
|
|
|
if "right_type" in operator:
|
2022-10-03 12:28:33 +00:00
|
|
|
if is_included(operator["right_type"], class_name):
|
2021-08-18 14:03:52 +00:00
|
|
|
used_classes.add(operator["right_type"])
|
|
|
|
|
|
|
|
for type_name in fully_used_classes:
|
|
|
|
if type_name in used_classes:
|
|
|
|
used_classes.remove(type_name)
|
2017-09-11 19:34:20 +00:00
|
|
|
|
2022-09-13 08:37:58 +00:00
|
|
|
used_classes = list(used_classes)
|
|
|
|
used_classes.sort()
|
|
|
|
fully_used_classes = list(fully_used_classes)
|
|
|
|
fully_used_classes.sort()
|
|
|
|
|
2023-06-16 07:38:01 +00:00
|
|
|
with header_filename.open("w+", encoding="utf-8") as header_file:
|
2021-08-18 14:03:52 +00:00
|
|
|
header_file.write(generate_builtin_class_header(builtin_api, size, used_classes, fully_used_classes))
|
2018-01-22 21:27:10 +00:00
|
|
|
|
2023-06-16 07:38:01 +00:00
|
|
|
with source_filename.open("w+", encoding="utf-8") as source_file:
|
2021-08-18 14:03:52 +00:00
|
|
|
source_file.write(generate_builtin_class_source(builtin_api, size, used_classes, fully_used_classes))
|
2018-01-22 21:27:10 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
# Create a header with all builtin types for convenience.
|
|
|
|
builtin_header_filename = include_gen_folder / "builtin_types.hpp"
|
2023-06-16 07:38:01 +00:00
|
|
|
with builtin_header_filename.open("w+", encoding="utf-8") as builtin_header_file:
|
2021-08-18 14:03:52 +00:00
|
|
|
builtin_header = []
|
|
|
|
add_header("builtin_types.hpp", builtin_header)
|
2017-09-11 19:34:20 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
builtin_header.append("#ifndef GODOT_CPP_BUILTIN_TYPES_HPP")
|
|
|
|
builtin_header.append("#define GODOT_CPP_BUILTIN_TYPES_HPP")
|
2017-09-11 19:34:20 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
builtin_header.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
for builtin in builtin_classes:
|
2021-08-19 17:47:56 +00:00
|
|
|
builtin_header.append(f"#include <godot_cpp/variant/{camel_to_snake(builtin)}.hpp>")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
builtin_header.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
builtin_header.append("#endif // ! GODOT_CPP_BUILTIN_TYPES_HPP")
|
2017-09-11 19:34:20 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
builtin_header_file.write("\n".join(builtin_header))
|
2017-09-11 19:34:20 +00:00
|
|
|
|
2022-10-06 08:32:48 +00:00
|
|
|
# Create a header with bindings for builtin types.
|
|
|
|
builtin_binds_filename = include_gen_folder / "builtin_binds.hpp"
|
2023-06-16 07:38:01 +00:00
|
|
|
with builtin_binds_filename.open("w+", encoding="utf-8") as builtin_binds_file:
|
2022-10-06 08:32:48 +00:00
|
|
|
builtin_binds = []
|
|
|
|
add_header("builtin_binds.hpp", builtin_binds)
|
|
|
|
|
|
|
|
builtin_binds.append("#ifndef GODOT_CPP_BUILTIN_BINDS_HPP")
|
|
|
|
builtin_binds.append("#define GODOT_CPP_BUILTIN_BINDS_HPP")
|
|
|
|
builtin_binds.append("")
|
|
|
|
builtin_binds.append("#include <godot_cpp/variant/builtin_types.hpp>")
|
|
|
|
builtin_binds.append("")
|
|
|
|
|
|
|
|
for builtin_api in api["builtin_classes"]:
|
|
|
|
if is_included_type(builtin_api["name"]):
|
|
|
|
if "enums" in builtin_api:
|
|
|
|
for enum_api in builtin_api["enums"]:
|
2022-12-08 05:34:19 +00:00
|
|
|
builtin_binds.append(f"VARIANT_ENUM_CAST({builtin_api['name']}::{enum_api['name']});")
|
2022-10-06 08:32:48 +00:00
|
|
|
|
|
|
|
builtin_binds.append("")
|
|
|
|
builtin_binds.append("#endif // ! GODOT_CPP_BUILTIN_BINDS_HPP")
|
|
|
|
|
|
|
|
builtin_binds_file.write("\n".join(builtin_binds))
|
|
|
|
|
2023-04-14 07:33:49 +00:00
|
|
|
# Create a header to implement all builtin class vararg methods and be included in "variant.hpp".
|
|
|
|
builtin_vararg_methods_header = include_gen_folder / "builtin_vararg_methods.hpp"
|
|
|
|
builtin_vararg_methods_header.open("w+").write(
|
|
|
|
generate_builtin_class_vararg_method_implements_header(api["builtin_classes"])
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def generate_builtin_class_vararg_method_implements_header(builtin_classes):
|
|
|
|
result = []
|
|
|
|
|
|
|
|
add_header("builtin_vararg_methods.hpp", result)
|
|
|
|
|
|
|
|
header_guard = "GODOT_CPP_BUILTIN_VARARG_METHODS_HPP"
|
|
|
|
result.append(f"#ifndef {header_guard}")
|
|
|
|
result.append(f"#define {header_guard}")
|
|
|
|
result.append("")
|
|
|
|
for builtin_api in builtin_classes:
|
|
|
|
if not "methods" in builtin_api:
|
|
|
|
continue
|
|
|
|
class_name = builtin_api["name"]
|
|
|
|
for method in builtin_api["methods"]:
|
|
|
|
if not method["is_vararg"]:
|
|
|
|
continue
|
|
|
|
|
|
|
|
result += make_varargs_template(
|
|
|
|
method, "is_static" in method and method["is_static"], class_name, False, False, True
|
|
|
|
)
|
|
|
|
result.append("")
|
|
|
|
|
|
|
|
result.append("")
|
|
|
|
result.append(f"#endif // ! {header_guard}")
|
|
|
|
|
|
|
|
return "\n".join(result)
|
|
|
|
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_classes):
|
|
|
|
result = []
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
class_name = builtin_api["name"]
|
|
|
|
snake_class_name = camel_to_snake(class_name).upper()
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
header_guard = f"GODOT_CPP_{snake_class_name}_HPP"
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
add_header(f"{snake_class_name.lower()}.hpp", result)
|
2018-02-11 14:50:01 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
result.append(f"#ifndef {header_guard}")
|
|
|
|
result.append(f"#define {header_guard}")
|
2018-02-11 14:50:01 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
result.append("")
|
|
|
|
result.append("#include <godot_cpp/core/defs.hpp>")
|
|
|
|
result.append("")
|
2018-02-11 14:50:01 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
# Special cases.
|
|
|
|
if class_name == "String":
|
|
|
|
result.append("#include <godot_cpp/variant/char_string.hpp>")
|
2022-02-15 10:58:28 +00:00
|
|
|
result.append("#include <godot_cpp/variant/char_utils.hpp>")
|
2023-07-07 13:26:55 +00:00
|
|
|
result.append("#include <godot_cpp/classes/global_constants.hpp>")
|
2018-02-11 14:50:01 +00:00
|
|
|
|
2022-11-28 12:47:55 +00:00
|
|
|
if class_name == "PackedStringArray":
|
|
|
|
result.append("#include <godot_cpp/variant/string.hpp>")
|
|
|
|
if class_name == "PackedColorArray":
|
|
|
|
result.append("#include <godot_cpp/variant/color.hpp>")
|
|
|
|
if class_name == "PackedVector2Array":
|
|
|
|
result.append("#include <godot_cpp/variant/vector2.hpp>")
|
|
|
|
if class_name == "PackedVector3Array":
|
|
|
|
result.append("#include <godot_cpp/variant/vector3.hpp>")
|
|
|
|
|
2024-03-03 10:47:57 +00:00
|
|
|
if is_packed_array(class_name):
|
|
|
|
result.append("#include <godot_cpp/core/error_macros.hpp>")
|
|
|
|
result.append("#include <initializer_list>")
|
|
|
|
|
2021-11-01 09:08:47 +00:00
|
|
|
if class_name == "Array":
|
|
|
|
result.append("#include <godot_cpp/variant/array_helpers.hpp>")
|
|
|
|
|
2023-10-21 23:32:45 +00:00
|
|
|
if class_name == "Callable":
|
|
|
|
result.append("#include <godot_cpp/variant/callable_custom.hpp>")
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
for include in fully_used_classes:
|
2022-09-15 07:33:07 +00:00
|
|
|
if include == "TypedArray":
|
|
|
|
result.append("#include <godot_cpp/variant/typed_array.hpp>")
|
|
|
|
else:
|
|
|
|
result.append(f"#include <godot_cpp/{get_include_path(include)}>")
|
2019-04-07 22:45:35 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
if len(fully_used_classes) > 0:
|
|
|
|
result.append("")
|
2019-04-07 22:45:35 +00:00
|
|
|
|
2022-12-26 18:44:56 +00:00
|
|
|
result.append("#include <gdextension_interface.h>")
|
2021-08-18 14:03:52 +00:00
|
|
|
result.append("")
|
|
|
|
result.append("namespace godot {")
|
|
|
|
result.append("")
|
2019-10-21 16:08:03 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
for type_name in used_classes:
|
2022-10-05 06:03:52 +00:00
|
|
|
if is_struct_type(type_name):
|
2022-02-16 09:29:01 +00:00
|
|
|
result.append(f"struct {type_name};")
|
|
|
|
else:
|
|
|
|
result.append(f"class {type_name};")
|
2017-09-11 19:34:20 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
if len(used_classes) > 0:
|
|
|
|
result.append("")
|
2018-02-11 14:50:01 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
result.append(f"class {class_name} {{")
|
|
|
|
result.append(f"\tstatic constexpr size_t {snake_class_name}_SIZE = {size};")
|
2021-09-19 17:54:34 +00:00
|
|
|
result.append(f"\tuint8_t opaque[{snake_class_name}_SIZE] = {{}};")
|
2018-02-11 14:50:01 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
result.append("")
|
|
|
|
result.append("\tfriend class Variant;")
|
2022-10-23 20:31:57 +00:00
|
|
|
if class_name == "String":
|
|
|
|
result.append("\tfriend class StringName;")
|
2018-02-11 14:50:01 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
result.append("")
|
|
|
|
result.append("\tstatic struct _MethodBindings {")
|
2017-09-11 19:34:20 +00:00
|
|
|
|
2024-02-01 20:22:26 +00:00
|
|
|
result.append("\t\tGDExtensionTypeFromVariantConstructorFunc from_variant_constructor;")
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
if "constructors" in builtin_api:
|
|
|
|
for constructor in builtin_api["constructors"]:
|
2022-12-07 13:11:44 +00:00
|
|
|
result.append(f'\t\tGDExtensionPtrConstructor constructor_{constructor["index"]};')
|
2018-01-14 22:03:57 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
if builtin_api["has_destructor"]:
|
2022-12-07 13:11:44 +00:00
|
|
|
result.append("\t\tGDExtensionPtrDestructor destructor;")
|
2017-09-11 19:34:20 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
if "methods" in builtin_api:
|
|
|
|
for method in builtin_api["methods"]:
|
2022-12-07 13:11:44 +00:00
|
|
|
result.append(f'\t\tGDExtensionPtrBuiltInMethod method_{method["name"]};')
|
2017-09-11 19:34:20 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
if "members" in builtin_api:
|
|
|
|
for member in builtin_api["members"]:
|
2022-12-07 13:11:44 +00:00
|
|
|
result.append(f'\t\tGDExtensionPtrSetter member_{member["name"]}_setter;')
|
|
|
|
result.append(f'\t\tGDExtensionPtrGetter member_{member["name"]}_getter;')
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
if "indexing_return_type" in builtin_api:
|
2022-12-26 18:44:56 +00:00
|
|
|
result.append("\t\tGDExtensionPtrIndexedSetter indexed_setter;")
|
|
|
|
result.append("\t\tGDExtensionPtrIndexedGetter indexed_getter;")
|
2017-06-19 00:03:59 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
if "is_keyed" in builtin_api and builtin_api["is_keyed"]:
|
2022-12-26 18:44:56 +00:00
|
|
|
result.append("\t\tGDExtensionPtrKeyedSetter keyed_setter;")
|
|
|
|
result.append("\t\tGDExtensionPtrKeyedGetter keyed_getter;")
|
|
|
|
result.append("\t\tGDExtensionPtrKeyedChecker keyed_checker;")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
if "operators" in builtin_api:
|
|
|
|
for operator in builtin_api["operators"]:
|
|
|
|
if "right_type" in operator:
|
|
|
|
result.append(
|
2022-12-07 13:11:44 +00:00
|
|
|
f'\t\tGDExtensionPtrOperatorEvaluator operator_{get_operator_id_name(operator["name"])}_{operator["right_type"]};'
|
2021-08-18 14:03:52 +00:00
|
|
|
)
|
|
|
|
else:
|
2022-12-07 13:11:44 +00:00
|
|
|
result.append(f'\t\tGDExtensionPtrOperatorEvaluator operator_{get_operator_id_name(operator["name"])};')
|
2021-08-18 14:03:52 +00:00
|
|
|
|
|
|
|
result.append("\t} _method_bindings;")
|
|
|
|
|
|
|
|
result.append("")
|
|
|
|
result.append("\tstatic void init_bindings();")
|
2022-10-23 20:31:57 +00:00
|
|
|
result.append("\tstatic void _init_bindings_constructors_destructor();")
|
2021-08-18 14:03:52 +00:00
|
|
|
|
2024-02-01 20:22:26 +00:00
|
|
|
result.append("")
|
|
|
|
result.append(f"\t{class_name}(const Variant *p_variant);")
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
result.append("")
|
|
|
|
result.append("public:")
|
|
|
|
|
2022-10-22 17:42:48 +00:00
|
|
|
result.append(
|
2022-12-07 13:11:44 +00:00
|
|
|
f"\t_FORCE_INLINE_ GDExtensionTypePtr _native_ptr() const {{ return const_cast<uint8_t (*)[{snake_class_name}_SIZE]>(&opaque); }}"
|
2022-10-22 17:42:48 +00:00
|
|
|
)
|
|
|
|
|
2021-08-26 20:26:33 +00:00
|
|
|
copy_constructor_index = -1
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
if "constructors" in builtin_api:
|
|
|
|
for constructor in builtin_api["constructors"]:
|
|
|
|
method_signature = f"\t{class_name}("
|
|
|
|
if "arguments" in constructor:
|
|
|
|
method_signature += make_function_parameters(
|
|
|
|
constructor["arguments"], include_default=True, for_builtin=True
|
|
|
|
)
|
2021-08-26 20:26:33 +00:00
|
|
|
if len(constructor["arguments"]) == 1 and constructor["arguments"][0]["type"] == class_name:
|
|
|
|
copy_constructor_index = constructor["index"]
|
2021-08-18 14:03:52 +00:00
|
|
|
method_signature += ");"
|
|
|
|
|
|
|
|
result.append(method_signature)
|
|
|
|
|
2021-08-26 20:26:33 +00:00
|
|
|
# Move constructor.
|
2024-06-14 10:49:41 +00:00
|
|
|
result.append(f"\t{class_name}({class_name} &&p_other);")
|
2021-08-26 20:26:33 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
# Special cases.
|
|
|
|
if class_name == "String" or class_name == "StringName" or class_name == "NodePath":
|
2023-10-09 21:59:29 +00:00
|
|
|
if class_name == "StringName":
|
2024-06-14 10:49:41 +00:00
|
|
|
result.append(f"\t{class_name}(const char *p_from, bool p_static = false);")
|
2023-10-09 21:59:29 +00:00
|
|
|
else:
|
2024-06-14 10:49:41 +00:00
|
|
|
result.append(f"\t{class_name}(const char *p_from);")
|
|
|
|
result.append(f"\t{class_name}(const wchar_t *p_from);")
|
|
|
|
result.append(f"\t{class_name}(const char16_t *p_from);")
|
|
|
|
result.append(f"\t{class_name}(const char32_t *p_from);")
|
2023-10-21 23:32:45 +00:00
|
|
|
if class_name == "Callable":
|
|
|
|
result.append("\tCallable(CallableCustom *p_custom);")
|
|
|
|
result.append("\tCallableCustom *get_custom() const;")
|
2021-08-18 14:03:52 +00:00
|
|
|
|
|
|
|
if "constants" in builtin_api:
|
|
|
|
axis_constants_count = 0
|
|
|
|
for constant in builtin_api["constants"]:
|
|
|
|
# Special case: Vector3.Axis is the only enum in the bindings.
|
|
|
|
# It's technically not supported by Variant but works for direct access.
|
|
|
|
if class_name == "Vector3" and constant["name"].startswith("AXIS"):
|
|
|
|
if axis_constants_count == 0:
|
|
|
|
result.append("\tenum Axis {")
|
|
|
|
result.append(f'\t\t{constant["name"]} = {constant["value"]},')
|
|
|
|
axis_constants_count += 1
|
|
|
|
if axis_constants_count == 3:
|
|
|
|
result.append("\t};")
|
|
|
|
else:
|
|
|
|
result.append(f'\tstatic const {correct_type(constant["type"])} {constant["name"]};')
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
if builtin_api["has_destructor"]:
|
|
|
|
result.append(f"\t~{class_name}();")
|
2018-02-11 14:50:01 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
method_list = []
|
2018-02-11 14:50:01 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
if "methods" in builtin_api:
|
|
|
|
for method in builtin_api["methods"]:
|
|
|
|
method_list.append(method["name"])
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
vararg = method["is_vararg"]
|
|
|
|
if vararg:
|
2024-03-27 12:54:05 +00:00
|
|
|
result.append("\ttemplate <typename... Args>")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
method_signature = "\t"
|
|
|
|
if "is_static" in method and method["is_static"]:
|
|
|
|
method_signature += "static "
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
if "return_type" in method:
|
2024-03-27 12:54:05 +00:00
|
|
|
method_signature += f'{correct_type(method["return_type"])}'
|
|
|
|
if not method_signature.endswith("*"):
|
|
|
|
method_signature += " "
|
2021-08-18 14:03:52 +00:00
|
|
|
else:
|
|
|
|
method_signature += "void "
|
|
|
|
|
|
|
|
method_signature += f'{method["name"]}('
|
|
|
|
|
|
|
|
method_arguments = []
|
|
|
|
if "arguments" in method:
|
|
|
|
method_arguments = method["arguments"]
|
|
|
|
|
|
|
|
method_signature += make_function_parameters(
|
|
|
|
method_arguments, include_default=True, for_builtin=True, is_vararg=vararg
|
|
|
|
)
|
|
|
|
|
|
|
|
method_signature += ")"
|
|
|
|
if method["is_const"]:
|
|
|
|
method_signature += " const"
|
|
|
|
method_signature += ";"
|
|
|
|
|
|
|
|
result.append(method_signature)
|
|
|
|
|
|
|
|
# Special cases.
|
|
|
|
if class_name == "String":
|
2024-06-14 10:49:41 +00:00
|
|
|
result.append("\tstatic String utf8(const char *p_from, int64_t p_len = -1);")
|
|
|
|
result.append("\tvoid parse_utf8(const char *p_from, int64_t p_len = -1);")
|
|
|
|
result.append("\tstatic String utf16(const char16_t *p_from, int64_t p_len = -1);")
|
|
|
|
result.append("\tvoid parse_utf16(const char16_t *p_from, int64_t p_len = -1);")
|
2021-08-18 14:03:52 +00:00
|
|
|
result.append("\tCharString utf8() const;")
|
|
|
|
result.append("\tCharString ascii() const;")
|
|
|
|
result.append("\tChar16String utf16() const;")
|
|
|
|
result.append("\tChar32String utf32() const;")
|
|
|
|
result.append("\tCharWideString wide_string() const;")
|
2022-07-20 20:49:08 +00:00
|
|
|
result.append("\tstatic String num_real(double p_num, bool p_trailing = true);")
|
2024-01-10 13:30:24 +00:00
|
|
|
result.append("\tError resize(int64_t p_size);")
|
2021-08-18 14:03:52 +00:00
|
|
|
|
|
|
|
if "members" in builtin_api:
|
|
|
|
for member in builtin_api["members"]:
|
|
|
|
if f'get_{member["name"]}' not in method_list:
|
|
|
|
result.append(f'\t{correct_type(member["type"])} get_{member["name"]}() const;')
|
|
|
|
if f'set_{member["name"]}' not in method_list:
|
|
|
|
result.append(f'\tvoid set_{member["name"]}({type_for_parameter(member["type"])}value);')
|
|
|
|
|
|
|
|
if "operators" in builtin_api:
|
|
|
|
for operator in builtin_api["operators"]:
|
|
|
|
if operator["name"] not in ["in", "xor"]:
|
|
|
|
if "right_type" in operator:
|
|
|
|
result.append(
|
2024-06-14 10:49:41 +00:00
|
|
|
f'\t{correct_type(operator["return_type"])} operator{operator["name"]}({type_for_parameter(operator["right_type"])}p_other) const;'
|
2021-08-18 14:03:52 +00:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
result.append(
|
|
|
|
f'\t{correct_type(operator["return_type"])} operator{operator["name"].replace("unary", "")}() const;'
|
|
|
|
)
|
|
|
|
|
2021-08-26 20:26:33 +00:00
|
|
|
# Copy assignment.
|
|
|
|
if copy_constructor_index >= 0:
|
2024-06-14 10:49:41 +00:00
|
|
|
result.append(f"\t{class_name} &operator=(const {class_name} &p_other);")
|
2021-08-26 20:26:33 +00:00
|
|
|
|
|
|
|
# Move assignment.
|
2024-06-14 10:49:41 +00:00
|
|
|
result.append(f"\t{class_name} &operator=({class_name} &&p_other);")
|
2021-08-26 20:26:33 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
# Special cases.
|
|
|
|
if class_name == "String":
|
2022-11-20 08:44:54 +00:00
|
|
|
result.append("\tString &operator=(const char *p_str);")
|
|
|
|
result.append("\tString &operator=(const wchar_t *p_str);")
|
|
|
|
result.append("\tString &operator=(const char16_t *p_str);")
|
|
|
|
result.append("\tString &operator=(const char32_t *p_str);")
|
|
|
|
result.append("\tbool operator==(const char *p_str) const;")
|
|
|
|
result.append("\tbool operator==(const wchar_t *p_str) const;")
|
|
|
|
result.append("\tbool operator==(const char16_t *p_str) const;")
|
|
|
|
result.append("\tbool operator==(const char32_t *p_str) const;")
|
|
|
|
result.append("\tbool operator!=(const char *p_str) const;")
|
|
|
|
result.append("\tbool operator!=(const wchar_t *p_str) const;")
|
|
|
|
result.append("\tbool operator!=(const char16_t *p_str) const;")
|
|
|
|
result.append("\tbool operator!=(const char32_t *p_str) const;")
|
2022-11-28 12:47:55 +00:00
|
|
|
result.append("\tString operator+(const char *p_str);")
|
|
|
|
result.append("\tString operator+(const wchar_t *p_str);")
|
|
|
|
result.append("\tString operator+(const char16_t *p_str);")
|
|
|
|
result.append("\tString operator+(const char32_t *p_str);")
|
|
|
|
result.append("\tString operator+(char32_t p_char);")
|
|
|
|
result.append("\tString &operator+=(const String &p_str);")
|
|
|
|
result.append("\tString &operator+=(char32_t p_char);")
|
|
|
|
result.append("\tString &operator+=(const char *p_str);")
|
|
|
|
result.append("\tString &operator+=(const wchar_t *p_str);")
|
|
|
|
result.append("\tString &operator+=(const char32_t *p_str);")
|
|
|
|
|
2024-01-10 13:30:24 +00:00
|
|
|
result.append("\tconst char32_t &operator[](int64_t p_index) const;")
|
|
|
|
result.append("\tchar32_t &operator[](int64_t p_index);")
|
2022-11-20 08:44:54 +00:00
|
|
|
result.append("\tconst char32_t *ptr() const;")
|
|
|
|
result.append("\tchar32_t *ptrw();")
|
2021-09-01 03:11:10 +00:00
|
|
|
|
2021-11-01 09:08:47 +00:00
|
|
|
if class_name == "Array":
|
2024-03-10 21:02:43 +00:00
|
|
|
result.append("\ttemplate <typename... Args>")
|
2024-06-14 10:49:41 +00:00
|
|
|
result.append("\tstatic Array make(Args... p_args) {")
|
|
|
|
result.append("\t\treturn helpers::append_all(Array(), p_args...);")
|
2021-11-01 09:08:47 +00:00
|
|
|
result.append("\t}")
|
|
|
|
|
2021-09-01 03:11:10 +00:00
|
|
|
if is_packed_array(class_name):
|
|
|
|
return_type = correct_type(builtin_api["indexing_return_type"])
|
|
|
|
if class_name == "PackedByteArray":
|
2021-09-08 18:11:12 +00:00
|
|
|
return_type = "uint8_t"
|
2021-09-01 03:11:10 +00:00
|
|
|
elif class_name == "PackedInt32Array":
|
2021-09-08 18:11:12 +00:00
|
|
|
return_type = "int32_t"
|
2021-09-01 03:11:10 +00:00
|
|
|
elif class_name == "PackedFloat32Array":
|
2021-09-08 18:11:12 +00:00
|
|
|
return_type = "float"
|
2024-01-10 13:30:24 +00:00
|
|
|
result.append(f"\tconst {return_type} &operator[](int64_t p_index) const;")
|
|
|
|
result.append(f"\t{return_type} &operator[](int64_t p_index);")
|
2022-12-26 18:44:56 +00:00
|
|
|
result.append(f"\tconst {return_type} *ptr() const;")
|
|
|
|
result.append(f"\t{return_type} *ptrw();")
|
2022-11-28 12:47:55 +00:00
|
|
|
iterators = """
|
|
|
|
struct Iterator {
|
|
|
|
_FORCE_INLINE_ $TYPE &operator*() const {
|
|
|
|
return *elem_ptr;
|
|
|
|
}
|
|
|
|
_FORCE_INLINE_ $TYPE *operator->() const { return elem_ptr; }
|
|
|
|
_FORCE_INLINE_ Iterator &operator++() {
|
|
|
|
elem_ptr++;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
_FORCE_INLINE_ Iterator &operator--() {
|
|
|
|
elem_ptr--;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ bool operator==(const Iterator &b) const { return elem_ptr == b.elem_ptr; }
|
|
|
|
_FORCE_INLINE_ bool operator!=(const Iterator &b) const { return elem_ptr != b.elem_ptr; }
|
|
|
|
|
|
|
|
Iterator($TYPE *p_ptr) { elem_ptr = p_ptr; }
|
|
|
|
Iterator() {}
|
|
|
|
Iterator(const Iterator &p_it) { elem_ptr = p_it.elem_ptr; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
$TYPE *elem_ptr = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ConstIterator {
|
|
|
|
_FORCE_INLINE_ const $TYPE &operator*() const {
|
|
|
|
return *elem_ptr;
|
|
|
|
}
|
|
|
|
_FORCE_INLINE_ const $TYPE *operator->() const { return elem_ptr; }
|
|
|
|
_FORCE_INLINE_ ConstIterator &operator++() {
|
|
|
|
elem_ptr++;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
_FORCE_INLINE_ ConstIterator &operator--() {
|
|
|
|
elem_ptr--;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ bool operator==(const ConstIterator &b) const { return elem_ptr == b.elem_ptr; }
|
|
|
|
_FORCE_INLINE_ bool operator!=(const ConstIterator &b) const { return elem_ptr != b.elem_ptr; }
|
|
|
|
|
|
|
|
ConstIterator(const $TYPE *p_ptr) { elem_ptr = p_ptr; }
|
|
|
|
ConstIterator() {}
|
|
|
|
ConstIterator(const ConstIterator &p_it) { elem_ptr = p_it.elem_ptr; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
const $TYPE *elem_ptr = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
_FORCE_INLINE_ Iterator begin() {
|
|
|
|
return Iterator(ptrw());
|
|
|
|
}
|
|
|
|
_FORCE_INLINE_ Iterator end() {
|
|
|
|
return Iterator(ptrw() + size());
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ ConstIterator begin() const {
|
|
|
|
return ConstIterator(ptr());
|
|
|
|
}
|
|
|
|
_FORCE_INLINE_ ConstIterator end() const {
|
|
|
|
return ConstIterator(ptr() + size());
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
result.append(iterators.replace("$TYPE", return_type))
|
2024-03-03 10:47:57 +00:00
|
|
|
init_list = """
|
|
|
|
_FORCE_INLINE_ $CLASS(std::initializer_list<$TYPE> p_init) {
|
|
|
|
ERR_FAIL_COND(resize(p_init.size()) != 0);
|
|
|
|
|
|
|
|
size_t i = 0;
|
|
|
|
for (const $TYPE &element : p_init) {
|
|
|
|
set(i++, element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
result.append(init_list.replace("$TYPE", return_type).replace("$CLASS", class_name))
|
2021-09-16 07:21:40 +00:00
|
|
|
|
|
|
|
if class_name == "Array":
|
2024-01-10 13:30:24 +00:00
|
|
|
result.append("\tconst Variant &operator[](int64_t p_index) const;")
|
|
|
|
result.append("\tVariant &operator[](int64_t p_index);")
|
2022-12-26 18:44:56 +00:00
|
|
|
result.append("\tvoid set_typed(uint32_t p_type, const StringName &p_class_name, const Variant &p_script);")
|
2023-01-31 09:27:47 +00:00
|
|
|
result.append("\tvoid _ref(const Array &p_from) const;")
|
2021-08-18 14:03:52 +00:00
|
|
|
|
2021-11-18 03:05:14 +00:00
|
|
|
if class_name == "Dictionary":
|
2022-12-26 18:44:56 +00:00
|
|
|
result.append("\tconst Variant &operator[](const Variant &p_key) const;")
|
|
|
|
result.append("\tVariant &operator[](const Variant &p_key);")
|
2021-11-18 03:05:14 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
result.append("};")
|
|
|
|
|
|
|
|
if class_name == "String":
|
|
|
|
result.append("")
|
|
|
|
result.append("bool operator==(const char *p_chr, const String &p_str);")
|
|
|
|
result.append("bool operator==(const wchar_t *p_chr, const String &p_str);")
|
|
|
|
result.append("bool operator==(const char16_t *p_chr, const String &p_str);")
|
|
|
|
result.append("bool operator==(const char32_t *p_chr, const String &p_str);")
|
|
|
|
result.append("bool operator!=(const char *p_chr, const String &p_str);")
|
|
|
|
result.append("bool operator!=(const wchar_t *p_chr, const String &p_str);")
|
|
|
|
result.append("bool operator!=(const char16_t *p_chr, const String &p_str);")
|
|
|
|
result.append("bool operator!=(const char32_t *p_chr, const String &p_str);")
|
|
|
|
result.append("String operator+(const char *p_chr, const String &p_str);")
|
|
|
|
result.append("String operator+(const wchar_t *p_chr, const String &p_str);")
|
|
|
|
result.append("String operator+(const char16_t *p_chr, const String &p_str);")
|
|
|
|
result.append("String operator+(const char32_t *p_chr, const String &p_str);")
|
2022-11-28 12:47:55 +00:00
|
|
|
result.append("String operator+(char32_t p_char, const String &p_str);")
|
2021-09-08 18:07:23 +00:00
|
|
|
|
2022-07-20 20:49:08 +00:00
|
|
|
result.append("String itos(int64_t p_val);")
|
|
|
|
result.append("String uitos(uint64_t p_val);")
|
|
|
|
result.append("String rtos(double p_val);")
|
|
|
|
result.append("String rtoss(double p_val);")
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
result.append("")
|
|
|
|
result.append("} // namespace godot")
|
|
|
|
|
|
|
|
result.append(f"#endif // ! {header_guard}")
|
|
|
|
|
|
|
|
return "\n".join(result)
|
|
|
|
|
|
|
|
|
|
|
|
def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_classes):
|
|
|
|
result = []
|
|
|
|
|
|
|
|
class_name = builtin_api["name"]
|
|
|
|
snake_class_name = camel_to_snake(class_name)
|
2022-12-07 13:11:44 +00:00
|
|
|
enum_type_name = f"GDEXTENSION_VARIANT_TYPE_{snake_class_name.upper()}"
|
2021-08-18 14:03:52 +00:00
|
|
|
|
|
|
|
add_header(f"{snake_class_name}.cpp", result)
|
|
|
|
|
|
|
|
result.append("")
|
|
|
|
result.append(f"#include <godot_cpp/variant/{snake_class_name}.hpp>")
|
|
|
|
result.append("")
|
|
|
|
result.append("#include <godot_cpp/core/binder_common.hpp>")
|
|
|
|
result.append("")
|
|
|
|
result.append("#include <godot_cpp/godot.hpp>")
|
|
|
|
result.append("")
|
|
|
|
|
|
|
|
# Only used since the "fully used" is included in header already.
|
|
|
|
for include in used_classes:
|
|
|
|
result.append(f"#include <godot_cpp/{get_include_path(include)}>")
|
|
|
|
|
|
|
|
if len(used_classes) > 0:
|
|
|
|
result.append("")
|
|
|
|
|
|
|
|
result.append("#include <godot_cpp/core/builtin_ptrcall.hpp>")
|
|
|
|
result.append("")
|
2021-08-26 20:26:33 +00:00
|
|
|
result.append("#include <utility>")
|
|
|
|
result.append("")
|
2021-08-18 14:03:52 +00:00
|
|
|
result.append("namespace godot {")
|
|
|
|
result.append("")
|
|
|
|
|
|
|
|
result.append(f"{class_name}::_MethodBindings {class_name}::_method_bindings;")
|
|
|
|
result.append("")
|
|
|
|
|
2022-10-23 20:31:57 +00:00
|
|
|
result.append(f"void {class_name}::_init_bindings_constructors_destructor() {{")
|
2021-08-18 14:03:52 +00:00
|
|
|
|
2024-02-01 20:22:26 +00:00
|
|
|
result.append(
|
|
|
|
f"\t_method_bindings.from_variant_constructor = internal::gdextension_interface_get_variant_to_type_constructor({enum_type_name});"
|
|
|
|
)
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
if "constructors" in builtin_api:
|
|
|
|
for constructor in builtin_api["constructors"]:
|
|
|
|
result.append(
|
2023-04-24 16:45:45 +00:00
|
|
|
f'\t_method_bindings.constructor_{constructor["index"]} = internal::gdextension_interface_variant_get_ptr_constructor({enum_type_name}, {constructor["index"]});'
|
2021-08-18 14:03:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if builtin_api["has_destructor"]:
|
|
|
|
result.append(
|
2023-04-24 16:45:45 +00:00
|
|
|
f"\t_method_bindings.destructor = internal::gdextension_interface_variant_get_ptr_destructor({enum_type_name});"
|
2021-08-18 14:03:52 +00:00
|
|
|
)
|
|
|
|
|
2022-10-23 20:31:57 +00:00
|
|
|
result.append("}")
|
|
|
|
|
|
|
|
result.append(f"void {class_name}::init_bindings() {{")
|
|
|
|
|
|
|
|
# StringName's constructor internally uses String, so it constructor must be ready !
|
|
|
|
if class_name == "StringName":
|
2022-12-26 18:44:56 +00:00
|
|
|
result.append("\tString::_init_bindings_constructors_destructor();")
|
2022-10-23 20:31:57 +00:00
|
|
|
result.append(f"\t{class_name}::_init_bindings_constructors_destructor();")
|
|
|
|
|
2023-02-16 15:38:38 +00:00
|
|
|
result.append("\tStringName _gde_name;")
|
2022-10-22 11:46:59 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
if "methods" in builtin_api:
|
|
|
|
for method in builtin_api["methods"]:
|
|
|
|
# TODO: Add error check for hash mismatch.
|
2023-02-16 15:38:38 +00:00
|
|
|
result.append(f'\t_gde_name = StringName("{method["name"]}");')
|
2021-08-18 14:03:52 +00:00
|
|
|
result.append(
|
2023-02-16 15:38:38 +00:00
|
|
|
f'\t_method_bindings.method_{method["name"]} = internal::gdextension_interface_variant_get_ptr_builtin_method({enum_type_name}, _gde_name._native_ptr(), {method["hash"]});'
|
2021-08-18 14:03:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if "members" in builtin_api:
|
|
|
|
for member in builtin_api["members"]:
|
2023-02-16 15:38:38 +00:00
|
|
|
result.append(f'\t_gde_name = StringName("{member["name"]}");')
|
2021-08-18 14:03:52 +00:00
|
|
|
result.append(
|
2023-02-16 15:38:38 +00:00
|
|
|
f'\t_method_bindings.member_{member["name"]}_setter = internal::gdextension_interface_variant_get_ptr_setter({enum_type_name}, _gde_name._native_ptr());'
|
2021-08-18 14:03:52 +00:00
|
|
|
)
|
|
|
|
result.append(
|
2023-02-16 15:38:38 +00:00
|
|
|
f'\t_method_bindings.member_{member["name"]}_getter = internal::gdextension_interface_variant_get_ptr_getter({enum_type_name}, _gde_name._native_ptr());'
|
2021-08-18 14:03:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if "indexing_return_type" in builtin_api:
|
|
|
|
result.append(
|
2023-04-24 16:45:45 +00:00
|
|
|
f"\t_method_bindings.indexed_setter = internal::gdextension_interface_variant_get_ptr_indexed_setter({enum_type_name});"
|
2021-08-18 14:03:52 +00:00
|
|
|
)
|
|
|
|
result.append(
|
2023-04-24 16:45:45 +00:00
|
|
|
f"\t_method_bindings.indexed_getter = internal::gdextension_interface_variant_get_ptr_indexed_getter({enum_type_name});"
|
2021-08-18 14:03:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if "is_keyed" in builtin_api and builtin_api["is_keyed"]:
|
|
|
|
result.append(
|
2023-04-24 16:45:45 +00:00
|
|
|
f"\t_method_bindings.keyed_setter = internal::gdextension_interface_variant_get_ptr_keyed_setter({enum_type_name});"
|
2021-08-18 14:03:52 +00:00
|
|
|
)
|
|
|
|
result.append(
|
2023-04-24 16:45:45 +00:00
|
|
|
f"\t_method_bindings.keyed_getter = internal::gdextension_interface_variant_get_ptr_keyed_getter({enum_type_name});"
|
2021-08-18 14:03:52 +00:00
|
|
|
)
|
|
|
|
result.append(
|
2023-04-24 16:45:45 +00:00
|
|
|
f"\t_method_bindings.keyed_checker = internal::gdextension_interface_variant_get_ptr_keyed_checker({enum_type_name});"
|
2021-08-18 14:03:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if "operators" in builtin_api:
|
|
|
|
for operator in builtin_api["operators"]:
|
|
|
|
if "right_type" in operator:
|
2022-10-03 12:28:33 +00:00
|
|
|
if operator["right_type"] == "Variant":
|
2022-12-07 13:11:44 +00:00
|
|
|
right_type_variant_type = "GDEXTENSION_VARIANT_TYPE_NIL"
|
2022-10-03 12:28:33 +00:00
|
|
|
else:
|
2022-12-07 13:11:44 +00:00
|
|
|
right_type_variant_type = (
|
|
|
|
f"GDEXTENSION_VARIANT_TYPE_{camel_to_snake(operator['right_type']).upper()}"
|
|
|
|
)
|
2021-08-18 14:03:52 +00:00
|
|
|
result.append(
|
2023-04-24 16:45:45 +00:00
|
|
|
f'\t_method_bindings.operator_{get_operator_id_name(operator["name"])}_{operator["right_type"]} = internal::gdextension_interface_variant_get_ptr_operator_evaluator(GDEXTENSION_VARIANT_OP_{get_operator_id_name(operator["name"]).upper()}, {enum_type_name}, {right_type_variant_type});'
|
2021-08-18 14:03:52 +00:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
result.append(
|
2023-04-24 16:45:45 +00:00
|
|
|
f'\t_method_bindings.operator_{get_operator_id_name(operator["name"])} = internal::gdextension_interface_variant_get_ptr_operator_evaluator(GDEXTENSION_VARIANT_OP_{get_operator_id_name(operator["name"]).upper()}, {enum_type_name}, GDEXTENSION_VARIANT_TYPE_NIL);'
|
2021-08-18 14:03:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
result.append("}")
|
|
|
|
result.append("")
|
|
|
|
|
2021-08-26 20:26:33 +00:00
|
|
|
copy_constructor_index = -1
|
|
|
|
|
2024-02-01 20:22:26 +00:00
|
|
|
result.append(f"{class_name}::{class_name}(const Variant *p_variant) {{")
|
|
|
|
result.append("\t_method_bindings.from_variant_constructor(&opaque, p_variant->_native_ptr());")
|
|
|
|
result.append("}")
|
|
|
|
result.append("")
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
if "constructors" in builtin_api:
|
|
|
|
for constructor in builtin_api["constructors"]:
|
|
|
|
method_signature = f"{class_name}::{class_name}("
|
|
|
|
if "arguments" in constructor:
|
|
|
|
method_signature += make_function_parameters(
|
|
|
|
constructor["arguments"], include_default=False, for_builtin=True
|
|
|
|
)
|
|
|
|
method_signature += ") {"
|
|
|
|
|
|
|
|
result.append(method_signature)
|
|
|
|
|
|
|
|
method_call = (
|
|
|
|
f'\tinternal::_call_builtin_constructor(_method_bindings.constructor_{constructor["index"]}, &opaque'
|
|
|
|
)
|
|
|
|
if "arguments" in constructor:
|
2021-08-26 20:26:33 +00:00
|
|
|
if len(constructor["arguments"]) == 1 and constructor["arguments"][0]["type"] == class_name:
|
|
|
|
copy_constructor_index = constructor["index"]
|
2021-08-18 14:03:52 +00:00
|
|
|
method_call += ", "
|
|
|
|
arguments = []
|
|
|
|
for argument in constructor["arguments"]:
|
|
|
|
(encode, arg_name) = get_encoded_arg(
|
|
|
|
argument["name"],
|
|
|
|
argument["type"],
|
|
|
|
argument["meta"] if "meta" in argument else None,
|
|
|
|
)
|
|
|
|
result += encode
|
|
|
|
arguments.append(arg_name)
|
|
|
|
method_call += ", ".join(arguments)
|
|
|
|
method_call += ");"
|
|
|
|
|
|
|
|
result.append(method_call)
|
|
|
|
result.append("}")
|
|
|
|
result.append("")
|
|
|
|
|
2021-08-26 20:26:33 +00:00
|
|
|
# Move constructor.
|
2024-06-14 10:49:41 +00:00
|
|
|
result.append(f"{class_name}::{class_name}({class_name} &&p_other) {{")
|
2021-11-18 03:05:14 +00:00
|
|
|
if needs_copy_instead_of_move(class_name) and copy_constructor_index >= 0:
|
2022-02-26 20:45:00 +00:00
|
|
|
result.append(
|
2024-06-14 10:49:41 +00:00
|
|
|
f"\tinternal::_call_builtin_constructor(_method_bindings.constructor_{copy_constructor_index}, &opaque, &p_other);"
|
2022-02-26 20:45:00 +00:00
|
|
|
)
|
2021-11-18 03:05:14 +00:00
|
|
|
else:
|
2024-06-14 10:49:41 +00:00
|
|
|
result.append("\tstd::swap(opaque, p_other.opaque);")
|
2021-08-26 20:26:33 +00:00
|
|
|
result.append("}")
|
|
|
|
result.append("")
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
if builtin_api["has_destructor"]:
|
|
|
|
result.append(f"{class_name}::~{class_name}() {{")
|
|
|
|
result.append("\t_method_bindings.destructor(&opaque);")
|
|
|
|
result.append("}")
|
|
|
|
result.append("")
|
|
|
|
|
|
|
|
method_list = []
|
|
|
|
|
|
|
|
if "methods" in builtin_api:
|
|
|
|
for method in builtin_api["methods"]:
|
|
|
|
method_list.append(method["name"])
|
|
|
|
|
|
|
|
if "is_vararg" in method and method["is_vararg"]:
|
|
|
|
# Done in the header because of the template.
|
|
|
|
continue
|
|
|
|
|
2021-08-19 23:03:11 +00:00
|
|
|
method_signature = make_signature(class_name, method, for_builtin=True)
|
2024-03-27 12:54:05 +00:00
|
|
|
result.append(method_signature + " {")
|
2021-08-18 14:03:52 +00:00
|
|
|
|
|
|
|
method_call = "\t"
|
2024-01-18 09:23:21 +00:00
|
|
|
is_ref = False
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
if "return_type" in method:
|
2024-01-18 09:23:21 +00:00
|
|
|
return_type = method["return_type"]
|
|
|
|
if is_enum(return_type):
|
|
|
|
method_call += f"return ({get_gdextension_type(correct_type(return_type))})internal::_call_builtin_method_ptr_ret<int64_t>("
|
|
|
|
elif is_pod_type(return_type) or is_variant(return_type):
|
|
|
|
method_call += f"return internal::_call_builtin_method_ptr_ret<{get_gdextension_type(correct_type(return_type))}>("
|
|
|
|
elif is_refcounted(return_type):
|
|
|
|
method_call += f"return Ref<{return_type}>::_gde_internal_constructor(internal::_call_builtin_method_ptr_ret_obj<{return_type}>("
|
|
|
|
is_ref = True
|
|
|
|
else:
|
|
|
|
method_call += f"return internal::_call_builtin_method_ptr_ret_obj<{return_type}>("
|
2021-08-18 14:03:52 +00:00
|
|
|
else:
|
2022-12-26 18:44:56 +00:00
|
|
|
method_call += "internal::_call_builtin_method_ptr_no_ret("
|
2021-08-18 14:03:52 +00:00
|
|
|
method_call += f'_method_bindings.method_{method["name"]}, '
|
|
|
|
if "is_static" in method and method["is_static"]:
|
|
|
|
method_call += "nullptr"
|
|
|
|
else:
|
2022-12-07 13:11:44 +00:00
|
|
|
method_call += "(GDExtensionTypePtr)&opaque"
|
2021-08-18 14:03:52 +00:00
|
|
|
|
|
|
|
if "arguments" in method:
|
|
|
|
arguments = []
|
|
|
|
method_call += ", "
|
|
|
|
for argument in method["arguments"]:
|
|
|
|
(encode, arg_name) = get_encoded_arg(
|
|
|
|
argument["name"],
|
|
|
|
argument["type"],
|
|
|
|
argument["meta"] if "meta" in argument else None,
|
|
|
|
)
|
|
|
|
result += encode
|
|
|
|
arguments.append(arg_name)
|
|
|
|
method_call += ", ".join(arguments)
|
2024-01-18 09:23:21 +00:00
|
|
|
|
|
|
|
if is_ref:
|
|
|
|
method_call += ")" # Close Ref<> constructor.
|
2021-08-18 14:03:52 +00:00
|
|
|
method_call += ");"
|
|
|
|
|
|
|
|
result.append(method_call)
|
|
|
|
result.append("}")
|
|
|
|
result.append("")
|
|
|
|
|
|
|
|
if "members" in builtin_api:
|
|
|
|
for member in builtin_api["members"]:
|
|
|
|
if f'get_{member["name"]}' not in method_list:
|
|
|
|
result.append(f'{correct_type(member["type"])} {class_name}::get_{member["name"]}() const {{')
|
|
|
|
result.append(
|
2022-12-07 13:11:44 +00:00
|
|
|
f'\treturn internal::_call_builtin_ptr_getter<{correct_type(member["type"])}>(_method_bindings.member_{member["name"]}_getter, (GDExtensionConstTypePtr)&opaque);'
|
2021-08-18 14:03:52 +00:00
|
|
|
)
|
|
|
|
result.append("}")
|
|
|
|
|
|
|
|
if f'set_{member["name"]}' not in method_list:
|
|
|
|
result.append(f'void {class_name}::set_{member["name"]}({type_for_parameter(member["type"])}value) {{')
|
|
|
|
(encode, arg_name) = get_encoded_arg("value", member["type"], None)
|
|
|
|
result += encode
|
|
|
|
result.append(
|
2022-12-07 13:11:44 +00:00
|
|
|
f'\t_method_bindings.member_{member["name"]}_setter((GDExtensionConstTypePtr)&opaque, (GDExtensionConstTypePtr){arg_name});'
|
2021-08-18 14:03:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
result.append("}")
|
|
|
|
result.append("")
|
|
|
|
|
|
|
|
if "operators" in builtin_api:
|
|
|
|
for operator in builtin_api["operators"]:
|
|
|
|
if operator["name"] not in ["in", "xor"]:
|
|
|
|
if "right_type" in operator:
|
|
|
|
result.append(
|
2024-06-14 10:49:41 +00:00
|
|
|
f'{correct_type(operator["return_type"])} {class_name}::operator{operator["name"]}({type_for_parameter(operator["right_type"])}p_other) const {{'
|
2021-08-18 14:03:52 +00:00
|
|
|
)
|
|
|
|
(encode, arg_name) = get_encoded_arg("other", operator["right_type"], None)
|
|
|
|
result += encode
|
|
|
|
result.append(
|
2022-12-07 13:11:44 +00:00
|
|
|
f'\treturn internal::_call_builtin_operator_ptr<{get_gdextension_type(correct_type(operator["return_type"]))}>(_method_bindings.operator_{get_operator_id_name(operator["name"])}_{operator["right_type"]}, (GDExtensionConstTypePtr)&opaque, (GDExtensionConstTypePtr){arg_name});'
|
2021-08-18 14:03:52 +00:00
|
|
|
)
|
|
|
|
result.append("}")
|
|
|
|
else:
|
|
|
|
result.append(
|
|
|
|
f'{correct_type(operator["return_type"])} {class_name}::operator{operator["name"].replace("unary", "")}() const {{'
|
|
|
|
)
|
|
|
|
result.append(
|
2022-12-07 13:11:44 +00:00
|
|
|
f'\treturn internal::_call_builtin_operator_ptr<{get_gdextension_type(correct_type(operator["return_type"]))}>(_method_bindings.operator_{get_operator_id_name(operator["name"])}, (GDExtensionConstTypePtr)&opaque, (GDExtensionConstTypePtr)nullptr);'
|
2021-08-18 14:03:52 +00:00
|
|
|
)
|
|
|
|
result.append("}")
|
2021-08-26 20:26:33 +00:00
|
|
|
result.append("")
|
|
|
|
|
|
|
|
# Copy assignment.
|
|
|
|
if copy_constructor_index >= 0:
|
2024-06-14 10:49:41 +00:00
|
|
|
result.append(f"{class_name} &{class_name}::operator=(const {class_name} &p_other) {{")
|
2021-08-26 20:26:33 +00:00
|
|
|
if builtin_api["has_destructor"]:
|
|
|
|
result.append("\t_method_bindings.destructor(&opaque);")
|
|
|
|
(encode, arg_name) = get_encoded_arg(
|
|
|
|
"other",
|
|
|
|
class_name,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
result += encode
|
|
|
|
result.append(
|
|
|
|
f"\tinternal::_call_builtin_constructor(_method_bindings.constructor_{copy_constructor_index}, &opaque, {arg_name});"
|
|
|
|
)
|
|
|
|
result.append("\treturn *this;")
|
|
|
|
result.append("}")
|
|
|
|
result.append("")
|
|
|
|
|
|
|
|
# Move assignment.
|
2024-06-14 10:49:41 +00:00
|
|
|
result.append(f"{class_name} &{class_name}::operator=({class_name} &&p_other) {{")
|
2021-11-18 03:05:14 +00:00
|
|
|
if needs_copy_instead_of_move(class_name) and copy_constructor_index >= 0:
|
2022-02-26 20:45:00 +00:00
|
|
|
result.append(
|
2024-06-14 10:49:41 +00:00
|
|
|
f"\tinternal::_call_builtin_constructor(_method_bindings.constructor_{copy_constructor_index}, &opaque, &p_other);"
|
2022-02-26 20:45:00 +00:00
|
|
|
)
|
2021-11-18 03:05:14 +00:00
|
|
|
else:
|
2024-06-14 10:49:41 +00:00
|
|
|
result.append("\tstd::swap(opaque, p_other.opaque);")
|
2021-08-26 20:26:33 +00:00
|
|
|
result.append("\treturn *this;")
|
|
|
|
result.append("}")
|
2021-08-18 14:03:52 +00:00
|
|
|
|
2021-08-26 20:26:33 +00:00
|
|
|
result.append("")
|
2021-08-18 14:03:52 +00:00
|
|
|
result.append("} //namespace godot")
|
|
|
|
|
|
|
|
return "\n".join(result)
|
|
|
|
|
|
|
|
|
|
|
|
def generate_engine_classes_bindings(api, output_dir, use_template_get_node):
|
|
|
|
global engine_classes
|
2021-08-25 00:50:39 +00:00
|
|
|
global singletons
|
2022-02-16 09:29:01 +00:00
|
|
|
global native_structures
|
2021-08-18 14:03:52 +00:00
|
|
|
|
|
|
|
include_gen_folder = Path(output_dir) / "include" / "godot_cpp" / "classes"
|
|
|
|
source_gen_folder = Path(output_dir) / "src" / "classes"
|
|
|
|
|
|
|
|
include_gen_folder.mkdir(parents=True, exist_ok=True)
|
|
|
|
source_gen_folder.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
2021-08-25 00:50:39 +00:00
|
|
|
# First create map of classes and singletons.
|
2021-08-18 14:03:52 +00:00
|
|
|
for class_api in api["classes"]:
|
2023-07-07 02:16:27 +00:00
|
|
|
# Generate code for the ClassDB singleton under a different name.
|
2024-04-23 18:37:43 +00:00
|
|
|
if class_api["name"] in CLASS_ALIASES:
|
|
|
|
class_api["alias_for"] = class_api["name"]
|
|
|
|
class_api["name"] = CLASS_ALIASES[class_api["alias_for"]]
|
2021-08-18 14:03:52 +00:00
|
|
|
engine_classes[class_api["name"]] = class_api["is_refcounted"]
|
2021-08-24 13:50:36 +00:00
|
|
|
for native_struct in api["native_structures"]:
|
2023-10-21 23:32:45 +00:00
|
|
|
if native_struct["name"] == "ObjectID":
|
|
|
|
continue
|
2021-08-24 13:50:36 +00:00
|
|
|
engine_classes[native_struct["name"]] = False
|
2022-02-16 09:29:01 +00:00
|
|
|
native_structures.append(native_struct["name"])
|
|
|
|
|
2021-08-25 00:50:39 +00:00
|
|
|
for singleton in api["singletons"]:
|
2023-07-07 02:16:27 +00:00
|
|
|
# Generate code for the ClassDB singleton under a different name.
|
2024-04-23 18:37:43 +00:00
|
|
|
if singleton["name"] in CLASS_ALIASES:
|
|
|
|
singleton["alias_for"] = singleton["name"]
|
|
|
|
singleton["name"] = CLASS_ALIASES[singleton["name"]]
|
2021-08-25 00:50:39 +00:00
|
|
|
singletons.append(singleton["name"])
|
2021-08-18 14:03:52 +00:00
|
|
|
|
|
|
|
for class_api in api["classes"]:
|
|
|
|
# Check used classes for header include.
|
|
|
|
used_classes = set()
|
|
|
|
fully_used_classes = set()
|
|
|
|
|
|
|
|
class_name = class_api["name"]
|
|
|
|
|
|
|
|
header_filename = include_gen_folder / (camel_to_snake(class_api["name"]) + ".hpp")
|
|
|
|
source_filename = source_gen_folder / (camel_to_snake(class_api["name"]) + ".cpp")
|
|
|
|
|
|
|
|
if "methods" in class_api:
|
|
|
|
for method in class_api["methods"]:
|
|
|
|
if "arguments" in method:
|
|
|
|
for argument in method["arguments"]:
|
2021-08-24 13:50:36 +00:00
|
|
|
type_name = argument["type"]
|
2022-02-16 09:35:13 +00:00
|
|
|
if type_name.startswith("const "):
|
|
|
|
type_name = type_name[6:]
|
2021-08-24 13:50:36 +00:00
|
|
|
if type_name.endswith("*"):
|
|
|
|
type_name = type_name[:-1]
|
|
|
|
if is_included(type_name, class_name):
|
2022-09-15 07:33:07 +00:00
|
|
|
if type_name.startswith("typedarray::"):
|
|
|
|
fully_used_classes.add("TypedArray")
|
|
|
|
array_type_name = type_name.replace("typedarray::", "")
|
|
|
|
if array_type_name.startswith("const "):
|
|
|
|
array_type_name = array_type_name[6:]
|
|
|
|
if array_type_name.endswith("*"):
|
|
|
|
array_type_name = array_type_name[:-1]
|
|
|
|
if is_included(array_type_name, class_name):
|
|
|
|
if is_enum(array_type_name):
|
|
|
|
fully_used_classes.add(get_enum_class(array_type_name))
|
|
|
|
elif "default_value" in argument:
|
|
|
|
fully_used_classes.add(array_type_name)
|
|
|
|
else:
|
|
|
|
used_classes.add(array_type_name)
|
|
|
|
elif is_enum(type_name):
|
2021-08-24 13:50:36 +00:00
|
|
|
fully_used_classes.add(get_enum_class(type_name))
|
2021-08-18 14:03:52 +00:00
|
|
|
elif "default_value" in argument:
|
2021-08-24 13:50:36 +00:00
|
|
|
fully_used_classes.add(type_name)
|
2021-08-18 14:03:52 +00:00
|
|
|
else:
|
2021-08-24 13:50:36 +00:00
|
|
|
used_classes.add(type_name)
|
|
|
|
if is_refcounted(type_name):
|
2021-08-18 14:03:52 +00:00
|
|
|
fully_used_classes.add("Ref")
|
|
|
|
if "return_value" in method:
|
2021-08-24 13:50:36 +00:00
|
|
|
type_name = method["return_value"]["type"]
|
2022-02-16 09:35:13 +00:00
|
|
|
if type_name.startswith("const "):
|
|
|
|
type_name = type_name[6:]
|
2021-08-24 13:50:36 +00:00
|
|
|
if type_name.endswith("*"):
|
|
|
|
type_name = type_name[:-1]
|
|
|
|
if is_included(type_name, class_name):
|
2022-09-15 07:33:07 +00:00
|
|
|
if type_name.startswith("typedarray::"):
|
|
|
|
fully_used_classes.add("TypedArray")
|
|
|
|
array_type_name = type_name.replace("typedarray::", "")
|
|
|
|
if array_type_name.startswith("const "):
|
|
|
|
array_type_name = array_type_name[6:]
|
|
|
|
if array_type_name.endswith("*"):
|
|
|
|
array_type_name = array_type_name[:-1]
|
|
|
|
if is_included(array_type_name, class_name):
|
|
|
|
if is_enum(array_type_name):
|
|
|
|
fully_used_classes.add(get_enum_class(array_type_name))
|
|
|
|
elif is_variant(array_type_name):
|
|
|
|
fully_used_classes.add(array_type_name)
|
|
|
|
else:
|
|
|
|
used_classes.add(array_type_name)
|
|
|
|
elif is_enum(type_name):
|
2021-08-24 13:50:36 +00:00
|
|
|
fully_used_classes.add(get_enum_class(type_name))
|
|
|
|
elif is_variant(type_name):
|
|
|
|
fully_used_classes.add(type_name)
|
2021-08-18 14:03:52 +00:00
|
|
|
else:
|
2021-08-24 13:50:36 +00:00
|
|
|
used_classes.add(type_name)
|
|
|
|
if is_refcounted(type_name):
|
2021-08-18 14:03:52 +00:00
|
|
|
fully_used_classes.add("Ref")
|
|
|
|
|
|
|
|
if "members" in class_api:
|
|
|
|
for member in class_api["members"]:
|
|
|
|
if is_included(member["type"], class_name):
|
|
|
|
if is_enum(member["type"]):
|
|
|
|
fully_used_classes.add(get_enum_class(member["type"]))
|
|
|
|
else:
|
|
|
|
used_classes.add(member["type"])
|
|
|
|
if is_refcounted(member["type"]):
|
|
|
|
fully_used_classes.add("Ref")
|
|
|
|
|
|
|
|
if "inherits" in class_api:
|
|
|
|
if is_included(class_api["inherits"], class_name):
|
|
|
|
fully_used_classes.add(class_api["inherits"])
|
|
|
|
if is_refcounted(class_api["name"]):
|
|
|
|
fully_used_classes.add("Ref")
|
|
|
|
else:
|
|
|
|
fully_used_classes.add("Wrapped")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2023-08-15 22:35:59 +00:00
|
|
|
# In order to ensure that PtrToArg specializations for native structs are
|
|
|
|
# always used, let's move any of them into 'fully_used_classes'.
|
|
|
|
for type_name in used_classes:
|
|
|
|
if is_struct_type(type_name) and not is_included_struct_type(type_name):
|
|
|
|
fully_used_classes.add(type_name)
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
for type_name in fully_used_classes:
|
|
|
|
if type_name in used_classes:
|
|
|
|
used_classes.remove(type_name)
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2022-09-13 08:37:58 +00:00
|
|
|
used_classes = list(used_classes)
|
|
|
|
used_classes.sort()
|
|
|
|
fully_used_classes = list(fully_used_classes)
|
|
|
|
fully_used_classes.sort()
|
|
|
|
|
2023-06-16 07:38:01 +00:00
|
|
|
with header_filename.open("w+", encoding="utf-8") as header_file:
|
2021-08-18 14:03:52 +00:00
|
|
|
header_file.write(
|
|
|
|
generate_engine_class_header(class_api, used_classes, fully_used_classes, use_template_get_node)
|
|
|
|
)
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2023-06-16 07:38:01 +00:00
|
|
|
with source_filename.open("w+", encoding="utf-8") as source_file:
|
2021-08-18 14:03:52 +00:00
|
|
|
source_file.write(
|
|
|
|
generate_engine_class_source(class_api, used_classes, fully_used_classes, use_template_get_node)
|
|
|
|
)
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-24 13:50:36 +00:00
|
|
|
for native_struct in api["native_structures"]:
|
|
|
|
struct_name = native_struct["name"]
|
2023-10-21 23:32:45 +00:00
|
|
|
if struct_name == "ObjectID":
|
|
|
|
continue
|
2021-08-24 13:50:36 +00:00
|
|
|
snake_struct_name = camel_to_snake(struct_name)
|
|
|
|
|
|
|
|
header_filename = include_gen_folder / (snake_struct_name + ".hpp")
|
|
|
|
|
|
|
|
result = []
|
|
|
|
add_header(f"{snake_struct_name}.hpp", result)
|
|
|
|
|
|
|
|
header_guard = f"GODOT_CPP_{snake_struct_name.upper()}_HPP"
|
|
|
|
result.append(f"#ifndef {header_guard}")
|
|
|
|
result.append(f"#define {header_guard}")
|
|
|
|
|
2022-03-16 18:33:06 +00:00
|
|
|
used_classes = []
|
|
|
|
expanded_format = native_struct["format"].replace("(", " ").replace(")", ";").replace(",", ";")
|
|
|
|
for field in expanded_format.split(";"):
|
|
|
|
field_type = field.strip().split(" ")[0].split("::")[0]
|
|
|
|
if field_type != "" and not is_included_type(field_type) and not is_pod_type(field_type):
|
|
|
|
if not field_type in used_classes:
|
|
|
|
used_classes.append(field_type)
|
|
|
|
|
2021-08-24 13:50:36 +00:00
|
|
|
result.append("")
|
2022-03-16 18:33:06 +00:00
|
|
|
|
|
|
|
for included in used_classes:
|
|
|
|
result.append(f"#include <godot_cpp/{get_include_path(included)}>")
|
|
|
|
|
2023-04-07 11:48:38 +00:00
|
|
|
if len(used_classes) == 0:
|
|
|
|
result.append("#include <godot_cpp/core/method_ptrcall.hpp>")
|
|
|
|
result.append("")
|
2022-03-16 18:33:06 +00:00
|
|
|
|
2021-08-24 13:50:36 +00:00
|
|
|
result.append("namespace godot {")
|
|
|
|
result.append("")
|
|
|
|
|
|
|
|
result.append(f"struct {struct_name} {{")
|
2022-03-16 18:33:06 +00:00
|
|
|
for field in native_struct["format"].split(";"):
|
|
|
|
if field != "":
|
|
|
|
result.append(f"\t{field};")
|
2021-08-24 13:50:36 +00:00
|
|
|
result.append("};")
|
|
|
|
|
2022-02-15 08:47:12 +00:00
|
|
|
result.append("")
|
|
|
|
result.append(f"GDVIRTUAL_NATIVE_PTR({struct_name});")
|
2021-08-24 13:50:36 +00:00
|
|
|
result.append("")
|
|
|
|
result.append("} // namespace godot")
|
|
|
|
result.append("")
|
|
|
|
result.append(f"#endif // ! {header_guard}")
|
|
|
|
|
2023-06-16 07:38:01 +00:00
|
|
|
with header_filename.open("w+", encoding="utf-8") as header_file:
|
2021-08-24 13:50:36 +00:00
|
|
|
header_file.write("\n".join(result))
|
|
|
|
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
def generate_engine_class_header(class_api, used_classes, fully_used_classes, use_template_get_node):
|
2021-08-25 00:50:39 +00:00
|
|
|
global singletons
|
2021-08-18 14:03:52 +00:00
|
|
|
result = []
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
class_name = class_api["name"]
|
|
|
|
snake_class_name = camel_to_snake(class_name).upper()
|
2021-08-25 00:50:39 +00:00
|
|
|
is_singleton = class_name in singletons
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
add_header(f"{snake_class_name.lower()}.hpp", result)
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
header_guard = f"GODOT_CPP_{snake_class_name}_HPP"
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
result.append(f"#ifndef {header_guard}")
|
|
|
|
result.append(f"#define {header_guard}")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
result.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
for included in fully_used_classes:
|
2022-09-15 07:33:07 +00:00
|
|
|
if included == "TypedArray":
|
|
|
|
result.append("#include <godot_cpp/variant/typed_array.hpp>")
|
|
|
|
else:
|
|
|
|
result.append(f"#include <godot_cpp/{get_include_path(included)}>")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2023-06-13 19:00:16 +00:00
|
|
|
if class_name == "EditorPlugin":
|
2023-10-12 03:59:59 +00:00
|
|
|
result.append("#include <godot_cpp/classes/editor_plugin_registration.hpp>")
|
2023-06-13 19:00:16 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
if len(fully_used_classes) > 0:
|
|
|
|
result.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2023-07-07 02:16:27 +00:00
|
|
|
if class_name != "Object" and class_name != "ClassDBSingleton":
|
2021-08-19 23:03:11 +00:00
|
|
|
result.append("#include <godot_cpp/core/class_db.hpp>")
|
|
|
|
result.append("")
|
|
|
|
result.append("#include <type_traits>")
|
|
|
|
result.append("")
|
|
|
|
|
2024-04-23 18:37:43 +00:00
|
|
|
if class_name == "ClassDBSingleton":
|
|
|
|
result.append("#include <godot_cpp/core/binder_common.hpp>")
|
|
|
|
result.append("")
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
result.append("namespace godot {")
|
|
|
|
result.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
for type_name in used_classes:
|
2022-10-05 06:03:52 +00:00
|
|
|
if is_struct_type(type_name):
|
2022-02-16 09:29:01 +00:00
|
|
|
result.append(f"struct {type_name};")
|
|
|
|
else:
|
|
|
|
result.append(f"class {type_name};")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
if len(used_classes) > 0:
|
|
|
|
result.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
inherits = class_api["inherits"] if "inherits" in class_api else "Wrapped"
|
|
|
|
result.append(f"class {class_name} : public {inherits} {{")
|
2017-05-12 19:53:07 +00:00
|
|
|
|
2023-07-07 02:16:27 +00:00
|
|
|
if "alias_for" in class_api:
|
|
|
|
result.append(f"\tGDEXTENSION_CLASS_ALIAS({class_name}, {class_api['alias_for']}, {inherits})")
|
|
|
|
else:
|
|
|
|
result.append(f"\tGDEXTENSION_CLASS({class_name}, {inherits})")
|
2021-08-18 14:03:52 +00:00
|
|
|
result.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2024-05-07 17:54:35 +00:00
|
|
|
if is_singleton:
|
|
|
|
result.append(f"\tstatic {class_name} *singleton;")
|
|
|
|
result.append("")
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
result.append("public:")
|
2021-09-09 23:40:40 +00:00
|
|
|
result.append("")
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
if "enums" in class_api:
|
|
|
|
for enum_api in class_api["enums"]:
|
2023-11-27 23:16:46 +00:00
|
|
|
if enum_api["is_bitfield"]:
|
|
|
|
result.append(f'\tenum {enum_api["name"]} : uint64_t {{')
|
|
|
|
else:
|
|
|
|
result.append(f'\tenum {enum_api["name"]} {{')
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
for value in enum_api["values"]:
|
|
|
|
result.append(f'\t\t{value["name"]} = {value["value"]},')
|
|
|
|
result.append("\t};")
|
|
|
|
result.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2022-09-03 02:32:38 +00:00
|
|
|
if "constants" in class_api:
|
|
|
|
for value in class_api["constants"]:
|
|
|
|
if "type" not in value:
|
|
|
|
value["type"] = "int"
|
2022-09-18 19:58:44 +00:00
|
|
|
result.append(f'\tstatic const {value["type"]} {value["name"]} = {value["value"]};')
|
2022-09-03 02:32:38 +00:00
|
|
|
result.append("")
|
|
|
|
|
2021-08-25 00:50:39 +00:00
|
|
|
if is_singleton:
|
|
|
|
result.append(f"\tstatic {class_name} *get_singleton();")
|
|
|
|
result.append("")
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
if "methods" in class_api:
|
|
|
|
for method in class_api["methods"]:
|
|
|
|
if method["is_virtual"]:
|
2021-08-19 23:03:11 +00:00
|
|
|
# Will be done later.
|
2021-08-18 14:03:52 +00:00
|
|
|
continue
|
2018-02-11 14:50:01 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
vararg = "is_vararg" in method and method["is_vararg"]
|
2021-08-19 23:03:11 +00:00
|
|
|
|
|
|
|
method_signature = "\t"
|
|
|
|
method_signature += make_signature(
|
|
|
|
class_name, method, for_header=True, use_template_get_node=use_template_get_node
|
|
|
|
)
|
|
|
|
result.append(method_signature + ";")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
if vararg:
|
|
|
|
# Add templated version.
|
2021-08-19 23:03:11 +00:00
|
|
|
result += make_varargs_template(method)
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-19 23:03:11 +00:00
|
|
|
# Virtuals now.
|
|
|
|
for method in class_api["methods"]:
|
|
|
|
if not method["is_virtual"]:
|
|
|
|
continue
|
2018-02-11 14:50:01 +00:00
|
|
|
|
2021-08-19 23:03:11 +00:00
|
|
|
method_signature = "\t"
|
2021-08-24 13:50:36 +00:00
|
|
|
method_signature += make_signature(
|
|
|
|
class_name, method, for_header=True, use_template_get_node=use_template_get_node
|
|
|
|
)
|
2021-08-19 23:03:11 +00:00
|
|
|
result.append(method_signature + ";")
|
|
|
|
|
|
|
|
result.append("protected:")
|
2022-09-19 23:29:39 +00:00
|
|
|
# T is the custom class we want to register (from which the call initiates, going up the inheritance chain),
|
|
|
|
# B is its base class (can be a custom class too, that's why we pass it).
|
2024-03-10 21:02:43 +00:00
|
|
|
result.append("\ttemplate <typename T, typename B>")
|
2021-08-19 23:03:11 +00:00
|
|
|
result.append("\tstatic void register_virtuals() {")
|
|
|
|
if class_name != "Object":
|
2022-09-19 23:29:39 +00:00
|
|
|
result.append(f"\t\t{inherits}::register_virtuals<T, B>();")
|
2021-08-19 23:03:11 +00:00
|
|
|
if "methods" in class_api:
|
|
|
|
for method in class_api["methods"]:
|
|
|
|
if not method["is_virtual"]:
|
|
|
|
continue
|
|
|
|
method_name = escape_identifier(method["name"])
|
2021-08-24 13:50:36 +00:00
|
|
|
result.append(
|
2022-09-19 23:29:39 +00:00
|
|
|
# If the method is different from the base class, it means T overrides it, so it needs to be bound.
|
|
|
|
# Note that with an `if constexpr`, the code inside the `if` will not even be compiled if the
|
|
|
|
# condition returns false (in such cases it can't compile due to ambiguity).
|
|
|
|
f"\t\tif constexpr (!std::is_same_v<decltype(&B::{method_name}),decltype(&T::{method_name})>) {{"
|
2021-08-24 13:50:36 +00:00
|
|
|
)
|
2021-08-19 23:03:11 +00:00
|
|
|
result.append(f"\t\t\tBIND_VIRTUAL_METHOD(T, {method_name});")
|
2021-08-18 14:03:52 +00:00
|
|
|
result.append("\t\t}")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-19 23:03:11 +00:00
|
|
|
result.append("\t}")
|
|
|
|
result.append("")
|
2024-05-07 17:54:35 +00:00
|
|
|
|
|
|
|
if is_singleton:
|
|
|
|
result.append(f"\t~{class_name}();")
|
|
|
|
result.append("")
|
|
|
|
|
2021-08-19 23:03:11 +00:00
|
|
|
result.append("public:")
|
2019-10-21 16:08:03 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
# Special cases.
|
2022-11-28 12:47:55 +00:00
|
|
|
if class_name == "XMLParser":
|
|
|
|
result.append("\tError _open_buffer(const uint8_t *p_buffer, size_t p_size);")
|
|
|
|
|
|
|
|
if class_name == "FileAccess":
|
|
|
|
result.append("\tuint64_t get_buffer(uint8_t *p_dst, uint64_t p_length) const;")
|
|
|
|
result.append("\tvoid store_buffer(const uint8_t *p_src, uint64_t p_length);")
|
|
|
|
|
|
|
|
if class_name == "WorkerThreadPool":
|
|
|
|
result.append("\tenum {")
|
|
|
|
result.append("\tINVALID_TASK_ID = -1")
|
|
|
|
result.append("\t};")
|
|
|
|
result.append("\ttypedef int64_t TaskID;")
|
|
|
|
result.append("\ttypedef int64_t GroupID;")
|
|
|
|
result.append(
|
|
|
|
"\tTaskID add_native_task(void (*p_func)(void *), void *p_userdata, bool p_high_priority = false, const String &p_description = String());"
|
|
|
|
)
|
|
|
|
result.append(
|
|
|
|
"\tGroupID add_native_group_task(void (*p_func)(void *, uint32_t), void *p_userdata, int p_elements, int p_tasks = -1, bool p_high_priority = false, const String &p_description = String());"
|
|
|
|
)
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
if class_name == "Object":
|
|
|
|
result.append("")
|
2022-09-18 21:23:49 +00:00
|
|
|
|
2024-03-27 12:54:05 +00:00
|
|
|
result.append("\ttemplate <typename T>")
|
2021-08-18 14:03:52 +00:00
|
|
|
result.append("\tstatic T *cast_to(Object *p_object);")
|
2021-09-21 14:40:57 +00:00
|
|
|
|
2024-03-27 12:54:05 +00:00
|
|
|
result.append("\ttemplate <typename T>")
|
2022-09-18 21:23:49 +00:00
|
|
|
result.append("\tstatic const T *cast_to(const Object *p_object);")
|
|
|
|
|
2021-09-27 17:24:14 +00:00
|
|
|
result.append("\tvirtual ~Object() = default;")
|
2022-09-18 21:23:49 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
elif use_template_get_node and class_name == "Node":
|
2024-03-27 12:54:05 +00:00
|
|
|
result.append("\ttemplate <typename T>")
|
2021-08-24 13:50:36 +00:00
|
|
|
result.append(
|
|
|
|
"\tT *get_node(const NodePath &p_path) const { return Object::cast_to<T>(get_node_internal(p_path)); }"
|
|
|
|
)
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
result.append("")
|
|
|
|
result.append("};")
|
|
|
|
result.append("")
|
2019-10-21 16:08:03 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
result.append("} // namespace godot")
|
2022-02-15 08:47:12 +00:00
|
|
|
result.append("")
|
|
|
|
|
|
|
|
if "enums" in class_api and class_name != "Object":
|
|
|
|
for enum_api in class_api["enums"]:
|
2022-07-11 11:37:14 +00:00
|
|
|
if enum_api["is_bitfield"]:
|
2022-12-08 05:34:19 +00:00
|
|
|
result.append(f'VARIANT_BITFIELD_CAST({class_name}::{enum_api["name"]});')
|
2022-07-11 11:37:14 +00:00
|
|
|
else:
|
2022-12-08 05:34:19 +00:00
|
|
|
result.append(f'VARIANT_ENUM_CAST({class_name}::{enum_api["name"]});')
|
2022-02-15 08:47:12 +00:00
|
|
|
result.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2023-07-07 02:16:27 +00:00
|
|
|
if class_name == "ClassDBSingleton":
|
|
|
|
result.append("#define CLASSDB_SINGLETON_FORWARD_METHODS \\")
|
2024-04-23 18:37:43 +00:00
|
|
|
|
|
|
|
if "enums" in class_api:
|
|
|
|
for enum_api in class_api["enums"]:
|
|
|
|
if enum_api["is_bitfield"]:
|
|
|
|
result.append(f'\tenum {enum_api["name"]} : uint64_t {{ \\')
|
|
|
|
else:
|
|
|
|
result.append(f'\tenum {enum_api["name"]} {{ \\')
|
|
|
|
|
|
|
|
for value in enum_api["values"]:
|
|
|
|
result.append(f'\t\t{value["name"]} = {value["value"]}, \\')
|
|
|
|
result.append("\t}; \\")
|
|
|
|
result.append("\t \\")
|
|
|
|
|
2023-07-07 02:16:27 +00:00
|
|
|
for method in class_api["methods"]:
|
2024-06-12 13:18:52 +00:00
|
|
|
# ClassDBSingleton shouldn't have any static methods, but if some appear later, lets skip them.
|
2023-07-07 02:16:27 +00:00
|
|
|
if "is_static" in method and method["is_static"]:
|
|
|
|
continue
|
|
|
|
|
2024-06-12 13:18:52 +00:00
|
|
|
vararg = "is_vararg" in method and method["is_vararg"]
|
|
|
|
if vararg:
|
2024-03-27 12:54:05 +00:00
|
|
|
method_signature = "\ttemplate <typename... Args> static "
|
2024-06-12 13:18:52 +00:00
|
|
|
else:
|
|
|
|
method_signature = "\tstatic "
|
|
|
|
|
2024-04-23 18:37:43 +00:00
|
|
|
return_type = None
|
2023-07-07 02:16:27 +00:00
|
|
|
if "return_type" in method:
|
2024-04-23 18:37:43 +00:00
|
|
|
return_type = correct_type(method["return_type"].replace("ClassDBSingleton", "ClassDB"), None, False)
|
2023-07-07 02:16:27 +00:00
|
|
|
elif "return_value" in method:
|
2024-04-23 18:37:43 +00:00
|
|
|
return_type = correct_type(
|
|
|
|
method["return_value"]["type"].replace("ClassDBSingleton", "ClassDB"),
|
|
|
|
method["return_value"].get("meta", None),
|
|
|
|
False,
|
2023-07-07 02:16:27 +00:00
|
|
|
)
|
2024-04-23 18:37:43 +00:00
|
|
|
if return_type is not None:
|
2024-03-27 12:54:05 +00:00
|
|
|
method_signature += return_type
|
|
|
|
if not method_signature.endswith("*"):
|
|
|
|
method_signature += " "
|
2023-07-07 02:16:27 +00:00
|
|
|
else:
|
|
|
|
method_signature += "void "
|
|
|
|
|
|
|
|
method_signature += f'{method["name"]}('
|
|
|
|
|
|
|
|
method_arguments = []
|
|
|
|
if "arguments" in method:
|
|
|
|
method_arguments = method["arguments"]
|
|
|
|
|
|
|
|
method_signature += make_function_parameters(
|
2024-06-12 13:18:52 +00:00
|
|
|
method_arguments, include_default=True, for_builtin=True, is_vararg=vararg
|
2023-07-07 02:16:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
method_signature += ") { \\"
|
|
|
|
|
|
|
|
result.append(method_signature)
|
|
|
|
|
|
|
|
method_body = "\t\t"
|
2024-04-23 18:37:43 +00:00
|
|
|
if return_type is not None:
|
2023-07-07 02:16:27 +00:00
|
|
|
method_body += "return "
|
2024-04-23 18:37:43 +00:00
|
|
|
if "alias_for" in class_api and return_type.startswith(class_api["alias_for"] + "::"):
|
|
|
|
method_body += f"({return_type})"
|
2023-07-07 02:16:27 +00:00
|
|
|
method_body += f'ClassDBSingleton::get_singleton()->{method["name"]}('
|
2024-06-14 10:49:41 +00:00
|
|
|
method_body += ", ".join(map(lambda x: escape_argument(x["name"]), method_arguments))
|
2024-06-12 13:18:52 +00:00
|
|
|
if vararg:
|
2024-06-14 10:49:41 +00:00
|
|
|
method_body += ", p_args..."
|
2023-07-07 02:16:27 +00:00
|
|
|
method_body += "); \\"
|
|
|
|
|
|
|
|
result.append(method_body)
|
|
|
|
result.append("\t} \\")
|
|
|
|
result.append("\t;")
|
|
|
|
result.append("")
|
|
|
|
|
2024-04-23 18:37:43 +00:00
|
|
|
result.append("#define CLASSDB_SINGLETON_VARIANT_CAST \\")
|
|
|
|
|
|
|
|
if "enums" in class_api:
|
|
|
|
for enum_api in class_api["enums"]:
|
|
|
|
if enum_api["is_bitfield"]:
|
|
|
|
result.append(f'\tVARIANT_BITFIELD_CAST({class_api["alias_for"]}::{enum_api["name"]}); \\')
|
|
|
|
else:
|
|
|
|
result.append(f'\tVARIANT_ENUM_CAST({class_api["alias_for"]}::{enum_api["name"]}); \\')
|
|
|
|
|
|
|
|
result.append("\t;")
|
|
|
|
result.append("")
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
result.append(f"#endif // ! {header_guard}")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
return "\n".join(result)
|
2018-02-11 14:50:01 +00:00
|
|
|
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
def generate_engine_class_source(class_api, used_classes, fully_used_classes, use_template_get_node):
|
2021-08-25 00:50:39 +00:00
|
|
|
global singletons
|
2021-08-18 14:03:52 +00:00
|
|
|
result = []
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
class_name = class_api["name"]
|
|
|
|
snake_class_name = camel_to_snake(class_name)
|
2021-08-25 00:50:39 +00:00
|
|
|
is_singleton = class_name in singletons
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
add_header(f"{snake_class_name}.cpp", result)
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
result.append(f"#include <godot_cpp/classes/{snake_class_name}.hpp>")
|
|
|
|
result.append("")
|
2024-05-07 17:54:35 +00:00
|
|
|
result.append("#include <godot_cpp/core/class_db.hpp>")
|
2022-12-26 18:44:56 +00:00
|
|
|
result.append("#include <godot_cpp/core/engine_ptrcall.hpp>")
|
|
|
|
result.append("#include <godot_cpp/core/error_macros.hpp>")
|
2021-08-18 14:03:52 +00:00
|
|
|
result.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
for included in used_classes:
|
|
|
|
result.append(f"#include <godot_cpp/{get_include_path(included)}>")
|
2018-02-11 14:50:01 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
if len(used_classes) > 0:
|
2022-12-26 18:44:56 +00:00
|
|
|
result.append("")
|
2018-02-11 14:50:01 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
result.append("namespace godot {")
|
|
|
|
result.append("")
|
2021-08-25 00:50:39 +00:00
|
|
|
|
|
|
|
if is_singleton:
|
2024-05-07 17:54:35 +00:00
|
|
|
result.append(f"{class_name} *{class_name}::singleton = nullptr;")
|
|
|
|
result.append("")
|
2021-08-25 00:50:39 +00:00
|
|
|
result.append(f"{class_name} *{class_name}::get_singleton() {{")
|
2023-07-22 14:48:11 +00:00
|
|
|
# We assume multi-threaded access is OK because each assignment will assign the same value every time
|
|
|
|
result.append("\tif (unlikely(singleton == nullptr)) {")
|
2021-08-26 20:26:33 +00:00
|
|
|
result.append(
|
2023-07-22 14:48:11 +00:00
|
|
|
f"\t\tGDExtensionObjectPtr singleton_obj = internal::gdextension_interface_global_get_singleton({class_name}::get_class_static()._native_ptr());"
|
2021-08-26 20:26:33 +00:00
|
|
|
)
|
2021-08-25 00:50:39 +00:00
|
|
|
result.append("#ifdef DEBUG_ENABLED")
|
2023-09-12 13:33:49 +00:00
|
|
|
result.append("\t\tERR_FAIL_NULL_V(singleton_obj, nullptr);")
|
2021-08-25 00:50:39 +00:00
|
|
|
result.append("#endif // DEBUG_ENABLED")
|
2021-08-26 20:26:33 +00:00
|
|
|
result.append(
|
2023-07-22 14:48:11 +00:00
|
|
|
f"\t\tsingleton = reinterpret_cast<{class_name} *>(internal::gdextension_interface_object_get_instance_binding(singleton_obj, internal::token, &{class_name}::_gde_binding_callbacks));"
|
2021-08-26 20:26:33 +00:00
|
|
|
)
|
2023-07-22 14:48:11 +00:00
|
|
|
result.append("#ifdef DEBUG_ENABLED")
|
2023-09-12 13:33:49 +00:00
|
|
|
result.append("\t\tERR_FAIL_NULL_V(singleton, nullptr);")
|
2023-07-22 14:48:11 +00:00
|
|
|
result.append("#endif // DEBUG_ENABLED")
|
2024-05-07 17:54:35 +00:00
|
|
|
result.append("\t\tif (likely(singleton)) {")
|
|
|
|
result.append(f"\t\t\tClassDB::_register_engine_singleton({class_name}::get_class_static(), singleton);")
|
|
|
|
result.append("\t\t}")
|
2023-07-22 14:48:11 +00:00
|
|
|
result.append("\t}")
|
2021-08-25 00:50:39 +00:00
|
|
|
result.append("\treturn singleton;")
|
|
|
|
result.append("}")
|
|
|
|
result.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2024-05-07 17:54:35 +00:00
|
|
|
result.append(f"{class_name}::~{class_name}() {{")
|
|
|
|
result.append("\tif (singleton == this) {")
|
|
|
|
result.append(f"\t\tClassDB::_unregister_engine_singleton({class_name}::get_class_static());")
|
|
|
|
result.append("\t\tsingleton = nullptr;")
|
|
|
|
result.append("\t}")
|
|
|
|
result.append("}")
|
|
|
|
result.append("")
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
if "methods" in class_api:
|
|
|
|
for method in class_api["methods"]:
|
|
|
|
if method["is_virtual"]:
|
2021-08-19 23:03:11 +00:00
|
|
|
# Will be done later
|
2021-08-18 14:03:52 +00:00
|
|
|
continue
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
vararg = "is_vararg" in method and method["is_vararg"]
|
|
|
|
|
|
|
|
# Method signature.
|
2021-08-19 23:03:11 +00:00
|
|
|
method_signature = make_signature(class_name, method, use_template_get_node=use_template_get_node)
|
|
|
|
result.append(method_signature + " {")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
# Method body.
|
|
|
|
result.append(
|
2023-07-13 01:38:46 +00:00
|
|
|
f'\tstatic GDExtensionMethodBindPtr _gde_method_bind = internal::gdextension_interface_classdb_get_method_bind({class_name}::get_class_static()._native_ptr(), StringName("{method["name"]}")._native_ptr(), {method["hash"]});'
|
2021-08-18 14:03:52 +00:00
|
|
|
)
|
|
|
|
method_call = "\t"
|
|
|
|
has_return = "return_value" in method and method["return_value"]["type"] != "void"
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
if has_return:
|
|
|
|
result.append(
|
2023-02-16 15:38:38 +00:00
|
|
|
f'\tCHECK_METHOD_BIND_RET(_gde_method_bind, {get_default_value_for_type(method["return_value"]["type"])});'
|
2021-08-18 14:03:52 +00:00
|
|
|
)
|
2017-05-12 19:53:07 +00:00
|
|
|
else:
|
2023-02-16 15:38:38 +00:00
|
|
|
result.append("\tCHECK_METHOD_BIND(_gde_method_bind);")
|
2021-08-18 14:03:52 +00:00
|
|
|
|
|
|
|
is_ref = False
|
|
|
|
if not vararg:
|
|
|
|
if has_return:
|
|
|
|
return_type = method["return_value"]["type"]
|
|
|
|
meta_type = method["return_value"]["meta"] if "meta" in method["return_value"] else None
|
2023-01-06 22:48:40 +00:00
|
|
|
if is_enum(return_type):
|
|
|
|
if method["is_static"]:
|
2023-02-16 15:38:38 +00:00
|
|
|
method_call += f"return ({get_gdextension_type(correct_type(return_type, meta_type))})internal::_call_native_mb_ret<int64_t>(_gde_method_bind, nullptr"
|
2023-01-06 22:48:40 +00:00
|
|
|
else:
|
2023-02-16 15:38:38 +00:00
|
|
|
method_call += f"return ({get_gdextension_type(correct_type(return_type, meta_type))})internal::_call_native_mb_ret<int64_t>(_gde_method_bind, _owner"
|
2023-01-06 22:48:40 +00:00
|
|
|
elif is_pod_type(return_type) or is_variant(return_type):
|
2022-03-22 14:06:13 +00:00
|
|
|
if method["is_static"]:
|
2023-02-16 15:38:38 +00:00
|
|
|
method_call += f"return internal::_call_native_mb_ret<{get_gdextension_type(correct_type(return_type, meta_type))}>(_gde_method_bind, nullptr"
|
2022-03-22 14:06:13 +00:00
|
|
|
else:
|
2023-02-16 15:38:38 +00:00
|
|
|
method_call += f"return internal::_call_native_mb_ret<{get_gdextension_type(correct_type(return_type, meta_type))}>(_gde_method_bind, _owner"
|
2021-08-18 14:03:52 +00:00
|
|
|
elif is_refcounted(return_type):
|
2022-03-22 14:06:13 +00:00
|
|
|
if method["is_static"]:
|
2023-02-16 15:38:38 +00:00
|
|
|
method_call += f"return Ref<{return_type}>::_gde_internal_constructor(internal::_call_native_mb_ret_obj<{return_type}>(_gde_method_bind, nullptr"
|
2022-03-22 14:06:13 +00:00
|
|
|
else:
|
2023-02-16 15:38:38 +00:00
|
|
|
method_call += f"return Ref<{return_type}>::_gde_internal_constructor(internal::_call_native_mb_ret_obj<{return_type}>(_gde_method_bind, _owner"
|
2021-08-18 14:03:52 +00:00
|
|
|
is_ref = True
|
|
|
|
else:
|
2022-03-22 14:06:13 +00:00
|
|
|
if method["is_static"]:
|
2022-05-02 19:13:56 +00:00
|
|
|
method_call += (
|
2023-02-16 15:38:38 +00:00
|
|
|
f"return internal::_call_native_mb_ret_obj<{return_type}>(_gde_method_bind, nullptr"
|
2022-05-02 19:13:56 +00:00
|
|
|
)
|
2022-03-22 14:06:13 +00:00
|
|
|
else:
|
2022-05-02 19:13:56 +00:00
|
|
|
method_call += (
|
2023-02-16 15:38:38 +00:00
|
|
|
f"return internal::_call_native_mb_ret_obj<{return_type}>(_gde_method_bind, _owner"
|
2022-05-02 19:13:56 +00:00
|
|
|
)
|
2021-08-18 14:03:52 +00:00
|
|
|
else:
|
2022-03-25 12:13:45 +00:00
|
|
|
if method["is_static"]:
|
2023-02-16 15:38:38 +00:00
|
|
|
method_call += "internal::_call_native_mb_no_ret(_gde_method_bind, nullptr"
|
2022-03-25 12:13:45 +00:00
|
|
|
else:
|
2023-02-16 15:38:38 +00:00
|
|
|
method_call += "internal::_call_native_mb_no_ret(_gde_method_bind, _owner"
|
2021-08-18 14:03:52 +00:00
|
|
|
|
|
|
|
if "arguments" in method:
|
|
|
|
method_call += ", "
|
|
|
|
arguments = []
|
|
|
|
for argument in method["arguments"]:
|
|
|
|
(encode, arg_name) = get_encoded_arg(
|
|
|
|
argument["name"],
|
|
|
|
argument["type"],
|
|
|
|
argument["meta"] if "meta" in argument else None,
|
|
|
|
)
|
|
|
|
result += encode
|
|
|
|
arguments.append(arg_name)
|
|
|
|
method_call += ", ".join(arguments)
|
|
|
|
else: # vararg.
|
2022-12-07 13:11:44 +00:00
|
|
|
result.append("\tGDExtensionCallError error;")
|
2021-08-18 14:03:52 +00:00
|
|
|
result.append("\tVariant ret;")
|
2024-06-14 10:49:41 +00:00
|
|
|
method_call += "internal::gdextension_interface_object_method_bind_call(_gde_method_bind, _owner, reinterpret_cast<GDExtensionConstVariantPtr *>(p_args), p_arg_count, &ret, &error"
|
2021-08-18 14:03:52 +00:00
|
|
|
|
|
|
|
if is_ref:
|
|
|
|
method_call += ")" # Close Ref<> constructor.
|
|
|
|
method_call += ");"
|
|
|
|
result.append(method_call)
|
|
|
|
|
|
|
|
if vararg and ("return_value" in method and method["return_value"]["type"] != "void"):
|
2022-07-11 11:37:14 +00:00
|
|
|
return_type = get_enum_fullname(method["return_value"]["type"])
|
2022-03-29 11:44:07 +00:00
|
|
|
if return_type != "Variant":
|
|
|
|
result.append(f"\treturn VariantCaster<{return_type}>::cast(ret);")
|
|
|
|
else:
|
|
|
|
result.append("\treturn ret;")
|
2021-08-18 14:03:52 +00:00
|
|
|
|
|
|
|
result.append("}")
|
|
|
|
result.append("")
|
|
|
|
|
2021-08-19 23:03:11 +00:00
|
|
|
# Virtuals now.
|
|
|
|
for method in class_api["methods"]:
|
|
|
|
if not method["is_virtual"]:
|
|
|
|
continue
|
|
|
|
|
|
|
|
method_signature = make_signature(class_name, method, use_template_get_node=use_template_get_node)
|
|
|
|
method_signature += " {"
|
|
|
|
if "return_value" in method and correct_type(method["return_value"]["type"]) != "void":
|
|
|
|
result.append(method_signature)
|
|
|
|
result.append(f'\treturn {get_default_value_for_type(method["return_value"]["type"])};')
|
|
|
|
result.append("}")
|
|
|
|
else:
|
|
|
|
method_signature += "}"
|
|
|
|
result.append(method_signature)
|
|
|
|
result.append("")
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
result.append("")
|
|
|
|
result.append("} // namespace godot ")
|
|
|
|
|
|
|
|
return "\n".join(result)
|
|
|
|
|
|
|
|
|
|
|
|
def generate_global_constants(api, output_dir):
|
|
|
|
include_gen_folder = Path(output_dir) / "include" / "godot_cpp" / "classes"
|
|
|
|
source_gen_folder = Path(output_dir) / "src" / "classes"
|
|
|
|
|
|
|
|
include_gen_folder.mkdir(parents=True, exist_ok=True)
|
|
|
|
source_gen_folder.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
|
|
# Generate header
|
|
|
|
|
|
|
|
header = []
|
|
|
|
add_header("global_constants.hpp", header)
|
|
|
|
|
|
|
|
header_filename = include_gen_folder / "global_constants.hpp"
|
|
|
|
|
|
|
|
header_guard = "GODOT_CPP_GLOBAL_CONSTANTS_HPP"
|
|
|
|
header.append(f"#ifndef {header_guard}")
|
|
|
|
header.append(f"#define {header_guard}")
|
|
|
|
header.append("")
|
2023-11-27 23:16:46 +00:00
|
|
|
header.append("#include <cstdint>")
|
|
|
|
header.append("")
|
2021-08-18 14:03:52 +00:00
|
|
|
header.append("namespace godot {")
|
|
|
|
header.append("")
|
|
|
|
|
|
|
|
for constant in api["global_constants"]:
|
2024-01-10 13:30:24 +00:00
|
|
|
header.append(f'\tconst int64_t {escape_identifier(constant["name"])} = {constant["value"]};')
|
2021-08-18 14:03:52 +00:00
|
|
|
|
|
|
|
header.append("")
|
|
|
|
|
|
|
|
for enum_def in api["global_enums"]:
|
|
|
|
if enum_def["name"].startswith("Variant."):
|
|
|
|
continue
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2023-11-27 23:16:46 +00:00
|
|
|
if enum_def["is_bitfield"]:
|
|
|
|
header.append(f'\tenum {enum_def["name"]} : uint64_t {{')
|
|
|
|
else:
|
|
|
|
header.append(f'\tenum {enum_def["name"]} {{')
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
for value in enum_def["values"]:
|
|
|
|
header.append(f'\t\t{value["name"]} = {value["value"]},')
|
|
|
|
header.append("\t};")
|
|
|
|
header.append("")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
header.append("} // namespace godot")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
header.append("")
|
|
|
|
header.append(f"#endif // ! {header_guard}")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2023-06-16 07:38:01 +00:00
|
|
|
with header_filename.open("w+", encoding="utf-8") as header_file:
|
2021-08-18 14:03:52 +00:00
|
|
|
header_file.write("\n".join(header))
|
2019-03-26 23:51:51 +00:00
|
|
|
|
|
|
|
|
2023-07-26 22:16:46 +00:00
|
|
|
def generate_version_header(api, output_dir):
|
|
|
|
header = []
|
|
|
|
header_filename = "version.hpp"
|
|
|
|
add_header(header_filename, header)
|
|
|
|
|
|
|
|
include_gen_folder = Path(output_dir) / "include" / "godot_cpp" / "core"
|
|
|
|
include_gen_folder.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
|
|
header_file_path = include_gen_folder / header_filename
|
|
|
|
|
|
|
|
header_guard = "GODOT_CPP_VERSION_HPP"
|
|
|
|
header.append(f"#ifndef {header_guard}")
|
|
|
|
header.append(f"#define {header_guard}")
|
|
|
|
header.append("")
|
|
|
|
|
|
|
|
header.append(f"#define GODOT_VERSION_MAJOR {api['header']['version_major']}")
|
|
|
|
header.append(f"#define GODOT_VERSION_MINOR {api['header']['version_minor']}")
|
|
|
|
header.append(f"#define GODOT_VERSION_PATCH {api['header']['version_patch']}")
|
|
|
|
header.append(f"#define GODOT_VERSION_STATUS \"{api['header']['version_status']}\"")
|
|
|
|
header.append(f"#define GODOT_VERSION_BUILD \"{api['header']['version_build']}\"")
|
|
|
|
|
|
|
|
header.append("")
|
|
|
|
header.append(f"#endif // {header_guard}")
|
|
|
|
header.append("")
|
|
|
|
|
|
|
|
with header_file_path.open("w+", encoding="utf-8") as header_file:
|
|
|
|
header_file.write("\n".join(header))
|
|
|
|
|
|
|
|
|
2022-02-15 08:47:12 +00:00
|
|
|
def generate_global_constant_binds(api, output_dir):
|
|
|
|
include_gen_folder = Path(output_dir) / "include" / "godot_cpp" / "classes"
|
|
|
|
source_gen_folder = Path(output_dir) / "src" / "classes"
|
|
|
|
|
|
|
|
include_gen_folder.mkdir(parents=True, exist_ok=True)
|
|
|
|
source_gen_folder.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
|
|
# Generate header
|
|
|
|
|
|
|
|
header = []
|
|
|
|
add_header("global_constants_binds.hpp", header)
|
|
|
|
|
|
|
|
header_filename = include_gen_folder / "global_constants_binds.hpp"
|
|
|
|
|
|
|
|
header_guard = "GODOT_CPP_GLOBAL_CONSTANTS_BINDS_HPP"
|
|
|
|
header.append(f"#ifndef {header_guard}")
|
|
|
|
header.append(f"#define {header_guard}")
|
|
|
|
header.append("")
|
|
|
|
header.append("#include <godot_cpp/classes/global_constants.hpp>")
|
|
|
|
header.append("")
|
|
|
|
|
|
|
|
for enum_def in api["global_enums"]:
|
|
|
|
if enum_def["name"].startswith("Variant."):
|
|
|
|
continue
|
|
|
|
|
2023-02-02 08:38:34 +00:00
|
|
|
if enum_def["is_bitfield"]:
|
2024-01-07 07:24:02 +00:00
|
|
|
header.append(f'VARIANT_BITFIELD_CAST({enum_def["name"]});')
|
2023-02-02 08:38:34 +00:00
|
|
|
else:
|
2024-01-07 07:24:02 +00:00
|
|
|
header.append(f'VARIANT_ENUM_CAST({enum_def["name"]});')
|
2023-02-02 08:38:34 +00:00
|
|
|
|
|
|
|
# Variant::Type is not a global enum, but only one line, it is worth to place in this file instead of creating new file.
|
|
|
|
header.append(f"VARIANT_ENUM_CAST(godot::Variant::Type);")
|
2022-02-15 08:47:12 +00:00
|
|
|
|
|
|
|
header.append("")
|
|
|
|
|
|
|
|
header.append(f"#endif // ! {header_guard}")
|
|
|
|
|
2023-06-16 07:38:01 +00:00
|
|
|
with header_filename.open("w+", encoding="utf-8") as header_file:
|
2022-02-15 08:47:12 +00:00
|
|
|
header_file.write("\n".join(header))
|
|
|
|
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
def generate_utility_functions(api, output_dir):
|
|
|
|
include_gen_folder = Path(output_dir) / "include" / "godot_cpp" / "variant"
|
|
|
|
source_gen_folder = Path(output_dir) / "src" / "variant"
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
include_gen_folder.mkdir(parents=True, exist_ok=True)
|
|
|
|
source_gen_folder.mkdir(parents=True, exist_ok=True)
|
2018-02-11 14:50:01 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
# Generate header.
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
header = []
|
|
|
|
add_header("utility_functions.hpp", header)
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
header_filename = include_gen_folder / "utility_functions.hpp"
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
header_guard = "GODOT_CPP_UTILITY_FUNCTIONS_HPP"
|
|
|
|
header.append(f"#ifndef {header_guard}")
|
|
|
|
header.append(f"#define {header_guard}")
|
|
|
|
header.append("")
|
|
|
|
header.append("#include <godot_cpp/variant/builtin_types.hpp>")
|
|
|
|
header.append("#include <godot_cpp/variant/variant.hpp>")
|
|
|
|
header.append("")
|
|
|
|
header.append("#include <array>")
|
|
|
|
header.append("")
|
|
|
|
header.append("namespace godot {")
|
|
|
|
header.append("")
|
|
|
|
header.append("class UtilityFunctions {")
|
|
|
|
header.append("public:")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
for function in api["utility_functions"]:
|
|
|
|
vararg = "is_vararg" in function and function["is_vararg"]
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
function_signature = "\t"
|
2021-08-19 23:03:11 +00:00
|
|
|
function_signature += make_signature("UtilityFunctions", function, for_header=True, static=True)
|
|
|
|
header.append(function_signature + ";")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
if vararg:
|
|
|
|
# Add templated version.
|
2021-08-19 23:03:11 +00:00
|
|
|
header += make_varargs_template(function, static=True)
|
2017-05-12 19:53:07 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
header.append("};")
|
|
|
|
header.append("")
|
|
|
|
header.append("} // namespace godot")
|
|
|
|
header.append("")
|
|
|
|
header.append(f"#endif // ! {header_guard}")
|
2017-05-12 19:53:07 +00:00
|
|
|
|
2023-06-16 07:38:01 +00:00
|
|
|
with header_filename.open("w+", encoding="utf-8") as header_file:
|
2021-08-18 14:03:52 +00:00
|
|
|
header_file.write("\n".join(header))
|
2017-05-12 19:53:07 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
# Generate source.
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
source = []
|
2021-08-18 14:03:52 +00:00
|
|
|
add_header("utility_functions.cpp", source)
|
|
|
|
source_filename = source_gen_folder / "utility_functions.cpp"
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
source.append("#include <godot_cpp/variant/utility_functions.hpp>")
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append("")
|
2021-08-18 14:03:52 +00:00
|
|
|
source.append("#include <godot_cpp/core/error_macros.hpp>")
|
|
|
|
source.append("#include <godot_cpp/core/engine_ptrcall.hpp>")
|
2017-05-12 19:53:07 +00:00
|
|
|
source.append("")
|
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
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
for function in api["utility_functions"]:
|
|
|
|
vararg = "is_vararg" in function and function["is_vararg"]
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-19 23:03:11 +00:00
|
|
|
function_signature = make_signature("UtilityFunctions", function)
|
|
|
|
source.append(function_signature + " {")
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
# Function body.
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
source.append(
|
2023-07-13 01:38:46 +00:00
|
|
|
f'\tstatic GDExtensionPtrUtilityFunction _gde_function = internal::gdextension_interface_variant_get_ptr_utility_function(StringName("{function["name"]}")._native_ptr(), {function["hash"]});'
|
2021-08-18 14:03:52 +00:00
|
|
|
)
|
|
|
|
has_return = "return_type" in function and function["return_type"] != "void"
|
|
|
|
if has_return:
|
|
|
|
source.append(
|
2023-02-16 15:38:38 +00:00
|
|
|
f'\tCHECK_METHOD_BIND_RET(_gde_function, {get_default_value_for_type(function["return_type"])});'
|
2021-08-18 14:03:52 +00:00
|
|
|
)
|
|
|
|
else:
|
2023-02-16 15:38:38 +00:00
|
|
|
source.append("\tCHECK_METHOD_BIND(_gde_function);")
|
2021-08-18 14:03:52 +00:00
|
|
|
|
|
|
|
function_call = "\t"
|
|
|
|
if not vararg:
|
|
|
|
if has_return:
|
|
|
|
function_call += "return "
|
|
|
|
if function["return_type"] == "Object":
|
2023-02-16 15:38:38 +00:00
|
|
|
function_call += "internal::_call_utility_ret_obj(_gde_function"
|
2021-08-18 14:03:52 +00:00
|
|
|
else:
|
2023-02-16 15:38:38 +00:00
|
|
|
function_call += f'internal::_call_utility_ret<{get_gdextension_type(correct_type(function["return_type"]))}>(_gde_function'
|
2017-05-12 19:53:07 +00:00
|
|
|
else:
|
2023-02-16 15:38:38 +00:00
|
|
|
function_call += "internal::_call_utility_no_ret(_gde_function"
|
2021-08-18 14:03:52 +00:00
|
|
|
|
|
|
|
if "arguments" in function:
|
|
|
|
function_call += ", "
|
|
|
|
arguments = []
|
|
|
|
for argument in function["arguments"]:
|
|
|
|
(encode, arg_name) = get_encoded_arg(
|
|
|
|
argument["name"],
|
|
|
|
argument["type"],
|
|
|
|
argument["meta"] if "meta" in argument else None,
|
|
|
|
)
|
|
|
|
source += encode
|
|
|
|
arguments.append(arg_name)
|
|
|
|
function_call += ", ".join(arguments)
|
|
|
|
else:
|
2023-05-30 20:35:05 +00:00
|
|
|
if has_return:
|
|
|
|
source.append(f'\t{get_gdextension_type(correct_type(function["return_type"]))} ret;')
|
|
|
|
else:
|
|
|
|
source.append("\tVariant ret;")
|
2024-06-14 10:49:41 +00:00
|
|
|
function_call += "_gde_function(&ret, reinterpret_cast<GDExtensionConstVariantPtr *>(p_args), p_arg_count"
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
function_call += ");"
|
|
|
|
source.append(function_call)
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
if vararg and has_return:
|
|
|
|
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
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
source.append("} // namespace godot")
|
2018-02-11 14:50:01 +00:00
|
|
|
|
2023-06-16 07:38:01 +00:00
|
|
|
with source_filename.open("w+", encoding="utf-8") as source_file:
|
2021-08-18 14:03:52 +00:00
|
|
|
source_file.write("\n".join(source))
|
2018-02-11 14:50:01 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
# Helper functions.
|
2017-05-12 19:53:07 +00:00
|
|
|
|
2018-02-11 14:50:01 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
def camel_to_snake(name):
|
|
|
|
name = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", name)
|
|
|
|
name = re.sub("([a-z0-9])([A-Z])", r"\1_\2", name)
|
|
|
|
return name.replace("2_D", "2D").replace("3_D", "3D").lower()
|
2017-05-12 19:53:07 +00:00
|
|
|
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
def make_function_parameters(parameters, include_default=False, for_builtin=False, is_vararg=False):
|
|
|
|
signature = []
|
2017-05-12 19:53:07 +00:00
|
|
|
|
2021-08-19 23:03:11 +00:00
|
|
|
for index, par in enumerate(parameters):
|
2021-08-18 14:03:52 +00:00
|
|
|
parameter = type_for_parameter(par["type"], par["meta"] if "meta" in par else None)
|
2024-06-14 10:49:41 +00:00
|
|
|
parameter_name = escape_argument(par["name"])
|
2021-08-19 23:03:11 +00:00
|
|
|
if len(parameter_name) == 0:
|
2024-06-14 10:49:41 +00:00
|
|
|
parameter_name = "p_arg_" + str(index + 1)
|
2021-08-19 23:03:11 +00:00
|
|
|
parameter += parameter_name
|
2017-05-12 19:53:07 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
if include_default and "default_value" in par and (not for_builtin or par["type"] != "Variant"):
|
|
|
|
parameter += " = "
|
|
|
|
if is_enum(par["type"]):
|
2021-08-19 23:03:11 +00:00
|
|
|
parameter_type = correct_type(par["type"])
|
|
|
|
if parameter_type == "void":
|
|
|
|
parameter_type = "Variant"
|
2021-08-24 13:50:36 +00:00
|
|
|
parameter += f"({parameter_type})"
|
2021-08-18 14:03:52 +00:00
|
|
|
parameter += correct_default_value(par["default_value"], par["type"])
|
|
|
|
signature.append(parameter)
|
2017-05-12 19:53:07 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
if is_vararg:
|
2024-06-14 10:49:41 +00:00
|
|
|
signature.append("const Args&... p_args")
|
2017-05-12 19:53:07 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
return ", ".join(signature)
|
2017-05-12 19:53:07 +00:00
|
|
|
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
def type_for_parameter(type_name, meta=None):
|
2021-08-19 23:03:11 +00:00
|
|
|
if type_name == "void":
|
|
|
|
return "Variant "
|
|
|
|
elif is_pod_type(type_name) and type_name != "Nil" or is_enum(type_name):
|
2021-08-18 14:03:52 +00:00
|
|
|
return f"{correct_type(type_name, meta)} "
|
|
|
|
elif is_variant(type_name) or is_refcounted(type_name):
|
|
|
|
return f"const {correct_type(type_name)} &"
|
|
|
|
else:
|
|
|
|
return f"{correct_type(type_name)}"
|
2017-05-12 19:53:07 +00:00
|
|
|
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
def get_include_path(type_name):
|
|
|
|
base_dir = ""
|
|
|
|
if type_name == "Object":
|
|
|
|
base_dir = "core"
|
|
|
|
elif is_variant(type_name):
|
|
|
|
base_dir = "variant"
|
|
|
|
else:
|
|
|
|
base_dir = "classes"
|
2017-05-12 19:53:07 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
return f"{base_dir}/{camel_to_snake(type_name)}.hpp"
|
2017-05-12 19:53:07 +00:00
|
|
|
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
def get_encoded_arg(arg_name, type_name, type_meta):
|
|
|
|
result = []
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2024-06-14 10:49:41 +00:00
|
|
|
name = escape_argument(arg_name)
|
2021-08-18 14:03:52 +00:00
|
|
|
arg_type = correct_type(type_name)
|
|
|
|
if is_pod_type(arg_type):
|
2022-12-07 13:11:44 +00:00
|
|
|
result.append(f"\t{get_gdextension_type(arg_type)} {name}_encoded;")
|
2021-08-28 13:41:50 +00:00
|
|
|
result.append(f"\tPtrToArg<{correct_type(type_name)}>::encode({name}, &{name}_encoded);")
|
2021-08-18 14:03:52 +00:00
|
|
|
name = f"&{name}_encoded"
|
|
|
|
elif is_engine_class(type_name):
|
2022-09-23 20:20:37 +00:00
|
|
|
# `{name}` is a C++ wrapper, it contains a field which is the object's pointer Godot expects.
|
|
|
|
# We have to check `nullptr` because when the caller sends `nullptr`, the wrapper itself will be null.
|
2023-05-23 20:17:06 +00:00
|
|
|
name = f"({name} != nullptr ? &{name}->_owner : nullptr)"
|
2021-08-18 14:03:52 +00:00
|
|
|
else:
|
|
|
|
name = f"&{name}"
|
2017-05-12 19:53:07 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
return (result, name)
|
2017-05-12 19:53:07 +00:00
|
|
|
|
|
|
|
|
2021-08-24 13:50:36 +00:00
|
|
|
def make_signature(
|
|
|
|
class_name, function_data, for_header=False, use_template_get_node=True, for_builtin=False, static=False
|
|
|
|
):
|
2021-08-19 23:03:11 +00:00
|
|
|
function_signature = ""
|
|
|
|
|
|
|
|
is_vararg = "is_vararg" in function_data and function_data["is_vararg"]
|
|
|
|
|
|
|
|
if for_header:
|
|
|
|
if "is_virtual" in function_data and function_data["is_virtual"]:
|
|
|
|
function_signature += "virtual "
|
|
|
|
|
|
|
|
if is_vararg:
|
|
|
|
function_signature += "private: "
|
|
|
|
|
|
|
|
if static:
|
|
|
|
function_signature += "static "
|
|
|
|
|
|
|
|
return_type = "void"
|
|
|
|
return_meta = None
|
|
|
|
if "return_type" in function_data:
|
|
|
|
return_type = correct_type(function_data["return_type"])
|
|
|
|
elif "return_value" in function_data:
|
|
|
|
return_type = function_data["return_value"]["type"]
|
|
|
|
return_meta = function_data["return_value"]["meta"] if "meta" in function_data["return_value"] else None
|
|
|
|
|
|
|
|
function_signature += correct_type(
|
|
|
|
return_type,
|
|
|
|
return_meta,
|
|
|
|
)
|
|
|
|
|
|
|
|
if not function_signature.endswith("*"):
|
|
|
|
function_signature += " "
|
|
|
|
|
|
|
|
if not for_header:
|
|
|
|
function_signature += f"{class_name}::"
|
|
|
|
|
|
|
|
function_signature += escape_identifier(function_data["name"])
|
|
|
|
|
|
|
|
if is_vararg or (
|
|
|
|
not for_builtin and use_template_get_node and class_name == "Node" and function_data["name"] == "get_node"
|
|
|
|
):
|
|
|
|
function_signature += "_internal"
|
|
|
|
|
|
|
|
function_signature += "("
|
|
|
|
|
|
|
|
arguments = function_data["arguments"] if "arguments" in function_data else []
|
|
|
|
|
|
|
|
if not is_vararg:
|
|
|
|
function_signature += make_function_parameters(arguments, for_header, for_builtin, is_vararg)
|
|
|
|
else:
|
2024-06-14 10:49:41 +00:00
|
|
|
function_signature += "const Variant **p_args, GDExtensionInt p_arg_count"
|
2021-08-19 23:03:11 +00:00
|
|
|
|
|
|
|
function_signature += ")"
|
|
|
|
|
2022-03-22 14:06:13 +00:00
|
|
|
if "is_static" in function_data and function_data["is_static"] and for_header:
|
|
|
|
function_signature = "static " + function_signature
|
|
|
|
elif "is_const" in function_data and function_data["is_const"]:
|
2021-08-19 23:03:11 +00:00
|
|
|
function_signature += " const"
|
|
|
|
|
|
|
|
return function_signature
|
|
|
|
|
|
|
|
|
2023-04-14 07:33:49 +00:00
|
|
|
def make_varargs_template(
|
|
|
|
function_data,
|
|
|
|
static=False,
|
|
|
|
class_befor_signature="",
|
|
|
|
with_public_declare=True,
|
|
|
|
with_indent=True,
|
|
|
|
for_builtin_classes=False,
|
|
|
|
):
|
2021-08-19 23:03:11 +00:00
|
|
|
result = []
|
|
|
|
|
2023-04-14 07:33:49 +00:00
|
|
|
function_signature = ""
|
|
|
|
|
|
|
|
if with_public_declare:
|
|
|
|
function_signature = "public: "
|
|
|
|
|
2024-03-27 12:54:05 +00:00
|
|
|
function_signature += "template <typename... Args> "
|
2021-08-19 23:03:11 +00:00
|
|
|
|
|
|
|
if static:
|
|
|
|
function_signature += "static "
|
|
|
|
|
|
|
|
return_type = "void"
|
|
|
|
return_meta = None
|
|
|
|
if "return_type" in function_data:
|
|
|
|
return_type = correct_type(function_data["return_type"])
|
|
|
|
elif "return_value" in function_data:
|
|
|
|
return_type = function_data["return_value"]["type"]
|
|
|
|
return_meta = function_data["return_value"]["meta"] if "meta" in function_data["return_value"] else None
|
|
|
|
|
|
|
|
function_signature += correct_type(
|
|
|
|
return_type,
|
|
|
|
return_meta,
|
|
|
|
)
|
|
|
|
|
|
|
|
if not function_signature.endswith("*"):
|
|
|
|
function_signature += " "
|
|
|
|
|
2023-04-14 07:33:49 +00:00
|
|
|
if len(class_befor_signature) > 0:
|
|
|
|
function_signature += class_befor_signature + "::"
|
2021-08-19 23:03:11 +00:00
|
|
|
function_signature += f'{escape_identifier(function_data["name"])}'
|
|
|
|
|
|
|
|
method_arguments = []
|
|
|
|
if "arguments" in function_data:
|
|
|
|
method_arguments = function_data["arguments"]
|
|
|
|
|
|
|
|
function_signature += "("
|
|
|
|
|
|
|
|
is_vararg = "is_vararg" in function_data and function_data["is_vararg"]
|
|
|
|
|
|
|
|
function_signature += make_function_parameters(method_arguments, include_default=True, is_vararg=is_vararg)
|
|
|
|
|
|
|
|
function_signature += ")"
|
|
|
|
|
|
|
|
if "is_const" in function_data and function_data["is_const"]:
|
|
|
|
function_signature += " const"
|
|
|
|
|
|
|
|
function_signature += " {"
|
|
|
|
result.append(function_signature)
|
|
|
|
|
2023-04-14 07:33:49 +00:00
|
|
|
args_array = f"\tstd::array<Variant, {len(method_arguments)} + sizeof...(Args)> variant_args {{ "
|
2021-08-19 23:03:11 +00:00
|
|
|
for argument in method_arguments:
|
|
|
|
if argument["type"] == "Variant":
|
2024-06-14 10:49:41 +00:00
|
|
|
args_array += escape_argument(argument["name"])
|
2021-08-19 23:03:11 +00:00
|
|
|
else:
|
2024-06-14 10:49:41 +00:00
|
|
|
args_array += f'Variant({escape_argument(argument["name"])})'
|
2021-08-19 23:03:11 +00:00
|
|
|
args_array += ", "
|
|
|
|
|
2024-06-14 10:49:41 +00:00
|
|
|
args_array += "Variant(p_args)... };"
|
2021-08-19 23:03:11 +00:00
|
|
|
result.append(args_array)
|
2023-04-14 07:33:49 +00:00
|
|
|
result.append(f"\tstd::array<const Variant *, {len(method_arguments)} + sizeof...(Args)> call_args;")
|
2024-03-27 12:54:05 +00:00
|
|
|
result.append("\tfor (size_t i = 0; i < variant_args.size(); i++) {")
|
2023-04-14 07:33:49 +00:00
|
|
|
result.append("\t\tcall_args[i] = &variant_args[i];")
|
|
|
|
result.append("\t}")
|
2021-08-19 23:03:11 +00:00
|
|
|
|
2023-04-14 07:33:49 +00:00
|
|
|
call_line = "\t"
|
2021-08-19 23:03:11 +00:00
|
|
|
|
2023-04-14 07:33:49 +00:00
|
|
|
if not for_builtin_classes:
|
|
|
|
if return_type != "void":
|
|
|
|
call_line += "return "
|
2021-08-19 23:03:11 +00:00
|
|
|
|
2023-04-14 07:33:49 +00:00
|
|
|
call_line += f'{escape_identifier(function_data["name"])}_internal(call_args.data(), variant_args.size());'
|
|
|
|
result.append(call_line)
|
|
|
|
else:
|
|
|
|
base = "(GDExtensionTypePtr)&opaque"
|
|
|
|
if static:
|
|
|
|
base = "nullptr"
|
|
|
|
|
|
|
|
ret = "nullptr"
|
|
|
|
if return_type != "void":
|
|
|
|
ret = "&ret"
|
|
|
|
result.append(f'\t{correct_type(function_data["return_type"])} ret;')
|
|
|
|
|
|
|
|
function_name = function_data["name"]
|
|
|
|
result.append(
|
|
|
|
f"\t_method_bindings.method_{function_name}({base}, reinterpret_cast<GDExtensionConstTypePtr *>(call_args.data()), {ret}, {len(method_arguments)} + sizeof...(Args));"
|
|
|
|
)
|
|
|
|
|
|
|
|
if return_type != "void":
|
|
|
|
result.append("\treturn ret;")
|
|
|
|
|
|
|
|
result.append("}")
|
|
|
|
|
|
|
|
if with_indent:
|
|
|
|
for i in range(len(result)):
|
|
|
|
result[i] = "\t" + result[i]
|
2021-08-19 23:03:11 +00:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
# Engine idiosyncrasies.
|
2017-05-12 19:53:07 +00:00
|
|
|
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
def is_pod_type(type_name):
|
|
|
|
"""
|
|
|
|
Those are types for which no class should be generated.
|
|
|
|
"""
|
|
|
|
return type_name in [
|
|
|
|
"Nil",
|
|
|
|
"void",
|
|
|
|
"bool",
|
2022-03-16 18:33:06 +00:00
|
|
|
"real_t",
|
|
|
|
"float",
|
2021-08-28 13:41:50 +00:00
|
|
|
"double",
|
2022-03-16 18:33:06 +00:00
|
|
|
"int",
|
|
|
|
"int8_t",
|
|
|
|
"uint8_t",
|
|
|
|
"int16_t",
|
|
|
|
"uint16_t",
|
2021-08-28 13:41:50 +00:00
|
|
|
"int32_t",
|
|
|
|
"int64_t",
|
|
|
|
"uint32_t",
|
|
|
|
"uint64_t",
|
2021-08-18 14:03:52 +00:00
|
|
|
]
|
2019-03-26 23:51:51 +00:00
|
|
|
|
2021-09-08 18:11:12 +00:00
|
|
|
|
2021-09-01 03:11:10 +00:00
|
|
|
def is_included_type(type_name):
|
2022-10-05 06:03:52 +00:00
|
|
|
# Types which we already have implemented.
|
|
|
|
return is_included_struct_type(type_name) or type_name in ["ObjectID"]
|
|
|
|
|
|
|
|
|
|
|
|
def is_included_struct_type(type_name):
|
|
|
|
# Struct types which we already have implemented.
|
2021-09-01 03:11:10 +00:00
|
|
|
return type_name in [
|
|
|
|
"AABB",
|
|
|
|
"Basis",
|
|
|
|
"Color",
|
|
|
|
"Plane",
|
2022-10-05 06:03:52 +00:00
|
|
|
"Projection",
|
2021-09-01 03:11:10 +00:00
|
|
|
"Quaternion",
|
|
|
|
"Rect2",
|
|
|
|
"Rect2i",
|
|
|
|
"Transform2D",
|
|
|
|
"Transform3D",
|
|
|
|
"Vector2",
|
|
|
|
"Vector2i",
|
|
|
|
"Vector3",
|
|
|
|
"Vector3i",
|
2022-07-20 20:49:08 +00:00
|
|
|
"Vector4",
|
|
|
|
"Vector4i",
|
2021-09-01 03:11:10 +00:00
|
|
|
]
|
|
|
|
|
2021-09-08 18:11:12 +00:00
|
|
|
|
2021-09-01 03:11:10 +00:00
|
|
|
def is_packed_array(type_name):
|
|
|
|
"""
|
|
|
|
Those are types for which we add our extra packed array functions.
|
|
|
|
"""
|
|
|
|
return type_name in [
|
|
|
|
"PackedByteArray",
|
|
|
|
"PackedColorArray",
|
|
|
|
"PackedFloat32Array",
|
|
|
|
"PackedFloat64Array",
|
|
|
|
"PackedInt32Array",
|
|
|
|
"PackedInt64Array",
|
|
|
|
"PackedStringArray",
|
|
|
|
"PackedVector2Array",
|
|
|
|
"PackedVector3Array",
|
|
|
|
]
|
|
|
|
|
2022-02-26 20:45:00 +00:00
|
|
|
|
2021-11-18 03:05:14 +00:00
|
|
|
def needs_copy_instead_of_move(type_name):
|
|
|
|
"""
|
|
|
|
Those are types which need initialised data or we'll get warning spam so need a copy instead of move.
|
|
|
|
"""
|
|
|
|
return type_name in [
|
|
|
|
"Dictionary",
|
|
|
|
]
|
2017-05-12 19:53:07 +00:00
|
|
|
|
2022-02-26 20:45:00 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
def is_enum(type_name):
|
2022-07-11 11:37:14 +00:00
|
|
|
return type_name.startswith("enum::") or type_name.startswith("bitfield::")
|
|
|
|
|
|
|
|
|
|
|
|
def is_bitfield(type_name):
|
|
|
|
return type_name.startswith("bitfield::")
|
2017-05-12 19:53:07 +00:00
|
|
|
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
def get_enum_class(enum_name: str):
|
|
|
|
if "." in enum_name:
|
2022-07-11 11:37:14 +00:00
|
|
|
if is_bitfield(enum_name):
|
|
|
|
return enum_name.replace("bitfield::", "").split(".")[0]
|
|
|
|
else:
|
|
|
|
return enum_name.replace("enum::", "").split(".")[0]
|
2021-08-18 14:03:52 +00:00
|
|
|
else:
|
|
|
|
return "GlobalConstants"
|
2017-05-12 19:53:07 +00:00
|
|
|
|
|
|
|
|
2022-07-11 11:37:14 +00:00
|
|
|
def get_enum_fullname(enum_name: str):
|
|
|
|
if is_bitfield(enum_name):
|
|
|
|
return enum_name.replace("bitfield::", "BitField<") + ">"
|
|
|
|
else:
|
|
|
|
return enum_name.replace("enum::", "")
|
|
|
|
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
def get_enum_name(enum_name: str):
|
2022-07-11 11:37:14 +00:00
|
|
|
if is_bitfield(enum_name):
|
|
|
|
return enum_name.replace("bitfield::", "").split(".")[-1]
|
|
|
|
else:
|
|
|
|
return enum_name.replace("enum::", "").split(".")[-1]
|
2017-05-12 19:53:07 +00:00
|
|
|
|
2017-09-11 19:34:20 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
def is_variant(type_name):
|
2022-09-15 07:33:07 +00:00
|
|
|
return (
|
|
|
|
type_name == "Variant"
|
|
|
|
or type_name in builtin_classes
|
|
|
|
or type_name == "Nil"
|
|
|
|
or type_name.startswith("typedarray::")
|
|
|
|
)
|
2017-09-11 19:34:20 +00:00
|
|
|
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
def is_engine_class(type_name):
|
2021-08-24 13:50:36 +00:00
|
|
|
global engine_classes
|
|
|
|
return type_name == "Object" or type_name in engine_classes
|
2017-09-11 19:34:20 +00:00
|
|
|
|
2017-05-12 19:53:07 +00:00
|
|
|
|
2022-10-05 06:03:52 +00:00
|
|
|
def is_struct_type(type_name):
|
|
|
|
# This is used to determine which keyword to use for forward declarations.
|
2022-02-16 09:29:01 +00:00
|
|
|
global native_structures
|
2022-10-05 06:03:52 +00:00
|
|
|
return is_included_struct_type(type_name) or type_name in native_structures
|
2022-02-16 09:29:01 +00:00
|
|
|
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
def is_refcounted(type_name):
|
|
|
|
return type_name in engine_classes and engine_classes[type_name]
|
2017-05-12 19:53:07 +00:00
|
|
|
|
|
|
|
|
2023-07-07 19:49:56 +00:00
|
|
|
def is_class_included(class_name, build_profile):
|
|
|
|
"""
|
|
|
|
Check if an engine class should be included.
|
|
|
|
This removes classes according to a build profile of enabled or disabled classes.
|
|
|
|
"""
|
|
|
|
included = build_profile.get("enabled_classes", [])
|
|
|
|
excluded = build_profile.get("disabled_classes", [])
|
|
|
|
if included:
|
|
|
|
return class_name in included
|
|
|
|
if excluded:
|
|
|
|
return class_name not in excluded
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
def is_included(type_name, current_type):
|
|
|
|
"""
|
|
|
|
Check if a builtin type should be included.
|
|
|
|
This removes Variant and POD types from inclusion, and the current type.
|
|
|
|
"""
|
2022-09-15 07:33:07 +00:00
|
|
|
if type_name.startswith("typedarray::"):
|
|
|
|
return True
|
2021-08-18 14:03:52 +00:00
|
|
|
to_include = get_enum_class(type_name) if is_enum(type_name) else type_name
|
2021-08-24 13:50:36 +00:00
|
|
|
if to_include == current_type or is_pod_type(to_include):
|
|
|
|
return False
|
|
|
|
if to_include == "GlobalConstants" or to_include == "UtilityFunctions":
|
|
|
|
return True
|
|
|
|
return is_engine_class(to_include) or is_variant(to_include)
|
2017-05-12 19:53:07 +00:00
|
|
|
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
def correct_default_value(value, type_name):
|
|
|
|
value_map = {
|
|
|
|
"null": "nullptr",
|
|
|
|
'""': "String()",
|
|
|
|
'&""': "StringName()",
|
2024-06-13 15:17:33 +00:00
|
|
|
'^""': "NodePath()",
|
2021-08-18 14:03:52 +00:00
|
|
|
"[]": "Array()",
|
|
|
|
"{}": "Dictionary()",
|
|
|
|
"Transform2D(1, 0, 0, 1, 0, 0)": "Transform2D()", # Default transform.
|
|
|
|
"Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0)": "Transform3D()", # Default transform.
|
|
|
|
}
|
|
|
|
if value in value_map:
|
|
|
|
return value_map[value]
|
|
|
|
if value == "":
|
|
|
|
return f"{type_name}()"
|
2023-01-28 17:01:27 +00:00
|
|
|
if value.startswith("Array["):
|
|
|
|
return f"{{}}"
|
2024-06-13 14:57:23 +00:00
|
|
|
if value.startswith("&"):
|
|
|
|
return value[1::]
|
2024-06-13 15:17:33 +00:00
|
|
|
if value.startswith("^"):
|
|
|
|
return value[1::]
|
2021-08-18 14:03:52 +00:00
|
|
|
return value
|
|
|
|
|
|
|
|
|
2022-09-15 07:33:07 +00:00
|
|
|
def correct_typed_array(type_name):
|
|
|
|
if type_name.startswith("typedarray::"):
|
|
|
|
return type_name.replace("typedarray::", "TypedArray<") + ">"
|
|
|
|
return type_name
|
|
|
|
|
|
|
|
|
2024-04-23 18:37:43 +00:00
|
|
|
def correct_type(type_name, meta=None, use_alias=True):
|
2021-08-18 14:03:52 +00:00
|
|
|
type_conversion = {"float": "double", "int": "int64_t", "Nil": "Variant"}
|
|
|
|
if meta != None:
|
|
|
|
if "int" in meta:
|
|
|
|
return f"{meta}_t"
|
2021-09-04 11:38:25 +00:00
|
|
|
elif meta in type_conversion:
|
|
|
|
return type_conversion[type_name]
|
2021-08-18 14:03:52 +00:00
|
|
|
else:
|
|
|
|
return meta
|
|
|
|
if type_name in type_conversion:
|
|
|
|
return type_conversion[type_name]
|
2022-09-15 07:33:07 +00:00
|
|
|
if type_name.startswith("typedarray::"):
|
|
|
|
return type_name.replace("typedarray::", "TypedArray<") + ">"
|
2021-08-18 14:03:52 +00:00
|
|
|
if is_enum(type_name):
|
2022-07-11 11:37:14 +00:00
|
|
|
if is_bitfield(type_name):
|
|
|
|
base_class = get_enum_class(type_name)
|
2024-04-23 18:37:43 +00:00
|
|
|
if use_alias and base_class in CLASS_ALIASES:
|
|
|
|
base_class = CLASS_ALIASES[base_class]
|
2022-07-11 11:37:14 +00:00
|
|
|
if base_class == "GlobalConstants":
|
|
|
|
return f"BitField<{get_enum_name(type_name)}>"
|
|
|
|
return f"BitField<{base_class}::{get_enum_name(type_name)}>"
|
|
|
|
else:
|
|
|
|
base_class = get_enum_class(type_name)
|
2024-04-23 18:37:43 +00:00
|
|
|
if use_alias and base_class in CLASS_ALIASES:
|
|
|
|
base_class = CLASS_ALIASES[base_class]
|
2022-07-11 11:37:14 +00:00
|
|
|
if base_class == "GlobalConstants":
|
|
|
|
return f"{get_enum_name(type_name)}"
|
|
|
|
return f"{base_class}::{get_enum_name(type_name)}"
|
2021-08-18 14:03:52 +00:00
|
|
|
if is_refcounted(type_name):
|
|
|
|
return f"Ref<{type_name}>"
|
|
|
|
if type_name == "Object" or is_engine_class(type_name):
|
|
|
|
return f"{type_name} *"
|
2024-03-27 12:54:05 +00:00
|
|
|
if type_name.endswith("*") and not type_name.endswith("**") and not type_name.endswith(" *"):
|
2021-08-24 13:50:36 +00:00
|
|
|
return f"{type_name[:-1]} *"
|
2021-08-18 14:03:52 +00:00
|
|
|
return type_name
|
|
|
|
|
|
|
|
|
2022-12-07 13:11:44 +00:00
|
|
|
def get_gdextension_type(type_name):
|
2021-08-18 14:03:52 +00:00
|
|
|
type_conversion_map = {
|
2022-03-09 10:37:48 +00:00
|
|
|
"bool": "int8_t",
|
|
|
|
"uint8_t": "int64_t",
|
|
|
|
"int8_t": "int64_t",
|
|
|
|
"uint16_t": "int64_t",
|
|
|
|
"int16_t": "int64_t",
|
|
|
|
"uint32_t": "int64_t",
|
|
|
|
"int32_t": "int64_t",
|
2021-08-18 14:03:52 +00:00
|
|
|
"int": "int64_t",
|
|
|
|
"float": "double",
|
|
|
|
}
|
2017-05-12 19:53:07 +00:00
|
|
|
|
2022-07-11 11:37:14 +00:00
|
|
|
if type_name.startswith("BitField<"):
|
|
|
|
return "int64_t"
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
if type_name in type_conversion_map:
|
|
|
|
return type_conversion_map[type_name]
|
|
|
|
return type_name
|
|
|
|
|
|
|
|
|
|
|
|
def escape_identifier(id):
|
|
|
|
cpp_keywords_map = {
|
|
|
|
"class": "_class",
|
|
|
|
"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",
|
2021-08-18 14:03:52 +00:00
|
|
|
"new": "new_",
|
2019-02-04 01:55:16 +00:00
|
|
|
"operator": "_operator",
|
2021-08-18 14:03:52 +00:00
|
|
|
"typeof": "type_of",
|
|
|
|
"typename": "type_name",
|
2023-07-07 02:16:27 +00:00
|
|
|
"enum": "_enum",
|
2021-08-18 14:03:52 +00:00
|
|
|
}
|
|
|
|
if id in cpp_keywords_map:
|
|
|
|
return cpp_keywords_map[id]
|
|
|
|
return id
|
|
|
|
|
|
|
|
|
2024-06-14 10:49:41 +00:00
|
|
|
def escape_argument(id):
|
|
|
|
if id.startswith("p_") or id.startswith("r_"):
|
|
|
|
return id
|
|
|
|
return "p_" + id
|
|
|
|
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
def get_operator_id_name(op):
|
|
|
|
op_id_map = {
|
|
|
|
"==": "equal",
|
|
|
|
"!=": "not_equal",
|
|
|
|
"<": "less",
|
|
|
|
"<=": "less_equal",
|
|
|
|
">": "greater",
|
|
|
|
">=": "greater_equal",
|
|
|
|
"+": "add",
|
|
|
|
"-": "subtract",
|
|
|
|
"*": "multiply",
|
|
|
|
"/": "divide",
|
|
|
|
"unary-": "negate",
|
|
|
|
"unary+": "positive",
|
|
|
|
"%": "module",
|
2024-01-06 19:54:57 +00:00
|
|
|
"**": "power",
|
2021-08-18 14:03:52 +00:00
|
|
|
"<<": "shift_left",
|
|
|
|
">>": "shift_right",
|
|
|
|
"&": "bit_and",
|
|
|
|
"|": "bit_or",
|
|
|
|
"^": "bit_xor",
|
|
|
|
"~": "bit_negate",
|
|
|
|
"and": "and",
|
|
|
|
"or": "or",
|
|
|
|
"xor": "xor",
|
|
|
|
"not": "not",
|
|
|
|
"and": "and",
|
|
|
|
"in": "in",
|
2017-05-12 19:53:07 +00:00
|
|
|
}
|
2021-08-18 14:03:52 +00:00
|
|
|
return op_id_map[op]
|
|
|
|
|
|
|
|
|
|
|
|
def get_default_value_for_type(type_name):
|
|
|
|
if type_name == "int":
|
|
|
|
return "0"
|
|
|
|
if type_name == "float":
|
|
|
|
return "0.0"
|
|
|
|
if type_name == "bool":
|
|
|
|
return "false"
|
2022-09-15 07:33:07 +00:00
|
|
|
if type_name.startswith("typedarray::"):
|
|
|
|
return f"{correct_type(type_name)}()"
|
2021-08-18 14:03:52 +00:00
|
|
|
if is_enum(type_name):
|
|
|
|
return f"{correct_type(type_name)}(0)"
|
|
|
|
if is_variant(type_name):
|
|
|
|
return f"{type_name}()"
|
|
|
|
if is_refcounted(type_name):
|
|
|
|
return f"Ref<{type_name}>()"
|
|
|
|
return "nullptr"
|
|
|
|
|
|
|
|
|
|
|
|
header = """\
|
2023-01-10 15:14:30 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* $filename */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2021-08-18 14:03:52 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
def add_header(filename, lines):
|
|
|
|
desired_length = len(header.split("\n")[0])
|
|
|
|
pad_spaces = desired_length - 6 - len(filename)
|
|
|
|
|
|
|
|
for num, line in enumerate(header.split("\n")):
|
|
|
|
if num == 1:
|
|
|
|
new_line = f"/* {filename}{' ' * pad_spaces}*/"
|
|
|
|
lines.append(new_line)
|
|
|
|
else:
|
|
|
|
lines.append(line)
|
|
|
|
|
|
|
|
lines.append("// THIS FILE IS GENERATED. EDITS WILL BE LOST.")
|
|
|
|
lines.append("")
|