2023-01-05 13:25:55 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* callable.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* 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. */
|
|
|
|
/**************************************************************************/
|
2020-02-19 20:27:19 +01:00
|
|
|
|
|
|
|
#include "callable.h"
|
2020-05-12 17:01:17 +02:00
|
|
|
|
2020-11-07 23:33:38 +01:00
|
|
|
#include "core/object/object.h"
|
2021-06-04 18:03:15 +02:00
|
|
|
#include "core/object/ref_counted.h"
|
2020-11-07 23:33:38 +01:00
|
|
|
#include "core/object/script_language.h"
|
2024-02-28 10:53:34 +01:00
|
|
|
#include "core/variant/callable_bind.h"
|
|
|
|
#include "core/variant/variant_callable.h"
|
2020-02-19 20:27:19 +01:00
|
|
|
|
2022-07-28 22:56:41 +02:00
|
|
|
void Callable::call_deferredp(const Variant **p_arguments, int p_argcount) const {
|
2023-12-18 15:46:56 +01:00
|
|
|
MessageQueue::get_singleton()->push_callablep(*this, p_arguments, p_argcount, true);
|
2020-02-19 20:27:19 +01:00
|
|
|
}
|
|
|
|
|
2022-07-28 22:56:41 +02:00
|
|
|
void Callable::callp(const Variant **p_arguments, int p_argcount, Variant &r_return_value, CallError &r_call_error) const {
|
2020-02-19 20:27:19 +01:00
|
|
|
if (is_null()) {
|
|
|
|
r_call_error.error = CallError::CALL_ERROR_INSTANCE_IS_NULL;
|
|
|
|
r_call_error.argument = 0;
|
|
|
|
r_call_error.expected = 0;
|
|
|
|
r_return_value = Variant();
|
|
|
|
} else if (is_custom()) {
|
2023-09-26 21:58:57 +02:00
|
|
|
if (!is_valid()) {
|
|
|
|
r_call_error.error = CallError::CALL_ERROR_INSTANCE_IS_NULL;
|
|
|
|
r_call_error.argument = 0;
|
|
|
|
r_call_error.expected = 0;
|
|
|
|
r_return_value = Variant();
|
|
|
|
return;
|
|
|
|
}
|
2020-02-19 20:27:19 +01:00
|
|
|
custom->call(p_arguments, p_argcount, r_return_value, r_call_error);
|
|
|
|
} else {
|
|
|
|
Object *obj = ObjectDB::get_instance(ObjectID(object));
|
2021-05-24 19:18:39 +02:00
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
if (!obj) {
|
|
|
|
r_call_error.error = CallError::CALL_ERROR_INSTANCE_IS_NULL;
|
|
|
|
r_call_error.argument = 0;
|
|
|
|
r_call_error.expected = 0;
|
|
|
|
r_return_value = Variant();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2022-03-09 14:58:40 +01:00
|
|
|
r_return_value = obj->callp(method, p_arguments, p_argcount, r_call_error);
|
2020-02-19 20:27:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-20 22:32:46 +02:00
|
|
|
Variant Callable::callv(const Array &p_arguments) const {
|
|
|
|
int argcount = p_arguments.size();
|
|
|
|
const Variant **argptrs = nullptr;
|
|
|
|
if (argcount) {
|
|
|
|
argptrs = (const Variant **)alloca(sizeof(Variant *) * argcount);
|
|
|
|
for (int i = 0; i < argcount; i++) {
|
|
|
|
argptrs[i] = &p_arguments[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CallError ce;
|
|
|
|
Variant ret;
|
|
|
|
callp(argptrs, argcount, ret, ce);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-07-28 22:56:41 +02:00
|
|
|
Error Callable::rpcp(int p_id, const Variant **p_arguments, int p_argcount, CallError &r_call_error) const {
|
2021-05-03 23:00:44 +02:00
|
|
|
if (is_null()) {
|
|
|
|
r_call_error.error = CallError::CALL_ERROR_INSTANCE_IS_NULL;
|
|
|
|
r_call_error.argument = 0;
|
|
|
|
r_call_error.expected = 0;
|
2022-07-12 23:12:42 +02:00
|
|
|
return ERR_UNCONFIGURED;
|
2021-05-03 23:00:44 +02:00
|
|
|
} else if (!is_custom()) {
|
2024-02-02 09:21:45 +01:00
|
|
|
Object *obj = ObjectDB::get_instance(ObjectID(object));
|
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
if (!obj || !obj->is_class("Node")) {
|
|
|
|
r_call_error.error = CallError::CALL_ERROR_INSTANCE_IS_NULL;
|
|
|
|
r_call_error.argument = 0;
|
|
|
|
r_call_error.expected = 0;
|
|
|
|
return ERR_UNCONFIGURED;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int argcount = p_argcount + 2;
|
|
|
|
const Variant **argptrs = (const Variant **)alloca(sizeof(Variant *) * argcount);
|
|
|
|
const Variant args[2] = { p_id, method };
|
|
|
|
|
|
|
|
argptrs[0] = &args[0];
|
|
|
|
argptrs[1] = &args[1];
|
|
|
|
for (int i = 0; i < p_argcount; ++i) {
|
|
|
|
argptrs[i + 2] = p_arguments[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
CallError tmp;
|
|
|
|
Error err = (Error)obj->callp(SNAME("rpc_id"), argptrs, argcount, tmp).operator int64_t();
|
|
|
|
|
|
|
|
r_call_error.error = Callable::CallError::CALL_OK;
|
|
|
|
return err;
|
2021-05-03 23:00:44 +02:00
|
|
|
} else {
|
2022-07-12 23:12:42 +02:00
|
|
|
return custom->rpc(p_id, p_arguments, p_argcount, r_call_error);
|
2021-05-03 23:00:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 22:56:41 +02:00
|
|
|
Callable Callable::bindp(const Variant **p_arguments, int p_argcount) const {
|
2020-10-09 23:41:53 +02:00
|
|
|
Vector<Variant> args;
|
|
|
|
args.resize(p_argcount);
|
|
|
|
for (int i = 0; i < p_argcount; i++) {
|
|
|
|
args.write[i] = *p_arguments[i];
|
|
|
|
}
|
|
|
|
return Callable(memnew(CallableCustomBind(*this, args)));
|
|
|
|
}
|
2023-01-06 16:35:42 +01:00
|
|
|
|
|
|
|
Callable Callable::bindv(const Array &p_arguments) {
|
|
|
|
if (p_arguments.is_empty()) {
|
|
|
|
return *this; // No point in creating a new callable if nothing is bound.
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<Variant> args;
|
|
|
|
args.resize(p_arguments.size());
|
|
|
|
for (int i = 0; i < p_arguments.size(); i++) {
|
|
|
|
args.write[i] = p_arguments[i];
|
|
|
|
}
|
|
|
|
return Callable(memnew(CallableCustomBind(*this, args)));
|
|
|
|
}
|
|
|
|
|
2020-10-09 23:41:53 +02:00
|
|
|
Callable Callable::unbind(int p_argcount) const {
|
2023-01-10 13:08:10 +01:00
|
|
|
ERR_FAIL_COND_V_MSG(p_argcount <= 0, Callable(*this), "Amount of unbind() arguments must be 1 or greater.");
|
2020-10-09 23:41:53 +02:00
|
|
|
return Callable(memnew(CallableCustomUnbind(*this, p_argcount)));
|
|
|
|
}
|
|
|
|
|
2021-06-30 16:36:46 +02:00
|
|
|
bool Callable::is_valid() const {
|
2023-01-18 22:04:09 +01:00
|
|
|
if (is_custom()) {
|
|
|
|
return get_custom()->is_valid();
|
|
|
|
} else {
|
|
|
|
return get_object() && get_object()->has_method(get_method());
|
|
|
|
}
|
2021-06-30 16:36:46 +02:00
|
|
|
}
|
|
|
|
|
2020-02-19 20:27:19 +01:00
|
|
|
Object *Callable::get_object() const {
|
|
|
|
if (is_null()) {
|
|
|
|
return nullptr;
|
|
|
|
} else if (is_custom()) {
|
|
|
|
return ObjectDB::get_instance(custom->get_object());
|
|
|
|
} else {
|
|
|
|
return ObjectDB::get_instance(ObjectID(object));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectID Callable::get_object_id() const {
|
|
|
|
if (is_null()) {
|
|
|
|
return ObjectID();
|
|
|
|
} else if (is_custom()) {
|
|
|
|
return custom->get_object();
|
|
|
|
} else {
|
|
|
|
return ObjectID(object);
|
|
|
|
}
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2020-02-19 20:27:19 +01:00
|
|
|
StringName Callable::get_method() const {
|
2022-02-06 19:02:53 +01:00
|
|
|
if (is_custom()) {
|
|
|
|
return get_custom()->get_method();
|
|
|
|
}
|
2020-02-19 20:27:19 +01:00
|
|
|
return method;
|
|
|
|
}
|
2020-02-28 14:32:36 +01:00
|
|
|
|
2024-01-28 15:16:09 +01:00
|
|
|
int Callable::get_argument_count(bool *r_is_valid) const {
|
|
|
|
if (is_custom()) {
|
|
|
|
bool valid = false;
|
|
|
|
return custom->get_argument_count(r_is_valid ? *r_is_valid : valid);
|
|
|
|
} else if (!is_null()) {
|
|
|
|
return get_object()->get_method_argument_count(method, r_is_valid);
|
|
|
|
} else {
|
|
|
|
if (r_is_valid) {
|
|
|
|
*r_is_valid = false;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-06 15:37:53 +01:00
|
|
|
int Callable::get_bound_arguments_count() const {
|
|
|
|
if (!is_null() && is_custom()) {
|
|
|
|
return custom->get_bound_arguments_count();
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-10 13:08:10 +01:00
|
|
|
void Callable::get_bound_arguments_ref(Vector<Variant> &r_arguments, int &r_argcount) const {
|
|
|
|
if (!is_null() && is_custom()) {
|
|
|
|
custom->get_bound_arguments(r_arguments, r_argcount);
|
|
|
|
} else {
|
|
|
|
r_arguments.clear();
|
|
|
|
r_argcount = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Array Callable::get_bound_arguments() const {
|
|
|
|
Vector<Variant> arr;
|
|
|
|
int ac;
|
|
|
|
get_bound_arguments_ref(arr, ac);
|
|
|
|
Array ret;
|
|
|
|
ret.resize(arr.size());
|
|
|
|
for (int i = 0; i < arr.size(); i++) {
|
|
|
|
ret[i] = arr[i];
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-03-14 19:20:17 +01:00
|
|
|
CallableCustom *Callable::get_custom() const {
|
2020-04-02 01:20:12 +02:00
|
|
|
ERR_FAIL_COND_V_MSG(!is_custom(), nullptr,
|
2020-03-14 19:20:17 +01:00
|
|
|
vformat("Can't get custom on non-CallableCustom \"%s\".", operator String()));
|
|
|
|
return custom;
|
|
|
|
}
|
|
|
|
|
2020-10-09 23:41:53 +02:00
|
|
|
const Callable *Callable::get_base_comparator() const {
|
|
|
|
const Callable *comparator = nullptr;
|
|
|
|
if (is_custom()) {
|
|
|
|
comparator = custom->get_base_comparator();
|
|
|
|
}
|
|
|
|
if (comparator) {
|
|
|
|
return comparator;
|
|
|
|
} else {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-19 20:27:19 +01:00
|
|
|
uint32_t Callable::hash() const {
|
|
|
|
if (is_custom()) {
|
|
|
|
return custom->hash();
|
|
|
|
} else {
|
|
|
|
uint32_t hash = method.hash();
|
2022-06-18 16:20:55 +02:00
|
|
|
hash = hash_murmur3_one_64(object, hash);
|
|
|
|
return hash_fmix32(hash);
|
2020-02-19 20:27:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Callable::operator==(const Callable &p_callable) const {
|
|
|
|
bool custom_a = is_custom();
|
|
|
|
bool custom_b = p_callable.is_custom();
|
|
|
|
|
|
|
|
if (custom_a == custom_b) {
|
|
|
|
if (custom_a) {
|
|
|
|
if (custom == p_callable.custom) {
|
2021-03-12 14:35:16 +01:00
|
|
|
return true; //same pointer, don't even compare
|
2020-02-19 20:27:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CallableCustom::CompareEqualFunc eq_a = custom->get_compare_equal_func();
|
|
|
|
CallableCustom::CompareEqualFunc eq_b = p_callable.custom->get_compare_equal_func();
|
|
|
|
if (eq_a == eq_b) {
|
|
|
|
return eq_a(custom, p_callable.custom);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return object == p_callable.object && method == p_callable.method;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2020-02-19 20:27:19 +01:00
|
|
|
bool Callable::operator!=(const Callable &p_callable) const {
|
|
|
|
return !(*this == p_callable);
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2020-02-19 20:27:19 +01:00
|
|
|
bool Callable::operator<(const Callable &p_callable) const {
|
|
|
|
bool custom_a = is_custom();
|
|
|
|
bool custom_b = p_callable.is_custom();
|
|
|
|
|
|
|
|
if (custom_a == custom_b) {
|
|
|
|
if (custom_a) {
|
|
|
|
if (custom == p_callable.custom) {
|
2021-03-12 14:35:16 +01:00
|
|
|
return false; //same pointer, don't even compare
|
2020-02-19 20:27:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CallableCustom::CompareLessFunc less_a = custom->get_compare_less_func();
|
|
|
|
CallableCustom::CompareLessFunc less_b = p_callable.custom->get_compare_less_func();
|
|
|
|
if (less_a == less_b) {
|
|
|
|
return less_a(custom, p_callable.custom);
|
|
|
|
} else {
|
|
|
|
return less_a < less_b; //it's something..
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (object == p_callable.object) {
|
|
|
|
return method < p_callable.method;
|
|
|
|
} else {
|
|
|
|
return object < p_callable.object;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return int(custom_a ? 1 : 0) < int(custom_b ? 1 : 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Callable::operator=(const Callable &p_callable) {
|
|
|
|
if (is_custom()) {
|
|
|
|
if (p_callable.is_custom()) {
|
|
|
|
if (custom == p_callable.custom) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (custom->ref_count.unref()) {
|
|
|
|
memdelete(custom);
|
2024-06-03 06:00:53 +02:00
|
|
|
custom = nullptr;
|
2020-02-19 20:27:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p_callable.is_custom()) {
|
|
|
|
method = StringName();
|
|
|
|
if (!p_callable.custom->ref_count.ref()) {
|
|
|
|
object = 0;
|
|
|
|
} else {
|
|
|
|
object = 0;
|
|
|
|
custom = p_callable.custom;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
method = p_callable.method;
|
|
|
|
object = p_callable.object;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Callable::operator String() const {
|
|
|
|
if (is_custom()) {
|
|
|
|
return custom->get_as_text();
|
|
|
|
} else {
|
|
|
|
if (is_null()) {
|
|
|
|
return "null::null";
|
|
|
|
}
|
|
|
|
|
|
|
|
Object *base = get_object();
|
|
|
|
if (base) {
|
|
|
|
String class_name = base->get_class();
|
|
|
|
Ref<Script> script = base->get_script();
|
|
|
|
if (script.is_valid() && script->get_path().is_resource_file()) {
|
|
|
|
class_name += "(" + script->get_path().get_file() + ")";
|
|
|
|
}
|
|
|
|
return class_name + "::" + String(method);
|
|
|
|
} else {
|
|
|
|
return "null::" + String(method);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-28 10:53:34 +01:00
|
|
|
Callable Callable::create(const Variant &p_variant, const StringName &p_method) {
|
|
|
|
ERR_FAIL_COND_V_MSG(p_method == StringName(), Callable(), "Method argument to Callable::create method must be a non-empty string.");
|
|
|
|
|
|
|
|
switch (p_variant.get_type()) {
|
|
|
|
case Variant::NIL:
|
|
|
|
return Callable(ObjectID(), p_method);
|
|
|
|
case Variant::OBJECT:
|
|
|
|
return Callable(p_variant.operator ObjectID(), p_method);
|
|
|
|
default:
|
|
|
|
return Callable(memnew(VariantCallable(p_variant, p_method)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-19 20:27:19 +01:00
|
|
|
Callable::Callable(const Object *p_object, const StringName &p_method) {
|
2024-02-28 10:53:34 +01:00
|
|
|
if (unlikely(p_method == StringName())) {
|
2020-02-19 20:27:19 +01:00
|
|
|
object = 0;
|
2024-02-28 10:53:34 +01:00
|
|
|
ERR_FAIL_MSG("Method argument to Callable constructor must be a non-empty string.");
|
2020-02-19 20:27:19 +01:00
|
|
|
}
|
2024-02-28 10:53:34 +01:00
|
|
|
if (unlikely(p_object == nullptr)) {
|
2020-02-19 20:27:19 +01:00
|
|
|
object = 0;
|
2024-02-28 10:53:34 +01:00
|
|
|
ERR_FAIL_MSG("Object argument to Callable constructor must be non-null.");
|
2020-02-19 20:27:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
object = p_object->get_instance_id();
|
|
|
|
method = p_method;
|
|
|
|
}
|
|
|
|
|
|
|
|
Callable::Callable(ObjectID p_object, const StringName &p_method) {
|
2024-02-28 10:53:34 +01:00
|
|
|
if (unlikely(p_method == StringName())) {
|
2020-02-19 20:27:19 +01:00
|
|
|
object = 0;
|
2024-02-28 10:53:34 +01:00
|
|
|
ERR_FAIL_MSG("Method argument to Callable constructor must be a non-empty string.");
|
2020-02-19 20:27:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
object = p_object;
|
|
|
|
method = p_method;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2020-02-19 20:27:19 +01:00
|
|
|
Callable::Callable(CallableCustom *p_custom) {
|
2024-02-28 10:53:34 +01:00
|
|
|
if (unlikely(p_custom->referenced)) {
|
2020-02-19 20:27:19 +01:00
|
|
|
object = 0;
|
2024-02-28 10:53:34 +01:00
|
|
|
ERR_FAIL_MSG("Callable custom is already referenced.");
|
2020-02-19 20:27:19 +01:00
|
|
|
}
|
|
|
|
p_custom->referenced = true;
|
|
|
|
object = 0; //ensure object is all zero, since pointer may be 32 bits
|
|
|
|
custom = p_custom;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2020-02-19 20:27:19 +01:00
|
|
|
Callable::Callable(const Callable &p_callable) {
|
|
|
|
if (p_callable.is_custom()) {
|
|
|
|
if (!p_callable.custom->ref_count.ref()) {
|
|
|
|
object = 0;
|
|
|
|
} else {
|
|
|
|
object = 0;
|
|
|
|
custom = p_callable.custom;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
method = p_callable.method;
|
|
|
|
object = p_callable.object;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Callable::~Callable() {
|
|
|
|
if (is_custom()) {
|
|
|
|
if (custom->ref_count.unref()) {
|
|
|
|
memdelete(custom);
|
2024-06-03 06:00:53 +02:00
|
|
|
custom = nullptr;
|
2020-02-19 20:27:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-18 22:04:09 +01:00
|
|
|
bool CallableCustom::is_valid() const {
|
|
|
|
// Sensible default implementation so most custom callables don't need their own.
|
|
|
|
return ObjectDB::get_instance(get_object());
|
|
|
|
}
|
|
|
|
|
2022-02-06 19:02:53 +01:00
|
|
|
StringName CallableCustom::get_method() const {
|
|
|
|
ERR_FAIL_V_MSG(StringName(), vformat("Can't get method on CallableCustom \"%s\".", get_as_text()));
|
|
|
|
}
|
|
|
|
|
2022-07-12 23:12:42 +02:00
|
|
|
Error CallableCustom::rpc(int p_peer_id, const Variant **p_arguments, int p_argcount, Callable::CallError &r_call_error) const {
|
2021-05-03 23:00:44 +02:00
|
|
|
r_call_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
|
|
|
|
r_call_error.argument = 0;
|
|
|
|
r_call_error.expected = 0;
|
2022-07-12 23:12:42 +02:00
|
|
|
return ERR_UNCONFIGURED;
|
2021-05-03 23:00:44 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 23:41:53 +02:00
|
|
|
const Callable *CallableCustom::get_base_comparator() const {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2024-01-28 15:16:09 +01:00
|
|
|
int CallableCustom::get_argument_count(bool &r_is_valid) const {
|
|
|
|
r_is_valid = false;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-01-06 15:37:53 +01:00
|
|
|
int CallableCustom::get_bound_arguments_count() const {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-01-10 13:08:10 +01:00
|
|
|
void CallableCustom::get_bound_arguments(Vector<Variant> &r_arguments, int &r_argcount) const {
|
|
|
|
r_arguments = Vector<Variant>();
|
|
|
|
r_argcount = 0;
|
|
|
|
}
|
|
|
|
|
2020-02-19 20:27:19 +01:00
|
|
|
CallableCustom::CallableCustom() {
|
|
|
|
ref_count.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////
|
|
|
|
|
|
|
|
Object *Signal::get_object() const {
|
|
|
|
return ObjectDB::get_instance(object);
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2020-02-19 20:27:19 +01:00
|
|
|
ObjectID Signal::get_object_id() const {
|
|
|
|
return object;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2020-02-19 20:27:19 +01:00
|
|
|
StringName Signal::get_name() const {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Signal::operator==(const Signal &p_signal) const {
|
|
|
|
return object == p_signal.object && name == p_signal.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Signal::operator!=(const Signal &p_signal) const {
|
|
|
|
return object != p_signal.object || name != p_signal.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Signal::operator<(const Signal &p_signal) const {
|
|
|
|
if (object == p_signal.object) {
|
|
|
|
return name < p_signal.name;
|
|
|
|
} else {
|
|
|
|
return object < p_signal.object;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Signal::operator String() const {
|
|
|
|
Object *base = get_object();
|
|
|
|
if (base) {
|
|
|
|
String class_name = base->get_class();
|
|
|
|
Ref<Script> script = base->get_script();
|
|
|
|
if (script.is_valid() && script->get_path().is_resource_file()) {
|
|
|
|
class_name += "(" + script->get_path().get_file() + ")";
|
|
|
|
}
|
|
|
|
return class_name + "::[signal]" + String(name);
|
|
|
|
} else {
|
|
|
|
return "null::[signal]" + String(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Error Signal::emit(const Variant **p_arguments, int p_argcount) const {
|
|
|
|
Object *obj = ObjectDB::get_instance(object);
|
|
|
|
if (!obj) {
|
|
|
|
return ERR_INVALID_DATA;
|
|
|
|
}
|
|
|
|
|
2022-03-09 14:58:40 +01:00
|
|
|
return obj->emit_signalp(name, p_arguments, p_argcount);
|
2020-02-19 20:27:19 +01:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2021-09-19 18:55:23 +02:00
|
|
|
Error Signal::connect(const Callable &p_callable, uint32_t p_flags) {
|
2022-09-29 11:53:28 +02:00
|
|
|
Object *obj = get_object();
|
2023-09-09 16:11:33 +02:00
|
|
|
ERR_FAIL_NULL_V(obj, ERR_UNCONFIGURED);
|
2020-02-19 20:27:19 +01:00
|
|
|
|
2022-09-29 11:53:28 +02:00
|
|
|
return obj->connect(name, p_callable, p_flags);
|
2020-02-19 20:27:19 +01:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2020-02-19 20:27:19 +01:00
|
|
|
void Signal::disconnect(const Callable &p_callable) {
|
2022-09-29 11:53:28 +02:00
|
|
|
Object *obj = get_object();
|
2023-09-09 16:11:33 +02:00
|
|
|
ERR_FAIL_NULL(obj);
|
2022-09-29 11:53:28 +02:00
|
|
|
obj->disconnect(name, p_callable);
|
2020-02-19 20:27:19 +01:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2020-02-19 20:27:19 +01:00
|
|
|
bool Signal::is_connected(const Callable &p_callable) const {
|
2022-09-29 11:53:28 +02:00
|
|
|
Object *obj = get_object();
|
2023-09-09 16:11:33 +02:00
|
|
|
ERR_FAIL_NULL_V(obj, false);
|
2020-02-19 20:27:19 +01:00
|
|
|
|
2022-09-29 11:53:28 +02:00
|
|
|
return obj->is_connected(name, p_callable);
|
2020-02-19 20:27:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Array Signal::get_connections() const {
|
2022-09-29 11:53:28 +02:00
|
|
|
Object *obj = get_object();
|
|
|
|
if (!obj) {
|
2020-02-19 20:27:19 +01:00
|
|
|
return Array();
|
|
|
|
}
|
|
|
|
|
|
|
|
List<Object::Connection> connections;
|
2022-09-29 11:53:28 +02:00
|
|
|
obj->get_signal_connection_list(name, &connections);
|
2020-02-19 20:27:19 +01:00
|
|
|
|
|
|
|
Array arr;
|
2021-07-24 15:46:25 +02:00
|
|
|
for (const Object::Connection &E : connections) {
|
2021-07-16 05:45:57 +02:00
|
|
|
arr.push_back(E);
|
2020-02-19 20:27:19 +01:00
|
|
|
}
|
|
|
|
return arr;
|
|
|
|
}
|
2020-05-12 17:01:17 +02:00
|
|
|
|
2020-02-19 20:27:19 +01:00
|
|
|
Signal::Signal(const Object *p_object, const StringName &p_name) {
|
2023-09-09 16:11:33 +02:00
|
|
|
ERR_FAIL_NULL_MSG(p_object, "Object argument to Signal constructor must be non-null.");
|
2020-02-19 20:27:19 +01:00
|
|
|
|
|
|
|
object = p_object->get_instance_id();
|
|
|
|
name = p_name;
|
|
|
|
}
|
2020-05-12 17:01:17 +02:00
|
|
|
|
2020-02-19 20:27:19 +01:00
|
|
|
Signal::Signal(ObjectID p_object, const StringName &p_name) {
|
|
|
|
object = p_object;
|
|
|
|
name = p_name;
|
|
|
|
}
|
2022-03-28 03:57:20 +02:00
|
|
|
|
|
|
|
bool CallableComparator::operator()(const Variant &p_l, const Variant &p_r) const {
|
|
|
|
const Variant *args[2] = { &p_l, &p_r };
|
|
|
|
Callable::CallError err;
|
|
|
|
Variant res;
|
2022-07-28 22:56:41 +02:00
|
|
|
func.callp(args, 2, res, err);
|
2022-03-28 03:57:20 +02:00
|
|
|
ERR_FAIL_COND_V_MSG(err.error != Callable::CallError::CALL_OK, false,
|
2022-07-17 23:57:20 +02:00
|
|
|
"Error calling compare method: " + Variant::get_callable_error_text(func, args, 2, err));
|
2022-03-28 03:57:20 +02:00
|
|
|
return res;
|
|
|
|
}
|