diff options
243 files changed, 11940 insertions, 5378 deletions
diff --git a/.github/workflows/android_builds.yml b/.github/workflows/android_builds.yml index 3ce3765efc..f4b571fc08 100644 --- a/.github/workflows/android_builds.yml +++ b/.github/workflows/android_builds.yml @@ -4,7 +4,7 @@ on: [push, pull_request] # Global Settings env: GODOT_BASE_BRANCH: master - SCONSFLAGS: platform=android verbose=yes warnings=all werror=yes --jobs=2 + SCONSFLAGS: platform=android verbose=yes warnings=extra werror=yes --jobs=2 SCONS_CACHE_LIMIT: 4096 jobs: diff --git a/.github/workflows/ios_builds.yml b/.github/workflows/ios_builds.yml index 80db5d68ce..fa965a45f1 100644 --- a/.github/workflows/ios_builds.yml +++ b/.github/workflows/ios_builds.yml @@ -4,7 +4,7 @@ on: [push, pull_request] # Global Settings env: GODOT_BASE_BRANCH: master - SCONSFLAGS: platform=iphone verbose=yes warnings=all werror=yes --jobs=2 + SCONSFLAGS: platform=iphone verbose=yes warnings=extra werror=yes --jobs=2 SCONS_CACHE_LIMIT: 4096 jobs: diff --git a/.github/workflows/javascript_builds.disabled b/.github/workflows/javascript_builds.disabled index 8ed77726cc..015a1f314c 100644 --- a/.github/workflows/javascript_builds.disabled +++ b/.github/workflows/javascript_builds.disabled @@ -4,7 +4,7 @@ on: [push, pull_request] # Global Settings env: GODOT_BASE_BRANCH: master - SCONSFLAGS: platform=javascript verbose=yes warnings=all werror=yes --jobs=2 + SCONSFLAGS: platform=javascript verbose=yes warnings=extra werror=yes --jobs=2 SCONS_CACHE_LIMIT: 4096 EM_VERSION: 1.39.20 EM_CACHE_FOLDER: 'emsdk-cache' diff --git a/.github/workflows/linux_builds.yml b/.github/workflows/linux_builds.yml index b81c06e54d..b7b40eccbd 100644 --- a/.github/workflows/linux_builds.yml +++ b/.github/workflows/linux_builds.yml @@ -4,7 +4,7 @@ on: [push, pull_request] # Global Settings env: GODOT_BASE_BRANCH: master - SCONSFLAGS: platform=linuxbsd verbose=yes warnings=all werror=yes --jobs=2 + SCONSFLAGS: platform=linuxbsd verbose=yes warnings=extra werror=yes --jobs=2 SCONS_CACHE_LIMIT: 4096 jobs: diff --git a/.github/workflows/macos_builds.yml b/.github/workflows/macos_builds.yml index 5f9ad24ac7..401cfadc30 100644 --- a/.github/workflows/macos_builds.yml +++ b/.github/workflows/macos_builds.yml @@ -4,7 +4,7 @@ on: [push, pull_request] # Global Settings env: GODOT_BASE_BRANCH: master - SCONSFLAGS: platform=osx verbose=yes warnings=all werror=yes --jobs=2 + SCONSFLAGS: platform=osx verbose=yes warnings=extra werror=yes --jobs=2 SCONS_CACHE_LIMIT: 4096 jobs: diff --git a/core/core_constants.cpp b/core/core_constants.cpp index 41fa188c5b..4229436e84 100644 --- a/core/core_constants.cpp +++ b/core/core_constants.cpp @@ -606,7 +606,7 @@ void register_global_constants() { BIND_CORE_ENUM_CONSTANT_CUSTOM("TYPE_COLOR", Variant::COLOR); BIND_CORE_ENUM_CONSTANT_CUSTOM("TYPE_STRING_NAME", Variant::STRING_NAME); BIND_CORE_ENUM_CONSTANT_CUSTOM("TYPE_NODE_PATH", Variant::NODE_PATH); - BIND_CORE_ENUM_CONSTANT_CUSTOM("TYPE_RID", Variant::_RID); + BIND_CORE_ENUM_CONSTANT_CUSTOM("TYPE_RID", Variant::RID); BIND_CORE_ENUM_CONSTANT_CUSTOM("TYPE_OBJECT", Variant::OBJECT); BIND_CORE_ENUM_CONSTANT_CUSTOM("TYPE_CALLABLE", Variant::CALLABLE); BIND_CORE_ENUM_CONSTANT_CUSTOM("TYPE_SIGNAL", Variant::SIGNAL); diff --git a/core/io/file_access_pack.cpp b/core/io/file_access_pack.cpp index b63e0adc45..a025ca5730 100644 --- a/core/io/file_access_pack.cpp +++ b/core/io/file_access_pack.cpp @@ -442,8 +442,14 @@ String DirAccessPack::get_drive(int p_drive) { return ""; } -Error DirAccessPack::change_dir(String p_dir) { +PackedData::PackedDir *DirAccessPack::_find_dir(String p_dir) { String nd = p_dir.replace("\\", "/"); + + // Special handling since simplify_path() will forbid it + if (p_dir == "..") { + return current->parent; + } + bool absolute = false; if (nd.begins_with("res://")) { nd = nd.replace_first("res://", ""); @@ -483,13 +489,21 @@ Error DirAccessPack::change_dir(String p_dir) { pd = pd->subdirs[p]; } else { - return ERR_INVALID_PARAMETER; + return nullptr; } } - current = pd; + return pd; +} - return OK; +Error DirAccessPack::change_dir(String p_dir) { + PackedData::PackedDir *pd = _find_dir(p_dir); + if (pd) { + current = pd; + return OK; + } else { + return ERR_INVALID_PARAMETER; + } } String DirAccessPack::get_current_dir(bool p_include_drive) { @@ -507,13 +521,17 @@ String DirAccessPack::get_current_dir(bool p_include_drive) { bool DirAccessPack::file_exists(String p_file) { p_file = fix_path(p_file); - return current->files.has(p_file); + PackedData::PackedDir *pd = _find_dir(p_file.get_base_dir()); + if (!pd) { + return false; + } + return pd->files.has(p_file.get_file()); } bool DirAccessPack::dir_exists(String p_dir) { p_dir = fix_path(p_dir); - return current->subdirs.has(p_dir); + return _find_dir(p_dir) != nullptr; } Error DirAccessPack::make_dir(String p_dir) { diff --git a/core/io/file_access_pack.h b/core/io/file_access_pack.h index 5c58dc01b4..c13626a5aa 100644 --- a/core/io/file_access_pack.h +++ b/core/io/file_access_pack.h @@ -122,6 +122,9 @@ public: _FORCE_INLINE_ FileAccess *try_open_path(const String &p_path); _FORCE_INLINE_ bool has_path(const String &p_path); + _FORCE_INLINE_ DirAccess *try_open_directory(const String &p_path); + _FORCE_INLINE_ bool has_directory(const String &p_path); + PackedData(); ~PackedData(); }; @@ -199,6 +202,16 @@ bool PackedData::has_path(const String &p_path) { return files.has(PathMD5(p_path.md5_buffer())); } +bool PackedData::has_directory(const String &p_path) { + DirAccess *da = try_open_directory(p_path); + if (da) { + memdelete(da); + return true; + } else { + return false; + } +} + class DirAccessPack : public DirAccess { PackedData::PackedDir *current; @@ -206,6 +219,8 @@ class DirAccessPack : public DirAccess { List<String> list_files; bool cdir = false; + PackedData::PackedDir *_find_dir(String p_dir); + public: virtual Error list_dir_begin(); virtual String get_next(); @@ -235,4 +250,13 @@ public: ~DirAccessPack() {} }; +DirAccess *PackedData::try_open_directory(const String &p_path) { + DirAccess *da = memnew(DirAccessPack()); + if (da->change_dir(p_path) != OK) { + memdelete(da); + da = nullptr; + } + return da; +} + #endif // FILE_ACCESS_PACK_H diff --git a/core/io/json.cpp b/core/io/json.cpp index 58bce1cf63..d61c2b8236 100644 --- a/core/io/json.cpp +++ b/core/io/json.cpp @@ -455,3 +455,35 @@ Error JSON::parse(const String &p_json, Variant &r_ret, String &r_err_str, int & return err; } + +Error JSONParser::parse_string(const String &p_json_string) { + return JSON::parse(p_json_string, data, err_text, err_line); +} +String JSONParser::get_error_text() const { + return err_text; +} +int JSONParser::get_error_line() const { + return err_line; +} +Variant JSONParser::get_data() const { + return data; +} + +Error JSONParser::decode_data(const Variant &p_data, const String &p_indent, bool p_sort_keys) { + string = JSON::print(p_data, p_indent, p_sort_keys); + data = p_data; + return OK; +} + +String JSONParser::get_string() const { + return string; +} + +void JSONParser::_bind_methods() { + ClassDB::bind_method(D_METHOD("parse_string", "json_string"), &JSONParser::parse_string); + ClassDB::bind_method(D_METHOD("get_error_text"), &JSONParser::get_error_text); + ClassDB::bind_method(D_METHOD("get_error_line"), &JSONParser::get_error_line); + ClassDB::bind_method(D_METHOD("get_data"), &JSONParser::get_data); + ClassDB::bind_method(D_METHOD("decode_data", "data", "indent", "sort_keys"), &JSONParser::decode_data, DEFVAL(""), DEFVAL(true)); + ClassDB::bind_method(D_METHOD("get_string"), &JSONParser::get_string); +} diff --git a/core/io/json.h b/core/io/json.h index 9fc6655fb4..2854d956ec 100644 --- a/core/io/json.h +++ b/core/io/json.h @@ -31,8 +31,8 @@ #ifndef JSON_H #define JSON_H +#include "core/object/reference.h" #include "core/variant/variant.h" - class JSON { enum TokenType { TK_CURLY_BRACKET_OPEN, @@ -75,4 +75,25 @@ public: static Error parse(const String &p_json, Variant &r_ret, String &r_err_str, int &r_err_line); }; +class JSONParser : public Reference { + GDCLASS(JSONParser, Reference); + + Variant data; + String string; + String err_text; + int err_line = 0; + +protected: + static void _bind_methods(); + +public: + Error parse_string(const String &p_json_string); + String get_error_text() const; + int get_error_line() const; + Variant get_data() const; + + Error decode_data(const Variant &p_data, const String &p_indent = "", bool p_sort_keys = true); + String get_string() const; +}; + #endif // JSON_H diff --git a/core/io/marshalls.cpp b/core/io/marshalls.cpp index c354c8ea63..3cf4acaf39 100644 --- a/core/io/marshalls.cpp +++ b/core/io/marshalls.cpp @@ -420,7 +420,7 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int } } break; - case Variant::_RID: { + case Variant::RID: { r_variant = RID(); } break; case Variant::OBJECT: { @@ -1172,7 +1172,7 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo r_len += 4 * 4; } break; - case Variant::_RID: { + case Variant::RID: { } break; case Variant::CALLABLE: { } break; diff --git a/core/io/packed_data_container.cpp b/core/io/packed_data_container.cpp index f70482288d..fbe8fa8a28 100644 --- a/core/io/packed_data_container.cpp +++ b/core/io/packed_data_container.cpp @@ -246,7 +246,7 @@ uint32_t PackedDataContainer::_pack(const Variant &p_data, Vector<uint8_t> &tmpd } break; // misc types - case Variant::_RID: + case Variant::RID: case Variant::OBJECT: { return _pack(Variant(), tmpdata, string_cache); } break; diff --git a/core/io/resource_format_binary.cpp b/core/io/resource_format_binary.cpp index fe93d9b067..c67e68e4fc 100644 --- a/core/io/resource_format_binary.cpp +++ b/core/io/resource_format_binary.cpp @@ -1467,7 +1467,7 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia } } break; - case Variant::_RID: { + case Variant::RID: { f->store_32(VARIANT_RID); WARN_PRINT("Can't save RIDs."); RID val = p_property; diff --git a/core/math/color.cpp b/core/math/color.cpp index 6e0f68a2d6..2afe14bd63 100644 --- a/core/math/color.cpp +++ b/core/math/color.cpp @@ -159,7 +159,7 @@ void Color::set_hsv(float p_h, float p_s, float p_v, float p_alpha) { a = p_alpha; if (p_s == 0) { - // acp_hromatic (grey) + // Achromatic (grey) r = g = b = p_v; return; } @@ -489,6 +489,13 @@ Color Color::operator+(const Color &p_color) const { a + p_color.a); } +void Color::operator+=(const Color &p_color) { + r = r + p_color.r; + g = g + p_color.g; + b = b + p_color.b; + a = a + p_color.a; +} + Color Color::operator-(const Color &p_color) const { return Color( r - p_color.r, @@ -512,12 +519,12 @@ Color Color::operator*(const Color &p_color) const { a * p_color.a); } -Color Color::operator*(real_t rvalue) const { +Color Color::operator*(real_t p_rvalue) const { return Color( - r * rvalue, - g * rvalue, - b * rvalue, - a * rvalue); + r * p_rvalue, + g * p_rvalue, + b * p_rvalue, + a * p_rvalue); } void Color::operator*=(const Color &p_color) { @@ -527,11 +534,11 @@ void Color::operator*=(const Color &p_color) { a = a * p_color.a; } -void Color::operator*=(real_t rvalue) { - r = r * rvalue; - g = g * rvalue; - b = b * rvalue; - a = a * rvalue; +void Color::operator*=(real_t p_rvalue) { + r = r * p_rvalue; + g = g * p_rvalue; + b = b * p_rvalue; + a = a * p_rvalue; } Color Color::operator/(const Color &p_color) const { @@ -542,12 +549,12 @@ Color Color::operator/(const Color &p_color) const { a / p_color.a); } -Color Color::operator/(real_t rvalue) const { +Color Color::operator/(real_t p_rvalue) const { return Color( - r / rvalue, - g / rvalue, - b / rvalue, - a / rvalue); + r / p_rvalue, + g / p_rvalue, + b / p_rvalue, + a / p_rvalue); } void Color::operator/=(const Color &p_color) { @@ -557,17 +564,17 @@ void Color::operator/=(const Color &p_color) { a = a / p_color.a; } -void Color::operator/=(real_t rvalue) { - if (rvalue == 0) { +void Color::operator/=(real_t p_rvalue) { + if (p_rvalue == 0) { r = 1.0; g = 1.0; b = 1.0; a = 1.0; } else { - r = r / rvalue; - g = g / rvalue; - b = b / rvalue; - a = a / rvalue; + r = r / p_rvalue; + g = g / p_rvalue; + b = b / p_rvalue; + a = a / p_rvalue; } } diff --git a/core/math/color.h b/core/math/color.h index a03ddbf5af..a9be9e9035 100644 --- a/core/math/color.h +++ b/core/math/color.h @@ -45,9 +45,6 @@ struct Color { float components[4] = { 0, 0, 0, 1.0 }; }; - bool operator==(const Color &p_color) const { return (r == p_color.r && g == p_color.g && b == p_color.b && a == p_color.a); } - bool operator!=(const Color &p_color) const { return (r != p_color.r || g != p_color.g || b != p_color.b || a != p_color.a); } - uint32_t to_rgba32() const; uint32_t to_argb32() const; uint32_t to_abgr32() const; @@ -59,34 +56,36 @@ struct Color { float get_v() const; void set_hsv(float p_h, float p_s, float p_v, float p_alpha = 1.0); - _FORCE_INLINE_ float &operator[](int idx) { - return components[idx]; + _FORCE_INLINE_ float &operator[](int p_idx) { + return components[p_idx]; } - _FORCE_INLINE_ const float &operator[](int idx) const { - return components[idx]; + _FORCE_INLINE_ const float &operator[](int p_idx) const { + return components[p_idx]; } - Color operator+(const Color &p_color) const; - _FORCE_INLINE_ void operator+=(const Color &p_color) { - r = r + p_color.r; - g = g + p_color.g; - b = b + p_color.b; - a = a + p_color.a; + bool operator==(const Color &p_color) const { + return (r == p_color.r && g == p_color.g && b == p_color.b && a == p_color.a); + } + bool operator!=(const Color &p_color) const { + return (r != p_color.r || g != p_color.g || b != p_color.b || a != p_color.a); } + 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*(real_t rvalue) const; + Color operator*(real_t p_rvalue) const; void operator*=(const Color &p_color); - void operator*=(real_t rvalue); + void operator*=(real_t p_rvalue); Color operator/(const Color &p_color) const; - Color operator/(real_t rvalue) const; + Color operator/(real_t p_rvalue) const; void operator/=(const Color &p_color); - void operator/=(real_t rvalue); + void operator/=(real_t p_rvalue); bool is_equal_approx(const Color &p_color) const; @@ -123,10 +122,9 @@ struct Color { _FORCE_INLINE_ uint32_t to_rgbe9995() const { const float pow2to9 = 512.0f; const float B = 15.0f; - //const float Emax = 31.0f; const float N = 9.0f; - float sharedexp = 65408.000f; //(( pow2to9 - 1.0f)/ pow2to9)*powf( 2.0f, 31.0f - 15.0f); + float sharedexp = 65408.000f; // Result of: ((pow2to9 - 1.0f) / pow2to9) * powf(2.0f, 31.0f - 15.0f) float cRed = MAX(0.0f, MIN(sharedexp, r)); float cGreen = MAX(0.0f, MIN(sharedexp, g)); @@ -134,8 +132,6 @@ struct Color { float cMax = MAX(cRed, MAX(cGreen, cBlue)); - // expp = MAX(-B - 1, log2(maxc)) + 1 + B - float expp = MAX(-B - 1.0f, floor(Math::log(cMax) / Math_LN2)) + 1.0f + B; float sMax = (float)floor((cMax / Math::pow(2.0f, expp - B - N)) + 0.5f); @@ -196,26 +192,27 @@ struct Color { _FORCE_INLINE_ bool operator<(const Color &p_color) const; //used in set keys operator String() const; - //for binder + // For the binder. _FORCE_INLINE_ void set_r8(int32_t r8) { r = (CLAMP(r8, 0, 255) / 255.0); } - _FORCE_INLINE_ int32_t get_r8() const { return CLAMP(uint32_t(r * 255.0), 0, 255); } + _FORCE_INLINE_ int32_t get_r8() const { return int32_t(CLAMP(r * 255.0, 0.0, 255.0)); } _FORCE_INLINE_ void set_g8(int32_t g8) { g = (CLAMP(g8, 0, 255) / 255.0); } - _FORCE_INLINE_ int32_t get_g8() const { return CLAMP(uint32_t(g * 255.0), 0, 255); } + _FORCE_INLINE_ int32_t get_g8() const { return int32_t(CLAMP(g * 255.0, 0.0, 255.0)); } _FORCE_INLINE_ void set_b8(int32_t b8) { b = (CLAMP(b8, 0, 255) / 255.0); } - _FORCE_INLINE_ int32_t get_b8() const { return CLAMP(uint32_t(b * 255.0), 0, 255); } + _FORCE_INLINE_ int32_t get_b8() const { return int32_t(CLAMP(b * 255.0, 0.0, 255.0)); } _FORCE_INLINE_ void set_a8(int32_t a8) { a = (CLAMP(a8, 0, 255) / 255.0); } - _FORCE_INLINE_ int32_t get_a8() const { return CLAMP(uint32_t(a * 255.0), 0, 255); } + _FORCE_INLINE_ int32_t get_a8() const { return int32_t(CLAMP(a * 255.0, 0.0, 255.0)); } - _FORCE_INLINE_ void set_h(float h) { set_hsv(h, get_s(), get_v()); } - _FORCE_INLINE_ void set_s(float s) { set_hsv(get_h(), s, get_v()); } - _FORCE_INLINE_ void set_v(float v) { set_hsv(get_h(), get_s(), v); } + _FORCE_INLINE_ void set_h(float p_h) { set_hsv(p_h, get_s(), get_v()); } + _FORCE_INLINE_ void set_s(float p_s) { set_hsv(get_h(), p_s, get_v()); } + _FORCE_INLINE_ void set_v(float p_v) { set_hsv(get_h(), get_s(), p_v); } _FORCE_INLINE_ Color() {} /** - * RGB / RGBA construct parameters. Alpha is optional, but defaults to 1.0 + * RGBA construct parameters. + * Alpha is not optional as otherwise we can't bind the RGB version for scripting. */ - _FORCE_INLINE_ Color(float p_r, float p_g, float p_b, float p_a = 1.0) { + _FORCE_INLINE_ Color(float p_r, float p_g, float p_b, float p_a) { r = p_r; g = p_g; b = p_b; @@ -223,6 +220,16 @@ struct Color { } /** + * RGB construct parameters. + */ + _FORCE_INLINE_ Color(float p_r, float p_g, float p_b) { + r = p_r; + g = p_g; + b = p_b; + a = 1.0; + } + + /** * Construct a Color from another Color, but with the specified alpha value. */ _FORCE_INLINE_ Color(const Color &p_c, float p_a) { diff --git a/core/math/color_names.inc b/core/math/color_names.inc index cbc821026e..523c7e3c59 100644 --- a/core/math/color_names.inc +++ b/core/math/color_names.inc @@ -61,9 +61,7 @@ static NamedColor named_colors[] = { { "gold", Color(1.00, 0.84, 0.00) }, { "goldenrod", Color(0.85, 0.65, 0.13) }, { "gray", Color(0.75, 0.75, 0.75) }, - { "webgray", Color(0.50, 0.50, 0.50) }, { "green", Color(0.00, 1.00, 0.00) }, - { "webgreen", Color(0.00, 0.50, 0.00) }, { "greenyellow", Color(0.68, 1.00, 0.18) }, { "honeydew", Color(0.94, 1.00, 0.94) }, { "hotpink", Color(1.00, 0.41, 0.71) }, @@ -93,7 +91,6 @@ static NamedColor named_colors[] = { { "linen", Color(0.98, 0.94, 0.90) }, { "magenta", Color(1.00, 0.00, 1.00) }, { "maroon", Color(0.69, 0.19, 0.38) }, - { "webmaroon", Color(0.50, 0.00, 0.00) }, { "mediumaquamarine", Color(0.40, 0.80, 0.67) }, { "mediumblue", Color(0.00, 0.00, 0.80) }, { "mediumorchid", Color(0.73, 0.33, 0.83) }, @@ -126,7 +123,6 @@ static NamedColor named_colors[] = { { "plum", Color(0.87, 0.63, 0.87) }, { "powderblue", Color(0.69, 0.88, 0.90) }, { "purple", Color(0.63, 0.13, 0.94) }, - { "webpurple", Color(0.50, 0.00, 0.50) }, { "rebeccapurple", Color(0.40, 0.20, 0.60) }, { "red", Color(1.00, 0.00, 0.00) }, { "rosybrown", Color(0.74, 0.56, 0.56) }, @@ -148,9 +144,13 @@ static NamedColor named_colors[] = { { "teal", Color(0.00, 0.50, 0.50) }, { "thistle", Color(0.85, 0.75, 0.85) }, { "tomato", Color(1.00, 0.39, 0.28) }, - { "turquoise", Color(0.25, 0.88, 0.82) }, { "transparent", Color(1.00, 1.00, 1.00, 0.00) }, + { "turquoise", Color(0.25, 0.88, 0.82) }, { "violet", Color(0.93, 0.51, 0.93) }, + { "webgray", Color(0.50, 0.50, 0.50) }, + { "webgreen", Color(0.00, 0.50, 0.00) }, + { "webmaroon", Color(0.50, 0.00, 0.00) }, + { "webpurple", Color(0.50, 0.00, 0.50) }, { "wheat", Color(0.96, 0.87, 0.70) }, { "white", Color(1.00, 1.00, 1.00) }, { "whitesmoke", Color(0.96, 0.96, 0.96) }, diff --git a/core/math/expression.cpp b/core/math/expression.cpp index 426586fc9c..d1f15caa5e 100644 --- a/core/math/expression.cpp +++ b/core/math/expression.cpp @@ -37,688 +37,6 @@ #include "core/os/os.h" #include "core/variant/variant_parser.h" -const char *Expression::func_name[Expression::FUNC_MAX] = { - "sin", - "cos", - "tan", - "sinh", - "cosh", - "tanh", - "asin", - "acos", - "atan", - "atan2", - "sqrt", - "fmod", - "fposmod", - "posmod", - "floor", - "ceil", - "round", - "abs", - "sign", - "pow", - "log", - "exp", - "is_nan", - "is_inf", - "ease", - "step_decimals", - "stepify", - "lerp", - "lerp_angle", - "inverse_lerp", - "range_lerp", - "smoothstep", - "move_toward", - "dectime", - "randomize", - "randi", - "randf", - "randf_range", - "randi_range", - "seed", - "rand_seed", - "deg2rad", - "rad2deg", - "linear2db", - "db2linear", - "polar2cartesian", - "cartesian2polar", - "wrapi", - "wrapf", - "max", - "min", - "clamp", - "nearest_po2", - "weakref", - "convert", - "typeof", - "type_exists", - "char", - "ord", - "str", - "print", - "printerr", - "printraw", - "var2str", - "str2var", - "var2bytes", - "bytes2var", - "color_named", -}; - -Expression::BuiltinFunc Expression::find_function(const String &p_string) { - for (int i = 0; i < FUNC_MAX; i++) { - if (p_string == func_name[i]) { - return BuiltinFunc(i); - } - } - - return FUNC_MAX; -} - -String Expression::get_func_name(BuiltinFunc p_func) { - ERR_FAIL_INDEX_V(p_func, FUNC_MAX, String()); - return func_name[p_func]; -} - -int Expression::get_func_argument_count(BuiltinFunc p_func) { - switch (p_func) { - case MATH_RANDOMIZE: - case MATH_RANDI: - case MATH_RANDF: - return 0; - case MATH_SIN: - case MATH_COS: - case MATH_TAN: - case MATH_SINH: - case MATH_COSH: - case MATH_TANH: - case MATH_ASIN: - case MATH_ACOS: - case MATH_ATAN: - case MATH_SQRT: - case MATH_FLOOR: - case MATH_CEIL: - case MATH_ROUND: - case MATH_ABS: - case MATH_SIGN: - case MATH_LOG: - case MATH_EXP: - case MATH_ISNAN: - case MATH_ISINF: - case MATH_STEP_DECIMALS: - case MATH_SEED: - case MATH_RANDSEED: - case MATH_DEG2RAD: - case MATH_RAD2DEG: - case MATH_LINEAR2DB: - case MATH_DB2LINEAR: - case LOGIC_NEAREST_PO2: - case OBJ_WEAKREF: - case TYPE_OF: - case TEXT_CHAR: - case TEXT_ORD: - case TEXT_STR: - case TEXT_PRINT: - case TEXT_PRINTERR: - case TEXT_PRINTRAW: - case VAR_TO_STR: - case STR_TO_VAR: - case TYPE_EXISTS: - return 1; - case VAR_TO_BYTES: - case BYTES_TO_VAR: - case MATH_ATAN2: - case MATH_FMOD: - case MATH_FPOSMOD: - case MATH_POSMOD: - case MATH_POW: - case MATH_EASE: - case MATH_STEPIFY: - case MATH_RANDF_RANGE: - case MATH_RANDI_RANGE: - case MATH_POLAR2CARTESIAN: - case MATH_CARTESIAN2POLAR: - case LOGIC_MAX: - case LOGIC_MIN: - case TYPE_CONVERT: - case COLORN: - return 2; - case MATH_LERP: - case MATH_LERP_ANGLE: - case MATH_INVERSE_LERP: - case MATH_SMOOTHSTEP: - case MATH_MOVE_TOWARD: - case MATH_DECTIME: - case MATH_WRAP: - case MATH_WRAPF: - case LOGIC_CLAMP: - return 3; - case MATH_RANGE_LERP: - return 5; - case FUNC_MAX: { - } - } - return 0; -} - -#define VALIDATE_ARG_NUM(m_arg) \ - if (!p_inputs[m_arg]->is_num()) { \ - r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; \ - r_error.argument = m_arg; \ - r_error.expected = Variant::FLOAT; \ - return; \ - } - -void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant *r_return, Callable::CallError &r_error, String &r_error_str) { - r_error.error = Callable::CallError::CALL_OK; - switch (p_func) { - case MATH_SIN: { - VALIDATE_ARG_NUM(0); - *r_return = Math::sin((double)*p_inputs[0]); - } break; - case MATH_COS: { - VALIDATE_ARG_NUM(0); - *r_return = Math::cos((double)*p_inputs[0]); - } break; - case MATH_TAN: { - VALIDATE_ARG_NUM(0); - *r_return = Math::tan((double)*p_inputs[0]); - } break; - case MATH_SINH: { - VALIDATE_ARG_NUM(0); - *r_return = Math::sinh((double)*p_inputs[0]); - } break; - case MATH_COSH: { - VALIDATE_ARG_NUM(0); - *r_return = Math::cosh((double)*p_inputs[0]); - } break; - case MATH_TANH: { - VALIDATE_ARG_NUM(0); - *r_return = Math::tanh((double)*p_inputs[0]); - } break; - case MATH_ASIN: { - VALIDATE_ARG_NUM(0); - *r_return = Math::asin((double)*p_inputs[0]); - } break; - case MATH_ACOS: { - VALIDATE_ARG_NUM(0); - *r_return = Math::acos((double)*p_inputs[0]); - } break; - case MATH_ATAN: { - VALIDATE_ARG_NUM(0); - *r_return = Math::atan((double)*p_inputs[0]); - } break; - case MATH_ATAN2: { - VALIDATE_ARG_NUM(0); - VALIDATE_ARG_NUM(1); - *r_return = Math::atan2((double)*p_inputs[0], (double)*p_inputs[1]); - } break; - case MATH_SQRT: { - VALIDATE_ARG_NUM(0); - *r_return = Math::sqrt((double)*p_inputs[0]); - } break; - case MATH_FMOD: { - VALIDATE_ARG_NUM(0); - VALIDATE_ARG_NUM(1); - *r_return = Math::fmod((double)*p_inputs[0], (double)*p_inputs[1]); - } break; - case MATH_FPOSMOD: { - VALIDATE_ARG_NUM(0); - VALIDATE_ARG_NUM(1); - *r_return = Math::fposmod((double)*p_inputs[0], (double)*p_inputs[1]); - } break; - case MATH_POSMOD: { - VALIDATE_ARG_NUM(0); - VALIDATE_ARG_NUM(1); - *r_return = Math::posmod((int)*p_inputs[0], (int)*p_inputs[1]); - } break; - case MATH_FLOOR: { - VALIDATE_ARG_NUM(0); - *r_return = Math::floor((double)*p_inputs[0]); - } break; - case MATH_CEIL: { - VALIDATE_ARG_NUM(0); - *r_return = Math::ceil((double)*p_inputs[0]); - } break; - case MATH_ROUND: { - VALIDATE_ARG_NUM(0); - *r_return = Math::round((double)*p_inputs[0]); - } break; - case MATH_ABS: { - if (p_inputs[0]->get_type() == Variant::INT) { - int64_t i = *p_inputs[0]; - *r_return = ABS(i); - } else if (p_inputs[0]->get_type() == Variant::FLOAT) { - real_t r = *p_inputs[0]; - *r_return = Math::abs(r); - } else { - r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; - r_error.argument = 0; - r_error.expected = Variant::FLOAT; - } - } break; - case MATH_SIGN: { - if (p_inputs[0]->get_type() == Variant::INT) { - int64_t i = *p_inputs[0]; - *r_return = i < 0 ? -1 : (i > 0 ? +1 : 0); - } else if (p_inputs[0]->get_type() == Variant::FLOAT) { - real_t r = *p_inputs[0]; - *r_return = r < 0.0 ? -1.0 : (r > 0.0 ? +1.0 : 0.0); - } else { - r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; - r_error.argument = 0; - r_error.expected = Variant::FLOAT; - } - } break; - case MATH_POW: { - VALIDATE_ARG_NUM(0); - VALIDATE_ARG_NUM(1); - *r_return = Math::pow((double)*p_inputs[0], (double)*p_inputs[1]); - } break; - case MATH_LOG: { - VALIDATE_ARG_NUM(0); - *r_return = Math::log((double)*p_inputs[0]); - } break; - case MATH_EXP: { - VALIDATE_ARG_NUM(0); - *r_return = Math::exp((double)*p_inputs[0]); - } break; - case MATH_ISNAN: { - VALIDATE_ARG_NUM(0); - *r_return = Math::is_nan((double)*p_inputs[0]); - } break; - case MATH_ISINF: { - VALIDATE_ARG_NUM(0); - *r_return = Math::is_inf((double)*p_inputs[0]); - } break; - case MATH_EASE: { - VALIDATE_ARG_NUM(0); - VALIDATE_ARG_NUM(1); - *r_return = Math::ease((double)*p_inputs[0], (double)*p_inputs[1]); - } break; - case MATH_STEP_DECIMALS: { - VALIDATE_ARG_NUM(0); - *r_return = Math::step_decimals((double)*p_inputs[0]); - } break; - case MATH_STEPIFY: { - VALIDATE_ARG_NUM(0); - VALIDATE_ARG_NUM(1); - *r_return = Math::stepify((double)*p_inputs[0], (double)*p_inputs[1]); - } break; - case MATH_LERP: { - VALIDATE_ARG_NUM(0); - VALIDATE_ARG_NUM(1); - VALIDATE_ARG_NUM(2); - *r_return = Math::lerp((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2]); - } break; - case MATH_LERP_ANGLE: { - VALIDATE_ARG_NUM(0); - VALIDATE_ARG_NUM(1); - VALIDATE_ARG_NUM(2); - *r_return = Math::lerp_angle((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2]); - } break; - case MATH_INVERSE_LERP: { - VALIDATE_ARG_NUM(0); - VALIDATE_ARG_NUM(1); - VALIDATE_ARG_NUM(2); - *r_return = Math::inverse_lerp((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2]); - } break; - case MATH_RANGE_LERP: { - VALIDATE_ARG_NUM(0); - VALIDATE_ARG_NUM(1); - VALIDATE_ARG_NUM(2); - VALIDATE_ARG_NUM(3); - VALIDATE_ARG_NUM(4); - *r_return = Math::range_lerp((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2], (double)*p_inputs[3], (double)*p_inputs[4]); - } break; - case MATH_SMOOTHSTEP: { - VALIDATE_ARG_NUM(0); - VALIDATE_ARG_NUM(1); - VALIDATE_ARG_NUM(2); - *r_return = Math::smoothstep((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2]); - } break; - case MATH_MOVE_TOWARD: { - VALIDATE_ARG_NUM(0); - VALIDATE_ARG_NUM(1); - VALIDATE_ARG_NUM(2); - *r_return = Math::move_toward((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2]); - } break; - case MATH_DECTIME: { - VALIDATE_ARG_NUM(0); - VALIDATE_ARG_NUM(1); - VALIDATE_ARG_NUM(2); - *r_return = Math::dectime((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2]); - } break; - case MATH_RANDOMIZE: { - Math::randomize(); - - } break; - case MATH_RANDI: { - *r_return = Math::rand(); - } break; - case MATH_RANDF: { - *r_return = Math::randf(); - } break; - case MATH_RANDF_RANGE: { - VALIDATE_ARG_NUM(0); - VALIDATE_ARG_NUM(1); - *r_return = Math::random((double)*p_inputs[0], (double)*p_inputs[1]); - } break; - case MATH_RANDI_RANGE: { - VALIDATE_ARG_NUM(0); - VALIDATE_ARG_NUM(1); - *r_return = Math::random((int)*p_inputs[0], (int)*p_inputs[1]); - } break; - case MATH_SEED: { - VALIDATE_ARG_NUM(0); - uint64_t seed = *p_inputs[0]; - Math::seed(seed); - - } break; - case MATH_RANDSEED: { - VALIDATE_ARG_NUM(0); - uint64_t seed = *p_inputs[0]; - int ret = Math::rand_from_seed(&seed); - Array reta; - reta.push_back(ret); - reta.push_back(seed); - *r_return = reta; - - } break; - case MATH_DEG2RAD: { - VALIDATE_ARG_NUM(0); - *r_return = Math::deg2rad((double)*p_inputs[0]); - } break; - case MATH_RAD2DEG: { - VALIDATE_ARG_NUM(0); - *r_return = Math::rad2deg((double)*p_inputs[0]); - } break; - case MATH_LINEAR2DB: { - VALIDATE_ARG_NUM(0); - *r_return = Math::linear2db((double)*p_inputs[0]); - } break; - case MATH_DB2LINEAR: { - VALIDATE_ARG_NUM(0); - *r_return = Math::db2linear((double)*p_inputs[0]); - } break; - case MATH_POLAR2CARTESIAN: { - VALIDATE_ARG_NUM(0); - VALIDATE_ARG_NUM(1); - double r = *p_inputs[0]; - double th = *p_inputs[1]; - *r_return = Vector2(r * Math::cos(th), r * Math::sin(th)); - } break; - case MATH_CARTESIAN2POLAR: { - VALIDATE_ARG_NUM(0); - VALIDATE_ARG_NUM(1); - double x = *p_inputs[0]; - double y = *p_inputs[1]; - *r_return = Vector2(Math::sqrt(x * x + y * y), Math::atan2(y, x)); - } break; - case MATH_WRAP: { - VALIDATE_ARG_NUM(0); - VALIDATE_ARG_NUM(1); - VALIDATE_ARG_NUM(2); - *r_return = Math::wrapi((int64_t)*p_inputs[0], (int64_t)*p_inputs[1], (int64_t)*p_inputs[2]); - } break; - case MATH_WRAPF: { - VALIDATE_ARG_NUM(0); - VALIDATE_ARG_NUM(1); - VALIDATE_ARG_NUM(2); - *r_return = Math::wrapf((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2]); - } break; - case LOGIC_MAX: { - if (p_inputs[0]->get_type() == Variant::INT && p_inputs[1]->get_type() == Variant::INT) { - int64_t a = *p_inputs[0]; - int64_t b = *p_inputs[1]; - *r_return = MAX(a, b); - } else { - VALIDATE_ARG_NUM(0); - VALIDATE_ARG_NUM(1); - - real_t a = *p_inputs[0]; - real_t b = *p_inputs[1]; - - *r_return = MAX(a, b); - } - - } break; - case LOGIC_MIN: { - if (p_inputs[0]->get_type() == Variant::INT && p_inputs[1]->get_type() == Variant::INT) { - int64_t a = *p_inputs[0]; - int64_t b = *p_inputs[1]; - *r_return = MIN(a, b); - } else { - VALIDATE_ARG_NUM(0); - VALIDATE_ARG_NUM(1); - - real_t a = *p_inputs[0]; - real_t b = *p_inputs[1]; - - *r_return = MIN(a, b); - } - } break; - case LOGIC_CLAMP: { - if (p_inputs[0]->get_type() == Variant::INT && p_inputs[1]->get_type() == Variant::INT && p_inputs[2]->get_type() == Variant::INT) { - int64_t a = *p_inputs[0]; - int64_t b = *p_inputs[1]; - int64_t c = *p_inputs[2]; - *r_return = CLAMP(a, b, c); - } else { - VALIDATE_ARG_NUM(0); - VALIDATE_ARG_NUM(1); - VALIDATE_ARG_NUM(2); - - real_t a = *p_inputs[0]; - real_t b = *p_inputs[1]; - real_t c = *p_inputs[2]; - - *r_return = CLAMP(a, b, c); - } - } break; - case LOGIC_NEAREST_PO2: { - VALIDATE_ARG_NUM(0); - int64_t num = *p_inputs[0]; - *r_return = next_power_of_2(num); - } break; - case OBJ_WEAKREF: { - if (p_inputs[0]->get_type() != Variant::OBJECT) { - r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; - r_error.argument = 0; - r_error.expected = Variant::OBJECT; - - return; - } - - if (p_inputs[0]->is_ref()) { - REF r = *p_inputs[0]; - if (!r.is_valid()) { - return; - } - - Ref<WeakRef> wref = memnew(WeakRef); - wref->set_ref(r); - *r_return = wref; - } else { - Object *obj = *p_inputs[0]; - if (!obj) { - return; - } - Ref<WeakRef> wref = memnew(WeakRef); - wref->set_obj(obj); - *r_return = wref; - } - - } break; - case TYPE_CONVERT: { - VALIDATE_ARG_NUM(1); - int type = *p_inputs[1]; - if (type < 0 || type >= Variant::VARIANT_MAX) { - r_error_str = RTR("Invalid type argument to convert(), use TYPE_* constants."); - r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; - r_error.argument = 0; - r_error.expected = Variant::INT; - return; - - } else { - *r_return = Variant::construct(Variant::Type(type), p_inputs, 1, r_error); - } - } break; - case TYPE_OF: { - *r_return = p_inputs[0]->get_type(); - - } break; - case TYPE_EXISTS: { - *r_return = ClassDB::class_exists(*p_inputs[0]); - - } break; - case TEXT_CHAR: { - char32_t result[2] = { *p_inputs[0], 0 }; - - *r_return = String(result); - - } break; - case TEXT_ORD: { - if (p_inputs[0]->get_type() != Variant::STRING) { - r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; - r_error.argument = 0; - r_error.expected = Variant::STRING; - - return; - } - - String str = *p_inputs[0]; - - if (str.length() != 1) { - r_error_str = RTR("Expected a string of length 1 (a character)."); - r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; - r_error.argument = 0; - r_error.expected = Variant::STRING; - - return; - } - - *r_return = str.get(0); - - } break; - case TEXT_STR: { - String str = *p_inputs[0]; - - *r_return = str; - - } break; - case TEXT_PRINT: { - String str = *p_inputs[0]; - print_line(str); - - } break; - - case TEXT_PRINTERR: { - String str = *p_inputs[0]; - print_error(str); - - } break; - case TEXT_PRINTRAW: { - String str = *p_inputs[0]; - OS::get_singleton()->print("%s", str.utf8().get_data()); - - } break; - case VAR_TO_STR: { - String vars; - VariantWriter::write_to_string(*p_inputs[0], vars); - *r_return = vars; - } break; - case STR_TO_VAR: { - if (p_inputs[0]->get_type() != Variant::STRING) { - r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; - r_error.argument = 0; - r_error.expected = Variant::STRING; - - return; - } - - VariantParser::StreamString ss; - ss.s = *p_inputs[0]; - - String errs; - int line; - Error err = VariantParser::parse(&ss, *r_return, errs, line); - - if (err != OK) { - r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; - r_error.argument = 0; - r_error.expected = Variant::STRING; - *r_return = "Parse error at line " + itos(line) + ": " + errs; - return; - } - - } break; - case VAR_TO_BYTES: { - PackedByteArray barr; - bool full_objects = *p_inputs[1]; - int len; - Error err = encode_variant(*p_inputs[0], nullptr, len, full_objects); - if (err) { - r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; - r_error.argument = 0; - r_error.expected = Variant::NIL; - r_error_str = "Unexpected error encoding variable to bytes, likely unserializable type found (Object or RID)."; - return; - } - - barr.resize(len); - { - uint8_t *w = barr.ptrw(); - encode_variant(*p_inputs[0], w, len, full_objects); - } - *r_return = barr; - } break; - case BYTES_TO_VAR: { - if (p_inputs[0]->get_type() != Variant::PACKED_BYTE_ARRAY) { - r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; - r_error.argument = 0; - r_error.expected = Variant::PACKED_BYTE_ARRAY; - - return; - } - - PackedByteArray varr = *p_inputs[0]; - bool allow_objects = *p_inputs[1]; - Variant ret; - { - const uint8_t *r = varr.ptr(); - Error err = decode_variant(ret, r, varr.size(), nullptr, allow_objects); - if (err != OK) { - r_error_str = RTR("Not enough bytes for decoding bytes, or invalid format."); - r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; - r_error.argument = 0; - r_error.expected = Variant::PACKED_BYTE_ARRAY; - return; - } - } - - *r_return = ret; - - } break; - case COLORN: { - VALIDATE_ARG_NUM(1); - - Color color = Color::named(*p_inputs[0]); - color.a = *p_inputs[1]; - - *r_return = String(color); - - } break; - default: { - } - } -} - -//////// - static bool _is_number(char32_t c) { return (c >= '0' && c <= '9'); } @@ -1092,18 +410,9 @@ Error Expression::_get_token(Token &r_token) { } else if (id == "self") { r_token.type = TK_SELF; } else { - for (int i = 0; i < Variant::VARIANT_MAX; i++) { - if (id == Variant::get_type_name(Variant::Type(i))) { - r_token.type = TK_BASIC_TYPE; - r_token.value = i; - return OK; - } - } - - BuiltinFunc bifunc = find_function(id); - if (bifunc != FUNC_MAX) { + if (Variant::has_utility_function(id)) { r_token.type = TK_BUILTIN_FUNC; - r_token.value = bifunc; + r_token.value = id; return OK; } @@ -1401,6 +710,8 @@ Expression::ENode *Expression::_parse_expression() { case TK_BUILTIN_FUNC: { //builtin function + StringName func = tk.value; + _get_token(tk); if (tk.type != TK_PARENTHESIS_OPEN) { _set_error("Expected '('"); @@ -1408,7 +719,7 @@ Expression::ENode *Expression::_parse_expression() { } BuiltinFuncNode *bifunc = alloc_node<BuiltinFuncNode>(); - bifunc->func = BuiltinFunc(int(tk.value)); + bifunc->func = func; while (true) { int cofs = str_ofs; @@ -1436,9 +747,11 @@ Expression::ENode *Expression::_parse_expression() { } } - int expected_args = get_func_argument_count(bifunc->func); - if (bifunc->arguments.size() != expected_args) { - _set_error("Builtin func '" + get_func_name(bifunc->func) + "' expects " + itos(expected_args) + " arguments."); + if (!Variant::is_utility_function_vararg(bifunc->func)) { + int expected_args = Variant::get_utility_function_argument_count(bifunc->func); + if (expected_args != bifunc->arguments.size()) { + _set_error("Builtin func '" + String(bifunc->func) + "' expects " + itos(expected_args) + " arguments."); + } } expr = bifunc; @@ -2021,7 +1334,7 @@ bool Expression::_execute(const Array &p_inputs, Object *p_instance, Expression: } Callable::CallError ce; - r_ret = Variant::construct(constructor->data_type, (const Variant **)argp.ptr(), argp.size(), ce); + Variant::construct(constructor->data_type, r_ret, (const Variant **)argp.ptr(), argp.size(), ce); if (ce.error != Callable::CallError::CALL_OK) { r_error_str = vformat(RTR("Invalid arguments to construct '%s'"), Variant::get_type_name(constructor->data_type)); @@ -2047,11 +1360,11 @@ bool Expression::_execute(const Array &p_inputs, Object *p_instance, Expression: argp.write[i] = &arr[i]; } + r_ret = Variant(); //may not return anything Callable::CallError ce; - exec_func(bifunc->func, (const Variant **)argp.ptr(), &r_ret, ce, r_error_str); - + Variant::call_utility_function(bifunc->func, &r_ret, (const Variant **)argp.ptr(), argp.size(), ce); if (ce.error != Callable::CallError::CALL_OK) { - r_error_str = "Builtin Call Failed. " + r_error_str; + r_error_str = "Builtin Call Failed. " + Variant::get_call_error_text(bifunc->func, (const Variant **)argp.ptr(), argp.size(), ce); return true; } @@ -2083,7 +1396,7 @@ bool Expression::_execute(const Array &p_inputs, Object *p_instance, Expression: } Callable::CallError ce; - r_ret = base.call(call->method, (const Variant **)argp.ptr(), argp.size(), ce); + base.call(call->method, (const Variant **)argp.ptr(), argp.size(), r_ret, ce); if (ce.error != Callable::CallError::CALL_OK) { r_error_str = vformat(RTR("On call to '%s':"), String(call->method)); diff --git a/core/math/expression.h b/core/math/expression.h index 991554f4fd..d9cedb8c2c 100644 --- a/core/math/expression.h +++ b/core/math/expression.h @@ -36,87 +36,7 @@ class Expression : public Reference { GDCLASS(Expression, Reference); -public: - enum BuiltinFunc { - MATH_SIN, - MATH_COS, - MATH_TAN, - MATH_SINH, - MATH_COSH, - MATH_TANH, - MATH_ASIN, - MATH_ACOS, - MATH_ATAN, - MATH_ATAN2, - MATH_SQRT, - MATH_FMOD, - MATH_FPOSMOD, - MATH_POSMOD, - MATH_FLOOR, - MATH_CEIL, - MATH_ROUND, - MATH_ABS, - MATH_SIGN, - MATH_POW, - MATH_LOG, - MATH_EXP, - MATH_ISNAN, - MATH_ISINF, - MATH_EASE, - MATH_STEP_DECIMALS, - MATH_STEPIFY, - MATH_LERP, - MATH_LERP_ANGLE, - MATH_INVERSE_LERP, - MATH_RANGE_LERP, - MATH_SMOOTHSTEP, - MATH_MOVE_TOWARD, - MATH_DECTIME, - MATH_RANDOMIZE, - MATH_RANDI, - MATH_RANDF, - MATH_RANDF_RANGE, - MATH_RANDI_RANGE, - MATH_SEED, - MATH_RANDSEED, - MATH_DEG2RAD, - MATH_RAD2DEG, - MATH_LINEAR2DB, - MATH_DB2LINEAR, - MATH_POLAR2CARTESIAN, - MATH_CARTESIAN2POLAR, - MATH_WRAP, - MATH_WRAPF, - LOGIC_MAX, - LOGIC_MIN, - LOGIC_CLAMP, - LOGIC_NEAREST_PO2, - OBJ_WEAKREF, - TYPE_CONVERT, - TYPE_OF, - TYPE_EXISTS, - TEXT_CHAR, - TEXT_ORD, - TEXT_STR, - TEXT_PRINT, - TEXT_PRINTERR, - TEXT_PRINTRAW, - VAR_TO_STR, - STR_TO_VAR, - VAR_TO_BYTES, - BYTES_TO_VAR, - COLORN, - FUNC_MAX - }; - - static int get_func_argument_count(BuiltinFunc p_func); - static String get_func_name(BuiltinFunc p_func); - static void exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant *r_return, Callable::CallError &r_error, String &r_error_str); - static BuiltinFunc find_function(const String &p_string); - private: - static const char *func_name[FUNC_MAX]; - struct Input { Variant::Type type = Variant::NIL; String name; @@ -315,7 +235,7 @@ private: }; struct BuiltinFuncNode : public ENode { - BuiltinFunc func; + StringName func; Vector<ENode *> arguments; BuiltinFuncNode() { type = TYPE_BUILTIN_FUNC; diff --git a/core/math/transform.cpp b/core/math/transform.cpp index d36fd6a63d..733bb4d55e 100644 --- a/core/math/transform.cpp +++ b/core/math/transform.cpp @@ -200,6 +200,13 @@ Transform::Transform(const Basis &p_basis, const Vector3 &p_origin) : origin(p_origin) { } +Transform::Transform(const Vector3 &p_x, const Vector3 &p_y, const Vector3 &p_z, const Vector3 &p_origin) : + origin(p_origin) { + basis.set_axis(0, p_x); + basis.set_axis(1, p_y); + basis.set_axis(2, p_z); +} + Transform::Transform(real_t xx, real_t xy, real_t xz, real_t yx, real_t yy, real_t yz, real_t zx, real_t zy, real_t zz, real_t ox, real_t oy, real_t oz) { basis = Basis(xx, xy, xz, yx, yy, yz, zx, zy, zz); origin = Vector3(ox, oy, oz); diff --git a/core/math/transform.h b/core/math/transform.h index 71847d36ac..c63dbcb989 100644 --- a/core/math/transform.h +++ b/core/math/transform.h @@ -106,9 +106,10 @@ public: operator String() const; - Transform(real_t xx, real_t xy, real_t xz, real_t yx, real_t yy, real_t yz, real_t zx, real_t zy, real_t zz, real_t ox, real_t oy, real_t oz); - Transform(const Basis &p_basis, const Vector3 &p_origin = Vector3()); Transform() {} + Transform(const Basis &p_basis, const Vector3 &p_origin = Vector3()); + Transform(const Vector3 &p_x, const Vector3 &p_y, const Vector3 &p_z, const Vector3 &p_origin); + Transform(real_t xx, real_t xy, real_t xz, real_t yx, real_t yy, real_t yz, real_t zx, real_t zy, real_t zz, real_t ox, real_t oy, real_t oz); }; _FORCE_INLINE_ Vector3 Transform::xform(const Vector3 &p_vector) const { diff --git a/core/math/transform_2d.cpp b/core/math/transform_2d.cpp index 180aeaa0af..00e561f973 100644 --- a/core/math/transform_2d.cpp +++ b/core/math/transform_2d.cpp @@ -251,7 +251,7 @@ Transform2D Transform2D::interpolate_with(const Transform2D &p_transform, real_t real_t dot = v1.dot(v2); - dot = (dot < -1.0) ? -1.0 : ((dot > 1.0) ? 1.0 : dot); //clamp dot to [-1,1] + dot = CLAMP(dot, -1.0, 1.0); Vector2 v; diff --git a/core/math/transform_2d.h b/core/math/transform_2d.h index 46e97abaa7..342623939e 100644 --- a/core/math/transform_2d.h +++ b/core/math/transform_2d.h @@ -128,6 +128,12 @@ struct Transform2D { elements[2][1] = oy; } + Transform2D(const Vector2 &p_x, const Vector2 &p_y, const Vector2 &p_origin) { + elements[0] = p_x; + elements[1] = p_y; + elements[2] = p_origin; + } + Transform2D(real_t p_rot, const Vector2 &p_pos); Transform2D() { elements[0][0] = 1.0; diff --git a/core/object/object.cpp b/core/object/object.cpp index ffec2ae867..c3f49856ca 100644 --- a/core/object/object.cpp +++ b/core/object/object.cpp @@ -1690,7 +1690,8 @@ Variant::Type Object::get_static_property_type_indexed(const Vector<StringName> } Callable::CallError ce; - Variant check = Variant::construct(t, nullptr, 0, ce); + Variant check; + Variant::construct(t, check, nullptr, 0, ce); for (int i = 1; i < p_path.size(); i++) { if (check.get_type() == Variant::OBJECT || check.get_type() == Variant::DICTIONARY || check.get_type() == Variant::ARRAY) { diff --git a/core/os/file_access.cpp b/core/os/file_access.cpp index ef3eb6800a..fd3c6f8806 100644 --- a/core/os/file_access.cpp +++ b/core/os/file_access.cpp @@ -51,7 +51,7 @@ FileAccess *FileAccess::create(AccessType p_access) { } bool FileAccess::exists(const String &p_name) { - if (PackedData::get_singleton() && PackedData::get_singleton()->has_path(p_name)) { + if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && PackedData::get_singleton()->has_path(p_name)) { return true; } @@ -456,7 +456,7 @@ void FileAccess::store_double(double p_dest) { } uint64_t FileAccess::get_modified_time(const String &p_file) { - if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && PackedData::get_singleton()->has_path(p_file)) { + if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && (PackedData::get_singleton()->has_path(p_file) || PackedData::get_singleton()->has_directory(p_file))) { return 0; } @@ -469,7 +469,7 @@ uint64_t FileAccess::get_modified_time(const String &p_file) { } uint32_t FileAccess::get_unix_permissions(const String &p_file) { - if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && PackedData::get_singleton()->has_path(p_file)) { + if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && (PackedData::get_singleton()->has_path(p_file) || PackedData::get_singleton()->has_directory(p_file))) { return 0; } @@ -482,6 +482,10 @@ uint32_t FileAccess::get_unix_permissions(const String &p_file) { } Error FileAccess::set_unix_permissions(const String &p_file, uint32_t p_permissions) { + if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && (PackedData::get_singleton()->has_path(p_file) || PackedData::get_singleton()->has_directory(p_file))) { + return ERR_UNAVAILABLE; + } + FileAccess *fa = create_for_path(p_file); ERR_FAIL_COND_V_MSG(!fa, ERR_CANT_CREATE, "Cannot create FileAccess for path '" + p_file + "'."); diff --git a/core/register_core_types.cpp b/core/register_core_types.cpp index c56de8ef93..7e32f215e7 100644 --- a/core/register_core_types.cpp +++ b/core/register_core_types.cpp @@ -43,6 +43,7 @@ #include "core/io/dtls_server.h" #include "core/io/http_client.h" #include "core/io/image_loader.h" +#include "core/io/json.h" #include "core/io/marshalls.h" #include "core/io/multiplayer_api.h" #include "core/io/networked_multiplayer_peer.h" @@ -96,12 +97,6 @@ extern Mutex _global_mutex; extern void register_global_constants(); extern void unregister_global_constants(); -extern void register_variant_methods(); -extern void unregister_variant_methods(); -extern void register_variant_operators(); -extern void unregister_variant_operators(); -extern void register_variant_setters_getters(); -extern void unregister_variant_setters_getters(); void register_core_types() { //consistency check @@ -114,9 +109,8 @@ void register_core_types() { ResourceLoader::initialize(); register_global_constants(); - register_variant_methods(); - register_variant_operators(); - register_variant_setters_getters(); + + Variant::register_types(); CoreStringNames::create(); @@ -204,6 +198,7 @@ void register_core_types() { ClassDB::register_class<_Semaphore>(); ClassDB::register_class<XMLParser>(); + ClassDB::register_class<JSONParser>(); ClassDB::register_class<ConfigFile>(); @@ -323,9 +318,8 @@ void unregister_core_types() { ClassDB::cleanup_defaults(); ObjectDB::cleanup(); - unregister_variant_setters_getters(); - unregister_variant_operators(); - unregister_variant_methods(); + Variant::unregister_types(); + unregister_global_constants(); ClassDB::cleanup(); diff --git a/core/string/ustring.h b/core/string/ustring.h index 35475a2124..b46733ab66 100644 --- a/core/string/ustring.h +++ b/core/string/ustring.h @@ -433,10 +433,10 @@ public: /** * The constructors must not depend on other overloads */ - /* String(char32_t p_char);*/ _FORCE_INLINE_ String() {} _FORCE_INLINE_ String(const String &p_str) { _cowdata._ref(p_str._cowdata); } + String &operator=(const String &p_str) { _cowdata._ref(p_str._cowdata); return *this; @@ -536,4 +536,24 @@ String RTRN(const String &p_text, const String &p_text_plural, int p_n, const St bool is_symbol(char32_t c); bool select_word(const String &p_s, int p_col, int &r_beg, int &r_end); +_FORCE_INLINE_ void sarray_add_str(Vector<String> &arr) { +} + +_FORCE_INLINE_ void sarray_add_str(Vector<String> &arr, const String &p_str) { + arr.push_back(p_str); +} + +template <class... P> +_FORCE_INLINE_ void sarray_add_str(Vector<String> &arr, const String &p_str, P... p_args) { + arr.push_back(p_str); + sarray_add_str(arr, p_args...); +} + +template <class... P> +_FORCE_INLINE_ Vector<String> sarray(P... p_args) { + Vector<String> arr; + sarray_add_str(arr, p_args...); + return arr; +} + #endif // USTRING_H diff --git a/core/variant/array.cpp b/core/variant/array.cpp index 64e31189de..79bc01b89c 100644 --- a/core/variant/array.cpp +++ b/core/variant/array.cpp @@ -165,7 +165,7 @@ void Array::_assign(const Array &p_array) { } else if (Variant::can_convert_strict(src_val.get_type(), _p->typed.type)) { Variant *ptr = &src_val; Callable::CallError ce; - new_array.write[i] = Variant::construct(_p->typed.type, (const Variant **)&ptr, 1, ce, true); + Variant::construct(_p->typed.type, new_array.write[i], (const Variant **)&ptr, 1, ce); if (ce.error != Callable::CallError::CALL_OK) { ERR_FAIL_MSG("Unable to convert array index " + itos(i) + " from '" + Variant::get_type_name(src_val.get_type()) + "' to '" + Variant::get_type_name(_p->typed.type) + "'."); } @@ -192,6 +192,11 @@ void Array::push_back(const Variant &p_value) { _p->array.push_back(p_value); } +void Array::append_array(const Array &p_array) { + ERR_FAIL_COND(!_p->typed.validate(p_array, "append_array")); + _p->array.append_array(p_array._p->array); +} + Error Array::resize(int p_new_size) { return _p->array.resize(p_new_size); } diff --git a/core/variant/array.h b/core/variant/array.h index b37d600abd..e01ac13168 100644 --- a/core/variant/array.h +++ b/core/variant/array.h @@ -68,6 +68,7 @@ public: void push_back(const Variant &p_value); _FORCE_INLINE_ void append(const Variant &p_value) { push_back(p_value); } //for python compatibility + void append_array(const Array &p_array); Error resize(int p_new_size); void insert(int p_pos, const Variant &p_value); diff --git a/core/variant/binder_common.h b/core/variant/binder_common.h index e72f76c5e1..2e38ce5b06 100644 --- a/core/variant/binder_common.h +++ b/core/variant/binder_common.h @@ -650,6 +650,39 @@ void call_with_variant_args_retc_static_helper(T *p_instance, R (*p_method)(T *, (void)p_args; } +template <class T, class R, class... P> +void call_with_variant_args_retc_static_helper_dv(T *p_instance, R (*p_method)(T *, P...), const Variant **p_args, int p_argcount, Variant &r_ret, const Vector<Variant> &default_values, Callable::CallError &r_error) { +#ifdef DEBUG_ENABLED + if ((size_t)p_argcount > sizeof...(P)) { + r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS; + r_error.argument = sizeof...(P); + return; + } +#endif + + int32_t missing = (int32_t)sizeof...(P) - (int32_t)p_argcount; + + int32_t dvs = default_values.size(); +#ifdef DEBUG_ENABLED + if (missing > dvs) { + r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; + r_error.argument = sizeof...(P); + return; + } +#endif + + const Variant *args[sizeof...(P) == 0 ? 1 : sizeof...(P)]; //avoid zero sized array + for (int32_t i = 0; i < (int32_t)sizeof...(P); i++) { + if (i < p_argcount) { + args[i] = p_args[i]; + } else { + args[i] = &default_values[i - p_argcount + (dvs - missing)]; + } + } + + call_with_variant_args_retc_static_helper(p_instance, p_method, args, r_ret, r_error, BuildIndexSequence<sizeof...(P)>{}); +} + #if defined(DEBUG_METHODS_ENABLED) && defined(__GNUC__) && !defined(__clang__) #pragma GCC diagnostic pop #endif diff --git a/core/variant/type_info.h b/core/variant/type_info.h index b9ae88d97c..ce7c2bfe14 100644 --- a/core/variant/type_info.h +++ b/core/variant/type_info.h @@ -153,7 +153,7 @@ MAKE_TYPE_INFO(Transform, Variant::TRANSFORM) MAKE_TYPE_INFO(Color, Variant::COLOR) MAKE_TYPE_INFO(StringName, Variant::STRING_NAME) MAKE_TYPE_INFO(NodePath, Variant::NODE_PATH) -MAKE_TYPE_INFO(RID, Variant::_RID) +MAKE_TYPE_INFO(RID, Variant::RID) MAKE_TYPE_INFO(Callable, Variant::CALLABLE) MAKE_TYPE_INFO(Signal, Variant::SIGNAL) MAKE_TYPE_INFO(Dictionary, Variant::DICTIONARY) diff --git a/core/variant/typed_array.h b/core/variant/typed_array.h index 0f3985b2f1..ff42cb687e 100644 --- a/core/variant/typed_array.h +++ b/core/variant/typed_array.h @@ -105,7 +105,7 @@ MAKE_TYPED_ARRAY(Transform, Variant::TRANSFORM) MAKE_TYPED_ARRAY(Color, Variant::COLOR) MAKE_TYPED_ARRAY(StringName, Variant::STRING_NAME) MAKE_TYPED_ARRAY(NodePath, Variant::NODE_PATH) -MAKE_TYPED_ARRAY(RID, Variant::_RID) +MAKE_TYPED_ARRAY(RID, Variant::RID) MAKE_TYPED_ARRAY(Callable, Variant::CALLABLE) MAKE_TYPED_ARRAY(Signal, Variant::SIGNAL) MAKE_TYPED_ARRAY(Dictionary, Variant::DICTIONARY) @@ -207,7 +207,7 @@ MAKE_TYPED_ARRAY_INFO(Transform, Variant::TRANSFORM) MAKE_TYPED_ARRAY_INFO(Color, Variant::COLOR) MAKE_TYPED_ARRAY_INFO(StringName, Variant::STRING_NAME) MAKE_TYPED_ARRAY_INFO(NodePath, Variant::NODE_PATH) -MAKE_TYPED_ARRAY_INFO(RID, Variant::_RID) +MAKE_TYPED_ARRAY_INFO(RID, Variant::RID) MAKE_TYPED_ARRAY_INFO(Callable, Variant::CALLABLE) MAKE_TYPED_ARRAY_INFO(Signal, Variant::SIGNAL) MAKE_TYPED_ARRAY_INFO(Dictionary, Variant::DICTIONARY) diff --git a/core/variant/variant.cpp b/core/variant/variant.cpp index 126420bd02..741d05c139 100644 --- a/core/variant/variant.cpp +++ b/core/variant/variant.cpp @@ -109,7 +109,7 @@ String Variant::get_type_name(Variant::Type p_type) { return "Color"; } break; - case _RID: { + case RID: { return "RID"; } break; case OBJECT: { @@ -342,7 +342,7 @@ bool Variant::can_convert(Variant::Type p_type_from, Variant::Type p_type_to) { } break; - case _RID: { + case RID: { static const Type valid[] = { OBJECT, NIL @@ -649,7 +649,7 @@ bool Variant::can_convert_strict(Variant::Type p_type_from, Variant::Type p_type } break; - case _RID: { + case RID: { static const Type valid[] = { OBJECT, NIL @@ -891,8 +891,8 @@ bool Variant::is_zero() const { return *reinterpret_cast<const Color *>(_data._mem) == Color(); } break; - case _RID: { - return *reinterpret_cast<const RID *>(_data._mem) == RID(); + case RID: { + return *reinterpret_cast<const ::RID *>(_data._mem) == ::RID(); } break; case OBJECT: { return _get_obj().obj == nullptr; @@ -1109,8 +1109,8 @@ void Variant::reference(const Variant &p_variant) { memnew_placement(_data._mem, Color(*reinterpret_cast<const Color *>(p_variant._data._mem))); } break; - case _RID: { - memnew_placement(_data._mem, RID(*reinterpret_cast<const RID *>(p_variant._data._mem))); + case RID: { + memnew_placement(_data._mem, ::RID(*reinterpret_cast<const ::RID *>(p_variant._data._mem))); } break; case OBJECT: { memnew_placement(_data._mem, ObjData); @@ -1266,7 +1266,7 @@ void Variant::zero() { } } -void Variant::clear() { +void Variant::_clear_internal() { switch (type) { case STRING: { reinterpret_cast<String *>(_data._mem)->~String(); @@ -1311,9 +1311,11 @@ void Variant::clear() { _get_obj().obj = nullptr; _get_obj().id = ObjectID(); } break; - case _RID: { + case RID: { // not much need probably - reinterpret_cast<RID *>(_data._mem)->~RID(); + // Can't seem to use destructor + scoping operator, so hack. + typedef ::RID RID_Class; + reinterpret_cast<RID_Class *>(_data._mem)->~RID_Class(); } break; case CALLABLE: { reinterpret_cast<Callable *>(_data._mem)->~Callable(); @@ -1358,8 +1360,6 @@ void Variant::clear() { default: { } /* not needed */ } - - type = NIL; } Variant::operator signed int() const { @@ -1849,8 +1849,8 @@ String Variant::stringify(List<const void *> &stack) const { const Signal &s = *reinterpret_cast<const Signal *>(_data._mem); return s; } break; - case _RID: { - const RID &s = *reinterpret_cast<const RID *>(_data._mem); + case RID: { + const ::RID &s = *reinterpret_cast<const ::RID *>(_data._mem); return "RID(" + itos(s.get_id()) + ")"; } break; default: { @@ -2041,25 +2041,25 @@ Variant::operator NodePath() const { } } -Variant::operator RID() const { - if (type == _RID) { - return *reinterpret_cast<const RID *>(_data._mem); +Variant::operator ::RID() const { + if (type == RID) { + return *reinterpret_cast<const ::RID *>(_data._mem); } else if (type == OBJECT && _get_obj().obj == nullptr) { - return RID(); + return ::RID(); } else if (type == OBJECT && _get_obj().obj) { #ifdef DEBUG_ENABLED if (EngineDebugger::is_active()) { - ERR_FAIL_COND_V_MSG(ObjectDB::get_instance(_get_obj().id) == nullptr, RID(), "Invalid pointer (object was freed)."); + ERR_FAIL_COND_V_MSG(ObjectDB::get_instance(_get_obj().id) == nullptr, ::RID(), "Invalid pointer (object was freed)."); } #endif Callable::CallError ce; Variant ret = _get_obj().obj->call(CoreStringNames::get_singleton()->get_rid, nullptr, 0, ce); - if (ce.error == Callable::CallError::CALL_OK && ret.get_type() == Variant::_RID) { + if (ce.error == Callable::CallError::CALL_OK && ret.get_type() == Variant::RID) { return ret; } - return RID(); + return ::RID(); } else { - return RID(); + return ::RID(); } } @@ -2263,9 +2263,9 @@ Variant::operator Vector<Color>() const { /* helpers */ -Variant::operator Vector<RID>() const { +Variant::operator Vector<::RID>() const { Array va = operator Array(); - Vector<RID> rids; + Vector<::RID> rids; rids.resize(va.size()); for (int i = 0; i < rids.size(); i++) { rids.write[i] = va[i]; @@ -2520,9 +2520,9 @@ Variant::Variant(const NodePath &p_node_path) { memnew_placement(_data._mem, NodePath(p_node_path)); } -Variant::Variant(const RID &p_rid) { - type = _RID; - memnew_placement(_data._mem, RID(p_rid)); +Variant::Variant(const ::RID &p_rid) { + type = RID; + memnew_placement(_data._mem, ::RID(p_rid)); } Variant::Variant(const Object *p_object) { @@ -2580,7 +2580,7 @@ Variant::Variant(const Vector<Plane> &p_array) { } } -Variant::Variant(const Vector<RID> &p_array) { +Variant::Variant(const Vector<::RID> &p_array) { type = ARRAY; Array *rid_array = memnew_placement(_data._mem, Array); @@ -2753,8 +2753,8 @@ void Variant::operator=(const Variant &p_variant) { 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); + case RID: { + *reinterpret_cast<::RID *>(_data._mem) = *reinterpret_cast<const ::RID *>(p_variant._data._mem); } break; case OBJECT: { if (_get_obj().id.is_reference()) { @@ -2955,8 +2955,8 @@ uint32_t Variant::hash() const { return hash_djb2_one_float(reinterpret_cast<const Color *>(_data._mem)->a, hash); } break; - case _RID: { - return hash_djb2_one_64(reinterpret_cast<const RID *>(_data._mem)->get_id()); + case RID: { + return hash_djb2_one_64(reinterpret_cast<const ::RID *>(_data._mem)->get_id()); } break; case OBJECT: { return hash_djb2_one_64(make_uint64_t(_get_obj().obj)); @@ -3401,7 +3401,8 @@ Variant Variant::call(const StringName &p_method, VARIANT_ARG_DECLARE) { Callable::CallError error; - Variant ret = call(p_method, argptr, argc, error); + Variant ret; + call(p_method, argptr, argc, ret, error); switch (error.error) { case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT: { @@ -3435,6 +3436,30 @@ String Variant::get_construct_string() const { return vars; } +String Variant::get_call_error_text(const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce) { + String err_text; + + if (ce.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) { + int errorarg = ce.argument; + if (p_argptrs) { + err_text = "Cannot convert argument " + itos(errorarg + 1) + " from " + Variant::get_type_name(p_argptrs[errorarg]->get_type()) + " to " + Variant::get_type_name(Variant::Type(ce.expected)) + "."; + } else { + err_text = "Cannot convert argument " + itos(errorarg + 1) + " from [missing argptr, type unknown] to " + Variant::get_type_name(Variant::Type(ce.expected)) + "."; + } + } else if (ce.error == Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) { + err_text = "Method expected " + itos(ce.argument) + " arguments, but called with " + itos(p_argcount) + "."; + } else if (ce.error == Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) { + err_text = "Method expected " + itos(ce.argument) + " arguments, but called with " + itos(p_argcount) + "."; + } else if (ce.error == Callable::CallError::CALL_ERROR_INVALID_METHOD) { + err_text = "Method not found."; + } else if (ce.error == Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL) { + err_text = "Instance is null"; + } else if (ce.error == Callable::CallError::CALL_OK) { + return "Call OK"; + } + return "'" + String(p_method) + "': " + err_text; +} + String Variant::get_call_error_text(Object *p_base, const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce) { String err_text; @@ -3519,3 +3544,18 @@ String vformat(const String &p_text, const Variant &p1, const Variant &p2, const return fmt; } + +void Variant::register_types() { + _register_variant_operators(); + _register_variant_methods(); + _register_variant_setters_getters(); + _register_variant_constructors(); + _register_variant_utility_functions(); +} +void Variant::unregister_types() { + _unregister_variant_operators(); + _unregister_variant_methods(); + _unregister_variant_setters_getters(); + _unregister_variant_constructors(); + _unregister_variant_utility_functions(); +} diff --git a/core/variant/variant.h b/core/variant/variant.h index c264bbcd48..1a684eeea0 100644 --- a/core/variant/variant.h +++ b/core/variant/variant.h @@ -97,7 +97,7 @@ public: COLOR, STRING_NAME, NODE_PATH, - _RID, + RID, OBJECT, CALLABLE, SIGNAL, @@ -207,7 +207,68 @@ private: } _data alignas(8); void reference(const Variant &p_variant); - void clear(); + + void _clear_internal(); + + _FORCE_INLINE_ void clear() { + static const bool needs_deinit[Variant::VARIANT_MAX] = { + false, //NIL, + false, //BOOL, + false, //INT, + false, //FLOAT, + true, //STRING, + false, //VECTOR2, + false, //VECTOR2I, + false, //RECT2, + false, //RECT2I, + false, //VECTOR3, + false, //VECTOR3I, + true, //TRANSFORM2D, + false, //PLANE, + false, //QUAT, + true, //AABB, + true, //BASIS, + true, //TRANSFORM, + + // misc types + false, //COLOR, + true, //STRING_NAME, + true, //NODE_PATH, + false, //RID, + true, //OBJECT, + true, //CALLABLE, + true, //SIGNAL, + true, //DICTIONARY, + true, //ARRAY, + + // typed arrays + true, //PACKED_BYTE_ARRAY, + true, //PACKED_INT32_ARRAY, + true, //PACKED_INT64_ARRAY, + true, //PACKED_FLOAT32_ARRAY, + true, //PACKED_FLOAT64_ARRAY, + true, //PACKED_STRING_ARRAY, + true, //PACKED_VECTOR2_ARRAY, + true, //PACKED_VECTOR3_ARRAY, + true, //PACKED_COLOR_ARRAY, + }; + + if (unlikely(needs_deinit[type])) { //make it fast for types that dont need deinit + _clear_internal(); + } + type = NIL; + } + + static void _register_variant_operators(); + static void _unregister_variant_operators(); + static void _register_variant_methods(); + static void _unregister_variant_methods(); + static void _register_variant_setters_getters(); + static void _unregister_variant_setters_getters(); + static void _register_variant_constructors(); + static void _unregister_variant_constructors(); + static void _register_variant_utility_functions(); + static void _unregister_variant_utility_functions(); public: _FORCE_INLINE_ Type get_type() const { @@ -266,7 +327,7 @@ public: operator Color() const; operator NodePath() const; - operator RID() const; + operator ::RID() const; operator Object *() const; operator Node *() const; @@ -291,7 +352,7 @@ public: operator Vector<Variant>() const; operator Vector<StringName>() const; - operator Vector<RID>() const; + operator Vector<::RID>() const; operator Vector<Vector2>() const; // some core type enums to convert to @@ -338,7 +399,7 @@ public: Variant(const Transform &p_transform); Variant(const Color &p_color); Variant(const NodePath &p_node_path); - Variant(const RID &p_rid); + Variant(const ::RID &p_rid); Variant(const Object *p_object); Variant(const Callable &p_callable); Variant(const Signal &p_signal); @@ -358,7 +419,7 @@ public: Variant(const Vector<Variant> &p_array); Variant(const Vector<StringName> &p_array); - Variant(const Vector<RID> &p_array); // helper + Variant(const Vector<::RID> &p_array); // helper Variant(const Vector<Vector2> &p_array); // helper Variant(const IP_Address &p_address); @@ -408,7 +469,7 @@ public: return res; } - Variant::Type get_operator_return_type(Operator p_operator, Type p_type_a, Type p_type_b); + static Variant::Type get_operator_return_type(Operator p_operator, Type p_type_a, Type p_type_b); typedef void (*ValidatedOperatorEvaluator)(const Variant *left, const Variant *right, Variant *r_ret); static ValidatedOperatorEvaluator get_validated_operator_evaluator(Operator p_operator, Type p_type_a, Type p_type_b); #ifdef PTRCALL_ENABLED @@ -421,61 +482,53 @@ public: static void blend(const Variant &a, const Variant &b, float c, Variant &r_dst); static void interpolate(const Variant &a, const Variant &b, float c, Variant &r_dst); - class InternalMethod { -#ifdef DEBUG_ENABLED - protected: - StringName method_name; - Variant::Type base_type; -#endif - public: - enum Flags { - FLAG_IS_CONST = 1, - FLAG_RETURNS_VARIANT = 2, - FLAG_NO_PTRCALL = 4, - FLAG_VARARGS = 8 - }; + /* Built-In Methods */ - virtual int get_argument_count() const = 0; - virtual Type get_argument_type(int p_arg) const = 0; - virtual Type get_return_type() const = 0; - virtual uint32_t get_flags() const = 0; + typedef void (*ValidatedBuiltInMethod)(Variant *base, const Variant **p_args, int p_argcount, Variant *r_ret); + typedef void (*PTRBuiltInMethod)(void *p_base, const void **p_args, void *r_ret, int p_argcount); -#ifdef DEBUG_ENABLED - virtual String get_argument_name(int p_arg) const = 0; - StringName get_name() const { - return method_name; - } - Variant::Type get_base_type() const { - return base_type; - } -#endif - virtual Vector<Variant> get_default_arguments() const = 0; - virtual void call(Variant *base, const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error) = 0; - virtual void validated_call(Variant *base, const Variant **p_args, Variant *r_ret) = 0; -#ifdef PTRCALL_ENABLED - virtual void ptrcall(void *p_base, const void **p_args, void *r_ret) = 0; -#endif - virtual ~InternalMethod() {} - }; + static bool has_builtin_method(Variant::Type p_type, const StringName &p_method); + + static ValidatedBuiltInMethod get_validated_builtin_method(Variant::Type p_type, const StringName &p_method); + static PTRBuiltInMethod get_ptr_builtin_method(Variant::Type p_type, const StringName &p_method); - static InternalMethod *get_internal_method(Type p_type, const StringName &p_method_name); + static int get_builtin_method_argument_count(Variant::Type p_type, const StringName &p_method); + static Variant::Type get_builtin_method_argument_type(Variant::Type p_type, const StringName &p_method, int p_argument); + static String get_builtin_method_argument_name(Variant::Type p_type, const StringName &p_method, int p_argument); + static Vector<Variant> get_builtin_method_default_arguments(Variant::Type p_type, const StringName &p_method); + static bool has_builtin_method_return_value(Variant::Type p_type, const StringName &p_method); + static Variant::Type get_builtin_method_return_type(Variant::Type p_type, const StringName &p_method); + static bool is_builtin_method_const(Variant::Type p_type, const StringName &p_method); + static bool is_builtin_method_vararg(Variant::Type p_type, const StringName &p_method); + static void get_builtin_method_list(Variant::Type p_type, List<StringName> *p_list); - void call_ptr(const StringName &p_method, const Variant **p_args, int p_argcount, Variant *r_ret, Callable::CallError &r_error); - Variant call(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error); + void call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error); Variant call(const StringName &p_method, const Variant &p_arg1 = Variant(), const Variant &p_arg2 = Variant(), const Variant &p_arg3 = Variant(), const Variant &p_arg4 = Variant(), const Variant &p_arg5 = Variant()); + static String get_call_error_text(const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce); static String get_call_error_text(Object *p_base, const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce); static String get_callable_error_text(const Callable &p_callable, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce); - static Variant construct(const Variant::Type, const Variant **p_args, int p_argcount, Callable::CallError &r_error, bool p_strict = true); - + //dynamic (includes Object) void get_method_list(List<MethodInfo> *p_list) const; bool has_method(const StringName &p_method) const; - static Vector<Variant::Type> get_method_argument_types(Variant::Type p_type, const StringName &p_method); - static Vector<Variant> get_method_default_arguments(Variant::Type p_type, const StringName &p_method); - static Variant::Type get_method_return_type(Variant::Type p_type, const StringName &p_method, bool *r_has_return = nullptr); - static Vector<StringName> get_method_argument_names(Variant::Type p_type, const StringName &p_method); - static bool is_method_const(Variant::Type p_type, const StringName &p_method); + + /* Constructors */ + + typedef void (*ValidatedConstructor)(Variant &r_base, const Variant **p_args); + typedef void (*PTRConstructor)(void *base, const void **p_args); + + static int get_constructor_count(Variant::Type p_type); + static ValidatedConstructor get_validated_constructor(Variant::Type p_type, int p_constructor); + static PTRConstructor get_ptr_constructor(Variant::Type p_type, int p_constructor); + static int get_constructor_argument_count(Variant::Type p_type, int p_constructor); + static Variant::Type get_constructor_argument_type(Variant::Type p_type, int p_constructor, int p_argument); + static String get_constructor_argument_name(Variant::Type p_type, int p_constructor, int p_argument); + static void construct(Variant::Type, Variant &base, const Variant **p_args, int p_argcount, Callable::CallError &r_error); + + static void get_constructor_list(Type p_type, List<MethodInfo> *r_list); //convenience + + /* Properties */ void set_named(const StringName &p_member, const Variant &p_value, bool &r_valid); Variant get_named(const StringName &p_member, bool &r_valid) const; @@ -496,6 +549,8 @@ public: static PTRSetter get_member_ptr_setter(Variant::Type p_type, const StringName &p_member); static PTRGetter get_member_ptr_getter(Variant::Type p_type, const StringName &p_member); + /* Indexing */ + static bool has_indexing(Variant::Type p_type); static Variant::Type get_indexed_element_type(Variant::Type p_type); @@ -516,6 +571,8 @@ public: uint64_t get_indexed_size() const; + /* Keying */ + static bool is_keyed(Variant::Type p_type); typedef void (*ValidatedKeyedSetter)(Variant *base, const Variant *key, const Variant *value, bool &valid); @@ -538,6 +595,8 @@ public: Variant get_keyed(const Variant &p_key, bool &r_valid) const; bool has_key(const Variant &p_key, bool &r_valid) const; + /* Generic */ + void set(const Variant &p_index, const Variant &p_value, bool *r_valid = nullptr); Variant get(const Variant &p_index, bool *r_valid = nullptr) const; bool in(const Variant &p_index, bool *r_valid = nullptr) const; @@ -548,6 +607,32 @@ public: void get_property_list(List<PropertyInfo> *p_list) const; + static void call_utility_function(const StringName &p_name, Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error); + static bool has_utility_function(const StringName &p_name); + + typedef void (*ValidatedUtilityFunction)(Variant *r_ret, const Variant **p_args, int p_argcount); + typedef void (*PTRUtilityFunction)(void *r_ret, const void **p_args, int p_argcount); + + static ValidatedUtilityFunction get_validated_utility_function(const StringName &p_name); + static PTRUtilityFunction get_ptr_utility_function(const StringName &p_name); + + enum UtilityFunctionType { + UTILITY_FUNC_TYPE_MATH, + UTILITY_FUNC_TYPE_RANDOM, + UTILITY_FUNC_TYPE_GENERAL, + }; + + static UtilityFunctionType get_utility_function_type(const StringName &p_name); + + static int get_utility_function_argument_count(const StringName &p_name); + static Variant::Type get_utility_function_argument_type(const StringName &p_name, int p_arg); + static String get_utility_function_argument_name(const StringName &p_name, int p_arg); + static bool has_utility_function_return_value(const StringName &p_name); + static Variant::Type get_utility_function_return_type(const StringName &p_name); + static bool is_utility_function_vararg(const StringName &p_name); + + static void get_utility_function_list(List<StringName> *r_functions); + //argsVariant call() bool operator==(const Variant &p_variant) const; @@ -560,7 +645,6 @@ public: String stringify(List<const void *> &stack) const; void static_assign(const Variant &p_variant); - static void get_constructor_list(Variant::Type p_type, List<MethodInfo> *p_list); static void get_constants_for_type(Variant::Type p_type, List<StringName> *p_constants); static bool has_constant(Variant::Type p_type, const StringName &p_value); static Variant get_constant_value(Variant::Type p_type, const StringName &p_value, bool *r_valid = nullptr); @@ -573,12 +657,13 @@ public: void operator=(const Variant &p_variant); // only this is enough for all the other types + static void register_types(); + static void unregister_types(); + Variant(const Variant &p_variant); _FORCE_INLINE_ Variant() {} _FORCE_INLINE_ ~Variant() { - if (type != Variant::NIL) { - clear(); - } + clear(); } }; diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp index 37424f5ff9..4cb8457ccd 100644 --- a/core/variant/variant_call.cpp +++ b/core/variant/variant_call.cpp @@ -36,521 +36,357 @@ #include "core/io/compression.h" #include "core/object/class_db.h" #include "core/os/os.h" +#include "core/templates/local_vector.h" #include "core/templates/oa_hash_map.h" -_FORCE_INLINE_ void sarray_add_str(Vector<String> &arr) { -} - -_FORCE_INLINE_ void sarray_add_str(Vector<String> &arr, const String &p_str) { - arr.push_back(p_str); -} - -template <class... P> -_FORCE_INLINE_ void sarray_add_str(Vector<String> &arr, const String &p_str, P... p_args) { - arr.push_back(p_str); - sarray_add_str(arr, p_args...); -} - -template <class... P> -_FORCE_INLINE_ Vector<String> sarray(P... p_args) { - Vector<String> arr; - sarray_add_str(arr, p_args...); - return arr; -} - typedef void (*VariantFunc)(Variant &r_ret, Variant &p_self, const Variant **p_args); typedef void (*VariantConstructFunc)(Variant &r_ret, const Variant **p_args); -struct _VariantCall { - template <class T, class... P> - class InternalMethod : public Variant::InternalMethod { - public: - void (T::*method)(P...); - Vector<Variant> default_values; -#ifdef DEBUG_ENABLED - Vector<String> argument_names; -#endif - - virtual int get_argument_count() const { - return sizeof...(P); - } - virtual Variant::Type get_argument_type(int p_arg) const { - return call_get_argument_type<P...>(p_arg); - } -#ifdef DEBUG_ENABLED - virtual String get_argument_name(int p_arg) const { - ERR_FAIL_INDEX_V(p_arg, argument_names.size(), String()); - return argument_names[p_arg]; - } -#endif - virtual Vector<Variant> get_default_arguments() const { - return default_values; - } - - virtual Variant::Type get_return_type() const { - return Variant::NIL; - } - virtual uint32_t get_flags() const { - return 0; - } - - virtual void call(Variant *base, const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error) { - call_with_variant_args_dv(VariantGetInternalPtr<T>::get_ptr(base), method, p_args, p_argcount, r_error, default_values); - } - - virtual void validated_call(Variant *base, const Variant **p_args, Variant *r_ret) { - call_with_validated_variant_args(base, method, p_args); - } +template <class T> +struct TypeAdjust { + _FORCE_INLINE_ static void adjust(Variant *r_ret) { + VariantTypeChanger<typename GetSimpleTypeT<T>::type_t>::change(r_ret); + } +}; -#ifdef PTRCALL_ENABLED - virtual void ptrcall(void *p_base, const void **p_args, void *r_ret) { - call_with_ptr_args<T, P...>(reinterpret_cast<T *>(p_base), method, p_args); - } -#endif - InternalMethod(void (T::*p_method)(P...), const Vector<Variant> &p_default_args -#ifdef DEBUG_ENABLED - , - const Vector<String> &p_arg_names, const StringName &p_method_name, Variant::Type p_base_type -#endif - ) { - method = p_method; - default_values = p_default_args; -#ifdef DEBUG_ENABLED - argument_names = p_arg_names; - method_name = p_method_name; - base_type = p_base_type; -#endif - } - }; +template <> //do nothing for variant +struct TypeAdjust<Variant> { + _FORCE_INLINE_ static void adjust(Variant *r_ret) { + } +}; - template <class T, class R, class... P> - class InternalMethodR : public Variant::InternalMethod { - public: - R(T::*method) - (P...); - Vector<Variant> default_values; -#ifdef DEBUG_ENABLED - Vector<String> argument_names; -#endif +template <> //do nothing for variant +struct TypeAdjust<Object *> { + _FORCE_INLINE_ static void adjust(Variant *r_ret) { + VariantInternal::clear(r_ret); + *r_ret = (Object *)nullptr; + } +}; - virtual int get_argument_count() const { - return sizeof...(P); - } - virtual Variant::Type get_argument_type(int p_arg) const { - return call_get_argument_type<P...>(p_arg); - return Variant::NIL; - } -#ifdef DEBUG_ENABLED - virtual String get_argument_name(int p_arg) const { - ERR_FAIL_INDEX_V(p_arg, argument_names.size(), String()); - return argument_names[p_arg]; - } -#endif - virtual Vector<Variant> get_default_arguments() const { - return default_values; - } +template <class R, class T, class... P> +static _FORCE_INLINE_ void vc_method_call(R (T::*method)(P...), Variant *base, const Variant **p_args, int p_argcount, Variant &r_ret, const Vector<Variant> &p_defvals, Callable::CallError &r_error) { + call_with_variant_args_ret_dv(VariantGetInternalPtr<T>::get_ptr(base), method, p_args, p_argcount, r_ret, r_error, p_defvals); +} - virtual Variant::Type get_return_type() const { - return GetTypeInfo<R>::VARIANT_TYPE; - } - virtual uint32_t get_flags() const { - uint32_t f = 0; - if (get_return_type() == Variant::NIL) { - f |= FLAG_RETURNS_VARIANT; - } - return f; - } +template <class R, class T, class... P> +static _FORCE_INLINE_ void vc_method_call(R (T::*method)(P...) const, Variant *base, const Variant **p_args, int p_argcount, Variant &r_ret, const Vector<Variant> &p_defvals, Callable::CallError &r_error) { + call_with_variant_args_retc_dv(VariantGetInternalPtr<T>::get_ptr(base), method, p_args, p_argcount, r_ret, r_error, p_defvals); +} - virtual void call(Variant *base, const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error) { - call_with_variant_args_ret_dv(VariantGetInternalPtr<T>::get_ptr(base), method, p_args, p_argcount, r_ret, r_error, default_values); - } +template <class T, class... P> +static _FORCE_INLINE_ void vc_method_call(void (T::*method)(P...), Variant *base, const Variant **p_args, int p_argcount, Variant &r_ret, const Vector<Variant> &p_defvals, Callable::CallError &r_error) { + call_with_variant_args_dv(VariantGetInternalPtr<T>::get_ptr(base), method, p_args, p_argcount, r_error, p_defvals); +} - virtual void validated_call(Variant *base, const Variant **p_args, Variant *r_ret) { - call_with_validated_variant_args_ret(base, method, p_args, r_ret); - } -#ifdef PTRCALL_ENABLED - virtual void ptrcall(void *p_base, const void **p_args, void *r_ret) { - call_with_ptr_args_ret<T, R, P...>(reinterpret_cast<T *>(p_base), method, p_args, r_ret); - } -#endif - InternalMethodR(R (T::*p_method)(P...), const Vector<Variant> &p_default_args -#ifdef DEBUG_ENABLED - , - const Vector<String> &p_arg_names, const StringName &p_method_name, Variant::Type p_base_type -#endif - ) { - method = p_method; - default_values = p_default_args; -#ifdef DEBUG_ENABLED - argument_names = p_arg_names; - method_name = p_method_name; - base_type = p_base_type; -#endif - } - }; +template <class T, class... P> +static _FORCE_INLINE_ void vc_method_call(void (T::*method)(P...) const, Variant *base, const Variant **p_args, int p_argcount, Variant &r_ret, const Vector<Variant> &p_defvals, Callable::CallError &r_error) { + call_with_variant_argsc_dv(VariantGetInternalPtr<T>::get_ptr(base), method, p_args, p_argcount, r_error, p_defvals); +} - template <class T, class R, class... P> - class InternalMethodRC : public Variant::InternalMethod { - public: - R(T::*method) - (P...) const; - Vector<Variant> default_values; -#ifdef DEBUG_ENABLED - Vector<String> argument_names; -#endif +template <class R, class T, class... P> +static _FORCE_INLINE_ void vc_validated_call(R (T::*method)(P...), Variant *base, const Variant **p_args, Variant *r_ret) { + call_with_validated_variant_args_ret(base, method, p_args, r_ret); +} - virtual int get_argument_count() const { - return sizeof...(P); - } - virtual Variant::Type get_argument_type(int p_arg) const { - return call_get_argument_type<P...>(p_arg); - } -#ifdef DEBUG_ENABLED - virtual String get_argument_name(int p_arg) const { - ERR_FAIL_INDEX_V(p_arg, argument_names.size(), String()); - return argument_names[p_arg]; - } -#endif - virtual Vector<Variant> get_default_arguments() const { - return default_values; - } +template <class R, class T, class... P> +static _FORCE_INLINE_ void vc_validated_call(R (T::*method)(P...) const, Variant *base, const Variant **p_args, Variant *r_ret) { + call_with_validated_variant_args_retc(base, method, p_args, r_ret); +} +template <class T, class... P> +static _FORCE_INLINE_ void vc_validated_call(void (T::*method)(P...), Variant *base, const Variant **p_args, Variant *r_ret) { + call_with_validated_variant_args(base, method, p_args); +} - virtual Variant::Type get_return_type() const { - return GetTypeInfo<R>::VARIANT_TYPE; - } - virtual uint32_t get_flags() const { - uint32_t f = FLAG_IS_CONST; - if (get_return_type() == Variant::NIL) { - f |= FLAG_RETURNS_VARIANT; - } - return f; - } +template <class T, class... P> +static _FORCE_INLINE_ void vc_validated_call(void (T::*method)(P...) const, Variant *base, const Variant **p_args, Variant *r_ret) { + call_with_validated_variant_argsc(base, method, p_args); +} - virtual void call(Variant *base, const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error) { - call_with_variant_args_retc_dv(VariantGetInternalPtr<T>::get_ptr(base), method, p_args, p_argcount, r_ret, r_error, default_values); - } +template <class R, class T, class... P> +static _FORCE_INLINE_ void vc_ptrcall(R (T::*method)(P...), void *p_base, const void **p_args, void *r_ret) { + call_with_ptr_args_ret(reinterpret_cast<T *>(p_base), method, p_args, r_ret); +} - virtual void validated_call(Variant *base, const Variant **p_args, Variant *r_ret) { - call_with_validated_variant_args_retc(base, method, p_args, r_ret); - } -#ifdef PTRCALL_ENABLED - virtual void ptrcall(void *p_base, const void **p_args, void *r_ret) { - call_with_ptr_args_retc<T, R, P...>(reinterpret_cast<T *>(p_base), method, p_args, r_ret); - } -#endif - InternalMethodRC(R (T::*p_method)(P...) const, const Vector<Variant> &p_default_args -#ifdef DEBUG_ENABLED - , - const Vector<String> &p_arg_names, const StringName &p_method_name, Variant::Type p_base_type -#endif - ) { - method = p_method; - default_values = p_default_args; -#ifdef DEBUG_ENABLED - argument_names = p_arg_names; - method_name = p_method_name; - base_type = p_base_type; -#endif - } - }; +template <class R, class T, class... P> +static _FORCE_INLINE_ void vc_ptrcall(R (T::*method)(P...) const, void *p_base, const void **p_args, void *r_ret) { + call_with_ptr_args_retc(reinterpret_cast<T *>(p_base), method, p_args, r_ret); +} - template <class T, class R, class... P> - class InternalMethodRS : public Variant::InternalMethod { - public: - R(*method) - (T *, P...); - Vector<Variant> default_values; -#ifdef DEBUG_ENABLED - Vector<String> argument_names; -#endif +template <class T, class... P> +static _FORCE_INLINE_ void vc_ptrcall(void (T::*method)(P...), void *p_base, const void **p_args, void *r_ret) { + call_with_ptr_args(reinterpret_cast<T *>(p_base), method, p_args); +} - virtual int get_argument_count() const { - return sizeof...(P); - } - virtual Variant::Type get_argument_type(int p_arg) const { - return call_get_argument_type<P...>(p_arg); - } -#ifdef DEBUG_ENABLED - virtual String get_argument_name(int p_arg) const { - ERR_FAIL_INDEX_V(p_arg, argument_names.size(), String()); - return argument_names[p_arg]; - } -#endif - virtual Vector<Variant> get_default_arguments() const { - return default_values; - } +template <class T, class... P> +static _FORCE_INLINE_ void vc_ptrcall(void (T::*method)(P...) const, void *p_base, const void **p_args, void *r_ret) { + call_with_ptr_argsc(reinterpret_cast<T *>(p_base), method, p_args); +} - virtual Variant::Type get_return_type() const { - return GetTypeInfo<R>::VARIANT_TYPE; - } - virtual uint32_t get_flags() const { - uint32_t f = 0; - if (get_return_type() == Variant::NIL) { - f |= FLAG_RETURNS_VARIANT; - } - return f; - } +template <class R, class T, class... P> +static _FORCE_INLINE_ int vc_get_argument_count(R (T::*method)(P...)) { + return sizeof...(P); +} +template <class R, class T, class... P> +static _FORCE_INLINE_ int vc_get_argument_count(R (T::*method)(P...) const) { + return sizeof...(P); +} - virtual void call(Variant *base, const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error) { - const Variant **args = p_args; -#ifdef DEBUG_ENABLED - if ((size_t)p_argcount > sizeof...(P)) { - r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS; - r_error.argument = sizeof...(P); - return; - } -#endif - if ((size_t)p_argcount < sizeof...(P)) { - size_t missing = sizeof...(P) - (size_t)p_argcount; - if (missing <= (size_t)default_values.size()) { - args = (const Variant **)alloca(sizeof...(P) * sizeof(const Variant *)); - // GCC fails to see that `sizeof...(P)` cannot be 0 here given the previous - // conditions, so it raises a warning on the potential use of `i < 0` as the - // execution condition. -#if defined(__GNUC__) && !defined(__clang__) -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wtype-limits" -#endif - for (size_t i = 0; i < sizeof...(P); i++) { - if (i < (size_t)p_argcount) { - args[i] = p_args[i]; - } else { - args[i] = &default_values[i - p_argcount + (default_values.size() - missing)]; - } - } -#if defined(__GNUC__) && !defined(__clang__) -#pragma GCC diagnostic pop -#endif - } else { -#ifdef DEBUG_ENABLED - r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; - r_error.argument = sizeof...(P); -#endif - return; - } - } - call_with_variant_args_retc_static_helper(VariantGetInternalPtr<T>::get_ptr(base), method, args, r_ret, r_error, BuildIndexSequence<sizeof...(P)>{}); - } +template <class T, class... P> +static _FORCE_INLINE_ int vc_get_argument_count(void (T::*method)(P...)) { + return sizeof...(P); +} - virtual void validated_call(Variant *base, const Variant **p_args, Variant *r_ret) { - call_with_validated_variant_args_static_retc(base, method, p_args, r_ret); - } -#ifdef PTRCALL_ENABLED - virtual void ptrcall(void *p_base, const void **p_args, void *r_ret) { - call_with_ptr_args_static_retc<T, R, P...>(reinterpret_cast<T *>(p_base), method, p_args, r_ret); - } -#endif - InternalMethodRS(R (*p_method)(T *, P...), const Vector<Variant> &p_default_args -#ifdef DEBUG_ENABLED - , - const Vector<String> &p_arg_names, const StringName &p_method_name, Variant::Type p_base_type -#endif - ) { - method = p_method; - default_values = p_default_args; -#ifdef DEBUG_ENABLED - argument_names = p_arg_names; - method_name = p_method_name; - base_type = p_base_type; -#endif - } - }; +template <class T, class... P> +static _FORCE_INLINE_ int vc_get_argument_count(void (T::*method)(P...) const) { + return sizeof...(P); +} - class InternalMethodVC : public Variant::InternalMethod { - public: - typedef void (*MethodVC)(Variant *, const Variant **, int, Variant &r_ret, Callable::CallError &); - MethodVC methodvc = nullptr; - uint32_t base_flags = 0; - Vector<String> argument_names; - Vector<Variant::Type> argument_types; - Variant::Type return_type = Variant::NIL; - - virtual int get_argument_count() const { - return argument_names.size(); - } - virtual Variant::Type get_argument_type(int p_arg) const { - ERR_FAIL_INDEX_V(p_arg, argument_types.size(), Variant::NIL); - return argument_types[p_arg]; - } -#ifdef DEBUG_ENABLED - virtual String get_argument_name(int p_arg) const { - ERR_FAIL_INDEX_V(p_arg, argument_names.size(), String()); - return argument_names[p_arg]; - } -#endif - virtual Vector<Variant> get_default_arguments() const { - return Vector<Variant>(); - } +template <class R, class T, class... P> +static _FORCE_INLINE_ int vc_get_argument_count(R (*method)(T *, P...)) { + return sizeof...(P); +} - virtual Variant::Type get_return_type() const { - return return_type; - } - virtual uint32_t get_flags() const { - return base_flags | FLAG_NO_PTRCALL; - } +template <class R, class T, class... P> +static _FORCE_INLINE_ Variant::Type vc_get_argument_type(R (T::*method)(P...), int p_arg) { + return call_get_argument_type<P...>(p_arg); +} +template <class R, class T, class... P> +static _FORCE_INLINE_ Variant::Type vc_get_argument_type(R (T::*method)(P...) const, int p_arg) { + return call_get_argument_type<P...>(p_arg); +} - virtual void call(Variant *base, const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error) { - methodvc(base, p_args, p_argcount, r_ret, r_error); - } +template <class T, class... P> +static _FORCE_INLINE_ Variant::Type vc_get_argument_type(void (T::*method)(P...), int p_arg) { + return call_get_argument_type<P...>(p_arg); +} - virtual void validated_call(Variant *base, const Variant **p_args, Variant *r_ret) { - ERR_FAIL_MSG("No support for validated call"); - } -#ifdef PTRCALL_ENABLED - virtual void ptrcall(void *p_base, const void **p_args, void *r_ret) { - ERR_FAIL_MSG("No support for ptrcall call"); - } -#endif - InternalMethodVC(MethodVC p_method, uint32_t p_flags, const Vector<Variant::Type> &p_argument_types, const Variant::Type &p_return_type -#ifdef DEBUG_ENABLED - , - const Vector<String> &p_arg_names, const StringName &p_method_name, Variant::Type p_base_type -#endif - ) { - methodvc = p_method; - argument_types = p_argument_types; - return_type = p_return_type; - base_flags = p_flags; -#ifdef DEBUG_ENABLED - argument_names = p_arg_names; - method_name = p_method_name; - base_type = p_base_type; -#endif - } - }; +template <class T, class... P> +static _FORCE_INLINE_ Variant::Type vc_get_argument_type(void (T::*method)(P...) const, int p_arg) { + return call_get_argument_type<P...>(p_arg); +} - typedef OAHashMap<StringName, Variant::InternalMethod *> MethodMap; - static MethodMap *type_internal_methods; - static List<StringName> *type_internal_method_names; +template <class R, class T, class... P> +static _FORCE_INLINE_ Variant::Type vc_get_argument_type(R (*method)(T *, P...), int p_arg) { + return call_get_argument_type<P...>(p_arg); +} - template <class T, class... P> - static void _bind_method(const StringName &p_name, void (T::*p_method)(P...), const Vector<Variant> &p_default_args = Vector<Variant>() -#ifdef DEBUG_ENABLED - , - const Vector<String> &p_argument_names = Vector<String>() -#endif - ) { +template <class R, class T, class... P> +static _FORCE_INLINE_ Variant::Type vc_get_return_type(R (T::*method)(P...)) { + return GetTypeInfo<R>::VARIANT_TYPE; +} -#ifdef DEBUG_ENABLED - ERR_FAIL_COND_MSG(p_argument_names.size() != sizeof...(P), "Wrong argument name count supplied for method: " + Variant::get_type_name(GetTypeInfo<T>::VARIANT_TYPE) + "::" + String(p_name)); - ERR_FAIL_COND(type_internal_methods[GetTypeInfo<T>::VARIANT_TYPE].has(p_name)); -#endif -#ifdef DEBUG_ENABLED - Variant::InternalMethod *m = memnew((InternalMethod<T, P...>)(p_method, p_default_args, p_argument_names, p_name, GetTypeInfo<T>::VARIANT_TYPE)); -#else - Variant::InternalMethod *m = memnew((InternalMethod<T, P...>)(p_method, p_default_args)); -#endif +template <class R, class T, class... P> +static _FORCE_INLINE_ Variant::Type vc_get_return_type(R (T::*method)(P...) const) { + return GetTypeInfo<R>::VARIANT_TYPE; +} - type_internal_methods[GetTypeInfo<T>::VARIANT_TYPE].insert(p_name, m); - type_internal_method_names[GetTypeInfo<T>::VARIANT_TYPE].push_back(p_name); - } +template <class T, class... P> +static _FORCE_INLINE_ Variant::Type vc_get_return_type(void (T::*method)(P...)) { + return Variant::NIL; +} - template <class T, class R, class... P> - static void _bind_method(const StringName &p_name, R (T::*p_method)(P...) const, const Vector<Variant> &p_default_args = Vector<Variant>() -#ifdef DEBUG_ENABLED - , - const Vector<String> &p_argument_names = Vector<String>() -#endif - ) { -#ifdef DEBUG_ENABLED - ERR_FAIL_COND_MSG(p_argument_names.size() != sizeof...(P), "Wrong argument name count supplied for method: " + Variant::get_type_name(GetTypeInfo<T>::VARIANT_TYPE) + "::" + String(p_name)); - ERR_FAIL_COND_MSG(type_internal_methods[GetTypeInfo<T>::VARIANT_TYPE].has(p_name), " Method already registered: " + Variant::get_type_name(GetTypeInfo<T>::VARIANT_TYPE) + "::" + String(p_name)); +template <class T, class... P> +static _FORCE_INLINE_ Variant::Type vc_get_return_type(void (T::*method)(P...) const) { + return Variant::NIL; +} -#endif -#ifdef DEBUG_ENABLED - Variant::InternalMethod *m = memnew((InternalMethodRC<T, R, P...>)(p_method, p_default_args, p_argument_names, p_name, GetTypeInfo<T>::VARIANT_TYPE)); -#else - Variant::InternalMethod *m = memnew((InternalMethodRC<T, R, P...>)(p_method, p_default_args)); -#endif +template <class R, class... P> +static _FORCE_INLINE_ Variant::Type vc_get_return_type(R (*method)(P...)) { + return GetTypeInfo<R>::VARIANT_TYPE; +} - type_internal_methods[GetTypeInfo<T>::VARIANT_TYPE].insert(p_name, m); - type_internal_method_names[GetTypeInfo<T>::VARIANT_TYPE].push_back(p_name); - } +template <class R, class T, class... P> +static _FORCE_INLINE_ bool vc_has_return_type(R (T::*method)(P...)) { + return true; +} +template <class R, class T, class... P> +static _FORCE_INLINE_ bool vc_has_return_type(R (T::*method)(P...) const) { + return true; +} - template <class T, class R, class... P> - static void _bind_method(const StringName &p_name, R (T::*p_method)(P...), const Vector<Variant> &p_default_args = Vector<Variant>() -#ifdef DEBUG_ENABLED - , - const Vector<String> &p_argument_names = Vector<String>() -#endif - ) { -#ifdef DEBUG_ENABLED - ERR_FAIL_COND_MSG(p_argument_names.size() != sizeof...(P), "Wrong argument name count supplied for method: " + Variant::get_type_name(GetTypeInfo<T>::VARIANT_TYPE) + "::" + String(p_name)); - ERR_FAIL_COND_MSG(type_internal_methods[GetTypeInfo<T>::VARIANT_TYPE].has(p_name), " Method already registered: " + Variant::get_type_name(GetTypeInfo<T>::VARIANT_TYPE) + "::" + String(p_name)); -#endif +template <class T, class... P> +static _FORCE_INLINE_ bool vc_has_return_type(void (T::*method)(P...)) { + return false; +} -#ifdef DEBUG_ENABLED - Variant::InternalMethod *m = memnew((InternalMethodR<T, R, P...>)(p_method, p_default_args, p_argument_names, p_name, GetTypeInfo<T>::VARIANT_TYPE)); -#else - Variant::InternalMethod *m = memnew((InternalMethodR<T, R, P...>)(p_method, p_default_args)); -#endif - type_internal_methods[GetTypeInfo<T>::VARIANT_TYPE].insert(p_name, m); - type_internal_method_names[GetTypeInfo<T>::VARIANT_TYPE].push_back(p_name); - } +template <class T, class... P> +static _FORCE_INLINE_ bool vc_has_return_type(void (T::*method)(P...) const) { + return false; +} -#ifdef DEBUG_ENABLED -#define bind_method(m_type, m_method, m_arg_names, m_default_args) _VariantCall::_bind_method(#m_method, &m_type ::m_method, m_default_args, m_arg_names) -#else -#define bind_method(m_type, m_method, m_arg_names, m_default_args) _VariantCall::_bind_method(#m_method, &m_type ::m_method, m_default_args) -#endif +template <class R, class T, class... P> +static _FORCE_INLINE_ bool vc_is_const(R (T::*method)(P...)) { + return false; +} +template <class R, class T, class... P> +static _FORCE_INLINE_ bool vc_is_const(R (T::*method)(P...) const) { + return true; +} -#ifdef DEBUG_ENABLED -#define bind_methodv(m_name, m_method, m_arg_names, m_default_args) _VariantCall::_bind_method(#m_name, m_method, m_default_args, m_arg_names) -#else -#define bind_methodv(m_name, m_method, m_arg_names, m_default_args) _VariantCall::_bind_method(#m_name, m_method, m_default_args) -#endif +template <class T, class... P> +static _FORCE_INLINE_ bool vc_is_const(void (T::*method)(P...)) { + return false; +} - template <class T, class R, class... P> - static void _bind_function(const StringName &p_name, R (*p_method)(T *, P...), const Vector<Variant> &p_default_args = Vector<Variant>() -#ifdef DEBUG_ENABLED - , - const Vector<String> &p_argument_names = Vector<String>() -#endif - ) { -#ifdef DEBUG_ENABLED - ERR_FAIL_COND_MSG(p_argument_names.size() != sizeof...(P), "Wrong argument name count supplied for method: " + Variant::get_type_name(GetTypeInfo<T>::VARIANT_TYPE) + "::" + String(p_name)); - ERR_FAIL_COND_MSG(type_internal_methods[GetTypeInfo<T>::VARIANT_TYPE].has(p_name), " Method already registered: " + Variant::get_type_name(GetTypeInfo<T>::VARIANT_TYPE) + "::" + String(p_name)); -#endif +template <class T, class... P> +static _FORCE_INLINE_ bool vc_is_const(void (T::*method)(P...) const) { + return true; +} -#ifdef DEBUG_ENABLED - Variant::InternalMethod *m = memnew((InternalMethodRS<T, R, P...>)(p_method, p_default_args, p_argument_names, p_name, GetTypeInfo<T>::VARIANT_TYPE)); -#else - Variant::InternalMethod *m = memnew((InternalMethodRS<T, R, P...>)(p_method, p_default_args)); -#endif +template <class R, class T, class... P> +static _FORCE_INLINE_ Variant::Type vc_get_base_type(R (T::*method)(P...)) { + return GetTypeInfo<T>::VARIANT_TYPE; +} +template <class R, class T, class... P> +static _FORCE_INLINE_ Variant::Type vc_get_base_type(R (T::*method)(P...) const) { + return GetTypeInfo<T>::VARIANT_TYPE; +} - type_internal_methods[GetTypeInfo<T>::VARIANT_TYPE].insert(p_name, m); - type_internal_method_names[GetTypeInfo<T>::VARIANT_TYPE].push_back(p_name); - } +template <class T, class... P> +static _FORCE_INLINE_ Variant::Type vc_get_base_type(void (T::*method)(P...)) { + return GetTypeInfo<T>::VARIANT_TYPE; +} -#ifdef DEBUG_ENABLED -#define bind_function(m_name, m_method, m_arg_names, m_default_args) _VariantCall::_bind_function(m_name, m_method, m_default_args, m_arg_names) -#else -#define bind_function(m_name, m_method, m_arg_names, m_default_args) _VariantCall::_bind_function(m_name, m_method, m_default_args) -#endif +template <class T, class... P> +static _FORCE_INLINE_ Variant::Type vc_get_base_type(void (T::*method)(P...) const) { + return GetTypeInfo<T>::VARIANT_TYPE; +} - static void _bind_custom(Variant::Type p_type, const StringName &p_name, InternalMethodVC::MethodVC p_method, uint32_t p_flags, const Vector<Variant::Type> &p_argument_types, const Variant::Type &p_return_type -#ifdef DEBUG_ENABLED - , - const Vector<String> &p_argument_names = Vector<String>() -#endif - ) { +#define METHOD_CLASS(m_class, m_method_name, m_method_ptr) \ + struct Method_##m_class##_##m_method_name { \ + static void call(Variant *base, const Variant **p_args, int p_argcount, Variant &r_ret, const Vector<Variant> &p_defvals, Callable::CallError &r_error) { \ + vc_method_call(m_method_ptr, base, p_args, p_argcount, r_ret, p_defvals, r_error); \ + } \ + static void validated_call(Variant *base, const Variant **p_args, int p_argcount, Variant *r_ret) { \ + TypeAdjust<m_class>::adjust(r_ret); \ + vc_validated_call(m_method_ptr, base, p_args, r_ret); \ + } \ + static void ptrcall(void *p_base, const void **p_args, void *r_ret, int p_argcount) { \ + vc_ptrcall(m_method_ptr, p_base, p_args, r_ret); \ + } \ + static int get_argument_count() { \ + return vc_get_argument_count(m_method_ptr); \ + } \ + static Variant::Type get_argument_type(int p_arg) { \ + return vc_get_argument_type(m_method_ptr, p_arg); \ + } \ + static Variant::Type get_return_type() { \ + return vc_get_return_type(m_method_ptr); \ + } \ + static bool has_return_type() { \ + return vc_has_return_type(m_method_ptr); \ + } \ + static bool is_const() { \ + return vc_is_const(m_method_ptr); \ + } \ + static bool is_vararg() { \ + return false; \ + } \ + static Variant::Type get_base_type() { \ + return vc_get_base_type(m_method_ptr); \ + } \ + static StringName get_name() { \ + return #m_method_name; \ + } \ + }; -#ifdef DEBUG_ENABLED - Variant::InternalMethod *m = memnew(InternalMethodVC(p_method, p_flags, p_argument_types, p_return_type, p_argument_names, p_name, p_type)); -#else - Variant::InternalMethod *m = memnew(InternalMethodVC(p_method, p_flags, p_argument_types, p_return_type)); -#endif +template <class R, class T, class... P> +static _FORCE_INLINE_ void vc_ptrcall(R (*method)(T *, P...), void *p_base, const void **p_args, void *r_ret) { + call_with_ptr_args_static_retc<T, R, P...>(reinterpret_cast<T *>(p_base), method, p_args, r_ret); +} - type_internal_methods[p_type].insert(p_name, m); - type_internal_method_names[p_type].push_back(p_name); - } +#define FUNCTION_CLASS(m_class, m_method_name, m_method_ptr) \ + struct Method_##m_class##_##m_method_name { \ + static void call(Variant *base, const Variant **p_args, int p_argcount, Variant &r_ret, const Vector<Variant> &p_defvals, Callable::CallError &r_error) { \ + call_with_variant_args_retc_static_helper_dv(VariantGetInternalPtr<m_class>::get_ptr(base), m_method_ptr, p_args, p_argcount, r_ret, p_defvals, r_error); \ + } \ + static void validated_call(Variant *base, const Variant **p_args, int p_argcount, Variant *r_ret) { \ + TypeAdjust<m_class>::adjust(r_ret); \ + call_with_validated_variant_args_static_retc(base, m_method_ptr, p_args, r_ret); \ + } \ + static void ptrcall(void *p_base, const void **p_args, void *r_ret, int p_argcount) { \ + vc_ptrcall(m_method_ptr, p_base, p_args, r_ret); \ + } \ + static int get_argument_count() { \ + return vc_get_argument_count(m_method_ptr); \ + } \ + static Variant::Type get_argument_type(int p_arg) { \ + return vc_get_argument_type(m_method_ptr, p_arg); \ + } \ + static Variant::Type get_return_type() { \ + return vc_get_return_type(m_method_ptr); \ + } \ + static bool has_return_type() { \ + return true; \ + } \ + static bool is_const() { \ + return true; \ + } \ + static bool is_vararg() { \ + return false; \ + } \ + static Variant::Type get_base_type() { \ + return GetTypeInfo<m_class>::VARIANT_TYPE; \ + } \ + static StringName get_name() { \ + return #m_method_name; \ + } \ + }; -#ifdef DEBUG_ENABLED -#define bind_custom(m_type, m_name, m_method, m_flags, m_arg_types, m_ret_type, m_arg_names) _VariantCall::_bind_custom(m_type, m_name, m_method, m_flags, m_arg_types, m_ret_type, m_arg_names) -#else -#define bind_custom(m_type, m_name, m_method, m_flags, m_arg_types, m_ret_type, m_arg_names) _VariantCall::_bind_custom(m_type, m_name, m_method, m_flags, m_arg_types, m_ret_type) -#endif +#define VARARG_CLASS(m_class, m_method_name, m_method_ptr, m_has_return, m_return_type) \ + struct Method_##m_class##_##m_method_name { \ + static void call(Variant *base, const Variant **p_args, int p_argcount, Variant &r_ret, const Vector<Variant> &p_defvals, Callable::CallError &r_error) { \ + m_method_ptr(base, p_args, p_argcount, r_ret, r_error); \ + } \ + static void validated_call(Variant *base, const Variant **p_args, int p_argcount, Variant *r_ret) { \ + Callable::CallError ce; \ + m_method_ptr(base, p_args, p_argcount, *r_ret, ce); \ + } \ + static void ptrcall(void *p_base, const void **p_args, void *r_ret, int p_argcount) { \ + LocalVector<Variant> vars; \ + vars.resize(p_argcount); \ + LocalVector<const Variant *> vars_ptrs; \ + vars_ptrs.resize(p_argcount); \ + for (int i = 0; i < p_argcount; i++) { \ + vars[i] = PtrToArg<Variant>::convert(p_args[i]); \ + vars_ptrs[i] = &vars[i]; \ + } \ + Variant base = PtrToArg<m_class>::convert(p_base); \ + Variant ret; \ + Callable::CallError ce; \ + m_method_ptr(&base, (const Variant **)&vars_ptrs[0], p_argcount, ret, ce); \ + if (m_has_return) { \ + m_return_type r = ret; \ + PtrToArg<m_return_type>::encode(ret, r_ret); \ + } \ + } \ + static int get_argument_count() { \ + return 0; \ + } \ + static Variant::Type get_argument_type(int p_arg) { \ + return Variant::NIL; \ + } \ + static Variant::Type get_return_type() { \ + return GetTypeInfo<m_return_type>::VARIANT_TYPE; \ + } \ + static bool has_return_type() { \ + return m_has_return; \ + } \ + static bool is_const() { \ + return true; \ + } \ + static bool is_vararg() { \ + return true; \ + } \ + static Variant::Type get_base_type() { \ + return GetTypeInfo<m_class>::VARIANT_TYPE; \ + } \ + static StringName get_name() { \ + return #m_method_name; \ + } \ + }; +struct _VariantCall { static String func_PackedByteArray_get_string_from_ascii(PackedByteArray *p_instance) { String s; if (p_instance->size() > 0) { @@ -670,190 +506,6 @@ struct _VariantCall { signal->emit(p_args, p_argcount); } - struct ConstructData { - int arg_count; - Vector<Variant::Type> arg_types; - Vector<String> arg_names; - VariantConstructFunc func; - }; - - struct ConstructFunc { - List<ConstructData> constructors; - }; - - static ConstructFunc *construct_funcs; - - static void Vector2_init1(Variant &r_ret, const Variant **p_args) { - r_ret = Vector2(*p_args[0], *p_args[1]); - } - - static void Vector2i_init1(Variant &r_ret, const Variant **p_args) { - r_ret = Vector2i(*p_args[0], *p_args[1]); - } - - static void Rect2_init1(Variant &r_ret, const Variant **p_args) { - r_ret = Rect2(*p_args[0], *p_args[1]); - } - - static void Rect2_init2(Variant &r_ret, const Variant **p_args) { - r_ret = Rect2(*p_args[0], *p_args[1], *p_args[2], *p_args[3]); - } - - static void Rect2i_init1(Variant &r_ret, const Variant **p_args) { - r_ret = Rect2i(*p_args[0], *p_args[1]); - } - - static void Rect2i_init2(Variant &r_ret, const Variant **p_args) { - r_ret = Rect2i(*p_args[0], *p_args[1], *p_args[2], *p_args[3]); - } - - static void Transform2D_init2(Variant &r_ret, const Variant **p_args) { - Transform2D m(*p_args[0], *p_args[1]); - r_ret = m; - } - - static void Transform2D_init3(Variant &r_ret, const Variant **p_args) { - Transform2D m; - m[0] = *p_args[0]; - m[1] = *p_args[1]; - m[2] = *p_args[2]; - r_ret = m; - } - - static void Vector3_init1(Variant &r_ret, const Variant **p_args) { - r_ret = Vector3(*p_args[0], *p_args[1], *p_args[2]); - } - - static void Vector3i_init1(Variant &r_ret, const Variant **p_args) { - r_ret = Vector3i(*p_args[0], *p_args[1], *p_args[2]); - } - - static void Plane_init1(Variant &r_ret, const Variant **p_args) { - r_ret = Plane(*p_args[0], *p_args[1], *p_args[2], *p_args[3]); - } - - static void Plane_init2(Variant &r_ret, const Variant **p_args) { - r_ret = Plane(*p_args[0], *p_args[1], *p_args[2]); - } - - static void Plane_init3(Variant &r_ret, const Variant **p_args) { - r_ret = Plane(p_args[0]->operator Vector3(), p_args[1]->operator real_t()); - } - static void Plane_init4(Variant &r_ret, const Variant **p_args) { - r_ret = Plane(p_args[0]->operator Vector3(), p_args[1]->operator Vector3()); - } - - static void Quat_init1(Variant &r_ret, const Variant **p_args) { - r_ret = Quat(*p_args[0], *p_args[1], *p_args[2], *p_args[3]); - } - - static void Quat_init2(Variant &r_ret, const Variant **p_args) { - r_ret = Quat(((Vector3)(*p_args[0])), ((real_t)(*p_args[1]))); - } - - static void Quat_init3(Variant &r_ret, const Variant **p_args) { - r_ret = Quat(((Vector3)(*p_args[0]))); - } - - static void Color_init1(Variant &r_ret, const Variant **p_args) { - r_ret = Color(*p_args[0], *p_args[1], *p_args[2], *p_args[3]); - } - - static void Color_init2(Variant &r_ret, const Variant **p_args) { - r_ret = Color(*p_args[0], *p_args[1], *p_args[2]); - } - - static void Color_init3(Variant &r_ret, const Variant **p_args) { - r_ret = Color::html(*p_args[0]); - } - - static void Color_init4(Variant &r_ret, const Variant **p_args) { - r_ret = Color::hex(*p_args[0]); - } - - static void Color_init5(Variant &r_ret, const Variant **p_args) { - r_ret = Color(((Color)(*p_args[0])), *p_args[1]); - } - - static void AABB_init1(Variant &r_ret, const Variant **p_args) { - r_ret = ::AABB(*p_args[0], *p_args[1]); - } - - static void Basis_init1(Variant &r_ret, const Variant **p_args) { - Basis m; - m.set_axis(0, *p_args[0]); - m.set_axis(1, *p_args[1]); - m.set_axis(2, *p_args[2]); - r_ret = m; - } - - static void Basis_init2(Variant &r_ret, const Variant **p_args) { - r_ret = Basis(p_args[0]->operator Vector3(), p_args[1]->operator real_t()); - } - - static void Transform_init1(Variant &r_ret, const Variant **p_args) { - Transform t; - t.basis.set_axis(0, *p_args[0]); - t.basis.set_axis(1, *p_args[1]); - t.basis.set_axis(2, *p_args[2]); - t.origin = *p_args[3]; - r_ret = t; - } - - static void Transform_init2(Variant &r_ret, const Variant **p_args) { - r_ret = Transform(p_args[0]->operator Basis(), p_args[1]->operator Vector3()); - } - - static void Callable_init2(Variant &r_ret, const Variant **p_args) { - r_ret = Callable(p_args[0]->operator ObjectID(), p_args[1]->operator String()); - } - - static void Signal_init2(Variant &r_ret, const Variant **p_args) { - r_ret = Signal(p_args[0]->operator ObjectID(), p_args[1]->operator String()); - } - - static void add_constructor(VariantConstructFunc p_func, const Variant::Type p_type, - const String &p_name1 = "", const Variant::Type p_type1 = Variant::NIL, - const String &p_name2 = "", const Variant::Type p_type2 = Variant::NIL, - const String &p_name3 = "", const Variant::Type p_type3 = Variant::NIL, - const String &p_name4 = "", const Variant::Type p_type4 = Variant::NIL) { - ConstructData cd; - cd.func = p_func; - cd.arg_count = 0; - - if (p_name1 == "") { - goto end; - } - cd.arg_count++; - cd.arg_names.push_back(p_name1); - cd.arg_types.push_back(p_type1); - - if (p_name2 == "") { - goto end; - } - cd.arg_count++; - cd.arg_names.push_back(p_name2); - cd.arg_types.push_back(p_type2); - - if (p_name3 == "") { - goto end; - } - cd.arg_count++; - cd.arg_names.push_back(p_name3); - cd.arg_types.push_back(p_type3); - - if (p_name4 == "") { - goto end; - } - cd.arg_count++; - cd.arg_names.push_back(p_name4); - cd.arg_types.push_back(p_type4); - - end: - - construct_funcs[p_type].constructors.push_back(cd); - } - struct ConstantData { Map<StringName, int> value; #ifdef DEBUG_ENABLED @@ -882,20 +534,62 @@ struct _VariantCall { } }; -_VariantCall::ConstructFunc *_VariantCall::construct_funcs = nullptr; _VariantCall::ConstantData *_VariantCall::constant_data = nullptr; -_VariantCall::MethodMap *_VariantCall::type_internal_methods = nullptr; -List<StringName> *_VariantCall::type_internal_method_names = nullptr; -Variant Variant::call(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { - Variant ret; - call_ptr(p_method, p_args, p_argcount, &ret, r_error); - return ret; -} +struct VariantBuiltInMethodInfo { + void (*call)(Variant *base, const Variant **p_args, int p_argcount, Variant &r_ret, const Vector<Variant> &p_defvals, Callable::CallError &r_error); + Variant::ValidatedBuiltInMethod validated_call; + Variant::PTRBuiltInMethod ptrcall; + + Vector<Variant> default_arguments; + Vector<String> argument_names; + + bool is_const; + bool has_return_type; + bool is_vararg; + Variant::Type return_type; + int argument_count; + Variant::Type (*get_argument_type)(int p_arg); +}; + +typedef OAHashMap<StringName, VariantBuiltInMethodInfo> BuiltinMethodMap; +static BuiltinMethodMap *builtin_method_info; +static List<StringName> *builtin_method_names; + +template <class T> +static void register_builtin_method(const Vector<String> &p_argnames, const Vector<Variant> &p_def_args) { + StringName name = T::get_name(); + + ERR_FAIL_COND(builtin_method_info[T::get_base_type()].has(name)); + + VariantBuiltInMethodInfo imi; + + imi.call = T::call; + imi.validated_call = T::validated_call; + if (T::is_vararg()) { + imi.ptrcall = nullptr; + } else { + imi.ptrcall = T::ptrcall; + } -void Variant::call_ptr(const StringName &p_method, const Variant **p_args, int p_argcount, Variant *r_ret, Callable::CallError &r_error) { - Variant ret; + imi.default_arguments = p_def_args; + imi.argument_names = p_argnames; + imi.is_const = T::is_const(); + imi.is_vararg = T::is_vararg(); + imi.has_return_type = T::has_return_type(); + imi.return_type = T::get_return_type(); + imi.argument_count = T::get_argument_count(); + imi.get_argument_type = T::get_argument_type; +#ifdef DEBUG_METHODS_ENABLED + ERR_FAIL_COND(!imi.is_vararg && imi.argument_count != imi.argument_names.size()); +#endif + + builtin_method_info[T::get_base_type()].insert(name, imi); + builtin_method_names[T::get_base_type()].push_back(name); +} + +void Variant::call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error) { if (type == Variant::OBJECT) { //call object Object *obj = _get_obj().obj; @@ -910,236 +604,22 @@ void Variant::call_ptr(const StringName &p_method, const Variant **p_args, int p } #endif - ret = _get_obj().obj->call(p_method, p_args, p_argcount, r_error); + r_ret = _get_obj().obj->call(p_method, p_args, p_argcount, r_error); //else if (type==Variant::METHOD) { } else { r_error.error = Callable::CallError::CALL_OK; - Variant::InternalMethod **m = _VariantCall::type_internal_methods[type].lookup_ptr(p_method); + const VariantBuiltInMethodInfo *imf = builtin_method_info[type].lookup_ptr(p_method); - if (m) { - (*m)->call((Variant *)this, p_args, p_argcount, ret, r_error); - } else { - //ok fail because not found + if (!imf) { r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; return; } - } - - if (r_error.error == Callable::CallError::CALL_OK && r_ret) { - *r_ret = ret; - } -} - -#define VCALL(m_type, m_method) _VariantCall::_call_##m_type##_##m_method - -Variant Variant::construct(const Variant::Type p_type, const Variant **p_args, int p_argcount, Callable::CallError &r_error, bool p_strict) { - r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; - ERR_FAIL_INDEX_V(p_type, VARIANT_MAX, Variant()); - - r_error.error = Callable::CallError::CALL_OK; - if (p_argcount == 0) { //generic construct - - switch (p_type) { - case NIL: - return Variant(); - - // atomic types - case BOOL: - return Variant(false); - case INT: - return 0; - case FLOAT: - return 0.0f; - case STRING: - return String(); - - // math types - case VECTOR2: - return Vector2(); - case VECTOR2I: - return Vector2i(); - case RECT2: - return Rect2(); - case RECT2I: - return Rect2i(); - case VECTOR3: - return Vector3(); - case VECTOR3I: - return Vector3i(); - case TRANSFORM2D: - return Transform2D(); - case PLANE: - return Plane(); - case QUAT: - return Quat(); - case AABB: - return ::AABB(); - case BASIS: - return Basis(); - case TRANSFORM: - return Transform(); - - // misc types - case COLOR: - return Color(); - case STRING_NAME: - return StringName(); - case NODE_PATH: - return NodePath(); - case _RID: - return RID(); - case OBJECT: - return (Object *)nullptr; - case CALLABLE: - return Callable(); - case SIGNAL: - return Signal(); - case DICTIONARY: - return Dictionary(); - case ARRAY: - return Array(); - case PACKED_BYTE_ARRAY: - return PackedByteArray(); - case PACKED_INT32_ARRAY: - return PackedInt32Array(); - case PACKED_INT64_ARRAY: - return PackedInt64Array(); - case PACKED_FLOAT32_ARRAY: - return PackedFloat32Array(); - case PACKED_FLOAT64_ARRAY: - return PackedFloat64Array(); - case PACKED_STRING_ARRAY: - return PackedStringArray(); - case PACKED_VECTOR2_ARRAY: - return PackedVector2Array(); - case PACKED_VECTOR3_ARRAY: - return PackedVector3Array(); - case PACKED_COLOR_ARRAY: - return PackedColorArray(); - default: - return Variant(); - } - - } else if (p_argcount == 1 && p_args[0]->type == p_type) { - return *p_args[0]; //copy construct - } else if (p_argcount == 1 && (!p_strict || Variant::can_convert(p_args[0]->type, p_type))) { - //near match construct - - switch (p_type) { - case NIL: { - return Variant(); - } break; - case BOOL: { - return Variant(bool(*p_args[0])); - } - case INT: { - return (int64_t(*p_args[0])); - } - case FLOAT: { - return double(*p_args[0]); - } - case STRING: { - return String(*p_args[0]); - } - case VECTOR2: { - return Vector2(*p_args[0]); - } - case VECTOR2I: { - return Vector2i(*p_args[0]); - } - case RECT2: - return (Rect2(*p_args[0])); - case RECT2I: - return (Rect2i(*p_args[0])); - case VECTOR3: - return (Vector3(*p_args[0])); - case VECTOR3I: - return (Vector3i(*p_args[0])); - case TRANSFORM2D: - return (Transform2D(p_args[0]->operator Transform2D())); - case PLANE: - return (Plane(*p_args[0])); - case QUAT: - return (p_args[0]->operator Quat()); - case AABB: - return (::AABB(*p_args[0])); - case BASIS: - return (Basis(p_args[0]->operator Basis())); - case TRANSFORM: - return (Transform(p_args[0]->operator Transform())); - - // misc types - case COLOR: - return p_args[0]->type == Variant::STRING ? Color::html(*p_args[0]) : Color::hex(*p_args[0]); - case STRING_NAME: - return (StringName(p_args[0]->operator StringName())); - case NODE_PATH: - return (NodePath(p_args[0]->operator NodePath())); - case _RID: - return (RID(*p_args[0])); - case OBJECT: - return ((Object *)(p_args[0]->operator Object *())); - case CALLABLE: - return ((Callable)(p_args[0]->operator Callable())); - case SIGNAL: - return ((Signal)(p_args[0]->operator Signal())); - case DICTIONARY: - return p_args[0]->operator Dictionary(); - case ARRAY: - return p_args[0]->operator Array(); - - // arrays - case PACKED_BYTE_ARRAY: - return (PackedByteArray(*p_args[0])); - case PACKED_INT32_ARRAY: - return (PackedInt32Array(*p_args[0])); - case PACKED_INT64_ARRAY: - return (PackedInt64Array(*p_args[0])); - case PACKED_FLOAT32_ARRAY: - return (PackedFloat32Array(*p_args[0])); - case PACKED_FLOAT64_ARRAY: - return (PackedFloat64Array(*p_args[0])); - case PACKED_STRING_ARRAY: - return (PackedStringArray(*p_args[0])); - case PACKED_VECTOR2_ARRAY: - return (PackedVector2Array(*p_args[0])); - case PACKED_VECTOR3_ARRAY: - return (PackedVector3Array(*p_args[0])); - case PACKED_COLOR_ARRAY: - return (PackedColorArray(*p_args[0])); - default: - return Variant(); - } - } else if (p_argcount >= 1) { - _VariantCall::ConstructFunc &c = _VariantCall::construct_funcs[p_type]; - - for (List<_VariantCall::ConstructData>::Element *E = c.constructors.front(); E; E = E->next()) { - const _VariantCall::ConstructData &cd = E->get(); - if (cd.arg_count != p_argcount) { - continue; - } - - //validate parameters - for (int i = 0; i < cd.arg_count; i++) { - if (!Variant::can_convert(p_args[i]->type, cd.arg_types[i])) { - r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; //no such constructor - r_error.argument = i; - r_error.expected = cd.arg_types[i]; - return Variant(); - } - } - - Variant v; - cd.func(v, p_args); - return v; - } + imf->call(this, p_args, p_argcount, r_ret, imf->default_arguments, r_error); } - r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; //no such constructor - return Variant(); } bool Variant::has_method(const StringName &p_method) const { @@ -1152,134 +632,143 @@ bool Variant::has_method(const StringName &p_method) const { return obj->has_method(p_method); } - return _VariantCall::type_internal_methods[type].has(p_method); + return builtin_method_info[type].has(p_method); } -Vector<Variant::Type> Variant::get_method_argument_types(Variant::Type p_type, const StringName &p_method) { - Vector<Variant::Type> types; +bool Variant::has_builtin_method(Variant::Type p_type, const StringName &p_method) { + ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, false); + return builtin_method_info[p_type].has(p_method); +} - Variant::InternalMethod **m = _VariantCall::type_internal_methods[p_type].lookup_ptr(p_method); - if (*m) { - types.resize((*m)->get_argument_count()); - for (int i = 0; i < (*m)->get_argument_count(); i++) { - types.write[i] = (*m)->get_argument_type(i); - } - } +Variant::ValidatedBuiltInMethod Variant::get_validated_builtin_method(Variant::Type p_type, const StringName &p_method) { + ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, nullptr); + const VariantBuiltInMethodInfo *method = builtin_method_info[p_type].lookup_ptr(p_method); + ERR_FAIL_COND_V(!method, nullptr); + return method->validated_call; +} - return types; +Variant::PTRBuiltInMethod Variant::get_ptr_builtin_method(Variant::Type p_type, const StringName &p_method) { + ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, nullptr); + const VariantBuiltInMethodInfo *method = builtin_method_info[p_type].lookup_ptr(p_method); + ERR_FAIL_COND_V(!method, nullptr); + return method->ptrcall; } -bool Variant::is_method_const(Variant::Type p_type, const StringName &p_method) { - Variant::InternalMethod **m = _VariantCall::type_internal_methods[p_type].lookup_ptr(p_method); - if (*m) { - return (*m)->get_flags() & Variant::InternalMethod::FLAG_IS_CONST; - } - return false; +int Variant::get_builtin_method_argument_count(Variant::Type p_type, const StringName &p_method) { + ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, 0); + const VariantBuiltInMethodInfo *method = builtin_method_info[p_type].lookup_ptr(p_method); + ERR_FAIL_COND_V(!method, 0); + return method->argument_count; } -Vector<StringName> Variant::get_method_argument_names(Variant::Type p_type, const StringName &p_method) { - Vector<StringName> argnames; +Variant::Type Variant::get_builtin_method_argument_type(Variant::Type p_type, const StringName &p_method, int p_argument) { + ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, Variant::NIL); + const VariantBuiltInMethodInfo *method = builtin_method_info[p_type].lookup_ptr(p_method); + ERR_FAIL_COND_V(!method, Variant::NIL); + ERR_FAIL_INDEX_V(p_argument, method->argument_count, Variant::NIL); + return method->get_argument_type(p_argument); +} -#ifdef DEBUG_ENABLED - Variant::InternalMethod **m = _VariantCall::type_internal_methods[p_type].lookup_ptr(p_method); - if (*m) { - argnames.resize((*m)->get_argument_count()); - for (int i = 0; i < (*m)->get_argument_count(); i++) { - argnames.write[i] = (*m)->get_argument_name(i); - } - } +String Variant::get_builtin_method_argument_name(Variant::Type p_type, const StringName &p_method, int p_argument) { + ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, String()); + const VariantBuiltInMethodInfo *method = builtin_method_info[p_type].lookup_ptr(p_method); + ERR_FAIL_COND_V(!method, String()); +#ifdef DEBUG_METHODS_ENABLED + ERR_FAIL_INDEX_V(p_argument, method->argument_count, String()); + return method->argument_names[p_argument]; +#else + return "arg" + itos(p_argument + 1); #endif - return argnames; } -Variant::Type Variant::get_method_return_type(Variant::Type p_type, const StringName &p_method, bool *r_has_return) { - Variant::Type rt = Variant::NIL; - Variant::InternalMethod **m = _VariantCall::type_internal_methods[p_type].lookup_ptr(p_method); - if (*m) { - rt = (*m)->get_return_type(); - if (r_has_return) { - *r_has_return = ((*m)->get_flags() & Variant::InternalMethod::FLAG_RETURNS_VARIANT) || rt != Variant::NIL; - } - } - return rt; +Vector<Variant> Variant::get_builtin_method_default_arguments(Variant::Type p_type, const StringName &p_method) { + ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, Vector<Variant>()); + const VariantBuiltInMethodInfo *method = builtin_method_info[p_type].lookup_ptr(p_method); + ERR_FAIL_COND_V(!method, Vector<Variant>()); + return method->default_arguments; } -Vector<Variant> Variant::get_method_default_arguments(Variant::Type p_type, const StringName &p_method) { - Variant::InternalMethod **m = _VariantCall::type_internal_methods[p_type].lookup_ptr(p_method); - if (*m) { - return (*m)->get_default_arguments(); +bool Variant::has_builtin_method_return_value(Variant::Type p_type, const StringName &p_method) { + ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, false); + const VariantBuiltInMethodInfo *method = builtin_method_info[p_type].lookup_ptr(p_method); + ERR_FAIL_COND_V(!method, false); + return method->has_return_type; +} + +void Variant::get_builtin_method_list(Variant::Type p_type, List<StringName> *p_list) { + ERR_FAIL_INDEX(p_type, Variant::VARIANT_MAX); + for (List<StringName>::Element *E = builtin_method_names[p_type].front(); E; E = E->next()) { + p_list->push_back(E->get()); } - return Vector<Variant>(); +} + +Variant::Type Variant::get_builtin_method_return_type(Variant::Type p_type, const StringName &p_method) { + ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, Variant::NIL); + const VariantBuiltInMethodInfo *method = builtin_method_info[p_type].lookup_ptr(p_method); + ERR_FAIL_COND_V(!method, Variant::NIL); + return method->return_type; +} + +bool Variant::is_builtin_method_const(Variant::Type p_type, const StringName &p_method) { + ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, false); + const VariantBuiltInMethodInfo *method = builtin_method_info[p_type].lookup_ptr(p_method); + ERR_FAIL_COND_V(!method, false); + return method->is_const; +} + +bool Variant::is_builtin_method_vararg(Variant::Type p_type, const StringName &p_method) { + ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, false); + const VariantBuiltInMethodInfo *method = builtin_method_info[p_type].lookup_ptr(p_method); + ERR_FAIL_COND_V(!method, false); + return method->is_vararg; } void Variant::get_method_list(List<MethodInfo> *p_list) const { - for (List<StringName>::Element *E = _VariantCall::type_internal_method_names[type].front(); E; E = E->next()) { - Variant::InternalMethod **m = _VariantCall::type_internal_methods[type].lookup_ptr(E->get()); - ERR_CONTINUE(!*m); - - MethodInfo mi; - mi.name = E->get(); - mi.return_val.type = (*m)->get_return_type(); - if ((*m)->get_flags() & Variant::InternalMethod::FLAG_RETURNS_VARIANT) { - mi.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT; - } - if ((*m)->get_flags() & Variant::InternalMethod::FLAG_IS_CONST) { - mi.flags |= METHOD_FLAG_CONST; - } - if ((*m)->get_flags() & Variant::InternalMethod::FLAG_VARARGS) { - mi.flags |= METHOD_FLAG_VARARG; + if (type == OBJECT) { + Object *obj = get_validated_object(); + if (obj) { + obj->get_method_list(p_list); } + } else { + for (List<StringName>::Element *E = builtin_method_names[type].front(); E; E = E->next()) { + const VariantBuiltInMethodInfo *method = builtin_method_info[type].lookup_ptr(E->get()); + ERR_CONTINUE(!method); + + MethodInfo mi; + mi.name = E->get(); + + //return type + if (method->has_return_type) { + mi.return_val.type = method->return_type; + if (mi.return_val.type == Variant::NIL) { + mi.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT; + } + } - for (int i = 0; i < (*m)->get_argument_count(); i++) { - PropertyInfo arg; -#ifdef DEBUG_ENABLED - arg.name = (*m)->get_argument_name(i); + if (method->is_const) { + mi.flags |= METHOD_FLAG_CONST; + } + if (method->is_vararg) { + mi.flags |= METHOD_FLAG_VARARG; + } + + for (int i = 0; i < method->argument_count; i++) { + PropertyInfo pi; +#ifdef DEBUG_METHODS_ENABLED + pi.name = method->argument_names[i]; #else - arg.name = "arg" + itos(i + 1); + pi.name = "arg" + itos(i + 1); #endif - arg.type = (*m)->get_argument_type(i); - mi.arguments.push_back(arg); - } - - mi.default_arguments = (*m)->get_default_arguments(); - p_list->push_back(mi); - } -} + pi.type = method->get_argument_type(i); + if (pi.type == Variant::NIL) { + pi.usage |= PROPERTY_USAGE_NIL_IS_VARIANT; + } + mi.arguments.push_back(pi); + } -void Variant::get_constructor_list(Variant::Type p_type, List<MethodInfo> *p_list) { - ERR_FAIL_INDEX(p_type, VARIANT_MAX); - - //custom constructors - for (const List<_VariantCall::ConstructData>::Element *E = _VariantCall::construct_funcs[p_type].constructors.front(); E; E = E->next()) { - const _VariantCall::ConstructData &cd = E->get(); - MethodInfo mi; - mi.name = Variant::get_type_name(p_type); - mi.return_val.type = p_type; - for (int i = 0; i < cd.arg_count; i++) { - PropertyInfo pi; - pi.name = cd.arg_names[i]; - pi.type = cd.arg_types[i]; - mi.arguments.push_back(pi); + mi.default_arguments = method->default_arguments; + p_list->push_back(mi); } - p_list->push_back(mi); - } - //default constructors - for (int i = 0; i < VARIANT_MAX; i++) { - if (i == p_type) { - continue; - } - if (!Variant::can_convert(Variant::Type(i), p_type)) { - continue; - } - - MethodInfo mi; - mi.name = Variant::get_type_name(p_type); - PropertyInfo pi; - pi.name = "from"; - pi.type = Variant::Type(i); - mi.arguments.push_back(pi); - mi.return_val.type = p_type; - p_list->push_back(mi); } } @@ -1340,21 +829,44 @@ Variant Variant::get_constant_value(Variant::Type p_type, const StringName &p_va return E->get(); } -Variant::InternalMethod *Variant::get_internal_method(Type p_type, const StringName &p_method_name) { - ERR_FAIL_INDEX_V(p_type, VARIANT_MAX, nullptr); +#ifdef DEBUG_METHODS_ENABLED +#define bind_method(m_type, m_method, m_arg_names, m_default_args) \ + METHOD_CLASS(m_type, m_method, &m_type::m_method); \ + register_builtin_method<Method_##m_type##_##m_method>(m_arg_names, m_default_args); +#else +#define bind_method(m_type, m_method, m_arg_names, m_default_args) \ + METHOD_CLASS(m_type, m_method, &m_type ::m_method); \ + register_builtin_method<Method_##m_type##_##m_method>(sarray(), m_default_args); +#endif - Variant::InternalMethod **m = _VariantCall::type_internal_methods[p_type].lookup_ptr(p_method_name); - if (*m) { - return *m; - } - return nullptr; -} +#ifdef DEBUG_METHODS_ENABLED +#define bind_methodv(m_type, m_name, m_method, m_arg_names, m_default_args) \ + METHOD_CLASS(m_type, m_name, m_method); \ + register_builtin_method<Method_##m_type##_##m_name>(m_arg_names, m_default_args); +#else +#define bind_methodv(m_type, m_name, m_method, m_arg_names, m_default_args) \ + METHOD_CLASS(m_type, m_name, m_method); \ + register_builtin_method<Method_##m_type##_##m_name>(sarray(), m_default_args); +#endif -void register_variant_methods() { - _VariantCall::type_internal_methods = memnew_arr(_VariantCall::MethodMap, Variant::VARIANT_MAX); - _VariantCall::type_internal_method_names = memnew_arr(List<StringName>, Variant::VARIANT_MAX); - _VariantCall::construct_funcs = memnew_arr(_VariantCall::ConstructFunc, Variant::VARIANT_MAX); +#ifdef DEBUG_METHODS_ENABLED +#define bind_function(m_type, m_name, m_method, m_arg_names, m_default_args) \ + FUNCTION_CLASS(m_type, m_name, m_method); \ + register_builtin_method<Method_##m_type##_##m_name>(m_arg_names, m_default_args); +#else +#define bind_function(m_type, m_name, m_method, m_arg_names, m_default_args) \ + FUNCTION_CLASS(m_type, m_name, m_method); \ + register_builtin_method<Method_##m_type##_##m_name>(sarray(), m_default_args); +#endif + +#define bind_custom(m_type, m_name, m_method, m_has_return, m_ret_type) \ + VARARG_CLASS(m_type, m_name, m_method, m_has_return, m_ret_type) \ + register_builtin_method<Method_##m_type##_##m_name>(sarray(), Vector<Variant>()); + +static void _register_variant_builtin_methods() { _VariantCall::constant_data = memnew_arr(_VariantCall::ConstantData, Variant::VARIANT_MAX); + builtin_method_info = memnew_arr(BuiltinMethodMap, Variant::VARIANT_MAX); + builtin_method_names = memnew_arr(List<StringName>, Variant::VARIANT_MAX); /* String */ @@ -1363,7 +875,7 @@ void register_variant_methods() { bind_method(String, naturalnocasecmp_to, sarray("to"), varray()); bind_method(String, length, sarray(), varray()); bind_method(String, substr, sarray("from", "len"), varray(-1)); - bind_methodv(find, static_cast<int (String::*)(const String &, int) const>(&String::find), sarray("what", "from"), varray(0)); + bind_methodv(String, find, static_cast<int (String::*)(const String &, int) const>(&String::find), sarray("what", "from"), varray(0)); bind_method(String, count, sarray("what", "from", "to"), varray(0, 0)); bind_method(String, countn, sarray("what", "from", "to"), varray(0, 0)); bind_method(String, findn, sarray("what", "from"), varray(0)); @@ -1371,7 +883,7 @@ void register_variant_methods() { bind_method(String, rfindn, sarray("what", "from"), varray(-1)); bind_method(String, match, sarray("expr"), varray()); bind_method(String, matchn, sarray("expr"), varray()); - bind_methodv(begins_with, static_cast<bool (String::*)(const String &) const>(&String::begins_with), sarray("text"), varray()); + bind_methodv(String, begins_with, static_cast<bool (String::*)(const String &) const>(&String::begins_with), sarray("text"), varray()); bind_method(String, ends_with, sarray("text"), varray()); bind_method(String, is_subsequence_of, sarray("text"), varray()); bind_method(String, is_subsequence_ofi, sarray("text"), varray()); @@ -1379,7 +891,7 @@ void register_variant_methods() { bind_method(String, similarity, sarray("text"), varray()); bind_method(String, format, sarray("values", "placeholder"), varray("{_}")); - bind_methodv(replace, static_cast<String (String::*)(const String &, const String &) const>(&String::replace), sarray("what", "forwhat"), varray()); + bind_methodv(String, replace, static_cast<String (String::*)(const String &, const String &) const>(&String::replace), sarray("what", "forwhat"), varray()); bind_method(String, replacen, sarray("what", "forwhat"), varray()); bind_method(String, repeat, sarray("count"), varray()); bind_method(String, insert, sarray("position", "what"), varray()); @@ -1510,7 +1022,7 @@ void register_variant_methods() { bind_method(Rect2, merge, sarray("b"), varray()); bind_method(Rect2, expand, sarray("to"), varray()); bind_method(Rect2, grow, sarray("by"), varray()); - bind_methodv(grow_margin, &Rect2::grow_margin_bind, sarray("margin", "by"), varray()); + bind_methodv(Rect2, grow_margin, &Rect2::grow_margin_bind, sarray("margin", "by"), varray()); bind_method(Rect2, grow_individual, sarray("left", "top", "right", "bottom"), varray()); bind_method(Rect2, abs, sarray(), varray()); @@ -1525,7 +1037,7 @@ void register_variant_methods() { bind_method(Rect2i, merge, sarray("b"), varray()); bind_method(Rect2i, expand, sarray("to"), varray()); bind_method(Rect2i, grow, sarray("by"), varray()); - bind_methodv(grow_margin, &Rect2i::grow_margin_bind, sarray("margin", "by"), varray()); + bind_methodv(Rect2i, grow_margin, &Rect2i::grow_margin_bind, sarray("margin", "by"), varray()); bind_method(Rect2i, grow_individual, sarray("left", "top", "right", "bottom"), varray()); bind_method(Rect2i, abs, sarray(), varray()); @@ -1581,9 +1093,9 @@ void register_variant_methods() { bind_method(Plane, distance_to, sarray("point"), varray()); bind_method(Plane, has_point, sarray("point", "epsilon"), varray(CMP_EPSILON)); bind_method(Plane, project, sarray("point"), varray()); - bind_methodv(intersect_3, &Plane::intersect_3_bind, sarray("b", "c"), varray()); - bind_methodv(intersects_ray, &Plane::intersects_ray_bind, sarray("from", "dir"), varray()); - bind_methodv(intersects_segment, &Plane::intersects_segment_bind, sarray("from", "to"), varray()); + bind_methodv(Plane, intersect_3, &Plane::intersect_3_bind, sarray("b", "c"), varray()); + bind_methodv(Plane, intersects_ray, &Plane::intersects_ray_bind, sarray("from", "dir"), varray()); + bind_methodv(Plane, intersects_segment, &Plane::intersects_segment_bind, sarray("from", "to"), varray()); /* Quat */ @@ -1649,9 +1161,9 @@ void register_variant_methods() { bind_method(Callable, hash, sarray(), varray()); bind_method(Callable, unbind, sarray("argcount"), varray()); - bind_custom(Variant::CALLABLE, "call", _VariantCall::func_Callable_call, Variant::InternalMethod::FLAG_VARARGS | Variant::InternalMethod::FLAG_RETURNS_VARIANT, Vector<Variant::Type>(), Variant::NIL, sarray()); - bind_custom(Variant::CALLABLE, "call_deferred", _VariantCall::func_Callable_call_deferred, Variant::InternalMethod::FLAG_VARARGS, Vector<Variant::Type>(), Variant::NIL, sarray()); - bind_custom(Variant::CALLABLE, "bind", _VariantCall::func_Callable_bind, Variant::InternalMethod::FLAG_VARARGS, Vector<Variant::Type>(), Variant::CALLABLE, sarray()); + bind_custom(Callable, call, _VariantCall::func_Callable_call, true, Variant); + bind_custom(Callable, call_deferred, _VariantCall::func_Callable_call_deferred, false, Variant); + bind_custom(Callable, bind, _VariantCall::func_Callable_bind, true, Callable); /* Signal */ @@ -1665,7 +1177,7 @@ void register_variant_methods() { bind_method(Signal, is_connected, sarray("callable"), varray()); bind_method(Signal, get_connections, sarray(), varray()); - bind_custom(Variant::SIGNAL, "emit", _VariantCall::func_Signal_emit, Variant::InternalMethod::FLAG_VARARGS, Vector<Variant::Type>(), Variant::NIL, sarray()); + bind_custom(Signal, emit, _VariantCall::func_Signal_emit, false, Variant); /* Transform2D */ @@ -1689,7 +1201,7 @@ void register_variant_methods() { bind_method(Basis, transposed, sarray(), varray()); bind_method(Basis, orthonormalized, sarray(), varray()); bind_method(Basis, determinant, sarray(), varray()); - bind_methodv(rotated, static_cast<Basis (Basis::*)(const Vector3 &, float) const>(&Basis::rotated), sarray("axis", "phi"), varray()); + bind_methodv(Basis, rotated, static_cast<Basis (Basis::*)(const Vector3 &, float) const>(&Basis::rotated), sarray("axis", "phi"), varray()); bind_method(Basis, scaled, sarray("scale"), varray()); bind_method(Basis, get_scale, sarray(), varray()); bind_method(Basis, get_euler, sarray(), varray()); @@ -1703,29 +1215,29 @@ void register_variant_methods() { /* AABB */ - bind_method(::AABB, abs, sarray(), varray()); - bind_method(::AABB, get_area, sarray(), varray()); - bind_method(::AABB, has_no_area, sarray(), varray()); - bind_method(::AABB, has_no_surface, sarray(), varray()); - bind_method(::AABB, has_point, sarray("point"), varray()); - bind_method(::AABB, is_equal_approx, sarray("aabb"), varray()); - bind_method(::AABB, intersects, sarray("with"), varray()); - bind_method(::AABB, encloses, sarray("with"), varray()); - bind_method(::AABB, intersects_plane, sarray("plane"), varray()); - bind_method(::AABB, intersection, sarray("with"), varray()); - bind_method(::AABB, merge, sarray("with"), varray()); - bind_method(::AABB, expand, sarray("to_point"), varray()); - bind_method(::AABB, grow, sarray("by"), varray()); - bind_method(::AABB, get_support, sarray("dir"), varray()); - bind_method(::AABB, get_longest_axis, sarray(), varray()); - bind_method(::AABB, get_longest_axis_index, sarray(), varray()); - bind_method(::AABB, get_longest_axis_size, sarray(), varray()); - bind_method(::AABB, get_shortest_axis, sarray(), varray()); - bind_method(::AABB, get_shortest_axis_index, sarray(), varray()); - bind_method(::AABB, get_shortest_axis_size, sarray(), varray()); - bind_method(::AABB, get_endpoint, sarray("idx"), varray()); - bind_methodv(intersects_segment, &AABB::intersects_segment_bind, sarray("from", "to"), varray()); - bind_methodv(intersects_ray, &AABB::intersects_ray_bind, sarray("from", "dir"), varray()); + bind_method(AABB, abs, sarray(), varray()); + bind_method(AABB, get_area, sarray(), varray()); + bind_method(AABB, has_no_area, sarray(), varray()); + bind_method(AABB, has_no_surface, sarray(), varray()); + bind_method(AABB, has_point, sarray("point"), varray()); + bind_method(AABB, is_equal_approx, sarray("aabb"), varray()); + bind_method(AABB, intersects, sarray("with"), varray()); + bind_method(AABB, encloses, sarray("with"), varray()); + bind_method(AABB, intersects_plane, sarray("plane"), varray()); + bind_method(AABB, intersection, sarray("with"), varray()); + bind_method(AABB, merge, sarray("with"), varray()); + bind_method(AABB, expand, sarray("to_point"), varray()); + bind_method(AABB, grow, sarray("by"), varray()); + bind_method(AABB, get_support, sarray("dir"), varray()); + bind_method(AABB, get_longest_axis, sarray(), varray()); + bind_method(AABB, get_longest_axis_index, sarray(), varray()); + bind_method(AABB, get_longest_axis_size, sarray(), varray()); + bind_method(AABB, get_shortest_axis, sarray(), varray()); + bind_method(AABB, get_shortest_axis_index, sarray(), varray()); + bind_method(AABB, get_shortest_axis_size, sarray(), varray()); + bind_method(AABB, get_endpoint, sarray("idx"), varray()); + bind_methodv(AABB, intersects_segment, &AABB::intersects_segment_bind, sarray("from", "to"), varray()); + bind_methodv(AABB, intersects_ray, &AABB::intersects_ray_bind, sarray("from", "dir"), varray()); /* Transform */ @@ -1762,6 +1274,7 @@ void register_variant_methods() { bind_method(Array, push_back, sarray("value"), varray()); bind_method(Array, push_front, sarray("value"), varray()); bind_method(Array, append, sarray("value"), varray()); + bind_method(Array, append_array, sarray("array"), varray()); bind_method(Array, resize, sarray("size"), varray()); bind_method(Array, insert, sarray("position", "value"), varray()); bind_method(Array, remove, sarray("position"), varray()); @@ -1801,14 +1314,14 @@ void register_variant_methods() { bind_method(PackedByteArray, subarray, sarray("from", "to"), varray()); bind_method(PackedByteArray, sort, sarray(), varray()); - bind_function("get_string_from_ascii", _VariantCall::func_PackedByteArray_get_string_from_ascii, sarray(), varray()); - bind_function("get_string_from_utf8", _VariantCall::func_PackedByteArray_get_string_from_utf8, sarray(), varray()); - bind_function("get_string_from_utf16", _VariantCall::func_PackedByteArray_get_string_from_utf16, sarray(), varray()); - bind_function("get_string_from_utf32", _VariantCall::func_PackedByteArray_get_string_from_utf32, sarray(), varray()); - bind_function("hex_encode", _VariantCall::func_PackedByteArray_hex_encode, sarray(), varray()); - bind_function("compress", _VariantCall::func_PackedByteArray_compress, sarray("compression_mode"), varray(0)); - bind_function("decompress", _VariantCall::func_PackedByteArray_decompress, sarray("buffer_size", "compression_mode"), varray(0)); - bind_function("decompress_dynamic", _VariantCall::func_PackedByteArray_decompress_dynamic, sarray("max_output_size", "compression_mode"), varray(0)); + bind_function(PackedByteArray, get_string_from_ascii, _VariantCall::func_PackedByteArray_get_string_from_ascii, sarray(), varray()); + bind_function(PackedByteArray, get_string_from_utf8, _VariantCall::func_PackedByteArray_get_string_from_utf8, sarray(), varray()); + bind_function(PackedByteArray, get_string_from_utf16, _VariantCall::func_PackedByteArray_get_string_from_utf16, sarray(), varray()); + bind_function(PackedByteArray, get_string_from_utf32, _VariantCall::func_PackedByteArray_get_string_from_utf32, sarray(), varray()); + bind_function(PackedByteArray, hex_encode, _VariantCall::func_PackedByteArray_hex_encode, sarray(), varray()); + bind_function(PackedByteArray, compress, _VariantCall::func_PackedByteArray_compress, sarray("compression_mode"), varray(0)); + bind_function(PackedByteArray, decompress, _VariantCall::func_PackedByteArray_decompress, sarray("buffer_size", "compression_mode"), varray(0)); + bind_function(PackedByteArray, decompress_dynamic, _VariantCall::func_PackedByteArray_decompress_dynamic, sarray("max_output_size", "compression_mode"), varray(0)); /* Int32 Array */ @@ -1946,47 +1459,6 @@ void register_variant_methods() { bind_method(PackedColorArray, to_byte_array, sarray(), varray()); bind_method(PackedColorArray, sort, sarray(), varray()); - /* Register constructors */ - - _VariantCall::add_constructor(_VariantCall::Vector2_init1, Variant::VECTOR2, "x", Variant::FLOAT, "y", Variant::FLOAT); - _VariantCall::add_constructor(_VariantCall::Vector2i_init1, Variant::VECTOR2I, "x", Variant::INT, "y", Variant::INT); - - _VariantCall::add_constructor(_VariantCall::Rect2_init1, Variant::RECT2, "position", Variant::VECTOR2, "size", Variant::VECTOR2); - _VariantCall::add_constructor(_VariantCall::Rect2_init2, Variant::RECT2, "x", Variant::FLOAT, "y", Variant::FLOAT, "width", Variant::FLOAT, "height", Variant::FLOAT); - - _VariantCall::add_constructor(_VariantCall::Rect2i_init1, Variant::RECT2I, "position", Variant::VECTOR2I, "size", Variant::VECTOR2I); - _VariantCall::add_constructor(_VariantCall::Rect2i_init2, Variant::RECT2I, "x", Variant::INT, "y", Variant::INT, "width", Variant::INT, "height", Variant::INT); - - _VariantCall::add_constructor(_VariantCall::Transform2D_init2, Variant::TRANSFORM2D, "rotation", Variant::FLOAT, "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::FLOAT, "y", Variant::FLOAT, "z", Variant::FLOAT); - _VariantCall::add_constructor(_VariantCall::Vector3i_init1, Variant::VECTOR3I, "x", Variant::INT, "y", Variant::INT, "z", Variant::INT); - - _VariantCall::add_constructor(_VariantCall::Plane_init1, Variant::PLANE, "a", Variant::FLOAT, "b", Variant::FLOAT, "c", Variant::FLOAT, "d", Variant::FLOAT); - _VariantCall::add_constructor(_VariantCall::Plane_init2, Variant::PLANE, "v1", Variant::VECTOR3, "v2", Variant::VECTOR3, "v3", Variant::VECTOR3); - _VariantCall::add_constructor(_VariantCall::Plane_init3, Variant::PLANE, "normal", Variant::VECTOR3, "d", Variant::FLOAT); - - _VariantCall::add_constructor(_VariantCall::Quat_init1, Variant::QUAT, "x", Variant::FLOAT, "y", Variant::FLOAT, "z", Variant::FLOAT, "w", Variant::FLOAT); - _VariantCall::add_constructor(_VariantCall::Quat_init2, Variant::QUAT, "axis", Variant::VECTOR3, "angle", Variant::FLOAT); - _VariantCall::add_constructor(_VariantCall::Quat_init3, Variant::QUAT, "euler", Variant::VECTOR3); - - _VariantCall::add_constructor(_VariantCall::Color_init1, Variant::COLOR, "r", Variant::FLOAT, "g", Variant::FLOAT, "b", Variant::FLOAT, "a", Variant::FLOAT); - _VariantCall::add_constructor(_VariantCall::Color_init2, Variant::COLOR, "r", Variant::FLOAT, "g", Variant::FLOAT, "b", Variant::FLOAT); - // init3 and init4 are the constructors for HTML hex strings and integers respectively which don't need binding here, so we skip to init5. - _VariantCall::add_constructor(_VariantCall::Color_init5, Variant::COLOR, "c", Variant::COLOR, "a", Variant::FLOAT); - - _VariantCall::add_constructor(_VariantCall::AABB_init1, Variant::AABB, "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::FLOAT); - - _VariantCall::add_constructor(_VariantCall::Transform_init1, Variant::TRANSFORM, "x_axis", Variant::VECTOR3, "y_axis", Variant::VECTOR3, "z_axis", Variant::VECTOR3, "origin", Variant::VECTOR3); - _VariantCall::add_constructor(_VariantCall::Transform_init2, Variant::TRANSFORM, "basis", Variant::BASIS, "origin", Variant::VECTOR3); - - _VariantCall::add_constructor(_VariantCall::Callable_init2, Variant::CALLABLE, "object", Variant::OBJECT, "method_name", Variant::STRING_NAME); - _VariantCall::add_constructor(_VariantCall::Signal_init2, Variant::SIGNAL, "object", Variant::OBJECT, "signal_name", Variant::STRING_NAME); - /* Register constants */ int ncc = Color::get_named_color_count(); @@ -2071,19 +1543,13 @@ void register_variant_methods() { _VariantCall::add_variant_constant(Variant::QUAT, "IDENTITY", Quat(0, 0, 0, 1)); } -void unregister_variant_methods() { - //clear methods - for (int i = 0; i < Variant::VARIANT_MAX; i++) { - for (List<StringName>::Element *E = _VariantCall::type_internal_method_names[i].front(); E; E = E->next()) { - Variant::InternalMethod **m = _VariantCall::type_internal_methods[i].lookup_ptr(E->get()); - if (*m) { - memdelete(*m); - } - } - } +void Variant::_register_variant_methods() { + _register_variant_builtin_methods(); //needs to be out due to namespace +} - memdelete_arr(_VariantCall::type_internal_methods); - memdelete_arr(_VariantCall::type_internal_method_names); - memdelete_arr(_VariantCall::construct_funcs); +void Variant::_unregister_variant_methods() { + //clear methods + memdelete_arr(builtin_method_names); + memdelete_arr(builtin_method_info); memdelete_arr(_VariantCall::constant_data); } diff --git a/core/variant/variant_construct.cpp b/core/variant/variant_construct.cpp new file mode 100644 index 0000000000..01f5b7df59 --- /dev/null +++ b/core/variant/variant_construct.cpp @@ -0,0 +1,812 @@ +/*************************************************************************/ +/* variant_construct.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 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. */ +/*************************************************************************/ + +#include "variant.h" + +#include "core/core_string_names.h" +#include "core/crypto/crypto_core.h" +#include "core/debugger/engine_debugger.h" +#include "core/io/compression.h" +#include "core/object/class_db.h" +#include "core/os/os.h" +#include "core/templates/local_vector.h" +#include "core/templates/oa_hash_map.h" + +template <class T, class... P> +class VariantConstructor { + template <size_t... Is> + static _FORCE_INLINE_ void construct_helper(T &base, const Variant **p_args, Callable::CallError &r_error, IndexSequence<Is...>) { + r_error.error = Callable::CallError::CALL_OK; + +#ifdef DEBUG_METHODS_ENABLED + base = T(VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...); +#else + base = T(VariantCaster<P>::cast(*p_args[Is])...); +#endif + } + + template <size_t... Is> + static _FORCE_INLINE_ void validated_construct_helper(T &base, const Variant **p_args, IndexSequence<Is...>) { + base = T((*VariantGetInternalPtr<P>::get_ptr(p_args[Is]))...); + } + + template <size_t... Is> + static _FORCE_INLINE_ void ptr_construct_helper(void *base, const void **p_args, IndexSequence<Is...>) { + PtrToArg<T>::encode(T(PtrToArg<P>::convert(p_args[Is])...), base); + } + +public: + static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) { + r_error.error = Callable::CallError::CALL_OK; + VariantTypeChanger<T>::change(&r_ret); + construct_helper(*VariantGetInternalPtr<T>::get_ptr(&r_ret), p_args, r_error, BuildIndexSequence<sizeof...(P)>{}); + } + + static void validated_construct(Variant &r_ret, const Variant **p_args) { + VariantTypeChanger<T>::change(&r_ret); + validated_construct_helper(*VariantGetInternalPtr<T>::get_ptr(&r_ret), p_args, BuildIndexSequence<sizeof...(P)>{}); + } + static void ptr_construct(void *base, const void **p_args) { + ptr_construct_helper(base, p_args, BuildIndexSequence<sizeof...(P)>{}); + } + + static int get_argument_count() { + return sizeof...(P); + } + + static Variant::Type get_argument_type(int p_arg) { + return call_get_argument_type<P...>(p_arg); + } + + static Variant::Type get_base_type() { + return GetTypeInfo<T>::VARIANT_TYPE; + } +}; + +class VariantConstructorObject { +public: + static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) { + VariantInternal::clear(&r_ret); + if (p_args[0]->get_type() == Variant::NIL) { + VariantInternal::object_assign_null(&r_ret); + r_error.error = Callable::CallError::CALL_OK; + } else if (p_args[0]->get_type() == Variant::OBJECT) { + VariantInternal::object_assign(&r_ret, p_args[0]); + r_error.error = Callable::CallError::CALL_OK; + } else { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.argument = 0; + r_error.expected = Variant::OBJECT; + } + } + + static void validated_construct(Variant &r_ret, const Variant **p_args) { + VariantInternal::clear(&r_ret); + VariantInternal::object_assign(&r_ret, p_args[0]); + } + static void ptr_construct(void *base, const void **p_args) { + PtrToArg<Object *>::encode(PtrToArg<Object *>::convert(p_args[0]), base); + } + + static int get_argument_count() { + return 1; + } + + static Variant::Type get_argument_type(int p_arg) { + return Variant::OBJECT; + } + + static Variant::Type get_base_type() { + return Variant::OBJECT; + } +}; + +class VariantConstructorNilObject { +public: + static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) { + if (p_args[0]->get_type() != Variant::NIL) { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.argument = 0; + r_error.expected = Variant::NIL; + } + + VariantInternal::clear(&r_ret); + VariantInternal::object_assign_null(&r_ret); + } + + static void validated_construct(Variant &r_ret, const Variant **p_args) { + VariantInternal::clear(&r_ret); + VariantInternal::object_assign_null(&r_ret); + } + static void ptr_construct(void *base, const void **p_args) { + PtrToArg<Object *>::encode(nullptr, base); + } + + static int get_argument_count() { + return 1; + } + + static Variant::Type get_argument_type(int p_arg) { + return Variant::NIL; + } + + static Variant::Type get_base_type() { + return Variant::OBJECT; + } +}; + +class VariantConstructorCallableArgs { +public: + static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) { + ObjectID object_id; + StringName method; + + if (p_args[0]->get_type() == Variant::NIL) { + // leave as is + } else if (p_args[0]->get_type() == Variant::OBJECT) { + object_id = VariantInternal::get_object_id(p_args[0]); + } else { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.argument = 0; + r_error.expected = Variant::OBJECT; + return; + } + + if (p_args[1]->get_type() == Variant::STRING_NAME) { + method = *VariantGetInternalPtr<StringName>::get_ptr(p_args[1]); + } else if (p_args[1]->get_type() == Variant::STRING) { + method = *VariantGetInternalPtr<String>::get_ptr(p_args[1]); + } else { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.argument = 1; + r_error.expected = Variant::STRING_NAME; + return; + } + + VariantTypeChanger<Callable>::change(&r_ret); + *VariantGetInternalPtr<Callable>::get_ptr(&r_ret) = Callable(object_id, method); + } + + static void validated_construct(Variant &r_ret, const Variant **p_args) { + VariantTypeChanger<Callable>::change(&r_ret); + *VariantGetInternalPtr<Callable>::get_ptr(&r_ret) = Callable(VariantInternal::get_object_id(p_args[0]), *VariantGetInternalPtr<StringName>::get_ptr(p_args[1])); + } + static void ptr_construct(void *base, const void **p_args) { + PtrToArg<Callable>::encode(Callable(PtrToArg<Object *>::convert(p_args[0]), PtrToArg<StringName>::convert(p_args[1])), base); + } + + static int get_argument_count() { + return 2; + } + + static Variant::Type get_argument_type(int p_arg) { + if (p_arg == 0) { + return Variant::OBJECT; + } else { + return Variant::STRING_NAME; + } + } + + static Variant::Type get_base_type() { + return Variant::CALLABLE; + } +}; + +class VariantConstructorSignalArgs { +public: + static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) { + ObjectID object_id; + StringName method; + + if (p_args[0]->get_type() == Variant::NIL) { + // leave as is + } else if (p_args[0]->get_type() == Variant::OBJECT) { + object_id = VariantInternal::get_object_id(p_args[0]); + } else { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.argument = 0; + r_error.expected = Variant::OBJECT; + return; + } + + if (p_args[1]->get_type() == Variant::STRING_NAME) { + method = *VariantGetInternalPtr<StringName>::get_ptr(p_args[1]); + } else if (p_args[1]->get_type() == Variant::STRING) { + method = *VariantGetInternalPtr<String>::get_ptr(p_args[1]); + } else { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.argument = 1; + r_error.expected = Variant::STRING_NAME; + return; + } + + VariantTypeChanger<Signal>::change(&r_ret); + *VariantGetInternalPtr<Signal>::get_ptr(&r_ret) = Signal(object_id, method); + } + + static void validated_construct(Variant &r_ret, const Variant **p_args) { + VariantTypeChanger<Signal>::change(&r_ret); + *VariantGetInternalPtr<Signal>::get_ptr(&r_ret) = Signal(VariantInternal::get_object_id(p_args[0]), *VariantGetInternalPtr<StringName>::get_ptr(p_args[1])); + } + static void ptr_construct(void *base, const void **p_args) { + PtrToArg<Signal>::encode(Signal(PtrToArg<Object *>::convert(p_args[0]), PtrToArg<StringName>::convert(p_args[1])), base); + } + + static int get_argument_count() { + return 2; + } + + static Variant::Type get_argument_type(int p_arg) { + if (p_arg == 0) { + return Variant::OBJECT; + } else { + return Variant::STRING_NAME; + } + } + + static Variant::Type get_base_type() { + return Variant::SIGNAL; + } +}; + +template <class T> +class VariantConstructorToArray { +public: + static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) { + if (p_args[0]->get_type() != GetTypeInfo<T>::VARIANT_TYPE) { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.argument = 0; + r_error.expected = GetTypeInfo<T>::VARIANT_TYPE; + return; + } + + VariantTypeChanger<Array>::change(&r_ret); + Array &dst_arr = *VariantGetInternalPtr<Array>::get_ptr(&r_ret); + const T &src_arr = *VariantGetInternalPtr<T>::get_ptr(p_args[0]); + + int size = src_arr.size(); + dst_arr.resize(size); + for (int i = 0; i < size; i++) { + dst_arr[i] = src_arr[i]; + } + } + + static void validated_construct(Variant &r_ret, const Variant **p_args) { + VariantTypeChanger<Array>::change(&r_ret); + Array &dst_arr = *VariantGetInternalPtr<Array>::get_ptr(&r_ret); + const T &src_arr = *VariantGetInternalPtr<T>::get_ptr(p_args[0]); + + int size = src_arr.size(); + dst_arr.resize(size); + for (int i = 0; i < size; i++) { + dst_arr[i] = src_arr[i]; + } + } + static void ptr_construct(void *base, const void **p_args) { + Array dst_arr; + T src_arr = PtrToArg<T>::convert(p_args[0]); + + int size = src_arr.size(); + dst_arr.resize(size); + for (int i = 0; i < size; i++) { + dst_arr[i] = src_arr[i]; + } + + PtrToArg<Array>::encode(dst_arr, base); + } + + static int get_argument_count() { + return 1; + } + + static Variant::Type get_argument_type(int p_arg) { + return GetTypeInfo<T>::VARIANT_TYPE; + } + + static Variant::Type get_base_type() { + return Variant::ARRAY; + } +}; + +template <class T> +class VariantConstructorFromArray { +public: + static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) { + if (p_args[0]->get_type() != Variant::ARRAY) { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.argument = 0; + r_error.expected = Variant::ARRAY; + return; + } + + VariantTypeChanger<T>::change(&r_ret); + const Array &src_arr = *VariantGetInternalPtr<Array>::get_ptr(p_args[0]); + T &dst_arr = *VariantGetInternalPtr<T>::get_ptr(&r_ret); + + int size = src_arr.size(); + dst_arr.resize(size); + for (int i = 0; i < size; i++) { + dst_arr.write[i] = src_arr[i]; + } + } + + static void validated_construct(Variant &r_ret, const Variant **p_args) { + VariantTypeChanger<T>::change(&r_ret); + const Array &src_arr = *VariantGetInternalPtr<Array>::get_ptr(p_args[0]); + T &dst_arr = *VariantGetInternalPtr<T>::get_ptr(&r_ret); + + int size = src_arr.size(); + dst_arr.resize(size); + for (int i = 0; i < size; i++) { + dst_arr.write[i] = src_arr[i]; + } + } + static void ptr_construct(void *base, const void **p_args) { + Array src_arr = PtrToArg<Array>::convert(p_args[0]); + T dst_arr; + + int size = src_arr.size(); + dst_arr.resize(size); + for (int i = 0; i < size; i++) { + dst_arr.write[i] = src_arr[i]; + } + + PtrToArg<T>::encode(dst_arr, base); + } + + static int get_argument_count() { + return 1; + } + + static Variant::Type get_argument_type(int p_arg) { + return Variant::ARRAY; + } + + static Variant::Type get_base_type() { + return GetTypeInfo<T>::VARIANT_TYPE; + } +}; + +class VariantConstructorNil { +public: + static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) { + if (p_args[0]->get_type() != Variant::NIL) { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.argument = 0; + r_error.expected = Variant::NIL; + return; + } + + r_error.error = Callable::CallError::CALL_OK; + VariantInternal::clear(&r_ret); + } + + static void validated_construct(Variant &r_ret, const Variant **p_args) { + VariantInternal::clear(&r_ret); + } + static void ptr_construct(void *base, const void **p_args) { + PtrToArg<Variant>::encode(Variant(), base); + } + + static int get_argument_count() { + return 1; + } + + static Variant::Type get_argument_type(int p_arg) { + return Variant::NIL; + } + + static Variant::Type get_base_type() { + return Variant::NIL; + } +}; + +template <class T> +class VariantConstructNoArgs { +public: + static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) { + VariantTypeChanger<T>::change_and_reset(&r_ret); + r_error.error = Callable::CallError::CALL_OK; + } + + static void validated_construct(Variant &r_ret, const Variant **p_args) { + VariantTypeChanger<T>::change_and_reset(&r_ret); + } + static void ptr_construct(void *base, const void **p_args) { + PtrToArg<T>::encode(T(), base); + } + + static int get_argument_count() { + return 0; + } + + static Variant::Type get_argument_type(int p_arg) { + return Variant::NIL; + } + + static Variant::Type get_base_type() { + return GetTypeInfo<T>::VARIANT_TYPE; + } +}; + +class VariantConstructNoArgsNil { +public: + static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) { + VariantInternal::clear(&r_ret); + r_error.error = Callable::CallError::CALL_OK; + } + + static void validated_construct(Variant &r_ret, const Variant **p_args) { + VariantInternal::clear(&r_ret); + } + static void ptr_construct(void *base, const void **p_args) { + ERR_FAIL_MSG("can't ptrcall nil constructor"); + } + + static int get_argument_count() { + return 0; + } + + static Variant::Type get_argument_type(int p_arg) { + return Variant::NIL; + } + + static Variant::Type get_base_type() { + return Variant::NIL; + } +}; + +class VariantConstructNoArgsObject { +public: + static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) { + VariantInternal::clear(&r_ret); + VariantInternal::object_assign_null(&r_ret); + r_error.error = Callable::CallError::CALL_OK; + } + + static void validated_construct(Variant &r_ret, const Variant **p_args) { + VariantInternal::clear(&r_ret); + VariantInternal::object_assign_null(&r_ret); + } + static void ptr_construct(void *base, const void **p_args) { + PtrToArg<Object *>::encode(nullptr, base); + } + + static int get_argument_count() { + return 0; + } + + static Variant::Type get_argument_type(int p_arg) { + return Variant::NIL; + } + + static Variant::Type get_base_type() { + return Variant::OBJECT; + } +}; + +struct VariantConstructData { + void (*construct)(Variant &r_base, const Variant **p_args, Callable::CallError &r_error); + Variant::ValidatedConstructor validated_construct; + Variant::PTRConstructor ptr_construct; + Variant::Type (*get_argument_type)(int); + int argument_count; + Vector<String> arg_names; +}; + +static LocalVector<VariantConstructData> construct_data[Variant::VARIANT_MAX]; + +template <class T> +static void add_constructor(const Vector<String> &arg_names) { + ERR_FAIL_COND_MSG(arg_names.size() != T::get_argument_count(), "Argument names size mismatch for " + Variant::get_type_name(T::get_base_type()) + "."); + + VariantConstructData cd; + cd.construct = T::construct; + cd.validated_construct = T::validated_construct; + cd.ptr_construct = T::ptr_construct; + cd.get_argument_type = T::get_argument_type; + cd.argument_count = T::get_argument_count(); + cd.arg_names = arg_names; + construct_data[T::get_base_type()].push_back(cd); +} + +void Variant::_register_variant_constructors() { + add_constructor<VariantConstructNoArgsNil>(sarray()); + add_constructor<VariantConstructorNil>(sarray("from")); + + add_constructor<VariantConstructNoArgs<bool>>(sarray()); + add_constructor<VariantConstructor<bool, bool>>(sarray("from")); + add_constructor<VariantConstructor<bool, int64_t>>(sarray("from")); + add_constructor<VariantConstructor<bool, double>>(sarray("from")); + + add_constructor<VariantConstructNoArgs<int64_t>>(sarray()); + add_constructor<VariantConstructor<int64_t, int64_t>>(sarray("from")); + add_constructor<VariantConstructor<int64_t, double>>(sarray("from")); + add_constructor<VariantConstructor<int64_t, bool>>(sarray("from")); + + add_constructor<VariantConstructNoArgs<double>>(sarray()); + add_constructor<VariantConstructor<double, double>>(sarray("from")); + add_constructor<VariantConstructor<double, int64_t>>(sarray("from")); + add_constructor<VariantConstructor<double, bool>>(sarray("from")); + + add_constructor<VariantConstructNoArgs<String>>(sarray()); + add_constructor<VariantConstructor<String, String>>(sarray("from")); + add_constructor<VariantConstructor<String, StringName>>(sarray("from")); + add_constructor<VariantConstructor<String, NodePath>>(sarray("from")); + + add_constructor<VariantConstructNoArgs<Vector2>>(sarray()); + add_constructor<VariantConstructor<Vector2, Vector2>>(sarray("from")); + add_constructor<VariantConstructor<Vector2, Vector2i>>(sarray("from")); + add_constructor<VariantConstructor<Vector2, double, double>>(sarray("x", "y")); + + add_constructor<VariantConstructNoArgs<Vector2i>>(sarray()); + add_constructor<VariantConstructor<Vector2i, Vector2i>>(sarray("from")); + add_constructor<VariantConstructor<Vector2i, Vector2>>(sarray("from")); + add_constructor<VariantConstructor<Vector2i, int64_t, int64_t>>(sarray("x", "y")); + + add_constructor<VariantConstructNoArgs<Rect2>>(sarray()); + add_constructor<VariantConstructor<Rect2, Rect2>>(sarray("from")); + add_constructor<VariantConstructor<Rect2, Rect2i>>(sarray("from")); + add_constructor<VariantConstructor<Rect2, Vector2, Vector2>>(sarray("position", "size")); + add_constructor<VariantConstructor<Rect2, double, double, double, double>>(sarray("x", "y", "width", "height")); + + add_constructor<VariantConstructNoArgs<Rect2i>>(sarray()); + add_constructor<VariantConstructor<Rect2i, Rect2i>>(sarray("from")); + add_constructor<VariantConstructor<Rect2i, Rect2>>(sarray("from")); + add_constructor<VariantConstructor<Rect2i, Vector2i, Vector2i>>(sarray("position", "size")); + add_constructor<VariantConstructor<Rect2i, int64_t, int64_t, int64_t, int64_t>>(sarray("x", "y", "width", "height")); + + add_constructor<VariantConstructNoArgs<Vector3>>(sarray()); + add_constructor<VariantConstructor<Vector3, Vector3>>(sarray("from")); + add_constructor<VariantConstructor<Vector3, Vector3i>>(sarray("from")); + add_constructor<VariantConstructor<Vector3, double, double, double>>(sarray("x", "y", "z")); + + add_constructor<VariantConstructNoArgs<Vector3i>>(sarray()); + add_constructor<VariantConstructor<Vector3i, Vector3i>>(sarray("from")); + add_constructor<VariantConstructor<Vector3i, Vector3>>(sarray("from")); + add_constructor<VariantConstructor<Vector3i, int64_t, int64_t, int64_t>>(sarray("x", "y", "z")); + + add_constructor<VariantConstructNoArgs<Transform2D>>(sarray()); + add_constructor<VariantConstructor<Transform2D, Transform2D>>(sarray("from")); + add_constructor<VariantConstructor<Transform2D, float, Vector2>>(sarray("rotation", "position")); + add_constructor<VariantConstructor<Transform2D, Vector2, Vector2, Vector2>>(sarray("x_axis", "y_axis", "origin")); + + add_constructor<VariantConstructNoArgs<Plane>>(sarray()); + add_constructor<VariantConstructor<Plane, Plane>>(sarray("from")); + add_constructor<VariantConstructor<Plane, Vector3, double>>(sarray("normal", "d")); + add_constructor<VariantConstructor<Plane, Vector3, Vector3>>(sarray("point", "normal")); + add_constructor<VariantConstructor<Plane, Vector3, Vector3, Vector3>>(sarray("point1", "point2", "point3")); + add_constructor<VariantConstructor<Plane, double, double, double, double>>(sarray("a", "b", "c", "d")); + + add_constructor<VariantConstructNoArgs<Quat>>(sarray()); + add_constructor<VariantConstructor<Quat, Quat>>(sarray("from")); + add_constructor<VariantConstructor<Quat, Basis>>(sarray("from")); + add_constructor<VariantConstructor<Quat, Vector3>>(sarray("euler")); + add_constructor<VariantConstructor<Quat, Vector3, double>>(sarray("axis", "angle")); + add_constructor<VariantConstructor<Quat, Vector3, Vector3>>(sarray("arc_from", "arc_to")); + add_constructor<VariantConstructor<Quat, double, double, double, double>>(sarray("x", "y", "z", "w")); + + add_constructor<VariantConstructNoArgs<::AABB>>(sarray()); + add_constructor<VariantConstructor<::AABB, ::AABB>>(sarray("from")); + add_constructor<VariantConstructor<::AABB, Vector3, Vector3>>(sarray("position", "size")); + + add_constructor<VariantConstructNoArgs<Basis>>(sarray()); + add_constructor<VariantConstructor<Basis, Basis>>(sarray("from")); + add_constructor<VariantConstructor<Basis, Quat>>(sarray("from")); + add_constructor<VariantConstructor<Basis, Vector3>>(sarray("euler")); + add_constructor<VariantConstructor<Basis, Vector3, double>>(sarray("axis", "phi")); + add_constructor<VariantConstructor<Basis, Vector3, Vector3, Vector3>>(sarray("x_axis", "y_axis", "z_axis")); + + add_constructor<VariantConstructNoArgs<Transform>>(sarray()); + add_constructor<VariantConstructor<Transform, Transform>>(sarray("from")); + add_constructor<VariantConstructor<Transform, Basis, Vector3>>(sarray("basis", "origin")); + add_constructor<VariantConstructor<Transform, Vector3, Vector3, Vector3, Vector3>>(sarray("x_axis", "y_axis", "z_axis", "origin")); + + add_constructor<VariantConstructNoArgs<Color>>(sarray()); + add_constructor<VariantConstructor<Color, Color>>(sarray("from")); + add_constructor<VariantConstructor<Color, Color, double>>(sarray("from", "alpha")); + add_constructor<VariantConstructor<Color, double, double, double>>(sarray("r", "g", "b")); + add_constructor<VariantConstructor<Color, double, double, double, double>>(sarray("r", "g", "b", "a")); + + add_constructor<VariantConstructNoArgs<StringName>>(sarray()); + add_constructor<VariantConstructor<StringName, StringName>>(sarray("from")); + add_constructor<VariantConstructor<StringName, String>>(sarray("from")); + + add_constructor<VariantConstructNoArgs<NodePath>>(sarray()); + add_constructor<VariantConstructor<NodePath, NodePath>>(sarray("from")); + add_constructor<VariantConstructor<NodePath, String>>(sarray("from")); + + add_constructor<VariantConstructNoArgs<::RID>>(sarray()); + add_constructor<VariantConstructor<::RID, ::RID>>(sarray("from")); + + add_constructor<VariantConstructNoArgsObject>(sarray()); + add_constructor<VariantConstructorObject>(sarray("from")); + add_constructor<VariantConstructorNilObject>(sarray("from")); + + add_constructor<VariantConstructNoArgs<Callable>>(sarray()); + add_constructor<VariantConstructor<Callable, Callable>>(sarray("from")); + add_constructor<VariantConstructorCallableArgs>(sarray("object", "method")); + + add_constructor<VariantConstructNoArgs<Signal>>(sarray()); + add_constructor<VariantConstructor<Signal, Signal>>(sarray("from")); + add_constructor<VariantConstructorSignalArgs>(sarray("object", "signal")); + + add_constructor<VariantConstructNoArgs<Dictionary>>(sarray()); + add_constructor<VariantConstructor<Dictionary, Dictionary>>(sarray("from")); + + add_constructor<VariantConstructNoArgs<Array>>(sarray()); + add_constructor<VariantConstructor<Array, Array>>(sarray("from")); + add_constructor<VariantConstructorToArray<PackedByteArray>>(sarray("from")); + add_constructor<VariantConstructorToArray<PackedInt32Array>>(sarray("from")); + add_constructor<VariantConstructorToArray<PackedInt64Array>>(sarray("from")); + add_constructor<VariantConstructorToArray<PackedFloat32Array>>(sarray("from")); + add_constructor<VariantConstructorToArray<PackedFloat64Array>>(sarray("from")); + add_constructor<VariantConstructorToArray<PackedStringArray>>(sarray("from")); + add_constructor<VariantConstructorToArray<PackedVector2Array>>(sarray("from")); + add_constructor<VariantConstructorToArray<PackedVector3Array>>(sarray("from")); + add_constructor<VariantConstructorToArray<PackedColorArray>>(sarray("from")); + + add_constructor<VariantConstructNoArgs<PackedByteArray>>(sarray()); + add_constructor<VariantConstructor<PackedByteArray, PackedByteArray>>(sarray("from")); + add_constructor<VariantConstructorFromArray<PackedByteArray>>(sarray("from")); + + add_constructor<VariantConstructNoArgs<PackedInt32Array>>(sarray()); + add_constructor<VariantConstructor<PackedInt32Array, PackedInt32Array>>(sarray("from")); + add_constructor<VariantConstructorFromArray<PackedInt32Array>>(sarray("from")); + + add_constructor<VariantConstructNoArgs<PackedInt64Array>>(sarray()); + add_constructor<VariantConstructor<PackedInt64Array, PackedInt64Array>>(sarray("from")); + add_constructor<VariantConstructorFromArray<PackedInt64Array>>(sarray("from")); + + add_constructor<VariantConstructNoArgs<PackedFloat32Array>>(sarray()); + add_constructor<VariantConstructor<PackedFloat32Array, PackedFloat32Array>>(sarray("from")); + add_constructor<VariantConstructorFromArray<PackedFloat32Array>>(sarray("from")); + + add_constructor<VariantConstructNoArgs<PackedFloat64Array>>(sarray()); + add_constructor<VariantConstructor<PackedFloat64Array, PackedFloat64Array>>(sarray("from")); + add_constructor<VariantConstructorFromArray<PackedFloat64Array>>(sarray("from")); + + add_constructor<VariantConstructNoArgs<PackedStringArray>>(sarray()); + add_constructor<VariantConstructor<PackedStringArray, PackedStringArray>>(sarray("from")); + add_constructor<VariantConstructorFromArray<PackedStringArray>>(sarray("from")); + + add_constructor<VariantConstructNoArgs<PackedVector2Array>>(sarray()); + add_constructor<VariantConstructor<PackedVector2Array, PackedVector2Array>>(sarray("from")); + add_constructor<VariantConstructorFromArray<PackedVector2Array>>(sarray("from")); + + add_constructor<VariantConstructNoArgs<PackedVector3Array>>(sarray()); + add_constructor<VariantConstructor<PackedVector3Array, PackedVector3Array>>(sarray("from")); + add_constructor<VariantConstructorFromArray<PackedVector3Array>>(sarray("from")); + + add_constructor<VariantConstructNoArgs<PackedColorArray>>(sarray()); + add_constructor<VariantConstructor<PackedColorArray, PackedColorArray>>(sarray("from")); + add_constructor<VariantConstructorFromArray<PackedColorArray>>(sarray("from")); +} + +void Variant::_unregister_variant_constructors() { + for (int i = 0; i < Variant::VARIANT_MAX; i++) { + construct_data[i].clear(); + } +} + +void Variant::construct(Variant::Type p_type, Variant &base, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { + uint32_t s = construct_data[p_type].size(); + for (uint32_t i = 0; i < s; i++) { + int argc = construct_data[p_type][i].argument_count; + if (argc != p_argcount) { + continue; + } + bool args_match = true; + for (int j = 0; j < argc; j++) { + if (!Variant::can_convert_strict(p_args[j]->get_type(), construct_data[p_type][i].get_argument_type(j))) { + args_match = false; + break; + } + } + + if (!args_match) { + continue; + } + + construct_data[p_type][i].construct(base, p_args, r_error); + return; + } + + r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; +} + +int Variant::get_constructor_count(Variant::Type p_type) { + ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, -1); + return construct_data[p_type].size(); +} + +Variant::ValidatedConstructor Variant::get_validated_constructor(Variant::Type p_type, int p_constructor) { + ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, nullptr); + ERR_FAIL_INDEX_V(p_constructor, (int)construct_data[p_type].size(), nullptr); + return construct_data[p_type][p_constructor].validated_construct; +} + +Variant::PTRConstructor Variant::get_ptr_constructor(Variant::Type p_type, int p_constructor) { + ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, nullptr); + ERR_FAIL_INDEX_V(p_constructor, (int)construct_data[p_type].size(), nullptr); + return construct_data[p_type][p_constructor].ptr_construct; +} + +int Variant::get_constructor_argument_count(Variant::Type p_type, int p_constructor) { + ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, -1); + ERR_FAIL_INDEX_V(p_constructor, (int)construct_data[p_type].size(), -1); + return construct_data[p_type][p_constructor].argument_count; +} + +Variant::Type Variant::get_constructor_argument_type(Variant::Type p_type, int p_constructor, int p_argument) { + ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, Variant::VARIANT_MAX); + ERR_FAIL_INDEX_V(p_constructor, (int)construct_data[p_type].size(), Variant::VARIANT_MAX); + return construct_data[p_type][p_constructor].get_argument_type(p_argument); +} + +String Variant::get_constructor_argument_name(Variant::Type p_type, int p_constructor, int p_argument) { + ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, String()); + ERR_FAIL_INDEX_V(p_constructor, (int)construct_data[p_type].size(), String()); + return construct_data[p_type][p_constructor].arg_names[p_argument]; +} + +void VariantInternal::object_assign(Variant *v, const Variant *o) { + if (o->_get_obj().obj && o->_get_obj().id.is_reference()) { + Reference *reference = static_cast<Reference *>(o->_get_obj().obj); + if (!reference->reference()) { + v->_get_obj().obj = nullptr; + v->_get_obj().id = ObjectID(); + return; + } + } + + v->_get_obj().obj = const_cast<Object *>(o->_get_obj().obj); + v->_get_obj().id = o->_get_obj().id; +} + +void Variant::get_constructor_list(Type p_type, List<MethodInfo> *r_list) { + ERR_FAIL_INDEX(p_type, Variant::VARIANT_MAX); + + MethodInfo mi; + mi.return_val.type = p_type; + mi.name = get_type_name(p_type); + + for (int i = 0; i < get_constructor_count(p_type); i++) { + int ac = get_constructor_argument_count(p_type, i); + mi.arguments.clear(); + for (int j = 0; j < ac; j++) { + PropertyInfo arg; + arg.name = get_constructor_argument_name(p_type, i, j); + arg.type = get_constructor_argument_type(p_type, i, j); + mi.arguments.push_back(arg); + } + r_list->push_back(mi); + } +} diff --git a/core/variant/variant_internal.h b/core/variant/variant_internal.h index 7893c6d382..3ac7f32dec 100644 --- a/core/variant/variant_internal.h +++ b/core/variant/variant_internal.h @@ -83,8 +83,8 @@ public: _FORCE_INLINE_ static const StringName *get_string_name(const Variant *v) { return reinterpret_cast<const StringName *>(v->_data._mem); } _FORCE_INLINE_ static NodePath *get_node_path(Variant *v) { return reinterpret_cast<NodePath *>(v->_data._mem); } _FORCE_INLINE_ static const NodePath *get_node_path(const Variant *v) { return reinterpret_cast<const NodePath *>(v->_data._mem); } - _FORCE_INLINE_ static RID *get_rid(Variant *v) { return reinterpret_cast<RID *>(v->_data._mem); } - _FORCE_INLINE_ static const RID *get_rid(const Variant *v) { return reinterpret_cast<const RID *>(v->_data._mem); } + _FORCE_INLINE_ static ::RID *get_rid(Variant *v) { return reinterpret_cast<::RID *>(v->_data._mem); } + _FORCE_INLINE_ static const ::RID *get_rid(const Variant *v) { return reinterpret_cast<const ::RID *>(v->_data._mem); } _FORCE_INLINE_ static Callable *get_callable(Variant *v) { return reinterpret_cast<Callable *>(v->_data._mem); } _FORCE_INLINE_ static const Callable *get_callable(const Variant *v) { return reinterpret_cast<const Callable *>(v->_data._mem); } _FORCE_INLINE_ static Signal *get_signal(Variant *v) { return reinterpret_cast<Signal *>(v->_data._mem); } @@ -116,6 +116,106 @@ public: _FORCE_INLINE_ static Object **get_object(Variant *v) { return (Object **)&v->_get_obj().obj; } _FORCE_INLINE_ static const Object **get_object(const Variant *v) { return (const Object **)&v->_get_obj().obj; } + + _FORCE_INLINE_ static const ObjectID get_object_id(const Variant *v) { return v->_get_obj().id; } + + template <class T> + _FORCE_INLINE_ static void init_generic(Variant *v) { + v->type = GetTypeInfo<T>::VARIANT_TYPE; + } + + _FORCE_INLINE_ static void init_string(Variant *v) { + memnew_placement(v->_data._mem, String); + v->type = Variant::STRING; + } + + _FORCE_INLINE_ static void init_transform2d(Variant *v) { + v->_data._transform2d = memnew(Transform2D); + v->type = Variant::TRANSFORM2D; + } + _FORCE_INLINE_ static void init_aabb(Variant *v) { + v->_data._aabb = memnew(AABB); + v->type = Variant::AABB; + } + _FORCE_INLINE_ static void init_basis(Variant *v) { + v->_data._basis = memnew(Basis); + v->type = Variant::BASIS; + } + _FORCE_INLINE_ static void init_transform(Variant *v) { + v->_data._transform = memnew(Transform); + v->type = Variant::TRANSFORM; + } + _FORCE_INLINE_ static void init_string_name(Variant *v) { + memnew_placement(v->_data._mem, StringName); + v->type = Variant::STRING_NAME; + } + _FORCE_INLINE_ static void init_node_path(Variant *v) { + memnew_placement(v->_data._mem, NodePath); + v->type = Variant::NODE_PATH; + } + _FORCE_INLINE_ static void init_callable(Variant *v) { + memnew_placement(v->_data._mem, Callable); + v->type = Variant::CALLABLE; + } + _FORCE_INLINE_ static void init_signal(Variant *v) { + memnew_placement(v->_data._mem, Signal); + v->type = Variant::SIGNAL; + } + _FORCE_INLINE_ static void init_dictionary(Variant *v) { + memnew_placement(v->_data._mem, Dictionary); + v->type = Variant::DICTIONARY; + } + _FORCE_INLINE_ static void init_array(Variant *v) { + memnew_placement(v->_data._mem, Array); + v->type = Variant::ARRAY; + } + _FORCE_INLINE_ static void init_byte_array(Variant *v) { + v->_data.packed_array = Variant::PackedArrayRef<uint8_t>::create(Vector<uint8_t>()); + v->type = Variant::PACKED_BYTE_ARRAY; + } + _FORCE_INLINE_ static void init_int32_array(Variant *v) { + v->_data.packed_array = Variant::PackedArrayRef<int32_t>::create(Vector<int32_t>()); + v->type = Variant::PACKED_INT32_ARRAY; + } + _FORCE_INLINE_ static void init_int64_array(Variant *v) { + v->_data.packed_array = Variant::PackedArrayRef<int64_t>::create(Vector<int64_t>()); + v->type = Variant::PACKED_INT64_ARRAY; + } + _FORCE_INLINE_ static void init_float32_array(Variant *v) { + v->_data.packed_array = Variant::PackedArrayRef<float>::create(Vector<float>()); + v->type = Variant::PACKED_FLOAT32_ARRAY; + } + _FORCE_INLINE_ static void init_float64_array(Variant *v) { + v->_data.packed_array = Variant::PackedArrayRef<double>::create(Vector<double>()); + v->type = Variant::PACKED_FLOAT64_ARRAY; + } + _FORCE_INLINE_ static void init_string_array(Variant *v) { + v->_data.packed_array = Variant::PackedArrayRef<String>::create(Vector<String>()); + v->type = Variant::PACKED_STRING_ARRAY; + } + _FORCE_INLINE_ static void init_vector2_array(Variant *v) { + v->_data.packed_array = Variant::PackedArrayRef<Vector2>::create(Vector<Vector2>()); + v->type = Variant::PACKED_VECTOR2_ARRAY; + } + _FORCE_INLINE_ static void init_vector3_array(Variant *v) { + v->_data.packed_array = Variant::PackedArrayRef<Vector3>::create(Vector<Vector3>()); + v->type = Variant::PACKED_VECTOR3_ARRAY; + } + _FORCE_INLINE_ static void init_color_array(Variant *v) { + v->_data.packed_array = Variant::PackedArrayRef<Color>::create(Vector<Color>()); + v->type = Variant::PACKED_COLOR_ARRAY; + } + + _FORCE_INLINE_ static void clear(Variant *v) { + v->clear(); + } + + static void object_assign(Variant *v, const Variant *o); //needs to use reference, do away + + _FORCE_INLINE_ static void object_assign_null(Variant *v) { + v->_get_obj().obj = nullptr; + v->_get_obj().id = ObjectID(); + } }; template <class T> @@ -305,9 +405,9 @@ struct VariantGetInternalPtr<NodePath> { }; template <> -struct VariantGetInternalPtr<RID> { - static RID *get_ptr(Variant *v) { return VariantInternal::get_rid(v); } - static const RID *get_ptr(const Variant *v) { return VariantInternal::get_rid(v); } +struct VariantGetInternalPtr<::RID> { + static ::RID *get_ptr(Variant *v) { return VariantInternal::get_rid(v); } + static const ::RID *get_ptr(const Variant *v) { return VariantInternal::get_rid(v); } }; template <> @@ -532,9 +632,9 @@ struct VariantInternalAccessor<NodePath> { }; template <> -struct VariantInternalAccessor<RID> { - static _FORCE_INLINE_ const RID &get(const Variant *v) { return *VariantInternal::get_rid(v); } - static _FORCE_INLINE_ void set(Variant *v, const RID &p_value) { *VariantInternal::get_rid(v) = p_value; } +struct VariantInternalAccessor<::RID> { + static _FORCE_INLINE_ const ::RID &get(const Variant *v) { return *VariantInternal::get_rid(v); } + static _FORCE_INLINE_ void set(Variant *v, const ::RID &p_value) { *VariantInternal::get_rid(v) = p_value; } }; template <> @@ -649,4 +749,383 @@ struct VariantInternalAccessor<Vector<Variant>> { } }; +template <class T> +struct VariantInitializer { +}; + +template <> +struct VariantInitializer<bool> { + static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_generic<bool>(v); } +}; + +#define INITIALIZER_INT(m_type) \ + template <> \ + struct VariantInitializer<m_type> { \ + static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_generic<int64_t>(v); } \ + }; + +INITIALIZER_INT(uint8_t) +INITIALIZER_INT(int8_t) +INITIALIZER_INT(uint16_t) +INITIALIZER_INT(int16_t) +INITIALIZER_INT(uint32_t) +INITIALIZER_INT(int32_t) +INITIALIZER_INT(uint64_t) +INITIALIZER_INT(int64_t) +INITIALIZER_INT(char32_t) +INITIALIZER_INT(Error) +INITIALIZER_INT(ObjectID) + +template <> +struct VariantInitializer<double> { + static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_generic<double>(v); } +}; + +template <> +struct VariantInitializer<float> { + static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_generic<double>(v); } +}; + +template <> +struct VariantInitializer<String> { + static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_string(v); } +}; + +template <> +struct VariantInitializer<Vector2> { + static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_generic<Vector2>(v); } +}; + +template <> +struct VariantInitializer<Vector2i> { + static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_generic<Vector2i>(v); } +}; + +template <> +struct VariantInitializer<Rect2> { + static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_generic<Rect2>(v); } +}; + +template <> +struct VariantInitializer<Rect2i> { + static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_generic<Rect2i>(v); } +}; + +template <> +struct VariantInitializer<Vector3> { + static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_generic<Vector3>(v); } +}; + +template <> +struct VariantInitializer<Vector3i> { + static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_generic<Vector3i>(v); } +}; + +template <> +struct VariantInitializer<Transform2D> { + static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_transform2d(v); } +}; + +template <> +struct VariantInitializer<Plane> { + static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_generic<Plane>(v); } +}; + +template <> +struct VariantInitializer<Quat> { + static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_generic<Quat>(v); } +}; + +template <> +struct VariantInitializer<AABB> { + static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_aabb(v); } +}; + +template <> +struct VariantInitializer<Basis> { + static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_basis(v); } +}; + +template <> +struct VariantInitializer<Transform> { + static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_transform(v); } +}; + +template <> +struct VariantInitializer<Color> { + static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_generic<Color>(v); } +}; + +template <> +struct VariantInitializer<StringName> { + static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_string_name(v); } +}; + +template <> +struct VariantInitializer<NodePath> { + static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_node_path(v); } +}; + +template <> +struct VariantInitializer<::RID> { + static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_generic<::RID>(v); } +}; + +template <> +struct VariantInitializer<Callable> { + static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_callable(v); } +}; + +template <> +struct VariantInitializer<Signal> { + static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_signal(v); } +}; + +template <> +struct VariantInitializer<Dictionary> { + static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_dictionary(v); } +}; + +template <> +struct VariantInitializer<Array> { + static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_array(v); } +}; + +template <> +struct VariantInitializer<PackedByteArray> { + static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_byte_array(v); } +}; + +template <> +struct VariantInitializer<PackedInt32Array> { + static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_int32_array(v); } +}; + +template <> +struct VariantInitializer<PackedInt64Array> { + static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_int64_array(v); } +}; + +template <> +struct VariantInitializer<PackedFloat32Array> { + static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_float32_array(v); } +}; + +template <> +struct VariantInitializer<PackedFloat64Array> { + static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_float64_array(v); } +}; + +template <> +struct VariantInitializer<PackedStringArray> { + static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_string_array(v); } +}; + +template <> +struct VariantInitializer<PackedVector2Array> { + static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_vector2_array(v); } +}; + +template <> +struct VariantInitializer<PackedVector3Array> { + static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_vector3_array(v); } +}; + +template <> +struct VariantInitializer<PackedColorArray> { + static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_color_array(v); } +}; + +template <class T> +struct VariantZeroAssigner { +}; + +template <> +struct VariantZeroAssigner<bool> { + static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_bool(v) = false; } +}; + +template <> +struct VariantZeroAssigner<int64_t> { + static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_int(v) = 0; } +}; + +template <> +struct VariantZeroAssigner<double> { + static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_float(v) = 0.0; } +}; + +template <> +struct VariantZeroAssigner<float> { + static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_float(v) = 0.0; } +}; + +template <> +struct VariantZeroAssigner<String> { + static _FORCE_INLINE_ void zero(Variant *v) {} +}; + +template <> +struct VariantZeroAssigner<Vector2> { + static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_vector2(v) = Vector2(); } +}; + +template <> +struct VariantZeroAssigner<Vector2i> { + static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_vector2i(v) = Vector2i(); } +}; + +template <> +struct VariantZeroAssigner<Rect2> { + static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_rect2(v) = Rect2(); } +}; + +template <> +struct VariantZeroAssigner<Rect2i> { + static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_rect2i(v) = Rect2i(); } +}; + +template <> +struct VariantZeroAssigner<Vector3> { + static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_vector3(v) = Vector3(); } +}; + +template <> +struct VariantZeroAssigner<Vector3i> { + static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_vector3i(v) = Vector3i(); } +}; + +template <> +struct VariantZeroAssigner<Transform2D> { + static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_transform2d(v) = Transform2D(); } +}; + +template <> +struct VariantZeroAssigner<Plane> { + static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_plane(v) = Plane(); } +}; + +template <> +struct VariantZeroAssigner<Quat> { + static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_quat(v) = Quat(); } +}; + +template <> +struct VariantZeroAssigner<AABB> { + static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_aabb(v) = AABB(); } +}; + +template <> +struct VariantZeroAssigner<Basis> { + static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_basis(v) = Basis(); } +}; + +template <> +struct VariantZeroAssigner<Transform> { + static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_transform(v) = Transform(); } +}; + +template <> +struct VariantZeroAssigner<Color> { + static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_color(v) = Color(); } +}; + +template <> +struct VariantZeroAssigner<StringName> { + static _FORCE_INLINE_ void zero(Variant *v) {} +}; + +template <> +struct VariantZeroAssigner<NodePath> { + static _FORCE_INLINE_ void zero(Variant *v) {} +}; + +template <> +struct VariantZeroAssigner<::RID> { + static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_rid(v) = RID(); } +}; + +template <> +struct VariantZeroAssigner<Callable> { + static _FORCE_INLINE_ void zero(Variant *v) {} +}; + +template <> +struct VariantZeroAssigner<Signal> { + static _FORCE_INLINE_ void zero(Variant *v) {} +}; + +template <> +struct VariantZeroAssigner<Dictionary> { + static _FORCE_INLINE_ void zero(Variant *v) {} +}; + +template <> +struct VariantZeroAssigner<Array> { + static _FORCE_INLINE_ void zero(Variant *v) {} +}; + +template <> +struct VariantZeroAssigner<PackedByteArray> { + static _FORCE_INLINE_ void zero(Variant *v) {} +}; + +template <> +struct VariantZeroAssigner<PackedInt32Array> { + static _FORCE_INLINE_ void zero(Variant *v) {} +}; + +template <> +struct VariantZeroAssigner<PackedInt64Array> { + static _FORCE_INLINE_ void zero(Variant *v) {} +}; + +template <> +struct VariantZeroAssigner<PackedFloat32Array> { + static _FORCE_INLINE_ void zero(Variant *v) {} +}; + +template <> +struct VariantZeroAssigner<PackedFloat64Array> { + static _FORCE_INLINE_ void zero(Variant *v) {} +}; + +template <> +struct VariantZeroAssigner<PackedStringArray> { + static _FORCE_INLINE_ void zero(Variant *v) {} +}; + +template <> +struct VariantZeroAssigner<PackedVector2Array> { + static _FORCE_INLINE_ void zero(Variant *v) {} +}; + +template <> +struct VariantZeroAssigner<PackedVector3Array> { + static _FORCE_INLINE_ void zero(Variant *v) {} +}; + +template <> +struct VariantZeroAssigner<PackedColorArray> { + static _FORCE_INLINE_ void zero(Variant *v) {} +}; + +template <class T> +struct VariantTypeChanger { + static _FORCE_INLINE_ void change(Variant *v) { + if (v->get_type() != GetTypeInfo<T>::VARIANT_TYPE || GetTypeInfo<T>::VARIANT_TYPE >= Variant::PACKED_BYTE_ARRAY) { //second condition removed by optimizer + VariantInternal::clear(v); + VariantInitializer<T>::init(v); + } + } + static _FORCE_INLINE_ void change_and_reset(Variant *v) { + if (v->get_type() != GetTypeInfo<T>::VARIANT_TYPE || GetTypeInfo<T>::VARIANT_TYPE >= Variant::PACKED_BYTE_ARRAY) { //second condition removed by optimizer + VariantInternal::clear(v); + VariantInitializer<T>::init(v); + } + + VariantZeroAssigner<T>::zero(v); + } +}; + #endif // VARIANT_INTERNAL_H diff --git a/core/variant/variant_op.cpp b/core/variant/variant_op.cpp index 25ba66c9de..8e55c1d6cd 100644 --- a/core/variant/variant_op.cpp +++ b/core/variant/variant_op.cpp @@ -44,6 +44,7 @@ public: r_valid = true; } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger<R>::change(r_ret); *VariantGetInternalPtr<R>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) + *VariantGetInternalPtr<B>::get_ptr(right); } #ifdef PTRCALL_ENABLED @@ -64,6 +65,7 @@ public: r_valid = true; } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger<R>::change(r_ret); *VariantGetInternalPtr<R>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) - *VariantGetInternalPtr<B>::get_ptr(right); } #ifdef PTRCALL_ENABLED @@ -84,6 +86,7 @@ public: r_valid = true; } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger<R>::change(r_ret); *VariantGetInternalPtr<R>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) * *VariantGetInternalPtr<B>::get_ptr(right); } #ifdef PTRCALL_ENABLED @@ -104,6 +107,7 @@ public: r_valid = true; } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger<R>::change(r_ret); *VariantGetInternalPtr<R>::get_ptr(r_ret) = VariantGetInternalPtr<A>::get_ptr(left)->xform(*VariantGetInternalPtr<B>::get_ptr(right)); } #ifdef PTRCALL_ENABLED @@ -124,6 +128,7 @@ public: r_valid = true; } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger<R>::change(r_ret); *VariantGetInternalPtr<R>::get_ptr(r_ret) = VariantGetInternalPtr<B>::get_ptr(right)->xform_inv(*VariantGetInternalPtr<A>::get_ptr(left)); } #ifdef PTRCALL_ENABLED @@ -144,6 +149,7 @@ public: r_valid = true; } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger<R>::change(r_ret); *VariantGetInternalPtr<R>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) / *VariantGetInternalPtr<B>::get_ptr(right); } #ifdef PTRCALL_ENABLED @@ -169,6 +175,7 @@ public: r_valid = true; } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger<R>::change(r_ret); *VariantGetInternalPtr<R>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) / *VariantGetInternalPtr<B>::get_ptr(right); } #ifdef PTRCALL_ENABLED @@ -189,6 +196,7 @@ public: r_valid = true; } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger<R>::change(r_ret); *VariantGetInternalPtr<R>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) % *VariantGetInternalPtr<B>::get_ptr(right); } #ifdef PTRCALL_ENABLED @@ -214,6 +222,7 @@ public: r_valid = true; } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger<R>::change(r_ret); *VariantGetInternalPtr<R>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) % *VariantGetInternalPtr<B>::get_ptr(right); } #ifdef PTRCALL_ENABLED @@ -233,6 +242,7 @@ public: r_valid = true; } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger<R>::change(r_ret); *VariantGetInternalPtr<R>::get_ptr(r_ret) = -*VariantGetInternalPtr<A>::get_ptr(left); } #ifdef PTRCALL_ENABLED @@ -252,6 +262,7 @@ public: r_valid = true; } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger<R>::change(r_ret); *VariantGetInternalPtr<R>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left); } #ifdef PTRCALL_ENABLED @@ -272,6 +283,7 @@ public: r_valid = true; } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger<R>::change(r_ret); *VariantGetInternalPtr<R>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) << *VariantGetInternalPtr<B>::get_ptr(right); } #ifdef PTRCALL_ENABLED @@ -292,6 +304,7 @@ public: r_valid = true; } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger<R>::change(r_ret); *VariantGetInternalPtr<R>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) >> *VariantGetInternalPtr<B>::get_ptr(right); } #ifdef PTRCALL_ENABLED @@ -312,6 +325,7 @@ public: r_valid = true; } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger<R>::change(r_ret); *VariantGetInternalPtr<R>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) | *VariantGetInternalPtr<B>::get_ptr(right); } #ifdef PTRCALL_ENABLED @@ -352,6 +366,7 @@ public: r_valid = true; } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger<R>::change(r_ret); *VariantGetInternalPtr<R>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) ^ *VariantGetInternalPtr<B>::get_ptr(right); } #ifdef PTRCALL_ENABLED @@ -371,6 +386,7 @@ public: r_valid = true; } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger<R>::change(r_ret); *VariantGetInternalPtr<R>::get_ptr(r_ret) = ~*VariantGetInternalPtr<A>::get_ptr(left); } #ifdef PTRCALL_ENABLED @@ -391,6 +407,7 @@ public: r_valid = true; } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger<bool>::change(r_ret); *VariantGetInternalPtr<bool>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) == *VariantGetInternalPtr<B>::get_ptr(right); } #ifdef PTRCALL_ENABLED @@ -412,6 +429,7 @@ public: static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { const Object *a = left->get_validated_object(); const Object *b = right->get_validated_object(); + VariantTypeChanger<bool>::change(r_ret); *VariantGetInternalPtr<bool>::get_ptr(r_ret) = a == b; } #ifdef PTRCALL_ENABLED @@ -431,6 +449,7 @@ public: } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { const Object *a = left->get_validated_object(); + VariantTypeChanger<bool>::change(r_ret); *VariantGetInternalPtr<bool>::get_ptr(r_ret) = a == nullptr; } #ifdef PTRCALL_ENABLED @@ -450,6 +469,7 @@ public: } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { const Object *b = right->get_validated_object(); + VariantTypeChanger<bool>::change(r_ret); *VariantGetInternalPtr<bool>::get_ptr(r_ret) = nullptr == b; } #ifdef PTRCALL_ENABLED @@ -470,6 +490,7 @@ public: r_valid = true; } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger<bool>::change(r_ret); *VariantGetInternalPtr<bool>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) != *VariantGetInternalPtr<B>::get_ptr(right); } #ifdef PTRCALL_ENABLED @@ -491,6 +512,7 @@ public: static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { Object *a = left->get_validated_object(); Object *b = right->get_validated_object(); + VariantTypeChanger<bool>::change(r_ret); *VariantGetInternalPtr<bool>::get_ptr(r_ret) = a != b; } #ifdef PTRCALL_ENABLED @@ -510,6 +532,7 @@ public: } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { Object *a = left->get_validated_object(); + VariantTypeChanger<bool>::change(r_ret); *VariantGetInternalPtr<bool>::get_ptr(r_ret) = a != nullptr; } #ifdef PTRCALL_ENABLED @@ -529,6 +552,7 @@ public: } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { Object *b = right->get_validated_object(); + VariantTypeChanger<bool>::change(r_ret); *VariantGetInternalPtr<bool>::get_ptr(r_ret) = nullptr != b; } #ifdef PTRCALL_ENABLED @@ -549,6 +573,7 @@ public: r_valid = true; } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger<bool>::change(r_ret); *VariantGetInternalPtr<bool>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) < *VariantGetInternalPtr<B>::get_ptr(right); } #ifdef PTRCALL_ENABLED @@ -569,6 +594,7 @@ public: r_valid = true; } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger<bool>::change(r_ret); *VariantGetInternalPtr<bool>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) <= *VariantGetInternalPtr<B>::get_ptr(right); } #ifdef PTRCALL_ENABLED @@ -589,6 +615,7 @@ public: r_valid = true; } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger<bool>::change(r_ret); *VariantGetInternalPtr<bool>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) > *VariantGetInternalPtr<B>::get_ptr(right); } #ifdef PTRCALL_ENABLED @@ -609,6 +636,7 @@ public: r_valid = true; } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger<bool>::change(r_ret); *VariantGetInternalPtr<bool>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) >= *VariantGetInternalPtr<B>::get_ptr(right); } #ifdef PTRCALL_ENABLED @@ -629,6 +657,7 @@ public: r_valid = true; } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger<bool>::change(r_ret); *VariantGetInternalPtr<bool>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) && *VariantGetInternalPtr<B>::get_ptr(right); } #ifdef PTRCALL_ENABLED @@ -650,6 +679,7 @@ public: r_valid = true; } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger<bool>::change(r_ret); *VariantGetInternalPtr<bool>::get_ptr(r_ret) = *VariantGetInternalPtr<A>::get_ptr(left) || *VariantGetInternalPtr<B>::get_ptr(right); } #ifdef PTRCALL_ENABLED @@ -675,6 +705,7 @@ public: r_valid = true; } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger<bool>::change(r_ret); *VariantGetInternalPtr<bool>::get_ptr(r_ret) = xor_op(*VariantGetInternalPtr<A>::get_ptr(left), *VariantGetInternalPtr<B>::get_ptr(right)); } #ifdef PTRCALL_ENABLED @@ -695,6 +726,7 @@ public: r_valid = true; } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger<bool>::change(r_ret); *VariantGetInternalPtr<bool>::get_ptr(r_ret) = !*VariantGetInternalPtr<A>::get_ptr(left); } #ifdef PTRCALL_ENABLED @@ -729,6 +761,7 @@ public: r_valid = true; } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger<Array>::change(r_ret); _add_arrays(*VariantGetInternalPtr<Array>::get_ptr(r_ret), *VariantGetInternalPtr<Array>::get_ptr(left), *VariantGetInternalPtr<Array>::get_ptr(right)); } #ifdef PTRCALL_ENABLED @@ -754,6 +787,7 @@ public: r_valid = true; } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger<Vector<T>>::change(r_ret); *VariantGetInternalPtr<Vector<T>>::get_ptr(r_ret) = *VariantGetInternalPtr<Vector<T>>::get_ptr(left); VariantGetInternalPtr<Vector<T>>::get_ptr(r_ret)->append_array(*VariantGetInternalPtr<Vector<T>>::get_ptr(right)); } @@ -787,6 +821,7 @@ public: } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger<String>::change(r_ret); *VariantGetInternalPtr<String>::get_ptr(r_ret) = do_mod(*VariantGetInternalPtr<String>::get_ptr(left), nullptr); } #ifdef PTRCALL_ENABLED @@ -813,6 +848,7 @@ public: } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger<String>::change(r_ret); *VariantGetInternalPtr<String>::get_ptr(r_ret) = do_mod(*VariantGetInternalPtr<String>::get_ptr(left), *VariantGetInternalPtr<Array>::get_ptr(right), nullptr); } #ifdef PTRCALL_ENABLED @@ -842,6 +878,7 @@ public: } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger<String>::change(r_ret); *VariantGetInternalPtr<String>::get_ptr(r_ret) = do_mod(*VariantGetInternalPtr<String>::get_ptr(left), right->get_validated_object(), nullptr); } #ifdef PTRCALL_ENABLED @@ -871,6 +908,7 @@ public: } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger<String>::change(r_ret); *VariantGetInternalPtr<String>::get_ptr(r_ret) = do_mod(*VariantGetInternalPtr<String>::get_ptr(left), *VariantGetInternalPtr<T>::get_ptr(right), nullptr); } #ifdef PTRCALL_ENABLED @@ -890,6 +928,7 @@ public: } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger<bool>::change(r_ret); *VariantGetInternalPtr<bool>::get_ptr(r_ret) = true; } #ifdef PTRCALL_ENABLED @@ -909,6 +948,7 @@ public: } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger<bool>::change(r_ret); *VariantGetInternalPtr<bool>::get_ptr(r_ret) = false; } #ifdef PTRCALL_ENABLED @@ -962,6 +1002,7 @@ _FORCE_INLINE_ static bool _operate_get_object(const Variant *p_ptr) { } \ \ static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { \ + VariantTypeChanger<bool>::change(r_ret); \ *VariantGetInternalPtr<bool>::get_ptr(r_ret) = m_op(_operate_get_##m_left(left), _operate_get_##m_right(right)); \ } \ \ @@ -1002,6 +1043,7 @@ _FORCE_INLINE_ static bool _operate_get_ptr_object(const void *p_ptr) { } \ \ static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { \ + VariantTypeChanger<bool>::change(r_ret); \ *VariantGetInternalPtr<bool>::get_ptr(r_ret) = m_op(_operate_get_##m_left(left), _operate_get_##m_right(right)); \ } \ \ @@ -1168,6 +1210,7 @@ public: } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger<bool>::change(r_ret); *VariantGetInternalPtr<bool>::get_ptr(r_ret) = !*VariantGetInternalPtr<bool>::get_ptr(left); } #ifdef PTRCALL_ENABLED @@ -1187,6 +1230,7 @@ public: } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger<bool>::change(r_ret); *VariantGetInternalPtr<bool>::get_ptr(r_ret) = !*VariantGetInternalPtr<int64_t>::get_ptr(left); } #ifdef PTRCALL_ENABLED @@ -1206,6 +1250,7 @@ public: } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger<bool>::change(r_ret); *VariantGetInternalPtr<bool>::get_ptr(r_ret) = !*VariantGetInternalPtr<double>::get_ptr(left); } #ifdef PTRCALL_ENABLED @@ -1225,6 +1270,7 @@ public: } static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { + VariantTypeChanger<bool>::change(r_ret); *VariantGetInternalPtr<bool>::get_ptr(r_ret) = left->get_validated_object() == nullptr; } #ifdef PTRCALL_ENABLED @@ -1251,6 +1297,7 @@ public: static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { const String &str_a = *VariantGetInternalPtr<String>::get_ptr(left); const String &str_b = *VariantGetInternalPtr<String>::get_ptr(right); + VariantTypeChanger<bool>::change(r_ret); *VariantGetInternalPtr<bool>::get_ptr(r_ret) = str_b.find(str_a) != -1; } #ifdef PTRCALL_ENABLED @@ -1275,6 +1322,7 @@ public: static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { const A &a = *VariantGetInternalPtr<A>::get_ptr(left); const B &b = *VariantGetInternalPtr<B>::get_ptr(right); + VariantTypeChanger<bool>::change(r_ret); *VariantGetInternalPtr<bool>::get_ptr(r_ret) = b.find(a) != -1; } #ifdef PTRCALL_ENABLED @@ -1295,6 +1343,7 @@ public: static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { const Array &b = *VariantGetInternalPtr<Array>::get_ptr(right); + VariantTypeChanger<bool>::change(r_ret); *VariantGetInternalPtr<bool>::get_ptr(r_ret) = b.find(Variant()) != -1; } #ifdef PTRCALL_ENABLED @@ -1315,6 +1364,7 @@ public: static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { const Array &b = *VariantGetInternalPtr<Array>::get_ptr(right); + VariantTypeChanger<bool>::change(r_ret); *VariantGetInternalPtr<bool>::get_ptr(r_ret) = b.find(*left) != -1; } #ifdef PTRCALL_ENABLED @@ -1339,6 +1389,7 @@ public: static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { const Dictionary &b = *VariantGetInternalPtr<Dictionary>::get_ptr(right); const A &a = *VariantGetInternalPtr<A>::get_ptr(left); + VariantTypeChanger<bool>::change(r_ret); *VariantGetInternalPtr<bool>::get_ptr(r_ret) = b.has(a); } #ifdef PTRCALL_ENABLED @@ -1360,6 +1411,7 @@ public: static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { const Dictionary &b = *VariantGetInternalPtr<Dictionary>::get_ptr(right); + VariantTypeChanger<bool>::change(r_ret); *VariantGetInternalPtr<bool>::get_ptr(r_ret) = b.has(Variant()); } #ifdef PTRCALL_ENABLED @@ -1381,6 +1433,7 @@ public: static void validated_evaluate(const Variant *left, const Variant *right, Variant *r_ret) { const Dictionary &b = *VariantGetInternalPtr<Dictionary>::get_ptr(right); + VariantTypeChanger<bool>::change(r_ret); *VariantGetInternalPtr<bool>::get_ptr(r_ret) = b.has(*left); } #ifdef PTRCALL_ENABLED @@ -1414,6 +1467,7 @@ public: bool valid; l->get(a, &valid); + VariantTypeChanger<bool>::change(r_ret); *VariantGetInternalPtr<bool>::get_ptr(r_ret) = valid; } #ifdef PTRCALL_ENABLED @@ -1449,6 +1503,7 @@ public: bool valid; l->get(a, &valid); + VariantTypeChanger<bool>::change(r_ret); *VariantGetInternalPtr<bool>::get_ptr(r_ret) = valid; } #ifdef PTRCALL_ENABLED @@ -1480,7 +1535,7 @@ void register_op(Variant::Operator p_op, Variant::Type p_type_a, Variant::Type p #endif } -void register_variant_operators() { +void Variant::_register_variant_operators() { zeromem(operator_return_type_table, sizeof(operator_return_type_table)); zeromem(operator_evaluator_table, sizeof(operator_evaluator_table)); zeromem(validated_operator_evaluator_table, sizeof(validated_operator_evaluator_table)); @@ -1568,8 +1623,8 @@ void register_variant_operators() { register_op<OperatorEvaluatorMul<Transform, Transform, Transform>>(Variant::OP_MULTIPLY, Variant::TRANSFORM, Variant::TRANSFORM); register_op<OperatorEvaluatorXForm<Vector3, Transform, Vector3>>(Variant::OP_MULTIPLY, Variant::TRANSFORM, Variant::VECTOR3); register_op<OperatorEvaluatorXFormInv<Vector3, Vector3, Transform>>(Variant::OP_MULTIPLY, Variant::VECTOR3, Variant::TRANSFORM); - register_op<OperatorEvaluatorXForm<AABB, Transform, AABB>>(Variant::OP_MULTIPLY, Variant::TRANSFORM, Variant::AABB); - register_op<OperatorEvaluatorXFormInv<AABB, AABB, Transform>>(Variant::OP_MULTIPLY, Variant::AABB, Variant::TRANSFORM); + register_op<OperatorEvaluatorXForm<::AABB, Transform, ::AABB>>(Variant::OP_MULTIPLY, Variant::TRANSFORM, Variant::AABB); + register_op<OperatorEvaluatorXFormInv<::AABB, ::AABB, Transform>>(Variant::OP_MULTIPLY, Variant::AABB, Variant::TRANSFORM); register_op<OperatorEvaluatorXForm<Vector<Vector3>, Transform, Vector<Vector3>>>(Variant::OP_MULTIPLY, Variant::TRANSFORM, Variant::PACKED_VECTOR3_ARRAY); register_op<OperatorEvaluatorXFormInv<Vector<Vector3>, Vector<Vector3>, Transform>>(Variant::OP_MULTIPLY, Variant::PACKED_VECTOR3_ARRAY, Variant::TRANSFORM); @@ -1645,7 +1700,7 @@ void register_variant_operators() { register_op<OperatorEvaluatorStringModT<Transform2D>>(Variant::OP_MODULE, Variant::STRING, Variant::TRANSFORM2D); register_op<OperatorEvaluatorStringModT<Plane>>(Variant::OP_MODULE, Variant::STRING, Variant::PLANE); register_op<OperatorEvaluatorStringModT<Quat>>(Variant::OP_MODULE, Variant::STRING, Variant::QUAT); - register_op<OperatorEvaluatorStringModT<AABB>>(Variant::OP_MODULE, Variant::STRING, Variant::AABB); + register_op<OperatorEvaluatorStringModT<::AABB>>(Variant::OP_MODULE, Variant::STRING, Variant::AABB); register_op<OperatorEvaluatorStringModT<Basis>>(Variant::OP_MODULE, Variant::STRING, Variant::BASIS); register_op<OperatorEvaluatorStringModT<Transform>>(Variant::OP_MODULE, Variant::STRING, Variant::TRANSFORM); @@ -1713,7 +1768,7 @@ void register_variant_operators() { register_op<OperatorEvaluatorEqual<Transform2D, Transform2D>>(Variant::OP_EQUAL, Variant::TRANSFORM2D, Variant::TRANSFORM2D); register_op<OperatorEvaluatorEqual<Plane, Plane>>(Variant::OP_EQUAL, Variant::PLANE, Variant::PLANE); register_op<OperatorEvaluatorEqual<Quat, Quat>>(Variant::OP_EQUAL, Variant::QUAT, Variant::QUAT); - register_op<OperatorEvaluatorEqual<AABB, AABB>>(Variant::OP_EQUAL, Variant::AABB, Variant::AABB); + register_op<OperatorEvaluatorEqual<::AABB, ::AABB>>(Variant::OP_EQUAL, Variant::AABB, Variant::AABB); register_op<OperatorEvaluatorEqual<Basis, Basis>>(Variant::OP_EQUAL, Variant::BASIS, Variant::BASIS); register_op<OperatorEvaluatorEqual<Transform, Transform>>(Variant::OP_EQUAL, Variant::TRANSFORM, Variant::TRANSFORM); register_op<OperatorEvaluatorEqual<Color, Color>>(Variant::OP_EQUAL, Variant::COLOR, Variant::COLOR); @@ -1723,7 +1778,7 @@ void register_variant_operators() { register_op<OperatorEvaluatorEqual<StringName, StringName>>(Variant::OP_EQUAL, Variant::STRING_NAME, Variant::STRING_NAME); register_op<OperatorEvaluatorEqual<NodePath, NodePath>>(Variant::OP_EQUAL, Variant::NODE_PATH, Variant::NODE_PATH); - register_op<OperatorEvaluatorEqual<RID, RID>>(Variant::OP_EQUAL, Variant::_RID, Variant::_RID); + register_op<OperatorEvaluatorEqual<::RID, ::RID>>(Variant::OP_EQUAL, Variant::RID, Variant::RID); register_op<OperatorEvaluatorEqualObject>(Variant::OP_EQUAL, Variant::OBJECT, Variant::OBJECT); register_op<OperatorEvaluatorEqualObjectNil>(Variant::OP_EQUAL, Variant::OBJECT, Variant::NIL); @@ -1759,7 +1814,7 @@ void register_variant_operators() { register_op<OperatorEvaluatorNotEqual<Transform2D, Transform2D>>(Variant::OP_NOT_EQUAL, Variant::TRANSFORM2D, Variant::TRANSFORM2D); register_op<OperatorEvaluatorNotEqual<Plane, Plane>>(Variant::OP_NOT_EQUAL, Variant::PLANE, Variant::PLANE); register_op<OperatorEvaluatorNotEqual<Quat, Quat>>(Variant::OP_NOT_EQUAL, Variant::QUAT, Variant::QUAT); - register_op<OperatorEvaluatorNotEqual<AABB, AABB>>(Variant::OP_NOT_EQUAL, Variant::AABB, Variant::AABB); + register_op<OperatorEvaluatorNotEqual<::AABB, ::AABB>>(Variant::OP_NOT_EQUAL, Variant::AABB, Variant::AABB); register_op<OperatorEvaluatorNotEqual<Basis, Basis>>(Variant::OP_NOT_EQUAL, Variant::BASIS, Variant::BASIS); register_op<OperatorEvaluatorNotEqual<Transform, Transform>>(Variant::OP_NOT_EQUAL, Variant::TRANSFORM, Variant::TRANSFORM); register_op<OperatorEvaluatorNotEqual<Color, Color>>(Variant::OP_NOT_EQUAL, Variant::COLOR, Variant::COLOR); @@ -1769,7 +1824,7 @@ void register_variant_operators() { register_op<OperatorEvaluatorNotEqual<StringName, StringName>>(Variant::OP_NOT_EQUAL, Variant::STRING_NAME, Variant::STRING_NAME); register_op<OperatorEvaluatorNotEqual<NodePath, NodePath>>(Variant::OP_NOT_EQUAL, Variant::NODE_PATH, Variant::NODE_PATH); - register_op<OperatorEvaluatorNotEqual<RID, RID>>(Variant::OP_NOT_EQUAL, Variant::_RID, Variant::_RID); + register_op<OperatorEvaluatorNotEqual<::RID, ::RID>>(Variant::OP_NOT_EQUAL, Variant::RID, Variant::RID); register_op<OperatorEvaluatorNotEqualObject>(Variant::OP_NOT_EQUAL, Variant::OBJECT, Variant::OBJECT); register_op<OperatorEvaluatorNotEqualObjectNil>(Variant::OP_NOT_EQUAL, Variant::OBJECT, Variant::NIL); @@ -1799,7 +1854,7 @@ void register_variant_operators() { register_op<OperatorEvaluatorLess<Vector2i, Vector2i>>(Variant::OP_LESS, Variant::VECTOR2I, Variant::VECTOR2I); register_op<OperatorEvaluatorLess<Vector3, Vector3>>(Variant::OP_LESS, Variant::VECTOR3, Variant::VECTOR3); register_op<OperatorEvaluatorLess<Vector3i, Vector3i>>(Variant::OP_LESS, Variant::VECTOR3I, Variant::VECTOR3I); - register_op<OperatorEvaluatorLess<RID, RID>>(Variant::OP_LESS, Variant::_RID, Variant::_RID); + register_op<OperatorEvaluatorLess<::RID, ::RID>>(Variant::OP_LESS, Variant::RID, Variant::RID); register_op<OperatorEvaluatorLess<Array, Array>>(Variant::OP_LESS, Variant::ARRAY, Variant::ARRAY); register_op<OperatorEvaluatorLessEqual<int64_t, int64_t>>(Variant::OP_LESS_EQUAL, Variant::INT, Variant::INT); @@ -1811,7 +1866,7 @@ void register_variant_operators() { register_op<OperatorEvaluatorLessEqual<Vector2i, Vector2i>>(Variant::OP_LESS_EQUAL, Variant::VECTOR2I, Variant::VECTOR2I); register_op<OperatorEvaluatorLessEqual<Vector3, Vector3>>(Variant::OP_LESS_EQUAL, Variant::VECTOR3, Variant::VECTOR3); register_op<OperatorEvaluatorLessEqual<Vector3i, Vector3i>>(Variant::OP_LESS_EQUAL, Variant::VECTOR3I, Variant::VECTOR3I); - register_op<OperatorEvaluatorLessEqual<RID, RID>>(Variant::OP_LESS_EQUAL, Variant::_RID, Variant::_RID); + register_op<OperatorEvaluatorLessEqual<::RID, ::RID>>(Variant::OP_LESS_EQUAL, Variant::RID, Variant::RID); register_op<OperatorEvaluatorLessEqual<Array, Array>>(Variant::OP_LESS_EQUAL, Variant::ARRAY, Variant::ARRAY); register_op<OperatorEvaluatorGreater<bool, bool>>(Variant::OP_GREATER, Variant::BOOL, Variant::BOOL); @@ -1824,7 +1879,7 @@ void register_variant_operators() { register_op<OperatorEvaluatorGreater<Vector2i, Vector2i>>(Variant::OP_GREATER, Variant::VECTOR2I, Variant::VECTOR2I); register_op<OperatorEvaluatorGreater<Vector3, Vector3>>(Variant::OP_GREATER, Variant::VECTOR3, Variant::VECTOR3); register_op<OperatorEvaluatorGreater<Vector3i, Vector3i>>(Variant::OP_GREATER, Variant::VECTOR3I, Variant::VECTOR3I); - register_op<OperatorEvaluatorGreater<RID, RID>>(Variant::OP_GREATER, Variant::_RID, Variant::_RID); + register_op<OperatorEvaluatorGreater<::RID, ::RID>>(Variant::OP_GREATER, Variant::RID, Variant::RID); register_op<OperatorEvaluatorGreater<Array, Array>>(Variant::OP_GREATER, Variant::ARRAY, Variant::ARRAY); register_op<OperatorEvaluatorGreaterEqual<int64_t, int64_t>>(Variant::OP_GREATER_EQUAL, Variant::INT, Variant::INT); @@ -1836,7 +1891,7 @@ void register_variant_operators() { register_op<OperatorEvaluatorGreaterEqual<Vector2i, Vector2i>>(Variant::OP_GREATER_EQUAL, Variant::VECTOR2I, Variant::VECTOR2I); register_op<OperatorEvaluatorGreaterEqual<Vector3, Vector3>>(Variant::OP_GREATER_EQUAL, Variant::VECTOR3, Variant::VECTOR3); register_op<OperatorEvaluatorGreaterEqual<Vector3i, Vector3i>>(Variant::OP_GREATER_EQUAL, Variant::VECTOR3I, Variant::VECTOR3I); - register_op<OperatorEvaluatorGreaterEqual<RID, RID>>(Variant::OP_GREATER_EQUAL, Variant::_RID, Variant::_RID); + register_op<OperatorEvaluatorGreaterEqual<::RID, ::RID>>(Variant::OP_GREATER_EQUAL, Variant::RID, Variant::RID); register_op<OperatorEvaluatorGreaterEqual<Array, Array>>(Variant::OP_GREATER_EQUAL, Variant::ARRAY, Variant::ARRAY); register_op<OperatorEvaluatorAlwaysFalse<Variant::OP_OR, Variant::NIL, Variant::NIL>>(Variant::OP_OR, Variant::NIL, Variant::NIL); @@ -1948,7 +2003,7 @@ void register_variant_operators() { register_op<OperatorEvaluatorInDictionaryHas<Transform2D>>(Variant::OP_IN, Variant::TRANSFORM2D, Variant::DICTIONARY); register_op<OperatorEvaluatorInDictionaryHas<Plane>>(Variant::OP_IN, Variant::PLANE, Variant::DICTIONARY); register_op<OperatorEvaluatorInDictionaryHas<Quat>>(Variant::OP_IN, Variant::QUAT, Variant::DICTIONARY); - register_op<OperatorEvaluatorInDictionaryHas<AABB>>(Variant::OP_IN, Variant::AABB, Variant::DICTIONARY); + register_op<OperatorEvaluatorInDictionaryHas<::AABB>>(Variant::OP_IN, Variant::AABB, Variant::DICTIONARY); register_op<OperatorEvaluatorInDictionaryHas<Basis>>(Variant::OP_IN, Variant::BASIS, Variant::DICTIONARY); register_op<OperatorEvaluatorInDictionaryHas<Transform>>(Variant::OP_IN, Variant::TRANSFORM, Variant::DICTIONARY); @@ -1985,7 +2040,7 @@ void register_variant_operators() { register_op<OperatorEvaluatorInArrayFind<Transform2D, Array>>(Variant::OP_IN, Variant::TRANSFORM2D, Variant::ARRAY); register_op<OperatorEvaluatorInArrayFind<Plane, Array>>(Variant::OP_IN, Variant::PLANE, Variant::ARRAY); register_op<OperatorEvaluatorInArrayFind<Quat, Array>>(Variant::OP_IN, Variant::QUAT, Variant::ARRAY); - register_op<OperatorEvaluatorInArrayFind<AABB, Array>>(Variant::OP_IN, Variant::AABB, Variant::ARRAY); + register_op<OperatorEvaluatorInArrayFind<::AABB, Array>>(Variant::OP_IN, Variant::AABB, Variant::ARRAY); register_op<OperatorEvaluatorInArrayFind<Basis, Array>>(Variant::OP_IN, Variant::BASIS, Variant::ARRAY); register_op<OperatorEvaluatorInArrayFind<Transform, Array>>(Variant::OP_IN, Variant::TRANSFORM, Variant::ARRAY); @@ -2034,7 +2089,7 @@ void register_variant_operators() { register_op<OperatorEvaluatorObjectHasPropertyStringName>(Variant::OP_IN, Variant::STRING_NAME, Variant::OBJECT); } -void unregister_variant_operators() { +void Variant::_unregister_variant_operators() { } void Variant::evaluate(const Operator &p_op, const Variant &p_a, @@ -2090,8 +2145,8 @@ static const char *_op_names[Variant::OP_MAX] = { "-", "*", "/", - "- (negation)", - "+ (positive)", + "-", + "+", "%", "<<", ">>", diff --git a/core/variant/variant_setget.cpp b/core/variant/variant_setget.cpp index 6d9599a27e..05fe2b80d9 100644 --- a/core/variant/variant_setget.cpp +++ b/core/variant/variant_setget.cpp @@ -2385,7 +2385,7 @@ void Variant::interpolate(const Variant &a, const Variant &b, float c, Variant & r_dst = a; } return; - case _RID: { + case RID: { r_dst = a; } return; @@ -2576,12 +2576,12 @@ void Variant::interpolate(const Variant &a, const Variant &b, float c, Variant & } } -void register_variant_setters_getters() { +void Variant::_register_variant_setters_getters() { register_named_setters_getters(); register_indexed_setters_getters(); register_keyed_setters_getters(); } -void unregister_variant_setters_getters() { +void Variant::_unregister_variant_setters_getters() { unregister_named_setters_getters(); unregister_indexed_setters_getters(); } diff --git a/core/variant/variant_utility.cpp b/core/variant/variant_utility.cpp new file mode 100644 index 0000000000..91a1b0262c --- /dev/null +++ b/core/variant/variant_utility.cpp @@ -0,0 +1,1392 @@ +/*************************************************************************/ +/* variant_utility.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 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. */ +/*************************************************************************/ + +#include "variant.h" + +#include "core/core_string_names.h" +#include "core/io/marshalls.h" +#include "core/object/reference.h" +#include "core/os/os.h" +#include "core/templates/oa_hash_map.h" +#include "core/variant/binder_common.h" +#include "core/variant/variant_parser.h" + +struct VariantUtilityFunctions { + // Math + static inline double sin(double arg) { + return Math::sin(arg); + } + static inline double cos(double arg) { + return Math::cos(arg); + } + static inline double tan(double arg) { + return Math::tan(arg); + } + + static inline double sinh(double arg) { + return Math::sinh(arg); + } + static inline double cosh(double arg) { + return Math::cosh(arg); + } + static inline double tanh(double arg) { + return Math::tanh(arg); + } + + static inline double asin(double arg) { + return Math::asin(arg); + } + static inline double acos(double arg) { + return Math::acos(arg); + } + static inline double atan(double arg) { + return Math::atan(arg); + } + + static inline double atan2(double y, double x) { + return Math::atan2(y, x); + } + + static inline double sqrt(double x) { + return Math::sqrt(x); + } + + static inline double fmod(double b, double r) { + return Math::fmod(b, r); + } + + static inline double fposmod(double b, double r) { + return Math::fposmod(b, r); + } + + static inline double floor(double x) { + return Math::floor(x); + } + + static inline double ceil(double x) { + return Math::ceil(x); + } + + static inline double round(double x) { + return Math::round(x); + } + + static inline Variant abs(const Variant &x, Callable::CallError &r_error) { + r_error.error = Callable::CallError::CALL_OK; + switch (x.get_type()) { + case Variant::INT: { + return ABS(VariantInternalAccessor<int64_t>::get(&x)); + } break; + case Variant::FLOAT: { + return Math::absd(VariantInternalAccessor<double>::get(&x)); + } break; + case Variant::VECTOR2: { + return VariantInternalAccessor<Vector2>::get(&x).abs(); + } break; + case Variant::VECTOR2I: { + return VariantInternalAccessor<Vector2i>::get(&x).abs(); + } break; + case Variant::VECTOR3: { + return VariantInternalAccessor<Vector3>::get(&x).abs(); + } break; + case Variant::VECTOR3I: { + return VariantInternalAccessor<Vector3i>::get(&x).abs(); + } break; + default: { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; + return Variant(); + } + } + } + + static inline double absf(double x) { + return Math::absd(x); + } + + static inline int64_t absi(int64_t x) { + return ABS(x); + } + + static inline Variant sign(const Variant &x, Callable::CallError &r_error) { + r_error.error = Callable::CallError::CALL_OK; + switch (x.get_type()) { + case Variant::INT: { + return SGN(VariantInternalAccessor<int64_t>::get(&x)); + } break; + case Variant::FLOAT: { + return SGN(VariantInternalAccessor<double>::get(&x)); + } break; + case Variant::VECTOR2: { + return VariantInternalAccessor<Vector2>::get(&x).sign(); + } break; + case Variant::VECTOR2I: { + return VariantInternalAccessor<Vector2i>::get(&x).sign(); + } break; + case Variant::VECTOR3: { + return VariantInternalAccessor<Vector3>::get(&x).sign(); + } break; + case Variant::VECTOR3I: { + return VariantInternalAccessor<Vector3i>::get(&x).sign(); + } break; + default: { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; + return Variant(); + } + } + } + + static inline double signf(double x) { + return SGN(x); + } + + static inline int64_t signi(int64_t x) { + return SGN(x); + } + + static inline double pow(double x, double y) { + return Math::pow(x, y); + } + static inline double log(double x) { + return Math::log(x); + } + + static inline double exp(double x) { + return Math::exp(x); + } + + static inline double is_nan(double x) { + return Math::is_nan(x); + } + + static inline double is_inf(double x) { + return Math::is_inf(x); + } + + static inline double is_equal_approx(double x, double y) { + return Math::is_equal_approx(x, y); + } + + static inline double is_zero_approx(double x) { + return Math::is_zero_approx(x); + } + + static inline double ease(float x, float c) { + return Math::ease(x, c); + } + + static inline int step_decimals(float step) { + return Math::step_decimals(step); + } + + static inline int range_step_decimals(float step) { + return Math::range_step_decimals(step); + } + + static inline double stepify(double value, double step) { + return Math::stepify(value, step); + } + + static inline double lerp(double from, double to, double weight) { + return Math::lerp(from, to, weight); + } + + static inline double lerp_angle(double from, double to, double weight) { + return Math::lerp_angle(from, to, weight); + } + + static inline double inverse_lerp(double from, double to, double weight) { + return Math::inverse_lerp(from, to, weight); + } + + static inline double range_lerp(double value, double istart, double istop, double ostart, double ostop) { + return Math::range_lerp(value, istart, istop, ostart, ostop); + } + + static inline double smoothstep(double from, double to, double val) { + return Math::smoothstep(from, to, val); + } + + static inline double move_toward(double from, double to, double delta) { + return Math::move_toward(from, to, delta); + } + + static inline double dectime(double value, double amount, double step) { + return Math::dectime(value, amount, step); + } + + static inline double deg2rad(double angle_deg) { + return Math::deg2rad(angle_deg); + } + + static inline double rad2deg(double angle_rad) { + return Math::rad2deg(angle_rad); + } + + static inline double linear2db(double linear) { + return Math::linear2db(linear); + } + + static inline double db2linear(double db) { + return Math::db2linear(db); + } + + static inline Vector2 polar2cartesian(double r, double th) { + return Vector2(r * Math::cos(th), r * Math::sin(th)); + } + + static inline Vector2 cartesian2polar(double x, double y) { + return Vector2(Math::sqrt(x * x + y * y), Math::atan2(y, x)); + } + + static inline int64_t wrapi(int64_t value, int64_t min, int64_t max) { + return Math::wrapi(value, min, max); + } + static inline double wrapf(double value, double min, double max) { + return Math::wrapf(value, min, max); + } + + static inline Variant max(const Variant **p_args, int p_argcount, Callable::CallError &r_error) { + if (p_argcount < 2) { + r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; + r_error.expected = 2; + return Variant(); + } + Variant base = *p_args[0]; + Variant ret; + for (int i = 1; i < p_argcount; i++) { + bool valid; + Variant::evaluate(Variant::OP_GREATER, base, *p_args[i], ret, valid); + if (!valid) { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.expected = base.get_type(); + r_error.argument = i; + return Variant(); + } + if (ret.booleanize()) { + base = *p_args[i]; + } + } + r_error.error = Callable::CallError::CALL_OK; + return base; + } + + static inline double maxf(double x, double y) { + return MAX(x, y); + } + + static inline int64_t maxi(int64_t x, int64_t y) { + return MAX(x, y); + } + + static inline Variant min(const Variant **p_args, int p_argcount, Callable::CallError &r_error) { + if (p_argcount < 2) { + r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; + r_error.expected = 2; + return Variant(); + } + Variant base = *p_args[0]; + Variant ret; + for (int i = 1; i < p_argcount; i++) { + bool valid; + Variant::evaluate(Variant::OP_LESS, base, *p_args[i], ret, valid); + if (!valid) { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.expected = base.get_type(); + r_error.argument = i; + return Variant(); + } + if (ret.booleanize()) { + base = *p_args[i]; + } + } + r_error.error = Callable::CallError::CALL_OK; + return base; + } + + static inline double minf(double x, double y) { + return MIN(x, y); + } + + static inline int64_t mini(int64_t x, int64_t y) { + return MIN(x, y); + } + + static inline Variant clamp(const Variant &x, const Variant &min, const Variant &max, Callable::CallError &r_error) { + Variant value = x; + + Variant ret; + + bool valid; + Variant::evaluate(Variant::OP_LESS, value, min, ret, valid); + if (!valid) { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.expected = value.get_type(); + r_error.argument = 1; + return Variant(); + } + if (ret.booleanize()) { + value = min; + } + Variant::evaluate(Variant::OP_GREATER, value, max, ret, valid); + if (!valid) { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.expected = value.get_type(); + r_error.argument = 2; + return Variant(); + } + if (ret.booleanize()) { + value = max; + } + + r_error.error = Callable::CallError::CALL_OK; + + return value; + } + + static inline double clampf(double x, double min, double max) { + return CLAMP(x, min, max); + } + + static inline int64_t clampi(int64_t x, int64_t min, int64_t max) { + return CLAMP(x, min, max); + } + + static inline int64_t nearest_po2(int64_t x) { + return nearest_power_of_2_templated(uint64_t(x)); + } + + // Random + + static inline void randomize() { + Math::randomize(); + } + + static inline int64_t randi() { + return Math::rand(); + } + + static inline double randf() { + return Math::randf(); + } + + static inline int64_t randi_range(int64_t from, int64_t to) { + return Math::random((int32_t)from, (int32_t)to); + } + + static inline double randf_range(double from, double to) { + return Math::random(from, to); + } + + static inline void seed(int64_t s) { + return Math::seed(s); + } + + static inline PackedInt64Array rand_from_seed(int64_t seed) { + uint64_t s = seed; + PackedInt64Array arr; + arr.resize(2); + arr.write[0] = Math::rand_from_seed(&s); + arr.write[1] = s; + return arr; + } + + // Utility + + static inline Variant weakref(const Variant &obj, Callable::CallError &r_error) { + if (obj.get_type() == Variant::OBJECT) { + r_error.error = Callable::CallError::CALL_OK; + if (obj.is_ref()) { + Ref<WeakRef> wref = memnew(WeakRef); + REF r = obj; + if (r.is_valid()) { + wref->set_ref(r); + } + return wref; + } else { + Ref<WeakRef> wref = memnew(WeakRef); + Object *o = obj.get_validated_object(); + if (o) { + wref->set_obj(o); + } + return wref; + } + } else if (obj.get_type() == Variant::NIL) { + r_error.error = Callable::CallError::CALL_OK; + Ref<WeakRef> wref = memnew(WeakRef); + return wref; + } else { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.argument = 0; + r_error.expected = Variant::OBJECT; + return Variant(); + } + } + + static inline int64_t _typeof(const Variant &obj) { + return obj.get_type(); + } + + static inline String str(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { + if (p_arg_count < 1) { + r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; + r_error.argument = 1; + return String(); + } + String str; + for (int i = 0; i < p_arg_count; i++) { + String os = p_args[i]->operator String(); + + if (i == 0) { + str = os; + } else { + str += os; + } + } + + r_error.error = Callable::CallError::CALL_OK; + + return str; + } + + static inline void print(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { + if (p_arg_count < 1) { + r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; + r_error.argument = 1; + } + String str; + for (int i = 0; i < p_arg_count; i++) { + String os = p_args[i]->operator String(); + + if (i == 0) { + str = os; + } else { + str += os; + } + } + + print_line(str); + r_error.error = Callable::CallError::CALL_OK; + } + + static inline void printerr(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { + if (p_arg_count < 1) { + r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; + r_error.argument = 1; + } + String str; + for (int i = 0; i < p_arg_count; i++) { + String os = p_args[i]->operator String(); + + if (i == 0) { + str = os; + } else { + str += os; + } + } + + print_error(str); + r_error.error = Callable::CallError::CALL_OK; + } + + static inline void printt(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { + if (p_arg_count < 1) { + r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; + r_error.argument = 1; + } + String str; + for (int i = 0; i < p_arg_count; i++) { + if (i) { + str += "\t"; + } + str += p_args[i]->operator String(); + } + + print_error(str); + r_error.error = Callable::CallError::CALL_OK; + } + + static inline void prints(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { + if (p_arg_count < 1) { + r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; + r_error.argument = 1; + } + String str; + for (int i = 0; i < p_arg_count; i++) { + if (i) { + str += " "; + } + str += p_args[i]->operator String(); + } + + print_error(str); + r_error.error = Callable::CallError::CALL_OK; + } + + static inline void printraw(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { + if (p_arg_count < 1) { + r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; + r_error.argument = 1; + } + String str; + for (int i = 0; i < p_arg_count; i++) { + String os = p_args[i]->operator String(); + + if (i == 0) { + str = os; + } else { + str += os; + } + } + + OS::get_singleton()->print("%s", str.utf8().get_data()); + r_error.error = Callable::CallError::CALL_OK; + } + + static inline void push_error(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { + if (p_arg_count < 1) { + r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; + r_error.argument = 1; + } + String str; + for (int i = 0; i < p_arg_count; i++) { + String os = p_args[i]->operator String(); + + if (i == 0) { + str = os; + } else { + str += os; + } + } + + ERR_PRINT(str); + r_error.error = Callable::CallError::CALL_OK; + } + + static inline void push_warning(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { + if (p_arg_count < 1) { + r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; + r_error.argument = 1; + } + String str; + for (int i = 0; i < p_arg_count; i++) { + String os = p_args[i]->operator String(); + + if (i == 0) { + str = os; + } else { + str += os; + } + } + + WARN_PRINT(str); + r_error.error = Callable::CallError::CALL_OK; + } + + static inline String var2str(const Variant &p_var) { + String vars; + VariantWriter::write_to_string(p_var, vars); + return vars; + } + + static inline Variant str2var(const String &p_var) { + VariantParser::StreamString ss; + ss.s = p_var; + + String errs; + int line; + Variant ret; + (void)VariantParser::parse(&ss, ret, errs, line); + + return ret; + } + + static inline PackedByteArray var2bytes(const Variant &p_var) { + int len; + Error err = encode_variant(p_var, nullptr, len, false); + if (err != OK) { + return PackedByteArray(); + } + + PackedByteArray barr; + barr.resize(len); + { + uint8_t *w = barr.ptrw(); + err = encode_variant(p_var, w, len, false); + if (err != OK) { + return PackedByteArray(); + } + } + + return barr; + } + + static inline PackedByteArray var2bytes_with_objects(const Variant &p_var) { + int len; + Error err = encode_variant(p_var, nullptr, len, true); + if (err != OK) { + return PackedByteArray(); + } + + PackedByteArray barr; + barr.resize(len); + { + uint8_t *w = barr.ptrw(); + err = encode_variant(p_var, w, len, true); + if (err != OK) { + return PackedByteArray(); + } + } + + return barr; + } + + static inline Variant bytes2var(const PackedByteArray &p_arr) { + Variant ret; + { + const uint8_t *r = p_arr.ptr(); + Error err = decode_variant(ret, r, p_arr.size(), nullptr, false); + if (err != OK) { + return Variant(); + } + } + return ret; + } + + static inline Variant bytes2var_with_objects(const PackedByteArray &p_arr) { + Variant ret; + { + const uint8_t *r = p_arr.ptr(); + Error err = decode_variant(ret, r, p_arr.size(), nullptr, true); + if (err != OK) { + return Variant(); + } + } + return ret; + } + + static inline int64_t hash(const Variant &p_arr) { + return p_arr.hash(); + } + + static inline Variant instance_from_id(int64_t p_id) { + ObjectID id = ObjectID((uint64_t)p_id); + Variant ret = ObjectDB::get_instance(id); + return ret; + } + + static inline bool is_instance_id_valid(int64_t p_id) { + return ObjectDB::get_instance(ObjectID((uint64_t)p_id)) != nullptr; + } + + static inline bool is_instance_valid(const Variant &p_instance) { + if (p_instance.get_type() != Variant::OBJECT) { + return false; + } + return p_instance.get_validated_object() != nullptr; + } +}; + +#ifdef DEBUG_METHODS_ENABLED +#define VCALLR *ret = p_func(VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...) +#define VCALL p_func(VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...) +#else +#define VCALLR *ret = p_func(VariantCaster<P>::cast(*p_args[Is])...) +#define VCALL p_func(VariantCaster<P>::cast(*p_args[Is])...) +#endif + +template <class R, class... P, size_t... Is> +static _FORCE_INLINE_ void call_helperpr(R (*p_func)(P...), Variant *ret, const Variant **p_args, Callable::CallError &r_error, IndexSequence<Is...>) { + r_error.error = Callable::CallError::CALL_OK; + VCALLR; + (void)p_args; // avoid gcc warning + (void)r_error; +} + +template <class R, class... P, size_t... Is> +static _FORCE_INLINE_ void validated_call_helperpr(R (*p_func)(P...), Variant *ret, const Variant **p_args, IndexSequence<Is...>) { + *ret = p_func(VariantCaster<P>::cast(*p_args[Is])...); + (void)p_args; +} + +template <class R, class... P, size_t... Is> +static _FORCE_INLINE_ void ptr_call_helperpr(R (*p_func)(P...), void *ret, const void **p_args, IndexSequence<Is...>) { + PtrToArg<R>::encode(p_func(PtrToArg<P>::convert(p_args[Is])...), ret); + (void)p_args; +} + +template <class R, class... P> +static _FORCE_INLINE_ void call_helperr(R (*p_func)(P...), Variant *ret, const Variant **p_args, Callable::CallError &r_error) { + call_helperpr(p_func, ret, p_args, r_error, BuildIndexSequence<sizeof...(P)>{}); +} + +template <class R, class... P> +static _FORCE_INLINE_ void validated_call_helperr(R (*p_func)(P...), Variant *ret, const Variant **p_args) { + validated_call_helperpr(p_func, ret, p_args, BuildIndexSequence<sizeof...(P)>{}); +} + +template <class R, class... P> +static _FORCE_INLINE_ void ptr_call_helperr(R (*p_func)(P...), void *ret, const void **p_args) { + ptr_call_helperpr(p_func, ret, p_args, BuildIndexSequence<sizeof...(P)>{}); +} + +template <class R, class... P> +static _FORCE_INLINE_ int get_arg_count_helperr(R (*p_func)(P...)) { + return sizeof...(P); +} + +template <class R, class... P> +static _FORCE_INLINE_ Variant::Type get_arg_type_helperr(R (*p_func)(P...), int p_arg) { + return call_get_argument_type<P...>(p_arg); +} + +template <class R, class... P> +static _FORCE_INLINE_ Variant::Type get_ret_type_helperr(R (*p_func)(P...)) { + return GetTypeInfo<R>::VARIANT_TYPE; +} + +// WITHOUT RET + +template <class... P, size_t... Is> +static _FORCE_INLINE_ void call_helperp(void (*p_func)(P...), const Variant **p_args, Callable::CallError &r_error, IndexSequence<Is...>) { + r_error.error = Callable::CallError::CALL_OK; + VCALL; + (void)p_args; + (void)r_error; +} + +template <class... P, size_t... Is> +static _FORCE_INLINE_ void validated_call_helperp(void (*p_func)(P...), const Variant **p_args, IndexSequence<Is...>) { + p_func(VariantCaster<P>::cast(*p_args[Is])...); + (void)p_args; +} + +template <class... P, size_t... Is> +static _FORCE_INLINE_ void ptr_call_helperp(void (*p_func)(P...), const void **p_args, IndexSequence<Is...>) { + p_func(PtrToArg<P>::convert(p_args[Is])...); + (void)p_args; +} + +template <class... P> +static _FORCE_INLINE_ void call_helper(void (*p_func)(P...), const Variant **p_args, Callable::CallError &r_error) { + call_helperp(p_func, p_args, r_error, BuildIndexSequence<sizeof...(P)>{}); +} + +template <class... P> +static _FORCE_INLINE_ void validated_call_helper(void (*p_func)(P...), const Variant **p_args) { + validated_call_helperp(p_func, p_args, BuildIndexSequence<sizeof...(P)>{}); +} + +template <class... P> +static _FORCE_INLINE_ void ptr_call_helper(void (*p_func)(P...), const void **p_args) { + ptr_call_helperp(p_func, p_args, BuildIndexSequence<sizeof...(P)>{}); +} + +template <class... P> +static _FORCE_INLINE_ int get_arg_count_helper(void (*p_func)(P...)) { + return sizeof...(P); +} + +template <class... P> +static _FORCE_INLINE_ Variant::Type get_arg_type_helper(void (*p_func)(P...), int p_arg) { + return call_get_argument_type<P...>(p_arg); +} + +template <class... P> +static _FORCE_INLINE_ Variant::Type get_ret_type_helper(void (*p_func)(P...)) { + return Variant::NIL; +} + +#define FUNCBINDR(m_func, m_args, m_category) \ + class Func_##m_func { \ + public: \ + static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \ + call_helperr(VariantUtilityFunctions::m_func, r_ret, p_args, r_error); \ + } \ + \ + static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \ + validated_call_helperr(VariantUtilityFunctions::m_func, r_ret, p_args); \ + } \ + static void ptrcall(void *ret, const void **p_args, int p_argcount) { \ + ptr_call_helperr(VariantUtilityFunctions::m_func, ret, p_args); \ + } \ + \ + static int get_argument_count() { \ + return get_arg_count_helperr(VariantUtilityFunctions::m_func); \ + } \ + \ + static Variant::Type get_argument_type(int p_arg) { \ + return get_arg_type_helperr(VariantUtilityFunctions::m_func, p_arg); \ + } \ + \ + static Variant::Type get_return_type() { \ + return get_ret_type_helperr(VariantUtilityFunctions::m_func); \ + } \ + static bool has_return_type() { \ + return true; \ + } \ + static bool is_vararg() { return false; } \ + static Variant::UtilityFunctionType get_type() { return m_category; } \ + }; \ + register_utility_function<Func_##m_func>(#m_func, m_args) + +#define FUNCBINDVR(m_func, m_args, m_category) \ + class Func_##m_func { \ + public: \ + static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \ + r_error.error = Callable::CallError::CALL_OK; \ + *r_ret = VariantUtilityFunctions::m_func(*p_args[0], r_error); \ + } \ + \ + static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \ + Callable::CallError ce; \ + *r_ret = VariantUtilityFunctions::m_func(*p_args[0], ce); \ + } \ + static void ptrcall(void *ret, const void **p_args, int p_argcount) { \ + Callable::CallError ce; \ + PtrToArg<Variant>::encode(VariantUtilityFunctions::m_func(PtrToArg<Variant>::convert(p_args[0]), ce), ret); \ + } \ + \ + static int get_argument_count() { \ + return 1; \ + } \ + \ + static Variant::Type get_argument_type(int p_arg) { \ + return Variant::NIL; \ + } \ + \ + static Variant::Type get_return_type() { \ + return Variant::NIL; \ + } \ + static bool has_return_type() { \ + return true; \ + } \ + static bool is_vararg() { return false; } \ + static Variant::UtilityFunctionType get_type() { return m_category; } \ + }; \ + register_utility_function<Func_##m_func>(#m_func, m_args) + +#define FUNCBINDVR3(m_func, m_args, m_category) \ + class Func_##m_func { \ + public: \ + static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \ + r_error.error = Callable::CallError::CALL_OK; \ + *r_ret = VariantUtilityFunctions::m_func(*p_args[0], *p_args[1], *p_args[2], r_error); \ + } \ + \ + static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \ + Callable::CallError ce; \ + *r_ret = VariantUtilityFunctions::m_func(*p_args[0], *p_args[1], *p_args[2], ce); \ + } \ + static void ptrcall(void *ret, const void **p_args, int p_argcount) { \ + Callable::CallError ce; \ + Variant r; \ + r = VariantUtilityFunctions::m_func(PtrToArg<Variant>::convert(p_args[0]), PtrToArg<Variant>::convert(p_args[1]), PtrToArg<Variant>::convert(p_args[2]), ce); \ + PtrToArg<Variant>::encode(r, ret); \ + } \ + \ + static int get_argument_count() { \ + return 3; \ + } \ + \ + static Variant::Type get_argument_type(int p_arg) { \ + return Variant::NIL; \ + } \ + \ + static Variant::Type get_return_type() { \ + return Variant::NIL; \ + } \ + static bool has_return_type() { \ + return true; \ + } \ + static bool is_vararg() { return false; } \ + static Variant::UtilityFunctionType get_type() { return m_category; } \ + }; \ + register_utility_function<Func_##m_func>(#m_func, m_args) + +#define FUNCBINDVARARG(m_func, m_args, m_category) \ + class Func_##m_func { \ + public: \ + static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \ + r_error.error = Callable::CallError::CALL_OK; \ + *r_ret = VariantUtilityFunctions::m_func(p_args, p_argcount, r_error); \ + } \ + \ + static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \ + Callable::CallError c; \ + *r_ret = VariantUtilityFunctions::m_func(p_args, p_argcount, c); \ + } \ + static void ptrcall(void *ret, const void **p_args, int p_argcount) { \ + Vector<Variant> args; \ + for (int i = 0; i < p_argcount; i++) { \ + args.push_back(PtrToArg<Variant>::convert(p_args[i])); \ + } \ + Vector<const Variant *> argsp; \ + for (int i = 0; i < p_argcount; i++) { \ + argsp.push_back(&args[i]); \ + } \ + Variant r; \ + validated_call(&r, (const Variant **)argsp.ptr(), p_argcount); \ + PtrToArg<Variant>::encode(r, ret); \ + } \ + \ + static int get_argument_count() { \ + return 2; \ + } \ + \ + static Variant::Type get_argument_type(int p_arg) { \ + return Variant::NIL; \ + } \ + \ + static Variant::Type get_return_type() { \ + return Variant::NIL; \ + } \ + static bool has_return_type() { \ + return true; \ + } \ + static bool is_vararg() { \ + return true; \ + } \ + static Variant::UtilityFunctionType get_type() { \ + return m_category; \ + } \ + }; \ + register_utility_function<Func_##m_func>(#m_func, m_args) + +#define FUNCBINDVARARGS(m_func, m_args, m_category) \ + class Func_##m_func { \ + public: \ + static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \ + r_error.error = Callable::CallError::CALL_OK; \ + *r_ret = VariantUtilityFunctions::m_func(p_args, p_argcount, r_error); \ + } \ + \ + static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \ + Callable::CallError c; \ + *r_ret = VariantUtilityFunctions::m_func(p_args, p_argcount, c); \ + } \ + static void ptrcall(void *ret, const void **p_args, int p_argcount) { \ + Vector<Variant> args; \ + for (int i = 0; i < p_argcount; i++) { \ + args.push_back(PtrToArg<Variant>::convert(p_args[i])); \ + } \ + Vector<const Variant *> argsp; \ + for (int i = 0; i < p_argcount; i++) { \ + argsp.push_back(&args[i]); \ + } \ + Variant r; \ + validated_call(&r, (const Variant **)argsp.ptr(), p_argcount); \ + PtrToArg<String>::encode(r.operator String(), ret); \ + } \ + \ + static int get_argument_count() { \ + return 1; \ + } \ + \ + static Variant::Type get_argument_type(int p_arg) { \ + return Variant::NIL; \ + } \ + \ + static Variant::Type get_return_type() { \ + return Variant::STRING; \ + } \ + static bool has_return_type() { \ + return true; \ + } \ + static bool is_vararg() { \ + return true; \ + } \ + static Variant::UtilityFunctionType get_type() { \ + return m_category; \ + } \ + }; \ + register_utility_function<Func_##m_func>(#m_func, m_args) + +#define FUNCBINDVARARGV(m_func, m_args, m_category) \ + class Func_##m_func { \ + public: \ + static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \ + r_error.error = Callable::CallError::CALL_OK; \ + VariantUtilityFunctions::m_func(p_args, p_argcount, r_error); \ + } \ + \ + static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \ + Callable::CallError c; \ + VariantUtilityFunctions::m_func(p_args, p_argcount, c); \ + } \ + static void ptrcall(void *ret, const void **p_args, int p_argcount) { \ + Vector<Variant> args; \ + for (int i = 0; i < p_argcount; i++) { \ + args.push_back(PtrToArg<Variant>::convert(p_args[i])); \ + } \ + Vector<const Variant *> argsp; \ + for (int i = 0; i < p_argcount; i++) { \ + argsp.push_back(&args[i]); \ + } \ + Variant r; \ + validated_call(&r, (const Variant **)argsp.ptr(), p_argcount); \ + } \ + \ + static int get_argument_count() { \ + return 1; \ + } \ + \ + static Variant::Type get_argument_type(int p_arg) { \ + return Variant::NIL; \ + } \ + \ + static Variant::Type get_return_type() { \ + return Variant::NIL; \ + } \ + static bool has_return_type() { \ + return false; \ + } \ + static bool is_vararg() { \ + return true; \ + } \ + static Variant::UtilityFunctionType get_type() { \ + return m_category; \ + } \ + }; \ + register_utility_function<Func_##m_func>(#m_func, m_args) + +#define FUNCBIND(m_func, m_args, m_category) \ + class Func_##m_func { \ + public: \ + static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \ + call_helper(VariantUtilityFunctions::m_func, p_args, r_error); \ + } \ + \ + static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \ + validated_call_helper(VariantUtilityFunctions::m_func, p_args); \ + } \ + static void ptrcall(void *ret, const void **p_args, int p_argcount) { \ + ptr_call_helper(VariantUtilityFunctions::m_func, p_args); \ + } \ + \ + static int get_argument_count() { \ + return get_arg_count_helper(VariantUtilityFunctions::m_func); \ + } \ + \ + static Variant::Type get_argument_type(int p_arg) { \ + return get_arg_type_helper(VariantUtilityFunctions::m_func, p_arg); \ + } \ + \ + static Variant::Type get_return_type() { \ + return get_ret_type_helper(VariantUtilityFunctions::m_func); \ + } \ + static bool has_return_type() { \ + return false; \ + } \ + static bool is_vararg() { return false; } \ + static Variant::UtilityFunctionType get_type() { return m_category; } \ + }; \ + register_utility_function<Func_##m_func>(#m_func, m_args) + +struct VariantUtilityFunctionInfo { + void (*call_utility)(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error); + Variant::ValidatedUtilityFunction validated_call_utility; + Variant::PTRUtilityFunction ptr_call_utility; + Vector<String> argnames; + bool is_vararg; + bool returns_value; + int argcount; + Variant::Type (*get_arg_type)(int); + Variant::Type return_type; + Variant::UtilityFunctionType type; +}; + +static OAHashMap<StringName, VariantUtilityFunctionInfo> utility_function_table; +static List<StringName> utility_function_name_table; + +template <class T> +static void register_utility_function(const String &p_name, const Vector<String> &argnames) { + String name = p_name; + if (name.begins_with("_")) { + name = name.substr(1, name.length() - 1); + } + StringName sname = name; + ERR_FAIL_COND(utility_function_table.has(sname)); + + VariantUtilityFunctionInfo bfi; + bfi.call_utility = T::call; + bfi.validated_call_utility = T::validated_call; + bfi.ptr_call_utility = T::ptrcall; + bfi.is_vararg = T::is_vararg(); + bfi.argnames = argnames; + bfi.argcount = T::get_argument_count(); + if (!bfi.is_vararg) { + ERR_FAIL_COND_MSG(argnames.size() != bfi.argcount, "wrong number of arguments binding utility function: " + name); + } + bfi.get_arg_type = T::get_argument_type; + bfi.return_type = T::get_return_type(); + bfi.type = T::get_type(); + bfi.returns_value = T::has_return_type(); + + utility_function_table.insert(sname, bfi); + utility_function_name_table.push_back(sname); +} + +void Variant::_register_variant_utility_functions() { + // Math + + FUNCBINDR(sin, sarray("angle_rad"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(cos, sarray("angle_rad"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(tan, sarray("angle_rad"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDR(sinh, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(cosh, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(tanh, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDR(asin, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(acos, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(atan, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDR(atan2, sarray("y", "x"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDR(sqrt, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(fmod, sarray("x", "y"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(fposmod, sarray("x", "y"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(floor, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(ceil, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(round, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDVR(abs, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDR(absf, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(absi, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDVR(sign, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDR(signf, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(signi, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDR(pow, sarray("x", "y"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(log, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(exp, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDR(is_nan, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(is_inf, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDR(is_equal_approx, sarray("a", "b"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(is_zero_approx, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDR(ease, sarray("x", "c"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(step_decimals, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(range_step_decimals, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(stepify, sarray("x", "y"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDR(lerp, sarray("from", "to", "c"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(lerp_angle, sarray("from", "to", "c"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(inverse_lerp, sarray("from", "to", "c"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(range_lerp, sarray("value", "istart", "istop", "ostart", "ostop"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDR(smoothstep, sarray("from", "to", "c"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(move_toward, sarray("from", "to", "delta"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(dectime, sarray("value", "amount", "step"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDR(deg2rad, sarray("deg"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(rad2deg, sarray("rad"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(linear2db, sarray("lin"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(db2linear, sarray("db"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDR(polar2cartesian, sarray("r", "th"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(cartesian2polar, sarray("x", "y"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDR(wrapi, sarray("value", "min", "max"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(wrapf, sarray("value", "min", "max"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDVARARG(max, sarray(), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDR(maxi, sarray("a", "b"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(maxf, sarray("a", "b"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDVARARG(min, sarray(), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDR(mini, sarray("a", "b"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(minf, sarray("a", "b"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDVR3(clamp, sarray("value", "min", "max"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(clampi, sarray("value", "min", "max"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(clampf, sarray("value", "min", "max"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDR(nearest_po2, sarray("value"), Variant::UTILITY_FUNC_TYPE_MATH); + + //Random + + FUNCBIND(randomize, sarray(), Variant::UTILITY_FUNC_TYPE_RANDOM); + FUNCBINDR(randi, sarray(), Variant::UTILITY_FUNC_TYPE_RANDOM); + FUNCBINDR(randf, sarray(), Variant::UTILITY_FUNC_TYPE_RANDOM); + FUNCBINDR(randi_range, sarray("from", "to"), Variant::UTILITY_FUNC_TYPE_RANDOM); + FUNCBINDR(randf_range, sarray("from", "to"), Variant::UTILITY_FUNC_TYPE_RANDOM); + FUNCBIND(seed, sarray("base"), Variant::UTILITY_FUNC_TYPE_RANDOM); + FUNCBINDR(rand_from_seed, sarray("seed"), Variant::UTILITY_FUNC_TYPE_RANDOM); + + // Utility + FUNCBINDVR(weakref, sarray("from"), Variant::UTILITY_FUNC_TYPE_GENERAL); + FUNCBINDR(_typeof, sarray("variable"), Variant::UTILITY_FUNC_TYPE_GENERAL); + FUNCBINDVARARGS(str, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL); + FUNCBINDVARARGV(print, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL); + FUNCBINDVARARGV(printerr, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL); + FUNCBINDVARARGV(printt, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL); + FUNCBINDVARARGV(prints, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL); + FUNCBINDVARARGV(printraw, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL); + FUNCBINDVARARGV(push_error, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL); + FUNCBINDVARARGV(push_warning, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL); + + FUNCBINDR(var2str, sarray("variable"), Variant::UTILITY_FUNC_TYPE_GENERAL); + FUNCBINDR(str2var, sarray("string"), Variant::UTILITY_FUNC_TYPE_GENERAL); + + FUNCBINDR(var2bytes, sarray("variable"), Variant::UTILITY_FUNC_TYPE_GENERAL); + FUNCBINDR(bytes2var, sarray("bytes"), Variant::UTILITY_FUNC_TYPE_GENERAL); + + FUNCBINDR(var2bytes_with_objects, sarray("variable"), Variant::UTILITY_FUNC_TYPE_GENERAL); + FUNCBINDR(bytes2var_with_objects, sarray("bytes"), Variant::UTILITY_FUNC_TYPE_GENERAL); + + FUNCBINDR(hash, sarray("variable"), Variant::UTILITY_FUNC_TYPE_GENERAL); + + FUNCBINDR(instance_from_id, sarray("id"), Variant::UTILITY_FUNC_TYPE_GENERAL); + FUNCBINDR(is_instance_id_valid, sarray("id"), Variant::UTILITY_FUNC_TYPE_GENERAL); + FUNCBINDR(is_instance_valid, sarray("instance"), Variant::UTILITY_FUNC_TYPE_GENERAL); +} +void Variant::_unregister_variant_utility_functions() { + utility_function_table.clear(); + utility_function_name_table.clear(); +} + +void Variant::call_utility_function(const StringName &p_name, Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { + const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name); + if (!bfi) { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; + r_error.argument = 0; + r_error.expected = 0; + return; + } + + if (unlikely(!bfi->is_vararg && p_argcount < bfi->argcount)) { + r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; + r_error.argument = 0; + r_error.expected = bfi->argcount; + return; + } + + if (unlikely(!bfi->is_vararg && p_argcount > bfi->argcount)) { + r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS; + r_error.argument = 0; + r_error.expected = bfi->argcount; + return; + } + + bfi->call_utility(r_ret, p_args, p_argcount, r_error); +} + +bool Variant::has_utility_function(const StringName &p_name) { + return utility_function_table.has(p_name); +} + +Variant::ValidatedUtilityFunction Variant::get_validated_utility_function(const StringName &p_name) { + const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name); + if (!bfi) { + return nullptr; + } + + return bfi->validated_call_utility; +} +Variant::PTRUtilityFunction Variant::get_ptr_utility_function(const StringName &p_name) { + const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name); + if (!bfi) { + return nullptr; + } + + return bfi->ptr_call_utility; +} + +Variant::UtilityFunctionType Variant::get_utility_function_type(const StringName &p_name) { + const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name); + if (!bfi) { + return Variant::UTILITY_FUNC_TYPE_MATH; + } + + return bfi->type; +} + +int Variant::get_utility_function_argument_count(const StringName &p_name) { + const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name); + if (!bfi) { + return 0; + } + + return bfi->argcount; +} +Variant::Type Variant::get_utility_function_argument_type(const StringName &p_name, int p_arg) { + const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name); + if (!bfi) { + return Variant::NIL; + } + + return bfi->get_arg_type(p_arg); +} +String Variant::get_utility_function_argument_name(const StringName &p_name, int p_arg) { + const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name); + if (!bfi) { + return String(); + } + ERR_FAIL_COND_V(bfi->is_vararg, String()); + ERR_FAIL_INDEX_V(p_arg, bfi->argnames.size(), String()); + return bfi->argnames[p_arg]; +} +bool Variant::has_utility_function_return_value(const StringName &p_name) { + const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name); + if (!bfi) { + return false; + } + return bfi->returns_value; +} +Variant::Type Variant::get_utility_function_return_type(const StringName &p_name) { + const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name); + if (!bfi) { + return Variant::NIL; + } + + return bfi->return_type; +} +bool Variant::is_utility_function_vararg(const StringName &p_name) { + const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name); + if (!bfi) { + return false; + } + + return bfi->is_vararg; +} + +void Variant::get_utility_function_list(List<StringName> *r_functions) { + for (List<StringName>::Element *E = utility_function_name_table.front(); E; E = E->next()) { + r_functions->push_back(E->get()); + } +} diff --git a/doc/classes/@GlobalScope.xml b/doc/classes/@GlobalScope.xml index 2b1770f12b..ee65bbc07e 100644 --- a/doc/classes/@GlobalScope.xml +++ b/doc/classes/@GlobalScope.xml @@ -1594,43 +1594,40 @@ <constant name="OP_MODULE" value="12" enum="Variant.Operator"> Remainder/modulo operator ([code]%[/code]). </constant> - <constant name="OP_STRING_CONCAT" value="13" enum="Variant.Operator"> - String concatenation operator ([code]+[/code]). - </constant> - <constant name="OP_SHIFT_LEFT" value="14" enum="Variant.Operator"> + <constant name="OP_SHIFT_LEFT" value="13" enum="Variant.Operator"> Left shift operator ([code]<<[/code]). </constant> - <constant name="OP_SHIFT_RIGHT" value="15" enum="Variant.Operator"> + <constant name="OP_SHIFT_RIGHT" value="14" enum="Variant.Operator"> Right shift operator ([code]>>[/code]). </constant> - <constant name="OP_BIT_AND" value="16" enum="Variant.Operator"> + <constant name="OP_BIT_AND" value="15" enum="Variant.Operator"> Bitwise AND operator ([code]&[/code]). </constant> - <constant name="OP_BIT_OR" value="17" enum="Variant.Operator"> + <constant name="OP_BIT_OR" value="16" enum="Variant.Operator"> Bitwise OR operator ([code]|[/code]). </constant> - <constant name="OP_BIT_XOR" value="18" enum="Variant.Operator"> + <constant name="OP_BIT_XOR" value="17" enum="Variant.Operator"> Bitwise XOR operator ([code]^[/code]). </constant> - <constant name="OP_BIT_NEGATE" value="19" enum="Variant.Operator"> + <constant name="OP_BIT_NEGATE" value="18" enum="Variant.Operator"> Bitwise NOT operator ([code]~[/code]). </constant> - <constant name="OP_AND" value="20" enum="Variant.Operator"> + <constant name="OP_AND" value="19" enum="Variant.Operator"> Logical AND operator ([code]and[/code] or [code]&&[/code]). </constant> - <constant name="OP_OR" value="21" enum="Variant.Operator"> + <constant name="OP_OR" value="20" enum="Variant.Operator"> Logical OR operator ([code]or[/code] or [code]||[/code]). </constant> - <constant name="OP_XOR" value="22" enum="Variant.Operator"> + <constant name="OP_XOR" value="21" enum="Variant.Operator"> Logical XOR operator (not implemented in GDScript). </constant> - <constant name="OP_NOT" value="23" enum="Variant.Operator"> + <constant name="OP_NOT" value="22" enum="Variant.Operator"> Logical NOT operator ([code]not[/code] or [code]![/code]). </constant> - <constant name="OP_IN" value="24" enum="Variant.Operator"> + <constant name="OP_IN" value="23" enum="Variant.Operator"> Logical IN operator ([code]in[/code]). </constant> - <constant name="OP_MAX" value="25" enum="Variant.Operator"> + <constant name="OP_MAX" value="24" enum="Variant.Operator"> Represents the size of the [enum Variant.Operator] enum. </constant> </constants> diff --git a/doc/classes/AABB.xml b/doc/classes/AABB.xml index 4f95b44f83..baea84df65 100644 --- a/doc/classes/AABB.xml +++ b/doc/classes/AABB.xml @@ -14,7 +14,23 @@ <link title="Advanced vector math">https://docs.godotengine.org/en/latest/tutorials/math/vectors_advanced.html</link> </tutorials> <methods> - <method name="AABB"> + <method name="AABB" qualifiers="constructor"> + <return type="AABB"> + </return> + <description> + Constructs a default-initialized [AABB] with default (zero) values of [member position] and [member size]. + </description> + </method> + <method name="AABB" qualifiers="constructor"> + <return type="AABB"> + </return> + <argument index="0" name="from" type="AABB"> + </argument> + <description> + Constructs an [AABB] as a copy of the given [AABB]. + </description> + </method> + <method name="AABB" qualifiers="constructor"> <return type="AABB"> </return> <argument index="0" name="position" type="Vector3"> @@ -215,6 +231,30 @@ Returns a larger [AABB] that contains both this [AABB] and [code]with[/code]. </description> </method> + <method name="operator !=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="AABB"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="AABB"> + </return> + <argument index="0" name="right" type="Transform"> + </argument> + <description> + </description> + </method> + <method name="operator ==" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="AABB"> + </argument> + <description> + </description> + </method> </methods> <members> <member name="end" type="Vector3" setter="" getter="" default="Vector3( 0, 0, 0 )"> diff --git a/doc/classes/Array.xml b/doc/classes/Array.xml index d0f90f513d..0ad5960d4a 100644 --- a/doc/classes/Array.xml +++ b/doc/classes/Array.xml @@ -38,48 +38,56 @@ GD.Print(array1 + array2); // Prints [One, 2, 3, Four] [/csharp] [/codeblocks] + Note that concatenating with [code]+=[/code] operator will create a new array. If you want to append another array to an existing array, [method append_array] is more efficient. [b]Note:[/b] Arrays are always passed by reference. To get a copy of an array which can be modified independently of the original array, use [method duplicate]. </description> <tutorials> </tutorials> <methods> - <method name="Array"> + <method name="Array" qualifiers="constructor"> <return type="Array"> </return> - <argument index="0" name="from" type="PackedColorArray"> + <description> + Constructs an empty [Array]. + </description> + </method> + <method name="Array" qualifiers="constructor"> + <return type="Array"> + </return> + <argument index="0" name="from" type="Array"> </argument> <description> - Constructs an array from a [PackedColorArray]. + Constructs an [Array] as a copy of the given [Array]. </description> </method> - <method name="Array"> + <method name="Array" qualifiers="constructor"> <return type="Array"> </return> - <argument index="0" name="from" type="PackedVector3Array"> + <argument index="0" name="from" type="PackedByteArray"> </argument> <description> - Constructs an array from a [PackedVector3Array]. + Constructs an array from a [PackedByteArray]. </description> </method> - <method name="Array"> + <method name="Array" qualifiers="constructor"> <return type="Array"> </return> - <argument index="0" name="from" type="PackedVector2Array"> + <argument index="0" name="from" type="PackedColorArray"> </argument> <description> - Constructs an array from a [PackedVector2Array]. + Constructs an array from a [PackedColorArray]. </description> </method> - <method name="Array"> + <method name="Array" qualifiers="constructor"> <return type="Array"> </return> - <argument index="0" name="from" type="PackedStringArray"> + <argument index="0" name="from" type="PackedFloat32Array"> </argument> <description> - Constructs an array from a [PackedStringArray]. + Constructs an array from a [PackedFloat32Array]. </description> </method> - <method name="Array"> + <method name="Array" qualifiers="constructor"> <return type="Array"> </return> <argument index="0" name="from" type="PackedFloat64Array"> @@ -88,16 +96,16 @@ Constructs an array from a [PackedFloat64Array]. </description> </method> - <method name="Array"> + <method name="Array" qualifiers="constructor"> <return type="Array"> </return> - <argument index="0" name="from" type="PackedFloat32Array"> + <argument index="0" name="from" type="PackedInt32Array"> </argument> <description> - Constructs an array from a [PackedFloat32Array]. + Constructs an array from a [PackedInt32Array]. </description> </method> - <method name="Array"> + <method name="Array" qualifiers="constructor"> <return type="Array"> </return> <argument index="0" name="from" type="PackedInt64Array"> @@ -106,22 +114,31 @@ Constructs an array from a [PackedInt64Array]. </description> </method> - <method name="Array"> + <method name="Array" qualifiers="constructor"> <return type="Array"> </return> - <argument index="0" name="from" type="PackedInt32Array"> + <argument index="0" name="from" type="PackedStringArray"> </argument> <description> - Constructs an array from a [PackedInt32Array]. + Constructs an array from a [PackedStringArray]. </description> </method> - <method name="Array"> + <method name="Array" qualifiers="constructor"> <return type="Array"> </return> - <argument index="0" name="from" type="PackedByteArray"> + <argument index="0" name="from" type="PackedVector2Array"> </argument> <description> - Constructs an array from a [PackedByteArray]. + Constructs an array from a [PackedVector2Array]. + </description> + </method> + <method name="Array" qualifiers="constructor"> + <return type="Array"> + </return> + <argument index="0" name="from" type="PackedVector3Array"> + </argument> + <description> + Constructs an array from a [PackedVector3Array]. </description> </method> <method name="append"> @@ -133,6 +150,21 @@ Appends an element at the end of the array (alias of [method push_back]). </description> </method> + <method name="append_array"> + <return type="void"> + </return> + <argument index="0" name="array" type="Array"> + </argument> + <description> + Appends another array at the end of this array. + [codeblock] + var array1 = [1, 2, 3] + var array2 = [4, 5, 6] + array1.append_array(array2) + print(array1) # Prints [1, 2, 3, 4, 5, 6]. + [/codeblock] + </description> + </method> <method name="back"> <return type="Variant"> </return> @@ -320,6 +352,70 @@ Returns the minimum value contained in the array if all elements are of comparable types. If the elements can't be compared, [code]null[/code] is returned. </description> </method> + <method name="operator !=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Array"> + </argument> + <description> + </description> + </method> + <method name="operator +" qualifiers="operator"> + <return type="Array"> + </return> + <argument index="0" name="right" type="Array"> + </argument> + <description> + </description> + </method> + <method name="operator <" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Array"> + </argument> + <description> + </description> + </method> + <method name="operator <=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Array"> + </argument> + <description> + </description> + </method> + <method name="operator ==" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Array"> + </argument> + <description> + </description> + </method> + <method name="operator >" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Array"> + </argument> + <description> + </description> + </method> + <method name="operator >=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Array"> + </argument> + <description> + </description> + </method> + <method name="operator []" qualifiers="operator"> + <return type="void"> + </return> + <argument index="0" name="index" type="int"> + </argument> + <description> + </description> + </method> <method name="pop_back"> <return type="Variant"> </return> diff --git a/doc/classes/Basis.xml b/doc/classes/Basis.xml index 4201a31402..877d3ca85a 100644 --- a/doc/classes/Basis.xml +++ b/doc/classes/Basis.xml @@ -19,26 +19,23 @@ <link title="2.5D Demo">https://godotengine.org/asset-library/asset/583</link> </tutorials> <methods> - <method name="Basis"> + <method name="Basis" qualifiers="constructor"> <return type="Basis"> </return> - <argument index="0" name="from" type="Quat"> - </argument> <description> - Constructs a pure rotation basis matrix from the given quaternion. + Constructs a default-initialized [Basis] set to [constant IDENTITY]. </description> </method> - <method name="Basis"> + <method name="Basis" qualifiers="constructor"> <return type="Basis"> </return> - <argument index="0" name="from" type="Vector3"> + <argument index="0" name="from" type="Basis"> </argument> <description> - Constructs a pure rotation basis matrix from the given Euler angles (in the YXZ convention: when *composing*, first Y, then X, and Z last), given in the vector format as (X angle, Y angle, Z angle). - Consider using the [Quat] constructor instead, which uses a quaternion instead of Euler angles. + Constructs a [Basis] as a copy of the given [Basis]. </description> </method> - <method name="Basis"> + <method name="Basis" qualifiers="constructor"> <return type="Basis"> </return> <argument index="0" name="axis" type="Vector3"> @@ -49,7 +46,26 @@ Constructs a pure rotation basis matrix, rotated around the given [code]axis[/code] by [code]phi[/code], in radians. The axis must be a normalized vector. </description> </method> - <method name="Basis"> + <method name="Basis" qualifiers="constructor"> + <return type="Basis"> + </return> + <argument index="0" name="euler" type="Vector3"> + </argument> + <description> + Constructs a pure rotation basis matrix from the given Euler angles (in the YXZ convention: when *composing*, first Y, then X, and Z last), given in the vector format as (X angle, Y angle, Z angle). + Consider using the [Quat] constructor instead, which uses a quaternion instead of Euler angles. + </description> + </method> + <method name="Basis" qualifiers="constructor"> + <return type="Basis"> + </return> + <argument index="0" name="from" type="Quat"> + </argument> + <description> + Constructs a pure rotation basis matrix from the given quaternion. + </description> + </method> + <method name="Basis" qualifiers="constructor"> <return type="Basis"> </return> <argument index="0" name="x_axis" type="Vector3"> @@ -115,6 +131,46 @@ Returns [code]true[/code] if this basis and [code]b[/code] are approximately equal, by calling [code]is_equal_approx[/code] on each component. </description> </method> + <method name="operator !=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Basis"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Vector3"> + </return> + <argument index="0" name="right" type="Vector3"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Basis"> + </return> + <argument index="0" name="right" type="Basis"> + </argument> + <description> + </description> + </method> + <method name="operator ==" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Basis"> + </argument> + <description> + </description> + </method> + <method name="operator []" qualifiers="operator"> + <return type="Vector3"> + </return> + <argument index="0" name="index" type="int"> + </argument> + <description> + </description> + </method> <method name="orthonormalized"> <return type="Basis"> </return> diff --git a/doc/classes/Callable.xml b/doc/classes/Callable.xml index 7aaf087540..f137ede90f 100644 --- a/doc/classes/Callable.xml +++ b/doc/classes/Callable.xml @@ -36,15 +36,31 @@ <tutorials> </tutorials> <methods> - <method name="Callable"> + <method name="Callable" qualifiers="constructor"> + <return type="Callable"> + </return> + <description> + Constructs a null [Callable] with no object nor method bound. + </description> + </method> + <method name="Callable" qualifiers="constructor"> + <return type="Callable"> + </return> + <argument index="0" name="from" type="Callable"> + </argument> + <description> + Constructs a [Callable] as a copy of the given [Callable]. + </description> + </method> + <method name="Callable" qualifiers="constructor"> <return type="Callable"> </return> <argument index="0" name="object" type="Object"> </argument> - <argument index="1" name="method_name" type="StringName"> + <argument index="1" name="method" type="StringName"> </argument> <description> - Creates a new [Callable] for the method called [code]method_name[/code] in the specified [code]object[/code]. + Creates a new [Callable] for the method called [code]method[/code] in the specified [code]object[/code]. </description> </method> <method name="bind" qualifiers="vararg"> @@ -112,6 +128,22 @@ <description> </description> </method> + <method name="operator !=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Callable"> + </argument> + <description> + </description> + </method> + <method name="operator ==" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Callable"> + </argument> + <description> + </description> + </method> <method name="unbind"> <return type="Callable"> </return> diff --git a/doc/classes/Color.xml b/doc/classes/Color.xml index ca52d27a52..ae8703c837 100644 --- a/doc/classes/Color.xml +++ b/doc/classes/Color.xml @@ -16,74 +16,31 @@ <link title="GUI Drag And Drop Demo">https://godotengine.org/asset-library/asset/133</link> </tutorials> <methods> - <method name="Color"> + <method name="Color" qualifiers="constructor"> <return type="Color"> </return> - <argument index="0" name="from" type="String"> - </argument> <description> - Constructs a color from an HTML hexadecimal color string in RGB or RGBA format. See also [method @GDScript.ColorN]. - [codeblocks] - [gdscript] - # Each of the following creates the same color RGBA(178, 217, 10, 255). - var c3 = Color("#b2d90a") # RGB format with "#". - var c4 = Color("b2d90a") # RGB format. - var c1 = Color("#b2d90aff") # RGBA format with "#". - var c2 = Color("b2d90aff") # RGBA format. - [/gdscript] - [csharp] - // Each of the following creates the same color RGBA(178, 217, 10, 255). - var c3 = new Color("#b2d90a"); - var c4 = new Color("b2d90a"); // RGB format. - var c1 = new Color("#b2d90aff"); - var c2 = new Color("b2d90aff"); // RGBA format. - [/csharp] - [/codeblocks] - You can also use the "web color" short-hand form by only using 3 or 4 digits. - [codeblocks] - [gdscript] - # Each of the following creates the same color RGBA(17, 34, 51, 255). - var c3 = Color("#123") # RGB format with "#". - var c4 = Color("123") # RGB format. - var c1 = Color("#123f") # RGBA format with "#". - var c2 = Color("123f") # RGBA format. - [/gdscript] - [csharp] - // Each of the following creates the same color RGBA(17, 34, 51, 255). - var c3 = new Color("#123"); - var c4 = new Color("123"); // RGB format. - var c1 = new Color("#123f"); - var c2 = new Color("123f"); // RGBA format. - [/csharp] - [/codeblocks] + Constructs a default-initialized [Color] with all components set to [code]0[/code]. </description> </method> - <method name="Color"> + <method name="Color" qualifiers="constructor"> <return type="Color"> </return> - <argument index="0" name="from" type="int"> + <argument index="0" name="from" type="Color"> </argument> <description> - Constructs a color from a 32-bit integer (each byte represents a component of the RGBA profile). - [codeblocks] - [gdscript] - var c = Color(274) # Equivalent to RGBA(0, 0, 1, 18) - [/gdscript] - [csharp] - var c = new Color(274); // Equivalent to RGBA(0, 0, 1, 18) - [/csharp] - [/codeblocks] + Constructs a [Color] as a copy of the given [Color]. </description> </method> - <method name="Color"> + <method name="Color" qualifiers="constructor"> <return type="Color"> </return> - <argument index="0" name="c" type="Color"> + <argument index="0" name="from" type="Color"> </argument> - <argument index="1" name="a" type="float"> + <argument index="1" name="alpha" type="float"> </argument> <description> - Constructs a color from an existing color, but with a custom alpha value. + Constructs a [Color] from an existing color, but with a custom alpha value. [codeblocks] [gdscript] var red = Color(Color.red, 0.5) # 50% transparent red. @@ -94,7 +51,7 @@ [/codeblocks] </description> </method> - <method name="Color"> + <method name="Color" qualifiers="constructor"> <return type="Color"> </return> <argument index="0" name="r" type="float"> @@ -103,19 +60,21 @@ </argument> <argument index="2" name="b" type="float"> </argument> + <argument index="3" name="a" type="float"> + </argument> <description> - Constructs a color from an RGB profile using values between 0 and 1. Alpha will always be 1. + Constructs a [Color] from an RGBA profile using values between 0 and 1. [codeblocks] [gdscript] - var color = Color(0.2, 1.0, 0.7) # Equivalent to RGBA(51, 255, 178, 255) + var color = Color(0.2, 1.0, 0.7, 0.8) # Equivalent to RGBA(51, 255, 178, 204) [/gdscript] [csharp] - var color = new Color(0.2f, 1.0f, 0.7f); // Equivalent to RGBA(51, 255, 178, 255) + var color = new Color(0.2f, 1.0f, 0.7f, 0.8f); // Equivalent to RGBA(51, 255, 178, 255, 204) [/csharp] [/codeblocks] </description> </method> - <method name="Color"> + <method name="Color" qualifiers="constructor"> <return type="Color"> </return> <argument index="0" name="r" type="float"> @@ -124,16 +83,14 @@ </argument> <argument index="2" name="b" type="float"> </argument> - <argument index="3" name="a" type="float"> - </argument> <description> - Constructs a color from an RGBA profile using values between 0 and 1. + Constructs a color from an RGB profile using values between 0 and 1. Alpha will always be 1. [codeblocks] [gdscript] - var color = Color(0.2, 1.0, 0.7, 0.8) # Equivalent to RGBA(51, 255, 178, 204) + var color = Color(0.2, 1.0, 0.7) # Equivalent to RGBA(51, 255, 178, 255) [/gdscript] [csharp] - var color = new Color(0.2f, 1.0f, 0.7f, 0.8f); // Equivalent to RGBA(51, 255, 178, 255, 204) + var color = new Color(0.2f, 1.0f, 0.7f); // Equivalent to RGBA(51, 255, 178, 255) [/csharp] [/codeblocks] </description> @@ -246,6 +203,98 @@ [/codeblocks] </description> </method> + <method name="operator !=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Color"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Color"> + </return> + <argument index="0" name="right" type="Color"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Color"> + </return> + <argument index="0" name="right" type="float"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Color"> + </return> + <argument index="0" name="right" type="int"> + </argument> + <description> + </description> + </method> + <method name="operator +" qualifiers="operator"> + <return type="Color"> + </return> + <description> + </description> + </method> + <method name="operator +" qualifiers="operator"> + <return type="Color"> + </return> + <argument index="0" name="right" type="Color"> + </argument> + <description> + </description> + </method> + <method name="operator -" qualifiers="operator"> + <return type="Color"> + </return> + <description> + </description> + </method> + <method name="operator /" qualifiers="operator"> + <return type="Color"> + </return> + <argument index="0" name="right" type="Color"> + </argument> + <description> + </description> + </method> + <method name="operator /" qualifiers="operator"> + <return type="Color"> + </return> + <argument index="0" name="right" type="float"> + </argument> + <description> + </description> + </method> + <method name="operator /" qualifiers="operator"> + <return type="Color"> + </return> + <argument index="0" name="right" type="int"> + </argument> + <description> + </description> + </method> + <method name="operator ==" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Color"> + </argument> + <description> + </description> + </method> + <method name="operator []" qualifiers="operator"> + <return type="float"> + </return> + <argument index="0" name="index" type="int"> + </argument> + <description> + </description> + </method> <method name="to_abgr32"> <return type="int"> </return> diff --git a/doc/classes/DTLSServer.xml b/doc/classes/DTLSServer.xml index 8c71b61553..91a04b1f28 100644 --- a/doc/classes/DTLSServer.xml +++ b/doc/classes/DTLSServer.xml @@ -6,8 +6,9 @@ <description> This class is used to store the state of a DTLS server. Upon [method setup] it converts connected [PacketPeerUDP] to [PacketPeerDTLS] accepting them via [method take_connection] as DTLS clients. Under the hood, this class is used to store the DTLS state and cookies of the server. The reason of why the state and cookies are needed is outside of the scope of this documentation. Below a small example of how to use it: - [codeblock] - # server.gd + [codeblocks] + [gdscript] + # ServerNode.gd extends Node var dtls := DTLSServer.new() @@ -28,15 +29,64 @@ continue # It is normal that 50% of the connections fails due to cookie exchange. print("Peer connected!") peers.append(dtls_peer) + for p in peers: p.poll() # Must poll to update the state. if p.get_status() == PacketPeerDTLS.STATUS_CONNECTED: while p.get_available_packet_count() > 0: print("Received message from client: %s" % p.get_packet().get_string_from_utf8()) p.put_packet("Hello DTLS client".to_utf8()) - [/codeblock] - [codeblock] - # client.gd + [/gdscript] + [csharp] + using Godot; + using System; + // ServerNode.cs + public class ServerNode : Node + { + public DTLSServer Dtls = new DTLSServer(); + public UDPServer Server = new UDPServer(); + public Godot.Collections.Array<PacketPeerDTLS> Peers = new Godot.Collections.Array<PacketPeerDTLS>(); + public override void _Ready() + { + Server.Listen(4242); + var key = GD.Load<CryptoKey>("key.key"); // Your private key. + var cert = GD.Load<X509Certificate>("cert.crt"); // Your X509 certificate. + Dtls.Setup(key, cert); + } + + public override void _Process(float delta) + { + while (Server.IsConnectionAvailable()) + { + PacketPeerUDP peer = Server.TakeConnection(); + PacketPeerDTLS dtlsPeer = Dtls.TakeConnection(peer); + if (dtlsPeer.GetStatus() != PacketPeerDTLS.Status.Handshaking) + { + continue; // It is normal that 50% of the connections fails due to cookie exchange. + } + GD.Print("Peer connected!"); + Peers.Add(dtlsPeer); + } + + foreach (var p in Peers) + { + p.Poll(); // Must poll to update the state. + if (p.GetStatus() == PacketPeerDTLS.Status.Connected) + { + while (p.GetAvailablePacketCount() > 0) + { + GD.Print("Received Message From Client: " + p.GetPacket().GetStringFromUTF8()); + p.PutPacket("Hello Dtls Client".ToUTF8()); + } + } + } + } + } + [/csharp] + [/codeblocks] + [codeblocks] + [gdscript] + # ClientNode.gd extends Node var dtls := PacketPeerDTLS.new() @@ -56,7 +106,42 @@ while dtls.get_available_packet_count() > 0: print("Connected: %s" % dtls.get_packet().get_string_from_utf8()) connected = true - [/codeblock] + [/gdscript] + [csharp] + using Godot; + using System.Text; + // ClientNode.cs + public class ClientNode : Node + { + public PacketPeerDTLS Dtls = new PacketPeerDTLS(); + public PacketPeerUDP Udp = new PacketPeerUDP(); + public bool Connected = false; + public override void _Ready() + { + Udp.ConnectToHost("127.0.0.1", 4242); + Dtls.ConnectToPeer(Udp, false); // Use true in production for certificate validation! + } + + public override void _Process(float delta) + { + Dtls.Poll(); + if (Dtls.GetStatus() == PacketPeerDTLS.Status.Connected) + { + if (!Connected) + { + // Try to contact server + Dtls.PutPacket("The Answer Is..42!".ToUTF8()); + } + while (Dtls.GetAvailablePacketCount() > 0) + { + GD.Print("Connected: " + Dtls.GetPacket().GetStringFromUTF8()); + Connected = true; + } + } + } + } + [/csharp] + [/codeblocks] </description> <tutorials> </tutorials> diff --git a/doc/classes/Decal.xml b/doc/classes/Decal.xml index f9baa3b09a..8107d97b67 100644 --- a/doc/classes/Decal.xml +++ b/doc/classes/Decal.xml @@ -20,10 +20,18 @@ Returns the [Texture2D] associated with the specified [enum DecalTexture]. This is a convenience method, in most cases you should access the texture directly. For example, instead of [code]albedo_tex = $Decal.get_texture(Decal.TEXTURE_ALBEDO)[/code], use [code]albedo_tex = $Decal.texture_albedo[/code]. One case where this is better than accessing the texture directly is when you want to copy one Decal's textures to another. For example: - [codeblock] + [codeblocks] + [gdscript] for i in Decal.TEXTURE_MAX: $NewDecal.set_texture(i, $OldDecal.get_texture(i)) - [/codeblock] + [/gdscript] + [csharp] + for (int i = 0; i < (int)Decal.DecalTexture.Max; i++) + { + GetNode<Decal>("NewDecal").SetTexture(i, GetNode<Decal>("OldDecal").GetTexture(i)); + } + [/csharp] + [/codeblocks] </description> </method> <method name="set_texture"> @@ -37,10 +45,18 @@ Sets the [Texture2D] associated with the specified [enum DecalTexture]. This is a convenience method, in most cases you should access the texture directly. For example, instead of [code]$Decal.set_texture(Decal.TEXTURE_ALBEDO, albedo_tex)[/code], use [code]$Decal.texture_albedo = albedo_tex[/code]. One case where this is better than accessing the texture directly is when you want to copy one Decal's textures to another. For example: - [codeblock] + [codeblocks] + [gdscript] for i in Decal.TEXTURE_MAX: $NewDecal.set_texture(i, $OldDecal.get_texture(i)) - [/codeblock] + [/gdscript] + [csharp] + for (int i = 0; i < (int)Decal.DecalTexture.Max; i++) + { + GetNode<Decal>("NewDecal").SetTexture(i, GetNode<Decal>("OldDecal").GetTexture(i)); + } + [/csharp] + [/codeblocks] </description> </method> </methods> diff --git a/doc/classes/Dictionary.xml b/doc/classes/Dictionary.xml index cb60d6e621..dc38fdf0e8 100644 --- a/doc/classes/Dictionary.xml +++ b/doc/classes/Dictionary.xml @@ -9,7 +9,8 @@ Erasing elements while iterating over them [b]is not supported[/b] and will result in undefined behavior. [b]Note:[/b] Dictionaries are always passed by reference. To get a copy of a dictionary which can be modified independently of the original dictionary, use [method duplicate]. Creating a dictionary: - [codeblock] + [codeblocks] + [gdscript] var my_dir = {} # Creates an empty dictionary. var points_dir = {"White": 50, "Yellow": 75, "Orange": 100} var another_dir = { @@ -17,28 +18,74 @@ key2: value2, key3: value3, } - [/codeblock] + [/gdscript] + [csharp] + var myDir = new Godot.Collections.Dictionary(); // Creates an empty dictionary. + var pointsDir = new Godot.Collections.Dictionary + { + {"White", 50}, + {"Yellow", 75}, + {"Orange", 100} + }; + [/csharp] + [/codeblocks] You can access a dictionary's values by referencing the appropriate key. In the above example, [code]points_dir["White"][/code] will return [code]50[/code]. You can also write [code]points_dir.White[/code], which is equivalent. However, you'll have to use the bracket syntax if the key you're accessing the dictionary with isn't a fixed string (such as a number or variable). - [codeblock] - export(String, "White", "Yellow", "Orange") var my_color + [codeblocks] + [gdscript] + export(string, "White", "Yellow", "Orange") var my_color var points_dir = {"White": 50, "Yellow": 75, "Orange": 100} - func _ready(): # We can't use dot syntax here as `my_color` is a variable. var points = points_dir[my_color] - [/codeblock] + [/gdscript] + [csharp] + [Export(PropertyHint.Enum, "White,Yellow,Orange")] + public string MyColor { get; set; } + public Godot.Collections.Dictionary pointsDir = new Godot.Collections.Dictionary + { + {"White", 50}, + {"Yellow", 75}, + {"Orange", 100} + }; + + public override void _Ready() + { + int points = (int)pointsDir[MyColor]; + } + [/csharp] + [/codeblocks] In the above code, [code]points[/code] will be assigned the value that is paired with the appropriate color selected in [code]my_color[/code]. Dictionaries can contain more complex data: - [codeblock] + [codeblocks] + [gdscript] my_dir = {"First Array": [1, 2, 3, 4]} # Assigns an Array to a String key. - [/codeblock] + [/gdscript] + [csharp] + var myDir = new Godot.Collections.Dictionary + { + {"First Array", new Godot.Collections.Array{1, 2, 3, 4}} + }; + [/csharp] + [/codeblocks] To add a key to an existing dictionary, access it like an existing key and assign to it: - [codeblock] + [codeblocks] + [gdscript] var points_dir = {"White": 50, "Yellow": 75, "Orange": 100} points_dir["Blue"] = 150 # Add "Blue" as a key and assign 150 as its value. - [/codeblock] + [/gdscript] + [csharp] + var pointsDir = new Godot.Collections.Dictionary + { + {"White", 50}, + {"Yellow", 75}, + {"Orange", 100} + }; + pointsDir["blue"] = 150; // Add "Blue" as a key and assign 150 as its value. + [/csharp] + [/codeblocks] Finally, dictionaries can contain different types of keys and values in the same dictionary: - [codeblock] + [codeblocks] + [gdscript] # This is a valid dictionary. # To access the string "Nested value" below, use `my_dir.sub_dir.sub_key` or `my_dir["sub_dir"]["sub_key"]`. # Indexing styles can be mixed and matched depending on your needs. @@ -48,29 +95,75 @@ 7: "Hello", "sub_dir": {"sub_key": "Nested value"}, } - [/codeblock] + [/gdscript] + [csharp] + // This is a valid dictionary. + // To access the string "Nested value" below, use `my_dir.sub_dir.sub_key` or `my_dir["sub_dir"]["sub_key"]`. + // Indexing styles can be mixed and matched depending on your needs. + var myDir = new Godot.Collections.Dictionary { + {"String Key", 5}, + {4, new Godot.Collections.Array{1,2,3}}, + {7, "Hello"}, + {"sub_dir", new Godot.Collections.Dictionary{{"sub_key", "Nested value"}}} + }; + [/csharp] + [/codeblocks] [b]Note:[/b] Unlike [Array]s, you can't compare dictionaries directly: - [codeblock] - array1 = [1, 2, 3] - array2 = [1, 2, 3] + [codeblocks] + [gdscript] + var array1 = [1, 2, 3] + var array2 = [1, 2, 3] func compare_arrays(): print(array1 == array2) # Will print true. - dir1 = {"a": 1, "b": 2, "c": 3} - dir2 = {"a": 1, "b": 2, "c": 3} + var dir1 = {"a": 1, "b": 2, "c": 3} + var dir2 = {"a": 1, "b": 2, "c": 3} func compare_dictionaries(): print(dir1 == dir2) # Will NOT print true. - [/codeblock] + [/gdscript] + [csharp] + // You have to use GD.Hash(). + + public Godot.Collections.Array array1 = new Godot.Collections.Array{1, 2, 3}; + public Godot.Collections.Array array2 = new Godot.Collections.Array{1, 2, 3}; + + public void CompareArrays() + { + GD.Print(array1 == array2); // Will print FALSE!! + GD.Print(GD.Hash(array1) == GD.Hash(array2)); // Will print true. + } + + public Godot.Collections.Dictionary dir1 = new Godot.Collections.Dictionary{{"a", 1}, {"b", 2}, {"c", 3}}; + public Godot.Collections.Dictionary dir2 = new Godot.Collections.Dictionary{{"a", 1}, {"b", 2}, {"c", 3}}; + + public void CompareDictionaries() + { + GD.Print(dir1 == dir2); // Will NOT print true. + } + [/csharp] + [/codeblocks] You need to first calculate the dictionary's hash with [method hash] before you can compare them: - [codeblock] - dir1 = {"a": 1, "b": 2, "c": 3} - dir2 = {"a": 1, "b": 2, "c": 3} + [codeblocks] + [gdscript] + var dir1 = {"a": 1, "b": 2, "c": 3} + var dir2 = {"a": 1, "b": 2, "c": 3} func compare_dictionaries(): print(dir1.hash() == dir2.hash()) # Will print true. - [/codeblock] + [/gdscript] + [csharp] + // You have to use GD.Hash(). + public Godot.Collections.Dictionary dir1 = new Godot.Collections.Dictionary{{"a", 1}, {"b", 2}, {"c", 3}}; + public Godot.Collections.Dictionary dir2 = new Godot.Collections.Dictionary{{"a", 1}, {"b", 2}, {"c", 3}}; + + public void CompareDictionaries() + { + GD.Print(GD.Hash(dir1) == GD.Hash(dir2)); // Will print true. + } + [/csharp] + [/codeblocks] </description> <tutorials> <link title="GDScript basics: Dictionary">https://docs.godotengine.org/en/latest/getting_started/scripting/gdscript/gdscript_basics.html#dictionary</link> @@ -78,6 +171,22 @@ <link title="OS Test Demo">https://godotengine.org/asset-library/asset/677</link> </tutorials> <methods> + <method name="Dictionary" qualifiers="constructor"> + <return type="Dictionary"> + </return> + <description> + Constructs an empty [Dictionary]. + </description> + </method> + <method name="Dictionary" qualifiers="constructor"> + <return type="Dictionary"> + </return> + <argument index="0" name="from" type="Dictionary"> + </argument> + <description> + Constructs a [Dictionary] as a copy of the given [Dictionary]. + </description> + </method> <method name="clear"> <return type="void"> </return> @@ -129,11 +238,20 @@ <description> Returns [code]true[/code] if the dictionary has a given key. [b]Note:[/b] This is equivalent to using the [code]in[/code] operator as follows: - [codeblock] + [codeblocks] + [gdscript] # Will evaluate to `true`. if "godot" in {"godot": "engine"}: pass - [/codeblock] + [/gdscript] + [csharp] + // You have to use Contains() here as an alternative to GDScript's `in` operator. + if (new Godot.Collections.Dictionary{{"godot", "engine"}}.Contains("godot")) + { + // I am executed. + } + [/csharp] + [/codeblocks] This method (like the [code]in[/code] operator) will evaluate to [code]true[/code] as long as the key exists, even if the associated value is [code]null[/code]. </description> </method> @@ -151,12 +269,21 @@ </return> <description> Returns a hashed integer value representing the dictionary contents. This can be used to compare dictionaries by value: - [codeblock] + [codeblocks] + [gdscript] var dict1 = {0: 10} var dict2 = {0: 10} # The line below prints `true`, whereas it would have printed `false` if both variables were compared directly. print(dict1.hash() == dict2.hash()) - [/codeblock] + [/gdscript] + [csharp] + var dict1 = new Godot.Collections.Dictionary{{0, 10}}; + var dict2 = new Godot.Collections.Dictionary{{0, 10}}; + // The line below prints `true`, whereas it would have printed `false` if both variables were compared directly. + // Dictionary has no Hash() method. Use GD.Hash() instead. + GD.Print(GD.Hash(dict1) == GD.Hash(dict2)); + [/csharp] + [/codeblocks] [b]Note:[/b] Dictionaries with the same keys/values but in a different order will have a different hash. </description> </method> @@ -167,6 +294,30 @@ Returns the list of keys in the [Dictionary]. </description> </method> + <method name="operator !=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Dictionary"> + </argument> + <description> + </description> + </method> + <method name="operator ==" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Dictionary"> + </argument> + <description> + </description> + </method> + <method name="operator []" qualifiers="operator"> + <return type="Variant"> + </return> + <argument index="0" name="key" type="Variant"> + </argument> + <description> + </description> + </method> <method name="size"> <return type="int"> </return> diff --git a/doc/classes/Directory.xml b/doc/classes/Directory.xml index bcdadcd970..2d7292717d 100644 --- a/doc/classes/Directory.xml +++ b/doc/classes/Directory.xml @@ -7,7 +7,8 @@ Directory type. It is used to manage directories and their content (not restricted to the project folder). When creating a new [Directory], it must be explicitly opened using [method open] before most methods can be used. However, [method file_exists] and [method dir_exists] can be used without opening a directory. If so, they use a path relative to [code]res://[/code]. Here is an example on how to iterate through the files of a directory: - [codeblock] + [codeblocks] + [gdscript] func dir_contents(path): var dir = Directory.new() if dir.open(path) == OK: @@ -21,7 +22,35 @@ file_name = dir.get_next() else: print("An error occurred when trying to access the path.") - [/codeblock] + [/gdscript] + [csharp] + public void DirContents(string path) + { + var dir = new Directory(); + if (dir.Open(path) == Error.Ok) + { + dir.ListDirBegin(); + string fileName = dir.GetNext(); + while (fileName != "") + { + if (dir.CurrentIsDir()) + { + GD.Print("Found directory: " + fileName); + } + else + { + GD.Print("Found file: " + fileName); + } + fileName = dir.GetNext(); + } + } + else + { + GD.Print("An error occurred when trying to access the path."); + } + } + [/csharp] + [/codeblocks] </description> <tutorials> <link title="File system">https://docs.godotengine.org/en/latest/getting_started/step_by_step/filesystem.html</link> diff --git a/doc/classes/DisplayServer.xml b/doc/classes/DisplayServer.xml index 1d437739e2..d91ea6528a 100644 --- a/doc/classes/DisplayServer.xml +++ b/doc/classes/DisplayServer.xml @@ -923,7 +923,8 @@ <description> Sets a polygonal region of the window which accepts mouse events. Mouse events outside the region will be passed through. Passing an empty array will disable passthrough support (all mouse events will be intercepted by the window, which is the default behavior). - [codeblock] + [codeblocks] + [gdscript] # Set region, using Path2D node. DisplayServer.window_set_mouse_passthrough($Path2D.curve.get_baked_points()) @@ -932,7 +933,18 @@ # Reset region to default. DisplayServer.window_set_mouse_passthrough([]) - [/codeblock] + [/gdscript] + [csharp] + // Set region, using Path2D node. + DisplayServer.WindowSetMousePassthrough(GetNode<Path2D>("Path2D").Curve.GetBakedPoints()); + + // Set region, using Polygon2D node. + DisplayServer.WindowSetMousePassthrough(GetNode<Polygon2D>("Polygon2D").Polygon); + + // Reset region to default. + DisplayServer.WindowSetMousePassthrough(new Vector2[] {}); + [/csharp] + [/codeblocks] [b]Note:[/b] On Windows, the portion of a window that lies outside the region is not drawn, while on Linux and macOS it is. [b]Note:[/b] This method is implemented on Linux, macOS and Windows. </description> diff --git a/doc/classes/DynamicFont.xml b/doc/classes/DynamicFont.xml index f687937e27..d2d0c54761 100644 --- a/doc/classes/DynamicFont.xml +++ b/doc/classes/DynamicFont.xml @@ -6,12 +6,20 @@ <description> DynamicFont renders vector font files (such as TTF or OTF) dynamically at runtime instead of using a prerendered texture atlas like [BitmapFont]. This trades the faster loading time of [BitmapFont]s for the ability to change font parameters like size and spacing during runtime. [DynamicFontData] is used for referencing the font file paths. DynamicFont also supports defining one or more fallback fonts, which will be used when displaying a character not supported by the main font. DynamicFont uses the [url=https://www.freetype.org/]FreeType[/url] library for rasterization. - [codeblock] + [codeblocks] + [gdscript] var dynamic_font = DynamicFont.new() dynamic_font.font_data = load("res://BarlowCondensed-Bold.ttf") dynamic_font.size = 64 $"Label".set("custom_fonts/font", dynamic_font) - [/codeblock] + [/gdscript] + [csharp] + var dynamicFont = new DynamicFont(); + dynamicFont.FontData = ResourceLoader.Load<DynamicFontData>("res://BarlowCondensed-Bold.ttf"); + dynamicFont.Size = 64; + GetNode("Label").Set("custom_fonts/font", dynamicFont); + [/csharp] + [/codeblocks] [b]Note:[/b] DynamicFont doesn't support features such as kerning, right-to-left typesetting, ligatures, text shaping, variable fonts and optional font features yet. If you wish to "bake" an optional font feature into a TTF font file, you can use [url=https://fontforge.org/]FontForge[/url] to do so. In FontForge, use [b]File > Generate Fonts[/b], click [b]Options[/b], choose the desired features then generate the font. </description> <tutorials> diff --git a/doc/classes/EditorImportPlugin.xml b/doc/classes/EditorImportPlugin.xml index 2a7f27ef55..e5401134bf 100644 --- a/doc/classes/EditorImportPlugin.xml +++ b/doc/classes/EditorImportPlugin.xml @@ -7,7 +7,8 @@ EditorImportPlugins provide a way to extend the editor's resource import functionality. Use them to import resources from custom files or to provide alternatives to the editor's existing importers. Register your [EditorPlugin] with [method EditorPlugin.add_import_plugin]. EditorImportPlugins work by associating with specific file extensions and a resource type. See [method get_recognized_extensions] and [method get_resource_type]. They may optionally specify some import presets that affect the import process. EditorImportPlugins are responsible for creating the resources and saving them in the [code].godot/imported[/code] directory. Below is an example EditorImportPlugin that imports a [Mesh] from a file with the extension ".special" or ".spec": - [codeblock] + [codeblocks] + [gdscript] tool extends EditorImportPlugin @@ -39,14 +40,76 @@ var file = File.new() if file.open(source_file, File.READ) != OK: return FAILED - - var mesh = Mesh.new() - # Fill the Mesh with data read in "file", left as an exercise to the reader + var mesh = ArrayMesh.new() + # Fill the Mesh with data read in "file", left as an exercise to the reader. var filename = save_path + "." + get_save_extension() ResourceSaver.save(filename, mesh) return OK - [/codeblock] + [/gdscript] + [csharp] + using Godot; + using System; + + public class MySpecialPlugin : EditorImportPlugin + { + public override String GetImporterName() + { + return "my.special.plugin"; + } + + public override String GetVisibleName() + { + return "Special Mesh Importer"; + } + + public override Godot.Collections.Array GetRecognizedExtensions() + { + return new Godot.Collections.Array{"special", "spec"}; + } + + public override String GetSaveExtension() + { + return "mesh"; + } + + public override String GetResourceType() + { + return "Mesh"; + } + + public override int GetPresetCount() + { + return 1; + } + + public override String GetPresetName(int i) + { + return "Default"; + } + + public override Godot.Collections.Array GetImportOptions(int i) + { + return new Godot.Collections.Array{new Godot.Collections.Dictionary{{"name", "myOption"}, {"defaultValue", false}}}; + } + + public override int Import(String sourceFile, String savePath, Godot.Collections.Dictionary options, Godot.Collections.Array platformVariants, Godot.Collections.Array genFiles) + { + var file = new File(); + if (file.Open(sourceFile, File.ModeFlags.Read) != Error.Ok) + { + return (int)Error.Failed; + } + + var mesh = new ArrayMesh(); + // Fill the Mesh with data read in "file", left as an exercise to the reader. + String filename = savePath + "." + GetSaveExtension(); + ResourceSaver.Save(filename, mesh); + return (int)Error.Ok; + } + } + [/csharp] + [/codeblocks] </description> <tutorials> <link title="Import plugins">https://docs.godotengine.org/en/latest/tutorials/plugins/editor/import_plugins.html</link> @@ -84,14 +147,28 @@ </argument> <description> This method can be overridden to hide specific import options if conditions are met. This is mainly useful for hiding options that depend on others if one of them is disabled. For example: - [codeblock] + [codeblocks] + [gdscript] func get_option_visibility(option, options): # Only show the lossy quality setting if the compression mode is set to "Lossy". if option == "compress/lossy_quality" and options.has("compress/mode"): - return int(options["compress/mode"]) == COMPRESS_LOSSY + return int(options["compress/mode"]) == COMPRESS_LOSSY # This is a constant that you set return true - [/codeblock] + [/gdscript] + [csharp] + public void GetOptionVisibility(string option, Godot.Collections.Dictionary options) + { + // Only show the lossy quality setting if the compression mode is set to "Lossy". + if (option == "compress/lossyQuality" && options.Contains("compress/mode")) + { + return (int)options["compress/mode"] == COMPRESS_LOSSY; // This is a constant you set + } + + return true; + } + [/csharp] + [/codeblocks] Return [code]true[/code] to make all options always visible. </description> </method> diff --git a/doc/classes/EditorPlugin.xml b/doc/classes/EditorPlugin.xml index 95d88e86a6..ca011abb36 100644 --- a/doc/classes/EditorPlugin.xml +++ b/doc/classes/EditorPlugin.xml @@ -210,6 +210,38 @@ <argument index="0" name="overlay" type="Control"> </argument> <description> + Called by the engine when the 2D editor's viewport is updated. Use the [code]overlay[/code] [Control] for drawing. You can update the viewport manually by calling [method update_overlays]. + [codeblocks] + [gdscript] + func forward_canvas_draw_over_viewport(overlay): + # Draw a circle at cursor position. + overlay.draw_circle(overlay.get_local_mouse_position(), 64) + + func forward_canvas_gui_input(event): + if event is InputEventMouseMotion: + # Redraw viewport when cursor is moved. + update_overlays() + return true + return false + [/gdscript] + [csharp] + public override void ForwardCanvasDrawOverViewport(Godot.Control overlay) + { + // Draw a circle at cursor position. + overlay.DrawCircle(overlay.GetLocalMousePosition(), 64, Colors.White); + } + + public override bool ForwardCanvasGuiInput(InputEvent @event) + { + if (@event is InputEventMouseMotion) + { + // Redraw viewport when cursor is moved. + UpdateOverlays(); + return true; + } + return false; + [/csharp] + [/codeblocks] </description> </method> <method name="forward_canvas_force_draw_over_viewport" qualifiers="virtual"> @@ -218,6 +250,8 @@ <argument index="0" name="overlay" type="Control"> </argument> <description> + This method is the same as [method forward_canvas_draw_over_viewport], except it draws on top of everything. Useful when you need an extra layer that shows over anything else. + You need to enable calling of this method by using [method set_force_draw_over_forwarding_enabled]. </description> </method> <method name="forward_canvas_gui_input" qualifiers="virtual"> @@ -227,21 +261,85 @@ </argument> <description> Called when there is a root node in the current edited scene, [method handles] is implemented and an [InputEvent] happens in the 2D viewport. Intercepts the [InputEvent], if [code]return true[/code] [EditorPlugin] consumes the [code]event[/code], otherwise forwards [code]event[/code] to other Editor classes. Example: - [codeblock] + [codeblocks] + [gdscript] # Prevents the InputEvent to reach other Editor classes func forward_canvas_gui_input(event): - var forward = true - return forward - [/codeblock] + return true + [/gdscript] + [csharp] + // Prevents the InputEvent to reach other Editor classes + public override bool ForwardCanvasGuiInput(InputEvent @event) + { + return true; + } + [/csharp] + [/codeblocks] Must [code]return false[/code] in order to forward the [InputEvent] to other Editor classes. Example: - [codeblock] - # Consumes InputEventMouseMotion and forwards other InputEvent types + [codeblocks] + [gdscript] + # Consumes InputEventMouseMotion and forwards other InputEvent types. func forward_canvas_gui_input(event): - var forward = false + return event is InputEventMouseMotion + [/gdscript] + [csharp] + // Consumes InputEventMouseMotion and forwards other InputEvent types. + public override bool ForwardCanvasGuiInput(InputEvent @event) + { + return @event is InputEventMouseMotion; + } + [/csharp] + [/codeblocks] + </description> + </method> + <method name="forward_spatial_draw_over_viewport" qualifiers="virtual"> + <return type="void"> + </return> + <argument index="0" name="overlay" type="Control"> + </argument> + <description> + Called by the engine when the 3D editor's viewport is updated. Use the [code]overlay[/code] [Control] for drawing. You can update the viewport manually by calling [method update_overlays]. + [codeblocks] + [gdscript] + func forward_spatial_draw_over_viewport(overlay): + # Draw a circle at cursor position. + overlay.draw_circle(overlay.get_local_mouse_position(), 64) + + func forward_spatial_gui_input(camera, event): if event is InputEventMouseMotion: - forward = true - return forward - [/codeblock] + # Redraw viewport when cursor is moved. + update_overlays() + return true + return false + [/gdscript] + [csharp] + public override void ForwardSpatialDrawOverViewport(Godot.Control overlay) + { + // Draw a circle at cursor position. + overlay.DrawCircle(overlay.GetLocalMousePosition(), 64, Colors.White); + } + + public override bool ForwardSpatialGuiInput(Godot.Camera3D camera, InputEvent @event) + { + if (@event is InputEventMouseMotion) + { + // Redraw viewport when cursor is moved. + UpdateOverlays(); + return true; + } + return false; + [/csharp] + [/codeblocks] + </description> + </method> + <method name="forward_spatial_force_draw_over_viewport" qualifiers="virtual"> + <return type="void"> + </return> + <argument index="0" name="overlay" type="Control"> + </argument> + <description> + This method is the same as [method forward_spatial_draw_over_viewport], except it draws on top of everything. Useful when you need an extra layer that shows over anything else. + You need to enable calling of this method by using [method set_force_draw_over_forwarding_enabled]. </description> </method> <method name="forward_spatial_gui_input" qualifiers="virtual"> @@ -253,21 +351,35 @@ </argument> <description> Called when there is a root node in the current edited scene, [method handles] is implemented and an [InputEvent] happens in the 3D viewport. Intercepts the [InputEvent], if [code]return true[/code] [EditorPlugin] consumes the [code]event[/code], otherwise forwards [code]event[/code] to other Editor classes. Example: - [codeblock] - # Prevents the InputEvent to reach other Editor classes + [codeblocks] + [gdscript] + # Prevents the InputEvent to reach other Editor classes. func forward_spatial_gui_input(camera, event): - var forward = true - return forward - [/codeblock] + return true + [/gdscript] + [csharp] + // Prevents the InputEvent to reach other Editor classes. + public override bool ForwardSpatialGuiInput(Camera3D camera, InputEvent @event) + { + return true; + } + [/csharp] + [/codeblocks] Must [code]return false[/code] in order to forward the [InputEvent] to other Editor classes. Example: - [codeblock] - # Consumes InputEventMouseMotion and forwards other InputEvent types + [codeblocks] + [gdscript] + # Consumes InputEventMouseMotion and forwards other InputEvent types. func forward_spatial_gui_input(camera, event): - var forward = false - if event is InputEventMouseMotion: - forward = true - return forward - [/codeblock] + return event is InputEventMouseMotion + [/gdscript] + [csharp] + // Consumes InputEventMouseMotion and forwards other InputEvent types. + public override bool ForwardSpatialGuiInput(Camera3D camera, InputEvent @event) + { + return @event is InputEventMouseMotion; + } + [/csharp] + [/codeblocks] </description> </method> <method name="get_breakpoints" qualifiers="virtual"> @@ -291,13 +403,24 @@ Override this method in your plugin to return a [Texture2D] in order to give it an icon. For main screen plugins, this appears at the top of the screen, to the right of the "2D", "3D", "Script", and "AssetLib" buttons. Ideally, the plugin icon should be white with a transparent background and 16x16 pixels in size. - [codeblock] + [codeblocks] + [gdscript] func get_plugin_icon(): # You can use a custom icon: return preload("res://addons/my_plugin/my_plugin_icon.svg") # Or use a built-in icon: return get_editor_interface().get_base_control().get_icon("Node", "EditorIcons") - [/codeblock] + [/gdscript] + [csharp] + public override Texture2D GetPluginIcon() + { + // You can use a custom icon: + return ResourceLoader.Load<Texture2D>("res://addons/my_plugin/my_plugin_icon.svg"); + // Or use a built-in icon: + return GetEditorInterface().GetBaseControl().GetIcon("Node", "EditorIcons"); + } + [/csharp] + [/codeblocks] </description> </method> <method name="get_plugin_name" qualifiers="virtual"> @@ -511,6 +634,7 @@ <return type="void"> </return> <description> + Enables calling of [method forward_canvas_force_draw_over_viewport] for the 2D editor and [method forward_spatial_force_draw_over_viewport] for the 3D editor when their viewports are updated. You need to call this method only once and it will work permanently for this plugin. </description> </method> <method name="set_input_event_forwarding_always_enabled"> @@ -542,7 +666,7 @@ <return type="int"> </return> <description> - Updates the overlays of the editor (2D/3D) viewport. + Updates the overlays of the 2D and 3D editor viewport. Causes methods [method forward_canvas_draw_over_viewport], [method forward_canvas_force_draw_over_viewport], [method forward_spatial_draw_over_viewport] and [method forward_spatial_force_draw_over_viewport] to be called. </description> </method> </methods> diff --git a/doc/classes/EditorScenePostImport.xml b/doc/classes/EditorScenePostImport.xml index cb1f5d2e77..5cddecffa8 100644 --- a/doc/classes/EditorScenePostImport.xml +++ b/doc/classes/EditorScenePostImport.xml @@ -6,24 +6,50 @@ <description> Imported scenes can be automatically modified right after import by setting their [b]Custom Script[/b] Import property to a [code]tool[/code] script that inherits from this class. The [method post_import] callback receives the imported scene's root node and returns the modified version of the scene. Usage example: - [codeblock] - tool # Needed so it runs in editor + [codeblocks] + [gdscript] + tool # Needed so it runs in editor. extends EditorScenePostImport - - # This sample changes all node names - - # Called right after the scene is imported and gets the root node + # This sample changes all node names. + # Called right after the scene is imported and gets the root node. func post_import(scene): # Change all node names to "modified_[oldnodename]" iterate(scene) return scene # Remember to return the imported scene - func iterate(node): if node != null: node.name = "modified_" + node.name for child in node.get_children(): iterate(child) - [/codeblock] + [/gdscript] + [csharp] + using Godot; + + // This sample changes all node names. + // Called right after the scene is imported and gets the root node. + [Tool] + public class NodeRenamer : EditorScenePostImport + { + public override Object PostImport(Object scene) + { + // Change all node names to "modified_[oldnodename]" + Iterate(scene as Node); + return scene; // Remember to return the imported scene + } + public void Iterate(Node node) + { + if (node != null) + { + node.Name = "modified_" + node.Name; + foreach (Node child in node.GetChildren()) + { + Iterate(child); + } + } + } + } + [/csharp] + [/codeblocks] </description> <tutorials> <link title="Importing 3D scenes: Custom script">https://docs.godotengine.org/en/latest/getting_started/workflow/assets/importing_scenes.html#custom-script</link> diff --git a/doc/classes/EditorScript.xml b/doc/classes/EditorScript.xml index e96044bf48..60ccf451b8 100644 --- a/doc/classes/EditorScript.xml +++ b/doc/classes/EditorScript.xml @@ -7,13 +7,28 @@ Scripts extending this class and implementing its [method _run] method can be executed from the Script Editor's [b]File > Run[/b] menu option (or by pressing [kbd]Ctrl + Shift + X[/kbd]) while the editor is running. This is useful for adding custom in-editor functionality to Godot. For more complex additions, consider using [EditorPlugin]s instead. [b]Note:[/b] Extending scripts need to have [code]tool[/code] mode enabled. [b]Example script:[/b] - [codeblock] + [codeblocks] + [gdscript] tool extends EditorScript func _run(): print("Hello from the Godot Editor!") - [/codeblock] + [/gdscript] + [csharp] + using Godot; + using System; + + [Tool] + public class HelloEditor : EditorScript + { + public override void _Run() + { + GD.Print("Hello from the Godot Editor!"); + } + } + [/csharp] + [/codeblocks] [b]Note:[/b] The script is run in the Editor context, which means the output is visible in the console window started with the Editor (stdout) instead of the usual Godot [b]Output[/b] dock. </description> <tutorials> diff --git a/doc/classes/EditorSettings.xml b/doc/classes/EditorSettings.xml index 0c6a2d61cd..6088ae7a43 100644 --- a/doc/classes/EditorSettings.xml +++ b/doc/classes/EditorSettings.xml @@ -7,15 +7,24 @@ Object that holds the project-independent editor settings. These settings are generally visible in the [b]Editor > Editor Settings[/b] menu. Property names use slash delimiters to distinguish sections. Setting values can be of any [Variant] type. It's recommended to use [code]snake_case[/code] for editor settings to be consistent with the Godot editor itself. Accessing the settings can be done using the following methods, such as: - [codeblock] - # `settings.set("some/property", value)` also works as this class overrides `_set()` internally. - settings.set_setting("some/property",value) - - # `settings.get("some/property", value)` also works as this class overrides `_get()` internally. + [codeblocks] + [gdscript] + var settings = EditorInterface.get_editor_settings() + # `settings.set("some/property", 10)` also works as this class overrides `_set()` internally. + settings.set_setting("some/property", 10) + # `settings.get("some/property")` also works as this class overrides `_get()` internally. settings.get_setting("some/property") - var list_of_settings = settings.get_property_list() - [/codeblock] + [/gdscript] + [csharp] + EditorSettings settings = GetEditorInterface().GetEditorSettings(); + // `settings.set("some/property", value)` also works as this class overrides `_set()` internally. + settings.SetSetting("some/property", Value); + // `settings.get("some/property", value)` also works as this class overrides `_get()` internally. + settings.GetSetting("some/property"); + Godot.Collections.Array listOfSettings = settings.GetPropertyList(); + [/csharp] + [/codeblocks] [b]Note:[/b] This class shouldn't be instantiated directly. Instead, access the singleton using [method EditorInterface.get_editor_settings]. </description> <tutorials> @@ -32,8 +41,10 @@ - [code]type[/code]: [int] (see [enum Variant.Type]) - optionally [code]hint[/code]: [int] (see [enum PropertyHint]) and [code]hint_string[/code]: [String] [b]Example:[/b] - [codeblock] - editor_settings.set("category/property_name", 0) + [codeblocks] + [gdscript] + var settings = EditorInterface.get_editor_settings() + settings.set("category/property_name", 0) var property_info = { "name": "category/property_name", @@ -42,8 +53,23 @@ "hint_string": "one,two,three" } - editor_settings.add_property_info(property_info) - [/codeblock] + settings.add_property_info(property_info) + [/gdscript] + [csharp] + var settings = GetEditorInterface().GetEditorSettings(); + settings.Set("category/property_name", 0); + + var propertyInfo = new Godot.Collections.Dictionary + { + {"name", "category/propertyName"}, + {"type", Variant.Type.Int}, + {"hint", PropertyHint.Enum}, + {"hint_string", "one,two,three"} + }; + + settings.AddPropertyInfo(propertyInfo); + [/csharp] + [/codeblocks] </description> </method> <method name="erase"> diff --git a/doc/classes/EditorTranslationParserPlugin.xml b/doc/classes/EditorTranslationParserPlugin.xml index f5204e7bab..c97459d9dc 100644 --- a/doc/classes/EditorTranslationParserPlugin.xml +++ b/doc/classes/EditorTranslationParserPlugin.xml @@ -9,45 +9,95 @@ When adding to [code]msgids_context_plural[/code], you must add the data using the format [code]["A", "B", "C"][/code], where [code]A[/code] represents the extracted string, [code]B[/code] represents the context, and [code]C[/code] represents the plural version of the extracted string. If you want to add only context but not plural, put [code]""[/code] for the plural slot. The idea is the same if you only want to add plural but not context. See the code below for concrete examples. The extracted strings will be written into a POT file selected by user under "POT Generation" in "Localization" tab in "Project Settings" menu. Below shows an example of a custom parser that extracts strings from a CSV file to write into a POT. - [codeblock] + [codeblocks] + [gdscript] tool extends EditorTranslationParserPlugin - func parse_file(path, msgids, msgids_context_plural): var file = File.new() file.open(path, File.READ) var text = file.get_as_text() - var split_strs = text.split(",", false, 0) + var split_strs = text.split(",", false) for s in split_strs: msgids.append(s) #print("Extracted string: " + s) - func get_recognized_extensions(): return ["csv"] - [/codeblock] + [/gdscript] + [csharp] + using Godot; + using System; + + [Tool] + public class CustomParser : EditorTranslationParserPlugin + { + public override void ParseFile(string path, Godot.Collections.Array msgids, Godot.Collections.Array msgidsContextPlural) + { + var file = new File(); + file.Open(path, File.ModeFlags.Read); + string text = file.GetAsText(); + string[] splitStrs = text.Split(",", false); + foreach (var s in splitStrs) + { + msgids.Add(s); + //GD.Print("Extracted string: " + s) + } + } + + public override Godot.Collections.Array GetRecognizedExtensions() + { + return new Godot.Collections.Array{"csv"}; + } + } + [/csharp] + [/codeblocks] To add a translatable string associated with context or plural, add it to [code]msgids_context_plural[/code]: - [codeblock] + [codeblocks] + [gdscript] # This will add a message with msgid "Test 1", msgctxt "context", and msgid_plural "test 1 plurals". msgids_context_plural.append(["Test 1", "context", "test 1 plurals"]) # This will add a message with msgid "A test without context" and msgid_plural "plurals". msgids_context_plural.append(["A test without context", "", "plurals"]) # This will add a message with msgid "Only with context" and msgctxt "a friendly context". msgids_context_plural.append(["Only with context", "a friendly context", ""]) - [/codeblock] + [/gdscript] + [csharp] + // This will add a message with msgid "Test 1", msgctxt "context", and msgid_plural "test 1 plurals". + msgidsContextPlural.Add(new Godot.Collections.Array{"Test 1", "context", "test 1 Plurals"}); + // This will add a message with msgid "A test without context" and msgid_plural "plurals". + msgidsContextPlural.Add(new Godot.Collections.Array{"A test without context", "", "plurals"}); + // This will add a message with msgid "Only with context" and msgctxt "a friendly context". + msgidsContextPlural.Add(new Godot.Collections.Array{"Only with context", "a friendly context", ""}); + [/csharp] + [/codeblocks] [b]Note:[/b] If you override parsing logic for standard script types (GDScript, C#, etc.), it would be better to load the [code]path[/code] argument using [method ResourceLoader.load]. This is because built-in scripts are loaded as [Resource] type, not [File] type. For example: - [codeblock] + [codeblocks] + [gdscript] func parse_file(path, msgids, msgids_context_plural): var res = ResourceLoader.load(path, "Script") - var text = res.get_source_code() + var text = res.source_code # Parsing logic. - func get_recognized_extensions(): return ["gd"] - [/codeblock] + [/gdscript] + [csharp] + public override void ParseFile(string path, Godot.Collections.Array msgids, Godot.Collections.Array msgidsContextPlural) + { + var res = ResourceLoader.Load<Script>(path, "Script"); + string text = res.SourceCode; + // Parsing logic. + } + + public override Godot.Collections.Array GetRecognizedExtensions() + { + return new Godot.Collections.Array{"gd"}; + } + [/csharp] + [/codeblocks] </description> <tutorials> </tutorials> diff --git a/doc/classes/Engine.xml b/doc/classes/Engine.xml index 297cccaaac..fab8512e4a 100644 --- a/doc/classes/Engine.xml +++ b/doc/classes/Engine.xml @@ -117,12 +117,24 @@ [code]year[/code] - Holds the year the version was released in as an int [code]string[/code] - [code]major[/code] + [code]minor[/code] + [code]patch[/code] + [code]status[/code] + [code]build[/code] in a single String The [code]hex[/code] value is encoded as follows, from left to right: one byte for the major, one byte for the minor, one byte for the patch version. For example, "3.1.12" would be [code]0x03010C[/code]. [b]Note:[/b] It's still an int internally, and printing it will give you its decimal representation, which is not particularly meaningful. Use hexadecimal literals for easy version comparisons from code: - [codeblock] + [codeblocks] + [gdscript] if Engine.get_version_info().hex >= 0x030200: # Do things specific to version 3.2 or later else: # Do things specific to versions before 3.2 - [/codeblock] + [/gdscript] + [csharp] + if ((int)Engine.GetVersionInfo()["hex"] >= 0x030200) + { + // Do things specific to version 3.2 or later + } + else + { + // Do things specific to versions before 3.2 + } + [/csharp] + [/codeblocks] </description> </method> <method name="has_singleton" qualifiers="const"> diff --git a/doc/classes/Expression.xml b/doc/classes/Expression.xml index f2611dc850..d777c6fd9d 100644 --- a/doc/classes/Expression.xml +++ b/doc/classes/Expression.xml @@ -7,21 +7,46 @@ An expression can be made of any arithmetic operation, built-in math function call, method call of a passed instance, or built-in type construction call. An example expression text using the built-in math functions could be [code]sqrt(pow(3, 2) + pow(4, 2))[/code]. In the following example we use a [LineEdit] node to write our expression and show the result. - [codeblock] - onready var expression = Expression.new() + [codeblocks] + [gdscript] + var expression = Expression.new() func _ready(): $LineEdit.connect("text_entered", self, "_on_text_entered") func _on_text_entered(command): - var error = expression.parse(command, []) + var error = expression.parse(command) if error != OK: print(expression.get_error_text()) return - var result = expression.execute([], null, true) + var result = expression.execute() if not expression.has_execute_failed(): $LineEdit.text = str(result) - [/codeblock] + [/gdscript] + [csharp] + public Expression expression = new Expression(); + + public override void _Ready() + { + GetNode("LineEdit").Connect("text_entered", this, nameof(OnTextEntered)); + } + + private void OnTextEntered(string command) + { + Error error = expression.Parse(command); + if (error != Error.Ok) + { + GD.Print(expression.GetErrorText()); + return; + } + object result = expression.Execute(); + if (!expression.HasExecuteFailed()) + { + GetNode<LineEdit>("LineEdit").Text = result.ToString(); + } + } + [/csharp] + [/codeblocks] </description> <tutorials> </tutorials> diff --git a/doc/classes/FuncRef.xml b/doc/classes/FuncRef.xml deleted file mode 100644 index dc9246ad35..0000000000 --- a/doc/classes/FuncRef.xml +++ /dev/null @@ -1,53 +0,0 @@ -<?xml version="1.0" encoding="UTF-8" ?> -<class name="FuncRef" inherits="Reference" version="4.0"> - <brief_description> - Reference to a function in an object. - </brief_description> - <description> - In GDScript, functions are not [i]first-class objects[/i]. This means it is impossible to store them directly as variables, return them from another function, or pass them as arguments. - However, by creating a [FuncRef] using the [method @GDScript.funcref] function, a reference to a function in a given object can be created, passed around and called. - </description> - <tutorials> - </tutorials> - <methods> - <method name="call_func" qualifiers="vararg"> - <return type="Variant"> - </return> - <description> - Calls the referenced function previously set in [member function] or [method @GDScript.funcref]. - </description> - </method> - <method name="call_funcv"> - <return type="Variant"> - </return> - <argument index="0" name="arg_array" type="Array"> - </argument> - <description> - Calls the referenced function previously set in [member function] or [method @GDScript.funcref]. Contrarily to [method call_func], this method does not support a variable number of arguments but expects all parameters to be passed via a single [Array]. - </description> - </method> - <method name="is_valid" qualifiers="const"> - <return type="bool"> - </return> - <description> - Returns whether the object still exists and has the function assigned. - </description> - </method> - <method name="set_instance"> - <return type="void"> - </return> - <argument index="0" name="instance" type="Object"> - </argument> - <description> - The object containing the referenced function. This object must be of a type actually inheriting from [Object], not a built-in type such as [int], [Vector2] or [Dictionary]. - </description> - </method> - </methods> - <members> - <member name="function" type="StringName" setter="set_function" getter="get_function" default="@"""> - The name of the referenced function. - </member> - </members> - <constants> - </constants> -</class> diff --git a/doc/classes/InstancePlaceholder.xml b/doc/classes/InstancePlaceholder.xml index 39827f6604..defd23afb1 100644 --- a/doc/classes/InstancePlaceholder.xml +++ b/doc/classes/InstancePlaceholder.xml @@ -18,13 +18,14 @@ <argument index="1" name="custom_scene" type="PackedScene" default="null"> </argument> <description> + Not thread-safe. Use [method Object.call_deferred] if calling from a thread. </description> </method> <method name="get_instance_path" qualifiers="const"> <return type="String"> </return> <description> - Gets the path to the [PackedScene] resource file that is loaded by default when calling [method create_instance]. + Gets the path to the [PackedScene] resource file that is loaded by default when calling [method create_instance]. Not thread-safe. Use [method Object.call_deferred] if calling from a thread. </description> </method> <method name="get_stored_values"> diff --git a/doc/classes/LineEdit.xml b/doc/classes/LineEdit.xml index f08a15d873..5c2dffd538 100644 --- a/doc/classes/LineEdit.xml +++ b/doc/classes/LineEdit.xml @@ -78,6 +78,13 @@ Returns the [PopupMenu] of this [LineEdit]. By default, this menu is displayed when right-clicking on the [LineEdit]. </description> </method> + <method name="get_scroll_offset" qualifiers="const"> + <return type="int"> + </return> + <description> + Returns the scroll offset due to [member caret_position], as a number of characters. + </description> + </method> <method name="menu_option"> <return type="void"> </return> diff --git a/doc/classes/MeshDataTool.xml b/doc/classes/MeshDataTool.xml index dcc3bbf2a6..e107b1a108 100644 --- a/doc/classes/MeshDataTool.xml +++ b/doc/classes/MeshDataTool.xml @@ -7,16 +7,44 @@ MeshDataTool provides access to individual vertices in a [Mesh]. It allows users to read and edit vertex data of meshes. It also creates an array of faces and edges. To use MeshDataTool, load a mesh with [method create_from_surface]. When you are finished editing the data commit the data to a mesh with [method commit_to_surface]. Below is an example of how MeshDataTool may be used. - [codeblock] + [codeblocks] + [gdscript] + var mesh = ArrayMesh.new() + mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, CubeMesh.new().get_mesh_arrays()) var mdt = MeshDataTool.new() mdt.create_from_surface(mesh, 0) for i in range(mdt.get_vertex_count()): var vertex = mdt.get_vertex(i) - ... + # In this example we extend the mesh by one unit, which results in seperated faces as it is flat shaded. + vertex += mdt.get_vertex_normal(i) + # Save your change. mdt.set_vertex(i, vertex) mesh.surface_remove(0) mdt.commit_to_surface(mesh) - [/codeblock] + var mi = MeshInstance.new() + mi.mesh = mesh + add_child(mi) + [/gdscript] + [csharp] + var mesh = new ArrayMesh(); + mesh.AddSurfaceFromArrays(Mesh.PrimitiveType.Triangles, new CubeMesh().GetMeshArrays()); + var mdt = new MeshDataTool(); + mdt.CreateFromSurface(mesh, 0); + for (var i = 0; i < mdt.GetVertexCount(); i++) + { + Vector3 vertex = mdt.GetVertex(i); + // In this example we extend the mesh by one unit, which results in seperated faces as it is flat shaded. + vertex += mdt.GetVertexNormal(i); + // Save your change. + mdt.SetVertex(i, vertex); + } + mesh.SurfaceRemove(0); + mdt.CommitToSurface(mesh); + var mi = new MeshInstance(); + mi.Mesh = mesh; + AddChild(mi); + [/csharp] + [/codeblocks] See also [ArrayMesh], [ImmediateGeometry3D] and [SurfaceTool] for procedural geometry generation. [b]Note:[/b] Godot uses clockwise [url=https://learnopengl.com/Advanced-OpenGL/Face-culling]winding order[/url] for front faces of triangle primitive modes. </description> diff --git a/doc/classes/NodePath.xml b/doc/classes/NodePath.xml index f711ba4d6b..93ede047fd 100644 --- a/doc/classes/NodePath.xml +++ b/doc/classes/NodePath.xml @@ -25,7 +25,23 @@ <link title="2D Role Playing Game Demo">https://godotengine.org/asset-library/asset/520</link> </tutorials> <methods> - <method name="NodePath"> + <method name="NodePath" qualifiers="constructor"> + <return type="NodePath"> + </return> + <description> + Constructs an empty [NodePath]. + </description> + </method> + <method name="NodePath" qualifiers="constructor"> + <return type="NodePath"> + </return> + <argument index="0" name="from" type="NodePath"> + </argument> + <description> + Constructs a [NodePath] as a copy of the given [NodePath]. + </description> + </method> + <method name="NodePath" qualifiers="constructor"> <return type="NodePath"> </return> <argument index="0" name="from" type="String"> @@ -134,6 +150,22 @@ Returns [code]true[/code] if the node path is empty. </description> </method> + <method name="operator !=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="NodePath"> + </argument> + <description> + </description> + </method> + <method name="operator ==" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="NodePath"> + </argument> + <description> + </description> + </method> </methods> <constants> </constants> diff --git a/doc/classes/PackedByteArray.xml b/doc/classes/PackedByteArray.xml index 7c2d566466..4a6893879d 100644 --- a/doc/classes/PackedByteArray.xml +++ b/doc/classes/PackedByteArray.xml @@ -10,7 +10,23 @@ <tutorials> </tutorials> <methods> - <method name="PackedByteArray"> + <method name="PackedByteArray" qualifiers="constructor"> + <return type="PackedByteArray"> + </return> + <description> + Constructs an empty [PackedByteArray]. + </description> + </method> + <method name="PackedByteArray" qualifiers="constructor"> + <return type="PackedByteArray"> + </return> + <argument index="0" name="from" type="PackedByteArray"> + </argument> + <description> + Constructs a [PackedByteArray] as a copy of the given [PackedByteArray]. + </description> + </method> + <method name="PackedByteArray" qualifiers="constructor"> <return type="PackedByteArray"> </return> <argument index="0" name="from" type="Array"> @@ -143,6 +159,38 @@ Reverses the order of the elements in the array. </description> </method> + <method name="operator !=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="PackedByteArray"> + </argument> + <description> + </description> + </method> + <method name="operator +" qualifiers="operator"> + <return type="PackedByteArray"> + </return> + <argument index="0" name="right" type="PackedByteArray"> + </argument> + <description> + </description> + </method> + <method name="operator ==" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="PackedByteArray"> + </argument> + <description> + </description> + </method> + <method name="operator []" qualifiers="operator"> + <return type="int"> + </return> + <argument index="0" name="index" type="int"> + </argument> + <description> + </description> + </method> <method name="push_back"> <return type="bool"> </return> diff --git a/doc/classes/PackedColorArray.xml b/doc/classes/PackedColorArray.xml index c42d14c5bd..3065d16945 100644 --- a/doc/classes/PackedColorArray.xml +++ b/doc/classes/PackedColorArray.xml @@ -10,7 +10,23 @@ <tutorials> </tutorials> <methods> - <method name="PackedColorArray"> + <method name="PackedColorArray" qualifiers="constructor"> + <return type="PackedColorArray"> + </return> + <description> + Constructs an empty [PackedColorArray]. + </description> + </method> + <method name="PackedColorArray" qualifiers="constructor"> + <return type="PackedColorArray"> + </return> + <argument index="0" name="from" type="PackedColorArray"> + </argument> + <description> + Constructs a [PackedColorArray] as a copy of the given [PackedColorArray]. + </description> + </method> + <method name="PackedColorArray" qualifiers="constructor"> <return type="PackedColorArray"> </return> <argument index="0" name="from" type="Array"> @@ -71,6 +87,38 @@ Reverses the order of the elements in the array. </description> </method> + <method name="operator !=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="PackedColorArray"> + </argument> + <description> + </description> + </method> + <method name="operator +" qualifiers="operator"> + <return type="PackedColorArray"> + </return> + <argument index="0" name="right" type="PackedColorArray"> + </argument> + <description> + </description> + </method> + <method name="operator ==" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="PackedColorArray"> + </argument> + <description> + </description> + </method> + <method name="operator []" qualifiers="operator"> + <return type="Color"> + </return> + <argument index="0" name="index" type="int"> + </argument> + <description> + </description> + </method> <method name="push_back"> <return type="bool"> </return> diff --git a/doc/classes/PackedFloat32Array.xml b/doc/classes/PackedFloat32Array.xml index dd84648251..ab9594d2e3 100644 --- a/doc/classes/PackedFloat32Array.xml +++ b/doc/classes/PackedFloat32Array.xml @@ -11,7 +11,23 @@ <tutorials> </tutorials> <methods> - <method name="PackedFloat32Array"> + <method name="PackedFloat32Array" qualifiers="constructor"> + <return type="PackedFloat32Array"> + </return> + <description> + Constructs an empty [PackedFloat32Array]. + </description> + </method> + <method name="PackedFloat32Array" qualifiers="constructor"> + <return type="PackedFloat32Array"> + </return> + <argument index="0" name="from" type="PackedFloat32Array"> + </argument> + <description> + Constructs a [PackedFloat32Array] as a copy of the given [PackedFloat32Array]. + </description> + </method> + <method name="PackedFloat32Array" qualifiers="constructor"> <return type="PackedFloat32Array"> </return> <argument index="0" name="from" type="Array"> @@ -72,6 +88,30 @@ Reverses the order of the elements in the array. </description> </method> + <method name="operator !=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="PackedFloat32Array"> + </argument> + <description> + </description> + </method> + <method name="operator +" qualifiers="operator"> + <return type="PackedFloat32Array"> + </return> + <argument index="0" name="right" type="PackedFloat32Array"> + </argument> + <description> + </description> + </method> + <method name="operator ==" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="PackedFloat32Array"> + </argument> + <description> + </description> + </method> <method name="push_back"> <return type="bool"> </return> diff --git a/doc/classes/PackedFloat64Array.xml b/doc/classes/PackedFloat64Array.xml index 91c3f4874b..3088aee483 100644 --- a/doc/classes/PackedFloat64Array.xml +++ b/doc/classes/PackedFloat64Array.xml @@ -11,7 +11,23 @@ <tutorials> </tutorials> <methods> - <method name="PackedFloat64Array"> + <method name="PackedFloat64Array" qualifiers="constructor"> + <return type="PackedFloat64Array"> + </return> + <description> + Constructs an empty [PackedFloat64Array]. + </description> + </method> + <method name="PackedFloat64Array" qualifiers="constructor"> + <return type="PackedFloat64Array"> + </return> + <argument index="0" name="from" type="PackedFloat64Array"> + </argument> + <description> + Constructs a [PackedFloat64Array] as a copy of the given [PackedFloat64Array]. + </description> + </method> + <method name="PackedFloat64Array" qualifiers="constructor"> <return type="PackedFloat64Array"> </return> <argument index="0" name="from" type="Array"> @@ -72,6 +88,38 @@ Reverses the order of the elements in the array. </description> </method> + <method name="operator !=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="PackedFloat64Array"> + </argument> + <description> + </description> + </method> + <method name="operator +" qualifiers="operator"> + <return type="PackedFloat64Array"> + </return> + <argument index="0" name="right" type="PackedFloat64Array"> + </argument> + <description> + </description> + </method> + <method name="operator ==" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="PackedFloat64Array"> + </argument> + <description> + </description> + </method> + <method name="operator []" qualifiers="operator"> + <return type="float"> + </return> + <argument index="0" name="index" type="int"> + </argument> + <description> + </description> + </method> <method name="push_back"> <return type="bool"> </return> diff --git a/doc/classes/PackedInt32Array.xml b/doc/classes/PackedInt32Array.xml index a0a9922d0c..eded545de8 100644 --- a/doc/classes/PackedInt32Array.xml +++ b/doc/classes/PackedInt32Array.xml @@ -11,7 +11,23 @@ <tutorials> </tutorials> <methods> - <method name="PackedInt32Array"> + <method name="PackedInt32Array" qualifiers="constructor"> + <return type="PackedInt32Array"> + </return> + <description> + Constructs an empty [PackedInt32Array]. + </description> + </method> + <method name="PackedInt32Array" qualifiers="constructor"> + <return type="PackedInt32Array"> + </return> + <argument index="0" name="from" type="PackedInt32Array"> + </argument> + <description> + Constructs a [PackedInt32Array] as a copy of the given [PackedInt32Array]. + </description> + </method> + <method name="PackedInt32Array" qualifiers="constructor"> <return type="PackedInt32Array"> </return> <argument index="0" name="from" type="Array"> @@ -72,6 +88,38 @@ Reverses the order of the elements in the array. </description> </method> + <method name="operator !=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="PackedInt32Array"> + </argument> + <description> + </description> + </method> + <method name="operator +" qualifiers="operator"> + <return type="PackedInt32Array"> + </return> + <argument index="0" name="right" type="PackedInt32Array"> + </argument> + <description> + </description> + </method> + <method name="operator ==" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="PackedInt32Array"> + </argument> + <description> + </description> + </method> + <method name="operator []" qualifiers="operator"> + <return type="int"> + </return> + <argument index="0" name="index" type="int"> + </argument> + <description> + </description> + </method> <method name="push_back"> <return type="bool"> </return> diff --git a/doc/classes/PackedInt64Array.xml b/doc/classes/PackedInt64Array.xml index 542dabcfa0..83731b1023 100644 --- a/doc/classes/PackedInt64Array.xml +++ b/doc/classes/PackedInt64Array.xml @@ -11,7 +11,23 @@ <tutorials> </tutorials> <methods> - <method name="PackedInt64Array"> + <method name="PackedInt64Array" qualifiers="constructor"> + <return type="PackedInt64Array"> + </return> + <description> + Constructs an empty [PackedInt64Array]. + </description> + </method> + <method name="PackedInt64Array" qualifiers="constructor"> + <return type="PackedInt64Array"> + </return> + <argument index="0" name="from" type="PackedInt64Array"> + </argument> + <description> + Constructs a [PackedInt64Array] as a copy of the given [PackedInt64Array]. + </description> + </method> + <method name="PackedInt64Array" qualifiers="constructor"> <return type="PackedInt64Array"> </return> <argument index="0" name="from" type="Array"> @@ -72,6 +88,38 @@ Reverses the order of the elements in the array. </description> </method> + <method name="operator !=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="PackedInt64Array"> + </argument> + <description> + </description> + </method> + <method name="operator +" qualifiers="operator"> + <return type="PackedInt64Array"> + </return> + <argument index="0" name="right" type="PackedInt64Array"> + </argument> + <description> + </description> + </method> + <method name="operator ==" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="PackedInt64Array"> + </argument> + <description> + </description> + </method> + <method name="operator []" qualifiers="operator"> + <return type="int"> + </return> + <argument index="0" name="index" type="int"> + </argument> + <description> + </description> + </method> <method name="push_back"> <return type="bool"> </return> diff --git a/doc/classes/PackedStringArray.xml b/doc/classes/PackedStringArray.xml index 7c710cf0fb..c71f5ffa7e 100644 --- a/doc/classes/PackedStringArray.xml +++ b/doc/classes/PackedStringArray.xml @@ -11,7 +11,23 @@ <link title="OS Test Demo">https://godotengine.org/asset-library/asset/677</link> </tutorials> <methods> - <method name="PackedStringArray"> + <method name="PackedStringArray" qualifiers="constructor"> + <return type="PackedStringArray"> + </return> + <description> + Constructs an empty [PackedStringArray]. + </description> + </method> + <method name="PackedStringArray" qualifiers="constructor"> + <return type="PackedStringArray"> + </return> + <argument index="0" name="from" type="PackedStringArray"> + </argument> + <description> + Constructs a [PackedStringArray] as a copy of the given [PackedStringArray]. + </description> + </method> + <method name="PackedStringArray" qualifiers="constructor"> <return type="PackedStringArray"> </return> <argument index="0" name="from" type="Array"> @@ -72,6 +88,38 @@ Reverses the order of the elements in the array. </description> </method> + <method name="operator !=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="PackedStringArray"> + </argument> + <description> + </description> + </method> + <method name="operator +" qualifiers="operator"> + <return type="PackedStringArray"> + </return> + <argument index="0" name="right" type="PackedStringArray"> + </argument> + <description> + </description> + </method> + <method name="operator ==" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="PackedStringArray"> + </argument> + <description> + </description> + </method> + <method name="operator []" qualifiers="operator"> + <return type="String"> + </return> + <argument index="0" name="index" type="int"> + </argument> + <description> + </description> + </method> <method name="push_back"> <return type="bool"> </return> diff --git a/doc/classes/PackedVector2Array.xml b/doc/classes/PackedVector2Array.xml index 98034afc93..5f68d9256d 100644 --- a/doc/classes/PackedVector2Array.xml +++ b/doc/classes/PackedVector2Array.xml @@ -11,7 +11,23 @@ <link title="2D Navigation Astar Demo">https://godotengine.org/asset-library/asset/519</link> </tutorials> <methods> - <method name="PackedVector2Array"> + <method name="PackedVector2Array" qualifiers="constructor"> + <return type="PackedVector2Array"> + </return> + <description> + Constructs an empty [PackedVector2Array]. + </description> + </method> + <method name="PackedVector2Array" qualifiers="constructor"> + <return type="PackedVector2Array"> + </return> + <argument index="0" name="from" type="PackedVector2Array"> + </argument> + <description> + Constructs a [PackedVector2Array] as a copy of the given [PackedVector2Array]. + </description> + </method> + <method name="PackedVector2Array" qualifiers="constructor"> <return type="PackedVector2Array"> </return> <argument index="0" name="from" type="Array"> @@ -72,6 +88,46 @@ Reverses the order of the elements in the array. </description> </method> + <method name="operator !=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="PackedVector2Array"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="PackedVector2Array"> + </return> + <argument index="0" name="right" type="Transform2D"> + </argument> + <description> + </description> + </method> + <method name="operator +" qualifiers="operator"> + <return type="PackedVector2Array"> + </return> + <argument index="0" name="right" type="PackedVector2Array"> + </argument> + <description> + </description> + </method> + <method name="operator ==" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="PackedVector2Array"> + </argument> + <description> + </description> + </method> + <method name="operator []" qualifiers="operator"> + <return type="Vector2"> + </return> + <argument index="0" name="index" type="int"> + </argument> + <description> + </description> + </method> <method name="push_back"> <return type="bool"> </return> diff --git a/doc/classes/PackedVector3Array.xml b/doc/classes/PackedVector3Array.xml index 3db33fbcd9..e681e1deb7 100644 --- a/doc/classes/PackedVector3Array.xml +++ b/doc/classes/PackedVector3Array.xml @@ -10,7 +10,23 @@ <tutorials> </tutorials> <methods> - <method name="PackedVector3Array"> + <method name="PackedVector3Array" qualifiers="constructor"> + <return type="PackedVector3Array"> + </return> + <description> + Constructs an empty [PackedVector3Array]. + </description> + </method> + <method name="PackedVector3Array" qualifiers="constructor"> + <return type="PackedVector3Array"> + </return> + <argument index="0" name="from" type="PackedVector3Array"> + </argument> + <description> + Constructs a [PackedVector3Array] as a copy of the given [PackedVector3Array]. + </description> + </method> + <method name="PackedVector3Array" qualifiers="constructor"> <return type="PackedVector3Array"> </return> <argument index="0" name="from" type="Array"> @@ -71,6 +87,46 @@ Reverses the order of the elements in the array. </description> </method> + <method name="operator !=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="PackedVector3Array"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="PackedVector3Array"> + </return> + <argument index="0" name="right" type="Transform"> + </argument> + <description> + </description> + </method> + <method name="operator +" qualifiers="operator"> + <return type="PackedVector3Array"> + </return> + <argument index="0" name="right" type="PackedVector3Array"> + </argument> + <description> + </description> + </method> + <method name="operator ==" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="PackedVector3Array"> + </argument> + <description> + </description> + </method> + <method name="operator []" qualifiers="operator"> + <return type="Vector3"> + </return> + <argument index="0" name="index" type="int"> + </argument> + <description> + </description> + </method> <method name="push_back"> <return type="bool"> </return> diff --git a/doc/classes/Plane.xml b/doc/classes/Plane.xml index 9352eee1eb..e3242512c4 100644 --- a/doc/classes/Plane.xml +++ b/doc/classes/Plane.xml @@ -10,7 +10,23 @@ <link title="Math tutorial index">https://docs.godotengine.org/en/latest/tutorials/math/index.html</link> </tutorials> <methods> - <method name="Plane"> + <method name="Plane" qualifiers="constructor"> + <return type="Plane"> + </return> + <description> + Constructs a default-initialized [Plane] with all components set to [code]0[/code]. + </description> + </method> + <method name="Plane" qualifiers="constructor"> + <return type="Plane"> + </return> + <argument index="0" name="from" type="Plane"> + </argument> + <description> + Constructs a [Plane] as a copy of the given [Plane]. + </description> + </method> + <method name="Plane" qualifiers="constructor"> <return type="Plane"> </return> <argument index="0" name="a" type="float"> @@ -25,28 +41,39 @@ Creates a plane from the four parameters. The three components of the resulting plane's [member normal] are [code]a[/code], [code]b[/code] and [code]c[/code], and the plane has a distance of [code]d[/code] from the origin. </description> </method> - <method name="Plane"> + <method name="Plane" qualifiers="constructor"> <return type="Plane"> </return> - <argument index="0" name="v1" type="Vector3"> + <argument index="0" name="normal" type="Vector3"> </argument> - <argument index="1" name="v2" type="Vector3"> + <argument index="1" name="d" type="float"> </argument> - <argument index="2" name="v3" type="Vector3"> + <description> + Creates a plane from the normal and the plane's distance to the origin. + </description> + </method> + <method name="Plane" qualifiers="constructor"> + <return type="Plane"> + </return> + <argument index="0" name="point" type="Vector3"> + </argument> + <argument index="1" name="normal" type="Vector3"> </argument> <description> - Creates a plane from the three points, given in clockwise order. + Creates a plane from the given position and a plane normal. </description> </method> - <method name="Plane"> + <method name="Plane" qualifiers="constructor"> <return type="Plane"> </return> - <argument index="0" name="normal" type="Vector3"> + <argument index="0" name="point1" type="Vector3"> </argument> - <argument index="1" name="d" type="float"> + <argument index="1" name="point2" type="Vector3"> + </argument> + <argument index="2" name="point3" type="Vector3"> </argument> <description> - Creates a plane from the normal and the plane's distance to the origin. + Creates a plane from the three points, given in clockwise order. </description> </method> <method name="center"> @@ -134,6 +161,34 @@ Returns a copy of the plane, normalized. </description> </method> + <method name="operator !=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Plane"> + </argument> + <description> + </description> + </method> + <method name="operator +" qualifiers="operator"> + <return type="Plane"> + </return> + <description> + </description> + </method> + <method name="operator -" qualifiers="operator"> + <return type="Plane"> + </return> + <description> + </description> + </method> + <method name="operator ==" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Plane"> + </argument> + <description> + </description> + </method> <method name="project"> <return type="Vector3"> </return> diff --git a/doc/classes/Quat.xml b/doc/classes/Quat.xml index 76cfa0d99d..5932a624f2 100644 --- a/doc/classes/Quat.xml +++ b/doc/classes/Quat.xml @@ -13,22 +13,33 @@ <link title="Third Person Shooter Demo">https://godotengine.org/asset-library/asset/678</link> </tutorials> <methods> - <method name="Quat"> + <method name="Quat" qualifiers="constructor"> <return type="Quat"> </return> - <argument index="0" name="x" type="float"> - </argument> - <argument index="1" name="y" type="float"> + <description> + Constructs a default-initialized quaternion with all components set to [code]0[/code]. + </description> + </method> + <method name="Quat" qualifiers="constructor"> + <return type="Quat"> + </return> + <argument index="0" name="from" type="Quat"> </argument> - <argument index="2" name="z" type="float"> + <description> + Constructs a [Quat] as a copy of the given [Quat]. + </description> + </method> + <method name="Quat" qualifiers="constructor"> + <return type="Quat"> + </return> + <argument index="0" name="arc_from" type="Vector3"> </argument> - <argument index="3" name="w" type="float"> + <argument index="1" name="arc_to" type="Vector3"> </argument> <description> - Constructs a quaternion defined by the given values. </description> </method> - <method name="Quat"> + <method name="Quat" qualifiers="constructor"> <return type="Quat"> </return> <argument index="0" name="axis" type="Vector3"> @@ -39,7 +50,7 @@ Constructs a quaternion that will rotate around the given axis by the specified angle. The axis must be a normalized vector. </description> </method> - <method name="Quat"> + <method name="Quat" qualifiers="constructor"> <return type="Quat"> </return> <argument index="0" name="euler" type="Vector3"> @@ -48,7 +59,7 @@ Constructs a quaternion that will perform a rotation specified by Euler angles (in the YXZ convention: when decomposing, first Z, then X, and Y last), given in the vector format as (X angle, Y angle, Z angle). </description> </method> - <method name="Quat"> + <method name="Quat" qualifiers="constructor"> <return type="Quat"> </return> <argument index="0" name="from" type="Basis"> @@ -57,6 +68,21 @@ Constructs a quaternion from the given [Basis]. </description> </method> + <method name="Quat" qualifiers="constructor"> + <return type="Quat"> + </return> + <argument index="0" name="x" type="float"> + </argument> + <argument index="1" name="y" type="float"> + </argument> + <argument index="2" name="z" type="float"> + </argument> + <argument index="3" name="w" type="float"> + </argument> + <description> + Constructs a quaternion defined by the given values. + </description> + </method> <method name="cubic_slerp"> <return type="Quat"> </return> @@ -132,6 +158,98 @@ Returns a copy of the quaternion, normalized to unit length. </description> </method> + <method name="operator !=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Quat"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Quat"> + </return> + <argument index="0" name="right" type="Quat"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Vector3"> + </return> + <argument index="0" name="right" type="Vector3"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Quat"> + </return> + <argument index="0" name="right" type="float"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Quat"> + </return> + <argument index="0" name="right" type="int"> + </argument> + <description> + </description> + </method> + <method name="operator +" qualifiers="operator"> + <return type="Quat"> + </return> + <description> + </description> + </method> + <method name="operator +" qualifiers="operator"> + <return type="Quat"> + </return> + <argument index="0" name="right" type="Quat"> + </argument> + <description> + </description> + </method> + <method name="operator -" qualifiers="operator"> + <return type="Quat"> + </return> + <description> + </description> + </method> + <method name="operator /" qualifiers="operator"> + <return type="Quat"> + </return> + <argument index="0" name="right" type="float"> + </argument> + <description> + </description> + </method> + <method name="operator /" qualifiers="operator"> + <return type="Quat"> + </return> + <argument index="0" name="right" type="int"> + </argument> + <description> + </description> + </method> + <method name="operator ==" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Quat"> + </argument> + <description> + </description> + </method> + <method name="operator []" qualifiers="operator"> + <return type="float"> + </return> + <argument index="0" name="index" type="int"> + </argument> + <description> + </description> + </method> <method name="slerp"> <return type="Quat"> </return> diff --git a/doc/classes/RID.xml b/doc/classes/RID.xml index 644c427120..0ee34d4194 100644 --- a/doc/classes/RID.xml +++ b/doc/classes/RID.xml @@ -9,13 +9,20 @@ <tutorials> </tutorials> <methods> - <method name="RID"> + <method name="RID" qualifiers="constructor"> <return type="RID"> </return> - <argument index="0" name="from" type="Object"> + <description> + Constructs an empty [RID] with the invalid ID [code]0[/code]. + </description> + </method> + <method name="RID" qualifiers="constructor"> + <return type="RID"> + </return> + <argument index="0" name="from" type="RID"> </argument> <description> - Creates a new RID instance with the ID of a given resource. When not handed a valid resource, silently stores the unused ID 0. + Constructs a [RID] as a copy of the given [RID]. </description> </method> <method name="get_id"> @@ -25,6 +32,54 @@ Returns the ID of the referenced resource. </description> </method> + <method name="operator !=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="RID"> + </argument> + <description> + </description> + </method> + <method name="operator <" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="RID"> + </argument> + <description> + </description> + </method> + <method name="operator <=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="RID"> + </argument> + <description> + </description> + </method> + <method name="operator ==" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="RID"> + </argument> + <description> + </description> + </method> + <method name="operator >" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="RID"> + </argument> + <description> + </description> + </method> + <method name="operator >=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="RID"> + </argument> + <description> + </description> + </method> </methods> <constants> </constants> diff --git a/doc/classes/Rect2.xml b/doc/classes/Rect2.xml index a72ba35a98..02a77a0e24 100644 --- a/doc/classes/Rect2.xml +++ b/doc/classes/Rect2.xml @@ -14,7 +14,32 @@ <link title="Advanced vector math">https://docs.godotengine.org/en/latest/tutorials/math/vectors_advanced.html</link> </tutorials> <methods> - <method name="Rect2"> + <method name="Rect2" qualifiers="constructor"> + <return type="Rect2"> + </return> + <description> + Constructs a default-initialized [Rect2] with default (zero) values of [member position] and [member size]. + </description> + </method> + <method name="Rect2" qualifiers="constructor"> + <return type="Rect2"> + </return> + <argument index="0" name="from" type="Rect2"> + </argument> + <description> + Constructs a [Rect2] as a copy of the given [Rect2]. + </description> + </method> + <method name="Rect2" qualifiers="constructor"> + <return type="Rect2"> + </return> + <argument index="0" name="from" type="Rect2i"> + </argument> + <description> + Constructs a [Rect2] from a [Rect2i]. + </description> + </method> + <method name="Rect2" qualifiers="constructor"> <return type="Rect2"> </return> <argument index="0" name="position" type="Vector2"> @@ -25,7 +50,7 @@ Constructs a [Rect2] by position and size. </description> </method> - <method name="Rect2"> + <method name="Rect2" qualifiers="constructor"> <return type="Rect2"> </return> <argument index="0" name="x" type="float"> @@ -40,15 +65,6 @@ Constructs a [Rect2] by x, y, width, and height. </description> </method> - <method name="Rect2"> - <return type="Rect2"> - </return> - <argument index="0" name="from" type="Rect2i"> - </argument> - <description> - Constructs a [Rect2] from a [Rect2i]. - </description> - </method> <method name="abs"> <return type="Rect2"> </return> @@ -171,6 +187,30 @@ Returns a larger [Rect2] that contains this [Rect2] and [code]b[/code]. </description> </method> + <method name="operator !=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Rect2"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Rect2"> + </return> + <argument index="0" name="right" type="Transform2D"> + </argument> + <description> + </description> + </method> + <method name="operator ==" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Rect2"> + </argument> + <description> + </description> + </method> </methods> <members> <member name="end" type="Vector2" setter="" getter="" default="Vector2( 0, 0 )"> diff --git a/doc/classes/Rect2i.xml b/doc/classes/Rect2i.xml index de2932e816..e8b75a6ac6 100644 --- a/doc/classes/Rect2i.xml +++ b/doc/classes/Rect2i.xml @@ -12,7 +12,32 @@ <link title="Vector math">https://docs.godotengine.org/en/latest/tutorials/math/vector_math.html</link> </tutorials> <methods> - <method name="Rect2i"> + <method name="Rect2i" qualifiers="constructor"> + <return type="Rect2i"> + </return> + <description> + Constructs a default-initialized [Rect2i] with default (zero) values of [member position] and [member size]. + </description> + </method> + <method name="Rect2i" qualifiers="constructor"> + <return type="Rect2i"> + </return> + <argument index="0" name="from" type="Rect2i"> + </argument> + <description> + Constructs a [Rect2i] as a copy of the given [Rect2i]. + </description> + </method> + <method name="Rect2i" qualifiers="constructor"> + <return type="Rect2i"> + </return> + <argument index="0" name="from" type="Rect2"> + </argument> + <description> + Constructs a new [Rect2i] from [Rect2]. The floating point coordinates will be truncated. + </description> + </method> + <method name="Rect2i" qualifiers="constructor"> <return type="Rect2i"> </return> <argument index="0" name="position" type="Vector2i"> @@ -23,7 +48,7 @@ Constructs a [Rect2i] by position and size. </description> </method> - <method name="Rect2i"> + <method name="Rect2i" qualifiers="constructor"> <return type="Rect2i"> </return> <argument index="0" name="x" type="int"> @@ -38,15 +63,6 @@ Constructs a [Rect2i] by x, y, width, and height. </description> </method> - <method name="Rect2i"> - <return type="Rect2i"> - </return> - <argument index="0" name="from" type="Rect2"> - </argument> - <description> - Constructs a new [Rect2i] from [Rect2]. The floating point coordinates will be truncated. - </description> - </method> <method name="abs"> <return type="Rect2i"> </return> @@ -158,6 +174,22 @@ Returns a larger [Rect2i] that contains this [Rect2i] and [code]b[/code]. </description> </method> + <method name="operator !=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Rect2i"> + </argument> + <description> + </description> + </method> + <method name="operator ==" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Rect2i"> + </argument> + <description> + </description> + </method> </methods> <members> <member name="end" type="Vector2i" setter="" getter="" default="Vector2i( 0, 0 )"> diff --git a/doc/classes/Signal.xml b/doc/classes/Signal.xml index 51490caf6f..b7a2258fc1 100644 --- a/doc/classes/Signal.xml +++ b/doc/classes/Signal.xml @@ -8,15 +8,31 @@ <tutorials> </tutorials> <methods> - <method name="Signal"> + <method name="Signal" qualifiers="constructor"> + <return type="Signal"> + </return> + <description> + Constructs a null [Signal] with no object nor signal name bound. + </description> + </method> + <method name="Signal" qualifiers="constructor"> + <return type="Signal"> + </return> + <argument index="0" name="from" type="Signal"> + </argument> + <description> + Constructs a [Signal] as a copy of the given [Signal]. + </description> + </method> + <method name="Signal" qualifiers="constructor"> <return type="Signal"> </return> <argument index="0" name="object" type="Object"> </argument> - <argument index="1" name="signal_name" type="StringName"> + <argument index="1" name="signal" type="StringName"> </argument> <description> - Creates a new signal named [code]signal_name[/code] in the given object. + Creates a new [Signal] with the name [code]signal[/code] in the specified [code]object[/code]. </description> </method> <method name="connect"> @@ -91,6 +107,22 @@ <description> </description> </method> + <method name="operator !=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Signal"> + </argument> + <description> + </description> + </method> + <method name="operator ==" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Signal"> + </argument> + <description> + </description> + </method> </methods> <constants> </constants> diff --git a/doc/classes/String.xml b/doc/classes/String.xml index fcd8f57cd1..4ee9dbf1f9 100644 --- a/doc/classes/String.xml +++ b/doc/classes/String.xml @@ -10,160 +10,23 @@ <link title="GDScript format strings">https://docs.godotengine.org/en/latest/getting_started/scripting/gdscript/gdscript_format_string.html</link> </tutorials> <methods> - <method name="String"> + <method name="String" qualifiers="constructor"> <return type="String"> </return> - <argument index="0" name="from" type="bool"> - </argument> - <description> - Constructs a new String from the given [bool]. - </description> - </method> - <method name="String"> - <return type="String"> - </return> - <argument index="0" name="from" type="int"> - </argument> - <description> - Constructs a new String from the given [int]. - </description> - </method> - <method name="String"> - <return type="String"> - </return> - <argument index="0" name="from" type="float"> - </argument> - <description> - Constructs a new String from the given [float]. - </description> - </method> - <method name="String"> - <return type="String"> - </return> - <argument index="0" name="from" type="Vector2"> - </argument> <description> - Constructs a new String from the given [Vector2]. + Constructs an empty [String] ([code]""[/code]). </description> </method> - <method name="String"> + <method name="String" qualifiers="constructor"> <return type="String"> </return> - <argument index="0" name="from" type="Vector2i"> + <argument index="0" name="from" type="String"> </argument> <description> - Constructs a new String from the given [Vector2i]. + Constructs a [String] as a copy of the given [String]. </description> </method> - <method name="String"> - <return type="String"> - </return> - <argument index="0" name="from" type="Rect2"> - </argument> - <description> - Constructs a new String from the given [Rect2]. - </description> - </method> - <method name="String"> - <return type="String"> - </return> - <argument index="0" name="from" type="Rect2i"> - </argument> - <description> - Constructs a new String from the given [Rect2i]. - </description> - </method> - <method name="String"> - <return type="String"> - </return> - <argument index="0" name="from" type="Vector3"> - </argument> - <description> - Constructs a new String from the given [Vector3]. - </description> - </method> - <method name="String"> - <return type="String"> - </return> - <argument index="0" name="from" type="Vector3i"> - </argument> - <description> - Constructs a new String from the given [Vector3i]. - </description> - </method> - <method name="String"> - <return type="String"> - </return> - <argument index="0" name="from" type="Transform2D"> - </argument> - <description> - Constructs a new String from the given [Transform2D]. - </description> - </method> - <method name="String"> - <return type="String"> - </return> - <argument index="0" name="from" type="Plane"> - </argument> - <description> - Constructs a new String from the given [Plane]. - </description> - </method> - <method name="String"> - <return type="String"> - </return> - <argument index="0" name="from" type="Quat"> - </argument> - <description> - Constructs a new String from the given [Quat]. - </description> - </method> - <method name="String"> - <return type="String"> - </return> - <argument index="0" name="from" type="AABB"> - </argument> - <description> - Constructs a new String from the given [AABB]. - </description> - </method> - <method name="String"> - <return type="String"> - </return> - <argument index="0" name="from" type="Basis"> - </argument> - <description> - Constructs a new String from the given [Basis]. - </description> - </method> - <method name="String"> - <return type="String"> - </return> - <argument index="0" name="from" type="Transform"> - </argument> - <description> - Constructs a new String from the given [Transform]. - </description> - </method> - <method name="String"> - <return type="String"> - </return> - <argument index="0" name="from" type="Color"> - </argument> - <description> - Constructs a new String from the given [Color]. - </description> - </method> - <method name="String"> - <return type="String"> - </return> - <argument index="0" name="from" type="StringName"> - </argument> - <description> - Constructs a new String from the given [StringName]. - </description> - </method> - <method name="String"> + <method name="String" qualifiers="constructor"> <return type="String"> </return> <argument index="0" name="from" type="NodePath"> @@ -172,130 +35,13 @@ Constructs a new String from the given [NodePath]. </description> </method> - <method name="String"> - <return type="String"> - </return> - <argument index="0" name="from" type="RID"> - </argument> - <description> - Constructs a new String from the given [RID]. - </description> - </method> - <method name="String"> - <return type="String"> - </return> - <argument index="0" name="from" type="Callable"> - </argument> - <description> - Constructs a new String from the given [Callable]. - </description> - </method> - <method name="String"> - <return type="String"> - </return> - <argument index="0" name="from" type="Signal"> - </argument> - <description> - Constructs a new String from the given [Signal]. - </description> - </method> - <method name="String"> - <return type="String"> - </return> - <argument index="0" name="from" type="Dictionary"> - </argument> - <description> - Constructs a new String from the given [Dictionary]. - </description> - </method> - <method name="String"> - <return type="String"> - </return> - <argument index="0" name="from" type="Array"> - </argument> - <description> - Constructs a new String from the given [Array]. - </description> - </method> - <method name="String"> - <return type="String"> - </return> - <argument index="0" name="from" type="PackedByteArray"> - </argument> - <description> - Constructs a new String from the given [PackedByteArray]. - </description> - </method> - <method name="String"> - <return type="String"> - </return> - <argument index="0" name="from" type="PackedInt32Array"> - </argument> - <description> - Constructs a new String from the given [PackedInt32Array]. - </description> - </method> - <method name="String"> + <method name="String" qualifiers="constructor"> <return type="String"> </return> - <argument index="0" name="from" type="PackedInt64Array"> - </argument> - <description> - Constructs a new String from the given [PackedInt64Array]. - </description> - </method> - <method name="String"> - <return type="String"> - </return> - <argument index="0" name="from" type="PackedFloat32Array"> - </argument> - <description> - Constructs a new String from the given [PackedFloat32Array]. - </description> - </method> - <method name="String"> - <return type="String"> - </return> - <argument index="0" name="from" type="PackedFloat64Array"> - </argument> - <description> - Constructs a new String from the given [PackedFloat64Array]. - </description> - </method> - <method name="String"> - <return type="String"> - </return> - <argument index="0" name="from" type="PackedStringArray"> - </argument> - <description> - Constructs a new String from the given [PackedStringArray]. - </description> - </method> - <method name="String"> - <return type="String"> - </return> - <argument index="0" name="from" type="PackedVector2Array"> - </argument> - <description> - Constructs a new String from the given [PackedVector2Array]. - </description> - </method> - <method name="String"> - <return type="String"> - </return> - <argument index="0" name="from" type="PackedVector3Array"> - </argument> - <description> - Constructs a new String from the given [PackedVector3Array]. - </description> - </method> - <method name="String"> - <return type="String"> - </return> - <argument index="0" name="from" type="PackedColorArray"> + <argument index="0" name="from" type="StringName"> </argument> <description> - Constructs a new String from the given [PackedColorArray]. + Constructs a new String from the given [StringName]. </description> </method> <method name="begins_with"> @@ -720,6 +466,86 @@ To get a boolean result from a string comparison, use the [code]==[/code] operator instead. See also [method casecmp_to] and [method naturalnocasecmp_to]. </description> </method> + <method name="operator !=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="String"> + </argument> + <description> + </description> + </method> + <method name="operator !=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="StringName"> + </argument> + <description> + </description> + </method> + <method name="operator %" qualifiers="operator"> + <return type="String"> + </return> + <argument index="0" name="right" type="Variant"> + </argument> + <description> + </description> + </method> + <method name="operator +" qualifiers="operator"> + <return type="String"> + </return> + <argument index="0" name="right" type="String"> + </argument> + <description> + </description> + </method> + <method name="operator <" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="String"> + </argument> + <description> + </description> + </method> + <method name="operator <=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="String"> + </argument> + <description> + </description> + </method> + <method name="operator ==" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="String"> + </argument> + <description> + </description> + </method> + <method name="operator ==" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="StringName"> + </argument> + <description> + </description> + </method> + <method name="operator >" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="String"> + </argument> + <description> + </description> + </method> + <method name="operator >=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="String"> + </argument> + <description> + </description> + </method> <method name="ord_at"> <return type="int"> </return> diff --git a/doc/classes/StringName.xml b/doc/classes/StringName.xml index 5d8ac6fdcc..af0074f080 100644 --- a/doc/classes/StringName.xml +++ b/doc/classes/StringName.xml @@ -9,7 +9,23 @@ <tutorials> </tutorials> <methods> - <method name="StringName"> + <method name="StringName" qualifiers="constructor"> + <return type="StringName"> + </return> + <description> + Constructs an empty [StringName]. + </description> + </method> + <method name="StringName" qualifiers="constructor"> + <return type="StringName"> + </return> + <argument index="0" name="from" type="StringName"> + </argument> + <description> + Constructs a [StringName] as a copy of the given [StringName]. + </description> + </method> + <method name="StringName" qualifiers="constructor"> <return type="StringName"> </return> <argument index="0" name="from" type="String"> @@ -18,6 +34,38 @@ Creates a new [StringName] from the given [String]. </description> </method> + <method name="operator !=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="String"> + </argument> + <description> + </description> + </method> + <method name="operator !=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="StringName"> + </argument> + <description> + </description> + </method> + <method name="operator ==" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="String"> + </argument> + <description> + </description> + </method> + <method name="operator ==" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="StringName"> + </argument> + <description> + </description> + </method> </methods> <constants> </constants> diff --git a/doc/classes/Transform.xml b/doc/classes/Transform.xml index 8e539e64f7..cda69f6a64 100644 --- a/doc/classes/Transform.xml +++ b/doc/classes/Transform.xml @@ -16,57 +16,46 @@ <link title="2.5D Demo">https://godotengine.org/asset-library/asset/583</link> </tutorials> <methods> - <method name="Transform"> + <method name="Transform" qualifiers="constructor"> <return type="Transform"> </return> - <argument index="0" name="x_axis" type="Vector3"> - </argument> - <argument index="1" name="y_axis" type="Vector3"> - </argument> - <argument index="2" name="z_axis" type="Vector3"> - </argument> - <argument index="3" name="origin" type="Vector3"> - </argument> <description> - Constructs a Transform from four [Vector3] values (matrix columns). Each axis corresponds to local basis vectors (some of which may be scaled). + Constructs a default-initialized [Transform] set to [constant IDENTITY]. </description> </method> - <method name="Transform"> + <method name="Transform" qualifiers="constructor"> <return type="Transform"> </return> - <argument index="0" name="basis" type="Basis"> - </argument> - <argument index="1" name="origin" type="Vector3"> + <argument index="0" name="from" type="Transform"> </argument> <description> - Constructs a Transform from a [Basis] and [Vector3]. + Constructs a [Transform] as a copy of the given [Transform]. </description> </method> - <method name="Transform"> + <method name="Transform" qualifiers="constructor"> <return type="Transform"> </return> - <argument index="0" name="from" type="Transform2D"> + <argument index="0" name="basis" type="Basis"> </argument> - <description> - Constructs a Transform from a [Transform2D]. - </description> - </method> - <method name="Transform"> - <return type="Transform"> - </return> - <argument index="0" name="from" type="Quat"> + <argument index="1" name="origin" type="Vector3"> </argument> <description> - Constructs a Transform from a [Quat]. The origin will be [code]Vector3(0, 0, 0)[/code]. + Constructs a Transform from a [Basis] and [Vector3]. </description> </method> - <method name="Transform"> + <method name="Transform" qualifiers="constructor"> <return type="Transform"> </return> - <argument index="0" name="from" type="Basis"> + <argument index="0" name="x_axis" type="Vector3"> + </argument> + <argument index="1" name="y_axis" type="Vector3"> + </argument> + <argument index="2" name="z_axis" type="Vector3"> + </argument> + <argument index="3" name="origin" type="Vector3"> </argument> <description> - Constructs the Transform from a [Basis]. The origin will be Vector3(0, 0, 0). + Constructs a Transform from four [Vector3] values (matrix columns). Each axis corresponds to local basis vectors (some of which may be scaled). </description> </method> <method name="affine_inverse"> @@ -116,6 +105,54 @@ Operations take place in global space. </description> </method> + <method name="operator !=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Transform"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="PackedVector3Array"> + </return> + <argument index="0" name="right" type="PackedVector3Array"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Transform"> + </return> + <argument index="0" name="right" type="Transform"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="AABB"> + </return> + <argument index="0" name="right" type="AABB"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Vector3"> + </return> + <argument index="0" name="right" type="Vector3"> + </argument> + <description> + </description> + </method> + <method name="operator ==" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Transform"> + </argument> + <description> + </description> + </method> <method name="orthonormalized"> <return type="Transform"> </return> diff --git a/doc/classes/Transform2D.xml b/doc/classes/Transform2D.xml index 66adeab3a6..ff291663fa 100644 --- a/doc/classes/Transform2D.xml +++ b/doc/classes/Transform2D.xml @@ -14,7 +14,23 @@ <link title="2.5D Demo">https://godotengine.org/asset-library/asset/583</link> </tutorials> <methods> - <method name="Transform2D"> + <method name="Transform2D" qualifiers="constructor"> + <return type="Transform2D"> + </return> + <description> + Constructs a default-initialized [Transform] set to [constant IDENTITY]. + </description> + </method> + <method name="Transform2D" qualifiers="constructor"> + <return type="Transform2D"> + </return> + <argument index="0" name="from" type="Transform2D"> + </argument> + <description> + Constructs a [Transform2D] as a copy of the given [Transform2D]. + </description> + </method> + <method name="Transform2D" qualifiers="constructor"> <return type="Transform2D"> </return> <argument index="0" name="rotation" type="float"> @@ -25,7 +41,7 @@ Constructs the transform from a given angle (in radians) and position. </description> </method> - <method name="Transform2D"> + <method name="Transform2D" qualifiers="constructor"> <return type="Transform2D"> </return> <argument index="0" name="x_axis" type="Vector2"> @@ -38,15 +54,6 @@ Constructs the transform from 3 [Vector2] values representing [member x], [member y], and the [member origin] (the three column vectors). </description> </method> - <method name="Transform2D"> - <return type="Transform2D"> - </return> - <argument index="0" name="from" type="Transform"> - </argument> - <description> - Constructs the transform from a 3D [Transform]. - </description> - </method> <method name="affine_inverse"> <return type="Transform2D"> </return> @@ -122,6 +129,62 @@ Returns [code]true[/code] if this transform and [code]transform[/code] are approximately equal, by calling [code]is_equal_approx[/code] on each component. </description> </method> + <method name="operator !=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Transform2D"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Vector2"> + </return> + <argument index="0" name="right" type="Vector2"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Rect2"> + </return> + <argument index="0" name="right" type="Rect2"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Transform2D"> + </return> + <argument index="0" name="right" type="Transform2D"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="PackedVector2Array"> + </return> + <argument index="0" name="right" type="PackedVector2Array"> + </argument> + <description> + </description> + </method> + <method name="operator ==" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Transform2D"> + </argument> + <description> + </description> + </method> + <method name="operator []" qualifiers="operator"> + <return type="Vector2"> + </return> + <argument index="0" name="index" type="int"> + </argument> + <description> + </description> + </method> <method name="orthonormalized"> <return type="Transform2D"> </return> diff --git a/doc/classes/Vector2.xml b/doc/classes/Vector2.xml index 231e0ed06a..f99231de39 100644 --- a/doc/classes/Vector2.xml +++ b/doc/classes/Vector2.xml @@ -17,7 +17,23 @@ <link title="All 2D Demos">https://github.com/godotengine/godot-demo-projects/tree/master/2d</link> </tutorials> <methods> - <method name="Vector2"> + <method name="Vector2" qualifiers="constructor"> + <return type="Vector2"> + </return> + <description> + Constructs a default-initialized [Vector2] with all components set to [code]0[/code]. + </description> + </method> + <method name="Vector2" qualifiers="constructor"> + <return type="Vector2"> + </return> + <argument index="0" name="from" type="Vector2"> + </argument> + <description> + Constructs a [Vector2] as a copy of the given [Vector2]. + </description> + </method> + <method name="Vector2" qualifiers="constructor"> <return type="Vector2"> </return> <argument index="0" name="from" type="Vector2i"> @@ -26,7 +42,7 @@ Constructs a new [Vector2] from [Vector2i]. </description> </method> - <method name="Vector2"> + <method name="Vector2" qualifiers="constructor"> <return type="Vector2"> </return> <argument index="0" name="x" type="float"> @@ -234,6 +250,146 @@ Returns the vector scaled to unit length. Equivalent to [code]v / v.length()[/code]. </description> </method> + <method name="operator !=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Vector2"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Vector2"> + </return> + <argument index="0" name="right" type="Vector2"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Vector2"> + </return> + <argument index="0" name="right" type="Transform2D"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Vector2"> + </return> + <argument index="0" name="right" type="float"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Vector2"> + </return> + <argument index="0" name="right" type="int"> + </argument> + <description> + </description> + </method> + <method name="operator +" qualifiers="operator"> + <return type="Vector2"> + </return> + <description> + </description> + </method> + <method name="operator +" qualifiers="operator"> + <return type="Vector2"> + </return> + <argument index="0" name="right" type="Vector2"> + </argument> + <description> + </description> + </method> + <method name="operator -" qualifiers="operator"> + <return type="Vector2"> + </return> + <description> + </description> + </method> + <method name="operator -" qualifiers="operator"> + <return type="Vector2"> + </return> + <argument index="0" name="right" type="Vector2"> + </argument> + <description> + </description> + </method> + <method name="operator /" qualifiers="operator"> + <return type="Vector2"> + </return> + <argument index="0" name="right" type="Vector2"> + </argument> + <description> + </description> + </method> + <method name="operator /" qualifiers="operator"> + <return type="Vector2"> + </return> + <argument index="0" name="right" type="float"> + </argument> + <description> + </description> + </method> + <method name="operator /" qualifiers="operator"> + <return type="Vector2"> + </return> + <argument index="0" name="right" type="int"> + </argument> + <description> + </description> + </method> + <method name="operator <" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Vector2"> + </argument> + <description> + </description> + </method> + <method name="operator <=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Vector2"> + </argument> + <description> + </description> + </method> + <method name="operator ==" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Vector2"> + </argument> + <description> + </description> + </method> + <method name="operator >" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Vector2"> + </argument> + <description> + </description> + </method> + <method name="operator >=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Vector2"> + </argument> + <description> + </description> + </method> + <method name="operator []" qualifiers="operator"> + <return type="float"> + </return> + <argument index="0" name="index" type="int"> + </argument> + <description> + </description> + </method> <method name="posmod"> <return type="Vector2"> </return> diff --git a/doc/classes/Vector2i.xml b/doc/classes/Vector2i.xml index 75ddc46dab..a4ea5c2742 100644 --- a/doc/classes/Vector2i.xml +++ b/doc/classes/Vector2i.xml @@ -14,18 +14,23 @@ <link title="3Blue1Brown Essence of Linear Algebra">https://www.youtube.com/playlist?list=PLZHQObOWTQDPD3MizzM2xVFitgF8hE_ab</link> </tutorials> <methods> - <method name="Vector2i"> + <method name="Vector2i" qualifiers="constructor"> <return type="Vector2i"> </return> - <argument index="0" name="x" type="int"> - </argument> - <argument index="1" name="y" type="int"> + <description> + Constructs a default-initialized [Vector2i] with all components set to [code]0[/code]. + </description> + </method> + <method name="Vector2i" qualifiers="constructor"> + <return type="Vector2i"> + </return> + <argument index="0" name="from" type="Vector2i"> </argument> <description> - Constructs a new [Vector2i] from the given [code]x[/code] and [code]y[/code]. + Constructs a [Vector2i] as a copy of the given [Vector2i]. </description> </method> - <method name="Vector2i"> + <method name="Vector2i" qualifiers="constructor"> <return type="Vector2i"> </return> <argument index="0" name="from" type="Vector2"> @@ -34,6 +39,17 @@ Constructs a new [Vector2i] from [Vector2]. The floating point coordinates will be truncated. </description> </method> + <method name="Vector2i" qualifiers="constructor"> + <return type="Vector2i"> + </return> + <argument index="0" name="x" type="int"> + </argument> + <argument index="1" name="y" type="int"> + </argument> + <description> + Constructs a new [Vector2i] from the given [code]x[/code] and [code]y[/code]. + </description> + </method> <method name="abs"> <return type="Vector2i"> </return> @@ -48,6 +64,154 @@ Returns the ratio of [member x] to [member y]. </description> </method> + <method name="operator !=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Vector2i"> + </argument> + <description> + </description> + </method> + <method name="operator %" qualifiers="operator"> + <return type="Vector2i"> + </return> + <argument index="0" name="right" type="Vector2i"> + </argument> + <description> + </description> + </method> + <method name="operator %" qualifiers="operator"> + <return type="Vector2i"> + </return> + <argument index="0" name="right" type="int"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Vector2i"> + </return> + <argument index="0" name="right" type="Vector2i"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Vector2i"> + </return> + <argument index="0" name="right" type="float"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Vector2i"> + </return> + <argument index="0" name="right" type="int"> + </argument> + <description> + </description> + </method> + <method name="operator +" qualifiers="operator"> + <return type="Vector2i"> + </return> + <description> + </description> + </method> + <method name="operator +" qualifiers="operator"> + <return type="Vector2i"> + </return> + <argument index="0" name="right" type="Vector2i"> + </argument> + <description> + </description> + </method> + <method name="operator -" qualifiers="operator"> + <return type="Vector2i"> + </return> + <description> + </description> + </method> + <method name="operator -" qualifiers="operator"> + <return type="Vector2i"> + </return> + <argument index="0" name="right" type="Vector2i"> + </argument> + <description> + </description> + </method> + <method name="operator /" qualifiers="operator"> + <return type="Vector2i"> + </return> + <argument index="0" name="right" type="Vector2i"> + </argument> + <description> + </description> + </method> + <method name="operator /" qualifiers="operator"> + <return type="Vector2i"> + </return> + <argument index="0" name="right" type="float"> + </argument> + <description> + </description> + </method> + <method name="operator /" qualifiers="operator"> + <return type="Vector2i"> + </return> + <argument index="0" name="right" type="int"> + </argument> + <description> + </description> + </method> + <method name="operator <" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Vector2i"> + </argument> + <description> + </description> + </method> + <method name="operator <=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Vector2i"> + </argument> + <description> + </description> + </method> + <method name="operator ==" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Vector2i"> + </argument> + <description> + </description> + </method> + <method name="operator >" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Vector2i"> + </argument> + <description> + </description> + </method> + <method name="operator >=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Vector2i"> + </argument> + <description> + </description> + </method> + <method name="operator []" qualifiers="operator"> + <return type="int"> + </return> + <argument index="0" name="index" type="int"> + </argument> + <description> + </description> + </method> <method name="sign"> <return type="Vector2i"> </return> diff --git a/doc/classes/Vector3.xml b/doc/classes/Vector3.xml index 7e47f768b4..6ba0d6ab8d 100644 --- a/doc/classes/Vector3.xml +++ b/doc/classes/Vector3.xml @@ -17,7 +17,23 @@ <link title="All 3D Demos">https://github.com/godotengine/godot-demo-projects/tree/master/3d</link> </tutorials> <methods> - <method name="Vector3"> + <method name="Vector3" qualifiers="constructor"> + <return type="Vector3"> + </return> + <description> + Constructs a default-initialized [Vector3] with all components set to [code]0[/code]. + </description> + </method> + <method name="Vector3" qualifiers="constructor"> + <return type="Vector3"> + </return> + <argument index="0" name="from" type="Vector3"> + </argument> + <description> + Constructs a [Vector3] as a copy of the given [Vector3]. + </description> + </method> + <method name="Vector3" qualifiers="constructor"> <return type="Vector3"> </return> <argument index="0" name="from" type="Vector3i"> @@ -26,7 +42,7 @@ Constructs a new [Vector3] from [Vector3i]. </description> </method> - <method name="Vector3"> + <method name="Vector3" qualifiers="constructor"> <return type="Vector3"> </return> <argument index="0" name="x" type="float"> @@ -223,6 +239,162 @@ Returns the vector scaled to unit length. Equivalent to [code]v / v.length()[/code]. </description> </method> + <method name="operator !=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Vector3"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Vector3"> + </return> + <argument index="0" name="right" type="Vector3"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Vector3"> + </return> + <argument index="0" name="right" type="Basis"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Vector3"> + </return> + <argument index="0" name="right" type="Quat"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Vector3"> + </return> + <argument index="0" name="right" type="Transform"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Vector3"> + </return> + <argument index="0" name="right" type="float"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Vector3"> + </return> + <argument index="0" name="right" type="int"> + </argument> + <description> + </description> + </method> + <method name="operator +" qualifiers="operator"> + <return type="Vector3"> + </return> + <description> + </description> + </method> + <method name="operator +" qualifiers="operator"> + <return type="Vector3"> + </return> + <argument index="0" name="right" type="Vector3"> + </argument> + <description> + </description> + </method> + <method name="operator -" qualifiers="operator"> + <return type="Vector3"> + </return> + <description> + </description> + </method> + <method name="operator -" qualifiers="operator"> + <return type="Vector3"> + </return> + <argument index="0" name="right" type="Vector3"> + </argument> + <description> + </description> + </method> + <method name="operator /" qualifiers="operator"> + <return type="Vector3"> + </return> + <argument index="0" name="right" type="Vector3"> + </argument> + <description> + </description> + </method> + <method name="operator /" qualifiers="operator"> + <return type="Vector3"> + </return> + <argument index="0" name="right" type="float"> + </argument> + <description> + </description> + </method> + <method name="operator /" qualifiers="operator"> + <return type="Vector3"> + </return> + <argument index="0" name="right" type="int"> + </argument> + <description> + </description> + </method> + <method name="operator <" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Vector3"> + </argument> + <description> + </description> + </method> + <method name="operator <=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Vector3"> + </argument> + <description> + </description> + </method> + <method name="operator ==" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Vector3"> + </argument> + <description> + </description> + </method> + <method name="operator >" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Vector3"> + </argument> + <description> + </description> + </method> + <method name="operator >=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Vector3"> + </argument> + <description> + </description> + </method> + <method name="operator []" qualifiers="operator"> + <return type="float"> + </return> + <argument index="0" name="index" type="int"> + </argument> + <description> + </description> + </method> <method name="outer"> <return type="Basis"> </return> diff --git a/doc/classes/Vector3i.xml b/doc/classes/Vector3i.xml index 45e237fb23..a1ae2aceab 100644 --- a/doc/classes/Vector3i.xml +++ b/doc/classes/Vector3i.xml @@ -14,20 +14,23 @@ <link title="3Blue1Brown Essence of Linear Algebra">https://www.youtube.com/playlist?list=PLZHQObOWTQDPD3MizzM2xVFitgF8hE_ab</link> </tutorials> <methods> - <method name="Vector3i"> + <method name="Vector3i" qualifiers="constructor"> <return type="Vector3i"> </return> - <argument index="0" name="x" type="int"> - </argument> - <argument index="1" name="y" type="int"> - </argument> - <argument index="2" name="z" type="int"> + <description> + Constructs a default-initialized [Vector3i] with all components set to [code]0[/code]. + </description> + </method> + <method name="Vector3i" qualifiers="constructor"> + <return type="Vector3i"> + </return> + <argument index="0" name="from" type="Vector3i"> </argument> <description> - Returns a [Vector3i] with the given components. + Constructs a [Vector3i] as a copy of the given [Vector3i]. </description> </method> - <method name="Vector3i"> + <method name="Vector3i" qualifiers="constructor"> <return type="Vector3i"> </return> <argument index="0" name="from" type="Vector3"> @@ -36,6 +39,19 @@ Constructs a new [Vector3i] from [Vector3]. The floating point coordinates will be truncated. </description> </method> + <method name="Vector3i" qualifiers="constructor"> + <return type="Vector3i"> + </return> + <argument index="0" name="x" type="int"> + </argument> + <argument index="1" name="y" type="int"> + </argument> + <argument index="2" name="z" type="int"> + </argument> + <description> + Returns a [Vector3i] with the given components. + </description> + </method> <method name="abs"> <return type="Vector3i"> </return> @@ -56,6 +72,154 @@ Returns the axis of the vector's smallest value. See [code]AXIS_*[/code] constants. If all components are equal, this method returns [constant AXIS_Z]. </description> </method> + <method name="operator !=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Vector3i"> + </argument> + <description> + </description> + </method> + <method name="operator %" qualifiers="operator"> + <return type="Vector3i"> + </return> + <argument index="0" name="right" type="Vector3i"> + </argument> + <description> + </description> + </method> + <method name="operator %" qualifiers="operator"> + <return type="Vector3i"> + </return> + <argument index="0" name="right" type="int"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Vector3i"> + </return> + <argument index="0" name="right" type="Vector3i"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Vector3i"> + </return> + <argument index="0" name="right" type="float"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Vector3i"> + </return> + <argument index="0" name="right" type="int"> + </argument> + <description> + </description> + </method> + <method name="operator +" qualifiers="operator"> + <return type="Vector3i"> + </return> + <description> + </description> + </method> + <method name="operator +" qualifiers="operator"> + <return type="Vector3i"> + </return> + <argument index="0" name="right" type="Vector3i"> + </argument> + <description> + </description> + </method> + <method name="operator -" qualifiers="operator"> + <return type="Vector3i"> + </return> + <description> + </description> + </method> + <method name="operator -" qualifiers="operator"> + <return type="Vector3i"> + </return> + <argument index="0" name="right" type="Vector3i"> + </argument> + <description> + </description> + </method> + <method name="operator /" qualifiers="operator"> + <return type="Vector3i"> + </return> + <argument index="0" name="right" type="Vector3i"> + </argument> + <description> + </description> + </method> + <method name="operator /" qualifiers="operator"> + <return type="Vector3i"> + </return> + <argument index="0" name="right" type="float"> + </argument> + <description> + </description> + </method> + <method name="operator /" qualifiers="operator"> + <return type="Vector3i"> + </return> + <argument index="0" name="right" type="int"> + </argument> + <description> + </description> + </method> + <method name="operator <" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Vector3i"> + </argument> + <description> + </description> + </method> + <method name="operator <=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Vector3i"> + </argument> + <description> + </description> + </method> + <method name="operator ==" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Vector3i"> + </argument> + <description> + </description> + </method> + <method name="operator >" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Vector3i"> + </argument> + <description> + </description> + </method> + <method name="operator >=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="Vector3i"> + </argument> + <description> + </description> + </method> + <method name="operator []" qualifiers="operator"> + <return type="int"> + </return> + <argument index="0" name="index" type="int"> + </argument> + <description> + </description> + </method> <method name="sign"> <return type="Vector3i"> </return> diff --git a/doc/classes/bool.xml b/doc/classes/bool.xml index ce4d000a9b..03e8bee7d5 100644 --- a/doc/classes/bool.xml +++ b/doc/classes/bool.xml @@ -91,16 +91,23 @@ <tutorials> </tutorials> <methods> - <method name="bool"> + <method name="bool" qualifiers="constructor"> <return type="bool"> </return> - <argument index="0" name="from" type="int"> + <description> + Constructs a default-initialized [bool] set to [code]false[/code]. + </description> + </method> + <method name="bool" qualifiers="constructor"> + <return type="bool"> + </return> + <argument index="0" name="from" type="bool"> </argument> <description> - Cast an [int] value to a boolean value, this method will return [code]false[/code] if [code]0[/code] is passed in, and [code]true[/code] for all other ints. + Constructs a [bool] as a copy of the given [bool]. </description> </method> - <method name="bool"> + <method name="bool" qualifiers="constructor"> <return type="bool"> </return> <argument index="0" name="from" type="float"> @@ -109,14 +116,45 @@ Cast a [float] value to a boolean value, this method will return [code]false[/code] if [code]0.0[/code] is passed in, and [code]true[/code] for all other floats. </description> </method> - <method name="bool"> + <method name="bool" qualifiers="constructor"> + <return type="bool"> + </return> + <argument index="0" name="from" type="int"> + </argument> + <description> + Cast an [int] value to a boolean value, this method will return [code]false[/code] if [code]0[/code] is passed in, and [code]true[/code] for all other ints. + </description> + </method> + <method name="operator !=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="bool"> + </argument> + <description> + </description> + </method> + <method name="operator <" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="bool"> + </argument> + <description> + </description> + </method> + <method name="operator ==" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="bool"> + </argument> + <description> + </description> + </method> + <method name="operator >" qualifiers="operator"> <return type="bool"> </return> - <argument index="0" name="from" type="String"> + <argument index="0" name="right" type="bool"> </argument> <description> - Cast a [String] value to a boolean value, this method will return [code]false[/code] if [code]""[/code] is passed in, and [code]true[/code] for all non-empty strings. - Examples: [code]bool("False")[/code] returns [code]true[/code], [code]bool("")[/code] returns [code]false[/code]. </description> </method> </methods> diff --git a/doc/classes/float.xml b/doc/classes/float.xml index 16a696f959..85fe31eec8 100644 --- a/doc/classes/float.xml +++ b/doc/classes/float.xml @@ -9,7 +9,23 @@ <tutorials> </tutorials> <methods> - <method name="float"> + <method name="float" qualifiers="constructor"> + <return type="float"> + </return> + <description> + Constructs a default-initialized [float] set to [code]0.0[/code]. + </description> + </method> + <method name="float" qualifiers="constructor"> + <return type="float"> + </return> + <argument index="0" name="from" type="float"> + </argument> + <description> + Constructs a [float] as a copy of the given [float]. + </description> + </method> + <method name="float" qualifiers="constructor"> <return type="float"> </return> <argument index="0" name="from" type="bool"> @@ -18,22 +34,233 @@ Cast a [bool] value to a floating-point value, [code]float(true)[/code] will be equal to 1.0 and [code]float(false)[/code] will be equal to 0.0. </description> </method> - <method name="float"> + <method name="float" qualifiers="constructor"> <return type="float"> </return> <argument index="0" name="from" type="int"> </argument> <description> - Cast an [int] value to a floating-point value, [code]float(1)[/code] will be equal to 1.0. + Cast an [int] value to a floating-point value, [code]float(1)[/code] will be equal to [code]1.0[/code]. + </description> + </method> + <method name="operator !=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="float"> + </argument> + <description> </description> </method> - <method name="float"> + <method name="operator !=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="int"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> <return type="float"> </return> - <argument index="0" name="from" type="String"> + <argument index="0" name="right" type="float"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Vector2"> + </return> + <argument index="0" name="right" type="Vector2"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Vector2i"> + </return> + <argument index="0" name="right" type="Vector2i"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Vector3"> + </return> + <argument index="0" name="right" type="Vector3"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Vector3i"> + </return> + <argument index="0" name="right" type="Vector3i"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Quat"> + </return> + <argument index="0" name="right" type="Quat"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Color"> + </return> + <argument index="0" name="right" type="Color"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="float"> + </return> + <argument index="0" name="right" type="int"> + </argument> + <description> + </description> + </method> + <method name="operator +" qualifiers="operator"> + <return type="float"> + </return> + <description> + </description> + </method> + <method name="operator +" qualifiers="operator"> + <return type="float"> + </return> + <argument index="0" name="right" type="float"> + </argument> + <description> + </description> + </method> + <method name="operator +" qualifiers="operator"> + <return type="float"> + </return> + <argument index="0" name="right" type="int"> + </argument> + <description> + </description> + </method> + <method name="operator -" qualifiers="operator"> + <return type="float"> + </return> + <description> + </description> + </method> + <method name="operator -" qualifiers="operator"> + <return type="float"> + </return> + <argument index="0" name="right" type="float"> + </argument> + <description> + </description> + </method> + <method name="operator -" qualifiers="operator"> + <return type="float"> + </return> + <argument index="0" name="right" type="int"> + </argument> + <description> + </description> + </method> + <method name="operator /" qualifiers="operator"> + <return type="float"> + </return> + <argument index="0" name="right" type="float"> + </argument> + <description> + </description> + </method> + <method name="operator /" qualifiers="operator"> + <return type="float"> + </return> + <argument index="0" name="right" type="int"> + </argument> + <description> + </description> + </method> + <method name="operator <" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="float"> + </argument> + <description> + </description> + </method> + <method name="operator <" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="int"> + </argument> + <description> + </description> + </method> + <method name="operator <=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="float"> + </argument> + <description> + </description> + </method> + <method name="operator <=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="int"> + </argument> + <description> + </description> + </method> + <method name="operator ==" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="float"> + </argument> + <description> + </description> + </method> + <method name="operator ==" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="int"> + </argument> + <description> + </description> + </method> + <method name="operator >" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="float"> + </argument> + <description> + </description> + </method> + <method name="operator >" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="int"> + </argument> + <description> + </description> + </method> + <method name="operator >=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="float"> + </argument> + <description> + </description> + </method> + <method name="operator >=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="int"> </argument> <description> - Cast a [String] value to a floating-point value. This method accepts float value strings like [code]"1.23"[/code] and exponential notation strings for its parameter so calling [code]float("1e3")[/code] will return 1000.0 and calling [code]float("1e-3")[/code] will return 0.001. Calling this method with an invalid float string will return 0. This method stops parsing at the first invalid character and will return the parsed result so far, so calling [code]float("1a3")[/code] will return 1 while calling [code]float("1e3a2")[/code] will return 1000.0. </description> </method> </methods> diff --git a/doc/classes/int.xml b/doc/classes/int.xml index 2c9f0ad371..5ac9f8405a 100644 --- a/doc/classes/int.xml +++ b/doc/classes/int.xml @@ -23,7 +23,23 @@ <tutorials> </tutorials> <methods> - <method name="int"> + <method name="int" qualifiers="constructor"> + <return type="int"> + </return> + <description> + Constructs a default-initialized [int] set to [code]0[/code]. + </description> + </method> + <method name="int" qualifiers="constructor"> + <return type="int"> + </return> + <argument index="0" name="from" type="int"> + </argument> + <description> + Constructs an [int] as a copy of the given [int]. + </description> + </method> + <method name="int" qualifiers="constructor"> <return type="int"> </return> <argument index="0" name="from" type="bool"> @@ -32,7 +48,7 @@ Cast a [bool] value to an integer value, [code]int(true)[/code] will be equals to 1 and [code]int(false)[/code] will be equals to 0. </description> </method> - <method name="int"> + <method name="int" qualifiers="constructor"> <return type="int"> </return> <argument index="0" name="from" type="float"> @@ -41,13 +57,278 @@ Cast a float value to an integer value, this method simply removes the number fractions, so for example [code]int(2.7)[/code] will be equals to 2, [code]int(.1)[/code] will be equals to 0 and [code]int(-2.7)[/code] will be equals to -2. </description> </method> - <method name="int"> + <method name="operator !=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="float"> + </argument> + <description> + </description> + </method> + <method name="operator !=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="int"> + </argument> + <description> + </description> + </method> + <method name="operator %" qualifiers="operator"> + <return type="int"> + </return> + <argument index="0" name="right" type="int"> + </argument> + <description> + </description> + </method> + <method name="operator &" qualifiers="operator"> + <return type="int"> + </return> + <argument index="0" name="right" type="int"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="float"> + </return> + <argument index="0" name="right" type="float"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="int"> + </return> + <argument index="0" name="right" type="int"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Vector2"> + </return> + <argument index="0" name="right" type="Vector2"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Vector2i"> + </return> + <argument index="0" name="right" type="Vector2i"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Vector3"> + </return> + <argument index="0" name="right" type="Vector3"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Vector3i"> + </return> + <argument index="0" name="right" type="Vector3i"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Quat"> + </return> + <argument index="0" name="right" type="Quat"> + </argument> + <description> + </description> + </method> + <method name="operator *" qualifiers="operator"> + <return type="Color"> + </return> + <argument index="0" name="right" type="Color"> + </argument> + <description> + </description> + </method> + <method name="operator +" qualifiers="operator"> + <return type="int"> + </return> + <description> + </description> + </method> + <method name="operator +" qualifiers="operator"> + <return type="float"> + </return> + <argument index="0" name="right" type="float"> + </argument> + <description> + </description> + </method> + <method name="operator +" qualifiers="operator"> + <return type="int"> + </return> + <argument index="0" name="right" type="int"> + </argument> + <description> + </description> + </method> + <method name="operator -" qualifiers="operator"> + <return type="int"> + </return> + <description> + </description> + </method> + <method name="operator -" qualifiers="operator"> + <return type="float"> + </return> + <argument index="0" name="right" type="float"> + </argument> + <description> + </description> + </method> + <method name="operator -" qualifiers="operator"> + <return type="int"> + </return> + <argument index="0" name="right" type="int"> + </argument> + <description> + </description> + </method> + <method name="operator /" qualifiers="operator"> + <return type="float"> + </return> + <argument index="0" name="right" type="float"> + </argument> + <description> + </description> + </method> + <method name="operator /" qualifiers="operator"> + <return type="int"> + </return> + <argument index="0" name="right" type="int"> + </argument> + <description> + </description> + </method> + <method name="operator <" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="float"> + </argument> + <description> + </description> + </method> + <method name="operator <" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="int"> + </argument> + <description> + </description> + </method> + <method name="operator <<" qualifiers="operator"> <return type="int"> </return> - <argument index="0" name="from" type="String"> + <argument index="0" name="right" type="int"> + </argument> + <description> + </description> + </method> + <method name="operator <=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="float"> + </argument> + <description> + </description> + </method> + <method name="operator <=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="int"> </argument> <description> - Cast a [String] value to an integer value, this method is an integer parser from a string, so calling this method with an invalid integer string will return 0, a valid string will be something like [code]'1.7'[/code]. This method will ignore all non-number characters, so calling [code]int('1e3')[/code] will return 13. + </description> + </method> + <method name="operator ==" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="float"> + </argument> + <description> + </description> + </method> + <method name="operator ==" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="int"> + </argument> + <description> + </description> + </method> + <method name="operator >" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="float"> + </argument> + <description> + </description> + </method> + <method name="operator >" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="int"> + </argument> + <description> + </description> + </method> + <method name="operator >=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="float"> + </argument> + <description> + </description> + </method> + <method name="operator >=" qualifiers="operator"> + <return type="bool"> + </return> + <argument index="0" name="right" type="int"> + </argument> + <description> + </description> + </method> + <method name="operator >>" qualifiers="operator"> + <return type="int"> + </return> + <argument index="0" name="right" type="int"> + </argument> + <description> + </description> + </method> + <method name="operator ^" qualifiers="operator"> + <return type="int"> + </return> + <argument index="0" name="right" type="int"> + </argument> + <description> + </description> + </method> + <method name="operator |" qualifiers="operator"> + <return type="int"> + </return> + <argument index="0" name="right" type="int"> + </argument> + <description> + </description> + </method> + <method name="operator ~" qualifiers="operator"> + <return type="int"> + </return> + <description> </description> </method> </methods> diff --git a/doc/tools/doc_merge.py b/doc/tools/doc_merge.py deleted file mode 100755 index f6f52f5d66..0000000000 --- a/doc/tools/doc_merge.py +++ /dev/null @@ -1,237 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- - -import sys -import xml.etree.ElementTree as ET - - -tree = ET.parse(sys.argv[1]) -old_doc = tree.getroot() - -tree = ET.parse(sys.argv[2]) -new_doc = tree.getroot() - -f = file(sys.argv[3], "wb") -tab = 0 - -old_classes = {} - - -def write_string(_f, text, newline=True): - for t in range(tab): - _f.write("\t") - _f.write(text) - if newline: - _f.write("\n") - - -def escape(ret): - ret = ret.replace("&", "&") - ret = ret.replace("<", ">") - ret = ret.replace(">", "<") - ret = ret.replace("'", "'") - ret = ret.replace('"', """) - return ret - - -def inc_tab(): - global tab - tab += 1 - - -def dec_tab(): - global tab - tab -= 1 - - -write_string(f, '<?xml version="1.0" encoding="UTF-8" ?>') -write_string(f, '<doc version="' + new_doc.attrib["version"] + '">') - - -def get_tag(node, name): - tag = "" - if name in node.attrib: - tag = " " + name + '="' + escape(node.attrib[name]) + '" ' - return tag - - -def find_method_descr(old_class, name): - - methods = old_class.find("methods") - if methods != None and len(list(methods)) > 0: - for m in list(methods): - if m.attrib["name"] == name: - description = m.find("description") - if description != None and description.text.strip() != "": - return description.text - - return None - - -def find_signal_descr(old_class, name): - - signals = old_class.find("signals") - if signals != None and len(list(signals)) > 0: - for m in list(signals): - if m.attrib["name"] == name: - description = m.find("description") - if description != None and description.text.strip() != "": - return description.text - - return None - - -def find_constant_descr(old_class, name): - - if old_class is None: - return None - constants = old_class.find("constants") - if constants != None and len(list(constants)) > 0: - for m in list(constants): - if m.attrib["name"] == name: - if m.text.strip() != "": - return m.text - return None - - -def write_class(c): - class_name = c.attrib["name"] - print("Parsing Class: " + class_name) - if class_name in old_classes: - old_class = old_classes[class_name] - else: - old_class = None - - category = get_tag(c, "category") - inherits = get_tag(c, "inherits") - write_string(f, '<class name="' + class_name + '" ' + category + inherits + ">") - inc_tab() - - write_string(f, "<brief_description>") - - if old_class != None: - old_brief_descr = old_class.find("brief_description") - if old_brief_descr != None: - write_string(f, escape(old_brief_descr.text.strip())) - - write_string(f, "</brief_description>") - - write_string(f, "<description>") - if old_class != None: - old_descr = old_class.find("description") - if old_descr != None: - write_string(f, escape(old_descr.text.strip())) - - write_string(f, "</description>") - - methods = c.find("methods") - if methods != None and len(list(methods)) > 0: - - write_string(f, "<methods>") - inc_tab() - - for m in list(methods): - qualifiers = get_tag(m, "qualifiers") - - write_string(f, '<method name="' + escape(m.attrib["name"]) + '" ' + qualifiers + ">") - inc_tab() - - for a in list(m): - if a.tag == "return": - typ = get_tag(a, "type") - write_string(f, "<return" + typ + ">") - write_string(f, "</return>") - elif a.tag == "argument": - - default = get_tag(a, "default") - - write_string( - f, - '<argument index="' - + a.attrib["index"] - + '" name="' - + escape(a.attrib["name"]) - + '" type="' - + a.attrib["type"] - + '"' - + default - + ">", - ) - write_string(f, "</argument>") - - write_string(f, "<description>") - if old_class != None: - old_method_descr = find_method_descr(old_class, m.attrib["name"]) - if old_method_descr: - write_string(f, escape(escape(old_method_descr.strip()))) - - write_string(f, "</description>") - dec_tab() - write_string(f, "</method>") - dec_tab() - write_string(f, "</methods>") - - signals = c.find("signals") - if signals != None and len(list(signals)) > 0: - - write_string(f, "<signals>") - inc_tab() - - for m in list(signals): - - write_string(f, '<signal name="' + escape(m.attrib["name"]) + '">') - inc_tab() - - for a in list(m): - if a.tag == "argument": - - write_string( - f, - '<argument index="' - + a.attrib["index"] - + '" name="' - + escape(a.attrib["name"]) - + '" type="' - + a.attrib["type"] - + '">', - ) - write_string(f, "</argument>") - - write_string(f, "<description>") - if old_class != None: - old_signal_descr = find_signal_descr(old_class, m.attrib["name"]) - if old_signal_descr: - write_string(f, escape(old_signal_descr.strip())) - write_string(f, "</description>") - dec_tab() - write_string(f, "</signal>") - dec_tab() - write_string(f, "</signals>") - - constants = c.find("constants") - if constants != None and len(list(constants)) > 0: - - write_string(f, "<constants>") - inc_tab() - - for m in list(constants): - - write_string(f, '<constant name="' + escape(m.attrib["name"]) + '" value="' + m.attrib["value"] + '">') - old_constant_descr = find_constant_descr(old_class, m.attrib["name"]) - if old_constant_descr: - write_string(f, escape(old_constant_descr.strip())) - write_string(f, "</constant>") - - dec_tab() - write_string(f, "</constants>") - - dec_tab() - write_string(f, "</class>") - - -for c in list(old_doc): - old_classes[c.attrib["name"]] = c - -for c in list(new_doc): - write_class(c) -write_string(f, "</doc>\n") diff --git a/doc/tools/makerst.py b/doc/tools/makerst.py index ed147f31cd..5335116c8a 100755 --- a/doc/tools/makerst.py +++ b/doc/tools/makerst.py @@ -1042,6 +1042,8 @@ def make_footer(): # type: () -> str ".. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`\n" ".. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`\n" ".. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`\n" + ".. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`\n" + ".. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`\n" ) # fmt: on diff --git a/drivers/dummy/rasterizer_dummy.h b/drivers/dummy/rasterizer_dummy.h index f0e62de144..2d1c4f8d85 100644 --- a/drivers/dummy/rasterizer_dummy.h +++ b/drivers/dummy/rasterizer_dummy.h @@ -98,7 +98,7 @@ public: void environment_set_adjustment(RID p_env, bool p_enable, float p_brightness, float p_contrast, float p_saturation, RID p_ramp) override {} - void environment_set_fog(RID p_env, bool p_enable, const Color &p_light_color, float p_light_energy, float p_sun_scatter, float p_density, float p_height, float p_height_density) override {} + void environment_set_fog(RID p_env, bool p_enable, const Color &p_light_color, float p_light_energy, float p_sun_scatter, float p_density, float p_height, float p_height_density, float p_aerial_perspective) override {} void environment_set_volumetric_fog(RID p_env, bool p_enable, float p_density, const Color &p_light, float p_light_energy, float p_length, float p_detail_spread, float p_gi_inject, RS::EnvVolumetricFogShadowFilter p_shadow_filter) override {} void environment_set_volumetric_fog_volume_size(int p_size, int p_depth) override {} void environment_set_volumetric_fog_filter_active(bool p_enable) override {} @@ -161,7 +161,7 @@ public: void set_debug_draw_mode(RS::ViewportDebugDraw p_debug_draw) override {} RID render_buffers_create() override { return RID(); } - void render_buffers_configure(RID p_render_buffers, RID p_render_target, int p_width, int p_height, RS::ViewportMSAA p_msaa, RS::ViewportScreenSpaceAA p_screen_space_aa) override {} + void render_buffers_configure(RID p_render_buffers, RID p_render_target, int p_width, int p_height, RS::ViewportMSAA p_msaa, RS::ViewportScreenSpaceAA p_screen_space_aa, bool p_use_debanding) override {} void screen_space_roughness_limiter_set_active(bool p_enable, float p_amount, float p_curve) override {} bool screen_space_roughness_limiter_is_active() const override { return false; } @@ -251,6 +251,15 @@ public: void texture_add_to_decal_atlas(RID p_texture, bool p_panorama_to_dp = false) override {} void texture_remove_from_decal_atlas(RID p_texture, bool p_panorama_to_dp = false) override {} + /* CANVAS TEXTURE API */ + + RID canvas_texture_create() override { return RID(); } + void canvas_texture_set_channel(RID p_canvas_texture, RS::CanvasTextureChannel p_channel, RID p_texture) override {} + void canvas_texture_set_shading_parameters(RID p_canvas_texture, const Color &p_base_color, float p_shininess) override {} + + void canvas_texture_set_texture_filter(RID p_item, RS::CanvasItemTextureFilter p_filter) override {} + void canvas_texture_set_texture_repeat(RID p_item, RS::CanvasItemTextureRepeat p_repeat) override {} + #if 0 RID texture_create() override { @@ -935,23 +944,22 @@ public: class RasterizerCanvasDummy : public RasterizerCanvas { public: - TextureBindingID request_texture_binding(RID p_texture, RID p_normalmap, RID p_specular, RS::CanvasItemTextureFilter p_filter, RS::CanvasItemTextureRepeat p_repeat, RID p_multimesh) override { return 0; } - void free_texture_binding(TextureBindingID p_binding) override {} - PolygonID request_polygon(const Vector<int> &p_indices, const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs = Vector<Point2>(), const Vector<int> &p_bones = Vector<int>(), const Vector<float> &p_weights = Vector<float>()) override { return 0; } void free_polygon(PolygonID p_polygon) override {} - void canvas_render_items(RID p_to_render_target, Item *p_item_list, const Color &p_modulate, Light *p_light_list, const Transform2D &p_canvas_transform) override {} + void canvas_render_items(RID p_to_render_target, Item *p_item_list, const Color &p_modulate, Light *p_light_list, Light *p_directional_list, const Transform2D &p_canvas_transform, RS::CanvasItemTextureFilter p_default_filter, RS::CanvasItemTextureRepeat p_default_repeat, bool p_snap_2d_vertices_to_pixel) override {} void canvas_debug_viewport_shadows(Light *p_lights_with_shadow) override {} RID light_create() override { return RID(); } void light_set_texture(RID p_rid, RID p_texture) override {} - void light_set_use_shadow(RID p_rid, bool p_enable, int p_resolution) override {} - void light_update_shadow(RID p_rid, const Transform2D &p_light_xform, int p_light_mask, float p_near, float p_far, LightOccluderInstance *p_occluders) override {} + void light_set_use_shadow(RID p_rid, bool p_enable) override {} + void light_update_shadow(RID p_rid, int p_shadow_index, const Transform2D &p_light_xform, int p_light_mask, float p_near, float p_far, LightOccluderInstance *p_occluders) override {} + void light_update_directional_shadow(RID p_rid, int p_shadow_index, const Transform2D &p_light_xform, int p_light_mask, float p_cull_distance, const Rect2 &p_clip_rect, LightOccluderInstance *p_occluders) override {} RID occluder_polygon_create() override { return RID(); } void occluder_polygon_set_shape_as_lines(RID p_occluder, const Vector<Vector2> &p_lines) override {} void occluder_polygon_set_cull_mode(RID p_occluder, RS::CanvasOccluderPolygonCullMode p_mode) override {} + void set_shadow_texture_size(int p_size) override {} void draw_window_margins(int *p_margins, RID *p_margin_textures) override {} diff --git a/editor/animation_track_editor.cpp b/editor/animation_track_editor.cpp index 5471a9907b..6f30d5a492 100644 --- a/editor/animation_track_editor.cpp +++ b/editor/animation_track_editor.cpp @@ -228,9 +228,9 @@ public: if (Variant::can_convert(args[idx].get_type(), t)) { Variant old = args[idx]; Variant *ptrs[1] = { &old }; - args.write[idx] = Variant::construct(t, (const Variant **)ptrs, 1, err); + Variant::construct(t, args.write[idx], (const Variant **)ptrs, 1, err); } else { - args.write[idx] = Variant::construct(t, nullptr, 0, err); + Variant::construct(t, args.write[idx], nullptr, 0, err); } change_notify_deserved = true; d_new["args"] = args; @@ -846,9 +846,9 @@ public: if (Variant::can_convert(args[idx].get_type(), t)) { Variant old = args[idx]; Variant *ptrs[1] = { &old }; - args.write[idx] = Variant::construct(t, (const Variant **)ptrs, 1, err); + Variant::construct(t, args.write[idx], (const Variant **)ptrs, 1, err); } else { - args.write[idx] = Variant::construct(t, nullptr, 0, err); + Variant::construct(t, args.write[idx], nullptr, 0, err); } change_notify_deserved = true; d_new["args"] = args; @@ -4587,7 +4587,8 @@ void AnimationTrackEditor::_add_method_key(const String &p_method) { params.push_back(arg); } else { Callable::CallError ce; - Variant arg = Variant::construct(E->get().arguments[i].type, nullptr, 0, ce); + Variant arg; + Variant::construct(E->get().arguments[i].type, arg, nullptr, 0, ce); params.push_back(arg); } } diff --git a/editor/array_property_edit.cpp b/editor/array_property_edit.cpp index 20f947e707..0b6b1ef6a7 100644 --- a/editor/array_property_edit.cpp +++ b/editor/array_property_edit.cpp @@ -43,7 +43,7 @@ Variant ArrayPropertyEdit::get_array() const { Variant arr = o->get(property); if (!arr.is_array()) { Callable::CallError ce; - arr = Variant::construct(default_type, nullptr, 0, ce); + Variant::construct(default_type, arr, nullptr, 0, ce); } return arr; } @@ -107,7 +107,7 @@ bool ArrayPropertyEdit::_set(const StringName &p_name, const Variant &p_value) { new_type = arr.get(size - 1).get_type(); } if (new_type != Variant::NIL) { - init = Variant::construct(new_type, nullptr, 0, ce); + Variant::construct(new_type, init, nullptr, 0, ce); for (int i = size; i < newsize; i++) { ur->add_do_method(this, "_set_value", i, init); } @@ -136,7 +136,8 @@ bool ArrayPropertyEdit::_set(const StringName &p_name, const Variant &p_value) { Variant value = arr.get(idx); if (value.get_type() != type && type >= 0 && type < Variant::VARIANT_MAX) { Callable::CallError ce; - Variant new_value = Variant::construct(Variant::Type(type), nullptr, 0, ce); + Variant new_value; + Variant::construct(Variant::Type(type), new_value, nullptr, 0, ce); UndoRedo *ur = EditorNode::get_undo_redo(); ur->create_action(TTR("Change Array Value Type")); diff --git a/editor/code_editor.cpp b/editor/code_editor.cpp index 236d1e884e..3182bca0eb 100644 --- a/editor/code_editor.cpp +++ b/editor/code_editor.cpp @@ -1138,6 +1138,7 @@ void CodeTextEditor::move_lines_up() { int from_col = text_editor->get_selection_from_column(); int to_line = text_editor->get_selection_to_line(); int to_column = text_editor->get_selection_to_column(); + int cursor_line = text_editor->cursor_get_line(); for (int i = from_line; i <= to_line; i++) { int line_id = i; @@ -1155,7 +1156,9 @@ void CodeTextEditor::move_lines_up() { } int from_line_up = from_line > 0 ? from_line - 1 : from_line; int to_line_up = to_line > 0 ? to_line - 1 : to_line; + int cursor_line_up = cursor_line > 0 ? cursor_line - 1 : cursor_line; text_editor->select(from_line_up, from_col, to_line_up, to_column); + text_editor->cursor_set_line(cursor_line_up); } else { int line_id = text_editor->cursor_get_line(); int next_id = line_id - 1; @@ -1181,6 +1184,7 @@ void CodeTextEditor::move_lines_down() { int from_col = text_editor->get_selection_from_column(); int to_line = text_editor->get_selection_to_line(); int to_column = text_editor->get_selection_to_column(); + int cursor_line = text_editor->cursor_get_line(); for (int i = to_line; i >= from_line; i--) { int line_id = i; @@ -1198,7 +1202,9 @@ void CodeTextEditor::move_lines_down() { } int from_line_down = from_line < text_editor->get_line_count() ? from_line + 1 : from_line; int to_line_down = to_line < text_editor->get_line_count() ? to_line + 1 : to_line; + int cursor_line_down = cursor_line < text_editor->get_line_count() ? cursor_line + 1 : cursor_line; text_editor->select(from_line_down, from_col, to_line_down, to_column); + text_editor->cursor_set_line(cursor_line_down); } else { int line_id = text_editor->cursor_get_line(); int next_id = line_id + 1; diff --git a/editor/debugger/script_editor_debugger.cpp b/editor/debugger/script_editor_debugger.cpp index 2d8a174773..248073c5a2 100644 --- a/editor/debugger/script_editor_debugger.cpp +++ b/editor/debugger/script_editor_debugger.cpp @@ -1023,7 +1023,7 @@ void ScriptEditorDebugger::_method_changed(Object *p_base, const StringName &p_n for (int i = 0; i < VARIANT_ARG_MAX; i++) { //no pointers, sorry - if (argptr[i] && (argptr[i]->get_type() == Variant::OBJECT || argptr[i]->get_type() == Variant::_RID)) { + if (argptr[i] && (argptr[i]->get_type() == Variant::OBJECT || argptr[i]->get_type() == Variant::RID)) { return; } } diff --git a/editor/doc_data.cpp b/editor/doc_data.cpp index 1888cc1e41..8504d61d2f 100644 --- a/editor/doc_data.cpp +++ b/editor/doc_data.cpp @@ -561,18 +561,87 @@ void DocData::generate(bool p_basic_types) { c.name = cname; Callable::CallError cerror; - Variant v = Variant::construct(Variant::Type(i), nullptr, 0, cerror); + Variant v; + Variant::construct(Variant::Type(i), v, nullptr, 0, cerror); List<MethodInfo> method_list; v.get_method_list(&method_list); method_list.sort(); Variant::get_constructor_list(Variant::Type(i), &method_list); + for (int j = 0; j < Variant::OP_AND; j++) { // Showing above 'and' is pretty confusing and there are a lot of variations. + for (int k = 0; k < Variant::VARIANT_MAX; k++) { + Variant::Type rt = Variant::get_operator_return_type(Variant::Operator(j), Variant::Type(i), Variant::Type(k)); + if (rt != Variant::NIL) { // Has operator. + // Skip String % operator as it's registered separately for each Variant arg type, + // we'll add it manually below. + if (i == Variant::STRING && Variant::Operator(j) == Variant::OP_MODULE) { + continue; + } + MethodInfo mi; + mi.name = "operator " + Variant::get_operator_name(Variant::Operator(j)); + mi.return_val.type = rt; + if (k != Variant::NIL) { + PropertyInfo arg; + arg.name = "right"; + arg.type = Variant::Type(k); + mi.arguments.push_back(arg); + } + method_list.push_back(mi); + } + } + } + + if (i == Variant::STRING) { + // We skipped % operator above, and we register it manually once for Variant arg type here. + MethodInfo mi; + mi.name = "operator %"; + mi.return_val.type = Variant::STRING; + + PropertyInfo arg; + arg.name = "right"; + arg.type = Variant::NIL; + arg.usage = PROPERTY_USAGE_NIL_IS_VARIANT; + mi.arguments.push_back(arg); + + method_list.push_back(mi); + } + + if (Variant::is_keyed(Variant::Type(i))) { + MethodInfo mi; + mi.name = "operator []"; + mi.return_val.type = Variant::NIL; + mi.return_val.usage = PROPERTY_USAGE_NIL_IS_VARIANT; + + PropertyInfo arg; + arg.name = "key"; + arg.type = Variant::NIL; + arg.usage = PROPERTY_USAGE_NIL_IS_VARIANT; + mi.arguments.push_back(arg); + + method_list.push_back(mi); + } else if (Variant::has_indexing(Variant::Type(i))) { + MethodInfo mi; + mi.name = "operator []"; + mi.return_val.type = Variant::get_indexed_element_type(Variant::Type(i)); + PropertyInfo arg; + arg.name = "index"; + arg.type = Variant::INT; + mi.arguments.push_back(arg); + + method_list.push_back(mi); + } + for (List<MethodInfo>::Element *E = method_list.front(); E; E = E->next()) { MethodInfo &mi = E->get(); MethodDoc method; method.name = mi.name; + if (method.name == cname) { + method.qualifiers = "constructor"; + } else if (method.name.begins_with("operator")) { + method.qualifiers = "operator"; + } for (int j = 0; j < mi.arguments.size(); j++) { PropertyInfo arginfo = mi.arguments[j]; @@ -667,6 +736,43 @@ void DocData::generate(bool p_basic_types) { } c.properties.push_back(pd); } + + List<StringName> utility_functions; + Variant::get_utility_function_list(&utility_functions); + utility_functions.sort_custom<StringName::AlphCompare>(); + for (List<StringName>::Element *E = utility_functions.front(); E; E = E->next()) { + MethodDoc md; + md.name = E->get(); + //return + if (Variant::has_utility_function_return_value(E->get())) { + PropertyInfo pi; + pi.type = Variant::get_utility_function_return_type(E->get()); + if (pi.type == Variant::NIL) { + pi.usage = PROPERTY_USAGE_NIL_IS_VARIANT; + } + DocData::ArgumentDoc ad; + argument_doc_from_arginfo(ad, pi); + md.return_type = ad.type; + } + + if (Variant::is_utility_function_vararg(E->get())) { + md.qualifiers = "vararg"; + } else { + for (int i = 0; i < Variant::get_utility_function_argument_count(E->get()); i++) { + PropertyInfo pi; + pi.type = Variant::get_utility_function_argument_type(E->get(), i); + pi.name = Variant::get_utility_function_argument_name(E->get(), i); + if (pi.type == Variant::NIL) { + pi.usage = PROPERTY_USAGE_NIL_IS_VARIANT; + } + DocData::ArgumentDoc ad; + argument_doc_from_arginfo(ad, pi); + md.arguments.push_back(ad); + } + } + + c.methods.push_back(md); + } } // Built-in script reference. @@ -1096,7 +1202,7 @@ Error DocData::save_classes(const String &p_default_path, const Map<String, Stri qualifiers += " qualifiers=\"" + m.qualifiers.xml_escape() + "\""; } - _write_string(f, 2, "<method name=\"" + m.name + "\"" + qualifiers + ">"); + _write_string(f, 2, "<method name=\"" + m.name.xml_escape() + "\"" + qualifiers + ">"); if (m.return_type != "") { String enum_text; diff --git a/editor/doc_data.h b/editor/doc_data.h index 5fa20c6f0c..2cb475d137 100644 --- a/editor/doc_data.h +++ b/editor/doc_data.h @@ -43,6 +43,9 @@ public: String enumeration; String default_value; bool operator<(const ArgumentDoc &p_arg) const { + if (name == p_arg.name) { + return type < p_arg.type; + } return name < p_arg.name; } }; @@ -55,6 +58,20 @@ public: String description; Vector<ArgumentDoc> arguments; bool operator<(const MethodDoc &p_method) const { + if (name == p_method.name) { + // Must be a constructor since there is no overloading. + // We want this arbitrary order for a class "Foo": + // - 1. Default constructor: Foo() + // - 2. Copy constructor: Foo(Foo) + // - 3+. Other constructors Foo(Bar, ...) based on first argument's name + if (arguments.size() == 0 || p_method.arguments.size() == 0) { // 1. + return arguments.size() < p_method.arguments.size(); + } + if (arguments[0].type == return_type || p_method.arguments[0].type == p_method.return_type) { // 2. + return (arguments[0].type == return_type) || (p_method.arguments[0].type != p_method.return_type); + } + return arguments[0] < p_method.arguments[0]; + } return name < p_method.name; } }; diff --git a/editor/editor_inspector.cpp b/editor/editor_inspector.cpp index 1837b23a0b..371100652f 100644 --- a/editor/editor_inspector.cpp +++ b/editor/editor_inspector.cpp @@ -2278,7 +2278,7 @@ void EditorInspector::_property_checked(const String &p_path, bool p_checked) { for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) { if (E->get().name == p_path) { Callable::CallError ce; - to_create = Variant::construct(E->get().type, nullptr, 0, ce); + Variant::construct(E->get().type, to_create, nullptr, 0, ce); break; } } diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp index d0d99e071a..c6613cdf63 100644 --- a/editor/editor_node.cpp +++ b/editor/editor_node.cpp @@ -2705,10 +2705,14 @@ void EditorNode::_screenshot(bool p_use_utc) { } void EditorNode::_save_screenshot(NodePath p_path) { - SubViewport *viewport = Object::cast_to<SubViewport>(EditorInterface::get_singleton()->get_editor_viewport()->get_viewport()); - viewport->set_clear_mode(SubViewport::CLEAR_MODE_ONLY_NEXT_FRAME); - Ref<Image> img = viewport->get_texture()->get_data(); - viewport->set_clear_mode(SubViewport::CLEAR_MODE_ALWAYS); + Control *editor_viewport = EditorInterface::get_singleton()->get_editor_viewport(); + ERR_FAIL_COND_MSG(!editor_viewport, "Cannot get editor viewport."); + Viewport *viewport = editor_viewport->get_viewport(); + ERR_FAIL_COND_MSG(!viewport, "Cannot get editor viewport."); + Ref<ViewportTexture> texture = viewport->get_texture(); + ERR_FAIL_COND_MSG(texture.is_null(), "Cannot get editor viewport texture."); + Ref<Image> img = texture->get_data(); + ERR_FAIL_COND_MSG(img.is_null(), "Cannot get editor viewport texture image."); Error error = img->save_png(p_path); ERR_FAIL_COND_MSG(error != OK, "Cannot save screenshot to file '" + p_path + "'."); } diff --git a/editor/editor_plugin.cpp b/editor/editor_plugin.cpp index e330713cfb..49d8e58955 100644 --- a/editor/editor_plugin.cpp +++ b/editor/editor_plugin.cpp @@ -866,6 +866,8 @@ void EditorPlugin::_bind_methods() { ClassDB::add_virtual_method(get_class_static(), MethodInfo("forward_canvas_draw_over_viewport", PropertyInfo(Variant::OBJECT, "overlay", PROPERTY_HINT_RESOURCE_TYPE, "Control"))); ClassDB::add_virtual_method(get_class_static(), MethodInfo("forward_canvas_force_draw_over_viewport", PropertyInfo(Variant::OBJECT, "overlay", PROPERTY_HINT_RESOURCE_TYPE, "Control"))); ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::BOOL, "forward_spatial_gui_input", PropertyInfo(Variant::OBJECT, "camera", PROPERTY_HINT_RESOURCE_TYPE, "Camera3D"), PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent"))); + ClassDB::add_virtual_method(get_class_static(), MethodInfo("forward_spatial_draw_over_viewport", PropertyInfo(Variant::OBJECT, "overlay", PROPERTY_HINT_RESOURCE_TYPE, "Control"))); + ClassDB::add_virtual_method(get_class_static(), MethodInfo("forward_spatial_force_draw_over_viewport", PropertyInfo(Variant::OBJECT, "overlay", PROPERTY_HINT_RESOURCE_TYPE, "Control"))); ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::STRING, "get_plugin_name")); ClassDB::add_virtual_method(get_class_static(), MethodInfo(PropertyInfo(Variant::OBJECT, "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "get_plugin_icon")); ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::BOOL, "has_main_screen")); diff --git a/editor/editor_properties.cpp b/editor/editor_properties.cpp index 9e7ddd9fac..1443302f3f 100644 --- a/editor/editor_properties.cpp +++ b/editor/editor_properties.cpp @@ -3645,7 +3645,7 @@ bool EditorInspectorDefaultPlugin::parse_property(Object *p_object, Variant::Typ add_property_editor(p_path, editor); } break; - case Variant::_RID: { + case Variant::RID: { EditorPropertyRID *editor = memnew(EditorPropertyRID); add_property_editor(p_path, editor); } break; diff --git a/editor/editor_properties_array_dict.cpp b/editor/editor_properties_array_dict.cpp index 51fac6acec..56fbfbd0c2 100644 --- a/editor/editor_properties_array_dict.cpp +++ b/editor/editor_properties_array_dict.cpp @@ -186,7 +186,7 @@ void EditorPropertyArray::_change_type_menu(int p_index) { Variant value; Callable::CallError ce; - value = Variant::construct(Variant::Type(p_index), nullptr, 0, ce); + Variant::construct(Variant::Type(p_index), value, nullptr, 0, ce); Variant array = object->get_array(); array.set(changing_type_idx, value); @@ -445,7 +445,7 @@ void EditorPropertyArray::drop_data_fw(const Point2 &p_point, const Variant &p_d // Handle the case where array is not initialised yet if (!array.is_array()) { Callable::CallError ce; - array = Variant::construct(array_type, nullptr, 0, ce); + Variant::construct(array_type, array, nullptr, 0, ce); } // Loop the file array and add to existing array @@ -491,7 +491,7 @@ void EditorPropertyArray::_edit_pressed() { Variant array = get_edited_object()->get(get_edited_property()); if (!array.is_array()) { Callable::CallError ce; - array = Variant::construct(array_type, nullptr, 0, ce); + Variant::construct(array_type, array, nullptr, 0, ce); get_edited_object()->set(get_edited_property(), array); } @@ -524,7 +524,9 @@ void EditorPropertyArray::_length_changed(double p_page) { for (int i = previous_size; i < size; i++) { if (array.get(i).get_type() == Variant::NIL) { Callable::CallError ce; - array.set(i, Variant::construct(subtype, nullptr, 0, ce)); + Variant r; + Variant::construct(subtype, r, nullptr, 0, ce); + array.set(i, r); } } } @@ -534,7 +536,9 @@ void EditorPropertyArray::_length_changed(double p_page) { // Pool*Array don't initialize their elements, have to do it manually for (int i = previous_size; i < size; i++) { Callable::CallError ce; - array.set(i, Variant::construct(array.get(i).get_type(), nullptr, 0, ce)); + Variant r; + Variant::construct(array.get(i).get_type(), r, nullptr, 0, ce); + array.set(i, r); } } @@ -657,7 +661,7 @@ void EditorPropertyDictionary::_change_type_menu(int p_index) { if (changing_type_idx < 0) { Variant value; Callable::CallError ce; - value = Variant::construct(Variant::Type(p_index), nullptr, 0, ce); + Variant::construct(Variant::Type(p_index), value, nullptr, 0, ce); if (changing_type_idx == -1) { object->set_new_item_key(value); } else { @@ -672,7 +676,7 @@ void EditorPropertyDictionary::_change_type_menu(int p_index) { if (p_index < Variant::VARIANT_MAX) { Variant value; Callable::CallError ce; - value = Variant::construct(Variant::Type(p_index), nullptr, 0, ce); + Variant::construct(Variant::Type(p_index), value, nullptr, 0, ce); Variant key = dict.get_key_at_index(changing_type_idx); dict[key] = value; } else { @@ -888,7 +892,7 @@ void EditorPropertyDictionary::update_property() { prop = memnew(EditorPropertyNodePath); } break; - case Variant::_RID: { + case Variant::RID: { prop = memnew(EditorPropertyRID); } break; @@ -1044,7 +1048,7 @@ void EditorPropertyDictionary::_edit_pressed() { Variant prop_val = get_edited_object()->get(get_edited_property()); if (prop_val.get_type() == Variant::NIL) { Callable::CallError ce; - prop_val = Variant::construct(Variant::DICTIONARY, nullptr, 0, ce); + Variant::construct(Variant::DICTIONARY, prop_val, nullptr, 0, ce); get_edited_object()->set(get_edited_property(), prop_val); } diff --git a/editor/filesystem_dock.cpp b/editor/filesystem_dock.cpp index 2052319e3e..ee0ee91893 100644 --- a/editor/filesystem_dock.cpp +++ b/editor/filesystem_dock.cpp @@ -2665,7 +2665,8 @@ FileSystemDock::FileSystemDock(EditorNode *p_editor) { editor = p_editor; path = "res://"; - ED_SHORTCUT("filesystem_dock/copy_path", TTR("Copy Path"), KEY_MASK_CMD | KEY_C); + // `KEY_MASK_CMD | KEY_C` conflicts with other editor shortcuts. + ED_SHORTCUT("filesystem_dock/copy_path", TTR("Copy Path"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_C); ED_SHORTCUT("filesystem_dock/duplicate", TTR("Duplicate..."), KEY_MASK_CMD | KEY_D); ED_SHORTCUT("filesystem_dock/delete", TTR("Delete"), KEY_DELETE); ED_SHORTCUT("filesystem_dock/rename", TTR("Rename")); diff --git a/editor/icons/CanvasGroup.svg b/editor/icons/CanvasGroup.svg new file mode 100644 index 0000000000..232ae53231 --- /dev/null +++ b/editor/icons/CanvasGroup.svg @@ -0,0 +1 @@ +<svg height="16" viewBox="0 0 16 16" width="16" xmlns="http://www.w3.org/2000/svg"><path d="m7 1v6h-6v8h8v-6h6v-8zm2 2h4v4h-4z" fill="#a5b8f3" fill-opacity=".588235"/><path d="m1 1v2c0 .0000234.446 0 1 0s1 .0000234 1 0v-2c0-.00002341-.446 0-1 0s-1-.00002341-1 0zm12 0v2c0 .0000234.446 0 1 0s1 .0000234 1 0v-2c0-.00002341-.446 0-1 0s-1-.00002341-1 0zm-12 12v2c0 .000023.446 0 1 0s1 .000023 1 0v-2c0-.000023-.446 0-1 0s-1-.000023-1 0zm12 0v2c0 .000023.446 0 1 0s1 .000023 1 0v-2c0-.000023-.446 0-1 0s-1-.000023-1 0z" fill="#a5b7f4"/></svg> diff --git a/editor/icons/CodeEdit.svg b/editor/icons/CodeEdit.svg new file mode 100644 index 0000000000..0750b072e7 --- /dev/null +++ b/editor/icons/CodeEdit.svg @@ -0,0 +1 @@ +<svg height="16" viewBox="0 0 16 16" width="16" xmlns="http://www.w3.org/2000/svg"><g transform="translate(0 -1036.4)"><path d="m29 1042.4h1v1h-1z" fill="#fefeff"/><path d="m3 1c-1.1046 0-2 .8954-2 2v10c0 1.1046.89543 2 2 2h10c1.1046 0 2-.8954 2-2v-10c0-1.1046-.89543-2-2-2zm0 2h10v10h-10zm2 1-1 1 1 1-1 1 1 1 2-2zm2 3v1h2v-1z" fill="#a5efac" transform="translate(0 1036.4)"/></g></svg> diff --git a/editor/plugins/node_3d_editor_plugin.cpp b/editor/plugins/node_3d_editor_plugin.cpp index 2bc4e4fbb0..28acb26012 100644 --- a/editor/plugins/node_3d_editor_plugin.cpp +++ b/editor/plugins/node_3d_editor_plugin.cpp @@ -2426,6 +2426,7 @@ void Node3DEditorViewport::_notification(int p_what) { t.basis = t.basis * aabb_s; RenderingServer::get_singleton()->instance_set_transform(se->sbox_instance, t); + RenderingServer::get_singleton()->instance_set_transform(se->sbox_instance_xray, t); } if (changed || (spatial_editor->is_gizmo_visible() && !exist)) { @@ -4417,6 +4418,9 @@ Node3DEditorSelectedItem::~Node3DEditorSelectedItem() { if (sbox_instance.is_valid()) { RenderingServer::get_singleton()->free(sbox_instance); } + if (sbox_instance_xray.is_valid()) { + RenderingServer::get_singleton()->free(sbox_instance_xray); + } } void Node3DEditor::select_gizmo_highlight_axis(int p_axis) { @@ -4500,42 +4504,73 @@ Object *Node3DEditor::_get_editor_data(Object *p_what) { Node3DEditorSelectedItem *si = memnew(Node3DEditorSelectedItem); si->sp = sp; - si->sbox_instance = RenderingServer::get_singleton()->instance_create2(selection_box->get_rid(), sp->get_world_3d()->get_scenario()); - RS::get_singleton()->instance_geometry_set_cast_shadows_setting(si->sbox_instance, RS::SHADOW_CASTING_SETTING_OFF); + si->sbox_instance = RenderingServer::get_singleton()->instance_create2( + selection_box->get_rid(), + sp->get_world_3d()->get_scenario()); + RS::get_singleton()->instance_geometry_set_cast_shadows_setting( + si->sbox_instance, + RS::SHADOW_CASTING_SETTING_OFF); + si->sbox_instance_xray = RenderingServer::get_singleton()->instance_create2( + selection_box_xray->get_rid(), + sp->get_world_3d()->get_scenario()); + RS::get_singleton()->instance_geometry_set_cast_shadows_setting( + si->sbox_instance_xray, + RS::SHADOW_CASTING_SETTING_OFF); return si; } -void Node3DEditor::_generate_selection_box() { +void Node3DEditor::_generate_selection_boxes() { + // Use two AABBs to create the illusion of a slightly thicker line. AABB aabb(Vector3(), Vector3(1, 1, 1)); - aabb.grow_by(aabb.get_longest_axis_size() / 20.0); - + AABB aabb_offset(Vector3(), Vector3(1, 1, 1)); + // Grow the bounding boxes slightly to avoid Z-fighting with the mesh's edges. + aabb.grow_by(0.005); + aabb_offset.grow_by(0.01); + + // Create a x-ray (visible through solid surfaces) and standard version of the selection box. + // Both will be drawn at the same position, but with different opacity. + // This lets the user see where the selection is while still having a sense of depth. Ref<SurfaceTool> st = memnew(SurfaceTool); + Ref<SurfaceTool> st_xray = memnew(SurfaceTool); st->begin(Mesh::PRIMITIVE_LINES); + st_xray->begin(Mesh::PRIMITIVE_LINES); for (int i = 0; i < 12; i++) { Vector3 a, b; aabb.get_edge(i, a, b); - st->add_color(Color(1.0, 1.0, 0.8, 0.8)); st->add_vertex(a); - st->add_color(Color(1.0, 1.0, 0.8, 0.4)); - st->add_vertex(a.lerp(b, 0.2)); + st->add_vertex(b); + st_xray->add_vertex(a); + st_xray->add_vertex(b); + } - st->add_color(Color(1.0, 1.0, 0.8, 0.4)); - st->add_vertex(a.lerp(b, 0.8)); - st->add_color(Color(1.0, 1.0, 0.8, 0.8)); + for (int i = 0; i < 12; i++) { + Vector3 a, b; + aabb_offset.get_edge(i, a, b); + + st->add_vertex(a); st->add_vertex(b); + st_xray->add_vertex(a); + st_xray->add_vertex(b); } Ref<StandardMaterial3D> mat = memnew(StandardMaterial3D); mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED); - mat->set_albedo(Color(1, 1, 1)); + // Use a similar color to the 2D editor selection. + mat->set_albedo(Color(1, 0.5, 0)); mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA); - mat->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true); - mat->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true); st->set_material(mat); selection_box = st->commit(); + + Ref<StandardMaterial3D> mat_xray = memnew(StandardMaterial3D); + mat_xray->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED); + mat_xray->set_flag(StandardMaterial3D::FLAG_DISABLE_DEPTH_TEST, true); + mat_xray->set_albedo(Color(1, 0.5, 0, 0.15)); + mat_xray->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA); + st_xray->set_material(mat_xray); + selection_box_xray = st_xray->commit(); } Dictionary Node3DEditor::get_state() const { @@ -5514,7 +5549,7 @@ void Node3DEditor::_init_indicators() { } } - _generate_selection_box(); + _generate_selection_boxes(); } void Node3DEditor::_update_gizmos_menu() { diff --git a/editor/plugins/node_3d_editor_plugin.h b/editor/plugins/node_3d_editor_plugin.h index d0723904ae..07f6d69d76 100644 --- a/editor/plugins/node_3d_editor_plugin.h +++ b/editor/plugins/node_3d_editor_plugin.h @@ -498,6 +498,7 @@ public: bool last_xform_dirty; Node3D *sp; RID sbox_instance; + RID sbox_instance_xray; Node3DEditorSelectedItem() { sp = nullptr; @@ -613,6 +614,7 @@ private: float snap_rotate_value; float snap_scale_value; + Ref<ArrayMesh> selection_box_xray; Ref<ArrayMesh> selection_box; RID indicators; RID indicators_instance; @@ -701,7 +703,7 @@ private: HBoxContainer *hbc_menu; - void _generate_selection_box(); + void _generate_selection_boxes(); UndoRedo *undo_redo; int camera_override_viewport_id; diff --git a/editor/project_settings_editor.cpp b/editor/project_settings_editor.cpp index d231e3f353..55d80021c8 100644 --- a/editor/project_settings_editor.cpp +++ b/editor/project_settings_editor.cpp @@ -98,7 +98,8 @@ void ProjectSettingsEditor::_add_setting() { // Initialize the property with the default value for the given type. // The type list starts at 1 (as we exclude Nil), so add 1 to the selected value. Callable::CallError ce; - const Variant value = Variant::construct(Variant::Type(type->get_selected() + 1), nullptr, 0, ce); + Variant value; + Variant::construct(Variant::Type(type->get_selected() + 1), value, nullptr, 0, ce); undo_redo->create_action(TTR("Add Project Setting")); undo_redo->add_do_property(ps, setting, value); diff --git a/editor/property_editor.cpp b/editor/property_editor.cpp index b6bd6345df..1e4ed0c552 100644 --- a/editor/property_editor.cpp +++ b/editor/property_editor.cpp @@ -345,7 +345,7 @@ bool CustomPropertyEditor::edit(Object *p_owner, const String &p_name, Variant:: checks20[i]->hide(); } - type = (p_variant.get_type() != Variant::NIL && p_variant.get_type() != Variant::_RID && p_type != Variant::OBJECT) ? p_variant.get_type() : p_type; + type = (p_variant.get_type() != Variant::NIL && p_variant.get_type() != Variant::RID && p_type != Variant::OBJECT) ? p_variant.get_type() : p_type; switch (type) { case Variant::BOOL: { diff --git a/editor/property_selector.cpp b/editor/property_selector.cpp index 27b11e4fb5..75420a1ef4 100644 --- a/editor/property_selector.cpp +++ b/editor/property_selector.cpp @@ -95,7 +95,7 @@ void PropertySelector::_update_search() { } else if (type != Variant::NIL) { Variant v; Callable::CallError ce; - v = Variant::construct(type, nullptr, 0, ce); + Variant::construct(type, v, nullptr, 0, ce); v.get_property_list(&props); } else { @@ -200,7 +200,7 @@ void PropertySelector::_update_search() { if (type != Variant::NIL) { Variant v; Callable::CallError ce; - v = Variant::construct(type, nullptr, 0, ce); + Variant::construct(type, v, nullptr, 0, ce); v.get_method_list(&methods); } else { Object *obj = ObjectDB::get_instance(script); diff --git a/main/main.cpp b/main/main.cpp index 0667180c28..3905366598 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -1608,7 +1608,7 @@ Error Main::setup2(Thread::ID p_main_tid_override) { GLOBAL_DEF("application/config/icon", String()); ProjectSettings::get_singleton()->set_custom_property_info("application/config/icon", PropertyInfo(Variant::STRING, "application/config/icon", - PROPERTY_HINT_FILE, "*.png,*.webp")); + PROPERTY_HINT_FILE, "*.png,*.webp,*.svg,*.svgz")); GLOBAL_DEF("application/config/macos_native_icon", String()); ProjectSettings::get_singleton()->set_custom_property_info("application/config/macos_native_icon", diff --git a/misc/dist/ios_xcode/godot_ios.xcodeproj/project.pbxproj b/misc/dist/ios_xcode/godot_ios.xcodeproj/project.pbxproj index d8da520ed7..bd21883259 100644 --- a/misc/dist/ios_xcode/godot_ios.xcodeproj/project.pbxproj +++ b/misc/dist/ios_xcode/godot_ios.xcodeproj/project.pbxproj @@ -153,81 +153,6 @@ DevelopmentTeam = $team_id; ProvisioningStyle = Automatic; SystemCapabilities = { - com.apple.AccessWiFi = { - enabled = $access_wifi; - }; - com.apple.ApplePay = { - enabled = 0; - }; - com.apple.ApplicationGroups.iOS = { - enabled = 0; - }; - com.apple.AutoFillCredentialProvider = { - enabled = 0; - }; - com.apple.BackgroundModes = { - enabled = 0; - }; - com.apple.ClassKit = { - enabled = 0; - }; - com.apple.DataProtection = { - enabled = 0; - }; - com.apple.GameCenter.iOS = { - enabled = $game_center; - }; - com.apple.HealthKit = { - enabled = 0; - }; - com.apple.HomeKit = { - enabled = 0; - }; - com.apple.HotspotConfiguration = { - enabled = 0; - }; - com.apple.InAppPurchase = { - enabled = $in_app_purchases; - }; - com.apple.InterAppAudio = { - enabled = 0; - }; - com.apple.Keychain = { - enabled = 0; - }; - com.apple.Maps.iOS = { - enabled = 0; - }; - com.apple.Multipath = { - enabled = 0; - }; - com.apple.NearFieldCommunicationTagReading = { - enabled = 0; - }; - com.apple.NetworkExtensions.iOS = { - enabled = 0; - }; - com.apple.Push = { - enabled = $push_notifications; - }; - com.apple.SafariKeychain = { - enabled = 0; - }; - com.apple.Siri = { - enabled = 0; - }; - com.apple.VPNLite = { - enabled = 0; - }; - com.apple.WAC = { - enabled = 0; - }; - com.apple.Wallet = { - enabled = 0; - }; - com.apple.iCloud = { - enabled = 0; - }; }; }; }; @@ -393,7 +318,7 @@ ); LIBRARY_SEARCH_PATHS = ( "$(inherited)", - "$(PROJECT_DIR)", + "$(PROJECT_DIR)/**", ); PRODUCT_BUNDLE_IDENTIFIER = $bundle_identifier; PRODUCT_NAME = "$(TARGET_NAME)"; @@ -423,7 +348,7 @@ ); LIBRARY_SEARCH_PATHS = ( "$(inherited)", - "$(PROJECT_DIR)", + "$(PROJECT_DIR)/**", ); PRODUCT_BUNDLE_IDENTIFIER = $bundle_identifier; PRODUCT_NAME = "$(TARGET_NAME)"; diff --git a/modules/SCsub b/modules/SCsub index edfc4ed9c6..24598f4b28 100644 --- a/modules/SCsub +++ b/modules/SCsub @@ -47,7 +47,14 @@ for name, path in env.module_list.items(): # Some modules are not linked automatically but can be enabled optionally # on iOS, so we handle those specially. - if env["platform"] == "iphone" and name in ["arkit", "camera"]: + if env["platform"] == "iphone" and name in [ + "arkit", + "camera", + "camera_iphone", + "gamecenter", + "inappstore", + "icloud", + ]: continue lib = env_modules.add_library("module_%s" % name, env.modules_sources) diff --git a/modules/arkit/arkit.gdip b/modules/arkit/arkit.gdip new file mode 100644 index 0000000000..22c0a07e26 --- /dev/null +++ b/modules/arkit/arkit.gdip @@ -0,0 +1,18 @@ +[config] +name="ARKit" +binary="arkit_lib.a" + +initialization="register_arkit_types" +deinitialization="unregister_arkit_types" + +[dependencies] +linked=[] +embedded=[] +system=["AVFoundation.framework", "ARKit.framework"] + +capabilities=["arkit"] + +files=[] + +[plist] +NSCameraUsageDescription="Device camera is used for some functionality" diff --git a/modules/arkit/register_types.cpp b/modules/arkit/arkit_module.cpp index 91069ab364..87ee3b87a5 100644 --- a/modules/arkit/register_types.cpp +++ b/modules/arkit/arkit_module.cpp @@ -1,5 +1,5 @@ /*************************************************************************/ -/* register_types.cpp */ +/* arkit_module.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ @@ -28,7 +28,7 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#include "register_types.h" +#include "arkit_module.h" #include "arkit_interface.h" diff --git a/modules/arkit/register_types.h b/modules/arkit/arkit_module.h index f8939a1e3f..8aa8175ed5 100644 --- a/modules/arkit/register_types.h +++ b/modules/arkit/arkit_module.h @@ -1,5 +1,5 @@ /*************************************************************************/ -/* register_types.h */ +/* arkit_module.h */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ diff --git a/modules/camera/SCsub b/modules/camera/SCsub index 631a65bde2..de97724d09 100644 --- a/modules/camera/SCsub +++ b/modules/camera/SCsub @@ -5,17 +5,7 @@ Import("env_modules") env_camera = env_modules.Clone() -if env["platform"] == "iphone": - # (iOS) Enable module support - env_camera.Append(CCFLAGS=["-fmodules", "-fcxx-modules"]) - - # (iOS) Build as separate static library - modules_sources = [] - env_camera.add_source_files(modules_sources, "register_types.cpp") - env_camera.add_source_files(modules_sources, "camera_ios.mm") - mod_lib = env_modules.add_library("#bin/libgodot_camera_module" + env["LIBSUFFIX"], modules_sources) - -elif env["platform"] == "windows": +if env["platform"] == "windows": env_camera.add_source_files(env.modules_sources, "register_types.cpp") env_camera.add_source_files(env.modules_sources, "camera_win.cpp") diff --git a/modules/camera/config.py b/modules/camera/config.py index 87d7542741..8a22751aa7 100644 --- a/modules/camera/config.py +++ b/modules/camera/config.py @@ -1,5 +1,5 @@ def can_build(env, platform): - return platform == "iphone" or platform == "osx" or platform == "windows" + return platform == "osx" or platform == "windows" def configure(env): diff --git a/modules/camera/register_types.cpp b/modules/camera/register_types.cpp index 3b6c916914..9479310a13 100644 --- a/modules/camera/register_types.cpp +++ b/modules/camera/register_types.cpp @@ -33,9 +33,6 @@ #if defined(WINDOWS_ENABLED) #include "camera_win.h" #endif -#if defined(IPHONE_ENABLED) -#include "camera_ios.h" -#endif #if defined(OSX_ENABLED) #include "camera_osx.h" #endif @@ -44,9 +41,6 @@ void register_camera_types() { #if defined(WINDOWS_ENABLED) CameraServer::make_default<CameraWindows>(); #endif -#if defined(IPHONE_ENABLED) - CameraServer::make_default<CameraIOS>(); -#endif #if defined(OSX_ENABLED) CameraServer::make_default<CameraOSX>(); #endif diff --git a/modules/camera_iphone/SCsub b/modules/camera_iphone/SCsub new file mode 100644 index 0000000000..0a37d9a6f5 --- /dev/null +++ b/modules/camera_iphone/SCsub @@ -0,0 +1,15 @@ +#!/usr/bin/env python + +Import("env") +Import("env_modules") + +env_camera = env_modules.Clone() + +# (iOS) Enable module support +env_camera.Append(CCFLAGS=["-fmodules", "-fcxx-modules"]) + +# (iOS) Build as separate static library +modules_sources = [] +env_camera.add_source_files(modules_sources, "*.cpp") +env_camera.add_source_files(modules_sources, "*.mm") +mod_lib = env_modules.add_library("#bin/libgodot_camera_module" + env["LIBSUFFIX"], modules_sources) diff --git a/modules/camera_iphone/camera.gdip b/modules/camera_iphone/camera.gdip new file mode 100644 index 0000000000..09017b8d27 --- /dev/null +++ b/modules/camera_iphone/camera.gdip @@ -0,0 +1,18 @@ +[config] +name="Camera" +binary="camera_lib.a" + +initialization="register_camera_types" +deinitialization="unregister_camera_types" + +[dependencies] +linked=[] +embedded=[] +system=["AVFoundation.framework"] + +capabilities=[] + +files=[] + +[plist] +NSCameraUsageDescription="Device camera is used for some functionality" diff --git a/modules/camera/camera_ios.h b/modules/camera_iphone/camera_ios.h index 7da43e4851..7da43e4851 100644 --- a/modules/camera/camera_ios.h +++ b/modules/camera_iphone/camera_ios.h diff --git a/modules/camera/camera_ios.mm b/modules/camera_iphone/camera_ios.mm index e4cb928805..e4cb928805 100644 --- a/modules/camera/camera_ios.mm +++ b/modules/camera_iphone/camera_ios.mm diff --git a/modules/camera_iphone/camera_module.cpp b/modules/camera_iphone/camera_module.cpp new file mode 100644 index 0000000000..f3d00be204 --- /dev/null +++ b/modules/camera_iphone/camera_module.cpp @@ -0,0 +1,40 @@ +/*************************************************************************/ +/* camera_module.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 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. */ +/*************************************************************************/ + +#include "camera_module.h" + +#include "camera_ios.h" + +void register_camera_types() { + CameraServer::make_default<CameraIOS>(); +} + +void unregister_camera_types() { +} diff --git a/modules/camera_iphone/camera_module.h b/modules/camera_iphone/camera_module.h new file mode 100644 index 0000000000..d123071a70 --- /dev/null +++ b/modules/camera_iphone/camera_module.h @@ -0,0 +1,32 @@ +/*************************************************************************/ +/* camera_module.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 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. */ +/*************************************************************************/ + +void register_camera_types(); +void unregister_camera_types(); diff --git a/modules/camera_iphone/config.py b/modules/camera_iphone/config.py new file mode 100644 index 0000000000..e68603fc93 --- /dev/null +++ b/modules/camera_iphone/config.py @@ -0,0 +1,6 @@ +def can_build(env, platform): + return platform == "iphone" + + +def configure(env): + pass diff --git a/modules/gamecenter/SCsub b/modules/gamecenter/SCsub new file mode 100644 index 0000000000..72fbf7ab0e --- /dev/null +++ b/modules/gamecenter/SCsub @@ -0,0 +1,15 @@ +#!/usr/bin/env python + +Import("env") +Import("env_modules") + +env_gamecenter = env_modules.Clone() + +# (iOS) Enable module support +env_gamecenter.Append(CCFLAGS=["-fmodules", "-fcxx-modules"]) + +# (iOS) Build as separate static library +modules_sources = [] +env_gamecenter.add_source_files(modules_sources, "*.cpp") +env_gamecenter.add_source_files(modules_sources, "*.mm") +mod_lib = env_modules.add_library("#bin/libgodot_gamecenter_module" + env["LIBSUFFIX"], modules_sources) diff --git a/modules/gamecenter/config.py b/modules/gamecenter/config.py new file mode 100644 index 0000000000..e68603fc93 --- /dev/null +++ b/modules/gamecenter/config.py @@ -0,0 +1,6 @@ +def can_build(env, platform): + return platform == "iphone" + + +def configure(env): + pass diff --git a/platform/iphone/game_center.h b/modules/gamecenter/game_center.h index cbfd616e56..76fd295460 100644 --- a/platform/iphone/game_center.h +++ b/modules/gamecenter/game_center.h @@ -28,8 +28,6 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#ifdef GAME_CENTER_ENABLED - #ifndef GAME_CENTER_H #define GAME_CENTER_H @@ -71,5 +69,3 @@ public: }; #endif - -#endif diff --git a/platform/iphone/game_center.mm b/modules/gamecenter/game_center.mm index 0f8c0100c3..114f639a32 100644 --- a/platform/iphone/game_center.mm +++ b/modules/gamecenter/game_center.mm @@ -28,28 +28,15 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#ifdef GAME_CENTER_ENABLED - #include "game_center.h" +#import "platform/iphone/app_delegate.h" -#ifdef __IPHONE_9_0 - -#import <GameKit/GameKit.h> -extern "C" { - -#else - -extern "C" { +#import "game_center_delegate.h" +#import "platform/iphone/view_controller.h" #import <GameKit/GameKit.h> -#endif - -#import "app_delegate.h" -}; - -#import "view_controller.h" - GameCenter *GameCenter::instance = NULL; +GodotGameCenterDelegate *gameCenterDelegate = nil; void GameCenter::_bind_methods() { ClassDB::bind_method(D_METHOD("authenticate"), &GameCenter::authenticate); @@ -76,7 +63,7 @@ Error GameCenter::authenticate() { GKLocalPlayer *player = [GKLocalPlayer localPlayer]; ERR_FAIL_COND_V(![player respondsToSelector:@selector(authenticateHandler)], ERR_UNAVAILABLE); - ViewController *root_controller = (ViewController *)((AppDelegate *)[[UIApplication sharedApplication] delegate]).window.rootViewController; + UIViewController *root_controller = [[UIApplication sharedApplication] delegate].window.rootViewController; ERR_FAIL_COND_V(!root_controller, FAILED); // This handler is called several times. First when the view needs to be shown, then again @@ -305,10 +292,10 @@ Error GameCenter::show_game_center(Dictionary p_params) { GKGameCenterViewController *controller = [[GKGameCenterViewController alloc] init]; ERR_FAIL_COND_V(!controller, FAILED); - ViewController *root_controller = (ViewController *)((AppDelegate *)[[UIApplication sharedApplication] delegate]).window.rootViewController; + UIViewController *root_controller = [[UIApplication sharedApplication] delegate].window.rootViewController; ERR_FAIL_COND_V(!root_controller, FAILED); - controller.gameCenterDelegate = root_controller; + controller.gameCenterDelegate = gameCenterDelegate; controller.viewState = view_state; if (view_state == GKGameCenterViewControllerStateLeaderboards) { controller.leaderboardIdentifier = nil; @@ -382,8 +369,12 @@ GameCenter::GameCenter() { ERR_FAIL_COND(instance != NULL); instance = this; authenticated = false; -}; -GameCenter::~GameCenter() {} + gameCenterDelegate = [[GodotGameCenterDelegate alloc] init]; +}; -#endif +GameCenter::~GameCenter() { + if (gameCenterDelegate) { + gameCenterDelegate = nil; + } +} diff --git a/modules/gamecenter/game_center_delegate.h b/modules/gamecenter/game_center_delegate.h new file mode 100644 index 0000000000..1b7025f915 --- /dev/null +++ b/modules/gamecenter/game_center_delegate.h @@ -0,0 +1,35 @@ +/*************************************************************************/ +/* game_center_delegate.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 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. */ +/*************************************************************************/ + +#import <GameKit/GameKit.h> + +@interface GodotGameCenterDelegate : NSObject <GKGameCenterControllerDelegate> + +@end diff --git a/modules/gamecenter/game_center_delegate.mm b/modules/gamecenter/game_center_delegate.mm new file mode 100644 index 0000000000..9a10c439c6 --- /dev/null +++ b/modules/gamecenter/game_center_delegate.mm @@ -0,0 +1,45 @@ +/*************************************************************************/ +/* game_center_delegate.mm */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 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. */ +/*************************************************************************/ + +#import "game_center_delegate.h" + +#include "game_center.h" + +@implementation GodotGameCenterDelegate + +- (void)gameCenterViewControllerDidFinish:(GKGameCenterViewController *)gameCenterViewController { + //[gameCenterViewController dismissViewControllerAnimated:YES completion:^{GameCenter::get_singleton()->game_center_closed();}];//version for signaling when overlay is completely gone + if (GameCenter::get_singleton()) { + GameCenter::get_singleton()->game_center_closed(); + } + [gameCenterViewController dismissViewControllerAnimated:YES completion:nil]; +} + +@end diff --git a/platform/javascript/native/id_handler.js b/modules/gamecenter/game_center_module.cpp index 67d29075b8..6c5157345f 100644 --- a/platform/javascript/native/id_handler.js +++ b/modules/gamecenter/game_center_module.cpp @@ -1,5 +1,5 @@ /*************************************************************************/ -/* id_handler.js */ +/* game_center_module.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ @@ -28,36 +28,21 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -var IDHandler = /** @constructor */ function() { +#include "game_center_module.h" - var ids = {}; - var size = 0; +#include "core/config/engine.h" - this.has = function(id) { - return ids.hasOwnProperty(id); - } +#include "game_center.h" - this.add = function(obj) { - size += 1; - var id = crypto.getRandomValues(new Int32Array(32))[0]; - ids[id] = obj; - return id; - } +GameCenter *game_center; - this.get = function(id) { - return ids[id]; - } +void register_gamecenter_types() { + game_center = memnew(GameCenter); + Engine::get_singleton()->add_singleton(Engine::Singleton("GameCenter", game_center)); +} - this.remove = function(id) { - size -= 1; - delete ids[id]; +void unregister_gamecenter_types() { + if (game_center) { + memdelete(game_center); } - - this.size = function() { - return size; - } - - this.ids = ids; -}; - -Module.IDHandler = new IDHandler; +} diff --git a/modules/gamecenter/game_center_module.h b/modules/gamecenter/game_center_module.h new file mode 100644 index 0000000000..8da3ae02ee --- /dev/null +++ b/modules/gamecenter/game_center_module.h @@ -0,0 +1,32 @@ +/*************************************************************************/ +/* game_center_module.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 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. */ +/*************************************************************************/ + +void register_gamecenter_types(); +void unregister_gamecenter_types(); diff --git a/modules/gamecenter/gamecenter.gdip b/modules/gamecenter/gamecenter.gdip new file mode 100644 index 0000000000..eb44effbdd --- /dev/null +++ b/modules/gamecenter/gamecenter.gdip @@ -0,0 +1,17 @@ +[config] +name="GameCenter" +binary="gamecenter_lib.a" + +initialization="register_gamecenter_types" +deinitialization="unregister_gamecenter_types" + +[dependencies] +linked=[] +embedded=[] +system=["GameKit.framework"] + +capabilities=["gamekit"] + +files=[] + +[plist] diff --git a/modules/gdnative/gdnative/variant.cpp b/modules/gdnative/gdnative/variant.cpp index 8e30eaae4d..417abeaad3 100644 --- a/modules/gdnative/gdnative/variant.cpp +++ b/modules/gdnative/gdnative/variant.cpp @@ -576,7 +576,9 @@ godot_variant GDAPI godot_variant_call(godot_variant *p_self, const godot_string godot_variant raw_dest; Variant *dest = (Variant *)&raw_dest; Callable::CallError error; - memnew_placement_custom(dest, Variant, Variant(self->call(*method, args, p_argcount, error))); + Variant ret; + self->call(*method, args, p_argcount, ret, error); + memnew_placement_custom(dest, Variant, Variant(ret)); if (r_error) { r_error->error = (godot_variant_call_error_error)error.error; r_error->argument = error.argument; diff --git a/modules/gdnative/include/gdnative/string.h b/modules/gdnative/include/gdnative/string.h index 0582d95f63..6043351e84 100644 --- a/modules/gdnative/include/gdnative/string.h +++ b/modules/gdnative/include/gdnative/string.h @@ -35,8 +35,13 @@ extern "C" { #endif +#include <stddef.h> #include <stdint.h> -#include <wchar.h> + +#ifndef __cplusplus +typedef uint32_t char32_t; +typedef uint16_t char16_t; +#endif typedef char32_t godot_char_type; diff --git a/modules/gdnative/net/webrtc_gdnative.cpp b/modules/gdnative/net/webrtc_gdnative.cpp index a7355e4d12..d8c3ddc5f8 100644 --- a/modules/gdnative/net/webrtc_gdnative.cpp +++ b/modules/gdnative/net/webrtc_gdnative.cpp @@ -54,7 +54,7 @@ godot_error GDAPI godot_net_set_webrtc_library(const godot_net_webrtc_library *p #ifdef WEBRTC_GDNATIVE_ENABLED return (godot_error)WebRTCPeerConnectionGDNative::set_default_library(p_lib); #else - return ERR_UNAVAILABLE; + return (godot_error)ERR_UNAVAILABLE; #endif } } diff --git a/modules/gdscript/doc_classes/@GDScript.xml b/modules/gdscript/doc_classes/@GDScript.xml index 95818e5fcf..d90b3e52d0 100644 --- a/modules/gdscript/doc_classes/@GDScript.xml +++ b/modules/gdscript/doc_classes/@GDScript.xml @@ -94,14 +94,15 @@ <argument index="1" name="message" type="String" default=""""> </argument> <description> - Asserts that the [code]condition[/code] is [code]true[/code]. If the [code]condition[/code] is [code]false[/code], an error is generated and the program is halted until you resume it. Only executes in debug builds, or when running the game from the editor. Use it for debugging purposes, to make sure a statement is [code]true[/code] during development. + Asserts that the [code]condition[/code] is [code]true[/code]. If the [code]condition[/code] is [code]false[/code], an error is generated. When running from the editor, the running project will also be paused until you resume it. This can be used as a stronger form of [method push_error] for reporting errors to project developers or add-on users. + [b]Note:[/b] For performance reasons, the code inside [method assert] is only executed in debug builds or when running the project from the editor. Don't include code that has side effects in an [method assert] call. Otherwise, the project will behave differently when exported in release mode. The optional [code]message[/code] argument, if given, is shown in addition to the generic "Assertion failed" message. You can use this to provide additional details about why the assertion failed. [codeblock] - # Imagine we always want speed to be between 0 and 20 - speed = -10 + # Imagine we always want speed to be between 0 and 20. + var speed = -10 assert(speed < 20) # True, the program will continue assert(speed >= 0) # False, the program will stop - assert(speed >= 0 && speed < 20) # You can also combine the two conditional statements in one check + assert(speed >= 0 and speed < 20) # You can also combine the two conditional statements in one check assert(speed < 20, "speed = %f, but the speed limit is 20" % speed) # Show a message with clarifying details [/codeblock] </description> @@ -386,24 +387,6 @@ [/codeblock] </description> </method> - <method name="funcref"> - <return type="FuncRef"> - </return> - <argument index="0" name="instance" type="Object"> - </argument> - <argument index="1" name="funcname" type="String"> - </argument> - <description> - Returns a reference to the specified function [code]funcname[/code] in the [code]instance[/code] node. As functions aren't first-class objects in GDscript, use [code]funcref[/code] to store a [FuncRef] in a variable and call it later. - [codeblock] - func foo(): - return("bar") - - a = funcref(self, "foo") - print(a.call_func()) # Prints bar - [/codeblock] - </description> - </method> <method name="get_stack"> <return type="Array"> </return> @@ -921,35 +904,6 @@ [/codeblock] </description> </method> - <method name="randf_range"> - <return type="float"> - </return> - <argument index="0" name="from" type="float"> - </argument> - <argument index="1" name="to" type="float"> - </argument> - <description> - Random range, any floating point value between [code]from[/code] and [code]to[/code]. - [codeblock] - prints(randf_range(-10, 10), randf_range(-10, 10)) # Prints e.g. -3.844535 7.45315 - [/codeblock] - </description> - </method> - <method name="randi_range"> - <return type="int"> - </return> - <argument index="0" name="from" type="int"> - </argument> - <argument index="1" name="to" type="int"> - </argument> - <description> - Random range, any 32-bit integer value between [code]from[/code] and [code]to[/code] (inclusive). If [code]to[/code] is lesser than [code]from[/code] they are swapped. - [codeblock] - print(randi_range(0, 1)) # Prints 0 or 1 - print(randi_range(-10, 1000)) # Prints any number from -10 to 1000 - [/codeblock] - </description> - </method> <method name="rand_seed"> <return type="Array"> </return> @@ -969,6 +923,20 @@ [/codeblock] </description> </method> + <method name="randf_range"> + <return type="float"> + </return> + <argument index="0" name="from" type="float"> + </argument> + <argument index="1" name="to" type="float"> + </argument> + <description> + Random range, any floating point value between [code]from[/code] and [code]to[/code]. + [codeblock] + prints(randf_range(-10, 10), randf_range(-10, 10)) # Prints e.g. -3.844535 7.45315 + [/codeblock] + </description> + </method> <method name="randi"> <return type="int"> </return> @@ -982,6 +950,21 @@ [/codeblock] </description> </method> + <method name="randi_range"> + <return type="int"> + </return> + <argument index="0" name="from" type="int"> + </argument> + <argument index="1" name="to" type="int"> + </argument> + <description> + Random range, any 32-bit integer value between [code]from[/code] and [code]to[/code] (inclusive). If [code]to[/code] is lesser than [code]from[/code] they are swapped. + [codeblock] + print(randi_range(0, 1)) # Prints 0 or 1 + print(randi_range(-10, 1000)) # Prints any number from -10 to 1000 + [/codeblock] + </description> + </method> <method name="randomize"> <return type="void"> </return> diff --git a/modules/gdscript/gdscript.cpp b/modules/gdscript/gdscript.cpp index a73268a79d..53602f7a9b 100644 --- a/modules/gdscript/gdscript.cpp +++ b/modules/gdscript/gdscript.cpp @@ -1092,7 +1092,8 @@ bool GDScriptInstance::set(const StringName &p_name, const Variant &p_value) { // Try conversion Callable::CallError ce; const Variant *value = &p_value; - Variant converted = Variant::construct(member->data_type.builtin_type, &value, 1, ce); + Variant converted; + Variant::construct(member->data_type.builtin_type, converted, &value, 1, ce); if (ce.error == Callable::CallError::CALL_OK) { members.write[member->index] = converted; return true; diff --git a/modules/gdscript/gdscript_analyzer.cpp b/modules/gdscript/gdscript_analyzer.cpp index a1de17b5a1..6b23ab1616 100644 --- a/modules/gdscript/gdscript_analyzer.cpp +++ b/modules/gdscript/gdscript_analyzer.cpp @@ -1711,7 +1711,8 @@ void GDScriptAnalyzer::reduce_call(GDScriptParser::CallNode *p_call, bool is_awa } Callable::CallError err; - Variant value = Variant::construct(builtin_type, (const Variant **)args.ptr(), args.size(), err); + Variant value; + Variant::construct(builtin_type, value, (const Variant **)args.ptr(), args.size(), err); switch (err.error) { case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT: @@ -2075,7 +2076,8 @@ void GDScriptAnalyzer::reduce_identifier_from_base(GDScriptParser::IdentifierNod } default: { Callable::CallError temp; - Variant dummy = Variant::construct(base.builtin_type, nullptr, 0, temp); + Variant dummy; + Variant::construct(base.builtin_type, dummy, nullptr, 0, temp); List<PropertyInfo> properties; dummy.get_property_list(&properties); for (const List<PropertyInfo>::Element *E = properties.front(); E != nullptr; E = E->next()) { @@ -2539,7 +2541,7 @@ void GDScriptAnalyzer::reduce_subscript(GDScriptParser::SubscriptNode *p_subscri error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::STRING; break; // Don't support indexing, but we will check it later. - case Variant::_RID: + case Variant::RID: case Variant::BOOL: case Variant::CALLABLE: case Variant::FLOAT: @@ -2572,7 +2574,7 @@ void GDScriptAnalyzer::reduce_subscript(GDScriptParser::SubscriptNode *p_subscri switch (base_type.builtin_type) { // Can't index at all. - case Variant::_RID: + case Variant::RID: case Variant::BOOL: case Variant::CALLABLE: case Variant::FLOAT: @@ -2869,7 +2871,8 @@ bool GDScriptAnalyzer::get_function_signature(GDScriptParser::Node *p_source, GD if (p_base_type.kind == GDScriptParser::DataType::BUILTIN) { // Construct a base type to get methods. Callable::CallError err; - Variant dummy = Variant::construct(p_base_type.builtin_type, nullptr, 0, err); + Variant dummy; + Variant::construct(p_base_type.builtin_type, dummy, nullptr, 0, err); if (err.error != Callable::CallError::CALL_OK) { ERR_FAIL_V_MSG(false, "Could not construct base Variant type."); } @@ -3095,7 +3098,7 @@ GDScriptParser::DataType GDScriptAnalyzer::get_operation_type(Variant::Operator a = a_ref; } else { Callable::CallError err; - a = Variant::construct(a_type, nullptr, 0, err); + Variant::construct(a_type, a, nullptr, 0, err); if (err.error != Callable::CallError::CALL_OK) { r_valid = false; ERR_FAIL_V_MSG(result, vformat("Could not construct value of type %s", Variant::get_type_name(a_type))); @@ -3108,7 +3111,7 @@ GDScriptParser::DataType GDScriptAnalyzer::get_operation_type(Variant::Operator b = b_ref; } else { Callable::CallError err; - b = Variant::construct(b_type, nullptr, 0, err); + Variant::construct(b_type, b, nullptr, 0, err); if (err.error != Callable::CallError::CALL_OK) { r_valid = false; ERR_FAIL_V_MSG(result, vformat("Could not construct value of type %s", Variant::get_type_name(b_type))); diff --git a/modules/gdscript/gdscript_editor.cpp b/modules/gdscript/gdscript_editor.cpp index 605883f6df..a426046797 100644 --- a/modules/gdscript/gdscript_editor.cpp +++ b/modules/gdscript/gdscript_editor.cpp @@ -973,7 +973,8 @@ static void _find_identifiers_in_base(const GDScriptCompletionIdentifier &p_base } break; case GDScriptParser::DataType::BUILTIN: { Callable::CallError err; - Variant tmp = Variant::construct(base_type.builtin_type, nullptr, 0, err); + Variant tmp; + Variant::construct(base_type.builtin_type, tmp, nullptr, 0, err); if (err.error != Callable::CallError::CALL_OK) { return; } @@ -1523,7 +1524,8 @@ static bool _guess_expression_type(GDScriptParser::CompletionContext &p_context, found = _guess_identifier_type_from_base(c, base, id, r_type); } else if (!found && index.type.kind == GDScriptParser::DataType::BUILTIN) { Callable::CallError err; - Variant base_val = Variant::construct(base.type.builtin_type, nullptr, 0, err); + Variant base_val; + Variant::construct(base.type.builtin_type, base_val, nullptr, 0, err); bool valid = false; Variant res = base_val.get(index.value, &valid); if (valid) { @@ -1560,9 +1562,14 @@ static bool _guess_expression_type(GDScriptParser::CompletionContext &p_context, Callable::CallError ce; bool v1_use_value = p1.value.get_type() != Variant::NIL && p1.value.get_type() != Variant::OBJECT; - Variant v1 = (v1_use_value) ? p1.value : Variant::construct(p1.type.builtin_type, nullptr, 0, ce); + Variant d1; + Variant::construct(p1.type.builtin_type, d1, nullptr, 0, ce); + Variant d2; + Variant::construct(p2.type.builtin_type, d2, nullptr, 0, ce); + + Variant v1 = (v1_use_value) ? p1.value : d1; bool v2_use_value = p2.value.get_type() != Variant::NIL && p2.value.get_type() != Variant::OBJECT; - Variant v2 = (v2_use_value) ? p2.value : Variant::construct(p2.type.builtin_type, nullptr, 0, ce); + Variant v2 = (v2_use_value) ? p2.value : d2; // avoid potential invalid ops if ((op->variant_op == Variant::OP_DIVIDE || op->variant_op == Variant::OP_MODULE) && v2.get_type() == Variant::INT) { v2 = 1; @@ -1952,7 +1959,8 @@ static bool _guess_identifier_type_from_base(GDScriptParser::CompletionContext & } break; case GDScriptParser::DataType::BUILTIN: { Callable::CallError err; - Variant tmp = Variant::construct(base_type.builtin_type, nullptr, 0, err); + Variant tmp; + Variant::construct(base_type.builtin_type, tmp, nullptr, 0, err); if (err.error != Callable::CallError::CALL_OK) { return false; @@ -2102,7 +2110,8 @@ static bool _guess_method_return_type_from_base(GDScriptParser::CompletionContex } break; case GDScriptParser::DataType::BUILTIN: { Callable::CallError err; - Variant tmp = Variant::construct(base_type.builtin_type, nullptr, 0, err); + Variant tmp; + Variant::construct(base_type.builtin_type, tmp, nullptr, 0, err); if (err.error != Callable::CallError::CALL_OK) { return false; } @@ -2257,7 +2266,7 @@ static void _find_call_arguments(GDScriptParser::CompletionContext &p_context, c case GDScriptParser::DataType::BUILTIN: { if (base.get_type() == Variant::NIL) { Callable::CallError err; - base = Variant::construct(base_type.builtin_type, nullptr, 0, err); + Variant::construct(base_type.builtin_type, base, nullptr, 0, err); if (err.error != Callable::CallError::CALL_OK) { return; } @@ -2306,11 +2315,6 @@ static void _find_call_arguments(GDScriptParser::CompletionContext &p_context, c if (GDScriptParser::get_builtin_function(call->function_name) < GDScriptFunctions::FUNC_MAX) { MethodInfo info = GDScriptFunctions::get_info(GDScriptParser::get_builtin_function(call->function_name)); - - if ((info.name == "load" || info.name == "preload") && bool(EditorSettings::get_singleton()->get("text_editor/completion/complete_file_paths"))) { - _get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), r_result); - } - r_arghint = _make_arguments_hint(info, p_argidx); return; } else if (GDScriptParser::get_builtin_type(call->function_name) < Variant::VARIANT_MAX) { @@ -2883,7 +2887,7 @@ static Error _lookup_symbol_from_base(const GDScriptParser::DataType &p_base, co v = v_ref; } else { Callable::CallError err; - v = Variant::construct(base_type.builtin_type, NULL, 0, err); + Variant::construct(base_type.builtin_type, v, NULL, 0, err); if (err.error != Callable::CallError::CALL_OK) { break; } diff --git a/modules/gdscript/gdscript_function.cpp b/modules/gdscript/gdscript_function.cpp index 3a7b38dac5..8372672cf7 100644 --- a/modules/gdscript/gdscript_function.cpp +++ b/modules/gdscript/gdscript_function.cpp @@ -331,7 +331,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a return Variant(); } if (argument_types[i].kind == GDScriptDataType::BUILTIN) { - Variant arg = Variant::construct(argument_types[i].builtin_type, &p_args[i], 1, r_err); + Variant arg; + Variant::construct(argument_types[i].builtin_type, arg, &p_args[i], 1, r_err); memnew_placement(&stack[i], Variant(arg)); } else { memnew_placement(&stack[i], Variant(*p_args[i])); @@ -755,7 +756,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a if (Variant::can_convert_strict(src->get_type(), var_type)) { #endif // DEBUG_ENABLED Callable::CallError ce; - *dst = Variant::construct(var_type, const_cast<const Variant **>(&src), 1, ce); + Variant::construct(var_type, *dst, const_cast<const Variant **>(&src), 1, ce); } else { #ifdef DEBUG_ENABLED err_text = "Trying to assign value of type '" + Variant::get_type_name(src->get_type()) + @@ -857,7 +858,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a GD_ERR_BREAK(to_type < 0 || to_type >= Variant::VARIANT_MAX); Callable::CallError err; - *dst = Variant::construct(to_type, (const Variant **)&src, 1, err); + Variant::construct(to_type, *dst, (const Variant **)&src, 1, err); #ifdef DEBUG_ENABLED if (err.error != Callable::CallError::CALL_OK) { @@ -955,7 +956,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a GET_VARIANT_PTR(dst, 3 + argc); Callable::CallError err; - *dst = Variant::construct(t, (const Variant **)argptrs, argc, err); + Variant::construct(t, *dst, (const Variant **)argptrs, argc, err); #ifdef DEBUG_ENABLED if (err.error != Callable::CallError::CALL_OK) { @@ -1047,7 +1048,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a Callable::CallError err; if (call_ret) { GET_VARIANT_PTR(ret, argc); - base->call_ptr(*methodname, (const Variant **)argptrs, argc, ret, err); + base->call(*methodname, (const Variant **)argptrs, argc, *ret, err); #ifdef DEBUG_ENABLED if (!call_async && ret->get_type() == Variant::OBJECT) { // Check if getting a function state without await. @@ -1065,7 +1066,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a } #endif } else { - base->call_ptr(*methodname, (const Variant **)argptrs, argc, nullptr, err); + Variant ret; + base->call(*methodname, (const Variant **)argptrs, argc, ret, err); } #ifdef DEBUG_ENABLED if (GDScriptLanguage::get_singleton()->profiling) { diff --git a/modules/gdscript/gdscript_functions.cpp b/modules/gdscript/gdscript_functions.cpp index 3c82545190..3a7c1a8676 100644 --- a/modules/gdscript/gdscript_functions.cpp +++ b/modules/gdscript/gdscript_functions.cpp @@ -285,7 +285,7 @@ void GDScriptFunctions::call(Function p_func, const Variant **p_args, int p_arg_ int64_t i = *p_args[0]; r_ret = i < 0 ? -1 : (i > 0 ? +1 : 0); } else if (p_args[0]->get_type() == Variant::FLOAT) { - real_t r = *p_args[0]; + double r = *p_args[0]; r_ret = r < 0.0 ? -1.0 : (r > 0.0 ? +1.0 : 0.0); } else { r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; @@ -510,8 +510,8 @@ void GDScriptFunctions::call(Function p_func, const Variant **p_args, int p_arg_ VALIDATE_ARG_NUM(0); VALIDATE_ARG_NUM(1); - real_t a = *p_args[0]; - real_t b = *p_args[1]; + double a = *p_args[0]; + double b = *p_args[1]; r_ret = MAX(a, b); } @@ -527,8 +527,8 @@ void GDScriptFunctions::call(Function p_func, const Variant **p_args, int p_arg_ VALIDATE_ARG_NUM(0); VALIDATE_ARG_NUM(1); - real_t a = *p_args[0]; - real_t b = *p_args[1]; + double a = *p_args[0]; + double b = *p_args[1]; r_ret = MIN(a, b); } @@ -545,9 +545,9 @@ void GDScriptFunctions::call(Function p_func, const Variant **p_args, int p_arg_ VALIDATE_ARG_NUM(1); VALIDATE_ARG_NUM(2); - real_t a = *p_args[0]; - real_t b = *p_args[1]; - real_t c = *p_args[2]; + double a = *p_args[0]; + double b = *p_args[1]; + double c = *p_args[2]; r_ret = CLAMP(a, b, c); } @@ -599,7 +599,7 @@ void GDScriptFunctions::call(Function p_func, const Variant **p_args, int p_arg_ return; } else { - r_ret = Variant::construct(Variant::Type(type), p_args, 1, r_error); + Variant::construct(Variant::Type(type), r_ret, p_args, 1, r_error); } } break; case TYPE_OF: { diff --git a/modules/gdscript/gdscript_parser.cpp b/modules/gdscript/gdscript_parser.cpp index 6bf8a3a908..fde3662d66 100644 --- a/modules/gdscript/gdscript_parser.cpp +++ b/modules/gdscript/gdscript_parser.cpp @@ -65,7 +65,7 @@ Variant::Type GDScriptParser::get_builtin_type(const StringName &p_type) { builtin_types["Basis"] = Variant::BASIS; builtin_types["Transform"] = Variant::TRANSFORM; builtin_types["Color"] = Variant::COLOR; - builtin_types["RID"] = Variant::_RID; + builtin_types["RID"] = Variant::RID; builtin_types["Object"] = Variant::OBJECT; builtin_types["StringName"] = Variant::STRING_NAME; builtin_types["NodePath"] = Variant::NODE_PATH; @@ -2486,26 +2486,28 @@ GDScriptParser::ExpressionNode *GDScriptParser::parse_call(ExpressionNode *p_pre } } - if (!check(GDScriptTokenizer::Token::PARENTHESIS_CLOSE)) { - // Arguments. - push_completion_call(call); - make_completion_context(COMPLETION_CALL_ARGUMENTS, call, 0, true); - int argument_index = 0; - do { - make_completion_context(COMPLETION_CALL_ARGUMENTS, call, argument_index++, true); - if (check(GDScriptTokenizer::Token::PARENTHESIS_CLOSE)) { - // Allow for trailing comma. - break; - } - ExpressionNode *argument = parse_expression(false); - if (argument == nullptr) { - push_error(R"(Expected expression as the function argument.)"); - } else { - call->arguments.push_back(argument); - } - } while (match(GDScriptTokenizer::Token::COMMA)); - pop_completion_call(); + // Arguments. + CompletionType ct = COMPLETION_CALL_ARGUMENTS; + if (get_builtin_function(call->function_name) == GDScriptFunctions::RESOURCE_LOAD) { + ct = COMPLETION_RESOURCE_PATH; } + push_completion_call(call); + int argument_index = 0; + do { + make_completion_context(ct, call, argument_index++, true); + if (check(GDScriptTokenizer::Token::PARENTHESIS_CLOSE)) { + // Allow for trailing comma. + break; + } + ExpressionNode *argument = parse_expression(false); + if (argument == nullptr) { + push_error(R"(Expected expression as the function argument.)"); + } else { + call->arguments.push_back(argument); + } + ct = COMPLETION_CALL_ARGUMENTS; + } while (match(GDScriptTokenizer::Token::COMMA)); + pop_completion_call(); pop_multiline(); consume(GDScriptTokenizer::Token::PARENTHESIS_CLOSE, R"*(Expected closing ")" after call arguments.)*"); @@ -2802,7 +2804,9 @@ bool GDScriptParser::validate_annotation_arguments(AnnotationNode *p_annotation) Callable::CallError error; Vector<Variant> args = varray(string->name); const Variant *name = args.ptr(); - p_annotation->resolved_arguments.push_back(Variant::construct(parameter.type, &(name), 1, error)); + Variant r; + Variant::construct(parameter.type, r, &(name), 1, error); + p_annotation->resolved_arguments.push_back(r); if (error.error != Callable::CallError::CALL_OK) { push_error(vformat(R"(Expected %s as argument %d of annotation "%s").)", Variant::get_type_name(parameter.type), i + 1, p_annotation->name)); p_annotation->resolved_arguments.remove(p_annotation->resolved_arguments.size() - 1); @@ -2824,7 +2828,9 @@ bool GDScriptParser::validate_annotation_arguments(AnnotationNode *p_annotation) } Callable::CallError error; const Variant *args = &value; - p_annotation->resolved_arguments.push_back(Variant::construct(parameter.type, &(args), 1, error)); + Variant r; + Variant::construct(parameter.type, r, &(args), 1, error); + p_annotation->resolved_arguments.push_back(r); if (error.error != Callable::CallError::CALL_OK) { push_error(vformat(R"(Expected %s as argument %d of annotation "%s").)", Variant::get_type_name(parameter.type), i + 1, p_annotation->name)); p_annotation->resolved_arguments.remove(p_annotation->resolved_arguments.size() - 1); diff --git a/modules/icloud/SCsub b/modules/icloud/SCsub new file mode 100644 index 0000000000..805a484600 --- /dev/null +++ b/modules/icloud/SCsub @@ -0,0 +1,15 @@ +#!/usr/bin/env python + +Import("env") +Import("env_modules") + +env_icloud = env_modules.Clone() + +# (iOS) Enable module support +env_icloud.Append(CCFLAGS=["-fmodules", "-fcxx-modules"]) + +# (iOS) Build as separate static library +modules_sources = [] +env_icloud.add_source_files(modules_sources, "*.cpp") +env_icloud.add_source_files(modules_sources, "*.mm") +mod_lib = env_modules.add_library("#bin/libgodot_icloud_module" + env["LIBSUFFIX"], modules_sources) diff --git a/modules/icloud/config.py b/modules/icloud/config.py new file mode 100644 index 0000000000..e68603fc93 --- /dev/null +++ b/modules/icloud/config.py @@ -0,0 +1,6 @@ +def can_build(env, platform): + return platform == "iphone" + + +def configure(env): + pass diff --git a/modules/icloud/icloud.gdip b/modules/icloud/icloud.gdip new file mode 100644 index 0000000000..9f81be8a34 --- /dev/null +++ b/modules/icloud/icloud.gdip @@ -0,0 +1,17 @@ +[config] +name="iCloud" +binary="icloud_lib.a" + +initialization="register_icloud_types" +deinitialization="unregister_icloud_types" + +[dependencies] +linked=[] +embedded=[] +system=[] + +capabilities=[] + +files=[] + +[plist] diff --git a/platform/iphone/icloud.h b/modules/icloud/icloud.h index 5f59abdfc0..35eede0bf9 100644 --- a/platform/iphone/icloud.h +++ b/modules/icloud/icloud.h @@ -28,8 +28,6 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#ifdef ICLOUD_ENABLED - #ifndef ICLOUD_H #define ICLOUD_H @@ -60,5 +58,3 @@ public: }; #endif - -#endif diff --git a/platform/iphone/icloud.mm b/modules/icloud/icloud.mm index 3d81349883..8a8ddbefe9 100644 --- a/platform/iphone/icloud.mm +++ b/modules/icloud/icloud.mm @@ -28,22 +28,12 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#ifdef ICLOUD_ENABLED - #include "icloud.h" -#ifndef __IPHONE_9_0 -extern "C" { -#endif - -#import "app_delegate.h" +#import "platform/iphone/app_delegate.h" #import <Foundation/Foundation.h> -#ifndef __IPHONE_9_0 -}; -#endif - ICloud *ICloud::instance = NULL; void ICloud::_bind_methods() { @@ -353,5 +343,3 @@ ICloud::ICloud() { } ICloud::~ICloud() {} - -#endif diff --git a/modules/icloud/icloud_module.cpp b/modules/icloud/icloud_module.cpp new file mode 100644 index 0000000000..43fdc7d45e --- /dev/null +++ b/modules/icloud/icloud_module.cpp @@ -0,0 +1,48 @@ +/*************************************************************************/ +/* icloud_module.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 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. */ +/*************************************************************************/ + +#include "icloud_module.h" + +#include "core/config/engine.h" + +#include "icloud.h" + +ICloud *icloud; + +void register_icloud_types() { + icloud = memnew(ICloud); + Engine::get_singleton()->add_singleton(Engine::Singleton("ICloud", icloud)); +} + +void unregister_icloud_types() { + if (icloud) { + memdelete(icloud); + } +} diff --git a/modules/icloud/icloud_module.h b/modules/icloud/icloud_module.h new file mode 100644 index 0000000000..7fd057525e --- /dev/null +++ b/modules/icloud/icloud_module.h @@ -0,0 +1,32 @@ +/*************************************************************************/ +/* icloud_module.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 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. */ +/*************************************************************************/ + +void register_icloud_types(); +void unregister_icloud_types(); diff --git a/modules/inappstore/SCsub b/modules/inappstore/SCsub new file mode 100644 index 0000000000..cee6a256d5 --- /dev/null +++ b/modules/inappstore/SCsub @@ -0,0 +1,15 @@ +#!/usr/bin/env python + +Import("env") +Import("env_modules") + +env_inappstore = env_modules.Clone() + +# (iOS) Enable module support +env_inappstore.Append(CCFLAGS=["-fmodules", "-fcxx-modules"]) + +# (iOS) Build as separate static library +modules_sources = [] +env_inappstore.add_source_files(modules_sources, "*.cpp") +env_inappstore.add_source_files(modules_sources, "*.mm") +mod_lib = env_modules.add_library("#bin/libgodot_inappstore_module" + env["LIBSUFFIX"], modules_sources) diff --git a/modules/inappstore/config.py b/modules/inappstore/config.py new file mode 100644 index 0000000000..e68603fc93 --- /dev/null +++ b/modules/inappstore/config.py @@ -0,0 +1,6 @@ +def can_build(env, platform): + return platform == "iphone" + + +def configure(env): + pass diff --git a/platform/iphone/in_app_store.h b/modules/inappstore/in_app_store.h index 85075e4e20..c8e5d17cec 100644 --- a/platform/iphone/in_app_store.h +++ b/modules/inappstore/in_app_store.h @@ -28,8 +28,6 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#ifdef STOREKIT_ENABLED - #ifndef IN_APP_STORE_H #define IN_APP_STORE_H @@ -77,5 +75,3 @@ public: }; #endif - -#endif diff --git a/platform/iphone/in_app_store.mm b/modules/inappstore/in_app_store.mm index 3eec9dae69..62977318c1 100644 --- a/platform/iphone/in_app_store.mm +++ b/modules/inappstore/in_app_store.mm @@ -28,8 +28,6 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#ifdef STOREKIT_ENABLED - #include "in_app_store.h" #import <Foundation/Foundation.h> @@ -411,5 +409,3 @@ InAppStore::~InAppStore() { [[SKPaymentQueue defaultQueue] removeTransactionObserver:transactions_observer]; transactions_observer = nil; } - -#endif diff --git a/modules/inappstore/in_app_store_module.cpp b/modules/inappstore/in_app_store_module.cpp new file mode 100644 index 0000000000..039bdd4f83 --- /dev/null +++ b/modules/inappstore/in_app_store_module.cpp @@ -0,0 +1,48 @@ +/*************************************************************************/ +/* in_app_store_module.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 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. */ +/*************************************************************************/ + +#include "in_app_store_module.h" + +#include "core/config/engine.h" + +#include "in_app_store.h" + +InAppStore *store_kit; + +void register_inappstore_types() { + store_kit = memnew(InAppStore); + Engine::get_singleton()->add_singleton(Engine::Singleton("InAppStore", store_kit)); +} + +void unregister_inappstore_types() { + if (store_kit) { + memdelete(store_kit); + } +} diff --git a/modules/inappstore/in_app_store_module.h b/modules/inappstore/in_app_store_module.h new file mode 100644 index 0000000000..44673e58bc --- /dev/null +++ b/modules/inappstore/in_app_store_module.h @@ -0,0 +1,32 @@ +/*************************************************************************/ +/* in_app_store_module.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 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. */ +/*************************************************************************/ + +void register_inappstore_types(); +void unregister_inappstore_types(); diff --git a/modules/inappstore/inappstore.gdip b/modules/inappstore/inappstore.gdip new file mode 100644 index 0000000000..7a5efb8ad3 --- /dev/null +++ b/modules/inappstore/inappstore.gdip @@ -0,0 +1,17 @@ +[config] +name="InAppStore" +binary="inappstore_lib.a" + +initialization="register_inappstore_types" +deinitialization="unregister_inappstore_types" + +[dependencies] +linked=[] +embedded=[] +system=["StoreKit.framework"] + +capabilities=[] + +files=[] + +[plist] diff --git a/modules/mono/csharp_script.cpp b/modules/mono/csharp_script.cpp index e59dd24c34..b4537f531d 100644 --- a/modules/mono/csharp_script.cpp +++ b/modules/mono/csharp_script.cpp @@ -477,7 +477,7 @@ static String variant_type_to_managed_name(const String &p_var_type_name) { Variant::COLOR, Variant::STRING_NAME, Variant::NODE_PATH, - Variant::_RID, + Variant::RID, Variant::CALLABLE }; diff --git a/modules/mono/editor/bindings_generator.cpp b/modules/mono/editor/bindings_generator.cpp index 4ad5cdbf47..ff3122a77f 100644 --- a/modules/mono/editor/bindings_generator.cpp +++ b/modules/mono/editor/bindings_generator.cpp @@ -2426,7 +2426,7 @@ bool BindingsGenerator::_arg_default_value_is_assignable_to_type(const Variant & case Variant::VECTOR2: case Variant::RECT2: case Variant::VECTOR3: - case Variant::_RID: + case Variant::RID: case Variant::ARRAY: case Variant::DICTIONARY: case Variant::PACKED_BYTE_ARRAY: @@ -2979,7 +2979,7 @@ bool BindingsGenerator::_arg_default_value_from_variant(const Variant &p_val, Ar r_iarg.default_argument = "new %s()"; r_iarg.def_param_mode = ArgumentInterface::NULLABLE_REF; break; - case Variant::_RID: + case Variant::RID: ERR_FAIL_COND_V_MSG(r_iarg.type.cname != name_cache.type_RID, false, "Parameter of type '" + String(r_iarg.type.cname) + "' cannot have a default value of type '" + String(name_cache.type_RID) + "'."); diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/GD.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/GD.cs index 7f4777777c..6699c5992c 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/GD.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/GD.cs @@ -39,14 +39,6 @@ namespace Godot return val * sgn; } - public static FuncRef FuncRef(Object instance, StringName funcName) - { - var ret = new FuncRef(); - ret.SetInstance(instance); - ret.SetFunction(funcName); - return ret; - } - public static int Hash(object var) { return godot_icall_GD_hash(var); diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Transform2D.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Transform2D.cs index 06bbe98497..bc0f81b2a7 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Transform2D.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Transform2D.cs @@ -221,8 +221,7 @@ namespace Godot real_t dot = v1.Dot(v2); - // Clamp dot to [-1, 1] - dot = dot < -1.0f ? -1.0f : (dot > 1.0f ? 1.0f : dot); + dot = Mathf.Clamp(dot, -1.0f, 1.0f); Vector2 v; diff --git a/modules/mono/glue/gd_glue.cpp b/modules/mono/glue/gd_glue.cpp index 4c1df529fc..58d8dceb25 100644 --- a/modules/mono/glue/gd_glue.cpp +++ b/modules/mono/glue/gd_glue.cpp @@ -55,7 +55,8 @@ MonoObject *godot_icall_GD_convert(MonoObject *p_what, int32_t p_type) { Variant what = GDMonoMarshal::mono_object_to_variant(p_what); const Variant *args[1] = { &what }; Callable::CallError ce; - Variant ret = Variant::construct(Variant::Type(p_type), args, 1, ce); + Variant ret; + Variant::construct(Variant::Type(p_type), ret, args, 1, ce); ERR_FAIL_COND_V(ce.error != Callable::CallError::CALL_OK, nullptr); return GDMonoMarshal::variant_to_mono_object(ret); } diff --git a/modules/mono/mono_gd/gd_mono_field.cpp b/modules/mono/mono_gd/gd_mono_field.cpp index 563c45e71f..00a1e1e507 100644 --- a/modules/mono/mono_gd/gd_mono_field.cpp +++ b/modules/mono/mono_gd/gd_mono_field.cpp @@ -355,7 +355,7 @@ void GDMonoField::set_value_from_variant(MonoObject *p_object, const Variant &p_ } if (CACHED_CLASS(RID) == type_class) { - MonoObject *managed = GDMonoUtils::create_managed_from(p_value.operator RID()); + MonoObject *managed = GDMonoUtils::create_managed_from(p_value.operator ::RID()); mono_field_set_value(p_object, mono_field, managed); break; } @@ -450,8 +450,8 @@ void GDMonoField::set_value_from_variant(MonoObject *p_object, const Variant &p_ MonoObject *managed = GDMonoUtils::create_managed_from(p_value.operator NodePath()); mono_field_set_value(p_object, mono_field, managed); } break; - case Variant::_RID: { - MonoObject *managed = GDMonoUtils::create_managed_from(p_value.operator RID()); + case Variant::RID: { + MonoObject *managed = GDMonoUtils::create_managed_from(p_value.operator ::RID()); mono_field_set_value(p_object, mono_field, managed); } break; case Variant::OBJECT: { diff --git a/modules/mono/mono_gd/gd_mono_marshal.cpp b/modules/mono/mono_gd/gd_mono_marshal.cpp index c460e283ea..9d193ab8ab 100644 --- a/modules/mono/mono_gd/gd_mono_marshal.cpp +++ b/modules/mono/mono_gd/gd_mono_marshal.cpp @@ -204,7 +204,7 @@ Variant::Type managed_to_variant_type(const ManagedType &p_type, bool *r_nil_is_ } if (CACHED_CLASS(RID) == type_class) { - return Variant::_RID; + return Variant::RID; } if (CACHED_CLASS(Dictionary) == type_class) { @@ -580,7 +580,7 @@ MonoObject *variant_to_mono_object(const Variant *p_var, const ManagedType &p_ty } if (CACHED_CLASS(RID) == type_class) { - return GDMonoUtils::create_managed_from(p_var->operator RID()); + return GDMonoUtils::create_managed_from(p_var->operator ::RID()); } // Godot.Collections.Dictionary or IDictionary @@ -673,8 +673,8 @@ MonoObject *variant_to_mono_object(const Variant *p_var, const ManagedType &p_ty return GDMonoUtils::create_managed_from(p_var->operator StringName()); case Variant::NODE_PATH: return GDMonoUtils::create_managed_from(p_var->operator NodePath()); - case Variant::_RID: - return GDMonoUtils::create_managed_from(p_var->operator RID()); + case Variant::RID: + return GDMonoUtils::create_managed_from(p_var->operator ::RID()); case Variant::OBJECT: return GDMonoUtils::unmanaged_get_managed(p_var->operator Object *()); case Variant::CALLABLE: { diff --git a/modules/visual_script/doc_classes/VisualScriptBuiltinFunc.xml b/modules/visual_script/doc_classes/VisualScriptBuiltinFunc.xml index ca1215b0bd..000fbd0140 100644 --- a/modules/visual_script/doc_classes/VisualScriptBuiltinFunc.xml +++ b/modules/visual_script/doc_classes/VisualScriptBuiltinFunc.xml @@ -166,63 +166,60 @@ <constant name="OBJ_WEAKREF" value="50" enum="BuiltinFunc"> Create a [WeakRef] from the input. </constant> - <constant name="FUNC_FUNCREF" value="51" enum="BuiltinFunc"> - Create a [FuncRef] from the input. - </constant> - <constant name="TYPE_CONVERT" value="52" enum="BuiltinFunc"> + <constant name="TYPE_CONVERT" value="51" enum="BuiltinFunc"> Convert between types. </constant> - <constant name="TYPE_OF" value="53" enum="BuiltinFunc"> + <constant name="TYPE_OF" value="52" enum="BuiltinFunc"> Return the type of the input as an integer. Check [enum Variant.Type] for the integers that might be returned. </constant> - <constant name="TYPE_EXISTS" value="54" enum="BuiltinFunc"> + <constant name="TYPE_EXISTS" value="53" enum="BuiltinFunc"> Checks if a type is registered in the [ClassDB]. </constant> - <constant name="TEXT_CHAR" value="55" enum="BuiltinFunc"> + <constant name="TEXT_CHAR" value="54" enum="BuiltinFunc"> Return a character with the given ascii value. </constant> - <constant name="TEXT_STR" value="56" enum="BuiltinFunc"> + <constant name="TEXT_STR" value="55" enum="BuiltinFunc"> Convert the input to a string. </constant> - <constant name="TEXT_PRINT" value="57" enum="BuiltinFunc"> + <constant name="TEXT_PRINT" value="56" enum="BuiltinFunc"> Print the given string to the output window. </constant> - <constant name="TEXT_PRINTERR" value="58" enum="BuiltinFunc"> + <constant name="TEXT_PRINTERR" value="57" enum="BuiltinFunc"> Print the given string to the standard error output. </constant> - <constant name="TEXT_PRINTRAW" value="59" enum="BuiltinFunc"> + <constant name="TEXT_PRINTRAW" value="58" enum="BuiltinFunc"> Print the given string to the standard output, without adding a newline. </constant> - <constant name="VAR_TO_STR" value="60" enum="BuiltinFunc"> + <constant name="VAR_TO_STR" value="59" enum="BuiltinFunc"> Serialize a [Variant] to a string. </constant> - <constant name="STR_TO_VAR" value="61" enum="BuiltinFunc"> + <constant name="STR_TO_VAR" value="60" enum="BuiltinFunc"> Deserialize a [Variant] from a string serialized using [constant VAR_TO_STR]. </constant> - <constant name="VAR_TO_BYTES" value="62" enum="BuiltinFunc"> + <constant name="VAR_TO_BYTES" value="61" enum="BuiltinFunc"> Serialize a [Variant] to a [PackedByteArray]. </constant> - <constant name="BYTES_TO_VAR" value="63" enum="BuiltinFunc"> + <constant name="BYTES_TO_VAR" value="62" enum="BuiltinFunc"> Deserialize a [Variant] from a [PackedByteArray] serialized using [constant VAR_TO_BYTES]. </constant> - <constant name="COLORN" value="64" enum="BuiltinFunc"> + <constant name="COLORN" value="63" enum="BuiltinFunc"> Return the [Color] with the given name and alpha ranging from 0 to 1. [b]Note:[/b] Names are defined in [code]color_names.inc[/code]. </constant> - <constant name="MATH_SMOOTHSTEP" value="65" enum="BuiltinFunc"> + <constant name="MATH_SMOOTHSTEP" value="64" enum="BuiltinFunc"> Return a number smoothly interpolated between the first two inputs, based on the third input. Similar to [constant MATH_LERP], but interpolates faster at the beginning and slower at the end. Using Hermite interpolation formula: [codeblock] var t = clamp((weight - from) / (to - from), 0.0, 1.0) return t * t * (3.0 - 2.0 * t) [/codeblock] </constant> - <constant name="MATH_POSMOD" value="66" enum="BuiltinFunc"> + <constant name="MATH_POSMOD" value="65" enum="BuiltinFunc"> </constant> - <constant name="MATH_LERP_ANGLE" value="67" enum="BuiltinFunc"> + <constant name="MATH_LERP_ANGLE" value="66" enum="BuiltinFunc"> </constant> - <constant name="TEXT_ORD" value="68" enum="BuiltinFunc"> + <constant name="TEXT_ORD" value="67" enum="BuiltinFunc"> </constant> - <constant name="FUNC_MAX" value="69" enum="BuiltinFunc"> + <constant name="FUNC_MAX" value="68" enum="BuiltinFunc"> Represents the size of the [enum BuiltinFunc] enum. </constant> </constants> diff --git a/modules/visual_script/visual_script.cpp b/modules/visual_script/visual_script.cpp index f6a7389eec..1c7d5472cb 100644 --- a/modules/visual_script/visual_script.cpp +++ b/modules/visual_script/visual_script.cpp @@ -84,10 +84,10 @@ void VisualScriptNode::validate_input_default_values() { Callable::CallError ce; Variant existing = default_input_values[i]; const Variant *existingp = &existing; - default_input_values[i] = Variant::construct(expected, &existingp, 1, ce, false); + Variant::construct(expected, default_input_values[i], &existingp, 1, ce); if (ce.error != Callable::CallError::CALL_OK) { //could not convert? force.. - default_input_values[i] = Variant::construct(expected, nullptr, 0, ce, false); + Variant::construct(expected, default_input_values[i], nullptr, 0, ce); } } } diff --git a/modules/visual_script/visual_script_builtin_funcs.cpp b/modules/visual_script/visual_script_builtin_funcs.cpp index 9301189eaa..a27307aec2 100644 --- a/modules/visual_script/visual_script_builtin_funcs.cpp +++ b/modules/visual_script/visual_script_builtin_funcs.cpp @@ -1027,7 +1027,7 @@ void VisualScriptBuiltinFunc::exec_func(BuiltinFunc p_func, const Variant **p_in return; } else { - *r_return = Variant::construct(Variant::Type(type), p_inputs, 1, r_error); + Variant::construct(Variant::Type(type), *r_return, p_inputs, 1, r_error); } } break; case VisualScriptBuiltinFunc::TYPE_OF: { diff --git a/modules/visual_script/visual_script_editor.cpp b/modules/visual_script/visual_script_editor.cpp index 082df25dbe..1bb96a47f3 100644 --- a/modules/visual_script/visual_script_editor.cpp +++ b/modules/visual_script/visual_script_editor.cpp @@ -388,7 +388,7 @@ static Color _color_from_type(Variant::Type p_type, bool dark_theme = true) { case Variant::NODE_PATH: color = Color(0.41, 0.58, 0.93); break; - case Variant::_RID: + case Variant::RID: color = Color(0.41, 0.93, 0.6); break; case Variant::OBJECT: @@ -494,7 +494,7 @@ static Color _color_from_type(Variant::Type p_type, bool dark_theme = true) { case Variant::NODE_PATH: color = Color(0.41, 0.58, 0.93); break; - case Variant::_RID: + case Variant::RID: color = Color(0.17, 0.9, 0.45); break; case Variant::OBJECT: @@ -885,7 +885,7 @@ void VisualScriptEditor::_update_graph(int p_only_id) { //not the same, reconvert Callable::CallError ce; const Variant *existingp = &value; - value = Variant::construct(left_type, &existingp, 1, ce, false); + Variant::construct(left_type, value, &existingp, 1, ce); } if (left_type == Variant::COLOR) { @@ -1173,7 +1173,9 @@ String VisualScriptEditor::_sanitized_variant_text(const StringName &property_na if (script->get_variable_info(property_name).type != Variant::NIL) { Callable::CallError ce; const Variant *converted = &var; - var = Variant::construct(script->get_variable_info(property_name).type, &converted, 1, ce, false); + Variant n; + Variant::construct(script->get_variable_info(property_name).type, n, &converted, 1, ce); + var = n; } return String(var); @@ -3959,8 +3961,9 @@ void VisualScriptEditor::_default_value_edited(Node *p_button, int p_id, int p_i Variant existing = vsn->get_default_input_value(p_input_port); if (pinfo.type != Variant::NIL && existing.get_type() != pinfo.type) { Callable::CallError ce; - const Variant *existingp = &existing; - existing = Variant::construct(pinfo.type, &existingp, 1, ce, false); + Variant e = existing; + const Variant *existingp = &e; + Variant::construct(pinfo.type, existing, &existingp, 1, ce); } default_value_edit->set_position(Object::cast_to<Control>(p_button)->get_global_position() + Vector2(0, Object::cast_to<Control>(p_button)->get_size().y)); diff --git a/modules/visual_script/visual_script_expression.cpp b/modules/visual_script/visual_script_expression.cpp index 3785e81b9f..10a18dfd5e 100644 --- a/modules/visual_script/visual_script_expression.cpp +++ b/modules/visual_script/visual_script_expression.cpp @@ -1405,7 +1405,7 @@ public: argp.write[i] = &arr[i]; } - r_ret = Variant::construct(constructor->data_type, (const Variant **)argp.ptr(), argp.size(), ce); + Variant::construct(constructor->data_type, r_ret, (const Variant **)argp.ptr(), argp.size(), ce); if (ce.error != Callable::CallError::CALL_OK) { r_error_str = "Invalid arguments to construct '" + Variant::get_type_name(constructor->data_type) + "'."; @@ -1463,7 +1463,7 @@ public: argp.write[i] = &arr[i]; } - r_ret = base.call(call->method, (const Variant **)argp.ptr(), argp.size(), ce); + base.call(call->method, (const Variant **)argp.ptr(), argp.size(), r_ret, ce); if (ce.error != Callable::CallError::CALL_OK) { r_error_str = "On call to '" + String(call->method) + "':"; diff --git a/modules/visual_script/visual_script_func_nodes.cpp b/modules/visual_script/visual_script_func_nodes.cpp index e159b039af..34a1cfc4fc 100644 --- a/modules/visual_script/visual_script_func_nodes.cpp +++ b/modules/visual_script/visual_script_func_nodes.cpp @@ -42,7 +42,7 @@ ////////////////////////////////////////// int VisualScriptFunctionCall::get_output_sequence_port_count() const { - if ((method_cache.flags & METHOD_FLAG_CONST && call_mode != CALL_MODE_INSTANCE) || (call_mode == CALL_MODE_BASIC_TYPE && Variant::is_method_const(basic_type, function))) { + if ((method_cache.flags & METHOD_FLAG_CONST && call_mode != CALL_MODE_INSTANCE) || (call_mode == CALL_MODE_BASIC_TYPE && Variant::is_builtin_method_const(basic_type, function))) { return 0; } else { return 1; @@ -50,7 +50,7 @@ int VisualScriptFunctionCall::get_output_sequence_port_count() const { } bool VisualScriptFunctionCall::has_input_sequence_port() const { - return !((method_cache.flags & METHOD_FLAG_CONST && call_mode != CALL_MODE_INSTANCE) || (call_mode == CALL_MODE_BASIC_TYPE && Variant::is_method_const(basic_type, function))); + return !((method_cache.flags & METHOD_FLAG_CONST && call_mode != CALL_MODE_INSTANCE) || (call_mode == CALL_MODE_BASIC_TYPE && Variant::is_builtin_method_const(basic_type, function))); } #ifdef TOOLS_ENABLED @@ -130,7 +130,11 @@ StringName VisualScriptFunctionCall::_get_base_type() const { int VisualScriptFunctionCall::get_input_value_port_count() const { if (call_mode == CALL_MODE_BASIC_TYPE) { - Vector<Variant::Type> types = Variant::get_method_argument_types(basic_type, function); + Vector<Variant::Type> types; + int argc = Variant::get_builtin_method_argument_count(basic_type, function); + for (int i = 0; i < argc; i++) { + types.push_back(Variant::get_builtin_method_argument_type(basic_type, function, i)); + } return types.size() + (rpc_call_mode >= RPC_RELIABLE_TO_ID ? 1 : 0) + 1; } else { @@ -147,8 +151,7 @@ int VisualScriptFunctionCall::get_input_value_port_count() const { int VisualScriptFunctionCall::get_output_value_port_count() const { if (call_mode == CALL_MODE_BASIC_TYPE) { - bool returns = false; - Variant::get_method_return_type(basic_type, function, &returns); + bool returns = Variant::has_builtin_method_return_value(basic_type, function); return returns ? 1 : 0; } else { @@ -195,10 +198,7 @@ PropertyInfo VisualScriptFunctionCall::get_input_value_port_info(int p_idx) cons #ifdef DEBUG_METHODS_ENABLED if (call_mode == CALL_MODE_BASIC_TYPE) { - Vector<StringName> names = Variant::get_method_argument_names(basic_type, function); - Vector<Variant::Type> types = Variant::get_method_argument_types(basic_type, function); - return PropertyInfo(types[p_idx], names[p_idx]); - + return PropertyInfo(Variant::get_builtin_method_argument_type(basic_type, function, p_idx), Variant::get_builtin_method_argument_name(basic_type, function, p_idx)); } else { MethodBind *mb = ClassDB::get_method(_get_base_type(), function); if (mb) { @@ -220,7 +220,7 @@ PropertyInfo VisualScriptFunctionCall::get_output_value_port_info(int p_idx) con #ifdef DEBUG_METHODS_ENABLED if (call_mode == CALL_MODE_BASIC_TYPE) { - return PropertyInfo(Variant::get_method_return_type(basic_type, function), ""); + return PropertyInfo(Variant::get_builtin_method_return_type(basic_type, function), ""); } else { if (call_mode == CALL_MODE_INSTANCE) { if (p_idx == 0) { @@ -419,7 +419,7 @@ void VisualScriptFunctionCall::set_function(const StringName &p_type) { function = p_type; if (call_mode == CALL_MODE_BASIC_TYPE) { - use_default_args = Variant::get_method_default_arguments(basic_type, function).size(); + use_default_args = Variant::get_builtin_method_default_arguments(basic_type, function).size(); } else { //update all caches @@ -606,7 +606,7 @@ void VisualScriptFunctionCall::_validate_property(PropertyInfo &property) const int mc = 0; if (call_mode == CALL_MODE_BASIC_TYPE) { - mc = Variant::get_method_default_arguments(basic_type, function).size(); + mc = Variant::get_builtin_method_default_arguments(basic_type, function).size(); } else { MethodBind *mb = ClassDB::get_method(_get_base_type(), function); if (mb) { @@ -805,19 +805,21 @@ public: } else if (returns) { if (call_mode == VisualScriptFunctionCall::CALL_MODE_INSTANCE) { if (returns >= 2) { - *p_outputs[1] = v.call(function, p_inputs + 1, input_args, r_error); + v.call(function, p_inputs + 1, input_args, *p_outputs[1], r_error); } else if (returns == 1) { - v.call(function, p_inputs + 1, input_args, r_error); + Variant ret; + v.call(function, p_inputs + 1, input_args, ret, r_error); } else { r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; r_error_str = "Invalid returns count for call_mode == CALL_MODE_INSTANCE"; return 0; } } else { - *p_outputs[0] = v.call(function, p_inputs + 1, input_args, r_error); + v.call(function, p_inputs + 1, input_args, *p_outputs[0], r_error); } } else { - v.call(function, p_inputs + 1, input_args, r_error); + Variant ret; + v.call(function, p_inputs + 1, input_args, ret, r_error); } if (call_mode == VisualScriptFunctionCall::CALL_MODE_INSTANCE) { @@ -976,7 +978,7 @@ void VisualScriptPropertySet::_adjust_input_index(PropertyInfo &pinfo) const { if (index != StringName()) { Variant v; Callable::CallError ce; - v = Variant::construct(pinfo.type, nullptr, 0, ce); + Variant::construct(pinfo.type, v, nullptr, 0, ce); Variant i = v.get(index); pinfo.type = i.get_type(); } @@ -1117,7 +1119,7 @@ void VisualScriptPropertySet::_update_cache() { Variant v; Callable::CallError ce; - v = Variant::construct(basic_type, nullptr, 0, ce); + Variant::construct(basic_type, v, nullptr, 0, ce); List<PropertyInfo> pinfo; v.get_property_list(&pinfo); @@ -1336,7 +1338,8 @@ void VisualScriptPropertySet::_validate_property(PropertyInfo &property) const { if (property.name == "index") { Callable::CallError ce; - Variant v = Variant::construct(type_cache.type, nullptr, 0, ce); + Variant v; + Variant::construct(type_cache.type, v, nullptr, 0, ce); List<PropertyInfo> plist; v.get_property_list(&plist); String options = ""; @@ -1786,7 +1789,7 @@ void VisualScriptPropertyGet::_update_cache() { Variant v; Callable::CallError ce; - v = Variant::construct(basic_type, nullptr, 0, ce); + Variant::construct(basic_type, v, nullptr, 0, ce); List<PropertyInfo> pinfo; v.get_property_list(&pinfo); @@ -2012,7 +2015,8 @@ void VisualScriptPropertyGet::_validate_property(PropertyInfo &property) const { if (property.name == "index") { Callable::CallError ce; - Variant v = Variant::construct(type_cache, nullptr, 0, ce); + Variant v; + Variant::construct(type_cache, v, nullptr, 0, ce); List<PropertyInfo> plist; v.get_property_list(&plist); String options = ""; @@ -2368,7 +2372,8 @@ void register_visual_script_func_nodes() { Variant::Type t = Variant::Type(i); String type_name = Variant::get_type_name(t); Callable::CallError ce; - Variant vt = Variant::construct(t, nullptr, 0, ce); + Variant vt; + Variant::construct(t, vt, nullptr, 0, ce); List<MethodInfo> ml; vt.get_method_list(&ml); diff --git a/modules/visual_script/visual_script_nodes.cpp b/modules/visual_script/visual_script_nodes.cpp index 436f000498..edec270adc 100644 --- a/modules/visual_script/visual_script_nodes.cpp +++ b/modules/visual_script/visual_script_nodes.cpp @@ -1431,7 +1431,7 @@ void VisualScriptConstant::set_constant_type(Variant::Type p_type) { type = p_type; Callable::CallError ce; - value = Variant::construct(type, nullptr, 0, ce); + Variant::construct(type, value, nullptr, 0, ce); ports_changed_notify(); _change_notify(); } @@ -3255,7 +3255,7 @@ public: virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Callable::CallError &r_error, String &r_error_str) { Callable::CallError ce; - *p_outputs[0] = Variant::construct(type, p_inputs, argcount, ce); + Variant::construct(type, *p_outputs[0], p_inputs, argcount, ce); if (ce.error != Callable::CallError::CALL_OK) { r_error_str = "Invalid arguments for constructor"; } @@ -3727,7 +3727,7 @@ void VisualScriptDeconstruct::_update_elements() { elements.clear(); Variant v; Callable::CallError ce; - v = Variant::construct(type, nullptr, 0, ce); + Variant::construct(type, v, nullptr, 0, ce); List<PropertyInfo> pinfo; v.get_property_list(&pinfo); diff --git a/modules/visual_script/visual_script_property_selector.cpp b/modules/visual_script/visual_script_property_selector.cpp index 3c44faab90..875270e74f 100644 --- a/modules/visual_script/visual_script_property_selector.cpp +++ b/modules/visual_script/visual_script_property_selector.cpp @@ -196,7 +196,7 @@ void VisualScriptPropertySelector::_update_search() { if (type != Variant::NIL) { Variant v; Callable::CallError ce; - v = Variant::construct(type, nullptr, 0, ce); + Variant::construct(type, v, nullptr, 0, ce); v.get_method_list(&methods); } else { Object *obj = ObjectDB::get_instance(script); diff --git a/modules/webrtc/SCsub b/modules/webrtc/SCsub index 20b4c8f8d2..4f870ddb2f 100644 --- a/modules/webrtc/SCsub +++ b/modules/webrtc/SCsub @@ -12,4 +12,8 @@ if use_gdnative: # GDNative is retained in Javascript for export compatibility env_webrtc.Append(CPPDEFINES=["WEBRTC_GDNATIVE_ENABLED"]) env_webrtc.Prepend(CPPPATH=["#modules/gdnative/include/"]) +if env["platform"] == "javascript": + # Our JavaScript/C++ interface. + env.AddJSLibraries(["library_godot_webrtc.js"]) + env_webrtc.add_source_files(env.modules_sources, "*.cpp") diff --git a/modules/webrtc/library_godot_webrtc.js b/modules/webrtc/library_godot_webrtc.js new file mode 100644 index 0000000000..b75996b1f3 --- /dev/null +++ b/modules/webrtc/library_godot_webrtc.js @@ -0,0 +1,407 @@ +/*************************************************************************/ +/* library_godot_webrtc.js */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 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. */ +/*************************************************************************/ + +var GodotRTCDataChannel = { + // Our socket implementation that forwards events to C++. + $GodotRTCDataChannel__deps: ['$IDHandler', '$GodotOS'], + $GodotRTCDataChannel: { + + connect: function(p_id, p_on_open, p_on_message, p_on_error, p_on_close) { + const ref = IDHandler.get(p_id); + if (!ref) { + return; + } + + ref.binaryType = 'arraybuffer'; + ref.onopen = function (event) { + p_on_open(); + }; + ref.onclose = function (event) { + p_on_close(); + }; + ref.onerror = function (event) { + p_on_error(); + }; + ref.onmessage = function(event) { + var buffer; + var is_string = 0; + if (event.data instanceof ArrayBuffer) { + buffer = new Uint8Array(event.data); + } else if (event.data instanceof Blob) { + console.error("Blob type not supported"); + return; + } else if (typeof event.data === "string") { + is_string = 1; + var enc = new TextEncoder("utf-8"); + buffer = new Uint8Array(enc.encode(event.data)); + } else { + console.error("Unknown message type"); + return; + } + var len = buffer.length*buffer.BYTES_PER_ELEMENT; + var out = _malloc(len); + HEAPU8.set(buffer, out); + p_on_message(out, len, is_string); + _free(out); + } + }, + + close: function(p_id) { + const ref = IDHandler.get(p_id); + if (!ref) { + return; + } + ref.onopen = null; + ref.onmessage = null; + ref.onerror = null; + ref.onclose = null; + ref.close(); + }, + + get_prop: function(p_id, p_prop, p_def) { + const ref = IDHandler.get(p_id); + return (ref && ref[p_prop] !== undefined) ? ref[p_prop] : p_def; + }, + }, + + godot_js_rtc_datachannel_ready_state_get: function(p_id) { + const ref = IDHandler.get(p_id); + if (!ref) { + return 3; // CLOSED + } + + switch(ref.readyState) { + case "connecting": + return 0; + case "open": + return 1; + case "closing": + return 2; + case "closed": + return 3; + } + return 3; // CLOSED + }, + + godot_js_rtc_datachannel_send: function(p_id, p_buffer, p_length, p_raw) { + const ref = IDHandler.get(p_id); + if (!ref) { + return 1; + } + + const bytes_array = new Uint8Array(p_length); + for (var i = 0; i < p_length; i++) { + bytes_array[i] = getValue(p_buffer + i, 'i8'); + } + + if (p_raw) { + ref.send(bytes_array.buffer); + } else { + const string = new TextDecoder('utf-8').decode(bytes_array); + ref.send(string); + } + }, + + godot_js_rtc_datachannel_is_ordered: function(p_id) { + return IDHandler.get_prop(p_id, 'ordered', true); + }, + + godot_js_rtc_datachannel_id_get: function(p_id) { + return IDHandler.get_prop(p_id, 'id', 65535); + }, + + godot_js_rtc_datachannel_max_packet_lifetime_get: function(p_id) { + const ref = IDHandler.get(p_id); + if (!ref) { + return 65535; + } + if (ref['maxPacketLifeTime'] !== undefined) { + return ref['maxPacketLifeTime']; + } else if (ref['maxRetransmitTime'] !== undefined) { + // Guess someone didn't appreciate the standardization process. + return ref['maxRetransmitTime']; + } + return 65535; + }, + + godot_js_rtc_datachannel_max_retransmits_get: function(p_id) { + return IDHandler.get_prop(p_id, 'maxRetransmits', 65535); + }, + + godot_js_rtc_datachannel_is_negotiated: function(p_id, p_def) { + return IDHandler.get_prop(p_id, 'negotiated', 65535); + }, + + godot_js_rtc_datachannel_label_get: function(p_id) { + const ref = IDHandler.get(p_id); + if (!ref || !ref.label) { + return 0; + } + return GodotOS.allocString(ref.label); + }, + + godot_js_rtc_datachannel_protocol_get: function(p_id) { + const ref = IDHandler.get(p_id); + if (!ref || !ref.protocol) { + return 0; + } + return GodotOS.allocString(ref.protocol); + }, + + godot_js_rtc_datachannel_destroy: function(p_id) { + GodotRTCDataChannel.close(p_id); + IDHandler.remove(p_id); + }, + + godot_js_rtc_datachannel_connect: function(p_id, p_ref, p_on_open, p_on_message, p_on_error, p_on_close) { + const onopen = GodotOS.get_func(p_on_open).bind(null, p_ref); + const onmessage = GodotOS.get_func(p_on_message).bind(null, p_ref); + const onerror = GodotOS.get_func(p_on_error).bind(null, p_ref); + const onclose = GodotOS.get_func(p_on_close).bind(null, p_ref); + GodotRTCDataChannel.connect(p_id, onopen, onmessage, onerror, onclose); + }, + + godot_js_rtc_datachannel_close: function(p_id) { + const ref = IDHandler.get(p_id); + if (!ref) { + return; + } + GodotRTCDataChannel.close(p_id); + }, +}; + +autoAddDeps(GodotRTCDataChannel, '$GodotRTCDataChannel'); +mergeInto(LibraryManager.library, GodotRTCDataChannel); + +var GodotRTCPeerConnection = { + + $GodotRTCPeerConnection__deps: ['$IDHandler', '$GodotOS', '$GodotRTCDataChannel'], + $GodotRTCPeerConnection: { + onstatechange: function(p_id, p_conn, callback, event) { + const ref = IDHandler.get(p_id); + if (!ref) { + return; + } + var state = 5; // CLOSED + switch(p_conn.iceConnectionState) { + case "new": + state = 0; + case "checking": + state = 1; + case "connected": + case "completed": + state = 2; + case "disconnected": + state = 3; + case "failed": + state = 4; + case "closed": + state = 5; + } + callback(state); + }, + + onicecandidate: function(p_id, callback, event) { + const ref = IDHandler.get(p_id); + if (!ref || !event.candidate) { + return; + } + + let c = event.candidate; + let candidate_str = GodotOS.allocString(c.candidate); + let mid_str = GodotOS.allocString(c.sdpMid); + callback(mid_str, c.sdpMLineIndex, candidate_str); + _free(candidate_str); + _free(mid_str); + }, + + ondatachannel: function(p_id, callback, event) { + const ref = IDHandler.get(p_id); + if (!ref) { + return; + } + + const cid = IDHandler.add(event.channel); + callback(cid); + }, + + onsession: function(p_id, callback, session) { + const ref = IDHandler.get(p_id); + if (!ref) { + return; + } + let type_str = GodotOS.allocString(session.type); + let sdp_str = GodotOS.allocString(session.sdp); + callback(type_str, sdp_str); + _free(type_str); + _free(sdp_str); + }, + + onerror: function(p_id, callback, error) { + const ref = IDHandler.get(p_id); + if (!ref) { + return; + } + console.error(error); + callback(); + }, + }, + + godot_js_rtc_pc_create: function(p_config, p_ref, p_on_state_change, p_on_candidate, p_on_datachannel) { + const onstatechange = GodotOS.get_func(p_on_state_change).bind(null, p_ref); + const oncandidate = GodotOS.get_func(p_on_candidate).bind(null, p_ref); + const ondatachannel = GodotOS.get_func(p_on_datachannel).bind(null, p_ref); + + var config = JSON.parse(UTF8ToString(p_config)); + var conn = null; + try { + conn = new RTCPeerConnection(config); + } catch (e) { + console.error(e); + return 0; + } + + const base = GodotRTCPeerConnection; + const id = IDHandler.add(conn); + conn.oniceconnectionstatechange = base.onstatechange.bind(null, id, conn, onstatechange); + conn.onicecandidate = base.onicecandidate.bind(null, id, oncandidate); + conn.ondatachannel = base.ondatachannel.bind(null, id, ondatachannel); + return id; + }, + + godot_js_rtc_pc_close: function(p_id) { + const ref = IDHandler.get(p_id); + if (!ref) { + return; + } + ref.close(); + }, + + godot_js_rtc_pc_destroy: function(p_id) { + const ref = IDHandler.get(p_id); + if (!ref) { + return; + } + ref.oniceconnectionstatechange = null; + ref.onicecandidate = null; + ref.ondatachannel = null; + IDHandler.remove(p_id); + }, + + godot_js_rtc_pc_offer_create: function(p_id, p_obj, p_on_session, p_on_error) { + const ref = IDHandler.get(p_id); + if (!ref) { + return; + } + const onsession = GodotOS.get_func(p_on_session).bind(null, p_obj); + const onerror = GodotOS.get_func(p_on_error).bind(null, p_obj); + ref.createOffer().then(function(session) { + GodotRTCPeerConnection.onsession(p_id, onsession, session); + }).catch(function(error) { + GodotRTCPeerConnection.onerror(p_id, onerror, error); + }); + }, + + godot_js_rtc_pc_local_description_set: function(p_id, p_type, p_sdp, p_obj, p_on_error) { + const ref = IDHandler.get(p_id); + if (!ref) { + return; + } + const type = UTF8ToString(p_type); + const sdp = UTF8ToString(p_sdp); + const onerror = GodotOS.get_func(p_on_error).bind(null, p_obj); + ref.setLocalDescription({ + 'sdp': sdp, + 'type': type + }).catch(function(error) { + GodotRTCPeerConnection.onerror(p_id, onerror, error); + }); + }, + + godot_js_rtc_pc_remote_description_set: function(p_id, p_type, p_sdp, p_obj, p_session_created, p_on_error) { + const ref = IDHandler.get(p_id); + if (!ref) { + return; + } + const type = UTF8ToString(p_type); + const sdp = UTF8ToString(p_sdp); + const onerror = GodotOS.get_func(p_on_error).bind(null, p_obj); + const onsession = GodotOS.get_func(p_session_created).bind(null, p_obj); + ref.setRemoteDescription({ + 'sdp': sdp, + 'type': type + }).then(function() { + if (type != 'offer') { + return; + } + return ref.createAnswer().then(function(session) { + GodotRTCPeerConnection.onsession(p_id, onsession, session); + }); + }).catch(function(error) { + GodotRTCPeerConnection.onerror(p_id, onerror, error); + }); + }, + + godot_js_rtc_pc_ice_candidate_add: function(p_id, p_mid_name, p_mline_idx, p_sdp) { + const ref = IDHandler.get(p_id); + if (!ref) { + return; + } + var sdpMidName = UTF8ToString(p_mid_name); + var sdpName = UTF8ToString(p_sdp); + ref.addIceCandidate(new RTCIceCandidate({ + "candidate": sdpName, + "sdpMid": sdpMidName, + "sdpMlineIndex": p_mline_idx, + })); + }, + + godot_js_rtc_pc_datachannel_create__deps: ['$GodotRTCDataChannel'], + godot_js_rtc_pc_datachannel_create: function(p_id, p_label, p_config) { + try { + const ref = IDHandler.get(p_id); + if (!ref) { + return 0; + } + + const label = UTF8ToString(p_label); + const config = JSON.parse(UTF8ToString(p_config)); + + const channel = ref.createDataChannel(label, config); + return IDHandler.add(channel); + } catch (e) { + console.error(e); + return 0; + } + }, +}; + +autoAddDeps(GodotRTCPeerConnection, '$GodotRTCPeerConnection') +mergeInto(LibraryManager.library, GodotRTCPeerConnection); diff --git a/modules/webrtc/webrtc_data_channel_js.cpp b/modules/webrtc/webrtc_data_channel_js.cpp index 2c648ba9f9..3a63001a56 100644 --- a/modules/webrtc/webrtc_data_channel_js.cpp +++ b/modules/webrtc/webrtc_data_channel_js.cpp @@ -34,65 +34,58 @@ #include "emscripten.h" extern "C" { -EMSCRIPTEN_KEEPALIVE void _emrtc_on_ch_error(void *obj) { - WebRTCDataChannelJS *peer = static_cast<WebRTCDataChannelJS *>(obj); - peer->_on_error(); -} +typedef void (*RTCChOnOpen)(void *p_obj); +typedef void (*RTCChOnMessage)(void *p_obj, const uint8_t *p_buffer, int p_size, int p_is_string); +typedef void (*RTCChOnClose)(void *p_obj); +typedef void (*RTCChOnError)(void *p_obj); -EMSCRIPTEN_KEEPALIVE void _emrtc_on_ch_open(void *obj) { - WebRTCDataChannelJS *peer = static_cast<WebRTCDataChannelJS *>(obj); - peer->_on_open(); +extern int godot_js_rtc_datachannel_ready_state_get(int p_id); +extern int godot_js_rtc_datachannel_send(int p_id, const uint8_t *p_buffer, int p_length, int p_raw); +extern int godot_js_rtc_datachannel_is_ordered(int p_id); +extern int godot_js_rtc_datachannel_id_get(int p_id); +extern int godot_js_rtc_datachannel_max_packet_lifetime_get(int p_id); +extern int godot_js_rtc_datachannel_max_retransmits_get(int p_id); +extern int godot_js_rtc_datachannel_is_negotiated(int p_id); +extern char *godot_js_rtc_datachannel_label_get(int p_id); // Must free the returned string. +extern char *godot_js_rtc_datachannel_protocol_get(int p_id); // Must free the returned string. +extern void godot_js_rtc_datachannel_destroy(int p_id); +extern void godot_js_rtc_datachannel_connect(int p_id, void *p_obj, RTCChOnOpen p_on_open, RTCChOnMessage p_on_message, RTCChOnError p_on_error, RTCChOnClose p_on_close); +extern void godot_js_rtc_datachannel_close(int p_id); } -EMSCRIPTEN_KEEPALIVE void _emrtc_on_ch_close(void *obj) { - WebRTCDataChannelJS *peer = static_cast<WebRTCDataChannelJS *>(obj); - peer->_on_close(); +void WebRTCDataChannelJS::_on_open(void *p_obj) { + WebRTCDataChannelJS *peer = static_cast<WebRTCDataChannelJS *>(p_obj); + peer->in_buffer.resize(peer->_in_buffer_shift); } -EMSCRIPTEN_KEEPALIVE void _emrtc_on_ch_message(void *obj, uint8_t *p_data, uint32_t p_size, bool p_is_string) { - WebRTCDataChannelJS *peer = static_cast<WebRTCDataChannelJS *>(obj); - peer->_on_message(p_data, p_size, p_is_string); -} +void WebRTCDataChannelJS::_on_close(void *p_obj) { + WebRTCDataChannelJS *peer = static_cast<WebRTCDataChannelJS *>(p_obj); + peer->close(); } -void WebRTCDataChannelJS::_on_open() { - in_buffer.resize(_in_buffer_shift); +void WebRTCDataChannelJS::_on_error(void *p_obj) { + WebRTCDataChannelJS *peer = static_cast<WebRTCDataChannelJS *>(p_obj); + peer->close(); } -void WebRTCDataChannelJS::_on_close() { - close(); -} +void WebRTCDataChannelJS::_on_message(void *p_obj, const uint8_t *p_data, int p_size, int p_is_string) { + WebRTCDataChannelJS *peer = static_cast<WebRTCDataChannelJS *>(p_obj); + RingBuffer<uint8_t> &in_buffer = peer->in_buffer; -void WebRTCDataChannelJS::_on_error() { - close(); -} - -void WebRTCDataChannelJS::_on_message(uint8_t *p_data, uint32_t p_size, bool p_is_string) { ERR_FAIL_COND_MSG(in_buffer.space_left() < (int)(p_size + 5), "Buffer full! Dropping data."); uint8_t is_string = p_is_string ? 1 : 0; in_buffer.write((uint8_t *)&p_size, 4); in_buffer.write((uint8_t *)&is_string, 1); in_buffer.write(p_data, p_size); - queue_count++; + peer->queue_count++; } void WebRTCDataChannelJS::close() { in_buffer.resize(0); queue_count = 0; _was_string = false; - /* clang-format off */ - EM_ASM({ - var dict = Module.IDHandler.get($0); - if (!dict) return; - var channel = dict["channel"]; - channel.onopen = null; - channel.onclose = null; - channel.onerror = null; - channel.onmessage = null; - channel.close(); - }, _js_id); - /* clang-format on */ + godot_js_rtc_datachannel_close(_js_id); } Error WebRTCDataChannelJS::poll() { @@ -100,24 +93,7 @@ Error WebRTCDataChannelJS::poll() { } WebRTCDataChannelJS::ChannelState WebRTCDataChannelJS::get_ready_state() const { - /* clang-format off */ - return (ChannelState) EM_ASM_INT({ - var dict = Module.IDHandler.get($0); - if (!dict) return 3; // CLOSED - var channel = dict["channel"]; - switch(channel.readyState) { - case "connecting": - return 0; - case "open": - return 1; - case "closing": - return 2; - case "closed": - return 3; - } - return 3; // CLOSED - }, _js_id); - /* clang-format on */ + return (ChannelState)godot_js_rtc_datachannel_ready_state_get(_js_id); } int WebRTCDataChannelJS::get_available_packet_count() const { @@ -157,27 +133,7 @@ Error WebRTCDataChannelJS::put_packet(const uint8_t *p_buffer, int p_buffer_size ERR_FAIL_COND_V(get_ready_state() != STATE_OPEN, ERR_UNCONFIGURED); int is_bin = _write_mode == WebRTCDataChannel::WRITE_MODE_BINARY ? 1 : 0; - - /* clang-format off */ - EM_ASM({ - var dict = Module.IDHandler.get($0); - var channel = dict["channel"]; - var bytes_array = new Uint8Array($2); - var i = 0; - - for(i=0; i<$2; i++) { - bytes_array[i] = getValue($1+i, 'i8'); - } - - if ($3) { - channel.send(bytes_array.buffer); - } else { - var string = new TextDecoder("utf-8").decode(bytes_array); - channel.send(string); - } - }, _js_id, p_buffer, p_buffer_size, is_bin); - /* clang-format on */ - + godot_js_rtc_datachannel_send(_js_id, p_buffer, p_buffer_size, is_bin); return OK; } @@ -201,46 +157,20 @@ String WebRTCDataChannelJS::get_label() const { return _label; } -/* clang-format off */ -#define _JS_GET(PROP, DEF) \ -EM_ASM_INT({ \ - var dict = Module.IDHandler.get($0); \ - if (!dict || !dict["channel"]) { \ - return DEF; \ - } \ - var out = dict["channel"].PROP; \ - return out === null ? DEF : out; \ -}, _js_id) -/* clang-format on */ - bool WebRTCDataChannelJS::is_ordered() const { - return _JS_GET(ordered, true); + return godot_js_rtc_datachannel_is_ordered(_js_id); } int WebRTCDataChannelJS::get_id() const { - return _JS_GET(id, 65535); + return godot_js_rtc_datachannel_id_get(_js_id); } int WebRTCDataChannelJS::get_max_packet_life_time() const { - // Can't use macro, webkit workaround. - /* clang-format off */ - return EM_ASM_INT({ - var dict = Module.IDHandler.get($0); - if (!dict || !dict["channel"]) { - return 65535; - } - if (dict["channel"].maxRetransmitTime !== undefined) { - // Guess someone didn't appreciate the standardization process. - return dict["channel"].maxRetransmitTime; - } - var out = dict["channel"].maxPacketLifeTime; - return out === null ? 65535 : out; - }, _js_id); - /* clang-format on */ + return godot_js_rtc_datachannel_max_packet_lifetime_get(_js_id); } int WebRTCDataChannelJS::get_max_retransmits() const { - return _JS_GET(maxRetransmits, 65535); + return godot_js_rtc_datachannel_max_retransmits_get(_js_id); } String WebRTCDataChannelJS::get_protocol() const { @@ -248,7 +178,7 @@ String WebRTCDataChannelJS::get_protocol() const { } bool WebRTCDataChannelJS::is_negotiated() const { - return _JS_GET(negotiated, false); + return godot_js_rtc_datachannel_is_negotiated(_js_id); } WebRTCDataChannelJS::WebRTCDataChannelJS() { @@ -264,101 +194,22 @@ WebRTCDataChannelJS::WebRTCDataChannelJS(int js_id) { _write_mode = WRITE_MODE_BINARY; _js_id = js_id; - /* clang-format off */ - EM_ASM({ - var c_ptr = $0; - var dict = Module.IDHandler.get($1); - if (!dict) return; - var channel = dict["channel"]; - dict["ptr"] = c_ptr; - - channel.binaryType = "arraybuffer"; - channel.onopen = function (evt) { - ccall("_emrtc_on_ch_open", - "void", - ["number"], - [c_ptr] - ); - }; - channel.onclose = function (evt) { - ccall("_emrtc_on_ch_close", - "void", - ["number"], - [c_ptr] - ); - }; - channel.onerror = function (evt) { - ccall("_emrtc_on_ch_error", - "void", - ["number"], - [c_ptr] - ); - }; - channel.onmessage = function(event) { - var buffer; - var is_string = 0; - if (event.data instanceof ArrayBuffer) { - buffer = new Uint8Array(event.data); - } else if (event.data instanceof Blob) { - console.error("Blob type not supported"); - return; - } else if (typeof event.data === "string") { - is_string = 1; - var enc = new TextEncoder("utf-8"); - buffer = new Uint8Array(enc.encode(event.data)); - } else { - console.error("Unknown message type"); - return; - } - var len = buffer.length*buffer.BYTES_PER_ELEMENT; - var out = _malloc(len); - HEAPU8.set(buffer, out); - ccall("_emrtc_on_ch_message", - "void", - ["number", "number", "number", "number"], - [c_ptr, out, len, is_string] - ); - _free(out); - } - - }, this, js_id); + godot_js_rtc_datachannel_connect(js_id, this, &_on_open, &_on_message, &_on_error, &_on_close); // Parse label - char *str; - str = (char *)EM_ASM_INT({ - var dict = Module.IDHandler.get($0); - if (!dict || !dict["channel"]) return 0; - var str = dict["channel"].label; - var len = lengthBytesUTF8(str)+1; - var ptr = _malloc(str); - stringToUTF8(str, ptr, len+1); - return ptr; - }, js_id); - if(str != nullptr) { - _label.parse_utf8(str); - EM_ASM({ _free($0) }, str); + char *label = godot_js_rtc_datachannel_label_get(js_id); + if (label) { + _label.parse_utf8(label); + free(label); } - str = (char *)EM_ASM_INT({ - var dict = Module.IDHandler.get($0); - if (!dict || !dict["channel"]) return 0; - var str = dict["channel"].protocol; - var len = lengthBytesUTF8(str)+1; - var ptr = _malloc(str); - stringToUTF8(str, ptr, len+1); - return ptr; - }, js_id); - if(str != nullptr) { - _protocol.parse_utf8(str); - EM_ASM({ _free($0) }, str); + char *protocol = godot_js_rtc_datachannel_protocol_get(js_id); + if (protocol) { + _protocol.parse_utf8(protocol); + free(protocol); } - /* clang-format on */ } WebRTCDataChannelJS::~WebRTCDataChannelJS() { close(); - /* clang-format off */ - EM_ASM({ - Module.IDHandler.remove($0); - }, _js_id); - /* clang-format on */ -}; + godot_js_rtc_datachannel_destroy(_js_id); +} #endif diff --git a/modules/webrtc/webrtc_data_channel_js.h b/modules/webrtc/webrtc_data_channel_js.h index 7545910e66..e251760019 100644 --- a/modules/webrtc/webrtc_data_channel_js.h +++ b/modules/webrtc/webrtc_data_channel_js.h @@ -54,12 +54,12 @@ private: int queue_count; uint8_t packet_buffer[PACKET_BUFFER_SIZE]; -public: - void _on_open(); - void _on_close(); - void _on_error(); - void _on_message(uint8_t *p_data, uint32_t p_size, bool p_is_string); + static void _on_open(void *p_obj); + static void _on_close(void *p_obj); + static void _on_error(void *p_obj); + static void _on_message(void *p_obj, const uint8_t *p_data, int p_size, int p_is_string); +public: virtual void set_write_mode(WriteMode mode) override; virtual WriteMode get_write_mode() const override; virtual bool was_string_packet() const override; diff --git a/modules/webrtc/webrtc_peer_connection_js.cpp b/modules/webrtc/webrtc_peer_connection_js.cpp index 593c3a5162..ad9b46a8af 100644 --- a/modules/webrtc/webrtc_peer_connection_js.cpp +++ b/modules/webrtc/webrtc_peer_connection_js.cpp @@ -37,116 +37,32 @@ #include "core/io/json.h" #include "emscripten.h" -extern "C" { -EMSCRIPTEN_KEEPALIVE void _emrtc_on_ice_candidate(void *obj, char *p_MidName, int p_MlineIndexName, char *p_sdpName) { - WebRTCPeerConnectionJS *peer = static_cast<WebRTCPeerConnectionJS *>(obj); - peer->emit_signal("ice_candidate_created", String(p_MidName), p_MlineIndexName, String(p_sdpName)); +void WebRTCPeerConnectionJS::_on_ice_candidate(void *p_obj, const char *p_mid_name, int p_mline_idx, const char *p_candidate) { + WebRTCPeerConnectionJS *peer = static_cast<WebRTCPeerConnectionJS *>(p_obj); + peer->emit_signal("ice_candidate_created", String(p_mid_name), p_mline_idx, String(p_candidate)); } -EMSCRIPTEN_KEEPALIVE void _emrtc_session_description_created(void *obj, char *p_type, char *p_offer) { - WebRTCPeerConnectionJS *peer = static_cast<WebRTCPeerConnectionJS *>(obj); - peer->emit_signal("session_description_created", String(p_type), String(p_offer)); +void WebRTCPeerConnectionJS::_on_session_created(void *p_obj, const char *p_type, const char *p_session) { + WebRTCPeerConnectionJS *peer = static_cast<WebRTCPeerConnectionJS *>(p_obj); + peer->emit_signal("session_description_created", String(p_type), String(p_session)); } -EMSCRIPTEN_KEEPALIVE void _emrtc_on_connection_state_changed(void *obj) { - WebRTCPeerConnectionJS *peer = static_cast<WebRTCPeerConnectionJS *>(obj); - peer->_on_connection_state_changed(); +void WebRTCPeerConnectionJS::_on_connection_state_changed(void *p_obj, int p_state) { + WebRTCPeerConnectionJS *peer = static_cast<WebRTCPeerConnectionJS *>(p_obj); + peer->_conn_state = (ConnectionState)p_state; } -EMSCRIPTEN_KEEPALIVE void _emrtc_on_error() { +void WebRTCPeerConnectionJS::_on_error(void *p_obj) { ERR_PRINT("RTCPeerConnection error!"); } -EMSCRIPTEN_KEEPALIVE void _emrtc_emit_channel(void *obj, int p_id) { - WebRTCPeerConnectionJS *peer = static_cast<WebRTCPeerConnectionJS *>(obj); +void WebRTCPeerConnectionJS::_on_data_channel(void *p_obj, int p_id) { + WebRTCPeerConnectionJS *peer = static_cast<WebRTCPeerConnectionJS *>(p_obj); peer->emit_signal("data_channel_received", Ref<WebRTCDataChannelJS>(new WebRTCDataChannelJS(p_id))); } -} - -void _emrtc_create_pc(int p_id, const Dictionary &p_config) { - String config = JSON::print(p_config); - /* clang-format off */ - EM_ASM({ - var dict = Module.IDHandler.get($0); - var c_ptr = dict["ptr"]; - var config = JSON.parse(UTF8ToString($1)); - // Setup local connaction - var conn = null; - try { - conn = new RTCPeerConnection(config); - } catch (e) { - console.log(e); - return; - } - conn.oniceconnectionstatechange = function(event) { - if (!Module.IDHandler.get($0)) return; - ccall("_emrtc_on_connection_state_changed", "void", ["number"], [c_ptr]); - }; - conn.onicecandidate = function(event) { - if (!Module.IDHandler.get($0)) return; - if (!event.candidate) return; - - var c = event.candidate; - // should emit on ice candidate - ccall("_emrtc_on_ice_candidate", - "void", - ["number", "string", "number", "string"], - [c_ptr, c.sdpMid, c.sdpMLineIndex, c.candidate] - ); - }; - conn.ondatachannel = function (evt) { - var dict = Module.IDHandler.get($0); - if (!dict) { - return; - } - var id = Module.IDHandler.add({"channel": evt.channel, "ptr": null}); - ccall("_emrtc_emit_channel", - "void", - ["number", "number"], - [c_ptr, id] - ); - }; - dict["conn"] = conn; - }, p_id, config.utf8().get_data()); - /* clang-format on */ -} - -void WebRTCPeerConnectionJS::_on_connection_state_changed() { - /* clang-format off */ - _conn_state = (ConnectionState)EM_ASM_INT({ - var dict = Module.IDHandler.get($0); - if (!dict) return 5; // CLOSED - var conn = dict["conn"]; - switch(conn.iceConnectionState) { - case "new": - return 0; - case "checking": - return 1; - case "connected": - case "completed": - return 2; - case "disconnected": - return 3; - case "failed": - return 4; - case "closed": - return 5; - } - return 5; // CLOSED - }, _js_id); - /* clang-format on */ -} void WebRTCPeerConnectionJS::close() { - /* clang-format off */ - EM_ASM({ - var dict = Module.IDHandler.get($0); - if (!dict) return; - if (dict["conn"]) { - dict["conn"].close(); - } - }, _js_id); - /* clang-format on */ + godot_js_rtc_pc_close(_js_id); _conn_state = STATE_CLOSED; } @@ -154,46 +70,12 @@ Error WebRTCPeerConnectionJS::create_offer() { ERR_FAIL_COND_V(_conn_state != STATE_NEW, FAILED); _conn_state = STATE_CONNECTING; - /* clang-format off */ - EM_ASM({ - var dict = Module.IDHandler.get($0); - var conn = dict["conn"]; - var c_ptr = dict["ptr"]; - var onError = function(error) { - console.error(error); - ccall("_emrtc_on_error", "void", [], []); - }; - var onCreated = function(offer) { - ccall("_emrtc_session_description_created", - "void", - ["number", "string", "string"], - [c_ptr, offer.type, offer.sdp] - ); - }; - conn.createOffer().then(onCreated).catch(onError); - }, _js_id); - /* clang-format on */ + godot_js_rtc_pc_offer_create(_js_id, this, &_on_session_created, &_on_error); return OK; } Error WebRTCPeerConnectionJS::set_local_description(String type, String sdp) { - /* clang-format off */ - EM_ASM({ - var dict = Module.IDHandler.get($0); - var conn = dict["conn"]; - var c_ptr = dict["ptr"]; - var type = UTF8ToString($1); - var sdp = UTF8ToString($2); - var onError = function(error) { - console.error(error); - ccall("_emrtc_on_error", "void", [], []); - }; - conn.setLocalDescription({ - "sdp": sdp, - "type": type - }).catch(onError); - }, _js_id, type.utf8().get_data(), sdp.utf8().get_data()); - /* clang-format on */ + godot_js_rtc_pc_local_description_set(_js_id, type.utf8().get_data(), sdp.utf8().get_data(), this, &_on_error); return OK; } @@ -202,83 +84,32 @@ Error WebRTCPeerConnectionJS::set_remote_description(String type, String sdp) { ERR_FAIL_COND_V(_conn_state != STATE_NEW, FAILED); _conn_state = STATE_CONNECTING; } - /* clang-format off */ - EM_ASM({ - var dict = Module.IDHandler.get($0); - var conn = dict["conn"]; - var c_ptr = dict["ptr"]; - var type = UTF8ToString($1); - var sdp = UTF8ToString($2); - - var onError = function(error) { - console.error(error); - ccall("_emrtc_on_error", "void", [], []); - }; - var onCreated = function(offer) { - ccall("_emrtc_session_description_created", - "void", - ["number", "string", "string"], - [c_ptr, offer.type, offer.sdp] - ); - }; - var onSet = function() { - if (type != "offer") { - return; - } - conn.createAnswer().then(onCreated); - }; - conn.setRemoteDescription({ - "sdp": sdp, - "type": type - }).then(onSet).catch(onError); - }, _js_id, type.utf8().get_data(), sdp.utf8().get_data()); - /* clang-format on */ + godot_js_rtc_pc_remote_description_set(_js_id, type.utf8().get_data(), sdp.utf8().get_data(), this, &_on_session_created, &_on_error); return OK; } Error WebRTCPeerConnectionJS::add_ice_candidate(String sdpMidName, int sdpMlineIndexName, String sdpName) { - /* clang-format off */ - EM_ASM({ - var dict = Module.IDHandler.get($0); - var conn = dict["conn"]; - var c_ptr = dict["ptr"]; - var sdpMidName = UTF8ToString($1); - var sdpMlineIndexName = UTF8ToString($2); - var sdpName = UTF8ToString($3); - conn.addIceCandidate(new RTCIceCandidate({ - "candidate": sdpName, - "sdpMid": sdpMidName, - "sdpMlineIndex": sdpMlineIndexName - })); - }, _js_id, sdpMidName.utf8().get_data(), sdpMlineIndexName, sdpName.utf8().get_data()); - /* clang-format on */ + godot_js_rtc_pc_ice_candidate_add(_js_id, sdpMidName.utf8().get_data(), sdpMlineIndexName, sdpName.utf8().get_data()); return OK; } Error WebRTCPeerConnectionJS::initialize(Dictionary p_config) { - _emrtc_create_pc(_js_id, p_config); - return OK; + if (_js_id) { + godot_js_rtc_pc_destroy(_js_id); + _js_id = 0; + } + _conn_state = STATE_NEW; + + String config = JSON::print(p_config); + _js_id = godot_js_rtc_pc_create(config.utf8().get_data(), this, &_on_connection_state_changed, &_on_ice_candidate, &_on_data_channel); + return _js_id ? OK : FAILED; } Ref<WebRTCDataChannel> WebRTCPeerConnectionJS::create_data_channel(String p_channel, Dictionary p_channel_config) { + ERR_FAIL_COND_V(_conn_state != STATE_NEW, nullptr); + String config = JSON::print(p_channel_config); - /* clang-format off */ - int id = EM_ASM_INT({ - try { - var dict = Module.IDHandler.get($0); - if (!dict) return 0; - var label = UTF8ToString($1); - var config = JSON.parse(UTF8ToString($2)); - var conn = dict["conn"]; - return Module.IDHandler.add({ - "channel": conn.createDataChannel(label, config), - "ptr": null - }) - } catch (e) { - return 0; - } - }, _js_id, p_channel.utf8().get_data(), config.utf8().get_data()); - /* clang-format on */ + int id = godot_js_rtc_pc_datachannel_create(_js_id, p_channel.utf8().get_data(), config.utf8().get_data()); ERR_FAIL_COND_V(id == 0, nullptr); return memnew(WebRTCDataChannelJS(id)); } @@ -293,22 +124,17 @@ WebRTCPeerConnection::ConnectionState WebRTCPeerConnectionJS::get_connection_sta WebRTCPeerConnectionJS::WebRTCPeerConnectionJS() { _conn_state = STATE_NEW; + _js_id = 0; - /* clang-format off */ - _js_id = EM_ASM_INT({ - return Module.IDHandler.add({"conn": null, "ptr": $0}); - }, this); - /* clang-format on */ Dictionary config; - _emrtc_create_pc(_js_id, config); + initialize(config); } WebRTCPeerConnectionJS::~WebRTCPeerConnectionJS() { close(); - /* clang-format off */ - EM_ASM({ - Module.IDHandler.remove($0); - }, _js_id); - /* clang-format on */ + if (_js_id) { + godot_js_rtc_pc_destroy(_js_id); + _js_id = 0; + } }; #endif diff --git a/modules/webrtc/webrtc_peer_connection_js.h b/modules/webrtc/webrtc_peer_connection_js.h index cdaf3068e3..e33dd5f259 100644 --- a/modules/webrtc/webrtc_peer_connection_js.h +++ b/modules/webrtc/webrtc_peer_connection_js.h @@ -35,16 +35,37 @@ #include "webrtc_peer_connection.h" +extern "C" { +typedef void (*RTCOnIceConnectionStateChange)(void *p_obj, int p_state); +typedef void (*RTCOnIceCandidate)(void *p_obj, const char *p_mid, int p_mline_idx, const char *p_candidate); +typedef void (*RTCOnDataChannel)(void *p_obj, int p_id); +typedef void (*RTCOnSession)(void *p_obj, const char *p_type, const char *p_sdp); +typedef void (*RTCOnError)(void *p_obj); +extern int godot_js_rtc_pc_create(const char *p_config, void *p_obj, RTCOnIceConnectionStateChange p_on_state_change, RTCOnIceCandidate p_on_candidate, RTCOnDataChannel p_on_datachannel); +extern void godot_js_rtc_pc_close(int p_id); +extern void godot_js_rtc_pc_destroy(int p_id); +extern void godot_js_rtc_pc_offer_create(int p_id, void *p_obj, RTCOnSession p_on_session, RTCOnError p_on_error); +extern void godot_js_rtc_pc_local_description_set(int p_id, const char *p_type, const char *p_sdp, void *p_obj, RTCOnError p_on_error); +extern void godot_js_rtc_pc_remote_description_set(int p_id, const char *p_type, const char *p_sdp, void *p_obj, RTCOnSession p_on_session, RTCOnError p_on_error); +extern void godot_js_rtc_pc_ice_candidate_add(int p_id, const char *p_mid_name, int p_mline_idx, const char *p_sdo); +extern int godot_js_rtc_pc_datachannel_create(int p_id, const char *p_label, const char *p_config); +} + class WebRTCPeerConnectionJS : public WebRTCPeerConnection { private: int _js_id; ConnectionState _conn_state; + static void _on_connection_state_changed(void *p_obj, int p_state); + static void _on_ice_candidate(void *p_obj, const char *p_mid_name, int p_mline_idx, const char *p_candidate); + static void _on_data_channel(void *p_obj, int p_channel); + static void _on_session_created(void *p_obj, const char *p_type, const char *p_session); + static void _on_error(void *p_obj); + public: static WebRTCPeerConnection *_create() { return memnew(WebRTCPeerConnectionJS); } static void make_default() { WebRTCPeerConnection::_create = WebRTCPeerConnectionJS::_create; } - void _on_connection_state_changed(); virtual ConnectionState get_connection_state() const; virtual Error initialize(Dictionary configuration = Dictionary()); diff --git a/modules/websocket/SCsub b/modules/websocket/SCsub index af60055855..13e51a39c0 100644 --- a/modules/websocket/SCsub +++ b/modules/websocket/SCsub @@ -3,11 +3,13 @@ Import("env") Import("env_modules") -# Thirdparty source files - env_ws = env_modules.Clone() -if env["builtin_wslay"] and not env["platform"] == "javascript": # already builtin for javascript +if env["platform"] == "javascript": + # Our JavaScript/C++ interface. + env.AddJSLibraries(["library_godot_websocket.js"]) +elif env["builtin_wslay"]: + # Thirdparty source files wslay_dir = "#thirdparty/wslay/" wslay_sources = [ "wslay_net.c", diff --git a/modules/websocket/emws_client.cpp b/modules/websocket/emws_client.cpp index 93d60dca08..d6e00a26af 100644 --- a/modules/websocket/emws_client.cpp +++ b/modules/websocket/emws_client.cpp @@ -35,14 +35,13 @@ #include "core/io/ip.h" #include "emscripten.h" -extern "C" { -EMSCRIPTEN_KEEPALIVE void _esws_on_connect(void *obj, char *proto) { +void EMWSClient::_esws_on_connect(void *obj, char *proto) { EMWSClient *client = static_cast<EMWSClient *>(obj); client->_is_connecting = false; client->_on_connect(String(proto)); } -EMSCRIPTEN_KEEPALIVE void _esws_on_message(void *obj, uint8_t *p_data, int p_data_size, int p_is_string) { +void EMWSClient::_esws_on_message(void *obj, const uint8_t *p_data, int p_data_size, int p_is_string) { EMWSClient *client = static_cast<EMWSClient *>(obj); Error err = static_cast<EMWSPeer *>(*client->get_peer(1))->read_msg(p_data, p_data_size, p_is_string == 1); @@ -50,21 +49,26 @@ EMSCRIPTEN_KEEPALIVE void _esws_on_message(void *obj, uint8_t *p_data, int p_dat client->_on_peer_packet(); } -EMSCRIPTEN_KEEPALIVE void _esws_on_error(void *obj) { +void EMWSClient::_esws_on_error(void *obj) { EMWSClient *client = static_cast<EMWSClient *>(obj); client->_is_connecting = false; client->_on_error(); } -EMSCRIPTEN_KEEPALIVE void _esws_on_close(void *obj, int code, char *reason, int was_clean) { +void EMWSClient::_esws_on_close(void *obj, int code, const char *reason, int was_clean) { EMWSClient *client = static_cast<EMWSClient *>(obj); client->_on_close_request(code, String(reason)); client->_is_connecting = false; + client->disconnect_from_host(); client->_on_disconnect(was_clean != 0); } -} Error EMWSClient::connect_to_host(String p_host, String p_path, uint16_t p_port, bool p_ssl, const Vector<String> p_protocols, const Vector<String> p_custom_headers) { + if (_js_id) { + godot_js_websocket_destroy(_js_id); + _js_id = 0; + } + String proto_string; for (int i = 0; i < p_protocols.size(); i++) { if (i != 0) @@ -84,106 +88,17 @@ Error EMWSClient::connect_to_host(String p_host, String p_path, uint16_t p_port, } } str += p_host + ":" + itos(p_port) + p_path; - _is_connecting = true; - /* clang-format off */ - int peer_sock = EM_ASM_INT({ - var proto_str = UTF8ToString($2); - var socket = null; - try { - if (proto_str) { - socket = new WebSocket(UTF8ToString($1), proto_str.split(",")); - } else { - socket = new WebSocket(UTF8ToString($1)); - } - } catch (e) { - return -1; - } - var c_ptr = Module.IDHandler.get($0); - socket.binaryType = "arraybuffer"; - - // Connection opened - socket.addEventListener("open", function (event) { - if (!Module.IDHandler.has($0)) - return; // Godot Object is gone! - ccall("_esws_on_connect", - "void", - ["number", "string"], - [c_ptr, socket.protocol] - ); - }); - - // Listen for messages - socket.addEventListener("message", function (event) { - if (!Module.IDHandler.has($0)) - return; // Godot Object is gone! - var buffer; - var is_string = 0; - if (event.data instanceof ArrayBuffer) { - - buffer = new Uint8Array(event.data); - - } else if (event.data instanceof Blob) { - - alert("Blob type not supported"); - return; - - } else if (typeof event.data === "string") { - - is_string = 1; - var enc = new TextEncoder("utf-8"); - buffer = new Uint8Array(enc.encode(event.data)); - - } else { - - alert("Unknown message type"); - return; - - } - var len = buffer.length*buffer.BYTES_PER_ELEMENT; - var out = _malloc(len); - HEAPU8.set(buffer, out); - ccall("_esws_on_message", - "void", - ["number", "number", "number", "number"], - [c_ptr, out, len, is_string] - ); - _free(out); - }); - - socket.addEventListener("error", function (event) { - if (!Module.IDHandler.has($0)) - return; // Godot Object is gone! - ccall("_esws_on_error", - "void", - ["number"], - [c_ptr] - ); - }); - - socket.addEventListener("close", function (event) { - if (!Module.IDHandler.has($0)) - return; // Godot Object is gone! - var was_clean = 0; - if (event.wasClean) - was_clean = 1; - ccall("_esws_on_close", - "void", - ["number", "number", "string", "number"], - [c_ptr, event.code, event.reason, was_clean] - ); - }); - - return Module.IDHandler.add(socket); - }, _js_id, str.utf8().get_data(), proto_string.utf8().get_data()); - /* clang-format on */ - if (peer_sock == -1) + + _js_id = godot_js_websocket_create(this, str.utf8().get_data(), proto_string.utf8().get_data(), &_esws_on_connect, &_esws_on_message, &_esws_on_error, &_esws_on_close); + if (!_js_id) { return FAILED; + } - static_cast<Ref<EMWSPeer>>(_peer)->set_sock(peer_sock, _in_buf_size, _in_pkt_size); + static_cast<Ref<EMWSPeer>>(_peer)->set_sock(_js_id, _in_buf_size, _in_pkt_size); return OK; -}; +} void EMWSClient::poll() { } @@ -200,19 +115,19 @@ NetworkedMultiplayerPeer::ConnectionStatus EMWSClient::get_connection_status() c } return CONNECTION_DISCONNECTED; -}; +} void EMWSClient::disconnect_from_host(int p_code, String p_reason) { _peer->close(p_code, p_reason); -}; +} IP_Address EMWSClient::get_connected_host() const { ERR_FAIL_V_MSG(IP_Address(), "Not supported in HTML5 export."); -}; +} uint16_t EMWSClient::get_connected_port() const { ERR_FAIL_V_MSG(0, "Not supported in HTML5 export."); -}; +} int EMWSClient::get_max_packet_size() const { return (1 << _in_buf_size) - PROTO_SIZE; @@ -227,24 +142,17 @@ Error EMWSClient::set_buffers(int p_in_buffer, int p_in_packets, int p_out_buffe EMWSClient::EMWSClient() { _in_buf_size = DEF_BUF_SHIFT; _in_pkt_size = DEF_PKT_SHIFT; - _is_connecting = false; _peer = Ref<EMWSPeer>(memnew(EMWSPeer)); - /* clang-format off */ - _js_id = EM_ASM_INT({ - return Module.IDHandler.add($0); - }, this); - /* clang-format on */ -}; + _js_id = 0; +} EMWSClient::~EMWSClient() { disconnect_from_host(); _peer = Ref<EMWSPeer>(); - /* clang-format off */ - EM_ASM({ - Module.IDHandler.remove($0); - }, _js_id); - /* clang-format on */ -}; + if (_js_id) { + godot_js_websocket_destroy(_js_id); + } +} #endif // JAVASCRIPT_ENABLED diff --git a/modules/websocket/emws_client.h b/modules/websocket/emws_client.h index 58973c52fa..0123c37457 100644 --- a/modules/websocket/emws_client.h +++ b/modules/websocket/emws_client.h @@ -41,13 +41,17 @@ class EMWSClient : public WebSocketClient { GDCIIMPL(EMWSClient, WebSocketClient); private: + int _js_id; + bool _is_connecting; int _in_buf_size; int _in_pkt_size; - int _js_id; -public: - bool _is_connecting; + static void _esws_on_connect(void *obj, char *proto); + static void _esws_on_message(void *obj, const uint8_t *p_data, int p_data_size, int p_is_string); + static void _esws_on_error(void *obj); + static void _esws_on_close(void *obj, int code, const char *reason, int was_clean); +public: Error set_buffers(int p_in_buffer, int p_in_packets, int p_out_buffer, int p_out_packets); Error connect_to_host(String p_host, String p_path, uint16_t p_port, bool p_ssl, const Vector<String> p_protocol = Vector<String>(), const Vector<String> p_custom_headers = Vector<String>()); Ref<WebSocketPeer> get_peer(int p_peer_id) const; diff --git a/modules/websocket/emws_peer.cpp b/modules/websocket/emws_peer.cpp index 749f45451a..5dcfba5567 100644 --- a/modules/websocket/emws_peer.cpp +++ b/modules/websocket/emws_peer.cpp @@ -47,38 +47,14 @@ EMWSPeer::WriteMode EMWSPeer::get_write_mode() const { return write_mode; } -Error EMWSPeer::read_msg(uint8_t *p_data, uint32_t p_size, bool p_is_string) { +Error EMWSPeer::read_msg(const uint8_t *p_data, uint32_t p_size, bool p_is_string) { uint8_t is_string = p_is_string ? 1 : 0; return _in_buffer.write_packet(p_data, p_size, &is_string); } Error EMWSPeer::put_packet(const uint8_t *p_buffer, int p_buffer_size) { int is_bin = write_mode == WebSocketPeer::WRITE_MODE_BINARY ? 1 : 0; - - /* clang-format off */ - EM_ASM({ - var sock = Module.IDHandler.get($0); - var bytes_array = new Uint8Array($2); - var i = 0; - - for(i=0; i<$2; i++) { - bytes_array[i] = getValue($1+i, 'i8'); - } - - try { - if ($3) { - sock.send(bytes_array.buffer); - } else { - var string = new TextDecoder("utf-8").decode(bytes_array); - sock.send(string); - } - } catch (e) { - return 1; - } - return 0; - }, peer_sock, p_buffer, p_buffer_size, is_bin); - /* clang-format on */ - + godot_js_websocket_send(peer_sock, p_buffer, p_buffer_size, is_bin); return OK; }; @@ -110,15 +86,7 @@ bool EMWSPeer::is_connected_to_host() const { void EMWSPeer::close(int p_code, String p_reason) { if (peer_sock != -1) { - /* clang-format off */ - EM_ASM({ - var sock = Module.IDHandler.get($0); - var code = $1; - var reason = UTF8ToString($2); - sock.close(code, reason); - Module.IDHandler.remove($0); - }, peer_sock, p_code, p_reason.utf8().get_data()); - /* clang-format on */ + godot_js_websocket_close(peer_sock, p_code, p_reason.utf8().get_data()); } _is_string = 0; _in_buffer.clear(); diff --git a/modules/websocket/emws_peer.h b/modules/websocket/emws_peer.h index c94d7e9148..2291a32bbc 100644 --- a/modules/websocket/emws_peer.h +++ b/modules/websocket/emws_peer.h @@ -40,6 +40,18 @@ #include "packet_buffer.h" #include "websocket_peer.h" +extern "C" { +typedef void (*WSOnOpen)(void *p_ref, char *p_protocol); +typedef void (*WSOnMessage)(void *p_ref, const uint8_t *p_buf, int p_buf_len, int p_is_string); +typedef void (*WSOnClose)(void *p_ref, int p_code, const char *p_reason, int p_is_clean); +typedef void (*WSOnError)(void *p_ref); + +extern int godot_js_websocket_create(void *p_ref, const char *p_url, const char *p_proto, WSOnOpen p_on_open, WSOnMessage p_on_message, WSOnError p_on_error, WSOnClose p_on_close); +extern int godot_js_websocket_send(int p_id, const uint8_t *p_buf, int p_buf_len, int p_raw); +extern void godot_js_websocket_close(int p_id, int p_code, const char *p_reason); +extern void godot_js_websocket_destroy(int p_id); +} + class EMWSPeer : public WebSocketPeer { GDCIIMPL(EMWSPeer, WebSocketPeer); @@ -52,7 +64,7 @@ private: uint8_t _is_string; public: - Error read_msg(uint8_t *p_data, uint32_t p_size, bool p_is_string); + Error read_msg(const uint8_t *p_data, uint32_t p_size, bool p_is_string); void set_sock(int p_sock, unsigned int p_in_buf_size, unsigned int p_in_pkt_size); virtual int get_available_packet_count() const; virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size); diff --git a/modules/websocket/library_godot_websocket.js b/modules/websocket/library_godot_websocket.js new file mode 100644 index 0000000000..f7d3195807 --- /dev/null +++ b/modules/websocket/library_godot_websocket.js @@ -0,0 +1,187 @@ +/*************************************************************************/ +/* library_godot_websocket.js */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 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. */ +/*************************************************************************/ + +var GodotWebSocket = { + + // Our socket implementation that forwards events to C++. + $GodotWebSocket__deps: ['$IDHandler'], + $GodotWebSocket: { + // Connection opened, report selected protocol + _onopen: function(p_id, callback, event) { + const ref = IDHandler.get(p_id); + if (!ref) { + return; // Godot object is gone. + } + let c_str = GodotOS.allocString(ref.protocol); + callback(c_str); + _free(c_str); + }, + + // Message received, report content and type (UTF8 vs binary) + _onmessage: function(p_id, callback, event) { + const ref = IDHandler.get(p_id); + if (!ref) { + return; // Godot object is gone. + } + var buffer; + var is_string = 0; + if (event.data instanceof ArrayBuffer) { + buffer = new Uint8Array(event.data); + } else if (event.data instanceof Blob) { + alert("Blob type not supported"); + return; + } else if (typeof event.data === "string") { + is_string = 1; + var enc = new TextEncoder("utf-8"); + buffer = new Uint8Array(enc.encode(event.data)); + } else { + alert("Unknown message type"); + return; + } + var len = buffer.length*buffer.BYTES_PER_ELEMENT; + var out = _malloc(len); + HEAPU8.set(buffer, out); + callback(out, len, is_string); + _free(out); + }, + + // An error happened, 'onclose' will be called after this. + _onerror: function(p_id, callback, event) { + const ref = IDHandler.get(p_id); + if (!ref) { + return; // Godot object is gone. + } + callback(); + }, + + // Connection is closed, this is always fired. Report close code, reason, and clean status. + _onclose: function(p_id, callback, event) { + const ref = IDHandler.get(p_id); + if (!ref) { + return; // Godot object is gone. + } + let c_str = GodotOS.allocString(event.reason); + callback(event.code, c_str, event.wasClean ? 1 : 0); + _free(c_str); + }, + + // Send a message + send: function(p_id, p_data) { + const ref = IDHandler.get(p_id); + if (!ref || ref.readyState != ref.OPEN) { + return 1; // Godot object is gone or socket is not in a ready state. + } + ref.send(p_data); + return 0; + }, + + create: function(socket, p_on_open, p_on_message, p_on_error, p_on_close) { + const id = IDHandler.add(socket); + socket.onopen = GodotWebSocket._onopen.bind(null, id, p_on_open); + socket.onmessage = GodotWebSocket._onmessage.bind(null, id, p_on_message); + socket.onerror = GodotWebSocket._onerror.bind(null, id, p_on_error); + socket.onclose = GodotWebSocket._onclose.bind(null, id, p_on_close); + return id; + }, + + // Closes the JavaScript WebSocket (if not already closing) associated to a given C++ object. + close: function(p_id, p_code, p_reason) { + const ref = IDHandler.get(p_id); + if (ref && ref.readyState < ref.CLOSING) { + const code = p_code; + const reason = UTF8ToString(p_reason); + ref.close(code, reason); + } + }, + + // Deletes the reference to a C++ object (closing any connected socket if necessary). + destroy: function(p_id) { + const ref = IDHandler.get(p_id); + if (!ref) { + return; + } + GodotWebSocket.close(p_id, 1001, ''); + IDHandler.remove(p_id); + ref.onopen = null; + ref.onmessage = null; + ref.onerror = null; + ref.onclose = null; + }, + }, + + godot_js_websocket_create: function(p_ref, p_url, p_proto, p_on_open, p_on_message, p_on_error, p_on_close) { + const on_open = GodotOS.get_func(p_on_open).bind(null, p_ref); + const on_message = GodotOS.get_func(p_on_message).bind(null, p_ref); + const on_error = GodotOS.get_func(p_on_error).bind(null, p_ref); + const on_close = GodotOS.get_func(p_on_close).bind(null, p_ref); + const url = UTF8ToString(p_url); + const protos = UTF8ToString(p_proto); + var socket = null; + try { + if (protos) { + socket = new WebSocket(url, protos.split(",")); + } else { + socket = new WebSocket(url); + } + } catch (e) { + return 0; + } + socket.binaryType = "arraybuffer"; + return GodotWebSocket.create(socket, on_open, on_message, on_error, on_close); + }, + + godot_js_websocket_send: function(p_id, p_buf, p_buf_len, p_raw) { + var bytes_array = new Uint8Array(p_buf_len); + var i = 0; + for(i = 0; i < p_buf_len; i++) { + bytes_array[i] = getValue(p_buf + i, 'i8'); + } + var out = bytes_array; + if (p_raw) { + out = bytes_array.buffer; + } else { + out = new TextDecoder("utf-8").decode(bytes_array); + } + return GodotWebSocket.send(p_id, out); + }, + + godot_js_websocket_close: function(p_id, p_code, p_reason) { + const code = p_code; + const reason = UTF8ToString(p_reason); + GodotWebSocket.close(p_id, code, reason); + }, + + godot_js_websocket_destroy: function(p_id) { + GodotWebSocket.destroy(p_id); + }, +}; + +autoAddDeps(GodotWebSocket, '$GodotWebSocket') +mergeInto(LibraryManager.library, GodotWebSocket); diff --git a/platform/iphone/SCsub b/platform/iphone/SCsub index 1dd37dabe5..0a558f8e3d 100644 --- a/platform/iphone/SCsub +++ b/platform/iphone/SCsub @@ -8,9 +8,6 @@ iphone_lib = [ "main.m", "app_delegate.mm", "view_controller.mm", - "game_center.mm", - "in_app_store.mm", - "icloud.mm", "ios.mm", "vulkan_context_iphone.mm", "display_server_iphone.mm", diff --git a/platform/iphone/app_delegate.mm b/platform/iphone/app_delegate.mm index c9891309fe..c1942e77dd 100644 --- a/platform/iphone/app_delegate.mm +++ b/platform/iphone/app_delegate.mm @@ -36,6 +36,7 @@ #include "os_iphone.h" #import "view_controller.h" +#import <AVFoundation/AVFoundation.h> #import <AudioToolbox/AudioServices.h> #define kRenderingFrequency 60 diff --git a/platform/iphone/detect.py b/platform/iphone/detect.py index ab453c353f..0456458326 100644 --- a/platform/iphone/detect.py +++ b/platform/iphone/detect.py @@ -35,9 +35,6 @@ def get_opts(): " validation layers)", False, ), - BoolVariable("game_center", "Support for game center", True), - BoolVariable("store_kit", "Support for in-app store", True), - BoolVariable("icloud", "Support for iCloud", True), BoolVariable("ios_exceptions", "Enable exceptions", False), ("ios_triple", "Triple for ios toolchain", ""), ] @@ -222,18 +219,6 @@ def configure(env): ] ) - # Feature options - if env["game_center"]: - env.Append(CPPDEFINES=["GAME_CENTER_ENABLED"]) - env.Append(LINKFLAGS=["-framework", "GameKit"]) - - if env["store_kit"]: - env.Append(CPPDEFINES=["STOREKIT_ENABLED"]) - env.Append(LINKFLAGS=["-framework", "StoreKit"]) - - if env["icloud"]: - env.Append(CPPDEFINES=["ICLOUD_ENABLED"]) - env.Prepend( CPPPATH=[ "$IPHONESDK/usr/include", diff --git a/platform/iphone/export/export.cpp b/platform/iphone/export/export.cpp index cf46883b92..cbef136247 100644 --- a/platform/iphone/export/export.cpp +++ b/platform/iphone/export/export.cpp @@ -43,6 +43,7 @@ #include "editor/editor_settings.h" #include "main/splash.gen.h" #include "platform/iphone/logo.gen.h" +#include "platform/iphone/plugin/godot_plugin_config.h" #include "string.h" #include <sys/stat.h> @@ -54,6 +55,13 @@ class EditorExportPlatformIOS : public EditorExportPlatform { Ref<ImageTexture> logo; + // Plugins + volatile bool plugins_changed; + Thread *check_for_changes_thread; + volatile bool quit_request; + Mutex plugins_lock; + Vector<PluginConfig> plugins; + typedef Error (*FileHandler)(String p_file, void *p_userdata); static Error _walk_dir_recursive(DirAccess *p_da, FileHandler p_handler, void *p_userdata); static Error _codesign(String p_file, void *p_userdata); @@ -70,6 +78,7 @@ class EditorExportPlatformIOS : public EditorExportPlatform { String modules_fileref; String modules_buildphase; String modules_buildgrp; + Vector<String> capabilities; }; struct ExportArchitecture { String name; @@ -102,7 +111,9 @@ class EditorExportPlatformIOS : public EditorExportPlatform { void _add_assets_to_project(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &p_project_data, const Vector<IOSExportAsset> &p_additional_assets); Error _export_additional_assets(const String &p_out_dir, const Vector<String> &p_assets, bool p_is_framework, bool p_should_embed, Vector<IOSExportAsset> &r_exported_assets); + Error _copy_asset(const String &p_out_dir, const String &p_asset, const String *p_custom_file_name, bool p_is_framework, bool p_should_embed, Vector<IOSExportAsset> &r_exported_assets); Error _export_additional_assets(const String &p_out_dir, const Vector<SharedObject> &p_libraries, Vector<IOSExportAsset> &r_exported_assets); + Error _export_ios_plugins(const Ref<EditorExportPreset> &p_preset, IOSConfigData &p_config_data, const String &dest_dir, Vector<IOSExportAsset> &r_exported_assets, bool p_debug); bool is_package_name_valid(const String &p_package, String *r_error = nullptr) const { String pname = p_package; @@ -127,6 +138,40 @@ class EditorExportPlatformIOS : public EditorExportPlatform { return true; } + static void _check_for_changes_poll_thread(void *ud) { + EditorExportPlatformIOS *ea = (EditorExportPlatformIOS *)ud; + + while (!ea->quit_request) { + // Nothing to do if we already know the plugins have changed. + if (!ea->plugins_changed) { + MutexLock lock(ea->plugins_lock); + + Vector<PluginConfig> loaded_plugins = get_plugins(); + + if (ea->plugins.size() != loaded_plugins.size()) { + ea->plugins_changed = true; + } else { + for (int i = 0; i < ea->plugins.size(); i++) { + if (ea->plugins[i].name != loaded_plugins[i].name || ea->plugins[i].last_updated != loaded_plugins[i].last_updated) { + ea->plugins_changed = true; + break; + } + } + } + } + + uint64_t wait = 3000000; + uint64_t time = OS::get_singleton()->get_ticks_usec(); + while (OS::get_singleton()->get_ticks_usec() - time < wait) { + OS::get_singleton()->delay_usec(300000); + + if (ea->quit_request) { + break; + } + } + } + } + protected: virtual void get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) override; virtual void get_export_options(List<ExportOption> *r_options) override; @@ -136,13 +181,21 @@ public: virtual String get_os_name() const override { return "iOS"; } virtual Ref<Texture2D> get_logo() const override { return logo; } + virtual bool should_update_export_options() override { + bool export_options_changed = plugins_changed; + if (export_options_changed) { + // don't clear unless we're reporting true, to avoid race + plugins_changed = false; + } + return export_options_changed; + } + virtual List<String> get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const override { List<String> list; list.push_back("ipa"); return list; } virtual Error export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags = 0) override; - virtual void add_module_code(const Ref<EditorExportPreset> &p_preset, IOSConfigData &p_config_data, const String &p_name, const String &p_fid, const String &p_gid); virtual bool can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const override; @@ -156,6 +209,85 @@ public: EditorExportPlatformIOS(); ~EditorExportPlatformIOS(); + + /// List the gdip files in the directory specified by the p_path parameter. + static Vector<String> list_plugin_config_files(const String &p_path, bool p_check_directories) { + Vector<String> dir_files; + DirAccessRef da = DirAccess::open(p_path); + if (da) { + da->list_dir_begin(); + while (true) { + String file = da->get_next(); + if (file.empty()) { + break; + } + + if (file == "." || file == "..") { + continue; + } + + if (da->current_is_hidden()) { + continue; + } + + if (da->current_is_dir()) { + if (p_check_directories) { + Vector<String> directory_files = list_plugin_config_files(p_path.plus_file(file), false); + for (int i = 0; i < directory_files.size(); ++i) { + dir_files.push_back(file.plus_file(directory_files[i])); + } + } + + continue; + } + + if (file.ends_with(PLUGIN_CONFIG_EXT)) { + dir_files.push_back(file); + } + } + da->list_dir_end(); + } + + return dir_files; + } + + static Vector<PluginConfig> get_plugins() { + Vector<PluginConfig> loaded_plugins; + + String plugins_dir = ProjectSettings::get_singleton()->get_resource_path().plus_file("ios/plugins"); + + if (DirAccess::exists(plugins_dir)) { + Vector<String> plugins_filenames = list_plugin_config_files(plugins_dir, true); + + if (!plugins_filenames.empty()) { + Ref<ConfigFile> config_file = memnew(ConfigFile); + for (int i = 0; i < plugins_filenames.size(); i++) { + PluginConfig config = load_plugin_config(config_file, plugins_dir.plus_file(plugins_filenames[i])); + if (config.valid_config) { + loaded_plugins.push_back(config); + } else { + print_error("Invalid plugin config file " + plugins_filenames[i]); + } + } + } + } + + return loaded_plugins; + } + + static Vector<PluginConfig> get_enabled_plugins(const Ref<EditorExportPreset> &p_presets) { + Vector<PluginConfig> enabled_plugins; + Vector<PluginConfig> all_plugins = get_plugins(); + for (int i = 0; i < all_plugins.size(); i++) { + PluginConfig plugin = all_plugins[i]; + bool enabled = p_presets->get("plugins/" + plugin.name); + if (enabled) { + enabled_plugins.push_back(plugin); + } + } + + return enabled_plugins; + } }; void EditorExportPlatformIOS::get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) { @@ -224,12 +356,16 @@ void EditorExportPlatformIOS::get_export_options(List<ExportOption> *r_options) r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/version"), "1.0")); r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "application/copyright"), "")); - r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/arkit"), false)); - r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/camera"), false)); + Vector<PluginConfig> found_plugins = get_plugins(); + + for (int i = 0; i < found_plugins.size(); i++) { + r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "plugins/" + found_plugins[i].name), false)); + } + + plugins_changed = false; + plugins = found_plugins; r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/access_wifi"), false)); - r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/game_center"), true)); - r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/in_app_purchases"), false)); r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "capabilities/push_notifications"), false)); r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "user_data/accessible_from_files_app"), false)); @@ -345,18 +481,6 @@ void EditorExportPlatformIOS::_fix_config_file(const Ref<EditorExportPreset> &p_ strnew += lines[i].replace("$docs_in_place", ((bool)p_preset->get("user_data/accessible_from_files_app")) ? "<true/>" : "<false/>") + "\n"; } else if (lines[i].find("$docs_sharing") != -1) { strnew += lines[i].replace("$docs_sharing", ((bool)p_preset->get("user_data/accessible_from_itunes_sharing")) ? "<true/>" : "<false/>") + "\n"; - } else if (lines[i].find("$access_wifi") != -1) { - bool is_on = p_preset->get("capabilities/access_wifi"); - strnew += lines[i].replace("$access_wifi", is_on ? "1" : "0") + "\n"; - } else if (lines[i].find("$game_center") != -1) { - bool is_on = p_preset->get("capabilities/game_center"); - strnew += lines[i].replace("$game_center", is_on ? "1" : "0") + "\n"; - } else if (lines[i].find("$in_app_purchases") != -1) { - bool is_on = p_preset->get("capabilities/in_app_purchases"); - strnew += lines[i].replace("$in_app_purchases", is_on ? "1" : "0") + "\n"; - } else if (lines[i].find("$push_notifications") != -1) { - bool is_on = p_preset->get("capabilities/push_notifications"); - strnew += lines[i].replace("$push_notifications", is_on ? "1" : "0") + "\n"; } else if (lines[i].find("$entitlements_push_notifications") != -1) { bool is_on = p_preset->get("capabilities/push_notifications"); strnew += lines[i].replace("$entitlements_push_notifications", is_on ? "<key>aps-environment</key><string>development</string>" : "") + "\n"; @@ -366,15 +490,14 @@ void EditorExportPlatformIOS::_fix_config_file(const Ref<EditorExportPreset> &p_ // I've removed armv7 as we can run on 64bit only devices // Note that capabilities listed here are requirements for the app to be installed. // They don't enable anything. + Vector<String> capabilities_list = p_config.capabilities; - if ((bool)p_preset->get("capabilities/arkit")) { - capabilities += "<string>arkit</string>\n"; - } - if ((bool)p_preset->get("capabilities/game_center")) { - capabilities += "<string>gamekit</string>\n"; + if ((bool)p_preset->get("capabilities/access_wifi") && !capabilities_list.has("wifi")) { + capabilities_list.push_back("wifi"); } - if ((bool)p_preset->get("capabilities/access_wifi")) { - capabilities += "<string>wifi</string>\n"; + + for (int idx = 0; idx < capabilities_list.size(); idx++) { + capabilities += "<string>" + capabilities_list[idx] + "</string>\n"; } strnew += lines[i].replace("$required_device_capabilities", capabilities); @@ -984,28 +1107,6 @@ void EditorExportPlatformIOS::_add_assets_to_project(const Ref<EditorExportPrese // Note, frameworks like gamekit are always included in our project.pbxprof file // even if turned off in capabilities. - // We do need our ARKit framework - if ((bool)p_preset->get("capabilities/arkit")) { - String build_id = (++current_id).str(); - String ref_id = (++current_id).str(); - - if (pbx_frameworks_build.length() > 0) { - pbx_frameworks_build += ",\n"; - pbx_frameworks_refs += ",\n"; - } - - pbx_frameworks_build += build_id; - pbx_frameworks_refs += ref_id; - - Dictionary format_dict; - format_dict["build_id"] = build_id; - format_dict["ref_id"] = ref_id; - format_dict["name"] = "ARKit.framework"; - format_dict["file_path"] = "System/Library/Frameworks/ARKit.framework"; - format_dict["file_type"] = "wrapper.framework"; - pbx_files += file_info_format.format(format_dict, "$_"); - } - String str = String::utf8((const char *)p_project_data.ptr(), p_project_data.size()); str = str.replace("$additional_pbx_files", pbx_files); str = str.replace("$additional_pbx_frameworks_build", pbx_frameworks_build); @@ -1021,142 +1122,179 @@ void EditorExportPlatformIOS::_add_assets_to_project(const Ref<EditorExportPrese } } -Error EditorExportPlatformIOS::_export_additional_assets(const String &p_out_dir, const Vector<String> &p_assets, bool p_is_framework, bool p_should_embed, Vector<IOSExportAsset> &r_exported_assets) { +Error EditorExportPlatformIOS::_copy_asset(const String &p_out_dir, const String &p_asset, const String *p_custom_file_name, bool p_is_framework, bool p_should_embed, Vector<IOSExportAsset> &r_exported_assets) { DirAccess *filesystem_da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); + ERR_FAIL_COND_V_MSG(!filesystem_da, ERR_CANT_CREATE, "Cannot create DirAccess for path '" + p_out_dir + "'."); + String binary_name = p_out_dir.get_file().get_basename(); - ERR_FAIL_COND_V_MSG(!filesystem_da, ERR_CANT_CREATE, "Cannot create DirAccess for path '" + p_out_dir + "'."); - for (int f_idx = 0; f_idx < p_assets.size(); ++f_idx) { - String asset = p_assets[f_idx]; - if (!asset.begins_with("res://")) { - // either SDK-builtin or already a part of the export template - IOSExportAsset exported_asset = { asset, p_is_framework, p_should_embed }; - r_exported_assets.push_back(exported_asset); + DirAccess *da = DirAccess::create_for_path(p_asset); + if (!da) { + memdelete(filesystem_da); + ERR_FAIL_V_MSG(ERR_CANT_CREATE, "Can't create directory: " + p_asset + "."); + } + bool file_exists = da->file_exists(p_asset); + bool dir_exists = da->dir_exists(p_asset); + if (!file_exists && !dir_exists) { + memdelete(da); + memdelete(filesystem_da); + return ERR_FILE_NOT_FOUND; + } + + String base_dir = p_asset.get_base_dir().replace("res://", ""); + String destination_dir; + String destination; + String asset_path; + + bool create_framework = false; + + if (p_is_framework && p_asset.ends_with(".dylib")) { + // For iOS we need to turn .dylib into .framework + // to be able to send application to AppStore + asset_path = String("dylibs").plus_file(base_dir); + + String file_name; + + if (!p_custom_file_name) { + file_name = p_asset.get_basename().get_file(); } else { - DirAccess *da = DirAccess::create_for_path(asset); - if (!da) { - memdelete(filesystem_da); - ERR_FAIL_V_MSG(ERR_CANT_CREATE, "Can't create directory: " + asset + "."); - } - bool file_exists = da->file_exists(asset); - bool dir_exists = da->dir_exists(asset); - if (!file_exists && !dir_exists) { - memdelete(da); - memdelete(filesystem_da); - return ERR_FILE_NOT_FOUND; - } + file_name = *p_custom_file_name; + } - String base_dir = asset.get_base_dir().replace("res://", ""); - String destination_dir; - String destination; - String asset_path; + String framework_name = file_name + ".framework"; - bool create_framework = false; + asset_path = asset_path.plus_file(framework_name); + destination_dir = p_out_dir.plus_file(asset_path); + destination = destination_dir.plus_file(file_name); + create_framework = true; + } else if (p_is_framework && (p_asset.ends_with(".framework") || p_asset.ends_with(".xcframework"))) { + asset_path = String("dylibs").plus_file(base_dir); - if (p_is_framework && asset.ends_with(".dylib")) { - // For iOS we need to turn .dylib into .framework - // to be able to send application to AppStore - asset_path = String("dylibs").plus_file(base_dir); + String file_name; - String file_name = asset.get_basename().get_file(); - String framework_name = file_name + ".framework"; + if (!p_custom_file_name) { + file_name = p_asset.get_file(); + } else { + file_name = *p_custom_file_name; + } - asset_path = asset_path.plus_file(framework_name); - destination_dir = p_out_dir.plus_file(asset_path); - destination = destination_dir.plus_file(file_name); - create_framework = true; - } else if (p_is_framework && (asset.ends_with(".framework") || asset.ends_with(".xcframework"))) { - asset_path = String("dylibs").plus_file(base_dir); + asset_path = asset_path.plus_file(file_name); + destination_dir = p_out_dir.plus_file(asset_path); + destination = destination_dir; + } else { + asset_path = base_dir; - String file_name = asset.get_file(); - asset_path = asset_path.plus_file(file_name); - destination_dir = p_out_dir.plus_file(asset_path); - destination = destination_dir; - } else { - asset_path = base_dir; + String file_name; - String file_name = asset.get_file(); - destination_dir = p_out_dir.plus_file(asset_path); - asset_path = asset_path.plus_file(file_name); - destination = p_out_dir.plus_file(asset_path); - } + if (!p_custom_file_name) { + file_name = p_asset.get_file(); + } else { + file_name = *p_custom_file_name; + } - if (!filesystem_da->dir_exists(destination_dir)) { - Error make_dir_err = filesystem_da->make_dir_recursive(destination_dir); - if (make_dir_err) { - memdelete(da); - memdelete(filesystem_da); - return make_dir_err; - } - } + destination_dir = p_out_dir.plus_file(asset_path); + asset_path = asset_path.plus_file(file_name); + destination = p_out_dir.plus_file(asset_path); + } - Error err = dir_exists ? da->copy_dir(asset, destination) : da->copy(asset, destination); + if (!filesystem_da->dir_exists(destination_dir)) { + Error make_dir_err = filesystem_da->make_dir_recursive(destination_dir); + if (make_dir_err) { memdelete(da); - if (err) { - memdelete(filesystem_da); - return err; - } - IOSExportAsset exported_asset = { binary_name.plus_file(asset_path), p_is_framework, p_should_embed }; - r_exported_assets.push_back(exported_asset); + memdelete(filesystem_da); + return make_dir_err; + } + } - if (create_framework) { - String file_name = asset.get_basename().get_file(); - String framework_name = file_name + ".framework"; + Error err = dir_exists ? da->copy_dir(p_asset, destination) : da->copy(p_asset, destination); + memdelete(da); + if (err) { + memdelete(filesystem_da); + return err; + } + IOSExportAsset exported_asset = { binary_name.plus_file(asset_path), p_is_framework, p_should_embed }; + r_exported_assets.push_back(exported_asset); - // Performing `install_name_tool -id @rpath/{name}.framework/{name} ./{name}` on dylib - { - List<String> install_name_args; - install_name_args.push_back("-id"); - install_name_args.push_back(String("@rpath").plus_file(framework_name).plus_file(file_name)); - install_name_args.push_back(destination); + if (create_framework) { + String file_name; - OS::get_singleton()->execute("install_name_tool", install_name_args, true); - } + if (!p_custom_file_name) { + file_name = p_asset.get_basename().get_file(); + } else { + file_name = *p_custom_file_name; + } - // Creating Info.plist - { - String info_plist_format = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" - "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n" - "<plist version=\"1.0\">\n" - "<dict>\n" - "<key>CFBundleShortVersionString</key>\n" - "<string>1.0</string>\n" - "<key>CFBundleIdentifier</key>\n" - "<string>com.gdnative.framework.$name</string>\n" - "<key>CFBundleName</key>\n" - "<string>$name</string>\n" - "<key>CFBundleExecutable</key>\n" - "<string>$name</string>\n" - "<key>DTPlatformName</key>\n" - "<string>iphoneos</string>\n" - "<key>CFBundleInfoDictionaryVersion</key>\n" - "<string>6.0</string>\n" - "<key>CFBundleVersion</key>\n" - "<string>1</string>\n" - "<key>CFBundlePackageType</key>\n" - "<string>FMWK</string>\n" - "<key>MinimumOSVersion</key>\n" - "<string>10.0</string>\n" - "</dict>\n" - "</plist>"; - - String info_plist = info_plist_format.replace("$name", file_name); - - FileAccess *f = FileAccess::open(destination_dir.plus_file("Info.plist"), FileAccess::WRITE); - if (f) { - f->store_string(info_plist); - f->close(); - memdelete(f); - } - } + String framework_name = file_name + ".framework"; + + // Performing `install_name_tool -id @rpath/{name}.framework/{name} ./{name}` on dylib + { + List<String> install_name_args; + install_name_args.push_back("-id"); + install_name_args.push_back(String("@rpath").plus_file(framework_name).plus_file(file_name)); + install_name_args.push_back(destination); + + OS::get_singleton()->execute("install_name_tool", install_name_args, true); + } + + // Creating Info.plist + { + String info_plist_format = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n" + "<plist version=\"1.0\">\n" + "<dict>\n" + "<key>CFBundleShortVersionString</key>\n" + "<string>1.0</string>\n" + "<key>CFBundleIdentifier</key>\n" + "<string>com.gdnative.framework.$name</string>\n" + "<key>CFBundleName</key>\n" + "<string>$name</string>\n" + "<key>CFBundleExecutable</key>\n" + "<string>$name</string>\n" + "<key>DTPlatformName</key>\n" + "<string>iphoneos</string>\n" + "<key>CFBundleInfoDictionaryVersion</key>\n" + "<string>6.0</string>\n" + "<key>CFBundleVersion</key>\n" + "<string>1</string>\n" + "<key>CFBundlePackageType</key>\n" + "<string>FMWK</string>\n" + "<key>MinimumOSVersion</key>\n" + "<string>10.0</string>\n" + "</dict>\n" + "</plist>"; + + String info_plist = info_plist_format.replace("$name", file_name); + + FileAccess *f = FileAccess::open(destination_dir.plus_file("Info.plist"), FileAccess::WRITE); + if (f) { + f->store_string(info_plist); + f->close(); + memdelete(f); } } } + memdelete(filesystem_da); return OK; } +Error EditorExportPlatformIOS::_export_additional_assets(const String &p_out_dir, const Vector<String> &p_assets, bool p_is_framework, bool p_should_embed, Vector<IOSExportAsset> &r_exported_assets) { + for (int f_idx = 0; f_idx < p_assets.size(); ++f_idx) { + String asset = p_assets[f_idx]; + if (!asset.begins_with("res://")) { + // either SDK-builtin or already a part of the export template + IOSExportAsset exported_asset = { asset, p_is_framework, p_should_embed }; + r_exported_assets.push_back(exported_asset); + } else { + Error err = _copy_asset(p_out_dir, asset, nullptr, p_is_framework, p_should_embed, r_exported_assets); + ERR_FAIL_COND_V(err, err); + } + } + + return OK; +} + Error EditorExportPlatformIOS::_export_additional_assets(const String &p_out_dir, const Vector<SharedObject> &p_libraries, Vector<IOSExportAsset> &r_exported_assets) { Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins(); for (int i = 0; i < export_plugins.size(); i++) { @@ -1202,20 +1340,173 @@ Vector<String> EditorExportPlatformIOS::_get_preset_architectures(const Ref<Edit return enabled_archs; } -void EditorExportPlatformIOS::add_module_code(const Ref<EditorExportPreset> &p_preset, EditorExportPlatformIOS::IOSConfigData &p_config_data, const String &p_name, const String &p_fid, const String &p_gid) { - if ((bool)p_preset->get("capabilities/" + p_name)) { - //add module static library - print_line("ADDING MODULE: " + p_name); +Error EditorExportPlatformIOS::_export_ios_plugins(const Ref<EditorExportPreset> &p_preset, IOSConfigData &p_config_data, const String &dest_dir, Vector<IOSExportAsset> &r_exported_assets, bool p_debug) { + String plugin_definition_cpp_code; + String plugin_initialization_cpp_code; + String plugin_deinitialization_cpp_code; - p_config_data.modules_buildfile += p_gid + " /* libgodot_" + p_name + "_module.a in Frameworks */ = {isa = PBXBuildFile; fileRef = " + p_fid + " /* libgodot_" + p_name + "_module.a */; };\n\t\t"; - p_config_data.modules_fileref += p_fid + " /* libgodot_" + p_name + "_module.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = godot_" + p_name + "_module ; path = \"libgodot_" + p_name + "_module.a\"; sourceTree = \"<group>\"; };\n\t\t"; - p_config_data.modules_buildphase += p_gid + " /* libgodot_" + p_name + "_module.a */,\n\t\t\t\t"; - p_config_data.modules_buildgrp += p_fid + " /* libgodot_" + p_name + "_module.a */,\n\t\t\t\t"; - } else { - //add stub function for disabled module - p_config_data.cpp_code += "void register_" + p_name + "_types() { /*stub*/ };\n"; - p_config_data.cpp_code += "void unregister_" + p_name + "_types() { /*stub*/ };\n"; + Vector<String> plugin_linked_dependencies; + Vector<String> plugin_embedded_dependencies; + Vector<String> plugin_files; + + Vector<PluginConfig> enabled_plugins = get_enabled_plugins(p_preset); + + Vector<String> added_linked_dependenciy_names; + Vector<String> added_embedded_dependenciy_names; + HashMap<String, String> plist_values; + + Error err; + + for (int i = 0; i < enabled_plugins.size(); i++) { + PluginConfig plugin = enabled_plugins[i]; + + // Export plugin binary. + if (!plugin.supports_targets) { + err = _copy_asset(dest_dir, plugin.binary, nullptr, true, true, r_exported_assets); + } else { + String plugin_binary_dir = plugin.binary.get_base_dir(); + String plugin_name_prefix = plugin.binary.get_basename().get_file(); + String plugin_file = plugin_name_prefix + "." + (p_debug ? "debug" : "release") + ".a"; + String result_file_name = plugin.binary.get_file(); + + err = _copy_asset(dest_dir, plugin_binary_dir.plus_file(plugin_file), &result_file_name, true, true, r_exported_assets); + } + + ERR_FAIL_COND_V(err, err); + + // Adding dependencies. + // Use separate container for names to check for duplicates. + for (int j = 0; j < plugin.linked_dependencies.size(); j++) { + String dependency = plugin.linked_dependencies[j]; + String name = dependency.get_file(); + + if (added_linked_dependenciy_names.has(name)) { + continue; + } + + added_linked_dependenciy_names.push_back(name); + plugin_linked_dependencies.push_back(dependency); + } + + for (int j = 0; j < plugin.system_dependencies.size(); j++) { + String dependency = plugin.system_dependencies[j]; + String name = dependency.get_file(); + + if (added_linked_dependenciy_names.has(name)) { + continue; + } + + added_linked_dependenciy_names.push_back(name); + plugin_linked_dependencies.push_back(dependency); + } + + for (int j = 0; j < plugin.embedded_dependencies.size(); j++) { + String dependency = plugin.embedded_dependencies[j]; + String name = dependency.get_file(); + + if (added_embedded_dependenciy_names.has(name)) { + continue; + } + + added_embedded_dependenciy_names.push_back(name); + plugin_embedded_dependencies.push_back(dependency); + } + + plugin_files.append_array(plugin.files_to_copy); + + // Capabilities + // Also checking for duplicates. + for (int j = 0; j < plugin.capabilities.size(); j++) { + String capability = plugin.capabilities[j]; + + if (p_config_data.capabilities.has(capability)) { + continue; + } + + p_config_data.capabilities.push_back(capability); + } + + // Plist + // Using hash map container to remove duplicates + const String *K = nullptr; + + while ((K = plugin.plist.next(K))) { + String key = *K; + String value = plugin.plist[key]; + + if (key.empty() || value.empty()) { + continue; + } + + plist_values[key] = value; + } + + // CPP Code + String definition_comment = "// Plugin: " + plugin.name + "\n"; + String initialization_method = plugin.initialization_method + "();\n"; + String deinitialization_method = plugin.deinitialization_method + "();\n"; + + plugin_definition_cpp_code += definition_comment + + "extern void " + initialization_method + + "extern void " + deinitialization_method + "\n"; + + plugin_initialization_cpp_code += "\t" + initialization_method; + plugin_deinitialization_cpp_code += "\t" + deinitialization_method; + } + + // Updating `Info.plist` + { + const String *K = nullptr; + while ((K = plist_values.next(K))) { + String key = *K; + String value = plist_values[key]; + + if (key.empty() || value.empty()) { + continue; + } + + p_config_data.plist_content += "<key>" + key + "</key><string>" + value + "</string>\n"; + } } + + // Export files + { + // Export linked plugin dependency + err = _export_additional_assets(dest_dir, plugin_linked_dependencies, true, false, r_exported_assets); + ERR_FAIL_COND_V(err, err); + + // Export embedded plugin dependency + err = _export_additional_assets(dest_dir, plugin_embedded_dependencies, true, true, r_exported_assets); + ERR_FAIL_COND_V(err, err); + + // Export plugin files + err = _export_additional_assets(dest_dir, plugin_files, false, false, r_exported_assets); + ERR_FAIL_COND_V(err, err); + } + + // Update CPP + { + Dictionary plugin_format; + plugin_format["definition"] = plugin_definition_cpp_code; + plugin_format["initialization"] = plugin_initialization_cpp_code; + plugin_format["deinitialization"] = plugin_deinitialization_cpp_code; + + String plugin_cpp_code = "\n// Godot Plugins\n" + "void godot_ios_plugins_initialize();\n" + "void godot_ios_plugins_deinitialize();\n" + "// Exported Plugins\n\n" + "$definition" + "// Use Plugins\n" + "void godot_ios_plugins_initialize() {\n" + "$initialization" + "}\n\n" + "void godot_ios_plugins_deinitialize() {\n" + "$deinitialization" + "}\n"; + + p_config_data.cpp_code += plugin_cpp_code.format(plugin_format, "$_"); + } + return OK; } Error EditorExportPlatformIOS::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) { @@ -1324,9 +1615,12 @@ Error EditorExportPlatformIOS::export_project(const Ref<EditorExportPreset> &p_p "", "", "", - "" + "", + Vector<String>() }; + Vector<IOSExportAsset> assets; + DirAccess *tmp_app_path = DirAccess::create_for_path(dest_dir); ERR_FAIL_COND_V(!tmp_app_path, ERR_CANT_CREATE); @@ -1339,8 +1633,8 @@ Error EditorExportPlatformIOS::export_project(const Ref<EditorExportPreset> &p_p return ERR_CANT_OPEN; } - add_module_code(p_preset, config_data, "arkit", "F9B95E6E2391205500AF0000", "F9C95E812391205C00BF0000"); - add_module_code(p_preset, config_data, "camera", "F9B95E6E2391205500AF0001", "F9C95E812391205C00BF0001"); + err = _export_ios_plugins(p_preset, config_data, dest_dir + binary_name, assets, p_debug); + ERR_FAIL_COND_V(err, err); //export rest of the files int ret = unzGoToFirstFile(src_pkg_zip); @@ -1382,21 +1676,8 @@ Error EditorExportPlatformIOS::export_project(const Ref<EditorExportPreset> &p_p is_execute = true; #endif file = "godot_ios.a"; - } else if (file.begins_with("libgodot_arkit")) { - if ((bool)p_preset->get("capabilities/arkit") && file.ends_with(String(p_debug ? "debug" : "release") + ".fat.a")) { - file = "libgodot_arkit_module.a"; - } else { - ret = unzGoToNextFile(src_pkg_zip); - continue; //ignore! - } - } else if (file.begins_with("libgodot_camera")) { - if ((bool)p_preset->get("capabilities/camera") && file.ends_with(String(p_debug ? "debug" : "release") + ".fat.a")) { - file = "libgodot_camera_module.a"; - } else { - ret = unzGoToNextFile(src_pkg_zip); - continue; //ignore! - } } + if (file == project_file) { project_file_data = data; } @@ -1530,7 +1811,6 @@ Error EditorExportPlatformIOS::export_project(const Ref<EditorExportPreset> &p_p } print_line("Exporting additional assets"); - Vector<IOSExportAsset> assets; _export_additional_assets(dest_dir + binary_name, libraries, assets); _add_assets_to_project(p_preset, project_file_data, assets); String project_file_name = dest_dir + binary_name + ".xcodeproj/project.pbxproj"; @@ -1665,9 +1945,17 @@ EditorExportPlatformIOS::EditorExportPlatformIOS() { Ref<Image> img = memnew(Image(_iphone_logo)); logo.instance(); logo->create_from_image(img); + + plugins_changed = true; + quit_request = false; + + check_for_changes_thread = Thread::create(_check_for_changes_poll_thread, this); } EditorExportPlatformIOS::~EditorExportPlatformIOS() { + quit_request = true; + Thread::wait_to_finish(check_for_changes_thread); + memdelete(check_for_changes_thread); } void register_iphone_exporter() { diff --git a/platform/iphone/os_iphone.h b/platform/iphone/os_iphone.h index c6f95869ee..04a0a478d5 100644 --- a/platform/iphone/os_iphone.h +++ b/platform/iphone/os_iphone.h @@ -35,9 +35,6 @@ #include "drivers/coreaudio/audio_driver_coreaudio.h" #include "drivers/unix/os_unix.h" -#include "game_center.h" -#include "icloud.h" -#include "in_app_store.h" #include "ios.h" #include "joypad_iphone.h" #include "servers/audio_server.h" @@ -48,6 +45,9 @@ #include "platform/iphone/vulkan_context_iphone.h" #endif +extern void godot_ios_plugins_initialize(); +extern void godot_ios_plugins_deinitialize(); + class OSIPhone : public OS_Unix { private: static HashMap<String, void *> dynamic_symbol_lookup_table; @@ -55,15 +55,6 @@ private: AudioDriverCoreAudio audio_driver; -#ifdef GAME_CENTER_ENABLED - GameCenter *game_center; -#endif -#ifdef STOREKIT_ENABLED - InAppStore *store_kit; -#endif -#ifdef ICLOUD_ENABLED - ICloud *icloud; -#endif iOS *ios; JoypadIPhone *joypad_iphone; diff --git a/platform/iphone/os_iphone.mm b/platform/iphone/os_iphone.mm index 32e9dda92b..b87e6f37a0 100644 --- a/platform/iphone/os_iphone.mm +++ b/platform/iphone/os_iphone.mm @@ -125,21 +125,6 @@ void OSIPhone::initialize() { } void OSIPhone::initialize_modules() { -#ifdef GAME_CENTER_ENABLED - game_center = memnew(GameCenter); - Engine::get_singleton()->add_singleton(Engine::Singleton("GameCenter", game_center)); -#endif - -#ifdef STOREKIT_ENABLED - store_kit = memnew(InAppStore); - Engine::get_singleton()->add_singleton(Engine::Singleton("InAppStore", store_kit)); -#endif - -#ifdef ICLOUD_ENABLED - icloud = memnew(ICloud); - Engine::get_singleton()->add_singleton(Engine::Singleton("ICloud", icloud)); -#endif - ios = memnew(iOS); Engine::get_singleton()->add_singleton(Engine::Singleton("iOS", ios)); @@ -155,26 +140,12 @@ void OSIPhone::deinitialize_modules() { memdelete(ios); } -#ifdef GAME_CENTER_ENABLED - if (game_center) { - memdelete(game_center); - } -#endif - -#ifdef STOREKIT_ENABLED - if (store_kit) { - memdelete(store_kit); - } -#endif - -#ifdef ICLOUD_ENABLED - if (icloud) { - memdelete(icloud); - } -#endif + godot_ios_plugins_deinitialize(); } void OSIPhone::set_main_loop(MainLoop *p_main_loop) { + godot_ios_plugins_initialize(); + main_loop = p_main_loop; if (main_loop) { diff --git a/platform/iphone/plugin/godot_plugin_config.h b/platform/iphone/plugin/godot_plugin_config.h new file mode 100644 index 0000000000..5323f94989 --- /dev/null +++ b/platform/iphone/plugin/godot_plugin_config.h @@ -0,0 +1,265 @@ +/*************************************************************************/ +/* godot_plugin_config.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 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 GODOT_PLUGIN_CONFIG_H +#define GODOT_PLUGIN_CONFIG_H + +#include "core/error/error_list.h" +#include "core/io/config_file.h" +#include "core/string/ustring.h" + +static const char *PLUGIN_CONFIG_EXT = ".gdip"; + +static const char *CONFIG_SECTION = "config"; +static const char *CONFIG_NAME_KEY = "name"; +static const char *CONFIG_BINARY_KEY = "binary"; +static const char *CONFIG_INITIALIZE_KEY = "initialization"; +static const char *CONFIG_DEINITIALIZE_KEY = "deinitialization"; + +static const char *DEPENDENCIES_SECTION = "dependencies"; +static const char *DEPENDENCIES_LINKED_KEY = "linked"; +static const char *DEPENDENCIES_EMBEDDED_KEY = "embedded"; +static const char *DEPENDENCIES_SYSTEM_KEY = "system"; +static const char *DEPENDENCIES_CAPABILITIES_KEY = "capabilities"; +static const char *DEPENDENCIES_FILES_KEY = "files"; + +static const char *PLIST_SECTION = "plist"; + +/* + The `config` section and fields are required and defined as follow: +- **name**: name of the plugin +- **binary**: path to static `.a` library + +The `dependencies` and fields are optional. +- **linked**: dependencies that should only be linked. +- **embedded**: dependencies that should be linked and embedded into application. +- **system**: system dependencies that should be linked. +- **capabilities**: capabilities that would be used for `UIRequiredDeviceCapabilities` options in Info.plist file. +- **files**: files that would be copied into application + +The `plist` section are optional. +- **key**: key and value that would be added in Info.plist file. + */ + +struct PluginConfig { + // Set to true when the config file is properly loaded. + bool valid_config = false; + bool supports_targets = false; + // Unix timestamp of last change to this plugin. + uint64_t last_updated = 0; + + // Required config section + String name; + String binary; + String initialization_method; + String deinitialization_method; + + // Optional dependencies section + Vector<String> linked_dependencies; + Vector<String> embedded_dependencies; + Vector<String> system_dependencies; + + Vector<String> files_to_copy; + Vector<String> capabilities; + + // Optional plist section + // Supports only string types for now + HashMap<String, String> plist; +}; + +static inline String resolve_local_dependency_path(String plugin_config_dir, String dependency_path) { + String absolute_path; + + if (dependency_path.empty()) { + return absolute_path; + } + + if (dependency_path.is_abs_path()) { + return dependency_path; + } + + String res_path = ProjectSettings::get_singleton()->globalize_path("res://"); + absolute_path = plugin_config_dir.plus_file(dependency_path); + + return absolute_path.replace(res_path, "res://"); +} + +static inline String resolve_system_dependency_path(String dependency_path) { + String absolute_path; + + if (dependency_path.empty()) { + return absolute_path; + } + + if (dependency_path.is_abs_path()) { + return dependency_path; + } + + String system_path = "/System/Library/Frameworks"; + + return system_path.plus_file(dependency_path); +} + +static inline Vector<String> resolve_local_dependencies(String plugin_config_dir, Vector<String> p_paths) { + Vector<String> paths; + + for (int i = 0; i < p_paths.size(); i++) { + String path = resolve_local_dependency_path(plugin_config_dir, p_paths[i]); + + if (path.empty()) { + continue; + } + + paths.push_back(path); + } + + return paths; +} + +static inline Vector<String> resolve_system_dependencies(Vector<String> p_paths) { + Vector<String> paths; + + for (int i = 0; i < p_paths.size(); i++) { + String path = resolve_system_dependency_path(p_paths[i]); + + if (path.empty()) { + continue; + } + + paths.push_back(path); + } + + return paths; +} + +static inline bool validate_plugin(PluginConfig &plugin_config) { + bool valid_name = !plugin_config.name.empty(); + bool valid_binary_name = !plugin_config.binary.empty(); + bool valid_initialize = !plugin_config.initialization_method.empty(); + bool valid_deinitialize = !plugin_config.deinitialization_method.empty(); + + bool fields_value = valid_name && valid_binary_name && valid_initialize && valid_deinitialize; + + if (fields_value && FileAccess::exists(plugin_config.binary)) { + plugin_config.valid_config = true; + plugin_config.supports_targets = false; + } else if (fields_value) { + String file_path = plugin_config.binary.get_base_dir(); + String file_name = plugin_config.binary.get_basename().get_file(); + String release_file_name = file_path.plus_file(file_name + ".release.a"); + String debug_file_name = file_path.plus_file(file_name + ".debug.a"); + + if (FileAccess::exists(release_file_name) && FileAccess::exists(debug_file_name)) { + plugin_config.valid_config = true; + plugin_config.supports_targets = true; + } + } + + return plugin_config.valid_config; +} + +static inline uint64_t get_plugin_modification_time(const PluginConfig &plugin_config, const String &config_path) { + uint64_t last_updated = FileAccess::get_modified_time(config_path); + + if (!plugin_config.supports_targets) { + last_updated = MAX(last_updated, FileAccess::get_modified_time(plugin_config.binary)); + } else { + String file_path = plugin_config.binary.get_base_dir(); + String file_name = plugin_config.binary.get_basename().get_file(); + String release_file_name = file_path.plus_file(file_name + ".release.a"); + String debug_file_name = file_path.plus_file(file_name + ".debug.a"); + + last_updated = MAX(last_updated, FileAccess::get_modified_time(release_file_name)); + last_updated = MAX(last_updated, FileAccess::get_modified_time(debug_file_name)); + } + + return last_updated; +} + +static inline PluginConfig load_plugin_config(Ref<ConfigFile> config_file, const String &path) { + PluginConfig plugin_config = {}; + + if (!config_file.is_valid()) { + return plugin_config; + } + + Error err = config_file->load(path); + + if (err != OK) { + return plugin_config; + } + + String config_base_dir = path.get_base_dir(); + + plugin_config.name = config_file->get_value(CONFIG_SECTION, CONFIG_NAME_KEY, String()); + plugin_config.initialization_method = config_file->get_value(CONFIG_SECTION, CONFIG_INITIALIZE_KEY, String()); + plugin_config.deinitialization_method = config_file->get_value(CONFIG_SECTION, CONFIG_DEINITIALIZE_KEY, String()); + + String binary_path = config_file->get_value(CONFIG_SECTION, CONFIG_BINARY_KEY, String()); + plugin_config.binary = resolve_local_dependency_path(config_base_dir, binary_path); + + if (config_file->has_section(DEPENDENCIES_SECTION)) { + Vector<String> linked_dependencies = config_file->get_value(DEPENDENCIES_SECTION, DEPENDENCIES_LINKED_KEY, Vector<String>()); + Vector<String> embedded_dependencies = config_file->get_value(DEPENDENCIES_SECTION, DEPENDENCIES_EMBEDDED_KEY, Vector<String>()); + Vector<String> system_dependencies = config_file->get_value(DEPENDENCIES_SECTION, DEPENDENCIES_SYSTEM_KEY, Vector<String>()); + Vector<String> files = config_file->get_value(DEPENDENCIES_SECTION, DEPENDENCIES_FILES_KEY, Vector<String>()); + + plugin_config.linked_dependencies = resolve_local_dependencies(config_base_dir, linked_dependencies); + plugin_config.embedded_dependencies = resolve_local_dependencies(config_base_dir, embedded_dependencies); + plugin_config.system_dependencies = resolve_system_dependencies(system_dependencies); + + plugin_config.files_to_copy = resolve_local_dependencies(config_base_dir, files); + + plugin_config.capabilities = config_file->get_value(DEPENDENCIES_SECTION, DEPENDENCIES_CAPABILITIES_KEY, Vector<String>()); + } + + if (config_file->has_section(PLIST_SECTION)) { + List<String> keys; + config_file->get_section_keys(PLIST_SECTION, &keys); + + for (int i = 0; i < keys.size(); i++) { + String value = config_file->get_value(PLIST_SECTION, keys[i], String()); + + if (value.empty()) { + continue; + } + + plugin_config.plist[keys[i]] = value; + } + } + + if (validate_plugin(plugin_config)) { + plugin_config.last_updated = get_plugin_modification_time(plugin_config, path); + } + + return plugin_config; +} + +#endif // GODOT_PLUGIN_CONFIG_H diff --git a/platform/iphone/view_controller.h b/platform/iphone/view_controller.h index b0b31ae377..2cc4e4c388 100644 --- a/platform/iphone/view_controller.h +++ b/platform/iphone/view_controller.h @@ -28,13 +28,12 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#import <GameKit/GameKit.h> #import <UIKit/UIKit.h> @class GodotView; @class GodotNativeVideoView; -@interface ViewController : UIViewController <GKGameCenterControllerDelegate> +@interface ViewController : UIViewController @property(nonatomic, readonly, strong) GodotView *godotView; @property(nonatomic, readonly, strong) GodotNativeVideoView *videoView; diff --git a/platform/iphone/view_controller.mm b/platform/iphone/view_controller.mm index e11eff423b..5d18a72be1 100644 --- a/platform/iphone/view_controller.mm +++ b/platform/iphone/view_controller.mm @@ -36,6 +36,7 @@ #import "native_video_view.h" #include "os_iphone.h" +#import <AVFoundation/AVFoundation.h> #import <GameController/GameController.h> @interface ViewController () @@ -214,14 +215,4 @@ } } -// MARK: Delegates - -#ifdef GAME_CENTER_ENABLED -- (void)gameCenterViewControllerDidFinish:(GKGameCenterViewController *)gameCenterViewController { - //[gameCenterViewController dismissViewControllerAnimated:YES completion:^{GameCenter::get_singleton()->game_center_closed();}];//version for signaling when overlay is completely gone - GameCenter::get_singleton()->game_center_closed(); - [gameCenterViewController dismissViewControllerAnimated:YES completion:nil]; -} -#endif - @end diff --git a/platform/javascript/SCsub b/platform/javascript/SCsub index 7381ea13b7..a0e6fa0e18 100644 --- a/platform/javascript/SCsub +++ b/platform/javascript/SCsub @@ -18,21 +18,22 @@ if env["threads_enabled"]: build = env.add_program(build_targets, javascript_files) -js_libraries = [ - "native/http_request.js", - "native/library_godot_audio.js", -] -for lib in js_libraries: - env.Append(LINKFLAGS=["--js-library", env.File(lib).path]) -env.Depends(build, js_libraries) +env.AddJSLibraries( + [ + "native/http_request.js", + "native/library_godot_audio.js", + "native/library_godot_display.js", + "native/library_godot_os.js", + ] +) -js_pre = [ - "native/id_handler.js", - "native/utils.js", -] -for js in js_pre: - env.Append(LINKFLAGS=["--pre-js", env.File(js).path]) -env.Depends(build, js_pre) +if env["tools"]: + env.AddJSLibraries(["native/library_godot_editor_tools.js"]) +if env["javascript_eval"]: + env.AddJSLibraries(["native/library_godot_eval.js"]) +for lib in env["JS_LIBS"]: + env.Append(LINKFLAGS=["--js-library", lib]) +env.Depends(build, env["JS_LIBS"]) engine = [ "engine/preloader.js", @@ -55,9 +56,10 @@ out_files = [ zip_dir.File(binary_name + ".js"), zip_dir.File(binary_name + ".wasm"), zip_dir.File(binary_name + ".html"), + zip_dir.File(binary_name + ".audio.worklet.js"), ] html_file = "#misc/dist/html/editor.html" if env["tools"] else "#misc/dist/html/full-size.html" -in_files = [js_wrapped, build[1], html_file] +in_files = [js_wrapped, build[1], html_file, "#platform/javascript/native/audio.worklet.js"] if env["threads_enabled"]: in_files.append(build[2]) out_files.append(zip_dir.File(binary_name + ".worker.js")) diff --git a/platform/javascript/api/javascript_tools_editor_plugin.cpp b/platform/javascript/api/javascript_tools_editor_plugin.cpp index d44e8139a1..8d781703ed 100644 --- a/platform/javascript/api/javascript_tools_editor_plugin.cpp +++ b/platform/javascript/api/javascript_tools_editor_plugin.cpp @@ -39,6 +39,11 @@ #include <emscripten/emscripten.h> +// JavaScript functions defined in library_godot_editor_tools.js +extern "C" { +extern void godot_js_editor_download_file(const char *p_path, const char *p_name, const char *p_mime); +} + static void _javascript_editor_init_callback() { EditorNode::get_singleton()->add_editor_plugin(memnew(JavaScriptToolsEditorPlugin(EditorNode::get_singleton()))); } @@ -65,25 +70,7 @@ void JavaScriptToolsEditorPlugin::_download_zip(Variant p_v) { String base_path = resource_path.substr(0, resource_path.rfind("/")) + "/"; _zip_recursive(resource_path, base_path, zip); zipClose(zip, NULL); - EM_ASM({ - const path = "/tmp/project.zip"; - const size = FS.stat(path)["size"]; - const buf = new Uint8Array(size); - const fd = FS.open(path, "r"); - FS.read(fd, buf, 0, size); - FS.close(fd); - FS.unlink(path); - const blob = new Blob([buf], { type: "application/zip" }); - const url = window.URL.createObjectURL(blob); - const a = document.createElement("a"); - a.href = url; - a.download = "project.zip"; - a.style.display = "none"; - document.body.appendChild(a); - a.click(); - a.remove(); - window.URL.revokeObjectURL(url); - }); + godot_js_editor_download_file("/tmp/project.zip", "project.zip", "application/zip"); } void JavaScriptToolsEditorPlugin::_bind_methods() { diff --git a/platform/javascript/audio_driver_javascript.cpp b/platform/javascript/audio_driver_javascript.cpp index ab6ed54cc8..dd982bc3a8 100644 --- a/platform/javascript/audio_driver_javascript.cpp +++ b/platform/javascript/audio_driver_javascript.cpp @@ -31,7 +31,6 @@ #include "audio_driver_javascript.h" #include "core/config/project_settings.h" -#include "godot_audio.h" #include <emscripten.h> @@ -45,92 +44,109 @@ const char *AudioDriverJavaScript::get_name() const { return "JavaScript"; } -#ifndef NO_THREADS -void AudioDriverJavaScript::_audio_thread_func(void *p_data) { - AudioDriverJavaScript *obj = static_cast<AudioDriverJavaScript *>(p_data); - while (!obj->quit) { - obj->lock(); - if (!obj->needs_process) { - obj->unlock(); - OS::get_singleton()->delay_usec(1000); // Give the browser some slack. - continue; - } - obj->_js_driver_process(); - obj->needs_process = false; - obj->unlock(); - } +void AudioDriverJavaScript::_state_change_callback(int p_state) { + singleton->state = p_state; } -#endif -extern "C" EMSCRIPTEN_KEEPALIVE void audio_driver_process_start() { -#ifndef NO_THREADS - AudioDriverJavaScript::singleton->lock(); -#else - AudioDriverJavaScript::singleton->_js_driver_process(); -#endif +void AudioDriverJavaScript::_latency_update_callback(float p_latency) { + singleton->output_latency = p_latency; } -extern "C" EMSCRIPTEN_KEEPALIVE void audio_driver_process_end() { -#ifndef NO_THREADS - AudioDriverJavaScript::singleton->needs_process = true; - AudioDriverJavaScript::singleton->unlock(); -#endif -} +void AudioDriverJavaScript::_audio_driver_process(int p_from, int p_samples) { + int32_t *stream_buffer = reinterpret_cast<int32_t *>(output_rb); + const int max_samples = memarr_len(output_rb); -extern "C" EMSCRIPTEN_KEEPALIVE void audio_driver_process_capture(float sample) { - AudioDriverJavaScript::singleton->process_capture(sample); + int write_pos = p_from; + int to_write = p_samples; + if (to_write == 0) { + to_write = max_samples; + } + // High part + if (write_pos + to_write > max_samples) { + const int samples_high = max_samples - write_pos; + audio_server_process(samples_high / channel_count, &stream_buffer[write_pos]); + for (int i = write_pos; i < max_samples; i++) { + output_rb[i] = float(stream_buffer[i] >> 16) / 32768.f; + } + to_write -= samples_high; + write_pos = 0; + } + // Leftover + audio_server_process(to_write / channel_count, &stream_buffer[write_pos]); + for (int i = write_pos; i < write_pos + to_write; i++) { + output_rb[i] = float(stream_buffer[i] >> 16) / 32768.f; + } } -void AudioDriverJavaScript::_js_driver_process() { - int sample_count = memarr_len(internal_buffer) / channel_count; - int32_t *stream_buffer = reinterpret_cast<int32_t *>(internal_buffer); - audio_server_process(sample_count, stream_buffer); - for (int i = 0; i < sample_count * channel_count; i++) { - internal_buffer[i] = float(stream_buffer[i] >> 16) / 32768.f; +void AudioDriverJavaScript::_audio_driver_capture(int p_from, int p_samples) { + if (get_input_buffer().size() == 0) { + return; // Input capture stopped. } -} + const int max_samples = memarr_len(input_rb); -void AudioDriverJavaScript::process_capture(float sample) { - int32_t sample32 = int32_t(sample * 32768.f) * (1U << 16); - input_buffer_write(sample32); + int read_pos = p_from; + int to_read = p_samples; + if (to_read == 0) { + to_read = max_samples; + } + // High part + if (read_pos + to_read > max_samples) { + const int samples_high = max_samples - read_pos; + for (int i = read_pos; i < max_samples; i++) { + input_buffer_write(int32_t(input_rb[i] * 32768.f) * (1U << 16)); + } + to_read -= samples_high; + read_pos = 0; + } + // Leftover + for (int i = read_pos; i < read_pos + to_read; i++) { + input_buffer_write(int32_t(input_rb[i] * 32768.f) * (1U << 16)); + } } Error AudioDriverJavaScript::init() { mix_rate = GLOBAL_GET("audio/mix_rate"); int latency = GLOBAL_GET("audio/output_latency"); - channel_count = godot_audio_init(mix_rate, latency); - buffer_length = closest_power_of_2(latency * mix_rate / 1000); - buffer_length = godot_audio_create_processor(buffer_length, channel_count); - if (!buffer_length) { - return FAILED; + channel_count = godot_audio_init(mix_rate, latency, &_state_change_callback, &_latency_update_callback); + buffer_length = closest_power_of_2((latency * mix_rate / 1000)); +#ifndef NO_THREADS + node = memnew(WorkletNode); +#else + node = memnew(ScriptProcessorNode); +#endif + buffer_length = node->create(buffer_length, channel_count); + if (output_rb) { + memdelete_arr(output_rb); } - - if (!internal_buffer || (int)memarr_len(internal_buffer) != buffer_length * channel_count) { - if (internal_buffer) - memdelete_arr(internal_buffer); - internal_buffer = memnew_arr(float, buffer_length *channel_count); + output_rb = memnew_arr(float, buffer_length *channel_count); + if (!output_rb) { + return ERR_OUT_OF_MEMORY; } - - if (!internal_buffer) { + if (input_rb) { + memdelete_arr(input_rb); + } + input_rb = memnew_arr(float, buffer_length *channel_count); + if (!input_rb) { return ERR_OUT_OF_MEMORY; } return OK; } void AudioDriverJavaScript::start() { -#ifndef NO_THREADS - thread = Thread::create(_audio_thread_func, this); -#endif - godot_audio_start(internal_buffer); + if (node) { + node->start(output_rb, memarr_len(output_rb), input_rb, memarr_len(input_rb)); + } } void AudioDriverJavaScript::resume() { - godot_audio_resume(); + if (state == 0) { // 'suspended' + godot_audio_resume(); + } } float AudioDriverJavaScript::get_latency() { - return godot_audio_get_latency(); + return output_latency + (float(buffer_length) / mix_rate); } int AudioDriverJavaScript::get_mix_rate() const { @@ -142,48 +158,128 @@ AudioDriver::SpeakerMode AudioDriverJavaScript::get_speaker_mode() const { } void AudioDriverJavaScript::lock() { -#ifndef NO_THREADS - mutex.lock(); -#endif + if (node) { + node->unlock(); + } } void AudioDriverJavaScript::unlock() { -#ifndef NO_THREADS - mutex.unlock(); -#endif -} - -void AudioDriverJavaScript::finish_async() { -#ifndef NO_THREADS - quit = true; // Ask thread to quit. -#endif - godot_audio_finish_async(); + if (node) { + node->unlock(); + } } void AudioDriverJavaScript::finish() { -#ifndef NO_THREADS - Thread::wait_to_finish(thread); - memdelete(thread); - thread = NULL; -#endif - if (internal_buffer) { - memdelete_arr(internal_buffer); - internal_buffer = nullptr; + if (node) { + node->finish(); + memdelete(node); + node = nullptr; + } + if (output_rb) { + memdelete_arr(output_rb); + output_rb = nullptr; + } + if (input_rb) { + memdelete_arr(input_rb); + input_rb = nullptr; } } Error AudioDriverJavaScript::capture_start() { - godot_audio_capture_stop(); + lock(); input_buffer_init(buffer_length); + unlock(); godot_audio_capture_start(); return OK; } Error AudioDriverJavaScript::capture_stop() { + godot_audio_capture_stop(); + lock(); input_buffer.clear(); + unlock(); return OK; } AudioDriverJavaScript::AudioDriverJavaScript() { singleton = this; } + +#ifdef NO_THREADS +/// ScriptProcessorNode implementation +void AudioDriverJavaScript::ScriptProcessorNode::_process_callback() { + AudioDriverJavaScript::singleton->_audio_driver_capture(); + AudioDriverJavaScript::singleton->_audio_driver_process(); +} + +int AudioDriverJavaScript::ScriptProcessorNode::create(int p_buffer_samples, int p_channels) { + return godot_audio_script_create(p_buffer_samples, p_channels); +} + +void AudioDriverJavaScript::ScriptProcessorNode::start(float *p_out_buf, int p_out_buf_size, float *p_in_buf, int p_in_buf_size) { + godot_audio_script_start(p_in_buf, p_in_buf_size, p_out_buf, p_out_buf_size, &_process_callback); +} +#else +/// AudioWorkletNode implementation +void AudioDriverJavaScript::WorkletNode::_audio_thread_func(void *p_data) { + AudioDriverJavaScript::WorkletNode *obj = static_cast<AudioDriverJavaScript::WorkletNode *>(p_data); + AudioDriverJavaScript *driver = AudioDriverJavaScript::singleton; + const int out_samples = memarr_len(driver->output_rb); + const int in_samples = memarr_len(driver->input_rb); + int wpos = 0; + int to_write = out_samples; + int rpos = 0; + int to_read = 0; + int32_t step = 0; + while (!obj->quit) { + if (to_read) { + driver->lock(); + driver->_audio_driver_capture(rpos, to_read); + godot_audio_worklet_state_add(obj->state, STATE_SAMPLES_IN, -to_read); + driver->unlock(); + rpos += to_read; + if (rpos >= in_samples) { + rpos -= in_samples; + } + } + if (to_write) { + driver->lock(); + driver->_audio_driver_process(wpos, to_write); + godot_audio_worklet_state_add(obj->state, STATE_SAMPLES_OUT, to_write); + driver->unlock(); + wpos += to_write; + if (wpos >= out_samples) { + wpos -= out_samples; + } + } + step = godot_audio_worklet_state_wait(obj->state, STATE_PROCESS, step, 1); + to_write = out_samples - godot_audio_worklet_state_get(obj->state, STATE_SAMPLES_OUT); + to_read = godot_audio_worklet_state_get(obj->state, STATE_SAMPLES_IN); + } +} + +int AudioDriverJavaScript::WorkletNode::create(int p_buffer_size, int p_channels) { + godot_audio_worklet_create(p_channels); + return p_buffer_size; +} + +void AudioDriverJavaScript::WorkletNode::start(float *p_out_buf, int p_out_buf_size, float *p_in_buf, int p_in_buf_size) { + godot_audio_worklet_start(p_in_buf, p_in_buf_size, p_out_buf, p_out_buf_size, state); + thread = Thread::create(_audio_thread_func, this); +} + +void AudioDriverJavaScript::WorkletNode::lock() { + mutex.lock(); +} + +void AudioDriverJavaScript::WorkletNode::unlock() { + mutex.unlock(); +} + +void AudioDriverJavaScript::WorkletNode::finish() { + quit = true; // Ask thread to quit. + Thread::wait_to_finish(thread); + memdelete(thread); + thread = nullptr; +} +#endif diff --git a/platform/javascript/audio_driver_javascript.h b/platform/javascript/audio_driver_javascript.h index 56a7da0307..f112a1ede4 100644 --- a/platform/javascript/audio_driver_javascript.h +++ b/platform/javascript/audio_driver_javascript.h @@ -31,53 +31,96 @@ #ifndef AUDIO_DRIVER_JAVASCRIPT_H #define AUDIO_DRIVER_JAVASCRIPT_H -#include "servers/audio_server.h" - #include "core/os/mutex.h" #include "core/os/thread.h" +#include "servers/audio_server.h" + +#include "godot_audio.h" class AudioDriverJavaScript : public AudioDriver { +public: + class AudioNode { + public: + virtual int create(int p_buffer_size, int p_output_channels) = 0; + virtual void start(float *p_out_buf, int p_out_buf_size, float *p_in_buf, int p_in_buf_size) = 0; + virtual void finish() {} + virtual void lock() {} + virtual void unlock() {} + virtual ~AudioNode() {} + }; + + class WorkletNode : public AudioNode { + private: + enum { + STATE_LOCK, + STATE_PROCESS, + STATE_SAMPLES_IN, + STATE_SAMPLES_OUT, + STATE_MAX, + }; + Mutex mutex; + Thread *thread = nullptr; + bool quit = false; + int32_t state[STATE_MAX] = { 0 }; + + static void _audio_thread_func(void *p_data); + + public: + int create(int p_buffer_size, int p_output_channels) override; + void start(float *p_out_buf, int p_out_buf_size, float *p_in_buf, int p_in_buf_size) override; + void finish() override; + void lock() override; + void unlock() override; + }; + + class ScriptProcessorNode : public AudioNode { + private: + static void _process_callback(); + + public: + int create(int p_buffer_samples, int p_channels) override; + void start(float *p_out_buf, int p_out_buf_size, float *p_in_buf, int p_in_buf_size) override; + }; + private: - float *internal_buffer = nullptr; + AudioNode *node = nullptr; + + float *output_rb = nullptr; + float *input_rb = nullptr; int buffer_length = 0; int mix_rate = 0; int channel_count = 0; + int state = 0; + float output_latency = 0.0; -public: -#ifndef NO_THREADS - Mutex mutex; - Thread *thread = nullptr; - bool quit = false; - bool needs_process = true; - - static void _audio_thread_func(void *p_data); -#endif + static void _state_change_callback(int p_state); + static void _latency_update_callback(float p_latency); - void _js_driver_process(); +protected: + void _audio_driver_process(int p_from = 0, int p_samples = 0); + void _audio_driver_capture(int p_from = 0, int p_samples = 0); +public: static bool is_available(); - void process_capture(float sample); static AudioDriverJavaScript *singleton; - const char *get_name() const override; + virtual const char *get_name() const; - Error init() override; - void start() override; + virtual Error init(); + virtual void start(); void resume(); - float get_latency() override; - int get_mix_rate() const override; - SpeakerMode get_speaker_mode() const override; - void lock() override; - void unlock() override; - void finish() override; - void finish_async(); + virtual float get_latency(); + virtual int get_mix_rate() const; + virtual SpeakerMode get_speaker_mode() const; + virtual void lock(); + virtual void unlock(); + virtual void finish(); - Error capture_start() override; - Error capture_stop() override; + virtual Error capture_start(); + virtual Error capture_stop(); AudioDriverJavaScript(); }; - #endif diff --git a/platform/javascript/detect.py b/platform/javascript/detect.py index 8f2961b33d..e6e35f6aa9 100644 --- a/platform/javascript/detect.py +++ b/platform/javascript/detect.py @@ -1,6 +1,6 @@ import os -from emscripten_helpers import run_closure_compiler, create_engine_file +from emscripten_helpers import run_closure_compiler, create_engine_file, add_js_libraries from SCons.Util import WhereIs @@ -85,7 +85,8 @@ def configure(env): if env["use_lto"]: env.Append(CCFLAGS=["-s", "WASM_OBJECT_FILES=0"]) env.Append(LINKFLAGS=["-s", "WASM_OBJECT_FILES=0"]) - env.Append(LINKFLAGS=["--llvm-lto", "1"]) + env.Append(CCFLAGS=["-flto"]) + env.Append(LINKFLAGS=["-flto"]) # Closure compiler if env["use_closure_compiler"]: @@ -95,6 +96,9 @@ def configure(env): jscc = env.Builder(generator=run_closure_compiler, suffix=".cc.js", src_suffix=".js") env.Append(BUILDERS={"BuildJS": jscc}) + # Add helper method for adding libraries. + env.AddMethod(add_js_libraries, "AddJSLibraries") + # Add method that joins/compiles our Engine files. env.AddMethod(create_engine_file, "CreateEngineFile") @@ -165,6 +169,6 @@ def configure(env): env.Append(LINKFLAGS=["-s", "OFFSCREEN_FRAMEBUFFER=1"]) # callMain for manual start, FS for preloading, PATH and ERRNO_CODES for BrowserFS. - env.Append(LINKFLAGS=["-s", "EXTRA_EXPORTED_RUNTIME_METHODS=['callMain', 'FS', 'PATH']"]) + env.Append(LINKFLAGS=["-s", "EXTRA_EXPORTED_RUNTIME_METHODS=['callMain']"]) # Add code that allow exiting runtime. env.Append(LINKFLAGS=["-s", "EXIT_RUNTIME=1"]) diff --git a/platform/javascript/display_server_javascript.cpp b/platform/javascript/display_server_javascript.cpp index 8dc33bdf64..768e326e80 100644 --- a/platform/javascript/display_server_javascript.cpp +++ b/platform/javascript/display_server_javascript.cpp @@ -37,6 +37,7 @@ #include <png.h> #include "dom_keys.inc" +#include "godot_js.h" #define DOM_BUTTON_LEFT 0 #define DOM_BUTTON_MIDDLE 1 @@ -44,28 +45,17 @@ #define DOM_BUTTON_XBUTTON1 3 #define DOM_BUTTON_XBUTTON2 4 -char DisplayServerJavaScript::canvas_id[256] = { 0 }; -static bool cursor_inside_canvas = true; - DisplayServerJavaScript *DisplayServerJavaScript::get_singleton() { return static_cast<DisplayServerJavaScript *>(DisplayServer::get_singleton()); } // Window (canvas) void DisplayServerJavaScript::focus_canvas() { - /* clang-format off */ - EM_ASM( - Module['canvas'].focus(); - ); - /* clang-format on */ + godot_js_display_canvas_focus(); } bool DisplayServerJavaScript::is_canvas_focused() { - /* clang-format off */ - return EM_ASM_INT_V( - return document.activeElement == Module['canvas']; - ); - /* clang-format on */ + return godot_js_display_canvas_is_focused() != 0; } bool DisplayServerJavaScript::check_size_force_redraw() { @@ -83,19 +73,17 @@ bool DisplayServerJavaScript::check_size_force_redraw() { } Point2 DisplayServerJavaScript::compute_position_in_canvas(int p_x, int p_y) { - int canvas_x = EM_ASM_INT({ - return Module['canvas'].getBoundingClientRect().x; - }); - int canvas_y = EM_ASM_INT({ - return Module['canvas'].getBoundingClientRect().y; - }); + DisplayServerJavaScript *display = get_singleton(); + int canvas_x; + int canvas_y; + godot_js_display_canvas_bounding_rect_position_get(&canvas_x, &canvas_y); int canvas_width; int canvas_height; - emscripten_get_canvas_element_size(canvas_id, &canvas_width, &canvas_height); + emscripten_get_canvas_element_size(display->canvas_id, &canvas_width, &canvas_height); double element_width; double element_height; - emscripten_get_element_css_size(canvas_id, &element_width, &element_height); + emscripten_get_element_css_size(display->canvas_id, &element_width, &element_height); return Point2((int)(canvas_width / element_width * (p_x - canvas_x)), (int)(canvas_height / element_height * (p_y - canvas_y))); @@ -105,8 +93,7 @@ EM_BOOL DisplayServerJavaScript::fullscreen_change_callback(int p_event_type, co DisplayServerJavaScript *display = get_singleton(); // Empty ID is canvas. String target_id = String::utf8(p_event->id); - String canvas_str_id = String::utf8(canvas_id); - if (target_id.empty() || target_id == canvas_str_id) { + if (target_id.empty() || target_id == String::utf8(display->canvas_id)) { // This event property is the only reliable data on // browser fullscreen state. if (p_event->isFullscreen) { @@ -118,14 +105,15 @@ EM_BOOL DisplayServerJavaScript::fullscreen_change_callback(int p_event_type, co return false; } -// Drag and drop callback (see native/utils.js). -extern "C" EMSCRIPTEN_KEEPALIVE void _drop_files_callback(char *p_filev[], int p_filec) { - DisplayServerJavaScript *ds = DisplayServerJavaScript::get_singleton(); +// Drag and drop callback. +void DisplayServerJavaScript::drop_files_js_callback(char **p_filev, int p_filec) { + DisplayServerJavaScript *ds = get_singleton(); if (!ds) { ERR_FAIL_MSG("Unable to drop files because the DisplayServer is not active"); } - if (ds->drop_files_callback.is_null()) + if (ds->drop_files_callback.is_null()) { return; + } Vector<String> files; for (int i = 0; i < p_filec; i++) { files.push_back(String::utf8(p_filev[i])); @@ -137,6 +125,18 @@ extern "C" EMSCRIPTEN_KEEPALIVE void _drop_files_callback(char *p_filev[], int p ds->drop_files_callback.call((const Variant **)&vp, 1, ret, ce); } +// JavaScript quit request callback. +void DisplayServerJavaScript::request_quit_callback() { + DisplayServerJavaScript *ds = get_singleton(); + if (ds && !ds->window_event_callback.is_null()) { + Variant event = int(DisplayServer::WINDOW_EVENT_CLOSE_REQUEST); + Variant *eventp = &event; + Variant ret; + Callable::CallError ce; + ds->window_event_callback.call((const Variant **)&eventp, 1, ret, ce); + } +} + // Keys template <typename T> @@ -272,12 +272,13 @@ EM_BOOL DisplayServerJavaScript::mouse_button_callback(int p_event_type, const E } EM_BOOL DisplayServerJavaScript::mousemove_callback(int p_event_type, const EmscriptenMouseEvent *p_event, void *p_user_data) { + DisplayServerJavaScript *ds = get_singleton(); Input *input = Input::get_singleton(); int input_mask = input->get_mouse_button_mask(); Point2 pos = compute_position_in_canvas(p_event->clientX, p_event->clientY); // For motion outside the canvas, only read mouse movement if dragging // started inside the canvas; imitating desktop app behaviour. - if (!cursor_inside_canvas && !input_mask) + if (!ds->cursor_inside_canvas && !input_mask) return false; Ref<InputEventMouseMotion> ev; @@ -339,35 +340,13 @@ const char *DisplayServerJavaScript::godot2dom_cursor(DisplayServer::CursorShape } } -void DisplayServerJavaScript::set_css_cursor(const char *p_cursor) { - /* clang-format off */ - EM_ASM_({ - Module['canvas'].style.cursor = UTF8ToString($0); - }, p_cursor); - /* clang-format on */ -} - -bool DisplayServerJavaScript::is_css_cursor_hidden() const { - /* clang-format off */ - return EM_ASM_INT({ - return Module['canvas'].style.cursor === 'none'; - }); - /* clang-format on */ -} - void DisplayServerJavaScript::cursor_set_shape(CursorShape p_shape) { ERR_FAIL_INDEX(p_shape, CURSOR_MAX); - - if (mouse_get_mode() == MOUSE_MODE_VISIBLE) { - if (cursors[p_shape] != "") { - Vector<String> url = cursors[p_shape].split("?"); - set_css_cursor(("url(\"" + url[0] + "\") " + url[1] + ", auto").utf8()); - } else { - set_css_cursor(godot2dom_cursor(p_shape)); - } + if (cursor_shape == p_shape) { + return; } - cursor_shape = p_shape; + godot_js_display_cursor_set_shape(godot2dom_cursor(cursor_shape)); } DisplayServer::CursorShape DisplayServerJavaScript::cursor_get_shape() const { @@ -376,17 +355,6 @@ DisplayServer::CursorShape DisplayServerJavaScript::cursor_get_shape() const { void DisplayServerJavaScript::cursor_set_custom_image(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) { if (p_cursor.is_valid()) { - Map<CursorShape, Vector<Variant>>::Element *cursor_c = cursors_cache.find(p_shape); - - if (cursor_c) { - if (cursor_c->get()[0] == p_cursor && cursor_c->get()[1] == p_hotspot) { - cursor_set_shape(p_shape); - return; - } - - cursors_cache.erase(p_shape); - } - Ref<Texture2D> texture = p_cursor; Ref<AtlasTexture> atlas_texture = p_cursor; Ref<Image> image; @@ -449,53 +417,10 @@ void DisplayServerJavaScript::cursor_set_custom_image(const RES &p_cursor, Curso png.resize(len); ERR_FAIL_COND(!png_image_write_to_memory(&png_meta, png.ptrw(), &len, 0, data.ptr(), 0, nullptr)); - char *object_url; - /* clang-format off */ - EM_ASM({ - var PNG_PTR = $0; - var PNG_LEN = $1; - var PTR = $2; - - var png = new Blob([HEAPU8.slice(PNG_PTR, PNG_PTR + PNG_LEN)], { type: 'image/png' }); - var url = URL.createObjectURL(png); - var length_bytes = lengthBytesUTF8(url) + 1; - var string_on_wasm_heap = _malloc(length_bytes); - setValue(PTR, string_on_wasm_heap, '*'); - stringToUTF8(url, string_on_wasm_heap, length_bytes); - }, png.ptr(), len, &object_url); - /* clang-format on */ - - String url = String::utf8(object_url) + "?" + itos(p_hotspot.x) + " " + itos(p_hotspot.y); - - /* clang-format off */ - EM_ASM({ _free($0); }, object_url); - /* clang-format on */ - - if (cursors[p_shape] != "") { - /* clang-format off */ - EM_ASM({ - URL.revokeObjectURL(UTF8ToString($0).split('?')[0]); - }, cursors[p_shape].utf8().get_data()); - /* clang-format on */ - cursors[p_shape] = ""; - } - - cursors[p_shape] = url; + godot_js_display_cursor_set_custom_shape(godot2dom_cursor(p_shape), png.ptr(), len, p_hotspot.x, p_hotspot.y); - Vector<Variant> params; - params.push_back(p_cursor); - params.push_back(p_hotspot); - cursors_cache.insert(p_shape, params); - - } else if (cursors[p_shape] != "") { - /* clang-format off */ - EM_ASM({ - URL.revokeObjectURL(UTF8ToString($0).split('?')[0]); - }, cursors[p_shape].utf8().get_data()); - /* clang-format on */ - cursors[p_shape] = ""; - - cursors_cache.erase(p_shape); + } else { + godot_js_display_cursor_set_custom_shape(godot2dom_cursor(p_shape), NULL, 0, 0, 0); } cursor_set_shape(cursor_shape); @@ -508,40 +433,37 @@ void DisplayServerJavaScript::mouse_set_mode(MouseMode p_mode) { return; if (p_mode == MOUSE_MODE_VISIBLE) { - // set_css_cursor must be called before set_cursor_shape to make the cursor visible - set_css_cursor(godot2dom_cursor(cursor_shape)); - cursor_set_shape(cursor_shape); + godot_js_display_cursor_set_visible(1); emscripten_exit_pointerlock(); } else if (p_mode == MOUSE_MODE_HIDDEN) { - set_css_cursor("none"); + godot_js_display_cursor_set_visible(0); emscripten_exit_pointerlock(); } else if (p_mode == MOUSE_MODE_CAPTURED) { - EMSCRIPTEN_RESULT result = emscripten_request_pointerlock("canvas", false); + godot_js_display_cursor_set_visible(1); + EMSCRIPTEN_RESULT result = emscripten_request_pointerlock(canvas_id, false); ERR_FAIL_COND_MSG(result == EMSCRIPTEN_RESULT_FAILED_NOT_DEFERRED, "MOUSE_MODE_CAPTURED can only be entered from within an appropriate input callback."); ERR_FAIL_COND_MSG(result != EMSCRIPTEN_RESULT_SUCCESS, "MOUSE_MODE_CAPTURED can only be entered from within an appropriate input callback."); - // set_css_cursor must be called before cursor_set_shape to make the cursor visible - set_css_cursor(godot2dom_cursor(cursor_shape)); - cursor_set_shape(cursor_shape); } } DisplayServer::MouseMode DisplayServerJavaScript::mouse_get_mode() const { - if (is_css_cursor_hidden()) + if (godot_js_display_cursor_is_hidden()) { return MOUSE_MODE_HIDDEN; + } EmscriptenPointerlockChangeEvent ev; emscripten_get_pointerlock_status(&ev); - return (ev.isActive && String::utf8(ev.id) == "canvas") ? MOUSE_MODE_CAPTURED : MOUSE_MODE_VISIBLE; + return (ev.isActive && String::utf8(ev.id) == String::utf8(canvas_id)) ? MOUSE_MODE_CAPTURED : MOUSE_MODE_VISIBLE; } // Wheel - EM_BOOL DisplayServerJavaScript::wheel_callback(int p_event_type, const EmscriptenWheelEvent *p_event, void *p_user_data) { ERR_FAIL_COND_V(p_event_type != EMSCRIPTEN_EVENT_WHEEL, false); + DisplayServerJavaScript *ds = get_singleton(); if (!is_canvas_focused()) { - if (cursor_inside_canvas) { + if (ds->cursor_inside_canvas) { focus_canvas(); } else { return false; @@ -632,7 +554,7 @@ EM_BOOL DisplayServerJavaScript::touchmove_callback(int p_event_type, const Emsc } bool DisplayServerJavaScript::screen_is_touchscreen(int p_screen) const { - return EM_ASM_INT({ return 'ontouchstart' in window; }); + return godot_js_display_touchscreen_is_available(); } // Gamepad @@ -696,53 +618,30 @@ Vector<String> DisplayServerJavaScript::get_rendering_drivers_func() { } // Clipboard -extern "C" EMSCRIPTEN_KEEPALIVE void update_clipboard(const char *p_text) { - // Only call set_clipboard from OS (sets local clipboard) - DisplayServerJavaScript::get_singleton()->clipboard = p_text; +void DisplayServerJavaScript::update_clipboard_callback(const char *p_text) { + get_singleton()->clipboard = p_text; } void DisplayServerJavaScript::clipboard_set(const String &p_text) { - /* clang-format off */ - int err = EM_ASM_INT({ - var text = UTF8ToString($0); - if (!navigator.clipboard || !navigator.clipboard.writeText) - return 1; - navigator.clipboard.writeText(text).catch(function(e) { - // Setting OS clipboard is only possible from an input callback. - console.error("Setting OS clipboard is only possible from an input callback for the HTML5 plafrom. Exception:", e); - }); - return 0; - }, p_text.utf8().get_data()); - /* clang-format on */ + clipboard = p_text; + int err = godot_js_display_clipboard_set(p_text.utf8().get_data()); ERR_FAIL_COND_MSG(err, "Clipboard API is not supported."); } String DisplayServerJavaScript::clipboard_get() const { - /* clang-format off */ - EM_ASM({ - try { - navigator.clipboard.readText().then(function (result) { - ccall('update_clipboard', 'void', ['string'], [result]); - }).catch(function (e) { - // Fail graciously. - }); - } catch (e) { - // Fail graciously. - } - }); - /* clang-format on */ + godot_js_display_clipboard_get(update_clipboard_callback); return clipboard; } -extern "C" EMSCRIPTEN_KEEPALIVE void send_window_event(int p_notification) { +void DisplayServerJavaScript::send_window_event_callback(int p_notification) { + DisplayServerJavaScript *ds = get_singleton(); + if (!ds) { + return; + } if (p_notification == DisplayServer::WINDOW_EVENT_MOUSE_ENTER || p_notification == DisplayServer::WINDOW_EVENT_MOUSE_EXIT) { - cursor_inside_canvas = p_notification == DisplayServer::WINDOW_EVENT_MOUSE_ENTER; + ds->cursor_inside_canvas = p_notification == DisplayServer::WINDOW_EVENT_MOUSE_ENTER; } - OS_JavaScript *os = OS_JavaScript::get_singleton(); - if (os->is_finalizing()) - return; // We don't want events anymore. - DisplayServerJavaScript *ds = DisplayServerJavaScript::get_singleton(); - if (ds && !ds->window_event_callback.is_null()) { + if (!ds->window_event_callback.is_null()) { Variant event = int(p_notification); Variant *eventp = &event; Variant ret; @@ -752,11 +651,7 @@ extern "C" EMSCRIPTEN_KEEPALIVE void send_window_event(int p_notification) { } void DisplayServerJavaScript::alert(const String &p_alert, const String &p_title) { - /* clang-format off */ - EM_ASM_({ - window.alert(UTF8ToString($0)); - }, p_alert.utf8().get_data()); - /* clang-format on */ + godot_js_display_alert(p_alert.utf8().get_data()); } void DisplayServerJavaScript::set_icon(const Ref<Image> &p_icon) { @@ -787,29 +682,11 @@ void DisplayServerJavaScript::set_icon(const Ref<Image> &p_icon) { png.resize(len); ERR_FAIL_COND(!png_image_write_to_memory(&png_meta, png.ptrw(), &len, 0, data.ptr(), 0, nullptr)); - /* clang-format off */ - EM_ASM({ - var PNG_PTR = $0; - var PNG_LEN = $1; - - var png = new Blob([HEAPU8.slice(PNG_PTR, PNG_PTR + PNG_LEN)], { type: "image/png" }); - var url = URL.createObjectURL(png); - var link = document.getElementById('-gd-engine-icon'); - if (link === null) { - link = document.createElement('link'); - link.rel = 'icon'; - link.id = '-gd-engine-icon'; - document.head.appendChild(link); - } - link.href = url; - }, png.ptr(), len); - /* clang-format on */ + godot_js_display_window_icon_set(png.ptr(), len); } void DisplayServerJavaScript::_dispatch_input_event(const Ref<InputEvent> &p_event) { OS_JavaScript *os = OS_JavaScript::get_singleton(); - if (os->is_finalizing()) - return; // We don't want events anymore. // Resume audio context after input in case autoplay was denied. os->resume_audio(); @@ -831,16 +708,14 @@ DisplayServer *DisplayServerJavaScript::create_func(const String &p_rendering_dr DisplayServerJavaScript::DisplayServerJavaScript(const String &p_rendering_driver, WindowMode p_mode, uint32_t p_flags, const Vector2i &p_resolution, Error &r_error) { r_error = OK; // Always succeeds for now. - /* clang-format off */ - swap_cancel_ok = EM_ASM_INT({ - const win = (['Windows', 'Win64', 'Win32', 'WinCE']); - const plat = navigator.platform || ""; - if (win.indexOf(plat) !== -1) { - return 1; - } - return 0; - }) == 1; - /* clang-format on */ + // Ensure the canvas ID. + godot_js_config_canvas_id_get(canvas_id, 256); + + // Check if it's windows. + swap_cancel_ok = godot_js_display_is_swap_ok_cancel() == 1; + + // Expose method for requesting quit. + godot_js_os_request_quit_cb(request_quit_callback); RasterizerDummy::make_current(); // TODO GLES2 in Godot 4.0... or webgpu? #if 0 @@ -878,10 +753,8 @@ DisplayServerJavaScript::DisplayServerJavaScript(const String &p_rendering_drive video_driver_index = p_video_driver; #endif - /* clang-format off */ window_set_mode(p_mode); - if (EM_ASM_INT_V({ return Module['resizeCanvasOnStart'] })) { - /* clang-format on */ + if (godot_js_config_is_resize_on_start()) { window_set_size(p_resolution); } @@ -899,8 +772,7 @@ DisplayServerJavaScript::DisplayServerJavaScript(const String &p_rendering_drive result = emscripten_set_##ev##_callback(nullptr, true, &cb); \ EM_CHECK(ev) // These callbacks from Emscripten's html5.h suffice to access most - // JavaScript APIs. For APIs that are not (sufficiently) exposed, EM_ASM - // is used below. + // JavaScript APIs. SET_EM_CALLBACK(canvas_id, mousedown, mouse_button_callback) SET_EM_WINDOW_CALLBACK(mousemove, mousemove_callback) SET_EM_WINDOW_CALLBACK(mouseup, mouse_button_callback) @@ -919,42 +791,20 @@ DisplayServerJavaScript::DisplayServerJavaScript(const String &p_rendering_drive #undef SET_EM_CALLBACK #undef EM_CHECK - /* clang-format off */ - EM_ASM_ARGS({ - // Bind native event listeners. - // Module.listeners, and Module.drop_handler are defined in native/utils.js - const canvas = Module['canvas']; - const send_window_event = cwrap('send_window_event', null, ['number']); - const notifications = arguments; - (['mouseover', 'mouseleave', 'focus', 'blur']).forEach(function(event, index) { - Module.listeners.add(canvas, event, send_window_event.bind(null, notifications[index]), true); - }); - // Clipboard - const update_clipboard = cwrap('update_clipboard', null, ['string']); - Module.listeners.add(window, 'paste', function(evt) { - update_clipboard(evt.clipboardData.getData('text')); - }, false); - // Drag an drop - Module.listeners.add(canvas, 'dragover', function(ev) { - // Prevent default behavior (which would try to open the file(s)) - ev.preventDefault(); - }, false); - Module.listeners.add(canvas, 'drop', Module.drop_handler, false); - }, - WINDOW_EVENT_MOUSE_ENTER, - WINDOW_EVENT_MOUSE_EXIT, - WINDOW_EVENT_FOCUS_IN, - WINDOW_EVENT_FOCUS_OUT - ); - /* clang-format on */ + // For APIs that are not (sufficiently) exposed, a + // library is used below (implemented in library_godot_display.js). + godot_js_display_notification_cb(&send_window_event_callback, + WINDOW_EVENT_MOUSE_ENTER, + WINDOW_EVENT_MOUSE_EXIT, + WINDOW_EVENT_FOCUS_IN, + WINDOW_EVENT_FOCUS_OUT); + godot_js_display_paste_cb(update_clipboard_callback); + godot_js_display_drop_files_cb(drop_files_js_callback); Input::get_singleton()->set_event_dispatch_function(_dispatch_input_event); } DisplayServerJavaScript::~DisplayServerJavaScript() { - EM_ASM({ - Module.listeners.clear(); - }); //emscripten_webgl_commit_frame(); //emscripten_webgl_destroy_context(webgl_ctx); } @@ -1057,11 +907,7 @@ void DisplayServerJavaScript::window_set_drop_files_callback(const Callable &p_c } void DisplayServerJavaScript::window_set_title(const String &p_title, WindowID p_window) { - /* clang-format off */ - EM_ASM_({ - document.title = UTF8ToString($0); - }, p_title.utf8().get_data()); - /* clang-format on */ + godot_js_display_window_title_set(p_title.utf8().get_data()); } int DisplayServerJavaScript::window_get_current_screen(WindowID p_window) const { @@ -1103,9 +949,7 @@ Size2i DisplayServerJavaScript::window_get_min_size(WindowID p_window) const { void DisplayServerJavaScript::window_set_size(const Size2i p_size, WindowID p_window) { last_width = p_size.x; last_height = p_size.y; - double scale = EM_ASM_DOUBLE({ - return window.devicePixelRatio || 1; - }); + double scale = godot_js_display_pixel_ratio_get(); emscripten_set_canvas_element_size(canvas_id, p_size.x * scale, p_size.y * scale); emscripten_set_element_css_size(canvas_id, p_size.x, p_size.y); } @@ -1130,7 +974,7 @@ void DisplayServerJavaScript::window_set_mode(WindowMode p_mode, WindowID p_wind emscripten_exit_fullscreen(); } window_mode = WINDOW_MODE_WINDOWED; - window_set_size(windowed_size); + window_set_size(Size2i(last_width, last_height)); } break; case WINDOW_MODE_FULLSCREEN: { EmscriptenFullscreenStrategy strategy; diff --git a/platform/javascript/display_server_javascript.h b/platform/javascript/display_server_javascript.h index d7116be36f..1f00295d48 100644 --- a/platform/javascript/display_server_javascript.h +++ b/platform/javascript/display_server_javascript.h @@ -37,18 +37,22 @@ #include <emscripten/html5.h> class DisplayServerJavaScript : public DisplayServer { - //int video_driver_index; - - Vector2 windowed_size; - +private: + WindowMode window_mode = WINDOW_MODE_WINDOWED; ObjectID window_attached_instance_id = {}; + Callable window_event_callback; + Callable input_event_callback; + Callable input_text_callback; + Callable drop_files_callback; + + String clipboard; Ref<InputEventKey> deferred_key_event; - CursorShape cursor_shape = CURSOR_ARROW; - String cursors[CURSOR_MAX]; - Map<CursorShape, Vector<Variant>> cursors_cache; Point2 touches[32]; + char canvas_id[256] = { 0 }; + bool cursor_inside_canvas = true; + CursorShape cursor_shape = CURSOR_ARROW; Point2i last_click_pos = Point2(-100, -100); // TODO check this again. double last_click_ms = 0; int last_click_button_index = -1; @@ -66,8 +70,6 @@ class DisplayServerJavaScript : public DisplayServer { static void dom2godot_mod(T *emscripten_event_ptr, Ref<InputEventWithModifiers> godot_event); static Ref<InputEventKey> setup_key_event(const EmscriptenKeyboardEvent *emscripten_event); static const char *godot2dom_cursor(DisplayServer::CursorShape p_shape); - static void set_css_cursor(const char *p_cursor); - bool is_css_cursor_hidden() const; // events static EM_BOOL fullscreen_change_callback(int p_event_type, const EmscriptenFullscreenChangeEvent *p_event, void *p_user_data); @@ -92,22 +94,17 @@ class DisplayServerJavaScript : public DisplayServer { static void _dispatch_input_event(const Ref<InputEvent> &p_event); + static void request_quit_callback(); + static void update_clipboard_callback(const char *p_text); + static void send_window_event_callback(int p_notification); + static void drop_files_js_callback(char **p_filev, int p_filec); + protected: int get_current_video_driver() const; public: // Override return type to make writing static callbacks less tedious. static DisplayServerJavaScript *get_singleton(); - static char canvas_id[256]; - - WindowMode window_mode = WINDOW_MODE_WINDOWED; - - String clipboard; - - Callable window_event_callback; - Callable input_event_callback; - Callable input_text_callback; - Callable drop_files_callback; // utilities bool check_size_force_redraw(); diff --git a/platform/javascript/emscripten_helpers.py b/platform/javascript/emscripten_helpers.py index f6db10fbbd..cc874c432e 100644 --- a/platform/javascript/emscripten_helpers.py +++ b/platform/javascript/emscripten_helpers.py @@ -19,3 +19,9 @@ def create_engine_file(env, target, source, externs): if env["use_closure_compiler"]: return env.BuildJS(target, source, JSEXTERNS=externs) return env.Textfile(target, [env.File(s) for s in source]) + + +def add_js_libraries(env, libraries): + if "JS_LIBS" not in env: + env["JS_LIBS"] = [] + env.Append(JS_LIBS=env.File(libraries)) diff --git a/platform/javascript/engine/engine.js b/platform/javascript/engine/engine.js index 05a11701c0..3745e04479 100644 --- a/platform/javascript/engine/engine.js +++ b/platform/javascript/engine/engine.js @@ -33,7 +33,7 @@ Function('return this')()['Engine'] = (function() { this.resizeCanvasOnStart = false; this.onExecute = null; this.onExit = null; - this.persistentPaths = []; + this.persistentPaths = ['/userfs']; }; Engine.prototype.init = /** @param {string=} basePath */ function(basePath) { @@ -114,18 +114,30 @@ Function('return this')()['Engine'] = (function() { locale = navigator.languages ? navigator.languages[0] : navigator.language; locale = locale.split('.')[0]; } - me.rtenv['locale'] = locale; - me.rtenv['canvas'] = me.canvas; + // Emscripten configuration. me.rtenv['thisProgram'] = me.executableName; - me.rtenv['resizeCanvasOnStart'] = me.resizeCanvasOnStart; me.rtenv['noExitRuntime'] = true; - me.rtenv['onExecute'] = me.onExecute; - me.rtenv['onExit'] = function(code) { - me.rtenv['deinitFS'](); - if (me.onExit) - me.onExit(code); - me.rtenv = null; - }; + // Godot configuration. + me.rtenv['initConfig']({ + 'resizeCanvasOnStart': me.resizeCanvasOnStart, + 'canvas': me.canvas, + 'locale': locale, + 'onExecute': function(p_args) { + if (me.onExecute) { + me.onExecute(p_args); + return 0; + } + return 1; + }, + 'onExit': function(p_code) { + me.rtenv['deinitFS'](); + if (me.onExit) { + me.onExit(p_code); + } + me.rtenv = null; + }, + }); + return new Promise(function(resolve, reject) { preloader.preloadedFiles.forEach(function(file) { me.rtenv['copyToFS'](file.path, file.buffer); @@ -208,8 +220,6 @@ Function('return this')()['Engine'] = (function() { }; Engine.prototype.setOnExecute = function(onExecute) { - if (this.rtenv) - this.rtenv.onExecute = onExecute; this.onExecute = onExecute; }; diff --git a/platform/javascript/engine/utils.js b/platform/javascript/engine/utils.js index 0c97b38199..10e3abe91e 100644 --- a/platform/javascript/engine/utils.js +++ b/platform/javascript/engine/utils.js @@ -4,6 +4,8 @@ var Utils = { function rw(path) { if (path.endsWith('.worker.js')) { return execName + '.worker.js'; + } else if (path.endsWith('.audio.worklet.js')) { + return execName + '.audio.worklet.js'; } else if (path.endsWith('.js')) { return execName + '.js'; } else if (path.endsWith('.wasm')) { diff --git a/platform/javascript/export/export.cpp b/platform/javascript/export/export.cpp index a83ff44d20..d520931067 100644 --- a/platform/javascript/export/export.cpp +++ b/platform/javascript/export/export.cpp @@ -94,6 +94,9 @@ public: } else if (req[1] == basereq + ".js") { filepath += ".js"; ctype = "application/javascript"; + } else if (req[1] == basereq + ".audio.worklet.js") { + filepath += ".audio.worklet.js"; + ctype = "application/javascript"; } else if (req[1] == basereq + ".worker.js") { filepath += ".worker.js"; ctype = "application/javascript"; @@ -440,6 +443,9 @@ Error EditorExportPlatformJavaScript::export_project(const Ref<EditorExportPrese } else if (file == "godot.worker.js") { file = p_path.get_file().get_basename() + ".worker.js"; + } else if (file == "godot.audio.worklet.js") { + file = p_path.get_file().get_basename() + ".audio.worklet.js"; + } else if (file == "godot.wasm") { file = p_path.get_file().get_basename() + ".wasm"; } @@ -566,6 +572,7 @@ Error EditorExportPlatformJavaScript::run(const Ref<EditorExportPreset> &p_prese DirAccess::remove_file_or_error(basepath + ".html"); DirAccess::remove_file_or_error(basepath + ".js"); DirAccess::remove_file_or_error(basepath + ".worker.js"); + DirAccess::remove_file_or_error(basepath + ".audio.worklet.js"); DirAccess::remove_file_or_error(basepath + ".pck"); DirAccess::remove_file_or_error(basepath + ".png"); DirAccess::remove_file_or_error(basepath + ".wasm"); diff --git a/platform/javascript/godot_audio.h b/platform/javascript/godot_audio.h index f7f26e5262..7ebda3ad39 100644 --- a/platform/javascript/godot_audio.h +++ b/platform/javascript/godot_audio.h @@ -38,19 +38,24 @@ extern "C" { #include "stddef.h" extern int godot_audio_is_available(); - -extern int godot_audio_init(int p_mix_rate, int p_latency); -extern int godot_audio_create_processor(int p_buffer_length, int p_channel_count); - -extern void godot_audio_start(float *r_buffer_ptr); +extern int godot_audio_init(int p_mix_rate, int p_latency, void (*_state_cb)(int), void (*_latency_cb)(float)); extern void godot_audio_resume(); -extern void godot_audio_finish_async(); - -extern float godot_audio_get_latency(); extern void godot_audio_capture_start(); extern void godot_audio_capture_stop(); +// Worklet +typedef int32_t GodotAudioState[4]; +extern void godot_audio_worklet_create(int p_channels); +extern void godot_audio_worklet_start(float *p_in_buf, int p_in_size, float *p_out_buf, int p_out_size, GodotAudioState p_state); +extern void godot_audio_worklet_state_add(GodotAudioState p_state, int p_idx, int p_value); +extern int godot_audio_worklet_state_get(GodotAudioState p_state, int p_idx); +extern int godot_audio_worklet_state_wait(int32_t *p_state, int p_idx, int32_t p_expected, int p_timeout); + +// Script +extern int godot_audio_script_create(int p_buffer_size, int p_channels); +extern void godot_audio_script_start(float *p_in_buf, int p_in_size, float *p_out_buf, int p_out_size, void (*p_cb)()); + #ifdef __cplusplus } #endif diff --git a/platform/javascript/godot_js.h b/platform/javascript/godot_js.h new file mode 100644 index 0000000000..23596a0897 --- /dev/null +++ b/platform/javascript/godot_js.h @@ -0,0 +1,87 @@ +/*************************************************************************/ +/* godot_js.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 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 GODOT_JS_H +#define GODOT_JS_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include "stddef.h" + +// Config +extern void godot_js_config_locale_get(char *p_ptr, int p_ptr_max); +extern void godot_js_config_canvas_id_get(char *p_ptr, int p_ptr_max); +extern int godot_js_config_is_resize_on_start(); + +// OS +extern void godot_js_os_finish_async(void (*p_callback)()); +extern void godot_js_os_request_quit_cb(void (*p_callback)()); +extern int godot_js_os_fs_is_persistent(); +extern void godot_js_os_fs_sync(void (*p_callback)()); +extern int godot_js_os_execute(const char *p_json); +extern void godot_js_os_shell_open(const char *p_uri); + +// Display +extern double godot_js_display_pixel_ratio_get(); +extern void godot_js_display_alert(const char *p_text); +extern int godot_js_display_touchscreen_is_available(); +extern int godot_js_display_is_swap_ok_cancel(); + +// Display canvas +extern void godot_js_display_canvas_focus(); +extern int godot_js_display_canvas_is_focused(); +extern void godot_js_display_canvas_bounding_rect_position_get(int32_t *p_x, int32_t *p_y); + +// Display window +extern void godot_js_display_window_request_fullscreen(); +extern void godot_js_display_window_title_set(const char *p_text); +extern void godot_js_display_window_icon_set(const uint8_t *p_ptr, int p_len); + +// Display clipboard +extern int godot_js_display_clipboard_set(const char *p_text); +extern int godot_js_display_clipboard_get(void (*p_callback)(const char *p_text)); + +// Display cursor +extern void godot_js_display_cursor_set_shape(const char *p_cursor); +extern int godot_js_display_cursor_is_hidden(); +extern void godot_js_display_cursor_set_custom_shape(const char *p_shape, const uint8_t *p_ptr, int p_len, int p_hotspot_x, int p_hotspot_y); +extern void godot_js_display_cursor_set_visible(int p_visible); + +// Display listeners +extern void godot_js_display_notification_cb(void (*p_callback)(int p_notification), int p_enter, int p_exit, int p_in, int p_out); +extern void godot_js_display_paste_cb(void (*p_callback)(const char *p_text)); +extern void godot_js_display_drop_files_cb(void (*p_callback)(char **p_filev, int p_filec)); +#ifdef __cplusplus +} +#endif + +#endif /* GODOT_JS_H */ diff --git a/platform/javascript/javascript_eval.cpp b/platform/javascript/javascript_eval.cpp index 3a72b10dd4..b203253a39 100644 --- a/platform/javascript/javascript_eval.cpp +++ b/platform/javascript/javascript_eval.cpp @@ -33,95 +33,30 @@ #include "api/javascript_eval.h" #include "emscripten.h" -extern "C" EMSCRIPTEN_KEEPALIVE uint8_t *resize_PackedByteArray_and_open_write(PackedByteArray *p_arr, VectorWriteProxy<uint8_t> *r_write, int p_len) { - p_arr->resize(p_len); - *r_write = p_arr->write; - return p_arr->ptrw(); +extern "C" { +union js_eval_ret { + uint32_t b; + double d; + char *s; +}; + +extern int godot_js_eval(const char *p_js, int p_use_global_ctx, union js_eval_ret *p_union_ptr, void *p_byte_arr, void *p_byte_arr_write, void *(*p_callback)(void *p_ptr, void *p_ptr2, int p_len)); } -Variant JavaScript::eval(const String &p_code, bool p_use_global_exec_context) { - union { - bool b; - double d; - char *s; - } js_data; +void *resize_PackedByteArray_and_open_write(void *p_arr, void *r_write, int p_len) { + PackedByteArray *arr = (PackedByteArray *)p_arr; + VectorWriteProxy<uint8_t> *write = (VectorWriteProxy<uint8_t> *)r_write; + arr->resize(p_len); + *write = arr->write; + return arr->ptrw(); +} +Variant JavaScript::eval(const String &p_code, bool p_use_global_exec_context) { + union js_eval_ret js_data; PackedByteArray arr; VectorWriteProxy<uint8_t> arr_write; - /* clang-format off */ - Variant::Type return_type = static_cast<Variant::Type>(EM_ASM_INT({ - - const CODE = $0; - const USE_GLOBAL_EXEC_CONTEXT = $1; - const PTR = $2; - const BYTEARRAY_PTR = $3; - const BYTEARRAY_WRITE_PTR = $4; - var eval_ret; - try { - if (USE_GLOBAL_EXEC_CONTEXT) { - // indirect eval call grants global execution context - var global_eval = eval; - eval_ret = global_eval(UTF8ToString(CODE)); - } else { - eval_ret = eval(UTF8ToString(CODE)); - } - } catch (e) { - err(e); - eval_ret = null; - } - - switch (typeof eval_ret) { - - case 'boolean': - setValue(PTR, eval_ret, 'i32'); - return 1; // BOOL - - case 'number': - setValue(PTR, eval_ret, 'double'); - return 3; // FLOAT - - case 'string': - var array_len = lengthBytesUTF8(eval_ret)+1; - var array_ptr = _malloc(array_len); - try { - if (array_ptr===0) { - throw new Error('String allocation failed (probably out of memory)'); - } - setValue(PTR, array_ptr , '*'); - stringToUTF8(eval_ret, array_ptr, array_len); - return 4; // STRING - } catch (e) { - if (array_ptr!==0) { - _free(array_ptr) - } - err(e); - // fall through - } - break; - - case 'object': - if (eval_ret === null) { - break; - } - - if (ArrayBuffer.isView(eval_ret) && !(eval_ret instanceof Uint8Array)) { - eval_ret = new Uint8Array(eval_ret.buffer); - } - else if (eval_ret instanceof ArrayBuffer) { - eval_ret = new Uint8Array(eval_ret); - } - if (eval_ret instanceof Uint8Array) { - var bytes_ptr = ccall('resize_PackedByteArray_and_open_write', 'number', ['number', 'number' ,'number'], [BYTEARRAY_PTR, BYTEARRAY_WRITE_PTR, eval_ret.length]); - HEAPU8.set(eval_ret, bytes_ptr); - return 20; // PACKED_BYTE_ARRAY - } - break; - } - return 0; // NIL - - }, p_code.utf8().get_data(), p_use_global_exec_context, &js_data, &arr, &arr_write)); - /* clang-format on */ + Variant::Type return_type = static_cast<Variant::Type>(godot_js_eval(p_code.utf8().get_data(), p_use_global_exec_context, &js_data, &arr, &arr_write, resize_PackedByteArray_and_open_write)); switch (return_type) { case Variant::BOOL: @@ -130,9 +65,7 @@ Variant JavaScript::eval(const String &p_code, bool p_use_global_exec_context) { return js_data.d; case Variant::STRING: { String str = String::utf8(js_data.s); - /* clang-format off */ - EM_ASM_({ _free($0); }, js_data.s); - /* clang-format on */ + free(js_data.s); // Must free the string allocated in JS. return str; } case Variant::PACKED_BYTE_ARRAY: diff --git a/platform/javascript/javascript_main.cpp b/platform/javascript/javascript_main.cpp index 01722c4bc8..2d28a63566 100644 --- a/platform/javascript/javascript_main.cpp +++ b/platform/javascript/javascript_main.cpp @@ -36,20 +36,11 @@ #include <emscripten/emscripten.h> #include <stdlib.h> +#include "godot_js.h" + static OS_JavaScript *os = nullptr; static uint64_t target_ticks = 0; -extern "C" EMSCRIPTEN_KEEPALIVE void _request_quit_callback(char *p_filev[], int p_filec) { - DisplayServerJavaScript *ds = DisplayServerJavaScript::get_singleton(); - if (ds) { - Variant event = int(DisplayServer::WINDOW_EVENT_CLOSE_REQUEST); - Variant *eventp = &event; - Variant ret; - Callable::CallError ce; - ds->window_event_callback.call((const Variant **)&eventp, 1, ret, ce); - } -} - void exit_callback() { emscripten_cancel_main_loop(); // After this, we can exit! Main::cleanup(); @@ -59,6 +50,10 @@ void exit_callback() { emscripten_force_exit(exit_code); // No matter that we call cancel_main_loop, regular "exit" will not work, forcing. } +void cleanup_after_sync() { + emscripten_set_main_loop(exit_callback, -1, false); +} + void main_loop_callback() { uint64_t current_ticks = os->get_ticks_usec(); @@ -74,68 +69,14 @@ void main_loop_callback() { target_ticks += (uint64_t)(1000000 / target_fps); } if (os->main_loop_iterate()) { - emscripten_cancel_main_loop(); // Cancel current loop and wait for finalize_async. - /* clang-format off */ - EM_ASM({ - // This will contain the list of operations that need to complete before cleanup. - Module.async_finish = [ - // Always contains at least one async promise, to avoid firing immediately if nothing is added. - new Promise(function(accept, reject) { - setTimeout(accept, 0); - }) - ]; - }); - /* clang-format on */ - os->get_main_loop()->finish(); - os->finalize_async(); // Will add all the async finish functions. - /* clang-format off */ - EM_ASM({ - Promise.all(Module.async_finish).then(function() { - Module.async_finish = []; - return new Promise(function(accept, reject) { - if (!Module.idbfs) { - accept(); - return; - } - FS.syncfs(function(error) { - if (error) { - err('Failed to save IDB file system: ' + error.message); - } - accept(); - }); - }); - }).then(function() { - ccall("cleanup_after_sync", null, []); - }); - }); - /* clang-format on */ + emscripten_cancel_main_loop(); // Cancel current loop and wait for cleanup_after_sync. + godot_js_os_finish_async(cleanup_after_sync); } } -extern "C" EMSCRIPTEN_KEEPALIVE void cleanup_after_sync() { - emscripten_set_main_loop(exit_callback, -1, false); -} - /// When calling main, it is assumed FS is setup and synced. int main(int argc, char *argv[]) { - // Configure locale. - char locale_ptr[16]; - /* clang-format off */ - EM_ASM({ - stringToUTF8(Module['locale'], $0, 16); - }, locale_ptr); - /* clang-format on */ - setenv("LANG", locale_ptr, true); - - // Ensure the canvas ID. - /* clang-format off */ - EM_ASM({ - stringToUTF8("#" + Module['canvas'].id, $0, 255); - }, DisplayServerJavaScript::canvas_id); - /* clang-format on */ - os = new OS_JavaScript(); - os->set_idb_available((bool)EM_ASM_INT({ return Module.idbfs })); // We must override main when testing is enabled TEST_MAIN_OVERRIDE @@ -147,14 +88,6 @@ int main(int argc, char *argv[]) { Main::start(); os->get_main_loop()->init(); - // Expose method for requesting quit. - /* clang-format off */ - EM_ASM({ - Module['request_quit'] = function() { - ccall("_request_quit_callback", null, []); - }; - }); - /* clang-format on */ emscripten_set_main_loop(main_loop_callback, -1, false); // Immediately run the first iteration. // We are inside an animation frame, we want to immediately draw on the newly setup canvas. diff --git a/platform/javascript/native/audio.worklet.js b/platform/javascript/native/audio.worklet.js new file mode 100644 index 0000000000..ad7957e45c --- /dev/null +++ b/platform/javascript/native/audio.worklet.js @@ -0,0 +1,186 @@ +/*************************************************************************/ +/* audio.worklet.js */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 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. */ +/*************************************************************************/ +class RingBuffer { + + constructor(p_buffer, p_state) { + this.buffer = p_buffer; + this.avail = p_state; + this.rpos = 0; + this.wpos = 0; + } + + data_left() { + return Atomics.load(this.avail, 0); + } + + space_left() { + return this.buffer.length - this.data_left(); + } + + read(output) { + const size = this.buffer.length; + let from = 0 + let to_write = output.length; + if (this.rpos + to_write > size) { + const high = size - this.rpos; + output.set(this.buffer.subarray(this.rpos, size)); + from = high; + to_write -= high; + this.rpos = 0; + } + output.set(this.buffer.subarray(this.rpos, this.rpos + to_write), from); + this.rpos += to_write; + Atomics.add(this.avail, 0, -output.length); + Atomics.notify(this.avail, 0); + } + + write(p_buffer) { + const to_write = p_buffer.length; + const mw = this.buffer.length - this.wpos; + if (mw >= to_write) { + this.buffer.set(p_buffer, this.wpos); + } else { + const high = p_buffer.subarray(0, to_write - mw); + const low = p_buffer.subarray(to_write - mw); + this.buffer.set(high, this.wpos); + this.buffer.set(low); + } + let diff = to_write; + if (this.wpos + diff >= this.buffer.length) { + diff -= this.buffer.length; + } + this.wpos += diff; + Atomics.add(this.avail, 0, to_write); + Atomics.notify(this.avail, 0); + } +} + +class GodotProcessor extends AudioWorkletProcessor { + constructor() { + super(); + this.running = true; + this.lock = null; + this.notifier = null; + this.output = null; + this.output_buffer = new Float32Array(); + this.input = null; + this.input_buffer = new Float32Array(); + this.port.onmessage = (event) => { + const cmd = event.data['cmd']; + const data = event.data['data']; + this.parse_message(cmd, data); + }; + } + + process_notify() { + Atomics.add(this.notifier, 0, 1); + Atomics.notify(this.notifier, 0); + } + + parse_message(p_cmd, p_data) { + if (p_cmd == "start" && p_data) { + const state = p_data[0]; + let idx = 0; + this.lock = state.subarray(idx, ++idx); + this.notifier = state.subarray(idx, ++idx); + const avail_in = state.subarray(idx, ++idx); + const avail_out = state.subarray(idx, ++idx); + this.input = new RingBuffer(p_data[1], avail_in); + this.output = new RingBuffer(p_data[2], avail_out); + } else if (p_cmd == "stop") { + this.runing = false; + this.output = null; + this.input = null; + } + } + + array_has_data(arr) { + return arr.length && arr[0].length && arr[0][0].length; + } + + process(inputs, outputs, parameters) { + if (!this.running) { + return false; // Stop processing. + } + if (this.output === null) { + return true; // Not ready yet, keep processing. + } + const process_input = this.array_has_data(inputs); + if (process_input) { + const input = inputs[0]; + const chunk = input[0].length * input.length; + if (this.input_buffer.length != chunk) { + this.input_buffer = new Float32Array(chunk); + } + if (this.input.space_left() >= chunk) { + this.write_input(this.input_buffer, input); + this.input.write(this.input_buffer); + } else { + this.port.postMessage("Input buffer is full! Skipping input frame."); + } + } + const process_output = this.array_has_data(outputs); + if (process_output) { + const output = outputs[0]; + const chunk = output[0].length * output.length; + if (this.output_buffer.length != chunk) { + this.output_buffer = new Float32Array(chunk) + } + if (this.output.data_left() >= chunk) { + this.output.read(this.output_buffer); + this.write_output(output, this.output_buffer); + } else { + this.port.postMessage("Output buffer has not enough frames! Skipping output frame."); + } + } + this.process_notify(); + return true; + } + + write_output(dest, source) { + const channels = dest.length; + for (let ch = 0; ch < channels; ch++) { + for (let sample = 0; sample < dest[ch].length; sample++) { + dest[ch][sample] = source[sample * channels + ch]; + } + } + } + + write_input(dest, source) { + const channels = source.length; + for (let ch = 0; ch < channels; ch++) { + for (let sample = 0; sample < source[ch].length; sample++) { + dest[sample * channels + ch] = source[ch][sample]; + } + } + } +} + +registerProcessor('godot-processor', GodotProcessor); diff --git a/platform/javascript/native/library_godot_audio.js b/platform/javascript/native/library_godot_audio.js index 4e7f3e2af5..846359b8b2 100644 --- a/platform/javascript/native/library_godot_audio.js +++ b/platform/javascript/native/library_godot_audio.js @@ -27,13 +27,109 @@ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -var GodotAudio = { - $GodotAudio: { +const GodotAudio = { + $GodotAudio__deps: ['$GodotOS'], + $GodotAudio: { ctx: null, input: null, - script: null, + driver: null, + interval: 0, + + init: function(mix_rate, latency, onstatechange, onlatencyupdate) { + const ctx = new (window.AudioContext || window.webkitAudioContext)({ + sampleRate: mix_rate, + // latencyHint: latency / 1000 // Do not specify, leave 'interactive' for good performance. + }); + GodotAudio.ctx = ctx; + onstatechange(ctx.state); // Immeditately notify state. + ctx.onstatechange = function() { + let state = 0; + switch (ctx.state) { + case 'suspended': + state = 0; + break; + case 'running': + state = 1; + break; + case 'closed': + state = 2; + break; + } + onstatechange(state); + } + // Update computed latency + GodotAudio.interval = setInterval(function() { + let latency = 0; + if (ctx.baseLatency) { + latency += GodotAudio.ctx.baseLatency; + } + if (ctx.outputLatency) { + latency += GodotAudio.ctx.outputLatency; + } + onlatencyupdate(latency); + }, 1000); + GodotOS.atexit(GodotAudio.close_async); + return ctx.destination.channelCount; + }, + + create_input: function(callback) { + if (GodotAudio.input) { + return; // Already started. + } + function gotMediaInput(stream) { + GodotAudio.input = GodotAudio.ctx.createMediaStreamSource(stream); + callback(GodotAudio.input) + } + if (navigator.mediaDevices.getUserMedia) { + navigator.mediaDevices.getUserMedia({ + "audio": true + }).then(gotMediaInput, function(e) { out(e) }); + } else { + if (!navigator.getUserMedia) { + navigator.getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia; + } + navigator.getUserMedia({ + "audio": true + }, gotMediaInput, function(e) { out(e) }); + } + }, + + close_async: function(resolve, reject) { + const ctx = GodotAudio.ctx; + GodotAudio.ctx = null; + // Audio was not initialized. + if (!ctx) { + resolve(); + return; + } + // Remove latency callback + if (GodotAudio.interval) { + clearInterval(GodotAudio.interval); + GodotAudio.interval = 0; + } + // Disconnect input, if it was started. + if (GodotAudio.input) { + GodotAudio.input.disconnect(); + GodotAudio.input = null; + } + // Disconnect output + let closed = Promise.resolve(); + if (GodotAudio.driver) { + closed = GodotAudio.driver.close(); + } + closed.then(function() { + return ctx.close(); + }).then(function() { + ctx.onstatechange = null; + resolve(); + }).catch(function(e) { + ctx.onstatechange = null; + console.error("Error closing AudioContext", e); + resolve(); + }); + }, }, godot_audio_is_available__proxy: 'sync', @@ -44,50 +140,10 @@ var GodotAudio = { return 1; }, - godot_audio_init: function(mix_rate, latency) { - GodotAudio.ctx = new (window.AudioContext || window.webkitAudioContext)({ - sampleRate: mix_rate, - // latencyHint: latency / 1000 // Do not specify, leave 'interactive' for good performance. - }); - return GodotAudio.ctx.destination.channelCount; - }, - - godot_audio_create_processor: function(buffer_length, channel_count) { - GodotAudio.script = GodotAudio.ctx.createScriptProcessor(buffer_length, 2, channel_count); - GodotAudio.script.connect(GodotAudio.ctx.destination); - return GodotAudio.script.bufferSize; - }, - - godot_audio_start: function(buffer_ptr) { - var audioDriverProcessStart = cwrap('audio_driver_process_start'); - var audioDriverProcessEnd = cwrap('audio_driver_process_end'); - var audioDriverProcessCapture = cwrap('audio_driver_process_capture', null, ['number']); - GodotAudio.script.onaudioprocess = function(audioProcessingEvent) { - audioDriverProcessStart(); - - var input = audioProcessingEvent.inputBuffer; - var output = audioProcessingEvent.outputBuffer; - var internalBuffer = HEAPF32.subarray( - buffer_ptr / HEAPF32.BYTES_PER_ELEMENT, - buffer_ptr / HEAPF32.BYTES_PER_ELEMENT + output.length * output.numberOfChannels); - for (var channel = 0; channel < output.numberOfChannels; channel++) { - var outputData = output.getChannelData(channel); - // Loop through samples. - for (var sample = 0; sample < outputData.length; sample++) { - outputData[sample] = internalBuffer[sample * output.numberOfChannels + channel]; - } - } - - if (GodotAudio.input) { - var inputDataL = input.getChannelData(0); - var inputDataR = input.getChannelData(1); - for (var i = 0; i < inputDataL.length; i++) { - audioDriverProcessCapture(inputDataL[i]); - audioDriverProcessCapture(inputDataR[i]); - } - } - audioDriverProcessEnd(); - }; + godot_audio_init: function(p_mix_rate, p_latency, p_state_change, p_latency_update) { + const statechange = GodotOS.get_func(p_state_change); + const latencyupdate = GodotOS.get_func(p_latency_update); + return GodotAudio.init(p_mix_rate, p_latency, statechange, latencyupdate); }, godot_audio_resume: function() { @@ -96,72 +152,22 @@ var GodotAudio = { } }, - godot_audio_finish_async: function() { - Module.async_finish.push(new Promise(function(accept, reject) { - if (!GodotAudio.ctx) { - setTimeout(accept, 0); - } else { - if (GodotAudio.script) { - GodotAudio.script.disconnect(); - GodotAudio.script = null; - } - if (GodotAudio.input) { - GodotAudio.input.disconnect(); - GodotAudio.input = null; - } - GodotAudio.ctx.close().then(function() { - accept(); - }).catch(function(e) { - accept(); - }); - GodotAudio.ctx = null; - } - })); - }, - - godot_audio_get_latency__proxy: 'sync', - godot_audio_get_latency: function() { - var latency = 0; - if (GodotAudio.ctx) { - if (GodotAudio.ctx.baseLatency) { - latency += GodotAudio.ctx.baseLatency; - } - if (GodotAudio.ctx.outputLatency) { - latency += GodotAudio.ctx.outputLatency; - } - } - return latency; - }, - godot_audio_capture_start__proxy: 'sync', godot_audio_capture_start: function() { if (GodotAudio.input) { return; // Already started. } - function gotMediaInput(stream) { - GodotAudio.input = GodotAudio.ctx.createMediaStreamSource(stream); - GodotAudio.input.connect(GodotAudio.script); - } - - function gotMediaInputError(e) { - out(e); - } - - if (navigator.mediaDevices.getUserMedia) { - navigator.mediaDevices.getUserMedia({"audio": true}).then(gotMediaInput, gotMediaInputError); - } else { - if (!navigator.getUserMedia) - navigator.getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia; - navigator.getUserMedia({"audio": true}, gotMediaInput, gotMediaInputError); - } + GodotAudio.create_input(function(input) { + input.connect(GodotAudio.driver.get_node()); + }); }, godot_audio_capture_stop__proxy: 'sync', godot_audio_capture_stop: function() { if (GodotAudio.input) { - const tracks = GodotAudio.input.mediaStream.getTracks(); - for (var i = 0; i < tracks.length; i++) { - tracks[i].stop(); + const tracks = GodotAudio.input['mediaStream']['getTracks'](); + for (let i = 0; i < tracks.length; i++) { + tracks[i]['stop'](); } GodotAudio.input.disconnect(); GodotAudio.input = null; @@ -171,3 +177,165 @@ var GodotAudio = { autoAddDeps(GodotAudio, "$GodotAudio"); mergeInto(LibraryManager.library, GodotAudio); + +/** + * The AudioWorklet API driver, used when threads are available. + */ +const GodotAudioWorklet = { + + $GodotAudioWorklet__deps: ['$GodotAudio'], + $GodotAudioWorklet: { + promise: null, + worklet: null, + + create: function(channels) { + const path = Module['locateFile']('godot.audio.worklet.js'); + GodotAudioWorklet.promise = GodotAudio.ctx.audioWorklet.addModule(path).then(function() { + GodotAudioWorklet.worklet = new AudioWorkletNode( + GodotAudio.ctx, + 'godot-processor', + { + 'outputChannelCount': [channels] + } + ); + return Promise.resolve(); + }); + GodotAudio.driver = GodotAudioWorklet; + }, + + start: function(in_buf, out_buf, state) { + GodotAudioWorklet.promise.then(function() { + const node = GodotAudioWorklet.worklet; + node.connect(GodotAudio.ctx.destination); + node.port.postMessage({ + 'cmd': 'start', + 'data': [state, in_buf, out_buf], + }); + node.port.onmessage = function(event) { + console.error(event.data); + }; + }); + }, + + get_node: function() { + return GodotAudioWorklet.worklet; + }, + + close: function() { + return new Promise(function(resolve, reject) { + GodotAudioWorklet.promise.then(function() { + GodotAudioWorklet.worklet.port.postMessage({ + 'cmd': 'stop', + 'data': null, + }); + GodotAudioWorklet.worklet.disconnect(); + GodotAudioWorklet.worklet = null; + GodotAudioWorklet.promise = null; + resolve(); + }); + }); + }, + }, + + godot_audio_worklet_create: function(channels) { + GodotAudioWorklet.create(channels); + }, + + godot_audio_worklet_start: function(p_in_buf, p_in_size, p_out_buf, p_out_size, p_state) { + const out_buffer = GodotOS.heapSub(HEAPF32, p_out_buf, p_out_size); + const in_buffer = GodotOS.heapSub(HEAPF32, p_in_buf, p_in_size); + const state = GodotOS.heapSub(HEAP32, p_state, 4); + GodotAudioWorklet.start(in_buffer, out_buffer, state); + }, + + godot_audio_worklet_state_wait: function(p_state, p_idx, p_expected, p_timeout) { + Atomics.wait(HEAP32, (p_state >> 2) + p_idx, p_expected, p_timeout); + return Atomics.load(HEAP32, (p_state >> 2) + p_idx); + }, + + godot_audio_worklet_state_add: function(p_state, p_idx, p_value) { + return Atomics.add(HEAP32, (p_state >> 2) + p_idx, p_value); + }, + + godot_audio_worklet_state_get: function(p_state, p_idx) { + return Atomics.load(HEAP32, (p_state >> 2) + p_idx); + }, +}; + +autoAddDeps(GodotAudioWorklet, "$GodotAudioWorklet"); +mergeInto(LibraryManager.library, GodotAudioWorklet); + +/* + * The deprecated ScriptProcessorNode API, used when threads are disabled. + */ +const GodotAudioScript = { + + $GodotAudioScript__deps: ['$GodotAudio'], + $GodotAudioScript: { + script: null, + + create: function(buffer_length, channel_count) { + GodotAudioScript.script = GodotAudio.ctx.createScriptProcessor(buffer_length, 2, channel_count); + GodotAudio.driver = GodotAudioScript; + return GodotAudioScript.script.bufferSize; + }, + + start: function(p_in_buf, p_in_size, p_out_buf, p_out_size, onprocess) { + GodotAudioScript.script.onaudioprocess = function(event) { + // Read input + const inb = GodotOS.heapSub(HEAPF32, p_in_buf, p_in_size); + const input = event.inputBuffer; + if (GodotAudio.input) { + const inlen = input.getChannelData(0).length; + for (let ch = 0; ch < 2; ch++) { + const data = input.getChannelData(ch); + for (let s = 0; s < inlen; s++) { + inb[s * 2 + ch] = data[s]; + } + } + } + + // Let Godot process the input/output. + onprocess(); + + // Write the output. + const outb = GodotOS.heapSub(HEAPF32, p_out_buf, p_out_size); + const output = event.outputBuffer; + const channels = output.numberOfChannels; + for (let ch = 0; ch < channels; ch++) { + const data = output.getChannelData(ch); + // Loop through samples and assign computed values. + for (let sample = 0; sample < data.length; sample++) { + data[sample] = outb[sample * channels + ch]; + } + } + }; + GodotAudioScript.script.connect(GodotAudio.ctx.destination); + }, + + get_node: function() { + return GodotAudioScript.script; + }, + + close: function() { + return new Promise(function(resolve, reject) { + GodotAudioScript.script.disconnect(); + GodotAudioScript.script.onaudioprocess = null; + GodotAudioScript.script = null; + resolve(); + }); + }, + }, + + godot_audio_script_create: function(buffer_length, channel_count) { + return GodotAudioScript.create(buffer_length, channel_count); + }, + + godot_audio_script_start: function(p_in_buf, p_in_size, p_out_buf, p_out_size, p_cb) { + const onprocess = GodotOS.get_func(p_cb); + GodotAudioScript.start(p_in_buf, p_in_size, p_out_buf, p_out_size, onprocess); + }, +}; + +autoAddDeps(GodotAudioScript, "$GodotAudioScript"); +mergeInto(LibraryManager.library, GodotAudioScript); diff --git a/platform/javascript/native/library_godot_display.js b/platform/javascript/native/library_godot_display.js new file mode 100644 index 0000000000..490b9181d0 --- /dev/null +++ b/platform/javascript/native/library_godot_display.js @@ -0,0 +1,478 @@ +/*************************************************************************/ +/* library_godot_display.js */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 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. */ +/*************************************************************************/ + +/* + * Display Server listeners. + * Keeps track of registered event listeners so it can remove them on shutdown. + */ +const GodotDisplayListeners = { + $GodotDisplayListeners__postset: 'GodotOS.atexit(function(resolve, reject) { GodotDisplayListeners.clear(); resolve(); });', + $GodotDisplayListeners: { + handlers: [], + + has: function(target, event, method, capture) { + return GodotDisplayListeners.handlers.findIndex(function(e) { + return e.target === target && e.event === event && e.method === method && e.capture == capture; + }) !== -1; + }, + + add: function(target, event, method, capture) { + if (GodotDisplayListeners.has(target, event, method, capture)) { + return; + } + function Handler(target, event, method, capture) { + this.target = target; + this.event = event; + this.method = method; + this.capture = capture; + }; + GodotDisplayListeners.handlers.push(new Handler(target, event, method, capture)); + target.addEventListener(event, method, capture); + }, + + clear: function() { + GodotDisplayListeners.handlers.forEach(function(h) { + h.target.removeEventListener(h.event, h.method, h.capture); + }); + GodotDisplayListeners.handlers.length = 0; + }, + }, +}; +mergeInto(LibraryManager.library, GodotDisplayListeners); + +/* + * Drag and drop handler. + * This is pretty big, but basically detect dropped files on GodotConfig.canvas, + * process them one by one (recursively for directories), and copies them to + * the temporary FS path '/tmp/drop-[random]/' so it can be emitted as a godot + * event (that requires a string array of paths). + * + * NOTE: The temporary files are removed after the callback. This means that + * deferred callbacks won't be able to access the files. + */ +const GodotDisplayDragDrop = { + + $GodotDisplayDragDrop__deps: ['$FS', '$GodotFS'], + $GodotDisplayDragDrop: { + promises: [], + pending_files: [], + + add_entry: function(entry) { + if (entry.isDirectory) { + GodotDisplayDragDrop.add_dir(entry); + } else if (entry.isFile) { + GodotDisplayDragDrop.add_file(entry); + } else { + console.error("Unrecognized entry...", entry); + } + }, + + add_dir: function(entry) { + GodotDisplayDragDrop.promises.push(new Promise(function(resolve, reject) { + const reader = entry.createReader(); + reader.readEntries(function(entries) { + for (let i = 0; i < entries.length; i++) { + GodotDisplayDragDrop.add_entry(entries[i]); + } + resolve(); + }); + })); + }, + + add_file: function(entry) { + GodotDisplayDragDrop.promises.push(new Promise(function(resolve, reject) { + entry.file(function(file) { + const reader = new FileReader(); + reader.onload = function() { + const f = { + "path": file.relativePath || file.webkitRelativePath, + "name": file.name, + "type": file.type, + "size": file.size, + "data": reader.result + }; + if (!f['path']) { + f['path'] = f['name']; + } + GodotDisplayDragDrop.pending_files.push(f); + resolve() + }; + reader.onerror = function() { + console.log("Error reading file"); + reject(); + } + reader.readAsArrayBuffer(file); + }, function(err) { + console.log("Error!"); + reject(); + }); + })); + }, + + process: function(resolve, reject) { + if (GodotDisplayDragDrop.promises.length == 0) { + resolve(); + return; + } + GodotDisplayDragDrop.promises.pop().then(function() { + setTimeout(function() { + GodotDisplayDragDrop.process(resolve, reject); + }, 0); + }); + }, + + _process_event: function(ev, callback) { + ev.preventDefault(); + if (ev.dataTransfer.items) { + // Use DataTransferItemList interface to access the file(s) + for (let i = 0; i < ev.dataTransfer.items.length; i++) { + const item = ev.dataTransfer.items[i]; + let entry = null; + if ("getAsEntry" in item) { + entry = item.getAsEntry(); + } else if ("webkitGetAsEntry" in item) { + entry = item.webkitGetAsEntry(); + } + if (entry) { + GodotDisplayDragDrop.add_entry(entry); + } + } + } else { + console.error("File upload not supported"); + } + new Promise(GodotDisplayDragDrop.process).then(function() { + const DROP = "/tmp/drop-" + parseInt(Math.random() * Math.pow(2, 31)) + "/"; + const drops = []; + const files = []; + FS.mkdir(DROP); + GodotDisplayDragDrop.pending_files.forEach((elem) => { + const path = elem['path']; + GodotFS.copy_to_fs(DROP + path, elem['data']); + let idx = path.indexOf("/"); + if (idx == -1) { + // Root file + drops.push(DROP + path); + } else { + // Subdir + const sub = path.substr(0, idx); + idx = sub.indexOf("/"); + if (idx < 0 && drops.indexOf(DROP + sub) == -1) { + drops.push(DROP + sub); + } + } + files.push(DROP + path); + }); + GodotDisplayDragDrop.promises = []; + GodotDisplayDragDrop.pending_files = []; + callback(drops); + const dirs = [DROP.substr(0, DROP.length -1)]; + // Remove temporary files + files.forEach(function (file) { + FS.unlink(file); + let dir = file.replace(DROP, ""); + let idx = dir.lastIndexOf("/"); + while (idx > 0) { + dir = dir.substr(0, idx); + if (dirs.indexOf(DROP + dir) == -1) { + dirs.push(DROP + dir); + } + idx = dir.lastIndexOf("/"); + } + }); + // Remove dirs. + dirs.sort(function(a, b) { + const al = (a.match(/\//g) || []).length; + const bl = (b.match(/\//g) || []).length; + if (al > bl) + return -1; + else if (al < bl) + return 1; + return 0; + }).forEach(function(dir) { + FS.rmdir(dir); + }); + }); + }, + + handler: function(callback) { + return function(ev) { + GodotDisplayDragDrop._process_event(ev, callback); + }; + }, + }, +}; +mergeInto(LibraryManager.library, GodotDisplayDragDrop); + +/* + * Display server cursor helper. + * Keeps track of cursor status and custom shapes. + */ +const GodotDisplayCursor = { + $GodotDisplayCursor__postset: 'GodotOS.atexit(function(resolve, reject) { GodotDisplayCursor.clear(); resolve(); });', + $GodotDisplayCursor__deps: ['$GodotConfig', '$GodotOS'], + $GodotDisplayCursor: { + shape: 'auto', + visible: true, + cursors: {}, + set_style: function(style) { + GodotConfig.canvas.style.cursor = style; + }, + set_shape: function(shape) { + GodotDisplayCursor.shape = shape; + let css = shape; + if (shape in GodotDisplayCursor.cursors) { + const c = GodotDisplayCursor.cursors[shape]; + css = 'url("' + c.url + '") ' + c.x + ' ' + c.y + ', auto'; + } + if (GodotDisplayCursor.visible) { + GodotDisplayCursor.set_style(css); + } + }, + clear: function() { + GodotDisplayCursor.set_style(''); + GodotDisplayCursor.shape = 'auto'; + GodotDisplayCursor.visible = true; + Object.keys(GodotDisplayCursor.cursors).forEach(function(key) { + URL.revokeObjectURL(GodotDisplayCursor.cursors[key]); + delete GodotDisplayCursor.cursors[key]; + }); + }, + }, +}; +mergeInto(LibraryManager.library, GodotDisplayCursor); + +/** + * Display server interface. + * + * Exposes all the functions needed by DisplayServer implementation. + */ +const GodotDisplay = { + $GodotDisplay__deps: ['$GodotConfig', '$GodotOS', '$GodotDisplayCursor', '$GodotDisplayListeners', '$GodotDisplayDragDrop'], + $GodotDisplay: { + window_icon: '', + }, + + godot_js_display_is_swap_ok_cancel: function() { + const win = (['Windows', 'Win64', 'Win32', 'WinCE']); + const plat = navigator.platform || ""; + if (win.indexOf(plat) !== -1) { + return 1; + } + return 0; + }, + + godot_js_display_alert: function(p_text) { + window.alert(UTF8ToString(p_text)); + }, + + godot_js_display_pixel_ratio_get: function() { + return window.devicePixelRatio || 1; + }, + + /* + * Canvas + */ + godot_js_display_canvas_focus: function() { + GodotConfig.canvas.focus(); + }, + + godot_js_display_canvas_is_focused: function() { + return document.activeElement == GodotConfig.canvas; + }, + + godot_js_display_canvas_bounding_rect_position_get: function(r_x, r_y) { + const brect = GodotConfig.canvas.getBoundingClientRect(); + setValue(r_x, brect.x, 'i32'); + setValue(r_y, brect.y, 'i32'); + }, + + /* + * Touchscreen + */ + godot_js_display_touchscreen_is_available: function() { + return 'ontouchstart' in window; + }, + + /* + * Clipboard + */ + godot_js_display_clipboard_set: function(p_text) { + const text = UTF8ToString(p_text); + if (!navigator.clipboard || !navigator.clipboard.writeText) { + return 1; + } + navigator.clipboard.writeText(text).catch(function(e) { + // Setting OS clipboard is only possible from an input callback. + console.error("Setting OS clipboard is only possible from an input callback for the HTML5 plafrom. Exception:", e); + }); + return 0; + }, + + godot_js_display_clipboard_get_deps: ['$GodotOS'], + godot_js_display_clipboard_get: function(callback) { + const func = GodotOS.get_func(callback); + try { + navigator.clipboard.readText().then(function (result) { + const ptr = allocate(intArrayFromString(result), ALLOC_NORMAL); + func(ptr); + _free(ptr); + }).catch(function (e) { + // Fail graciously. + }); + } catch (e) { + // Fail graciously. + } + }, + + /* + * Window + */ + godot_js_display_window_request_fullscreen: function() { + const canvas = GodotConfig.canvas; + (canvas.requestFullscreen || canvas.msRequestFullscreen || + canvas.mozRequestFullScreen || canvas.mozRequestFullscreen || + canvas.webkitRequestFullscreen + ).call(canvas); + }, + + godot_js_display_window_title_set: function(p_data) { + document.title = UTF8ToString(p_data); + }, + + godot_js_display_window_icon_set: function(p_ptr, p_len) { + let link = document.getElementById('-gd-engine-icon'); + if (link === null) { + link = document.createElement('link'); + link.rel = 'icon'; + link.id = '-gd-engine-icon'; + document.head.appendChild(link); + } + const old_icon = GodotDisplay.window_icon; + const png = new Blob([GodotOS.heapCopy(HEAPU8, p_ptr, p_len)], { type: "image/png" }); + GodotDisplay.window_icon = URL.createObjectURL(png); + link.href = GodotDisplay.window_icon; + if (old_icon) { + URL.revokeObjectURL(old_icon); + } + }, + + /* + * Cursor + */ + godot_js_display_cursor_set_visible: function(p_visible) { + const visible = p_visible != 0; + if (visible == GodotDisplayCursor.visible) { + return; + } + GodotDisplayCursor.visible = visible; + if (visible) { + GodotDisplayCursor.set_shape(GodotDisplayCursor.shape); + } else { + GodotDisplayCursor.set_style('none'); + } + }, + + godot_js_display_cursor_is_hidden: function() { + return !GodotDisplayCursor.visible; + }, + + godot_js_display_cursor_set_shape: function(p_string) { + GodotDisplayCursor.set_shape(UTF8ToString(p_string)); + }, + + godot_js_display_cursor_set_custom_shape: function(p_shape, p_ptr, p_len, p_hotspot_x, p_hotspot_y) { + const shape = UTF8ToString(p_shape); + const old_shape = GodotDisplayCursor.cursors[shape]; + if (p_len > 0) { + const png = new Blob([GodotOS.heapCopy(HEAPU8, p_ptr, p_len)], { type: 'image/png' }); + const url = URL.createObjectURL(png); + GodotDisplayCursor.cursors[shape] = { + url: url, + x: p_hotspot_x, + y: p_hotspot_y, + }; + } else { + delete GodotDisplayCursor.cursors[shape]; + } + if (shape == GodotDisplayCursor.shape) { + GodotDisplayCursor.set_shape(GodotDisplayCursor.shape); + } + if (old_shape) { + URL.revokeObjectURL(old_shape.url); + } + }, + + /* + * Listeners + */ + godot_js_display_notification_cb: function(callback, p_enter, p_exit, p_in, p_out) { + const canvas = GodotConfig.canvas; + const func = GodotOS.get_func(callback); + const notif = [p_enter, p_exit, p_in, p_out]; + ['mouseover', 'mouseleave', 'focus', 'blur'].forEach(function(evt_name, idx) { + GodotDisplayListeners.add(canvas, evt_name, function() { + func.bind(null, notif[idx]); + }, true); + }); + }, + + godot_js_display_paste_cb: function(callback) { + const func = GodotOS.get_func(callback); + GodotDisplayListeners.add(window, 'paste', function(evt) { + const text = evt.clipboardData.getData('text'); + const ptr = allocate(intArrayFromString(text), ALLOC_NORMAL); + func(ptr); + _free(ptr); + }, false); + }, + + godot_js_display_drop_files_cb: function(callback) { + const func = GodotOS.get_func(callback) + const dropFiles = function(files) { + const args = files || []; + if (!args.length) { + return; + } + const argc = args.length; + const argv = GodotOS.allocStringArray(args); + func(argv, argc); + GodotOS.freeStringArray(argv, argc); + }; + const canvas = GodotConfig.canvas; + GodotDisplayListeners.add(canvas, 'dragover', function(ev) { + // Prevent default behavior (which would try to open the file(s)) + ev.preventDefault(); + }, false); + GodotDisplayListeners.add(canvas, 'drop', GodotDisplayDragDrop.handler(dropFiles)); + }, +}; + +autoAddDeps(GodotDisplay, '$GodotDisplay'); +mergeInto(LibraryManager.library, GodotDisplay); diff --git a/platform/javascript/native/library_godot_editor_tools.js b/platform/javascript/native/library_godot_editor_tools.js new file mode 100644 index 0000000000..bd62bbf4e1 --- /dev/null +++ b/platform/javascript/native/library_godot_editor_tools.js @@ -0,0 +1,57 @@ +/*************************************************************************/ +/* library_godot_editor_tools.js */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 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. */ +/*************************************************************************/ + +const GodotEditorTools = { + + godot_js_editor_download_file__deps: ['$FS'], + godot_js_editor_download_file: function(p_path, p_name, p_mime) { + const path = UTF8ToString(p_path); + const name = UTF8ToString(p_name); + const mime = UTF8ToString(p_mime); + const size = FS.stat(path)['size']; + const buf = new Uint8Array(size); + const fd = FS.open(path, 'r'); + FS.read(fd, buf, 0, size); + FS.close(fd); + FS.unlink(path); + const blob = new Blob([buf], { type: mime }); + const url = window.URL.createObjectURL(blob); + const a = document.createElement('a'); + a.href = url; + a.download = name; + a.style.display = 'none'; + document.body.appendChild(a); + a.click(); + a.remove(); + window.URL.revokeObjectURL(url); + }, +}; + +mergeInto(LibraryManager.library, GodotEditorTools); diff --git a/platform/javascript/native/library_godot_eval.js b/platform/javascript/native/library_godot_eval.js new file mode 100644 index 0000000000..e83c61dd9d --- /dev/null +++ b/platform/javascript/native/library_godot_eval.js @@ -0,0 +1,87 @@ +/*************************************************************************/ +/* library_godot_eval.js */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 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. */ +/*************************************************************************/ + +const GodotEval = { + + godot_js_eval__deps: ['$GodotOS'], + godot_js_eval: function(p_js, p_use_global_ctx, p_union_ptr, p_byte_arr, p_byte_arr_write, p_callback) { + const js_code = UTF8ToString(p_js); + let eval_ret = null; + try { + if (p_use_global_ctx) { + // indirect eval call grants global execution context + const global_eval = eval; + eval_ret = global_eval(js_code); + } else { + eval_ret = eval(js_code); + } + } catch (e) { + err(e); + } + + switch (typeof eval_ret) { + + case 'boolean': + setValue(p_union_ptr, eval_ret, 'i32'); + return 1; // BOOL + + case 'number': + setValue(p_union_ptr, eval_ret, 'double'); + return 3; // REAL + + case 'string': + let array_ptr = GodotOS.allocString(eval_ret); + setValue(p_union_ptr, array_ptr , '*'); + return 4; // STRING + + case 'object': + if (eval_ret === null) { + break; + } + + if (ArrayBuffer.isView(eval_ret) && !(eval_ret instanceof Uint8Array)) { + eval_ret = new Uint8Array(eval_ret.buffer); + } + else if (eval_ret instanceof ArrayBuffer) { + eval_ret = new Uint8Array(eval_ret); + } + if (eval_ret instanceof Uint8Array) { + const func = GodotOS.get_func(p_callback); + const bytes_ptr = func(p_byte_arr, p_byte_arr_write, eval_ret.length); + HEAPU8.set(eval_ret, bytes_ptr); + return 20; // POOL_BYTE_ARRAY + } + break; + } + return 0; // NIL + }, +} + +mergeInto(LibraryManager.library, GodotEval); diff --git a/platform/javascript/native/library_godot_os.js b/platform/javascript/native/library_godot_os.js new file mode 100644 index 0000000000..ed48280674 --- /dev/null +++ b/platform/javascript/native/library_godot_os.js @@ -0,0 +1,313 @@ +/*************************************************************************/ +/* library_godot_os.js */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 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. */ +/*************************************************************************/ + +const IDHandler = { + $IDHandler: { + _last_id: 0, + _references: {}, + + get: function(p_id) { + return IDHandler._references[p_id]; + }, + + add: function(p_data) { + const id = ++IDHandler._last_id; + IDHandler._references[id] = p_data; + return id; + }, + + remove: function(p_id) { + delete IDHandler._references[p_id]; + }, + }, +}; + +autoAddDeps(IDHandler, "$IDHandler"); +mergeInto(LibraryManager.library, IDHandler); + +const GodotConfig = { + + $GodotConfig__postset: 'Module["initConfig"] = GodotConfig.init_config;', + $GodotConfig: { + canvas: null, + locale: "en", + resize_on_start: false, + on_execute: null, + + init_config: function(p_opts) { + GodotConfig.resize_on_start = p_opts['resizeCanvasOnStart'] ? true : false; + GodotConfig.canvas = p_opts['canvas']; + GodotConfig.locale = p_opts['locale'] || GodotConfig.locale; + GodotConfig.on_execute = p_opts['onExecute']; + // This is called by emscripten, even if undocumented. + Module['onExit'] = p_opts['onExit']; + }, + }, + + godot_js_config_canvas_id_get: function(p_ptr, p_ptr_max) { + stringToUTF8('#' + GodotConfig.canvas.id, p_ptr, p_ptr_max); + }, + + godot_js_config_locale_get: function(p_ptr, p_ptr_max) { + stringToUTF8(GodotConfig.locale, p_ptr, p_ptr_max); + }, + + godot_js_config_is_resize_on_start: function() { + return GodotConfig.resize_on_start ? 1 : 0; + }, +}; + +autoAddDeps(GodotConfig, '$GodotConfig'); +mergeInto(LibraryManager.library, GodotConfig); + +const GodotFS = { + $GodotFS__deps: ['$FS', '$IDBFS'], + $GodotFS__postset: [ + 'Module["initFS"] = GodotFS.init;', + 'Module["deinitFS"] = GodotFS.deinit;', + 'Module["copyToFS"] = GodotFS.copy_to_fs;', + ].join(''), + $GodotFS: { + _idbfs: false, + _syncing: false, + _mount_points: [], + + is_persistent: function() { + return GodotFS._idbfs ? 1 : 0; + }, + + // Initialize godot file system, setting up persistent paths. + // Returns a promise that resolves when the FS is ready. + // We keep track of mount_points, so that we can properly close the IDBFS + // since emscripten is not doing it by itself. (emscripten GH#12516). + init: function(persistentPaths) { + GodotFS._idbfs = false; + if (!Array.isArray(persistentPaths)) { + return Promise.reject(new Error('Persistent paths must be an array')); + } + if (!persistentPaths.length) { + return Promise.resolve(); + } + GodotFS._mount_points = persistentPaths.slice(); + + function createRecursive(dir) { + try { + FS.stat(dir); + } catch (e) { + if (e.errno !== ERRNO_CODES.ENOENT) { + throw e; + } + FS.mkdirTree(dir); + } + } + + GodotFS._mount_points.forEach(function(path) { + createRecursive(path); + FS.mount(IDBFS, {}, path); + }); + return new Promise(function(resolve, reject) { + FS.syncfs(true, function(err) { + if (err) { + GodotFS._mount_points = []; + GodotFS._idbfs = false; + console.log("IndexedDB not available: " + err.message); + } else { + GodotFS._idbfs = true; + } + resolve(err); + }); + }); + }, + + // Deinit godot file system, making sure to unmount file systems, and close IDBFS(s). + deinit: function() { + GodotFS._mount_points.forEach(function(path) { + try { + FS.unmount(path); + } catch (e) { + console.log("Already unmounted", e); + } + if (GodotFS._idbfs && IDBFS.dbs[path]) { + IDBFS.dbs[path].close(); + delete IDBFS.dbs[path]; + } + }); + GodotFS._mount_points = []; + GodotFS._idbfs = false; + GodotFS._syncing = false; + }, + + sync: function() { + if (GodotFS._syncing) { + err('Already syncing!'); + return Promise.resolve(); + } + GodotFS._syncing = true; + return new Promise(function (resolve, reject) { + FS.syncfs(false, function(error) { + if (error) { + err('Failed to save IDB file system: ' + error.message); + } + GodotFS._syncing = false; + resolve(error); + }); + }); + }, + + // Copies a buffer to the internal file system. Creating directories recursively. + copy_to_fs: function(path, buffer) { + const idx = path.lastIndexOf("/"); + let dir = "/"; + if (idx > 0) { + dir = path.slice(0, idx); + } + try { + FS.stat(dir); + } catch (e) { + if (e.errno !== ERRNO_CODES.ENOENT) { + throw e; + } + FS.mkdirTree(dir); + } + FS.writeFile(path, new Uint8Array(buffer), {'flags': 'wx+'}); + }, + }, +}; +mergeInto(LibraryManager.library, GodotFS); + +const GodotOS = { + $GodotOS__deps: ['$GodotFS'], + $GodotOS__postset: [ + 'Module["request_quit"] = function() { GodotOS.request_quit() };', + 'GodotOS._fs_sync_promise = Promise.resolve();', + ].join(''), + $GodotOS: { + + request_quit: function() {}, + _async_cbs: [], + _fs_sync_promise: null, + + get_func: function(ptr) { + return wasmTable.get(ptr); + }, + + atexit: function(p_promise_cb) { + GodotOS._async_cbs.push(p_promise_cb); + }, + + finish_async: function(callback) { + GodotOS._fs_sync_promise.then(function(err) { + const promises = []; + GodotOS._async_cbs.forEach(function(cb) { + promises.push(new Promise(cb)); + }); + return Promise.all(promises); + }).then(function() { + return GodotFS.sync(); // Final FS sync. + }).then(function(err) { + // Always deferred. + setTimeout(function() { + callback(); + }, 0); + }); + }, + + allocString: function(p_str) { + const length = lengthBytesUTF8(p_str)+1; + const c_str = _malloc(length); + stringToUTF8(p_str, c_str, length); + return c_str; + }, + + allocStringArray: function(strings) { + const size = strings.length; + const c_ptr = _malloc(size * 4); + for (let i = 0; i < size; i++) { + HEAP32[(c_ptr >> 2) + i] = GodotOS.allocString(strings[i]); + } + return c_ptr; + }, + + freeStringArray: function(c_ptr, size) { + for (let i = 0; i < size; i++) { + _free(HEAP32[(c_ptr >> 2) + i]); + } + _free(c_ptr); + }, + + heapSub: function(heap, ptr, size) { + const bytes = heap.BYTES_PER_ELEMENT; + return heap.subarray(ptr / bytes, ptr / bytes + size); + }, + + heapCopy: function(heap, ptr, size) { + const bytes = heap.BYTES_PER_ELEMENT; + return heap.slice(ptr / bytes, ptr / bytes + size); + }, + }, + + godot_js_os_finish_async: function(p_callback) { + const func = GodotOS.get_func(p_callback); + GodotOS.finish_async(func); + }, + + godot_js_os_request_quit_cb: function(p_callback) { + GodotOS.request_quit = GodotOS.get_func(p_callback); + }, + + godot_js_os_fs_is_persistent: function() { + return GodotFS.is_persistent(); + }, + + godot_js_os_fs_sync: function(callback) { + const func = GodotOS.get_func(callback); + GodotOS._fs_sync_promise = GodotFS.sync(); + GodotOS._fs_sync_promise.then(function(err) { + func(); + }); + }, + + godot_js_os_execute: function(p_json) { + const json_args = UTF8ToString(p_json); + const args = JSON.parse(json_args); + if (GodotConfig.on_execute) { + GodotConfig.on_execute(args); + return 0; + } + return 1; + }, + + godot_js_os_shell_open: function(p_uri) { + window.open(UTF8ToString(p_uri), '_blank'); + }, +}; + +autoAddDeps(GodotOS, '$GodotOS'); +mergeInto(LibraryManager.library, GodotOS); diff --git a/platform/javascript/native/utils.js b/platform/javascript/native/utils.js deleted file mode 100644 index 8d0beba454..0000000000 --- a/platform/javascript/native/utils.js +++ /dev/null @@ -1,292 +0,0 @@ -/*************************************************************************/ -/* utils.js */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 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. */ -/*************************************************************************/ - -Module['initFS'] = function(persistentPaths) { - Module.mount_points = ['/userfs'].concat(persistentPaths); - - function createRecursive(dir) { - try { - FS.stat(dir); - } catch (e) { - if (e.errno !== ERRNO_CODES.ENOENT) { - throw e; - } - FS.mkdirTree(dir); - } - } - - Module.mount_points.forEach(function(path) { - createRecursive(path); - FS.mount(IDBFS, {}, path); - }); - return new Promise(function(resolve, reject) { - FS.syncfs(true, function(err) { - if (err) { - Module.mount_points = []; - Module.idbfs = false; - console.log("IndexedDB not available: " + err.message); - } else { - Module.idbfs = true; - } - resolve(err); - }); - }); -}; - -Module['deinitFS'] = function() { - Module.mount_points.forEach(function(path) { - try { - FS.unmount(path); - } catch (e) { - console.log("Already unmounted", e); - } - if (Module.idbfs && IDBFS.dbs[path]) { - IDBFS.dbs[path].close(); - delete IDBFS.dbs[path]; - } - }); - Module.mount_points = []; -}; - -Module['copyToFS'] = function(path, buffer) { - var p = path.lastIndexOf("/"); - var dir = "/"; - if (p > 0) { - dir = path.slice(0, path.lastIndexOf("/")); - } - try { - FS.stat(dir); - } catch (e) { - if (e.errno !== ERRNO_CODES.ENOENT) { - throw e; - } - FS.mkdirTree(dir); - } - // With memory growth, canOwn should be false. - FS.writeFile(path, new Uint8Array(buffer), {'flags': 'wx+'}); -} - -Module.drop_handler = (function() { - var upload = []; - var uploadPromises = []; - var uploadCallback = null; - - function readFilePromise(entry, path) { - return new Promise(function(resolve, reject) { - entry.file(function(file) { - var reader = new FileReader(); - reader.onload = function() { - var f = { - "path": file.relativePath || file.webkitRelativePath, - "name": file.name, - "type": file.type, - "size": file.size, - "data": reader.result - }; - if (!f['path']) - f['path'] = f['name']; - upload.push(f); - resolve() - }; - reader.onerror = function() { - console.log("Error reading file"); - reject(); - } - - reader.readAsArrayBuffer(file); - - }, function(err) { - console.log("Error!"); - reject(); - }); - }); - } - - function readDirectoryPromise(entry) { - return new Promise(function(resolve, reject) { - var reader = entry.createReader(); - reader.readEntries(function(entries) { - for (var i = 0; i < entries.length; i++) { - var ent = entries[i]; - if (ent.isDirectory) { - uploadPromises.push(readDirectoryPromise(ent)); - } else if (ent.isFile) { - uploadPromises.push(readFilePromise(ent)); - } - } - resolve(); - }); - }); - } - - function processUploadsPromises(resolve, reject) { - if (uploadPromises.length == 0) { - resolve(); - return; - } - uploadPromises.pop().then(function() { - setTimeout(function() { - processUploadsPromises(resolve, reject); - //processUploadsPromises.bind(null, resolve, reject) - }, 0); - }); - } - - function dropFiles(files) { - var args = files || []; - var argc = args.length; - var argv = stackAlloc((argc + 1) * 4); - for (var i = 0; i < argc; i++) { - HEAP32[(argv >> 2) + i] = allocateUTF8OnStack(args[i]); - } - HEAP32[(argv >> 2) + argc] = 0; - // Defined in display_server_javascript.cpp - ccall('_drop_files_callback', 'void', ['number', 'number'], [argv, argc]); - } - - return function(ev) { - ev.preventDefault(); - if (ev.dataTransfer.items) { - // Use DataTransferItemList interface to access the file(s) - for (var i = 0; i < ev.dataTransfer.items.length; i++) { - const item = ev.dataTransfer.items[i]; - var entry = null; - if ("getAsEntry" in item) { - entry = item.getAsEntry(); - } else if ("webkitGetAsEntry" in item) { - entry = item.webkitGetAsEntry(); - } - if (!entry) { - console.error("File upload not supported"); - } else if (entry.isDirectory) { - uploadPromises.push(readDirectoryPromise(entry)); - } else if (entry.isFile) { - uploadPromises.push(readFilePromise(entry)); - } else { - console.error("Unrecognized entry...", entry); - } - } - } else { - console.error("File upload not supported"); - } - uploadCallback = new Promise(processUploadsPromises).then(function() { - const DROP = "/tmp/drop-" + parseInt(Math.random() * Math.pow(2, 31)) + "/"; - var drops = []; - var files = []; - upload.forEach((elem) => { - var path = elem['path']; - Module['copyToFS'](DROP + path, elem['data']); - var idx = path.indexOf("/"); - if (idx == -1) { - // Root file - drops.push(DROP + path); - } else { - // Subdir - var sub = path.substr(0, idx); - idx = sub.indexOf("/"); - if (idx < 0 && drops.indexOf(DROP + sub) == -1) { - drops.push(DROP + sub); - } - } - files.push(DROP + path); - }); - uploadPromises = []; - upload = []; - dropFiles(drops); - var dirs = [DROP.substr(0, DROP.length -1)]; - files.forEach(function (file) { - FS.unlink(file); - var dir = file.replace(DROP, ""); - var idx = dir.lastIndexOf("/"); - while (idx > 0) { - dir = dir.substr(0, idx); - if (dirs.indexOf(DROP + dir) == -1) { - dirs.push(DROP + dir); - } - idx = dir.lastIndexOf("/"); - } - }); - // Remove dirs. - dirs = dirs.sort(function(a, b) { - var al = (a.match(/\//g) || []).length; - var bl = (b.match(/\//g) || []).length; - if (al > bl) - return -1; - else if (al < bl) - return 1; - return 0; - }); - dirs.forEach(function(dir) { - FS.rmdir(dir); - }); - }); - } -})(); - -function EventHandlers() { - function Handler(target, event, method, capture) { - this.target = target; - this.event = event; - this.method = method; - this.capture = capture; - } - - var listeners = []; - - function has(target, event, method, capture) { - return listeners.findIndex(function(e) { - return e.target === target && e.event === event && e.method === method && e.capture == capture; - }) !== -1; - } - - this.add = function(target, event, method, capture) { - if (has(target, event, method, capture)) { - return; - } - listeners.push(new Handler(target, event, method, capture)); - target.addEventListener(event, method, capture); - }; - - this.remove = function(target, event, method, capture) { - if (!has(target, event, method, capture)) { - return; - } - target.removeEventListener(event, method, capture); - }; - - this.clear = function() { - listeners.forEach(function(h) { - h.target.removeEventListener(h.event, h.method, h.capture); - }); - listeners.length = 0; - }; -} - -Module.listeners = new EventHandlers(); diff --git a/platform/javascript/os_javascript.cpp b/platform/javascript/os_javascript.cpp index cf5751f384..80723d54fc 100644 --- a/platform/javascript/os_javascript.cpp +++ b/platform/javascript/os_javascript.cpp @@ -46,6 +46,8 @@ #include <emscripten.h> #include <stdlib.h> +#include "godot_js.h" + // Lifecycle void OS_JavaScript::initialize() { OS_Unix::initialize_core(); @@ -72,24 +74,15 @@ MainLoop *OS_JavaScript::get_main_loop() const { return main_loop; } -extern "C" EMSCRIPTEN_KEEPALIVE void _idb_synced() { - OS_JavaScript::get_singleton()->idb_is_syncing = false; +void OS_JavaScript::fs_sync_callback() { + get_singleton()->idb_is_syncing = false; } bool OS_JavaScript::main_loop_iterate() { if (is_userfs_persistent() && idb_needs_sync && !idb_is_syncing) { idb_is_syncing = true; idb_needs_sync = false; - /* clang-format off */ - EM_ASM({ - FS.syncfs(function(error) { - if (error) { - err('Failed to save IDB file system: ' + error.message); - } - ccall("_idb_synced", 'void', [], []); - }); - }); - /* clang-format on */ + godot_js_os_fs_sync(&fs_sync_callback); } DisplayServer::get_singleton()->process_events(); @@ -104,13 +97,6 @@ void OS_JavaScript::delete_main_loop() { main_loop = nullptr; } -void OS_JavaScript::finalize_async() { - finalizing = true; - if (audio_driver_javascript) { - audio_driver_javascript->finish_async(); - } -} - void OS_JavaScript::finalize() { delete_main_loop(); if (audio_driver_javascript) { @@ -127,17 +113,7 @@ Error OS_JavaScript::execute(const String &p_path, const List<String> &p_argumen args.push_back(E->get()); } String json_args = JSON::print(args); - /* clang-format off */ - int failed = EM_ASM_INT({ - const json_args = UTF8ToString($0); - const args = JSON.parse(json_args); - if (Module["onExecute"]) { - Module["onExecute"](args); - return 0; - } - return 1; - }, json_args.utf8().get_data()); - /* clang-format on */ + int failed = godot_js_os_execute(json_args.utf8().get_data()); ERR_FAIL_COND_V_MSG(failed, ERR_UNAVAILABLE, "OS::execute() must be implemented in JavaScript via 'engine.setOnExecute' if required."); return OK; } @@ -168,11 +144,7 @@ String OS_JavaScript::get_executable_path() const { Error OS_JavaScript::shell_open(String p_uri) { // Open URI in a new tab, browser will deal with it by protocol. - /* clang-format off */ - EM_ASM_({ - window.open(UTF8ToString($0), '_blank'); - }, p_uri.utf8().get_data()); - /* clang-format on */ + godot_js_os_shell_open(p_uri.utf8().get_data()); return OK; } @@ -211,10 +183,6 @@ void OS_JavaScript::file_access_close_callback(const String &p_file, int p_flags } } -void OS_JavaScript::set_idb_available(bool p_idb_available) { - idb_available = p_idb_available; -} - bool OS_JavaScript::is_userfs_persistent() const { return idb_available; } @@ -227,11 +195,17 @@ void OS_JavaScript::initialize_joypads() { } OS_JavaScript::OS_JavaScript() { + char locale_ptr[16]; + godot_js_config_locale_get(locale_ptr, 16); + setenv("LANG", locale_ptr, true); + if (AudioDriverJavaScript::is_available()) { audio_driver_javascript = memnew(AudioDriverJavaScript); AudioDriverManager::add_driver(audio_driver_javascript); } + idb_available = godot_js_os_fs_is_persistent(); + Vector<Logger *> loggers; loggers.push_back(memnew(StdLogger)); _set_logger(memnew(CompositeLogger(loggers))); diff --git a/platform/javascript/os_javascript.h b/platform/javascript/os_javascript.h index 85551d708b..03a3053367 100644 --- a/platform/javascript/os_javascript.h +++ b/platform/javascript/os_javascript.h @@ -42,13 +42,14 @@ class OS_JavaScript : public OS_Unix { MainLoop *main_loop = nullptr; AudioDriverJavaScript *audio_driver_javascript = nullptr; - bool finalizing = false; + bool idb_is_syncing = false; bool idb_available = false; bool idb_needs_sync = false; static void main_loop_callback(); static void file_access_close_callback(const String &p_file, int p_flags); + static void fs_sync_callback(); protected: void initialize() override; @@ -61,15 +62,12 @@ protected: bool _check_internal_feature_support(const String &p_feature) override; public: - bool idb_is_syncing = false; - // Override return type to make writing static callbacks less tedious. static OS_JavaScript *get_singleton(); void initialize_joypads() override; MainLoop *get_main_loop() const override; - void finalize_async(); bool main_loop_iterate(); Error execute(const String &p_path, const List<String> &p_arguments, bool p_blocking = true, ProcessID *r_child_id = nullptr, String *r_pipe = nullptr, int *r_exitcode = nullptr, bool read_stderr = false, Mutex *p_pipe_mutex = nullptr) override; @@ -88,11 +86,9 @@ public: String get_data_path() const override; String get_user_data_dir() const override; - void set_idb_available(bool p_idb_available); bool is_userfs_persistent() const override; void resume_audio(); - bool is_finalizing() { return finalizing; } OS_JavaScript(); }; diff --git a/platform/linuxbsd/SCsub b/platform/linuxbsd/SCsub index ae75a75830..6e43ffcedb 100644 --- a/platform/linuxbsd/SCsub +++ b/platform/linuxbsd/SCsub @@ -18,5 +18,5 @@ common_x11 = [ prog = env.add_program("#bin/godot", ["godot_linuxbsd.cpp"] + common_x11) -if (env["debug_symbols"] == "full" or env["debug_symbols"] == "yes") and env["separate_debug_symbols"]: +if env["debug_symbols"] == "yes" and env["separate_debug_symbols"]: env.AddPostAction(prog, run_in_subprocess(platform_linuxbsd_builders.make_debug_linuxbsd)) diff --git a/platform/linuxbsd/detect.py b/platform/linuxbsd/detect.py index f5e2c72bbc..ea101e24dd 100644 --- a/platform/linuxbsd/detect.py +++ b/platform/linuxbsd/detect.py @@ -73,7 +73,7 @@ def get_opts(): BoolVariable("use_tsan", "Use LLVM/GCC compiler thread sanitizer (TSAN))", False), BoolVariable("pulseaudio", "Detect and use PulseAudio", True), BoolVariable("udev", "Use udev for gamepad connection callbacks", False), - EnumVariable("debug_symbols", "Add debugging symbols to release builds", "yes", ("yes", "no", "full")), + EnumVariable("debug_symbols", "Add debugging symbols to release/release_debug builds", "yes", ("yes", "no")), BoolVariable("separate_debug_symbols", "Create a separate file containing debugging symbols", False), BoolVariable("touch", "Enable touch events", True), BoolVariable("execinfo", "Use libexecinfo on systems where glibc is not available", False), @@ -96,8 +96,6 @@ def configure(env): env.Prepend(CCFLAGS=["-Os"]) if env["debug_symbols"] == "yes": - env.Prepend(CCFLAGS=["-g1"]) - if env["debug_symbols"] == "full": env.Prepend(CCFLAGS=["-g2"]) elif env["target"] == "release_debug": @@ -108,8 +106,6 @@ def configure(env): env.Prepend(CPPDEFINES=["DEBUG_ENABLED"]) if env["debug_symbols"] == "yes": - env.Prepend(CCFLAGS=["-g1"]) - if env["debug_symbols"] == "full": env.Prepend(CCFLAGS=["-g2"]) elif env["target"] == "debug": diff --git a/platform/osx/SCsub b/platform/osx/SCsub index ad62db358b..aa95a89444 100644 --- a/platform/osx/SCsub +++ b/platform/osx/SCsub @@ -18,5 +18,5 @@ files = [ prog = env.add_program("#bin/godot", files) -if (env["debug_symbols"] == "full" or env["debug_symbols"] == "yes") and env["separate_debug_symbols"]: +if env["debug_symbols"] == "yes" and env["separate_debug_symbols"]: env.AddPostAction(prog, run_in_subprocess(platform_osx_builders.make_debug_osx)) diff --git a/platform/osx/detect.py b/platform/osx/detect.py index 50e9bd2653..64e1d94f39 100644 --- a/platform/osx/detect.py +++ b/platform/osx/detect.py @@ -31,7 +31,7 @@ def get_opts(): " validation layers)", False, ), - EnumVariable("debug_symbols", "Add debugging symbols to release builds", "yes", ("yes", "no", "full")), + EnumVariable("debug_symbols", "Add debugging symbols to release/release_debug builds", "yes", ("yes", "no")), BoolVariable("separate_debug_symbols", "Create a separate file containing debugging symbols", False), BoolVariable("use_ubsan", "Use LLVM/GCC compiler undefined behavior sanitizer (UBSAN)", False), BoolVariable("use_asan", "Use LLVM/GCC compiler address sanitizer (ASAN))", False), @@ -57,8 +57,6 @@ def configure(env): env.Prepend(CCFLAGS=["-msse2"]) if env["debug_symbols"] == "yes": - env.Prepend(CCFLAGS=["-g1"]) - if env["debug_symbols"] == "full": env.Prepend(CCFLAGS=["-g2"]) elif env["target"] == "release_debug": @@ -68,8 +66,6 @@ def configure(env): env.Prepend(CCFLAGS=["-Os"]) env.Prepend(CPPDEFINES=["DEBUG_ENABLED"]) if env["debug_symbols"] == "yes": - env.Prepend(CCFLAGS=["-g1"]) - if env["debug_symbols"] == "full": env.Prepend(CCFLAGS=["-g2"]) elif env["target"] == "debug": diff --git a/platform/server/detect.py b/platform/server/detect.py index 4c5a4527ae..f9e151f956 100644 --- a/platform/server/detect.py +++ b/platform/server/detect.py @@ -39,7 +39,7 @@ def get_opts(): BoolVariable("use_asan", "Use LLVM/GCC compiler address sanitizer (ASAN))", False), BoolVariable("use_lsan", "Use LLVM/GCC compiler leak sanitizer (LSAN))", False), BoolVariable("use_tsan", "Use LLVM/GCC compiler thread sanitizer (TSAN))", False), - EnumVariable("debug_symbols", "Add debugging symbols to release builds", "yes", ("yes", "no", "full")), + EnumVariable("debug_symbols", "Add debugging symbols to release/release_debug builds", "yes", ("yes", "no")), BoolVariable("separate_debug_symbols", "Create a separate file containing debugging symbols", False), BoolVariable("execinfo", "Use libexecinfo on systems where glibc is not available", False), ] @@ -61,8 +61,6 @@ def configure(env): env.Prepend(CCFLAGS=["-Os"]) if env["debug_symbols"] == "yes": - env.Prepend(CCFLAGS=["-g1"]) - if env["debug_symbols"] == "full": env.Prepend(CCFLAGS=["-g2"]) elif env["target"] == "release_debug": @@ -73,8 +71,6 @@ def configure(env): env.Prepend(CPPDEFINES=["DEBUG_ENABLED"]) if env["debug_symbols"] == "yes": - env.Prepend(CCFLAGS=["-g1"]) - if env["debug_symbols"] == "full": env.Prepend(CCFLAGS=["-g2"]) elif env["target"] == "debug": diff --git a/platform/windows/SCsub b/platform/windows/SCsub index e3f86977a4..0c9aa77803 100644 --- a/platform/windows/SCsub +++ b/platform/windows/SCsub @@ -32,5 +32,5 @@ if env["vsproj"]: env.vs_srcs += ["platform/windows/" + str(x)] if not os.getenv("VCINSTALLDIR"): - if (env["debug_symbols"] == "full" or env["debug_symbols"] == "yes") and env["separate_debug_symbols"]: + if env["debug_symbols"] == "yes" and env["separate_debug_symbols"]: env.AddPostAction(prog, run_in_subprocess(platform_windows_builders.make_debug_mingw)) diff --git a/platform/windows/detect.py b/platform/windows/detect.py index 489e45404f..934314b2f2 100644 --- a/platform/windows/detect.py +++ b/platform/windows/detect.py @@ -64,7 +64,7 @@ def get_opts(): # XP support dropped after EOL due to missing API for IPv6 and other issues # Vista support dropped after EOL due to GH-10243 ("target_win_version", "Targeted Windows version, >= 0x0601 (Windows 7)", "0x0601"), - EnumVariable("debug_symbols", "Add debugging symbols to release builds", "yes", ("yes", "no", "full")), + EnumVariable("debug_symbols", "Add debugging symbols to release/release_debug builds", "yes", ("yes", "no")), EnumVariable("windows_subsystem", "Windows subsystem", "default", ("default", "console", "gui")), BoolVariable("separate_debug_symbols", "Create a separate file containing debugging symbols", False), ("msvc_version", "MSVC version to use. Ignored if VCINSTALLDIR is set in shell env.", None), @@ -209,7 +209,7 @@ def configure_msvc(env, manual_msvc_config): env.AppendUnique(CPPDEFINES=["DEBUG_ENABLED"]) env.Append(LINKFLAGS=["/DEBUG"]) - if env["debug_symbols"] == "full" or env["debug_symbols"] == "yes": + if env["debug_symbols"] == "yes": env.AppendUnique(CCFLAGS=["/Z7"]) env.AppendUnique(LINKFLAGS=["/DEBUG"]) @@ -337,16 +337,12 @@ def configure_mingw(env): env.Prepend(CCFLAGS=["-Os"]) if env["debug_symbols"] == "yes": - env.Prepend(CCFLAGS=["-g1"]) - if env["debug_symbols"] == "full": env.Prepend(CCFLAGS=["-g2"]) elif env["target"] == "release_debug": env.Append(CCFLAGS=["-O2"]) env.Append(CPPDEFINES=["DEBUG_ENABLED"]) if env["debug_symbols"] == "yes": - env.Prepend(CCFLAGS=["-g1"]) - if env["debug_symbols"] == "full": env.Prepend(CCFLAGS=["-g2"]) if env["optimize"] == "speed": # optimize for speed (default) env.Append(CCFLAGS=["-O2"]) diff --git a/platform/windows/godot.natvis b/platform/windows/godot.natvis index 90f0b55d0a..1f625cfb77 100644 --- a/platform/windows/godot.natvis +++ b/platform/windows/godot.natvis @@ -49,7 +49,7 @@ <DisplayString Condition="type == Variant::QUAT">{*(Quat *)_data._mem}</DisplayString> <DisplayString Condition="type == Variant::COLOR">{*(Color *)_data._mem}</DisplayString> <DisplayString Condition="type == Variant::NODE_PATH">{*(NodePath *)_data._mem}</DisplayString> - <DisplayString Condition="type == Variant::_RID">{*(RID *)_data._mem}</DisplayString> + <DisplayString Condition="type == Variant::RID">{*(RID *)_data._mem}</DisplayString> <DisplayString Condition="type == Variant::OBJECT">{*(Object *)_data._mem}</DisplayString> <DisplayString Condition="type == Variant::DICTIONARY">{*(Dictionary *)_data._mem}</DisplayString> <DisplayString Condition="type == Variant::ARRAY">{*(Array *)_data._mem}</DisplayString> @@ -79,7 +79,7 @@ <Item Name="[value]" Condition="type == Variant::QUAT">*(Quat *)_data._mem</Item> <Item Name="[value]" Condition="type == Variant::COLOR">*(Color *)_data._mem</Item> <Item Name="[value]" Condition="type == Variant::NODE_PATH">*(NodePath *)_data._mem</Item> - <Item Name="[value]" Condition="type == Variant::_RID">*(RID *)_data._mem</Item> + <Item Name="[value]" Condition="type == Variant::RID">*(RID *)_data._mem</Item> <Item Name="[value]" Condition="type == Variant::OBJECT">*(Object *)_data._mem</Item> <Item Name="[value]" Condition="type == Variant::DICTIONARY">*(Dictionary *)_data._mem</Item> <Item Name="[value]" Condition="type == Variant::ARRAY">*(Array *)_data._mem</Item> diff --git a/scene/gui/line_edit.cpp b/scene/gui/line_edit.cpp index 5fccde3597..649f5a5f66 100644 --- a/scene/gui/line_edit.cpp +++ b/scene/gui/line_edit.cpp @@ -679,7 +679,7 @@ void LineEdit::_notification(int p_what) { } break; #endif case NOTIFICATION_RESIZED: { - window_pos = 0; + scroll_offset = 0; set_cursor_position(get_cursor_position()); } break; @@ -735,7 +735,7 @@ void LineEdit::_notification(int p_what) { x_ofs = style->get_offset().x; } break; case ALIGN_CENTER: { - if (window_pos != 0) { + if (scroll_offset != 0) { x_ofs = style->get_offset().x; } else { x_ofs = MAX(style->get_margin(MARGIN_LEFT), int(size.width - (cached_text_width)) / 2); @@ -747,7 +747,7 @@ void LineEdit::_notification(int p_what) { } int ofs_max = width - style->get_margin(MARGIN_RIGHT); - int char_ofs = window_pos; + int char_ofs = scroll_offset; int y_area = height - style->get_minimum_size().height; int y_ofs = style->get_offset().y + (y_area - font->get_height()) / 2; @@ -780,7 +780,7 @@ void LineEdit::_notification(int p_what) { r_icon->draw(ci, Point2(width - r_icon->get_width() - style->get_margin(MARGIN_RIGHT), height / 2 - r_icon->get_height() / 2), color_icon); if (align == ALIGN_CENTER) { - if (window_pos == 0) { + if (scroll_offset == 0) { x_ofs = MAX(style->get_margin(MARGIN_LEFT), int(size.width - cached_text_width - r_icon->get_width() - style->get_margin(MARGIN_RIGHT) * 2) / 2); } } else { @@ -1023,7 +1023,7 @@ void LineEdit::undo() { TextOperation op = undo_stack_pos->get(); text = op.text; cached_width = op.cached_width; - window_pos = op.window_pos; + scroll_offset = op.scroll_offset; set_cursor_position(op.cursor_pos); if (expand_to_text_length) { @@ -1044,7 +1044,7 @@ void LineEdit::redo() { TextOperation op = undo_stack_pos->get(); text = op.text; cached_width = op.cached_width; - window_pos = op.window_pos; + scroll_offset = op.scroll_offset; set_cursor_position(op.cursor_pos); if (expand_to_text_length) { @@ -1071,7 +1071,7 @@ void LineEdit::shift_selection_check_post(bool p_shift) { void LineEdit::set_cursor_at_pixel_pos(int p_x) { Ref<Font> font = get_theme_font("font"); - int ofs = window_pos; + int ofs = scroll_offset; Ref<StyleBox> style = get_theme_stylebox("normal"); int pixel_ofs = 0; Size2 size = get_size(); @@ -1084,7 +1084,7 @@ void LineEdit::set_cursor_at_pixel_pos(int p_x) { pixel_ofs = int(style->get_offset().x); } break; case ALIGN_CENTER: { - if (window_pos != 0) { + if (scroll_offset != 0) { pixel_ofs = int(style->get_offset().x); } else { pixel_ofs = int(size.width - (cached_width)) / 2; @@ -1122,7 +1122,7 @@ void LineEdit::set_cursor_at_pixel_pos(int p_x) { int LineEdit::get_cursor_pixel_pos() { Ref<Font> font = get_theme_font("font"); - int ofs = window_pos; + int ofs = scroll_offset; Ref<StyleBox> style = get_theme_stylebox("normal"); int pixel_ofs = 0; Size2 size = get_size(); @@ -1135,7 +1135,7 @@ int LineEdit::get_cursor_pixel_pos() { pixel_ofs = int(style->get_offset().x); } break; case ALIGN_CENTER: { - if (window_pos != 0) { + if (scroll_offset != 0) { pixel_ofs = int(style->get_offset().x); } else { pixel_ofs = int(size.width - (cached_width)) / 2; @@ -1236,7 +1236,7 @@ void LineEdit::delete_char() { set_cursor_position(get_cursor_position() - 1); if (align == ALIGN_CENTER || align == ALIGN_RIGHT) { - window_pos = CLAMP(window_pos - 1, 0, MAX(text.length() - 1, 0)); + scroll_offset = CLAMP(scroll_offset - 1, 0, MAX(text.length() - 1, 0)); } _text_changed(); @@ -1262,12 +1262,12 @@ void LineEdit::delete_text(int p_from_column, int p_to_column) { if (cursor_pos >= text.length()) { cursor_pos = text.length(); } - if (window_pos > cursor_pos) { - window_pos = cursor_pos; + if (scroll_offset > cursor_pos) { + scroll_offset = cursor_pos; } if (align == ALIGN_CENTER || align == ALIGN_RIGHT) { - window_pos = CLAMP(window_pos - (p_to_column - p_from_column), 0, MAX(text.length() - 1, 0)); + scroll_offset = CLAMP(scroll_offset - (p_to_column - p_from_column), 0, MAX(text.length() - 1, 0)); } if (!text_changed_dirty) { @@ -1288,7 +1288,7 @@ void LineEdit::set_text(String p_text) { update(); cursor_pos = 0; - window_pos = 0; + scroll_offset = 0; } void LineEdit::clear() { @@ -1332,16 +1332,16 @@ void LineEdit::set_cursor_position(int p_pos) { cursor_pos = p_pos; if (!is_inside_tree()) { - window_pos = cursor_pos; + scroll_offset = cursor_pos; return; } Ref<StyleBox> style = get_theme_stylebox("normal"); Ref<Font> font = get_theme_font("font"); - if (cursor_pos <= window_pos) { + if (cursor_pos <= scroll_offset) { // Adjust window if cursor goes too much to the left. - set_window_pos(MAX(0, cursor_pos - 1)); + set_scroll_offset(MAX(0, cursor_pos - 1)); } else { // Adjust window if cursor goes too much to the right. int window_width = get_size().width - style->get_minimum_size().width; @@ -1354,12 +1354,12 @@ void LineEdit::set_cursor_position(int p_pos) { if (window_width < 0) { return; } - int wp = window_pos; + int wp = scroll_offset; if (font.is_valid()) { int accum_width = 0; - for (int i = cursor_pos; i >= window_pos; i--) { + for (int i = cursor_pos; i >= scroll_offset; i--) { if (i >= text.length()) { // Do not do this, because if the cursor is at the end, its just fine that it takes no space. // accum_width = font->get_char_size(' ').width; @@ -1378,8 +1378,8 @@ void LineEdit::set_cursor_position(int p_pos) { } } - if (wp != window_pos) { - set_window_pos(wp); + if (wp != scroll_offset) { + set_scroll_offset(wp); } } update(); @@ -1389,13 +1389,17 @@ int LineEdit::get_cursor_position() const { return cursor_pos; } -void LineEdit::set_window_pos(int p_pos) { - window_pos = p_pos; - if (window_pos < 0) { - window_pos = 0; +void LineEdit::set_scroll_offset(int p_pos) { + scroll_offset = p_pos; + if (scroll_offset < 0) { + scroll_offset = 0; } } +int LineEdit::get_scroll_offset() const { + return scroll_offset; +} + void LineEdit::append_at_cursor(String p_text) { if ((max_length <= 0) || (text.length() + p_text.length() <= max_length)) { String pre = text.substr(0, cursor_pos); @@ -1413,7 +1417,7 @@ void LineEdit::clear_internal() { _clear_undo_stack(); cached_width = 0; cursor_pos = 0; - window_pos = 0; + scroll_offset = 0; undo_text = ""; text = ""; update(); @@ -1644,7 +1648,7 @@ void LineEdit::_editor_settings_changed() { void LineEdit::set_expand_to_text_length(bool p_enabled) { expand_to_text_length = p_enabled; minimum_size_changed(); - set_window_pos(0); + set_scroll_offset(0); } bool LineEdit::get_expand_to_text_length() const { @@ -1771,7 +1775,7 @@ void LineEdit::_create_undo_state() { op.text = text; op.cached_width = cached_width; op.cursor_pos = cursor_pos; - op.window_pos = window_pos; + op.scroll_offset = scroll_offset; undo_stack.push_back(op); } @@ -1816,6 +1820,7 @@ void LineEdit::_bind_methods() { ClassDB::bind_method(D_METHOD("get_placeholder_alpha"), &LineEdit::get_placeholder_alpha); ClassDB::bind_method(D_METHOD("set_cursor_position", "position"), &LineEdit::set_cursor_position); ClassDB::bind_method(D_METHOD("get_cursor_position"), &LineEdit::get_cursor_position); + ClassDB::bind_method(D_METHOD("get_scroll_offset"), &LineEdit::get_scroll_offset); ClassDB::bind_method(D_METHOD("set_expand_to_text_length", "enabled"), &LineEdit::set_expand_to_text_length); ClassDB::bind_method(D_METHOD("get_expand_to_text_length"), &LineEdit::get_expand_to_text_length); ClassDB::bind_method(D_METHOD("cursor_set_blink_enabled", "enabled"), &LineEdit::cursor_set_blink_enabled); @@ -1898,7 +1903,7 @@ LineEdit::LineEdit() { cached_width = 0; cached_placeholder_width = 0; cursor_pos = 0; - window_pos = 0; + scroll_offset = 0; window_has_focus = true; max_length = 0; pass = false; diff --git a/scene/gui/line_edit.h b/scene/gui/line_edit.h index d6cc1f1f11..a5e5b6988f 100644 --- a/scene/gui/line_edit.h +++ b/scene/gui/line_edit.h @@ -80,7 +80,7 @@ private: PopupMenu *menu; int cursor_pos; - int window_pos; + int scroll_offset; int max_length; // 0 for no maximum. int cached_width; @@ -106,7 +106,7 @@ private: struct TextOperation { int cursor_pos; - int window_pos; + int scroll_offset; int cached_width; String text; }; @@ -144,7 +144,8 @@ private: void shift_selection_check_post(bool); void selection_fill_at_cursor(); - void set_window_pos(int p_pos); + void set_scroll_offset(int p_pos); + int get_scroll_offset() const; void set_cursor_at_pixel_pos(int p_x); int get_cursor_pixel_pos(); diff --git a/scene/main/shader_globals_override.cpp b/scene/main/shader_globals_override.cpp index 86c08ca6e9..432fb5b4fb 100644 --- a/scene/main/shader_globals_override.cpp +++ b/scene/main/shader_globals_override.cpp @@ -202,7 +202,7 @@ void ShaderGlobalsOverride::_get_property_list(List<PropertyInfo> *p_list) const Override o; o.in_use = false; Callable::CallError ce; - o.override = Variant::construct(pinfo.type, nullptr, 0, ce); + Variant::construct(pinfo.type, o.override, nullptr, 0, ce); overrides[variables[i]] = o; } diff --git a/scene/resources/shader.cpp b/scene/resources/shader.cpp index 92f0353abf..76d37eaa71 100644 --- a/scene/resources/shader.cpp +++ b/scene/resources/shader.cpp @@ -29,6 +29,7 @@ /*************************************************************************/ #include "shader.h" + #include "core/os/file_access.h" #include "scene/scene_string_names.h" #include "servers/rendering/shader_language.h" @@ -80,7 +81,7 @@ void Shader::get_param_list(List<PropertyInfo> *p_params) const { params_cache[pi.name] = E->get().name; if (p_params) { //small little hack - if (pi.type == Variant::_RID) { + if (pi.type == Variant::RID) { pi.type = Variant::OBJECT; } p_params->push_back(pi); diff --git a/scene/resources/world_2d.cpp b/scene/resources/world_2d.cpp index 7bff850a15..41d3fe20be 100644 --- a/scene/resources/world_2d.cpp +++ b/scene/resources/world_2d.cpp @@ -341,8 +341,8 @@ void World2D::_bind_methods() { ClassDB::bind_method(D_METHOD("get_direct_space_state"), &World2D::get_direct_space_state); - ADD_PROPERTY(PropertyInfo(Variant::_RID, "canvas", PROPERTY_HINT_NONE, "", 0), "", "get_canvas"); - ADD_PROPERTY(PropertyInfo(Variant::_RID, "space", PROPERTY_HINT_NONE, "", 0), "", "get_space"); + ADD_PROPERTY(PropertyInfo(Variant::RID, "canvas", PROPERTY_HINT_NONE, "", 0), "", "get_canvas"); + ADD_PROPERTY(PropertyInfo(Variant::RID, "space", PROPERTY_HINT_NONE, "", 0), "", "get_space"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "direct_space_state", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsDirectSpaceState2D", 0), "", "get_direct_space_state"); } diff --git a/scene/resources/world_3d.cpp b/scene/resources/world_3d.cpp index 8100f150ef..b8fb3825ce 100644 --- a/scene/resources/world_3d.cpp +++ b/scene/resources/world_3d.cpp @@ -321,8 +321,8 @@ void World3D::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "environment", PROPERTY_HINT_RESOURCE_TYPE, "Environment"), "set_environment", "get_environment"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "fallback_environment", PROPERTY_HINT_RESOURCE_TYPE, "Environment"), "set_fallback_environment", "get_fallback_environment"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "camera_effects", PROPERTY_HINT_RESOURCE_TYPE, "CameraEffects"), "set_camera_effects", "get_camera_effects"); - ADD_PROPERTY(PropertyInfo(Variant::_RID, "space", PROPERTY_HINT_NONE, "", 0), "", "get_space"); - ADD_PROPERTY(PropertyInfo(Variant::_RID, "scenario", PROPERTY_HINT_NONE, "", 0), "", "get_scenario"); + ADD_PROPERTY(PropertyInfo(Variant::RID, "space", PROPERTY_HINT_NONE, "", 0), "", "get_space"); + ADD_PROPERTY(PropertyInfo(Variant::RID, "scenario", PROPERTY_HINT_NONE, "", 0), "", "get_scenario"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "direct_space_state", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsDirectSpaceState3D", 0), "", "get_direct_space_state"); } diff --git a/servers/audio_server.cpp b/servers/audio_server.cpp index fdce55320c..bea5e9e432 100644 --- a/servers/audio_server.cpp +++ b/servers/audio_server.cpp @@ -905,6 +905,8 @@ bool AudioServer::is_bus_channel_active(int p_bus, int p_channel) const { } void AudioServer::set_global_rate_scale(float p_scale) { + ERR_FAIL_COND(p_scale <= 0); + global_rate_scale = p_scale; } diff --git a/servers/physics_server_2d.cpp b/servers/physics_server_2d.cpp index 88cdd9f991..1ea8985543 100644 --- a/servers/physics_server_2d.cpp +++ b/servers/physics_server_2d.cpp @@ -245,11 +245,11 @@ void PhysicsShapeQueryParameters2D::_bind_methods() { ClassDB::bind_method(D_METHOD("is_collide_with_areas_enabled"), &PhysicsShapeQueryParameters2D::is_collide_with_areas_enabled); ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_collision_layer", "get_collision_layer"); - ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "exclude", PROPERTY_HINT_NONE, itos(Variant::_RID) + ":"), "set_exclude", "get_exclude"); + ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "exclude", PROPERTY_HINT_NONE, itos(Variant::RID) + ":"), "set_exclude", "get_exclude"); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "margin", PROPERTY_HINT_RANGE, "0,100,0.01"), "set_margin", "get_margin"); ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "motion"), "set_motion", "get_motion"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape2D"), "set_shape", "get_shape"); - ADD_PROPERTY(PropertyInfo(Variant::_RID, "shape_rid"), "set_shape_rid", "get_shape_rid"); + ADD_PROPERTY(PropertyInfo(Variant::RID, "shape_rid"), "set_shape_rid", "get_shape_rid"); ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "transform"), "set_transform", "get_transform"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_bodies"), "set_collide_with_bodies", "is_collide_with_bodies_enabled"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_areas"), "set_collide_with_areas", "is_collide_with_areas_enabled"); @@ -504,7 +504,7 @@ void PhysicsTestMotionResult2D::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "collision_normal"), "", "get_collision_normal"); ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "collider_velocity"), "", "get_collider_velocity"); ADD_PROPERTY(PropertyInfo(Variant::INT, "collider_id", PROPERTY_HINT_OBJECT_ID), "", "get_collider_id"); - ADD_PROPERTY(PropertyInfo(Variant::_RID, "collider_rid"), "", "get_collider_rid"); + ADD_PROPERTY(PropertyInfo(Variant::RID, "collider_rid"), "", "get_collider_rid"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "collider"), "", "get_collider"); ADD_PROPERTY(PropertyInfo(Variant::INT, "collider_shape"), "", "get_collider_shape"); } diff --git a/servers/physics_server_3d.cpp b/servers/physics_server_3d.cpp index 5796bccbac..fabd661970 100644 --- a/servers/physics_server_3d.cpp +++ b/servers/physics_server_3d.cpp @@ -238,10 +238,10 @@ void PhysicsShapeQueryParameters3D::_bind_methods() { ClassDB::bind_method(D_METHOD("is_collide_with_areas_enabled"), &PhysicsShapeQueryParameters3D::is_collide_with_areas_enabled); ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask"); - ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "exclude", PROPERTY_HINT_NONE, itos(Variant::_RID) + ":"), "set_exclude", "get_exclude"); + ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "exclude", PROPERTY_HINT_NONE, itos(Variant::RID) + ":"), "set_exclude", "get_exclude"); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "margin", PROPERTY_HINT_RANGE, "0,100,0.01"), "set_margin", "get_margin"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape3D"), "set_shape", "get_shape"); - ADD_PROPERTY(PropertyInfo(Variant::_RID, "shape_rid"), "set_shape_rid", "get_shape_rid"); + ADD_PROPERTY(PropertyInfo(Variant::RID, "shape_rid"), "set_shape_rid", "get_shape_rid"); ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM, "transform"), "set_transform", "get_transform"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_bodies"), "set_collide_with_bodies", "is_collide_with_bodies_enabled"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_areas"), "set_collide_with_areas", "is_collide_with_areas_enabled"); diff --git a/servers/rendering/rasterizer_rd/rasterizer_canvas_rd.cpp b/servers/rendering/rasterizer_rd/rasterizer_canvas_rd.cpp index 01ed7b8d6a..5d9e68f2b4 100644 --- a/servers/rendering/rasterizer_rd/rasterizer_canvas_rd.cpp +++ b/servers/rendering/rasterizer_rd/rasterizer_canvas_rd.cpp @@ -2567,6 +2567,4 @@ RasterizerCanvasRD::~RasterizerCanvasRD() { storage->free(default_canvas_texture); //pipelines don't need freeing, they are all gone after shaders are gone - - RD::get_singleton()->free(state.default_transforms_uniform_set); } diff --git a/tests/test_class_db.h b/tests/test_class_db.h index 63c3a55ea8..29fe837587 100644 --- a/tests/test_class_db.h +++ b/tests/test_class_db.h @@ -255,7 +255,7 @@ bool arg_default_value_is_assignable_to_type(const Context &p_context, const Var case Variant::VECTOR2: case Variant::RECT2: case Variant::VECTOR3: - case Variant::_RID: + case Variant::RID: case Variant::ARRAY: case Variant::DICTIONARY: case Variant::PACKED_BYTE_ARRAY: diff --git a/thirdparty/README.md b/thirdparty/README.md index ac1ce6d5fc..f590acaa0b 100644 --- a/thirdparty/README.md +++ b/thirdparty/README.md @@ -378,7 +378,7 @@ Collection of single-file libraries used in Godot components. * License: Apache 2.0 - `open-simplex-noise.{c,h}` * Upstream: https://github.com/smcameron/open-simplex-noise-in-c - * Version: git (0d555e7f40527d0870906fe9469a3b1bb4020b7f, 2015) + custom changes + * Version: git (0fef0dbedd76f767da7e3c894822729d0f07e54d, 2020) + custom changes * License: Unlicense - `pcg.{cpp,h}` * Upstream: http://www.pcg-random.org diff --git a/thirdparty/jpeg-compressor/jpgd.cpp b/thirdparty/jpeg-compressor/jpgd.cpp index baf6ea0484..d6b5d96aac 100644 --- a/thirdparty/jpeg-compressor/jpgd.cpp +++ b/thirdparty/jpeg-compressor/jpgd.cpp @@ -1669,7 +1669,7 @@ namespace jpgd { int row = m_max_mcu_y_size - m_mcu_lines_left; uint8* d0 = m_pScan_line_0; - const int half_image_x_size = (m_image_x_size >> 1) - 1; + const int half_image_x_size = (m_image_x_size == 1) ? 0 : (m_image_x_size >> 1) - 1; const int row_x8 = row * 8; for (int x = 0; x < m_image_x_size; x++) @@ -1762,7 +1762,7 @@ namespace jpgd { int y = m_image_y_size - m_total_lines_left; int row = y & 15; - const int half_image_y_size = (m_image_y_size >> 1) - 1; + const int half_image_y_size = (m_image_y_size == 1) ? 0 : (m_image_y_size >> 1) - 1; uint8* d0 = m_pScan_line_0; @@ -1891,7 +1891,7 @@ namespace jpgd { int y = m_image_y_size - m_total_lines_left; int row = y & 15; - const int half_image_y_size = (m_image_y_size >> 1) - 1; + const int half_image_y_size = (m_image_y_size == 1) ? 0 : (m_image_y_size >> 1) - 1; uint8* d0 = m_pScan_line_0; @@ -1915,7 +1915,7 @@ namespace jpgd { const int y0_base = (c_y0 & 7) * 8 + 256; const int y1_base = (c_y1 & 7) * 8 + 256; - const int half_image_x_size = (m_image_x_size >> 1) - 1; + const int half_image_x_size = (m_image_x_size == 1) ? 0 : (m_image_x_size >> 1) - 1; static const uint8_t s_muls[2][2][4] = { diff --git a/thirdparty/misc/open-simplex-noise.c b/thirdparty/misc/open-simplex-noise.c index 42f2fbb5be..88fbd3e51d 100644 --- a/thirdparty/misc/open-simplex-noise.c +++ b/thirdparty/misc/open-simplex-noise.c @@ -189,14 +189,15 @@ int open_simplex_noise(int64_t seed, struct osn_context *ctx) permGradIndex3D = ctx->permGradIndex3D; // -- GODOT end -- + uint64_t seedU = seed; for (i = 0; i < 256; i++) source[i] = (int16_t) i; - seed = seed * 6364136223846793005LL + 1442695040888963407LL; - seed = seed * 6364136223846793005LL + 1442695040888963407LL; - seed = seed * 6364136223846793005LL + 1442695040888963407LL; + seedU = seedU * 6364136223846793005ULL + 1442695040888963407ULL; + seedU = seedU * 6364136223846793005ULL + 1442695040888963407ULL; + seedU = seedU * 6364136223846793005ULL + 1442695040888963407ULL; for (i = 255; i >= 0; i--) { - seed = seed * 6364136223846793005LL + 1442695040888963407LL; - r = (int)((seed + 31) % (i + 1)); + seedU = seedU * 6364136223846793005ULL + 1442695040888963407ULL; + r = (int)((seedU + 31) % (i + 1)); if (r < 0) r += (i + 1); perm[i] = source[r]; |