diff options
Diffstat (limited to 'core')
52 files changed, 2390 insertions, 1051 deletions
diff --git a/core/bind/core_bind.cpp b/core/bind/core_bind.cpp index d0acd04497..2477b1b187 100644 --- a/core/bind/core_bind.cpp +++ b/core/bind/core_bind.cpp @@ -33,6 +33,7 @@ #include "geometry.h" #include "io/file_access_compressed.h" #include "io/file_access_encrypted.h" +#include "io/json.h" #include "io/marshalls.h" #include "os/keyboard.h" #include "os/os.h" @@ -440,8 +441,8 @@ bool _OS::is_vsync_enabled() const { return OS::get_singleton()->is_vsync_enabled(); } -OS::PowerState _OS::get_power_state() { - return OS::get_singleton()->get_power_state(); +_OS::PowerState _OS::get_power_state() { + return _OS::PowerState(OS::get_singleton()->get_power_state()); } int _OS::get_power_seconds_left() { @@ -1032,10 +1033,8 @@ void _OS::_bind_methods() { ClassDB::bind_method(D_METHOD("get_time", "utc"), &_OS::get_time, DEFVAL(false)); ClassDB::bind_method(D_METHOD("get_time_zone_info"), &_OS::get_time_zone_info); ClassDB::bind_method(D_METHOD("get_unix_time"), &_OS::get_unix_time); - ClassDB::bind_method(D_METHOD("get_datetime_from_unix_time", "unix_time_val"), - &_OS::get_datetime_from_unix_time); - ClassDB::bind_method(D_METHOD("get_unix_time_from_datetime", "datetime"), - &_OS::get_unix_time_from_datetime); + ClassDB::bind_method(D_METHOD("get_datetime_from_unix_time", "unix_time_val"), &_OS::get_datetime_from_unix_time); + ClassDB::bind_method(D_METHOD("get_unix_time_from_datetime", "datetime"), &_OS::get_unix_time_from_datetime); ClassDB::bind_method(D_METHOD("get_system_time_secs"), &_OS::get_system_time_secs); ClassDB::bind_method(D_METHOD("set_icon", "icon"), &_OS::set_icon); @@ -1142,11 +1141,11 @@ void _OS::_bind_methods() { BIND_ENUM_CONSTANT(SYSTEM_DIR_PICTURES); BIND_ENUM_CONSTANT(SYSTEM_DIR_RINGTONES); - BIND_ENUM_CONSTANT(OS::POWERSTATE_UNKNOWN); - BIND_ENUM_CONSTANT(OS::POWERSTATE_ON_BATTERY); - BIND_ENUM_CONSTANT(OS::POWERSTATE_NO_BATTERY); - BIND_ENUM_CONSTANT(OS::POWERSTATE_CHARGING); - BIND_ENUM_CONSTANT(OS::POWERSTATE_CHARGED); + BIND_ENUM_CONSTANT(POWERSTATE_UNKNOWN); + BIND_ENUM_CONSTANT(POWERSTATE_ON_BATTERY); + BIND_ENUM_CONSTANT(POWERSTATE_NO_BATTERY); + BIND_ENUM_CONSTANT(POWERSTATE_CHARGING); + BIND_ENUM_CONSTANT(POWERSTATE_CHARGED); } _OS::_OS() { @@ -1336,7 +1335,7 @@ void _Geometry::_bind_methods() { ClassDB::bind_method(D_METHOD("build_box_planes", "extents"), &_Geometry::build_box_planes); ClassDB::bind_method(D_METHOD("build_cylinder_planes", "radius", "height", "sides", "axis"), &_Geometry::build_cylinder_planes, DEFVAL(Vector3::AXIS_Z)); ClassDB::bind_method(D_METHOD("build_capsule_planes", "radius", "height", "sides", "lats", "axis"), &_Geometry::build_capsule_planes, DEFVAL(Vector3::AXIS_Z)); - ClassDB::bind_method(D_METHOD("segment_intersects_circle", "segment_from", "segment_to", "circle_pos", "circle_radius"), &_Geometry::segment_intersects_circle); + ClassDB::bind_method(D_METHOD("segment_intersects_circle", "segment_from", "segment_to", "circle_position", "circle_radius"), &_Geometry::segment_intersects_circle); ClassDB::bind_method(D_METHOD("segment_intersects_segment_2d", "from_a", "to_a", "from_b", "to_b"), &_Geometry::segment_intersects_segment_2d); ClassDB::bind_method(D_METHOD("get_closest_points_between_segments_2d", "p1", "q1", "p2", "q2"), &_Geometry::get_closest_points_between_segments_2d); @@ -1352,7 +1351,7 @@ void _Geometry::_bind_methods() { ClassDB::bind_method(D_METHOD("ray_intersects_triangle", "from", "dir", "a", "b", "c"), &_Geometry::ray_intersects_triangle); ClassDB::bind_method(D_METHOD("segment_intersects_triangle", "from", "to", "a", "b", "c"), &_Geometry::segment_intersects_triangle); - ClassDB::bind_method(D_METHOD("segment_intersects_sphere", "from", "to", "spos", "sradius"), &_Geometry::segment_intersects_sphere); + ClassDB::bind_method(D_METHOD("segment_intersects_sphere", "from", "to", "sphere_position", "sphere_radius"), &_Geometry::segment_intersects_sphere); ClassDB::bind_method(D_METHOD("segment_intersects_cylinder", "from", "to", "height", "radius"), &_Geometry::segment_intersects_cylinder); ClassDB::bind_method(D_METHOD("segment_intersects_convex", "from", "to", "planes"), &_Geometry::segment_intersects_convex); ClassDB::bind_method(D_METHOD("point_is_inside_triangle", "point", "a", "b", "c"), &_Geometry::point_is_inside_triangle); @@ -1451,10 +1450,10 @@ void _File::seek_end(int64_t p_position) { ERR_FAIL_COND(!f); f->seek_end(p_position); } -int64_t _File::get_pos() const { +int64_t _File::get_position() const { ERR_FAIL_COND_V(!f, 0); - return f->get_pos(); + return f->get_position(); } int64_t _File::get_len() const { @@ -1533,7 +1532,7 @@ String _File::get_as_text() const { ERR_FAIL_COND_V(!f, String()); String text; - size_t original_pos = f->get_pos(); + size_t original_pos = f->get_position(); f->seek(0); String l = get_line(); @@ -1730,9 +1729,9 @@ void _File::_bind_methods() { ClassDB::bind_method(D_METHOD("open", "path", "flags"), &_File::open); ClassDB::bind_method(D_METHOD("close"), &_File::close); ClassDB::bind_method(D_METHOD("is_open"), &_File::is_open); - ClassDB::bind_method(D_METHOD("seek", "pos"), &_File::seek); - ClassDB::bind_method(D_METHOD("seek_end", "pos"), &_File::seek_end, DEFVAL(0)); - ClassDB::bind_method(D_METHOD("get_pos"), &_File::get_pos); + ClassDB::bind_method(D_METHOD("seek", "position"), &_File::seek); + ClassDB::bind_method(D_METHOD("seek_end", "position"), &_File::seek_end, DEFVAL(0)); + ClassDB::bind_method(D_METHOD("get_position"), &_File::get_position); ClassDB::bind_method(D_METHOD("get_len"), &_File::get_len); ClassDB::bind_method(D_METHOD("eof_reached"), &_File::eof_reached); ClassDB::bind_method(D_METHOD("get_8"), &_File::get_8); @@ -2600,3 +2599,76 @@ _Engine *_Engine::singleton = NULL; _Engine::_Engine() { singleton = this; } + +void JSONParseResult::_bind_methods() { + ClassDB::bind_method(D_METHOD("get_error"), &JSONParseResult::get_error); + ClassDB::bind_method(D_METHOD("get_error_string"), &JSONParseResult::get_error_string); + ClassDB::bind_method(D_METHOD("get_error_line"), &JSONParseResult::get_error_line); + ClassDB::bind_method(D_METHOD("get_result"), &JSONParseResult::get_result); + + ClassDB::bind_method(D_METHOD("set_error", "error"), &JSONParseResult::set_error); + ClassDB::bind_method(D_METHOD("set_error_string", "error_string"), &JSONParseResult::set_error_string); + ClassDB::bind_method(D_METHOD("set_error_line", "error_line"), &JSONParseResult::set_error_line); + ClassDB::bind_method(D_METHOD("set_result", "result"), &JSONParseResult::set_result); + + ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "error", PROPERTY_HINT_NONE, "Error", PROPERTY_USAGE_CLASS_IS_ENUM), "set_error", "get_error"); + ADD_PROPERTYNZ(PropertyInfo(Variant::STRING, "error_string"), "set_error_string", "get_error_string"); + ADD_PROPERTYNZ(PropertyInfo(Variant::INT, "error_line"), "set_error_line", "get_error_line"); + ADD_PROPERTYNZ(PropertyInfo(Variant::NIL, "result", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT), "set_result", "get_result"); +} + +void JSONParseResult::set_error(Error p_error) { + error = p_error; +} + +Error JSONParseResult::get_error() const { + return error; +} + +void JSONParseResult::set_error_string(const String &p_error_string) { + error_string = p_error_string; +} + +String JSONParseResult::get_error_string() const { + return error_string; +} + +void JSONParseResult::set_error_line(int p_error_line) { + error_line = p_error_line; +} + +int JSONParseResult::get_error_line() const { + return error_line; +} + +void JSONParseResult::set_result(const Variant &p_result) { + result = p_result; +} + +Variant JSONParseResult::get_result() const { + return result; +} + +void _JSON::_bind_methods() { + ClassDB::bind_method(D_METHOD("print", "value"), &_JSON::print); + ClassDB::bind_method(D_METHOD("parse", "json"), &_JSON::parse); +} + +String _JSON::print(const Variant &p_value) { + return JSON::print(p_value); +} + +Ref<JSONParseResult> _JSON::parse(const String &p_json) { + Ref<JSONParseResult> result; + result.instance(); + + result->error = JSON::parse(p_json, result->result, result->error_string, result->error_line); + + return result; +} + +_JSON *_JSON::singleton = NULL; + +_JSON::_JSON() { + singleton = this; +} diff --git a/core/bind/core_bind.h b/core/bind/core_bind.h index 0578c2b80f..1a22d45932 100644 --- a/core/bind/core_bind.h +++ b/core/bind/core_bind.h @@ -97,6 +97,14 @@ protected: static _OS *singleton; public: + enum PowerState { + POWERSTATE_UNKNOWN, /**< cannot determine power status */ + POWERSTATE_ON_BATTERY, /**< Not plugged in, running on the battery */ + POWERSTATE_NO_BATTERY, /**< Plugged in, no battery available */ + POWERSTATE_CHARGING, /**< Plugged in, charging battery */ + POWERSTATE_CHARGED /**< Plugged in, battery charged */ + }; + enum Weekday { DAY_SUNDAY, DAY_MONDAY, @@ -303,7 +311,7 @@ public: void set_use_vsync(bool p_enable); bool is_vsync_enabled() const; - OS::PowerState get_power_state(); + PowerState get_power_state(); int get_power_seconds_left(); int get_power_percent_left(); @@ -312,6 +320,7 @@ public: _OS(); }; +VARIANT_ENUM_CAST(_OS::PowerState); VARIANT_ENUM_CAST(_OS::Weekday); VARIANT_ENUM_CAST(_OS::Month); VARIANT_ENUM_CAST(_OS::SystemDir); @@ -390,7 +399,7 @@ public: void seek(int64_t p_position); ///< seek to a given position void seek_end(int64_t p_position = 0); ///< seek from the end of file - int64_t get_pos() const; ///< get position in the file + int64_t get_position() const; ///< get position in the file int64_t get_len() const; ///< get size of the file bool eof_reached() const; ///< reading passed EOF @@ -660,4 +669,50 @@ public: _Engine(); }; +class _JSON; + +class JSONParseResult : public Reference { + GDCLASS(JSONParseResult, Reference) + + friend class _JSON; + + Error error; + String error_string; + int error_line; + + Variant result; + +protected: + static void _bind_methods(); + +public: + void set_error(Error p_error); + Error get_error() const; + + void set_error_string(const String &p_error_string); + String get_error_string() const; + + void set_error_line(int p_error_line); + int get_error_line() const; + + void set_result(const Variant &p_result); + Variant get_result() const; +}; + +class _JSON : public Object { + GDCLASS(_JSON, Object) + +protected: + static void _bind_methods(); + static _JSON *singleton; + +public: + static _JSON *get_singleton() { return singleton; } + + String print(const Variant &p_value); + Ref<JSONParseResult> parse(const String &p_json); + + _JSON(); +}; + #endif // CORE_BIND_H diff --git a/core/color.cpp b/core/color.cpp index 259a4988b1..78b11a84df 100644 --- a/core/color.cpp +++ b/core/color.cpp @@ -250,6 +250,14 @@ Color Color::html(const String &p_color) { return Color(); if (color[0] == '#') color = color.substr(1, color.length() - 1); + if (color.length() == 3 || color.length() == 4) { + String exp_color; + for (int i = 0; i < color.length(); i++) { + exp_color += color[i]; + exp_color += color[i]; + } + color = exp_color; + } bool alpha = false; @@ -400,3 +408,122 @@ Color::operator String() const { return rtos(r) + ", " + rtos(g) + ", " + rtos(b) + ", " + rtos(a); } + +Color Color::operator+(const Color &p_color) const { + + return Color( + CLAMP(r + p_color.r, 0.0, 1.0), + CLAMP(g + p_color.g, 0.0, 1.0), + CLAMP(b + p_color.b, 0.0, 1.0), + CLAMP(a + p_color.a, 0.0, 1.0)); +} + +void Color::operator+=(const Color &p_color) { + + r = CLAMP(r + p_color.r, 0.0, 1.0); + g = CLAMP(g + p_color.g, 0.0, 1.0); + b = CLAMP(b + p_color.b, 0.0, 1.0); + a = CLAMP(a + p_color.a, 0.0, 1.0); +} + +Color Color::operator-(const Color &p_color) const { + + return Color( + CLAMP(r - p_color.r, 0.0, 1.0), + CLAMP(g - p_color.g, 0.0, 1.0), + CLAMP(b - p_color.b, 0.0, 1.0), + CLAMP(a - p_color.a, 0.0, 1.0)); +} + +void Color::operator-=(const Color &p_color) { + + r = CLAMP(r - p_color.r, 0.0, 1.0); + g = CLAMP(g - p_color.g, 0.0, 1.0); + b = CLAMP(b - p_color.b, 0.0, 1.0); + a = CLAMP(a - p_color.a, 0.0, 1.0); +} + +Color Color::operator*(const Color &p_color) const { + + return Color( + CLAMP(r * p_color.r, 0.0, 1.0), + CLAMP(g * p_color.g, 0.0, 1.0), + CLAMP(b * p_color.b, 0.0, 1.0), + CLAMP(a * p_color.a, 0.0, 1.0)); +} + +Color Color::operator*(const real_t &rvalue) const { + + return Color( + CLAMP(r * rvalue, 0.0, 1.0), + CLAMP(g * rvalue, 0.0, 1.0), + CLAMP(b * rvalue, 0.0, 1.0), + CLAMP(a * rvalue, 0.0, 1.0)); +} + +void Color::operator*=(const Color &p_color) { + + r = CLAMP(r * p_color.r, 0.0, 1.0); + g = CLAMP(g * p_color.g, 0.0, 1.0); + b = CLAMP(b * p_color.b, 0.0, 1.0); + a = CLAMP(a * p_color.a, 0.0, 1.0); +} + +void Color::operator*=(const real_t &rvalue) { + + r = CLAMP(r * rvalue, 0.0, 1.0); + g = CLAMP(g * rvalue, 0.0, 1.0); + b = CLAMP(b * rvalue, 0.0, 1.0); + a = CLAMP(a * rvalue, 0.0, 1.0); +}; + +Color Color::operator/(const Color &p_color) const { + + return Color( + p_color.r == 0 ? 1 : CLAMP(r / p_color.r, 0.0, 1.0), + p_color.g == 0 ? 1 : CLAMP(g / p_color.g, 0.0, 1.0), + p_color.b == 0 ? 1 : CLAMP(b / p_color.b, 0.0, 1.0), + p_color.a == 0 ? 1 : CLAMP(a / p_color.a, 0.0, 1.0)); +} + +Color Color::operator/(const real_t &rvalue) const { + + if (rvalue == 0) return Color(1.0, 1.0, 1.0, 1.0); + return Color( + CLAMP(r / rvalue, 0.0, 1.0), + CLAMP(g / rvalue, 0.0, 1.0), + CLAMP(b / rvalue, 0.0, 1.0), + CLAMP(a / rvalue, 0.0, 1.0)); +} + +void Color::operator/=(const Color &p_color) { + + r = p_color.r == 0 ? 1 : CLAMP(r / p_color.r, 0.0, 1.0); + g = p_color.g == 0 ? 1 : CLAMP(g / p_color.g, 0.0, 1.0); + b = p_color.b == 0 ? 1 : CLAMP(b / p_color.b, 0.0, 1.0); + a = p_color.a == 0 ? 1 : CLAMP(a / p_color.a, 0.0, 1.0); +} + +void Color::operator/=(const real_t &rvalue) { + + if (rvalue == 0) { + r = 1.0; + g = 1.0; + b = 1.0; + a = 1.0; + } else { + r = CLAMP(r / rvalue, 0.0, 1.0); + g = CLAMP(g / rvalue, 0.0, 1.0); + b = CLAMP(b / rvalue, 0.0, 1.0); + a = CLAMP(a / rvalue, 0.0, 1.0); + } +}; + +Color Color::operator-() const { + + return Color( + CLAMP(1.0 - r, 0.0, 1.0), + CLAMP(1.0 - g, 0.0, 1.0), + CLAMP(1.0 - b, 0.0, 1.0), + CLAMP(1.0 - a, 0.0, 1.0)); +} diff --git a/core/color.h b/core/color.h index d3d5db09f9..972b6a1b33 100644 --- a/core/color.h +++ b/core/color.h @@ -67,6 +67,23 @@ struct Color { return components[idx]; } + Color operator+(const Color &p_color) const; + void operator+=(const Color &p_color); + + Color operator-() const; + Color operator-(const Color &p_color) const; + void operator-=(const Color &p_color); + + Color operator*(const Color &p_color) const; + Color operator*(const real_t &rvalue) const; + void operator*=(const Color &p_color); + void operator*=(const real_t &rvalue); + + Color operator/(const Color &p_color) const; + Color operator/(const real_t &rvalue) const; + void operator/=(const Color &p_color); + void operator/=(const real_t &rvalue); + void invert(); void contrast(); Color inverted() const; diff --git a/core/hash_map.h b/core/hash_map.h index 37391a4c83..e100d7a904 100644 --- a/core/hash_map.h +++ b/core/hash_map.h @@ -37,39 +37,6 @@ #include "os/memory.h" #include "ustring.h" -struct HashMapHasherDefault { - static _FORCE_INLINE_ uint32_t hash(const String &p_string) { return p_string.hash(); } - static _FORCE_INLINE_ uint32_t hash(const char *p_cstr) { return hash_djb2(p_cstr); } - static _FORCE_INLINE_ uint32_t hash(const uint64_t p_int) { return hash_one_uint64(p_int); } - - static _FORCE_INLINE_ uint32_t hash(const int64_t p_int) { return hash(uint64_t(p_int)); } - static _FORCE_INLINE_ uint32_t hash(const float p_float) { return hash_djb2_one_float(p_float); } - static _FORCE_INLINE_ uint32_t hash(const double p_double) { return hash_djb2_one_float(p_double); } - static _FORCE_INLINE_ uint32_t hash(const uint32_t p_int) { return p_int; } - static _FORCE_INLINE_ uint32_t hash(const int32_t p_int) { return (uint32_t)p_int; } - static _FORCE_INLINE_ uint32_t hash(const uint16_t p_int) { return p_int; } - static _FORCE_INLINE_ uint32_t hash(const int16_t p_int) { return (uint32_t)p_int; } - static _FORCE_INLINE_ uint32_t hash(const uint8_t p_int) { return p_int; } - static _FORCE_INLINE_ uint32_t hash(const int8_t p_int) { return (uint32_t)p_int; } - static _FORCE_INLINE_ uint32_t hash(const wchar_t p_wchar) { return (uint32_t)p_wchar; } - //static _FORCE_INLINE_ uint32_t hash(const void* p_ptr) { return uint32_t(uint64_t(p_ptr))*(0x9e3779b1L); } -}; - -template <typename T> -struct HashMapComparatorDefault { - static bool compare(const T &p_lhs, const T &p_rhs) { - return p_lhs == p_rhs; - } - - bool compare(const float &p_lhs, const float &p_rhs) { - return (p_lhs == p_rhs) || (Math::is_nan(p_lhs) && Math::is_nan(p_rhs)); - } - - bool compare(const double &p_lhs, const double &p_rhs) { - return (p_lhs == p_rhs) || (Math::is_nan(p_lhs) && Math::is_nan(p_rhs)); - } -}; - /** * @class HashMap * @author Juan Linietsky <reduzio@gmail.com> diff --git a/core/hashfuncs.h b/core/hashfuncs.h index 56d40f1dd7..2880cc451e 100644 --- a/core/hashfuncs.h +++ b/core/hashfuncs.h @@ -33,6 +33,7 @@ #include "math_defs.h" #include "math_funcs.h" #include "typedefs.h" +#include "ustring.h" /** * Hashing functions @@ -128,4 +129,37 @@ static inline uint64_t make_uint64_t(T p_in) { return _u._u64; } +struct HashMapHasherDefault { + static _FORCE_INLINE_ uint32_t hash(const String &p_string) { return p_string.hash(); } + static _FORCE_INLINE_ uint32_t hash(const char *p_cstr) { return hash_djb2(p_cstr); } + static _FORCE_INLINE_ uint32_t hash(const uint64_t p_int) { return hash_one_uint64(p_int); } + + static _FORCE_INLINE_ uint32_t hash(const int64_t p_int) { return hash(uint64_t(p_int)); } + static _FORCE_INLINE_ uint32_t hash(const float p_float) { return hash_djb2_one_float(p_float); } + static _FORCE_INLINE_ uint32_t hash(const double p_double) { return hash_djb2_one_float(p_double); } + static _FORCE_INLINE_ uint32_t hash(const uint32_t p_int) { return p_int; } + static _FORCE_INLINE_ uint32_t hash(const int32_t p_int) { return (uint32_t)p_int; } + static _FORCE_INLINE_ uint32_t hash(const uint16_t p_int) { return p_int; } + static _FORCE_INLINE_ uint32_t hash(const int16_t p_int) { return (uint32_t)p_int; } + static _FORCE_INLINE_ uint32_t hash(const uint8_t p_int) { return p_int; } + static _FORCE_INLINE_ uint32_t hash(const int8_t p_int) { return (uint32_t)p_int; } + static _FORCE_INLINE_ uint32_t hash(const wchar_t p_wchar) { return (uint32_t)p_wchar; } + //static _FORCE_INLINE_ uint32_t hash(const void* p_ptr) { return uint32_t(uint64_t(p_ptr))*(0x9e3779b1L); } +}; + +template <typename T> +struct HashMapComparatorDefault { + static bool compare(const T &p_lhs, const T &p_rhs) { + return p_lhs == p_rhs; + } + + bool compare(const float &p_lhs, const float &p_rhs) { + return (p_lhs == p_rhs) || (Math::is_nan(p_lhs) && Math::is_nan(p_rhs)); + } + + bool compare(const double &p_lhs, const double &p_rhs) { + return (p_lhs == p_rhs) || (Math::is_nan(p_lhs) && Math::is_nan(p_rhs)); + } +}; + #endif diff --git a/core/io/file_access_buffered.cpp b/core/io/file_access_buffered.cpp index 859f2e3f8c..9ec03bf32b 100644 --- a/core/io/file_access_buffered.cpp +++ b/core/io/file_access_buffered.cpp @@ -76,7 +76,7 @@ void FileAccessBuffered::seek_end(int64_t p_position) { file.offset = file.size + p_position; }; -size_t FileAccessBuffered::get_pos() const { +size_t FileAccessBuffered::get_position() const { return file.offset; }; diff --git a/core/io/file_access_buffered.h b/core/io/file_access_buffered.h index d3137058fb..70aaeb8ae0 100644 --- a/core/io/file_access_buffered.h +++ b/core/io/file_access_buffered.h @@ -72,7 +72,7 @@ protected: int get_cache_size(); public: - virtual size_t get_pos() const; ///< get position in the file + virtual size_t get_position() const; ///< get position in the file virtual size_t get_len() const; ///< get size of the file virtual void seek(size_t p_position); ///< seek to a given position diff --git a/core/io/file_access_compressed.cpp b/core/io/file_access_compressed.cpp index 70430ca5d3..4750945854 100644 --- a/core/io/file_access_compressed.cpp +++ b/core/io/file_access_compressed.cpp @@ -62,7 +62,7 @@ Error FileAccessCompressed::open_after_magic(FileAccess *p_base) { block_size = f->get_32(); read_total = f->get_32(); int bc = (read_total / block_size) + 1; - int acc_ofs = f->get_pos() + bc * 4; + int acc_ofs = f->get_position() + bc * 4; int max_bs = 0; for (int i = 0; i < bc; i++) { @@ -232,7 +232,7 @@ void FileAccessCompressed::seek_end(int64_t p_position) { seek(read_total + p_position); } } -size_t FileAccessCompressed::get_pos() const { +size_t FileAccessCompressed::get_position() const { ERR_FAIL_COND_V(!f, 0); if (writing) { diff --git a/core/io/file_access_compressed.h b/core/io/file_access_compressed.h index ba84c9767c..1a57e2d4ee 100644 --- a/core/io/file_access_compressed.h +++ b/core/io/file_access_compressed.h @@ -74,7 +74,7 @@ public: virtual void seek(size_t p_position); ///< seek to a given position virtual void seek_end(int64_t p_position = 0); ///< seek from the end of file - virtual size_t get_pos() const; ///< get position in the file + virtual size_t get_position() const; ///< get position in the file virtual size_t get_len() const; ///< get size of the file virtual bool eof_reached() const; ///< reading passed EOF diff --git a/core/io/file_access_encrypted.cpp b/core/io/file_access_encrypted.cpp index 12503f3be4..461c5bafe2 100644 --- a/core/io/file_access_encrypted.cpp +++ b/core/io/file_access_encrypted.cpp @@ -71,7 +71,7 @@ Error FileAccessEncrypted::open_and_parse(FileAccess *p_base, const Vector<uint8 unsigned char md5d[16]; p_base->get_buffer(md5d, 16); length = p_base->get_64(); - base = p_base->get_pos(); + base = p_base->get_position(); ERR_FAIL_COND_V(p_base->get_len() < base + length, ERR_FILE_CORRUPT); uint32_t ds = length; if (ds % 16) { @@ -199,7 +199,7 @@ void FileAccessEncrypted::seek_end(int64_t p_position) { seek(data.size() + p_position); } -size_t FileAccessEncrypted::get_pos() const { +size_t FileAccessEncrypted::get_position() const { return pos; } diff --git a/core/io/file_access_encrypted.h b/core/io/file_access_encrypted.h index 74d00a5a8f..82f60ac654 100644 --- a/core/io/file_access_encrypted.h +++ b/core/io/file_access_encrypted.h @@ -61,7 +61,7 @@ public: virtual void seek(size_t p_position); ///< seek to a given position virtual void seek_end(int64_t p_position = 0); ///< seek from the end of file - virtual size_t get_pos() const; ///< get position in the file + virtual size_t get_position() const; ///< get position in the file virtual size_t get_len() const; ///< get size of the file virtual bool eof_reached() const; ///< reading passed EOF diff --git a/core/io/file_access_memory.cpp b/core/io/file_access_memory.cpp index 5b186b7798..b948394385 100644 --- a/core/io/file_access_memory.cpp +++ b/core/io/file_access_memory.cpp @@ -120,7 +120,7 @@ void FileAccessMemory::seek_end(int64_t p_position) { pos = length + p_position; } -size_t FileAccessMemory::get_pos() const { +size_t FileAccessMemory::get_position() const { ERR_FAIL_COND_V(!data, 0); return pos; diff --git a/core/io/file_access_memory.h b/core/io/file_access_memory.h index 7feb16461b..b7b8430089 100644 --- a/core/io/file_access_memory.h +++ b/core/io/file_access_memory.h @@ -51,7 +51,7 @@ public: virtual void seek(size_t p_position); ///< seek to a given position virtual void seek_end(int64_t p_position); ///< seek from the end of file - virtual size_t get_pos() const; ///< get position in the file + virtual size_t get_position() const; ///< get position in the file virtual size_t get_len() const; ///< get size of the file virtual bool eof_reached() const; ///< reading passed EOF diff --git a/core/io/file_access_network.cpp b/core/io/file_access_network.cpp index 58ca2d4c58..8c624226a1 100644 --- a/core/io/file_access_network.cpp +++ b/core/io/file_access_network.cpp @@ -350,7 +350,7 @@ void FileAccessNetwork::seek_end(int64_t p_position) { seek(total_size + p_position); } -size_t FileAccessNetwork::get_pos() const { +size_t FileAccessNetwork::get_position() const { ERR_FAIL_COND_V(!opened, 0); return pos; diff --git a/core/io/file_access_network.h b/core/io/file_access_network.h index cd5046f007..abbe378b60 100644 --- a/core/io/file_access_network.h +++ b/core/io/file_access_network.h @@ -145,7 +145,7 @@ public: virtual void seek(size_t p_position); ///< seek to a given position virtual void seek_end(int64_t p_position = 0); ///< seek from the end of file - virtual size_t get_pos() const; ///< get position in the file + virtual size_t get_position() const; ///< get position in the file virtual size_t get_len() const; ///< get size of the file virtual bool eof_reached() const; ///< reading passed EOF diff --git a/core/io/file_access_pack.cpp b/core/io/file_access_pack.cpp index e511085ac5..ff4c28ec39 100644 --- a/core/io/file_access_pack.cpp +++ b/core/io/file_access_pack.cpp @@ -140,17 +140,17 @@ bool PackedSourcePCK::try_open_pack(const String &p_path) { if (magic != 0x43504447) { //maybe at he end.... self contained exe f->seek_end(); - f->seek(f->get_pos() - 4); + f->seek(f->get_position() - 4); magic = f->get_32(); if (magic != 0x43504447) { memdelete(f); return false; } - f->seek(f->get_pos() - 12); + f->seek(f->get_position() - 12); uint64_t ds = f->get_64(); - f->seek(f->get_pos() - ds - 8); + f->seek(f->get_position() - ds - 8); magic = f->get_32(); if (magic != 0x43504447) { @@ -236,7 +236,7 @@ void FileAccessPack::seek_end(int64_t p_position) { seek(pf.size + p_position); } -size_t FileAccessPack::get_pos() const { +size_t FileAccessPack::get_position() const { return pos; } diff --git a/core/io/file_access_pack.h b/core/io/file_access_pack.h index 758e9afa8e..3deb0d2bd3 100644 --- a/core/io/file_access_pack.h +++ b/core/io/file_access_pack.h @@ -148,7 +148,7 @@ public: virtual void seek(size_t p_position); virtual void seek_end(int64_t p_position = 0); - virtual size_t get_pos() const; + virtual size_t get_position() const; virtual size_t get_len() const; virtual bool eof_reached() const; diff --git a/core/io/file_access_zip.cpp b/core/io/file_access_zip.cpp index 8c99ef2983..73b23ac702 100644 --- a/core/io/file_access_zip.cpp +++ b/core/io/file_access_zip.cpp @@ -65,7 +65,7 @@ static uLong godot_write(voidpf opaque, voidpf stream, const void *buf, uLong si static long godot_tell(voidpf opaque, voidpf stream) { FileAccess *f = (FileAccess *)opaque; - return f->get_pos(); + return f->get_position(); }; static long godot_seek(voidpf opaque, voidpf stream, uLong offset, int origin) { @@ -76,7 +76,7 @@ static long godot_seek(voidpf opaque, voidpf stream, uLong offset, int origin) { switch (origin) { case ZLIB_FILEFUNC_SEEK_CUR: - pos = f->get_pos() + offset; + pos = f->get_position() + offset; break; case ZLIB_FILEFUNC_SEEK_END: pos = f->get_len() + offset; @@ -301,7 +301,7 @@ void FileAccessZip::seek_end(int64_t p_position) { unzSeekCurrentFile(zfile, get_len() + p_position); }; -size_t FileAccessZip::get_pos() const { +size_t FileAccessZip::get_position() const { ERR_FAIL_COND_V(!zfile, 0); return unztell(zfile); diff --git a/core/io/file_access_zip.h b/core/io/file_access_zip.h index 2a8ec3fca5..a40e1a753d 100644 --- a/core/io/file_access_zip.h +++ b/core/io/file_access_zip.h @@ -98,7 +98,7 @@ public: virtual void seek(size_t p_position); ///< seek to a given position virtual void seek_end(int64_t p_position = 0); ///< seek from the end of file - virtual size_t get_pos() const; ///< get position in the file + virtual size_t get_position() const; ///< get position in the file virtual size_t get_len() const; ///< get size of the file virtual bool eof_reached() const; ///< reading passed EOF diff --git a/core/io/pck_packer.cpp b/core/io/pck_packer.cpp index f1f5b6f538..23e86580d1 100644 --- a/core/io/pck_packer.cpp +++ b/core/io/pck_packer.cpp @@ -119,7 +119,7 @@ Error PCKPacker::flush(bool p_verbose) { for (int i = 0; i < files.size(); i++) { file->store_pascal_string(files[i].path); - files[i].offset_offset = file->get_pos(); + files[i].offset_offset = file->get_position(); file->store_64(0); // offset file->store_64(files[i].size); // size @@ -130,10 +130,10 @@ Error PCKPacker::flush(bool p_verbose) { file->store_32(0); }; - uint64_t ofs = file->get_pos(); + uint64_t ofs = file->get_position(); ofs = _align(ofs, alignment); - _pad(file, ofs - file->get_pos()); + _pad(file, ofs - file->get_position()); const uint32_t buf_max = 65536; uint8_t *buf = memnew_arr(uint8_t, buf_max); @@ -150,7 +150,7 @@ Error PCKPacker::flush(bool p_verbose) { to_write -= read; }; - uint64_t pos = file->get_pos(); + uint64_t pos = file->get_position(); file->seek(files[i].offset_offset); // go back to store the file's offset file->store_64(ofs); file->seek(pos); diff --git a/core/io/resource_format_binary.cpp b/core/io/resource_format_binary.cpp index 16ec6cd3c5..900db7c2dc 100644 --- a/core/io/resource_format_binary.cpp +++ b/core/io/resource_format_binary.cpp @@ -1179,7 +1179,7 @@ Error ResourceFormatLoaderBinary::rename_dependencies(const String &p_path, cons save_ustring(fw, get_ustring(f)); //type - size_t md_ofs = f->get_pos(); + size_t md_ofs = f->get_position(); size_t importmd_ofs = f->get_64(); fw->store_64(0); //metadata offset @@ -1227,7 +1227,7 @@ Error ResourceFormatLoaderBinary::rename_dependencies(const String &p_path, cons save_ustring(fw, path); } - int64_t size_diff = (int64_t)fw->get_pos() - (int64_t)f->get_pos(); + int64_t size_diff = (int64_t)fw->get_position() - (int64_t)f->get_position(); //internal resources uint32_t int_resources_size = f->get_32(); @@ -1880,7 +1880,7 @@ Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const RES &p } else { save_unicode_string(r->get_path()); //actual external } - ofs_pos.push_back(f->get_pos()); + ofs_pos.push_back(f->get_position()); f->store_64(0); //offset in 64 bits } @@ -1891,7 +1891,7 @@ Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const RES &p ResourceData &rd = E->get(); - ofs_table.push_back(f->get_pos()); + ofs_table.push_back(f->get_position()); save_unicode_string(rd.type); f->store_32(rd.properties.size()); diff --git a/core/io/resource_import.cpp b/core/io/resource_import.cpp index be486a86a3..bc7ea47762 100644 --- a/core/io/resource_import.cpp +++ b/core/io/resource_import.cpp @@ -87,6 +87,8 @@ Error ResourceFormatImporter::_get_path_and_type(const String &p_path, PathAndTy path_found = true; //first match must have priority } else if (assign == "type") { r_path_and_type.type = value; + } else if (assign == "importer") { + r_path_and_type.importer = value; } else if (assign == "valid") { if (r_valid) { *r_valid = value; @@ -184,6 +186,29 @@ bool ResourceFormatImporter::can_be_imported(const String &p_path) const { return ResourceFormatLoader::recognize_path(p_path); } +int ResourceFormatImporter::get_import_order(const String &p_path) const { + + Ref<ResourceImporter> importer; + + if (FileAccess::exists(p_path + ".import")) { + + PathAndType pat; + Error err = _get_path_and_type(p_path, pat); + + if (err == OK) { + importer = get_importer_by_name(pat.importer); + } + } else { + + importer = get_importer_by_extension(p_path.get_extension().to_lower()); + } + + if (importer.is_valid()) + return importer->get_import_order(); + + return 0; +} + bool ResourceFormatImporter::handles_type(const String &p_type) const { for (Set<Ref<ResourceImporter> >::Element *E = importers.front(); E; E = E->next()) { @@ -291,7 +316,7 @@ void ResourceFormatImporter::get_dependencies(const String &p_path, List<String> return ResourceLoader::get_dependencies(pat.path, p_dependencies, p_add_types); } -Ref<ResourceImporter> ResourceFormatImporter::get_importer_by_name(const String &p_name) { +Ref<ResourceImporter> ResourceFormatImporter::get_importer_by_name(const String &p_name) const { for (Set<Ref<ResourceImporter> >::Element *E = importers.front(); E; E = E->next()) { if (E->get()->get_importer_name() == p_name) { @@ -315,7 +340,7 @@ void ResourceFormatImporter::get_importers_for_extension(const String &p_extensi } } -Ref<ResourceImporter> ResourceFormatImporter::get_importer_by_extension(const String &p_extension) { +Ref<ResourceImporter> ResourceFormatImporter::get_importer_by_extension(const String &p_extension) const { Ref<ResourceImporter> importer; float priority = 0; diff --git a/core/io/resource_import.h b/core/io/resource_import.h index b10255fbab..28489b5d34 100644 --- a/core/io/resource_import.h +++ b/core/io/resource_import.h @@ -38,6 +38,7 @@ class ResourceFormatImporter : public ResourceFormatLoader { struct PathAndType { String path; String type; + String importer; }; Error _get_path_and_type(const String &p_path, PathAndType &r_path_and_type, bool *r_valid = NULL) const; @@ -58,14 +59,15 @@ public: virtual void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false); virtual bool can_be_imported(const String &p_path) const; + virtual int get_import_order(const String &p_path) const; String get_internal_resource_path(const String &p_path) const; void get_internal_resource_path_list(const String &p_path, List<String> *r_paths); void add_importer(const Ref<ResourceImporter> &p_importer) { importers.insert(p_importer); } void remove_importer(const Ref<ResourceImporter> &p_importer) { importers.erase(p_importer); } - Ref<ResourceImporter> get_importer_by_name(const String &p_name); - Ref<ResourceImporter> get_importer_by_extension(const String &p_extension); + Ref<ResourceImporter> get_importer_by_name(const String &p_name) const; + Ref<ResourceImporter> get_importer_by_extension(const String &p_extension) const; void get_importers_for_extension(const String &p_extension, List<Ref<ResourceImporter> > *r_importers); String get_import_base_path(const String &p_for_file) const; @@ -82,6 +84,7 @@ public: virtual String get_save_extension() const = 0; virtual String get_resource_type() const = 0; virtual float get_priority() const { return 1.0; } + virtual int get_import_order() const { return 0; } struct ImportOption { PropertyInfo option; diff --git a/core/io/resource_loader.cpp b/core/io/resource_loader.cpp index 4f266df43e..89cb4a22c2 100644 --- a/core/io/resource_loader.cpp +++ b/core/io/resource_loader.cpp @@ -308,6 +308,31 @@ void ResourceLoader::add_resource_format_loader(ResourceFormatLoader *p_format_l } } +int ResourceLoader::get_import_order(const String &p_path) { + + String path = _path_remap(p_path); + + String local_path; + if (path.is_rel_path()) + local_path = "res://" + path; + else + local_path = ProjectSettings::get_singleton()->localize_path(path); + + for (int i = 0; i < loader_count; i++) { + + if (!loader[i]->recognize_path(local_path)) + continue; + /* + if (p_type_hint!="" && !loader[i]->handles_type(p_type_hint)) + continue; + */ + + return loader[i]->get_import_order(p_path); + } + + return 0; +} + bool ResourceLoader::is_import_valid(const String &p_path) { String path = _path_remap(p_path); diff --git a/core/io/resource_loader.h b/core/io/resource_loader.h index a71a568569..5deffbca1a 100644 --- a/core/io/resource_loader.h +++ b/core/io/resource_loader.h @@ -67,6 +67,7 @@ public: virtual void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false); virtual Error rename_dependencies(const String &p_path, const Map<String, String> &p_map) { return OK; } virtual bool is_import_valid(const String &p_path) const { return true; } + virtual int get_import_order(const String &p_path) const { return 0; } virtual ~ResourceFormatLoader() {} }; @@ -110,6 +111,7 @@ public: static void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false); static Error rename_dependencies(const String &p_path, const Map<String, String> &p_map); static bool is_import_valid(const String &p_path); + static int get_import_order(const String &p_path); static void set_timestamp_on_load(bool p_timestamp) { timestamp_on_load = p_timestamp; } diff --git a/core/io/stream_peer.cpp b/core/io/stream_peer.cpp index f4f81f0807..2583eb369d 100644 --- a/core/io/stream_peer.cpp +++ b/core/io/stream_peer.cpp @@ -405,9 +405,9 @@ void StreamPeer::_bind_methods() { void StreamPeerBuffer::_bind_methods() { - ClassDB::bind_method(D_METHOD("seek", "pos"), &StreamPeerBuffer::seek); + ClassDB::bind_method(D_METHOD("seek", "position"), &StreamPeerBuffer::seek); ClassDB::bind_method(D_METHOD("get_size"), &StreamPeerBuffer::get_size); - ClassDB::bind_method(D_METHOD("get_pos"), &StreamPeerBuffer::get_pos); + ClassDB::bind_method(D_METHOD("get_position"), &StreamPeerBuffer::get_position); ClassDB::bind_method(D_METHOD("resize", "size"), &StreamPeerBuffer::resize); ClassDB::bind_method(D_METHOD("set_data_array", "data"), &StreamPeerBuffer::set_data_array); ClassDB::bind_method(D_METHOD("get_data_array"), &StreamPeerBuffer::get_data_array); @@ -484,7 +484,7 @@ int StreamPeerBuffer::get_size() const { return data.size(); } -int StreamPeerBuffer::get_pos() const { +int StreamPeerBuffer::get_position() const { return pointer; } diff --git a/core/io/stream_peer.h b/core/io/stream_peer.h index 1ee997c123..b120d18501 100644 --- a/core/io/stream_peer.h +++ b/core/io/stream_peer.h @@ -111,7 +111,7 @@ public: void seek(int p_pos); int get_size() const; - int get_pos() const; + int get_position() const; void resize(int p_size); void set_data_array(const PoolVector<uint8_t> &p_data); diff --git a/core/io/xml_parser.cpp b/core/io/xml_parser.cpp index 3a4be7cd13..8ae68183d7 100644 --- a/core/io/xml_parser.cpp +++ b/core/io/xml_parser.cpp @@ -369,7 +369,7 @@ void XMLParser::_bind_methods() { ClassDB::bind_method(D_METHOD("is_empty"), &XMLParser::is_empty); ClassDB::bind_method(D_METHOD("get_current_line"), &XMLParser::get_current_line); ClassDB::bind_method(D_METHOD("skip_section"), &XMLParser::skip_section); - ClassDB::bind_method(D_METHOD("seek", "pos"), &XMLParser::seek); + ClassDB::bind_method(D_METHOD("seek", "position"), &XMLParser::seek); ClassDB::bind_method(D_METHOD("open", "file"), &XMLParser::open); ClassDB::bind_method(D_METHOD("open_buffer", "buffer"), &XMLParser::open_buffer); diff --git a/core/io/zip_io.h b/core/io/zip_io.h index 8cf971ee08..ce3c919b77 100644 --- a/core/io/zip_io.h +++ b/core/io/zip_io.h @@ -72,7 +72,7 @@ static uLong zipio_write(voidpf opaque, voidpf stream, const void *buf, uLong si static long zipio_tell(voidpf opaque, voidpf stream) { FileAccess *f = *(FileAccess **)opaque; - return f->get_pos(); + return f->get_position(); }; static long zipio_seek(voidpf opaque, voidpf stream, uLong offset, int origin) { @@ -83,7 +83,7 @@ static long zipio_seek(voidpf opaque, voidpf stream, uLong offset, int origin) { switch (origin) { case ZLIB_FILEFUNC_SEEK_CUR: - pos = f->get_pos() + offset; + pos = f->get_position() + offset; break; case ZLIB_FILEFUNC_SEEK_END: pos = f->get_len() + offset; diff --git a/core/math/a_star.cpp b/core/math/a_star.cpp index d1afcec18f..4f80fb2491 100644 --- a/core/math/a_star.cpp +++ b/core/math/a_star.cpp @@ -58,7 +58,7 @@ void AStar::add_point(int p_id, const Vector3 &p_pos, real_t p_weight_scale) { } } -Vector3 AStar::get_point_pos(int p_id) const { +Vector3 AStar::get_point_position(int p_id) const { ERR_FAIL_COND_V(!points.has(p_id), Vector3()); @@ -171,7 +171,7 @@ int AStar::get_closest_point(const Vector3 &p_point) const { return closest_id; } -Vector3 AStar::get_closest_pos_in_segment(const Vector3 &p_point) const { +Vector3 AStar::get_closest_position_in_segment(const Vector3 &p_point) const { real_t closest_dist = 1e20; bool found = false; @@ -412,8 +412,8 @@ PoolVector<int> AStar::get_id_path(int p_from_id, int p_to_id) { void AStar::_bind_methods() { ClassDB::bind_method(D_METHOD("get_available_point_id"), &AStar::get_available_point_id); - ClassDB::bind_method(D_METHOD("add_point", "id", "pos", "weight_scale"), &AStar::add_point, DEFVAL(1.0)); - ClassDB::bind_method(D_METHOD("get_point_pos", "id"), &AStar::get_point_pos); + ClassDB::bind_method(D_METHOD("add_point", "id", "position", "weight_scale"), &AStar::add_point, DEFVAL(1.0)); + ClassDB::bind_method(D_METHOD("get_point_position", "id"), &AStar::get_point_position); ClassDB::bind_method(D_METHOD("get_point_weight_scale", "id"), &AStar::get_point_weight_scale); ClassDB::bind_method(D_METHOD("remove_point", "id"), &AStar::remove_point); ClassDB::bind_method(D_METHOD("has_point", "id"), &AStar::has_point); @@ -425,8 +425,8 @@ void AStar::_bind_methods() { ClassDB::bind_method(D_METHOD("clear"), &AStar::clear); - ClassDB::bind_method(D_METHOD("get_closest_point", "to_pos"), &AStar::get_closest_point); - ClassDB::bind_method(D_METHOD("get_closest_pos_in_segment", "to_pos"), &AStar::get_closest_pos_in_segment); + ClassDB::bind_method(D_METHOD("get_closest_point", "to_position"), &AStar::get_closest_point); + ClassDB::bind_method(D_METHOD("get_closest_position_in_segment", "to_position"), &AStar::get_closest_position_in_segment); ClassDB::bind_method(D_METHOD("get_point_path", "from_id", "to_id"), &AStar::get_point_path); ClassDB::bind_method(D_METHOD("get_id_path", "from_id", "to_id"), &AStar::get_id_path); diff --git a/core/math/a_star.h b/core/math/a_star.h index 38d13d510b..2c1e2e2cf7 100644 --- a/core/math/a_star.h +++ b/core/math/a_star.h @@ -101,7 +101,7 @@ public: int get_available_point_id() const; void add_point(int p_id, const Vector3 &p_pos, real_t p_weight_scale = 1); - Vector3 get_point_pos(int p_id) const; + Vector3 get_point_position(int p_id) const; real_t get_point_weight_scale(int p_id) const; void remove_point(int p_id); bool has_point(int p_id) const; @@ -114,7 +114,7 @@ public: void clear(); int get_closest_point(const Vector3 &p_point) const; - Vector3 get_closest_pos_in_segment(const Vector3 &p_point) const; + Vector3 get_closest_position_in_segment(const Vector3 &p_point) const; PoolVector<Vector3> get_point_path(int p_from_id, int p_to_id); PoolVector<int> get_id_path(int p_from_id, int p_to_id); diff --git a/core/math/camera_matrix.cpp b/core/math/camera_matrix.cpp index 7132b6573e..2c587762e8 100644 --- a/core/math/camera_matrix.cpp +++ b/core/math/camera_matrix.cpp @@ -577,7 +577,7 @@ real_t CameraMatrix::get_fov() const { if ((matrix[8] == 0) && (matrix[9] == 0)) { return Math::rad2deg(Math::acos(Math::abs(right_plane.normal.x))) * 2.0; } else { - // our frustum is asymetrical need to calculate the left planes angle seperately.. + // our frustum is asymmetrical need to calculate the left planes angle separately.. Plane left_plane = Plane(matrix[3] + matrix[0], matrix[7] + matrix[4], matrix[11] + matrix[8], diff --git a/core/math/math_funcs.h b/core/math/math_funcs.h index 9651e37f3e..d63da322a5 100644 --- a/core/math/math_funcs.h +++ b/core/math/math_funcs.h @@ -104,8 +104,44 @@ public: static _ALWAYS_INLINE_ double exp(double p_x) { return ::exp(p_x); } static _ALWAYS_INLINE_ float exp(float p_x) { return ::expf(p_x); } - static _ALWAYS_INLINE_ bool is_nan(double p_val) { return (p_val != p_val); } - static _ALWAYS_INLINE_ bool is_nan(float p_val) { return (p_val != p_val); } + static _ALWAYS_INLINE_ bool is_nan(double p_val) { +#ifdef _MSC_VER + return _isnan(p_val); +#elif defined(__GNUC__) && __GNUC__ < 6 + union { + uint64_t u; + double f; + } ieee754; + ieee754.f = p_val; + // (unsigned)(0x7ff0000000000001 >> 32) : 0x7ff00000 + return ((((unsigned)(ieee754.u >> 32) & 0x7fffffff) + ((unsigned)ieee754.u != 0)) > 0x7ff00000); +#else + return isnan(p_val); +#endif + } + + static _ALWAYS_INLINE_ bool is_nan(float p_val) { +#ifdef _MSC_VER + return _isnan(p_val); +#elif defined(__GNUC__) && __GNUC__ < 6 + union { + uint32_t u; + float f; + } ieee754; + ieee754.f = p_val; + // ----------------------------------- + // (single-precision floating-point) + // NaN : s111 1111 1xxx xxxx xxxx xxxx xxxx xxxx + // : (> 0x7f800000) + // where, + // s : sign + // x : non-zero number + // ----------------------------------- + return ((ieee754.u & 0x7fffffff) > 0x7f800000); +#else + return isnan(p_val); +#endif + } static _ALWAYS_INLINE_ bool is_inf(double p_val) { #ifdef _MSC_VER diff --git a/core/oa_hash_map.h b/core/oa_hash_map.h new file mode 100644 index 0000000000..66a1e348a1 --- /dev/null +++ b/core/oa_hash_map.h @@ -0,0 +1,593 @@ +/*************************************************************************/ +/* oa_hash_map.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ +#ifndef OA_HASH_MAP_H +#define OA_HASH_MAP_H + +#include "hashfuncs.h" +#include "math_funcs.h" +#include "os/copymem.h" +#include "os/memory.h" + +// uncomment this to disable intial local storage. +#define OA_HASH_MAP_INITIAL_LOCAL_STORAGE + +/** + * This class implements a hash map datastructure that uses open addressing with + * local probing. + * + * It can give huge performance improvements over a chained HashMap because of + * the increased data locality. + * + * Because of that locality property it's important to not use "large" value + * types as the "TData" type. If TData values are too big it can cause more + * cache misses then chaining. If larger values are needed then storing those + * in a separate array and using pointers or indices to reference them is the + * better solution. + * + * This hash map also implements real-time incremental rehashing. + * + */ +template <class TKey, class TData, + uint16_t INITIAL_NUM_ELEMENTS = 64, + class Hasher = HashMapHasherDefault, + class Comparator = HashMapComparatorDefault<TKey> > +class OAHashMap { + +private: +#ifdef OA_HASH_MAP_INITIAL_LOCAL_STORAGE + TData local_data[INITIAL_NUM_ELEMENTS]; + TKey local_keys[INITIAL_NUM_ELEMENTS]; + uint32_t local_hashes[INITIAL_NUM_ELEMENTS]; + uint8_t local_flags[INITIAL_NUM_ELEMENTS / 4 + (INITIAL_NUM_ELEMENTS % 4 != 0 ? 1 : 0)]; +#endif + + struct { + TData *data; + TKey *keys; + uint32_t *hashes; + + // This is actually an array of bits, 4 bit pairs per octet. + // | ba ba ba ba | ba ba ba ba | .... + // + // if a is set it means that there is an element present. + // if b is set it means that an element was deleted. This is needed for + // the local probing to work without relocating any succeeding and + // colliding entries. + uint8_t *flags; + + uint32_t capacity; + } table, old_table; + + bool is_rehashing; + uint32_t rehash_position; + uint32_t rehash_amount; + + uint32_t elements; + + /* Methods */ + + // returns true if the value already existed, false if it's a new entry + bool _raw_set_with_hash(uint32_t p_hash, const TKey &p_key, const TData &p_data) { + for (int i = 0; i < table.capacity; i++) { + + int pos = (p_hash + i) % table.capacity; + + int flags_pos = pos / 4; + int flags_pos_offset = pos % 4; + + bool is_filled_flag = table.flags[flags_pos] & (1 << (2 * flags_pos_offset)); + bool is_deleted_flag = table.flags[flags_pos] & (1 << (2 * flags_pos_offset + 1)); + + if (is_filled_flag) { + if (table.hashes[pos] == p_hash && Comparator::compare(table.keys[pos], p_key)) { + table.data[pos] = p_data; + return true; + } + continue; + } + + table.keys[pos] = p_key; + table.data[pos] = p_data; + table.hashes[pos] = p_hash; + + table.flags[flags_pos] |= (1 << (2 * flags_pos_offset)); + table.flags[flags_pos] &= ~(1 << (2 * flags_pos_offset + 1)); + + return false; + } + return false; + } + +public: + _FORCE_INLINE_ uint32_t get_capacity() const { return table.capacity; } + _FORCE_INLINE_ uint32_t get_num_elements() const { return elements; } + + void set(const TKey &p_key, const TData &p_data) { + + uint32_t hash = Hasher::hash(p_key); + + // We don't progress the rehashing if the table just got resized + // to keep the cost of this function low. + if (is_rehashing) { + + // rehash progress + + for (int i = 0; i <= rehash_amount && rehash_position < old_table.capacity; rehash_position++) { + + int flags_pos = rehash_position / 4; + int flags_pos_offset = rehash_position % 4; + + bool is_filled_flag = (old_table.flags[flags_pos] & (1 << (2 * flags_pos_offset))) > 0; + bool is_deleted_flag = (old_table.flags[flags_pos] & (1 << (2 * flags_pos_offset + 1))) > 0; + + if (is_filled_flag) { + _raw_set_with_hash(old_table.hashes[rehash_position], old_table.keys[rehash_position], old_table.data[rehash_position]); + + old_table.keys[rehash_position].~TKey(); + old_table.data[rehash_position].~TData(); + + memnew_placement(&old_table.keys[rehash_position], TKey); + memnew_placement(&old_table.data[rehash_position], TData); + + old_table.flags[flags_pos] &= ~(1 << (2 * flags_pos_offset)); + old_table.flags[flags_pos] |= (1 << (2 * flags_pos_offset + 1)); + } + } + + if (rehash_position >= old_table.capacity) { + + // wohooo, we can get rid of the old table. + is_rehashing = false; + +#ifdef OA_HASH_MAP_INITIAL_LOCAL_STORAGE + if (old_table.data == local_data) { + // Everything is local, so no cleanup :P + } else +#endif + { + memdelete_arr(old_table.data); + memdelete_arr(old_table.keys); + memdelete_arr(old_table.hashes); + memdelete_arr(old_table.flags); + } + } + } + + // Table is almost full, resize and start rehashing process. + if (elements >= table.capacity * 0.7) { + + old_table.capacity = table.capacity; + old_table.data = table.data; + old_table.flags = table.flags; + old_table.hashes = table.hashes; + old_table.keys = table.keys; + + table.capacity = old_table.capacity * 2; + + table.data = memnew_arr(TData, table.capacity); + table.flags = memnew_arr(uint8_t, table.capacity / 4 + (table.capacity % 4 != 0 ? 1 : 0)); + table.hashes = memnew_arr(uint32_t, table.capacity); + table.keys = memnew_arr(TKey, table.capacity); + + zeromem(table.flags, table.capacity / 4 + (table.capacity % 4 != 0 ? 1 : 0)); + + is_rehashing = true; + rehash_position = 0; + rehash_amount = (elements * 2) / (table.capacity * 0.7 - old_table.capacity); + } + + if (!_raw_set_with_hash(hash, p_key, p_data)) + elements++; + } + + /** + * returns true if the value was found, false otherwise. + * + * if r_data is not NULL then the value will be written to the object + * it points to. + */ + bool lookup(const TKey &p_key, TData *r_data) { + + uint32_t hash = Hasher::hash(p_key); + + bool check_old_table = is_rehashing; + bool check_new_table = true; + + // search for the key and return the value associated with it + // + // if we're rehashing we need to check both the old and the + // current table. If we find a value in the old table we still + // need to continue searching in the new table as it might have + // been added after + + TData *value = NULL; + + for (int i = 0; i < table.capacity; i++) { + + if (!check_new_table && !check_old_table) { + + break; + } + + // if we're rehashing check the old table + if (check_old_table && i < old_table.capacity) { + + int pos = (hash + i) % old_table.capacity; + + int flags_pos = pos / 4; + int flags_pos_offset = pos % 4; + + bool is_filled_flag = (old_table.flags[flags_pos] & (1 << (2 * flags_pos_offset))) > 0; + bool is_deleted_flag = (old_table.flags[flags_pos] & (1 << (2 * flags_pos_offset + 1))) > 0; + + if (is_filled_flag) { + // found our entry? + if (old_table.hashes[pos] == hash && Comparator::compare(old_table.keys[pos], p_key)) { + value = &old_table.data[pos]; + check_old_table = false; + } + } else if (!is_deleted_flag) { + + // we hit an empty field here, we don't + // need to further check this old table + // because we know it's not in here. + + check_old_table = false; + } + } + + if (check_new_table) { + + int pos = (hash + i) % table.capacity; + + int flags_pos = pos / 4; + int flags_pos_offset = pos % 4; + + bool is_filled_flag = (table.flags[flags_pos] & (1 << (2 * flags_pos_offset))) > 0; + bool is_deleted_flag = (table.flags[flags_pos] & (1 << (2 * flags_pos_offset + 1))) > 0; + + if (is_filled_flag) { + // found our entry? + if (table.hashes[pos] == hash && Comparator::compare(table.keys[pos], p_key)) { + if (r_data != NULL) + *r_data = table.data[pos]; + return true; + } + continue; + } else if (is_deleted_flag) { + continue; + } else if (value != NULL) { + + // We found a value in the old table + if (r_data != NULL) + *r_data = *value; + return true; + } else { + check_new_table = false; + } + } + } + + if (value != NULL) { + if (r_data != NULL) + *r_data = *value; + return true; + } + return false; + } + + _FORCE_INLINE_ bool has(const TKey &p_key) { + return lookup(p_key, NULL); + } + + void remove(const TKey &p_key) { + uint32_t hash = Hasher::hash(p_key); + + bool check_old_table = is_rehashing; + bool check_new_table = true; + + for (int i = 0; i < table.capacity; i++) { + + if (!check_new_table && !check_old_table) { + return; + } + + // if we're rehashing check the old table + if (check_old_table && i < old_table.capacity) { + + int pos = (hash + i) % old_table.capacity; + + int flags_pos = pos / 4; + int flags_pos_offset = pos % 4; + + bool is_filled_flag = (old_table.flags[flags_pos] & (1 << (2 * flags_pos_offset))) > 0; + bool is_deleted_flag = (old_table.flags[flags_pos] & (1 << (2 * flags_pos_offset + 1))) > 0; + + if (is_filled_flag) { + // found our entry? + if (old_table.hashes[pos] == hash && Comparator::compare(old_table.keys[pos], p_key)) { + old_table.keys[pos].~TKey(); + old_table.data[pos].~TData(); + + memnew_placement(&old_table.keys[pos], TKey); + memnew_placement(&old_table.data[pos], TData); + + old_table.flags[flags_pos] &= ~(1 << (2 * flags_pos_offset)); + old_table.flags[flags_pos] |= (1 << (2 * flags_pos_offset + 1)); + + elements--; + return; + } + } else if (!is_deleted_flag) { + + // we hit an empty field here, we don't + // need to further check this old table + // because we know it's not in here. + + check_old_table = false; + } + } + + if (check_new_table) { + + int pos = (hash + i) % table.capacity; + + int flags_pos = pos / 4; + int flags_pos_offset = pos % 4; + + bool is_filled_flag = (table.flags[flags_pos] & (1 << (2 * flags_pos_offset))) > 0; + bool is_deleted_flag = (table.flags[flags_pos] & (1 << (2 * flags_pos_offset + 1))) > 0; + + if (is_filled_flag) { + // found our entry? + if (table.hashes[pos] == hash && Comparator::compare(table.keys[pos], p_key)) { + table.keys[pos].~TKey(); + table.data[pos].~TData(); + + memnew_placement(&table.keys[pos], TKey); + memnew_placement(&table.data[pos], TData); + + table.flags[flags_pos] &= ~(1 << (2 * flags_pos_offset)); + table.flags[flags_pos] |= (1 << (2 * flags_pos_offset + 1)); + + // don't return here, this value might still be in the old table + // if it was already relocated. + + elements--; + return; + } + continue; + } else if (is_deleted_flag) { + continue; + } else { + check_new_table = false; + } + } + } + } + + struct Iterator { + bool valid; + + uint32_t hash; + + const TKey *key; + const TData *data; + + private: + friend class OAHashMap; + bool was_from_old_table; + }; + + Iterator iter() const { + Iterator it; + + it.valid = false; + it.was_from_old_table = false; + + bool check_old_table = is_rehashing; + + for (int i = 0; i < table.capacity; i++) { + + // if we're rehashing check the old table first + if (check_old_table && i < old_table.capacity) { + + int pos = i; + + int flags_pos = pos / 4; + int flags_pos_offset = pos % 4; + + bool is_filled_flag = (old_table.flags[flags_pos] & (1 << (2 * flags_pos_offset))) > 0; + + if (is_filled_flag) { + it.valid = true; + it.hash = old_table.hashes[pos]; + it.data = &old_table.data[pos]; + it.key = &old_table.keys[pos]; + + it.was_from_old_table = true; + + return it; + } + } + + { + + int pos = i; + + int flags_pos = pos / 4; + int flags_pos_offset = pos % 4; + + bool is_filled_flag = (table.flags[flags_pos] & (1 << (2 * flags_pos_offset))) > 0; + + if (is_filled_flag) { + it.valid = true; + it.hash = table.hashes[pos]; + it.data = &table.data[pos]; + it.key = &table.keys[pos]; + + return it; + } + } + } + + return it; + } + + Iterator next_iter(const Iterator &p_iter) const { + if (!p_iter.valid) { + return p_iter; + } + + Iterator it; + + it.valid = false; + it.was_from_old_table = false; + + bool check_old_table = is_rehashing; + + // we use this to skip the first check or not + bool was_from_old_table = p_iter.was_from_old_table; + + int prev_index = (p_iter.data - (p_iter.was_from_old_table ? old_table.data : table.data)); + + if (!was_from_old_table) { + prev_index++; + } + + for (int i = prev_index; i < table.capacity; i++) { + + // if we're rehashing check the old table first + if (check_old_table && i < old_table.capacity && !was_from_old_table) { + + int pos = i; + + int flags_pos = pos / 4; + int flags_pos_offset = pos % 4; + + bool is_filled_flag = (old_table.flags[flags_pos] & (1 << (2 * flags_pos_offset))) > 0; + + if (is_filled_flag) { + it.valid = true; + it.hash = old_table.hashes[pos]; + it.data = &old_table.data[pos]; + it.key = &old_table.keys[pos]; + + it.was_from_old_table = true; + + return it; + } + } + + was_from_old_table = false; + + { + int pos = i; + + int flags_pos = pos / 4; + int flags_pos_offset = pos % 4; + + bool is_filled_flag = (table.flags[flags_pos] & (1 << (2 * flags_pos_offset))) > 0; + + if (is_filled_flag) { + it.valid = true; + it.hash = table.hashes[pos]; + it.data = &table.data[pos]; + it.key = &table.keys[pos]; + + return it; + } + } + } + + return it; + } + + OAHashMap(uint32_t p_initial_capacity = INITIAL_NUM_ELEMENTS) { + +#ifdef OA_HASH_MAP_INITIAL_LOCAL_STORAGE + + if (p_initial_capacity <= INITIAL_NUM_ELEMENTS) { + table.data = local_data; + table.keys = local_keys; + table.hashes = local_hashes; + table.flags = local_flags; + + zeromem(table.flags, INITIAL_NUM_ELEMENTS / 4 + (INITIAL_NUM_ELEMENTS % 4 != 0 ? 1 : 0)); + + table.capacity = INITIAL_NUM_ELEMENTS; + elements = 0; + } else +#endif + { + table.data = memnew_arr(TData, p_initial_capacity); + table.keys = memnew_arr(TKey, p_initial_capacity); + table.hashes = memnew_arr(uint32_t, p_initial_capacity); + table.flags = memnew_arr(uint8_t, p_initial_capacity / 4 + (p_initial_capacity % 4 != 0 ? 1 : 0)); + + zeromem(table.flags, p_initial_capacity / 4 + (p_initial_capacity % 4 != 0 ? 1 : 0)); + + table.capacity = p_initial_capacity; + elements = 0; + } + + is_rehashing = false; + rehash_position = 0; + } + + ~OAHashMap() { +#ifdef OA_HASH_MAP_INITIAL_LOCAL_STORAGE + if (table.capacity <= INITIAL_NUM_ELEMENTS) { + return; // Everything is local, so no cleanup :P + } +#endif + if (is_rehashing) { + +#ifdef OA_HASH_MAP_INITIAL_LOCAL_STORAGE + if (old_table.data == local_data) { + // Everything is local, so no cleanup :P + } else +#endif + { + memdelete_arr(old_table.data); + memdelete_arr(old_table.keys); + memdelete_arr(old_table.hashes); + memdelete_arr(old_table.flags); + } + } + + memdelete_arr(table.data); + memdelete_arr(table.keys); + memdelete_arr(table.hashes); + memdelete_arr(table.flags); + } +}; + +#endif diff --git a/core/object.cpp b/core/object.cpp index 23e32a214a..b1770f1d7a 100644 --- a/core/object.cpp +++ b/core/object.cpp @@ -277,32 +277,32 @@ MethodInfo::MethodInfo(Variant::Type ret, const String &p_name, const PropertyIn MethodInfo::MethodInfo(const PropertyInfo &p_ret, const String &p_name) : name(p_name), flags(METHOD_FLAG_NORMAL), + return_val(p_ret), id(0) { - return_val = p_ret; } MethodInfo::MethodInfo(const PropertyInfo &p_ret, const String &p_name, const PropertyInfo &p_param1) : name(p_name), + return_val(p_ret), flags(METHOD_FLAG_NORMAL), id(0) { - return_val = p_ret; arguments.push_back(p_param1); } MethodInfo::MethodInfo(const PropertyInfo &p_ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2) : name(p_name), + return_val(p_ret), flags(METHOD_FLAG_NORMAL), id(0) { - return_val = p_ret; arguments.push_back(p_param1); arguments.push_back(p_param2); } MethodInfo::MethodInfo(const PropertyInfo &p_ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3) : name(p_name), + return_val(p_ret), flags(METHOD_FLAG_NORMAL), id(0) { - return_val = p_ret; arguments.push_back(p_param1); arguments.push_back(p_param2); arguments.push_back(p_param3); @@ -310,9 +310,9 @@ MethodInfo::MethodInfo(const PropertyInfo &p_ret, const String &p_name, const Pr MethodInfo::MethodInfo(const PropertyInfo &p_ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3, const PropertyInfo &p_param4) : name(p_name), + return_val(p_ret), flags(METHOD_FLAG_NORMAL), id(0) { - return_val = p_ret; arguments.push_back(p_param1); arguments.push_back(p_param2); arguments.push_back(p_param3); @@ -321,9 +321,9 @@ MethodInfo::MethodInfo(const PropertyInfo &p_ret, const String &p_name, const Pr MethodInfo::MethodInfo(const PropertyInfo &p_ret, const String &p_name, const PropertyInfo &p_param1, const PropertyInfo &p_param2, const PropertyInfo &p_param3, const PropertyInfo &p_param4, const PropertyInfo &p_param5) : name(p_name), + return_val(p_ret), flags(METHOD_FLAG_NORMAL), id(0) { - return_val = p_ret; arguments.push_back(p_param1); arguments.push_back(p_param2); arguments.push_back(p_param3); diff --git a/core/object.h b/core/object.h index 644e2b8270..3070439138 100644 --- a/core/object.h +++ b/core/object.h @@ -148,6 +148,7 @@ struct PropertyInfo { hint(PROPERTY_HINT_NONE), usage(PROPERTY_USAGE_DEFAULT) { } + PropertyInfo(Variant::Type p_type, const String p_name, PropertyHint p_hint = PROPERTY_HINT_NONE, const String &p_hint_string = "", uint32_t p_usage = PROPERTY_USAGE_DEFAULT, const StringName &p_class_name = StringName()) : type(p_type), name(p_name), @@ -161,12 +162,12 @@ struct PropertyInfo { class_name = p_class_name; } } + PropertyInfo(const StringName &p_class_name) : type(Variant::OBJECT), + class_name(p_class_name), hint(PROPERTY_HINT_NONE), usage(PROPERTY_USAGE_DEFAULT) { - - class_name = p_class_name; } bool operator<(const PropertyInfo &p_info) const { diff --git a/core/os/dir_access.cpp b/core/os/dir_access.cpp index f24d6d16ca..0875f78478 100644 --- a/core/os/dir_access.cpp +++ b/core/os/dir_access.cpp @@ -292,7 +292,7 @@ String DirAccess::get_full_path(const String &p_path, AccessType p_access) { return full; } -Error DirAccess::copy(String p_from, String p_to) { +Error DirAccess::copy(String p_from, String p_to, int chmod_flags) { //printf("copy %s -> %s\n",p_from.ascii().get_data(),p_to.ascii().get_data()); Error err; @@ -312,7 +312,7 @@ Error DirAccess::copy(String p_from, String p_to) { } fsrc->seek_end(0); - int size = fsrc->get_pos(); + int size = fsrc->get_position(); fsrc->seek(0); err = OK; while (size--) { @@ -329,6 +329,11 @@ Error DirAccess::copy(String p_from, String p_to) { fdst->store_8(fsrc->get_8()); } + if (err == OK && chmod_flags != -1) { + fdst->close(); + err = fdst->_chmod(p_to, chmod_flags); + } + memdelete(fsrc); memdelete(fdst); diff --git a/core/os/dir_access.h b/core/os/dir_access.h index 6ad8b4c49b..7fa3ce5cf1 100644 --- a/core/os/dir_access.h +++ b/core/os/dir_access.h @@ -89,7 +89,7 @@ public: static bool exists(String p_dir); virtual size_t get_space_left() = 0; - virtual Error copy(String p_from, String p_to); + virtual Error copy(String p_from, String p_to, int chmod_flags = -1); virtual Error rename(String p_from, String p_to) = 0; virtual Error remove(String p_name) = 0; diff --git a/core/os/file_access.h b/core/os/file_access.h index 8e5728f525..34e7549fa3 100644 --- a/core/os/file_access.h +++ b/core/os/file_access.h @@ -90,7 +90,7 @@ public: virtual void seek(size_t p_position) = 0; ///< seek to a given position virtual void seek_end(int64_t p_position = 0) = 0; ///< seek from the end of file - virtual size_t get_pos() const = 0; ///< get position in the file + virtual size_t get_position() const = 0; ///< get position in the file virtual size_t get_len() const = 0; ///< get size of the file virtual bool eof_reached() const = 0; ///< reading passed EOF @@ -140,6 +140,8 @@ public: virtual Error reopen(const String &p_path, int p_mode_flags); ///< does not change the AccessType + virtual Error _chmod(const String &p_path, int p_mod) { return FAILED; } + static FileAccess *create(AccessType p_access); /// Create a file access (for the current platform) this is the only portable way of accessing files. static FileAccess *create_for_path(const String &p_path); static FileAccess *open(const String &p_path, int p_mode_flags, Error *r_error = NULL); /// Create a file access (for the current platform) this is the only portable way of accessing files. diff --git a/core/os/input.cpp b/core/os/input.cpp index 65752662d7..a4b82299a7 100644 --- a/core/os/input.cpp +++ b/core/os/input.cpp @@ -80,7 +80,7 @@ void Input::_bind_methods() { ClassDB::bind_method(D_METHOD("get_mouse_button_mask"), &Input::get_mouse_button_mask); ClassDB::bind_method(D_METHOD("set_mouse_mode", "mode"), &Input::set_mouse_mode); ClassDB::bind_method(D_METHOD("get_mouse_mode"), &Input::get_mouse_mode); - ClassDB::bind_method(D_METHOD("warp_mouse_pos", "to"), &Input::warp_mouse_pos); + ClassDB::bind_method(D_METHOD("warp_mouse_position", "to"), &Input::warp_mouse_position); ClassDB::bind_method(D_METHOD("action_press", "action"), &Input::action_press); ClassDB::bind_method(D_METHOD("action_release", "action"), &Input::action_release); ClassDB::bind_method(D_METHOD("set_custom_mouse_cursor", "image", "hotspot"), &Input::set_custom_mouse_cursor, DEFVAL(Vector2())); diff --git a/core/os/input.h b/core/os/input.h index f98b97e647..97d3bef4f9 100644 --- a/core/os/input.h +++ b/core/os/input.h @@ -81,7 +81,7 @@ public: virtual Point2 get_last_mouse_speed() const = 0; virtual int get_mouse_button_mask() const = 0; - virtual void warp_mouse_pos(const Vector2 &p_to) = 0; + virtual void warp_mouse_position(const Vector2 &p_to) = 0; virtual Point2i warp_mouse_motion(const Ref<InputEventMouseMotion> &p_motion, const Rect2 &p_rect) = 0; virtual Vector3 get_gravity() const = 0; diff --git a/core/os/input_event.cpp b/core/os/input_event.cpp index 88037859aa..bef98ac3f2 100644 --- a/core/os/input_event.cpp +++ b/core/os/input_event.cpp @@ -781,7 +781,7 @@ void InputEventScreenTouch::_bind_methods() { ClassDB::bind_method(D_METHOD("set_index", "index"), &InputEventScreenTouch::set_index); ClassDB::bind_method(D_METHOD("get_index"), &InputEventScreenTouch::get_index); - ClassDB::bind_method(D_METHOD("set_position", "pos"), &InputEventScreenTouch::set_position); + ClassDB::bind_method(D_METHOD("set_position", "position"), &InputEventScreenTouch::set_position); ClassDB::bind_method(D_METHOD("get_position"), &InputEventScreenTouch::get_position); ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventScreenTouch::set_pressed); diff --git a/core/os/os.h b/core/os/os.h index 2fc87e44a0..38bbbc0a57 100644 --- a/core/os/os.h +++ b/core/os/os.h @@ -156,7 +156,7 @@ public: virtual void set_mouse_mode(MouseMode p_mode); virtual MouseMode get_mouse_mode() const; - virtual void warp_mouse_pos(const Point2 &p_to) {} + virtual void warp_mouse_position(const Point2 &p_to) {} virtual Point2 get_mouse_position() const = 0; virtual int get_mouse_button_state() const = 0; virtual void set_window_title(const String &p_title) = 0; diff --git a/core/project_settings.cpp b/core/project_settings.cpp index 72d40b42c3..14ebe87dc5 100644 --- a/core/project_settings.cpp +++ b/core/project_settings.cpp @@ -307,8 +307,8 @@ Error ProjectSettings::setup(const String &p_path, const String &p_main_pack) { if (exec_path != "") { bool found = false; - // get our filename without our path (note, not using exec_path.get_basename anymore because not all file systems have dots in their file names!) - String filebase_name = exec_path.get_file(); + // get our filename without our path (note, using exec_path.get_file before get_basename anymore because not all file systems have dots in their file names!) + String filebase_name = exec_path.get_file().get_basename(); // try to open at the location of executable String datapack_name = exec_path.get_base_dir().plus_file(filebase_name) + ".pck"; @@ -910,7 +910,7 @@ Variant ProjectSettings::property_get_revert(const String &p_name) { void ProjectSettings::_bind_methods() { ClassDB::bind_method(D_METHOD("has", "name"), &ProjectSettings::has); - ClassDB::bind_method(D_METHOD("set_order", "name", "pos"), &ProjectSettings::set_order); + ClassDB::bind_method(D_METHOD("set_order", "name", "position"), &ProjectSettings::set_order); ClassDB::bind_method(D_METHOD("get_order", "name"), &ProjectSettings::get_order); ClassDB::bind_method(D_METHOD("set_initial_value", "name", "value"), &ProjectSettings::set_initial_value); ClassDB::bind_method(D_METHOD("add_property_info", "hint"), &ProjectSettings::_add_property_info_bind); diff --git a/core/register_core_types.cpp b/core/register_core_types.cpp index 27c31127a4..0e34a3eea5 100644 --- a/core/register_core_types.cpp +++ b/core/register_core_types.cpp @@ -68,6 +68,7 @@ static _Engine *_engine = NULL; static _ClassDB *_classdb = NULL; static _Marshalls *_marshalls = NULL; static TranslationLoaderPO *resource_format_po = NULL; +static _JSON *_json = NULL; static IP *ip = NULL; @@ -162,6 +163,8 @@ void register_core_types() { ClassDB::register_class<AStar>(); ClassDB::register_class<EncodedObjectAsID>(); + ClassDB::register_class<JSONParseResult>(); + ip = IP::create(); _geometry = memnew(_Geometry); @@ -172,6 +175,7 @@ void register_core_types() { _engine = memnew(_Engine); _classdb = memnew(_ClassDB); _marshalls = memnew(_Marshalls); + _json = memnew(_JSON); } void register_core_settings() { @@ -193,6 +197,7 @@ void register_core_singletons() { ProjectSettings::get_singleton()->add_singleton(ProjectSettings::Singleton("TranslationServer", TranslationServer::get_singleton())); ProjectSettings::get_singleton()->add_singleton(ProjectSettings::Singleton("Input", Input::get_singleton())); ProjectSettings::get_singleton()->add_singleton(ProjectSettings::Singleton("InputMap", InputMap::get_singleton())); + ProjectSettings::get_singleton()->add_singleton(ProjectSettings::Singleton("JSON", _JSON::get_singleton())); } void unregister_core_types() { @@ -203,6 +208,7 @@ void unregister_core_types() { memdelete(_engine); memdelete(_classdb); memdelete(_marshalls); + memdelete(_json); memdelete(_geometry); diff --git a/core/set.h b/core/set.h index f68d78cea1..d91dd9b3ea 100644 --- a/core/set.h +++ b/core/set.h @@ -135,7 +135,6 @@ private: #ifdef GLOBALNIL_DISABLED memdelete_allocator<Element, A>(_nil); #endif - //memdelete_allocator<Element,A>(_root); } }; @@ -146,6 +145,7 @@ private: ERR_FAIL_COND(p_node == _data._nil && p_color == RED); p_node->color = p_color; } + inline void _rotate_left(Element *p_node) { Element *r = p_node->right; @@ -194,8 +194,9 @@ private: while (node == node->parent->right) { node = node->parent; } + if (node->parent == _data._root) - return NULL; + return NULL; // No successor, as p_node is the last node. return node->parent; } } @@ -213,11 +214,11 @@ private: } else { while (node == node->parent->left) { - if (node->parent == _data._root) - return NULL; - node = node->parent; } + + if (node == _data._root) + return NULL; // No predecessor, as p_node is the first node. return node->parent; } } @@ -228,16 +229,15 @@ private: C less; while (node != _data._nil) { - if (less(p_value, node->value)) node = node->left; else if (less(node->value, p_value)) node = node->right; else - break; // found + return node; // found } - return (node != _data._nil) ? node : NULL; + return NULL; } Element *_lower_bound(const T &p_value) const { @@ -254,21 +254,16 @@ private: else if (less(node->value, p_value)) node = node->right; else - break; // found + return node; // found } - if (node == _data._nil) { - if (prev == NULL) - return NULL; - if (less(prev->value, p_value)) { - - prev = prev->_next; - } + if (prev == NULL) + return NULL; // tree empty - return prev; + if (less(prev->value, p_value)) + prev = prev->_next; - } else - return node; + return prev; } Element *_insert(const T &p_value, bool &r_exists) { @@ -291,22 +286,21 @@ private: } } - Element *new_node = memnew_allocator(Element, A); + r_exists = false; + Element *new_node = memnew_allocator(Element, A); new_node->parent = new_parent; new_node->right = _data._nil; new_node->left = _data._nil; new_node->value = p_value; //new_node->data=_data; - if (new_parent == _data._root || less(p_value, new_parent->value)) { + if (new_parent == _data._root || less(p_value, new_parent->value)) { new_parent->left = new_node; } else { new_parent->right = new_node; } - r_exists = false; - new_node->_next = _successor(new_node); new_node->_prev = _predecessor(new_node); if (new_node->_next) @@ -324,154 +318,159 @@ private: if (exists) return new_node; - Element *node = new_node; _data.size_cache++; + Element *node = new_node; + Element *nparent = node->parent; + Element *ngrand_parent; - while (node->parent->color == RED) { - - if (node->parent == node->parent->parent->left) { + while (nparent->color == RED) { - Element *aux = node->parent->parent->right; + ngrand_parent = nparent->parent; - if (aux->color == RED) { - _set_color(node->parent, BLACK); - _set_color(aux, BLACK); - _set_color(node->parent->parent, RED); - node = node->parent->parent; + if (nparent == ngrand_parent->left) { + if (ngrand_parent->right->color == RED) { + _set_color(nparent, BLACK); + _set_color(ngrand_parent->right, BLACK); + _set_color(ngrand_parent, RED); + node = ngrand_parent; + nparent = node->parent; } else { - if (node == node->parent->right) { - node = node->parent; - _rotate_left(node); + if (node == nparent->right) { + _rotate_left(nparent); + node = nparent; + nparent = node->parent; } - _set_color(node->parent, BLACK); - _set_color(node->parent->parent, RED); - _rotate_right(node->parent->parent); + _set_color(nparent, BLACK); + _set_color(ngrand_parent, RED); + _rotate_right(ngrand_parent); } } else { - Element *aux = node->parent->parent->left; - - if (aux->color == RED) { - _set_color(node->parent, BLACK); - _set_color(aux, BLACK); - _set_color(node->parent->parent, RED); - node = node->parent->parent; + if (ngrand_parent->left->color == RED) { + _set_color(nparent, BLACK); + _set_color(ngrand_parent->left, BLACK); + _set_color(ngrand_parent, RED); + node = ngrand_parent; + nparent = node->parent; } else { - if (node == node->parent->left) { - node = node->parent; - _rotate_right(node); + if (node == nparent->left) { + _rotate_right(nparent); + node = nparent; + nparent = node->parent; } - _set_color(node->parent, BLACK); - _set_color(node->parent->parent, RED); - _rotate_left(node->parent->parent); + _set_color(nparent, BLACK); + _set_color(ngrand_parent, RED); + _rotate_left(ngrand_parent); } } } + _set_color(_data._root->left, BLACK); + return new_node; } void _erase_fix(Element *p_node) { Element *root = _data._root->left; - Element *node = p_node; - - while ((node->color == BLACK) && (root != node)) { - if (node == node->parent->left) { - Element *aux = node->parent->right; - if (aux->color == RED) { - _set_color(aux, BLACK); - _set_color(node->parent, RED); - _rotate_left(node->parent); - aux = node->parent->right; - } - if ((aux->right->color == BLACK) && (aux->left->color == BLACK)) { - _set_color(aux, RED); - node = node->parent; + Element *node = _data._nil; + Element *sibling = p_node; + Element *parent = sibling->parent; + + while (node != root) { // If red node found, will exit at a break + if (sibling->color == RED) { + _set_color(sibling, BLACK); + _set_color(parent, RED); + if (sibling == parent->right) { + sibling = sibling->left; + _rotate_left(parent); } else { - if (aux->right->color == BLACK) { - _set_color(aux->left, BLACK); - _set_color(aux, RED); - _rotate_right(aux); - aux = node->parent->right; - } - _set_color(aux, node->parent->color); - _set_color(node->parent, BLACK); - _set_color(aux->right, BLACK); - _rotate_left(node->parent); - node = root; /* this is to exit while loop */ + sibling = sibling->right; + _rotate_right(parent); } - } else { /* the code below is has left and right switched from above */ - Element *aux = node->parent->left; - if (aux->color == RED) { - _set_color(aux, BLACK); - _set_color(node->parent, RED); - _rotate_right(node->parent); - aux = node->parent->left; + } + if ((sibling->left->color == BLACK) && (sibling->right->color == BLACK)) { + _set_color(sibling, RED); + if (parent->color == RED) { + _set_color(parent, BLACK); + break; + } else { // loop: haven't found any red nodes yet + node = parent; + parent = node->parent; + sibling = (node == parent->left) ? parent->right : parent->left; } - if ((aux->right->color == BLACK) && (aux->left->color == BLACK)) { - _set_color(aux, RED); - node = node->parent; + } else { + if (sibling == parent->right) { + if (sibling->right->color == BLACK) { + _set_color(sibling->left, BLACK); + _set_color(sibling, RED); + _rotate_right(sibling); + sibling = sibling->parent; + } + _set_color(sibling, parent->color); + _set_color(parent, BLACK); + _set_color(sibling->right, BLACK); + _rotate_left(parent); + break; } else { - if (aux->left->color == BLACK) { - _set_color(aux->right, BLACK); - _set_color(aux, RED); - _rotate_left(aux); - aux = node->parent->left; + if (sibling->left->color == BLACK) { + _set_color(sibling->right, BLACK); + _set_color(sibling, RED); + _rotate_left(sibling); + sibling = sibling->parent; } - _set_color(aux, node->parent->color); - _set_color(node->parent, BLACK); - _set_color(aux->left, BLACK); - _rotate_right(node->parent); - node = root; + + _set_color(sibling, parent->color); + _set_color(parent, BLACK); + _set_color(sibling->left, BLACK); + _rotate_right(parent); + break; } } } - _set_color(node, BLACK); - ERR_FAIL_COND(_data._nil->color != BLACK); } void _erase(Element *p_node) { - Element *rp = ((p_node->left == _data._nil) || (p_node->right == _data._nil)) ? p_node : _successor(p_node); - if (!rp) - rp = _data._nil; + Element *rp = ((p_node->left == _data._nil) || (p_node->right == _data._nil)) ? p_node : p_node->_next; Element *node = (rp->left == _data._nil) ? rp->right : rp->left; node->parent = rp->parent; - if (_data._root == node->parent) { - _data._root->left = node; + Element *sibling; + if (rp == rp->parent->left) { + rp->parent->left = node; + sibling = rp->parent->right; } else { - if (rp == rp->parent->left) { - rp->parent->left = node; - } else { - rp->parent->right = node; - } + rp->parent->right = node; + sibling = rp->parent->left; + } + + if (node->color == RED) { + node->parent = rp->parent; + _set_color(node, BLACK); + } else if (rp->color == BLACK && rp->parent != _data._root) { + _erase_fix(sibling); } if (rp != p_node) { ERR_FAIL_COND(rp == _data._nil); - if (rp->color == BLACK) - _erase_fix(node); - rp->left = p_node->left; rp->right = p_node->right; rp->parent = p_node->parent; rp->color = p_node->color; - p_node->left->parent = rp; - p_node->right->parent = rp; + if (p_node->left != _data._nil) + p_node->left->parent = rp; + if (p_node->right != _data._nil) + p_node->right->parent = rp; if (p_node == p_node->parent->left) { p_node->parent->left = rp; } else { p_node->parent->right = rp; } - } else { - if (p_node->color == BLACK) - _erase_fix(node); } if (p_node->_next) @@ -529,6 +528,7 @@ public: if (!_data._root) return NULL; + Element *res = _find(p_value); return res; } @@ -549,8 +549,9 @@ public: void erase(Element *p_element) { - if (!_data._root) + if (!_data._root || !p_element) return; + _erase(p_element); if (_data.size_cache == 0 && _data._root) _data._free_root(); @@ -560,9 +561,11 @@ public: if (!_data._root) return false; + Element *e = find(p_value); if (!e) return false; + _erase(e); if (_data.size_cache == 0 && _data._root) _data._free_root(); @@ -573,6 +576,7 @@ public: if (!_data._root) return NULL; + Element *e = _data._root->left; if (e == _data._nil) return NULL; @@ -587,6 +591,7 @@ public: if (!_data._root) return NULL; + Element *e = _data._root->left; if (e == _data._nil) return NULL; @@ -603,10 +608,12 @@ public: } inline int size() const { return _data.size_cache; } + int calculate_depth() const { // used for debug mostly if (!_data._root) return 0; + int max_d = 0; _calculate_depth(_data._root->left, max_d, 0); return max_d; @@ -620,7 +627,6 @@ public: _cleanup_tree(_data._root->left); _data._root->left = _data._nil; _data.size_cache = 0; - _data._nil->parent = _data._nil; _data._free_root(); } @@ -633,6 +639,7 @@ public: _copy_from(p_set); } + _FORCE_INLINE_ Set() { } diff --git a/core/undo_redo.cpp b/core/undo_redo.cpp index 4760047959..27fc73ec63 100644 --- a/core/undo_redo.cpp +++ b/core/undo_redo.cpp @@ -503,6 +503,10 @@ void UndoRedo::_bind_methods() { ClassDB::bind_method(D_METHOD("clear_history"), &UndoRedo::clear_history); ClassDB::bind_method(D_METHOD("get_current_action_name"), &UndoRedo::get_current_action_name); ClassDB::bind_method(D_METHOD("get_version"), &UndoRedo::get_version); + ClassDB::bind_method(D_METHOD("set_max_steps", "max_steps"), &UndoRedo::set_max_steps); + ClassDB::bind_method(D_METHOD("get_max_steps"), &UndoRedo::get_max_steps); + ClassDB::bind_method(D_METHOD("redo"), &UndoRedo::redo); + ClassDB::bind_method(D_METHOD("undo"), &UndoRedo::undo); BIND_ENUM_CONSTANT(MERGE_DISABLE); BIND_ENUM_CONSTANT(MERGE_ENDS); diff --git a/core/variant.cpp b/core/variant.cpp index 10d86152ee..52bdd4e22d 100644 --- a/core/variant.cpp +++ b/core/variant.cpp @@ -903,9 +903,6 @@ bool Variant::is_one() const { void Variant::reference(const Variant &p_variant) { - if (this == &p_variant) - return; - clear(); type = p_variant.type; @@ -924,17 +921,14 @@ void Variant::reference(const Variant &p_variant) { case INT: { _data._int = p_variant._data._int; - } break; case REAL: { _data._real = p_variant._data._real; - } break; case STRING: { memnew_placement(_data._mem, String(*reinterpret_cast<const String *>(p_variant._data._mem))); - } break; // math types @@ -942,33 +936,24 @@ void Variant::reference(const Variant &p_variant) { case VECTOR2: { memnew_placement(_data._mem, Vector2(*reinterpret_cast<const Vector2 *>(p_variant._data._mem))); - } break; case RECT2: { memnew_placement(_data._mem, Rect2(*reinterpret_cast<const Rect2 *>(p_variant._data._mem))); - } break; case TRANSFORM2D: { _data._transform2d = memnew(Transform2D(*p_variant._data._transform2d)); - } break; case VECTOR3: { memnew_placement(_data._mem, Vector3(*reinterpret_cast<const Vector3 *>(p_variant._data._mem))); - } break; case PLANE: { memnew_placement(_data._mem, Plane(*reinterpret_cast<const Plane *>(p_variant._data._mem))); - } break; - /* - case QUAT: { - - } break;*/ case RECT3: { _data._rect3 = memnew(Rect3(*p_variant._data._rect3)); @@ -986,7 +971,6 @@ void Variant::reference(const Variant &p_variant) { case TRANSFORM: { _data._transform = memnew(Transform(*p_variant._data._transform)); - } break; // misc types @@ -1058,6 +1042,7 @@ void Variant::reference(const Variant &p_variant) { default: {} } } + void Variant::zero() { switch (type) { case NIL: break; @@ -1073,6 +1058,7 @@ void Variant::zero() { default: this->clear(); break; } } + void Variant::clear() { switch (type) { @@ -1092,12 +1078,10 @@ void Variant::clear() { case TRANSFORM2D: { memdelete(_data._transform2d); - } break; case RECT3: { memdelete(_data._rect3); - } break; case BASIS: { @@ -1106,14 +1090,12 @@ void Variant::clear() { case TRANSFORM: { memdelete(_data._transform); - } break; // misc types case NODE_PATH: { reinterpret_cast<NodePath *>(_data._mem)->~NodePath(); - } break; case OBJECT: { @@ -1127,48 +1109,39 @@ void Variant::clear() { case DICTIONARY: { reinterpret_cast<Dictionary *>(_data._mem)->~Dictionary(); - } break; case ARRAY: { reinterpret_cast<Array *>(_data._mem)->~Array(); - } break; // arrays case POOL_BYTE_ARRAY: { reinterpret_cast<PoolVector<uint8_t> *>(_data._mem)->~PoolVector<uint8_t>(); - } break; case POOL_INT_ARRAY: { reinterpret_cast<PoolVector<int> *>(_data._mem)->~PoolVector<int>(); - } break; case POOL_REAL_ARRAY: { reinterpret_cast<PoolVector<real_t> *>(_data._mem)->~PoolVector<real_t>(); - } break; case POOL_STRING_ARRAY: { reinterpret_cast<PoolVector<String> *>(_data._mem)->~PoolVector<String>(); - } break; case POOL_VECTOR2_ARRAY: { reinterpret_cast<PoolVector<Vector2> *>(_data._mem)->~PoolVector<Vector2>(); - } break; case POOL_VECTOR3_ARRAY: { reinterpret_cast<PoolVector<Vector3> *>(_data._mem)->~PoolVector<Vector3>(); - } break; case POOL_COLOR_ARRAY: { reinterpret_cast<PoolVector<Color> *>(_data._mem)->~PoolVector<Color>(); - } break; default: {} /* not needed */ } @@ -2496,7 +2469,135 @@ Variant::Variant(const Vector<Color> &p_array) { void Variant::operator=(const Variant &p_variant) { - reference(p_variant); + if (this == &p_variant) + return; + + if (type != p_variant.type) { + reference(p_variant); + return; + } + + switch (p_variant.type) { + case NIL: { + + // none + } break; + + // atomic types + case BOOL: { + + _data._bool = p_variant._data._bool; + } break; + case INT: { + + _data._int = p_variant._data._int; + } break; + case REAL: { + + _data._real = p_variant._data._real; + } break; + case STRING: { + + *reinterpret_cast<String *>(_data._mem) = *reinterpret_cast<const String *>(p_variant._data._mem); + } break; + + // math types + + case VECTOR2: { + + *reinterpret_cast<Vector2 *>(_data._mem) = *reinterpret_cast<const Vector2 *>(p_variant._data._mem); + } break; + case RECT2: { + + *reinterpret_cast<Rect2 *>(_data._mem) = *reinterpret_cast<const Rect2 *>(p_variant._data._mem); + } break; + case TRANSFORM2D: { + + *_data._transform2d = *(p_variant._data._transform2d); + } break; + case VECTOR3: { + + *reinterpret_cast<Vector3 *>(_data._mem) = *reinterpret_cast<const Vector3 *>(p_variant._data._mem); + } break; + case PLANE: { + + *reinterpret_cast<Plane *>(_data._mem) = *reinterpret_cast<const Plane *>(p_variant._data._mem); + } break; + + case RECT3: { + + *_data._rect3 = *(p_variant._data._rect3); + } break; + case QUAT: { + + *reinterpret_cast<Quat *>(_data._mem) = *reinterpret_cast<const Quat *>(p_variant._data._mem); + } break; + case BASIS: { + + *_data._basis = *(p_variant._data._basis); + } break; + case TRANSFORM: { + + *_data._transform = *(p_variant._data._transform); + } break; + + // misc types + case COLOR: { + + *reinterpret_cast<Color *>(_data._mem) = *reinterpret_cast<const Color *>(p_variant._data._mem); + } break; + case _RID: { + + *reinterpret_cast<RID *>(_data._mem) = *reinterpret_cast<const RID *>(p_variant._data._mem); + } break; + case OBJECT: { + + *reinterpret_cast<ObjData *>(_data._mem) = p_variant._get_obj(); + } break; + case NODE_PATH: { + + *reinterpret_cast<NodePath *>(_data._mem) = *reinterpret_cast<const NodePath *>(p_variant._data._mem); + } break; + case DICTIONARY: { + + *reinterpret_cast<Dictionary *>(_data._mem) = *reinterpret_cast<const Dictionary *>(p_variant._data._mem); + } break; + case ARRAY: { + + *reinterpret_cast<Array *>(_data._mem) = *reinterpret_cast<const Array *>(p_variant._data._mem); + } break; + + // arrays + case POOL_BYTE_ARRAY: { + + *reinterpret_cast<PoolVector<uint8_t> *>(_data._mem) = *reinterpret_cast<const PoolVector<uint8_t> *>(p_variant._data._mem); + } break; + case POOL_INT_ARRAY: { + + *reinterpret_cast<PoolVector<int> *>(_data._mem) = *reinterpret_cast<const PoolVector<int> *>(p_variant._data._mem); + } break; + case POOL_REAL_ARRAY: { + + *reinterpret_cast<PoolVector<real_t> *>(_data._mem) = *reinterpret_cast<const PoolVector<real_t> *>(p_variant._data._mem); + } break; + case POOL_STRING_ARRAY: { + + *reinterpret_cast<PoolVector<String> *>(_data._mem) = *reinterpret_cast<const PoolVector<String> *>(p_variant._data._mem); + } break; + case POOL_VECTOR2_ARRAY: { + + *reinterpret_cast<PoolVector<Vector2> *>(_data._mem) = *reinterpret_cast<const PoolVector<Vector2> *>(p_variant._data._mem); + } break; + case POOL_VECTOR3_ARRAY: { + + *reinterpret_cast<PoolVector<Vector3> *>(_data._mem) = *reinterpret_cast<const PoolVector<Vector3> *>(p_variant._data._mem); + } break; + case POOL_COLOR_ARRAY: { + + *reinterpret_cast<PoolVector<Color> *>(_data._mem) = *reinterpret_cast<const PoolVector<Color> *>(p_variant._data._mem); + } break; + default: {} + } } Variant::Variant(const IP_Address &p_address) { diff --git a/core/variant.h b/core/variant.h index e77e2e93c4..5ea540a63f 100644 --- a/core/variant.h +++ b/core/variant.h @@ -70,6 +70,7 @@ typedef PoolVector<Color> PoolColorArray; class Variant { public: + // If this changes the table in variant_op must be updated enum Type { NIL, @@ -288,6 +289,7 @@ public: Variant(const IP_Address &p_address); + // If this changes the table in variant_op must be updated enum Operator { //comparation @@ -299,7 +301,7 @@ public: OP_GREATER_EQUAL, //mathematic OP_ADD, - OP_SUBSTRACT, + OP_SUBTRACT, OP_MULTIPLY, OP_DIVIDE, OP_NEGATE, @@ -388,7 +390,7 @@ public: uint32_t hash() const; bool hash_compare(const Variant &p_variant) const; - bool booleanize(bool &valid) const; + bool booleanize() const; void static_assign(const Variant &p_variant); static void get_constructor_list(Variant::Type p_type, List<MethodInfo> *p_list); diff --git a/core/variant_call.cpp b/core/variant_call.cpp index 19d9b0297f..7205280938 100644 --- a/core/variant_call.cpp +++ b/core/variant_call.cpp @@ -1394,7 +1394,7 @@ void register_variant_methods() { ADDFUNC2(STRING, STRING, String, format, NIL, "values", STRING, "placeholder", varray("{_}")); ADDFUNC2(STRING, STRING, String, replace, STRING, "what", STRING, "forwhat", varray()); ADDFUNC2(STRING, STRING, String, replacen, STRING, "what", STRING, "forwhat", varray()); - ADDFUNC2(STRING, STRING, String, insert, INT, "pos", STRING, "what", varray()); + ADDFUNC2(STRING, STRING, String, insert, INT, "position", STRING, "what", varray()); ADDFUNC0(STRING, STRING, String, capitalize, varray()); ADDFUNC2(STRING, POOL_STRING_ARRAY, String, split, STRING, "divisor", BOOL, "allow_empty", varray(true)); ADDFUNC2(STRING, POOL_REAL_ARRAY, String, split_floats, STRING, "divisor", BOOL, "allow_empty", varray(true)); @@ -1402,14 +1402,14 @@ void register_variant_methods() { ADDFUNC0(STRING, STRING, String, to_upper, varray()); ADDFUNC0(STRING, STRING, String, to_lower, varray()); - ADDFUNC1(STRING, STRING, String, left, INT, "pos", varray()); - ADDFUNC1(STRING, STRING, String, right, INT, "pos", varray()); + ADDFUNC1(STRING, STRING, String, left, INT, "position", varray()); + ADDFUNC1(STRING, STRING, String, right, INT, "position", varray()); ADDFUNC2(STRING, STRING, String, strip_edges, BOOL, "left", BOOL, "right", varray(true, true)); ADDFUNC0(STRING, STRING, String, get_extension, varray()); ADDFUNC0(STRING, STRING, String, get_basename, varray()); ADDFUNC1(STRING, STRING, String, plus_file, STRING, "file", varray()); ADDFUNC1(STRING, INT, String, ord_at, INT, "at", varray()); - ADDFUNC2(STRING, NIL, String, erase, INT, "pos", INT, "chars", varray()); + ADDFUNC2(STRING, NIL, String, erase, INT, "position", INT, "chars", varray()); ADDFUNC0(STRING, INT, String, hash, varray()); ADDFUNC0(STRING, STRING, String, md5_text, varray()); ADDFUNC0(STRING, STRING, String, sha256_text, varray()); @@ -1560,9 +1560,9 @@ void register_variant_methods() { ADDFUNC1(ARRAY, NIL, Array, push_back, NIL, "value", varray()); ADDFUNC1(ARRAY, NIL, Array, push_front, NIL, "value", varray()); ADDFUNC1(ARRAY, NIL, Array, append, NIL, "value", varray()); - ADDFUNC1(ARRAY, NIL, Array, resize, INT, "pos", varray()); - ADDFUNC2(ARRAY, NIL, Array, insert, INT, "pos", NIL, "value", varray()); - ADDFUNC1(ARRAY, NIL, Array, remove, INT, "pos", varray()); + ADDFUNC1(ARRAY, NIL, Array, resize, INT, "size", varray()); + ADDFUNC2(ARRAY, NIL, Array, insert, INT, "position", NIL, "value", varray()); + ADDFUNC1(ARRAY, NIL, Array, remove, INT, "position", varray()); ADDFUNC1(ARRAY, NIL, Array, erase, NIL, "value", varray()); ADDFUNC0(ARRAY, NIL, Array, front, varray()); ADDFUNC0(ARRAY, NIL, Array, back, varray()); @@ -1728,10 +1728,10 @@ void register_variant_methods() { _VariantCall::add_constructor(_VariantCall::Vector2_init1, Variant::VECTOR2, "x", Variant::REAL, "y", Variant::REAL); - _VariantCall::add_constructor(_VariantCall::Rect2_init1, Variant::RECT2, "pos", Variant::VECTOR2, "size", Variant::VECTOR2); + _VariantCall::add_constructor(_VariantCall::Rect2_init1, Variant::RECT2, "position", Variant::VECTOR2, "size", Variant::VECTOR2); _VariantCall::add_constructor(_VariantCall::Rect2_init2, Variant::RECT2, "x", Variant::REAL, "y", Variant::REAL, "width", Variant::REAL, "height", Variant::REAL); - _VariantCall::add_constructor(_VariantCall::Transform2D_init2, Variant::TRANSFORM2D, "rot", Variant::REAL, "pos", Variant::VECTOR2); + _VariantCall::add_constructor(_VariantCall::Transform2D_init2, Variant::TRANSFORM2D, "rotation", Variant::REAL, "position", Variant::VECTOR2); _VariantCall::add_constructor(_VariantCall::Transform2D_init3, Variant::TRANSFORM2D, "x_axis", Variant::VECTOR2, "y_axis", Variant::VECTOR2, "origin", Variant::VECTOR2); _VariantCall::add_constructor(_VariantCall::Vector3_init1, Variant::VECTOR3, "x", Variant::REAL, "y", Variant::REAL, "z", Variant::REAL); @@ -1746,7 +1746,7 @@ void register_variant_methods() { _VariantCall::add_constructor(_VariantCall::Color_init1, Variant::COLOR, "r", Variant::REAL, "g", Variant::REAL, "b", Variant::REAL, "a", Variant::REAL); _VariantCall::add_constructor(_VariantCall::Color_init2, Variant::COLOR, "r", Variant::REAL, "g", Variant::REAL, "b", Variant::REAL); - _VariantCall::add_constructor(_VariantCall::Rect3_init1, Variant::RECT3, "pos", Variant::VECTOR3, "size", Variant::VECTOR3); + _VariantCall::add_constructor(_VariantCall::Rect3_init1, Variant::RECT3, "position", Variant::VECTOR3, "size", Variant::VECTOR3); _VariantCall::add_constructor(_VariantCall::Basis_init1, Variant::BASIS, "x_axis", Variant::VECTOR3, "y_axis", Variant::VECTOR3, "z_axis", Variant::VECTOR3); _VariantCall::add_constructor(_VariantCall::Basis_init2, Variant::BASIS, "axis", Variant::VECTOR3, "phi", Variant::REAL); diff --git a/core/variant_op.cpp b/core/variant_op.cpp index b6e114b853..1c5a4ce8d0 100644 --- a/core/variant_op.cpp +++ b/core/variant_op.cpp @@ -33,49 +33,123 @@ #include "object.h" #include "script_language.h" -Variant::operator bool() const { - - bool b; - return booleanize(b); +#define CASE_TYPE_ALL(PREFIX, OP) \ + CASE_TYPE(PREFIX, OP, INT) \ + CASE_TYPE_ALL_BUT_INT(PREFIX, OP) + +#define CASE_TYPE_ALL_BUT_INT(PREFIX, OP) \ + CASE_TYPE(PREFIX, OP, NIL) \ + CASE_TYPE(PREFIX, OP, BOOL) \ + CASE_TYPE(PREFIX, OP, REAL) \ + CASE_TYPE(PREFIX, OP, STRING) \ + CASE_TYPE(PREFIX, OP, VECTOR2) \ + CASE_TYPE(PREFIX, OP, RECT2) \ + CASE_TYPE(PREFIX, OP, VECTOR3) \ + CASE_TYPE(PREFIX, OP, TRANSFORM2D) \ + CASE_TYPE(PREFIX, OP, PLANE) \ + CASE_TYPE(PREFIX, OP, QUAT) \ + CASE_TYPE(PREFIX, OP, RECT3) \ + CASE_TYPE(PREFIX, OP, BASIS) \ + CASE_TYPE(PREFIX, OP, TRANSFORM) \ + CASE_TYPE(PREFIX, OP, COLOR) \ + CASE_TYPE(PREFIX, OP, NODE_PATH) \ + CASE_TYPE(PREFIX, OP, _RID) \ + CASE_TYPE(PREFIX, OP, OBJECT) \ + CASE_TYPE(PREFIX, OP, DICTIONARY) \ + CASE_TYPE(PREFIX, OP, ARRAY) \ + CASE_TYPE(PREFIX, OP, POOL_BYTE_ARRAY) \ + CASE_TYPE(PREFIX, OP, POOL_INT_ARRAY) \ + CASE_TYPE(PREFIX, OP, POOL_REAL_ARRAY) \ + CASE_TYPE(PREFIX, OP, POOL_STRING_ARRAY) \ + CASE_TYPE(PREFIX, OP, POOL_VECTOR2_ARRAY) \ + CASE_TYPE(PREFIX, OP, POOL_VECTOR3_ARRAY) \ + CASE_TYPE(PREFIX, OP, POOL_COLOR_ARRAY) + +#ifdef __GNUC__ +#define TYPE(PREFIX, OP, TYPE) &&PREFIX##_##OP##_##TYPE + +/* clang-format off */ +#define TYPES(PREFIX, OP) { \ + TYPE(PREFIX, OP, NIL), \ + TYPE(PREFIX, OP, BOOL), \ + TYPE(PREFIX, OP, INT), \ + TYPE(PREFIX, OP, REAL), \ + TYPE(PREFIX, OP, STRING), \ + TYPE(PREFIX, OP, VECTOR2), \ + TYPE(PREFIX, OP, RECT2), \ + TYPE(PREFIX, OP, VECTOR3), \ + TYPE(PREFIX, OP, TRANSFORM2D), \ + TYPE(PREFIX, OP, PLANE), \ + TYPE(PREFIX, OP, QUAT), \ + TYPE(PREFIX, OP, RECT3), \ + TYPE(PREFIX, OP, BASIS), \ + TYPE(PREFIX, OP, TRANSFORM), \ + TYPE(PREFIX, OP, COLOR), \ + TYPE(PREFIX, OP, NODE_PATH), \ + TYPE(PREFIX, OP, _RID), \ + TYPE(PREFIX, OP, OBJECT), \ + TYPE(PREFIX, OP, DICTIONARY), \ + TYPE(PREFIX, OP, ARRAY), \ + TYPE(PREFIX, OP, POOL_BYTE_ARRAY), \ + TYPE(PREFIX, OP, POOL_INT_ARRAY), \ + TYPE(PREFIX, OP, POOL_REAL_ARRAY), \ + TYPE(PREFIX, OP, POOL_STRING_ARRAY), \ + TYPE(PREFIX, OP, POOL_VECTOR2_ARRAY), \ + TYPE(PREFIX, OP, POOL_VECTOR3_ARRAY), \ + TYPE(PREFIX, OP, POOL_COLOR_ARRAY), \ +} +/* clang-format on */ + +#define CASES(PREFIX) static void *switch_table_##PREFIX[25][27] = { \ + TYPES(PREFIX, OP_EQUAL), \ + TYPES(PREFIX, OP_NOT_EQUAL), \ + TYPES(PREFIX, OP_LESS), \ + TYPES(PREFIX, OP_LESS_EQUAL), \ + TYPES(PREFIX, OP_GREATER), \ + TYPES(PREFIX, OP_GREATER_EQUAL), \ + TYPES(PREFIX, OP_ADD), \ + TYPES(PREFIX, OP_SUBTRACT), \ + TYPES(PREFIX, OP_MULTIPLY), \ + TYPES(PREFIX, OP_DIVIDE), \ + TYPES(PREFIX, OP_NEGATE), \ + TYPES(PREFIX, OP_POSITIVE), \ + TYPES(PREFIX, OP_MODULE), \ + TYPES(PREFIX, OP_STRING_CONCAT), \ + TYPES(PREFIX, OP_SHIFT_LEFT), \ + TYPES(PREFIX, OP_SHIFT_RIGHT), \ + TYPES(PREFIX, OP_BIT_AND), \ + TYPES(PREFIX, OP_BIT_OR), \ + TYPES(PREFIX, OP_BIT_XOR), \ + TYPES(PREFIX, OP_BIT_NEGATE), \ + TYPES(PREFIX, OP_AND), \ + TYPES(PREFIX, OP_OR), \ + TYPES(PREFIX, OP_XOR), \ + TYPES(PREFIX, OP_NOT), \ + TYPES(PREFIX, OP_IN), \ } -bool Variant::booleanize(bool &r_valid) const { +#define SWITCH(PREFIX, op, val) goto *switch_table_##PREFIX[op][val]; +#define SWITCH_OP(PREFIX, OP, val) +#define CASE_TYPE(PREFIX, OP, TYPE) PREFIX##_##OP##_##TYPE: + +#else +#define CASES(PREFIX) +#define SWITCH(PREFIX, op, val) switch (op) +#define SWITCH_OP(PREFIX, OP, val) \ + case OP: \ + switch (val) +#define CASE_TYPE(PREFIX, OP, TYPE) case TYPE: +#endif - r_valid = true; - switch (type) { - case NIL: return false; - case BOOL: return _data._bool; - case INT: return _data._int; - case REAL: return _data._real; - case STRING: return (*reinterpret_cast<const String *>(_data._mem)) != ""; - case VECTOR2: - case RECT2: - case TRANSFORM2D: - case VECTOR3: - case PLANE: - case RECT3: - case QUAT: - case BASIS: - case TRANSFORM: - case COLOR: - case _RID: return (*reinterpret_cast<const RID *>(_data._mem)).is_valid(); - case OBJECT: return _get_obj().obj; - case NODE_PATH: return (*reinterpret_cast<const NodePath *>(_data._mem)) != NodePath(); - case DICTIONARY: - case ARRAY: - case POOL_BYTE_ARRAY: - case POOL_INT_ARRAY: - case POOL_REAL_ARRAY: - case POOL_STRING_ARRAY: - case POOL_VECTOR2_ARRAY: - case POOL_VECTOR3_ARRAY: - case POOL_COLOR_ARRAY: - r_valid = false; - return false; - default: {} - } +Variant::operator bool() const { - return false; + return booleanize(); +} + +// We consider all unitialized or empty types to be false based on the type's +// zeroiness. +bool Variant::booleanize() const { + return !is_zero(); } #define _RETURN(m_what) \ @@ -84,146 +158,221 @@ bool Variant::booleanize(bool &r_valid) const { return; \ } -#define DEFAULT_OP_NUM(m_op, m_name, m_type) \ - case m_name: { \ - switch (p_b.type) { \ - case BOOL: _RETURN(p_a._data.m_type m_op p_b._data._bool); \ - case INT: _RETURN(p_a._data.m_type m_op p_b._data._int); \ - case REAL: _RETURN(p_a._data.m_type m_op p_b._data._real); \ - default: {} \ - } \ - r_valid = false; \ - return; \ +#define _RETURN_FAIL \ + { \ + r_valid = false; \ + return; \ + } + +#define DEFAULT_OP_NUM(m_prefix, m_op_name, m_name, m_op, m_type) \ + CASE_TYPE(m_prefix, m_op_name, m_name) { \ + if (p_b.type == INT) _RETURN(p_a._data.m_type m_op p_b._data._int); \ + if (p_b.type == REAL) _RETURN(p_a._data.m_type m_op p_b._data._real); \ + \ + _RETURN_FAIL \ }; -#define DEFAULT_OP_NUM_NEG(m_name, m_type) \ - case m_name: { \ - \ - _RETURN(-p_a._data.m_type); \ +#define DEFAULT_OP_NUM_NULL(m_prefix, m_op_name, m_name, m_op, m_type) \ + CASE_TYPE(m_prefix, m_op_name, m_name) { \ + if (p_b.type == INT) _RETURN(p_a._data.m_type m_op p_b._data._int); \ + if (p_b.type == REAL) _RETURN(p_a._data.m_type m_op p_b._data._real); \ + if (p_b.type == NIL) _RETURN(!p_b.type m_op NIL); \ + \ + _RETURN_FAIL \ }; -#define DEFAULT_OP_NUM_POS(m_name, m_type) \ - case m_name: { \ - \ - _RETURN(p_a._data.m_type); \ +#ifdef DEBUG_ENABLED +#define DEFAULT_OP_NUM_DIV(m_prefix, m_op_name, m_name, m_type) \ + CASE_TYPE(m_prefix, m_op_name, m_name) { \ + if (p_b.type == INT) { \ + if (p_b._data._int == 0) { \ + r_valid = false; \ + _RETURN("Division By Zero"); \ + } \ + _RETURN(p_a._data.m_type / p_b._data._int); \ + } \ + if (p_b.type == REAL) { \ + if (p_b._data._real == 0) { \ + r_valid = false; \ + _RETURN("Division By Zero"); \ + } \ + _RETURN(p_a._data.m_type / p_b._data._real); \ + } \ + \ + _RETURN_FAIL \ + }; +#else +#define DEFAULT_OP_NUM_DIV(m_prefix, m_op_name, m_name, m_type) \ + CASE_TYPE(m_prefix, m_op_name, m_name) { \ + if (p_b.type == INT) _RETURN(p_a._data.m_type / p_b._data._int); \ + if (p_b.type == REAL) _RETURN(p_a._data.m_type / p_b._data._real); \ + \ + _RETURN_FAIL \ }; +#endif -#define DEFAULT_OP_NUM_VEC(m_op, m_name, m_type) \ - case m_name: { \ - switch (p_b.type) { \ - case BOOL: _RETURN(p_a._data.m_type m_op p_b._data._bool); \ - case INT: _RETURN(p_a._data.m_type m_op p_b._data._int); \ - case REAL: _RETURN(p_a._data.m_type m_op p_b._data._real); \ - case VECTOR2: _RETURN(p_a._data.m_type m_op *reinterpret_cast<const Vector2 *>(p_b._data._mem)); \ - case VECTOR3: _RETURN(p_a._data.m_type m_op *reinterpret_cast<const Vector3 *>(p_b._data._mem)); \ - default: {} \ - } \ - r_valid = false; \ - return; \ +#define DEFAULT_OP_NUM_NEG(m_prefix, m_op_name, m_name, m_type) \ + CASE_TYPE(m_prefix, m_op_name, m_name) { \ + _RETURN(-p_a._data.m_type); \ }; -#define DEFAULT_OP_STR(m_op, m_name, m_type) \ - case m_name: { \ - switch (p_b.type) { \ - case STRING: _RETURN(*reinterpret_cast<const m_type *>(p_a._data._mem) m_op *reinterpret_cast<const String *>(p_b._data._mem)); \ - case NODE_PATH: _RETURN(*reinterpret_cast<const m_type *>(p_a._data._mem) m_op *reinterpret_cast<const NodePath *>(p_b._data._mem)); \ - default: {} \ - } \ - r_valid = false; \ - return; \ +#define DEFAULT_OP_NUM_POS(m_prefix, m_op_name, m_name, m_type) \ + CASE_TYPE(m_prefix, m_op_name, m_name) { \ + _RETURN(p_a._data.m_type); \ }; -#define DEFAULT_OP_LOCALMEM(m_op, m_name, m_type) \ - case m_name: { \ - switch (p_b.type) { \ - case m_name: _RETURN(*reinterpret_cast<const m_type *>(p_a._data._mem) m_op *reinterpret_cast<const m_type *>(p_b._data._mem)); \ - default: {} \ - } \ - r_valid = false; \ - return; \ - } +#define DEFAULT_OP_NUM_VEC(m_prefix, m_op_name, m_name, m_op, m_type) \ + CASE_TYPE(m_prefix, m_op_name, m_name) { \ + if (p_b.type == INT) _RETURN(p_a._data.m_type m_op p_b._data._int); \ + if (p_b.type == REAL) _RETURN(p_a._data.m_type m_op p_b._data._real); \ + if (p_b.type == VECTOR2) _RETURN(p_a._data.m_type m_op *reinterpret_cast<const Vector2 *>(p_b._data._mem)); \ + if (p_b.type == VECTOR3) _RETURN(p_a._data.m_type m_op *reinterpret_cast<const Vector3 *>(p_b._data._mem)); \ + \ + _RETURN_FAIL \ + }; -#define DEFAULT_OP_LOCALMEM_NEG(m_name, m_type) \ - case m_name: { \ +#define DEFAULT_OP_STR_REV(m_prefix, m_op_name, m_name, m_op, m_type) \ + CASE_TYPE(m_prefix, m_op_name, m_name) { \ + if (p_b.type == STRING) _RETURN(*reinterpret_cast<const m_type *>(p_b._data._mem) m_op *reinterpret_cast<const String *>(p_a._data._mem)); \ + if (p_b.type == NODE_PATH) _RETURN(*reinterpret_cast<const m_type *>(p_b._data._mem) m_op *reinterpret_cast<const NodePath *>(p_a._data._mem)); \ + \ + _RETURN_FAIL \ + }; + +#define DEFAULT_OP_STR(m_prefix, m_op_name, m_name, m_op, m_type) \ + CASE_TYPE(m_prefix, m_op_name, m_name) { \ + if (p_b.type == STRING) _RETURN(*reinterpret_cast<const m_type *>(p_a._data._mem) m_op *reinterpret_cast<const String *>(p_b._data._mem)); \ + if (p_b.type == NODE_PATH) _RETURN(*reinterpret_cast<const m_type *>(p_a._data._mem) m_op *reinterpret_cast<const NodePath *>(p_b._data._mem)); \ + \ + _RETURN_FAIL \ + }; + +#define DEFAULT_OP_STR_NULL(m_prefix, m_op_name, m_name, m_op, m_type) \ + CASE_TYPE(m_prefix, m_op_name, m_name) { \ + if (p_b.type == STRING) _RETURN(*reinterpret_cast<const m_type *>(p_a._data._mem) m_op *reinterpret_cast<const String *>(p_b._data._mem)); \ + if (p_b.type == NODE_PATH) _RETURN(*reinterpret_cast<const m_type *>(p_a._data._mem) m_op *reinterpret_cast<const NodePath *>(p_b._data._mem)); \ + if (p_b.type == NIL) _RETURN(!p_b.type m_op NIL); \ + \ + _RETURN_FAIL \ + }; + +#define DEFAULT_OP_LOCALMEM_REV(m_prefix, m_op_name, m_name, m_op, m_type) \ + CASE_TYPE(m_prefix, m_op_name, m_name) { \ + if (p_b.type == m_name) \ + _RETURN(*reinterpret_cast<const m_type *>(p_b._data._mem) m_op *reinterpret_cast<const m_type *>(p_a._data._mem)); \ + \ + _RETURN_FAIL \ + }; + +#define DEFAULT_OP_LOCALMEM(m_prefix, m_op_name, m_name, m_op, m_type) \ + CASE_TYPE(m_prefix, m_op_name, m_name) { \ + if (p_b.type == m_name) \ + _RETURN(*reinterpret_cast<const m_type *>(p_a._data._mem) m_op *reinterpret_cast<const m_type *>(p_b._data._mem)); \ + \ + _RETURN_FAIL \ + }; + +#define DEFAULT_OP_LOCALMEM_NULL(m_prefix, m_op_name, m_name, m_op, m_type) \ + CASE_TYPE(m_prefix, m_op_name, m_name) { \ + if (p_b.type == m_name) \ + _RETURN(*reinterpret_cast<const m_type *>(p_a._data._mem) m_op *reinterpret_cast<const m_type *>(p_b._data._mem)); \ + if (p_b.type == NIL) \ + _RETURN(!p_b.type m_op NIL); \ + \ + _RETURN_FAIL \ + }; + +#define DEFAULT_OP_LOCALMEM_NEG(m_prefix, m_op_name, m_name, m_type) \ + CASE_TYPE(m_prefix, m_op_name, m_name) { \ _RETURN(-*reinterpret_cast<const m_type *>(p_a._data._mem)); \ } -#define DEFAULT_OP_LOCALMEM_POS(m_name, m_type) \ - case m_name: { \ - _RETURN(*reinterpret_cast<const m_type *>(p_a._data._mem)); \ +#define DEFAULT_OP_LOCALMEM_POS(m_prefix, m_op_name, m_name, m_type) \ + CASE_TYPE(m_prefix, m_op_name, m_name) { \ + _RETURN(*reinterpret_cast<const m_type *>(p_a._data._mem)); \ } -#define DEFAULT_OP_LOCALMEM_NUM(m_op, m_name, m_type) \ - case m_name: { \ - switch (p_b.type) { \ - case m_name: _RETURN(*reinterpret_cast<const m_type *>(p_a._data._mem) m_op *reinterpret_cast<const m_type *>(p_b._data._mem)); \ - case BOOL: _RETURN(*reinterpret_cast<const m_type *>(p_a._data._mem) m_op p_b._data._bool); \ - case INT: _RETURN(*reinterpret_cast<const m_type *>(p_a._data._mem) m_op p_b._data._int); \ - case REAL: _RETURN(*reinterpret_cast<const m_type *>(p_a._data._mem) m_op p_b._data._real); \ - default: {} \ - } \ - r_valid = false; \ - return; \ +#define DEFAULT_OP_LOCALMEM_NUM(m_prefix, m_op_name, m_name, m_op, m_type) \ + CASE_TYPE(m_prefix, m_op_name, m_name) { \ + if (p_b.type == m_name) _RETURN(*reinterpret_cast<const m_type *>(p_a._data._mem) m_op *reinterpret_cast<const m_type *>(p_b._data._mem)); \ + if (p_b.type == INT) _RETURN(*reinterpret_cast<const m_type *>(p_a._data._mem) m_op p_b._data._int); \ + if (p_b.type == REAL) _RETURN(*reinterpret_cast<const m_type *>(p_a._data._mem) m_op p_b._data._real); \ + \ + _RETURN_FAIL \ } -#define DEFAULT_OP_PTR(m_op, m_name, m_sub) \ - case m_name: { \ - switch (p_b.type) { \ - case m_name: _RETURN(p_a._data.m_sub m_op p_b._data.m_sub); \ - default: {} \ - } \ - r_valid = false; \ - return; \ +#define DEFAULT_OP_PTR(m_op, m_name, m_sub) \ + CASE_TYPE(m_prefix, m_op_name, m_name) { \ + if (p_b.type == m_name) \ + _RETURN(p_a._data.m_sub m_op p_b._data.m_sub); \ + \ + _RETURN_FAIL \ } -#define DEFAULT_OP_PTRREF(m_op, m_name, m_sub) \ - case m_name: { \ - switch (p_b.type) { \ - case m_name: _RETURN(*p_a._data.m_sub m_op *p_b._data.m_sub); \ - default: {} \ - } \ - r_valid = false; \ - return; \ +#define DEFAULT_OP_PTRREF(m_prefix, m_op_name, m_name, m_op, m_sub) \ + CASE_TYPE(m_prefix, m_op_name, m_name) { \ + if (p_b.type == m_name) \ + _RETURN(*p_a._data.m_sub m_op *p_b._data.m_sub); \ + \ + _RETURN_FAIL \ } -#define DEFAULT_OP_ARRAY_EQ(m_name, m_type) \ - DEFAULT_OP_ARRAY_OP(m_name, m_type, !=, !=, true, false, false) +#define DEFAULT_OP_PTRREF_NULL(m_prefix, m_op_name, m_name, m_op, m_sub) \ + CASE_TYPE(m_prefix, m_op_name, m_name) { \ + if (p_b.type == m_name) \ + _RETURN(*p_a._data.m_sub m_op *p_b._data.m_sub); \ + if (p_b.type == NIL) \ + _RETURN(!p_b.type m_op NIL); \ + \ + _RETURN_FAIL \ + } -#define DEFAULT_OP_ARRAY_LT(m_name, m_type) \ - DEFAULT_OP_ARRAY_OP(m_name, m_type, <, !=, false, a_len < array_b.size(), true) +#define DEFAULT_OP_ARRAY_EQ(m_prefix, m_op_name, m_name, m_type) \ + CASE_TYPE(m_prefix, m_op_name, m_name) { \ + if (p_b.type == NIL) \ + _RETURN(false) \ + DEFAULT_OP_ARRAY_OP_BODY(m_prefix, m_op_name, m_name, m_type, !=, !=, true, false, false) \ + } -#define DEFAULT_OP_ARRAY_OP(m_name, m_type, m_opa, m_opb, m_ret_def, m_ret_s, m_ret_f) \ - case m_name: { \ - if (p_a.type != p_b.type) { \ - r_valid = false; \ - return; \ - } \ - const PoolVector<m_type> &array_a = *reinterpret_cast<const PoolVector<m_type> *>(p_a._data._mem); \ - const PoolVector<m_type> &array_b = *reinterpret_cast<const PoolVector<m_type> *>(p_b._data._mem); \ - \ - int a_len = array_a.size(); \ - if (a_len m_opa array_b.size()) { \ - _RETURN(m_ret_s); \ - } else { \ - \ - PoolVector<m_type>::Read ra = array_a.read(); \ - PoolVector<m_type>::Read rb = array_b.read(); \ - \ - for (int i = 0; i < a_len; i++) { \ - if (ra[i] m_opb rb[i]) \ - _RETURN(m_ret_f); \ - } \ - \ - _RETURN(m_ret_def); \ - } \ +#define DEFAULT_OP_ARRAY_LT(m_prefix, m_op_name, m_name, m_type) \ + DEFAULT_OP_ARRAY_OP(m_prefix, m_op_name, m_name, m_type, <, !=, false, a_len < array_b.size(), true) + +#define DEFAULT_OP_ARRAY_GT(m_prefix, m_op_name, m_name, m_type) \ + DEFAULT_OP_ARRAY_OP(m_prefix, m_op_name, m_name, m_type, >, !=, false, a_len < array_b.size(), true) + +#define DEFAULT_OP_ARRAY_OP(m_prefix, m_op_name, m_name, m_type, m_opa, m_opb, m_ret_def, m_ret_s, m_ret_f) \ + CASE_TYPE(m_prefix, m_op_name, m_name) { \ + DEFAULT_OP_ARRAY_OP_BODY(m_prefix, m_op_name, m_name, m_type, m_opa, m_opb, m_ret_def, m_ret_s, m_ret_f) \ } -#define DEFAULT_OP_ARRAY_ADD(m_name, m_type) \ - case m_name: { \ - if (p_a.type != p_b.type) { \ - r_valid = false; \ - _RETURN(NIL); \ - } \ +#define DEFAULT_OP_ARRAY_OP_BODY(m_prefix, m_op_name, m_name, m_type, m_opa, m_opb, m_ret_def, m_ret_s, m_ret_f) \ + if (p_a.type != p_b.type) \ + _RETURN_FAIL \ + \ + const PoolVector<m_type> &array_a = *reinterpret_cast<const PoolVector<m_type> *>(p_a._data._mem); \ + const PoolVector<m_type> &array_b = *reinterpret_cast<const PoolVector<m_type> *>(p_b._data._mem); \ + \ + int a_len = array_a.size(); \ + if (a_len m_opa array_b.size()) { \ + _RETURN(m_ret_s); \ + } else { \ + \ + PoolVector<m_type>::Read ra = array_a.read(); \ + PoolVector<m_type>::Read rb = array_b.read(); \ + \ + for (int i = 0; i < a_len; i++) { \ + if (ra[i] m_opb rb[i]) \ + _RETURN(m_ret_f); \ + } \ + \ + _RETURN(m_ret_def); \ + } + +#define DEFAULT_OP_ARRAY_ADD(m_prefix, m_op_name, m_name, m_type) \ + CASE_TYPE(m_prefix, m_op_name, m_name) { \ + if (p_a.type != p_b.type) \ + _RETURN_FAIL; \ + \ const PoolVector<m_type> &array_a = *reinterpret_cast<const PoolVector<m_type> *>(p_a._data._mem); \ const PoolVector<m_type> &array_b = *reinterpret_cast<const PoolVector<m_type> *>(p_b._data._mem); \ PoolVector<m_type> sum = array_a; \ @@ -231,595 +380,671 @@ bool Variant::booleanize(bool &r_valid) const { _RETURN(sum); \ } -#define DEFAULT_OP_FAIL(m_name) \ - case m_name: { \ - r_valid = false; \ - return; \ - } - -void Variant::evaluate(const Operator &p_op, const Variant &p_a, const Variant &p_b, Variant &r_ret, bool &r_valid) { +void Variant::evaluate(const Operator &p_op, const Variant &p_a, + const Variant &p_b, Variant &r_ret, bool &r_valid) { + CASES(math); r_valid = true; - switch (p_op) { + SWITCH(math, p_op, p_a.type) { + SWITCH_OP(math, OP_EQUAL, p_a.type) { + CASE_TYPE(math, OP_EQUAL, NIL) { + if (p_b.type == NIL) _RETURN(true); + if (p_b.type == OBJECT) + _RETURN(p_b._get_obj().obj == NULL); - case OP_EQUAL: { + _RETURN(false); + } - if ((int(p_a.type) * int(p_b.type)) == 0) { - //null case is an exception, one of both is null - if (p_a.type == p_b.type) //null against null is true - _RETURN(true); - //only against object is allowed - if (p_a.type == Variant::OBJECT) { - _RETURN(p_a._get_obj().obj == NULL); - } else if (p_b.type == Variant::OBJECT) { - _RETURN(p_b._get_obj().obj == NULL); + CASE_TYPE(math, OP_EQUAL, BOOL) { + if (p_b.type != BOOL) { + if (p_b.type == NIL) + _RETURN(false); + _RETURN_FAIL; } - //otherwise, always false - _RETURN(false); + + _RETURN(p_a._data._bool == p_b._data._bool); } - switch (p_a.type) { + CASE_TYPE(math, OP_EQUAL, OBJECT) { + if (p_b.type == OBJECT) + _RETURN((p_a._get_obj().obj == p_b._get_obj().obj)); + if (p_b.type == NIL) + _RETURN(p_a._get_obj().obj == NULL); - case NIL: { + _RETURN_FAIL; + } - _RETURN(p_b.type == NIL || (p_b.type == Variant::OBJECT && !p_b._get_obj().obj)); - } break; + CASE_TYPE(math, OP_EQUAL, DICTIONARY) { + if (p_b.type != DICTIONARY) { + if (p_b.type == NIL) + _RETURN(false); + _RETURN_FAIL; + } - DEFAULT_OP_NUM(==, BOOL, _bool); - DEFAULT_OP_NUM(==, INT, _int); - DEFAULT_OP_NUM(==, REAL, _real); - DEFAULT_OP_STR(==, STRING, String); - DEFAULT_OP_LOCALMEM(==, VECTOR2, Vector2); - DEFAULT_OP_LOCALMEM(==, RECT2, Rect2); - DEFAULT_OP_PTRREF(==, TRANSFORM2D, _transform2d); - DEFAULT_OP_LOCALMEM(==, VECTOR3, Vector3); - DEFAULT_OP_LOCALMEM(==, PLANE, Plane); - DEFAULT_OP_LOCALMEM(==, QUAT, Quat); - DEFAULT_OP_PTRREF(==, RECT3, _rect3); - DEFAULT_OP_PTRREF(==, BASIS, _basis); - DEFAULT_OP_PTRREF(==, TRANSFORM, _transform); + const Dictionary *arr_a = reinterpret_cast<const Dictionary *>(p_a._data._mem); + const Dictionary *arr_b = reinterpret_cast<const Dictionary *>(p_b._data._mem); - DEFAULT_OP_LOCALMEM(==, COLOR, Color); - DEFAULT_OP_STR(==, NODE_PATH, NodePath); - DEFAULT_OP_LOCALMEM(==, _RID, RID); - case OBJECT: { + _RETURN(*arr_a == *arr_b); + } - if (p_b.type == OBJECT) - _RETURN((p_a._get_obj().obj == p_b._get_obj().obj)); + CASE_TYPE(math, OP_EQUAL, ARRAY) { + if (p_b.type != ARRAY) { if (p_b.type == NIL) - _RETURN(!p_a._get_obj().obj); - } break; - - case DICTIONARY: { + _RETURN(false); + _RETURN_FAIL; + } + const Array *arr_a = reinterpret_cast<const Array *>(p_a._data._mem); + const Array *arr_b = reinterpret_cast<const Array *>(p_b._data._mem); - if (p_b.type != DICTIONARY) + int l = arr_a->size(); + if (arr_b->size() != l) + _RETURN(false); + for (int i = 0; i < l; i++) { + if (!((*arr_a)[i] == (*arr_b)[i])) { _RETURN(false); + } + } - const Dictionary *arr_a = reinterpret_cast<const Dictionary *>(p_a._data._mem); - const Dictionary *arr_b = reinterpret_cast<const Dictionary *>(p_b._data._mem); + _RETURN(true); + } + + DEFAULT_OP_NUM_NULL(math, OP_EQUAL, INT, ==, _int); + DEFAULT_OP_NUM_NULL(math, OP_EQUAL, REAL, ==, _real); + DEFAULT_OP_STR_NULL(math, OP_EQUAL, STRING, ==, String); + DEFAULT_OP_LOCALMEM_NULL(math, OP_EQUAL, VECTOR2, ==, Vector2); + DEFAULT_OP_LOCALMEM_NULL(math, OP_EQUAL, RECT2, ==, Rect2); + DEFAULT_OP_PTRREF_NULL(math, OP_EQUAL, TRANSFORM2D, ==, _transform2d); + DEFAULT_OP_LOCALMEM_NULL(math, OP_EQUAL, VECTOR3, ==, Vector3); + DEFAULT_OP_LOCALMEM_NULL(math, OP_EQUAL, PLANE, ==, Plane); + DEFAULT_OP_LOCALMEM_NULL(math, OP_EQUAL, QUAT, ==, Quat); + DEFAULT_OP_PTRREF_NULL(math, OP_EQUAL, RECT3, ==, _rect3); + DEFAULT_OP_PTRREF_NULL(math, OP_EQUAL, BASIS, ==, _basis); + DEFAULT_OP_PTRREF_NULL(math, OP_EQUAL, TRANSFORM, ==, _transform); + DEFAULT_OP_LOCALMEM_NULL(math, OP_EQUAL, COLOR, ==, Color); + DEFAULT_OP_STR_NULL(math, OP_EQUAL, NODE_PATH, ==, NodePath); + DEFAULT_OP_LOCALMEM_NULL(math, OP_EQUAL, _RID, ==, RID); + + DEFAULT_OP_ARRAY_EQ(math, OP_EQUAL, POOL_BYTE_ARRAY, uint8_t); + DEFAULT_OP_ARRAY_EQ(math, OP_EQUAL, POOL_INT_ARRAY, int); + DEFAULT_OP_ARRAY_EQ(math, OP_EQUAL, POOL_REAL_ARRAY, real_t); + DEFAULT_OP_ARRAY_EQ(math, OP_EQUAL, POOL_STRING_ARRAY, String); + DEFAULT_OP_ARRAY_EQ(math, OP_EQUAL, POOL_VECTOR2_ARRAY, Vector2); + DEFAULT_OP_ARRAY_EQ(math, OP_EQUAL, POOL_VECTOR3_ARRAY, Vector3); + DEFAULT_OP_ARRAY_EQ(math, OP_EQUAL, POOL_COLOR_ARRAY, Color); + } - _RETURN(*arr_a == *arr_b); + SWITCH_OP(math, OP_NOT_EQUAL, p_a.type) { + CASE_TYPE(math, OP_NOT_EQUAL, NIL) { + if (p_b.type == NIL) _RETURN(false); + if (p_b.type == OBJECT) + _RETURN(p_b._get_obj().obj != NULL); - } break; - case ARRAY: { + _RETURN(true); + } - if (p_b.type != ARRAY) - _RETURN(false); + CASE_TYPE(math, OP_NOT_EQUAL, BOOL) { + if (p_b.type != BOOL) { + if (p_b.type == NIL) + _RETURN(true); - const Array *arr_a = reinterpret_cast<const Array *>(p_a._data._mem); - const Array *arr_b = reinterpret_cast<const Array *>(p_b._data._mem); + _RETURN_FAIL; + } - int l = arr_a->size(); - if (arr_b->size() != l) - _RETURN(false); - for (int i = 0; i < l; i++) { - if (!((*arr_a)[i] == (*arr_b)[i])) { - _RETURN(false); - } - } + _RETURN(p_a._data._bool != p_b._data._bool); + } - _RETURN(true); + CASE_TYPE(math, OP_NOT_EQUAL, OBJECT) { + if (p_b.type == OBJECT) + _RETURN((p_a._get_obj().obj != p_b._get_obj().obj)); + if (p_b.type == NIL) + _RETURN(p_a._get_obj().obj != NULL); - } break; + _RETURN_FAIL; + } - DEFAULT_OP_ARRAY_EQ(POOL_BYTE_ARRAY, uint8_t); - DEFAULT_OP_ARRAY_EQ(POOL_INT_ARRAY, int); - DEFAULT_OP_ARRAY_EQ(POOL_REAL_ARRAY, real_t); - DEFAULT_OP_ARRAY_EQ(POOL_STRING_ARRAY, String); - DEFAULT_OP_ARRAY_EQ(POOL_VECTOR2_ARRAY, Vector3); - DEFAULT_OP_ARRAY_EQ(POOL_VECTOR3_ARRAY, Vector3); - DEFAULT_OP_ARRAY_EQ(POOL_COLOR_ARRAY, Color); + CASE_TYPE(math, OP_NOT_EQUAL, DICTIONARY) { + if (p_b.type != DICTIONARY) { + if (p_b.type == NIL) + _RETURN(true); + _RETURN_FAIL; + } - case VARIANT_MAX: { - r_valid = false; - return; + const Dictionary *arr_a = reinterpret_cast<const Dictionary *>(p_a._data._mem); + const Dictionary *arr_b = reinterpret_cast<const Dictionary *>(p_b._data._mem); - } break; + _RETURN((*arr_a == *arr_b) == false); } - } break; - case OP_NOT_EQUAL: { - Variant res; - evaluate(OP_EQUAL, p_a, p_b, res, r_valid); - if (!r_valid) - return; - if (res.type == BOOL) - res._data._bool = !res._data._bool; - _RETURN(res); - - } break; - case OP_LESS: { - - switch (p_a.type) { - - DEFAULT_OP_FAIL(NIL); - DEFAULT_OP_NUM(<, BOOL, _bool); - DEFAULT_OP_NUM(<, INT, _int); - DEFAULT_OP_NUM(<, REAL, _real); - DEFAULT_OP_STR(<, STRING, String); - DEFAULT_OP_LOCALMEM(<, VECTOR2, Vector2); - DEFAULT_OP_FAIL(RECT2); - DEFAULT_OP_FAIL(TRANSFORM2D); - DEFAULT_OP_LOCALMEM(<, VECTOR3, Vector3); - DEFAULT_OP_FAIL(PLANE); - DEFAULT_OP_FAIL(QUAT); - DEFAULT_OP_FAIL(RECT3); - DEFAULT_OP_FAIL(BASIS); - DEFAULT_OP_FAIL(TRANSFORM); - - DEFAULT_OP_FAIL(COLOR); - - DEFAULT_OP_FAIL(NODE_PATH); - DEFAULT_OP_LOCALMEM(<, _RID, RID); - case OBJECT: { - - if (p_b.type == OBJECT) - _RETURN((p_a._get_obj().obj < p_b._get_obj().obj)); - } break; - DEFAULT_OP_FAIL(DICTIONARY); - case ARRAY: { - - if (p_b.type != ARRAY) - _RETURN(false); - const Array *arr_a = reinterpret_cast<const Array *>(p_a._data._mem); - const Array *arr_b = reinterpret_cast<const Array *>(p_b._data._mem); + CASE_TYPE(math, OP_NOT_EQUAL, ARRAY) { + if (p_b.type != ARRAY) { + if (p_b.type == NIL) + _RETURN(true); + + _RETURN_FAIL; + } + + const Array *arr_a = reinterpret_cast<const Array *>(p_a._data._mem); + const Array *arr_b = reinterpret_cast<const Array *>(p_b._data._mem); - int l = arr_a->size(); - if (arr_b->size() < l) + int l = arr_a->size(); + if (arr_b->size() != l) + _RETURN(true); + for (int i = 0; i < l; i++) { + if (((*arr_a)[i] == (*arr_b)[i])) { _RETURN(false); - for (int i = 0; i < l; i++) { - if (!((*arr_a)[i] < (*arr_b)[i])) { - _RETURN(true); - } } + } - _RETURN(false); + _RETURN(true); + } + + DEFAULT_OP_NUM_NULL(math, OP_NOT_EQUAL, INT, !=, _int); + DEFAULT_OP_NUM_NULL(math, OP_NOT_EQUAL, REAL, !=, _real); + DEFAULT_OP_STR_NULL(math, OP_NOT_EQUAL, STRING, !=, String); + DEFAULT_OP_LOCALMEM_NULL(math, OP_NOT_EQUAL, VECTOR2, !=, Vector2); + DEFAULT_OP_LOCALMEM_NULL(math, OP_NOT_EQUAL, RECT2, !=, Rect2); + DEFAULT_OP_PTRREF_NULL(math, OP_NOT_EQUAL, TRANSFORM2D, !=, _transform2d); + DEFAULT_OP_LOCALMEM_NULL(math, OP_NOT_EQUAL, VECTOR3, !=, Vector3); + DEFAULT_OP_LOCALMEM_NULL(math, OP_NOT_EQUAL, PLANE, !=, Plane); + DEFAULT_OP_LOCALMEM_NULL(math, OP_NOT_EQUAL, QUAT, !=, Quat); + DEFAULT_OP_PTRREF_NULL(math, OP_NOT_EQUAL, RECT3, !=, _rect3); + DEFAULT_OP_PTRREF_NULL(math, OP_NOT_EQUAL, BASIS, !=, _basis); + DEFAULT_OP_PTRREF_NULL(math, OP_NOT_EQUAL, TRANSFORM, !=, _transform); + DEFAULT_OP_LOCALMEM_NULL(math, OP_NOT_EQUAL, COLOR, !=, Color); + DEFAULT_OP_STR_NULL(math, OP_NOT_EQUAL, NODE_PATH, !=, NodePath); + DEFAULT_OP_LOCALMEM_NULL(math, OP_NOT_EQUAL, _RID, !=, RID); + + CASE_TYPE(math, OP_NOT_EQUAL, POOL_BYTE_ARRAY); + CASE_TYPE(math, OP_NOT_EQUAL, POOL_INT_ARRAY); + CASE_TYPE(math, OP_NOT_EQUAL, POOL_REAL_ARRAY); + CASE_TYPE(math, OP_NOT_EQUAL, POOL_STRING_ARRAY); + CASE_TYPE(math, OP_NOT_EQUAL, POOL_VECTOR2_ARRAY); + CASE_TYPE(math, OP_NOT_EQUAL, POOL_VECTOR3_ARRAY); + CASE_TYPE(math, OP_NOT_EQUAL, POOL_COLOR_ARRAY); + _RETURN_FAIL; + } - } break; - DEFAULT_OP_ARRAY_LT(POOL_BYTE_ARRAY, uint8_t); - DEFAULT_OP_ARRAY_LT(POOL_INT_ARRAY, int); - DEFAULT_OP_ARRAY_LT(POOL_REAL_ARRAY, real_t); - DEFAULT_OP_ARRAY_LT(POOL_STRING_ARRAY, String); - DEFAULT_OP_ARRAY_LT(POOL_VECTOR2_ARRAY, Vector3); - DEFAULT_OP_ARRAY_LT(POOL_VECTOR3_ARRAY, Vector3); - DEFAULT_OP_ARRAY_LT(POOL_COLOR_ARRAY, Color); - case VARIANT_MAX: { - r_valid = false; - return; + SWITCH_OP(math, OP_LESS, p_a.type) { + CASE_TYPE(math, OP_LESS, BOOL) { + if (p_b.type != BOOL) + _RETURN_FAIL; - } break; - } - - } break; - case OP_LESS_EQUAL: { - - switch (p_a.type) { - - DEFAULT_OP_FAIL(NIL); - DEFAULT_OP_NUM(<=, BOOL, _bool); - DEFAULT_OP_NUM(<=, INT, _int); - DEFAULT_OP_NUM(<=, REAL, _real); - DEFAULT_OP_STR(<=, STRING, String); - DEFAULT_OP_LOCALMEM(<=, VECTOR2, Vector2); - DEFAULT_OP_FAIL(RECT2); - DEFAULT_OP_FAIL(TRANSFORM2D); - DEFAULT_OP_LOCALMEM(<=, VECTOR3, Vector3); - DEFAULT_OP_FAIL(PLANE); - DEFAULT_OP_FAIL(QUAT); - DEFAULT_OP_FAIL(RECT3); - DEFAULT_OP_FAIL(BASIS); - DEFAULT_OP_FAIL(TRANSFORM); - - DEFAULT_OP_FAIL(COLOR); - - DEFAULT_OP_FAIL(NODE_PATH); - DEFAULT_OP_LOCALMEM(<=, _RID, RID); - case OBJECT: { - - if (p_b.type == OBJECT) - _RETURN((p_a._get_obj().obj <= p_b._get_obj().obj)); - } break; - DEFAULT_OP_FAIL(DICTIONARY); - DEFAULT_OP_FAIL(ARRAY); - DEFAULT_OP_FAIL(POOL_BYTE_ARRAY); - DEFAULT_OP_FAIL(POOL_INT_ARRAY); - DEFAULT_OP_FAIL(POOL_REAL_ARRAY); - DEFAULT_OP_FAIL(POOL_STRING_ARRAY); - DEFAULT_OP_FAIL(POOL_VECTOR2_ARRAY); - DEFAULT_OP_FAIL(POOL_VECTOR3_ARRAY); - DEFAULT_OP_FAIL(POOL_COLOR_ARRAY); - case VARIANT_MAX: { - r_valid = false; - return; + if (p_a._data._bool == p_b._data._bool) + _RETURN(false); + + if (p_a._data._bool && !p_b._data._bool) + _RETURN(false); - } break; + _RETURN(true); } - } break; - case OP_GREATER: { + CASE_TYPE(math, OP_LESS, OBJECT) { + if (p_b.type != OBJECT) + _RETURN_FAIL; + _RETURN((p_a._get_obj().obj < p_b._get_obj().obj)); + } - Variant res; - evaluate(OP_LESS, p_b, p_a, res, r_valid); - if (!r_valid) - return; - _RETURN(res); + CASE_TYPE(math, OP_LESS, ARRAY) { + if (p_b.type != ARRAY) + _RETURN_FAIL; - } break; - case OP_GREATER_EQUAL: { + const Array *arr_a = reinterpret_cast<const Array *>(p_a._data._mem); + const Array *arr_b = reinterpret_cast<const Array *>(p_b._data._mem); - Variant res; - evaluate(OP_LESS_EQUAL, p_b, p_a, res, r_valid); - if (!r_valid) - return; - _RETURN(res); - } break; - //mathematic - case OP_ADD: { - switch (p_a.type) { - - DEFAULT_OP_FAIL(NIL); - DEFAULT_OP_NUM(+, BOOL, _bool); - DEFAULT_OP_NUM(+, INT, _int); - DEFAULT_OP_NUM(+, REAL, _real); - DEFAULT_OP_STR(+, STRING, String); - DEFAULT_OP_LOCALMEM(+, VECTOR2, Vector2); - DEFAULT_OP_FAIL(RECT2); - DEFAULT_OP_FAIL(TRANSFORM2D); - DEFAULT_OP_LOCALMEM(+, VECTOR3, Vector3); - DEFAULT_OP_FAIL(PLANE); - DEFAULT_OP_LOCALMEM(+, QUAT, Quat); - DEFAULT_OP_FAIL(RECT3); - DEFAULT_OP_FAIL(BASIS); - DEFAULT_OP_FAIL(TRANSFORM); - - DEFAULT_OP_FAIL(COLOR); - - DEFAULT_OP_FAIL(NODE_PATH); - DEFAULT_OP_FAIL(_RID); - DEFAULT_OP_FAIL(OBJECT); - DEFAULT_OP_FAIL(DICTIONARY); - - case ARRAY: { - if (p_a.type != p_b.type) { - r_valid = false; - return; + int l = arr_a->size(); + if (arr_b->size() < l) + _RETURN(false); + for (int i = 0; i < l; i++) { + if (!((*arr_a)[i] < (*arr_b)[i])) { + _RETURN(true); } - const Array &array_a = *reinterpret_cast<const Array *>(p_a._data._mem); - const Array &array_b = *reinterpret_cast<const Array *>(p_b._data._mem); - Array sum; - int asize = array_a.size(); - int bsize = array_b.size(); - sum.resize(asize + bsize); - for (int i = 0; i < asize; i++) - sum[i] = array_a[i]; - for (int i = 0; i < bsize; i++) - sum[i + asize] = array_b[i]; - _RETURN(sum); - } - DEFAULT_OP_ARRAY_ADD(POOL_BYTE_ARRAY, uint8_t); - DEFAULT_OP_ARRAY_ADD(POOL_INT_ARRAY, int); - DEFAULT_OP_ARRAY_ADD(POOL_REAL_ARRAY, real_t); - DEFAULT_OP_ARRAY_ADD(POOL_STRING_ARRAY, String); - DEFAULT_OP_ARRAY_ADD(POOL_VECTOR2_ARRAY, Vector2); - DEFAULT_OP_ARRAY_ADD(POOL_VECTOR3_ARRAY, Vector3); - DEFAULT_OP_ARRAY_ADD(POOL_COLOR_ARRAY, Color); - case VARIANT_MAX: { - r_valid = false; - return; - - } break; - } - } break; - case OP_SUBSTRACT: { - switch (p_a.type) { - - DEFAULT_OP_FAIL(NIL); - DEFAULT_OP_NUM(-, BOOL, _bool); - DEFAULT_OP_NUM(-, INT, _int); - DEFAULT_OP_NUM(-, REAL, _real); - DEFAULT_OP_FAIL(STRING); - DEFAULT_OP_LOCALMEM(-, VECTOR2, Vector2); - DEFAULT_OP_FAIL(RECT2); - DEFAULT_OP_FAIL(TRANSFORM2D); - DEFAULT_OP_LOCALMEM(-, VECTOR3, Vector3); - DEFAULT_OP_FAIL(PLANE); - DEFAULT_OP_LOCALMEM(-, QUAT, Quat); - DEFAULT_OP_FAIL(RECT3); - DEFAULT_OP_FAIL(BASIS); - DEFAULT_OP_FAIL(TRANSFORM); - - DEFAULT_OP_FAIL(COLOR); - - DEFAULT_OP_FAIL(NODE_PATH); - DEFAULT_OP_FAIL(_RID); - DEFAULT_OP_FAIL(OBJECT); - DEFAULT_OP_FAIL(DICTIONARY); - DEFAULT_OP_FAIL(ARRAY); - DEFAULT_OP_FAIL(POOL_BYTE_ARRAY); - DEFAULT_OP_FAIL(POOL_INT_ARRAY); - DEFAULT_OP_FAIL(POOL_REAL_ARRAY); - DEFAULT_OP_FAIL(POOL_STRING_ARRAY); - DEFAULT_OP_FAIL(POOL_VECTOR2_ARRAY); - DEFAULT_OP_FAIL(POOL_VECTOR3_ARRAY); - DEFAULT_OP_FAIL(POOL_COLOR_ARRAY); - case VARIANT_MAX: { - r_valid = false; - return; + } - } break; + _RETURN(false); } - } break; - case OP_MULTIPLY: { - switch (p_a.type) { - DEFAULT_OP_FAIL(NIL); - DEFAULT_OP_NUM(*, BOOL, _bool); - DEFAULT_OP_NUM_VEC(*, INT, _int); - DEFAULT_OP_NUM_VEC(*, REAL, _real); - DEFAULT_OP_FAIL(STRING); - DEFAULT_OP_LOCALMEM_NUM(*, VECTOR2, Vector2); - DEFAULT_OP_FAIL(RECT2); - case TRANSFORM2D: { + DEFAULT_OP_NUM(math, OP_LESS, INT, <, _int); + DEFAULT_OP_NUM(math, OP_LESS, REAL, <, _real); + DEFAULT_OP_STR(math, OP_LESS, STRING, <, String); + DEFAULT_OP_LOCALMEM(math, OP_LESS, VECTOR2, <, Vector2); + DEFAULT_OP_LOCALMEM(math, OP_LESS, VECTOR3, <, Vector3); + DEFAULT_OP_LOCALMEM(math, OP_LESS, _RID, <, RID); + DEFAULT_OP_ARRAY_LT(math, OP_LESS, POOL_BYTE_ARRAY, uint8_t); + DEFAULT_OP_ARRAY_LT(math, OP_LESS, POOL_INT_ARRAY, int); + DEFAULT_OP_ARRAY_LT(math, OP_LESS, POOL_REAL_ARRAY, real_t); + DEFAULT_OP_ARRAY_LT(math, OP_LESS, POOL_STRING_ARRAY, String); + DEFAULT_OP_ARRAY_LT(math, OP_LESS, POOL_VECTOR2_ARRAY, Vector3); + DEFAULT_OP_ARRAY_LT(math, OP_LESS, POOL_VECTOR3_ARRAY, Vector3); + DEFAULT_OP_ARRAY_LT(math, OP_LESS, POOL_COLOR_ARRAY, Color); + + CASE_TYPE(math, OP_LESS, NIL) + CASE_TYPE(math, OP_LESS, RECT2) + CASE_TYPE(math, OP_LESS, TRANSFORM2D) + CASE_TYPE(math, OP_LESS, PLANE) + CASE_TYPE(math, OP_LESS, QUAT) + CASE_TYPE(math, OP_LESS, RECT3) + CASE_TYPE(math, OP_LESS, BASIS) + CASE_TYPE(math, OP_LESS, TRANSFORM) + CASE_TYPE(math, OP_LESS, COLOR) + CASE_TYPE(math, OP_LESS, NODE_PATH) + CASE_TYPE(math, OP_LESS, DICTIONARY) + _RETURN_FAIL; + } - if (p_b.type == TRANSFORM2D) { - _RETURN(*p_a._data._transform2d * *p_b._data._transform2d); - }; - if (p_b.type == VECTOR2) { - _RETURN(p_a._data._transform2d->xform(*(const Vector2 *)p_b._data._mem)); - }; - r_valid = false; - return; - } break; - DEFAULT_OP_LOCALMEM_NUM(*, VECTOR3, Vector3); - DEFAULT_OP_FAIL(PLANE); - case QUAT: { - - switch (p_b.type) { - case VECTOR3: { - - _RETURN(reinterpret_cast<const Quat *>(p_a._data._mem)->xform(*(const Vector3 *)p_b._data._mem)); - } break; - case QUAT: { - - _RETURN(*reinterpret_cast<const Quat *>(p_a._data._mem) * *reinterpret_cast<const Quat *>(p_b._data._mem)); - } break; - case REAL: { - _RETURN(*reinterpret_cast<const Quat *>(p_a._data._mem) * p_b._data._real); - } break; - default: {} - }; - r_valid = false; - return; - } break; - DEFAULT_OP_FAIL(RECT3); - case BASIS: { + SWITCH_OP(math, OP_LESS_EQUAL, p_a.type) { + CASE_TYPE(math, OP_LESS_EQUAL, OBJECT) { + if (p_b.type != OBJECT) + _RETURN_FAIL; + _RETURN((p_a._get_obj().obj <= p_b._get_obj().obj)); + } + + DEFAULT_OP_NUM(math, OP_LESS_EQUAL, INT, <=, _int); + DEFAULT_OP_NUM(math, OP_LESS_EQUAL, REAL, <=, _real); + DEFAULT_OP_STR(math, OP_LESS_EQUAL, STRING, <=, String); + DEFAULT_OP_LOCALMEM(math, OP_LESS_EQUAL, VECTOR2, <=, Vector2); + DEFAULT_OP_LOCALMEM(math, OP_LESS_EQUAL, VECTOR3, <=, Vector3); + DEFAULT_OP_LOCALMEM(math, OP_LESS_EQUAL, _RID, <=, RID); + + CASE_TYPE(math, OP_LESS_EQUAL, NIL) + CASE_TYPE(math, OP_LESS_EQUAL, BOOL) + CASE_TYPE(math, OP_LESS_EQUAL, RECT2) + CASE_TYPE(math, OP_LESS_EQUAL, TRANSFORM2D) + CASE_TYPE(math, OP_LESS_EQUAL, PLANE) + CASE_TYPE(math, OP_LESS_EQUAL, QUAT) + CASE_TYPE(math, OP_LESS_EQUAL, RECT3) + CASE_TYPE(math, OP_LESS_EQUAL, BASIS) + CASE_TYPE(math, OP_LESS_EQUAL, TRANSFORM) + CASE_TYPE(math, OP_LESS_EQUAL, COLOR) + CASE_TYPE(math, OP_LESS_EQUAL, NODE_PATH) + CASE_TYPE(math, OP_LESS_EQUAL, DICTIONARY) + CASE_TYPE(math, OP_LESS_EQUAL, ARRAY) + CASE_TYPE(math, OP_LESS_EQUAL, POOL_BYTE_ARRAY); + CASE_TYPE(math, OP_LESS_EQUAL, POOL_INT_ARRAY); + CASE_TYPE(math, OP_LESS_EQUAL, POOL_REAL_ARRAY); + CASE_TYPE(math, OP_LESS_EQUAL, POOL_STRING_ARRAY); + CASE_TYPE(math, OP_LESS_EQUAL, POOL_VECTOR2_ARRAY); + CASE_TYPE(math, OP_LESS_EQUAL, POOL_VECTOR3_ARRAY); + CASE_TYPE(math, OP_LESS_EQUAL, POOL_COLOR_ARRAY); + _RETURN_FAIL; + } - switch (p_b.type) { - case VECTOR3: { + SWITCH_OP(math, OP_GREATER, p_a.type) { + CASE_TYPE(math, OP_GREATER, BOOL) { + if (p_b.type != BOOL) + _RETURN_FAIL; - _RETURN(p_a._data._basis->xform(*(const Vector3 *)p_b._data._mem)); - }; - case BASIS: { + if (p_a._data._bool == p_b._data._bool) + _RETURN(false); - _RETURN(*p_a._data._basis * *p_b._data._basis); - }; - default: {} - }; - r_valid = false; - return; - } break; - case TRANSFORM: { + if (!p_a._data._bool && p_b._data._bool) + _RETURN(false); - switch (p_b.type) { - case VECTOR3: { + _RETURN(true); + } - _RETURN(p_a._data._transform->xform(*(const Vector3 *)p_b._data._mem)); - }; - case TRANSFORM: { + CASE_TYPE(math, OP_GREATER, OBJECT) { + if (p_b.type != OBJECT) + _RETURN_FAIL; + _RETURN((p_a._get_obj().obj > p_b._get_obj().obj)); + } - _RETURN(*p_a._data._transform * *p_b._data._transform); - }; - default: {} - }; - r_valid = false; - return; - } break; - DEFAULT_OP_FAIL(COLOR); - - DEFAULT_OP_FAIL(NODE_PATH); - DEFAULT_OP_FAIL(_RID); - DEFAULT_OP_FAIL(OBJECT); - DEFAULT_OP_FAIL(DICTIONARY); - DEFAULT_OP_FAIL(ARRAY); - DEFAULT_OP_FAIL(POOL_BYTE_ARRAY); - DEFAULT_OP_FAIL(POOL_INT_ARRAY); - DEFAULT_OP_FAIL(POOL_REAL_ARRAY); - DEFAULT_OP_FAIL(POOL_STRING_ARRAY); - DEFAULT_OP_FAIL(POOL_VECTOR2_ARRAY); - DEFAULT_OP_FAIL(POOL_VECTOR3_ARRAY); - DEFAULT_OP_FAIL(POOL_COLOR_ARRAY); - case VARIANT_MAX: { - r_valid = false; - return; + CASE_TYPE(math, OP_GREATER, ARRAY) { + if (p_b.type != ARRAY) + _RETURN_FAIL; - } break; - } - } break; - case OP_DIVIDE: { - switch (p_a.type) { + const Array *arr_a = reinterpret_cast<const Array *>(p_a._data._mem); + const Array *arr_b = reinterpret_cast<const Array *>(p_b._data._mem); - DEFAULT_OP_FAIL(NIL); - DEFAULT_OP_NUM(/, BOOL, _bool); - case INT: { - switch (p_b.type) { - case BOOL: { - int64_t b = p_b._data._bool; - if (b == 0) { + int l = arr_a->size(); + if (arr_b->size() > l) + _RETURN(false); + for (int i = 0; i < l; i++) { + if (((*arr_a)[i] < (*arr_b)[i])) { + _RETURN(false); + } + } + + _RETURN(true); + } + + DEFAULT_OP_NUM(math, OP_GREATER, INT, >, _int); + DEFAULT_OP_NUM(math, OP_GREATER, REAL, >, _real); + DEFAULT_OP_STR_REV(math, OP_GREATER, STRING, <, String); + DEFAULT_OP_LOCALMEM_REV(math, OP_GREATER, VECTOR2, <, Vector2); + DEFAULT_OP_LOCALMEM_REV(math, OP_GREATER, VECTOR3, <, Vector3); + DEFAULT_OP_LOCALMEM_REV(math, OP_GREATER, _RID, <, RID); + DEFAULT_OP_ARRAY_GT(math, OP_GREATER, POOL_BYTE_ARRAY, uint8_t); + DEFAULT_OP_ARRAY_GT(math, OP_GREATER, POOL_INT_ARRAY, int); + DEFAULT_OP_ARRAY_GT(math, OP_GREATER, POOL_REAL_ARRAY, real_t); + DEFAULT_OP_ARRAY_GT(math, OP_GREATER, POOL_STRING_ARRAY, String); + DEFAULT_OP_ARRAY_GT(math, OP_GREATER, POOL_VECTOR2_ARRAY, Vector3); + DEFAULT_OP_ARRAY_GT(math, OP_GREATER, POOL_VECTOR3_ARRAY, Vector3); + DEFAULT_OP_ARRAY_GT(math, OP_GREATER, POOL_COLOR_ARRAY, Color); + + CASE_TYPE(math, OP_GREATER, NIL) + CASE_TYPE(math, OP_GREATER, RECT2) + CASE_TYPE(math, OP_GREATER, TRANSFORM2D) + CASE_TYPE(math, OP_GREATER, PLANE) + CASE_TYPE(math, OP_GREATER, QUAT) + CASE_TYPE(math, OP_GREATER, RECT3) + CASE_TYPE(math, OP_GREATER, BASIS) + CASE_TYPE(math, OP_GREATER, TRANSFORM) + CASE_TYPE(math, OP_GREATER, COLOR) + CASE_TYPE(math, OP_GREATER, NODE_PATH) + CASE_TYPE(math, OP_GREATER, DICTIONARY) + _RETURN_FAIL; + } - r_valid = false; - _RETURN("Division By False"); - } - _RETURN(p_a._data._int / b); + SWITCH_OP(math, OP_GREATER_EQUAL, p_a.type) { + CASE_TYPE(math, OP_GREATER_EQUAL, OBJECT) { + if (p_b.type != OBJECT) + _RETURN_FAIL; + _RETURN((p_a._get_obj().obj >= p_b._get_obj().obj)); + } + + DEFAULT_OP_NUM(math, OP_GREATER_EQUAL, INT, >=, _int); + DEFAULT_OP_NUM(math, OP_GREATER_EQUAL, REAL, >=, _real); + DEFAULT_OP_STR_REV(math, OP_GREATER_EQUAL, STRING, <=, String); + DEFAULT_OP_LOCALMEM_REV(math, OP_GREATER_EQUAL, VECTOR2, <=, Vector2); + DEFAULT_OP_LOCALMEM_REV(math, OP_GREATER_EQUAL, VECTOR3, <=, Vector3); + DEFAULT_OP_LOCALMEM_REV(math, OP_GREATER_EQUAL, _RID, <=, RID); + + CASE_TYPE(math, OP_GREATER_EQUAL, NIL) + CASE_TYPE(math, OP_GREATER_EQUAL, BOOL) + CASE_TYPE(math, OP_GREATER_EQUAL, RECT2) + CASE_TYPE(math, OP_GREATER_EQUAL, TRANSFORM2D) + CASE_TYPE(math, OP_GREATER_EQUAL, PLANE) + CASE_TYPE(math, OP_GREATER_EQUAL, QUAT) + CASE_TYPE(math, OP_GREATER_EQUAL, RECT3) + CASE_TYPE(math, OP_GREATER_EQUAL, BASIS) + CASE_TYPE(math, OP_GREATER_EQUAL, TRANSFORM) + CASE_TYPE(math, OP_GREATER_EQUAL, COLOR) + CASE_TYPE(math, OP_GREATER_EQUAL, NODE_PATH) + CASE_TYPE(math, OP_GREATER_EQUAL, DICTIONARY) + CASE_TYPE(math, OP_GREATER_EQUAL, ARRAY) + CASE_TYPE(math, OP_GREATER_EQUAL, POOL_BYTE_ARRAY); + CASE_TYPE(math, OP_GREATER_EQUAL, POOL_INT_ARRAY); + CASE_TYPE(math, OP_GREATER_EQUAL, POOL_REAL_ARRAY); + CASE_TYPE(math, OP_GREATER_EQUAL, POOL_STRING_ARRAY); + CASE_TYPE(math, OP_GREATER_EQUAL, POOL_VECTOR2_ARRAY); + CASE_TYPE(math, OP_GREATER_EQUAL, POOL_VECTOR3_ARRAY); + CASE_TYPE(math, OP_GREATER_EQUAL, POOL_COLOR_ARRAY); + _RETURN_FAIL; + } - } break; - case INT: { - int64_t b = p_b._data._int; - if (b == 0) { + SWITCH_OP(math, OP_ADD, p_a.type) { + CASE_TYPE(math, OP_ADD, ARRAY) { + if (p_a.type != p_b.type) + _RETURN_FAIL; + + const Array &array_a = *reinterpret_cast<const Array *>(p_a._data._mem); + const Array &array_b = *reinterpret_cast<const Array *>(p_b._data._mem); + Array sum; + int asize = array_a.size(); + int bsize = array_b.size(); + sum.resize(asize + bsize); + for (int i = 0; i < asize; i++) + sum[i] = array_a[i]; + for (int i = 0; i < bsize; i++) + sum[i + asize] = array_b[i]; + _RETURN(sum); + } + + DEFAULT_OP_NUM(math, OP_ADD, INT, +, _int); + DEFAULT_OP_NUM(math, OP_ADD, REAL, +, _real); + DEFAULT_OP_STR(math, OP_ADD, STRING, +, String); + DEFAULT_OP_LOCALMEM(math, OP_ADD, VECTOR2, +, Vector2); + DEFAULT_OP_LOCALMEM(math, OP_ADD, VECTOR3, +, Vector3); + DEFAULT_OP_LOCALMEM(math, OP_ADD, QUAT, +, Quat); + DEFAULT_OP_LOCALMEM(math, OP_ADD, COLOR, +, Color); + + DEFAULT_OP_ARRAY_ADD(math, OP_ADD, POOL_BYTE_ARRAY, uint8_t); + DEFAULT_OP_ARRAY_ADD(math, OP_ADD, POOL_INT_ARRAY, int); + DEFAULT_OP_ARRAY_ADD(math, OP_ADD, POOL_REAL_ARRAY, real_t); + DEFAULT_OP_ARRAY_ADD(math, OP_ADD, POOL_STRING_ARRAY, String); + DEFAULT_OP_ARRAY_ADD(math, OP_ADD, POOL_VECTOR2_ARRAY, Vector2); + DEFAULT_OP_ARRAY_ADD(math, OP_ADD, POOL_VECTOR3_ARRAY, Vector3); + DEFAULT_OP_ARRAY_ADD(math, OP_ADD, POOL_COLOR_ARRAY, Color); + + CASE_TYPE(math, OP_ADD, NIL) + CASE_TYPE(math, OP_ADD, BOOL) + CASE_TYPE(math, OP_ADD, RECT2) + CASE_TYPE(math, OP_ADD, TRANSFORM2D) + CASE_TYPE(math, OP_ADD, PLANE) + CASE_TYPE(math, OP_ADD, RECT3) + CASE_TYPE(math, OP_ADD, BASIS) + CASE_TYPE(math, OP_ADD, TRANSFORM) + CASE_TYPE(math, OP_ADD, NODE_PATH) + CASE_TYPE(math, OP_ADD, _RID) + CASE_TYPE(math, OP_ADD, OBJECT) + CASE_TYPE(math, OP_ADD, DICTIONARY) + _RETURN_FAIL; + } - r_valid = false; - _RETURN("Division By Zero"); - } - _RETURN(p_a._data._int / b); + SWITCH_OP(math, OP_SUBTRACT, p_a.type) { + DEFAULT_OP_NUM(math, OP_SUBTRACT, INT, -, _int); + DEFAULT_OP_NUM(math, OP_SUBTRACT, REAL, -, _real); + DEFAULT_OP_LOCALMEM(math, OP_SUBTRACT, VECTOR2, -, Vector2); + DEFAULT_OP_LOCALMEM(math, OP_SUBTRACT, VECTOR3, -, Vector3); + DEFAULT_OP_LOCALMEM(math, OP_SUBTRACT, QUAT, -, Quat); + DEFAULT_OP_LOCALMEM(math, OP_SUBTRACT, COLOR, -, Color); + + CASE_TYPE(math, OP_SUBTRACT, NIL) + CASE_TYPE(math, OP_SUBTRACT, BOOL) + CASE_TYPE(math, OP_SUBTRACT, STRING) + CASE_TYPE(math, OP_SUBTRACT, RECT2) + CASE_TYPE(math, OP_SUBTRACT, TRANSFORM2D) + CASE_TYPE(math, OP_SUBTRACT, PLANE) + CASE_TYPE(math, OP_SUBTRACT, RECT3) + CASE_TYPE(math, OP_SUBTRACT, BASIS) + CASE_TYPE(math, OP_SUBTRACT, TRANSFORM) + CASE_TYPE(math, OP_SUBTRACT, NODE_PATH) + CASE_TYPE(math, OP_SUBTRACT, _RID) + CASE_TYPE(math, OP_SUBTRACT, OBJECT) + CASE_TYPE(math, OP_SUBTRACT, DICTIONARY) + CASE_TYPE(math, OP_SUBTRACT, ARRAY) + CASE_TYPE(math, OP_SUBTRACT, POOL_BYTE_ARRAY); + CASE_TYPE(math, OP_SUBTRACT, POOL_INT_ARRAY); + CASE_TYPE(math, OP_SUBTRACT, POOL_REAL_ARRAY); + CASE_TYPE(math, OP_SUBTRACT, POOL_STRING_ARRAY); + CASE_TYPE(math, OP_SUBTRACT, POOL_VECTOR2_ARRAY); + CASE_TYPE(math, OP_SUBTRACT, POOL_VECTOR3_ARRAY); + CASE_TYPE(math, OP_SUBTRACT, POOL_COLOR_ARRAY); + _RETURN_FAIL; + } - } break; - case REAL: _RETURN(p_a._data._int / p_b._data._real); - default: {} + SWITCH_OP(math, OP_MULTIPLY, p_a.type) { + CASE_TYPE(math, OP_MULTIPLY, TRANSFORM2D) { + switch (p_b.type) { + case TRANSFORM2D: { + _RETURN(*p_a._data._transform2d * *p_b._data._transform2d); } - r_valid = false; - return; - }; - DEFAULT_OP_NUM(/, REAL, _real); - DEFAULT_OP_FAIL(STRING); - DEFAULT_OP_LOCALMEM_NUM(/, VECTOR2, Vector2); - DEFAULT_OP_FAIL(RECT2); - DEFAULT_OP_FAIL(TRANSFORM2D); - DEFAULT_OP_LOCALMEM_NUM(/, VECTOR3, Vector3); - DEFAULT_OP_FAIL(PLANE); - case QUAT: { - if (p_b.type != REAL) { - r_valid = false; - return; + case VECTOR2: { + _RETURN(p_a._data._transform2d->xform(*(const Vector2 *)p_b._data._mem)); } - _RETURN(*reinterpret_cast<const Quat *>(p_a._data._mem) / p_b._data._real); - } break; - DEFAULT_OP_FAIL(RECT3); - DEFAULT_OP_FAIL(BASIS); - DEFAULT_OP_FAIL(TRANSFORM); - - DEFAULT_OP_FAIL(COLOR); - - DEFAULT_OP_FAIL(NODE_PATH); - DEFAULT_OP_FAIL(_RID); - DEFAULT_OP_FAIL(OBJECT); - DEFAULT_OP_FAIL(DICTIONARY); - DEFAULT_OP_FAIL(ARRAY); - DEFAULT_OP_FAIL(POOL_BYTE_ARRAY); - DEFAULT_OP_FAIL(POOL_INT_ARRAY); - DEFAULT_OP_FAIL(POOL_REAL_ARRAY); - DEFAULT_OP_FAIL(POOL_STRING_ARRAY); - DEFAULT_OP_FAIL(POOL_VECTOR2_ARRAY); - DEFAULT_OP_FAIL(POOL_VECTOR3_ARRAY); - DEFAULT_OP_FAIL(POOL_COLOR_ARRAY); - case VARIANT_MAX: { - r_valid = false; - return; + default: _RETURN_FAIL; + } + } - } break; - } - - } break; - case OP_POSITIVE: { - // Simple case when user defines variable as +value. - switch (p_a.type) { - - DEFAULT_OP_FAIL(NIL); - DEFAULT_OP_FAIL(STRING); - DEFAULT_OP_FAIL(RECT2); - DEFAULT_OP_FAIL(TRANSFORM2D); - DEFAULT_OP_FAIL(RECT3); - DEFAULT_OP_FAIL(BASIS); - DEFAULT_OP_FAIL(TRANSFORM); - DEFAULT_OP_NUM_POS(BOOL, _bool); - DEFAULT_OP_NUM_POS(INT, _int); - DEFAULT_OP_NUM_POS(REAL, _real); - DEFAULT_OP_LOCALMEM_POS(VECTOR3, Vector3); - DEFAULT_OP_LOCALMEM_POS(PLANE, Plane); - DEFAULT_OP_LOCALMEM_POS(QUAT, Quat); - DEFAULT_OP_LOCALMEM_POS(VECTOR2, Vector2); - - DEFAULT_OP_FAIL(COLOR); - - DEFAULT_OP_FAIL(NODE_PATH); - DEFAULT_OP_FAIL(_RID); - DEFAULT_OP_FAIL(OBJECT); - DEFAULT_OP_FAIL(DICTIONARY); - DEFAULT_OP_FAIL(ARRAY); - DEFAULT_OP_FAIL(POOL_BYTE_ARRAY); - DEFAULT_OP_FAIL(POOL_INT_ARRAY); - DEFAULT_OP_FAIL(POOL_REAL_ARRAY); - DEFAULT_OP_FAIL(POOL_STRING_ARRAY); - DEFAULT_OP_FAIL(POOL_VECTOR2_ARRAY); - DEFAULT_OP_FAIL(POOL_VECTOR3_ARRAY); - DEFAULT_OP_FAIL(POOL_COLOR_ARRAY); - case VARIANT_MAX: { - r_valid = false; - return; + CASE_TYPE(math, OP_MULTIPLY, QUAT) { + switch (p_b.type) { + case VECTOR3: { + _RETURN(reinterpret_cast<const Quat *>(p_a._data._mem)->xform(*(const Vector3 *)p_b._data._mem)); + } + case QUAT: { + _RETURN(*reinterpret_cast<const Quat *>(p_a._data._mem) * *reinterpret_cast<const Quat *>(p_b._data._mem)); + } + case REAL: { + _RETURN(*reinterpret_cast<const Quat *>(p_a._data._mem) * p_b._data._real); + } + default: _RETURN_FAIL; + } + } - } break; - } - } break; - case OP_NEGATE: { - switch (p_a.type) { - - DEFAULT_OP_FAIL(NIL); - DEFAULT_OP_NUM_NEG(BOOL, _bool); - DEFAULT_OP_NUM_NEG(INT, _int); - DEFAULT_OP_NUM_NEG(REAL, _real); - DEFAULT_OP_FAIL(STRING); - DEFAULT_OP_LOCALMEM_NEG(VECTOR2, Vector2); - DEFAULT_OP_FAIL(RECT2); - DEFAULT_OP_FAIL(TRANSFORM2D); - DEFAULT_OP_LOCALMEM_NEG(VECTOR3, Vector3); - DEFAULT_OP_LOCALMEM_NEG(PLANE, Plane); - DEFAULT_OP_LOCALMEM_NEG(QUAT, Quat); - DEFAULT_OP_FAIL(RECT3); - DEFAULT_OP_FAIL(BASIS); - DEFAULT_OP_FAIL(TRANSFORM); - - DEFAULT_OP_FAIL(COLOR); - - DEFAULT_OP_FAIL(NODE_PATH); - DEFAULT_OP_FAIL(_RID); - DEFAULT_OP_FAIL(OBJECT); - DEFAULT_OP_FAIL(DICTIONARY); - DEFAULT_OP_FAIL(ARRAY); - DEFAULT_OP_FAIL(POOL_BYTE_ARRAY); - DEFAULT_OP_FAIL(POOL_INT_ARRAY); - DEFAULT_OP_FAIL(POOL_REAL_ARRAY); - DEFAULT_OP_FAIL(POOL_STRING_ARRAY); - DEFAULT_OP_FAIL(POOL_VECTOR2_ARRAY); - DEFAULT_OP_FAIL(POOL_VECTOR3_ARRAY); - DEFAULT_OP_FAIL(POOL_COLOR_ARRAY); - case VARIANT_MAX: { - r_valid = false; - return; + CASE_TYPE(math, OP_MULTIPLY, BASIS) { + switch (p_b.type) { + case VECTOR3: { + _RETURN(p_a._data._basis->xform(*(const Vector3 *)p_b._data._mem)); + } + case BASIS: { + _RETURN(*p_a._data._basis * *p_b._data._basis); + } + default: _RETURN_FAIL; + } + } - } break; + CASE_TYPE(math, OP_MULTIPLY, TRANSFORM) { + switch (p_b.type) { + case VECTOR3: { + _RETURN(p_a._data._transform->xform(*(const Vector3 *)p_b._data._mem)); + } + case TRANSFORM: { + _RETURN(*p_a._data._transform * *p_b._data._transform); + } + default: _RETURN_FAIL; + } } - } break; - case OP_MODULE: { - if (p_a.type == INT && p_b.type == INT) { + + DEFAULT_OP_NUM_VEC(math, OP_MULTIPLY, INT, *, _int); + DEFAULT_OP_NUM_VEC(math, OP_MULTIPLY, REAL, *, _real); + DEFAULT_OP_LOCALMEM_NUM(math, OP_MULTIPLY, VECTOR2, *, Vector2); + DEFAULT_OP_LOCALMEM_NUM(math, OP_MULTIPLY, VECTOR3, *, Vector3); + DEFAULT_OP_LOCALMEM_NUM(math, OP_MULTIPLY, COLOR, *, Color); + + CASE_TYPE(math, OP_MULTIPLY, NIL) + CASE_TYPE(math, OP_MULTIPLY, BOOL) + CASE_TYPE(math, OP_MULTIPLY, STRING) + CASE_TYPE(math, OP_MULTIPLY, RECT2) + CASE_TYPE(math, OP_MULTIPLY, PLANE) + CASE_TYPE(math, OP_MULTIPLY, RECT3) + CASE_TYPE(math, OP_MULTIPLY, NODE_PATH) + CASE_TYPE(math, OP_MULTIPLY, _RID) + CASE_TYPE(math, OP_MULTIPLY, OBJECT) + CASE_TYPE(math, OP_MULTIPLY, DICTIONARY) + CASE_TYPE(math, OP_MULTIPLY, ARRAY) + CASE_TYPE(math, OP_MULTIPLY, POOL_BYTE_ARRAY); + CASE_TYPE(math, OP_MULTIPLY, POOL_INT_ARRAY); + CASE_TYPE(math, OP_MULTIPLY, POOL_REAL_ARRAY); + CASE_TYPE(math, OP_MULTIPLY, POOL_STRING_ARRAY); + CASE_TYPE(math, OP_MULTIPLY, POOL_VECTOR2_ARRAY); + CASE_TYPE(math, OP_MULTIPLY, POOL_VECTOR3_ARRAY); + CASE_TYPE(math, OP_MULTIPLY, POOL_COLOR_ARRAY); + _RETURN_FAIL; + } + + SWITCH_OP(math, OP_DIVIDE, p_a.type) { + CASE_TYPE(math, OP_DIVIDE, QUAT) { + if (p_b.type != REAL) + _RETURN_FAIL; +#ifdef DEBUG_ENABLED + if (p_b._data._real == 0) { + r_valid = false; + _RETURN("Division By Zero"); + } +#endif + _RETURN(*reinterpret_cast<const Quat *>(p_a._data._mem) / p_b._data._real); + } + + DEFAULT_OP_NUM_DIV(math, OP_DIVIDE, INT, _int); + DEFAULT_OP_NUM_DIV(math, OP_DIVIDE, REAL, _real); + DEFAULT_OP_LOCALMEM_NUM(math, OP_DIVIDE, VECTOR2, /, Vector2); + DEFAULT_OP_LOCALMEM_NUM(math, OP_DIVIDE, VECTOR3, /, Vector3); + DEFAULT_OP_LOCALMEM_NUM(math, OP_DIVIDE, COLOR, /, Color); + + CASE_TYPE(math, OP_DIVIDE, NIL) + CASE_TYPE(math, OP_DIVIDE, BOOL) + CASE_TYPE(math, OP_DIVIDE, STRING) + CASE_TYPE(math, OP_DIVIDE, RECT2) + CASE_TYPE(math, OP_DIVIDE, TRANSFORM2D) + CASE_TYPE(math, OP_DIVIDE, PLANE) + CASE_TYPE(math, OP_DIVIDE, RECT3) + CASE_TYPE(math, OP_DIVIDE, BASIS) + CASE_TYPE(math, OP_DIVIDE, TRANSFORM) + CASE_TYPE(math, OP_DIVIDE, NODE_PATH) + CASE_TYPE(math, OP_DIVIDE, _RID) + CASE_TYPE(math, OP_DIVIDE, OBJECT) + CASE_TYPE(math, OP_DIVIDE, DICTIONARY) + CASE_TYPE(math, OP_DIVIDE, ARRAY) + CASE_TYPE(math, OP_DIVIDE, POOL_BYTE_ARRAY); + CASE_TYPE(math, OP_DIVIDE, POOL_INT_ARRAY); + CASE_TYPE(math, OP_DIVIDE, POOL_REAL_ARRAY); + CASE_TYPE(math, OP_DIVIDE, POOL_STRING_ARRAY); + CASE_TYPE(math, OP_DIVIDE, POOL_VECTOR2_ARRAY); + CASE_TYPE(math, OP_DIVIDE, POOL_VECTOR3_ARRAY); + CASE_TYPE(math, OP_DIVIDE, POOL_COLOR_ARRAY); + _RETURN_FAIL; + } + + SWITCH_OP(math, OP_POSITIVE, p_a.type) { + DEFAULT_OP_NUM_POS(math, OP_POSITIVE, INT, _int); + DEFAULT_OP_NUM_POS(math, OP_POSITIVE, REAL, _real); + DEFAULT_OP_LOCALMEM_POS(math, OP_POSITIVE, VECTOR3, Vector3); + DEFAULT_OP_LOCALMEM_POS(math, OP_POSITIVE, PLANE, Plane); + DEFAULT_OP_LOCALMEM_POS(math, OP_POSITIVE, QUAT, Quat); + DEFAULT_OP_LOCALMEM_POS(math, OP_POSITIVE, VECTOR2, Vector2); + + CASE_TYPE(math, OP_POSITIVE, NIL) + CASE_TYPE(math, OP_POSITIVE, BOOL) + CASE_TYPE(math, OP_POSITIVE, STRING) + CASE_TYPE(math, OP_POSITIVE, RECT2) + CASE_TYPE(math, OP_POSITIVE, TRANSFORM2D) + CASE_TYPE(math, OP_POSITIVE, RECT3) + CASE_TYPE(math, OP_POSITIVE, BASIS) + CASE_TYPE(math, OP_POSITIVE, TRANSFORM) + CASE_TYPE(math, OP_POSITIVE, COLOR) + CASE_TYPE(math, OP_POSITIVE, NODE_PATH) + CASE_TYPE(math, OP_POSITIVE, _RID) + CASE_TYPE(math, OP_POSITIVE, OBJECT) + CASE_TYPE(math, OP_POSITIVE, DICTIONARY) + CASE_TYPE(math, OP_POSITIVE, ARRAY) + CASE_TYPE(math, OP_POSITIVE, POOL_BYTE_ARRAY) + CASE_TYPE(math, OP_POSITIVE, POOL_INT_ARRAY) + CASE_TYPE(math, OP_POSITIVE, POOL_REAL_ARRAY) + CASE_TYPE(math, OP_POSITIVE, POOL_STRING_ARRAY) + CASE_TYPE(math, OP_POSITIVE, POOL_VECTOR2_ARRAY) + CASE_TYPE(math, OP_POSITIVE, POOL_VECTOR3_ARRAY) + CASE_TYPE(math, OP_POSITIVE, POOL_COLOR_ARRAY) + _RETURN_FAIL; + } + + SWITCH_OP(math, OP_NEGATE, p_a.type) { + DEFAULT_OP_NUM_NEG(math, OP_NEGATE, INT, _int); + DEFAULT_OP_NUM_NEG(math, OP_NEGATE, REAL, _real); + + DEFAULT_OP_LOCALMEM_NEG(math, OP_NEGATE, VECTOR2, Vector2); + DEFAULT_OP_LOCALMEM_NEG(math, OP_NEGATE, VECTOR3, Vector3); + DEFAULT_OP_LOCALMEM_NEG(math, OP_NEGATE, PLANE, Plane); + DEFAULT_OP_LOCALMEM_NEG(math, OP_NEGATE, QUAT, Quat); + DEFAULT_OP_LOCALMEM_NEG(math, OP_NEGATE, COLOR, Color); + + CASE_TYPE(math, OP_NEGATE, NIL) + CASE_TYPE(math, OP_NEGATE, BOOL) + CASE_TYPE(math, OP_NEGATE, STRING) + CASE_TYPE(math, OP_NEGATE, RECT2) + CASE_TYPE(math, OP_NEGATE, TRANSFORM2D) + CASE_TYPE(math, OP_NEGATE, RECT3) + CASE_TYPE(math, OP_NEGATE, BASIS) + CASE_TYPE(math, OP_NEGATE, TRANSFORM) + CASE_TYPE(math, OP_NEGATE, NODE_PATH) + CASE_TYPE(math, OP_NEGATE, _RID) + CASE_TYPE(math, OP_NEGATE, OBJECT) + CASE_TYPE(math, OP_NEGATE, DICTIONARY) + CASE_TYPE(math, OP_NEGATE, ARRAY) + CASE_TYPE(math, OP_NEGATE, POOL_BYTE_ARRAY) + CASE_TYPE(math, OP_NEGATE, POOL_INT_ARRAY) + CASE_TYPE(math, OP_NEGATE, POOL_REAL_ARRAY) + CASE_TYPE(math, OP_NEGATE, POOL_STRING_ARRAY) + CASE_TYPE(math, OP_NEGATE, POOL_VECTOR2_ARRAY) + CASE_TYPE(math, OP_NEGATE, POOL_VECTOR3_ARRAY) + CASE_TYPE(math, OP_NEGATE, POOL_COLOR_ARRAY) + _RETURN_FAIL; + } + + SWITCH_OP(math, OP_MODULE, p_a.type) { + CASE_TYPE(math, OP_MODULE, INT) { + if (p_b.type != INT) + _RETURN_FAIL; #ifdef DEBUG_ENABLED if (p_b._data._int == 0) { r_valid = false; @@ -827,8 +1052,9 @@ void Variant::evaluate(const Operator &p_op, const Variant &p_a, const Variant & } #endif _RETURN(p_a._data._int % p_b._data._int); + } - } else if (p_a.type == STRING) { + CASE_TYPE(math, OP_MODULE, STRING) { const String *format = reinterpret_cast<const String *>(p_a._data._mem); String result; @@ -847,119 +1073,143 @@ void Variant::evaluate(const Operator &p_op, const Variant &p_a, const Variant & _RETURN(result); } - r_valid = false; - return; + CASE_TYPE(math, OP_MODULE, NIL) + CASE_TYPE(math, OP_MODULE, BOOL) + CASE_TYPE(math, OP_MODULE, REAL) + CASE_TYPE(math, OP_MODULE, VECTOR2) + CASE_TYPE(math, OP_MODULE, RECT2) + CASE_TYPE(math, OP_MODULE, VECTOR3) + CASE_TYPE(math, OP_MODULE, TRANSFORM2D) + CASE_TYPE(math, OP_MODULE, PLANE) + CASE_TYPE(math, OP_MODULE, QUAT) + CASE_TYPE(math, OP_MODULE, RECT3) + CASE_TYPE(math, OP_MODULE, BASIS) + CASE_TYPE(math, OP_MODULE, TRANSFORM) + CASE_TYPE(math, OP_MODULE, COLOR) + CASE_TYPE(math, OP_MODULE, NODE_PATH) + CASE_TYPE(math, OP_MODULE, _RID) + CASE_TYPE(math, OP_MODULE, OBJECT) + CASE_TYPE(math, OP_MODULE, DICTIONARY) + CASE_TYPE(math, OP_MODULE, ARRAY) + CASE_TYPE(math, OP_MODULE, POOL_BYTE_ARRAY) + CASE_TYPE(math, OP_MODULE, POOL_INT_ARRAY) + CASE_TYPE(math, OP_MODULE, POOL_REAL_ARRAY) + CASE_TYPE(math, OP_MODULE, POOL_STRING_ARRAY) + CASE_TYPE(math, OP_MODULE, POOL_VECTOR2_ARRAY) + CASE_TYPE(math, OP_MODULE, POOL_VECTOR3_ARRAY) + CASE_TYPE(math, OP_MODULE, POOL_COLOR_ARRAY) + _RETURN_FAIL; + } - } break; - case OP_STRING_CONCAT: { + SWITCH_OP(math, OP_STRING_CONCAT, p_a.type) { + CASE_TYPE_ALL(math, OP_STRING_CONCAT) _RETURN(p_a.operator String() + p_b.operator String()); - } break; - //bitwise - case OP_SHIFT_LEFT: { - if (p_a.type == INT && p_b.type == INT) + } + + SWITCH_OP(math, OP_SHIFT_LEFT, p_a.type) { + CASE_TYPE(math, OP_SHIFT_LEFT, INT) { + if (p_b.type != INT) + _RETURN_FAIL; _RETURN(p_a._data._int << p_b._data._int); + } - r_valid = false; - return; + CASE_TYPE_ALL_BUT_INT(math, OP_SHIFT_LEFT) + _RETURN_FAIL; + } - } break; - case OP_SHIFT_RIGHT: { - if (p_a.type == INT && p_b.type == INT) + SWITCH_OP(math, OP_SHIFT_RIGHT, p_a.type) { + CASE_TYPE(math, OP_SHIFT_RIGHT, INT) { + if (p_b.type != INT) + _RETURN_FAIL; _RETURN(p_a._data._int >> p_b._data._int); + } - r_valid = false; - return; + CASE_TYPE_ALL_BUT_INT(math, OP_SHIFT_RIGHT) + _RETURN_FAIL; + } - } break; - case OP_BIT_AND: { - if (p_a.type == INT && p_b.type == INT) + SWITCH_OP(math, OP_BIT_AND, p_a.type) { + CASE_TYPE(math, OP_BIT_AND, INT) { + if (p_b.type != INT) + _RETURN_FAIL; _RETURN(p_a._data._int & p_b._data._int); + } - r_valid = false; - return; - - } break; - case OP_BIT_OR: { + CASE_TYPE_ALL_BUT_INT(math, OP_BIT_AND) + _RETURN_FAIL; + } - if (p_a.type == INT && p_b.type == INT) + SWITCH_OP(math, OP_BIT_OR, p_a.type) { + CASE_TYPE(math, OP_BIT_OR, INT) { + if (p_b.type != INT) + _RETURN_FAIL; _RETURN(p_a._data._int | p_b._data._int); + } - r_valid = false; - return; - - } break; - case OP_BIT_XOR: { + CASE_TYPE_ALL_BUT_INT(math, OP_BIT_OR) + _RETURN_FAIL; + } - if (p_a.type == INT && p_b.type == INT) + SWITCH_OP(math, OP_BIT_XOR, p_a.type) { + CASE_TYPE(math, OP_BIT_XOR, INT) { + if (p_b.type != INT) + _RETURN_FAIL; _RETURN(p_a._data._int ^ p_b._data._int); + } - r_valid = false; - return; - - } break; - case OP_BIT_NEGATE: { + CASE_TYPE_ALL_BUT_INT(math, OP_BIT_XOR) + _RETURN_FAIL; + } - if (p_a.type == INT) + SWITCH_OP(math, OP_BIT_NEGATE, p_a.type) { + CASE_TYPE(math, OP_BIT_NEGATE, INT) { _RETURN(~p_a._data._int); + } - r_valid = false; - return; + CASE_TYPE_ALL_BUT_INT(math, OP_BIT_NEGATE) + _RETURN_FAIL; + } - } break; - //logic - case OP_AND: { - bool l = p_a.booleanize(r_valid); - if (!r_valid) - return; - bool r = p_b.booleanize(r_valid); - if (!r_valid) - return; + SWITCH_OP(math, OP_AND, p_a.type) { + CASE_TYPE_ALL(math, OP_AND) { + bool l = p_a.booleanize(); + bool r = p_b.booleanize(); - _RETURN(l && r); - } break; - case OP_OR: { - bool l = p_a.booleanize(r_valid); - if (!r_valid) - return; - bool r = p_b.booleanize(r_valid); - if (!r_valid) - return; + _RETURN(l && r); + } + } - _RETURN(l || r); + SWITCH_OP(math, OP_OR, p_a.type) { + CASE_TYPE_ALL(math, OP_OR) { + bool l = p_a.booleanize(); + bool r = p_b.booleanize(); - } break; - case OP_XOR: { - bool l = p_a.booleanize(r_valid); - if (!r_valid) - return; - bool r = p_b.booleanize(r_valid); - if (!r_valid) - return; + _RETURN(l || r); + } + } - _RETURN((l || r) && !(l && r)); - } break; - case OP_NOT: { + SWITCH_OP(math, OP_XOR, p_a.type) { + CASE_TYPE_ALL(math, OP_XOR) { + bool l = p_a.booleanize(); + bool r = p_b.booleanize(); - bool l = p_a.booleanize(r_valid); - if (!r_valid) - return; - _RETURN(!l); + _RETURN((l || r) && !(l && r)); + } + } - } break; - case OP_IN: { + SWITCH_OP(math, OP_NOT, p_a.type) { + CASE_TYPE_ALL(math, OP_NOT) { + bool l = p_a.booleanize(); + _RETURN(!l); + } + } + SWITCH_OP(math, OP_IN, p_a.type) { + CASE_TYPE_ALL(math, OP_IN) _RETURN(p_b.in(p_a, &r_valid)); - - } break; - case OP_MAX: { - - r_valid = false; - ERR_FAIL(); } } - - r_valid = false; } void Variant::set_named(const StringName &p_index, const Variant &p_value, bool *r_valid) { @@ -1512,7 +1762,8 @@ void Variant::set(const Variant &p_index, const Variant &p_value, bool *r_valid) DEFAULT_OP_DVECTOR_SET(POOL_VECTOR2_ARRAY, Vector2, p_value.type != Variant::VECTOR2) // 25 DEFAULT_OP_DVECTOR_SET(POOL_VECTOR3_ARRAY, Vector3, p_value.type != Variant::VECTOR3) DEFAULT_OP_DVECTOR_SET(POOL_COLOR_ARRAY, Color, p_value.type != Variant::COLOR) - default: return; + default: + return; } } @@ -1885,7 +2136,8 @@ Variant Variant::get(const Variant &p_index, bool *r_valid) const { DEFAULT_OP_DVECTOR_GET(POOL_VECTOR2_ARRAY, Vector2) // 25 DEFAULT_OP_DVECTOR_GET(POOL_VECTOR3_ARRAY, Vector3) DEFAULT_OP_DVECTOR_GET(POOL_COLOR_ARRAY, Color) - default: return Variant(); + default: + return Variant(); } return Variant(); @@ -2374,7 +2626,8 @@ bool Variant::iter_init(Variant &r_iter, bool &valid) const { return true; } break; - default: {} + default: { + } } valid = false; @@ -2401,7 +2654,7 @@ bool Variant::iter_next(Variant &r_iter, bool &valid) const { return true; } break; case VECTOR2: { - int64_t to = reinterpret_cast<const Vector3 *>(_data._mem)->y; + int64_t to = reinterpret_cast<const Vector2 *>(_data._mem)->y; int64_t idx = r_iter; idx++; @@ -2793,7 +3046,9 @@ void Variant::blend(const Variant &a, const Variant &b, float c, Variant &r_dst) r_dst = Color(r, g, b, a); } return; - default: { r_dst = c < 0.5 ? a : b; } + default: { + r_dst = c < 0.5 ? a : b; + } return; } } |