2020-11-10 21:31:33 +00:00
|
|
|
/*************************************************************************/
|
2020-11-11 16:16:08 +00:00
|
|
|
/* variant_utility.cpp */
|
2020-11-10 21:31:33 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2022-01-03 20:27:34 +00:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2020-11-10 21:31:33 +00:00
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
|
|
|
#include "variant.h"
|
|
|
|
|
|
|
|
#include "core/core_string_names.h"
|
|
|
|
#include "core/io/marshalls.h"
|
2021-06-04 16:03:15 +00:00
|
|
|
#include "core/object/ref_counted.h"
|
2020-11-10 21:31:33 +00:00
|
|
|
#include "core/os/os.h"
|
|
|
|
#include "core/templates/oa_hash_map.h"
|
2021-08-23 23:57:52 +00:00
|
|
|
#include "core/templates/rid.h"
|
|
|
|
#include "core/templates/rid_owner.h"
|
2020-11-10 21:31:33 +00:00
|
|
|
#include "core/variant/binder_common.h"
|
|
|
|
#include "core/variant/variant_parser.h"
|
|
|
|
|
2020-11-11 16:16:08 +00:00
|
|
|
struct VariantUtilityFunctions {
|
2020-11-10 21:31:33 +00:00
|
|
|
// Math
|
2020-11-17 10:21:08 +00:00
|
|
|
|
2020-11-10 21:31:33 +00:00
|
|
|
static inline double sin(double arg) {
|
|
|
|
return Math::sin(arg);
|
|
|
|
}
|
2020-11-17 10:21:08 +00:00
|
|
|
|
2020-11-10 21:31:33 +00:00
|
|
|
static inline double cos(double arg) {
|
|
|
|
return Math::cos(arg);
|
|
|
|
}
|
2020-11-17 10:21:08 +00:00
|
|
|
|
2020-11-10 21:31:33 +00:00
|
|
|
static inline double tan(double arg) {
|
|
|
|
return Math::tan(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline double sinh(double arg) {
|
|
|
|
return Math::sinh(arg);
|
|
|
|
}
|
2020-11-17 10:21:08 +00:00
|
|
|
|
2020-11-10 21:31:33 +00:00
|
|
|
static inline double cosh(double arg) {
|
|
|
|
return Math::cosh(arg);
|
|
|
|
}
|
2020-11-17 10:21:08 +00:00
|
|
|
|
2020-11-10 21:31:33 +00:00
|
|
|
static inline double tanh(double arg) {
|
|
|
|
return Math::tanh(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline double asin(double arg) {
|
|
|
|
return Math::asin(arg);
|
|
|
|
}
|
2020-11-17 10:21:08 +00:00
|
|
|
|
2020-11-10 21:31:33 +00:00
|
|
|
static inline double acos(double arg) {
|
|
|
|
return Math::acos(arg);
|
|
|
|
}
|
2020-11-17 10:21:08 +00:00
|
|
|
|
2020-11-10 21:31:33 +00:00
|
|
|
static inline double atan(double arg) {
|
|
|
|
return Math::atan(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline double atan2(double y, double x) {
|
|
|
|
return Math::atan2(y, x);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline double sqrt(double x) {
|
|
|
|
return Math::sqrt(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline double fmod(double b, double r) {
|
|
|
|
return Math::fmod(b, r);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline double fposmod(double b, double r) {
|
|
|
|
return Math::fposmod(b, r);
|
|
|
|
}
|
|
|
|
|
2021-05-04 10:51:03 +00:00
|
|
|
static inline int64_t posmod(int64_t b, int64_t r) {
|
|
|
|
return Math::posmod(b, r);
|
|
|
|
}
|
|
|
|
|
2022-07-23 20:12:23 +00:00
|
|
|
static inline Variant floor(Variant x, Callable::CallError &r_error) {
|
|
|
|
r_error.error = Callable::CallError::CALL_OK;
|
|
|
|
switch (x.get_type()) {
|
|
|
|
case Variant::INT: {
|
|
|
|
return VariantInternalAccessor<int64_t>::get(&x);
|
|
|
|
} break;
|
|
|
|
case Variant::FLOAT: {
|
|
|
|
return Math::floor(VariantInternalAccessor<double>::get(&x));
|
|
|
|
} break;
|
|
|
|
case Variant::VECTOR2: {
|
|
|
|
return VariantInternalAccessor<Vector2>::get(&x).floor();
|
|
|
|
} break;
|
|
|
|
case Variant::VECTOR3: {
|
|
|
|
return VariantInternalAccessor<Vector3>::get(&x).floor();
|
|
|
|
} break;
|
|
|
|
case Variant::VECTOR4: {
|
|
|
|
return VariantInternalAccessor<Vector4>::get(&x).floor();
|
|
|
|
} break;
|
|
|
|
default: {
|
|
|
|
r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
|
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline double floorf(double x) {
|
2020-11-10 21:31:33 +00:00
|
|
|
return Math::floor(x);
|
|
|
|
}
|
|
|
|
|
2022-07-23 20:12:23 +00:00
|
|
|
static inline int floori(double x) {
|
2022-09-27 15:40:56 +00:00
|
|
|
return int(Math::floor(x));
|
2022-07-23 20:12:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline Variant ceil(Variant x, Callable::CallError &r_error) {
|
|
|
|
r_error.error = Callable::CallError::CALL_OK;
|
|
|
|
switch (x.get_type()) {
|
|
|
|
case Variant::INT: {
|
|
|
|
return VariantInternalAccessor<int64_t>::get(&x);
|
|
|
|
} break;
|
|
|
|
case Variant::FLOAT: {
|
|
|
|
return Math::ceil(VariantInternalAccessor<double>::get(&x));
|
|
|
|
} break;
|
|
|
|
case Variant::VECTOR2: {
|
|
|
|
return VariantInternalAccessor<Vector2>::get(&x).ceil();
|
|
|
|
} break;
|
|
|
|
case Variant::VECTOR3: {
|
|
|
|
return VariantInternalAccessor<Vector3>::get(&x).ceil();
|
|
|
|
} break;
|
|
|
|
case Variant::VECTOR4: {
|
|
|
|
return VariantInternalAccessor<Vector4>::get(&x).ceil();
|
|
|
|
} break;
|
|
|
|
default: {
|
|
|
|
r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
|
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline double ceilf(double x) {
|
2020-11-10 21:31:33 +00:00
|
|
|
return Math::ceil(x);
|
|
|
|
}
|
|
|
|
|
2022-07-23 20:12:23 +00:00
|
|
|
static inline int ceili(double x) {
|
|
|
|
return int(Math::ceil(x));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Variant round(Variant x, Callable::CallError &r_error) {
|
|
|
|
r_error.error = Callable::CallError::CALL_OK;
|
|
|
|
switch (x.get_type()) {
|
|
|
|
case Variant::INT: {
|
|
|
|
return VariantInternalAccessor<int64_t>::get(&x);
|
|
|
|
} break;
|
|
|
|
case Variant::FLOAT: {
|
|
|
|
return Math::round(VariantInternalAccessor<double>::get(&x));
|
|
|
|
} break;
|
|
|
|
case Variant::VECTOR2: {
|
|
|
|
return VariantInternalAccessor<Vector2>::get(&x).round();
|
|
|
|
} break;
|
|
|
|
case Variant::VECTOR3: {
|
|
|
|
return VariantInternalAccessor<Vector3>::get(&x).round();
|
|
|
|
} break;
|
|
|
|
case Variant::VECTOR4: {
|
|
|
|
return VariantInternalAccessor<Vector4>::get(&x).round();
|
|
|
|
} break;
|
|
|
|
default: {
|
|
|
|
r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
|
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline double roundf(double x) {
|
2020-11-10 21:31:33 +00:00
|
|
|
return Math::round(x);
|
|
|
|
}
|
|
|
|
|
2022-07-23 20:12:23 +00:00
|
|
|
static inline int roundi(double x) {
|
|
|
|
return int(Math::round(x));
|
|
|
|
}
|
|
|
|
|
2020-11-10 21:31:33 +00:00
|
|
|
static inline Variant abs(const Variant &x, Callable::CallError &r_error) {
|
|
|
|
r_error.error = Callable::CallError::CALL_OK;
|
|
|
|
switch (x.get_type()) {
|
|
|
|
case Variant::INT: {
|
|
|
|
return ABS(VariantInternalAccessor<int64_t>::get(&x));
|
|
|
|
} break;
|
|
|
|
case Variant::FLOAT: {
|
|
|
|
return Math::absd(VariantInternalAccessor<double>::get(&x));
|
|
|
|
} break;
|
|
|
|
case Variant::VECTOR2: {
|
|
|
|
return VariantInternalAccessor<Vector2>::get(&x).abs();
|
|
|
|
} break;
|
|
|
|
case Variant::VECTOR2I: {
|
|
|
|
return VariantInternalAccessor<Vector2i>::get(&x).abs();
|
|
|
|
} break;
|
|
|
|
case Variant::VECTOR3: {
|
|
|
|
return VariantInternalAccessor<Vector3>::get(&x).abs();
|
|
|
|
} break;
|
|
|
|
case Variant::VECTOR3I: {
|
|
|
|
return VariantInternalAccessor<Vector3i>::get(&x).abs();
|
|
|
|
} break;
|
Implement Vector4, Vector4i, Projection
Implement built-in classes Vector4, Vector4i and Projection.
* Two versions of Vector4 (float and integer).
* A Projection class, which is a 4x4 matrix specialized in projection types.
These types have been requested for a long time, but given they were very corner case they were not added before.
Because in Godot 4, reimplementing parts of the rendering engine is now possible, access to these types (heavily used by the rendering code) becomes a necessity.
**Q**: Why Projection and not Matrix4?
**A**: Godot does not use Matrix2, Matrix3, Matrix4x3, etc. naming convention because, within the engine, these types always have a *purpose*. As such, Godot names them: Transform2D, Transform3D or Basis. In this case, this 4x4 matrix is _always_ used as a _Projection_, hence the naming.
2022-07-19 23:11:13 +00:00
|
|
|
case Variant::VECTOR4: {
|
|
|
|
return VariantInternalAccessor<Vector4>::get(&x).abs();
|
|
|
|
} break;
|
|
|
|
case Variant::VECTOR4I: {
|
|
|
|
return VariantInternalAccessor<Vector4i>::get(&x).abs();
|
|
|
|
} break;
|
2020-11-10 21:31:33 +00:00
|
|
|
default: {
|
|
|
|
r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
|
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline double absf(double x) {
|
|
|
|
return Math::absd(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int64_t absi(int64_t x) {
|
|
|
|
return ABS(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Variant sign(const Variant &x, Callable::CallError &r_error) {
|
|
|
|
r_error.error = Callable::CallError::CALL_OK;
|
|
|
|
switch (x.get_type()) {
|
|
|
|
case Variant::INT: {
|
2021-10-15 23:22:57 +00:00
|
|
|
return SIGN(VariantInternalAccessor<int64_t>::get(&x));
|
2020-11-10 21:31:33 +00:00
|
|
|
} break;
|
|
|
|
case Variant::FLOAT: {
|
2021-10-15 23:22:57 +00:00
|
|
|
return SIGN(VariantInternalAccessor<double>::get(&x));
|
2020-11-10 21:31:33 +00:00
|
|
|
} break;
|
|
|
|
case Variant::VECTOR2: {
|
|
|
|
return VariantInternalAccessor<Vector2>::get(&x).sign();
|
|
|
|
} break;
|
|
|
|
case Variant::VECTOR2I: {
|
|
|
|
return VariantInternalAccessor<Vector2i>::get(&x).sign();
|
|
|
|
} break;
|
|
|
|
case Variant::VECTOR3: {
|
|
|
|
return VariantInternalAccessor<Vector3>::get(&x).sign();
|
|
|
|
} break;
|
|
|
|
case Variant::VECTOR3I: {
|
|
|
|
return VariantInternalAccessor<Vector3i>::get(&x).sign();
|
|
|
|
} break;
|
Implement Vector4, Vector4i, Projection
Implement built-in classes Vector4, Vector4i and Projection.
* Two versions of Vector4 (float and integer).
* A Projection class, which is a 4x4 matrix specialized in projection types.
These types have been requested for a long time, but given they were very corner case they were not added before.
Because in Godot 4, reimplementing parts of the rendering engine is now possible, access to these types (heavily used by the rendering code) becomes a necessity.
**Q**: Why Projection and not Matrix4?
**A**: Godot does not use Matrix2, Matrix3, Matrix4x3, etc. naming convention because, within the engine, these types always have a *purpose*. As such, Godot names them: Transform2D, Transform3D or Basis. In this case, this 4x4 matrix is _always_ used as a _Projection_, hence the naming.
2022-07-19 23:11:13 +00:00
|
|
|
case Variant::VECTOR4: {
|
|
|
|
return VariantInternalAccessor<Vector4>::get(&x).sign();
|
|
|
|
} break;
|
|
|
|
case Variant::VECTOR4I: {
|
|
|
|
return VariantInternalAccessor<Vector4i>::get(&x).sign();
|
|
|
|
} break;
|
2020-11-10 21:31:33 +00:00
|
|
|
default: {
|
|
|
|
r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
|
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline double signf(double x) {
|
2021-10-15 23:22:57 +00:00
|
|
|
return SIGN(x);
|
2020-11-10 21:31:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int64_t signi(int64_t x) {
|
2021-10-15 23:22:57 +00:00
|
|
|
return SIGN(x);
|
2020-11-10 21:31:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline double pow(double x, double y) {
|
|
|
|
return Math::pow(x, y);
|
|
|
|
}
|
2020-11-17 10:21:08 +00:00
|
|
|
|
2020-11-10 21:31:33 +00:00
|
|
|
static inline double log(double x) {
|
|
|
|
return Math::log(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline double exp(double x) {
|
|
|
|
return Math::exp(x);
|
|
|
|
}
|
|
|
|
|
2020-11-17 10:21:08 +00:00
|
|
|
static inline bool is_nan(double x) {
|
2020-11-10 21:31:33 +00:00
|
|
|
return Math::is_nan(x);
|
|
|
|
}
|
|
|
|
|
2020-11-17 10:21:08 +00:00
|
|
|
static inline bool is_inf(double x) {
|
2020-11-10 21:31:33 +00:00
|
|
|
return Math::is_inf(x);
|
|
|
|
}
|
|
|
|
|
2020-11-17 10:21:08 +00:00
|
|
|
static inline bool is_equal_approx(double x, double y) {
|
2020-11-10 21:31:33 +00:00
|
|
|
return Math::is_equal_approx(x, y);
|
|
|
|
}
|
|
|
|
|
2020-11-17 10:21:08 +00:00
|
|
|
static inline bool is_zero_approx(double x) {
|
2020-11-10 21:31:33 +00:00
|
|
|
return Math::is_zero_approx(x);
|
|
|
|
}
|
|
|
|
|
2020-11-17 10:21:08 +00:00
|
|
|
static inline double ease(float x, float curve) {
|
|
|
|
return Math::ease(x, curve);
|
2020-11-10 21:31:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int step_decimals(float step) {
|
|
|
|
return Math::step_decimals(step);
|
|
|
|
}
|
|
|
|
|
2020-12-21 18:02:57 +00:00
|
|
|
static inline double snapped(double value, double step) {
|
|
|
|
return Math::snapped(value, step);
|
2020-11-10 21:31:33 +00:00
|
|
|
}
|
|
|
|
|
2022-05-10 14:08:35 +00:00
|
|
|
static inline Variant lerp(const Variant &from, const Variant &to, double weight, Callable::CallError &r_error) {
|
|
|
|
r_error.error = Callable::CallError::CALL_OK;
|
|
|
|
if (from.get_type() != to.get_type()) {
|
|
|
|
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
|
|
|
|
r_error.argument = 1;
|
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (from.get_type()) {
|
|
|
|
case Variant::FLOAT: {
|
|
|
|
return lerpf(VariantInternalAccessor<double>::get(&from), to, weight);
|
|
|
|
} break;
|
|
|
|
case Variant::VECTOR2: {
|
|
|
|
return VariantInternalAccessor<Vector2>::get(&from).lerp(VariantInternalAccessor<Vector2>::get(&to), weight);
|
|
|
|
} break;
|
|
|
|
case Variant::VECTOR3: {
|
|
|
|
return VariantInternalAccessor<Vector3>::get(&from).lerp(VariantInternalAccessor<Vector3>::get(&to), weight);
|
|
|
|
} break;
|
|
|
|
case Variant::VECTOR4: {
|
|
|
|
return VariantInternalAccessor<Vector4>::get(&from).lerp(VariantInternalAccessor<Vector4>::get(&to), weight);
|
|
|
|
} break;
|
|
|
|
case Variant::QUATERNION: {
|
|
|
|
return VariantInternalAccessor<Quaternion>::get(&from).slerp(VariantInternalAccessor<Quaternion>::get(&to), weight);
|
|
|
|
} break;
|
|
|
|
case Variant::BASIS: {
|
|
|
|
return VariantInternalAccessor<Basis>::get(&from).slerp(VariantInternalAccessor<Basis>::get(&to), weight);
|
|
|
|
} break;
|
|
|
|
case Variant::COLOR: {
|
|
|
|
return VariantInternalAccessor<Color>::get(&from).lerp(VariantInternalAccessor<Color>::get(&to), weight);
|
|
|
|
} break;
|
|
|
|
default: {
|
|
|
|
r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
|
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline double lerpf(double from, double to, double weight) {
|
2020-11-10 21:31:33 +00:00
|
|
|
return Math::lerp(from, to, weight);
|
|
|
|
}
|
|
|
|
|
2022-02-11 03:43:21 +00:00
|
|
|
static inline double cubic_interpolate(double from, double to, double pre, double post, double weight) {
|
|
|
|
return Math::cubic_interpolate(from, to, pre, post, weight);
|
|
|
|
}
|
|
|
|
|
2022-08-26 02:42:00 +00:00
|
|
|
static inline double cubic_interpolate_angle(double from, double to, double pre, double post, double weight) {
|
|
|
|
return Math::cubic_interpolate_angle(from, to, pre, post, weight);
|
|
|
|
}
|
|
|
|
|
2022-07-28 19:55:10 +00:00
|
|
|
static inline double cubic_interpolate_in_time(double from, double to, double pre, double post, double weight,
|
|
|
|
double to_t, double pre_t, double post_t) {
|
|
|
|
return Math::cubic_interpolate_in_time(from, to, pre, post, weight, to_t, pre_t, post_t);
|
|
|
|
}
|
|
|
|
|
2022-08-26 02:42:00 +00:00
|
|
|
static inline double cubic_interpolate_angle_in_time(double from, double to, double pre, double post, double weight,
|
|
|
|
double to_t, double pre_t, double post_t) {
|
|
|
|
return Math::cubic_interpolate_angle_in_time(from, to, pre, post, weight, to_t, pre_t, post_t);
|
|
|
|
}
|
|
|
|
|
2022-06-27 17:41:32 +00:00
|
|
|
static inline double bezier_interpolate(double p_start, double p_control_1, double p_control_2, double p_end, double p_t) {
|
|
|
|
return Math::bezier_interpolate(p_start, p_control_1, p_control_2, p_end, p_t);
|
|
|
|
}
|
|
|
|
|
2020-11-10 21:31:33 +00:00
|
|
|
static inline double lerp_angle(double from, double to, double weight) {
|
|
|
|
return Math::lerp_angle(from, to, weight);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline double inverse_lerp(double from, double to, double weight) {
|
|
|
|
return Math::inverse_lerp(from, to, weight);
|
|
|
|
}
|
|
|
|
|
2022-09-05 12:05:50 +00:00
|
|
|
static inline double remap(double value, double istart, double istop, double ostart, double ostop) {
|
|
|
|
return Math::remap(value, istart, istop, ostart, ostop);
|
2020-11-10 21:31:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline double smoothstep(double from, double to, double val) {
|
|
|
|
return Math::smoothstep(from, to, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline double move_toward(double from, double to, double delta) {
|
|
|
|
return Math::move_toward(from, to, delta);
|
|
|
|
}
|
|
|
|
|
2022-08-13 15:45:42 +00:00
|
|
|
static inline double deg_to_rad(double angle_deg) {
|
|
|
|
return Math::deg_to_rad(angle_deg);
|
2020-11-10 21:31:33 +00:00
|
|
|
}
|
|
|
|
|
2022-08-13 15:45:42 +00:00
|
|
|
static inline double rad_to_deg(double angle_rad) {
|
|
|
|
return Math::rad_to_deg(angle_rad);
|
2020-11-10 21:31:33 +00:00
|
|
|
}
|
|
|
|
|
2022-08-13 15:45:42 +00:00
|
|
|
static inline double linear_to_db(double linear) {
|
|
|
|
return Math::linear_to_db(linear);
|
2020-11-10 21:31:33 +00:00
|
|
|
}
|
|
|
|
|
2022-08-13 15:45:42 +00:00
|
|
|
static inline double db_to_linear(double db) {
|
|
|
|
return Math::db_to_linear(db);
|
2020-11-10 21:31:33 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 09:37:55 +00:00
|
|
|
static inline Variant wrap(const Variant &p_x, const Variant &p_min, const Variant &p_max, Callable::CallError &r_error) {
|
|
|
|
Variant::Type x_type = p_x.get_type();
|
|
|
|
if (x_type != Variant::INT && x_type != Variant::FLOAT) {
|
|
|
|
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
|
|
|
|
r_error.argument = 0;
|
|
|
|
r_error.expected = x_type;
|
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
|
|
|
|
Variant::Type min_type = p_min.get_type();
|
|
|
|
if (min_type != Variant::INT && min_type != Variant::FLOAT) {
|
|
|
|
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
|
|
|
|
r_error.argument = 1;
|
|
|
|
r_error.expected = x_type;
|
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
|
|
|
|
Variant::Type max_type = p_max.get_type();
|
|
|
|
if (max_type != Variant::INT && max_type != Variant::FLOAT) {
|
|
|
|
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
|
|
|
|
r_error.argument = 2;
|
|
|
|
r_error.expected = x_type;
|
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
|
|
|
|
Variant value;
|
|
|
|
|
|
|
|
switch (x_type) {
|
|
|
|
case Variant::INT: {
|
|
|
|
if (x_type != min_type || x_type != max_type) {
|
|
|
|
value = wrapf((double)p_x, (double)p_min, (double)p_max);
|
|
|
|
} else {
|
|
|
|
value = wrapi((int)p_x, (int)p_min, (int)p_max);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case Variant::FLOAT: {
|
|
|
|
value = wrapf((double)p_x, (double)p_min, (double)p_max);
|
|
|
|
} break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
r_error.error = Callable::CallError::CALL_OK;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2020-11-10 21:31:33 +00:00
|
|
|
static inline int64_t wrapi(int64_t value, int64_t min, int64_t max) {
|
|
|
|
return Math::wrapi(value, min, max);
|
|
|
|
}
|
2020-11-17 10:21:08 +00:00
|
|
|
|
2020-11-10 21:31:33 +00:00
|
|
|
static inline double wrapf(double value, double min, double max) {
|
|
|
|
return Math::wrapf(value, min, max);
|
|
|
|
}
|
|
|
|
|
2021-10-15 13:25:00 +00:00
|
|
|
static inline double pingpong(double value, double length) {
|
|
|
|
return Math::pingpong(value, length);
|
|
|
|
}
|
|
|
|
|
2020-11-10 21:31:33 +00:00
|
|
|
static inline Variant max(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
|
|
|
|
if (p_argcount < 2) {
|
|
|
|
r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
|
|
|
|
r_error.expected = 2;
|
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
Variant base = *p_args[0];
|
|
|
|
Variant ret;
|
|
|
|
for (int i = 1; i < p_argcount; i++) {
|
|
|
|
bool valid;
|
2020-12-24 17:03:50 +00:00
|
|
|
Variant::evaluate(Variant::OP_LESS, base, *p_args[i], ret, valid);
|
2020-11-10 21:31:33 +00:00
|
|
|
if (!valid) {
|
|
|
|
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
|
|
|
|
r_error.expected = base.get_type();
|
|
|
|
r_error.argument = i;
|
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
if (ret.booleanize()) {
|
|
|
|
base = *p_args[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r_error.error = Callable::CallError::CALL_OK;
|
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline double maxf(double x, double y) {
|
|
|
|
return MAX(x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int64_t maxi(int64_t x, int64_t y) {
|
|
|
|
return MAX(x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Variant min(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
|
|
|
|
if (p_argcount < 2) {
|
|
|
|
r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
|
|
|
|
r_error.expected = 2;
|
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
Variant base = *p_args[0];
|
|
|
|
Variant ret;
|
|
|
|
for (int i = 1; i < p_argcount; i++) {
|
|
|
|
bool valid;
|
2020-12-24 17:03:50 +00:00
|
|
|
Variant::evaluate(Variant::OP_GREATER, base, *p_args[i], ret, valid);
|
2020-11-10 21:31:33 +00:00
|
|
|
if (!valid) {
|
|
|
|
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
|
|
|
|
r_error.expected = base.get_type();
|
|
|
|
r_error.argument = i;
|
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
if (ret.booleanize()) {
|
|
|
|
base = *p_args[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r_error.error = Callable::CallError::CALL_OK;
|
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline double minf(double x, double y) {
|
|
|
|
return MIN(x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int64_t mini(int64_t x, int64_t y) {
|
|
|
|
return MIN(x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Variant clamp(const Variant &x, const Variant &min, const Variant &max, Callable::CallError &r_error) {
|
|
|
|
Variant value = x;
|
|
|
|
|
|
|
|
Variant ret;
|
|
|
|
|
|
|
|
bool valid;
|
|
|
|
Variant::evaluate(Variant::OP_LESS, value, min, ret, valid);
|
|
|
|
if (!valid) {
|
|
|
|
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
|
|
|
|
r_error.expected = value.get_type();
|
|
|
|
r_error.argument = 1;
|
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
if (ret.booleanize()) {
|
|
|
|
value = min;
|
|
|
|
}
|
|
|
|
Variant::evaluate(Variant::OP_GREATER, value, max, ret, valid);
|
|
|
|
if (!valid) {
|
|
|
|
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
|
|
|
|
r_error.expected = value.get_type();
|
|
|
|
r_error.argument = 2;
|
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
if (ret.booleanize()) {
|
|
|
|
value = max;
|
|
|
|
}
|
|
|
|
|
|
|
|
r_error.error = Callable::CallError::CALL_OK;
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline double clampf(double x, double min, double max) {
|
|
|
|
return CLAMP(x, min, max);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int64_t clampi(int64_t x, int64_t min, int64_t max) {
|
|
|
|
return CLAMP(x, min, max);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int64_t nearest_po2(int64_t x) {
|
|
|
|
return nearest_power_of_2_templated(uint64_t(x));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Random
|
|
|
|
|
|
|
|
static inline void randomize() {
|
|
|
|
Math::randomize();
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int64_t randi() {
|
|
|
|
return Math::rand();
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline double randf() {
|
|
|
|
return Math::randf();
|
|
|
|
}
|
|
|
|
|
2021-06-21 09:58:31 +00:00
|
|
|
static inline double randfn(double mean, double deviation) {
|
|
|
|
return Math::randfn(mean, deviation);
|
|
|
|
}
|
|
|
|
|
2020-11-10 21:31:33 +00:00
|
|
|
static inline int64_t randi_range(int64_t from, int64_t to) {
|
|
|
|
return Math::random((int32_t)from, (int32_t)to);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline double randf_range(double from, double to) {
|
|
|
|
return Math::random(from, to);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void seed(int64_t s) {
|
|
|
|
return Math::seed(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline PackedInt64Array rand_from_seed(int64_t seed) {
|
|
|
|
uint64_t s = seed;
|
|
|
|
PackedInt64Array arr;
|
|
|
|
arr.resize(2);
|
|
|
|
arr.write[0] = Math::rand_from_seed(&s);
|
|
|
|
arr.write[1] = s;
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Utility
|
|
|
|
|
|
|
|
static inline Variant weakref(const Variant &obj, Callable::CallError &r_error) {
|
|
|
|
if (obj.get_type() == Variant::OBJECT) {
|
|
|
|
r_error.error = Callable::CallError::CALL_OK;
|
2021-08-26 19:37:17 +00:00
|
|
|
if (obj.is_ref_counted()) {
|
2020-11-10 21:31:33 +00:00
|
|
|
Ref<WeakRef> wref = memnew(WeakRef);
|
2022-05-02 23:43:50 +00:00
|
|
|
Ref<RefCounted> r = obj;
|
2020-11-10 21:31:33 +00:00
|
|
|
if (r.is_valid()) {
|
|
|
|
wref->set_ref(r);
|
|
|
|
}
|
|
|
|
return wref;
|
|
|
|
} else {
|
|
|
|
Ref<WeakRef> wref = memnew(WeakRef);
|
|
|
|
Object *o = obj.get_validated_object();
|
|
|
|
if (o) {
|
|
|
|
wref->set_obj(o);
|
|
|
|
}
|
|
|
|
return wref;
|
|
|
|
}
|
|
|
|
} else if (obj.get_type() == Variant::NIL) {
|
|
|
|
r_error.error = Callable::CallError::CALL_OK;
|
|
|
|
Ref<WeakRef> wref = memnew(WeakRef);
|
|
|
|
return wref;
|
|
|
|
} else {
|
|
|
|
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
|
|
|
|
r_error.argument = 0;
|
|
|
|
r_error.expected = Variant::OBJECT;
|
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int64_t _typeof(const Variant &obj) {
|
|
|
|
return obj.get_type();
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline String str(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
|
|
|
|
if (p_arg_count < 1) {
|
|
|
|
r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
|
|
|
|
r_error.argument = 1;
|
|
|
|
return String();
|
|
|
|
}
|
2022-04-05 10:40:26 +00:00
|
|
|
String s;
|
2020-11-10 21:31:33 +00:00
|
|
|
for (int i = 0; i < p_arg_count; i++) {
|
|
|
|
String os = p_args[i]->operator String();
|
|
|
|
|
|
|
|
if (i == 0) {
|
2022-04-05 10:40:26 +00:00
|
|
|
s = os;
|
2020-11-10 21:31:33 +00:00
|
|
|
} else {
|
2022-04-05 10:40:26 +00:00
|
|
|
s += os;
|
2020-11-10 21:31:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r_error.error = Callable::CallError::CALL_OK;
|
|
|
|
|
2022-04-05 10:40:26 +00:00
|
|
|
return s;
|
2020-11-10 21:31:33 +00:00
|
|
|
}
|
|
|
|
|
2020-05-23 20:38:48 +00:00
|
|
|
static inline String error_string(Error error) {
|
|
|
|
if (error < 0 || error >= ERR_MAX) {
|
|
|
|
return String("(invalid error code)");
|
|
|
|
}
|
|
|
|
|
|
|
|
return String(error_names[error]);
|
|
|
|
}
|
|
|
|
|
2020-11-10 21:31:33 +00:00
|
|
|
static inline void print(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
|
2022-04-05 10:40:26 +00:00
|
|
|
String s;
|
2020-11-10 21:31:33 +00:00
|
|
|
for (int i = 0; i < p_arg_count; i++) {
|
|
|
|
String os = p_args[i]->operator String();
|
|
|
|
|
|
|
|
if (i == 0) {
|
2022-04-05 10:40:26 +00:00
|
|
|
s = os;
|
2020-11-10 21:31:33 +00:00
|
|
|
} else {
|
2022-04-05 10:40:26 +00:00
|
|
|
s += os;
|
2020-11-10 21:31:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-05 10:40:26 +00:00
|
|
|
print_line(s);
|
2020-11-10 21:31:33 +00:00
|
|
|
r_error.error = Callable::CallError::CALL_OK;
|
|
|
|
}
|
|
|
|
|
2022-05-02 23:29:38 +00:00
|
|
|
static inline void print_rich(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
|
|
|
|
String s;
|
|
|
|
for (int i = 0; i < p_arg_count; i++) {
|
|
|
|
String os = p_args[i]->operator String();
|
|
|
|
|
|
|
|
if (i == 0) {
|
|
|
|
s = os;
|
|
|
|
} else {
|
|
|
|
s += os;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
print_line_rich(s);
|
|
|
|
r_error.error = Callable::CallError::CALL_OK;
|
|
|
|
}
|
|
|
|
|
2021-09-20 17:14:27 +00:00
|
|
|
static inline void print_verbose(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
|
|
|
|
if (OS::get_singleton()->is_stdout_verbose()) {
|
2022-04-05 10:40:26 +00:00
|
|
|
String s;
|
2021-09-20 17:14:27 +00:00
|
|
|
for (int i = 0; i < p_arg_count; i++) {
|
|
|
|
String os = p_args[i]->operator String();
|
|
|
|
|
|
|
|
if (i == 0) {
|
2022-04-05 10:40:26 +00:00
|
|
|
s = os;
|
2021-09-20 17:14:27 +00:00
|
|
|
} else {
|
2022-04-05 10:40:26 +00:00
|
|
|
s += os;
|
2021-09-20 17:14:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No need to use `print_verbose()` as this call already only happens
|
|
|
|
// when verbose mode is enabled. This avoids performing string argument concatenation
|
|
|
|
// when not needed.
|
2022-04-05 10:40:26 +00:00
|
|
|
print_line(s);
|
2020-11-10 21:31:33 +00:00
|
|
|
}
|
2021-09-20 17:14:27 +00:00
|
|
|
|
|
|
|
r_error.error = Callable::CallError::CALL_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void printerr(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
|
2022-04-05 10:40:26 +00:00
|
|
|
String s;
|
2020-11-10 21:31:33 +00:00
|
|
|
for (int i = 0; i < p_arg_count; i++) {
|
|
|
|
String os = p_args[i]->operator String();
|
|
|
|
|
|
|
|
if (i == 0) {
|
2022-04-05 10:40:26 +00:00
|
|
|
s = os;
|
2020-11-10 21:31:33 +00:00
|
|
|
} else {
|
2022-04-05 10:40:26 +00:00
|
|
|
s += os;
|
2020-11-10 21:31:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-05 10:40:26 +00:00
|
|
|
print_error(s);
|
2020-11-10 21:31:33 +00:00
|
|
|
r_error.error = Callable::CallError::CALL_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void printt(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
|
2022-04-05 10:40:26 +00:00
|
|
|
String s;
|
2020-11-10 21:31:33 +00:00
|
|
|
for (int i = 0; i < p_arg_count; i++) {
|
|
|
|
if (i) {
|
2022-04-05 10:40:26 +00:00
|
|
|
s += "\t";
|
2020-11-10 21:31:33 +00:00
|
|
|
}
|
2022-04-05 10:40:26 +00:00
|
|
|
s += p_args[i]->operator String();
|
2020-11-10 21:31:33 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 10:40:26 +00:00
|
|
|
print_line(s);
|
2020-11-10 21:31:33 +00:00
|
|
|
r_error.error = Callable::CallError::CALL_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void prints(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
|
2022-04-05 10:40:26 +00:00
|
|
|
String s;
|
2020-11-10 21:31:33 +00:00
|
|
|
for (int i = 0; i < p_arg_count; i++) {
|
|
|
|
if (i) {
|
2022-04-05 10:40:26 +00:00
|
|
|
s += " ";
|
2020-11-10 21:31:33 +00:00
|
|
|
}
|
2022-04-05 10:40:26 +00:00
|
|
|
s += p_args[i]->operator String();
|
2020-11-10 21:31:33 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 10:40:26 +00:00
|
|
|
print_line(s);
|
2020-11-10 21:31:33 +00:00
|
|
|
r_error.error = Callable::CallError::CALL_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void printraw(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
|
2022-04-05 10:40:26 +00:00
|
|
|
String s;
|
2020-11-10 21:31:33 +00:00
|
|
|
for (int i = 0; i < p_arg_count; i++) {
|
|
|
|
String os = p_args[i]->operator String();
|
|
|
|
|
|
|
|
if (i == 0) {
|
2022-04-05 10:40:26 +00:00
|
|
|
s = os;
|
2020-11-10 21:31:33 +00:00
|
|
|
} else {
|
2022-04-05 10:40:26 +00:00
|
|
|
s += os;
|
2020-11-10 21:31:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-05 10:40:26 +00:00
|
|
|
OS::get_singleton()->print("%s", s.utf8().get_data());
|
2020-11-10 21:31:33 +00:00
|
|
|
r_error.error = Callable::CallError::CALL_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void push_error(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
|
|
|
|
if (p_arg_count < 1) {
|
|
|
|
r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
|
|
|
|
r_error.argument = 1;
|
|
|
|
}
|
2022-04-05 10:40:26 +00:00
|
|
|
String s;
|
2020-11-10 21:31:33 +00:00
|
|
|
for (int i = 0; i < p_arg_count; i++) {
|
|
|
|
String os = p_args[i]->operator String();
|
|
|
|
|
|
|
|
if (i == 0) {
|
2022-04-05 10:40:26 +00:00
|
|
|
s = os;
|
2020-11-10 21:31:33 +00:00
|
|
|
} else {
|
2022-04-05 10:40:26 +00:00
|
|
|
s += os;
|
2020-11-10 21:31:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-05 10:40:26 +00:00
|
|
|
ERR_PRINT(s);
|
2020-11-10 21:31:33 +00:00
|
|
|
r_error.error = Callable::CallError::CALL_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void push_warning(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
|
|
|
|
if (p_arg_count < 1) {
|
|
|
|
r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
|
|
|
|
r_error.argument = 1;
|
|
|
|
}
|
2022-04-05 10:40:26 +00:00
|
|
|
String s;
|
2020-11-10 21:31:33 +00:00
|
|
|
for (int i = 0; i < p_arg_count; i++) {
|
|
|
|
String os = p_args[i]->operator String();
|
|
|
|
|
|
|
|
if (i == 0) {
|
2022-04-05 10:40:26 +00:00
|
|
|
s = os;
|
2020-11-10 21:31:33 +00:00
|
|
|
} else {
|
2022-04-05 10:40:26 +00:00
|
|
|
s += os;
|
2020-11-10 21:31:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-05 10:40:26 +00:00
|
|
|
WARN_PRINT(s);
|
2020-11-10 21:31:33 +00:00
|
|
|
r_error.error = Callable::CallError::CALL_OK;
|
|
|
|
}
|
|
|
|
|
2022-08-13 15:45:42 +00:00
|
|
|
static inline String var_to_str(const Variant &p_var) {
|
2020-11-10 21:31:33 +00:00
|
|
|
String vars;
|
|
|
|
VariantWriter::write_to_string(p_var, vars);
|
|
|
|
return vars;
|
|
|
|
}
|
|
|
|
|
2022-08-13 15:45:42 +00:00
|
|
|
static inline Variant str_to_var(const String &p_var) {
|
2020-11-10 21:31:33 +00:00
|
|
|
VariantParser::StreamString ss;
|
|
|
|
ss.s = p_var;
|
|
|
|
|
|
|
|
String errs;
|
|
|
|
int line;
|
|
|
|
Variant ret;
|
|
|
|
(void)VariantParser::parse(&ss, ret, errs, line);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-08-13 15:45:42 +00:00
|
|
|
static inline PackedByteArray var_to_bytes(const Variant &p_var) {
|
2020-11-10 21:31:33 +00:00
|
|
|
int len;
|
|
|
|
Error err = encode_variant(p_var, nullptr, len, false);
|
|
|
|
if (err != OK) {
|
|
|
|
return PackedByteArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
PackedByteArray barr;
|
|
|
|
barr.resize(len);
|
|
|
|
{
|
|
|
|
uint8_t *w = barr.ptrw();
|
|
|
|
err = encode_variant(p_var, w, len, false);
|
|
|
|
if (err != OK) {
|
|
|
|
return PackedByteArray();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return barr;
|
|
|
|
}
|
|
|
|
|
2022-08-13 15:45:42 +00:00
|
|
|
static inline PackedByteArray var_to_bytes_with_objects(const Variant &p_var) {
|
2020-11-10 21:31:33 +00:00
|
|
|
int len;
|
|
|
|
Error err = encode_variant(p_var, nullptr, len, true);
|
|
|
|
if (err != OK) {
|
|
|
|
return PackedByteArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
PackedByteArray barr;
|
|
|
|
barr.resize(len);
|
|
|
|
{
|
|
|
|
uint8_t *w = barr.ptrw();
|
|
|
|
err = encode_variant(p_var, w, len, true);
|
|
|
|
if (err != OK) {
|
|
|
|
return PackedByteArray();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return barr;
|
|
|
|
}
|
|
|
|
|
2022-08-13 15:45:42 +00:00
|
|
|
static inline Variant bytes_to_var(const PackedByteArray &p_arr) {
|
2020-11-10 21:31:33 +00:00
|
|
|
Variant ret;
|
|
|
|
{
|
|
|
|
const uint8_t *r = p_arr.ptr();
|
|
|
|
Error err = decode_variant(ret, r, p_arr.size(), nullptr, false);
|
|
|
|
if (err != OK) {
|
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-08-13 15:45:42 +00:00
|
|
|
static inline Variant bytes_to_var_with_objects(const PackedByteArray &p_arr) {
|
2020-11-10 21:31:33 +00:00
|
|
|
Variant ret;
|
|
|
|
{
|
|
|
|
const uint8_t *r = p_arr.ptr();
|
|
|
|
Error err = decode_variant(ret, r, p_arr.size(), nullptr, true);
|
|
|
|
if (err != OK) {
|
|
|
|
return Variant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int64_t hash(const Variant &p_arr) {
|
|
|
|
return p_arr.hash();
|
|
|
|
}
|
|
|
|
|
2020-11-17 10:21:08 +00:00
|
|
|
static inline Object *instance_from_id(int64_t p_id) {
|
2020-11-10 21:31:33 +00:00
|
|
|
ObjectID id = ObjectID((uint64_t)p_id);
|
2020-11-17 10:21:08 +00:00
|
|
|
Object *ret = ObjectDB::get_instance(id);
|
2020-11-10 21:31:33 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool is_instance_id_valid(int64_t p_id) {
|
|
|
|
return ObjectDB::get_instance(ObjectID((uint64_t)p_id)) != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool is_instance_valid(const Variant &p_instance) {
|
|
|
|
if (p_instance.get_type() != Variant::OBJECT) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return p_instance.get_validated_object() != nullptr;
|
|
|
|
}
|
2021-08-23 23:57:52 +00:00
|
|
|
|
|
|
|
static inline uint64_t rid_allocate_id() {
|
|
|
|
return RID_AllocBase::_gen_id();
|
|
|
|
}
|
|
|
|
static inline RID rid_from_int64(uint64_t p_base) {
|
|
|
|
return RID::from_uint64(p_base);
|
|
|
|
}
|
2020-11-10 21:31:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef DEBUG_METHODS_ENABLED
|
|
|
|
#define VCALLR *ret = p_func(VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...)
|
|
|
|
#define VCALL p_func(VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...)
|
|
|
|
#else
|
|
|
|
#define VCALLR *ret = p_func(VariantCaster<P>::cast(*p_args[Is])...)
|
|
|
|
#define VCALL p_func(VariantCaster<P>::cast(*p_args[Is])...)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
template <class R, class... P, size_t... Is>
|
|
|
|
static _FORCE_INLINE_ void call_helperpr(R (*p_func)(P...), Variant *ret, const Variant **p_args, Callable::CallError &r_error, IndexSequence<Is...>) {
|
|
|
|
r_error.error = Callable::CallError::CALL_OK;
|
|
|
|
VCALLR;
|
2020-11-11 16:16:08 +00:00
|
|
|
(void)p_args; // avoid gcc warning
|
2020-11-10 21:31:33 +00:00
|
|
|
(void)r_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class R, class... P, size_t... Is>
|
|
|
|
static _FORCE_INLINE_ void validated_call_helperpr(R (*p_func)(P...), Variant *ret, const Variant **p_args, IndexSequence<Is...>) {
|
|
|
|
*ret = p_func(VariantCaster<P>::cast(*p_args[Is])...);
|
|
|
|
(void)p_args;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class R, class... P, size_t... Is>
|
|
|
|
static _FORCE_INLINE_ void ptr_call_helperpr(R (*p_func)(P...), void *ret, const void **p_args, IndexSequence<Is...>) {
|
|
|
|
PtrToArg<R>::encode(p_func(PtrToArg<P>::convert(p_args[Is])...), ret);
|
|
|
|
(void)p_args;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class R, class... P>
|
|
|
|
static _FORCE_INLINE_ void call_helperr(R (*p_func)(P...), Variant *ret, const Variant **p_args, Callable::CallError &r_error) {
|
|
|
|
call_helperpr(p_func, ret, p_args, r_error, BuildIndexSequence<sizeof...(P)>{});
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class R, class... P>
|
|
|
|
static _FORCE_INLINE_ void validated_call_helperr(R (*p_func)(P...), Variant *ret, const Variant **p_args) {
|
|
|
|
validated_call_helperpr(p_func, ret, p_args, BuildIndexSequence<sizeof...(P)>{});
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class R, class... P>
|
|
|
|
static _FORCE_INLINE_ void ptr_call_helperr(R (*p_func)(P...), void *ret, const void **p_args) {
|
|
|
|
ptr_call_helperpr(p_func, ret, p_args, BuildIndexSequence<sizeof...(P)>{});
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class R, class... P>
|
|
|
|
static _FORCE_INLINE_ int get_arg_count_helperr(R (*p_func)(P...)) {
|
|
|
|
return sizeof...(P);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class R, class... P>
|
|
|
|
static _FORCE_INLINE_ Variant::Type get_arg_type_helperr(R (*p_func)(P...), int p_arg) {
|
|
|
|
return call_get_argument_type<P...>(p_arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class R, class... P>
|
|
|
|
static _FORCE_INLINE_ Variant::Type get_ret_type_helperr(R (*p_func)(P...)) {
|
|
|
|
return GetTypeInfo<R>::VARIANT_TYPE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// WITHOUT RET
|
|
|
|
|
|
|
|
template <class... P, size_t... Is>
|
|
|
|
static _FORCE_INLINE_ void call_helperp(void (*p_func)(P...), const Variant **p_args, Callable::CallError &r_error, IndexSequence<Is...>) {
|
|
|
|
r_error.error = Callable::CallError::CALL_OK;
|
|
|
|
VCALL;
|
|
|
|
(void)p_args;
|
|
|
|
(void)r_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class... P, size_t... Is>
|
|
|
|
static _FORCE_INLINE_ void validated_call_helperp(void (*p_func)(P...), const Variant **p_args, IndexSequence<Is...>) {
|
|
|
|
p_func(VariantCaster<P>::cast(*p_args[Is])...);
|
|
|
|
(void)p_args;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class... P, size_t... Is>
|
|
|
|
static _FORCE_INLINE_ void ptr_call_helperp(void (*p_func)(P...), const void **p_args, IndexSequence<Is...>) {
|
|
|
|
p_func(PtrToArg<P>::convert(p_args[Is])...);
|
|
|
|
(void)p_args;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class... P>
|
|
|
|
static _FORCE_INLINE_ void call_helper(void (*p_func)(P...), const Variant **p_args, Callable::CallError &r_error) {
|
|
|
|
call_helperp(p_func, p_args, r_error, BuildIndexSequence<sizeof...(P)>{});
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class... P>
|
|
|
|
static _FORCE_INLINE_ void validated_call_helper(void (*p_func)(P...), const Variant **p_args) {
|
|
|
|
validated_call_helperp(p_func, p_args, BuildIndexSequence<sizeof...(P)>{});
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class... P>
|
|
|
|
static _FORCE_INLINE_ void ptr_call_helper(void (*p_func)(P...), const void **p_args) {
|
|
|
|
ptr_call_helperp(p_func, p_args, BuildIndexSequence<sizeof...(P)>{});
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class... P>
|
|
|
|
static _FORCE_INLINE_ int get_arg_count_helper(void (*p_func)(P...)) {
|
|
|
|
return sizeof...(P);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class... P>
|
|
|
|
static _FORCE_INLINE_ Variant::Type get_arg_type_helper(void (*p_func)(P...), int p_arg) {
|
|
|
|
return call_get_argument_type<P...>(p_arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class... P>
|
|
|
|
static _FORCE_INLINE_ Variant::Type get_ret_type_helper(void (*p_func)(P...)) {
|
|
|
|
return Variant::NIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define FUNCBINDR(m_func, m_args, m_category) \
|
|
|
|
class Func_##m_func { \
|
|
|
|
public: \
|
|
|
|
static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \
|
2020-11-11 16:16:08 +00:00
|
|
|
call_helperr(VariantUtilityFunctions::m_func, r_ret, p_args, r_error); \
|
2020-11-10 21:31:33 +00:00
|
|
|
} \
|
|
|
|
static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \
|
2020-11-11 16:16:08 +00:00
|
|
|
validated_call_helperr(VariantUtilityFunctions::m_func, r_ret, p_args); \
|
2020-11-10 21:31:33 +00:00
|
|
|
} \
|
|
|
|
static void ptrcall(void *ret, const void **p_args, int p_argcount) { \
|
2020-11-11 16:16:08 +00:00
|
|
|
ptr_call_helperr(VariantUtilityFunctions::m_func, ret, p_args); \
|
2020-11-10 21:31:33 +00:00
|
|
|
} \
|
|
|
|
static int get_argument_count() { \
|
2020-11-11 16:16:08 +00:00
|
|
|
return get_arg_count_helperr(VariantUtilityFunctions::m_func); \
|
2020-11-10 21:31:33 +00:00
|
|
|
} \
|
|
|
|
static Variant::Type get_argument_type(int p_arg) { \
|
2020-11-11 16:16:08 +00:00
|
|
|
return get_arg_type_helperr(VariantUtilityFunctions::m_func, p_arg); \
|
2020-11-10 21:31:33 +00:00
|
|
|
} \
|
|
|
|
static Variant::Type get_return_type() { \
|
2020-11-11 16:16:08 +00:00
|
|
|
return get_ret_type_helperr(VariantUtilityFunctions::m_func); \
|
2020-11-10 21:31:33 +00:00
|
|
|
} \
|
|
|
|
static bool has_return_type() { \
|
|
|
|
return true; \
|
|
|
|
} \
|
|
|
|
static bool is_vararg() { return false; } \
|
2020-11-11 16:16:08 +00:00
|
|
|
static Variant::UtilityFunctionType get_type() { return m_category; } \
|
2020-11-10 21:31:33 +00:00
|
|
|
}; \
|
2020-11-11 16:16:08 +00:00
|
|
|
register_utility_function<Func_##m_func>(#m_func, m_args)
|
2020-11-10 21:31:33 +00:00
|
|
|
|
|
|
|
#define FUNCBINDVR(m_func, m_args, m_category) \
|
|
|
|
class Func_##m_func { \
|
|
|
|
public: \
|
|
|
|
static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \
|
|
|
|
r_error.error = Callable::CallError::CALL_OK; \
|
2020-11-11 16:16:08 +00:00
|
|
|
*r_ret = VariantUtilityFunctions::m_func(*p_args[0], r_error); \
|
2020-11-10 21:31:33 +00:00
|
|
|
} \
|
|
|
|
static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \
|
|
|
|
Callable::CallError ce; \
|
2020-11-11 16:16:08 +00:00
|
|
|
*r_ret = VariantUtilityFunctions::m_func(*p_args[0], ce); \
|
2020-11-10 21:31:33 +00:00
|
|
|
} \
|
|
|
|
static void ptrcall(void *ret, const void **p_args, int p_argcount) { \
|
|
|
|
Callable::CallError ce; \
|
2020-11-11 16:16:08 +00:00
|
|
|
PtrToArg<Variant>::encode(VariantUtilityFunctions::m_func(PtrToArg<Variant>::convert(p_args[0]), ce), ret); \
|
2020-11-10 21:31:33 +00:00
|
|
|
} \
|
|
|
|
static int get_argument_count() { \
|
|
|
|
return 1; \
|
|
|
|
} \
|
|
|
|
static Variant::Type get_argument_type(int p_arg) { \
|
|
|
|
return Variant::NIL; \
|
|
|
|
} \
|
|
|
|
static Variant::Type get_return_type() { \
|
|
|
|
return Variant::NIL; \
|
|
|
|
} \
|
|
|
|
static bool has_return_type() { \
|
|
|
|
return true; \
|
|
|
|
} \
|
|
|
|
static bool is_vararg() { return false; } \
|
2020-11-11 16:16:08 +00:00
|
|
|
static Variant::UtilityFunctionType get_type() { return m_category; } \
|
2020-11-10 21:31:33 +00:00
|
|
|
}; \
|
2020-11-11 16:16:08 +00:00
|
|
|
register_utility_function<Func_##m_func>(#m_func, m_args)
|
2020-11-10 21:31:33 +00:00
|
|
|
|
|
|
|
#define FUNCBINDVR3(m_func, m_args, m_category) \
|
|
|
|
class Func_##m_func { \
|
|
|
|
public: \
|
|
|
|
static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \
|
|
|
|
r_error.error = Callable::CallError::CALL_OK; \
|
2020-11-11 16:16:08 +00:00
|
|
|
*r_ret = VariantUtilityFunctions::m_func(*p_args[0], *p_args[1], *p_args[2], r_error); \
|
2020-11-10 21:31:33 +00:00
|
|
|
} \
|
|
|
|
static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \
|
|
|
|
Callable::CallError ce; \
|
2020-11-11 16:16:08 +00:00
|
|
|
*r_ret = VariantUtilityFunctions::m_func(*p_args[0], *p_args[1], *p_args[2], ce); \
|
2020-11-10 21:31:33 +00:00
|
|
|
} \
|
|
|
|
static void ptrcall(void *ret, const void **p_args, int p_argcount) { \
|
|
|
|
Callable::CallError ce; \
|
|
|
|
Variant r; \
|
2020-11-11 16:16:08 +00:00
|
|
|
r = VariantUtilityFunctions::m_func(PtrToArg<Variant>::convert(p_args[0]), PtrToArg<Variant>::convert(p_args[1]), PtrToArg<Variant>::convert(p_args[2]), ce); \
|
2020-11-10 21:31:33 +00:00
|
|
|
PtrToArg<Variant>::encode(r, ret); \
|
|
|
|
} \
|
|
|
|
static int get_argument_count() { \
|
|
|
|
return 3; \
|
|
|
|
} \
|
|
|
|
static Variant::Type get_argument_type(int p_arg) { \
|
|
|
|
return Variant::NIL; \
|
|
|
|
} \
|
|
|
|
static Variant::Type get_return_type() { \
|
|
|
|
return Variant::NIL; \
|
|
|
|
} \
|
|
|
|
static bool has_return_type() { \
|
|
|
|
return true; \
|
|
|
|
} \
|
|
|
|
static bool is_vararg() { return false; } \
|
2020-11-11 16:16:08 +00:00
|
|
|
static Variant::UtilityFunctionType get_type() { return m_category; } \
|
2020-11-10 21:31:33 +00:00
|
|
|
}; \
|
2020-11-11 16:16:08 +00:00
|
|
|
register_utility_function<Func_##m_func>(#m_func, m_args)
|
2020-11-10 21:31:33 +00:00
|
|
|
|
|
|
|
#define FUNCBINDVARARG(m_func, m_args, m_category) \
|
|
|
|
class Func_##m_func { \
|
|
|
|
public: \
|
|
|
|
static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \
|
|
|
|
r_error.error = Callable::CallError::CALL_OK; \
|
2020-11-11 16:16:08 +00:00
|
|
|
*r_ret = VariantUtilityFunctions::m_func(p_args, p_argcount, r_error); \
|
2020-11-10 21:31:33 +00:00
|
|
|
} \
|
|
|
|
static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \
|
|
|
|
Callable::CallError c; \
|
2020-11-11 16:16:08 +00:00
|
|
|
*r_ret = VariantUtilityFunctions::m_func(p_args, p_argcount, c); \
|
2020-11-10 21:31:33 +00:00
|
|
|
} \
|
|
|
|
static void ptrcall(void *ret, const void **p_args, int p_argcount) { \
|
|
|
|
Vector<Variant> args; \
|
|
|
|
for (int i = 0; i < p_argcount; i++) { \
|
|
|
|
args.push_back(PtrToArg<Variant>::convert(p_args[i])); \
|
|
|
|
} \
|
|
|
|
Vector<const Variant *> argsp; \
|
|
|
|
for (int i = 0; i < p_argcount; i++) { \
|
|
|
|
argsp.push_back(&args[i]); \
|
|
|
|
} \
|
|
|
|
Variant r; \
|
|
|
|
validated_call(&r, (const Variant **)argsp.ptr(), p_argcount); \
|
|
|
|
PtrToArg<Variant>::encode(r, ret); \
|
|
|
|
} \
|
|
|
|
static int get_argument_count() { \
|
|
|
|
return 2; \
|
|
|
|
} \
|
|
|
|
static Variant::Type get_argument_type(int p_arg) { \
|
|
|
|
return Variant::NIL; \
|
|
|
|
} \
|
|
|
|
static Variant::Type get_return_type() { \
|
|
|
|
return Variant::NIL; \
|
|
|
|
} \
|
|
|
|
static bool has_return_type() { \
|
|
|
|
return true; \
|
|
|
|
} \
|
|
|
|
static bool is_vararg() { \
|
|
|
|
return true; \
|
|
|
|
} \
|
2020-11-11 16:16:08 +00:00
|
|
|
static Variant::UtilityFunctionType get_type() { \
|
2020-11-10 21:31:33 +00:00
|
|
|
return m_category; \
|
|
|
|
} \
|
|
|
|
}; \
|
2020-11-11 16:16:08 +00:00
|
|
|
register_utility_function<Func_##m_func>(#m_func, m_args)
|
2020-11-10 21:31:33 +00:00
|
|
|
|
|
|
|
#define FUNCBINDVARARGS(m_func, m_args, m_category) \
|
|
|
|
class Func_##m_func { \
|
|
|
|
public: \
|
|
|
|
static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \
|
|
|
|
r_error.error = Callable::CallError::CALL_OK; \
|
2020-11-11 16:16:08 +00:00
|
|
|
*r_ret = VariantUtilityFunctions::m_func(p_args, p_argcount, r_error); \
|
2020-11-10 21:31:33 +00:00
|
|
|
} \
|
|
|
|
static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \
|
|
|
|
Callable::CallError c; \
|
2020-11-11 16:16:08 +00:00
|
|
|
*r_ret = VariantUtilityFunctions::m_func(p_args, p_argcount, c); \
|
2020-11-10 21:31:33 +00:00
|
|
|
} \
|
|
|
|
static void ptrcall(void *ret, const void **p_args, int p_argcount) { \
|
|
|
|
Vector<Variant> args; \
|
|
|
|
for (int i = 0; i < p_argcount; i++) { \
|
|
|
|
args.push_back(PtrToArg<Variant>::convert(p_args[i])); \
|
|
|
|
} \
|
|
|
|
Vector<const Variant *> argsp; \
|
|
|
|
for (int i = 0; i < p_argcount; i++) { \
|
|
|
|
argsp.push_back(&args[i]); \
|
|
|
|
} \
|
|
|
|
Variant r; \
|
|
|
|
validated_call(&r, (const Variant **)argsp.ptr(), p_argcount); \
|
|
|
|
PtrToArg<String>::encode(r.operator String(), ret); \
|
|
|
|
} \
|
|
|
|
static int get_argument_count() { \
|
|
|
|
return 1; \
|
|
|
|
} \
|
|
|
|
static Variant::Type get_argument_type(int p_arg) { \
|
|
|
|
return Variant::NIL; \
|
|
|
|
} \
|
|
|
|
static Variant::Type get_return_type() { \
|
|
|
|
return Variant::STRING; \
|
|
|
|
} \
|
|
|
|
static bool has_return_type() { \
|
|
|
|
return true; \
|
|
|
|
} \
|
|
|
|
static bool is_vararg() { \
|
|
|
|
return true; \
|
|
|
|
} \
|
2020-11-11 16:16:08 +00:00
|
|
|
static Variant::UtilityFunctionType get_type() { \
|
2020-11-10 21:31:33 +00:00
|
|
|
return m_category; \
|
|
|
|
} \
|
|
|
|
}; \
|
2020-11-11 16:16:08 +00:00
|
|
|
register_utility_function<Func_##m_func>(#m_func, m_args)
|
2020-11-10 21:31:33 +00:00
|
|
|
|
|
|
|
#define FUNCBINDVARARGV(m_func, m_args, m_category) \
|
|
|
|
class Func_##m_func { \
|
|
|
|
public: \
|
|
|
|
static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \
|
|
|
|
r_error.error = Callable::CallError::CALL_OK; \
|
2020-11-11 16:16:08 +00:00
|
|
|
VariantUtilityFunctions::m_func(p_args, p_argcount, r_error); \
|
2020-11-10 21:31:33 +00:00
|
|
|
} \
|
|
|
|
static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \
|
|
|
|
Callable::CallError c; \
|
2020-11-11 16:16:08 +00:00
|
|
|
VariantUtilityFunctions::m_func(p_args, p_argcount, c); \
|
2020-11-10 21:31:33 +00:00
|
|
|
} \
|
|
|
|
static void ptrcall(void *ret, const void **p_args, int p_argcount) { \
|
|
|
|
Vector<Variant> args; \
|
|
|
|
for (int i = 0; i < p_argcount; i++) { \
|
|
|
|
args.push_back(PtrToArg<Variant>::convert(p_args[i])); \
|
|
|
|
} \
|
|
|
|
Vector<const Variant *> argsp; \
|
|
|
|
for (int i = 0; i < p_argcount; i++) { \
|
|
|
|
argsp.push_back(&args[i]); \
|
|
|
|
} \
|
|
|
|
Variant r; \
|
|
|
|
validated_call(&r, (const Variant **)argsp.ptr(), p_argcount); \
|
|
|
|
} \
|
|
|
|
static int get_argument_count() { \
|
|
|
|
return 1; \
|
|
|
|
} \
|
|
|
|
static Variant::Type get_argument_type(int p_arg) { \
|
|
|
|
return Variant::NIL; \
|
|
|
|
} \
|
|
|
|
static Variant::Type get_return_type() { \
|
|
|
|
return Variant::NIL; \
|
|
|
|
} \
|
|
|
|
static bool has_return_type() { \
|
|
|
|
return false; \
|
|
|
|
} \
|
|
|
|
static bool is_vararg() { \
|
|
|
|
return true; \
|
|
|
|
} \
|
2020-11-11 16:16:08 +00:00
|
|
|
static Variant::UtilityFunctionType get_type() { \
|
2020-11-10 21:31:33 +00:00
|
|
|
return m_category; \
|
|
|
|
} \
|
|
|
|
}; \
|
2020-11-11 16:16:08 +00:00
|
|
|
register_utility_function<Func_##m_func>(#m_func, m_args)
|
2020-11-10 21:31:33 +00:00
|
|
|
|
|
|
|
#define FUNCBIND(m_func, m_args, m_category) \
|
|
|
|
class Func_##m_func { \
|
|
|
|
public: \
|
|
|
|
static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \
|
2020-11-11 16:16:08 +00:00
|
|
|
call_helper(VariantUtilityFunctions::m_func, p_args, r_error); \
|
2020-11-10 21:31:33 +00:00
|
|
|
} \
|
|
|
|
static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \
|
2020-11-11 16:16:08 +00:00
|
|
|
validated_call_helper(VariantUtilityFunctions::m_func, p_args); \
|
2020-11-10 21:31:33 +00:00
|
|
|
} \
|
|
|
|
static void ptrcall(void *ret, const void **p_args, int p_argcount) { \
|
2020-11-11 16:16:08 +00:00
|
|
|
ptr_call_helper(VariantUtilityFunctions::m_func, p_args); \
|
2020-11-10 21:31:33 +00:00
|
|
|
} \
|
|
|
|
static int get_argument_count() { \
|
2020-11-11 16:16:08 +00:00
|
|
|
return get_arg_count_helper(VariantUtilityFunctions::m_func); \
|
2020-11-10 21:31:33 +00:00
|
|
|
} \
|
|
|
|
static Variant::Type get_argument_type(int p_arg) { \
|
2020-11-11 16:16:08 +00:00
|
|
|
return get_arg_type_helper(VariantUtilityFunctions::m_func, p_arg); \
|
2020-11-10 21:31:33 +00:00
|
|
|
} \
|
|
|
|
static Variant::Type get_return_type() { \
|
2020-11-11 16:16:08 +00:00
|
|
|
return get_ret_type_helper(VariantUtilityFunctions::m_func); \
|
2020-11-10 21:31:33 +00:00
|
|
|
} \
|
|
|
|
static bool has_return_type() { \
|
|
|
|
return false; \
|
|
|
|
} \
|
|
|
|
static bool is_vararg() { return false; } \
|
2020-11-11 16:16:08 +00:00
|
|
|
static Variant::UtilityFunctionType get_type() { return m_category; } \
|
2020-11-10 21:31:33 +00:00
|
|
|
}; \
|
2020-11-11 16:16:08 +00:00
|
|
|
register_utility_function<Func_##m_func>(#m_func, m_args)
|
2020-11-10 21:31:33 +00:00
|
|
|
|
2020-11-11 16:16:08 +00:00
|
|
|
struct VariantUtilityFunctionInfo {
|
2022-05-02 14:28:25 +00:00
|
|
|
void (*call_utility)(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) = nullptr;
|
|
|
|
Variant::ValidatedUtilityFunction validated_call_utility = nullptr;
|
|
|
|
Variant::PTRUtilityFunction ptr_call_utility = nullptr;
|
2020-11-10 21:31:33 +00:00
|
|
|
Vector<String> argnames;
|
2022-05-02 14:28:25 +00:00
|
|
|
bool is_vararg = false;
|
|
|
|
bool returns_value = false;
|
|
|
|
int argcount = 0;
|
|
|
|
Variant::Type (*get_arg_type)(int) = nullptr;
|
2020-11-10 21:31:33 +00:00
|
|
|
Variant::Type return_type;
|
2020-11-11 16:16:08 +00:00
|
|
|
Variant::UtilityFunctionType type;
|
2020-11-10 21:31:33 +00:00
|
|
|
};
|
|
|
|
|
2020-11-11 16:16:08 +00:00
|
|
|
static OAHashMap<StringName, VariantUtilityFunctionInfo> utility_function_table;
|
|
|
|
static List<StringName> utility_function_name_table;
|
2020-11-10 21:31:33 +00:00
|
|
|
|
|
|
|
template <class T>
|
2020-11-11 16:16:08 +00:00
|
|
|
static void register_utility_function(const String &p_name, const Vector<String> &argnames) {
|
2020-11-10 21:31:33 +00:00
|
|
|
String name = p_name;
|
|
|
|
if (name.begins_with("_")) {
|
|
|
|
name = name.substr(1, name.length() - 1);
|
|
|
|
}
|
|
|
|
StringName sname = name;
|
2020-11-11 16:16:08 +00:00
|
|
|
ERR_FAIL_COND(utility_function_table.has(sname));
|
2020-11-10 21:31:33 +00:00
|
|
|
|
2020-11-11 16:16:08 +00:00
|
|
|
VariantUtilityFunctionInfo bfi;
|
|
|
|
bfi.call_utility = T::call;
|
|
|
|
bfi.validated_call_utility = T::validated_call;
|
|
|
|
bfi.ptr_call_utility = T::ptrcall;
|
2020-11-10 21:31:33 +00:00
|
|
|
bfi.is_vararg = T::is_vararg();
|
|
|
|
bfi.argnames = argnames;
|
|
|
|
bfi.argcount = T::get_argument_count();
|
|
|
|
if (!bfi.is_vararg) {
|
2020-11-11 16:16:08 +00:00
|
|
|
ERR_FAIL_COND_MSG(argnames.size() != bfi.argcount, "wrong number of arguments binding utility function: " + name);
|
2020-11-10 21:31:33 +00:00
|
|
|
}
|
|
|
|
bfi.get_arg_type = T::get_argument_type;
|
|
|
|
bfi.return_type = T::get_return_type();
|
|
|
|
bfi.type = T::get_type();
|
|
|
|
bfi.returns_value = T::has_return_type();
|
|
|
|
|
2020-11-11 16:16:08 +00:00
|
|
|
utility_function_table.insert(sname, bfi);
|
|
|
|
utility_function_name_table.push_back(sname);
|
2020-11-10 21:31:33 +00:00
|
|
|
}
|
|
|
|
|
2020-11-11 16:16:08 +00:00
|
|
|
void Variant::_register_variant_utility_functions() {
|
2020-11-10 21:31:33 +00:00
|
|
|
// Math
|
|
|
|
|
2020-11-11 16:16:08 +00:00
|
|
|
FUNCBINDR(sin, sarray("angle_rad"), Variant::UTILITY_FUNC_TYPE_MATH);
|
|
|
|
FUNCBINDR(cos, sarray("angle_rad"), Variant::UTILITY_FUNC_TYPE_MATH);
|
|
|
|
FUNCBINDR(tan, sarray("angle_rad"), Variant::UTILITY_FUNC_TYPE_MATH);
|
2020-11-10 21:31:33 +00:00
|
|
|
|
2020-11-11 16:16:08 +00:00
|
|
|
FUNCBINDR(sinh, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
|
|
|
|
FUNCBINDR(cosh, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
|
|
|
|
FUNCBINDR(tanh, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
|
2020-11-10 21:31:33 +00:00
|
|
|
|
2020-11-11 16:16:08 +00:00
|
|
|
FUNCBINDR(asin, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
|
|
|
|
FUNCBINDR(acos, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
|
|
|
|
FUNCBINDR(atan, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
|
2020-11-10 21:31:33 +00:00
|
|
|
|
2020-11-11 16:16:08 +00:00
|
|
|
FUNCBINDR(atan2, sarray("y", "x"), Variant::UTILITY_FUNC_TYPE_MATH);
|
2020-11-10 21:31:33 +00:00
|
|
|
|
2020-11-11 16:16:08 +00:00
|
|
|
FUNCBINDR(sqrt, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
|
|
|
|
FUNCBINDR(fmod, sarray("x", "y"), Variant::UTILITY_FUNC_TYPE_MATH);
|
|
|
|
FUNCBINDR(fposmod, sarray("x", "y"), Variant::UTILITY_FUNC_TYPE_MATH);
|
2021-05-04 10:51:03 +00:00
|
|
|
FUNCBINDR(posmod, sarray("x", "y"), Variant::UTILITY_FUNC_TYPE_MATH);
|
2020-11-10 21:31:33 +00:00
|
|
|
|
2022-07-23 20:12:23 +00:00
|
|
|
FUNCBINDVR(floor, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
|
|
|
|
FUNCBINDR(floorf, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
|
|
|
|
FUNCBINDR(floori, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
|
|
|
|
|
|
|
|
FUNCBINDVR(ceil, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
|
|
|
|
FUNCBINDR(ceilf, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
|
|
|
|
FUNCBINDR(ceili, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
|
2020-11-10 21:31:33 +00:00
|
|
|
|
2022-07-23 20:12:23 +00:00
|
|
|
FUNCBINDVR(round, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
|
|
|
|
FUNCBINDR(roundf, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
|
|
|
|
FUNCBINDR(roundi, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
|
2020-11-10 21:31:33 +00:00
|
|
|
|
2022-07-23 20:12:23 +00:00
|
|
|
FUNCBINDVR(abs, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
|
2020-11-11 16:16:08 +00:00
|
|
|
FUNCBINDR(absf, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
|
|
|
|
FUNCBINDR(absi, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
|
2020-11-10 21:31:33 +00:00
|
|
|
|
2020-11-11 16:16:08 +00:00
|
|
|
FUNCBINDVR(sign, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
|
|
|
|
FUNCBINDR(signf, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
|
|
|
|
FUNCBINDR(signi, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
|
2020-11-10 21:31:33 +00:00
|
|
|
|
2020-11-17 10:21:08 +00:00
|
|
|
FUNCBINDR(pow, sarray("base", "exp"), Variant::UTILITY_FUNC_TYPE_MATH);
|
2020-11-11 16:16:08 +00:00
|
|
|
FUNCBINDR(log, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
|
|
|
|
FUNCBINDR(exp, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
|
2020-11-10 21:31:33 +00:00
|
|
|
|
2020-11-11 16:16:08 +00:00
|
|
|
FUNCBINDR(is_nan, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
|
|
|
|
FUNCBINDR(is_inf, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
|
2020-11-10 21:31:33 +00:00
|
|
|
|
2020-11-11 16:16:08 +00:00
|
|
|
FUNCBINDR(is_equal_approx, sarray("a", "b"), Variant::UTILITY_FUNC_TYPE_MATH);
|
|
|
|
FUNCBINDR(is_zero_approx, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
|
2020-11-10 21:31:33 +00:00
|
|
|
|
2020-11-17 10:21:08 +00:00
|
|
|
FUNCBINDR(ease, sarray("x", "curve"), Variant::UTILITY_FUNC_TYPE_MATH);
|
2020-11-11 16:16:08 +00:00
|
|
|
FUNCBINDR(step_decimals, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
|
2020-12-21 18:02:57 +00:00
|
|
|
FUNCBINDR(snapped, sarray("x", "step"), Variant::UTILITY_FUNC_TYPE_MATH);
|
2020-11-10 21:31:33 +00:00
|
|
|
|
2022-05-10 14:08:35 +00:00
|
|
|
FUNCBINDVR3(lerp, sarray("from", "to", "weight"), Variant::UTILITY_FUNC_TYPE_MATH);
|
|
|
|
FUNCBINDR(lerpf, sarray("from", "to", "weight"), Variant::UTILITY_FUNC_TYPE_MATH);
|
2022-02-11 03:43:21 +00:00
|
|
|
FUNCBINDR(cubic_interpolate, sarray("from", "to", "pre", "post", "weight"), Variant::UTILITY_FUNC_TYPE_MATH);
|
2022-08-26 02:42:00 +00:00
|
|
|
FUNCBINDR(cubic_interpolate_angle, sarray("from", "to", "pre", "post", "weight"), Variant::UTILITY_FUNC_TYPE_MATH);
|
2022-07-28 19:55:10 +00:00
|
|
|
FUNCBINDR(cubic_interpolate_in_time, sarray("from", "to", "pre", "post", "weight", "to_t", "pre_t", "post_t"), Variant::UTILITY_FUNC_TYPE_MATH);
|
2022-08-26 02:42:00 +00:00
|
|
|
FUNCBINDR(cubic_interpolate_angle_in_time, sarray("from", "to", "pre", "post", "weight", "to_t", "pre_t", "post_t"), Variant::UTILITY_FUNC_TYPE_MATH);
|
2022-06-27 17:41:32 +00:00
|
|
|
FUNCBINDR(bezier_interpolate, sarray("start", "control_1", "control_2", "end", "t"), Variant::UTILITY_FUNC_TYPE_MATH);
|
2020-11-17 10:21:08 +00:00
|
|
|
FUNCBINDR(lerp_angle, sarray("from", "to", "weight"), Variant::UTILITY_FUNC_TYPE_MATH);
|
|
|
|
FUNCBINDR(inverse_lerp, sarray("from", "to", "weight"), Variant::UTILITY_FUNC_TYPE_MATH);
|
2022-09-05 12:05:50 +00:00
|
|
|
FUNCBINDR(remap, sarray("value", "istart", "istop", "ostart", "ostop"), Variant::UTILITY_FUNC_TYPE_MATH);
|
2020-11-10 21:31:33 +00:00
|
|
|
|
2020-11-17 10:21:08 +00:00
|
|
|
FUNCBINDR(smoothstep, sarray("from", "to", "x"), Variant::UTILITY_FUNC_TYPE_MATH);
|
2020-11-11 16:16:08 +00:00
|
|
|
FUNCBINDR(move_toward, sarray("from", "to", "delta"), Variant::UTILITY_FUNC_TYPE_MATH);
|
2020-11-10 21:31:33 +00:00
|
|
|
|
2022-08-13 15:45:42 +00:00
|
|
|
FUNCBINDR(deg_to_rad, sarray("deg"), Variant::UTILITY_FUNC_TYPE_MATH);
|
|
|
|
FUNCBINDR(rad_to_deg, sarray("rad"), Variant::UTILITY_FUNC_TYPE_MATH);
|
|
|
|
FUNCBINDR(linear_to_db, sarray("lin"), Variant::UTILITY_FUNC_TYPE_MATH);
|
|
|
|
FUNCBINDR(db_to_linear, sarray("db"), Variant::UTILITY_FUNC_TYPE_MATH);
|
2020-11-10 21:31:33 +00:00
|
|
|
|
2022-06-27 09:37:55 +00:00
|
|
|
FUNCBINDVR3(wrap, sarray("value", "min", "max"), Variant::UTILITY_FUNC_TYPE_MATH);
|
2020-11-11 16:16:08 +00:00
|
|
|
FUNCBINDR(wrapi, sarray("value", "min", "max"), Variant::UTILITY_FUNC_TYPE_MATH);
|
|
|
|
FUNCBINDR(wrapf, sarray("value", "min", "max"), Variant::UTILITY_FUNC_TYPE_MATH);
|
2020-11-10 21:31:33 +00:00
|
|
|
|
2020-11-11 16:16:08 +00:00
|
|
|
FUNCBINDVARARG(max, sarray(), Variant::UTILITY_FUNC_TYPE_MATH);
|
|
|
|
FUNCBINDR(maxi, sarray("a", "b"), Variant::UTILITY_FUNC_TYPE_MATH);
|
|
|
|
FUNCBINDR(maxf, sarray("a", "b"), Variant::UTILITY_FUNC_TYPE_MATH);
|
2020-11-10 21:31:33 +00:00
|
|
|
|
2020-11-11 16:16:08 +00:00
|
|
|
FUNCBINDVARARG(min, sarray(), Variant::UTILITY_FUNC_TYPE_MATH);
|
|
|
|
FUNCBINDR(mini, sarray("a", "b"), Variant::UTILITY_FUNC_TYPE_MATH);
|
|
|
|
FUNCBINDR(minf, sarray("a", "b"), Variant::UTILITY_FUNC_TYPE_MATH);
|
2020-11-10 21:31:33 +00:00
|
|
|
|
2020-11-11 16:16:08 +00:00
|
|
|
FUNCBINDVR3(clamp, sarray("value", "min", "max"), Variant::UTILITY_FUNC_TYPE_MATH);
|
|
|
|
FUNCBINDR(clampi, sarray("value", "min", "max"), Variant::UTILITY_FUNC_TYPE_MATH);
|
|
|
|
FUNCBINDR(clampf, sarray("value", "min", "max"), Variant::UTILITY_FUNC_TYPE_MATH);
|
2020-11-10 21:31:33 +00:00
|
|
|
|
2020-11-11 16:16:08 +00:00
|
|
|
FUNCBINDR(nearest_po2, sarray("value"), Variant::UTILITY_FUNC_TYPE_MATH);
|
2021-10-15 13:25:00 +00:00
|
|
|
FUNCBINDR(pingpong, sarray("value", "length"), Variant::UTILITY_FUNC_TYPE_MATH);
|
2020-11-10 21:31:33 +00:00
|
|
|
|
2020-11-17 10:21:08 +00:00
|
|
|
// Random
|
2020-11-10 21:31:33 +00:00
|
|
|
|
2020-11-11 16:16:08 +00:00
|
|
|
FUNCBIND(randomize, sarray(), Variant::UTILITY_FUNC_TYPE_RANDOM);
|
|
|
|
FUNCBINDR(randi, sarray(), Variant::UTILITY_FUNC_TYPE_RANDOM);
|
|
|
|
FUNCBINDR(randf, sarray(), Variant::UTILITY_FUNC_TYPE_RANDOM);
|
|
|
|
FUNCBINDR(randi_range, sarray("from", "to"), Variant::UTILITY_FUNC_TYPE_RANDOM);
|
|
|
|
FUNCBINDR(randf_range, sarray("from", "to"), Variant::UTILITY_FUNC_TYPE_RANDOM);
|
2021-06-21 09:58:31 +00:00
|
|
|
FUNCBINDR(randfn, sarray("mean", "deviation"), Variant::UTILITY_FUNC_TYPE_RANDOM);
|
2020-11-11 16:16:08 +00:00
|
|
|
FUNCBIND(seed, sarray("base"), Variant::UTILITY_FUNC_TYPE_RANDOM);
|
|
|
|
FUNCBINDR(rand_from_seed, sarray("seed"), Variant::UTILITY_FUNC_TYPE_RANDOM);
|
2020-11-10 21:31:33 +00:00
|
|
|
|
|
|
|
// Utility
|
2020-11-17 10:21:08 +00:00
|
|
|
|
|
|
|
FUNCBINDVR(weakref, sarray("obj"), Variant::UTILITY_FUNC_TYPE_GENERAL);
|
2020-11-11 16:16:08 +00:00
|
|
|
FUNCBINDR(_typeof, sarray("variable"), Variant::UTILITY_FUNC_TYPE_GENERAL);
|
|
|
|
FUNCBINDVARARGS(str, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL);
|
2020-05-23 20:38:48 +00:00
|
|
|
FUNCBINDR(error_string, sarray("error"), Variant::UTILITY_FUNC_TYPE_GENERAL);
|
2020-11-11 16:16:08 +00:00
|
|
|
FUNCBINDVARARGV(print, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL);
|
2022-05-02 23:29:38 +00:00
|
|
|
FUNCBINDVARARGV(print_rich, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL);
|
2020-11-11 16:16:08 +00:00
|
|
|
FUNCBINDVARARGV(printerr, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL);
|
|
|
|
FUNCBINDVARARGV(printt, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL);
|
|
|
|
FUNCBINDVARARGV(prints, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL);
|
|
|
|
FUNCBINDVARARGV(printraw, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL);
|
2021-09-20 17:14:27 +00:00
|
|
|
FUNCBINDVARARGV(print_verbose, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL);
|
2020-11-11 16:16:08 +00:00
|
|
|
FUNCBINDVARARGV(push_error, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL);
|
|
|
|
FUNCBINDVARARGV(push_warning, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL);
|
|
|
|
|
2022-08-13 15:45:42 +00:00
|
|
|
FUNCBINDR(var_to_str, sarray("variable"), Variant::UTILITY_FUNC_TYPE_GENERAL);
|
|
|
|
FUNCBINDR(str_to_var, sarray("string"), Variant::UTILITY_FUNC_TYPE_GENERAL);
|
2020-11-11 16:16:08 +00:00
|
|
|
|
2022-08-13 15:45:42 +00:00
|
|
|
FUNCBINDR(var_to_bytes, sarray("variable"), Variant::UTILITY_FUNC_TYPE_GENERAL);
|
|
|
|
FUNCBINDR(bytes_to_var, sarray("bytes"), Variant::UTILITY_FUNC_TYPE_GENERAL);
|
2020-11-11 16:16:08 +00:00
|
|
|
|
2022-08-13 15:45:42 +00:00
|
|
|
FUNCBINDR(var_to_bytes_with_objects, sarray("variable"), Variant::UTILITY_FUNC_TYPE_GENERAL);
|
|
|
|
FUNCBINDR(bytes_to_var_with_objects, sarray("bytes"), Variant::UTILITY_FUNC_TYPE_GENERAL);
|
2020-11-11 16:16:08 +00:00
|
|
|
|
|
|
|
FUNCBINDR(hash, sarray("variable"), Variant::UTILITY_FUNC_TYPE_GENERAL);
|
|
|
|
|
2020-11-17 10:21:08 +00:00
|
|
|
FUNCBINDR(instance_from_id, sarray("instance_id"), Variant::UTILITY_FUNC_TYPE_GENERAL);
|
2020-11-11 16:16:08 +00:00
|
|
|
FUNCBINDR(is_instance_id_valid, sarray("id"), Variant::UTILITY_FUNC_TYPE_GENERAL);
|
|
|
|
FUNCBINDR(is_instance_valid, sarray("instance"), Variant::UTILITY_FUNC_TYPE_GENERAL);
|
2021-08-23 23:57:52 +00:00
|
|
|
|
|
|
|
FUNCBINDR(rid_allocate_id, Vector<String>(), Variant::UTILITY_FUNC_TYPE_GENERAL);
|
|
|
|
FUNCBINDR(rid_from_int64, sarray("base"), Variant::UTILITY_FUNC_TYPE_GENERAL);
|
2020-11-10 21:31:33 +00:00
|
|
|
}
|
2020-11-17 10:21:08 +00:00
|
|
|
|
2020-11-11 16:16:08 +00:00
|
|
|
void Variant::_unregister_variant_utility_functions() {
|
|
|
|
utility_function_table.clear();
|
|
|
|
utility_function_name_table.clear();
|
2020-11-10 21:31:33 +00:00
|
|
|
}
|
|
|
|
|
2020-11-11 16:16:08 +00:00
|
|
|
void Variant::call_utility_function(const StringName &p_name, Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
|
|
|
|
const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name);
|
2020-11-10 21:31:33 +00:00
|
|
|
if (!bfi) {
|
|
|
|
r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
|
|
|
|
r_error.argument = 0;
|
|
|
|
r_error.expected = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unlikely(!bfi->is_vararg && p_argcount < bfi->argcount)) {
|
|
|
|
r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
|
|
|
|
r_error.argument = 0;
|
|
|
|
r_error.expected = bfi->argcount;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unlikely(!bfi->is_vararg && p_argcount > bfi->argcount)) {
|
|
|
|
r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
|
|
|
|
r_error.argument = 0;
|
|
|
|
r_error.expected = bfi->argcount;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-11 16:16:08 +00:00
|
|
|
bfi->call_utility(r_ret, p_args, p_argcount, r_error);
|
2020-11-10 21:31:33 +00:00
|
|
|
}
|
|
|
|
|
2020-11-11 16:16:08 +00:00
|
|
|
bool Variant::has_utility_function(const StringName &p_name) {
|
|
|
|
return utility_function_table.has(p_name);
|
2020-11-10 21:31:33 +00:00
|
|
|
}
|
|
|
|
|
2020-11-11 16:16:08 +00:00
|
|
|
Variant::ValidatedUtilityFunction Variant::get_validated_utility_function(const StringName &p_name) {
|
|
|
|
const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name);
|
2020-11-10 21:31:33 +00:00
|
|
|
if (!bfi) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-11-11 16:16:08 +00:00
|
|
|
return bfi->validated_call_utility;
|
2020-11-10 21:31:33 +00:00
|
|
|
}
|
2020-11-17 10:21:08 +00:00
|
|
|
|
2020-11-11 16:16:08 +00:00
|
|
|
Variant::PTRUtilityFunction Variant::get_ptr_utility_function(const StringName &p_name) {
|
|
|
|
const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name);
|
2020-11-10 21:31:33 +00:00
|
|
|
if (!bfi) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-11-11 16:16:08 +00:00
|
|
|
return bfi->ptr_call_utility;
|
2020-11-10 21:31:33 +00:00
|
|
|
}
|
|
|
|
|
2020-11-11 16:16:08 +00:00
|
|
|
Variant::UtilityFunctionType Variant::get_utility_function_type(const StringName &p_name) {
|
|
|
|
const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name);
|
2020-11-10 21:31:33 +00:00
|
|
|
if (!bfi) {
|
2020-11-11 16:16:08 +00:00
|
|
|
return Variant::UTILITY_FUNC_TYPE_MATH;
|
2020-11-10 21:31:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return bfi->type;
|
|
|
|
}
|
|
|
|
|
2021-10-10 19:22:07 +00:00
|
|
|
MethodInfo Variant::get_utility_function_info(const StringName &p_name) {
|
|
|
|
MethodInfo info;
|
|
|
|
const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name);
|
|
|
|
if (bfi) {
|
|
|
|
info.name = p_name;
|
|
|
|
if (bfi->returns_value && bfi->return_type == Variant::NIL) {
|
|
|
|
info.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
|
|
|
|
}
|
|
|
|
info.return_val.type = bfi->return_type;
|
|
|
|
if (bfi->is_vararg) {
|
|
|
|
info.flags |= METHOD_FLAG_VARARG;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < bfi->argnames.size(); ++i) {
|
|
|
|
PropertyInfo arg;
|
|
|
|
arg.type = bfi->get_arg_type(i);
|
|
|
|
arg.name = bfi->argnames[i];
|
|
|
|
info.arguments.push_back(arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2020-11-11 16:16:08 +00:00
|
|
|
int Variant::get_utility_function_argument_count(const StringName &p_name) {
|
|
|
|
const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name);
|
2020-11-10 21:31:33 +00:00
|
|
|
if (!bfi) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bfi->argcount;
|
|
|
|
}
|
2020-11-17 10:21:08 +00:00
|
|
|
|
2020-11-11 16:16:08 +00:00
|
|
|
Variant::Type Variant::get_utility_function_argument_type(const StringName &p_name, int p_arg) {
|
|
|
|
const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name);
|
2020-11-10 21:31:33 +00:00
|
|
|
if (!bfi) {
|
|
|
|
return Variant::NIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bfi->get_arg_type(p_arg);
|
|
|
|
}
|
2020-11-17 10:21:08 +00:00
|
|
|
|
2020-11-11 16:16:08 +00:00
|
|
|
String Variant::get_utility_function_argument_name(const StringName &p_name, int p_arg) {
|
|
|
|
const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name);
|
2020-11-10 21:31:33 +00:00
|
|
|
if (!bfi) {
|
|
|
|
return String();
|
|
|
|
}
|
|
|
|
ERR_FAIL_INDEX_V(p_arg, bfi->argnames.size(), String());
|
2021-06-19 15:58:49 +00:00
|
|
|
ERR_FAIL_COND_V(bfi->is_vararg, String());
|
2020-11-10 21:31:33 +00:00
|
|
|
return bfi->argnames[p_arg];
|
|
|
|
}
|
2020-11-17 10:21:08 +00:00
|
|
|
|
2020-11-11 16:16:08 +00:00
|
|
|
bool Variant::has_utility_function_return_value(const StringName &p_name) {
|
|
|
|
const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name);
|
2020-11-10 21:31:33 +00:00
|
|
|
if (!bfi) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return bfi->returns_value;
|
|
|
|
}
|
2020-11-17 10:21:08 +00:00
|
|
|
|
2020-11-11 16:16:08 +00:00
|
|
|
Variant::Type Variant::get_utility_function_return_type(const StringName &p_name) {
|
|
|
|
const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name);
|
2020-11-10 21:31:33 +00:00
|
|
|
if (!bfi) {
|
|
|
|
return Variant::NIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bfi->return_type;
|
|
|
|
}
|
2020-11-17 10:21:08 +00:00
|
|
|
|
2020-11-11 16:16:08 +00:00
|
|
|
bool Variant::is_utility_function_vararg(const StringName &p_name) {
|
|
|
|
const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name);
|
2020-11-10 21:31:33 +00:00
|
|
|
if (!bfi) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bfi->is_vararg;
|
|
|
|
}
|
|
|
|
|
2021-06-19 15:58:49 +00:00
|
|
|
uint32_t Variant::get_utility_function_hash(const StringName &p_name) {
|
|
|
|
const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name);
|
|
|
|
ERR_FAIL_COND_V(!bfi, 0);
|
|
|
|
|
2022-06-18 14:20:55 +00:00
|
|
|
uint32_t hash = hash_murmur3_one_32(bfi->is_vararg);
|
|
|
|
hash = hash_murmur3_one_32(bfi->returns_value, hash);
|
2021-06-19 15:58:49 +00:00
|
|
|
if (bfi->returns_value) {
|
2022-06-18 14:20:55 +00:00
|
|
|
hash = hash_murmur3_one_32(bfi->return_type, hash);
|
2021-06-19 15:58:49 +00:00
|
|
|
}
|
2022-06-18 14:20:55 +00:00
|
|
|
hash = hash_murmur3_one_32(bfi->argcount, hash);
|
2021-06-19 15:58:49 +00:00
|
|
|
for (int i = 0; i < bfi->argcount; i++) {
|
2022-06-18 14:20:55 +00:00
|
|
|
hash = hash_murmur3_one_32(bfi->get_arg_type(i), hash);
|
2021-06-19 15:58:49 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 14:20:55 +00:00
|
|
|
return hash_fmix32(hash);
|
2021-06-19 15:58:49 +00:00
|
|
|
}
|
|
|
|
|
2020-11-11 16:16:08 +00:00
|
|
|
void Variant::get_utility_function_list(List<StringName> *r_functions) {
|
2021-07-24 13:46:25 +00:00
|
|
|
for (const StringName &E : utility_function_name_table) {
|
2021-07-16 03:45:57 +00:00
|
|
|
r_functions->push_back(E);
|
2020-11-10 21:31:33 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-27 14:09:06 +00:00
|
|
|
|
|
|
|
int Variant::get_utility_function_count() {
|
|
|
|
return utility_function_name_table.size();
|
|
|
|
}
|