2021-08-02 16:34:47 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* Godot.hpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
2017-05-13 11:55:04 +00:00
|
|
|
#ifndef GODOT_HPP
|
|
|
|
#define GODOT_HPP
|
2017-03-06 17:39:56 +00:00
|
|
|
|
|
|
|
#include <cstdlib>
|
2017-05-23 21:03:57 +00:00
|
|
|
#include <cstring>
|
2017-03-06 17:39:56 +00:00
|
|
|
|
2017-10-20 23:42:10 +00:00
|
|
|
#include <gdnative_api_struct.gen.h>
|
2017-10-03 10:37:34 +00:00
|
|
|
#include <nativescript/godot_nativescript.h>
|
2018-02-11 14:50:01 +00:00
|
|
|
#include <typeinfo>
|
2017-03-06 17:39:56 +00:00
|
|
|
|
2018-02-23 11:56:36 +00:00
|
|
|
#include "CoreTypes.hpp"
|
|
|
|
#include "Ref.hpp"
|
2018-02-11 14:50:01 +00:00
|
|
|
#include "TagDB.hpp"
|
2018-11-23 22:09:41 +00:00
|
|
|
#include "Variant.hpp"
|
2017-03-06 17:39:56 +00:00
|
|
|
|
2018-02-23 11:56:36 +00:00
|
|
|
#include "Object.hpp"
|
2017-04-11 01:04:29 +00:00
|
|
|
|
2018-02-23 11:56:36 +00:00
|
|
|
#include "GodotGlobal.hpp"
|
2017-05-13 11:55:04 +00:00
|
|
|
|
2017-03-06 17:39:56 +00:00
|
|
|
namespace godot {
|
2020-05-30 18:20:13 +00:00
|
|
|
namespace detail {
|
2017-03-06 17:39:56 +00:00
|
|
|
|
2020-05-30 18:20:13 +00:00
|
|
|
// Godot classes are wrapped by heap-allocated instances mimicking them through the C API.
|
|
|
|
// They all inherit `_Wrapped`.
|
2018-11-23 22:09:41 +00:00
|
|
|
template <class T>
|
2020-05-30 18:20:13 +00:00
|
|
|
T *get_wrapper(godot_object *obj) {
|
|
|
|
return (T *)godot::nativescript_1_1_api->godot_nativescript_get_instance_binding_data(godot::_RegisterState::language_index, obj);
|
|
|
|
}
|
2017-03-06 17:39:56 +00:00
|
|
|
|
2020-05-30 18:20:13 +00:00
|
|
|
// Custom class instances are not obtainable by just casting the pointer to the base class they inherit,
|
|
|
|
// partly because in Godot, scripts are not instances of the classes themselves, they are only attached to them.
|
|
|
|
// Yet we want to "fake" it as if they were the same entity.
|
2018-11-23 22:09:41 +00:00
|
|
|
template <class T>
|
2020-05-30 18:20:13 +00:00
|
|
|
T *get_custom_class_instance(const Object *obj) {
|
2018-11-24 15:36:27 +00:00
|
|
|
return (obj) ? (T *)godot::nativescript_api->godot_nativescript_get_userdata(obj->_owner) : nullptr;
|
2018-01-14 22:03:57 +00:00
|
|
|
}
|
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
template <class T>
|
2020-05-30 18:20:13 +00:00
|
|
|
inline T *create_custom_class_instance() {
|
|
|
|
// Usually, script instances hold a reference to their NativeScript resource.
|
|
|
|
// that resource is obtained from a `.gdns` file, which in turn exists because
|
|
|
|
// of the resource system of Godot. We can't cleanly hardcode that here,
|
|
|
|
// so the easiest for now (though not really clean) is to create new resource instances,
|
|
|
|
// individually attached to the script instances.
|
|
|
|
|
|
|
|
// We cannot use wrappers because of https://github.com/godotengine/godot/issues/39181
|
|
|
|
// godot::NativeScript *script = godot::NativeScript::_new();
|
|
|
|
// script->set_library(get_wrapper<godot::GDNativeLibrary>((godot_object *)godot::gdnlib));
|
2020-09-12 16:38:46 +00:00
|
|
|
// script->set_class_name(T::___get_class_name());
|
|
|
|
|
|
|
|
static_assert(T::___CLASS_IS_SCRIPT, "This function must only be used on custom classes");
|
2020-05-30 18:20:13 +00:00
|
|
|
|
|
|
|
// So we use the C API directly.
|
|
|
|
static godot_class_constructor script_constructor = godot::api->godot_get_class_constructor("NativeScript");
|
|
|
|
static godot_method_bind *mb_set_library = godot::api->godot_method_bind_get_method("NativeScript", "set_library");
|
|
|
|
static godot_method_bind *mb_set_class_name = godot::api->godot_method_bind_get_method("NativeScript", "set_class_name");
|
|
|
|
godot_object *script = script_constructor();
|
|
|
|
{
|
|
|
|
const void *args[] = { godot::gdnlib };
|
|
|
|
godot::api->godot_method_bind_ptrcall(mb_set_library, script, args, nullptr);
|
|
|
|
}
|
|
|
|
{
|
2020-09-12 16:38:46 +00:00
|
|
|
const String class_name = T::___get_class_name();
|
2020-05-30 18:20:13 +00:00
|
|
|
const void *args[] = { &class_name };
|
|
|
|
godot::api->godot_method_bind_ptrcall(mb_set_class_name, script, args, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now to instanciate T, we initially did this, however in case of Reference it returns a variant with refcount
|
|
|
|
// already initialized, which woud cause inconsistent behavior compared to other classes (we still have to return a pointer).
|
|
|
|
//Variant instance_variant = script->new_();
|
|
|
|
//T *instance = godot::get_custom_class_instance<T>(instance_variant);
|
|
|
|
|
|
|
|
// So we should do this instead, however while convenient, it uses unnecessary wrapper objects.
|
|
|
|
// Object *base_obj = T::___new_godot_base();
|
|
|
|
// base_obj->set_script(script);
|
|
|
|
// return get_custom_class_instance<T>(base_obj);
|
|
|
|
|
|
|
|
// Again using the C API to do exactly what we have to do.
|
2020-09-12 16:38:46 +00:00
|
|
|
static godot_class_constructor base_constructor = godot::api->godot_get_class_constructor(T::___get_godot_class_name());
|
2020-05-30 18:20:13 +00:00
|
|
|
static godot_method_bind *mb_set_script = godot::api->godot_method_bind_get_method("Object", "set_script");
|
|
|
|
godot_object *base_obj = base_constructor();
|
|
|
|
{
|
|
|
|
const void *args[] = { script };
|
|
|
|
godot::api->godot_method_bind_ptrcall(mb_set_script, base_obj, args, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (T *)godot::nativescript_api->godot_nativescript_get_userdata(base_obj);
|
2018-02-11 14:50:01 +00:00
|
|
|
}
|
2017-05-13 11:55:04 +00:00
|
|
|
|
2020-05-30 18:20:13 +00:00
|
|
|
} // namespace detail
|
|
|
|
|
2020-09-12 16:38:46 +00:00
|
|
|
// Used in the definition of a custom class.
|
|
|
|
//
|
|
|
|
// Name: Name of your class, without namespace
|
|
|
|
// Base: Name of the direct base class, with namespace if necessary
|
|
|
|
//
|
|
|
|
// ___get_class_name: Name of the class
|
|
|
|
// ___get_godot_class_name: Name of the Godot base class this class inherits from (i.e not direct)
|
|
|
|
// _new: Creates a new instance of the class
|
|
|
|
// ___get_id: Gets the unique ID of the class. Godot and custom classes are both within that set.
|
|
|
|
// ___get_base_id: Gets the ID of the direct base class, as returned by ___get_id
|
|
|
|
// ___get_base_class_name: Name of the direct base class
|
|
|
|
// ___get_from_variant: Converts a Variant into an Object*. Will be non-null if the class matches.
|
|
|
|
#define GODOT_CLASS(Name, Base) \
|
|
|
|
\
|
|
|
|
public: \
|
|
|
|
inline static const char *___get_class_name() { return #Name; } \
|
|
|
|
enum { ___CLASS_IS_SCRIPT = 1 }; \
|
|
|
|
inline static const char *___get_godot_class_name() { \
|
|
|
|
return Base::___get_godot_class_name(); \
|
|
|
|
} \
|
|
|
|
inline static Name *_new() { \
|
|
|
|
return godot::detail::create_custom_class_instance<Name>(); \
|
|
|
|
} \
|
|
|
|
inline static size_t ___get_id() { return typeid(Name).hash_code(); } \
|
|
|
|
inline static size_t ___get_base_id() { return Base::___get_id(); } \
|
|
|
|
inline static const char *___get_base_class_name() { return Base::___get_class_name(); } \
|
|
|
|
inline static godot::Object *___get_from_variant(godot::Variant a) { \
|
|
|
|
return (godot::Object *)godot::detail::get_custom_class_instance<Name>( \
|
|
|
|
godot::Object::___get_from_variant(a)); \
|
|
|
|
} \
|
|
|
|
\
|
2018-11-23 22:09:41 +00:00
|
|
|
private:
|
|
|
|
|
2020-09-12 16:38:46 +00:00
|
|
|
// Legacy compatibility
|
|
|
|
#define GODOT_SUBCLASS(Name, Base) GODOT_CLASS(Name, Base)
|
2018-11-23 22:09:41 +00:00
|
|
|
|
|
|
|
template <class T>
|
2017-04-06 00:32:24 +00:00
|
|
|
struct _ArgCast {
|
2018-11-23 22:09:41 +00:00
|
|
|
static T _arg_cast(Variant a) {
|
2017-10-03 08:29:08 +00:00
|
|
|
return a;
|
2017-04-06 00:32:24 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
template <class T>
|
|
|
|
struct _ArgCast<T *> {
|
|
|
|
static T *_arg_cast(Variant a) {
|
|
|
|
return (T *)T::___get_from_variant(a);
|
2017-04-06 00:32:24 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
template <>
|
2017-06-19 10:05:38 +00:00
|
|
|
struct _ArgCast<Variant> {
|
2018-11-23 22:09:41 +00:00
|
|
|
static Variant _arg_cast(Variant a) {
|
2017-06-19 10:05:38 +00:00
|
|
|
return a;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-03-06 17:39:56 +00:00
|
|
|
// instance and destroy funcs
|
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
template <class T>
|
2020-02-23 02:13:37 +00:00
|
|
|
void *_godot_class_instance_func(godot_object *p, void * /*method_data*/) {
|
2017-05-13 11:55:04 +00:00
|
|
|
T *d = new T();
|
2018-02-11 14:50:01 +00:00
|
|
|
d->_owner = p;
|
|
|
|
d->_type_tag = typeid(T).hash_code();
|
2017-03-06 17:39:56 +00:00
|
|
|
d->_init();
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
template <class T>
|
2020-02-23 02:13:37 +00:00
|
|
|
void _godot_class_destroy_func(godot_object * /*p*/, void * /*method_data*/, void *data) {
|
2018-11-23 22:09:41 +00:00
|
|
|
T *d = (T *)data;
|
2017-03-06 17:39:56 +00:00
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
template <class T>
|
|
|
|
void register_class() {
|
2020-09-12 16:38:46 +00:00
|
|
|
static_assert(T::___CLASS_IS_SCRIPT, "This function must only be used on custom classes");
|
|
|
|
|
2017-03-14 00:12:55 +00:00
|
|
|
godot_instance_create_func create = {};
|
|
|
|
create.create_func = _godot_class_instance_func<T>;
|
|
|
|
|
|
|
|
godot_instance_destroy_func destroy = {};
|
|
|
|
destroy.destroy_func = _godot_class_destroy_func<T>;
|
|
|
|
|
2018-11-27 22:08:30 +00:00
|
|
|
_TagDB::register_type(T::___get_id(), T::___get_base_id());
|
2017-03-14 00:12:55 +00:00
|
|
|
|
2020-09-12 16:38:46 +00:00
|
|
|
godot::nativescript_api->godot_nativescript_register_class(godot::_RegisterState::nativescript_handle,
|
|
|
|
T::___get_class_name(), T::___get_base_class_name(), create, destroy);
|
|
|
|
|
|
|
|
godot::nativescript_1_1_api->godot_nativescript_set_type_tag(godot::_RegisterState::nativescript_handle,
|
|
|
|
T::___get_class_name(), (const void *)T::___get_id());
|
|
|
|
|
2017-03-06 17:39:56 +00:00
|
|
|
T::_register_methods();
|
|
|
|
}
|
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
template <class T>
|
|
|
|
void register_tool_class() {
|
2020-09-12 16:38:46 +00:00
|
|
|
static_assert(T::___CLASS_IS_SCRIPT, "This function must only be used on custom classes");
|
|
|
|
|
2017-03-18 18:01:11 +00:00
|
|
|
godot_instance_create_func create = {};
|
|
|
|
create.create_func = _godot_class_instance_func<T>;
|
|
|
|
|
|
|
|
godot_instance_destroy_func destroy = {};
|
|
|
|
destroy.destroy_func = _godot_class_destroy_func<T>;
|
|
|
|
|
2018-11-27 22:08:30 +00:00
|
|
|
_TagDB::register_type(T::___get_id(), T::___get_base_id());
|
2017-03-18 18:01:11 +00:00
|
|
|
|
2020-09-12 16:38:46 +00:00
|
|
|
godot::nativescript_api->godot_nativescript_register_tool_class(godot::_RegisterState::nativescript_handle,
|
|
|
|
T::___get_class_name(), T::___get_base_class_name(), create, destroy);
|
|
|
|
|
|
|
|
godot::nativescript_1_1_api->godot_nativescript_set_type_tag(godot::_RegisterState::nativescript_handle,
|
|
|
|
T::___get_class_name(), (const void *)T::___get_id());
|
|
|
|
|
2017-03-18 18:01:11 +00:00
|
|
|
T::_register_methods();
|
|
|
|
}
|
|
|
|
|
2017-03-14 00:12:55 +00:00
|
|
|
// method registering
|
|
|
|
|
|
|
|
typedef godot_variant (*__godot_wrapper_method)(godot_object *, void *, void *, int, godot_variant **);
|
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
template <class T, class R, class... args>
|
2021-01-31 22:21:31 +00:00
|
|
|
const char *___get_method_class_name(R (T::*p)(args... a)) {
|
2020-09-12 16:38:46 +00:00
|
|
|
static_assert(T::___CLASS_IS_SCRIPT, "This function must only be used on custom classes");
|
2021-01-31 22:21:31 +00:00
|
|
|
(void)p; // To avoid "unused parameter" warnings. `p` is required for template matching.
|
2020-09-12 16:38:46 +00:00
|
|
|
return T::___get_class_name();
|
2017-03-14 00:12:55 +00:00
|
|
|
}
|
|
|
|
|
2021-01-31 22:21:31 +00:00
|
|
|
// This second version is also required to match constant functions
|
2018-11-23 22:09:41 +00:00
|
|
|
template <class T, class R, class... args>
|
2021-01-31 22:21:31 +00:00
|
|
|
const char *___get_method_class_name(R (T::*p)(args... a) const) {
|
2020-09-12 16:38:46 +00:00
|
|
|
static_assert(T::___CLASS_IS_SCRIPT, "This function must only be used on custom classes");
|
2021-01-31 22:21:31 +00:00
|
|
|
(void)p; // To avoid "unused parameter" warnings. `p` is required for template matching.
|
2020-09-12 16:38:46 +00:00
|
|
|
return T::___get_class_name();
|
2017-06-21 00:26:06 +00:00
|
|
|
}
|
|
|
|
|
2017-07-23 14:13:55 +00:00
|
|
|
// Okay, time for some template magic.
|
|
|
|
// Many thanks to manpat from the GDL Discord Server.
|
2017-03-14 00:12:55 +00:00
|
|
|
|
2017-07-23 14:13:55 +00:00
|
|
|
// This is stuff that's available in C++14 I think, but whatever.
|
2017-03-14 00:12:55 +00:00
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
template <int... I>
|
|
|
|
struct __Sequence {};
|
2017-03-14 00:12:55 +00:00
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
template <int N, int... I>
|
2017-07-23 14:13:55 +00:00
|
|
|
struct __construct_sequence {
|
2018-11-23 22:09:41 +00:00
|
|
|
using type = typename __construct_sequence<N - 1, N - 1, I...>::type;
|
2017-03-06 17:39:56 +00:00
|
|
|
};
|
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
template <int... I>
|
2017-07-23 14:13:55 +00:00
|
|
|
struct __construct_sequence<0, I...> {
|
|
|
|
using type = __Sequence<I...>;
|
2017-03-06 17:39:56 +00:00
|
|
|
};
|
|
|
|
|
2017-07-23 14:13:55 +00:00
|
|
|
// Now the wrapping part.
|
2018-11-23 22:09:41 +00:00
|
|
|
template <class T, class R, class... As>
|
2017-07-23 14:13:55 +00:00
|
|
|
struct _WrappedMethod {
|
2018-11-23 22:09:41 +00:00
|
|
|
R(T::*f)
|
|
|
|
(As...);
|
2017-03-14 00:12:55 +00:00
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
template <int... I>
|
|
|
|
void apply(Variant *ret, T *obj, Variant **args, __Sequence<I...>) {
|
|
|
|
*ret = (obj->*f)(_ArgCast<As>::_arg_cast(*args[I])...);
|
2017-03-06 17:39:56 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
template <class T, class... As>
|
2017-07-23 14:13:55 +00:00
|
|
|
struct _WrappedMethod<T, void, As...> {
|
|
|
|
void (T::*f)(As...);
|
2017-03-14 00:12:55 +00:00
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
template <int... I>
|
2020-02-23 02:13:37 +00:00
|
|
|
void apply(Variant * /*ret*/, T *obj, Variant **args, __Sequence<I...>) {
|
2018-11-23 22:09:41 +00:00
|
|
|
(obj->*f)(_ArgCast<As>::_arg_cast(*args[I])...);
|
2017-03-06 17:39:56 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
template <class T, class R, class... As>
|
2020-02-23 02:13:37 +00:00
|
|
|
godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int /*num_args*/, godot_variant **args) {
|
2017-07-23 14:13:55 +00:00
|
|
|
godot_variant v;
|
2017-10-20 23:42:10 +00:00
|
|
|
godot::api->godot_variant_new_nil(&v);
|
2017-03-14 00:12:55 +00:00
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
T *obj = (T *)user_data;
|
|
|
|
_WrappedMethod<T, R, As...> *method = (_WrappedMethod<T, R, As...> *)method_data;
|
2017-03-14 00:12:55 +00:00
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
Variant *var = (Variant *)&v;
|
|
|
|
Variant **arg = (Variant **)args;
|
2017-03-14 00:12:55 +00:00
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
method->apply(var, obj, arg, typename __construct_sequence<sizeof...(As)>::type{});
|
2017-03-14 00:12:55 +00:00
|
|
|
|
2017-07-23 14:13:55 +00:00
|
|
|
return v;
|
2017-03-14 00:12:55 +00:00
|
|
|
}
|
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
template <class T, class R, class... As>
|
|
|
|
void *___make_wrapper_function(R (T::*f)(As...)) {
|
2017-07-23 14:13:55 +00:00
|
|
|
using MethodType = _WrappedMethod<T, R, As...>;
|
2018-11-23 22:09:41 +00:00
|
|
|
MethodType *p = (MethodType *)godot::api->godot_alloc(sizeof(MethodType));
|
2017-03-14 00:12:55 +00:00
|
|
|
p->f = f;
|
2018-11-23 22:09:41 +00:00
|
|
|
return (void *)p;
|
2017-03-14 00:12:55 +00:00
|
|
|
}
|
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
template <class T, class R, class... As>
|
2020-02-23 02:13:37 +00:00
|
|
|
__godot_wrapper_method ___get_wrapper_function(R (T::* /*f*/)(As...)) {
|
2018-11-23 22:09:41 +00:00
|
|
|
return (__godot_wrapper_method)&__wrapped_method<T, R, As...>;
|
2017-03-14 00:12:55 +00:00
|
|
|
}
|
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
template <class T, class R, class... A>
|
|
|
|
void *___make_wrapper_function(R (T::*f)(A...) const) {
|
|
|
|
return ___make_wrapper_function((R(T::*)(A...))f);
|
2017-06-21 00:26:06 +00:00
|
|
|
}
|
2017-03-06 17:39:56 +00:00
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
template <class T, class R, class... A>
|
|
|
|
__godot_wrapper_method ___get_wrapper_function(R (T::*f)(A...) const) {
|
|
|
|
return ___get_wrapper_function((R(T::*)(A...))f);
|
2017-06-21 00:26:06 +00:00
|
|
|
}
|
2017-03-06 17:39:56 +00:00
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
template <class M>
|
|
|
|
void register_method(const char *name, M method_ptr, godot_method_rpc_mode rpc_type = GODOT_METHOD_RPC_MODE_DISABLED) {
|
2017-03-14 00:12:55 +00:00
|
|
|
godot_instance_method method = {};
|
|
|
|
method.method_data = ___make_wrapper_function(method_ptr);
|
2017-10-20 23:42:10 +00:00
|
|
|
method.free_func = godot::api->godot_free;
|
2018-11-23 22:09:41 +00:00
|
|
|
method.method = (__godot_wrapper_method)___get_wrapper_function(method_ptr);
|
2017-03-14 00:12:55 +00:00
|
|
|
|
|
|
|
godot_method_attributes attr = {};
|
|
|
|
attr.rpc_type = rpc_type;
|
|
|
|
|
2020-09-12 16:38:46 +00:00
|
|
|
godot::nativescript_api->godot_nativescript_register_method(godot::_RegisterState::nativescript_handle,
|
|
|
|
___get_method_class_name(method_ptr), name, attr, method);
|
2017-03-14 00:12:55 +00:00
|
|
|
}
|
|
|
|
|
2019-06-10 22:24:16 +00:00
|
|
|
// User can specify a derived class D to register the method for, instead of it being inferred.
|
|
|
|
template <class D, class B, class R, class... As>
|
2020-09-12 16:38:46 +00:00
|
|
|
void register_method_explicit(const char *name, R (B::*method_ptr)(As...),
|
|
|
|
godot_method_rpc_mode rpc_type = GODOT_METHOD_RPC_MODE_DISABLED) {
|
2019-06-10 22:24:16 +00:00
|
|
|
static_assert(std::is_base_of<B, D>::value, "Explicit class must derive from method class");
|
|
|
|
register_method(name, static_cast<R (D::*)(As...)>(method_ptr), rpc_type);
|
2017-03-14 00:12:55 +00:00
|
|
|
}
|
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
template <class T, class P>
|
2017-03-18 15:58:03 +00:00
|
|
|
struct _PropertySetFunc {
|
|
|
|
void (T::*f)(P);
|
2020-02-23 02:13:37 +00:00
|
|
|
static void _wrapped_setter(godot_object * /*object*/, void *method_data, void *user_data, godot_variant *value) {
|
2018-11-23 22:09:41 +00:00
|
|
|
_PropertySetFunc<T, P> *set_func = (_PropertySetFunc<T, P> *)method_data;
|
|
|
|
T *obj = (T *)user_data;
|
2017-03-18 15:58:03 +00:00
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
Variant *v = (Variant *)value;
|
2017-03-18 15:58:03 +00:00
|
|
|
|
2017-04-11 01:04:29 +00:00
|
|
|
(obj->*(set_func->f))(_ArgCast<P>::_arg_cast(*v));
|
2017-03-18 15:58:03 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
template <class T, class P>
|
2017-03-18 15:58:03 +00:00
|
|
|
struct _PropertyGetFunc {
|
2018-11-23 22:09:41 +00:00
|
|
|
P(T::*f)
|
|
|
|
();
|
2020-02-23 02:13:37 +00:00
|
|
|
static godot_variant _wrapped_getter(godot_object * /*object*/, void *method_data, void *user_data) {
|
2018-11-23 22:09:41 +00:00
|
|
|
_PropertyGetFunc<T, P> *get_func = (_PropertyGetFunc<T, P> *)method_data;
|
|
|
|
T *obj = (T *)user_data;
|
2017-03-18 15:58:03 +00:00
|
|
|
|
|
|
|
godot_variant var;
|
2017-10-20 23:42:10 +00:00
|
|
|
godot::api->godot_variant_new_nil(&var);
|
2017-03-18 15:58:03 +00:00
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
Variant *v = (Variant *)&var;
|
2017-03-18 15:58:03 +00:00
|
|
|
|
|
|
|
*v = (obj->*(get_func->f))();
|
|
|
|
|
|
|
|
return var;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
template <class T, class P>
|
2017-04-11 01:04:29 +00:00
|
|
|
struct _PropertyDefaultSetFunc {
|
2018-11-23 22:09:41 +00:00
|
|
|
P(T::*f);
|
2020-02-23 02:13:37 +00:00
|
|
|
static void _wrapped_setter(godot_object * /*object*/, void *method_data, void *user_data, godot_variant *value) {
|
2018-11-23 22:09:41 +00:00
|
|
|
_PropertyDefaultSetFunc<T, P> *set_func = (_PropertyDefaultSetFunc<T, P> *)method_data;
|
|
|
|
T *obj = (T *)user_data;
|
2017-04-11 01:04:29 +00:00
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
Variant *v = (Variant *)value;
|
2017-04-11 01:04:29 +00:00
|
|
|
|
|
|
|
(obj->*(set_func->f)) = _ArgCast<P>::_arg_cast(*v);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
template <class T, class P>
|
2017-04-11 01:04:29 +00:00
|
|
|
struct _PropertyDefaultGetFunc {
|
2018-11-23 22:09:41 +00:00
|
|
|
P(T::*f);
|
2020-02-23 02:13:37 +00:00
|
|
|
static godot_variant _wrapped_getter(godot_object * /*object*/, void *method_data, void *user_data) {
|
2018-11-23 22:09:41 +00:00
|
|
|
_PropertyDefaultGetFunc<T, P> *get_func = (_PropertyDefaultGetFunc<T, P> *)method_data;
|
|
|
|
T *obj = (T *)user_data;
|
2017-04-11 01:04:29 +00:00
|
|
|
|
|
|
|
godot_variant var;
|
2017-10-20 23:42:10 +00:00
|
|
|
godot::api->godot_variant_new_nil(&var);
|
2017-04-11 01:04:29 +00:00
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
Variant *v = (Variant *)&var;
|
2017-04-11 01:04:29 +00:00
|
|
|
|
|
|
|
*v = (obj->*(get_func->f));
|
|
|
|
|
|
|
|
return var;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
template <class T, class P>
|
2020-09-12 16:38:46 +00:00
|
|
|
void register_property(const char *name, P(T::*var), P default_value,
|
|
|
|
godot_method_rpc_mode rpc_mode = GODOT_METHOD_RPC_MODE_DISABLED,
|
|
|
|
godot_property_usage_flags usage = GODOT_PROPERTY_USAGE_DEFAULT,
|
|
|
|
godot_property_hint hint = GODOT_PROPERTY_HINT_NONE, String hint_string = "") {
|
|
|
|
static_assert(T::___CLASS_IS_SCRIPT, "This function must only be used on custom classes");
|
|
|
|
|
2017-04-11 01:04:29 +00:00
|
|
|
Variant def_val = default_value;
|
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
usage = (godot_property_usage_flags)((int)usage | GODOT_PROPERTY_USAGE_SCRIPT_VARIABLE);
|
2017-04-11 01:04:29 +00:00
|
|
|
|
|
|
|
if (def_val.get_type() == Variant::OBJECT) {
|
2020-05-30 18:20:13 +00:00
|
|
|
Object *o = detail::get_wrapper<Object>(def_val.operator godot_object *());
|
2017-04-11 01:04:29 +00:00
|
|
|
if (o && o->is_class("Resource")) {
|
2018-11-23 22:09:41 +00:00
|
|
|
hint = (godot_property_hint)((int)hint | GODOT_PROPERTY_HINT_RESOURCE_TYPE);
|
2017-04-11 01:04:29 +00:00
|
|
|
hint_string = o->get_class();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
godot_string *_hint_string = (godot_string *)&hint_string;
|
2017-04-11 01:04:29 +00:00
|
|
|
|
|
|
|
godot_property_attributes attr = {};
|
2019-04-12 11:50:20 +00:00
|
|
|
if (def_val.get_type() == Variant::NIL) {
|
|
|
|
attr.type = Variant::OBJECT;
|
|
|
|
} else {
|
|
|
|
attr.type = def_val.get_type();
|
|
|
|
attr.default_value = *(godot_variant *)&def_val;
|
|
|
|
}
|
|
|
|
|
2017-04-11 01:04:29 +00:00
|
|
|
attr.hint = hint;
|
|
|
|
attr.rset_type = rpc_mode;
|
|
|
|
attr.usage = usage;
|
|
|
|
attr.hint_string = *_hint_string;
|
|
|
|
|
2020-09-12 16:38:46 +00:00
|
|
|
_PropertyDefaultSetFunc<T, P> *wrapped_set =
|
|
|
|
(_PropertyDefaultSetFunc<T, P> *)godot::api->godot_alloc(sizeof(_PropertyDefaultSetFunc<T, P>));
|
2017-04-11 01:04:29 +00:00
|
|
|
wrapped_set->f = var;
|
|
|
|
|
2020-09-12 16:38:46 +00:00
|
|
|
_PropertyDefaultGetFunc<T, P> *wrapped_get =
|
|
|
|
(_PropertyDefaultGetFunc<T, P> *)godot::api->godot_alloc(sizeof(_PropertyDefaultGetFunc<T, P>));
|
2017-04-11 01:04:29 +00:00
|
|
|
wrapped_get->f = var;
|
|
|
|
|
|
|
|
godot_property_set_func set_func = {};
|
2018-11-23 22:09:41 +00:00
|
|
|
set_func.method_data = (void *)wrapped_set;
|
|
|
|
set_func.free_func = godot::api->godot_free;
|
|
|
|
set_func.set_func = &_PropertyDefaultSetFunc<T, P>::_wrapped_setter;
|
2017-04-11 01:04:29 +00:00
|
|
|
|
|
|
|
godot_property_get_func get_func = {};
|
2018-11-23 22:09:41 +00:00
|
|
|
get_func.method_data = (void *)wrapped_get;
|
|
|
|
get_func.free_func = godot::api->godot_free;
|
|
|
|
get_func.get_func = &_PropertyDefaultGetFunc<T, P>::_wrapped_getter;
|
2017-04-11 01:04:29 +00:00
|
|
|
|
2020-09-12 16:38:46 +00:00
|
|
|
godot::nativescript_api->godot_nativescript_register_property(godot::_RegisterState::nativescript_handle,
|
|
|
|
T::___get_class_name(), name, &attr, set_func, get_func);
|
2017-04-11 01:04:29 +00:00
|
|
|
}
|
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
template <class T, class P>
|
2020-09-12 16:38:46 +00:00
|
|
|
void register_property(const char *name, void (T::*setter)(P), P (T::*getter)(), P default_value,
|
|
|
|
godot_method_rpc_mode rpc_mode = GODOT_METHOD_RPC_MODE_DISABLED,
|
|
|
|
godot_property_usage_flags usage = GODOT_PROPERTY_USAGE_DEFAULT,
|
|
|
|
godot_property_hint hint = GODOT_PROPERTY_HINT_NONE, String hint_string = "") {
|
|
|
|
static_assert(T::___CLASS_IS_SCRIPT, "This function must only be used on custom classes");
|
|
|
|
|
2017-03-18 15:58:03 +00:00
|
|
|
Variant def_val = default_value;
|
|
|
|
|
2019-04-12 11:50:20 +00:00
|
|
|
godot_string *_hint_string = (godot_string *)&hint_string;
|
|
|
|
|
2017-03-18 15:58:03 +00:00
|
|
|
godot_property_attributes attr = {};
|
2019-04-12 11:50:20 +00:00
|
|
|
if (def_val.get_type() == Variant::NIL) {
|
|
|
|
attr.type = Variant::OBJECT;
|
|
|
|
} else {
|
|
|
|
attr.type = def_val.get_type();
|
|
|
|
attr.default_value = *(godot_variant *)&def_val;
|
|
|
|
}
|
2017-03-18 15:58:03 +00:00
|
|
|
attr.hint = hint;
|
|
|
|
attr.rset_type = rpc_mode;
|
|
|
|
attr.usage = usage;
|
2019-04-12 11:50:20 +00:00
|
|
|
attr.hint_string = *_hint_string;
|
2017-03-18 15:58:03 +00:00
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
_PropertySetFunc<T, P> *wrapped_set = (_PropertySetFunc<T, P> *)godot::api->godot_alloc(sizeof(_PropertySetFunc<T, P>));
|
2017-03-18 15:58:03 +00:00
|
|
|
wrapped_set->f = setter;
|
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
_PropertyGetFunc<T, P> *wrapped_get = (_PropertyGetFunc<T, P> *)godot::api->godot_alloc(sizeof(_PropertyGetFunc<T, P>));
|
2017-03-18 15:58:03 +00:00
|
|
|
wrapped_get->f = getter;
|
|
|
|
|
|
|
|
godot_property_set_func set_func = {};
|
2018-11-23 22:09:41 +00:00
|
|
|
set_func.method_data = (void *)wrapped_set;
|
|
|
|
set_func.free_func = godot::api->godot_free;
|
|
|
|
set_func.set_func = &_PropertySetFunc<T, P>::_wrapped_setter;
|
2017-03-18 15:58:03 +00:00
|
|
|
|
|
|
|
godot_property_get_func get_func = {};
|
2018-11-23 22:09:41 +00:00
|
|
|
get_func.method_data = (void *)wrapped_get;
|
|
|
|
get_func.free_func = godot::api->godot_free;
|
|
|
|
get_func.get_func = &_PropertyGetFunc<T, P>::_wrapped_getter;
|
2017-03-18 15:58:03 +00:00
|
|
|
|
2020-09-12 16:38:46 +00:00
|
|
|
godot::nativescript_api->godot_nativescript_register_property(godot::_RegisterState::nativescript_handle,
|
|
|
|
T::___get_class_name(), name, &attr, set_func, get_func);
|
2017-03-18 15:58:03 +00:00
|
|
|
}
|
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
template <class T, class P>
|
2020-09-12 16:38:46 +00:00
|
|
|
void register_property(const char *name, void (T::*setter)(P), P (T::*getter)() const, P default_value,
|
|
|
|
godot_method_rpc_mode rpc_mode = GODOT_METHOD_RPC_MODE_DISABLED,
|
|
|
|
godot_property_usage_flags usage = GODOT_PROPERTY_USAGE_DEFAULT,
|
|
|
|
godot_property_hint hint = GODOT_PROPERTY_HINT_NONE, String hint_string = "") {
|
2018-11-23 22:09:41 +00:00
|
|
|
register_property(name, setter, (P(T::*)())getter, default_value, rpc_mode, usage, hint, hint_string);
|
2018-01-19 10:49:28 +00:00
|
|
|
}
|
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
template <class T>
|
|
|
|
void register_signal(String name, Dictionary args = Dictionary()) {
|
2020-09-12 16:38:46 +00:00
|
|
|
static_assert(T::___CLASS_IS_SCRIPT, "This function must only be used on custom classes");
|
|
|
|
|
2017-04-11 01:04:29 +00:00
|
|
|
godot_signal signal = {};
|
|
|
|
signal.name = *(godot_string *)&name;
|
|
|
|
signal.num_args = args.size();
|
|
|
|
signal.num_default_args = 0;
|
|
|
|
|
2018-02-23 13:08:36 +00:00
|
|
|
// Need to check because malloc(0) is platform-dependent. Zero arguments will leave args to nullptr.
|
2018-11-23 22:09:41 +00:00
|
|
|
if (signal.num_args != 0) {
|
|
|
|
signal.args = (godot_signal_argument *)godot::api->godot_alloc(sizeof(godot_signal_argument) * signal.num_args);
|
|
|
|
memset((void *)signal.args, 0, sizeof(godot_signal_argument) * signal.num_args);
|
2018-01-17 01:00:55 +00:00
|
|
|
}
|
2017-04-11 01:04:29 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < signal.num_args; i++) {
|
|
|
|
// Array entry = args[i];
|
|
|
|
// String name = entry[0];
|
|
|
|
String name = args.keys()[i];
|
|
|
|
godot_string *_key = (godot_string *)&name;
|
2017-10-20 23:42:10 +00:00
|
|
|
godot::api->godot_string_new_copy(&signal.args[i].name, _key);
|
2017-04-11 01:04:29 +00:00
|
|
|
|
|
|
|
// if (entry.size() > 1) {
|
|
|
|
// signal.args[i].type = entry[1];
|
|
|
|
// }
|
|
|
|
signal.args[i].type = args.values()[i];
|
|
|
|
}
|
|
|
|
|
2020-09-12 16:38:46 +00:00
|
|
|
godot::nativescript_api->godot_nativescript_register_signal(godot::_RegisterState::nativescript_handle,
|
|
|
|
T::___get_class_name(), &signal);
|
2017-04-11 01:04:29 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < signal.num_args; i++) {
|
2017-10-20 23:42:10 +00:00
|
|
|
godot::api->godot_string_destroy(&signal.args[i].name);
|
2017-04-11 01:04:29 +00:00
|
|
|
}
|
2017-07-27 10:43:53 +00:00
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
if (signal.args) {
|
2018-01-17 01:00:55 +00:00
|
|
|
godot::api->godot_free(signal.args);
|
|
|
|
}
|
2017-04-11 01:04:29 +00:00
|
|
|
}
|
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
template <class T, class... Args>
|
|
|
|
void register_signal(String name, Args... varargs) {
|
2018-03-07 10:27:34 +00:00
|
|
|
register_signal<T>(name, Dictionary::make(varargs...));
|
2018-03-06 10:26:07 +00:00
|
|
|
}
|
2017-04-11 01:04:29 +00:00
|
|
|
|
2018-02-11 14:50:01 +00:00
|
|
|
#ifndef GODOT_CPP_NO_OBJECT_CAST
|
2018-11-23 22:09:41 +00:00
|
|
|
template <class T>
|
|
|
|
T *Object::cast_to(const Object *obj) {
|
2018-11-24 15:36:27 +00:00
|
|
|
if (!obj)
|
|
|
|
return nullptr;
|
|
|
|
|
2019-10-21 16:08:03 +00:00
|
|
|
if (T::___CLASS_IS_SCRIPT) {
|
|
|
|
size_t have_tag = (size_t)godot::nativescript_1_1_api->godot_nativescript_get_type_tag(obj->_owner);
|
|
|
|
if (have_tag) {
|
|
|
|
if (!godot::_TagDB::is_type_known((size_t)have_tag)) {
|
|
|
|
have_tag = 0;
|
|
|
|
}
|
2018-02-11 14:50:01 +00:00
|
|
|
}
|
|
|
|
|
2019-10-21 16:08:03 +00:00
|
|
|
if (!have_tag) {
|
|
|
|
have_tag = obj->_type_tag;
|
|
|
|
}
|
2018-02-11 14:50:01 +00:00
|
|
|
|
2019-10-21 16:08:03 +00:00
|
|
|
if (godot::_TagDB::is_type_compatible(T::___get_id(), have_tag)) {
|
|
|
|
return detail::get_custom_class_instance<T>(obj);
|
|
|
|
}
|
2018-02-11 14:50:01 +00:00
|
|
|
} else {
|
2019-10-21 16:08:03 +00:00
|
|
|
if (godot::core_1_2_api->godot_object_cast_to(obj->_owner, (void *)T::___get_id())) {
|
|
|
|
return (T *)obj;
|
|
|
|
}
|
2018-02-11 14:50:01 +00:00
|
|
|
}
|
2019-10-21 16:08:03 +00:00
|
|
|
|
|
|
|
return nullptr;
|
2018-02-11 14:50:01 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-11-23 22:09:41 +00:00
|
|
|
} // namespace godot
|
2017-03-06 17:39:56 +00:00
|
|
|
|
2020-09-12 16:38:46 +00:00
|
|
|
#endif // GODOT_HPP
|