2023-12-05 02:48:44 +01:00
|
|
|
proto = """#define GDVIRTUAL$VER($RET m_name $ARG)\\
|
|
|
|
StringName _gdvirtual_##m_name##_sn = #m_name;\\
|
|
|
|
mutable bool _gdvirtual_##m_name##_initialized = false;\\
|
|
|
|
mutable void *_gdvirtual_##m_name = nullptr;\\
|
|
|
|
template <bool required>\\
|
|
|
|
_FORCE_INLINE_ bool _gdvirtual_##m_name##_call($CALLARGS) $CONST {\\
|
|
|
|
ScriptInstance *_script_instance = ((Object *)(this))->get_script_instance();\\
|
|
|
|
if (_script_instance) {\\
|
|
|
|
Callable::CallError ce;\\
|
|
|
|
$CALLSIARGS\\
|
|
|
|
$CALLSIBEGIN_script_instance->callp(_gdvirtual_##m_name##_sn, $CALLSIARGPASS, ce);\\
|
|
|
|
if (ce.error == Callable::CallError::CALL_OK) {\\
|
|
|
|
$CALLSIRET\\
|
|
|
|
return true;\\
|
|
|
|
}\\
|
|
|
|
}\\
|
|
|
|
if (unlikely(_get_extension() && !_gdvirtual_##m_name##_initialized)) {\\
|
|
|
|
_gdvirtual_##m_name = nullptr;\\
|
|
|
|
if (_get_extension()->get_virtual_call_data && _get_extension()->call_virtual_with_data) {\\
|
|
|
|
_gdvirtual_##m_name = _get_extension()->get_virtual_call_data(_get_extension()->class_userdata, &_gdvirtual_##m_name##_sn);\\
|
|
|
|
} else if (_get_extension()->get_virtual) {\\
|
|
|
|
_gdvirtual_##m_name = (void *)_get_extension()->get_virtual(_get_extension()->class_userdata, &_gdvirtual_##m_name##_sn);\\
|
|
|
|
}\\
|
|
|
|
GDVIRTUAL_TRACK(_gdvirtual_##m_name, _gdvirtual_##m_name##_initialized);\\
|
|
|
|
_gdvirtual_##m_name##_initialized = true;\\
|
|
|
|
}\\
|
|
|
|
if (_gdvirtual_##m_name) {\\
|
|
|
|
$CALLPTRARGS\\
|
|
|
|
$CALLPTRRETDEF\\
|
|
|
|
if (_get_extension()->get_virtual_call_data && _get_extension()->call_virtual_with_data) {\\
|
|
|
|
_get_extension()->call_virtual_with_data(_get_extension_instance(), &_gdvirtual_##m_name##_sn, _gdvirtual_##m_name, $CALLPTRARGPASS, $CALLPTRRETPASS);\\
|
|
|
|
$CALLPTRRET\\
|
|
|
|
} else {\\
|
|
|
|
((GDExtensionClassCallVirtual)_gdvirtual_##m_name)(_get_extension_instance(), $CALLPTRARGPASS, $CALLPTRRETPASS);\\
|
|
|
|
$CALLPTRRET\\
|
|
|
|
}\\
|
2021-06-19 17:58:49 +02:00
|
|
|
return true;\\
|
2023-12-05 02:48:44 +01:00
|
|
|
}\\
|
|
|
|
if (required) {\\
|
|
|
|
ERR_PRINT_ONCE("Required virtual method " + get_class() + "::" + #m_name + " must be overridden before calling.");\\
|
|
|
|
$RVOID\\
|
|
|
|
}\\
|
|
|
|
return false;\\
|
2021-06-19 17:58:49 +02:00
|
|
|
}\\
|
2023-12-05 02:48:44 +01:00
|
|
|
_FORCE_INLINE_ bool _gdvirtual_##m_name##_overridden() const {\\
|
|
|
|
ScriptInstance *_script_instance = ((Object *)(this))->get_script_instance();\\
|
|
|
|
if (_script_instance && _script_instance->has_method(_gdvirtual_##m_name##_sn)) {\\
|
|
|
|
return true;\\
|
|
|
|
}\\
|
|
|
|
if (unlikely(_get_extension() && !_gdvirtual_##m_name##_initialized)) {\\
|
|
|
|
_gdvirtual_##m_name = nullptr;\\
|
|
|
|
if (_get_extension()->get_virtual_call_data && _get_extension()->call_virtual_with_data) {\\
|
|
|
|
_gdvirtual_##m_name = _get_extension()->get_virtual_call_data(_get_extension()->class_userdata, &_gdvirtual_##m_name##_sn);\\
|
|
|
|
} else if (_get_extension()->get_virtual) {\\
|
|
|
|
_gdvirtual_##m_name = (void *)_get_extension()->get_virtual(_get_extension()->class_userdata, &_gdvirtual_##m_name##_sn);\\
|
|
|
|
}\\
|
|
|
|
GDVIRTUAL_TRACK(_gdvirtual_##m_name, _gdvirtual_##m_name##_initialized);\\
|
|
|
|
_gdvirtual_##m_name##_initialized = true;\\
|
|
|
|
}\\
|
|
|
|
if (_gdvirtual_##m_name) {\\
|
|
|
|
return true;\\
|
|
|
|
}\\
|
|
|
|
return false;\\
|
2021-08-22 03:52:44 +02:00
|
|
|
}\\
|
2023-12-05 02:48:44 +01:00
|
|
|
_FORCE_INLINE_ static MethodInfo _gdvirtual_##m_name##_get_method_info() {\\
|
|
|
|
MethodInfo method_info;\\
|
|
|
|
method_info.name = #m_name;\\
|
|
|
|
method_info.flags = $METHOD_FLAGS;\\
|
|
|
|
$FILL_METHOD_INFO\\
|
|
|
|
return method_info;\\
|
|
|
|
}
|
2021-06-19 17:58:49 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
def generate_version(argcount, const=False, returns=False):
|
|
|
|
s = proto
|
|
|
|
sproto = str(argcount)
|
|
|
|
method_info = ""
|
|
|
|
if returns:
|
|
|
|
sproto += "R"
|
2023-12-05 02:48:44 +01:00
|
|
|
s = s.replace("$RET", "m_ret,")
|
2022-03-14 15:52:03 +01:00
|
|
|
s = s.replace("$RVOID", "(void)r_ret;") # If required, may lead to uninitialized errors
|
2021-06-19 17:58:49 +02:00
|
|
|
s = s.replace("$CALLPTRRETDEF", "PtrToArg<m_ret>::EncodeT ret;")
|
2023-12-05 02:48:44 +01:00
|
|
|
method_info += "method_info.return_val = GetTypeInfo<m_ret>::get_class_info();\\\n"
|
|
|
|
method_info += "\t\tmethod_info.return_val_metadata = GetTypeInfo<m_ret>::METADATA;"
|
2021-06-19 17:58:49 +02:00
|
|
|
else:
|
2023-12-05 02:48:44 +01:00
|
|
|
s = s.replace("$RET ", "")
|
|
|
|
s = s.replace("\t\t\t$RVOID\\\n", "")
|
|
|
|
s = s.replace("\t\t\t$CALLPTRRETDEF\\\n", "")
|
2021-06-19 17:58:49 +02:00
|
|
|
|
|
|
|
if const:
|
|
|
|
sproto += "C"
|
|
|
|
s = s.replace("$CONST", "const")
|
2023-12-05 02:48:44 +01:00
|
|
|
s = s.replace("$METHOD_FLAGS", "METHOD_FLAG_VIRTUAL | METHOD_FLAG_CONST")
|
2021-06-19 17:58:49 +02:00
|
|
|
else:
|
2023-12-05 02:48:44 +01:00
|
|
|
s = s.replace("$CONST ", "")
|
|
|
|
s = s.replace("$METHOD_FLAGS", "METHOD_FLAG_VIRTUAL")
|
2021-06-19 17:58:49 +02:00
|
|
|
|
|
|
|
s = s.replace("$VER", sproto)
|
|
|
|
argtext = ""
|
|
|
|
callargtext = ""
|
|
|
|
callsiargs = ""
|
|
|
|
callsiargptrs = ""
|
|
|
|
callptrargsptr = ""
|
|
|
|
if argcount > 0:
|
|
|
|
argtext += ", "
|
2023-12-05 02:48:44 +01:00
|
|
|
callsiargs = f"Variant vargs[{argcount}] = {{ "
|
|
|
|
callsiargptrs = f"\t\t\tconst Variant *vargptrs[{argcount}] = {{ "
|
|
|
|
callptrargsptr = f"\t\t\tGDExtensionConstTypePtr argptrs[{argcount}] = {{ "
|
2021-06-19 17:58:49 +02:00
|
|
|
callptrargs = ""
|
|
|
|
for i in range(argcount):
|
|
|
|
if i > 0:
|
|
|
|
argtext += ", "
|
|
|
|
callargtext += ", "
|
|
|
|
callsiargs += ", "
|
|
|
|
callsiargptrs += ", "
|
2023-12-05 02:48:44 +01:00
|
|
|
callptrargs += "\t\t\t"
|
2021-06-19 17:58:49 +02:00
|
|
|
callptrargsptr += ", "
|
2023-12-05 02:48:44 +01:00
|
|
|
argtext += f"m_type{i + 1}"
|
|
|
|
callargtext += f"m_type{i + 1} arg{i + 1}"
|
|
|
|
callsiargs += f"Variant(arg{i + 1})"
|
|
|
|
callsiargptrs += f"&vargs[{i}]"
|
2023-12-05 15:34:22 +01:00
|
|
|
callptrargs += (
|
|
|
|
f"PtrToArg<m_type{i + 1}>::EncodeT argval{i + 1} = (PtrToArg<m_type{i + 1}>::EncodeT)arg{i + 1};\\\n"
|
|
|
|
)
|
2023-12-05 02:48:44 +01:00
|
|
|
callptrargsptr += f"&argval{i + 1}"
|
|
|
|
if method_info:
|
|
|
|
method_info += "\\\n\t\t"
|
|
|
|
method_info += f"method_info.arguments.push_back(GetTypeInfo<m_type{i + 1}>::get_class_info());\\\n"
|
|
|
|
method_info += f"\t\tmethod_info.arguments_metadata.push_back(GetTypeInfo<m_type{i + 1}>::METADATA);"
|
2021-06-19 17:58:49 +02:00
|
|
|
|
|
|
|
if argcount:
|
2023-12-05 02:48:44 +01:00
|
|
|
callsiargs += " };\\\n"
|
|
|
|
callsiargptrs += " };"
|
2021-06-19 17:58:49 +02:00
|
|
|
s = s.replace("$CALLSIARGS", callsiargs + callsiargptrs)
|
2023-12-05 02:48:44 +01:00
|
|
|
s = s.replace("$CALLSIARGPASS", f"(const Variant **)vargptrs, {argcount}")
|
|
|
|
callptrargsptr += " };"
|
2021-06-19 17:58:49 +02:00
|
|
|
s = s.replace("$CALLPTRARGS", callptrargs + callptrargsptr)
|
2023-12-05 02:48:44 +01:00
|
|
|
s = s.replace("$CALLPTRARGPASS", "reinterpret_cast<GDExtensionConstTypePtr *>(argptrs)")
|
2021-06-19 17:58:49 +02:00
|
|
|
else:
|
2023-12-05 02:48:44 +01:00
|
|
|
s = s.replace("\t\t\t$CALLSIARGS\\\n", "")
|
2021-06-19 17:58:49 +02:00
|
|
|
s = s.replace("$CALLSIARGPASS", "nullptr, 0")
|
2023-12-05 02:48:44 +01:00
|
|
|
s = s.replace("\t\t\t$CALLPTRARGS\\\n", "")
|
2021-06-19 17:58:49 +02:00
|
|
|
s = s.replace("$CALLPTRARGPASS", "nullptr")
|
|
|
|
|
|
|
|
if returns:
|
|
|
|
if argcount > 0:
|
2023-12-05 02:48:44 +01:00
|
|
|
callargtext += ", "
|
|
|
|
callargtext += "m_ret &r_ret"
|
2021-06-19 17:58:49 +02:00
|
|
|
s = s.replace("$CALLSIBEGIN", "Variant ret = ")
|
2022-02-03 14:03:51 +01:00
|
|
|
s = s.replace("$CALLSIRET", "r_ret = VariantCaster<m_ret>::cast(ret);")
|
2021-06-19 17:58:49 +02:00
|
|
|
s = s.replace("$CALLPTRRETPASS", "&ret")
|
2022-02-03 14:03:51 +01:00
|
|
|
s = s.replace("$CALLPTRRET", "r_ret = (m_ret)ret;")
|
2021-06-19 17:58:49 +02:00
|
|
|
else:
|
|
|
|
s = s.replace("$CALLSIBEGIN", "")
|
2023-12-05 02:48:44 +01:00
|
|
|
s = s.replace("\t\t\t\t$CALLSIRET\\\n", "")
|
2021-06-19 17:58:49 +02:00
|
|
|
s = s.replace("$CALLPTRRETPASS", "nullptr")
|
2023-12-05 02:48:44 +01:00
|
|
|
s = s.replace("\t\t\t\t$CALLPTRRET\\\n", "")
|
2021-06-19 17:58:49 +02:00
|
|
|
|
2023-12-05 02:48:44 +01:00
|
|
|
s = s.replace(" $ARG", argtext)
|
2021-06-19 17:58:49 +02:00
|
|
|
s = s.replace("$CALLARGS", callargtext)
|
2023-12-05 02:48:44 +01:00
|
|
|
if method_info:
|
|
|
|
s = s.replace("$FILL_METHOD_INFO", method_info)
|
|
|
|
else:
|
|
|
|
s = s.replace("\t\t$FILL_METHOD_INFO\\\n", method_info)
|
2021-06-19 17:58:49 +02:00
|
|
|
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
|
|
|
def run(target, source, env):
|
|
|
|
max_versions = 12
|
|
|
|
|
2023-12-05 02:48:44 +01:00
|
|
|
txt = """/* THIS FILE IS GENERATED DO NOT EDIT */
|
2021-06-19 17:58:49 +02:00
|
|
|
#ifndef GDVIRTUAL_GEN_H
|
|
|
|
#define GDVIRTUAL_GEN_H
|
|
|
|
|
2023-09-06 21:02:52 +02:00
|
|
|
#include "core/object/script_instance.h"
|
|
|
|
|
2023-08-05 03:34:14 +02:00
|
|
|
#ifdef TOOLS_ENABLED
|
2023-12-05 02:48:44 +01:00
|
|
|
#define GDVIRTUAL_TRACK(m_virtual, m_initialized)\\
|
|
|
|
if (_get_extension()->reloadable) {\\
|
|
|
|
VirtualMethodTracker *tracker = memnew(VirtualMethodTracker);\\
|
|
|
|
tracker->method = (void **)&m_virtual;\\
|
|
|
|
tracker->initialized = &m_initialized;\\
|
|
|
|
tracker->next = virtual_method_list;\\
|
|
|
|
virtual_method_list = tracker;\\
|
|
|
|
}
|
2023-08-05 03:34:14 +02:00
|
|
|
#else
|
|
|
|
#define GDVIRTUAL_TRACK(m_virtual, m_initialized)
|
|
|
|
#endif
|
2021-06-19 17:58:49 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
for i in range(max_versions + 1):
|
2023-12-05 02:48:44 +01:00
|
|
|
txt += f"/* {i} Arguments */\n\n"
|
2021-06-19 17:58:49 +02:00
|
|
|
txt += generate_version(i, False, False)
|
|
|
|
txt += generate_version(i, False, True)
|
|
|
|
txt += generate_version(i, True, False)
|
|
|
|
txt += generate_version(i, True, True)
|
|
|
|
|
2023-12-05 02:48:44 +01:00
|
|
|
txt += "#endif // GDVIRTUAL_GEN_H\n"
|
2021-06-19 17:58:49 +02:00
|
|
|
|
2024-03-11 19:05:37 +01:00
|
|
|
with open(str(target[0]), "w", encoding="utf-8", newline="\n") as f:
|
2021-06-19 17:58:49 +02:00
|
|
|
f.write(txt)
|