diff options
Diffstat (limited to 'core')
142 files changed, 2866 insertions, 1674 deletions
diff --git a/core/SCsub b/core/SCsub index 85e5f1b089..ed9a0a231d 100644 --- a/core/SCsub +++ b/core/SCsub @@ -159,6 +159,7 @@ env.CommandNoCache('#core/license.gen.h', ["../COPYRIGHT.txt", "../LICENSE.txt"] # Chain load SCsubs SConscript('os/SCsub') SConscript('math/SCsub') +SConscript('crypto/SCsub') SConscript('io/SCsub') SConscript('bind/SCsub') diff --git a/core/array.cpp b/core/array.cpp index a334af2c04..fd507f46c3 100644 --- a/core/array.cpp +++ b/core/array.cpp @@ -133,18 +133,12 @@ void Array::erase(const Variant &p_value) { } Variant Array::front() const { - if (_p->array.size() == 0) { - ERR_EXPLAIN("Can't take value from empty array"); - ERR_FAIL_V(Variant()); - } + ERR_FAIL_COND_V_MSG(_p->array.size() == 0, Variant(), "Can't take value from empty array."); return operator[](0); } Variant Array::back() const { - if (_p->array.size() == 0) { - ERR_EXPLAIN("Can't take value from empty array"); - ERR_FAIL_V(Variant()); - } + ERR_FAIL_COND_V_MSG(_p->array.size() == 0, Variant(), "Can't take value from empty array."); return operator[](_p->array.size() - 1); } @@ -228,6 +222,66 @@ Array Array::duplicate(bool p_deep) const { return new_arr; } + +int Array::_fix_slice_index(int p_index, int p_arr_len, int p_top_mod) { + p_index = CLAMP(p_index, -p_arr_len, p_arr_len + p_top_mod); + if (p_index < 0) { + p_index = (p_index % p_arr_len + p_arr_len) % p_arr_len; // positive modulo + } + return p_index; +} + +int Array::_clamp_index(int p_index) const { + return CLAMP(p_index, -size() + 1, size() - 1); +} + +#define ARRAY_GET_DEEP(idx, is_deep) is_deep ? get(idx).duplicate(is_deep) : get(idx) + +Array Array::slice(int p_begin, int p_end, int p_step, bool p_deep) const { // like python, but inclusive on upper bound + Array new_arr; + + if (empty()) // Don't try to slice empty arrays. + return new_arr; + + p_begin = Array::_fix_slice_index(p_begin, size(), -1); // can't start out of range + p_end = Array::_fix_slice_index(p_end, size(), 0); + + int x = p_begin; + int new_arr_i = 0; + + ERR_FAIL_COND_V(p_step == 0, new_arr); + if (Array::_clamp_index(p_begin) == Array::_clamp_index(p_end)) { // don't include element twice + new_arr.resize(1); + // new_arr[0] = 1; + new_arr[0] = ARRAY_GET_DEEP(Array::_clamp_index(p_begin), p_deep); + return new_arr; + } else { + int element_count = ceil((int)MAX(0, (p_end - p_begin) / p_step)) + 1; + if (element_count == 1) { // delta going in wrong direction to reach end + new_arr.resize(0); + return new_arr; + } + new_arr.resize(element_count); + } + + // if going backwards, have to have a different terminating condition + if (p_step < 0) { + while (x >= p_end) { + new_arr[new_arr_i] = ARRAY_GET_DEEP(Array::_clamp_index(x), p_deep); + x += p_step; + new_arr_i += 1; + } + } else if (p_step > 0) { + while (x <= p_end) { + new_arr[new_arr_i] = ARRAY_GET_DEEP(Array::_clamp_index(x), p_deep); + x += p_step; + new_arr_i += 1; + } + } + + return new_arr; +} + struct _ArrayVariantSort { _FORCE_INLINE_ bool operator()(const Variant &p_l, const Variant &p_r) const { diff --git a/core/array.h b/core/array.h index d4e937a486..7a754d53ea 100644 --- a/core/array.h +++ b/core/array.h @@ -44,6 +44,9 @@ class Array { void _ref(const Array &p_from) const; void _unref() const; + int _clamp_index(int p_index) const; + static int _fix_slice_index(int p_index, int p_arr_len, int p_top_mod); + public: Variant &operator[](int p_idx); const Variant &operator[](int p_idx) const; @@ -91,6 +94,8 @@ public: Array duplicate(bool p_deep = false) const; + Array slice(int p_begin, int p_end, int p_step = 1, bool p_deep = false) const; + Variant min() const; Variant max() const; diff --git a/core/bind/core_bind.cpp b/core/bind/core_bind.cpp index b41b84ab1e..d07ba44788 100644 --- a/core/bind/core_bind.cpp +++ b/core/bind/core_bind.cpp @@ -30,11 +30,11 @@ #include "core_bind.h" +#include "core/crypto/crypto_core.h" #include "core/io/file_access_compressed.h" #include "core/io/file_access_encrypted.h" #include "core/io/json.h" #include "core/io/marshalls.h" -#include "core/math/crypto_core.h" #include "core/math/geometry.h" #include "core/os/keyboard.h" #include "core/os/os.h" @@ -73,10 +73,7 @@ RES _ResourceLoader::load(const String &p_path, const String &p_type_hint, bool Error err = OK; RES ret = ResourceLoader::load(p_path, p_type_hint, p_no_cache, &err); - if (err != OK) { - ERR_EXPLAIN("Error loading resource: '" + p_path + "'"); - ERR_FAIL_V(ret); - } + ERR_FAIL_COND_V_MSG(err != OK, ret, "Error loading resource: '" + p_path + "'."); return ret; } @@ -148,16 +145,13 @@ _ResourceLoader::_ResourceLoader() { } Error _ResourceSaver::save(const String &p_path, const RES &p_resource, SaverFlags p_flags) { - if (p_resource.is_null()) { - ERR_EXPLAIN("Can't save empty resource to path: " + String(p_path)) - ERR_FAIL_V(ERR_INVALID_PARAMETER); - } + ERR_FAIL_COND_V_MSG(p_resource.is_null(), ERR_INVALID_PARAMETER, "Can't save empty resource to path '" + String(p_path) + "'."); return ResourceSaver::save(p_path, p_resource, p_flags); } PoolVector<String> _ResourceSaver::get_recognized_extensions(const RES &p_resource) { - ERR_FAIL_COND_V(p_resource.is_null(), PoolVector<String>()); + ERR_FAIL_COND_V_MSG(p_resource.is_null(), PoolVector<String>(), "It's not a reference to a valid Resource object."); List<String> exts; ResourceSaver::get_recognized_extensions(p_resource, &exts); PoolVector<String> ret; @@ -191,10 +185,31 @@ _ResourceSaver::_ResourceSaver() { /////////////////OS +void _OS::global_menu_add_item(const String &p_menu, const String &p_label, const Variant &p_signal, const Variant &p_meta) { + + OS::get_singleton()->global_menu_add_item(p_menu, p_label, p_signal, p_meta); +} + +void _OS::global_menu_add_separator(const String &p_menu) { + + OS::get_singleton()->global_menu_add_separator(p_menu); +} + +void _OS::global_menu_remove_item(const String &p_menu, int p_idx) { + + OS::get_singleton()->global_menu_remove_item(p_menu, p_idx); +} + +void _OS::global_menu_clear(const String &p_menu) { + + OS::get_singleton()->global_menu_clear(p_menu); +} + Point2 _OS::get_mouse_position() const { return OS::get_singleton()->get_mouse_position(); } + void _OS::set_window_title(const String &p_title) { OS::get_singleton()->set_window_title(p_title); @@ -208,6 +223,7 @@ int _OS::get_mouse_button_state() const { String _OS::get_unique_id() const { return OS::get_singleton()->get_unique_id(); } + bool _OS::has_touchscreen_ui_hint() const { return OS::get_singleton()->has_touchscreen_ui_hint(); @@ -217,6 +233,7 @@ void _OS::set_clipboard(const String &p_text) { OS::get_singleton()->set_clipboard(p_text); } + String _OS::get_clipboard() const { return OS::get_singleton()->get_clipboard(); @@ -263,12 +280,14 @@ void _OS::set_video_mode(const Size2 &p_size, bool p_fullscreen, bool p_resizeab vm.resizable = p_resizeable; OS::get_singleton()->set_video_mode(vm, p_screen); } + Size2 _OS::get_video_mode(int p_screen) const { OS::VideoMode vm; vm = OS::get_singleton()->get_video_mode(p_screen); return Size2(vm.width, vm.height); } + bool _OS::is_video_mode_fullscreen(int p_screen) const { OS::VideoMode vm; @@ -462,15 +481,18 @@ Error _OS::shell_open(String p_uri) { int _OS::execute(const String &p_path, const Vector<String> &p_arguments, bool p_blocking, Array p_output, bool p_read_stderr) { OS::ProcessID pid = -2; + int exitcode = 0; List<String> args; for (int i = 0; i < p_arguments.size(); i++) args.push_back(p_arguments[i]); String pipe; - Error err = OS::get_singleton()->execute(p_path, args, p_blocking, &pid, &pipe, NULL, p_read_stderr); + Error err = OS::get_singleton()->execute(p_path, args, p_blocking, &pid, &pipe, &exitcode, p_read_stderr); p_output.clear(); p_output.push_back(pipe); if (err != OK) return -1; + else if (p_blocking) + return exitcode; else return pid; } @@ -727,22 +749,16 @@ int64_t _OS::get_unix_time_from_datetime(Dictionary datetime) const { { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 } }; - ERR_EXPLAIN("Invalid second value of: " + itos(second)); - ERR_FAIL_COND_V(second > 59, 0); + ERR_FAIL_COND_V_MSG(second > 59, 0, "Invalid second value of: " + itos(second) + "."); - ERR_EXPLAIN("Invalid minute value of: " + itos(minute)); - ERR_FAIL_COND_V(minute > 59, 0); + ERR_FAIL_COND_V_MSG(minute > 59, 0, "Invalid minute value of: " + itos(minute) + "."); - ERR_EXPLAIN("Invalid hour value of: " + itos(hour)); - ERR_FAIL_COND_V(hour > 23, 0); + ERR_FAIL_COND_V_MSG(hour > 23, 0, "Invalid hour value of: " + itos(hour) + "."); - ERR_EXPLAIN("Invalid month value of: " + itos(month)); - ERR_FAIL_COND_V(month > 12 || month == 0, 0); + ERR_FAIL_COND_V_MSG(month > 12 || month == 0, 0, "Invalid month value of: " + itos(month) + "."); // Do this check after month is tested as valid - ERR_EXPLAIN("Invalid day value of: " + itos(day) + " which is larger than " + itos(MONTH_DAYS_TABLE[LEAPYEAR(year)][month - 1]) + " or 0"); - ERR_FAIL_COND_V(day > MONTH_DAYS_TABLE[LEAPYEAR(year)][month - 1] || day == 0, 0); - + ERR_FAIL_COND_V_MSG(day > MONTH_DAYS_TABLE[LEAPYEAR(year)][month - 1] || day == 0, 0, "Invalid day value of '" + itos(day) + "' which is larger than '" + itos(MONTH_DAYS_TABLE[LEAPYEAR(year)][month - 1]) + "' or 0."); // Calculate all the seconds from months past in this year uint64_t SECONDS_FROM_MONTHS_PAST_THIS_YEAR = DAYS_PAST_THIS_YEAR_TABLE[LEAPYEAR(year)][month - 1] * SECONDS_PER_DAY; @@ -1137,6 +1153,11 @@ void _OS::_bind_methods() { //ClassDB::bind_method(D_METHOD("is_video_mode_resizable","screen"),&_OS::is_video_mode_resizable,DEFVAL(0)); //ClassDB::bind_method(D_METHOD("get_fullscreen_mode_list","screen"),&_OS::get_fullscreen_mode_list,DEFVAL(0)); + ClassDB::bind_method(D_METHOD("global_menu_add_item", "menu", "label", "id", "meta"), &_OS::global_menu_add_item); + ClassDB::bind_method(D_METHOD("global_menu_add_separator", "menu"), &_OS::global_menu_add_separator); + ClassDB::bind_method(D_METHOD("global_menu_remove_item", "menu", "idx"), &_OS::global_menu_remove_item); + ClassDB::bind_method(D_METHOD("global_menu_clear", "menu"), &_OS::global_menu_clear); + ClassDB::bind_method(D_METHOD("get_video_driver_count"), &_OS::get_video_driver_count); ClassDB::bind_method(D_METHOD("get_video_driver_name", "driver"), &_OS::get_video_driver_name); ClassDB::bind_method(D_METHOD("get_current_video_driver"), &_OS::get_current_video_driver); @@ -1414,6 +1435,11 @@ PoolVector<Plane> _Geometry::build_capsule_planes(float p_radius, float p_height return Geometry::build_capsule_planes(p_radius, p_height, p_sides, p_lats, p_axis); } +bool _Geometry::is_point_in_circle(const Vector2 &p_point, const Vector2 &p_circle_pos, real_t p_circle_radius) { + + return Geometry::is_point_in_circle(p_point, p_circle_pos, p_circle_radius); +} + real_t _Geometry::segment_intersects_circle(const Vector2 &p_from, const Vector2 &p_to, const Vector2 &p_circle_pos, real_t p_circle_radius) { return Geometry::segment_intersects_circle(p_from, p_to, p_circle_pos, p_circle_radius); @@ -1666,11 +1692,6 @@ Array _Geometry::offset_polyline_2d(const Vector<Vector2> &p_polygon, real_t p_d return ret; } -Vector<Point2> _Geometry::transform_points_2d(const Vector<Point2> &p_points, const Transform2D &p_mat) { - - return Geometry::transform_points_2d(p_points, p_mat); -} - Dictionary _Geometry::make_atlas(const Vector<Size2> &p_rects) { Dictionary ret; @@ -1709,6 +1730,7 @@ void _Geometry::_bind_methods() { ClassDB::bind_method(D_METHOD("build_box_planes", "extents"), &_Geometry::build_box_planes); ClassDB::bind_method(D_METHOD("build_cylinder_planes", "radius", "height", "sides", "axis"), &_Geometry::build_cylinder_planes, DEFVAL(Vector3::AXIS_Z)); ClassDB::bind_method(D_METHOD("build_capsule_planes", "radius", "height", "sides", "lats", "axis"), &_Geometry::build_capsule_planes, DEFVAL(Vector3::AXIS_Z)); + ClassDB::bind_method(D_METHOD("is_point_in_circle", "point", "circle_position", "circle_radius"), &_Geometry::is_point_in_circle); ClassDB::bind_method(D_METHOD("segment_intersects_circle", "segment_from", "segment_to", "circle_position", "circle_radius"), &_Geometry::segment_intersects_circle); ClassDB::bind_method(D_METHOD("segment_intersects_segment_2d", "from_a", "to_a", "from_b", "to_b"), &_Geometry::segment_intersects_segment_2d); ClassDB::bind_method(D_METHOD("line_intersects_line_2d", "from_a", "dir_a", "from_b", "dir_b"), &_Geometry::line_intersects_line_2d); @@ -1749,8 +1771,6 @@ void _Geometry::_bind_methods() { ClassDB::bind_method(D_METHOD("offset_polygon_2d", "polygon", "delta", "join_type"), &_Geometry::offset_polygon_2d, DEFVAL(JOIN_SQUARE)); ClassDB::bind_method(D_METHOD("offset_polyline_2d", "polyline", "delta", "join_type", "end_type"), &_Geometry::offset_polyline_2d, DEFVAL(JOIN_SQUARE), DEFVAL(END_SQUARE)); - ClassDB::bind_method(D_METHOD("transform_points_2d", "points", "transform"), &_Geometry::transform_points_2d); - ClassDB::bind_method(D_METHOD("make_atlas", "sizes"), &_Geometry::make_atlas); BIND_ENUM_CONSTANT(OPERATION_UNION); @@ -1849,92 +1869,92 @@ bool _File::is_open() const { } String _File::get_path() const { - ERR_FAIL_COND_V(!f, ""); + ERR_FAIL_COND_V_MSG(!f, "", "File must be opened before use."); return f->get_path(); } String _File::get_path_absolute() const { - ERR_FAIL_COND_V(!f, ""); + ERR_FAIL_COND_V_MSG(!f, "", "File must be opened before use."); return f->get_path_absolute(); } void _File::seek(int64_t p_position) { - ERR_FAIL_COND(!f); + ERR_FAIL_COND_MSG(!f, "File must be opened before use."); f->seek(p_position); } void _File::seek_end(int64_t p_position) { - ERR_FAIL_COND(!f); + ERR_FAIL_COND_MSG(!f, "File must be opened before use."); f->seek_end(p_position); } int64_t _File::get_position() const { - ERR_FAIL_COND_V(!f, 0); + ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use."); return f->get_position(); } int64_t _File::get_len() const { - ERR_FAIL_COND_V(!f, 0); + ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use."); return f->get_len(); } bool _File::eof_reached() const { - ERR_FAIL_COND_V(!f, false); + ERR_FAIL_COND_V_MSG(!f, false, "File must be opened before use."); return f->eof_reached(); } uint8_t _File::get_8() const { - ERR_FAIL_COND_V(!f, 0); + ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use."); return f->get_8(); } uint16_t _File::get_16() const { - ERR_FAIL_COND_V(!f, 0); + ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use."); return f->get_16(); } uint32_t _File::get_32() const { - ERR_FAIL_COND_V(!f, 0); + ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use."); return f->get_32(); } uint64_t _File::get_64() const { - ERR_FAIL_COND_V(!f, 0); + ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use."); return f->get_64(); } float _File::get_float() const { - ERR_FAIL_COND_V(!f, 0); + ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use."); return f->get_float(); } double _File::get_double() const { - ERR_FAIL_COND_V(!f, 0); + ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use."); return f->get_double(); } real_t _File::get_real() const { - ERR_FAIL_COND_V(!f, 0); + ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use."); return f->get_real(); } PoolVector<uint8_t> _File::get_buffer(int p_length) const { PoolVector<uint8_t> data; - ERR_FAIL_COND_V(!f, data); + ERR_FAIL_COND_V_MSG(!f, data, "File must be opened before use."); - ERR_FAIL_COND_V(p_length < 0, data); + ERR_FAIL_COND_V_MSG(p_length < 0, data, "Length of buffer cannot be smaller than 0."); if (p_length == 0) return data; Error err = data.resize(p_length); - ERR_FAIL_COND_V(err != OK, data); + ERR_FAIL_COND_V_MSG(err != OK, data, "Can't resize data to " + itos(p_length) + " elements."); PoolVector<uint8_t>::Write w = data.write(); int len = f->get_buffer(&w[0], p_length); @@ -1950,7 +1970,7 @@ PoolVector<uint8_t> _File::get_buffer(int p_length) const { String _File::get_as_text() const { - ERR_FAIL_COND_V(!f, String()); + ERR_FAIL_COND_V_MSG(!f, String(), "File must be opened before use."); String text; size_t original_pos = f->get_position(); @@ -1980,12 +2000,12 @@ String _File::get_sha256(const String &p_path) const { String _File::get_line() const { - ERR_FAIL_COND_V(!f, String()); + ERR_FAIL_COND_V_MSG(!f, String(), "File must be opened before use."); return f->get_line(); } Vector<String> _File::get_csv_line(const String &p_delim) const { - ERR_FAIL_COND_V(!f, Vector<String>()); + ERR_FAIL_COND_V_MSG(!f, Vector<String>(), "File must be opened before use."); return f->get_csv_line(p_delim); } @@ -2014,83 +2034,83 @@ Error _File::get_error() const { void _File::store_8(uint8_t p_dest) { - ERR_FAIL_COND(!f); + ERR_FAIL_COND_MSG(!f, "File must be opened before use."); f->store_8(p_dest); } void _File::store_16(uint16_t p_dest) { - ERR_FAIL_COND(!f); + ERR_FAIL_COND_MSG(!f, "File must be opened before use."); f->store_16(p_dest); } void _File::store_32(uint32_t p_dest) { - ERR_FAIL_COND(!f); + ERR_FAIL_COND_MSG(!f, "File must be opened before use."); f->store_32(p_dest); } void _File::store_64(uint64_t p_dest) { - ERR_FAIL_COND(!f); + ERR_FAIL_COND_MSG(!f, "File must be opened before use."); f->store_64(p_dest); } void _File::store_float(float p_dest) { - ERR_FAIL_COND(!f); + ERR_FAIL_COND_MSG(!f, "File must be opened before use."); f->store_float(p_dest); } void _File::store_double(double p_dest) { - ERR_FAIL_COND(!f); + ERR_FAIL_COND_MSG(!f, "File must be opened before use."); f->store_double(p_dest); } void _File::store_real(real_t p_real) { - ERR_FAIL_COND(!f); + ERR_FAIL_COND_MSG(!f, "File must be opened before use."); f->store_real(p_real); } void _File::store_string(const String &p_string) { - ERR_FAIL_COND(!f); + ERR_FAIL_COND_MSG(!f, "File must be opened before use."); f->store_string(p_string); } void _File::store_pascal_string(const String &p_string) { - ERR_FAIL_COND(!f); + ERR_FAIL_COND_MSG(!f, "File must be opened before use."); f->store_pascal_string(p_string); }; String _File::get_pascal_string() { - ERR_FAIL_COND_V(!f, ""); + ERR_FAIL_COND_V_MSG(!f, "", "File must be opened before use."); return f->get_pascal_string(); }; void _File::store_line(const String &p_string) { - ERR_FAIL_COND(!f); + ERR_FAIL_COND_MSG(!f, "File must be opened before use."); f->store_line(p_string); } void _File::store_csv_line(const Vector<String> &p_values, const String &p_delim) { - ERR_FAIL_COND(!f); + ERR_FAIL_COND_MSG(!f, "File must be opened before use."); f->store_csv_line(p_values, p_delim); } void _File::store_buffer(const PoolVector<uint8_t> &p_buffer) { - ERR_FAIL_COND(!f); + ERR_FAIL_COND_MSG(!f, "File must be opened before use."); int len = p_buffer.size(); if (len == 0) @@ -2108,17 +2128,17 @@ bool _File::file_exists(const String &p_name) const { void _File::store_var(const Variant &p_var, bool p_full_objects) { - ERR_FAIL_COND(!f); + ERR_FAIL_COND_MSG(!f, "File must be opened before use."); int len; Error err = encode_variant(p_var, NULL, len, p_full_objects); - ERR_FAIL_COND(err != OK); + ERR_FAIL_COND_MSG(err != OK, "Error when trying to encode Variant."); PoolVector<uint8_t> buff; buff.resize(len); PoolVector<uint8_t>::Write w = buff.write(); err = encode_variant(p_var, &w[0], len, p_full_objects); - ERR_FAIL_COND(err != OK); + ERR_FAIL_COND_MSG(err != OK, "Error when trying to encode Variant."); w.release(); store_32(len); @@ -2127,7 +2147,7 @@ void _File::store_var(const Variant &p_var, bool p_full_objects) { Variant _File::get_var(bool p_allow_objects) const { - ERR_FAIL_COND_V(!f, Variant()); + ERR_FAIL_COND_V_MSG(!f, Variant(), "File must be opened before use."); uint32_t len = get_32(); PoolVector<uint8_t> buff = get_buffer(len); ERR_FAIL_COND_V((uint32_t)buff.size() != len, Variant()); @@ -2136,7 +2156,7 @@ Variant _File::get_var(bool p_allow_objects) const { Variant v; Error err = decode_variant(v, &r[0], len, NULL, p_allow_objects); - ERR_FAIL_COND_V(err != OK, Variant()); + ERR_FAIL_COND_V_MSG(err != OK, Variant(), "Error when trying to encode Variant."); return v; } @@ -2241,7 +2261,7 @@ Error _Directory::open(const String &p_path) { Error _Directory::list_dir_begin(bool p_skip_navigational, bool p_skip_hidden) { - ERR_FAIL_COND_V(!d, ERR_UNCONFIGURED); + ERR_FAIL_COND_V_MSG(!d, ERR_UNCONFIGURED, "Directory must be opened before use."); _list_skip_navigational = p_skip_navigational; _list_skip_hidden = p_skip_hidden; @@ -2251,7 +2271,7 @@ Error _Directory::list_dir_begin(bool p_skip_navigational, bool p_skip_hidden) { String _Directory::get_next() { - ERR_FAIL_COND_V(!d, ""); + ERR_FAIL_COND_V_MSG(!d, "", "Directory must be opened before use."); String next = d->get_next(); while (next != "" && ((_list_skip_navigational && (next == "." || next == "..")) || (_list_skip_hidden && d->current_is_hidden()))) { @@ -2262,44 +2282,44 @@ String _Directory::get_next() { } bool _Directory::current_is_dir() const { - ERR_FAIL_COND_V(!d, false); + ERR_FAIL_COND_V_MSG(!d, false, "Directory must be opened before use."); return d->current_is_dir(); } void _Directory::list_dir_end() { - ERR_FAIL_COND(!d); + ERR_FAIL_COND_MSG(!d, "Directory must be opened before use."); d->list_dir_end(); } int _Directory::get_drive_count() { - ERR_FAIL_COND_V(!d, 0); + ERR_FAIL_COND_V_MSG(!d, 0, "Directory must be opened before use."); return d->get_drive_count(); } String _Directory::get_drive(int p_drive) { - ERR_FAIL_COND_V(!d, ""); + ERR_FAIL_COND_V_MSG(!d, "", "Directory must be opened before use."); return d->get_drive(p_drive); } int _Directory::get_current_drive() { - ERR_FAIL_COND_V(!d, 0); + ERR_FAIL_COND_V_MSG(!d, 0, "Directory must be opened before use."); return d->get_current_drive(); } Error _Directory::change_dir(String p_dir) { - ERR_FAIL_COND_V(!d, ERR_UNCONFIGURED); + ERR_FAIL_COND_V_MSG(!d, ERR_UNCONFIGURED, "Directory must be opened before use."); return d->change_dir(p_dir); } String _Directory::get_current_dir() { - ERR_FAIL_COND_V(!d, ""); + ERR_FAIL_COND_V_MSG(!d, "", "Directory must be opened before use."); return d->get_current_dir(); } Error _Directory::make_dir(String p_dir) { - ERR_FAIL_COND_V(!d, ERR_UNCONFIGURED); + ERR_FAIL_COND_V_MSG(!d, ERR_UNCONFIGURED, "Directory must be opened before use."); if (!p_dir.is_rel_path()) { DirAccess *d = DirAccess::create_for_path(p_dir); Error err = d->make_dir(p_dir); @@ -2310,7 +2330,7 @@ Error _Directory::make_dir(String p_dir) { } Error _Directory::make_dir_recursive(String p_dir) { - ERR_FAIL_COND_V(!d, ERR_UNCONFIGURED); + ERR_FAIL_COND_V_MSG(!d, ERR_UNCONFIGURED, "Directory must be opened before use."); if (!p_dir.is_rel_path()) { DirAccess *d = DirAccess::create_for_path(p_dir); Error err = d->make_dir_recursive(p_dir); @@ -2322,7 +2342,7 @@ Error _Directory::make_dir_recursive(String p_dir) { bool _Directory::file_exists(String p_file) { - ERR_FAIL_COND_V(!d, false); + ERR_FAIL_COND_V_MSG(!d, false, "Directory must be opened before use."); if (!p_file.is_rel_path()) { return FileAccess::exists(p_file); @@ -2332,7 +2352,7 @@ bool _Directory::file_exists(String p_file) { } bool _Directory::dir_exists(String p_dir) { - ERR_FAIL_COND_V(!d, false); + ERR_FAIL_COND_V_MSG(!d, false, "Directory must be opened before use."); if (!p_dir.is_rel_path()) { DirAccess *d = DirAccess::create_for_path(p_dir); @@ -2347,18 +2367,18 @@ bool _Directory::dir_exists(String p_dir) { int _Directory::get_space_left() { - ERR_FAIL_COND_V(!d, 0); + ERR_FAIL_COND_V_MSG(!d, 0, "Directory must be opened before use."); return d->get_space_left() / 1024 * 1024; //return value in megabytes, given binding is int } Error _Directory::copy(String p_from, String p_to) { - ERR_FAIL_COND_V(!d, ERR_UNCONFIGURED); + ERR_FAIL_COND_V_MSG(!d, ERR_UNCONFIGURED, "Directory must be opened before use."); return d->copy(p_from, p_to); } Error _Directory::rename(String p_from, String p_to) { - ERR_FAIL_COND_V(!d, ERR_UNCONFIGURED); + ERR_FAIL_COND_V_MSG(!d, ERR_UNCONFIGURED, "Directory must be opened before use."); if (!p_from.is_rel_path()) { DirAccess *d = DirAccess::create_for_path(p_from); Error err = d->rename(p_from, p_to); @@ -2370,7 +2390,7 @@ Error _Directory::rename(String p_from, String p_to) { } Error _Directory::remove(String p_name) { - ERR_FAIL_COND_V(!d, ERR_UNCONFIGURED); + ERR_FAIL_COND_V_MSG(!d, ERR_UNCONFIGURED, "Directory must be opened before use."); if (!p_name.is_rel_path()) { DirAccess *d = DirAccess::create_for_path(p_name); Error err = d->remove(p_name); @@ -2425,14 +2445,14 @@ String _Marshalls::variant_to_base64(const Variant &p_var, bool p_full_objects) int len; Error err = encode_variant(p_var, NULL, len, p_full_objects); - ERR_FAIL_COND_V(err != OK, ""); + ERR_FAIL_COND_V_MSG(err != OK, "", "Error when trying to encode Variant."); PoolVector<uint8_t> buff; buff.resize(len); PoolVector<uint8_t>::Write w = buff.write(); err = encode_variant(p_var, &w[0], len, p_full_objects); - ERR_FAIL_COND_V(err != OK, ""); + ERR_FAIL_COND_V_MSG(err != OK, "", "Error when trying to encode Variant."); String ret = CryptoCore::b64_encode_str(&w[0], len); ERR_FAIL_COND_V(ret == "", ret); @@ -2454,7 +2474,7 @@ Variant _Marshalls::base64_to_variant(const String &p_str, bool p_allow_objects) Variant v; Error err = decode_variant(v, &w[0], len, NULL, p_allow_objects); - ERR_FAIL_COND_V(err != OK, Variant()); + ERR_FAIL_COND_V_MSG(err != OK, Variant(), "Error when trying to decode Variant."); return v; }; @@ -2621,14 +2641,13 @@ void _Thread::_start_func(void *ud) { } } - ERR_EXPLAIN("Could not call function '" + t->target_method.operator String() + "'' starting thread ID: " + t->get_id() + " Reason: " + reason); - ERR_FAIL(); + ERR_FAIL_MSG("Could not call function '" + t->target_method.operator String() + "' to start thread " + t->get_id() + ": " + reason + "."); } } Error _Thread::start(Object *p_instance, const StringName &p_method, const Variant &p_userdata, Priority p_priority) { - ERR_FAIL_COND_V(active, ERR_ALREADY_IN_USE); + ERR_FAIL_COND_V_MSG(active, ERR_ALREADY_IN_USE, "Thread already started."); ERR_FAIL_COND_V(!p_instance, ERR_INVALID_PARAMETER); ERR_FAIL_COND_V(p_method == StringName(), ERR_INVALID_PARAMETER); ERR_FAIL_INDEX_V(p_priority, PRIORITY_MAX, ERR_INVALID_PARAMETER); @@ -2669,8 +2688,8 @@ bool _Thread::is_active() const { } Variant _Thread::wait_to_finish() { - ERR_FAIL_COND_V(!thread, Variant()); - ERR_FAIL_COND_V(!active, Variant()); + ERR_FAIL_COND_V_MSG(!thread, Variant(), "Thread must exist to wait for its completion."); + ERR_FAIL_COND_V_MSG(!active, Variant(), "Thread must be active to wait for its completion."); Thread::wait_to_finish(thread); Variant r = ret; active = false; @@ -2704,10 +2723,7 @@ _Thread::_Thread() { _Thread::~_Thread() { - if (active) { - ERR_EXPLAIN("Reference to a Thread object object was lost while the thread is still running..."); - } - ERR_FAIL_COND(active); + ERR_FAIL_COND_MSG(active, "Reference to a Thread object object was lost while the thread is still running..."); } ///////////////////////////////////// diff --git a/core/bind/core_bind.h b/core/bind/core_bind.h index f0f86e003f..693b85710a 100644 --- a/core/bind/core_bind.h +++ b/core/bind/core_bind.h @@ -109,11 +109,11 @@ public: }; enum PowerState { - POWERSTATE_UNKNOWN, /**< cannot determine power status */ - POWERSTATE_ON_BATTERY, /**< Not plugged in, running on the battery */ - POWERSTATE_NO_BATTERY, /**< Plugged in, no battery available */ - POWERSTATE_CHARGING, /**< Plugged in, charging battery */ - POWERSTATE_CHARGED /**< Plugged in, battery charged */ + POWERSTATE_UNKNOWN, // Cannot determine power status. + POWERSTATE_ON_BATTERY, // Not plugged in, running on the battery. + POWERSTATE_NO_BATTERY, // Plugged in, no battery available. + POWERSTATE_CHARGING, // Plugged in, charging battery. + POWERSTATE_CHARGED // Plugged in, battery charged. }; enum Weekday { @@ -127,8 +127,8 @@ public: }; enum Month { - /// Start at 1 to follow Windows SYSTEMTIME structure - /// https://msdn.microsoft.com/en-us/library/windows/desktop/ms724950(v=vs.85).aspx + // Start at 1 to follow Windows SYSTEMTIME structure + // https://msdn.microsoft.com/en-us/library/windows/desktop/ms724950(v=vs.85).aspx MONTH_JANUARY = 1, MONTH_FEBRUARY, MONTH_MARCH, @@ -143,6 +143,11 @@ public: MONTH_DECEMBER }; + void global_menu_add_item(const String &p_menu, const String &p_label, const Variant &p_signal, const Variant &p_meta); + void global_menu_add_separator(const String &p_menu); + void global_menu_remove_item(const String &p_menu, int p_idx); + void global_menu_clear(const String &p_menu); + Point2 get_mouse_position() const; void set_window_title(const String &p_title); int get_mouse_button_state() const; @@ -259,24 +264,6 @@ public: bool is_scancode_unicode(uint32_t p_unicode) const; int find_scancode_from_string(const String &p_code) const; - /* - struct Date { - - int year; - Month month; - int day; - Weekday weekday; - bool dst; - }; - - struct Time { - - int hour; - int min; - int sec; - }; -*/ - void set_use_file_access_save_and_swap(bool p_enable); void set_native_icon(const String &p_filename); @@ -404,6 +391,7 @@ public: PoolVector<Vector3> segment_intersects_sphere(const Vector3 &p_from, const Vector3 &p_to, const Vector3 &p_sphere_pos, real_t p_sphere_radius); PoolVector<Vector3> segment_intersects_cylinder(const Vector3 &p_from, const Vector3 &p_to, float p_height, float p_radius); PoolVector<Vector3> segment_intersects_convex(const Vector3 &p_from, const Vector3 &p_to, const Vector<Plane> &p_planes); + bool is_point_in_circle(const Vector2 &p_point, const Vector2 &p_circle_pos, real_t p_circle_radius); real_t segment_intersects_circle(const Vector2 &p_from, const Vector2 &p_to, const Vector2 &p_circle_pos, real_t p_circle_radius); int get_uv84_normal_bit(const Vector3 &p_vector); @@ -420,17 +408,17 @@ public: OPERATION_INTERSECTION, OPERATION_XOR }; - // 2D polygon boolean operations - Array merge_polygons_2d(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b); // union (add) - Array clip_polygons_2d(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b); // difference (subtract) - Array intersect_polygons_2d(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b); // common area (multiply) - Array exclude_polygons_2d(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b); // all but common area (xor) + // 2D polygon boolean operations. + Array merge_polygons_2d(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b); // Union (add). + Array clip_polygons_2d(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b); // Difference (subtract). + Array intersect_polygons_2d(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b); // Common area (multiply). + Array exclude_polygons_2d(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b); // All but common area (xor). - // 2D polyline vs polygon operations - Array clip_polyline_with_polygon_2d(const Vector<Vector2> &p_polyline, const Vector<Vector2> &p_polygon); // cut - Array intersect_polyline_with_polygon_2d(const Vector<Vector2> &p_polyline, const Vector<Vector2> &p_polygon); // chop + // 2D polyline vs polygon operations. + Array clip_polyline_with_polygon_2d(const Vector<Vector2> &p_polyline, const Vector<Vector2> &p_polygon); // Cut. + Array intersect_polyline_with_polygon_2d(const Vector<Vector2> &p_polyline, const Vector<Vector2> &p_polygon); // Chop. - // 2D offset polygons/polylines + // 2D offset polygons/polylines. enum PolyJoinType { JOIN_SQUARE, JOIN_ROUND, @@ -446,8 +434,6 @@ public: Array offset_polygon_2d(const Vector<Vector2> &p_polygon, real_t p_delta, PolyJoinType p_join_type = JOIN_SQUARE); Array offset_polyline_2d(const Vector<Vector2> &p_polygon, real_t p_delta, PolyJoinType p_join_type = JOIN_SQUARE, PolyEndType p_end_type = END_SQUARE); - Vector<Point2> transform_points_2d(const Vector<Point2> &p_points, const Transform2D &p_mat); - Dictionary make_atlas(const Vector<Size2> &p_rects); _Geometry(); @@ -486,24 +472,24 @@ public: Error open_encrypted_pass(const String &p_path, ModeFlags p_mode_flags, const String &p_pass); Error open_compressed(const String &p_path, ModeFlags p_mode_flags, CompressionMode p_compress_mode = COMPRESSION_FASTLZ); - Error open(const String &p_path, ModeFlags p_mode_flags); ///< open a file - void close(); ///< close a file - bool is_open() const; ///< true when file is open + Error open(const String &p_path, ModeFlags p_mode_flags); // open a file. + void close(); // Close a file. + bool is_open() const; // True when file is open. - String get_path() const; /// returns the path for the current open file - String get_path_absolute() const; /// returns the absolute path for the current open file + String get_path() const; // Returns the path for the current open file. + String get_path_absolute() const; // Returns the absolute path for the current open file. - void seek(int64_t p_position); ///< seek to a given position - void seek_end(int64_t p_position = 0); ///< seek from the end of file - int64_t get_position() const; ///< get position in the file - int64_t get_len() const; ///< get size of the file + void seek(int64_t p_position); // Seek to a given position. + void seek_end(int64_t p_position = 0); // Seek from the end of file. + int64_t get_position() const; // Get position in the file. + int64_t get_len() const; // Get size of the file. - bool eof_reached() const; ///< reading passed EOF + bool eof_reached() const; // Reading passed EOF. - uint8_t get_8() const; ///< get a byte - uint16_t get_16() const; ///< get 16 bits uint - uint32_t get_32() const; ///< get 32 bits uint - uint64_t get_64() const; ///< get 64 bits uint + uint8_t get_8() const; // Get a byte. + uint16_t get_16() const; // Get 16 bits uint. + uint32_t get_32() const; // Get 32 bits uint. + uint64_t get_64() const; // Get 64 bits uint. float get_float() const; double get_double() const; @@ -511,27 +497,27 @@ public: Variant get_var(bool p_allow_objects = false) const; - PoolVector<uint8_t> get_buffer(int p_length) const; ///< get an array of bytes + PoolVector<uint8_t> get_buffer(int p_length) const; // Get an array of bytes. String get_line() const; Vector<String> get_csv_line(const String &p_delim = ",") const; String get_as_text() const; String get_md5(const String &p_path) const; String get_sha256(const String &p_path) const; - /**< use this for files WRITTEN in _big_ endian machines (ie, amiga/mac) + /* Use this for files WRITTEN in _big_ endian machines (ie, amiga/mac). * It's not about the current CPU type but file formats. - * this flags get reset to false (little endian) on each open + * This flags get reset to false (little endian) on each open. */ void set_endian_swap(bool p_swap); bool get_endian_swap(); - Error get_error() const; ///< get last error + Error get_error() const; // Get last error. - void store_8(uint8_t p_dest); ///< store a byte - void store_16(uint16_t p_dest); ///< store 16 bits uint - void store_32(uint32_t p_dest); ///< store 32 bits uint - void store_64(uint64_t p_dest); ///< store 64 bits uint + void store_8(uint8_t p_dest); // Store a byte. + void store_16(uint16_t p_dest); // Store 16 bits uint. + void store_32(uint32_t p_dest); // Store 32 bits uint. + void store_64(uint64_t p_dest); // Store 64 bits uint. void store_float(float p_dest); void store_double(double p_dest); @@ -544,11 +530,11 @@ public: virtual void store_pascal_string(const String &p_string); virtual String get_pascal_string(); - void store_buffer(const PoolVector<uint8_t> &p_buffer); ///< store an array of bytes + void store_buffer(const PoolVector<uint8_t> &p_buffer); // Store an array of bytes. void store_var(const Variant &p_var, bool p_full_objects = false); - bool file_exists(const String &p_name) const; ///< return true if a file exists + bool file_exists(const String &p_name) const; // Return true if a file exists. uint64_t get_modified_time(const String &p_file) const; @@ -570,18 +556,18 @@ protected: public: Error open(const String &p_path); - Error list_dir_begin(bool p_skip_navigational = false, bool p_skip_hidden = false); ///< This starts dir listing + Error list_dir_begin(bool p_skip_navigational = false, bool p_skip_hidden = false); // This starts dir listing. String get_next(); bool current_is_dir() const; - void list_dir_end(); ///< + void list_dir_end(); int get_drive_count(); String get_drive(int p_drive); int get_current_drive(); - Error change_dir(String p_dir); ///< can be relative or absolute, return false on success - String get_current_dir(); ///< return current dir location + Error change_dir(String p_dir); // Can be relative or absolute, return false on success. + String get_current_dir(); // Return current dir location. Error make_dir(String p_dir); Error make_dir_recursive(String p_dir); diff --git a/core/class_db.cpp b/core/class_db.cpp index 49e3f94d8f..f52937bdca 100644 --- a/core/class_db.cpp +++ b/core/class_db.cpp @@ -340,7 +340,7 @@ StringName ClassDB::get_parent_class(const StringName &p_class) { OBJTYPE_RLOCK; ClassInfo *ti = classes.getptr(p_class); - ERR_FAIL_COND_V(!ti, StringName()); + ERR_FAIL_COND_V_MSG(!ti, StringName(), "Cannot get class '" + String(p_class) + "'."); return ti->inherits; } @@ -350,7 +350,7 @@ ClassDB::APIType ClassDB::get_api_type(const StringName &p_class) { ClassInfo *ti = classes.getptr(p_class); - ERR_FAIL_COND_V(!ti, API_NONE); + ERR_FAIL_COND_V_MSG(!ti, API_NONE, "Cannot get class '" + String(p_class) + "'."); return ti->api; } @@ -375,7 +375,7 @@ uint64_t ClassDB::get_api_hash(APIType p_api) { for (List<StringName>::Element *E = names.front(); E; E = E->next()) { ClassInfo *t = classes.getptr(E->get()); - ERR_FAIL_COND_V(!t, 0); + ERR_FAIL_COND_V_MSG(!t, 0, "Cannot get class '" + String(E->get()) + "'."); if (t->api != p_api || !t->exposed) continue; hash = hash_djb2_one_64(t->name.hash(), hash); @@ -528,8 +528,8 @@ Object *ClassDB::instance(const StringName &p_class) { ti = classes.getptr(compat_classes[p_class]); } } - ERR_FAIL_COND_V(!ti, NULL); - ERR_FAIL_COND_V(ti->disabled, NULL); + ERR_FAIL_COND_V_MSG(!ti, NULL, "Cannot get class '" + String(p_class) + "'."); + ERR_FAIL_COND_V_MSG(ti->disabled, NULL, "Class '" + String(p_class) + "' is disabled."); ERR_FAIL_COND_V(!ti->creation_func, NULL); } #ifdef TOOLS_ENABLED @@ -545,7 +545,7 @@ bool ClassDB::can_instance(const StringName &p_class) { OBJTYPE_RLOCK; ClassInfo *ti = classes.getptr(p_class); - ERR_FAIL_COND_V(!ti, false); + ERR_FAIL_COND_V_MSG(!ti, false, "Cannot get class '" + String(p_class) + "'."); #ifdef TOOLS_ENABLED if (ti->api == API_EDITOR && !Engine::get_singleton()->is_editor_hint()) { return false; @@ -560,7 +560,7 @@ void ClassDB::_add_class2(const StringName &p_class, const StringName &p_inherit const StringName &name = p_class; - ERR_FAIL_COND(classes.has(name)); + ERR_FAIL_COND_MSG(classes.has(name), "Class '" + String(p_class) + "' already exists."); classes[name] = ClassInfo(); ClassInfo &ti = classes[name]; @@ -836,10 +836,7 @@ void ClassDB::add_signal(StringName p_class, const MethodInfo &p_signal) { #ifdef DEBUG_METHODS_ENABLED ClassInfo *check = type; while (check) { - if (check->signal_map.has(sname)) { - ERR_EXPLAIN("Type " + String(p_class) + " already has signal: " + String(sname)); - ERR_FAIL(); - } + ERR_FAIL_COND_MSG(check->signal_map.has(sname), "Class '" + String(p_class) + "' already has signal '" + String(sname) + "'."); check = check->inherits_ptr; } #endif @@ -924,16 +921,11 @@ void ClassDB::add_property(StringName p_class, const PropertyInfo &p_pinfo, cons if (p_setter) { mb_set = get_method(p_class, p_setter); #ifdef DEBUG_METHODS_ENABLED - if (!mb_set) { - ERR_EXPLAIN("Invalid Setter: " + p_class + "::" + p_setter + " for property: " + p_pinfo.name); - ERR_FAIL(); - } else { - int exp_args = 1 + (p_index >= 0 ? 1 : 0); - if (mb_set->get_argument_count() != exp_args) { - ERR_EXPLAIN("Invalid Function for Setter: " + p_class + "::" + p_setter + " for property: " + p_pinfo.name); - ERR_FAIL(); - } - } + + ERR_FAIL_COND_MSG(!mb_set, "Invalid setter '" + p_class + "::" + p_setter + "' for property '" + p_pinfo.name + "'."); + + int exp_args = 1 + (p_index >= 0 ? 1 : 0); + ERR_FAIL_COND_MSG(mb_set->get_argument_count() != exp_args, "Invalid function for setter '" + p_class + "::" + p_setter + " for property '" + p_pinfo.name + "'."); #endif } @@ -943,25 +935,15 @@ void ClassDB::add_property(StringName p_class, const PropertyInfo &p_pinfo, cons mb_get = get_method(p_class, p_getter); #ifdef DEBUG_METHODS_ENABLED - if (!mb_get) { - ERR_EXPLAIN("Invalid Getter: " + p_class + "::" + p_getter + " for property: " + p_pinfo.name); - ERR_FAIL(); - } else { + ERR_FAIL_COND_MSG(!mb_get, "Invalid getter '" + p_class + "::" + p_getter + "' for property '" + p_pinfo.name + "'."); - int exp_args = 0 + (p_index >= 0 ? 1 : 0); - if (mb_get->get_argument_count() != exp_args) { - ERR_EXPLAIN("Invalid Function for Getter: " + p_class + "::" + p_getter + " for property: " + p_pinfo.name); - ERR_FAIL(); - } - } + int exp_args = 0 + (p_index >= 0 ? 1 : 0); + ERR_FAIL_COND_MSG(mb_get->get_argument_count() != exp_args, "Invalid function for getter '" + p_class + "::" + p_getter + "' for property: '" + p_pinfo.name + "'."); #endif } #ifdef DEBUG_METHODS_ENABLED - if (type->property_setget.has(p_pinfo.name)) { - ERR_EXPLAIN("Object " + p_class + " already has property: " + p_pinfo.name); - ERR_FAIL(); - } + ERR_FAIL_COND_MSG(type->property_setget.has(p_pinfo.name), "Object '" + p_class + "' already has property '" + p_pinfo.name + "'."); #endif OBJTYPE_WLOCK @@ -1240,32 +1222,26 @@ MethodBind *ClassDB::bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const c #ifdef DEBUG_ENABLED - if (has_method(instance_type, mdname)) { - ERR_EXPLAIN("Class " + String(instance_type) + " already has a method " + String(mdname)); - ERR_FAIL_V(NULL); - } + ERR_FAIL_COND_V_MSG(has_method(instance_type, mdname), NULL, "Class " + String(instance_type) + " already has a method " + String(mdname) + "."); #endif ClassInfo *type = classes.getptr(instance_type); if (!type) { - ERR_PRINTS("Couldn't bind method '" + mdname + "' for instance: " + instance_type); memdelete(p_bind); - ERR_FAIL_V(NULL); + ERR_FAIL_V_MSG(NULL, "Couldn't bind method '" + mdname + "' for instance '" + instance_type + "'."); } if (type->method_map.has(mdname)) { memdelete(p_bind); // overloading not supported - ERR_EXPLAIN("Method already bound: " + instance_type + "::" + mdname); - ERR_FAIL_V(NULL); + ERR_FAIL_V_MSG(NULL, "Method already bound '" + instance_type + "::" + mdname + "'."); } #ifdef DEBUG_METHODS_ENABLED if (method_name.args.size() > p_bind->get_argument_count()) { memdelete(p_bind); - ERR_EXPLAIN("Method definition provides more arguments than the method actually has: " + instance_type + "::" + mdname); - ERR_FAIL_V(NULL); + ERR_FAIL_V_MSG(NULL, "Method definition provides more arguments than the method actually has '" + instance_type + "::" + mdname + "'."); } p_bind->set_argument_names(method_name.args); @@ -1289,7 +1265,7 @@ MethodBind *ClassDB::bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const c } void ClassDB::add_virtual_method(const StringName &p_class, const MethodInfo &p_method, bool p_virtual) { - ERR_FAIL_COND(!classes.has(p_class)); + ERR_FAIL_COND_MSG(!classes.has(p_class), "Request for nonexistent class '" + p_class + "'."); OBJTYPE_WLOCK; @@ -1304,7 +1280,7 @@ void ClassDB::add_virtual_method(const StringName &p_class, const MethodInfo &p_ void ClassDB::get_virtual_methods(const StringName &p_class, List<MethodInfo> *p_methods, bool p_no_inheritance) { - ERR_FAIL_COND(!classes.has(p_class)); + ERR_FAIL_COND_MSG(!classes.has(p_class), "Request for nonexistent class '" + p_class + "'."); #ifdef DEBUG_METHODS_ENABLED @@ -1328,7 +1304,7 @@ void ClassDB::set_class_enabled(StringName p_class, bool p_enable) { OBJTYPE_WLOCK; - ERR_FAIL_COND(!classes.has(p_class)); + ERR_FAIL_COND_MSG(!classes.has(p_class), "Request for nonexistent class '" + p_class + "'."); classes[p_class].disabled = !p_enable; } @@ -1343,7 +1319,7 @@ bool ClassDB::is_class_enabled(StringName p_class) { } } - ERR_FAIL_COND_V(!ti, false); + ERR_FAIL_COND_V_MSG(!ti, false, "Cannot get class '" + String(p_class) + "'."); return !ti->disabled; } @@ -1352,7 +1328,7 @@ bool ClassDB::is_class_exposed(StringName p_class) { OBJTYPE_RLOCK; ClassInfo *ti = classes.getptr(p_class); - ERR_FAIL_COND_V(!ti, false); + ERR_FAIL_COND_V_MSG(!ti, false, "Cannot get class '" + String(p_class) + "'."); return ti->exposed; } diff --git a/core/class_db.h b/core/class_db.h index 3d9a695f02..092469beb7 100644 --- a/core/class_db.h +++ b/core/class_db.h @@ -35,10 +35,6 @@ #include "core/object.h" #include "core/print_string.h" -/** - @author Juan Linietsky <reduzio@gmail.com> -*/ - /** To bind more then 6 parameters include this: * #include "core/method_bind_ext.gen.inc" */ @@ -310,8 +306,7 @@ public: if (type->method_map.has(p_name)) { memdelete(bind); // overloading not supported - ERR_EXPLAIN("Method already bound: " + instance_type + "::" + p_name); - ERR_FAIL_V(NULL); + ERR_FAIL_V_MSG(NULL, "Method already bound: " + instance_type + "::" + p_name + "."); } type->method_map[p_name] = bind; #ifdef DEBUG_METHODS_ENABLED diff --git a/core/color.cpp b/core/color.cpp index 1843532124..a54a3115cc 100644 --- a/core/color.cpp +++ b/core/color.cpp @@ -335,36 +335,23 @@ Color Color::html(const String &p_color) { } else if (color.length() == 6) { alpha = false; } else { - ERR_EXPLAIN("Invalid Color Code: " + p_color); - ERR_FAIL_V(Color()); + ERR_FAIL_V_MSG(Color(), "Invalid color code: " + p_color + "."); } int a = 255; if (alpha) { a = _parse_col(color, 0); - if (a < 0) { - ERR_EXPLAIN("Invalid Color Code: " + p_color); - ERR_FAIL_V(Color()); - } + ERR_FAIL_COND_V_MSG(a < 0, Color(), "Invalid color code: " + p_color + "."); } int from = alpha ? 2 : 0; int r = _parse_col(color, from + 0); - if (r < 0) { - ERR_EXPLAIN("Invalid Color Code: " + p_color); - ERR_FAIL_V(Color()); - } + ERR_FAIL_COND_V_MSG(r < 0, Color(), "Invalid color code: " + p_color + "."); int g = _parse_col(color, from + 2); - if (g < 0) { - ERR_EXPLAIN("Invalid Color Code: " + p_color); - ERR_FAIL_V(Color()); - } + ERR_FAIL_COND_V_MSG(g < 0, Color(), "Invalid color code: " + p_color + "."); int b = _parse_col(color, from + 4); - if (b < 0) { - ERR_EXPLAIN("Invalid Color Code: " + p_color); - ERR_FAIL_V(Color()); - } + ERR_FAIL_COND_V_MSG(b < 0, Color(), "Invalid color code: " + p_color + "."); return Color(r / 255.0, g / 255.0, b / 255.0, a / 255.0); } @@ -425,12 +412,8 @@ Color Color::named(const String &p_name) { name = name.to_lower(); const Map<String, Color>::Element *color = _named_colors.find(name); - if (color) { - return color->value(); - } else { - ERR_EXPLAIN("Invalid Color Name: " + p_name); - ERR_FAIL_V(Color()); - } + ERR_FAIL_NULL_V_MSG(color, Color(), "Invalid color name: " + p_name + "."); + return color->value(); } String _to_hex(float p_val) { @@ -523,8 +506,7 @@ Color Color::from_hsv(float p_h, float p_s, float p_v, float p_a) const { // FIXME: Remove once Godot 3.1 has been released float Color::gray() const { - ERR_EXPLAIN("Color.gray() is deprecated and will be removed in a future version. Use Color.get_v() for a better grayscale approximation."); - WARN_DEPRECATED; + WARN_DEPRECATED_MSG("Color.gray() is deprecated and will be removed in a future version. Use Color.get_v() for a better grayscale approximation."); return (r + g + b) / 3.0; } diff --git a/core/color.h b/core/color.h index 77f95b5dc9..8fb78d1ced 100644 --- a/core/color.h +++ b/core/color.h @@ -33,9 +33,7 @@ #include "core/math/math_funcs.h" #include "core/ustring.h" -/** - @author Juan Linietsky <reduzio@gmail.com> -*/ + struct Color { union { diff --git a/core/command_queue_mt.h b/core/command_queue_mt.h index 3789eda5db..98f5bc56d7 100644 --- a/core/command_queue_mt.h +++ b/core/command_queue_mt.h @@ -37,10 +37,6 @@ #include "core/simple_type.h" #include "core/typedefs.h" -/** - @author Juan Linietsky <reduzio@gmail.com> -*/ - #define COMMA(N) _COMMA_##N #define _COMMA_0 #define _COMMA_1 , diff --git a/core/compressed_translation.cpp b/core/compressed_translation.cpp index f102721470..d927b74897 100644 --- a/core/compressed_translation.cpp +++ b/core/compressed_translation.cpp @@ -136,6 +136,8 @@ void PHashTranslation::generate(const Ref<Translation> &p_from) { bucket_table_size += 2 + b.size() * 4; } + ERR_FAIL_COND(bucket_table_size == 0); + hash_table.resize(size); bucket_table.resize(bucket_table_size); diff --git a/core/cowdata.h b/core/cowdata.h index 3e40ad0f4b..c92e20920c 100644 --- a/core/cowdata.h +++ b/core/cowdata.h @@ -33,6 +33,7 @@ #include <string.h> +#include "core/error_macros.h" #include "core/os/memory.h" #include "core/safe_refcount.h" diff --git a/core/crypto/SCsub b/core/crypto/SCsub new file mode 100644 index 0000000000..0a3f05d87a --- /dev/null +++ b/core/crypto/SCsub @@ -0,0 +1,38 @@ +#!/usr/bin/env python + +Import('env') + +env_crypto = env.Clone() + +is_builtin = env["builtin_mbedtls"] +has_module = env["module_mbedtls_enabled"] + +if is_builtin or not has_module: + # Use our headers for builtin or if the module is not going to be compiled. + # We decided not to depend on system mbedtls just for these few files that can + # be easily extracted. + env_crypto.Prepend(CPPPATH=["#thirdparty/mbedtls/include"]) + +# MbedTLS core functions (for CryptoCore). +# If the mbedtls module is compiled we don't need to add the .c files with our +# custom config since they will be built by the module itself. +# Only if the module is not enabled, we must compile here the required sources +# to make a "light" build with only the necessary mbedtls files. +if not has_module: + env_thirdparty = env_crypto.Clone() + env_thirdparty.disable_warnings() + # Custom config file + env_thirdparty.Append(CPPDEFINES=[('MBEDTLS_CONFIG_FILE', '\\"thirdparty/mbedtls/include/godot_core_mbedtls_config.h\\"')]) + thirdparty_mbedtls_dir = "#thirdparty/mbedtls/library/" + thirdparty_mbedtls_sources = [ + "aes.c", + "base64.c", + "md5.c", + "sha1.c", + "sha256.c", + "godot_core_mbedtls_platform.c" + ] + thirdparty_mbedtls_sources = [thirdparty_mbedtls_dir + file for file in thirdparty_mbedtls_sources] + env_thirdparty.add_source_files(env.core_sources, thirdparty_mbedtls_sources) + +env_crypto.add_source_files(env.core_sources, "*.cpp") diff --git a/core/crypto/crypto.cpp b/core/crypto/crypto.cpp new file mode 100644 index 0000000000..83a25da901 --- /dev/null +++ b/core/crypto/crypto.cpp @@ -0,0 +1,170 @@ +/*************************************************************************/ +/* crypto.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 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 "crypto.h" + +#include "core/engine.h" +#include "core/io/certs_compressed.gen.h" +#include "core/io/compression.h" + +/// Resources + +CryptoKey *(*CryptoKey::_create)() = NULL; +CryptoKey *CryptoKey::create() { + if (_create) + return _create(); + return NULL; +} + +void CryptoKey::_bind_methods() { + ClassDB::bind_method(D_METHOD("save", "path"), &CryptoKey::save); + ClassDB::bind_method(D_METHOD("load", "path"), &CryptoKey::load); +} + +X509Certificate *(*X509Certificate::_create)() = NULL; +X509Certificate *X509Certificate::create() { + if (_create) + return _create(); + return NULL; +} + +void X509Certificate::_bind_methods() { + ClassDB::bind_method(D_METHOD("save", "path"), &X509Certificate::save); + ClassDB::bind_method(D_METHOD("load", "path"), &X509Certificate::load); +} + +/// Crypto + +void (*Crypto::_load_default_certificates)(String p_path) = NULL; +Crypto *(*Crypto::_create)() = NULL; +Crypto *Crypto::create() { + if (_create) + return _create(); + return memnew(Crypto); +} + +void Crypto::load_default_certificates(String p_path) { + + if (_load_default_certificates) + _load_default_certificates(p_path); +} + +void Crypto::_bind_methods() { + ClassDB::bind_method(D_METHOD("generate_random_bytes", "size"), &Crypto::generate_random_bytes); + ClassDB::bind_method(D_METHOD("generate_rsa", "size"), &Crypto::generate_rsa); + ClassDB::bind_method(D_METHOD("generate_self_signed_certificate", "key", "issuer_name", "not_before", "not_after"), &Crypto::generate_self_signed_certificate, DEFVAL("CN=myserver,O=myorganisation,C=IT"), DEFVAL("20140101000000"), DEFVAL("20340101000000")); +} + +PoolByteArray Crypto::generate_random_bytes(int p_bytes) { + ERR_FAIL_V_MSG(PoolByteArray(), "generate_random_bytes is not available when mbedtls module is disabled."); +} + +Ref<CryptoKey> Crypto::generate_rsa(int p_bytes) { + ERR_FAIL_V_MSG(NULL, "generate_rsa is not available when mbedtls module is disabled."); +} + +Ref<X509Certificate> Crypto::generate_self_signed_certificate(Ref<CryptoKey> p_key, String p_issuer_name, String p_not_before, String p_not_after) { + ERR_FAIL_V_MSG(NULL, "generate_self_signed_certificate is not available when mbedtls module is disabled."); +} + +Crypto::Crypto() { +} + +/// Resource loader/saver + +RES ResourceFormatLoaderCrypto::load(const String &p_path, const String &p_original_path, Error *r_error) { + + String el = p_path.get_extension().to_lower(); + if (el == "crt") { + X509Certificate *cert = X509Certificate::create(); + if (cert) + cert->load(p_path); + return cert; + } else if (el == "key") { + CryptoKey *key = CryptoKey::create(); + if (key) + key->load(p_path); + return key; + } + return NULL; +} + +void ResourceFormatLoaderCrypto::get_recognized_extensions(List<String> *p_extensions) const { + + p_extensions->push_back("crt"); + p_extensions->push_back("key"); +} + +bool ResourceFormatLoaderCrypto::handles_type(const String &p_type) const { + + return p_type == "X509Certificate" || p_type == "CryptoKey"; +} + +String ResourceFormatLoaderCrypto::get_resource_type(const String &p_path) const { + + String el = p_path.get_extension().to_lower(); + if (el == "crt") + return "X509Certificate"; + else if (el == "key") + return "CryptoKey"; + return ""; +} + +Error ResourceFormatSaverCrypto::save(const String &p_path, const RES &p_resource, uint32_t p_flags) { + + Error err; + Ref<X509Certificate> cert = p_resource; + Ref<CryptoKey> key = p_resource; + if (cert.is_valid()) { + err = cert->save(p_path); + } else if (key.is_valid()) { + err = key->save(p_path); + } else { + ERR_FAIL_V(ERR_INVALID_PARAMETER); + } + ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot save Crypto resource to file '" + p_path + "'."); + return OK; +} + +void ResourceFormatSaverCrypto::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const { + + const X509Certificate *cert = Object::cast_to<X509Certificate>(*p_resource); + const CryptoKey *key = Object::cast_to<CryptoKey>(*p_resource); + if (cert) { + p_extensions->push_back("crt"); + } + if (key) { + p_extensions->push_back("key"); + } +} +bool ResourceFormatSaverCrypto::recognize(const RES &p_resource) const { + + return Object::cast_to<X509Certificate>(*p_resource) || Object::cast_to<CryptoKey>(*p_resource); +} diff --git a/core/crypto/crypto.h b/core/crypto/crypto.h new file mode 100644 index 0000000000..2de81f5b57 --- /dev/null +++ b/core/crypto/crypto.h @@ -0,0 +1,105 @@ +/*************************************************************************/ +/* crypto.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 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 CRYPTO_H +#define CRYPTO_H + +#include "core/reference.h" +#include "core/resource.h" + +#include "core/io/resource_loader.h" +#include "core/io/resource_saver.h" + +class CryptoKey : public Resource { + GDCLASS(CryptoKey, Resource); + +protected: + static void _bind_methods(); + static CryptoKey *(*_create)(); + +public: + static CryptoKey *create(); + virtual Error load(String p_path) = 0; + virtual Error save(String p_path) = 0; +}; + +class X509Certificate : public Resource { + GDCLASS(X509Certificate, Resource); + +protected: + static void _bind_methods(); + static X509Certificate *(*_create)(); + +public: + static X509Certificate *create(); + virtual Error load(String p_path) = 0; + virtual Error load_from_memory(const uint8_t *p_buffer, int p_len) = 0; + virtual Error save(String p_path) = 0; +}; + +class Crypto : public Reference { + GDCLASS(Crypto, Reference); + +protected: + static void _bind_methods(); + static Crypto *(*_create)(); + static void (*_load_default_certificates)(String p_path); + +public: + static Crypto *create(); + static void load_default_certificates(String p_path); + + virtual PoolByteArray generate_random_bytes(int p_bytes); + virtual Ref<CryptoKey> generate_rsa(int p_bytes); + virtual Ref<X509Certificate> generate_self_signed_certificate(Ref<CryptoKey> p_key, String p_issuer_name, String p_not_before, String p_not_after); + + Crypto(); +}; + +class ResourceFormatLoaderCrypto : public ResourceFormatLoader { + GDCLASS(ResourceFormatLoaderCrypto, ResourceFormatLoader); + +public: + virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL); + virtual void get_recognized_extensions(List<String> *p_extensions) const; + virtual bool handles_type(const String &p_type) const; + virtual String get_resource_type(const String &p_path) const; +}; + +class ResourceFormatSaverCrypto : public ResourceFormatSaver { + GDCLASS(ResourceFormatSaverCrypto, ResourceFormatSaver); + +public: + virtual Error save(const String &p_path, const RES &p_resource, uint32_t p_flags = 0); + virtual void get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const; + virtual bool recognize(const RES &p_resource) const; +}; + +#endif // CRYPTO_H diff --git a/core/math/crypto_core.cpp b/core/crypto/crypto_core.cpp index d7ba54e469..51c2e3c9e5 100644 --- a/core/math/crypto_core.cpp +++ b/core/crypto/crypto_core.cpp @@ -52,7 +52,7 @@ Error CryptoCore::MD5Context::start() { return ret ? FAILED : OK; } -Error CryptoCore::MD5Context::update(uint8_t *p_src, size_t p_len) { +Error CryptoCore::MD5Context::update(const uint8_t *p_src, size_t p_len) { int ret = mbedtls_md5_update_ret((mbedtls_md5_context *)ctx, p_src, p_len); return ret ? FAILED : OK; } @@ -62,6 +62,32 @@ Error CryptoCore::MD5Context::finish(unsigned char r_hash[16]) { return ret ? FAILED : OK; } +// SHA1 +CryptoCore::SHA1Context::SHA1Context() { + ctx = memalloc(sizeof(mbedtls_sha1_context)); + mbedtls_sha1_init((mbedtls_sha1_context *)ctx); +} + +CryptoCore::SHA1Context::~SHA1Context() { + mbedtls_sha1_free((mbedtls_sha1_context *)ctx); + memfree((mbedtls_sha1_context *)ctx); +} + +Error CryptoCore::SHA1Context::start() { + int ret = mbedtls_sha1_starts_ret((mbedtls_sha1_context *)ctx); + return ret ? FAILED : OK; +} + +Error CryptoCore::SHA1Context::update(const uint8_t *p_src, size_t p_len) { + int ret = mbedtls_sha1_update_ret((mbedtls_sha1_context *)ctx, p_src, p_len); + return ret ? FAILED : OK; +} + +Error CryptoCore::SHA1Context::finish(unsigned char r_hash[20]) { + int ret = mbedtls_sha1_finish_ret((mbedtls_sha1_context *)ctx, r_hash); + return ret ? FAILED : OK; +} + // SHA256 CryptoCore::SHA256Context::SHA256Context() { ctx = memalloc(sizeof(mbedtls_sha256_context)); @@ -78,12 +104,12 @@ Error CryptoCore::SHA256Context::start() { return ret ? FAILED : OK; } -Error CryptoCore::SHA256Context::update(uint8_t *p_src, size_t p_len) { +Error CryptoCore::SHA256Context::update(const uint8_t *p_src, size_t p_len) { int ret = mbedtls_sha256_update_ret((mbedtls_sha256_context *)ctx, p_src, p_len); return ret ? FAILED : OK; } -Error CryptoCore::SHA256Context::finish(unsigned char r_hash[16]) { +Error CryptoCore::SHA256Context::finish(unsigned char r_hash[32]) { int ret = mbedtls_sha256_finish_ret((mbedtls_sha256_context *)ctx, r_hash); return ret ? FAILED : OK; } diff --git a/core/math/crypto_core.h b/core/crypto/crypto_core.h index e28cb5a792..c859d612d4 100644 --- a/core/math/crypto_core.h +++ b/core/crypto/crypto_core.h @@ -46,10 +46,24 @@ public: ~MD5Context(); Error start(); - Error update(uint8_t *p_src, size_t p_len); + Error update(const uint8_t *p_src, size_t p_len); Error finish(unsigned char r_hash[16]); }; + class SHA1Context { + + private: + void *ctx; // To include, or not to include... + + public: + SHA1Context(); + ~SHA1Context(); + + Error start(); + Error update(const uint8_t *p_src, size_t p_len); + Error finish(unsigned char r_hash[20]); + }; + class SHA256Context { private: @@ -60,8 +74,8 @@ public: ~SHA256Context(); Error start(); - Error update(uint8_t *p_src, size_t p_len); - Error finish(unsigned char r_hash[16]); + Error update(const uint8_t *p_src, size_t p_len); + Error finish(unsigned char r_hash[32]); }; class AESContext { diff --git a/core/crypto/hashing_context.cpp b/core/crypto/hashing_context.cpp new file mode 100644 index 0000000000..bd863f546b --- /dev/null +++ b/core/crypto/hashing_context.cpp @@ -0,0 +1,137 @@ +/*************************************************************************/ +/* hashing_context.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 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 "hashing_context.h" + +#include "core/crypto/crypto_core.h" + +Error HashingContext::start(HashType p_type) { + ERR_FAIL_COND_V(ctx != NULL, ERR_ALREADY_IN_USE); + _create_ctx(p_type); + ERR_FAIL_COND_V(ctx == NULL, ERR_UNAVAILABLE); + switch (type) { + case HASH_MD5: + return ((CryptoCore::MD5Context *)ctx)->start(); + case HASH_SHA1: + return ((CryptoCore::SHA1Context *)ctx)->start(); + case HASH_SHA256: + return ((CryptoCore::SHA256Context *)ctx)->start(); + } + return ERR_UNAVAILABLE; +} + +Error HashingContext::update(PoolByteArray p_chunk) { + ERR_FAIL_COND_V(ctx == NULL, ERR_UNCONFIGURED); + size_t len = p_chunk.size(); + PoolByteArray::Read r = p_chunk.read(); + switch (type) { + case HASH_MD5: + return ((CryptoCore::MD5Context *)ctx)->update(&r[0], len); + case HASH_SHA1: + return ((CryptoCore::SHA1Context *)ctx)->update(&r[0], len); + case HASH_SHA256: + return ((CryptoCore::SHA256Context *)ctx)->update(&r[0], len); + } + return ERR_UNAVAILABLE; +} + +PoolByteArray HashingContext::finish() { + ERR_FAIL_COND_V(ctx == NULL, PoolByteArray()); + PoolByteArray out; + Error err = FAILED; + switch (type) { + case HASH_MD5: + out.resize(16); + err = ((CryptoCore::MD5Context *)ctx)->finish(out.write().ptr()); + break; + case HASH_SHA1: + out.resize(20); + err = ((CryptoCore::SHA1Context *)ctx)->finish(out.write().ptr()); + break; + case HASH_SHA256: + out.resize(32); + err = ((CryptoCore::SHA256Context *)ctx)->finish(out.write().ptr()); + break; + } + _delete_ctx(); + ERR_FAIL_COND_V(err != OK, PoolByteArray()); + return out; +} + +void HashingContext::_create_ctx(HashType p_type) { + type = p_type; + switch (type) { + case HASH_MD5: + ctx = memnew(CryptoCore::MD5Context); + break; + case HASH_SHA1: + ctx = memnew(CryptoCore::SHA1Context); + break; + case HASH_SHA256: + ctx = memnew(CryptoCore::SHA256Context); + break; + default: + ctx = NULL; + } +} + +void HashingContext::_delete_ctx() { + + switch (type) { + case HASH_MD5: + memdelete((CryptoCore::MD5Context *)ctx); + break; + case HASH_SHA1: + memdelete((CryptoCore::SHA1Context *)ctx); + break; + case HASH_SHA256: + memdelete((CryptoCore::SHA256Context *)ctx); + break; + } + ctx = NULL; +} + +void HashingContext::_bind_methods() { + ClassDB::bind_method(D_METHOD("start", "type"), &HashingContext::start); + ClassDB::bind_method(D_METHOD("update", "chunk"), &HashingContext::update); + ClassDB::bind_method(D_METHOD("finish"), &HashingContext::finish); + BIND_ENUM_CONSTANT(HASH_MD5); + BIND_ENUM_CONSTANT(HASH_SHA1); + BIND_ENUM_CONSTANT(HASH_SHA256); +} + +HashingContext::HashingContext() { + ctx = NULL; +} + +HashingContext::~HashingContext() { + if (ctx != NULL) + _delete_ctx(); +} diff --git a/core/crypto/hashing_context.h b/core/crypto/hashing_context.h new file mode 100644 index 0000000000..aa69636f2c --- /dev/null +++ b/core/crypto/hashing_context.h @@ -0,0 +1,66 @@ +/*************************************************************************/ +/* hashing_context.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 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 HASHING_CONTEXT_H +#define HASHING_CONTEXT_H + +#include "core/reference.h" + +class HashingContext : public Reference { + GDCLASS(HashingContext, Reference); + +public: + enum HashType { + HASH_MD5, + HASH_SHA1, + HASH_SHA256 + }; + +private: + void *ctx; + HashType type; + +protected: + static void _bind_methods(); + void _create_ctx(HashType p_type); + void _delete_ctx(); + +public: + Error start(HashType p_type); + Error update(PoolByteArray p_chunk); + PoolByteArray finish(); + + HashingContext(); + ~HashingContext(); +}; + +VARIANT_ENUM_CAST(HashingContext::HashType); + +#endif // HASHING_CONTEXT_H diff --git a/core/engine.cpp b/core/engine.cpp index 0dd0459403..b9dc057257 100644 --- a/core/engine.cpp +++ b/core/engine.cpp @@ -38,7 +38,7 @@ void Engine::set_iterations_per_second(int p_ips) { - ERR_FAIL_COND(p_ips <= 0); + ERR_FAIL_COND_MSG(p_ips <= 0, "Engine iterations per second must be greater than 0."); ips = p_ips; } int Engine::get_iterations_per_second() const { @@ -197,10 +197,7 @@ void Engine::add_singleton(const Singleton &p_singleton) { Object *Engine::get_singleton_object(const String &p_name) const { const Map<StringName, Object *>::Element *E = singleton_ptrs.find(p_name); - if (!E) { - ERR_EXPLAIN("Failed to retrieve non-existent singleton '" + p_name + "'"); - ERR_FAIL_V(NULL); - } + ERR_FAIL_COND_V_MSG(!E, NULL, "Failed to retrieve non-existent singleton '" + p_name + "'."); return E->get(); }; diff --git a/core/error_macros.h b/core/error_macros.h index 69874e280b..65802de9d2 100644 --- a/core/error_macros.h +++ b/core/error_macros.h @@ -140,6 +140,16 @@ extern bool _err_error_exists; _err_error_exists = false; \ } while (0); // (*) +#define ERR_FAIL_INDEX_MSG(m_index, m_size, m_msg) \ + do { \ + if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \ + ERR_EXPLAIN(m_msg); \ + _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \ + return; \ + } \ + _err_error_exists = false; \ + } while (0); // (*) + /** An index has failed if m_index<0 or m_index >=m_size, the function exits. * This function returns an error value, if returning Error, please select the most * appropriate error condition from error_macros.h @@ -154,6 +164,16 @@ extern bool _err_error_exists; _err_error_exists = false; \ } while (0); // (*) +#define ERR_FAIL_INDEX_V_MSG(m_index, m_size, m_retval, m_msg) \ + do { \ + if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \ + ERR_EXPLAIN(m_msg); \ + _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \ + return m_retval; \ + } \ + _err_error_exists = false; \ + } while (0); // (*) + /** An index has failed if m_index >=m_size, the function exits. * This function returns an error value, if returning Error, please select the most * appropriate error condition from error_macros.h @@ -168,6 +188,16 @@ extern bool _err_error_exists; _err_error_exists = false; \ } while (0); // (*) +#define ERR_FAIL_UNSIGNED_INDEX_V_MSG(m_index, m_size, m_retval, m_msg) \ + do { \ + if (unlikely((m_index) >= (m_size))) { \ + ERR_EXPLAIN(m_msg); \ + _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \ + return m_retval; \ + } \ + _err_error_exists = false; \ + } while (0); // (*) + /** Use this one if there is no sensible fallback, that is, the error is unrecoverable. * We'll return a null reference and try to keep running. */ @@ -179,6 +209,15 @@ extern bool _err_error_exists; } \ } while (0); // (*) +#define CRASH_BAD_INDEX_MSG(m_index, m_size, m_msg) \ + do { \ + if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \ + ERR_EXPLAIN(m_msg); \ + _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), true); \ + GENERATE_TRAP \ + } \ + } while (0); // (*) + /** An error condition happened (m_cond tested true) (WARNING this is the opposite as assert(). * the function will exit. */ @@ -192,6 +231,16 @@ extern bool _err_error_exists; _err_error_exists = false; \ } +#define ERR_FAIL_NULL_MSG(m_param, m_msg) \ + { \ + if (unlikely(!m_param)) { \ + ERR_EXPLAIN(m_msg); \ + _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter ' " _STR(m_param) " ' is null."); \ + return; \ + } \ + _err_error_exists = false; \ + } + #define ERR_FAIL_NULL_V(m_param, m_retval) \ { \ if (unlikely(!m_param)) { \ @@ -201,6 +250,16 @@ extern bool _err_error_exists; _err_error_exists = false; \ } +#define ERR_FAIL_NULL_V_MSG(m_param, m_retval, m_msg) \ + { \ + if (unlikely(!m_param)) { \ + ERR_EXPLAIN(m_msg); \ + _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter ' " _STR(m_param) " ' is null."); \ + return m_retval; \ + } \ + _err_error_exists = false; \ + } + /** An error condition happened (m_cond tested true) (WARNING this is the opposite as assert(). * the function will exit. */ @@ -214,6 +273,16 @@ extern bool _err_error_exists; _err_error_exists = false; \ } +#define ERR_FAIL_COND_MSG(m_cond, m_msg) \ + { \ + if (unlikely(m_cond)) { \ + ERR_EXPLAIN(m_msg); \ + _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition ' " _STR(m_cond) " ' is true."); \ + return; \ + } \ + _err_error_exists = false; \ + } + /** Use this one if there is no sensible fallback, that is, the error is unrecoverable. */ @@ -225,6 +294,15 @@ extern bool _err_error_exists; } \ } +#define CRASH_COND_MSG(m_cond, m_msg) \ + { \ + if (unlikely(m_cond)) { \ + ERR_EXPLAIN(m_msg); \ + _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Condition ' " _STR(m_cond) " ' is true."); \ + GENERATE_TRAP \ + } \ + } + /** An error condition happened (m_cond tested true) (WARNING this is the opposite as assert(). * the function will exit. * This function returns an error value, if returning Error, please select the most @@ -240,6 +318,16 @@ extern bool _err_error_exists; _err_error_exists = false; \ } +#define ERR_FAIL_COND_V_MSG(m_cond, m_retval, m_msg) \ + { \ + if (unlikely(m_cond)) { \ + ERR_EXPLAIN(m_msg); \ + _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition ' " _STR(m_cond) " ' is true. returned: " _STR(m_retval)); \ + return m_retval; \ + } \ + _err_error_exists = false; \ + } + /** An error condition happened (m_cond tested true) (WARNING this is the opposite as assert(). * the loop will skip to the next iteration. */ @@ -253,6 +341,16 @@ extern bool _err_error_exists; _err_error_exists = false; \ } +#define ERR_CONTINUE_MSG(m_cond, m_msg) \ + { \ + if (unlikely(m_cond)) { \ + ERR_EXPLAIN(m_msg); \ + _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition ' " _STR(m_cond) " ' is true. Continuing..:"); \ + continue; \ + } \ + _err_error_exists = false; \ + } + /** An error condition happened (m_cond tested true) (WARNING this is the opposite as assert(). * the loop will break */ @@ -266,6 +364,16 @@ extern bool _err_error_exists; _err_error_exists = false; \ } +#define ERR_BREAK_MSG(m_cond, m_msg) \ + { \ + if (unlikely(m_cond)) { \ + ERR_EXPLAIN(m_msg); \ + _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition ' " _STR(m_cond) " ' is true. Breaking..:"); \ + break; \ + } \ + _err_error_exists = false; \ + } + /** Print an error string and return */ @@ -276,6 +384,12 @@ extern bool _err_error_exists; return; \ } +#define ERR_FAIL_MSG(m_msg) \ + { \ + ERR_EXPLAIN(m_msg); \ + ERR_FAIL(); \ + } + /** Print an error string and return with value */ @@ -286,6 +400,12 @@ extern bool _err_error_exists; return m_value; \ } +#define ERR_FAIL_V_MSG(m_value, m_msg) \ + { \ + ERR_EXPLAIN(m_msg); \ + ERR_FAIL_V(m_value); \ + } + /** Use this one if there is no sensible fallback, that is, the error is unrecoverable. */ @@ -295,6 +415,12 @@ extern bool _err_error_exists; GENERATE_TRAP \ } +#define CRASH_NOW_MSG(m_msg) \ + { \ + ERR_EXPLAIN(m_msg); \ + CRASH_NOW(); \ + } + /** Print an error string. */ @@ -355,4 +481,15 @@ extern bool _err_error_exists; } \ } +#define WARN_DEPRECATED_MSG(m_msg) \ + { \ + static volatile bool warning_shown = false; \ + if (!warning_shown) { \ + ERR_EXPLAIN(m_msg); \ + _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "This method has been deprecated and will be removed in the future", ERR_HANDLER_WARNING); \ + _err_error_exists = false; \ + warning_shown = true; \ + } \ + } + #endif diff --git a/core/func_ref.cpp b/core/func_ref.cpp index 3d03137d09..66ef27f6b9 100644 --- a/core/func_ref.cpp +++ b/core/func_ref.cpp @@ -46,6 +46,17 @@ Variant FuncRef::call_func(const Variant **p_args, int p_argcount, Variant::Call return obj->call(function, p_args, p_argcount, r_error); } +Variant FuncRef::call_funcv(const Array &p_args) { + + ERR_FAIL_COND_V(id == 0, Variant()); + + Object *obj = ObjectDB::get_instance(id); + + ERR_FAIL_COND_V(!obj, Variant()); + + return obj->callv(function, p_args); +} + void FuncRef::set_instance(Object *p_obj) { ERR_FAIL_NULL(p_obj); @@ -77,6 +88,8 @@ void FuncRef::_bind_methods() { ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_func", &FuncRef::call_func, mi, defargs); } + ClassDB::bind_method(D_METHOD("call_funcv", "arg_array"), &FuncRef::call_funcv); + ClassDB::bind_method(D_METHOD("set_instance", "instance"), &FuncRef::set_instance); ClassDB::bind_method(D_METHOD("set_function", "name"), &FuncRef::set_function); ClassDB::bind_method(D_METHOD("is_valid"), &FuncRef::is_valid); diff --git a/core/func_ref.h b/core/func_ref.h index a143b58bf0..af0bf63203 100644 --- a/core/func_ref.h +++ b/core/func_ref.h @@ -44,6 +44,7 @@ protected: public: Variant call_func(const Variant **p_args, int p_argcount, Variant::CallError &r_error); + Variant call_funcv(const Array &p_args); void set_instance(Object *p_obj); void set_function(const StringName &p_func); bool is_valid() const; diff --git a/core/hash_map.h b/core/hash_map.h index 1513d7a65b..38da1d59ab 100644 --- a/core/hash_map.h +++ b/core/hash_map.h @@ -112,7 +112,7 @@ private: void erase_hash_table() { - ERR_FAIL_COND(elements); + ERR_FAIL_COND_MSG(elements, "Cannot erase hash table if there are still elements inside."); memdelete_arr(hash_table); hash_table = 0; @@ -151,11 +151,7 @@ private: return; Element **new_hash_table = memnew_arr(Element *, ((uint64_t)1 << new_hash_table_power)); - if (!new_hash_table) { - - ERR_PRINT("Out of Memory"); - return; - } + ERR_FAIL_COND_MSG(!new_hash_table, "Out of memory."); for (int i = 0; i < (1 << new_hash_table_power); i++) { @@ -208,10 +204,7 @@ private: /* if element doesn't exist, create it */ Element *e = memnew(Element); - if (!e) { - ERR_EXPLAIN("Out of memory"); - ERR_FAIL_V(NULL); - } + ERR_FAIL_COND_V_MSG(!e, NULL, "Out of memory."); uint32_t hash = Hasher::hash(p_key); uint32_t index = hash & ((1 << hash_table_power) - 1); e->next = hash_table[index]; @@ -498,10 +491,7 @@ public: } else { /* get the next key */ const Element *e = get_element(*p_key); - if (!e) { - ERR_EXPLAIN("Invalid key supplied") - ERR_FAIL_V(NULL); - } + ERR_FAIL_COND_V_MSG(!e, NULL, "Invalid key supplied."); if (e->next) { /* if there is a "next" in the list, return that */ return &e->next->pair.key; diff --git a/core/image.cpp b/core/image.cpp index 10778eced6..e0b0a1f8be 100644 --- a/core/image.cpp +++ b/core/image.cpp @@ -83,6 +83,7 @@ const char *Image::format_names[Image::FORMAT_MAX] = { }; SavePNGFunc Image::save_png_func = NULL; +SaveEXRFunc Image::save_exr_func = NULL; void Image::_put_pixelb(int p_x, int p_y, uint32_t p_pixelsize, uint8_t *p_data, const uint8_t *p_pixel) { @@ -422,8 +423,7 @@ void Image::convert(Format p_new_format) { if (format > FORMAT_RGBE9995 || p_new_format > FORMAT_RGBE9995) { - ERR_EXPLAIN("Cannot convert to <-> from compressed formats. Use compress() and decompress() instead."); - ERR_FAIL(); + ERR_FAIL_MSG("Cannot convert to <-> from compressed formats. Use compress() and decompress() instead."); } else if (format > FORMAT_RGBA8 || p_new_format > FORMAT_RGBA8) { @@ -753,15 +753,14 @@ static void _scale_lanczos(const uint8_t *__restrict p_src, uint8_t *__restrict for (int32_t buffer_x = 0; buffer_x < dst_width; buffer_x++) { - float src_real_x = buffer_x * x_scale; - int32_t src_x = src_real_x; - - int32_t start_x = MAX(0, src_x - half_kernel + 1); - int32_t end_x = MIN(src_width - 1, src_x + half_kernel); + // The corresponding point on the source image + float src_x = (buffer_x + 0.5f) * x_scale; // Offset by 0.5 so it uses the pixel's center + int32_t start_x = MAX(0, int32_t(src_x) - half_kernel + 1); + int32_t end_x = MIN(src_width - 1, int32_t(src_x) + half_kernel); // Create the kernel used by all the pixels of the column for (int32_t target_x = start_x; target_x <= end_x; target_x++) - kernel[target_x - start_x] = _lanczos((src_real_x - target_x) / scale_factor); + kernel[target_x - start_x] = _lanczos((target_x + 0.5f - src_x) / scale_factor); for (int32_t buffer_y = 0; buffer_y < src_height; buffer_y++) { @@ -804,14 +803,12 @@ static void _scale_lanczos(const uint8_t *__restrict p_src, uint8_t *__restrict for (int32_t dst_y = 0; dst_y < dst_height; dst_y++) { - float buffer_real_y = dst_y * y_scale; - int32_t buffer_y = buffer_real_y; - - int32_t start_y = MAX(0, buffer_y - half_kernel + 1); - int32_t end_y = MIN(src_height - 1, buffer_y + half_kernel); + float buffer_y = (dst_y + 0.5f) * y_scale; + int32_t start_y = MAX(0, int32_t(buffer_y) - half_kernel + 1); + int32_t end_y = MIN(src_height - 1, int32_t(buffer_y) + half_kernel); for (int32_t target_y = start_y; target_y <= end_y; target_y++) - kernel[target_y - start_y] = _lanczos((buffer_real_y - target_y) / scale_factor); + kernel[target_y - start_y] = _lanczos((target_y + 0.5f - buffer_y) / scale_factor); for (int32_t dst_x = 0; dst_x < dst_width; dst_x++) { @@ -866,10 +863,7 @@ bool Image::is_size_po2() const { void Image::resize_to_po2(bool p_square) { - if (!_can_modify(format)) { - ERR_EXPLAIN("Cannot resize in indexed, compressed or custom image formats."); - ERR_FAIL(); - } + ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot resize in compressed or custom image formats."); int w = next_power_of_2(width); int h = next_power_of_2(height); @@ -885,22 +879,16 @@ void Image::resize_to_po2(bool p_square) { void Image::resize(int p_width, int p_height, Interpolation p_interpolation) { - if (data.size() == 0) { - ERR_EXPLAIN("Cannot resize image before creating it, use create() or create_from_data() first."); - ERR_FAIL(); - } + ERR_FAIL_COND_MSG(data.size() == 0, "Cannot resize image before creating it, use create() or create_from_data() first."); - if (!_can_modify(format)) { - ERR_EXPLAIN("Cannot resize in indexed, compressed or custom image formats."); - ERR_FAIL(); - } + ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot resize in compressed or custom image formats."); bool mipmap_aware = p_interpolation == INTERPOLATE_TRILINEAR /* || p_interpolation == INTERPOLATE_TRICUBIC */; - ERR_FAIL_COND(p_width <= 0); - ERR_FAIL_COND(p_height <= 0); - ERR_FAIL_COND(p_width > MAX_WIDTH); - ERR_FAIL_COND(p_height > MAX_HEIGHT); + ERR_FAIL_COND_MSG(p_width <= 0, "Image width cannot be greater than 0."); + ERR_FAIL_COND_MSG(p_height <= 0, "Image height cannot be greater than 0."); + ERR_FAIL_COND_MSG(p_width > MAX_WIDTH, "Image width cannot be greater than " + itos(MAX_WIDTH) + "."); + ERR_FAIL_COND_MSG(p_height > MAX_HEIGHT, "Image height cannot be greater than " + itos(MAX_HEIGHT) + "."); if (p_width == width && p_height == height) return; @@ -1106,16 +1094,14 @@ void Image::resize(int p_width, int p_height, Interpolation p_interpolation) { void Image::crop_from_point(int p_x, int p_y, int p_width, int p_height) { - if (!_can_modify(format)) { - ERR_EXPLAIN("Cannot crop in indexed, compressed or custom image formats."); - ERR_FAIL(); - } - ERR_FAIL_COND(p_x < 0); - ERR_FAIL_COND(p_y < 0); - ERR_FAIL_COND(p_width <= 0); - ERR_FAIL_COND(p_height <= 0); - ERR_FAIL_COND(p_x + p_width > MAX_WIDTH); - ERR_FAIL_COND(p_y + p_height > MAX_HEIGHT); + ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot crop in compressed or custom image formats."); + + ERR_FAIL_COND_MSG(p_x < 0, "Start x position cannot be smaller than 0."); + ERR_FAIL_COND_MSG(p_y < 0, "Start y position cannot be smaller than 0."); + ERR_FAIL_COND_MSG(p_width <= 0, "Width of image must be greater than 0."); + ERR_FAIL_COND_MSG(p_height <= 0, "Height of image must be greater than 0."); + ERR_FAIL_COND_MSG(p_x + p_width > MAX_WIDTH, "End x position cannot be greater than " + itos(MAX_WIDTH) + "."); + ERR_FAIL_COND_MSG(p_y + p_height > MAX_HEIGHT, "End y position cannot be greater than " + itos(MAX_HEIGHT) + "."); /* to save memory, cropping should be done in-place, however, since this function will most likely either not be used much, or in critical areas, for now it won't, because @@ -1163,10 +1149,7 @@ void Image::crop(int p_width, int p_height) { void Image::flip_y() { - if (!_can_modify(format)) { - ERR_EXPLAIN("Cannot flip_y in indexed, compressed or custom image formats."); - ERR_FAIL(); - } + ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot flip_y in compressed or custom image formats."); bool used_mipmaps = has_mipmaps(); if (used_mipmaps) { @@ -1199,10 +1182,7 @@ void Image::flip_y() { void Image::flip_x() { - if (!_can_modify(format)) { - ERR_EXPLAIN("Cannot flip_x in indexed, compressed or custom image formats."); - ERR_FAIL(); - } + ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot flip_x in compressed or custom image formats."); bool used_mipmaps = has_mipmaps(); if (used_mipmaps) { @@ -1461,15 +1441,9 @@ void Image::normalize() { Error Image::generate_mipmaps(bool p_renormalize) { - if (!_can_modify(format)) { - ERR_EXPLAIN("Cannot generate mipmaps in indexed, compressed or custom image formats."); - ERR_FAIL_V(ERR_UNAVAILABLE); - } + ERR_FAIL_COND_V_MSG(!_can_modify(format), ERR_UNAVAILABLE, "Cannot generate mipmaps in compressed or custom image formats."); - if (width == 0 || height == 0) { - ERR_EXPLAIN("Cannot generate mipmaps with width or height equal to 0."); - ERR_FAIL_V(ERR_UNCONFIGURED); - } + ERR_FAIL_COND_V_MSG(width == 0 || height == 0, ERR_UNCONFIGURED, "Cannot generate mipmaps with width or height equal to 0."); int mmcount; @@ -1620,10 +1594,7 @@ void Image::create(int p_width, int p_height, bool p_use_mipmaps, Format p_forma int mm; int size = _get_dst_image_size(p_width, p_height, p_format, mm, p_use_mipmaps ? -1 : 0); - if (size != p_data.size()) { - ERR_EXPLAIN("Expected data size of " + itos(size) + " bytes in Image::create(), got instead " + itos(p_data.size()) + " bytes."); - ERR_FAIL_COND(p_data.size() != size); - } + ERR_FAIL_COND_MSG(p_data.size() != size, "Expected data size of " + itos(size) + " bytes in Image::create(), got instead " + itos(p_data.size()) + " bytes."); height = p_height; width = p_width; @@ -1917,6 +1888,14 @@ Error Image::save_png(const String &p_path) const { return save_png_func(p_path, Ref<Image>((Image *)this)); } +Error Image::save_exr(const String &p_path, bool p_grayscale) const { + + if (save_exr_func == NULL) + return ERR_UNAVAILABLE; + + return save_exr_func(p_path, Ref<Image>((Image *)this), p_grayscale); +} + int Image::get_image_data_size(int p_width, int p_height, Format p_format, bool p_mipmaps) { int mm; @@ -2076,7 +2055,7 @@ Ref<Image> Image::get_rect(const Rect2 &p_area) const { void Image::blit_rect(const Ref<Image> &p_src, const Rect2 &p_src_rect, const Point2 &p_dest) { - ERR_FAIL_COND(p_src.is_null()); + ERR_FAIL_COND_MSG(p_src.is_null(), "It's not a reference to a valid Image object."); int dsize = data.size(); int srcdsize = p_src->data.size(); ERR_FAIL_COND(dsize == 0); @@ -2126,16 +2105,16 @@ void Image::blit_rect(const Ref<Image> &p_src, const Rect2 &p_src_rect, const Po void Image::blit_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest) { - ERR_FAIL_COND(p_src.is_null()); - ERR_FAIL_COND(p_mask.is_null()); + ERR_FAIL_COND_MSG(p_src.is_null(), "It's not a reference to a valid Image object."); + ERR_FAIL_COND_MSG(p_mask.is_null(), "It's not a reference to a valid Image object."); int dsize = data.size(); int srcdsize = p_src->data.size(); int maskdsize = p_mask->data.size(); ERR_FAIL_COND(dsize == 0); ERR_FAIL_COND(srcdsize == 0); ERR_FAIL_COND(maskdsize == 0); - ERR_FAIL_COND(p_src->width != p_mask->width); - ERR_FAIL_COND(p_src->height != p_mask->height); + ERR_FAIL_COND_MSG(p_src->width != p_mask->width, "Source image width is different from mask width."); + ERR_FAIL_COND_MSG(p_src->height != p_mask->height, "Source image height is different from mask height."); ERR_FAIL_COND(format != p_src->format); Rect2i clipped_src_rect = Rect2i(0, 0, p_src->width, p_src->height).clip(p_src_rect); @@ -2189,7 +2168,7 @@ void Image::blit_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, co void Image::blend_rect(const Ref<Image> &p_src, const Rect2 &p_src_rect, const Point2 &p_dest) { - ERR_FAIL_COND(p_src.is_null()); + ERR_FAIL_COND_MSG(p_src.is_null(), "It's not a reference to a valid Image object."); int dsize = data.size(); int srcdsize = p_src->data.size(); ERR_FAIL_COND(dsize == 0); @@ -2239,16 +2218,16 @@ void Image::blend_rect(const Ref<Image> &p_src, const Rect2 &p_src_rect, const P void Image::blend_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest) { - ERR_FAIL_COND(p_src.is_null()); - ERR_FAIL_COND(p_mask.is_null()); + ERR_FAIL_COND_MSG(p_src.is_null(), "It's not a reference to a valid Image object."); + ERR_FAIL_COND_MSG(p_mask.is_null(), "It's not a reference to a valid Image object."); int dsize = data.size(); int srcdsize = p_src->data.size(); int maskdsize = p_mask->data.size(); ERR_FAIL_COND(dsize == 0); ERR_FAIL_COND(srcdsize == 0); ERR_FAIL_COND(maskdsize == 0); - ERR_FAIL_COND(p_src->width != p_mask->width); - ERR_FAIL_COND(p_src->height != p_mask->height); + ERR_FAIL_COND_MSG(p_src->width != p_mask->width, "Source image width is different from mask width."); + ERR_FAIL_COND_MSG(p_src->height != p_mask->height, "Source image height is different from mask height."); ERR_FAIL_COND(format != p_src->format); Rect2i clipped_src_rect = Rect2i(0, 0, p_src->width, p_src->height).clip(p_src_rect); @@ -2405,10 +2384,7 @@ Color Image::get_pixel(int p_x, int p_y) const { uint8_t *ptr = write_lock.ptr(); #ifdef DEBUG_ENABLED - if (!ptr) { - ERR_EXPLAIN("Image must be locked with 'lock()' before using get_pixel()"); - ERR_FAIL_V(Color()); - } + ERR_FAIL_COND_V_MSG(!ptr, Color(), "Image must be locked with 'lock()' before using get_pixel()."); ERR_FAIL_INDEX_V(p_x, width, Color()); ERR_FAIL_INDEX_V(p_y, height, Color()); @@ -2524,8 +2500,7 @@ Color Image::get_pixel(int p_x, int p_y) const { return Color::from_rgbe9995(((uint32_t *)ptr)[ofs]); } default: { - ERR_EXPLAIN("Can't get_pixel() on compressed image, sorry."); - ERR_FAIL_V(Color()); + ERR_FAIL_V_MSG(Color(), "Can't get_pixel() on compressed image, sorry."); } } } @@ -2538,10 +2513,7 @@ void Image::set_pixel(int p_x, int p_y, const Color &p_color) { uint8_t *ptr = write_lock.ptr(); #ifdef DEBUG_ENABLED - if (!ptr) { - ERR_EXPLAIN("Image must be locked with 'lock()' before using set_pixel()"); - ERR_FAIL(); - } + ERR_FAIL_COND_MSG(!ptr, "Image must be locked with 'lock()' before using set_pixel()."); ERR_FAIL_INDEX(p_x, width); ERR_FAIL_INDEX(p_y, height); @@ -2653,8 +2625,7 @@ void Image::set_pixel(int p_x, int p_y, const Color &p_color) { } break; default: { - ERR_EXPLAIN("Can't set_pixel() on compressed image, sorry."); - ERR_FAIL(); + ERR_FAIL_MSG("Can't set_pixel() on compressed image, sorry."); } } } @@ -2746,6 +2717,7 @@ void Image::_bind_methods() { ClassDB::bind_method(D_METHOD("load", "path"), &Image::load); ClassDB::bind_method(D_METHOD("save_png", "path"), &Image::save_png); + ClassDB::bind_method(D_METHOD("save_exr", "path", "grayscale"), &Image::save_exr, DEFVAL(false)); ClassDB::bind_method(D_METHOD("detect_alpha"), &Image::detect_alpha); ClassDB::bind_method(D_METHOD("is_invisible"), &Image::is_invisible); diff --git a/core/image.h b/core/image.h index cc796789cd..94ee8a2c33 100644 --- a/core/image.h +++ b/core/image.h @@ -49,11 +49,14 @@ class Image; typedef Error (*SavePNGFunc)(const String &p_path, const Ref<Image> &p_img); typedef Ref<Image> (*ImageMemLoadFunc)(const uint8_t *p_png, int p_size); +typedef Error (*SaveEXRFunc)(const String &p_path, const Ref<Image> &p_img, bool p_grayscale); + class Image : public Resource { GDCLASS(Image, Resource); public: static SavePNGFunc save_png_func; + static SaveEXRFunc save_exr_func; enum { MAX_WIDTH = 16384, // force a limit somehow @@ -217,9 +220,7 @@ public: /** * Resize the image, using the preferred interpolation method. - * Indexed-Color images always use INTERPOLATE_NEAREST. */ - void resize_to_po2(bool p_square = false); void resize(int p_width, int p_height, Interpolation p_interpolation = INTERPOLATE_BILINEAR); void shrink_x2(); @@ -258,6 +259,7 @@ public: Error load(const String &p_path); Error save_png(const String &p_path) const; + Error save_exr(const String &p_path, bool p_grayscale) const; /** * create an empty image @@ -354,7 +356,7 @@ public: void set_pixel(int p_x, int p_y, const Color &p_color); void copy_internals_from(const Ref<Image> &p_image) { - ERR_FAIL_COND(p_image.is_null()); + ERR_FAIL_COND_MSG(p_image.is_null(), "It's not a reference to a valid Image object."); format = p_image->format; width = p_image->width; height = p_image->height; diff --git a/core/input_map.cpp b/core/input_map.cpp index 165999f081..05c75febf2 100644 --- a/core/input_map.cpp +++ b/core/input_map.cpp @@ -56,7 +56,7 @@ void InputMap::_bind_methods() { void InputMap::add_action(const StringName &p_action, float p_deadzone) { - ERR_FAIL_COND(input_map.has(p_action)); + ERR_FAIL_COND_MSG(input_map.has(p_action), "InputMap already has action '" + String(p_action) + "'."); input_map[p_action] = Action(); static int last_id = 1; input_map[p_action].id = last_id; @@ -66,7 +66,7 @@ void InputMap::add_action(const StringName &p_action, float p_deadzone) { void InputMap::erase_action(const StringName &p_action) { - ERR_FAIL_COND(!input_map.has(p_action)); + ERR_FAIL_COND_MSG(!input_map.has(p_action), "Request for nonexistent InputMap action '" + String(p_action) + "'."); input_map.erase(p_action); } @@ -126,15 +126,15 @@ bool InputMap::has_action(const StringName &p_action) const { void InputMap::action_set_deadzone(const StringName &p_action, float p_deadzone) { - ERR_FAIL_COND(!input_map.has(p_action)); + ERR_FAIL_COND_MSG(!input_map.has(p_action), "Request for nonexistent InputMap action '" + String(p_action) + "'."); input_map[p_action].deadzone = p_deadzone; } void InputMap::action_add_event(const StringName &p_action, const Ref<InputEvent> &p_event) { - ERR_FAIL_COND(p_event.is_null()); - ERR_FAIL_COND(!input_map.has(p_action)); + ERR_FAIL_COND_MSG(p_event.is_null(), "It's not a reference to a valid InputEvent object."); + ERR_FAIL_COND_MSG(!input_map.has(p_action), "Request for nonexistent InputMap action '" + String(p_action) + "'."); if (_find_event(input_map[p_action], p_event)) return; //already gots @@ -143,13 +143,13 @@ void InputMap::action_add_event(const StringName &p_action, const Ref<InputEvent bool InputMap::action_has_event(const StringName &p_action, const Ref<InputEvent> &p_event) { - ERR_FAIL_COND_V(!input_map.has(p_action), false); + ERR_FAIL_COND_V_MSG(!input_map.has(p_action), false, "Request for nonexistent InputMap action '" + String(p_action) + "'."); return (_find_event(input_map[p_action], p_event) != NULL); } void InputMap::action_erase_event(const StringName &p_action, const Ref<InputEvent> &p_event) { - ERR_FAIL_COND(!input_map.has(p_action)); + ERR_FAIL_COND_MSG(!input_map.has(p_action), "Request for nonexistent InputMap action '" + String(p_action) + "'."); List<Ref<InputEvent> >::Element *E = _find_event(input_map[p_action], p_event); if (E) @@ -158,7 +158,7 @@ void InputMap::action_erase_event(const StringName &p_action, const Ref<InputEve void InputMap::action_erase_events(const StringName &p_action) { - ERR_FAIL_COND(!input_map.has(p_action)); + ERR_FAIL_COND_MSG(!input_map.has(p_action), "Request for nonexistent InputMap action '" + String(p_action) + "'."); input_map[p_action].inputs.clear(); } @@ -192,10 +192,7 @@ bool InputMap::event_is_action(const Ref<InputEvent> &p_event, const StringName bool InputMap::event_get_action_status(const Ref<InputEvent> &p_event, const StringName &p_action, bool *p_pressed, float *p_strength) const { Map<StringName, Action>::Element *E = input_map.find(p_action); - if (!E) { - ERR_EXPLAIN("Request for nonexistent InputMap action: " + String(p_action)); - ERR_FAIL_V(false); - } + ERR_FAIL_COND_V_MSG(!E, false, "Request for nonexistent InputMap action '" + String(p_action) + "'."); Ref<InputEventAction> input_event_action = p_event; if (input_event_action.is_valid()) { @@ -336,6 +333,6 @@ void InputMap::load_default() { InputMap::InputMap() { - ERR_FAIL_COND(singleton); + ERR_FAIL_COND_MSG(singleton, "Singleton in InputMap already exist."); singleton = this; } diff --git a/core/io/config_file.cpp b/core/io/config_file.cpp index f7fb72c089..5684c82d1c 100644 --- a/core/io/config_file.cpp +++ b/core/io/config_file.cpp @@ -86,10 +86,7 @@ void ConfigFile::set_value(const String &p_section, const String &p_key, const V Variant ConfigFile::get_value(const String &p_section, const String &p_key, Variant p_default) const { if (!values.has(p_section) || !values[p_section].has(p_key)) { - if (p_default.get_type() == Variant::NIL) { - ERR_EXPLAIN("Couldn't find the given section/key and no default was given"); - ERR_FAIL_V(p_default); - } + ERR_FAIL_COND_V_MSG(p_default.get_type() == Variant::NIL, p_default, "Couldn't find the given section/key and no default was given."); return p_default; } return values[p_section][p_key]; @@ -114,7 +111,7 @@ void ConfigFile::get_sections(List<String> *r_sections) const { } void ConfigFile::get_section_keys(const String &p_section, List<String> *r_keys) const { - ERR_FAIL_COND(!values.has(p_section)); + ERR_FAIL_COND_MSG(!values.has(p_section), "Cannont get keys from nonexistent section '" + p_section + "'."); for (OrderedHashMap<String, Variant>::ConstElement E = values[p_section].front(); E; E = E.next()) { r_keys->push_back(E.key()); @@ -126,6 +123,13 @@ void ConfigFile::erase_section(const String &p_section) { values.erase(p_section); } +void ConfigFile::erase_section_key(const String &p_section, const String &p_key) { + + ERR_FAIL_COND_MSG(!values.has(p_section), "Cannot erase key from nonexistent section '" + p_section + "'."); + + values[p_section].erase(p_key); +} + Error ConfigFile::save(const String &p_path) { Error err; @@ -204,7 +208,7 @@ Error ConfigFile::load(const String &p_path) { FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err); if (!f) - return ERR_CANT_OPEN; + return err; return _internal_load(p_path, f); } @@ -271,7 +275,7 @@ Error ConfigFile::_internal_load(const String &p_path, FileAccess *f) { memdelete(f); return OK; } else if (err != OK) { - ERR_PRINTS("ConfgFile::load - " + p_path + ":" + itos(lines) + " error: " + error_text); + ERR_PRINTS("ConfgFile::load - " + p_path + ":" + itos(lines) + " error: " + error_text + "."); memdelete(f); return err; } @@ -296,6 +300,7 @@ void ConfigFile::_bind_methods() { ClassDB::bind_method(D_METHOD("get_section_keys", "section"), &ConfigFile::_get_section_keys); ClassDB::bind_method(D_METHOD("erase_section", "section"), &ConfigFile::erase_section); + ClassDB::bind_method(D_METHOD("erase_section_key", "section", "key"), &ConfigFile::erase_section_key); ClassDB::bind_method(D_METHOD("load", "path"), &ConfigFile::load); ClassDB::bind_method(D_METHOD("save", "path"), &ConfigFile::save); diff --git a/core/io/config_file.h b/core/io/config_file.h index 3ab6fef868..d927779f9c 100644 --- a/core/io/config_file.h +++ b/core/io/config_file.h @@ -60,6 +60,7 @@ public: void get_section_keys(const String &p_section, List<String> *r_keys) const; void erase_section(const String &p_section); + void erase_section_key(const String &p_section, const String &p_key); Error save(const String &p_path); Error load(const String &p_path); diff --git a/core/io/file_access_buffered.cpp b/core/io/file_access_buffered.cpp index 15523a49a9..f72ad61da6 100644 --- a/core/io/file_access_buffered.cpp +++ b/core/io/file_access_buffered.cpp @@ -87,10 +87,8 @@ bool FileAccessBuffered::eof_reached() const { } uint8_t FileAccessBuffered::get_8() const { - if (!file.open) { - ERR_EXPLAIN("Can't get data, when file is not opened."); - ERR_FAIL_V(0); - } + + ERR_FAIL_COND_V_MSG(!file.open, 0, "Can't get data, when file is not opened."); uint8_t byte = 0; if (cache_data_left() >= 1) { @@ -104,10 +102,8 @@ uint8_t FileAccessBuffered::get_8() const { } int FileAccessBuffered::get_buffer(uint8_t *p_dest, int p_length) const { - if (!file.open) { - ERR_EXPLAIN("Can't get buffer, when file is not opened."); - ERR_FAIL_V(-1); - } + + ERR_FAIL_COND_V_MSG(!file.open, -1, "Can't get buffer, when file is not opened."); if (p_length > cache_size) { diff --git a/core/io/file_access_buffered_fa.h b/core/io/file_access_buffered_fa.h index 6e806e7b3f..c8cee04208 100644 --- a/core/io/file_access_buffered_fa.h +++ b/core/io/file_access_buffered_fa.h @@ -40,10 +40,7 @@ class FileAccessBufferedFA : public FileAccessBuffered { int read_data_block(int p_offset, int p_size, uint8_t *p_dest = 0) const { - if (!f.is_open()) { - ERR_EXPLAIN("Can't read data block, when file is not opened."); - ERR_FAIL_V(-1); - } + ERR_FAIL_COND_V_MSG(!f.is_open(), -1, "Can't read data block when file is not opened."); ((T *)&f)->seek(p_offset); diff --git a/core/io/file_access_compressed.cpp b/core/io/file_access_compressed.cpp index 6c4310a572..a52c6f79c9 100644 --- a/core/io/file_access_compressed.cpp +++ b/core/io/file_access_compressed.cpp @@ -195,7 +195,7 @@ bool FileAccessCompressed::is_open() const { void FileAccessCompressed::seek(size_t p_position) { - ERR_FAIL_COND(!f); + ERR_FAIL_COND_MSG(!f, "File must be opened before use."); if (writing) { ERR_FAIL_COND(p_position > write_max); @@ -208,7 +208,8 @@ void FileAccessCompressed::seek(size_t p_position) { if (p_position == read_total) { at_end = true; } else { - + at_end = false; + read_eof = false; int block_idx = p_position / block_size; if (block_idx != read_block) { @@ -226,7 +227,7 @@ void FileAccessCompressed::seek(size_t p_position) { void FileAccessCompressed::seek_end(int64_t p_position) { - ERR_FAIL_COND(!f); + ERR_FAIL_COND_MSG(!f, "File must be opened before use."); if (writing) { seek(write_max + p_position); @@ -237,7 +238,7 @@ void FileAccessCompressed::seek_end(int64_t p_position) { } size_t FileAccessCompressed::get_position() const { - ERR_FAIL_COND_V(!f, 0); + ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use."); if (writing) { return write_pos; @@ -248,7 +249,7 @@ size_t FileAccessCompressed::get_position() const { } size_t FileAccessCompressed::get_len() const { - ERR_FAIL_COND_V(!f, 0); + ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use."); if (writing) { return write_max; @@ -259,7 +260,7 @@ size_t FileAccessCompressed::get_len() const { bool FileAccessCompressed::eof_reached() const { - ERR_FAIL_COND_V(!f, false); + ERR_FAIL_COND_V_MSG(!f, false, "File must be opened before use."); if (writing) { return false; } else { @@ -269,8 +270,8 @@ bool FileAccessCompressed::eof_reached() const { uint8_t FileAccessCompressed::get_8() const { - ERR_FAIL_COND_V(writing, 0); - ERR_FAIL_COND_V(!f, 0); + ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use."); + ERR_FAIL_COND_V_MSG(writing, 0, "File has not been opened in read mode."); if (at_end) { read_eof = true; @@ -300,8 +301,8 @@ uint8_t FileAccessCompressed::get_8() const { } int FileAccessCompressed::get_buffer(uint8_t *p_dst, int p_length) const { - ERR_FAIL_COND_V(writing, 0); - ERR_FAIL_COND_V(!f, 0); + ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use."); + ERR_FAIL_COND_V_MSG(writing, 0, "File has not been opened in read mode."); if (at_end) { read_eof = true; @@ -341,16 +342,16 @@ Error FileAccessCompressed::get_error() const { } void FileAccessCompressed::flush() { - ERR_FAIL_COND(!f); - ERR_FAIL_COND(!writing); + ERR_FAIL_COND_MSG(!f, "File must be opened before use."); + ERR_FAIL_COND_MSG(!writing, "File has not been opened in read mode."); // compressed files keep data in memory till close() } void FileAccessCompressed::store_8(uint8_t p_dest) { - ERR_FAIL_COND(!f); - ERR_FAIL_COND(!writing); + ERR_FAIL_COND_MSG(!f, "File must be opened before use."); + ERR_FAIL_COND_MSG(!writing, "File has not been opened in read mode."); WRITE_FIT(1); write_ptr[write_pos++] = p_dest; diff --git a/core/io/file_access_encrypted.cpp b/core/io/file_access_encrypted.cpp index ccee6aeb15..c2e4e0f575 100644 --- a/core/io/file_access_encrypted.cpp +++ b/core/io/file_access_encrypted.cpp @@ -30,7 +30,7 @@ #include "file_access_encrypted.h" -#include "core/math/crypto_core.h" +#include "core/crypto/crypto_core.h" #include "core/os/copymem.h" #include "core/print_string.h" #include "core/variant.h" @@ -41,7 +41,7 @@ Error FileAccessEncrypted::open_and_parse(FileAccess *p_base, const Vector<uint8_t> &p_key, Mode p_mode) { - ERR_FAIL_COND_V(file != NULL, ERR_ALREADY_IN_USE); + ERR_FAIL_COND_V_MSG(file != NULL, ERR_ALREADY_IN_USE, "Can't open file while another file from path '" + file->get_path_absolute() + "' is open."); ERR_FAIL_COND_V(p_key.size() != 32, ERR_INVALID_PARAMETER); pos = 0; @@ -94,8 +94,7 @@ Error FileAccessEncrypted::open_and_parse(FileAccess *p_base, const Vector<uint8 unsigned char hash[16]; ERR_FAIL_COND_V(CryptoCore::md5(data.ptr(), data.size(), hash) != OK, ERR_BUG); - ERR_EXPLAIN("The MD5 sum of the decrypted file does not match the expected value. It could be that the file is corrupt, or that the provided decryption key is invalid."); - ERR_FAIL_COND_V(String::md5(hash) != String::md5(md5d), ERR_FILE_CORRUPT); + ERR_FAIL_COND_V_MSG(String::md5(hash) != String::md5(md5d), ERR_FILE_CORRUPT, "The MD5 sum of the decrypted file does not match the expected value. It could be that the file is corrupt, or that the provided decryption key is invalid."); file = p_base; } @@ -177,6 +176,22 @@ bool FileAccessEncrypted::is_open() const { return file != NULL; } +String FileAccessEncrypted::get_path() const { + + if (file) + return file->get_path(); + else + return ""; +} + +String FileAccessEncrypted::get_path_absolute() const { + + if (file) + return file->get_path_absolute(); + else + return ""; +} + void FileAccessEncrypted::seek(size_t p_position) { if (p_position > (size_t)data.size()) @@ -206,7 +221,7 @@ bool FileAccessEncrypted::eof_reached() const { uint8_t FileAccessEncrypted::get_8() const { - ERR_FAIL_COND_V(writing, 0); + ERR_FAIL_COND_V_MSG(writing, 0, "File has not been opened in read mode."); if (pos >= data.size()) { eofed = true; return 0; @@ -218,7 +233,7 @@ uint8_t FileAccessEncrypted::get_8() const { } int FileAccessEncrypted::get_buffer(uint8_t *p_dst, int p_length) const { - ERR_FAIL_COND_V(writing, 0); + ERR_FAIL_COND_V_MSG(writing, 0, "File has not been opened in read mode."); int to_copy = MIN(p_length, data.size() - pos); for (int i = 0; i < to_copy; i++) { @@ -240,7 +255,7 @@ Error FileAccessEncrypted::get_error() const { void FileAccessEncrypted::store_buffer(const uint8_t *p_src, int p_length) { - ERR_FAIL_COND(!writing); + ERR_FAIL_COND_MSG(!writing, "File has not been opened in read mode."); if (pos < data.size()) { @@ -260,14 +275,14 @@ void FileAccessEncrypted::store_buffer(const uint8_t *p_src, int p_length) { } void FileAccessEncrypted::flush() { - ERR_FAIL_COND(!writing); + ERR_FAIL_COND_MSG(!writing, "File has not been opened in read mode."); // encrypted files keep data in memory till close() } void FileAccessEncrypted::store_8(uint8_t p_dest) { - ERR_FAIL_COND(!writing); + ERR_FAIL_COND_MSG(!writing, "File has not been opened in read mode."); if (pos < data.size()) { data.write[pos] = p_dest; @@ -298,7 +313,7 @@ uint32_t FileAccessEncrypted::_get_unix_permissions(const String &p_file) { } Error FileAccessEncrypted::_set_unix_permissions(const String &p_file, uint32_t p_permissions) { - ERR_PRINT("Setting UNIX permissions on encrypted files is not implemented yet"); + ERR_PRINT("Setting UNIX permissions on encrypted files is not implemented yet."); return ERR_UNAVAILABLE; } diff --git a/core/io/file_access_encrypted.h b/core/io/file_access_encrypted.h index d779a150ac..c3be0f7de8 100644 --- a/core/io/file_access_encrypted.h +++ b/core/io/file_access_encrypted.h @@ -60,6 +60,9 @@ public: virtual void close(); ///< close a file virtual bool is_open() const; ///< true when file is open + virtual String get_path() const; /// returns the path for the current open file + virtual String get_path_absolute() const; /// returns the absolute path for the current open file + virtual void seek(size_t p_position); ///< seek to a given position virtual void seek_end(int64_t p_position = 0); ///< seek from the end of file virtual size_t get_position() const; ///< get position in the file diff --git a/core/io/file_access_memory.cpp b/core/io/file_access_memory.cpp index fbcf5b8021..c0acd36751 100644 --- a/core/io/file_access_memory.cpp +++ b/core/io/file_access_memory.cpp @@ -90,7 +90,7 @@ Error FileAccessMemory::_open(const String &p_path, int p_mode_flags) { //name = DirAccess::normalize_path(name); Map<String, Vector<uint8_t> >::Element *E = files->find(name); - ERR_FAIL_COND_V(!E, ERR_FILE_NOT_FOUND); + ERR_FAIL_COND_V_MSG(!E, ERR_FILE_NOT_FOUND, "Can't find file '" + p_path + "'."); data = E->get().ptrw(); length = E->get().size(); diff --git a/core/io/file_access_network.cpp b/core/io/file_access_network.cpp index d1c7f5c334..e653a924ba 100644 --- a/core/io/file_access_network.cpp +++ b/core/io/file_access_network.cpp @@ -195,7 +195,7 @@ Error FileAccessNetworkClient::connect(const String &p_host, int p_port, const S DEBUG_PRINT("IP: " + String(ip) + " port " + itos(p_port)); Error err = client->connect_to_host(ip, p_port); - ERR_FAIL_COND_V(err, err); + ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot connect to host with IP: " + String(ip) + " and port: " + itos(p_port)); while (client->get_status() == StreamPeerTCP::STATUS_CONNECTING) { //DEBUG_PRINT("trying to connect...."); OS::get_singleton()->delay_usec(1000); @@ -339,7 +339,7 @@ bool FileAccessNetwork::is_open() const { void FileAccessNetwork::seek(size_t p_position) { - ERR_FAIL_COND(!opened); + ERR_FAIL_COND_MSG(!opened, "File must be opened before use."); eof_flag = p_position > total_size; if (p_position >= total_size) { @@ -355,18 +355,18 @@ void FileAccessNetwork::seek_end(int64_t p_position) { } size_t FileAccessNetwork::get_position() const { - ERR_FAIL_COND_V(!opened, 0); + ERR_FAIL_COND_V_MSG(!opened, 0, "File must be opened before use."); return pos; } size_t FileAccessNetwork::get_len() const { - ERR_FAIL_COND_V(!opened, 0); + ERR_FAIL_COND_V_MSG(!opened, 0, "File must be opened before use."); return total_size; } bool FileAccessNetwork::eof_reached() const { - ERR_FAIL_COND_V(!opened, false); + ERR_FAIL_COND_V_MSG(!opened, false, "File must be opened before use."); return eof_flag; } diff --git a/core/io/file_access_pack.cpp b/core/io/file_access_pack.cpp index ca66b34e17..34d3eb5344 100644 --- a/core/io/file_access_pack.cpp +++ b/core/io/file_access_pack.cpp @@ -36,11 +36,11 @@ #define PACK_VERSION 1 -Error PackedData::add_pack(const String &p_path) { +Error PackedData::add_pack(const String &p_path, bool p_replace_files) { for (int i = 0; i < sources.size(); i++) { - if (sources[i]->try_open_pack(p_path)) { + if (sources[i]->try_open_pack(p_path, p_replace_files)) { return OK; }; @@ -49,7 +49,7 @@ Error PackedData::add_pack(const String &p_path) { return ERR_FILE_UNRECOGNIZED; }; -void PackedData::add_path(const String &pkg_path, const String &path, uint64_t ofs, uint64_t size, const uint8_t *p_md5, PackSource *p_src) { +void PackedData::add_path(const String &pkg_path, const String &path, uint64_t ofs, uint64_t size, const uint8_t *p_md5, PackSource *p_src, bool p_replace_files) { PathMD5 pmd5(path.md5_buffer()); //printf("adding path %ls, %lli, %lli\n", path.c_str(), pmd5.a, pmd5.b); @@ -64,7 +64,8 @@ void PackedData::add_path(const String &pkg_path, const String &path, uint64_t o pf.md5[i] = p_md5[i]; pf.src = p_src; - files[pmd5] = pf; + if (!exists || p_replace_files) + files[pmd5] = pf; if (!exists) { //search for dir @@ -90,7 +91,7 @@ void PackedData::add_path(const String &pkg_path, const String &path, uint64_t o } } String filename = path.get_file(); - // Don't add as a file if the path points to a directoryy + // Don't add as a file if the path points to a directory if (!filename.empty()) { cd->files.insert(filename); } @@ -133,7 +134,7 @@ PackedData::~PackedData() { ////////////////////////////////////////////////////////////////// -bool PackedSourcePCK::try_open_pack(const String &p_path) { +bool PackedSourcePCK::try_open_pack(const String &p_path, bool p_replace_files) { FileAccess *f = FileAccess::open(p_path, FileAccess::READ); if (!f) @@ -150,6 +151,7 @@ bool PackedSourcePCK::try_open_pack(const String &p_path) { magic = f->get_32(); if (magic != 0x43504447) { + f->close(); memdelete(f); return false; } @@ -161,6 +163,7 @@ bool PackedSourcePCK::try_open_pack(const String &p_path) { magic = f->get_32(); if (magic != 0x43504447) { + f->close(); memdelete(f); return false; } @@ -171,10 +174,16 @@ bool PackedSourcePCK::try_open_pack(const String &p_path) { uint32_t ver_minor = f->get_32(); f->get_32(); // ver_rev - ERR_EXPLAIN("Pack version unsupported: " + itos(version)); - ERR_FAIL_COND_V(version != PACK_VERSION, false); - ERR_EXPLAIN("Pack created with a newer version of the engine: " + itos(ver_major) + "." + itos(ver_minor)); - ERR_FAIL_COND_V(ver_major > VERSION_MAJOR || (ver_major == VERSION_MAJOR && ver_minor > VERSION_MINOR), false); + if (version != PACK_VERSION) { + f->close(); + memdelete(f); + ERR_FAIL_V_MSG(false, "Pack version unsupported: " + itos(version) + "."); + } + if (ver_major > VERSION_MAJOR || (ver_major == VERSION_MAJOR && ver_minor > VERSION_MINOR)) { + f->close(); + memdelete(f); + ERR_FAIL_V_MSG(false, "Pack created with a newer version of the engine: " + itos(ver_major) + "." + itos(ver_minor) + "."); + } for (int i = 0; i < 16; i++) { //reserved @@ -198,9 +207,11 @@ bool PackedSourcePCK::try_open_pack(const String &p_path) { uint64_t size = f->get_64(); uint8_t md5[16]; f->get_buffer(md5, 16); - PackedData::get_singleton()->add_path(p_path, path, ofs, size, md5, this); + PackedData::get_singleton()->add_path(p_path, path, ofs, size, md5, this, p_replace_files); }; + f->close(); + memdelete(f); return true; }; @@ -322,10 +333,9 @@ bool FileAccessPack::file_exists(const String &p_name) { FileAccessPack::FileAccessPack(const String &p_path, const PackedData::PackedFile &p_file) : pf(p_file), f(FileAccess::open(pf.pack, FileAccess::READ)) { - if (!f) { - ERR_EXPLAIN("Can't open pack-referenced file: " + String(pf.pack)); - ERR_FAIL_COND(!f); - } + + ERR_FAIL_COND_MSG(!f, "Can't open pack-referenced file '" + String(pf.pack) + "'."); + f->seek(pf.offset); pos = 0; eof = false; @@ -463,11 +473,15 @@ String DirAccessPack::get_current_dir() { bool DirAccessPack::file_exists(String p_file) { + p_file = fix_path(p_file); + return current->files.has(p_file); } bool DirAccessPack::dir_exists(String p_dir) { + p_dir = fix_path(p_dir); + return current->subdirs.has(p_dir); } diff --git a/core/io/file_access_pack.h b/core/io/file_access_pack.h index a21dd7d22d..8c34069f3a 100644 --- a/core/io/file_access_pack.h +++ b/core/io/file_access_pack.h @@ -102,13 +102,13 @@ private: public: void add_pack_source(PackSource *p_source); - void add_path(const String &pkg_path, const String &path, uint64_t ofs, uint64_t size, const uint8_t *p_md5, PackSource *p_src); // for PackSource + void add_path(const String &pkg_path, const String &path, uint64_t ofs, uint64_t size, const uint8_t *p_md5, PackSource *p_src, bool p_replace_files); // for PackSource void set_disabled(bool p_disabled) { disabled = p_disabled; } _FORCE_INLINE_ bool is_disabled() const { return disabled; } static PackedData *get_singleton() { return singleton; } - Error add_pack(const String &p_path); + Error add_pack(const String &p_path, bool p_replace_files); _FORCE_INLINE_ FileAccess *try_open_path(const String &p_path); _FORCE_INLINE_ bool has_path(const String &p_path); @@ -120,7 +120,7 @@ public: class PackSource { public: - virtual bool try_open_pack(const String &p_path) = 0; + virtual bool try_open_pack(const String &p_path, bool p_replace_files) = 0; virtual FileAccess *get_file(const String &p_path, PackedData::PackedFile *p_file) = 0; virtual ~PackSource() {} }; @@ -128,7 +128,7 @@ public: class PackedSourcePCK : public PackSource { public: - virtual bool try_open_pack(const String &p_path); + virtual bool try_open_pack(const String &p_path, bool p_replace_files); virtual FileAccess *get_file(const String &p_path, PackedData::PackedFile *p_file); }; diff --git a/core/io/file_access_zip.cpp b/core/io/file_access_zip.cpp index be28c9a877..3187f3bab6 100644 --- a/core/io/file_access_zip.cpp +++ b/core/io/file_access_zip.cpp @@ -117,7 +117,7 @@ static void godot_free(voidpf opaque, voidpf address) { void ZipArchive::close_handle(unzFile p_file) const { - ERR_FAIL_COND(!p_file); + ERR_FAIL_COND_MSG(!p_file, "Cannot close a file if none is open."); FileAccess *f = (FileAccess *)unzGetOpaque(p_file); unzCloseCurrentFile(p_file); unzClose(p_file); @@ -126,11 +126,11 @@ void ZipArchive::close_handle(unzFile p_file) const { unzFile ZipArchive::get_file_handle(String p_file) const { - ERR_FAIL_COND_V(!file_exists(p_file), NULL); + ERR_FAIL_COND_V_MSG(!file_exists(p_file), NULL, "File '" + p_file + " doesn't exist."); File file = files[p_file]; FileAccess *f = FileAccess::open(packages[file.package].filename, FileAccess::READ); - ERR_FAIL_COND_V(!f, NULL); + ERR_FAIL_COND_V_MSG(!f, NULL, "Cannot open file '" + packages[file.package].filename + "'."); zlib_filefunc_def io; zeromem(&io, sizeof(io)); @@ -160,7 +160,7 @@ unzFile ZipArchive::get_file_handle(String p_file) const { return pkg; } -bool ZipArchive::try_open_pack(const String &p_path) { +bool ZipArchive::try_open_pack(const String &p_path, bool p_replace_files) { //printf("opening zip pack %ls, %i, %i\n", p_name.c_str(), p_name.extension().nocasecmp_to("zip"), p_name.extension().nocasecmp_to("pcz")); if (p_path.get_extension().nocasecmp_to("zip") != 0 && p_path.get_extension().nocasecmp_to("pcz") != 0) @@ -194,7 +194,7 @@ bool ZipArchive::try_open_pack(const String &p_path) { packages.push_back(pkg); int pkg_num = packages.size() - 1; - for (unsigned int i = 0; i < gi.number_entry; i++) { + for (uint64_t i = 0; i < gi.number_entry; i++) { char filename_inzip[256]; @@ -210,7 +210,7 @@ bool ZipArchive::try_open_pack(const String &p_path) { files[fname] = f; uint8_t md5[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; - PackedData::get_singleton()->add_path(p_path, fname, 1, 0, md5, this); + PackedData::get_singleton()->add_path(p_path, fname, 1, 0, md5, this, p_replace_files); //printf("packed data add path %ls, %ls\n", p_name.c_str(), fname.c_str()); if ((i + 1) < gi.number_entry) { diff --git a/core/io/file_access_zip.h b/core/io/file_access_zip.h index 217176c0af..cdd50f9eb3 100644 --- a/core/io/file_access_zip.h +++ b/core/io/file_access_zip.h @@ -74,7 +74,7 @@ public: bool file_exists(String p_name) const; - virtual bool try_open_pack(const String &p_path); + virtual bool try_open_pack(const String &p_path, bool p_replace_files); FileAccess *get_file(const String &p_path, PackedData::PackedFile *p_file); static ZipArchive *get_singleton(); diff --git a/core/io/image_loader.cpp b/core/io/image_loader.cpp index a759e615c7..095c2abb54 100644 --- a/core/io/image_loader.cpp +++ b/core/io/image_loader.cpp @@ -46,14 +46,14 @@ bool ImageFormatLoader::recognize(const String &p_extension) const { } Error ImageLoader::load_image(String p_file, Ref<Image> p_image, FileAccess *p_custom, bool p_force_linear, float p_scale) { - ERR_FAIL_COND_V(p_image.is_null(), ERR_INVALID_PARAMETER); + ERR_FAIL_COND_V_MSG(p_image.is_null(), ERR_INVALID_PARAMETER, "It's not a reference to a valid Image object."); FileAccess *f = p_custom; if (!f) { Error err; f = FileAccess::open(p_file, FileAccess::READ, &err); if (!f) { - ERR_PRINTS("Error opening file: " + p_file); + ERR_PRINTS("Error opening file '" + p_file + "'."); return err; } } diff --git a/core/io/image_loader.h b/core/io/image_loader.h index ae4b72a534..af6b0551a3 100644 --- a/core/io/image_loader.h +++ b/core/io/image_loader.h @@ -37,24 +37,8 @@ #include "core/os/file_access.h" #include "core/ustring.h" -/** - @author Juan Linietsky <reduzio@gmail.com> -*/ - -/** - * @class ImageScanLineLoader - * @author Juan Linietsky <reduzio@gmail.com> - * - - */ class ImageLoader; -/** - * @class ImageLoader - * Base Class and singleton for loading images from disk - * Can load images in one go, or by scanline - */ - class ImageFormatLoader { friend class ImageLoader; friend class ResourceFormatLoaderImage; diff --git a/core/io/ip.cpp b/core/io/ip.cpp index 3d87131b51..f1b6570799 100644 --- a/core/io/ip.cpp +++ b/core/io/ip.cpp @@ -305,7 +305,7 @@ IP *(*IP::_create)() = NULL; IP *IP::create() { - ERR_FAIL_COND_V(singleton, NULL); + ERR_FAIL_COND_V_MSG(singleton, NULL, "IP singleton already exist."); ERR_FAIL_COND_V(!_create, NULL); return _create(); } diff --git a/core/io/ip_address.cpp b/core/io/ip_address.cpp index 9305afac5f..0980027f42 100644 --- a/core/io/ip_address.cpp +++ b/core/io/ip_address.cpp @@ -81,8 +81,7 @@ static void _parse_hex(const String &p_string, int p_start, uint8_t *p_dst) { } else if (c == ':') { break; } else { - ERR_EXPLAIN("Invalid character in ipv6 address: " + p_string); - ERR_FAIL(); + ERR_FAIL_MSG("Invalid character in IPv6 address: " + p_string + "."); }; ret = ret << 4; ret += n; @@ -126,9 +125,7 @@ void IP_Address::_parse_ipv6(const String &p_string) { ++parts_count; }; } else { - - ERR_EXPLAIN("Invalid character in IPv6 address: " + p_string); - ERR_FAIL(); + ERR_FAIL_MSG("Invalid character in IPv6 address: " + p_string + "."); }; }; @@ -166,10 +163,7 @@ void IP_Address::_parse_ipv4(const String &p_string, int p_start, uint8_t *p_ret }; int slices = ip.get_slice_count("."); - if (slices != 4) { - ERR_EXPLAIN("Invalid IP Address String: " + ip); - ERR_FAIL(); - } + ERR_FAIL_COND_MSG(slices != 4, "Invalid IP address string: " + ip + "."); for (int i = 0; i < 4; i++) { p_ret[i] = ip.get_slicec('.', i).to_int(); } @@ -187,7 +181,7 @@ bool IP_Address::is_ipv4() const { } const uint8_t *IP_Address::get_ipv4() const { - ERR_FAIL_COND_V(!is_ipv4(), &(field8[12])); // Not the correct IPv4 (it's an IPv6), but we don't want to return a null pointer risking an engine crash. + ERR_FAIL_COND_V_MSG(!is_ipv4(), &(field8[12]), "IPv4 requested, but current IP is IPv6."); // Not the correct IPv4 (it's an IPv6), but we don't want to return a null pointer risking an engine crash. return &(field8[12]); } @@ -229,7 +223,7 @@ IP_Address::IP_Address(const String &p_string) { valid = true; } else { - ERR_PRINT("Invalid IP address"); + ERR_PRINT("Invalid IP address."); } } diff --git a/core/io/marshalls.cpp b/core/io/marshalls.cpp index dc5581ea01..2ae542bca7 100644 --- a/core/io/marshalls.cpp +++ b/core/io/marshalls.cpp @@ -377,11 +377,6 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int } } break; - /*case Variant::RESOURCE: { - - ERR_EXPLAIN("Can't marshallize resources"); - ERR_FAIL_V(ERR_INVALID_DATA); //no, i'm sorry, no go - } break;*/ case Variant::_RID: { r_variant = RID(); @@ -483,7 +478,7 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int int used; Error err = decode_variant(key, buf, len, &used, p_allow_objects); - ERR_FAIL_COND_V(err, err); + ERR_FAIL_COND_V_MSG(err != OK, err, "Error when trying to decode Variant."); buf += used; len -= used; @@ -492,7 +487,7 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int } err = decode_variant(value, buf, len, &used, p_allow_objects); - ERR_FAIL_COND_V(err, err); + ERR_FAIL_COND_V_MSG(err != OK, err, "Error when trying to decode Variant."); buf += used; len -= used; @@ -527,7 +522,7 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int int used = 0; Variant v; Error err = decode_variant(v, buf, len, &used, p_allow_objects); - ERR_FAIL_COND_V(err, err); + ERR_FAIL_COND_V_MSG(err != OK, err, "Error when trying to decode Variant."); buf += used; len -= used; varr.push_back(v); @@ -1066,11 +1061,6 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo r_len += 4 * 4; } break; - /*case Variant::RESOURCE: { - - ERR_EXPLAIN("Can't marshallize resources"); - ERR_FAIL_V(ERR_INVALID_DATA); //no, i'm sorry, no go - } break;*/ case Variant::_RID: { } break; diff --git a/core/io/multiplayer_api.cpp b/core/io/multiplayer_api.cpp index 33dc4dbde4..0ba84d0c8f 100644 --- a/core/io/multiplayer_api.cpp +++ b/core/io/multiplayer_api.cpp @@ -33,6 +33,10 @@ #include "core/io/marshalls.h" #include "scene/main/node.h" +#ifdef DEBUG_ENABLED +#include "core/os/os.h" +#endif + _FORCE_INLINE_ bool _should_call_local(MultiplayerAPI::RPCMode mode, bool is_master, bool &r_skip_rpc) { switch (mode) { @@ -146,8 +150,7 @@ void MultiplayerAPI::set_network_peer(const Ref<NetworkedMultiplayerPeer> &p_pee network_peer = p_peer; - ERR_EXPLAIN("Supplied NetworkedNetworkPeer must be connecting or connected."); - ERR_FAIL_COND(p_peer.is_valid() && p_peer->get_connection_status() == NetworkedMultiplayerPeer::CONNECTION_DISCONNECTED); + ERR_FAIL_COND_MSG(p_peer.is_valid() && p_peer->get_connection_status() == NetworkedMultiplayerPeer::CONNECTION_DISCONNECTED, "Supplied NetworkedNetworkPeer must be connecting or connected."); if (network_peer.is_valid()) { network_peer->connect("peer_connected", this, "_add_peer"); @@ -164,10 +167,16 @@ Ref<NetworkedMultiplayerPeer> MultiplayerAPI::get_network_peer() const { void MultiplayerAPI::_process_packet(int p_from, const uint8_t *p_packet, int p_packet_len) { - ERR_EXPLAIN("Multiplayer root node was not initialized. If you are using custom multiplayer, remember to set the root node via MultiplayerAPI.set_root_node before using it"); - ERR_FAIL_COND(root_node == NULL); - ERR_EXPLAIN("Invalid packet received. Size too small."); - ERR_FAIL_COND(p_packet_len < 1); + ERR_FAIL_COND_MSG(root_node == NULL, "Multiplayer root node was not initialized. If you are using custom multiplayer, remember to set the root node via MultiplayerAPI.set_root_node before using it."); + ERR_FAIL_COND_MSG(p_packet_len < 1, "Invalid packet received. Size too small."); + +#ifdef DEBUG_ENABLED + if (profiling) { + bandwidth_incoming_data.write[bandwidth_incoming_pointer].timestamp = OS::get_singleton()->get_ticks_msec(); + bandwidth_incoming_data.write[bandwidth_incoming_pointer].packet_size = p_packet_len; + bandwidth_incoming_pointer = (bandwidth_incoming_pointer + 1) % bandwidth_incoming_data.size(); + } +#endif uint8_t packet_type = p_packet[0]; @@ -186,13 +195,11 @@ void MultiplayerAPI::_process_packet(int p_from, const uint8_t *p_packet, int p_ case NETWORK_COMMAND_REMOTE_CALL: case NETWORK_COMMAND_REMOTE_SET: { - ERR_EXPLAIN("Invalid packet received. Size too small."); - ERR_FAIL_COND(p_packet_len < 6); + ERR_FAIL_COND_MSG(p_packet_len < 6, "Invalid packet received. Size too small."); Node *node = _process_get_node(p_from, p_packet, p_packet_len); - ERR_EXPLAIN("Invalid packet received. Requested node was not found."); - ERR_FAIL_COND(node == NULL); + ERR_FAIL_COND_MSG(node == NULL, "Invalid packet received. Requested node was not found."); // Detect cstring end. int len_end = 5; @@ -202,8 +209,7 @@ void MultiplayerAPI::_process_packet(int p_from, const uint8_t *p_packet, int p_ } } - ERR_EXPLAIN("Invalid packet received. Size too small."); - ERR_FAIL_COND(len_end >= p_packet_len); + ERR_FAIL_COND_MSG(len_end >= p_packet_len, "Invalid packet received. Size too small."); StringName name = String::utf8((const char *)&p_packet[5]); @@ -235,8 +241,7 @@ Node *MultiplayerAPI::_process_get_node(int p_from, const uint8_t *p_packet, int int ofs = target & 0x7FFFFFFF; - ERR_EXPLAIN("Invalid packet received. Size smaller than declared."); - ERR_FAIL_COND_V(ofs >= p_packet_len, NULL); + ERR_FAIL_COND_V_MSG(ofs >= p_packet_len, NULL, "Invalid packet received. Size smaller than declared."); String paths; paths.parse_utf8((const char *)&p_packet[ofs], p_packet_len - ofs); @@ -246,33 +251,30 @@ Node *MultiplayerAPI::_process_get_node(int p_from, const uint8_t *p_packet, int node = root_node->get_node(np); if (!node) - ERR_PRINTS("Failed to get path from RPC: " + String(np)); + ERR_PRINTS("Failed to get path from RPC: " + String(np) + "."); } else { // Use cached path. int id = target; Map<int, PathGetCache>::Element *E = path_get_cache.find(p_from); - ERR_EXPLAIN("Invalid packet received. Requests invalid peer cache."); - ERR_FAIL_COND_V(!E, NULL); + ERR_FAIL_COND_V_MSG(!E, NULL, "Invalid packet received. Requests invalid peer cache."); Map<int, PathGetCache::NodeInfo>::Element *F = E->get().nodes.find(id); - ERR_EXPLAIN("Invalid packet received. Unabled to find requested cached node."); - ERR_FAIL_COND_V(!F, NULL); + ERR_FAIL_COND_V_MSG(!F, NULL, "Invalid packet received. Unabled to find requested cached node."); PathGetCache::NodeInfo *ni = &F->get(); // Do proper caching later. node = root_node->get_node(ni->path); if (!node) - ERR_PRINTS("Failed to get cached path from RPC: " + String(ni->path)); + ERR_PRINTS("Failed to get cached path from RPC: " + String(ni->path) + "."); } return node; } void MultiplayerAPI::_process_rpc(Node *p_node, const StringName &p_name, int p_from, const uint8_t *p_packet, int p_packet_len, int p_offset) { - ERR_EXPLAIN("Invalid packet received. Size too small."); - ERR_FAIL_COND(p_offset >= p_packet_len); + ERR_FAIL_COND_MSG(p_offset >= p_packet_len, "Invalid packet received. Size too small."); // Check that remote can call the RPC on this node. RPCMode rpc_mode = RPC_MODE_DISABLED; @@ -284,8 +286,7 @@ void MultiplayerAPI::_process_rpc(Node *p_node, const StringName &p_name, int p_ } bool can_call = _can_call_mode(p_node, rpc_mode, p_from); - ERR_EXPLAIN("RPC '" + String(p_name) + "' is not allowed on node " + p_node->get_path() + " from: " + itos(p_from) + ". Mode is " + itos((int)rpc_mode) + ", master is " + itos(p_node->get_network_master()) + "."); - ERR_FAIL_COND(!can_call); + ERR_FAIL_COND_MSG(!can_call, "RPC '" + String(p_name) + "' is not allowed on node " + p_node->get_path() + " from: " + itos(p_from) + ". Mode is " + itos((int)rpc_mode) + ", master is " + itos(p_node->get_network_master()) + "."); int argc = p_packet[p_offset]; Vector<Variant> args; @@ -295,15 +296,21 @@ void MultiplayerAPI::_process_rpc(Node *p_node, const StringName &p_name, int p_ p_offset++; +#ifdef DEBUG_ENABLED + if (profiling) { + ObjectID id = p_node->get_instance_id(); + _init_node_profile(id); + profiler_frame_data[id].incoming_rpc += 1; + } +#endif + for (int i = 0; i < argc; i++) { - ERR_EXPLAIN("Invalid packet received. Size too small."); - ERR_FAIL_COND(p_offset >= p_packet_len); + ERR_FAIL_COND_MSG(p_offset >= p_packet_len, "Invalid packet received. Size too small."); int vlen; Error err = decode_variant(args.write[i], &p_packet[p_offset], p_packet_len - p_offset, &vlen, allow_object_decoding || network_peer->is_object_decoding_allowed()); - ERR_EXPLAIN("Invalid packet received. Unable to decode RPC argument."); - ERR_FAIL_COND(err != OK); + ERR_FAIL_COND_MSG(err != OK, "Invalid packet received. Unable to decode RPC argument."); argp.write[i] = &args[i]; p_offset += vlen; @@ -321,8 +328,7 @@ void MultiplayerAPI::_process_rpc(Node *p_node, const StringName &p_name, int p_ void MultiplayerAPI::_process_rset(Node *p_node, const StringName &p_name, int p_from, const uint8_t *p_packet, int p_packet_len, int p_offset) { - ERR_EXPLAIN("Invalid packet received. Size too small."); - ERR_FAIL_COND(p_offset >= p_packet_len); + ERR_FAIL_COND_MSG(p_offset >= p_packet_len, "Invalid packet received. Size too small."); // Check that remote can call the RSET on this node. RPCMode rset_mode = RPC_MODE_DISABLED; @@ -334,28 +340,33 @@ void MultiplayerAPI::_process_rset(Node *p_node, const StringName &p_name, int p } bool can_call = _can_call_mode(p_node, rset_mode, p_from); - ERR_EXPLAIN("RSET '" + String(p_name) + "' is not allowed on node " + p_node->get_path() + " from: " + itos(p_from) + ". Mode is " + itos((int)rset_mode) + ", master is " + itos(p_node->get_network_master()) + "."); - ERR_FAIL_COND(!can_call); + ERR_FAIL_COND_MSG(!can_call, "RSET '" + String(p_name) + "' is not allowed on node " + p_node->get_path() + " from: " + itos(p_from) + ". Mode is " + itos((int)rset_mode) + ", master is " + itos(p_node->get_network_master()) + "."); + +#ifdef DEBUG_ENABLED + if (profiling) { + ObjectID id = p_node->get_instance_id(); + _init_node_profile(id); + profiler_frame_data[id].incoming_rset += 1; + } +#endif Variant value; Error err = decode_variant(value, &p_packet[p_offset], p_packet_len - p_offset, NULL, allow_object_decoding || network_peer->is_object_decoding_allowed()); - ERR_EXPLAIN("Invalid packet received. Unable to decode RSET value."); - ERR_FAIL_COND(err != OK); + ERR_FAIL_COND_MSG(err != OK, "Invalid packet received. Unable to decode RSET value."); bool valid; p_node->set(p_name, value, &valid); if (!valid) { - String error = "Error setting remote property '" + String(p_name) + "', not found in object of type " + p_node->get_class(); + String error = "Error setting remote property '" + String(p_name) + "', not found in object of type " + p_node->get_class() + "."; ERR_PRINTS(error); } } void MultiplayerAPI::_process_simplify_path(int p_from, const uint8_t *p_packet, int p_packet_len) { - ERR_EXPLAIN("Invalid packet received. Size too small."); - ERR_FAIL_COND(p_packet_len < 5); + ERR_FAIL_COND_MSG(p_packet_len < 5, "Invalid packet received. Size too small."); int id = decode_uint32(&p_packet[1]); String paths; @@ -390,8 +401,7 @@ void MultiplayerAPI::_process_simplify_path(int p_from, const uint8_t *p_packet, void MultiplayerAPI::_process_confirm_path(int p_from, const uint8_t *p_packet, int p_packet_len) { - ERR_EXPLAIN("Invalid packet received. Size too small."); - ERR_FAIL_COND(p_packet_len < 2); + ERR_FAIL_COND_MSG(p_packet_len < 2, "Invalid packet received. Size too small."); String paths; paths.parse_utf8((const char *)&p_packet[1], p_packet_len - 1); @@ -399,12 +409,10 @@ void MultiplayerAPI::_process_confirm_path(int p_from, const uint8_t *p_packet, NodePath path = paths; PathSentCache *psc = path_send_cache.getptr(path); - ERR_EXPLAIN("Invalid packet received. Tries to confirm a path which was not found in cache."); - ERR_FAIL_COND(!psc); + ERR_FAIL_COND_MSG(!psc, "Invalid packet received. Tries to confirm a path which was not found in cache."); Map<int, bool>::Element *E = psc->confirmed_peers.find(p_from); - ERR_EXPLAIN("Invalid packet received. Source peer was not found in cache for the given path."); - ERR_FAIL_COND(!E); + ERR_FAIL_COND_MSG(!E, "Invalid packet received. Source peer was not found in cache for the given path."); E->get() = true; } @@ -460,39 +468,22 @@ bool MultiplayerAPI::_send_confirm_path(NodePath p_path, PathSentCache *psc, int void MultiplayerAPI::_send_rpc(Node *p_from, int p_to, bool p_unreliable, bool p_set, const StringName &p_name, const Variant **p_arg, int p_argcount) { - if (network_peer.is_null()) { - ERR_EXPLAIN("Attempt to remote call/set when networking is not active in SceneTree."); - ERR_FAIL(); - } + ERR_FAIL_COND_MSG(network_peer.is_null(), "Attempt to remote call/set when networking is not active in SceneTree."); - if (network_peer->get_connection_status() == NetworkedMultiplayerPeer::CONNECTION_CONNECTING) { - ERR_EXPLAIN("Attempt to remote call/set when networking is not connected yet in SceneTree."); - ERR_FAIL(); - } + ERR_FAIL_COND_MSG(network_peer->get_connection_status() == NetworkedMultiplayerPeer::CONNECTION_CONNECTING, "Attempt to remote call/set when networking is not connected yet in SceneTree."); - if (network_peer->get_connection_status() == NetworkedMultiplayerPeer::CONNECTION_DISCONNECTED) { - ERR_EXPLAIN("Attempt to remote call/set when networking is disconnected."); - ERR_FAIL(); - } + ERR_FAIL_COND_MSG(network_peer->get_connection_status() == NetworkedMultiplayerPeer::CONNECTION_DISCONNECTED, "Attempt to remote call/set when networking is disconnected."); - if (p_argcount > 255) { - ERR_EXPLAIN("Too many arguments >255."); - ERR_FAIL(); - } + ERR_FAIL_COND_MSG(p_argcount > 255, "Too many arguments >255."); if (p_to != 0 && !connected_peers.has(ABS(p_to))) { - if (p_to == network_peer->get_unique_id()) { - ERR_EXPLAIN("Attempt to remote call/set yourself! unique ID: " + itos(network_peer->get_unique_id())); - } else { - ERR_EXPLAIN("Attempt to remote call unexisting ID: " + itos(p_to)); - } + ERR_FAIL_COND_MSG(p_to == network_peer->get_unique_id(), "Attempt to remote call/set yourself! unique ID: " + itos(network_peer->get_unique_id()) + "."); - ERR_FAIL(); + ERR_FAIL_MSG("Attempt to remote call unexisting ID: " + itos(p_to) + "."); } NodePath from_path = (root_node->get_path()).rel_path_to(p_from->get_path()); - ERR_EXPLAIN("Unable to send RPC. Relative path is empty. THIS IS LIKELY A BUG IN THE ENGINE!"); - ERR_FAIL_COND(from_path.is_empty()); + ERR_FAIL_COND_MSG(from_path.is_empty(), "Unable to send RPC. Relative path is empty. THIS IS LIKELY A BUG IN THE ENGINE!"); // See if the path is cached. PathSentCache *psc = path_send_cache.getptr(from_path); @@ -530,8 +521,7 @@ void MultiplayerAPI::_send_rpc(Node *p_from, int p_to, bool p_unreliable, bool p if (p_set) { // Set argument. Error err = encode_variant(*p_arg[0], NULL, len, allow_object_decoding || network_peer->is_object_decoding_allowed()); - ERR_EXPLAIN("Unable to encode RSET value. THIS IS LIKELY A BUG IN THE ENGINE!"); - ERR_FAIL_COND(err != OK); + ERR_FAIL_COND_MSG(err != OK, "Unable to encode RSET value. THIS IS LIKELY A BUG IN THE ENGINE!"); MAKE_ROOM(ofs + len); encode_variant(*p_arg[0], &(packet_cache.write[ofs]), len, allow_object_decoding || network_peer->is_object_decoding_allowed()); ofs += len; @@ -543,14 +533,21 @@ void MultiplayerAPI::_send_rpc(Node *p_from, int p_to, bool p_unreliable, bool p ofs += 1; for (int i = 0; i < p_argcount; i++) { Error err = encode_variant(*p_arg[i], NULL, len, allow_object_decoding || network_peer->is_object_decoding_allowed()); - ERR_EXPLAIN("Unable to encode RPC argument. THIS IS LIKELY A BUG IN THE ENGINE!"); - ERR_FAIL_COND(err != OK); + ERR_FAIL_COND_MSG(err != OK, "Unable to encode RPC argument. THIS IS LIKELY A BUG IN THE ENGINE!"); MAKE_ROOM(ofs + len); encode_variant(*p_arg[i], &(packet_cache.write[ofs]), len, allow_object_decoding || network_peer->is_object_decoding_allowed()); ofs += len; } } +#ifdef DEBUG_ENABLED + if (profiling) { + bandwidth_outgoing_data.write[bandwidth_outgoing_pointer].timestamp = OS::get_singleton()->get_ticks_msec(); + bandwidth_outgoing_data.write[bandwidth_outgoing_pointer].packet_size = ofs; + bandwidth_outgoing_pointer = (bandwidth_outgoing_pointer + 1) % bandwidth_outgoing_data.size(); + } +#endif + // See if all peers have cached path (is so, call can be fast). bool has_all_peers = _send_confirm_path(from_path, psc, p_to); @@ -626,12 +623,9 @@ void MultiplayerAPI::_server_disconnected() { void MultiplayerAPI::rpcp(Node *p_node, int p_peer_id, bool p_unreliable, const StringName &p_method, const Variant **p_arg, int p_argcount) { - ERR_EXPLAIN("Trying to call an RPC while no network peer is active."); - ERR_FAIL_COND(!network_peer.is_valid()); - ERR_EXPLAIN("Trying to call an RPC on a node which is not inside SceneTree."); - ERR_FAIL_COND(!p_node->is_inside_tree()); - ERR_EXPLAIN("Trying to call an RPC via a network peer which is not connected."); - ERR_FAIL_COND(network_peer->get_connection_status() != NetworkedMultiplayerPeer::CONNECTION_CONNECTED); + ERR_FAIL_COND_MSG(!network_peer.is_valid(), "Trying to call an RPC while no network peer is active."); + ERR_FAIL_COND_MSG(!p_node->is_inside_tree(), "Trying to call an RPC on a node which is not inside SceneTree."); + ERR_FAIL_COND_MSG(network_peer->get_connection_status() != NetworkedMultiplayerPeer::CONNECTION_CONNECTED, "Trying to call an RPC via a network peer which is not connected."); int node_id = network_peer->get_unique_id(); bool skip_rpc = node_id == p_peer_id; @@ -657,6 +651,15 @@ void MultiplayerAPI::rpcp(Node *p_node, int p_peer_id, bool p_unreliable, const } if (!skip_rpc) { + +#ifdef DEBUG_ENABLED + if (profiling) { + ObjectID id = p_node->get_instance_id(); + _init_node_profile(id); + profiler_frame_data[id].outgoing_rpc += 1; + } +#endif + _send_rpc(p_node, p_peer_id, p_unreliable, false, p_method, p_arg, p_argcount); } @@ -668,7 +671,7 @@ void MultiplayerAPI::rpcp(Node *p_node, int p_peer_id, bool p_unreliable, const rpc_sender_id = temp_id; if (ce.error != Variant::CallError::CALL_OK) { String error = Variant::get_call_error_text(p_node, p_method, p_arg, p_argcount, ce); - error = "rpc() aborted in local call: - " + error; + error = "rpc() aborted in local call: - " + error + "."; ERR_PRINTS(error); return; } @@ -683,24 +686,20 @@ void MultiplayerAPI::rpcp(Node *p_node, int p_peer_id, bool p_unreliable, const rpc_sender_id = temp_id; if (ce.error != Variant::CallError::CALL_OK) { String error = Variant::get_call_error_text(p_node, p_method, p_arg, p_argcount, ce); - error = "rpc() aborted in script local call: - " + error; + error = "rpc() aborted in script local call: - " + error + "."; ERR_PRINTS(error); return; } } - ERR_EXPLAIN("RPC '" + p_method + "' on yourself is not allowed by selected mode"); - ERR_FAIL_COND(skip_rpc && !(call_local_native || call_local_script)); + ERR_FAIL_COND_MSG(skip_rpc && !(call_local_native || call_local_script), "RPC '" + p_method + "' on yourself is not allowed by selected mode."); } void MultiplayerAPI::rsetp(Node *p_node, int p_peer_id, bool p_unreliable, const StringName &p_property, const Variant &p_value) { - ERR_EXPLAIN("Trying to RSET while no network peer is active."); - ERR_FAIL_COND(!network_peer.is_valid()); - ERR_EXPLAIN("Trying to RSET on a node which is not inside SceneTree."); - ERR_FAIL_COND(!p_node->is_inside_tree()); - ERR_EXPLAIN("Trying to send an RSET via a network peer which is not connected."); - ERR_FAIL_COND(network_peer->get_connection_status() != NetworkedMultiplayerPeer::CONNECTION_CONNECTED); + ERR_FAIL_COND_MSG(!network_peer.is_valid(), "Trying to RSET while no network peer is active."); + ERR_FAIL_COND_MSG(!p_node->is_inside_tree(), "Trying to RSET on a node which is not inside SceneTree."); + ERR_FAIL_COND_MSG(network_peer->get_connection_status() != NetworkedMultiplayerPeer::CONNECTION_CONNECTED, "Trying to send an RSET via a network peer which is not connected."); int node_id = network_peer->get_unique_id(); bool is_master = p_node->is_network_master(); @@ -724,7 +723,7 @@ void MultiplayerAPI::rsetp(Node *p_node, int p_peer_id, bool p_unreliable, const rpc_sender_id = temp_id; if (!valid) { - String error = "rset() aborted in local set, property not found: - " + String(p_property); + String error = "rset() aborted in local set, property not found: - " + String(p_property) + "."; ERR_PRINTS(error); return; } @@ -742,7 +741,7 @@ void MultiplayerAPI::rsetp(Node *p_node, int p_peer_id, bool p_unreliable, const rpc_sender_id = temp_id; if (!valid) { - String error = "rset() aborted in local script set, property not found: - " + String(p_property); + String error = "rset() aborted in local script set, property not found: - " + String(p_property) + "."; ERR_PRINTS(error); return; } @@ -751,11 +750,18 @@ void MultiplayerAPI::rsetp(Node *p_node, int p_peer_id, bool p_unreliable, const } if (skip_rset) { - ERR_EXPLAIN("RSET for '" + p_property + "' on yourself is not allowed by selected mode"); - ERR_FAIL_COND(!set_local); + ERR_FAIL_COND_MSG(!set_local, "RSET for '" + p_property + "' on yourself is not allowed by selected mode."); return; } +#ifdef DEBUG_ENABLED + if (profiling) { + ObjectID id = p_node->get_instance_id(); + _init_node_profile(id); + profiler_frame_data[id].outgoing_rset += 1; + } +#endif + const Variant *vptr = &p_value; _send_rpc(p_node, p_peer_id, p_unreliable, true, p_property, &vptr, 1); @@ -763,12 +769,9 @@ void MultiplayerAPI::rsetp(Node *p_node, int p_peer_id, bool p_unreliable, const Error MultiplayerAPI::send_bytes(PoolVector<uint8_t> p_data, int p_to, NetworkedMultiplayerPeer::TransferMode p_mode) { - ERR_EXPLAIN("Trying to send an empty raw packet."); - ERR_FAIL_COND_V(p_data.size() < 1, ERR_INVALID_DATA); - ERR_EXPLAIN("Trying to send a raw packet while no network peer is active."); - ERR_FAIL_COND_V(!network_peer.is_valid(), ERR_UNCONFIGURED); - ERR_EXPLAIN("Trying to send a raw packet via a network peer which is not connected."); - ERR_FAIL_COND_V(network_peer->get_connection_status() != NetworkedMultiplayerPeer::CONNECTION_CONNECTED, ERR_UNCONFIGURED); + ERR_FAIL_COND_V_MSG(p_data.size() < 1, ERR_INVALID_DATA, "Trying to send an empty raw packet."); + ERR_FAIL_COND_V_MSG(!network_peer.is_valid(), ERR_UNCONFIGURED, "Trying to send a raw packet while no network peer is active."); + ERR_FAIL_COND_V_MSG(network_peer->get_connection_status() != NetworkedMultiplayerPeer::CONNECTION_CONNECTED, ERR_UNCONFIGURED, "Trying to send a raw packet via a network peer which is not connected."); MAKE_ROOM(p_data.size() + 1); PoolVector<uint8_t>::Read r = p_data.read(); @@ -783,8 +786,7 @@ Error MultiplayerAPI::send_bytes(PoolVector<uint8_t> p_data, int p_to, Networked void MultiplayerAPI::_process_raw(int p_from, const uint8_t *p_packet, int p_packet_len) { - ERR_EXPLAIN("Invalid packet received. Size too small."); - ERR_FAIL_COND(p_packet_len < 2); + ERR_FAIL_COND_MSG(p_packet_len < 2, "Invalid packet received. Size too small."); PoolVector<uint8_t> out; int len = p_packet_len - 1; @@ -798,37 +800,32 @@ void MultiplayerAPI::_process_raw(int p_from, const uint8_t *p_packet, int p_pac int MultiplayerAPI::get_network_unique_id() const { - ERR_EXPLAIN("No network peer is assigned. Unable to get unique network ID."); - ERR_FAIL_COND_V(!network_peer.is_valid(), 0); + ERR_FAIL_COND_V_MSG(!network_peer.is_valid(), 0, "No network peer is assigned. Unable to get unique network ID."); return network_peer->get_unique_id(); } bool MultiplayerAPI::is_network_server() const { // XXX Maybe fail silently? Maybe should actually return true to make development of both local and online multiplayer easier? - ERR_EXPLAIN("No network peer is assigned. I can't be a server."); - ERR_FAIL_COND_V(!network_peer.is_valid(), false); + ERR_FAIL_COND_V_MSG(!network_peer.is_valid(), false, "No network peer is assigned. I can't be a server."); return network_peer->is_server(); } void MultiplayerAPI::set_refuse_new_network_connections(bool p_refuse) { - ERR_EXPLAIN("No network peer is assigned. Unable to set 'refuse_new_connections'."); - ERR_FAIL_COND(!network_peer.is_valid()); + ERR_FAIL_COND_MSG(!network_peer.is_valid(), "No network peer is assigned. Unable to set 'refuse_new_connections'."); network_peer->set_refuse_new_connections(p_refuse); } bool MultiplayerAPI::is_refusing_new_network_connections() const { - ERR_EXPLAIN("No network peer is assigned. Unable to get 'refuse_new_connections'."); - ERR_FAIL_COND_V(!network_peer.is_valid(), false); + ERR_FAIL_COND_V_MSG(!network_peer.is_valid(), false, "No network peer is assigned. Unable to get 'refuse_new_connections'."); return network_peer->is_refusing_new_connections(); } Vector<int> MultiplayerAPI::get_network_connected_peers() const { - ERR_EXPLAIN("No network peer is assigned. Assume no peers are connected."); - ERR_FAIL_COND_V(!network_peer.is_valid(), Vector<int>()); + ERR_FAIL_COND_V_MSG(!network_peer.is_valid(), Vector<int>(), "No network peer is assigned. Assume no peers are connected."); Vector<int> ret; for (Set<int>::Element *E = connected_peers.front(); E; E = E->next()) { @@ -848,6 +845,95 @@ bool MultiplayerAPI::is_object_decoding_allowed() const { return allow_object_decoding; } +void MultiplayerAPI::profiling_start() { +#ifdef DEBUG_ENABLED + profiling = true; + profiler_frame_data.clear(); + + bandwidth_incoming_pointer = 0; + bandwidth_incoming_data.resize(16384); // ~128kB + for (int i = 0; i < bandwidth_incoming_data.size(); ++i) { + bandwidth_incoming_data.write[i].packet_size = -1; + } + + bandwidth_outgoing_pointer = 0; + bandwidth_outgoing_data.resize(16384); // ~128kB + for (int i = 0; i < bandwidth_outgoing_data.size(); ++i) { + bandwidth_outgoing_data.write[i].packet_size = -1; + } +#endif +} + +void MultiplayerAPI::profiling_end() { +#ifdef DEBUG_ENABLED + profiling = false; + bandwidth_incoming_data.clear(); + bandwidth_outgoing_data.clear(); +#endif +} + +int MultiplayerAPI::get_profiling_frame(ProfilingInfo *r_info) { + int i = 0; +#ifdef DEBUG_ENABLED + for (Map<ObjectID, ProfilingInfo>::Element *E = profiler_frame_data.front(); E; E = E->next()) { + r_info[i] = E->get(); + ++i; + } + profiler_frame_data.clear(); +#endif + return i; +} + +int MultiplayerAPI::get_incoming_bandwidth_usage() { +#ifdef DEBUG_ENABLED + return _get_bandwidth_usage(bandwidth_incoming_data, bandwidth_incoming_pointer); +#else + return 0; +#endif +} + +int MultiplayerAPI::get_outgoing_bandwidth_usage() { +#ifdef DEBUG_ENABLED + return _get_bandwidth_usage(bandwidth_outgoing_data, bandwidth_outgoing_pointer); +#else + return 0; +#endif +} + +#ifdef DEBUG_ENABLED +int MultiplayerAPI::_get_bandwidth_usage(const Vector<BandwidthFrame> &p_buffer, int p_pointer) { + int total_bandwidth = 0; + + uint32_t timestamp = OS::get_singleton()->get_ticks_msec(); + uint32_t final_timestamp = timestamp - 1000; + + int i = (p_pointer + p_buffer.size() - 1) % p_buffer.size(); + + while (i != p_pointer && p_buffer[i].packet_size > 0) { + if (p_buffer[i].timestamp < final_timestamp) { + return total_bandwidth; + } + total_bandwidth += p_buffer[i].packet_size; + i = (i + p_buffer.size() - 1) % p_buffer.size(); + } + + ERR_FAIL_COND_V_MSG(i == p_pointer, total_bandwidth, "Reached the end of the bandwidth profiler buffer, values might be inaccurate."); + return total_bandwidth; +} + +void MultiplayerAPI::_init_node_profile(ObjectID p_node) { + if (profiler_frame_data.has(p_node)) + return; + profiler_frame_data.insert(p_node, ProfilingInfo()); + profiler_frame_data[p_node].node = p_node; + profiler_frame_data[p_node].node_path = Object::cast_to<Node>(ObjectDB::get_instance(p_node))->get_path(); + profiler_frame_data[p_node].incoming_rpc = 0; + profiler_frame_data[p_node].incoming_rset = 0; + profiler_frame_data[p_node].outgoing_rpc = 0; + profiler_frame_data[p_node].outgoing_rset = 0; +} +#endif + void MultiplayerAPI::_bind_methods() { ClassDB::bind_method(D_METHOD("set_root_node", "node"), &MultiplayerAPI::set_root_node); ClassDB::bind_method(D_METHOD("send_bytes", "bytes", "id", "mode"), &MultiplayerAPI::send_bytes, DEFVAL(NetworkedMultiplayerPeer::TARGET_PEER_BROADCAST), DEFVAL(NetworkedMultiplayerPeer::TRANSFER_MODE_RELIABLE)); @@ -898,6 +984,9 @@ MultiplayerAPI::MultiplayerAPI() : allow_object_decoding(false) { rpc_sender_id = 0; root_node = NULL; +#ifdef DEBUG_ENABLED + profiling = false; +#endif clear(); } diff --git a/core/io/multiplayer_api.h b/core/io/multiplayer_api.h index 5258dde5d7..b824456e0f 100644 --- a/core/io/multiplayer_api.h +++ b/core/io/multiplayer_api.h @@ -38,6 +38,16 @@ class MultiplayerAPI : public Reference { GDCLASS(MultiplayerAPI, Reference); +public: + struct ProfilingInfo { + ObjectID node; + String node_path; + int incoming_rpc; + int incoming_rset; + int outgoing_rpc; + int outgoing_rset; + }; + private: //path sent caches struct PathSentCache { @@ -55,6 +65,23 @@ private: Map<int, NodeInfo> nodes; }; +#ifdef DEBUG_ENABLED + struct BandwidthFrame { + uint32_t timestamp; + int packet_size; + }; + + int bandwidth_incoming_pointer; + Vector<BandwidthFrame> bandwidth_incoming_data; + int bandwidth_outgoing_pointer; + Vector<BandwidthFrame> bandwidth_outgoing_data; + Map<ObjectID, ProfilingInfo> profiler_frame_data; + bool profiling; + + void _init_node_profile(ObjectID p_node); + int _get_bandwidth_usage(const Vector<BandwidthFrame> &p_buffer, int p_pointer); +#endif + Ref<NetworkedMultiplayerPeer> network_peer; int rpc_sender_id; Set<int> connected_peers; @@ -130,6 +157,13 @@ public: void set_allow_object_decoding(bool p_enable); bool is_object_decoding_allowed() const; + void profiling_start(); + void profiling_end(); + + int get_profiling_frame(ProfilingInfo *r_info); + int get_incoming_bandwidth_usage(); + int get_outgoing_bandwidth_usage(); + MultiplayerAPI(); ~MultiplayerAPI(); }; diff --git a/core/io/packet_peer.cpp b/core/io/packet_peer.cpp index 1e4ea715b3..821a04ebad 100644 --- a/core/io/packet_peer.cpp +++ b/core/io/packet_peer.cpp @@ -101,9 +101,9 @@ Error PacketPeer::put_var(const Variant &p_packet, bool p_full_objects) { return OK; uint8_t *buf = (uint8_t *)alloca(len); - ERR_FAIL_COND_V(!buf, ERR_OUT_OF_MEMORY); + ERR_FAIL_COND_V_MSG(!buf, ERR_OUT_OF_MEMORY, "Out of memory."); err = encode_variant(p_packet, buf, len, p_full_objects || allow_object_decoding); - ERR_FAIL_COND_V(err, err); + ERR_FAIL_COND_V_MSG(err != OK, err, "Error when trying to encode Variant."); return put_packet(buf, len); } @@ -150,7 +150,7 @@ void PacketPeer::_bind_methods() { void PacketPeerStream::_set_stream_peer(REF p_peer) { - ERR_FAIL_COND(p_peer.is_null()); + ERR_FAIL_COND_MSG(p_peer.is_null(), "It's not a reference to a valid Resource object."); set_stream_peer(p_peer); } @@ -280,8 +280,7 @@ Ref<StreamPeer> PacketPeerStream::get_stream_peer() const { void PacketPeerStream::set_input_buffer_max_size(int p_max_size) { //warning may lose packets - ERR_EXPLAIN("Buffer in use, resizing would cause loss of data"); - ERR_FAIL_COND(ring_buffer.data_left()); + ERR_FAIL_COND_MSG(ring_buffer.data_left(), "Buffer in use, resizing would cause loss of data."); ring_buffer.resize(nearest_shift(p_max_size + 4)); input_buffer.resize(next_power_of_2(p_max_size + 4)); } diff --git a/core/io/pck_packer.cpp b/core/io/pck_packer.cpp index c16d89d695..443f390bb7 100644 --- a/core/io/pck_packer.cpp +++ b/core/io/pck_packer.cpp @@ -64,10 +64,7 @@ Error PCKPacker::pck_start(const String &p_file, int p_alignment) { file = FileAccess::open(p_file, FileAccess::WRITE); - if (!file) { - ERR_EXPLAIN("Can't open file to write: " + String(p_file)); - ERR_FAIL_V(ERR_CANT_CREATE); - } + ERR_FAIL_COND_V_MSG(!file, ERR_CANT_CREATE, "Can't open file to write: " + String(p_file) + "."); alignment = p_alignment; @@ -110,7 +107,7 @@ Error PCKPacker::add_file(const String &p_file, const String &p_src) { Error PCKPacker::flush(bool p_verbose) { - ERR_FAIL_COND_V(!file, ERR_INVALID_PARAMETER); + ERR_FAIL_COND_V_MSG(!file, ERR_INVALID_PARAMETER, "File must be opened before use."); // write the index diff --git a/core/io/resource_format_binary.cpp b/core/io/resource_format_binary.cpp index 146480e5a2..e91dd579b5 100644 --- a/core/io/resource_format_binary.cpp +++ b/core/io/resource_format_binary.cpp @@ -378,10 +378,10 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant &r_v) { for (uint32_t i = 0; i < len; i++) { Variant key; Error err = parse_variant(key); - ERR_FAIL_COND_V(err, ERR_FILE_CORRUPT); + ERR_FAIL_COND_V_MSG(err, ERR_FILE_CORRUPT, "Error when trying to parse Variant."); Variant value; err = parse_variant(value); - ERR_FAIL_COND_V(err, ERR_FILE_CORRUPT); + ERR_FAIL_COND_V_MSG(err, ERR_FILE_CORRUPT, "Error when trying to parse Variant."); d[key] = value; } r_v = d; @@ -395,7 +395,7 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant &r_v) { for (uint32_t i = 0; i < len; i++) { Variant val; Error err = parse_variant(val); - ERR_FAIL_COND_V(err, ERR_FILE_CORRUPT); + ERR_FAIL_COND_V_MSG(err, ERR_FILE_CORRUPT, "Error when trying to parse Variant."); a[i] = val; } r_v = a; @@ -490,8 +490,7 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant &r_v) { #endif } else { - ERR_EXPLAIN("Vector2 size is NOT 8!"); - ERR_FAIL_V(ERR_UNAVAILABLE); + ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "Vector2 size is NOT 8!"); } w.release(); r_v = array; @@ -518,8 +517,7 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant &r_v) { #endif } else { - ERR_EXPLAIN("Vector3 size is NOT 12!"); - ERR_FAIL_V(ERR_UNAVAILABLE); + ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "Vector3 size is NOT 12!"); } w.release(); r_v = array; @@ -546,8 +544,7 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant &r_v) { #endif } else { - ERR_EXPLAIN("Color size is NOT 16!"); - ERR_FAIL_V(ERR_UNAVAILABLE); + ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "Color size is NOT 16!"); } w.release(); r_v = array; @@ -571,7 +568,7 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant &r_v) { const uint32_t current_version = 0; if (format_version > current_version) { - ERR_PRINT("Format version for encoded binary image is too new"); + ERR_PRINT("Format version for encoded binary image is too new."); return ERR_PARSE_ERROR; } @@ -655,8 +652,7 @@ Error ResourceInteractiveLoaderBinary::poll() { } else { error = ERR_FILE_MISSING_DEPENDENCIES; - ERR_EXPLAIN("Can't load dependency: " + path); - ERR_FAIL_V(error); + ERR_FAIL_V_MSG(error, "Can't load dependency: " + path + "."); } } else { @@ -711,16 +707,15 @@ Error ResourceInteractiveLoaderBinary::poll() { Object *obj = ClassDB::instance(t); if (!obj) { error = ERR_FILE_CORRUPT; - ERR_EXPLAIN(local_path + ":Resource of unrecognized type in file: " + t); - ERR_FAIL_V(ERR_FILE_CORRUPT); + ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, local_path + ":Resource of unrecognized type in file: " + t + "."); } Resource *r = Object::cast_to<Resource>(obj); if (!r) { + String obj_class = obj->get_class(); error = ERR_FILE_CORRUPT; - ERR_EXPLAIN(local_path + ":Resource type in resource field not a resource, type is: " + obj->get_class()); memdelete(obj); //bye - ERR_FAIL_V(ERR_FILE_CORRUPT); + ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, local_path + ":Resource type in resource field not a resource, type is: " + obj_class + "."); } RES res = RES(r); @@ -850,8 +845,7 @@ void ResourceInteractiveLoaderBinary::open(FileAccess *p_f) { //not normal error = ERR_FILE_UNRECOGNIZED; - ERR_EXPLAIN("Unrecognized binary resource file: " + local_path); - ERR_FAIL(); + ERR_FAIL_MSG("Unrecognized binary resource file: " + local_path + "."); } bool big_endian = f->get_32(); @@ -877,8 +871,7 @@ void ResourceInteractiveLoaderBinary::open(FileAccess *p_f) { if (ver_format > FORMAT_VERSION || ver_major > VERSION_MAJOR) { f->close(); - ERR_EXPLAIN("File Format '" + itos(FORMAT_VERSION) + "." + itos(ver_major) + "." + itos(ver_minor) + "' is too new! Please upgrade to a new engine version: " + local_path); - ERR_FAIL(); + ERR_FAIL_MSG("File format '" + itos(FORMAT_VERSION) + "." + itos(ver_major) + "." + itos(ver_minor) + "' is too new! Please upgrade to a new engine version: " + local_path + "."); } type = get_unicode_string(); @@ -926,8 +919,7 @@ void ResourceInteractiveLoaderBinary::open(FileAccess *p_f) { if (f->eof_reached()) { error = ERR_FILE_CORRUPT; - ERR_EXPLAIN("Premature End Of File: " + local_path); - ERR_FAIL(); + ERR_FAIL_MSG("Premature end of file (EOF): " + local_path + "."); } } @@ -991,7 +983,7 @@ Ref<ResourceInteractiveLoader> ResourceFormatLoaderBinary::load_interactive(cons Error err; FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err); - ERR_FAIL_COND_V(err != OK, Ref<ResourceInteractiveLoader>()); + ERR_FAIL_COND_V_MSG(err != OK, Ref<ResourceInteractiveLoader>(), "Cannot open file '" + p_path + "'."); Ref<ResourceInteractiveLoaderBinary> ria = memnew(ResourceInteractiveLoaderBinary); String path = p_original_path != "" ? p_original_path : p_path; @@ -1040,7 +1032,7 @@ bool ResourceFormatLoaderBinary::handles_type(const String &p_type) const { void ResourceFormatLoaderBinary::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) { FileAccess *f = FileAccess::open(p_path, FileAccess::READ); - ERR_FAIL_COND(!f); + ERR_FAIL_COND_MSG(!f, "Cannot open file '" + p_path + "'."); Ref<ResourceInteractiveLoaderBinary> ria = memnew(ResourceInteractiveLoaderBinary); ria->local_path = ProjectSettings::get_singleton()->localize_path(p_path); @@ -1054,7 +1046,7 @@ Error ResourceFormatLoaderBinary::rename_dependencies(const String &p_path, cons //Error error=OK; FileAccess *f = FileAccess::open(p_path, FileAccess::READ); - ERR_FAIL_COND_V(!f, ERR_CANT_OPEN); + ERR_FAIL_COND_V_MSG(!f, ERR_CANT_OPEN, "Cannot open file '" + p_path + "'."); FileAccess *fw = NULL; //=FileAccess::open(p_path+".depren"); @@ -1074,7 +1066,7 @@ Error ResourceFormatLoaderBinary::rename_dependencies(const String &p_path, cons if (err) { memdelete(fac); memdelete(facw); - ERR_FAIL_COND_V(err, ERR_FILE_CORRUPT); + ERR_FAIL_COND_V_MSG(err, ERR_FILE_CORRUPT, "Cannot create file '" + p_path + ".depren'."); } fw = facw; @@ -1084,14 +1076,13 @@ Error ResourceFormatLoaderBinary::rename_dependencies(const String &p_path, cons //error=ERR_FILE_UNRECOGNIZED; memdelete(f); - ERR_EXPLAIN("Unrecognized binary resource file: " + local_path); - ERR_FAIL_V(ERR_FILE_UNRECOGNIZED); + ERR_FAIL_V_MSG(ERR_FILE_UNRECOGNIZED, "Unrecognized binary resource file '" + local_path + "'."); } else { fw = FileAccess::open(p_path + ".depren", FileAccess::WRITE); if (!fw) { memdelete(f); } - ERR_FAIL_COND_V(!fw, ERR_CANT_CREATE); + ERR_FAIL_COND_V_MSG(!fw, ERR_CANT_CREATE, "Cannot create file '" + p_path + ".depren'."); uint8_t magic[4] = { 'R', 'S', 'R', 'C' }; fw->store_buffer(magic, 4); @@ -1122,12 +1113,12 @@ Error ResourceFormatLoaderBinary::rename_dependencies(const String &p_path, cons memdelete(da); //use the old approach - WARN_PRINT(("This file is old, so it can't refactor dependencies, opening and resaving: " + p_path).utf8().get_data()); + WARN_PRINTS("This file is old, so it can't refactor dependencies, opening and resaving '" + p_path + "'."); Error err; f = FileAccess::open(p_path, FileAccess::READ, &err); - ERR_FAIL_COND_V(err != OK, ERR_FILE_CANT_OPEN); + ERR_FAIL_COND_V_MSG(err != OK, ERR_FILE_CANT_OPEN, "Cannot open file '" + p_path + "'."); Ref<ResourceInteractiveLoaderBinary> ria = memnew(ResourceInteractiveLoaderBinary); ria->local_path = ProjectSettings::get_singleton()->localize_path(p_path); @@ -1153,8 +1144,7 @@ Error ResourceFormatLoaderBinary::rename_dependencies(const String &p_path, cons memdelete(f); memdelete(fw); - ERR_EXPLAIN("File Format '" + itos(FORMAT_VERSION) + "." + itos(ver_major) + "." + itos(ver_minor) + "' is too new! Please upgrade to a new engine version: " + local_path); - ERR_FAIL_V(ERR_FILE_UNRECOGNIZED); + ERR_FAIL_V_MSG(ERR_FILE_UNRECOGNIZED, "File format '" + itos(FORMAT_VERSION) + "." + itos(ver_major) + "." + itos(ver_minor) + "' is too new! Please upgrade to a new engine version: " + local_path + "."); } // Since we're not actually converting the file contents, leave the version @@ -1477,7 +1467,7 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia case Variant::_RID: { f->store_32(VARIANT_RID); - WARN_PRINT("Can't save RIDs"); + WARN_PRINT("Can't save RIDs."); RID val = p_property; f->store_32(val.get_id()); } break; @@ -1497,8 +1487,7 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia if (!resource_set.has(res)) { f->store_32(OBJECT_EMPTY); - ERR_EXPLAIN("Resource was not pre cached for the resource section, most likely due to circular refedence."); - ERR_FAIL(); + ERR_FAIL_MSG("Resource was not pre cached for the resource section, most likely due to circular reference."); } f->store_32(OBJECT_INTERNAL_RESOURCE); @@ -1629,8 +1618,7 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia } break; default: { - ERR_EXPLAIN("Invalid variant"); - ERR_FAIL(); + ERR_FAIL_MSG("Invalid variant."); } } } @@ -1763,7 +1751,7 @@ Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const RES &p f = FileAccess::open(p_path, FileAccess::WRITE, &err); } - ERR_FAIL_COND_V(err, err); + ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot create file '" + p_path + "'."); relative_paths = p_flags & ResourceSaver::FLAG_RELATIVE_PATHS; skip_editor = p_flags & ResourceSaver::FLAG_OMIT_EDITOR_PROPERTIES; @@ -1798,6 +1786,7 @@ Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const RES &p if (f->get_error() != OK && f->get_error() != ERR_FILE_EOF) { f->close(); + memdelete(f); return ERR_CANT_CREATE; } @@ -1950,10 +1939,12 @@ Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const RES &p if (f->get_error() != OK && f->get_error() != ERR_FILE_EOF) { f->close(); + memdelete(f); return ERR_CANT_CREATE; } f->close(); + memdelete(f); return OK; } diff --git a/core/io/resource_loader.cpp b/core/io/resource_loader.cpp index a29b9d1ddb..f3eba44973 100644 --- a/core/io/resource_loader.cpp +++ b/core/io/resource_loader.cpp @@ -205,7 +205,7 @@ RES ResourceFormatLoader::load(const String &p_path, const String &p_original_pa if (r_error) *r_error = err; - ERR_FAIL_COND_V(err != OK, RES()); + ERR_FAIL_COND_V_MSG(err != OK, RES(), "Failed to load resource '" + p_path + "'."); } } @@ -275,12 +275,9 @@ RES ResourceLoader::_load(const String &p_path, const String &p_original_path, c return res; } - if (found) { - ERR_EXPLAIN("Failed loading resource: " + p_path); - } else { - ERR_EXPLAIN("No loader found for resource: " + p_path); - } - ERR_FAIL_V(RES()); + ERR_FAIL_COND_V_MSG(found, RES(), "Failed loading resource: " + p_path + "."); + + ERR_FAIL_V_MSG(RES(), "No loader found for resource: " + p_path + "."); } bool ResourceLoader::_add_to_loading_map(const String &p_path) { @@ -355,10 +352,7 @@ RES ResourceLoader::load(const String &p_path, const String &p_type_hint, bool p { bool success = _add_to_loading_map(local_path); - if (!success) { - ERR_EXPLAIN("Resource: '" + local_path + "' is already being loaded. Cyclic reference?"); - ERR_FAIL_V(RES()); - } + ERR_FAIL_COND_V_MSG(!success, RES(), "Resource: '" + local_path + "' is already being loaded. Cyclic reference?"); } //lock first if possible @@ -395,8 +389,7 @@ RES ResourceLoader::load(const String &p_path, const String &p_type_hint, bool p if (!p_no_cache) { _remove_from_loading_map(local_path); } - ERR_EXPLAIN("Remapping '" + local_path + "'failed."); - ERR_FAIL_V(RES()); + ERR_FAIL_V_MSG(RES(), "Remapping '" + local_path + "' failed."); } print_verbose("Loading resource: " + path); @@ -479,10 +472,7 @@ Ref<ResourceInteractiveLoader> ResourceLoader::load_interactive(const String &p_ if (!p_no_cache) { bool success = _add_to_loading_map(local_path); - if (!success) { - ERR_EXPLAIN("Resource: '" + local_path + "' is already being loaded. Cyclic reference?"); - ERR_FAIL_V(RES()); - } + ERR_FAIL_COND_V_MSG(!success, RES(), "Resource: '" + local_path + "' is already being loaded. Cyclic reference?"); if (ResourceCache::has(local_path)) { @@ -503,8 +493,7 @@ Ref<ResourceInteractiveLoader> ResourceLoader::load_interactive(const String &p_ if (!p_no_cache) { _remove_from_loading_map(local_path); } - ERR_EXPLAIN("Remapping '" + local_path + "'failed."); - ERR_FAIL_V(RES()); + ERR_FAIL_V_MSG(RES(), "Remapping '" + local_path + "' failed."); } print_verbose("Loading resource: " + path); @@ -534,12 +523,9 @@ Ref<ResourceInteractiveLoader> ResourceLoader::load_interactive(const String &p_ _remove_from_loading_map(local_path); } - if (found) { - ERR_EXPLAIN("Failed loading resource: " + path); - } else { - ERR_EXPLAIN("No loader found for resource: " + path); - } - ERR_FAIL_V(Ref<ResourceInteractiveLoader>()); + ERR_FAIL_COND_V_MSG(found, Ref<ResourceInteractiveLoader>(), "Failed loading resource: " + path + "."); + + ERR_FAIL_V_MSG(Ref<ResourceInteractiveLoader>(), "No loader found for resource: " + path + "."); } void ResourceLoader::add_resource_format_loader(Ref<ResourceFormatLoader> p_format_loader, bool p_at_front) { @@ -801,7 +787,7 @@ String ResourceLoader::_path_remap(const String &p_path, bool *r_translation_rem if (err == ERR_FILE_EOF) { break; } else if (err != OK) { - ERR_PRINTS("Parse error: " + p_path + ".remap:" + itos(lines) + " error: " + error_text); + ERR_PRINTS("Parse error: " + p_path + ".remap:" + itos(lines) + " error: " + error_text + "."); break; } @@ -932,16 +918,13 @@ bool ResourceLoader::add_custom_resource_format_loader(String script_path) { Ref<Script> s = res; StringName ibt = s->get_instance_base_type(); bool valid_type = ClassDB::is_parent_class(ibt, "ResourceFormatLoader"); - ERR_EXPLAIN("Script does not inherit a CustomResourceLoader: " + script_path); - ERR_FAIL_COND_V(!valid_type, false); + ERR_FAIL_COND_V_MSG(!valid_type, false, "Script does not inherit a CustomResourceLoader: " + script_path + "."); Object *obj = ClassDB::instance(ibt); - ERR_EXPLAIN("Cannot instance script as custom resource loader, expected 'ResourceFormatLoader' inheritance, got: " + String(ibt)); - ERR_FAIL_COND_V(obj == NULL, false); + ERR_FAIL_COND_V_MSG(obj == NULL, false, "Cannot instance script as custom resource loader, expected 'ResourceFormatLoader' inheritance, got: " + String(ibt) + "."); - ResourceFormatLoader *crl = NULL; - crl = Object::cast_to<ResourceFormatLoader>(obj); + ResourceFormatLoader *crl = Object::cast_to<ResourceFormatLoader>(obj); crl->set_script(s.get_ref_ptr()); ResourceLoader::add_resource_format_loader(crl); diff --git a/core/io/resource_loader.h b/core/io/resource_loader.h index 70e7bdc224..93df8cadb0 100644 --- a/core/io/resource_loader.h +++ b/core/io/resource_loader.h @@ -33,9 +33,6 @@ #include "core/os/thread.h" #include "core/resource.h" -/** - @author Juan Linietsky <reduzio@gmail.com> -*/ class ResourceInteractiveLoader : public Reference { diff --git a/core/io/resource_saver.cpp b/core/io/resource_saver.cpp index e2c1c3402a..7aa8732366 100644 --- a/core/io/resource_saver.cpp +++ b/core/io/resource_saver.cpp @@ -158,7 +158,7 @@ void ResourceSaver::get_recognized_extensions(const RES &p_resource, List<String void ResourceSaver::add_resource_format_saver(Ref<ResourceFormatSaver> p_format_saver, bool p_at_front) { - ERR_FAIL_COND(p_format_saver.is_null()); + ERR_FAIL_COND_MSG(p_format_saver.is_null(), "It's not a reference to a valid ResourceFormatSaver object."); ERR_FAIL_COND(saver_count >= MAX_SAVERS); if (p_at_front) { @@ -174,7 +174,7 @@ void ResourceSaver::add_resource_format_saver(Ref<ResourceFormatSaver> p_format_ void ResourceSaver::remove_resource_format_saver(Ref<ResourceFormatSaver> p_format_saver) { - ERR_FAIL_COND(p_format_saver.is_null()); + ERR_FAIL_COND_MSG(p_format_saver.is_null(), "It's not a reference to a valid ResourceFormatSaver object."); // Find saver int i = 0; @@ -214,16 +214,13 @@ bool ResourceSaver::add_custom_resource_format_saver(String script_path) { Ref<Script> s = res; StringName ibt = s->get_instance_base_type(); bool valid_type = ClassDB::is_parent_class(ibt, "ResourceFormatSaver"); - ERR_EXPLAIN("Script does not inherit a CustomResourceSaver: " + script_path); - ERR_FAIL_COND_V(!valid_type, false); + ERR_FAIL_COND_V_MSG(!valid_type, false, "Script does not inherit a CustomResourceSaver: " + script_path + "."); Object *obj = ClassDB::instance(ibt); - ERR_EXPLAIN("Cannot instance script as custom resource saver, expected 'ResourceFormatSaver' inheritance, got: " + String(ibt)); - ERR_FAIL_COND_V(obj == NULL, false); + ERR_FAIL_COND_V_MSG(obj == NULL, false, "Cannot instance script as custom resource saver, expected 'ResourceFormatSaver' inheritance, got: " + String(ibt) + "."); - ResourceFormatSaver *crl = NULL; - crl = Object::cast_to<ResourceFormatSaver>(obj); + ResourceFormatSaver *crl = Object::cast_to<ResourceFormatSaver>(obj); crl->set_script(s.get_ref_ptr()); ResourceSaver::add_resource_format_saver(crl); diff --git a/core/io/resource_saver.h b/core/io/resource_saver.h index 0fba47a5e8..20e05d827a 100644 --- a/core/io/resource_saver.h +++ b/core/io/resource_saver.h @@ -33,10 +33,6 @@ #include "core/resource.h" -/** - @author Juan Linietsky <reduzio@gmail.com> -*/ - class ResourceFormatSaver : public Reference { GDCLASS(ResourceFormatSaver, Reference); diff --git a/core/io/stream_peer.cpp b/core/io/stream_peer.cpp index 84b8554d54..f19e055b64 100644 --- a/core/io/stream_peer.cpp +++ b/core/io/stream_peer.cpp @@ -370,7 +370,7 @@ Variant StreamPeer::get_var(bool p_allow_objects) { Variant ret; err = decode_variant(ret, var.ptr(), len, NULL, p_allow_objects); - ERR_FAIL_COND_V(err != OK, Variant()); + ERR_FAIL_COND_V_MSG(err != OK, Variant(), "Error when trying to decode Variant."); return ret; } diff --git a/core/io/stream_peer_ssl.cpp b/core/io/stream_peer_ssl.cpp index ccce48ccd7..f2eaf57acc 100644 --- a/core/io/stream_peer_ssl.cpp +++ b/core/io/stream_peer_ssl.cpp @@ -30,10 +30,7 @@ #include "stream_peer_ssl.h" -#include "core/io/certs_compressed.gen.h" -#include "core/io/compression.h" -#include "core/os/file_access.h" -#include "core/project_settings.h" +#include "core/engine.h" StreamPeerSSL *(*StreamPeerSSL::_create)() = NULL; @@ -44,22 +41,8 @@ StreamPeerSSL *StreamPeerSSL::create() { return NULL; } -StreamPeerSSL::LoadCertsFromMemory StreamPeerSSL::load_certs_func = NULL; bool StreamPeerSSL::available = false; -void StreamPeerSSL::load_certs_from_memory(const PoolByteArray &p_memory) { - if (load_certs_func) - load_certs_func(p_memory); -} - -void StreamPeerSSL::load_certs_from_file(String p_path) { - if (p_path != "") { - PoolByteArray certs = get_cert_file_as_array(p_path); - if (certs.size() > 0) - load_certs_func(certs); - } -} - bool StreamPeerSSL::is_available() { return available; } @@ -72,56 +55,11 @@ bool StreamPeerSSL::is_blocking_handshake_enabled() const { return blocking_handshake; } -PoolByteArray StreamPeerSSL::get_cert_file_as_array(String p_path) { - - PoolByteArray out; - FileAccess *f = FileAccess::open(p_path, FileAccess::READ); - if (f) { - int flen = f->get_len(); - out.resize(flen + 1); - PoolByteArray::Write w = out.write(); - f->get_buffer(w.ptr(), flen); - w[flen] = 0; // Make sure it ends with string terminator - memdelete(f); -#ifdef DEBUG_ENABLED - print_verbose(vformat("Loaded certs from '%s'.", p_path)); -#endif - } - - return out; -} - -PoolByteArray StreamPeerSSL::get_project_cert_array() { - - PoolByteArray out; - String certs_path = GLOBAL_DEF("network/ssl/certificates", ""); - ProjectSettings::get_singleton()->set_custom_property_info("network/ssl/certificates", PropertyInfo(Variant::STRING, "network/ssl/certificates", PROPERTY_HINT_FILE, "*.crt")); - - if (certs_path != "") { - // Use certs defined in project settings. - return get_cert_file_as_array(certs_path); - } -#ifdef BUILTIN_CERTS_ENABLED - else { - // Use builtin certs only if user did not override it in project settings. - out.resize(_certs_uncompressed_size + 1); - PoolByteArray::Write w = out.write(); - Compression::decompress(w.ptr(), _certs_uncompressed_size, _certs_compressed, _certs_compressed_size, Compression::MODE_DEFLATE); - w[_certs_uncompressed_size] = 0; // Make sure it ends with string terminator -#ifdef DEBUG_ENABLED - print_verbose("Loaded builtin certs"); -#endif - } -#endif - - return out; -} - void StreamPeerSSL::_bind_methods() { ClassDB::bind_method(D_METHOD("poll"), &StreamPeerSSL::poll); - ClassDB::bind_method(D_METHOD("accept_stream", "base"), &StreamPeerSSL::accept_stream); - ClassDB::bind_method(D_METHOD("connect_to_stream", "stream", "validate_certs", "for_hostname"), &StreamPeerSSL::connect_to_stream, DEFVAL(false), DEFVAL(String())); + ClassDB::bind_method(D_METHOD("accept_stream", "stream", "private_key", "certificate", "chain"), &StreamPeerSSL::accept_stream, DEFVAL(Ref<X509Certificate>())); + ClassDB::bind_method(D_METHOD("connect_to_stream", "stream", "validate_certs", "for_hostname", "valid_certificate"), &StreamPeerSSL::connect_to_stream, DEFVAL(false), DEFVAL(String()), DEFVAL(Ref<X509Certificate>())); ClassDB::bind_method(D_METHOD("get_status"), &StreamPeerSSL::get_status); ClassDB::bind_method(D_METHOD("disconnect_from_stream"), &StreamPeerSSL::disconnect_from_stream); ClassDB::bind_method(D_METHOD("set_blocking_handshake_enabled", "enabled"), &StreamPeerSSL::set_blocking_handshake_enabled); diff --git a/core/io/stream_peer_ssl.h b/core/io/stream_peer_ssl.h index 482576c4c6..dedc35b9ac 100644 --- a/core/io/stream_peer_ssl.h +++ b/core/io/stream_peer_ssl.h @@ -31,19 +31,16 @@ #ifndef STREAM_PEER_SSL_H #define STREAM_PEER_SSL_H +#include "core/crypto/crypto.h" #include "core/io/stream_peer.h" class StreamPeerSSL : public StreamPeer { GDCLASS(StreamPeerSSL, StreamPeer); -public: - typedef void (*LoadCertsFromMemory)(const PoolByteArray &p_certs); - protected: static StreamPeerSSL *(*_create)(); static void _bind_methods(); - static LoadCertsFromMemory load_certs_func; static bool available; bool blocking_handshake; @@ -61,18 +58,14 @@ public: bool is_blocking_handshake_enabled() const; virtual void poll() = 0; - virtual Error accept_stream(Ref<StreamPeer> p_base) = 0; - virtual Error connect_to_stream(Ref<StreamPeer> p_base, bool p_validate_certs = false, const String &p_for_hostname = String()) = 0; + virtual Error accept_stream(Ref<StreamPeer> p_base, Ref<CryptoKey> p_key, Ref<X509Certificate> p_cert, Ref<X509Certificate> p_ca_chain = Ref<X509Certificate>()) = 0; + virtual Error connect_to_stream(Ref<StreamPeer> p_base, bool p_validate_certs = false, const String &p_for_hostname = String(), Ref<X509Certificate> p_valid_cert = Ref<X509Certificate>()) = 0; virtual Status get_status() const = 0; virtual void disconnect_from_stream() = 0; static StreamPeerSSL *create(); - static PoolByteArray get_cert_file_as_array(String p_path); - static PoolByteArray get_project_cert_array(); - static void load_certs_from_file(String p_path); - static void load_certs_from_memory(const PoolByteArray &p_memory); static bool is_available(); StreamPeerSSL(); diff --git a/core/io/stream_peer_tcp.cpp b/core/io/stream_peer_tcp.cpp index 310bb12bc0..b9c5896b24 100644 --- a/core/io/stream_peer_tcp.cpp +++ b/core/io/stream_peer_tcp.cpp @@ -248,16 +248,7 @@ void StreamPeerTCP::set_no_delay(bool p_enabled) { bool StreamPeerTCP::is_connected_to_host() const { - if (status == STATUS_NONE || status == STATUS_ERROR) { - - return false; - } - - if (status != STATUS_CONNECTED) { - return true; - } - - return _sock.is_valid() && _sock->is_open(); + return _sock.is_valid() && _sock->is_open() && (status == STATUS_CONNECTED || status == STATUS_CONNECTING); } StreamPeerTCP::Status StreamPeerTCP::get_status() { diff --git a/core/io/translation_loader_po.cpp b/core/io/translation_loader_po.cpp index 67a0a905bd..9b6888ac21 100644 --- a/core/io/translation_loader_po.cpp +++ b/core/io/translation_loader_po.cpp @@ -67,8 +67,7 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const S if (status == STATUS_READING_ID) { memdelete(f); - ERR_EXPLAIN(p_path + ":" + itos(line) + " Unexpected EOF while reading 'msgid' at file: "); - ERR_FAIL_V(RES()); + ERR_FAIL_V_MSG(RES(), p_path + ":" + itos(line) + " Unexpected EOF while reading 'msgid' at file: "); } else { break; } @@ -79,8 +78,7 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const S if (status == STATUS_READING_ID) { memdelete(f); - ERR_EXPLAIN(p_path + ":" + itos(line) + " Unexpected 'msgid', was expecting 'msgstr' while parsing: "); - ERR_FAIL_V(RES()); + ERR_FAIL_V_MSG(RES(), p_path + ":" + itos(line) + " Unexpected 'msgid', was expecting 'msgstr' while parsing: "); } if (msg_id != "") { @@ -102,8 +100,7 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const S if (status != STATUS_READING_ID) { memdelete(f); - ERR_EXPLAIN(p_path + ":" + itos(line) + " Unexpected 'msgstr', was expecting 'msgid' while parsing: "); - ERR_FAIL_V(RES()); + ERR_FAIL_V_MSG(RES(), p_path + ":" + itos(line) + " Unexpected 'msgstr', was expecting 'msgid' while parsing: "); } l = l.substr(6, l.length()).strip_edges(); @@ -118,11 +115,7 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const S continue; //nothing to read or comment } - if (!l.begins_with("\"") || status == STATUS_NONE) { - //not a string? failure! - ERR_EXPLAIN(p_path + ":" + itos(line) + " Invalid line '" + l + "' while parsing: "); - ERR_FAIL_V(RES()); - } + ERR_FAIL_COND_V_MSG(!l.begins_with("\"") || status == STATUS_NONE, RES(), p_path + ":" + itos(line) + " Invalid line '" + l + "' while parsing: "); l = l.substr(1, l.length()); //find final quote @@ -135,10 +128,7 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const S } } - if (end_pos == -1) { - ERR_EXPLAIN(p_path + ":" + itos(line) + " Expected '\"' at end of message while parsing file: "); - ERR_FAIL_V(RES()); - } + ERR_FAIL_COND_V_MSG(end_pos == -1, RES(), p_path + ":" + itos(line) + " Expected '\"' at end of message while parsing file: "); l = l.substr(0, end_pos); l = l.c_unescape(); @@ -163,10 +153,7 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const S config = msg_str; } - if (config == "") { - ERR_EXPLAIN("No config found in file: " + p_path); - ERR_FAIL_V(RES()); - } + ERR_FAIL_COND_V_MSG(config == "", RES(), "No config found in file: " + p_path + "."); Vector<String> configs = config.split("\n"); for (int i = 0; i < configs.size(); i++) { @@ -195,7 +182,7 @@ RES TranslationLoaderPO::load(const String &p_path, const String &p_original_pat *r_error = ERR_CANT_OPEN; FileAccess *f = FileAccess::open(p_path, FileAccess::READ); - ERR_FAIL_COND_V(!f, RES()); + ERR_FAIL_COND_V_MSG(!f, RES(), "Cannot open file '" + p_path + "'."); return load_translation(f, r_error); } diff --git a/core/io/xml_parser.cpp b/core/io/xml_parser.cpp index 82527d3f38..575c78734f 100644 --- a/core/io/xml_parser.cpp +++ b/core/io/xml_parser.cpp @@ -443,10 +443,8 @@ String XMLParser::get_attribute_value(const String &p_name) const { } } - if (idx < 0) { - ERR_EXPLAIN("Attribute not found: " + p_name); - } - ERR_FAIL_COND_V(idx < 0, ""); + ERR_FAIL_COND_V_MSG(idx < 0, "", "Attribute not found: " + p_name + "."); + return attributes[idx].value; } @@ -486,7 +484,7 @@ Error XMLParser::open(const String &p_path) { Error err; FileAccess *file = FileAccess::open(p_path, FileAccess::READ, &err); - ERR_FAIL_COND_V(err != OK, err); + ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot open file '" + p_path + "'."); length = file->get_len(); ERR_FAIL_COND_V(length < 1, ERR_FILE_CORRUPT); diff --git a/core/list.h b/core/list.h index d1b528562d..c46888e01c 100644 --- a/core/list.h +++ b/core/list.h @@ -31,6 +31,7 @@ #ifndef GLOBALS_LIST_H #define GLOBALS_LIST_H +#include "core/error_macros.h" #include "core/os/memory.h" #include "core/sort_array.h" diff --git a/core/map.h b/core/map.h index c8197639f2..77e73d70cb 100644 --- a/core/map.h +++ b/core/map.h @@ -31,12 +31,9 @@ #ifndef MAP_H #define MAP_H +#include "core/error_macros.h" #include "core/set.h" -/** - @author Juan Linietsky <reduzio@gmail.com> -*/ - // based on the very nice implementation of rb-trees by: // https://web.archive.org/web/20120507164830/http://web.mit.edu/~emin/www/source_code/red_black_tree/index.html diff --git a/core/math/SCsub b/core/math/SCsub index 0995298a4b..be438fcfbe 100644 --- a/core/math/SCsub +++ b/core/math/SCsub @@ -2,37 +2,6 @@ Import('env') -env_math = env.Clone() # Maybe make one specific for crypto? - -is_builtin = env["builtin_mbedtls"] -has_module = env["module_mbedtls_enabled"] - -if is_builtin or not has_module: - # Use our headers for builtin or if the module is not going to be compiled. - # We decided not to depend on system mbedtls just for these few files that can - # be easily extracted. - env_math.Prepend(CPPPATH=["#thirdparty/mbedtls/include"]) - -# MbedTLS core functions (for CryptoCore). -# If the mbedtls module is compiled we don't need to add the .c files with our -# custom config since they will be built by the module itself. -# Only if the module is not enabled, we must compile here the required sources -# to make a "light" build with only the necessary mbedtls files. -if not has_module: - env_thirdparty = env_math.Clone() - env_thirdparty.disable_warnings() - # Custom config file - env_thirdparty.Append(CPPDEFINES=[('MBEDTLS_CONFIG_FILE', '\\"thirdparty/mbedtls/include/godot_core_mbedtls_config.h\\"')]) - thirdparty_mbedtls_dir = "#thirdparty/mbedtls/library/" - thirdparty_mbedtls_sources = [ - "aes.c", - "base64.c", - "md5.c", - "sha1.c", - "sha256.c", - "godot_core_mbedtls_platform.c" - ] - thirdparty_mbedtls_sources = [thirdparty_mbedtls_dir + file for file in thirdparty_mbedtls_sources] - env_thirdparty.add_source_files(env.core_sources, thirdparty_mbedtls_sources) +env_math = env.Clone() env_math.add_source_files(env.core_sources, "*.cpp") diff --git a/core/math/a_star.cpp b/core/math/a_star.cpp index b61119d8df..ae2b56e7b7 100644 --- a/core/math/a_star.cpp +++ b/core/math/a_star.cpp @@ -40,7 +40,17 @@ int AStar::get_available_point_id() const { return 1; } - return points.back()->key() + 1; + // calculate our new next available point id if bigger than before or next id already contained in set of points. + if (points.has(last_free_id)) { + int cur_new_id = last_free_id; + while (points.has(cur_new_id)) { + cur_new_id++; + } + int &non_const = const_cast<int &>(last_free_id); + non_const = cur_new_id; + } + + return last_free_id; } void AStar::add_point(int p_id, const Vector3 &p_pos, real_t p_weight_scale) { @@ -48,7 +58,10 @@ void AStar::add_point(int p_id, const Vector3 &p_pos, real_t p_weight_scale) { ERR_FAIL_COND(p_id < 0); ERR_FAIL_COND(p_weight_scale < 1); - if (!points.has(p_id)) { + Point *found_pt; + bool p_exists = points.lookup(p_id, found_pt); + + if (!p_exists) { Point *pt = memnew(Point); pt->id = p_id; pt->pos = p_pos; @@ -57,84 +70,98 @@ void AStar::add_point(int p_id, const Vector3 &p_pos, real_t p_weight_scale) { pt->open_pass = 0; pt->closed_pass = 0; pt->enabled = true; - points[p_id] = pt; + points.set(p_id, pt); } else { - points[p_id]->pos = p_pos; - points[p_id]->weight_scale = p_weight_scale; + found_pt->pos = p_pos; + found_pt->weight_scale = p_weight_scale; } } Vector3 AStar::get_point_position(int p_id) const { - ERR_FAIL_COND_V(!points.has(p_id), Vector3()); + Point *p; + bool p_exists = points.lookup(p_id, p); + ERR_FAIL_COND_V(!p_exists, Vector3()); - return points[p_id]->pos; + return p->pos; } void AStar::set_point_position(int p_id, const Vector3 &p_pos) { - ERR_FAIL_COND(!points.has(p_id)); + Point *p; + bool p_exists = points.lookup(p_id, p); + ERR_FAIL_COND(!p_exists); - points[p_id]->pos = p_pos; + p->pos = p_pos; } real_t AStar::get_point_weight_scale(int p_id) const { - ERR_FAIL_COND_V(!points.has(p_id), 0); + Point *p; + bool p_exists = points.lookup(p_id, p); + ERR_FAIL_COND_V(!p_exists, 0); - return points[p_id]->weight_scale; + return p->weight_scale; } void AStar::set_point_weight_scale(int p_id, real_t p_weight_scale) { - ERR_FAIL_COND(!points.has(p_id)); + Point *p; + bool p_exists = points.lookup(p_id, p); + ERR_FAIL_COND(!p_exists); ERR_FAIL_COND(p_weight_scale < 1); - points[p_id]->weight_scale = p_weight_scale; + p->weight_scale = p_weight_scale; } void AStar::remove_point(int p_id) { - ERR_FAIL_COND(!points.has(p_id)); - - Point *p = points[p_id]; + Point *p; + bool p_exists = points.lookup(p_id, p); + ERR_FAIL_COND(!p_exists); - for (Set<Point *>::Element *E = p->neighbours.front(); E; E = E->next()) { + for (OAHashMap<int, Point *>::Iterator it = p->neighbours.iter(); it.valid; it = p->neighbours.next_iter(it)) { - Segment s(p_id, E->get()->id); + Segment s(p_id, (*it.key)); segments.erase(s); - E->get()->neighbours.erase(p); - E->get()->unlinked_neighbours.erase(p); + (*it.value)->neighbours.remove(p->id); + (*it.value)->unlinked_neighbours.remove(p->id); } - for (Set<Point *>::Element *E = p->unlinked_neighbours.front(); E; E = E->next()) { + for (OAHashMap<int, Point *>::Iterator it = p->unlinked_neighbours.iter(); it.valid; it = p->unlinked_neighbours.next_iter(it)) { - Segment s(p_id, E->get()->id); + Segment s(p_id, (*it.key)); segments.erase(s); - E->get()->neighbours.erase(p); - E->get()->unlinked_neighbours.erase(p); + (*it.value)->neighbours.remove(p->id); + (*it.value)->unlinked_neighbours.remove(p->id); } memdelete(p); - points.erase(p_id); + points.remove(p_id); + last_free_id = p_id; } void AStar::connect_points(int p_id, int p_with_id, bool bidirectional) { - ERR_FAIL_COND(!points.has(p_id)); - ERR_FAIL_COND(!points.has(p_with_id)); ERR_FAIL_COND(p_id == p_with_id); - Point *a = points[p_id]; - Point *b = points[p_with_id]; - a->neighbours.insert(b); + Point *a; + bool from_exists = points.lookup(p_id, a); + ERR_FAIL_COND(!from_exists); + + Point *b; + bool to_exists = points.lookup(p_with_id, b); + ERR_FAIL_COND(!to_exists); - if (bidirectional) - b->neighbours.insert(a); - else - b->unlinked_neighbours.insert(a); + a->neighbours.set(b->id, b); + + if (bidirectional) { + b->neighbours.set(a->id, a); + } else { + b->unlinked_neighbours.set(a->id, a); + } Segment s(p_id, p_with_id); if (s.from == p_id) { @@ -147,6 +174,7 @@ void AStar::connect_points(int p_id, int p_with_id, bool bidirectional) { segments.insert(s); } + void AStar::disconnect_points(int p_id, int p_with_id) { Segment s(p_id, p_with_id); @@ -154,12 +182,18 @@ void AStar::disconnect_points(int p_id, int p_with_id) { segments.erase(s); - Point *a = points[p_id]; - Point *b = points[p_with_id]; - a->neighbours.erase(b); - a->unlinked_neighbours.erase(b); - b->neighbours.erase(a); - b->unlinked_neighbours.erase(a); + Point *a; + bool a_exists = points.lookup(p_id, a); + CRASH_COND(!a_exists); + + Point *b; + bool b_exists = points.lookup(p_with_id, b); + CRASH_COND(!b_exists); + + a->neighbours.remove(b->id); + a->unlinked_neighbours.remove(b->id); + b->neighbours.remove(a->id); + b->unlinked_neighbours.remove(a->id); } bool AStar::has_point(int p_id) const { @@ -171,8 +205,8 @@ Array AStar::get_points() { Array point_list; - for (const Map<int, Point *>::Element *E = points.front(); E; E = E->next()) { - point_list.push_back(E->key()); + for (OAHashMap<int, Point *>::Iterator it = points.iter(); it.valid; it = points.next_iter(it)) { + point_list.push_back(*(it.key)); } return point_list; @@ -180,14 +214,14 @@ Array AStar::get_points() { PoolVector<int> AStar::get_point_connections(int p_id) { - ERR_FAIL_COND_V(!points.has(p_id), PoolVector<int>()); + Point *p; + bool p_exists = points.lookup(p_id, p); + ERR_FAIL_COND_V(!p_exists, PoolVector<int>()); PoolVector<int> point_list; - Point *p = points[p_id]; - - for (Set<Point *>::Element *E = p->neighbours.front(); E; E = E->next()) { - point_list.push_back(E->get()->id); + for (OAHashMap<int, Point *>::Iterator it = p->neighbours.iter(); it.valid; it = p->neighbours.next_iter(it)) { + point_list.push_back((*it.key)); } return point_list; @@ -201,27 +235,41 @@ bool AStar::are_points_connected(int p_id, int p_with_id) const { void AStar::clear() { - for (const Map<int, Point *>::Element *E = points.front(); E; E = E->next()) { - - memdelete(E->get()); + last_free_id = 0; + for (OAHashMap<int, Point *>::Iterator it = points.iter(); it.valid; it = points.next_iter(it)) { + memdelete(*(it.value)); } segments.clear(); points.clear(); } -int AStar::get_closest_point(const Vector3 &p_point) const { +int AStar::get_point_count() const { + return points.get_num_elements(); +} + +int AStar::get_point_capacity() const { + return points.get_capacity(); +} + +void AStar::reserve_space(int p_num_nodes) { + ERR_FAIL_COND_MSG(p_num_nodes <= 0, "New capacity must be greater than 0, was: " + itos(p_num_nodes) + "."); + ERR_FAIL_COND_MSG((uint32_t)p_num_nodes < points.get_capacity(), "New capacity must be greater than current capacity: " + itos(points.get_capacity()) + ", new was: " + itos(p_num_nodes) + "."); + points.reserve(p_num_nodes); +} + +int AStar::get_closest_point(const Vector3 &p_point, bool p_include_disabled) const { int closest_id = -1; real_t closest_dist = 1e20; - for (const Map<int, Point *>::Element *E = points.front(); E; E = E->next()) { + for (OAHashMap<int, Point *>::Iterator it = points.iter(); it.valid; it = points.next_iter(it)) { + + if (!p_include_disabled && !(*it.value)->enabled) continue; // Disabled points should not be considered. - if (!E->get()->enabled) - continue; //Disabled points should not be considered - real_t d = p_point.distance_squared_to(E->get()->pos); + real_t d = p_point.distance_squared_to((*it.value)->pos); if (closest_id < 0 || d < closest_dist) { closest_dist = d; - closest_id = E->key(); + closest_id = *(it.key); } } @@ -230,8 +278,8 @@ int AStar::get_closest_point(const Vector3 &p_point) const { Vector3 AStar::get_closest_position_in_segment(const Vector3 &p_point) const { - real_t closest_dist = 1e20; bool found = false; + real_t closest_dist = 1e20; Vector3 closest_point; for (const Set<Segment>::Element *E = segments.front(); E; E = E->next()) { @@ -262,8 +310,7 @@ bool AStar::_solve(Point *begin_point, Point *end_point) { pass++; - if (!end_point->enabled) - return false; + if (!end_point->enabled) return false; bool found_route = false; @@ -272,13 +319,9 @@ bool AStar::_solve(Point *begin_point, Point *end_point) { begin_point->g_score = 0; begin_point->f_score = _estimate_cost(begin_point->id, end_point->id); - open_list.push_back(begin_point); - while (true) { - - if (open_list.size() == 0) // No path found - break; + while (!open_list.empty()) { Point *p = open_list[0]; // The currently processed point @@ -291,24 +334,23 @@ bool AStar::_solve(Point *begin_point, Point *end_point) { open_list.remove(open_list.size() - 1); p->closed_pass = pass; // Mark the point as closed - for (Set<Point *>::Element *E = p->neighbours.front(); E; E = E->next()) { + for (OAHashMap<int, Point *>::Iterator it = p->neighbours.iter(); it.valid; it = p->neighbours.next_iter(it)) { - Point *e = E->get(); // The neighbour point + Point *e = *(it.value); // The neighbour point - if (!e->enabled || e->closed_pass == pass) + if (!e->enabled || e->closed_pass == pass) { continue; + } real_t tentative_g_score = p->g_score + _compute_cost(p->id, e->id) * e->weight_scale; bool new_point = false; - if (e->open_pass != pass) { // The point wasn't inside the open list - + if (e->open_pass != pass) { // The point wasn't inside the open list. e->open_pass = pass; open_list.push_back(e); new_point = true; - } else if (tentative_g_score >= e->g_score) { // The new path is worse than the previous - + } else if (tentative_g_score >= e->g_score) { // The new path is worse than the previous. continue; } @@ -316,10 +358,11 @@ bool AStar::_solve(Point *begin_point, Point *end_point) { e->g_score = tentative_g_score; e->f_score = e->g_score + _estimate_cost(e->id, end_point->id); - if (new_point) // The position of the new points is already known + if (new_point) { // The position of the new points is already known. sorter.push_heap(0, open_list.size() - 1, 0, e, open_list.ptrw()); - else + } else { sorter.push_heap(0, open_list.find(e), 0, e, open_list.ptrw()); + } } } @@ -331,7 +374,15 @@ float AStar::_estimate_cost(int p_from_id, int p_to_id) { if (get_script_instance() && get_script_instance()->has_method(SceneStringNames::get_singleton()->_estimate_cost)) return get_script_instance()->call(SceneStringNames::get_singleton()->_estimate_cost, p_from_id, p_to_id); - return points[p_from_id]->pos.distance_to(points[p_to_id]->pos); + Point *from_point; + bool from_exists = points.lookup(p_from_id, from_point); + CRASH_COND(!from_exists); + + Point *to_point; + bool to_exists = points.lookup(p_to_id, to_point); + CRASH_COND(!to_exists); + + return from_point->pos.distance_to(to_point->pos); } float AStar::_compute_cost(int p_from_id, int p_to_id) { @@ -339,16 +390,26 @@ float AStar::_compute_cost(int p_from_id, int p_to_id) { if (get_script_instance() && get_script_instance()->has_method(SceneStringNames::get_singleton()->_compute_cost)) return get_script_instance()->call(SceneStringNames::get_singleton()->_compute_cost, p_from_id, p_to_id); - return points[p_from_id]->pos.distance_to(points[p_to_id]->pos); + Point *from_point; + bool from_exists = points.lookup(p_from_id, from_point); + CRASH_COND(!from_exists); + + Point *to_point; + bool to_exists = points.lookup(p_to_id, to_point); + CRASH_COND(!to_exists); + + return from_point->pos.distance_to(to_point->pos); } PoolVector<Vector3> AStar::get_point_path(int p_from_id, int p_to_id) { - ERR_FAIL_COND_V(!points.has(p_from_id), PoolVector<Vector3>()); - ERR_FAIL_COND_V(!points.has(p_to_id), PoolVector<Vector3>()); + Point *a; + bool from_exists = points.lookup(p_from_id, a); + ERR_FAIL_COND_V(!from_exists, PoolVector<Vector3>()); - Point *a = points[p_from_id]; - Point *b = points[p_to_id]; + Point *b; + bool to_exists = points.lookup(p_to_id, b); + ERR_FAIL_COND_V(!to_exists, PoolVector<Vector3>()); if (a == b) { PoolVector<Vector3> ret; @@ -360,11 +421,8 @@ PoolVector<Vector3> AStar::get_point_path(int p_from_id, int p_to_id) { Point *end_point = b; bool found_route = _solve(begin_point, end_point); + if (!found_route) return PoolVector<Vector3>(); - if (!found_route) - return PoolVector<Vector3>(); - - // Midpoints Point *p = end_point; int pc = 1; // Begin point while (p != begin_point) { @@ -393,11 +451,13 @@ PoolVector<Vector3> AStar::get_point_path(int p_from_id, int p_to_id) { PoolVector<int> AStar::get_id_path(int p_from_id, int p_to_id) { - ERR_FAIL_COND_V(!points.has(p_from_id), PoolVector<int>()); - ERR_FAIL_COND_V(!points.has(p_to_id), PoolVector<int>()); + Point *a; + bool from_exists = points.lookup(p_from_id, a); + ERR_FAIL_COND_V(!from_exists, PoolVector<int>()); - Point *a = points[p_from_id]; - Point *b = points[p_to_id]; + Point *b; + bool to_exists = points.lookup(p_to_id, b); + ERR_FAIL_COND_V(!to_exists, PoolVector<int>()); if (a == b) { PoolVector<int> ret; @@ -409,11 +469,8 @@ PoolVector<int> AStar::get_id_path(int p_from_id, int p_to_id) { Point *end_point = b; bool found_route = _solve(begin_point, end_point); + if (!found_route) return PoolVector<int>(); - if (!found_route) - return PoolVector<int>(); - - // Midpoints Point *p = end_point; int pc = 1; // Begin point while (p != begin_point) { @@ -442,16 +499,20 @@ PoolVector<int> AStar::get_id_path(int p_from_id, int p_to_id) { void AStar::set_point_disabled(int p_id, bool p_disabled) { - ERR_FAIL_COND(!points.has(p_id)); + Point *p; + bool p_exists = points.lookup(p_id, p); + ERR_FAIL_COND(!p_exists); - points[p_id]->enabled = !p_disabled; + p->enabled = !p_disabled; } bool AStar::is_point_disabled(int p_id) const { - ERR_FAIL_COND_V(!points.has(p_id), false); + Point *p; + bool p_exists = points.lookup(p_id, p); + ERR_FAIL_COND_V(!p_exists, false); - return !points[p_id]->enabled; + return !p->enabled; } void AStar::_bind_methods() { @@ -474,9 +535,12 @@ void AStar::_bind_methods() { ClassDB::bind_method(D_METHOD("disconnect_points", "id", "to_id"), &AStar::disconnect_points); ClassDB::bind_method(D_METHOD("are_points_connected", "id", "to_id"), &AStar::are_points_connected); + ClassDB::bind_method(D_METHOD("get_point_count"), &AStar::get_point_count); + ClassDB::bind_method(D_METHOD("get_point_capacity"), &AStar::get_point_capacity); + ClassDB::bind_method(D_METHOD("reserve_space", "num_nodes"), &AStar::reserve_space); ClassDB::bind_method(D_METHOD("clear"), &AStar::clear); - ClassDB::bind_method(D_METHOD("get_closest_point", "to_position"), &AStar::get_closest_point); + ClassDB::bind_method(D_METHOD("get_closest_point", "to_position", "include_disabled"), &AStar::get_closest_point, DEFVAL(false)); ClassDB::bind_method(D_METHOD("get_closest_position_in_segment", "to_position"), &AStar::get_closest_position_in_segment); ClassDB::bind_method(D_METHOD("get_point_path", "from_id", "to_id"), &AStar::get_point_path); @@ -487,13 +551,11 @@ void AStar::_bind_methods() { } AStar::AStar() { - + last_free_id = 0; pass = 1; } AStar::~AStar() { - - pass = 1; clear(); } @@ -560,12 +622,24 @@ bool AStar2D::are_points_connected(int p_id, int p_with_id) const { return astar.are_points_connected(p_id, p_with_id); } +int AStar2D::get_point_count() const { + return astar.get_point_count(); +} + +int AStar2D::get_point_capacity() const { + return astar.get_point_capacity(); +} + void AStar2D::clear() { astar.clear(); } -int AStar2D::get_closest_point(const Vector2 &p_point) const { - return astar.get_closest_point(Vector3(p_point.x, p_point.y, 0)); +void AStar2D::reserve_space(int p_num_nodes) { + astar.reserve_space(p_num_nodes); +} + +int AStar2D::get_closest_point(const Vector2 &p_point, bool p_include_disabled) const { + return astar.get_closest_point(Vector3(p_point.x, p_point.y, 0), p_include_disabled); } Vector2 AStar2D::get_closest_position_in_segment(const Vector2 &p_point) const { @@ -614,9 +688,12 @@ void AStar2D::_bind_methods() { ClassDB::bind_method(D_METHOD("disconnect_points", "id", "to_id"), &AStar2D::disconnect_points); ClassDB::bind_method(D_METHOD("are_points_connected", "id", "to_id"), &AStar2D::are_points_connected); + ClassDB::bind_method(D_METHOD("get_point_count"), &AStar2D::get_point_count); + ClassDB::bind_method(D_METHOD("get_point_capacity"), &AStar2D::get_point_capacity); + ClassDB::bind_method(D_METHOD("reserve_space", "num_nodes"), &AStar2D::reserve_space); ClassDB::bind_method(D_METHOD("clear"), &AStar2D::clear); - ClassDB::bind_method(D_METHOD("get_closest_point", "to_position"), &AStar2D::get_closest_point); + ClassDB::bind_method(D_METHOD("get_closest_point", "to_position", "include_disabled"), &AStar2D::get_closest_point, DEFVAL(false)); ClassDB::bind_method(D_METHOD("get_closest_position_in_segment", "to_position"), &AStar2D::get_closest_position_in_segment); ClassDB::bind_method(D_METHOD("get_point_path", "from_id", "to_id"), &AStar2D::get_point_path); diff --git a/core/math/a_star.h b/core/math/a_star.h index ec333efc1d..0a5d3e992c 100644 --- a/core/math/a_star.h +++ b/core/math/a_star.h @@ -31,8 +31,8 @@ #ifndef ASTAR_H #define ASTAR_H +#include "core/oa_hash_map.h" #include "core/reference.h" -#include "core/self_list.h" /** A* pathfinding algorithm @@ -44,19 +44,21 @@ class AStar : public Reference { GDCLASS(AStar, Reference); - uint64_t pass; - struct Point { + Point() : + neighbours(4u), + unlinked_neighbours(4u) {} + int id; Vector3 pos; real_t weight_scale; bool enabled; - Set<Point *> neighbours; - Set<Point *> unlinked_neighbours; + OAHashMap<int, Point *> neighbours; + OAHashMap<int, Point *> unlinked_neighbours; - // Used for pathfinding + // Used for pathfinding. Point *prev_point; real_t g_score; real_t f_score; @@ -64,16 +66,15 @@ class AStar : public Reference { uint64_t closed_pass; }; - Map<int, Point *> points; - struct SortPoints { - _FORCE_INLINE_ bool operator()(const Point *A, const Point *B) const { // Returns true when the Point A is worse than Point B - if (A->f_score > B->f_score) + _FORCE_INLINE_ bool operator()(const Point *A, const Point *B) const { // Returns true when the Point A is worse than Point B. + if (A->f_score > B->f_score) { return true; - else if (A->f_score < B->f_score) + } else if (A->f_score < B->f_score) { return false; - else - return A->g_score < B->g_score; // If the f_costs are the same then prioritize the points that are further away from the start + } else { + return A->g_score < B->g_score; // If the f_costs are the same then prioritize the points that are further away from the start. + } } }; @@ -101,6 +102,10 @@ class AStar : public Reference { } }; + int last_free_id; + uint64_t pass; + + OAHashMap<int, Point *> points; Set<Segment> segments; bool _solve(Point *begin_point, Point *end_point); @@ -131,9 +136,12 @@ public: void disconnect_points(int p_id, int p_with_id); bool are_points_connected(int p_id, int p_with_id) const; + int get_point_count() const; + int get_point_capacity() const; + void reserve_space(int p_num_nodes); void clear(); - int get_closest_point(const Vector3 &p_point) const; + int get_closest_point(const Vector3 &p_point, bool p_include_disabled = false) const; Vector3 get_closest_position_in_segment(const Vector3 &p_point) const; PoolVector<Vector3> get_point_path(int p_from_id, int p_to_id); @@ -170,9 +178,12 @@ public: void disconnect_points(int p_id, int p_with_id); bool are_points_connected(int p_id, int p_with_id) const; + int get_point_count() const; + int get_point_capacity() const; + void reserve_space(int p_num_nodes); void clear(); - int get_closest_point(const Vector2 &p_point) const; + int get_closest_point(const Vector2 &p_point, bool p_include_disabled = false) const; Vector2 get_closest_position_in_segment(const Vector2 &p_point) const; PoolVector<Vector2> get_point_path(int p_from_id, int p_to_id); diff --git a/core/math/basis.cpp b/core/math/basis.cpp index 400f342018..0a491010e2 100644 --- a/core/math/basis.cpp +++ b/core/math/basis.cpp @@ -618,10 +618,7 @@ Basis::operator String() const { Quat Basis::get_quat() const { #ifdef MATH_CHECKS - if (!is_rotation()) { - ERR_EXPLAIN("Basis must be normalized in order to be casted to a Quaternion. Use get_rotation_quat() or call orthonormalized() instead."); - ERR_FAIL_V(Quat()); - } + ERR_FAIL_COND_V_MSG(!is_rotation(), Quat(), "Basis must be normalized in order to be casted to a Quaternion. Use get_rotation_quat() or call orthonormalized() instead."); #endif /* Allow getting a quaternion from an unnormalized transform */ Basis m = *this; @@ -810,7 +807,7 @@ void Basis::set_quat(const Quat &p_quat) { void Basis::set_axis_angle(const Vector3 &p_axis, real_t p_phi) { // Rotation matrix from axis and angle, see https://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_angle #ifdef MATH_CHECKS - ERR_FAIL_COND(!p_axis.is_normalized()); + ERR_FAIL_COND_MSG(!p_axis.is_normalized(), "Axis must be normalized."); #endif Vector3 axis_sq(p_axis.x * p_axis.x, p_axis.y * p_axis.y, p_axis.z * p_axis.z); real_t cosine = Math::cos(p_phi); diff --git a/core/math/basis.h b/core/math/basis.h index d3adad3d90..4be4ea4cd3 100644 --- a/core/math/basis.h +++ b/core/math/basis.h @@ -28,17 +28,11 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -// Circular dependency between Vector3 and Basis :/ -#include "core/math/vector3.h" - #ifndef BASIS_H #define BASIS_H #include "core/math/quat.h" - -/** - @author Juan Linietsky <reduzio@gmail.com> -*/ +#include "core/math/vector3.h" class Basis { public: diff --git a/core/math/bsp_tree.h b/core/math/bsp_tree.h index a7a3697990..90b5e8322a 100644 --- a/core/math/bsp_tree.h +++ b/core/math/bsp_tree.h @@ -38,9 +38,7 @@ #include "core/pool_vector.h" #include "core/variant.h" #include "core/vector.h" -/** - @author Juan Linietsky <reduzio@gmail.com> -*/ + class BSP_Tree { public: enum { diff --git a/core/math/camera_matrix.h b/core/math/camera_matrix.h index 3bcf48f5da..63cc88553d 100644 --- a/core/math/camera_matrix.h +++ b/core/math/camera_matrix.h @@ -34,10 +34,6 @@ #include "core/math/rect2.h" #include "core/math/transform.h" -/** - @author Juan Linietsky <reduzio@gmail.com> -*/ - struct CameraMatrix { enum Planes { diff --git a/core/math/delaunay.h b/core/math/delaunay.h index ed52c506db..3f8013a3e6 100644 --- a/core/math/delaunay.h +++ b/core/math/delaunay.h @@ -80,11 +80,11 @@ public: } static bool edge_compare(const Vector<Vector2> &p_vertices, const Edge &p_a, const Edge &p_b) { - if (Math::is_zero_approx(p_vertices[p_a.edge[0]].distance_to(p_vertices[p_b.edge[0]])) && Math::is_zero_approx(p_vertices[p_a.edge[1]].distance_to(p_vertices[p_b.edge[1]]))) { + if (p_vertices[p_a.edge[0]] == p_vertices[p_b.edge[0]] && p_vertices[p_a.edge[1]] == p_vertices[p_b.edge[1]]) { return true; } - if (Math::is_zero_approx(p_vertices[p_a.edge[0]].distance_to(p_vertices[p_b.edge[1]])) && Math::is_zero_approx(p_vertices[p_a.edge[1]].distance_to(p_vertices[p_b.edge[0]]))) { + if (p_vertices[p_a.edge[0]] == p_vertices[p_b.edge[1]] && p_vertices[p_a.edge[1]] == p_vertices[p_b.edge[0]]) { return true; } diff --git a/core/math/disjoint_set.cpp b/core/math/disjoint_set.cpp new file mode 100644 index 0000000000..c9d47aa0ae --- /dev/null +++ b/core/math/disjoint_set.cpp @@ -0,0 +1,31 @@ +/*************************************************************************/ +/* disjoint_set.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 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 "disjoint_set.h" diff --git a/core/math/disjoint_set.h b/core/math/disjoint_set.h new file mode 100644 index 0000000000..c9b3d0b65d --- /dev/null +++ b/core/math/disjoint_set.h @@ -0,0 +1,155 @@ +/*************************************************************************/ +/* disjoint_set.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 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 DISJOINT_SET_H +#define DISJOINT_SET_H + +#include "core/map.h" +#include "core/vector.h" + +/** + @author Marios Staikopoulos <marios@staik.net> +*/ + +/* This DisjointSet class uses Find with path compression and Union by rank */ +template <typename T, class C = Comparator<T>, class AL = DefaultAllocator> +class DisjointSet { + + struct Element { + T object; + Element *parent = nullptr; + int rank = 0; + }; + + typedef Map<T, Element *, C, AL> MapT; + + MapT elements; + + Element *get_parent(Element *element); + + _FORCE_INLINE_ Element *insert_or_get(T object); + +public: + ~DisjointSet(); + + _FORCE_INLINE_ void insert(T object) { (void)insert_or_get(object); } + + void create_union(T a, T b); + + void get_representatives(Vector<T> &out_roots); + + void get_members(Vector<T> &out_members, T representative); +}; + +/* FUNCTIONS */ + +template <typename T, class C, class AL> +DisjointSet<T, C, AL>::~DisjointSet() { + for (typename MapT::Element *itr = elements.front(); itr != nullptr; itr = itr->next()) { + memdelete_allocator<Element, AL>(itr->value()); + } +} + +template <typename T, class C, class AL> +typename DisjointSet<T, C, AL>::Element *DisjointSet<T, C, AL>::get_parent(Element *element) { + if (element->parent != element) { + element->parent = get_parent(element->parent); + } + + return element->parent; +} + +template <typename T, class C, class AL> +typename DisjointSet<T, C, AL>::Element *DisjointSet<T, C, AL>::insert_or_get(T object) { + typename MapT::Element *itr = elements.find(object); + if (itr != nullptr) { + return itr->value(); + } + + Element *new_element = memnew_allocator(Element, AL); + new_element->object = object; + new_element->parent = new_element; + elements.insert(object, new_element); + + return new_element; +} + +template <typename T, class C, class AL> +void DisjointSet<T, C, AL>::create_union(T a, T b) { + + Element *x = insert_or_get(a); + Element *y = insert_or_get(b); + + Element *x_root = get_parent(x); + Element *y_root = get_parent(y); + + // Already in the same set + if (x_root == y_root) + return; + + // Not in the same set, merge + if (x_root->rank < y_root->rank) { + SWAP(x_root, y_root); + } + + // Merge y_root into x_root + y_root->parent = x_root; + if (x_root->rank == y_root->rank) { + ++x_root->rank; + } +} + +template <typename T, class C, class AL> +void DisjointSet<T, C, AL>::get_representatives(Vector<T> &out_representatives) { + for (typename MapT::Element *itr = elements.front(); itr != nullptr; itr = itr->next()) { + Element *element = itr->value(); + if (element->parent == element) { + out_representatives.push_back(element->object); + } + } +} + +template <typename T, class C, class AL> +void DisjointSet<T, C, AL>::get_members(Vector<T> &out_members, T representative) { + typename MapT::Element *rep_itr = elements.find(representative); + ERR_FAIL_COND(rep_itr == nullptr); + + Element *rep_element = rep_itr->value(); + ERR_FAIL_COND(rep_element->parent != rep_element); + + for (typename MapT::Element *itr = elements.front(); itr != nullptr; itr = itr->next()) { + Element *parent = get_parent(itr->value()); + if (parent == rep_element) { + out_members.push_back(itr->key()); + } + } +} + +#endif diff --git a/core/math/expression.cpp b/core/math/expression.cpp index 15eea1d308..46f81ce5c3 100644 --- a/core/math/expression.cpp +++ b/core/math/expression.cpp @@ -2161,10 +2161,8 @@ Error Expression::parse(const String &p_expression, const Vector<String> &p_inpu } Variant Expression::execute(Array p_inputs, Object *p_base, bool p_show_error) { - if (error_set) { - ERR_EXPLAIN("There was previously a parse error: " + error_str); - ERR_FAIL_V(Variant()); - } + + ERR_FAIL_COND_V_MSG(error_set, Variant(), "There was previously a parse error: " + error_str + "."); execution_error = false; Variant output; @@ -2173,10 +2171,7 @@ Variant Expression::execute(Array p_inputs, Object *p_base, bool p_show_error) { if (err) { execution_error = true; error_str = error_txt; - if (p_show_error) { - ERR_EXPLAIN(error_str); - ERR_FAIL_V(Variant()); - } + ERR_FAIL_COND_V_MSG(p_show_error, Variant(), error_str); } return output; diff --git a/core/math/geometry.cpp b/core/math/geometry.cpp index 8314cb827c..e0ead8446f 100644 --- a/core/math/geometry.cpp +++ b/core/math/geometry.cpp @@ -34,9 +34,10 @@ #include "thirdparty/misc/clipper.hpp" #include "thirdparty/misc/triangulator.h" -#define SCALE_FACTOR 100000.0 // based on CMP_EPSILON +#define SCALE_FACTOR 100000.0 // Based on CMP_EPSILON. -/* this implementation is very inefficient, commenting unless bugs happen. See the other one. +// This implementation is very inefficient, commenting unless bugs happen. See the other one. +/* bool Geometry::is_point_in_polygon(const Vector2 &p_point, const Vector<Vector2> &p_polygon) { Vector<int> indices = Geometry::triangulate_polygon(p_polygon); @@ -124,8 +125,8 @@ struct _FaceClassify { }; static bool _connect_faces(_FaceClassify *p_faces, int len, int p_group) { - /* connect faces, error will occur if an edge is shared between more than 2 faces */ - /* clear connections */ + // Connect faces, error will occur if an edge is shared between more than 2 faces. + // Clear connections. bool error = false; @@ -195,13 +196,6 @@ static bool _connect_faces(_FaceClassify *p_faces, int len, int p_group) { if (p_faces[i].links[j].face == -1) p_faces[i].valid = false; } - /*printf("face %i is valid: %i, group %i. connected to %i:%i,%i:%i,%i:%i\n",i,p_faces[i].valid,p_faces[i].group, - p_faces[i].links[0].face, - p_faces[i].links[0].edge, - p_faces[i].links[1].face, - p_faces[i].links[1].edge, - p_faces[i].links[2].face, - p_faces[i].links[2].edge);*/ } return error; } @@ -247,12 +241,9 @@ PoolVector<PoolVector<Face3> > Geometry::separate_objects(PoolVector<Face3> p_ar bool error = _connect_faces(_fcptr, len, -1); - if (error) { + ERR_FAIL_COND_V_MSG(error, PoolVector<PoolVector<Face3> >(), "Invalid geometry."); - ERR_FAIL_COND_V(error, PoolVector<PoolVector<Face3> >()); // invalid geometry - } - - /* group connected faces in separate objects */ + // Group connected faces in separate objects. int group = 0; for (int i = 0; i < len; i++) { @@ -264,7 +255,7 @@ PoolVector<PoolVector<Face3> > Geometry::separate_objects(PoolVector<Face3> p_ar } } - /* group connected faces in separate objects */ + // Group connected faces in separate objects. for (int i = 0; i < len; i++) { @@ -376,7 +367,7 @@ static inline void _plot_face(uint8_t ***p_cell_status, int x, int y, int z, int static inline void _mark_outside(uint8_t ***p_cell_status, int x, int y, int z, int len_x, int len_y, int len_z) { if (p_cell_status[x][y][z] & 3) - return; // nothing to do, already used and/or visited + return; // Nothing to do, already used and/or visited. p_cell_status[x][y][z] = _CELL_PREV_FIRST; @@ -384,29 +375,20 @@ static inline void _mark_outside(uint8_t ***p_cell_status, int x, int y, int z, uint8_t &c = p_cell_status[x][y][z]; - //printf("at %i,%i,%i\n",x,y,z); - if ((c & _CELL_STEP_MASK) == _CELL_STEP_NONE) { - /* Haven't been in here, mark as outside */ + // Haven't been in here, mark as outside. p_cell_status[x][y][z] |= _CELL_EXTERIOR; - //printf("not marked as anything, marking exterior\n"); } - //printf("cell step is %i\n",(c&_CELL_STEP_MASK)); - if ((c & _CELL_STEP_MASK) != _CELL_STEP_DONE) { - /* if not done, increase step */ + // If not done, increase step. c += 1 << 2; - //printf("incrementing cell step\n"); } if ((c & _CELL_STEP_MASK) == _CELL_STEP_DONE) { - /* Go back */ - //printf("done, going back a cell\n"); - + // Go back. switch (c & _CELL_PREV_MASK) { case _CELL_PREV_FIRST: { - //printf("at end, finished marking\n"); return; } break; case _CELL_PREV_Y_POS: { @@ -440,8 +422,6 @@ static inline void _mark_outside(uint8_t ***p_cell_status, int x, int y, int z, continue; } - //printf("attempting new cell!\n"); - int next_x = x, next_y = y, next_z = z; uint8_t prev = 0; @@ -475,8 +455,6 @@ static inline void _mark_outside(uint8_t ***p_cell_status, int x, int y, int z, default: ERR_FAIL(); } - //printf("testing if new cell will be ok...!\n"); - if (next_x < 0 || next_x >= len_x) continue; if (next_y < 0 || next_y >= len_y) @@ -484,13 +462,9 @@ static inline void _mark_outside(uint8_t ***p_cell_status, int x, int y, int z, if (next_z < 0 || next_z >= len_z) continue; - //printf("testing if new cell is traversable\n"); - if (p_cell_status[next_x][next_y][next_z] & 3) continue; - //printf("move to it\n"); - x = next_x; y = next_y; z = next_z; @@ -507,17 +481,6 @@ static inline void _build_faces(uint8_t ***p_cell_status, int x, int y, int z, i if (p_cell_status[x][y][z] & _CELL_EXTERIOR) return; -/* static const Vector3 vertices[8]={ - Vector3(0,0,0), - Vector3(0,0,1), - Vector3(0,1,0), - Vector3(0,1,1), - Vector3(1,0,0), - Vector3(1,0,1), - Vector3(1,1,0), - Vector3(1,1,1), - }; -*/ #define vert(m_idx) Vector3(((m_idx)&4) >> 2, ((m_idx)&2) >> 1, (m_idx)&1) static const uint8_t indices[6][4] = { @@ -529,22 +492,6 @@ static inline void _build_faces(uint8_t ***p_cell_status, int x, int y, int z, i { 0, 4, 6, 2 }, }; - /* - - {0,1,2,3}, - {0,1,4,5}, - {0,2,4,6}, - {4,5,6,7}, - {2,3,7,6}, - {1,3,5,7}, - - {0,2,3,1}, - {0,1,5,4}, - {0,4,6,2}, - {7,6,4,5}, - {7,3,2,6}, - {7,5,1,3}, -*/ for (int i = 0; i < 6; i++) { @@ -607,9 +554,9 @@ PoolVector<Face3> Geometry::wrap_geometry(PoolVector<Face3> p_array, real_t *p_e } } - global_aabb.grow_by(0.01); // avoid numerical error + global_aabb.grow_by(0.01); // Avoid numerical error. - // determine amount of cells in grid axis + // Determine amount of cells in grid axis. int div_x, div_y, div_z; if (global_aabb.size.x / _MIN_SIZE < _MAX_LENGTH) @@ -632,7 +579,7 @@ PoolVector<Face3> Geometry::wrap_geometry(PoolVector<Face3> p_array, real_t *p_e voxelsize.y /= div_y; voxelsize.z /= div_z; - // create and initialize cells to zero + // Create and initialize cells to zero. uint8_t ***cell_status = memnew_arr(uint8_t **, div_x); for (int i = 0; i < div_x; i++) { @@ -650,7 +597,7 @@ PoolVector<Face3> Geometry::wrap_geometry(PoolVector<Face3> p_array, real_t *p_e } } - // plot faces into cells + // Plot faces into cells. for (int i = 0; i < face_count; i++) { @@ -662,7 +609,7 @@ PoolVector<Face3> Geometry::wrap_geometry(PoolVector<Face3> p_array, real_t *p_e _plot_face(cell_status, 0, 0, 0, div_x, div_y, div_z, voxelsize, f); } - // determine which cells connect to the outside by traversing the outside and recursively flood-fill marking + // Determine which cells connect to the outside by traversing the outside and recursively flood-fill marking. for (int i = 0; i < div_x; i++) { @@ -691,7 +638,7 @@ PoolVector<Face3> Geometry::wrap_geometry(PoolVector<Face3> p_array, real_t *p_e } } - // build faces for the inside-outside cell divisors + // Build faces for the inside-outside cell divisors. PoolVector<Face3> wrapped_faces; @@ -706,7 +653,7 @@ PoolVector<Face3> Geometry::wrap_geometry(PoolVector<Face3> p_array, real_t *p_e } } - // transform face vertices to global coords + // Transform face vertices to global coords. int wrapped_faces_count = wrapped_faces.size(); PoolVector<Face3>::Write wrapped_facesw = wrapped_faces.write(); @@ -753,7 +700,7 @@ Vector<Vector<Vector2> > Geometry::decompose_polygon_in_convex(Vector<Point2> po inp.SetOrientation(TRIANGULATOR_CCW); in_poly.push_back(inp); TriangulatorPartition tpart; - if (tpart.ConvexPartition_HM(&in_poly, &out_poly) == 0) { //failed! + if (tpart.ConvexPartition_HM(&in_poly, &out_poly) == 0) { // Failed. ERR_PRINT("Convex decomposing failed!"); return decomp; } @@ -765,7 +712,7 @@ Vector<Vector<Vector2> > Geometry::decompose_polygon_in_convex(Vector<Point2> po decomp.write[idx].resize(tp.GetNumPoints()); - for (int i = 0; i < tp.GetNumPoints(); i++) { + for (int64_t i = 0; i < tp.GetNumPoints(); i++) { decomp.write[idx].write[i] = tp.GetPoint(i); } @@ -781,7 +728,7 @@ Geometry::MeshData Geometry::build_convex_mesh(const PoolVector<Plane> &p_planes #define SUBPLANE_SIZE 1024.0 - real_t subplane_size = 1024.0; // should compute this from the actual plane + real_t subplane_size = 1024.0; // Should compute this from the actual plane. for (int i = 0; i < p_planes.size(); i++) { Plane p = p_planes[i]; @@ -789,7 +736,7 @@ Geometry::MeshData Geometry::build_convex_mesh(const PoolVector<Plane> &p_planes Vector3 ref = Vector3(0.0, 1.0, 0.0); if (ABS(p.normal.dot(ref)) > 0.95) - ref = Vector3(0.0, 0.0, 1.0); // change axis + ref = Vector3(0.0, 0.0, 1.0); // Change axis. Vector3 right = p.normal.cross(ref).normalized(); Vector3 up = p.normal.cross(right).normalized(); @@ -827,20 +774,20 @@ Geometry::MeshData Geometry::build_convex_mesh(const PoolVector<Plane> &p_planes real_t dist0 = clip.distance_to(edge0_A); real_t dist1 = clip.distance_to(edge1_A); - if (dist0 <= 0) { // behind plane + if (dist0 <= 0) { // Behind plane. new_vertices.push_back(vertices[k]); } - // check for different sides and non coplanar + // Check for different sides and non coplanar. if ((dist0 * dist1) < 0) { - // calculate intersection + // Calculate intersection. Vector3 rel = edge1_A - edge0_A; real_t den = clip.normal.dot(rel); if (Math::is_zero_approx(den)) - continue; // point too short + continue; // Point too short. real_t dist = -(clip.normal.dot(edge0_A) - clip.d) / den; Vector3 inters = edge0_A + rel * dist; @@ -854,11 +801,11 @@ Geometry::MeshData Geometry::build_convex_mesh(const PoolVector<Plane> &p_planes if (vertices.size() < 3) continue; - //result is a clockwise face + // Result is a clockwise face. MeshData::Face face; - // add face indices + // Add face indices. for (int j = 0; j < vertices.size(); j++) { int idx = -1; @@ -882,7 +829,7 @@ Geometry::MeshData Geometry::build_convex_mesh(const PoolVector<Plane> &p_planes face.plane = p; mesh.faces.push_back(face); - //add edge + // Add edge. for (int j = 0; j < face.indices.size(); j++) { @@ -972,7 +919,7 @@ PoolVector<Plane> Geometry::build_sphere_planes(real_t p_radius, int p_lats, int for (int j = 1; j <= p_lats; j++) { - //todo this is stupid, fix + // FIXME: This is stupid. Vector3 angle = normal.linear_interpolate(axis, j / (real_t)p_lats).normalized(); Vector3 pos = angle * p_radius; planes.push_back(Plane(pos, angle)); @@ -1032,12 +979,12 @@ struct _AtlasWorkRectResult { void Geometry::make_atlas(const Vector<Size2i> &p_rects, Vector<Point2i> &r_result, Size2i &r_size) { - //super simple, almost brute force scanline stacking fitter - //it's pretty basic for now, but it tries to make sure that the aspect ratio of the - //resulting atlas is somehow square. This is necessary because video cards have limits - //on texture size (usually 2048 or 4096), so the more square a texture, the more chances - //it will work in every hardware. - // for example, it will prioritize a 1024x1024 atlas (works everywhere) instead of a + // Super simple, almost brute force scanline stacking fitter. + // It's pretty basic for now, but it tries to make sure that the aspect ratio of the + // resulting atlas is somehow square. This is necessary because video cards have limits. + // On texture size (usually 2048 or 4096), so the more square a texture, the more chances. + // It will work in every hardware. + // For example, it will prioritize a 1024x1024 atlas (works everywhere) instead of a // 256x8192 atlas (won't work anywhere). ERR_FAIL_COND(p_rects.size() == 0); @@ -1066,7 +1013,7 @@ void Geometry::make_atlas(const Vector<Size2i> &p_rects, Vector<Point2i> &r_resu for (int j = 0; j < w; j++) hmax.write[j] = 0; - //place them + // Place them. int ofs = 0; int limit_h = 0; for (int j = 0; j < wrects.size(); j++) { @@ -1101,7 +1048,7 @@ void Geometry::make_atlas(const Vector<Size2i> &p_rects, Vector<Point2i> &r_resu if (end_w > max_w) max_w = end_w; - if (ofs == 0 || end_h > limit_h) //while h limit not reached, keep stacking + if (ofs == 0 || end_h > limit_h) // While h limit not reached, keep stacking. ofs += wrects[j].s.width; } @@ -1112,7 +1059,7 @@ void Geometry::make_atlas(const Vector<Size2i> &p_rects, Vector<Point2i> &r_resu results.push_back(result); } - //find the result with the best aspect ratio + // Find the result with the best aspect ratio. int best = -1; real_t best_aspect = 1e20; @@ -1152,7 +1099,7 @@ Vector<Vector<Point2> > Geometry::_polypaths_do_operation(PolyBooleanOperation p } Path path_a, path_b; - // Need to scale points (Clipper's requirement for robust computation) + // Need to scale points (Clipper's requirement for robust computation). for (int i = 0; i != p_polypath_a.size(); ++i) { path_a << IntPoint(p_polypath_a[i].x * SCALE_FACTOR, p_polypath_a[i].y * SCALE_FACTOR); } @@ -1160,19 +1107,19 @@ Vector<Vector<Point2> > Geometry::_polypaths_do_operation(PolyBooleanOperation p path_b << IntPoint(p_polypath_b[i].x * SCALE_FACTOR, p_polypath_b[i].y * SCALE_FACTOR); } Clipper clp; - clp.AddPath(path_a, ptSubject, !is_a_open); // forward compatible with Clipper 10.0.0 - clp.AddPath(path_b, ptClip, true); // polylines cannot be set as clip + clp.AddPath(path_a, ptSubject, !is_a_open); // Forward compatible with Clipper 10.0.0. + clp.AddPath(path_b, ptClip, true); // Polylines cannot be set as clip. Paths paths; if (is_a_open) { - PolyTree tree; // needed to populate polylines + PolyTree tree; // Needed to populate polylines. clp.Execute(op, tree); OpenPathsFromPolyTree(tree, paths); } else { - clp.Execute(op, paths); // works on closed polygons only + clp.Execute(op, paths); // Works on closed polygons only. } - // Have to scale points down now + // Have to scale points down now. Vector<Vector<Point2> > polypaths; for (Paths::size_type i = 0; i < paths.size(); ++i) { @@ -1214,16 +1161,16 @@ Vector<Vector<Point2> > Geometry::_polypath_offset(const Vector<Point2> &p_polyp ClipperOffset co; Path path; - // Need to scale points (Clipper's requirement for robust computation) + // Need to scale points (Clipper's requirement for robust computation). for (int i = 0; i != p_polypath.size(); ++i) { path << IntPoint(p_polypath[i].x * SCALE_FACTOR, p_polypath[i].y * SCALE_FACTOR); } co.AddPath(path, jt, et); Paths paths; - co.Execute(paths, p_delta * SCALE_FACTOR); // inflate/deflate + co.Execute(paths, p_delta * SCALE_FACTOR); // Inflate/deflate. - // Have to scale points down now + // Have to scale points down now. Vector<Vector<Point2> > polypaths; for (Paths::size_type i = 0; i < paths.size(); ++i) { diff --git a/core/math/geometry.h b/core/math/geometry.h index e4f3ff799e..8b0a51c651 100644 --- a/core/math/geometry.h +++ b/core/math/geometry.h @@ -41,47 +41,43 @@ #include "core/print_string.h" #include "core/vector.h" -/** - @author Juan Linietsky <reduzio@gmail.com> -*/ - class Geometry { Geometry(); public: static real_t get_closest_points_between_segments(const Vector2 &p1, const Vector2 &q1, const Vector2 &p2, const Vector2 &q2, Vector2 &c1, Vector2 &c2) { - Vector2 d1 = q1 - p1; // Direction vector of segment S1 - Vector2 d2 = q2 - p2; // Direction vector of segment S2 + Vector2 d1 = q1 - p1; // Direction vector of segment S1. + Vector2 d2 = q2 - p2; // Direction vector of segment S2. Vector2 r = p1 - p2; - real_t a = d1.dot(d1); // Squared length of segment S1, always nonnegative - real_t e = d2.dot(d2); // Squared length of segment S2, always nonnegative + real_t a = d1.dot(d1); // Squared length of segment S1, always nonnegative. + real_t e = d2.dot(d2); // Squared length of segment S2, always nonnegative. real_t f = d2.dot(r); real_t s, t; - // Check if either or both segments degenerate into points + // Check if either or both segments degenerate into points. if (a <= CMP_EPSILON && e <= CMP_EPSILON) { - // Both segments degenerate into points + // Both segments degenerate into points. c1 = p1; c2 = p2; return Math::sqrt((c1 - c2).dot(c1 - c2)); } if (a <= CMP_EPSILON) { - // First segment degenerates into a point + // First segment degenerates into a point. s = 0.0; t = f / e; // s = 0 => t = (b*s + f) / e = f / e t = CLAMP(t, 0.0, 1.0); } else { real_t c = d1.dot(r); if (e <= CMP_EPSILON) { - // Second segment degenerates into a point + // Second segment degenerates into a point. t = 0.0; s = CLAMP(-c / a, 0.0, 1.0); // t = 0 => s = (b*t - c) / a = -c / a } else { - // The general nondegenerate case starts here + // The general nondegenerate case starts here. real_t b = d1.dot(d2); - real_t denom = a * e - b * b; // Always nonnegative + real_t denom = a * e - b * b; // Always nonnegative. // If segments not parallel, compute closest point on L1 to L2 and - // clamp to segment S1. Else pick arbitrary s (here 0) + // clamp to segment S1. Else pick arbitrary s (here 0). if (denom != 0.0) { s = CLAMP((b * f - c * e) / denom, 0.0, 1.0); } else @@ -92,7 +88,7 @@ public: //If t in [0,1] done. Else clamp t, recompute s for the new value // of t using s = Dot((P2 + D2*t) - P1,D1) / Dot(D1,D1)= (t*b - c) / a - // and clamp s to [0, 1] + // and clamp s to [0, 1]. if (t < 0.0) { t = 0.0; s = CLAMP(-c / a, 0.0, 1.0); @@ -109,14 +105,14 @@ public: static void get_closest_points_between_segments(const Vector3 &p1, const Vector3 &p2, const Vector3 &q1, const Vector3 &q2, Vector3 &c1, Vector3 &c2) { -//do the function 'd' as defined by pb. I think is is dot product of some sort +// Do the function 'd' as defined by pb. I think is is dot product of some sort. #define d_of(m, n, o, p) ((m.x - n.x) * (o.x - p.x) + (m.y - n.y) * (o.y - p.y) + (m.z - n.z) * (o.z - p.z)) - //calculate the parametric position on the 2 curves, mua and mub + // Calculate the parametric position on the 2 curves, mua and mub. real_t mua = (d_of(p1, q1, q2, q1) * d_of(q2, q1, p2, p1) - d_of(p1, q1, p2, p1) * d_of(q2, q1, q2, q1)) / (d_of(p2, p1, p2, p1) * d_of(q2, q1, q2, q1) - d_of(q2, q1, p2, p1) * d_of(q2, q1, p2, p1)); real_t mub = (d_of(p1, q1, q2, q1) + mua * d_of(q2, q1, p2, p1)) / d_of(q2, q1, q2, q1); - //clip the value between [0..1] constraining the solution to lie on the original curves + // Clip the value between [0..1] constraining the solution to lie on the original curves. if (mua < 0) mua = 0; if (mub < 0) mub = 0; if (mua > 1) mua = 1; @@ -129,38 +125,38 @@ public: Vector3 u = p_to_a - p_from_a; Vector3 v = p_to_b - p_from_b; Vector3 w = p_from_a - p_to_a; - real_t a = u.dot(u); // always >= 0 + real_t a = u.dot(u); // Always >= 0 real_t b = u.dot(v); - real_t c = v.dot(v); // always >= 0 + real_t c = v.dot(v); // Always >= 0 real_t d = u.dot(w); real_t e = v.dot(w); - real_t D = a * c - b * b; // always >= 0 + real_t D = a * c - b * b; // Always >= 0 real_t sc, sN, sD = D; // sc = sN / sD, default sD = D >= 0 real_t tc, tN, tD = D; // tc = tN / tD, default tD = D >= 0 - // compute the line parameters of the two closest points - if (D < CMP_EPSILON) { // the lines are almost parallel - sN = 0.0; // force using point P0 on segment S1 - sD = 1.0; // to prevent possible division by 0.0 later + // Compute the line parameters of the two closest points. + if (D < CMP_EPSILON) { // The lines are almost parallel. + sN = 0.0; // Force using point P0 on segment S1 + sD = 1.0; // to prevent possible division by 0.0 later. tN = e; tD = c; - } else { // get the closest points on the infinite lines + } else { // Get the closest points on the infinite lines sN = (b * e - c * d); tN = (a * e - b * d); - if (sN < 0.0) { // sc < 0 => the s=0 edge is visible + if (sN < 0.0) { // sc < 0 => the s=0 edge is visible. sN = 0.0; tN = e; tD = c; - } else if (sN > sD) { // sc > 1 => the s=1 edge is visible + } else if (sN > sD) { // sc > 1 => the s=1 edge is visible. sN = sD; tN = e + b; tD = c; } } - if (tN < 0.0) { // tc < 0 => the t=0 edge is visible + if (tN < 0.0) { // tc < 0 => the t=0 edge is visible. tN = 0.0; - // recompute sc for this edge + // Recompute sc for this edge. if (-d < 0.0) sN = 0.0; else if (-d > a) @@ -169,9 +165,9 @@ public: sN = -d; sD = a; } - } else if (tN > tD) { // tc > 1 => the t=1 edge is visible + } else if (tN > tD) { // tc > 1 => the t=1 edge is visible. tN = tD; - // recompute sc for this edge + // Recompute sc for this edge. if ((-d + b) < 0.0) sN = 0; else if ((-d + b) > a) @@ -181,14 +177,14 @@ public: sD = a; } } - // finally do the division to get sc and tc + // Finally do the division to get sc and tc. sc = (Math::is_zero_approx(sN) ? 0.0 : sN / sD); tc = (Math::is_zero_approx(tN) ? 0.0 : tN / tD); - // get the difference of the two closest points + // Get the difference of the two closest points. Vector3 dP = w + (sc * u) - (tc * v); // = S1(sc) - S2(tc) - return dP.length(); // return the closest distance + return dP.length(); // Return the closest distance. } static inline bool ray_intersects_triangle(const Vector3 &p_from, const Vector3 &p_dir, const Vector3 &p_v0, const Vector3 &p_v1, const Vector3 &p_v2, Vector3 *r_res = 0) { @@ -196,7 +192,7 @@ public: Vector3 e2 = p_v2 - p_v0; Vector3 h = p_dir.cross(e2); real_t a = e1.dot(h); - if (Math::is_zero_approx(a)) // parallel test + if (Math::is_zero_approx(a)) // Parallel test. return false; real_t f = 1.0 / a; @@ -214,16 +210,15 @@ public: if (v < 0.0 || u + v > 1.0) return false; - // at this stage we can compute t to find out where - // the intersection point is on the line + // At this stage we can compute t to find out where + // the intersection point is on the line. real_t t = f * e2.dot(q); if (t > 0.00001) { // ray intersection if (r_res) *r_res = p_from + p_dir * t; return true; - } else // this means that there is a line intersection - // but not a ray intersection + } else // This means that there is a line intersection but not a ray intersection. return false; } @@ -234,7 +229,7 @@ public: Vector3 e2 = p_v2 - p_v0; Vector3 h = rel.cross(e2); real_t a = e1.dot(h); - if (Math::is_zero_approx(a)) // parallel test + if (Math::is_zero_approx(a)) // Parallel test. return false; real_t f = 1.0 / a; @@ -252,16 +247,15 @@ public: if (v < 0.0 || u + v > 1.0) return false; - // at this stage we can compute t to find out where - // the intersection point is on the line + // At this stage we can compute t to find out where + // the intersection point is on the line. real_t t = f * e2.dot(q); - if (t > CMP_EPSILON && t <= 1.0) { // ray intersection + if (t > CMP_EPSILON && t <= 1.0) { // Ray intersection. if (r_res) *r_res = p_from + rel * t; return true; - } else // this means that there is a line intersection - // but not a ray intersection + } else // This means that there is a line intersection but not a ray intersection. return false; } @@ -271,13 +265,11 @@ public: Vector3 rel = (p_to - p_from); real_t rel_l = rel.length(); if (rel_l < CMP_EPSILON) - return false; // both points are the same + return false; // Both points are the same. Vector3 normal = rel / rel_l; real_t sphere_d = normal.dot(sphere_pos); - //Vector3 ray_closest=normal*sphere_d; - real_t ray_distance = sphere_pos.distance_to(normal * sphere_d); if (ray_distance >= p_sphere_radius) @@ -289,7 +281,7 @@ public: if (inters_d2 >= CMP_EPSILON) inters_d -= Math::sqrt(inters_d2); - // check in segment + // Check in segment. if (inters_d < 0 || inters_d > rel_l) return false; @@ -308,9 +300,9 @@ public: Vector3 rel = (p_to - p_from); real_t rel_l = rel.length(); if (rel_l < CMP_EPSILON) - return false; // both points are the same + return false; // Both points are the same. - // first check if they are parallel + // First check if they are parallel. Vector3 normal = (rel / rel_l); Vector3 crs = normal.cross(Vector3(0, 0, 1)); real_t crs_l = crs.length(); @@ -318,8 +310,7 @@ public: Vector3 z_dir; if (crs_l < CMP_EPSILON) { - //blahblah parallel - z_dir = Vector3(1, 0, 0); //any x/y vector ok + z_dir = Vector3(1, 0, 0); // Any x/y vector OK. } else { z_dir = crs / crs_l; } @@ -327,12 +318,12 @@ public: real_t dist = z_dir.dot(p_from); if (dist >= p_radius) - return false; // too far away + return false; // Too far away. - // convert to 2D + // Convert to 2D. real_t w2 = p_radius * p_radius - dist * dist; if (w2 < CMP_EPSILON) - return false; //avoid numerical error + return false; // Avoid numerical error. Size2 size(Math::sqrt(w2), p_height * 0.5); Vector3 x_dir = z_dir.cross(Vector3(0, 0, 1)).normalized(); @@ -379,7 +370,7 @@ public: return false; } - // convert to 3D again + // Convert to 3D again. Vector3 result = p_from + (rel * min); Vector3 res_normal = result; @@ -420,19 +411,18 @@ public: real_t den = p.normal.dot(dir); - //printf("den is %i\n",den); if (Math::abs(den) <= CMP_EPSILON) - continue; // ignore parallel plane + continue; // Ignore parallel plane. real_t dist = -p.distance_to(p_from) / den; if (den > 0) { - //backwards facing plane + // Backwards facing plane. if (dist < max) max = dist; } else { - //front facing plane + // Front facing plane. if (dist > min) { min = dist; min_index = i; @@ -440,8 +430,8 @@ public: } } - if (max <= min || min < 0 || min > rel_l || min_index == -1) // exit conditions - return false; // no intersection + if (max <= min || min < 0 || min > rel_l || min_index == -1) // Exit conditions. + return false; // No intersection. if (p_res) *p_res = p_from + dir * min; @@ -457,16 +447,16 @@ public: Vector3 n = p_segment[1] - p_segment[0]; real_t l2 = n.length_squared(); if (l2 < 1e-20) - return p_segment[0]; // both points are the same, just give any + return p_segment[0]; // Both points are the same, just give any. real_t d = n.dot(p) / l2; if (d <= 0.0) - return p_segment[0]; // before first point + return p_segment[0]; // Before first point. else if (d >= 1.0) - return p_segment[1]; // after first point + return p_segment[1]; // After first point. else - return p_segment[0] + n * d; // inside + return p_segment[0] + n * d; // Inside. } static Vector3 get_closest_point_to_segment_uncapped(const Vector3 &p_point, const Vector3 *p_segment) { @@ -475,11 +465,11 @@ public: Vector3 n = p_segment[1] - p_segment[0]; real_t l2 = n.length_squared(); if (l2 < 1e-20) - return p_segment[0]; // both points are the same, just give any + return p_segment[0]; // Both points are the same, just give any. real_t d = n.dot(p) / l2; - return p_segment[0] + n * d; // inside + return p_segment[0] + n * d; // Inside. } static Vector2 get_closest_point_to_segment_2d(const Vector2 &p_point, const Vector2 *p_segment) { @@ -488,16 +478,16 @@ public: Vector2 n = p_segment[1] - p_segment[0]; real_t l2 = n.length_squared(); if (l2 < 1e-20) - return p_segment[0]; // both points are the same, just give any + return p_segment[0]; // Both points are the same, just give any. real_t d = n.dot(p) / l2; if (d <= 0.0) - return p_segment[0]; // before first point + return p_segment[0]; // Before first point. else if (d >= 1.0) - return p_segment[1]; // after first point + return p_segment[1]; // After first point. else - return p_segment[0] + n * d; // inside + return p_segment[0] + n * d; // Inside. } static bool is_point_in_triangle(const Vector2 &s, const Vector2 &a, const Vector2 &b, const Vector2 &c) { @@ -512,27 +502,25 @@ public: return (cn.cross(an) > 0) == orientation; } - //static bool is_point_in_polygon(const Vector2 &p_point, const Vector<Vector2> &p_polygon); - static Vector2 get_closest_point_to_segment_uncapped_2d(const Vector2 &p_point, const Vector2 *p_segment) { Vector2 p = p_point - p_segment[0]; Vector2 n = p_segment[1] - p_segment[0]; real_t l2 = n.length_squared(); if (l2 < 1e-20) - return p_segment[0]; // both points are the same, just give any + return p_segment[0]; // Both points are the same, just give any. real_t d = n.dot(p) / l2; - return p_segment[0] + n * d; // inside + return p_segment[0] + n * d; // Inside. } static bool line_intersects_line_2d(const Vector2 &p_from_a, const Vector2 &p_dir_a, const Vector2 &p_from_b, const Vector2 &p_dir_b, Vector2 &r_result) { - // see http://paulbourke.net/geometry/pointlineplane/ + // See http://paulbourke.net/geometry/pointlineplane/ const real_t denom = p_dir_b.y * p_dir_a.x - p_dir_b.x * p_dir_a.y; - if (Math::is_zero_approx(denom)) { // parallel? + if (Math::is_zero_approx(denom)) { // Parallel? return false; } @@ -560,11 +548,11 @@ public: real_t ABpos = D.x + (C.x - D.x) * D.y / (D.y - C.y); - // Fail if segment C-D crosses line A-B outside of segment A-B. + // Fail if segment C-D crosses line A-B outside of segment A-B. if (ABpos < 0 || ABpos > 1.0) return false; - // (4) Apply the discovered position to line A-B in the original coordinate system. + // (4) Apply the discovered position to line A-B in the original coordinate system. if (r_result) *r_result = p_from_a + B * ABpos; @@ -597,7 +585,7 @@ public: real_t d = p_normal.dot(p_sphere_pos) - p_normal.dot(p_triangle[0]); - if (d > p_sphere_radius || d < -p_sphere_radius) // not touching the plane of the face, return + if (d > p_sphere_radius || d < -p_sphere_radius) // Not touching the plane of the face, return. return false; Vector3 contact = p_sphere_pos - (p_normal * d); @@ -617,25 +605,25 @@ public: for (int i = 0; i < 3; i++) { - // check edge cylinder + // Check edge cylinder. Vector3 n1 = verts[i] - verts[i + 1]; Vector3 n2 = p_sphere_pos - verts[i + 1]; - ///@TODO i could discard by range here to make the algorithm quicker? dunno.. + ///@TODO Maybe discard by range here to make the algorithm quicker. - // check point within cylinder radius + // Check point within cylinder radius. Vector3 axis = n1.cross(n2).cross(n1); - axis.normalize(); // ugh + axis.normalize(); real_t ad = axis.dot(n2); if (ABS(ad) > p_sphere_radius) { - // no chance with this edge, too far away + // No chance with this edge, too far away. continue; } - // check point within edge capsule cylinder + // Check point within edge capsule cylinder. /** 4th TEST INSIDE EDGE POINTS **/ real_t sphere_at = n1.dot(n2); @@ -644,8 +632,7 @@ public: r_triangle_contact = p_sphere_pos - axis * (axis.dot(n2)); r_sphere_contact = p_sphere_pos - axis * p_sphere_radius; - // point inside here - //printf("solved inside edge\n"); + // Point inside here. return true; } @@ -655,48 +642,51 @@ public: Vector3 n = (p_sphere_pos - verts[i + 1]).normalized(); - //r_triangle_contact=verts[i+1]+n*p_sphere_radius;p_sphere_pos+axis*(p_sphere_radius-axis.dot(n2)); r_triangle_contact = verts[i + 1]; r_sphere_contact = p_sphere_pos - n * p_sphere_radius; - //printf("solved inside point segment 1\n"); return true; } if (n2.distance_squared_to(n1) < r2) { Vector3 n = (p_sphere_pos - verts[i]).normalized(); - //r_triangle_contact=verts[i]+n*p_sphere_radius;p_sphere_pos+axis*(p_sphere_radius-axis.dot(n2)); r_triangle_contact = verts[i]; r_sphere_contact = p_sphere_pos - n * p_sphere_radius; - //printf("solved inside point segment 1\n"); return true; } - break; // It's pointless to continue at this point, so save some cpu cycles + break; // It's pointless to continue at this point, so save some CPU cycles. } return false; } + static inline bool is_point_in_circle(const Vector2 &p_point, const Vector2 &p_circle_pos, real_t p_circle_radius) { + + return p_point.distance_squared_to(p_circle_pos) <= p_circle_radius * p_circle_radius; + } + static real_t segment_intersects_circle(const Vector2 &p_from, const Vector2 &p_to, const Vector2 &p_circle_pos, real_t p_circle_radius) { Vector2 line_vec = p_to - p_from; Vector2 vec_to_line = p_from - p_circle_pos; - /* create a quadratic formula of the form ax^2 + bx + c = 0 */ + // Create a quadratic formula of the form ax^2 + bx + c = 0 real_t a, b, c; a = line_vec.dot(line_vec); b = 2 * vec_to_line.dot(line_vec); c = vec_to_line.dot(vec_to_line) - p_circle_radius * p_circle_radius; - /* solve for t */ + // Solve for t. real_t sqrtterm = b * b - 4 * a * c; - /* if the term we intend to square root is less than 0 then the answer won't be real, so it definitely won't be t in the range 0 to 1 */ + // If the term we intend to square root is less than 0 then the answer won't be real, + // so it definitely won't be t in the range 0 to 1. if (sqrtterm < 0) return -1; - /* if we can assume that the line segment starts outside the circle (e.g. for continuous time collision detection) then the following can be skipped and we can just return the equivalent of res1 */ + // If we can assume that the line segment starts outside the circle (e.g. for continuous time collision detection) + // then the following can be skipped and we can just return the equivalent of res1. sqrtterm = Math::sqrt(sqrtterm); real_t res1 = (-b - sqrtterm) / (2 * a); real_t res2 = (-b + sqrtterm) / (2 * a); @@ -722,7 +712,6 @@ public: int outside_count = 0; for (int a = 0; a < polygon.size(); a++) { - //real_t p_plane.d = (*this) * polygon[a]; real_t dist = p_plane.distance_to(polygon[a]); if (dist < -CMP_POINT_IN_PLANE_EPSILON) { location_cache[a] = LOC_INSIDE; @@ -739,11 +728,11 @@ public: if (outside_count == 0) { - return polygon; // no changes + return polygon; // No changes. } else if (inside_count == 0) { - return Vector<Vector3>(); //empty + return Vector<Vector3>(); // Empty. } long previous = polygon.size() - 1; @@ -838,22 +827,11 @@ public: static Vector<Vector<Point2> > offset_polyline_2d(const Vector<Vector2> &p_polygon, real_t p_delta, PolyJoinType p_join_type, PolyEndType p_end_type) { - ERR_EXPLAIN("Attempt to offset a polyline like a polygon (use offset_polygon_2d instead)."); - ERR_FAIL_COND_V(p_end_type == END_POLYGON, Vector<Vector<Point2> >()); + ERR_FAIL_COND_V_MSG(p_end_type == END_POLYGON, Vector<Vector<Point2> >(), "Attempt to offset a polyline like a polygon (use offset_polygon_2d instead)."); return _polypath_offset(p_polygon, p_delta, p_join_type, p_end_type); } - static Vector<Point2> transform_points_2d(const Vector<Point2> &p_points, const Transform2D &p_mat) { - - Vector<Point2> points; - - for (int i = 0; i < p_points.size(); ++i) { - points.push_back(p_mat.xform(p_points[i])); - } - return points; - } - static Vector<int> triangulate_delaunay_2d(const Vector<Vector2> &p_points) { Vector<Delaunay2D::Triangle> tr = Delaunay2D::triangulate(p_points); @@ -899,7 +877,7 @@ public: return sum > 0.0f; } - /* alternate implementation that should be faster */ + // Alternate implementation that should be faster. static bool is_point_in_polygon(const Vector2 &p_point, const Vector<Vector2> &p_polygon) { int c = p_polygon.size(); if (c < 3) @@ -915,7 +893,8 @@ public: further_away_opposite.y = MIN(p[i].y, further_away_opposite.y); } - further_away += (further_away - further_away_opposite) * Vector2(1.221313, 1.512312); // make point outside that won't intersect with points in segment from p_point + // Make point outside that won't intersect with points in segment from p_point. + further_away += (further_away - further_away_opposite) * Vector2(1.221313, 1.512312); int intersections = 0; for (int i = 0; i < c; i++) { @@ -931,7 +910,8 @@ public: static PoolVector<PoolVector<Face3> > separate_objects(PoolVector<Face3> p_array); - static PoolVector<Face3> wrap_geometry(PoolVector<Face3> p_array, real_t *p_error = NULL); ///< create a "wrap" that encloses the given geometry + // Create a "wrap" that encloses the given geometry. + static PoolVector<Face3> wrap_geometry(PoolVector<Face3> p_array, real_t *p_error = NULL); struct MeshData { @@ -1013,17 +993,17 @@ public: Vector<Point2> H; H.resize(2 * n); - // Sort points lexicographically + // Sort points lexicographically. P.sort(); - // Build lower hull + // Build lower hull. for (int i = 0; i < n; ++i) { while (k >= 2 && vec2_cross(H[k - 2], H[k - 1], P[i]) <= 0) k--; H.write[k++] = P[i]; } - // Build upper hull + // Build upper hull. for (int i = n - 2, t = k + 1; i >= 0; i--) { while (k >= t && vec2_cross(H[k - 2], H[k - 1], P[i]) <= 0) k--; diff --git a/core/math/math_funcs.cpp b/core/math/math_funcs.cpp index f04e40cb6c..50fcdb2c13 100644 --- a/core/math/math_funcs.cpp +++ b/core/math/math_funcs.cpp @@ -30,6 +30,8 @@ #include "math_funcs.h" +#include "core/error_macros.h" + RandomPCG Math::default_rand(RandomPCG::DEFAULT_SEED, RandomPCG::DEFAULT_INC); #define PHI 0x9e3779b9 diff --git a/core/math/math_funcs.h b/core/math/math_funcs.h index a712356ddc..9078abea68 100644 --- a/core/math/math_funcs.h +++ b/core/math/math_funcs.h @@ -255,16 +255,16 @@ public: static _ALWAYS_INLINE_ float round(float p_val) { return (p_val >= 0) ? Math::floor(p_val + 0.5) : -Math::floor(-p_val + 0.5); } static _ALWAYS_INLINE_ int64_t wrapi(int64_t value, int64_t min, int64_t max) { - int64_t rng = max - min; - return (rng != 0) ? min + ((((value - min) % rng) + rng) % rng) : min; + int64_t range = max - min; + return range == 0 ? min : min + ((((value - min) % range) + range) % range); } static _ALWAYS_INLINE_ double wrapf(double value, double min, double max) { - double rng = max - min; - return (!is_equal_approx(rng, 0.0)) ? value - (rng * Math::floor((value - min) / rng)) : min; + double range = max - min; + return is_zero_approx(range) ? min : value - (range * Math::floor((value - min) / range)); } static _ALWAYS_INLINE_ float wrapf(float value, float min, float max) { - float rng = max - min; - return (!is_equal_approx(rng, 0.0f)) ? value - (rng * Math::floor((value - min) / rng)) : min; + float range = max - min; + return is_zero_approx(range) ? min : value - (range * Math::floor((value - min) / range)); } // double only, as these functions are mainly used by the editor and not performance-critical, @@ -300,6 +300,11 @@ public: } static _ALWAYS_INLINE_ bool is_equal_approx(real_t a, real_t b) { + // Check for exact equality first, required to handle "infinity" values. + if (a == b) { + return true; + } + // Then check for approximate equality. real_t tolerance = CMP_EPSILON * abs(a); if (tolerance < CMP_EPSILON) { tolerance = CMP_EPSILON; @@ -308,6 +313,11 @@ public: } static _ALWAYS_INLINE_ bool is_equal_approx(real_t a, real_t b, real_t tolerance) { + // Check for exact equality first, required to handle "infinity" values. + if (a == b) { + return true; + } + // Then check for approximate equality. return abs(a - b) < tolerance; } diff --git a/core/math/octree.h b/core/math/octree.h index d6fc9776bc..db15c8a1f8 100644 --- a/core/math/octree.h +++ b/core/math/octree.h @@ -38,10 +38,6 @@ #include "core/print_string.h" #include "core/variant.h" -/** - @author Juan Linietsky <reduzio@gmail.com> -*/ - typedef uint32_t OctreeElementID; #define OCTREE_ELEMENT_INVALID_ID 0 @@ -568,10 +564,7 @@ void Octree<T, use_pairs, AL>::_ensure_valid_root(const AABB &p_aabb) { while (!base.encloses(p_aabb)) { - if (base.size.x > OCTREE_SIZE_LIMIT) { - ERR_EXPLAIN("Octree upper size limit reeached, does the AABB supplied contain NAN?"); - ERR_FAIL(); - } + ERR_FAIL_COND_MSG(base.size.x > OCTREE_SIZE_LIMIT, "Octree upper size limit reached, does the AABB supplied contain NAN?"); Octant *gp = memnew_allocator(Octant, AL); octant_count++; diff --git a/core/math/quat.h b/core/math/quat.h index 8ed2fa7cc2..3d6602e466 100644 --- a/core/math/quat.h +++ b/core/math/quat.h @@ -38,10 +38,6 @@ #include "core/math/math_funcs.h" #include "core/ustring.h" -/** - @author Juan Linietsky <reduzio@gmail.com> -*/ - class Quat { public: real_t x, y, z, w; diff --git a/core/math/rect2.h b/core/math/rect2.h index d636aa223f..f58756ee40 100644 --- a/core/math/rect2.h +++ b/core/math/rect2.h @@ -99,8 +99,8 @@ struct Rect2 { inline bool encloses(const Rect2 &p_rect) const { return (p_rect.position.x >= position.x) && (p_rect.position.y >= position.y) && - ((p_rect.position.x + p_rect.size.x) < (position.x + size.x)) && - ((p_rect.position.y + p_rect.size.y) < (position.y + size.y)); + ((p_rect.position.x + p_rect.size.x) <= (position.x + size.x)) && + ((p_rect.position.y + p_rect.size.y) <= (position.y + size.y)); } _FORCE_INLINE_ bool has_no_area() const { diff --git a/core/math/transform.cpp b/core/math/transform.cpp index 7ff7cac914..4056975da8 100644 --- a/core/math/transform.cpp +++ b/core/math/transform.cpp @@ -213,3 +213,8 @@ Transform::Transform(const Basis &p_basis, const Vector3 &p_origin) : basis(p_basis), origin(p_origin) { } + +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 2f43f6b035..90e2b07583 100644 --- a/core/math/transform.h +++ b/core/math/transform.h @@ -34,10 +34,7 @@ #include "core/math/aabb.h" #include "core/math/basis.h" #include "core/math/plane.h" - -/** - @author Juan Linietsky <reduzio@gmail.com> -*/ +#include "core/pool_vector.h" class Transform { public: @@ -86,6 +83,9 @@ public: _FORCE_INLINE_ AABB xform(const AABB &p_aabb) const; _FORCE_INLINE_ AABB xform_inv(const AABB &p_aabb) const; + _FORCE_INLINE_ PoolVector<Vector3> xform(const PoolVector<Vector3> &p_array) const; + _FORCE_INLINE_ PoolVector<Vector3> xform_inv(const PoolVector<Vector3> &p_array) const; + void operator*=(const Transform &p_transform); Transform operator*(const Transform &p_transform) const; @@ -108,6 +108,7 @@ 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() {} }; @@ -157,22 +158,29 @@ _FORCE_INLINE_ Plane Transform::xform_inv(const Plane &p_plane) const { } _FORCE_INLINE_ AABB Transform::xform(const AABB &p_aabb) const { - /* define vertices */ - Vector3 x = basis.get_axis(0) * p_aabb.size.x; - Vector3 y = basis.get_axis(1) * p_aabb.size.y; - Vector3 z = basis.get_axis(2) * p_aabb.size.z; - Vector3 pos = xform(p_aabb.position); - //could be even further optimized - AABB new_aabb; - new_aabb.position = pos; - new_aabb.expand_to(pos + x); - new_aabb.expand_to(pos + y); - new_aabb.expand_to(pos + z); - new_aabb.expand_to(pos + x + y); - new_aabb.expand_to(pos + x + z); - new_aabb.expand_to(pos + y + z); - new_aabb.expand_to(pos + x + y + z); - return new_aabb; + + /* http://dev.theomader.com/transform-bounding-boxes/ */ + Vector3 min = p_aabb.position; + Vector3 max = p_aabb.position + p_aabb.size; + Vector3 tmin, tmax; + for (int i = 0; i < 3; i++) { + tmin[i] = tmax[i] = origin[i]; + for (int j = 0; j < 3; j++) { + real_t e = basis[i][j] * min[j]; + real_t f = basis[i][j] * max[j]; + if (e < f) { + tmin[i] += e; + tmax[i] += f; + } else { + tmin[i] += f; + tmax[i] += e; + } + } + } + AABB r_aabb; + r_aabb.position = tmin; + r_aabb.size = tmax - tmin; + return r_aabb; } _FORCE_INLINE_ AABB Transform::xform_inv(const AABB &p_aabb) const { @@ -201,4 +209,32 @@ _FORCE_INLINE_ AABB Transform::xform_inv(const AABB &p_aabb) const { return ret; } +PoolVector<Vector3> Transform::xform(const PoolVector<Vector3> &p_array) const { + + PoolVector<Vector3> array; + array.resize(p_array.size()); + + PoolVector<Vector3>::Read r = p_array.read(); + PoolVector<Vector3>::Write w = array.write(); + + for (int i = 0; i < p_array.size(); ++i) { + w[i] = xform(r[i]); + } + return array; +} + +PoolVector<Vector3> Transform::xform_inv(const PoolVector<Vector3> &p_array) const { + + PoolVector<Vector3> array; + array.resize(p_array.size()); + + PoolVector<Vector3>::Read r = p_array.read(); + PoolVector<Vector3>::Write w = array.write(); + + for (int i = 0; i < p_array.size(); ++i) { + w[i] = xform_inv(r[i]); + } + return array; +} + #endif // TRANSFORM_H diff --git a/core/math/transform_2d.h b/core/math/transform_2d.h index c44678674a..e8b44ab197 100644 --- a/core/math/transform_2d.h +++ b/core/math/transform_2d.h @@ -32,6 +32,7 @@ #define TRANSFORM_2D_H #include "core/math/rect2.h" // also includes vector2, math_funcs, and ustring +#include "core/pool_vector.h" struct Transform2D { // Warning #1: basis of Transform2D is stored differently from Basis. In terms of elements array, the basis matrix looks like "on paper": @@ -110,6 +111,8 @@ struct Transform2D { _FORCE_INLINE_ Vector2 xform_inv(const Vector2 &p_vec) const; _FORCE_INLINE_ Rect2 xform(const Rect2 &p_rect) const; _FORCE_INLINE_ Rect2 xform_inv(const Rect2 &p_rect) const; + _FORCE_INLINE_ PoolVector<Vector2> xform(const PoolVector<Vector2> &p_array) const; + _FORCE_INLINE_ PoolVector<Vector2> xform_inv(const PoolVector<Vector2> &p_array) const; operator String() const; @@ -199,4 +202,32 @@ Rect2 Transform2D::xform_inv(const Rect2 &p_rect) const { return new_rect; } +PoolVector<Vector2> Transform2D::xform(const PoolVector<Vector2> &p_array) const { + + PoolVector<Vector2> array; + array.resize(p_array.size()); + + PoolVector<Vector2>::Read r = p_array.read(); + PoolVector<Vector2>::Write w = array.write(); + + for (int i = 0; i < p_array.size(); ++i) { + w[i] = xform(r[i]); + } + return array; +} + +PoolVector<Vector2> Transform2D::xform_inv(const PoolVector<Vector2> &p_array) const { + + PoolVector<Vector2> array; + array.resize(p_array.size()); + + PoolVector<Vector2>::Read r = p_array.read(); + PoolVector<Vector2>::Write w = array.write(); + + for (int i = 0; i < p_array.size(); ++i) { + w[i] = xform_inv(r[i]); + } + return array; +} + #endif // TRANSFORM_2D_H diff --git a/core/math/vector2.cpp b/core/math/vector2.cpp index 779a28be66..972bccc0ac 100644 --- a/core/math/vector2.cpp +++ b/core/math/vector2.cpp @@ -98,6 +98,11 @@ real_t Vector2::cross(const Vector2 &p_other) const { return x * p_other.y - y * p_other.x; } +Vector2 Vector2::sign() const { + + return Vector2(SGN(x), SGN(y)); +} + Vector2 Vector2::floor() const { return Vector2(Math::floor(x), Math::floor(y)); @@ -121,6 +126,14 @@ Vector2 Vector2::rotated(real_t p_by) const { return v; } +Vector2 Vector2::posmod(const real_t p_mod) const { + return Vector2(Math::fposmod(x, p_mod), Math::fposmod(y, p_mod)); +} + +Vector2 Vector2::posmodv(const Vector2 &p_modv) const { + return Vector2(Math::fposmod(x, p_modv.x), Math::fposmod(y, p_modv.y)); +} + Vector2 Vector2::project(const Vector2 &p_b) const { return p_b * (dot(p_b) / p_b.length_squared()); } diff --git a/core/math/vector2.h b/core/math/vector2.h index 78a1641c1e..1a73831891 100644 --- a/core/math/vector2.h +++ b/core/math/vector2.h @@ -38,6 +38,11 @@ struct Vector2i; struct Vector2 { + enum Axis { + AXIS_X, + AXIS_Y, + }; + union { real_t x; real_t width; @@ -69,6 +74,8 @@ struct Vector2 { real_t dot(const Vector2 &p_other) const; real_t cross(const Vector2 &p_other) const; + Vector2 posmod(const real_t p_mod) const; + Vector2 posmodv(const Vector2 &p_modv) const; Vector2 project(const Vector2 &p_b) const; Vector2 plane_project(real_t p_d, const Vector2 &p_vec) const; @@ -107,8 +114,10 @@ struct Vector2 { bool operator==(const Vector2 &p_vec2) const; bool operator!=(const Vector2 &p_vec2) const; - bool operator<(const Vector2 &p_vec2) const { return (Math::is_equal_approx(x, p_vec2.x)) ? (y < p_vec2.y) : (x < p_vec2.x); } - bool operator<=(const Vector2 &p_vec2) const { return (Math::is_equal_approx(x, p_vec2.x)) ? (y <= p_vec2.y) : (x < p_vec2.x); } + bool operator<(const Vector2 &p_vec2) const { return Math::is_equal_approx(x, p_vec2.x) ? (y < p_vec2.y) : (x < p_vec2.x); } + bool operator>(const Vector2 &p_vec2) const { return Math::is_equal_approx(x, p_vec2.x) ? (y > p_vec2.y) : (x > p_vec2.x); } + bool operator<=(const Vector2 &p_vec2) const { return Math::is_equal_approx(x, p_vec2.x) ? (y <= p_vec2.y) : (x < p_vec2.x); } + bool operator>=(const Vector2 &p_vec2) const { return Math::is_equal_approx(x, p_vec2.x) ? (y >= p_vec2.y) : (x > p_vec2.x); } real_t angle() const; @@ -129,6 +138,7 @@ struct Vector2 { return Vector2(y, -x); } + Vector2 sign() const; Vector2 floor() const; Vector2 ceil() const; Vector2 round() const; @@ -141,10 +151,7 @@ struct Vector2 { x = p_x; y = p_y; } - _FORCE_INLINE_ Vector2() { - x = 0; - y = 0; - } + _FORCE_INLINE_ Vector2() { x = y = 0; } }; _FORCE_INLINE_ Vector2 Vector2::plane_project(real_t p_d, const Vector2 &p_vec) const { @@ -262,6 +269,11 @@ typedef Vector2 Point2; struct Vector2i { + enum Axis { + AXIS_X, + AXIS_Y, + }; + union { int x; int width; diff --git a/core/math/vector3.cpp b/core/math/vector3.cpp index 73927821cf..ebc1599820 100644 --- a/core/math/vector3.cpp +++ b/core/math/vector3.cpp @@ -134,6 +134,21 @@ Vector3 Vector3::move_toward(const Vector3 &p_to, const real_t p_delta) const { return len <= p_delta || len < CMP_EPSILON ? p_to : v + vd / len * p_delta; } +Basis Vector3::outer(const Vector3 &p_b) const { + + Vector3 row0(x * p_b.x, x * p_b.y, x * p_b.z); + Vector3 row1(y * p_b.x, y * p_b.y, y * p_b.z); + Vector3 row2(z * p_b.x, z * p_b.y, z * p_b.z); + + return Basis(row0, row1, row2); +} + +Basis Vector3::to_diagonal_matrix() const { + return Basis(x, 0, 0, + 0, y, 0, + 0, 0, z); +} + Vector3::operator String() const { return (rtos(x) + ", " + rtos(y) + ", " + rtos(z)); diff --git a/core/math/vector3.h b/core/math/vector3.h index 45bdfee487..de1743d88f 100644 --- a/core/math/vector3.h +++ b/core/math/vector3.h @@ -31,9 +31,7 @@ #ifndef VECTOR3_H #define VECTOR3_H -#include "core/math/math_defs.h" #include "core/math/math_funcs.h" -#include "core/typedefs.h" #include "core/ustring.h" class Basis; @@ -98,8 +96,8 @@ struct Vector3 { _FORCE_INLINE_ Vector3 cross(const Vector3 &p_b) const; _FORCE_INLINE_ real_t dot(const Vector3 &p_b) const; - _FORCE_INLINE_ Basis outer(const Vector3 &p_b) const; - _FORCE_INLINE_ Basis to_diagonal_matrix() const; + Basis outer(const Vector3 &p_b) const; + Basis to_diagonal_matrix() const; _FORCE_INLINE_ Vector3 abs() const; _FORCE_INLINE_ Vector3 floor() const; @@ -110,6 +108,8 @@ struct Vector3 { _FORCE_INLINE_ real_t distance_to(const Vector3 &p_b) const; _FORCE_INLINE_ real_t distance_squared_to(const Vector3 &p_b) const; + _FORCE_INLINE_ Vector3 posmod(const real_t p_mod) const; + _FORCE_INLINE_ Vector3 posmodv(const Vector3 &p_modv) const; _FORCE_INLINE_ Vector3 project(const Vector3 &p_b) const; _FORCE_INLINE_ real_t angle_to(const Vector3 &p_b) const; @@ -141,20 +141,19 @@ struct Vector3 { _FORCE_INLINE_ bool operator!=(const Vector3 &p_v) const; _FORCE_INLINE_ bool operator<(const Vector3 &p_v) const; _FORCE_INLINE_ bool operator<=(const Vector3 &p_v) const; + _FORCE_INLINE_ bool operator>(const Vector3 &p_v) const; + _FORCE_INLINE_ bool operator>=(const Vector3 &p_v) const; operator String() const; - _FORCE_INLINE_ Vector3() { x = y = z = 0; } _FORCE_INLINE_ Vector3(real_t p_x, real_t p_y, real_t p_z) { x = p_x; y = p_y; z = p_z; } + _FORCE_INLINE_ Vector3() { x = y = z = 0; } }; -// Should be included after class definition, otherwise we get circular refs -#include "core/math/basis.h" - Vector3 Vector3::cross(const Vector3 &p_b) const { Vector3 ret( @@ -170,21 +169,6 @@ real_t Vector3::dot(const Vector3 &p_b) const { return x * p_b.x + y * p_b.y + z * p_b.z; } -Basis Vector3::outer(const Vector3 &p_b) const { - - Vector3 row0(x * p_b.x, x * p_b.y, x * p_b.z); - Vector3 row1(y * p_b.x, y * p_b.y, y * p_b.z); - Vector3 row2(z * p_b.x, z * p_b.y, z * p_b.z); - - return Basis(row0, row1, row2); -} - -Basis Vector3::to_diagonal_matrix() const { - return Basis(x, 0, 0, - 0, y, 0, - 0, 0, z); -} - Vector3 Vector3::abs() const { return Vector3(Math::abs(x), Math::abs(y), Math::abs(z)); @@ -233,6 +217,14 @@ real_t Vector3::distance_squared_to(const Vector3 &p_b) const { return (p_b - *this).length_squared(); } +Vector3 Vector3::posmod(const real_t p_mod) const { + return Vector3(Math::fposmod(x, p_mod), Math::fposmod(y, p_mod), Math::fposmod(z, p_mod)); +} + +Vector3 Vector3::posmodv(const Vector3 &p_modv) const { + return Vector3(Math::fposmod(x, p_modv.x), Math::fposmod(y, p_modv.y), Math::fposmod(z, p_modv.z)); +} + Vector3 Vector3::project(const Vector3 &p_b) const { return p_b * (dot(p_b) / p_b.length_squared()); } @@ -357,6 +349,18 @@ bool Vector3::operator<(const Vector3 &p_v) const { } } +bool Vector3::operator>(const Vector3 &p_v) const { + + if (Math::is_equal_approx(x, p_v.x)) { + if (Math::is_equal_approx(y, p_v.y)) + return z > p_v.z; + else + return y > p_v.y; + } else { + return x > p_v.x; + } +} + bool Vector3::operator<=(const Vector3 &p_v) const { if (Math::is_equal_approx(x, p_v.x)) { @@ -369,6 +373,18 @@ bool Vector3::operator<=(const Vector3 &p_v) const { } } +bool Vector3::operator>=(const Vector3 &p_v) const { + + if (Math::is_equal_approx(x, p_v.x)) { + if (Math::is_equal_approx(y, p_v.y)) + return z >= p_v.z; + else + return y > p_v.y; + } else { + return x > p_v.x; + } +} + _FORCE_INLINE_ Vector3 vec3_cross(const Vector3 &p_a, const Vector3 &p_b) { return p_a.cross(p_b); diff --git a/core/message_queue.cpp b/core/message_queue.cpp index 32d2b805f6..a76b5167b6 100644 --- a/core/message_queue.cpp +++ b/core/message_queue.cpp @@ -52,8 +52,7 @@ Error MessageQueue::push_call(ObjectID p_id, const StringName &p_method, const V type = ObjectDB::get_instance(p_id)->get_class(); print_line("Failed method: " + type + ":" + p_method + " target ID: " + itos(p_id)); statistics(); - ERR_EXPLAIN("Message queue out of memory. Try increasing 'message_queue_size_kb' in project settings."); - ERR_FAIL_V(ERR_OUT_OF_MEMORY); + ERR_FAIL_V_MSG(ERR_OUT_OF_MEMORY, "Message queue out of memory. Try increasing 'message_queue_size_kb' in project settings."); } Message *msg = memnew_placement(&buffer[buffer_end], Message); @@ -103,8 +102,7 @@ Error MessageQueue::push_set(ObjectID p_id, const StringName &p_prop, const Vari type = ObjectDB::get_instance(p_id)->get_class(); print_line("Failed set: " + type + ":" + p_prop + " target ID: " + itos(p_id)); statistics(); - ERR_EXPLAIN("Message queue out of memory. Try increasing 'message_queue_size_kb' in project settings."); - ERR_FAIL_V(ERR_OUT_OF_MEMORY); + ERR_FAIL_V_MSG(ERR_OUT_OF_MEMORY, "Message queue out of memory. Try increasing 'message_queue_size_kb' in project settings."); } Message *msg = memnew_placement(&buffer[buffer_end], Message); @@ -136,8 +134,7 @@ Error MessageQueue::push_notification(ObjectID p_id, int p_notification) { type = ObjectDB::get_instance(p_id)->get_class(); print_line("Failed notification: " + itos(p_notification) + " target ID: " + itos(p_id)); statistics(); - ERR_EXPLAIN("Message queue out of memory. Try increasing 'message_queue_size_kb' in project settings."); - ERR_FAIL_V(ERR_OUT_OF_MEMORY); + ERR_FAIL_V_MSG(ERR_OUT_OF_MEMORY, "Message queue out of memory. Try increasing 'message_queue_size_kb' in project settings."); } Message *msg = memnew_placement(&buffer[buffer_end], Message); @@ -256,7 +253,7 @@ void MessageQueue::_call_function(Object *p_target, const StringName &p_func, co p_target->call(p_func, argptrs, p_argcount, ce); if (p_show_error && ce.error != Variant::CallError::CALL_OK) { - ERR_PRINTS("Error calling deferred method: " + Variant::get_call_error_text(p_target, p_func, argptrs, p_argcount, ce)); + ERR_PRINTS("Error calling deferred method: " + Variant::get_call_error_text(p_target, p_func, argptrs, p_argcount, ce) + "."); } } @@ -343,7 +340,7 @@ bool MessageQueue::is_flushing() const { MessageQueue::MessageQueue() { - ERR_FAIL_COND(singleton != NULL); + ERR_FAIL_COND_MSG(singleton != NULL, "MessageQueue singleton already exist."); singleton = this; flushing = false; diff --git a/core/method_bind.h b/core/method_bind.h index 1b0c3b27c0..7bb75e778f 100644 --- a/core/method_bind.h +++ b/core/method_bind.h @@ -38,10 +38,6 @@ #include <stdio.h> -/** - @author Juan Linietsky <reduzio@gmail.com> -*/ - #ifdef DEBUG_ENABLED #define DEBUG_METHODS_ENABLED #endif diff --git a/core/node_path.cpp b/core/node_path.cpp index a4b7cbe2eb..970ed100fe 100644 --- a/core/node_path.cpp +++ b/core/node_path.cpp @@ -269,7 +269,7 @@ NodePath NodePath::rel_path_to(const NodePath &p_np) const { NodePath NodePath::get_as_property_path() const { - if (!data->path.size()) { + if (!data || !data->path.size()) { return *this; } else { Vector<StringName> new_path = data->subpath; @@ -375,8 +375,7 @@ NodePath::NodePath(const String &p_path) { if (str == "") { if (path[i] == 0) continue; // Allow end-of-path : - ERR_EXPLAIN("Invalid NodePath: " + p_path); - ERR_FAIL(); + ERR_FAIL_MSG("Invalid NodePath '" + p_path + "'."); } subpath.push_back(str); diff --git a/core/node_path.h b/core/node_path.h index 24725123d6..1b21c4ef1c 100644 --- a/core/node_path.h +++ b/core/node_path.h @@ -34,10 +34,6 @@ #include "core/string_name.h" #include "core/ustring.h" -/** - @author Juan Linietsky <reduzio@gmail.com> -*/ - class NodePath { struct Data { diff --git a/core/oa_hash_map.h b/core/oa_hash_map.h index e52d36a859..1a466e57f4 100644 --- a/core/oa_hash_map.h +++ b/core/oa_hash_map.h @@ -37,10 +37,11 @@ #include "core/os/memory.h" /** - * A HashMap implementation that uses open addressing with robinhood hashing. - * Robinhood hashing swaps out entries that have a smaller probing distance + * A HashMap implementation that uses open addressing with Robin Hood hashing. + * Robin Hood hashing swaps out entries that have a smaller probing distance * than the to-be-inserted entry, that evens out the average probing distance - * and enables faster lookups. + * and enables faster lookups. Backward shift deletion is employed to further + * improve the performance and to avoid infinite loops in rare cases. * * The entries are stored inplace, so huge keys or values might fill cache lines * a lot faster. @@ -60,26 +61,20 @@ private: uint32_t num_elements; static const uint32_t EMPTY_HASH = 0; - static const uint32_t DELETED_HASH_BIT = 1 << 31; - _FORCE_INLINE_ uint32_t _hash(const TKey &p_key) { + _FORCE_INLINE_ uint32_t _hash(const TKey &p_key) const { uint32_t hash = Hasher::hash(p_key); if (hash == EMPTY_HASH) { hash = EMPTY_HASH + 1; - } else if (hash & DELETED_HASH_BIT) { - hash &= ~DELETED_HASH_BIT; } return hash; } - _FORCE_INLINE_ uint32_t _get_probe_length(uint32_t p_pos, uint32_t p_hash) { - p_hash = p_hash & ~DELETED_HASH_BIT; // we don't care if it was deleted or not - + _FORCE_INLINE_ uint32_t _get_probe_length(uint32_t p_pos, uint32_t p_hash) const { uint32_t original_pos = p_hash % capacity; - - return p_pos - original_pos; + return (p_pos - original_pos + capacity) % capacity; } _FORCE_INLINE_ void _construct(uint32_t p_pos, uint32_t p_hash, const TKey &p_key, const TValue &p_value) { @@ -90,7 +85,7 @@ private: num_elements++; } - bool _lookup_pos(const TKey &p_key, uint32_t &r_pos) { + bool _lookup_pos(const TKey &p_key, uint32_t &r_pos) const { uint32_t hash = _hash(p_key); uint32_t pos = hash % capacity; uint32_t distance = 0; @@ -133,14 +128,6 @@ private: // not an empty slot, let's check the probing length of the existing one uint32_t existing_probe_len = _get_probe_length(pos, hashes[pos]); if (existing_probe_len < distance) { - - if (hashes[pos] & DELETED_HASH_BIT) { - // we found a place where we can fit in! - _construct(pos, hash, key, value); - - return; - } - SWAP(hash, hashes[pos]); SWAP(key, keys[pos]); SWAP(value, values[pos]); @@ -151,17 +138,17 @@ private: distance++; } } - void _resize_and_rehash() { + + void _resize_and_rehash(uint32_t p_new_capacity) { + + uint32_t old_capacity = capacity; + capacity = p_new_capacity; TKey *old_keys = keys; TValue *old_values = values; uint32_t *old_hashes = hashes; - uint32_t old_capacity = capacity; - - capacity = old_capacity * 2; num_elements = 0; - keys = memnew_arr(TKey, capacity); values = memnew_arr(TValue, capacity); hashes = memnew_arr(uint32_t, capacity); @@ -174,9 +161,6 @@ private: if (old_hashes[i] == EMPTY_HASH) { continue; } - if (old_hashes[i] & DELETED_HASH_BIT) { - continue; - } _insert_with_hash(old_hashes[i], old_keys[i], old_values[i]); } @@ -186,13 +170,37 @@ private: memdelete_arr(old_hashes); } + void _resize_and_rehash() { + _resize_and_rehash(capacity * 2); + } + public: _FORCE_INLINE_ uint32_t get_capacity() const { return capacity; } _FORCE_INLINE_ uint32_t get_num_elements() const { return num_elements; } + bool empty() const { + return num_elements == 0; + } + + void clear() { + + for (uint32_t i = 0; i < capacity; i++) { + + if (hashes[i] == EMPTY_HASH) { + continue; + } + + hashes[i] = EMPTY_HASH; + values[i].~TValue(); + keys[i].~TKey(); + } + + num_elements = 0; + } + void insert(const TKey &p_key, const TValue &p_value) { - if ((float)num_elements / (float)capacity > 0.9) { + if (num_elements + 1 > 0.9 * capacity) { _resize_and_rehash(); } @@ -219,7 +227,7 @@ public: * if r_data is not NULL then the value will be written to the object * it points to. */ - bool lookup(const TKey &p_key, TValue &r_data) { + bool lookup(const TKey &p_key, TValue &r_data) const { uint32_t pos = 0; bool exists = _lookup_pos(p_key, pos); @@ -232,7 +240,7 @@ public: return false; } - _FORCE_INLINE_ bool has(const TKey &p_key) { + _FORCE_INLINE_ bool has(const TKey &p_key) const { uint32_t _pos = 0; return _lookup_pos(p_key, _pos); } @@ -245,12 +253,33 @@ public: return; } - hashes[pos] |= DELETED_HASH_BIT; + uint32_t next_pos = (pos + 1) % capacity; + while (hashes[next_pos] != EMPTY_HASH && + _get_probe_length(next_pos, hashes[next_pos]) != 0) { + SWAP(hashes[next_pos], hashes[pos]); + SWAP(keys[next_pos], keys[pos]); + SWAP(values[next_pos], values[pos]); + pos = next_pos; + next_pos = (pos + 1) % capacity; + } + + hashes[pos] = EMPTY_HASH; values[pos].~TValue(); keys[pos].~TKey(); + num_elements--; } + /** + * reserves space for a number of elements, useful to avoid many resizes and rehashes + * if adding a known (possibly large) number of elements at once, must be larger than old + * capacity. + **/ + void reserve(uint32_t p_new_capacity) { + ERR_FAIL_COND(p_new_capacity < capacity); + _resize_and_rehash(p_new_capacity); + } + struct Iterator { bool valid; @@ -289,9 +318,6 @@ public: if (hashes[i] == EMPTY_HASH) { continue; } - if (hashes[i] & DELETED_HASH_BIT) { - continue; - } it.valid = true; it.key = &keys[i]; @@ -302,6 +328,9 @@ public: return it; } + OAHashMap(const OAHashMap &) = delete; // Delete the copy constructor so we don't get unexpected copies and dangling pointers. + OAHashMap &operator=(const OAHashMap &) = delete; // Same for assignment operator. + OAHashMap(uint32_t p_initial_capacity = 64) { capacity = p_initial_capacity; @@ -312,7 +341,7 @@ public: hashes = memnew_arr(uint32_t, p_initial_capacity); for (uint32_t i = 0; i < p_initial_capacity; i++) { - hashes[i] = 0; + hashes[i] = EMPTY_HASH; } } diff --git a/core/object.cpp b/core/object.cpp index 3367d6b6c3..6facf38733 100644 --- a/core/object.cpp +++ b/core/object.cpp @@ -709,20 +709,17 @@ static void _test_call_error(const StringName &p_func, const Variant::CallError break; case Variant::CallError::CALL_ERROR_INVALID_ARGUMENT: { - ERR_EXPLAIN("Error Calling Function: " + String(p_func) + " - Invalid type for argument " + itos(error.argument) + ", expected " + Variant::get_type_name(error.expected)); - ERR_FAIL(); + ERR_FAIL_MSG("Error calling function: " + String(p_func) + " - Invalid type for argument " + itos(error.argument) + ", expected " + Variant::get_type_name(error.expected) + "."); break; } case Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: { - ERR_EXPLAIN("Error Calling Function: " + String(p_func) + " - Too many arguments, expected " + itos(error.argument)); - ERR_FAIL(); + ERR_FAIL_MSG("Error calling function: " + String(p_func) + " - Too many arguments, expected " + itos(error.argument) + "."); break; } case Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS: { - ERR_EXPLAIN("Error Calling Function: " + String(p_func) + " - Too few arguments, expected " + itos(error.argument)); - ERR_FAIL(); + ERR_FAIL_MSG("Error calling function: " + String(p_func) + " - Too few arguments, expected " + itos(error.argument) + "."); break; } case Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL: @@ -739,15 +736,9 @@ void Object::call_multilevel(const StringName &p_method, const Variant **p_args, if (p_method == CoreStringNames::get_singleton()->_free) { #ifdef DEBUG_ENABLED - if (Object::cast_to<Reference>(this)) { - ERR_EXPLAIN("Can't 'free' a reference."); - ERR_FAIL(); - } + ERR_FAIL_COND_MSG(Object::cast_to<Reference>(this), "Can't 'free' a reference."); - if (_lock_index.get() > 1) { - ERR_EXPLAIN("Object is locked and can't be freed."); - ERR_FAIL(); - } + ERR_FAIL_COND_MSG(_lock_index.get() > 1, "Object is locked and can't be freed."); #endif //must be here, must be before everything, @@ -835,8 +826,7 @@ Variant Object::callv(const StringName &p_method, const Array &p_args) { Variant::CallError ce; Variant ret = call(p_method, argptrs, p_args.size(), ce); if (ce.error != Variant::CallError::CALL_OK) { - ERR_EXPLAIN("Error calling method from 'callv': " + Variant::get_call_error_text(this, p_method, argptrs, p_args.size(), ce)); - ERR_FAIL_V(Variant()); + ERR_FAIL_V_MSG(Variant(), "Error calling method from 'callv': " + Variant::get_call_error_text(this, p_method, argptrs, p_args.size(), ce) + "."); } return ret; } @@ -888,15 +878,13 @@ Variant Object::call(const StringName &p_method, const Variant **p_args, int p_a if (Object::cast_to<Reference>(this)) { r_error.argument = 0; r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD; - ERR_EXPLAIN("Can't 'free' a reference."); - ERR_FAIL_V(Variant()); + ERR_FAIL_V_MSG(Variant(), "Can't 'free' a reference."); } if (_lock_index.get() > 1) { r_error.argument = 0; r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD; - ERR_EXPLAIN("Object is locked and can't be freed."); - ERR_FAIL_V(Variant()); + ERR_FAIL_V_MSG(Variant(), "Object is locked and can't be freed."); } #endif @@ -1112,9 +1100,9 @@ void Object::get_meta_list(List<String> *p_list) const { void Object::add_user_signal(const MethodInfo &p_signal) { - ERR_FAIL_COND(p_signal.name == ""); - ERR_FAIL_COND(ClassDB::has_signal(get_class_name(), p_signal.name)); - ERR_FAIL_COND(signal_map.has(p_signal.name)); + ERR_FAIL_COND_MSG(p_signal.name == "", "Signal name cannot be empty."); + ERR_FAIL_COND_MSG(ClassDB::has_signal(get_class_name(), p_signal.name), "User signal's name conflicts with a built-in signal of '" + get_class_name() + "'."); + ERR_FAIL_COND_MSG(signal_map.has(p_signal.name), "Trying to add already existing signal '" + p_signal.name + "'."); Signal s; s.user = p_signal; signal_map[p_signal.name] = s; @@ -1172,10 +1160,7 @@ Error Object::emit_signal(const StringName &p_name, const Variant **p_args, int #ifdef DEBUG_ENABLED bool signal_is_valid = ClassDB::has_signal(get_class_name(), p_name); //check in script - if (!signal_is_valid && !script.is_null() && !Ref<Script>(script)->has_script_signal(p_name)) { - ERR_EXPLAIN("Can't emit non-existing signal " + String("\"") + p_name + "\"."); - ERR_FAIL_V(ERR_UNAVAILABLE); - } + ERR_FAIL_COND_V_MSG(!signal_is_valid && !script.is_null() && !Ref<Script>(script)->has_script_signal(p_name), ERR_UNAVAILABLE, "Can't emit non-existing signal " + String("\"") + p_name + "\"."); #endif //not connected? just return return ERR_UNAVAILABLE; @@ -1240,7 +1225,7 @@ Error Object::emit_signal(const StringName &p_name, const Variant **p_args, int if (ce.error == Variant::CallError::CALL_ERROR_INVALID_METHOD && !ClassDB::class_exists(target->get_class_name())) { //most likely object is not initialized yet, do not throw error. } else { - ERR_PRINTS("Error calling method from signal '" + String(p_name) + "': " + Variant::get_call_error_text(target, c.method, args, argc, ce)); + ERR_PRINTS("Error calling method from signal '" + String(p_name) + "': " + Variant::get_call_error_text(target, c.method, args, argc, ce) + "."); err = ERR_METHOD_NOT_FOUND; } } @@ -1415,8 +1400,9 @@ void Object::get_signal_connection_list(const StringName &p_signal, List<Connect p_connections->push_back(s->slot_map.getv(i).conn); } -bool Object::has_persistent_signal_connections() const { +int Object::get_persistent_signal_connection_count() const { + int count = 0; const StringName *S = NULL; while ((S = signal_map.next(S))) { @@ -1424,13 +1410,13 @@ bool Object::has_persistent_signal_connections() const { const Signal *s = &signal_map[*S]; for (int i = 0; i < s->slot_map.size(); i++) { - - if (s->slot_map.getv(i).conn.flags & CONNECT_PERSIST) - return true; + if (s->slot_map.getv(i).conn.flags & CONNECT_PERSIST) { + count += 1; + } } } - return false; + return count; } void Object::get_signals_connected_to_this(List<Connection> *p_connections) const { @@ -1463,10 +1449,8 @@ Error Object::connect(const StringName &p_signal, Object *p_to_object, const Str #endif } - if (!signal_is_valid) { - ERR_EXPLAIN("In Object of type '" + String(get_class()) + "': Attempt to connect nonexistent signal '" + p_signal + "' to method '" + p_to_object->get_class() + "." + p_to_method + "'"); - ERR_FAIL_V(ERR_INVALID_PARAMETER); - } + ERR_FAIL_COND_V_MSG(!signal_is_valid, ERR_INVALID_PARAMETER, "In Object of type '" + String(get_class()) + "': Attempt to connect nonexistent signal '" + p_signal + "' to method '" + p_to_object->get_class() + "." + p_to_method + "'."); + signal_map[p_signal] = Signal(); s = &signal_map[p_signal]; } @@ -1477,8 +1461,7 @@ Error Object::connect(const StringName &p_signal, Object *p_to_object, const Str s->slot_map[target].reference_count++; return OK; } else { - ERR_EXPLAIN("Signal '" + p_signal + "' is already connected to given method '" + p_to_method + "' in that object."); - ERR_FAIL_V(ERR_INVALID_PARAMETER); + ERR_FAIL_V_MSG(ERR_INVALID_PARAMETER, "Signal '" + p_signal + "' is already connected to given method '" + p_to_method + "' in that object."); } } @@ -1514,8 +1497,7 @@ bool Object::is_connected(const StringName &p_signal, Object *p_to_object, const if (!script.is_null() && Ref<Script>(script)->has_script_signal(p_signal)) return false; - ERR_EXPLAIN("Nonexistent signal: " + p_signal); - ERR_FAIL_V(false); + ERR_FAIL_V_MSG(false, "Nonexistent signal: " + p_signal + "."); } Signal::Target target(p_to_object->get_instance_id(), p_to_method); @@ -1533,21 +1515,13 @@ void Object::_disconnect(const StringName &p_signal, Object *p_to_object, const ERR_FAIL_NULL(p_to_object); Signal *s = signal_map.getptr(p_signal); - if (!s) { - ERR_EXPLAIN("Nonexistent signal: " + p_signal); - ERR_FAIL(); - } - if (s->lock > 0) { - ERR_EXPLAIN("Attempt to disconnect signal '" + p_signal + "' while emitting (locks: " + itos(s->lock) + ")"); - ERR_FAIL(); - } + ERR_FAIL_COND_MSG(!s, "Nonexistent signal: " + p_signal + "."); + + ERR_FAIL_COND_MSG(s->lock > 0, "Attempt to disconnect signal '" + p_signal + "' while emitting (locks: " + itos(s->lock) + ")."); Signal::Target target(p_to_object->get_instance_id(), p_to_method); - if (!s->slot_map.has(target)) { - ERR_EXPLAIN("Disconnecting nonexistent signal '" + p_signal + "', slot: " + itos(target._id) + ":" + target.method); - ERR_FAIL(); - } + ERR_FAIL_COND_MSG(!s->slot_map.has(target), "Disconnecting nonexistent signal '" + p_signal + "', slot: " + itos(target._id) + ":" + target.method + "."); Signal::Slot *slot = &s->slot_map[target]; @@ -1974,10 +1948,7 @@ Object::~Object() { Signal *s = &signal_map[*S]; - if (s->lock) { - ERR_EXPLAIN("Attempt to delete an object in the middle of a signal emission from it"); - ERR_CONTINUE(s->lock > 0); - } + ERR_CONTINUE_MSG(s->lock > 0, "Attempt to delete an object in the middle of a signal emission from it."); //brute force disconnect for performance int slot_count = s->slot_map.size(); diff --git a/core/object.h b/core/object.h index dce1cc74ae..ac8620757c 100644 --- a/core/object.h +++ b/core/object.h @@ -707,7 +707,7 @@ public: void get_signal_list(List<MethodInfo> *p_signals) const; void get_signal_connection_list(const StringName &p_signal, List<Connection> *p_connections) const; void get_all_signal_connections(List<Connection> *p_connections) const; - bool has_persistent_signal_connections() const; + int get_persistent_signal_connection_count() const; void get_signals_connected_to_this(List<Connection> *p_connections) const; Error connect(const StringName &p_signal, Object *p_to_object, const StringName &p_to_method, const Vector<Variant> &p_binds = Vector<Variant>(), uint32_t p_flags = 0); @@ -794,8 +794,13 @@ public: static int get_object_count(); _FORCE_INLINE_ static bool instance_validate(Object *p_ptr) { + rw_lock->read_lock(); - return instance_checks.has(p_ptr); + bool exists = instance_checks.has(p_ptr); + + rw_lock->read_unlock(); + + return exists; } }; diff --git a/core/os/dir_access.cpp b/core/os/dir_access.cpp index b444f0ae1e..e7496055ec 100644 --- a/core/os/dir_access.cpp +++ b/core/os/dir_access.cpp @@ -244,7 +244,7 @@ DirAccess *DirAccess::open(const String &p_path, Error *r_error) { DirAccess *da = create_for_path(p_path); - ERR_FAIL_COND_V(!da, NULL); + ERR_FAIL_COND_V_MSG(!da, NULL, "Cannot create DirAccess for path '" + p_path + "'."); Error err = da->change_dir(p_path); if (r_error) *r_error = err; @@ -384,39 +384,36 @@ Error DirAccess::_copy_dir(DirAccess *p_target_da, String p_to, int p_chmod_flag String target_dir = p_to + rel_path; if (!p_target_da->dir_exists(target_dir)) { Error err = p_target_da->make_dir(target_dir); - ERR_FAIL_COND_V(err, err); + ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot create directory '" + target_dir + "'."); } Error err = change_dir(E->get()); - ERR_FAIL_COND_V(err, err); + ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot change current directory to '" + E->get() + "'."); + err = _copy_dir(p_target_da, p_to + rel_path + "/", p_chmod_flags); if (err) { change_dir(".."); - ERR_PRINT("Failed to copy recursively"); - return err; + ERR_FAIL_V_MSG(err, "Failed to copy recursively."); } err = change_dir(".."); - if (err) { - ERR_PRINT("Failed to go back"); - return err; - } + ERR_FAIL_COND_V_MSG(err != OK, err, "Failed to go back."); } return OK; } Error DirAccess::copy_dir(String p_from, String p_to, int p_chmod_flags) { - ERR_FAIL_COND_V(!dir_exists(p_from), ERR_FILE_NOT_FOUND); + ERR_FAIL_COND_V_MSG(!dir_exists(p_from), ERR_FILE_NOT_FOUND, "Source directory doesn't exist."); DirAccess *target_da = DirAccess::create_for_path(p_to); - ERR_FAIL_COND_V(!target_da, ERR_CANT_CREATE); + ERR_FAIL_COND_V_MSG(!target_da, ERR_CANT_CREATE, "Cannot create DirAccess for path '" + p_to + "'."); if (!target_da->dir_exists(p_to)) { Error err = target_da->make_dir_recursive(p_to); if (err) { memdelete(target_da); } - ERR_FAIL_COND_V(err, err); + ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot create directory '" + p_to + "'."); } if (!p_to.ends_with("/")) { diff --git a/core/os/dir_access.h b/core/os/dir_access.h index 704eedae5b..d3eb1e13f6 100644 --- a/core/os/dir_access.h +++ b/core/os/dir_access.h @@ -34,10 +34,6 @@ #include "core/typedefs.h" #include "core/ustring.h" -/** - @author Juan Linietsky <reduzio@gmail.com> -*/ - //@ TODO, excellent candidate for THREAD_SAFE MACRO, should go through all these and add THREAD_SAFE where it applies class DirAccess { public: @@ -97,6 +93,18 @@ public: virtual Error rename(String p_from, String p_to) = 0; virtual Error remove(String p_name) = 0; + // Meant for editor code when we want to quickly remove a file without custom + // handling (e.g. removing a cache file). + static void remove_file_or_error(String p_path) { + DirAccess *da = create(ACCESS_FILESYSTEM); + if (da->file_exists(p_path)) { + if (da->remove(p_path) != OK) { + ERR_FAIL_MSG("Cannot remove file or directory: " + p_path); + } + } + memdelete(da); + } + virtual String get_filesystem_type() const = 0; static String get_full_path(const String &p_path, AccessType p_access); static DirAccess *create_for_path(const String &p_path); diff --git a/core/os/file_access.cpp b/core/os/file_access.cpp index 7509050b2b..738e597730 100644 --- a/core/os/file_access.cpp +++ b/core/os/file_access.cpp @@ -30,9 +30,9 @@ #include "file_access.h" +#include "core/crypto/crypto_core.h" #include "core/io/file_access_pack.h" #include "core/io/marshalls.h" -#include "core/math/crypto_core.h" #include "core/os/os.h" #include "core/project_settings.h" @@ -498,7 +498,7 @@ uint64_t FileAccess::get_modified_time(const String &p_file) { return 0; FileAccess *fa = create_for_path(p_file); - ERR_FAIL_COND_V(!fa, 0); + ERR_FAIL_COND_V_MSG(!fa, 0, "Cannot create FileAccess for path '" + p_file + "'."); uint64_t mt = fa->_get_modified_time(p_file); memdelete(fa); @@ -511,7 +511,7 @@ uint32_t FileAccess::get_unix_permissions(const String &p_file) { return 0; FileAccess *fa = create_for_path(p_file); - ERR_FAIL_COND_V(!fa, 0); + ERR_FAIL_COND_V_MSG(!fa, 0, "Cannot create FileAccess for path '" + p_file + "'."); uint32_t mt = fa->_get_unix_permissions(p_file); memdelete(fa); @@ -521,7 +521,7 @@ uint32_t FileAccess::get_unix_permissions(const String &p_file) { Error FileAccess::set_unix_permissions(const String &p_file, uint32_t p_permissions) { FileAccess *fa = create_for_path(p_file); - ERR_FAIL_COND_V(!fa, ERR_CANT_CREATE); + ERR_FAIL_COND_V_MSG(!fa, ERR_CANT_CREATE, "Cannot create FileAccess for path '" + p_file + "'."); Error err = fa->_set_unix_permissions(p_file, p_permissions); memdelete(fa); @@ -599,8 +599,7 @@ Vector<uint8_t> FileAccess::get_file_as_array(const String &p_path, Error *r_err if (r_error) { // if error requested, do not throw error return Vector<uint8_t>(); } - ERR_EXPLAIN("Can't open file from path: " + String(p_path)); - ERR_FAIL_V(Vector<uint8_t>()); + ERR_FAIL_V_MSG(Vector<uint8_t>(), "Can't open file from path '" + String(p_path) + "'."); } Vector<uint8_t> data; data.resize(f->get_len()); @@ -620,8 +619,7 @@ String FileAccess::get_file_as_string(const String &p_path, Error *r_error) { if (r_error) { return String(); } - ERR_EXPLAIN("Can't get file as string from path: " + String(p_path)); - ERR_FAIL_V(String()); + ERR_FAIL_V_MSG(String(), "Can't get file as string from path '" + String(p_path) + "'."); } String ret; diff --git a/core/os/input.cpp b/core/os/input.cpp index f04d4a1b3e..51cb41b184 100644 --- a/core/os/input.cpp +++ b/core/os/input.cpp @@ -80,6 +80,7 @@ void Input::_bind_methods() { ClassDB::bind_method(D_METHOD("get_joy_axis_index_from_string", "axis"), &Input::get_joy_axis_index_from_string); ClassDB::bind_method(D_METHOD("start_joy_vibration", "device", "weak_magnitude", "strong_magnitude", "duration"), &Input::start_joy_vibration, DEFVAL(0)); ClassDB::bind_method(D_METHOD("stop_joy_vibration", "device"), &Input::stop_joy_vibration); + ClassDB::bind_method(D_METHOD("vibrate_handheld", "duration_ms"), &Input::vibrate_handheld, DEFVAL(500)); ClassDB::bind_method(D_METHOD("get_gravity"), &Input::get_gravity); ClassDB::bind_method(D_METHOD("get_accelerometer"), &Input::get_accelerometer); ClassDB::bind_method(D_METHOD("get_magnetometer"), &Input::get_magnetometer); diff --git a/core/os/input.h b/core/os/input.h index de04f239e6..a12ded176b 100644 --- a/core/os/input.h +++ b/core/os/input.h @@ -100,6 +100,7 @@ public: virtual uint64_t get_joy_vibration_timestamp(int p_device) = 0; virtual void start_joy_vibration(int p_device, float p_weak_magnitude, float p_strong_magnitude, float p_duration = 0) = 0; virtual void stop_joy_vibration(int p_device) = 0; + virtual void vibrate_handheld(int p_duration_ms = 500) = 0; virtual Point2 get_mouse_position() const = 0; virtual Point2 get_last_mouse_speed() const = 0; diff --git a/core/os/input_event.cpp b/core/os/input_event.cpp index a40a50cfce..30fca0c155 100644 --- a/core/os/input_event.cpp +++ b/core/os/input_event.cpp @@ -450,7 +450,7 @@ bool InputEventMouseButton::is_doubleclick() const { Ref<InputEvent> InputEventMouseButton::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const { - Vector2 g = p_xform.xform(get_global_position()); + Vector2 g = get_global_position(); Vector2 l = p_xform.xform(get_position() + p_local_ofs); Ref<InputEventMouseButton> mb; @@ -577,7 +577,7 @@ Vector2 InputEventMouseMotion::get_speed() const { Ref<InputEvent> InputEventMouseMotion::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const { - Vector2 g = p_xform.xform(get_global_position()); + Vector2 g = get_global_position(); Vector2 l = p_xform.xform(get_position() + p_local_ofs); Vector2 r = p_xform.basis_xform(get_relative()); Vector2 s = p_xform.basis_xform(get_speed()); diff --git a/core/os/input_event.h b/core/os/input_event.h index 4f5762e756..28658e3865 100644 --- a/core/os/input_event.h +++ b/core/os/input_event.h @@ -38,10 +38,6 @@ #include "core/ustring.h" /** - @author Juan Linietsky <reduzio@gmail.com> -*/ - -/** * Input Event classes. These are used in the main loop. * The events are pretty obvious. */ diff --git a/core/os/keyboard.h b/core/os/keyboard.h index 58a0807579..5c8a2e90e9 100644 --- a/core/os/keyboard.h +++ b/core/os/keyboard.h @@ -33,10 +33,6 @@ #include "core/ustring.h" -/** - @author Juan Linietsky <reduzio@gmail.com> -*/ - /* Special Key: diff --git a/core/os/main_loop.cpp b/core/os/main_loop.cpp index 9946ced2f3..5587e827ba 100644 --- a/core/os/main_loop.cpp +++ b/core/os/main_loop.cpp @@ -49,6 +49,8 @@ void MainLoop::_bind_methods() { BIND_VMETHOD(MethodInfo("_drop_files", PropertyInfo(Variant::POOL_STRING_ARRAY, "files"), PropertyInfo(Variant::INT, "from_screen"))); BIND_VMETHOD(MethodInfo("_finalize")); + BIND_VMETHOD(MethodInfo("_global_menu_action", PropertyInfo(Variant::NIL, "id"), PropertyInfo(Variant::NIL, "meta"))); + BIND_CONSTANT(NOTIFICATION_WM_MOUSE_ENTER); BIND_CONSTANT(NOTIFICATION_WM_MOUSE_EXIT); BIND_CONSTANT(NOTIFICATION_WM_FOCUS_IN); @@ -61,6 +63,8 @@ void MainLoop::_bind_methods() { BIND_CONSTANT(NOTIFICATION_WM_ABOUT); BIND_CONSTANT(NOTIFICATION_CRASH); BIND_CONSTANT(NOTIFICATION_OS_IME_UPDATE); + BIND_CONSTANT(NOTIFICATION_APP_RESUMED); + BIND_CONSTANT(NOTIFICATION_APP_PAUSED); }; void MainLoop::set_init_script(const Ref<Script> &p_init_script) { @@ -115,6 +119,12 @@ void MainLoop::drop_files(const Vector<String> &p_files, int p_from_screen) { get_script_instance()->call("_drop_files", p_files, p_from_screen); } +void MainLoop::global_menu_action(const Variant &p_id, const Variant &p_meta) { + + if (get_script_instance()) + get_script_instance()->call("_global_menu_action", p_id, p_meta); +} + void MainLoop::finish() { if (get_script_instance()) { diff --git a/core/os/main_loop.h b/core/os/main_loop.h index ad734d3fc8..aca920efcb 100644 --- a/core/os/main_loop.h +++ b/core/os/main_loop.h @@ -35,10 +35,6 @@ #include "core/reference.h" #include "core/script_language.h" -/** - @author Juan Linietsky <reduzio@gmail.com> -*/ - class MainLoop : public Object { GDCLASS(MainLoop, Object); @@ -64,6 +60,8 @@ public: NOTIFICATION_WM_ABOUT = 1011, NOTIFICATION_CRASH = 1012, NOTIFICATION_OS_IME_UPDATE = 1013, + NOTIFICATION_APP_RESUMED = 1014, + NOTIFICATION_APP_PAUSED = 1015, }; virtual void input_event(const Ref<InputEvent> &p_event); @@ -75,6 +73,7 @@ public: virtual void finish(); virtual void drop_files(const Vector<String> &p_files, int p_from_screen = 0); + virtual void global_menu_action(const Variant &p_id, const Variant &p_meta); void set_init_script(const Ref<Script> &p_init_script); diff --git a/core/os/memory.h b/core/os/memory.h index e073b11e76..a68a359546 100644 --- a/core/os/memory.h +++ b/core/os/memory.h @@ -31,14 +31,11 @@ #ifndef MEMORY_H #define MEMORY_H +#include "core/error_macros.h" #include "core/safe_refcount.h" #include <stddef.h> -/** - @author Juan Linietsky <reduzio@gmail.com> -*/ - #ifndef PAD_ALIGN #define PAD_ALIGN 16 //must always be greater than this at much #endif diff --git a/core/os/os.cpp b/core/os/os.cpp index 925154af7d..b44487b908 100644 --- a/core/os/os.cpp +++ b/core/os/os.cpp @@ -186,6 +186,11 @@ int OS::get_process_id() const { return -1; }; +void OS::vibrate_handheld(int p_duration_ms) { + + WARN_PRINTS("vibrate_handheld() only works with Android and iOS"); +} + bool OS::is_stdout_verbose() const { return _verbose_stdout; @@ -268,8 +273,7 @@ void OS::print_all_resources(String p_to_file) { _OSPRF = FileAccess::open(p_to_file, FileAccess::WRITE, &err); if (err != OK) { _OSPRF = NULL; - ERR_EXPLAIN("Can't print all resources to file: " + String(p_to_file)); - ERR_FAIL(); + ERR_FAIL_MSG("Can't print all resources to file: " + String(p_to_file) + "."); } } @@ -487,10 +491,7 @@ void OS::_ensure_user_data_dir() { da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); Error err = da->make_dir_recursive(dd); - if (err != OK) { - ERR_EXPLAIN("Error attempting to create data dir: " + dd); - } - ERR_FAIL_COND(err != OK); + ERR_FAIL_COND_MSG(err != OK, "Error attempting to create data dir: " + dd + "."); memdelete(da); } @@ -721,7 +722,7 @@ int OS::get_audio_driver_count() const { const char *OS::get_audio_driver_name(int p_driver) const { AudioDriver *driver = AudioDriverManager::get_driver(p_driver); - ERR_FAIL_COND_V(!driver, ""); + ERR_FAIL_COND_V_MSG(!driver, "", "Cannot get audio driver at index '" + itos(p_driver) + "'."); return AudioDriverManager::get_driver(p_driver)->get_name(); } diff --git a/core/os/os.h b/core/os/os.h index 2224d3b006..9b46b43081 100644 --- a/core/os/os.h +++ b/core/os/os.h @@ -41,10 +41,6 @@ #include <stdarg.h> -/** - @author Juan Linietsky <reduzio@gmail.com> -*/ - class Mutex; class OS { @@ -147,6 +143,11 @@ public: static OS *get_singleton(); + virtual void global_menu_add_item(const String &p_menu, const String &p_label, const Variant &p_signal, const Variant &p_meta){}; + virtual void global_menu_add_separator(const String &p_menu){}; + virtual void global_menu_remove_item(const String &p_menu, int p_idx){}; + virtual void global_menu_clear(const String &p_menu){}; + void print_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, Logger::ErrorType p_type = Logger::ERR_ERROR); void print(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3; void printerr(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3; @@ -269,6 +270,7 @@ public: virtual Error execute(const String &p_path, const List<String> &p_arguments, bool p_blocking, ProcessID *r_child_id = NULL, String *r_pipe = NULL, int *r_exitcode = NULL, bool read_stderr = false, Mutex *p_pipe_mutex = NULL) = 0; virtual Error kill(const ProcessID &p_pid) = 0; virtual int get_process_id() const; + virtual void vibrate_handheld(int p_duration_ms = 500); virtual Error shell_open(String p_uri); virtual Error set_cwd(const String &p_cwd); diff --git a/core/os/semaphore.h b/core/os/semaphore.h index ccbba0dacd..a0862dce84 100644 --- a/core/os/semaphore.h +++ b/core/os/semaphore.h @@ -33,10 +33,6 @@ #include "core/error_list.h" -/** - @author Juan Linietsky <reduzio@gmail.com> -*/ - class Semaphore { protected: static Semaphore *(*create_func)(); diff --git a/core/os/thread.h b/core/os/thread.h index e7a6e8cb1f..169280a208 100644 --- a/core/os/thread.h +++ b/core/os/thread.h @@ -34,10 +34,6 @@ #include "core/typedefs.h" #include "core/ustring.h" -/** - @author Juan Linietsky <reduzio@gmail.com> -*/ - typedef void (*ThreadCreateCallback)(void *p_userdata); class Thread { diff --git a/core/packed_data_container.cpp b/core/packed_data_container.cpp index 54bf12b314..003e7e7428 100644 --- a/core/packed_data_container.cpp +++ b/core/packed_data_container.cpp @@ -119,7 +119,7 @@ Variant PackedDataContainer::_get_at_ofs(uint32_t p_ofs, const uint8_t *p_buf, b if (rerr != OK) { err = true; - ERR_FAIL_COND_V(err != OK, Variant()); + ERR_FAIL_COND_V_MSG(err != OK, Variant(), "Error when trying to decode Variant."); } return v; } diff --git a/core/pool_allocator.cpp b/core/pool_allocator.cpp index 9b342ef913..6c1f2756f2 100644 --- a/core/pool_allocator.cpp +++ b/core/pool_allocator.cpp @@ -204,10 +204,8 @@ PoolAllocator::ID PoolAllocator::alloc(int p_size) { /* Then search again */ if (!find_hole(&new_entry_indices_pos, size_to_alloc)) { - mt_unlock(); - ERR_EXPLAIN("Memory can't be compacted further"); - ERR_FAIL_V(POOL_ALLOCATOR_INVALID_ID); + ERR_FAIL_V_MSG(POOL_ALLOCATOR_INVALID_ID, "Memory can't be compacted further."); } } @@ -217,8 +215,7 @@ PoolAllocator::ID PoolAllocator::alloc(int p_size) { if (!found_free_entry) { mt_unlock(); - ERR_EXPLAIN("No free entry found in PoolAllocator"); - ERR_FAIL_V(POOL_ALLOCATOR_INVALID_ID); + ERR_FAIL_V_MSG(POOL_ALLOCATOR_INVALID_ID, "No free entry found in PoolAllocator."); } /* move all entry indices up, make room for this one */ @@ -539,6 +536,10 @@ void PoolAllocator::unlock(ID p_mem) { return; mt_lock(); Entry *e = get_entry(p_mem); + if (!e) { + mt_unlock(); + ERR_FAIL_COND(!e); + } if (e->lock == 0) { mt_unlock(); ERR_PRINT("e->lock == 0"); diff --git a/core/pool_vector.cpp b/core/pool_vector.cpp index b9d2316315..50ea898bef 100644 --- a/core/pool_vector.cpp +++ b/core/pool_vector.cpp @@ -66,6 +66,5 @@ void MemoryPool::cleanup() { memdelete_arr(allocs); memdelete(alloc_mutex); - ERR_EXPLAINC("There are still MemoryPool allocs in use at exit!"); - ERR_FAIL_COND(allocs_used > 0); + ERR_FAIL_COND_MSG(allocs_used > 0, "There are still MemoryPool allocs in use at exit!"); } diff --git a/core/pool_vector.h b/core/pool_vector.h index 3d28d86803..fbd4d630be 100644 --- a/core/pool_vector.h +++ b/core/pool_vector.h @@ -77,10 +77,6 @@ struct MemoryPool { static void cleanup(); }; -/** - @author Juan Linietsky <reduzio@gmail.com> -*/ - template <class T> class PoolVector { @@ -102,8 +98,7 @@ class PoolVector { MemoryPool::alloc_mutex->lock(); if (MemoryPool::allocs_used == MemoryPool::alloc_count) { MemoryPool::alloc_mutex->unlock(); - ERR_EXPLAINC("All memory pool allocations are in use, can't COW."); - ERR_FAIL(); + ERR_FAIL_MSG("All memory pool allocations are in use, can't COW."); } MemoryPool::Alloc *old_alloc = alloc; @@ -513,7 +508,7 @@ T PoolVector<T>::operator[](int p_index) const { template <class T> Error PoolVector<T>::resize(int p_size) { - ERR_FAIL_COND_V(p_size < 0, ERR_INVALID_PARAMETER); + ERR_FAIL_COND_V_MSG(p_size < 0, ERR_INVALID_PARAMETER, "Size of PoolVector cannot be negative."); if (alloc == NULL) { @@ -524,8 +519,7 @@ Error PoolVector<T>::resize(int p_size) { MemoryPool::alloc_mutex->lock(); if (MemoryPool::allocs_used == MemoryPool::alloc_count) { MemoryPool::alloc_mutex->unlock(); - ERR_EXPLAINC("All memory pool allocations are in use."); - ERR_FAIL_V(ERR_OUT_OF_MEMORY); + ERR_FAIL_V_MSG(ERR_OUT_OF_MEMORY, "All memory pool allocations are in use."); } //take one from the free list @@ -542,7 +536,7 @@ Error PoolVector<T>::resize(int p_size) { } else { - ERR_FAIL_COND_V(alloc->lock > 0, ERR_LOCKED); //can't resize if locked! + ERR_FAIL_COND_V_MSG(alloc->lock > 0, ERR_LOCKED, "Can't resize PoolVector if locked."); //can't resize if locked! } size_t new_size = sizeof(T) * p_size; diff --git a/core/project_settings.cpp b/core/project_settings.cpp index c1d4967f55..c2241ed926 100644 --- a/core/project_settings.cpp +++ b/core/project_settings.cpp @@ -113,12 +113,12 @@ String ProjectSettings::localize_path(const String &p_path) const { void ProjectSettings::set_initial_value(const String &p_name, const Variant &p_value) { - ERR_FAIL_COND(!props.has(p_name)); + ERR_FAIL_COND_MSG(!props.has(p_name), "Request for nonexistent project setting: " + p_name + "."); props[p_name].initial = p_value; } void ProjectSettings::set_restart_if_changed(const String &p_name, bool p_restart) { - ERR_FAIL_COND(!props.has(p_name)); + ERR_FAIL_COND_MSG(!props.has(p_name), "Request for nonexistent project setting: " + p_name + "."); props[p_name].restart_if_changed = p_restart; } @@ -264,12 +264,12 @@ void ProjectSettings::_get_property_list(List<PropertyInfo> *p_list) const { } } -bool ProjectSettings::_load_resource_pack(const String &p_pack) { +bool ProjectSettings::_load_resource_pack(const String &p_pack, bool p_replace_files) { if (PackedData::get_singleton()->is_disabled()) return false; - bool ok = PackedData::get_singleton()->add_pack(p_pack) == OK; + bool ok = PackedData::get_singleton()->add_pack(p_pack, p_replace_files) == OK; if (!ok) return false; @@ -336,7 +336,7 @@ Error ProjectSettings::_setup(const String &p_path, const String &p_main_pack, b if (p_main_pack != "") { bool ok = _load_resource_pack(p_main_pack); - ERR_FAIL_COND_V(!ok, ERR_CANT_OPEN); + ERR_FAIL_COND_V_MSG(!ok, ERR_CANT_OPEN, "Cannot open resource pack '" + p_main_pack + "'."); Error err = _load_settings_text_or_binary("res://project.godot", "res://project.binary"); if (err == OK) { @@ -421,7 +421,7 @@ Error ProjectSettings::_setup(const String &p_path, const String &p_main_pack, b // or, if requested (`p_upwards`) in parent directories. DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); - ERR_FAIL_COND_V(!d, ERR_CANT_CREATE); + ERR_FAIL_COND_V_MSG(!d, ERR_CANT_CREATE, "Cannot create DirAccess for path '" + p_path + "'."); d->change_dir(p_path); String current_dir = d->get_current_dir(); @@ -500,8 +500,7 @@ Error ProjectSettings::_load_settings_binary(const String &p_path) { if (hdr[0] != 'E' || hdr[1] != 'C' || hdr[2] != 'F' || hdr[3] != 'G') { memdelete(f); - ERR_EXPLAIN("Corrupted header in binary project.binary (not ECFG)"); - ERR_FAIL_V(ERR_FILE_CORRUPT); + ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Corrupted header in binary project.binary (not ECFG)."); } uint32_t count = f->get_32(); @@ -522,8 +521,7 @@ Error ProjectSettings::_load_settings_binary(const String &p_path) { f->get_buffer(d.ptrw(), vlen); Variant value; err = decode_variant(value, d.ptr(), d.size(), NULL, true); - ERR_EXPLAIN("Error decoding property: " + key); - ERR_CONTINUE(err != OK); + ERR_CONTINUE_MSG(err != OK, "Error decoding property: " + key + "."); set(key, value); } @@ -577,8 +575,7 @@ Error ProjectSettings::_load_settings_text(const String &p_path) { config_version = value; if (config_version > CONFIG_VERSION) { memdelete(f); - ERR_EXPLAIN(vformat("Can't open project at '%s', its `config_version` (%d) is from a more recent and incompatible version of the engine. Expected config version: %d.", p_path, config_version, CONFIG_VERSION)); - ERR_FAIL_V(ERR_FILE_CANT_OPEN); + ERR_FAIL_V_MSG(ERR_FILE_CANT_OPEN, vformat("Can't open project at '%s', its `config_version` (%d) is from a more recent and incompatible version of the engine. Expected config version: %d.", p_path, config_version, CONFIG_VERSION)); } } else { if (section == String()) { @@ -612,19 +609,19 @@ Error ProjectSettings::_load_settings_text_or_binary(const String &p_text_path, int ProjectSettings::get_order(const String &p_name) const { - ERR_FAIL_COND_V(!props.has(p_name), -1); + ERR_FAIL_COND_V_MSG(!props.has(p_name), -1, "Request for nonexistent project setting: " + p_name + "."); return props[p_name].order; } void ProjectSettings::set_order(const String &p_name, int p_order) { - ERR_FAIL_COND(!props.has(p_name)); + ERR_FAIL_COND_MSG(!props.has(p_name), "Request for nonexistent project setting: " + p_name + "."); props[p_name].order = p_order; } void ProjectSettings::set_builtin_order(const String &p_name) { - ERR_FAIL_COND(!props.has(p_name)); + ERR_FAIL_COND_MSG(!props.has(p_name), "Request for nonexistent project setting: " + p_name + "."); if (props[p_name].order >= NO_BUILTIN_ORDER_BASE) { props[p_name].order = last_builtin_order++; } @@ -632,7 +629,7 @@ void ProjectSettings::set_builtin_order(const String &p_name) { void ProjectSettings::clear(const String &p_name) { - ERR_FAIL_COND(!props.has(p_name)); + ERR_FAIL_COND_MSG(!props.has(p_name), "Request for nonexistent project setting: " + p_name + "."); props.erase(p_name); } @@ -645,11 +642,7 @@ Error ProjectSettings::_save_settings_binary(const String &p_file, const Map<Str Error err; FileAccess *file = FileAccess::open(p_file, FileAccess::WRITE, &err); - if (err != OK) { - - ERR_EXPLAIN("Couldn't save project.binary at " + p_file); - ERR_FAIL_COND_V(err, err); - } + ERR_FAIL_COND_V_MSG(err != OK, err, "Couldn't save project.binary at " + p_file + "."); uint8_t hdr[4] = { 'E', 'C', 'F', 'G' }; file->store_buffer(hdr, 4); @@ -713,7 +706,7 @@ Error ProjectSettings::_save_settings_binary(const String &p_file, const Map<Str err = encode_variant(value, NULL, len, true); if (err != OK) memdelete(file); - ERR_FAIL_COND_V(err != OK, ERR_INVALID_DATA); + ERR_FAIL_COND_V_MSG(err != OK, ERR_INVALID_DATA, "Error when trying to encode Variant."); Vector<uint8_t> buff; buff.resize(len); @@ -721,7 +714,7 @@ Error ProjectSettings::_save_settings_binary(const String &p_file, const Map<Str err = encode_variant(value, buff.ptrw(), len, true); if (err != OK) memdelete(file); - ERR_FAIL_COND_V(err != OK, ERR_INVALID_DATA); + ERR_FAIL_COND_V_MSG(err != OK, ERR_INVALID_DATA, "Error when trying to encode Variant."); file->store_32(len); file->store_buffer(buff.ptr(), buff.size()); } @@ -738,10 +731,7 @@ Error ProjectSettings::_save_settings_text(const String &p_file, const Map<Strin Error err; FileAccess *file = FileAccess::open(p_file, FileAccess::WRITE, &err); - if (err) { - ERR_EXPLAIN("Couldn't save project.godot - " + p_file); - ERR_FAIL_COND_V(err, err); - } + ERR_FAIL_COND_V_MSG(err != OK, err, "Couldn't save project.godot - " + p_file + "."); file->store_line("; Engine configuration file."); file->store_line("; It's best edited using the editor UI and not directly,"); @@ -797,7 +787,7 @@ Error ProjectSettings::_save_custom_bnd(const String &p_file) { // add other par Error ProjectSettings::save_custom(const String &p_path, const CustomMap &p_custom, const Vector<String> &p_custom_features, bool p_merge_with_current) { - ERR_FAIL_COND_V(p_path == "", ERR_INVALID_PARAMETER); + ERR_FAIL_COND_V_MSG(p_path == "", ERR_INVALID_PARAMETER, "Project settings save path cannot be empty."); Set<_VCSort> vclist; @@ -872,8 +862,7 @@ Error ProjectSettings::save_custom(const String &p_path, const CustomMap &p_cust return _save_settings_binary(p_path, props, p_custom, custom_features); else { - ERR_EXPLAIN("Unknown config file format: " + p_path); - ERR_FAIL_V(ERR_FILE_UNRECOGNIZED); + ERR_FAIL_V_MSG(ERR_FILE_UNRECOGNIZED, "Unknown config file format: " + p_path + "."); } } @@ -990,7 +979,7 @@ void ProjectSettings::_bind_methods() { ClassDB::bind_method(D_METHOD("localize_path", "path"), &ProjectSettings::localize_path); ClassDB::bind_method(D_METHOD("globalize_path", "path"), &ProjectSettings::globalize_path); ClassDB::bind_method(D_METHOD("save"), &ProjectSettings::save); - ClassDB::bind_method(D_METHOD("load_resource_pack", "pack"), &ProjectSettings::_load_resource_pack); + ClassDB::bind_method(D_METHOD("load_resource_pack", "pack", "replace_files"), &ProjectSettings::_load_resource_pack, DEFVAL(true)); ClassDB::bind_method(D_METHOD("property_can_revert", "name"), &ProjectSettings::property_can_revert); ClassDB::bind_method(D_METHOD("property_get_revert", "name"), &ProjectSettings::property_get_revert); @@ -1011,6 +1000,8 @@ ProjectSettings::ProjectSettings() { Ref<InputEventJoypadButton> joyb; GLOBAL_DEF("application/config/name", ""); + GLOBAL_DEF("application/config/description", ""); + custom_prop_info["application/config/description"] = PropertyInfo(Variant::STRING, "application/config/description", PROPERTY_HINT_MULTILINE_TEXT); GLOBAL_DEF("application/run/main_scene", ""); custom_prop_info["application/run/main_scene"] = PropertyInfo(Variant::STRING, "application/run/main_scene", PROPERTY_HINT_FILE, "*.tscn,*.scn,*.res"); GLOBAL_DEF("application/run/disable_stdout", false); @@ -1030,6 +1021,9 @@ ProjectSettings::ProjectSettings() { GLOBAL_DEF("editor/search_in_file_extensions", extensions); custom_prop_info["editor/search_in_file_extensions"] = PropertyInfo(Variant::POOL_STRING_ARRAY, "editor/search_in_file_extensions"); + GLOBAL_DEF("editor/script_templates_search_path", "res://script_templates"); + custom_prop_info["editor/script_templates_search_path"] = PropertyInfo(Variant::STRING, "editor/script_templates_search_path", PROPERTY_HINT_DIR); + action = Dictionary(); action["deadzone"] = Variant(0.5f); events = Array(); diff --git a/core/project_settings.h b/core/project_settings.h index d7651417d5..b32470361b 100644 --- a/core/project_settings.h +++ b/core/project_settings.h @@ -35,10 +35,6 @@ #include "core/os/thread_safe.h" #include "core/set.h" -/** - @author Juan Linietsky <reduzio@gmail.com> -*/ - class ProjectSettings : public Object { GDCLASS(ProjectSettings, Object); @@ -108,7 +104,7 @@ protected: void _convert_to_last_version(int p_from_version); - bool _load_resource_pack(const String &p_pack); + bool _load_resource_pack(const String &p_pack, bool p_replace_files = true); void _add_property_info_bind(const Dictionary &p_info); diff --git a/core/reference.cpp b/core/reference.cpp index 1984af9a34..92bbdacd5d 100644 --- a/core/reference.cpp +++ b/core/reference.cpp @@ -36,12 +36,7 @@ bool Reference::init_ref() { if (reference()) { - // this may fail in the scenario of two threads assigning the pointer for the FIRST TIME - // at the same time, which is never likely to happen (would be crazy to do) - // so don't do it. - - if (refcount_init.get() > 0) { - refcount_init.unref(); + if (!is_referenced() && refcount_init.unref()) { unreference(); // first referencing is already 1, so compensate for the ref above } @@ -64,9 +59,11 @@ int Reference::reference_get_count() const { } bool Reference::reference() { - bool success = refcount.ref(); - if (success && refcount.get() <= 2 /* higher is not relevant */) { + uint32_t rc_val = refcount.refval(); + bool success = rc_val != 0; + + if (success && rc_val <= 2 /* higher is not relevant */) { if (get_script_instance()) { get_script_instance()->refcount_incremented(); } @@ -84,9 +81,10 @@ bool Reference::reference() { bool Reference::unreference() { - bool die = refcount.unref(); + uint32_t rc_val = refcount.unrefval(); + bool die = rc_val == 0; - if (refcount.get() <= 1 /* higher is not relevant */) { + if (rc_val <= 1 /* higher is not relevant */) { if (get_script_instance()) { bool script_ret = get_script_instance()->refcount_decremented(); die = die && script_ret; diff --git a/core/reference.h b/core/reference.h index 8a19f846c7..b8d00a94ad 100644 --- a/core/reference.h +++ b/core/reference.h @@ -36,9 +36,6 @@ #include "core/ref_ptr.h" #include "core/safe_refcount.h" -/** - @author Juan Linietsky <reduzio@gmail.com> -*/ class Reference : public Object { GDCLASS(Reference, Object); @@ -50,7 +47,7 @@ protected: static void _bind_methods(); public: - _FORCE_INLINE_ bool is_referenced() const { return refcount_init.get() < 1; } + _FORCE_INLINE_ bool is_referenced() const { return refcount_init.get() != 1; } bool init_ref(); bool reference(); // returns false if refcount is at zero and didn't get increased bool unreference(); diff --git a/core/register_core_types.cpp b/core/register_core_types.cpp index e442546124..efc77bde48 100644 --- a/core/register_core_types.cpp +++ b/core/register_core_types.cpp @@ -34,6 +34,8 @@ #include "core/class_db.h" #include "core/compressed_translation.h" #include "core/core_string_names.h" +#include "core/crypto/crypto.h" +#include "core/crypto/hashing_context.h" #include "core/engine.h" #include "core/func_ref.h" #include "core/input_map.h" @@ -70,6 +72,8 @@ static Ref<ResourceFormatLoaderBinary> resource_loader_binary; static Ref<ResourceFormatImporter> resource_format_importer; static Ref<ResourceFormatLoaderImage> resource_format_image; static Ref<TranslationLoaderPO> resource_format_po; +static Ref<ResourceFormatSaverCrypto> resource_format_saver_crypto; +static Ref<ResourceFormatLoaderCrypto> resource_format_loader_crypto; static _ResourceLoader *_resource_loader = NULL; static _ResourceSaver *_resource_saver = NULL; @@ -151,7 +155,19 @@ void register_core_types() { ClassDB::register_class<StreamPeerTCP>(); ClassDB::register_class<TCP_Server>(); ClassDB::register_class<PacketPeerUDP>(); + + // Crypto + ClassDB::register_class<HashingContext>(); + ClassDB::register_custom_instance_class<X509Certificate>(); + ClassDB::register_custom_instance_class<CryptoKey>(); + ClassDB::register_custom_instance_class<Crypto>(); ClassDB::register_custom_instance_class<StreamPeerSSL>(); + + resource_format_saver_crypto.instance(); + ResourceSaver::add_resource_format_saver(resource_format_saver_crypto); + resource_format_loader_crypto.instance(); + ResourceLoader::add_resource_format_loader(resource_format_loader_crypto); + ClassDB::register_virtual_class<IP>(); ClassDB::register_virtual_class<PacketPeer>(); ClassDB::register_class<PacketPeerStream>(); @@ -211,6 +227,9 @@ void register_core_settings() { ProjectSettings::get_singleton()->set_custom_property_info("network/limits/tcp/connect_timeout_seconds", PropertyInfo(Variant::INT, "network/limits/tcp/connect_timeout_seconds", PROPERTY_HINT_RANGE, "1,1800,1")); GLOBAL_DEF_RST("network/limits/packet_peer_stream/max_buffer_po2", (16)); ProjectSettings::get_singleton()->set_custom_property_info("network/limits/packet_peer_stream/max_buffer_po2", PropertyInfo(Variant::INT, "network/limits/packet_peer_stream/max_buffer_po2", PROPERTY_HINT_RANGE, "0,64,1,or_greater")); + + GLOBAL_DEF("network/ssl/certificates", ""); + ProjectSettings::get_singleton()->set_custom_property_info("network/ssl/certificates", PropertyInfo(Variant::STRING, "network/ssl/certificates", PROPERTY_HINT_FILE, "*.crt")); } void register_core_singletons() { @@ -272,6 +291,11 @@ void unregister_core_types() { ResourceLoader::remove_resource_format_loader(resource_format_po); resource_format_po.unref(); + ResourceSaver::remove_resource_format_saver(resource_format_saver_crypto); + resource_format_saver_crypto.unref(); + ResourceLoader::remove_resource_format_loader(resource_format_loader_crypto); + resource_format_loader_crypto.unref(); + if (ip) memdelete(ip); diff --git a/core/register_core_types.h b/core/register_core_types.h index b5a6aa985b..2d397b55f9 100644 --- a/core/register_core_types.h +++ b/core/register_core_types.h @@ -31,10 +31,6 @@ #ifndef REGISTER_CORE_TYPES_H #define REGISTER_CORE_TYPES_H -/** - @author Juan Linietsky <reduzio@gmail.com> -*/ - void register_core_types(); void register_core_settings(); void register_core_singletons(); diff --git a/core/resource.cpp b/core/resource.cpp index 93afbfb02a..e0a40b6f3c 100644 --- a/core/resource.cpp +++ b/core/resource.cpp @@ -75,8 +75,7 @@ void Resource::set_path(const String &p_path, bool p_take_over) { bool exists = ResourceCache::resources.has(p_path); ResourceCache::lock->read_unlock(); - ERR_EXPLAIN("Another resource is loaded from path: " + p_path + " (possible cyclic resource inclusion)"); - ERR_FAIL_COND(exists); + ERR_FAIL_COND_MSG(exists, "Another resource is loaded from path '" + p_path + "' (possible cyclic resource inclusion)."); } } path_cache = p_path; @@ -277,8 +276,7 @@ void Resource::notify_change_to_owners() { for (Set<ObjectID>::Element *E = owners.front(); E; E = E->next()) { Object *obj = ObjectDB::get_instance(E->get()); - ERR_EXPLAIN("Object was deleted, while still owning a resource"); - ERR_CONTINUE(!obj); //wtf + ERR_CONTINUE_MSG(!obj, "Object was deleted, while still owning a resource."); //wtf //TODO store string obj->call("resource_changed", RES(this)); } @@ -448,7 +446,7 @@ Resource::~Resource() { ResourceCache::lock->write_unlock(); } if (owners.size()) { - WARN_PRINT("Resource is still owned"); + WARN_PRINT("Resource is still owned."); } } @@ -541,7 +539,7 @@ void ResourceCache::dump(const char *p_file, bool p_short) { FileAccess *f = NULL; if (p_file) { f = FileAccess::open(p_file, FileAccess::WRITE); - ERR_FAIL_COND(!f); + ERR_FAIL_COND_MSG(!f, "Cannot create file at path '" + String(p_file) + "'."); } const String *K = NULL; diff --git a/core/resource.h b/core/resource.h index 4a899ce4b7..3e1fe07137 100644 --- a/core/resource.h +++ b/core/resource.h @@ -38,10 +38,6 @@ #include "core/safe_refcount.h" #include "core/self_list.h" -/** - @author Juan Linietsky <reduzio@gmail.com> -*/ - #define RES_BASE_EXTENSION(m_ext) \ public: \ static void register_custom_data_to_otdb() { ClassDB::add_resource_base_extension(m_ext, get_class_static()); } \ diff --git a/core/rid.h b/core/rid.h index c7a71a03a0..381eee645b 100644 --- a/core/rid.h +++ b/core/rid.h @@ -37,10 +37,6 @@ #include "core/set.h" #include "core/typedefs.h" -/** - @author Juan Linietsky <reduzio@gmail.com> -*/ - class RID_OwnerBase; class RID_Data { diff --git a/core/safe_refcount.h b/core/safe_refcount.h index 54f540b0c7..47161eed57 100644 --- a/core/safe_refcount.h +++ b/core/safe_refcount.h @@ -177,12 +177,12 @@ struct SafeRefCount { public: // destroy() is called when weak_count_ drops to zero. - _ALWAYS_INLINE_ bool ref() { //true on success + _ALWAYS_INLINE_ bool ref() { // true on success return atomic_conditional_increment(&count) != 0; } - _ALWAYS_INLINE_ uint32_t refval() { //true on success + _ALWAYS_INLINE_ uint32_t refval() { // none-zero on success return atomic_conditional_increment(&count); } @@ -192,6 +192,11 @@ public: return atomic_decrement(&count) == 0; } + _ALWAYS_INLINE_ uint32_t unrefval() { // 0 if must be disposed of + + return atomic_decrement(&count); + } + _ALWAYS_INLINE_ uint32_t get() const { // nothrow return count; diff --git a/core/script_debugger_local.cpp b/core/script_debugger_local.cpp index ac4dafcf59..e61f9f7158 100644 --- a/core/script_debugger_local.cpp +++ b/core/script_debugger_local.cpp @@ -33,7 +33,7 @@ #include "core/os/os.h" #include "scene/main/scene_tree.h" -void ScriptDebuggerLocal::debug(ScriptLanguage *p_script, bool p_can_continue) { +void ScriptDebuggerLocal::debug(ScriptLanguage *p_script, bool p_can_continue, bool p_is_error_breakpoint) { if (!target_function.empty()) { String current_function = p_script->debug_get_stack_level_function(0); diff --git a/core/script_debugger_local.h b/core/script_debugger_local.h index 19151d4cb0..b3aed5e358 100644 --- a/core/script_debugger_local.h +++ b/core/script_debugger_local.h @@ -48,7 +48,7 @@ class ScriptDebuggerLocal : public ScriptDebugger { void print_variables(const List<String> &names, const List<Variant> &values, const String &variable_prefix); public: - void debug(ScriptLanguage *p_script, bool p_can_continue); + void debug(ScriptLanguage *p_script, bool p_can_continue, bool p_is_error_breakpoint); virtual void send_message(const String &p_message, const Array &p_args); virtual void send_error(const String &p_func, const String &p_file, int p_line, const String &p_err, const String &p_descr, ErrorHandlerType p_type, const Vector<ScriptLanguage::StackInfo> &p_stack_info); diff --git a/core/script_debugger_remote.cpp b/core/script_debugger_remote.cpp index e7ff7a3aef..65ef2a0978 100644 --- a/core/script_debugger_remote.cpp +++ b/core/script_debugger_remote.cpp @@ -89,7 +89,7 @@ Error ScriptDebuggerRemote::connect_to_host(const String &p_host, uint16_t p_por if (tcp_client->get_status() != StreamPeerTCP::STATUS_CONNECTED) { - ERR_PRINTS("Remote Debugger: Unable to connect. Status: " + String::num(tcp_client->get_status())); + ERR_PRINTS("Remote Debugger: Unable to connect. Status: " + String::num(tcp_client->get_status()) + "."); return FAILED; }; @@ -110,7 +110,7 @@ void ScriptDebuggerRemote::_put_variable(const String &p_name, const Variant &p_ int len = 0; Error err = encode_variant(var, NULL, len, true); if (err != OK) - ERR_PRINT("Failed to encode variant"); + ERR_PRINT("Failed to encode variant."); if (len > packet_peer_stream->get_output_buffer_max_size()) { //limit to max size packet_peer_stream->put_var(Variant()); @@ -129,15 +129,15 @@ void ScriptDebuggerRemote::_save_node(ObjectID id, const String &p_path) { ResourceSaver::save(p_path, ps); } -void ScriptDebuggerRemote::debug(ScriptLanguage *p_script, bool p_can_continue) { +void ScriptDebuggerRemote::debug(ScriptLanguage *p_script, bool p_can_continue, bool p_is_error_breakpoint) { //this function is called when there is a debugger break (bug on script) //or when execution is paused from editor - if (!tcp_client->is_connected_to_host()) { - ERR_EXPLAIN("Script Debugger failed to connect, but being used anyway."); - ERR_FAIL(); - } + if (skip_breakpoints && !p_is_error_breakpoint) + return; + + ERR_FAIL_COND_MSG(!tcp_client->is_connected_to_host(), "Script Debugger failed to connect, but being used anyway."); packet_peer_stream->put_var("debug_enter"); packet_peer_stream->put_var(2); @@ -158,6 +158,7 @@ void ScriptDebuggerRemote::debug(ScriptLanguage *p_script, bool p_can_continue) Variant var; Error err = packet_peer_stream->get_var(var); + ERR_CONTINUE(err != OK); ERR_CONTINUE(var.get_type() != Variant::ARRAY); @@ -269,7 +270,6 @@ void ScriptDebuggerRemote::debug(ScriptLanguage *p_script, bool p_can_continue) break; } else if (command == "continue") { - set_depth(-1); set_lines_left(-1); OS::get_singleton()->move_window_to_foreground(); @@ -305,6 +305,8 @@ void ScriptDebuggerRemote::debug(ScriptLanguage *p_script, bool p_can_continue) } else if (command == "save_node") { _save_node(cmd[1], cmd[2]); + } else if (command == "set_skip_breakpoints") { + skip_breakpoints = cmd[1]; } else { _parse_live_edit(cmd); } @@ -357,10 +359,11 @@ void ScriptDebuggerRemote::_get_output() { locking = false; } - if (n_errors_dropped > 0) { + if (n_errors_dropped == 1) { + // Only print one message about dropping per second OutputError oe; oe.error = "TOO_MANY_ERRORS"; - oe.error_descr = "Too many errors! " + String::num_int64(n_errors_dropped) + " errors were dropped."; + oe.error_descr = "Too many errors! Ignoring errors for up to 1 second."; oe.warning = false; uint64_t time = OS::get_singleton()->get_ticks_msec(); oe.hr = time / 3600000; @@ -368,7 +371,20 @@ void ScriptDebuggerRemote::_get_output() { oe.sec = (time / 1000) % 60; oe.msec = time % 1000; errors.push_back(oe); - n_errors_dropped = 0; + } + + if (n_warnings_dropped == 1) { + // Only print one message about dropping per second + OutputError oe; + oe.error = "TOO_MANY_WARNINGS"; + oe.error_descr = "Too many warnings! Ignoring warnings for up to 1 second."; + oe.warning = true; + uint64_t time = OS::get_singleton()->get_ticks_msec(); + oe.hr = time / 3600000; + oe.min = (time / 60000) % 60; + oe.sec = (time / 1000) % 60; + oe.msec = time % 1000; + errors.push_back(oe); } while (errors.size()) { @@ -587,9 +603,19 @@ void ScriptDebuggerRemote::_send_object_id(ObjectID p_id) { } } } + if (Node *node = Object::cast_to<Node>(obj)) { - PropertyInfo pi(Variant::NODE_PATH, String("Node/path")); - properties.push_front(PropertyDesc(pi, node->get_path())); + // in some cases node will not be in tree here + // for instance where it created as variable and not yet added to tree + // in such cases we can't ask for it's path + if (node->is_inside_tree()) { + PropertyInfo pi(Variant::NODE_PATH, String("Node/path")); + properties.push_front(PropertyDesc(pi, node->get_path())); + } else { + PropertyInfo pi(Variant::STRING, String("Node/path")); + properties.push_front(PropertyDesc(pi, "[Orphan]")); + } + } else if (Resource *res = Object::cast_to<Resource>(obj)) { if (Script *s = Object::cast_to<Script>(res)) { Map<StringName, Variant> constants; @@ -743,6 +769,14 @@ void ScriptDebuggerRemote::_poll_events() { profiling = false; _send_profiling_data(false); print_line("PROFILING END!"); + } else if (command == "start_network_profiling") { + + multiplayer->profiling_start(); + profiling_network = true; + } else if (command == "stop_network_profiling") { + + multiplayer->profiling_end(); + profiling_network = false; } else if (command == "reload_scripts") { reload_all_scripts = true; } else if (command == "breakpoint") { @@ -752,6 +786,8 @@ void ScriptDebuggerRemote::_poll_events() { insert_breakpoint(cmd[2], cmd[1]); else remove_breakpoint(cmd[2], cmd[1]); + } else if (command == "set_skip_breakpoints") { + skip_breakpoints = cmd[1]; } else { _parse_live_edit(cmd); } @@ -890,6 +926,18 @@ void ScriptDebuggerRemote::idle_poll() { } } + if (profiling_network) { + uint64_t pt = OS::get_singleton()->get_ticks_msec(); + if (pt - last_net_bandwidth_time > 200) { + last_net_bandwidth_time = pt; + _send_network_bandwidth_usage(); + } + if (pt - last_net_prof_time > 100) { + last_net_prof_time = pt; + _send_network_profiling_data(); + } + } + if (reload_all_scripts) { for (int i = 0; i < ScriptServer::get_language_count(); i++) { @@ -901,6 +949,35 @@ void ScriptDebuggerRemote::idle_poll() { _poll_events(); } +void ScriptDebuggerRemote::_send_network_profiling_data() { + ERR_FAIL_COND(multiplayer.is_null()); + + int n_nodes = multiplayer->get_profiling_frame(&network_profile_info.write[0]); + + packet_peer_stream->put_var("network_profile"); + packet_peer_stream->put_var(n_nodes * 6); + for (int i = 0; i < n_nodes; ++i) { + packet_peer_stream->put_var(network_profile_info[i].node); + packet_peer_stream->put_var(network_profile_info[i].node_path); + packet_peer_stream->put_var(network_profile_info[i].incoming_rpc); + packet_peer_stream->put_var(network_profile_info[i].incoming_rset); + packet_peer_stream->put_var(network_profile_info[i].outgoing_rpc); + packet_peer_stream->put_var(network_profile_info[i].outgoing_rset); + } +} + +void ScriptDebuggerRemote::_send_network_bandwidth_usage() { + ERR_FAIL_COND(multiplayer.is_null()); + + int incoming_bandwidth = multiplayer->get_incoming_bandwidth_usage(); + int outgoing_bandwidth = multiplayer->get_outgoing_bandwidth_usage(); + + packet_peer_stream->put_var("network_bandwidth"); + packet_peer_stream->put_var(2); + packet_peer_stream->put_var(incoming_bandwidth); + packet_peer_stream->put_var(outgoing_bandwidth); +} + void ScriptDebuggerRemote::send_message(const String &p_message, const Array &p_args) { mutex->lock(); @@ -934,6 +1011,19 @@ void ScriptDebuggerRemote::send_error(const String &p_func, const String &p_file oe.msec = time % 1000; Array cstack; + uint64_t ticks = OS::get_singleton()->get_ticks_usec() / 1000; + msec_count += ticks - last_msec; + last_msec = ticks; + + if (msec_count > 1000) { + msec_count = 0; + + err_count = 0; + n_errors_dropped = 0; + warn_count = 0; + n_warnings_dropped = 0; + } + cstack.resize(p_stack_info.size() * 3); for (int i = 0; i < p_stack_info.size(); i++) { cstack[i * 3 + 0] = p_stack_info[i].file; @@ -942,15 +1032,28 @@ void ScriptDebuggerRemote::send_error(const String &p_func, const String &p_file } oe.callstack = cstack; + if (oe.warning) { + warn_count++; + } else { + err_count++; + } mutex->lock(); if (!locking && tcp_client->is_connected_to_host()) { - if (errors.size() >= max_errors_per_frame) { - n_errors_dropped++; + if (oe.warning) { + if (warn_count > max_warnings_per_second) { + n_warnings_dropped++; + } else { + errors.push_back(oe); + } } else { - errors.push_back(oe); + if (err_count > max_errors_per_second) { + n_errors_dropped++; + } else { + errors.push_back(oe); + } } } @@ -1014,6 +1117,10 @@ void ScriptDebuggerRemote::set_live_edit_funcs(LiveEditFuncs *p_funcs) { live_edit_funcs = p_funcs; } +void ScriptDebuggerRemote::set_multiplayer(Ref<MultiplayerAPI> p_multiplayer) { + multiplayer = p_multiplayer; +} + bool ScriptDebuggerRemote::is_profiling() const { return profiling; @@ -1055,25 +1162,35 @@ void ScriptDebuggerRemote::profiling_set_frame_times(float p_frame_time, float p physics_frame_time = p_physics_frame_time; } +void ScriptDebuggerRemote::set_skip_breakpoints(bool p_skip_breakpoints) { + skip_breakpoints = p_skip_breakpoints; +} + ScriptDebuggerRemote::ResourceUsageFunc ScriptDebuggerRemote::resource_usage_func = NULL; ScriptDebuggerRemote::ScriptDebuggerRemote() : profiling(false), + profiling_network(false), max_frame_functions(16), skip_profile_frame(false), reload_all_scripts(false), tcp_client(Ref<StreamPeerTCP>(memnew(StreamPeerTCP))), packet_peer_stream(Ref<PacketPeerStream>(memnew(PacketPeerStream))), last_perf_time(0), + last_net_prof_time(0), + last_net_bandwidth_time(0), performance(Engine::get_singleton()->get_singleton_object("Performance")), requested_quit(false), mutex(Mutex::create()), max_messages_per_frame(GLOBAL_GET("network/limits/debugger_stdout/max_messages_per_frame")), n_messages_dropped(0), - max_errors_per_frame(GLOBAL_GET("network/limits/debugger_stdout/max_errors_per_frame")), + max_errors_per_second(GLOBAL_GET("network/limits/debugger_stdout/max_errors_per_second")), + max_warnings_per_second(GLOBAL_GET("network/limits/debugger_stdout/max_warnings_per_second")), n_errors_dropped(0), max_cps(GLOBAL_GET("network/limits/debugger_stdout/max_chars_per_second")), char_count(0), + err_count(0), + warn_count(0), last_msec(0), msec_count(0), locking(false), @@ -1093,6 +1210,7 @@ ScriptDebuggerRemote::ScriptDebuggerRemote() : add_error_handler(&eh); profile_info.resize(GLOBAL_GET("debug/settings/profiler/max_functions")); + network_profile_info.resize(GLOBAL_GET("debug/settings/profiler/max_functions")); profile_info_ptrs.resize(profile_info.size()); } diff --git a/core/script_debugger_remote.h b/core/script_debugger_remote.h index 1fc9d7c7f1..b6dd925181 100644 --- a/core/script_debugger_remote.h +++ b/core/script_debugger_remote.h @@ -54,11 +54,13 @@ class ScriptDebuggerRemote : public ScriptDebugger { Vector<ScriptLanguage::ProfilingInfo> profile_info; Vector<ScriptLanguage::ProfilingInfo *> profile_info_ptrs; + Vector<MultiplayerAPI::ProfilingInfo> network_profile_info; Map<StringName, int> profiler_function_signature_map; float frame_time, idle_time, physics_time, physics_frame_time; bool profiling; + bool profiling_network; int max_frame_functions; bool skip_profile_frame; bool reload_all_scripts; @@ -67,6 +69,8 @@ class ScriptDebuggerRemote : public ScriptDebugger { Ref<PacketPeerStream> packet_peer_stream; uint64_t last_perf_time; + uint64_t last_net_prof_time; + uint64_t last_net_bandwidth_time; Object *performance; bool requested_quit; Mutex *mutex; @@ -91,11 +95,15 @@ class ScriptDebuggerRemote : public ScriptDebugger { int max_messages_per_frame; int n_messages_dropped; List<OutputError> errors; - int max_errors_per_frame; + int max_errors_per_second; + int max_warnings_per_second; int n_errors_dropped; + int n_warnings_dropped; int max_cps; int char_count; + int err_count; + int warn_count; uint64_t last_msec; uint64_t msec_count; @@ -119,10 +127,14 @@ class ScriptDebuggerRemote : public ScriptDebugger { void _send_video_memory(); LiveEditFuncs *live_edit_funcs; + Ref<MultiplayerAPI> multiplayer; + ErrorHandlerList eh; static void _err_handler(void *, const char *, const char *, int p_line, const char *, const char *, ErrorHandlerType p_type); void _send_profiling_data(bool p_for_frame); + void _send_network_profiling_data(); + void _send_network_bandwidth_usage(); struct FrameData { @@ -136,6 +148,8 @@ class ScriptDebuggerRemote : public ScriptDebugger { void _save_node(ObjectID id, const String &p_path); + bool skip_breakpoints; + public: struct ResourceUsage { @@ -152,7 +166,7 @@ public: static ResourceUsageFunc resource_usage_func; Error connect_to_host(const String &p_host, uint16_t p_port); - virtual void debug(ScriptLanguage *p_script, bool p_can_continue = true); + virtual void debug(ScriptLanguage *p_script, bool p_can_continue = true, bool p_is_error_breakpoint = false); virtual void idle_poll(); virtual void line_poll(); @@ -164,6 +178,7 @@ public: virtual void set_request_scene_tree_message_func(RequestSceneTreeMessageFunc p_func, void *p_udata); virtual void set_live_edit_funcs(LiveEditFuncs *p_funcs); + virtual void set_multiplayer(Ref<MultiplayerAPI> p_multiplayer); virtual bool is_profiling() const; virtual void add_profiling_frame_data(const StringName &p_name, const Array &p_data); @@ -172,6 +187,8 @@ public: virtual void profiling_end(); virtual void profiling_set_frame_times(float p_frame_time, float p_idle_time, float p_physics_time, float p_physics_frame_time); + virtual void set_skip_breakpoints(bool p_skip_breakpoints); + ScriptDebuggerRemote(); ~ScriptDebuggerRemote(); }; diff --git a/core/script_language.cpp b/core/script_language.cpp index 97758ced66..7201773ea5 100644 --- a/core/script_language.cpp +++ b/core/script_language.cpp @@ -50,6 +50,52 @@ void Script::_notification(int p_what) { } } +Variant Script::_get_property_default_value(const StringName &p_property) { + Variant ret; + get_property_default_value(p_property, ret); + return ret; +} + +Array Script::_get_script_property_list() { + Array ret; + List<PropertyInfo> list; + get_script_property_list(&list); + for (List<PropertyInfo>::Element *E = list.front(); E; E = E->next()) { + ret.append(E->get().operator Dictionary()); + } + return ret; +} + +Array Script::_get_script_method_list() { + Array ret; + List<MethodInfo> list; + get_script_method_list(&list); + for (List<MethodInfo>::Element *E = list.front(); E; E = E->next()) { + ret.append(E->get().operator Dictionary()); + } + return ret; +} + +Array Script::_get_script_signal_list() { + Array ret; + List<MethodInfo> list; + get_script_signal_list(&list); + for (List<MethodInfo>::Element *E = list.front(); E; E = E->next()) { + ret.append(E->get().operator Dictionary()); + } + return ret; +} + +Dictionary Script::_get_script_constant_map() { + Dictionary ret; + Map<StringName, Variant> map; + get_constants(&map); + for (Map<StringName, Variant>::Element *E = map.front(); E; E = E->next()) { + ret[E->key()] = E->value(); + } + return ret; +} + void Script::_bind_methods() { ClassDB::bind_method(D_METHOD("can_instance"), &Script::can_instance); @@ -64,6 +110,12 @@ void Script::_bind_methods() { ClassDB::bind_method(D_METHOD("has_script_signal", "signal_name"), &Script::has_script_signal); + ClassDB::bind_method(D_METHOD("get_script_property_list"), &Script::_get_script_property_list); + ClassDB::bind_method(D_METHOD("get_script_method_list"), &Script::_get_script_method_list); + ClassDB::bind_method(D_METHOD("get_script_signal_list"), &Script::_get_script_signal_list); + ClassDB::bind_method(D_METHOD("get_script_constant_map"), &Script::_get_script_constant_map); + ClassDB::bind_method(D_METHOD("get_property_default_value", "property"), &Script::_get_property_default_value); + ClassDB::bind_method(D_METHOD("is_tool"), &Script::is_tool); ADD_PROPERTY(PropertyInfo(Variant::STRING, "source_code", PROPERTY_HINT_NONE, "", 0), "set_source_code", "get_source_code"); diff --git a/core/script_language.h b/core/script_language.h index 87f103bb33..116918fdc0 100644 --- a/core/script_language.h +++ b/core/script_language.h @@ -36,10 +36,6 @@ #include "core/pair.h" #include "core/resource.h" -/** - @author Juan Linietsky <reduzio@gmail.com> -*/ - class ScriptLanguage; typedef void (*ScriptEditRequestFunction)(const String &p_path); @@ -113,6 +109,12 @@ protected: friend class PlaceHolderScriptInstance; virtual void _placeholder_erased(PlaceHolderScriptInstance *p_placeholder) {} + Variant _get_property_default_value(const StringName &p_property); + Array _get_script_property_list(); + Array _get_script_method_list(); + Array _get_script_signal_list(); + Dictionary _get_script_constant_map(); + public: virtual bool can_instance() const = 0; @@ -465,7 +467,7 @@ public: void clear_breakpoints(); const Map<int, Set<StringName> > &get_breakpoints() const { return breakpoints; } - virtual void debug(ScriptLanguage *p_script, bool p_can_continue = true) = 0; + virtual void debug(ScriptLanguage *p_script, bool p_can_continue = true, bool p_is_error_breakpoint = false) = 0; virtual void idle_poll(); virtual void line_poll(); @@ -480,6 +482,7 @@ public: virtual void set_request_scene_tree_message_func(RequestSceneTreeMessageFunc p_func, void *p_udata) {} virtual void set_live_edit_funcs(LiveEditFuncs *p_funcs) {} + virtual void set_multiplayer(Ref<MultiplayerAPI> p_multiplayer) {} virtual bool is_profiling() const = 0; virtual void add_profiling_frame_data(const StringName &p_name, const Array &p_data) = 0; diff --git a/core/self_list.h b/core/self_list.h index 314d440977..07abcd1d53 100644 --- a/core/self_list.h +++ b/core/self_list.h @@ -31,6 +31,7 @@ #ifndef SELF_LIST_H #define SELF_LIST_H +#include "core/error_macros.h" #include "core/typedefs.h" template <class T> diff --git a/core/set.h b/core/set.h index b2c717880d..68431c294a 100644 --- a/core/set.h +++ b/core/set.h @@ -34,10 +34,6 @@ #include "core/os/memory.h" #include "core/typedefs.h" -/** - @author Juan Linietsky <reduzio@gmail.com> -*/ - // based on the very nice implementation of rb-trees by: // https://web.archive.org/web/20120507164830/http://web.mit.edu/~emin/www/source_code/red_black_tree/index.html diff --git a/core/sort_array.h b/core/sort_array.h index 8660ee3333..d330e0c647 100644 --- a/core/sort_array.h +++ b/core/sort_array.h @@ -31,6 +31,7 @@ #ifndef SORT_ARRAY_H #define SORT_ARRAY_H +#include "core/error_macros.h" #include "core/typedefs.h" #define ERR_BAD_COMPARE(cond) \ diff --git a/core/string_builder.cpp b/core/string_builder.cpp index 35526e0d70..22eed70f8b 100644 --- a/core/string_builder.cpp +++ b/core/string_builder.cpp @@ -34,6 +34,9 @@ StringBuilder &StringBuilder::append(const String &p_string) { + if (p_string == String()) + return *this; + strings.push_back(p_string); appended_strings.push_back(-1); diff --git a/core/string_name.h b/core/string_name.h index 0984b0181f..6dd960abd5 100644 --- a/core/string_name.h +++ b/core/string_name.h @@ -34,9 +34,6 @@ #include "core/os/mutex.h" #include "core/safe_refcount.h" #include "core/ustring.h" -/** - @author Juan Linietsky <reduzio@gmail.com> -*/ struct StaticCString { diff --git a/core/translation.cpp b/core/translation.cpp index 0b55badc61..a0902d71fc 100644 --- a/core/translation.cpp +++ b/core/translation.cpp @@ -848,8 +848,7 @@ void Translation::set_locale(const String &p_locale) { if (!TranslationServer::is_locale_valid(univ_locale)) { String trimmed_locale = get_trimmed_locale(univ_locale); - ERR_EXPLAIN("Invalid locale: " + trimmed_locale); - ERR_FAIL_COND(!TranslationServer::is_locale_valid(trimmed_locale)); + ERR_FAIL_COND_MSG(!TranslationServer::is_locale_valid(trimmed_locale), "Invalid locale: " + trimmed_locale + "."); locale = trimmed_locale; } else { @@ -1044,6 +1043,13 @@ StringName TranslationServer::translate(const StringName &p_message) const { if (!enabled) return p_message; + // Locale can be of the form 'll_CC', i.e. language code and regional code, + // e.g. 'en_US', 'en_GB', etc. It might also be simply 'll', e.g. 'en'. + // To find the relevant translation, we look for those with locale starting + // with the language code, and then if any is an exact match for the long + // form. If not found, we fall back to a near match (another locale with + // same language code). + StringName res; bool near_match = false; const CharType *lptr = &locale[0]; @@ -1053,13 +1059,11 @@ StringName TranslationServer::translate(const StringName &p_message) const { const Ref<Translation> &t = E->get(); String l = t->get_locale(); if (lptr[0] != l[0] || lptr[1] != l[1]) - continue; // locale not match - - //near match - bool match = (l != locale); + continue; // Language code does not match. - if (near_match && !match) - continue; //only near-match once + bool exact_match = (l == locale); + if (!exact_match && near_match) + continue; // Only near-match once, but keep looking for exact matches. StringName r = t->get_message(p_message); @@ -1068,43 +1072,38 @@ StringName TranslationServer::translate(const StringName &p_message) const { res = r; - if (match) + if (exact_match) break; else near_match = true; } - if (!res) { - //try again with fallback - if (fallback.length() >= 2) { - - const CharType *fptr = &fallback[0]; - near_match = false; - for (const Set<Ref<Translation> >::Element *E = translations.front(); E; E = E->next()) { + if (!res && fallback.length() >= 2) { + // Try again with the fallback locale. + const CharType *fptr = &fallback[0]; + near_match = false; + for (const Set<Ref<Translation> >::Element *E = translations.front(); E; E = E->next()) { - const Ref<Translation> &t = E->get(); - String l = t->get_locale(); - if (fptr[0] != l[0] || fptr[1] != l[1]) - continue; // locale not match + const Ref<Translation> &t = E->get(); + String l = t->get_locale(); + if (fptr[0] != l[0] || fptr[1] != l[1]) + continue; // Language code does not match. - //near match - bool match = (l != fallback); + bool exact_match = (l == fallback); + if (!exact_match && near_match) + continue; // Only near-match once, but keep looking for exact matches. - if (near_match && !match) - continue; //only near-match once + StringName r = t->get_message(p_message); - StringName r = t->get_message(p_message); + if (!r) + continue; - if (!r) - continue; + res = r; - res = r; - - if (match) - break; - else - near_match = true; - } + if (exact_match) + break; + else + near_match = true; } } diff --git a/core/typedefs.h b/core/typedefs.h index 660139b90a..767a97ac38 100644 --- a/core/typedefs.h +++ b/core/typedefs.h @@ -108,7 +108,6 @@ T *_nullptr() { #include "core/int_types.h" #include "core/error_list.h" -#include "core/error_macros.h" /** Generic ABS function, for math uses please use Math::abs */ diff --git a/core/ustring.cpp b/core/ustring.cpp index ed401c3763..f029ae4200 100644 --- a/core/ustring.cpp +++ b/core/ustring.cpp @@ -31,7 +31,7 @@ #include "ustring.h" #include "core/color.h" -#include "core/math/crypto_core.h" +#include "core/crypto/crypto_core.h" #include "core/math/math_funcs.h" #include "core/os/memory.h" #include "core/print_string.h" @@ -40,6 +40,7 @@ #include "core/variant.h" #include <wchar.h> +#include <cstdint> #ifndef NO_USE_STDLIB #include <stdio.h> @@ -1668,6 +1669,7 @@ int String::hex_to_int(bool p_with_prefix) const { return 0; } + ERR_FAIL_COND_V_MSG(hex > INT32_MAX / 16, sign == 1 ? INT32_MAX : INT32_MIN, "Cannot represent " + *this + " as integer, provided value is " + (sign == 1 ? "too big." : "too small.")); hex *= 16; hex += n; s++; @@ -1709,6 +1711,7 @@ int64_t String::hex_to_int64(bool p_with_prefix) const { return 0; } + ERR_FAIL_COND_V_MSG(hex > INT64_MAX / 16, sign == 1 ? INT64_MAX : INT64_MIN, "Cannot represent " + *this + " as 64-bit integer, provided value is " + (sign == 1 ? "too big." : "too small.")); hex *= 16; hex += n; s++; @@ -1748,6 +1751,7 @@ int64_t String::bin_to_int64(bool p_with_prefix) const { return 0; } + ERR_FAIL_COND_V_MSG(binary > INT64_MAX / 2, sign == 1 ? INT64_MAX : INT64_MIN, "Cannot represent " + *this + " as 64-bit integer, provided value is " + (sign == 1 ? "too big." : "too small.")); binary *= 2; binary += n; s++; @@ -1771,6 +1775,7 @@ int String::to_int() const { CharType c = operator[](i); if (c >= '0' && c <= '9') { + ERR_FAIL_COND_V_MSG(integer > INT32_MAX / 10, sign == 1 ? INT32_MAX : INT32_MIN, "Cannot represent " + *this + " as integer, provided value is " + (sign == 1 ? "too big." : "too small.")); integer *= 10; integer += c - '0'; @@ -1798,6 +1803,7 @@ int64_t String::to_int64() const { CharType c = operator[](i); if (c >= '0' && c <= '9') { + ERR_FAIL_COND_V_MSG(integer > INT64_MAX / 10, sign == 1 ? INT64_MAX : INT64_MIN, "Cannot represent " + *this + " as 64-bit integer, provided value is " + (sign == 1 ? "too big." : "too small.")); integer *= 10; integer += c - '0'; @@ -1828,6 +1834,7 @@ int String::to_int(const char *p_str, int p_len) { char c = p_str[i]; if (c >= '0' && c <= '9') { + ERR_FAIL_COND_V_MSG(integer > INT32_MAX / 10, sign == 1 ? INT32_MAX : INT32_MIN, "Cannot represent " + String(p_str).substr(0, to) + " as integer, provided value is " + (sign == 1 ? "too big." : "too small.")); integer *= 10; integer += c - '0'; @@ -2140,6 +2147,14 @@ int64_t String::to_int(const CharType *p_str, int p_len) { if (c >= '0' && c <= '9') { + if (integer > INT64_MAX / 10) { + String number(""); + str = p_str; + while (*str && str != limit) { + number += *(str++); + } + ERR_FAIL_V_MSG(sign == 1 ? INT64_MAX : INT64_MIN, "Cannot represent " + number + " as integer, provided value is " + (sign == 1 ? "too big." : "too small.")); + } integer *= 10; integer += c - '0'; } else { @@ -3034,6 +3049,22 @@ String String::replacen(const String &p_key, const String &p_with) const { return new_string; } +String String::repeat(int p_count) const { + + ERR_FAIL_COND_V_MSG(p_count < 0, "", "Parameter count should be a positive number."); + + String new_string; + const CharType *src = this->c_str(); + + new_string.resize(length() * p_count + 1); + + for (int i = 0; i < p_count; i++) + for (int j = 0; j < length(); j++) + new_string[i * length() + j] = src[j]; + + return new_string; +} + String String::left(int p_pos) const { if (p_pos <= 0) @@ -3257,9 +3288,7 @@ String String::simplify_path() const { static int _humanize_digits(int p_num) { - if (p_num < 10) - return 2; - else if (p_num < 100) + if (p_num < 100) return 2; else if (p_num < 1024) return 1; @@ -3267,21 +3296,29 @@ static int _humanize_digits(int p_num) { return 0; } -String String::humanize_size(size_t p_size) { +String String::humanize_size(uint64_t p_size) { uint64_t _div = 1; - static const char *prefix[] = { " Bytes", " KB", " MB", " GB", " TB", " PB", " EB", "" }; + Vector<String> prefixes; + prefixes.push_back(RTR("B")); + prefixes.push_back(RTR("KiB")); + prefixes.push_back(RTR("MiB")); + prefixes.push_back(RTR("GiB")); + prefixes.push_back(RTR("TiB")); + prefixes.push_back(RTR("PiB")); + prefixes.push_back(RTR("EiB")); + int prefix_idx = 0; - while (p_size > (_div * 1024) && prefix[prefix_idx][0]) { + while (prefix_idx < prefixes.size() && p_size > (_div * 1024)) { _div *= 1024; prefix_idx++; } - int digits = prefix_idx > 0 ? _humanize_digits(p_size / _div) : 0; - double divisor = prefix_idx > 0 ? _div : 1; + const int digits = prefix_idx > 0 ? _humanize_digits(p_size / _div) : 0; + const double divisor = prefix_idx > 0 ? _div : 1; - return String::num(p_size / divisor).pad_decimals(digits) + prefix[prefix_idx]; + return String::num(p_size / divisor).pad_decimals(digits) + " " + prefixes[prefix_idx]; } bool String::is_abs_path() const { diff --git a/core/ustring.h b/core/ustring.h index 3eb5c47b3a..15e2c07d9f 100644 --- a/core/ustring.h +++ b/core/ustring.h @@ -36,10 +36,6 @@ #include "core/typedefs.h" #include "core/vector.h" -/** - @author Juan Linietsky <reduzio@gmail.com> -*/ - template <class T> class CharProxy { friend class CharString; @@ -227,6 +223,7 @@ public: String replace(const String &p_key, const String &p_with) const; String replace(const char *p_key, const char *p_with) const; String replacen(const String &p_key, const String &p_with) const; + String repeat(int p_count) const; String insert(int p_at_pos, const String &p_string) const; String pad_decimals(int p_digits) const; String pad_zeros(int p_digits) const; @@ -325,7 +322,7 @@ public: String path_to_file(const String &p_path) const; String get_base_dir() const; String get_file() const; - static String humanize_size(size_t p_size); + static String humanize_size(uint64_t p_size); String simplify_path() const; String xml_escape(bool p_escape_quotes = false) const; diff --git a/core/variant.cpp b/core/variant.cpp index 1574af5239..16bbf94c54 100644 --- a/core/variant.cpp +++ b/core/variant.cpp @@ -910,7 +910,15 @@ bool Variant::is_one() const { void Variant::reference(const Variant &p_variant) { - clear(); + switch (type) { + case NIL: + case BOOL: + case INT: + case REAL: + break; + default: + clear(); + } type = p_variant.type; @@ -1740,10 +1748,7 @@ Variant::operator RID() const { } else if (type == OBJECT && _get_obj().obj) { #ifdef DEBUG_ENABLED if (ScriptDebugger::get_singleton()) { - if (!ObjectDB::instance_validate(_get_obj().obj)) { - ERR_EXPLAIN("Invalid pointer (object was deleted)"); - ERR_FAIL_V(RID()); - }; + ERR_FAIL_COND_V_MSG(!ObjectDB::instance_validate(_get_obj().obj), RID(), "Invalid pointer (object was deleted)."); }; #endif Variant::CallError ce; diff --git a/core/variant.h b/core/variant.h index a8e99c13f1..c4f69c3e8d 100644 --- a/core/variant.h +++ b/core/variant.h @@ -31,10 +31,6 @@ #ifndef VARIANT_H #define VARIANT_H -/** - @author Juan Linietsky <reduzio@gmail.com> -*/ - #include "core/array.h" #include "core/color.h" #include "core/dictionary.h" diff --git a/core/variant_call.cpp b/core/variant_call.cpp index 377cc889b8..40b944744b 100644 --- a/core/variant_call.cpp +++ b/core/variant_call.cpp @@ -32,8 +32,8 @@ #include "core/color_names.inc" #include "core/core_string_names.h" +#include "core/crypto/crypto_core.h" #include "core/io/compression.h" -#include "core/math/crypto_core.h" #include "core/object.h" #include "core/os/os.h" #include "core/script_language.h" @@ -256,6 +256,7 @@ struct _VariantCall { VCALL_LOCALMEM2R(String, format); VCALL_LOCALMEM2R(String, replace); VCALL_LOCALMEM2R(String, replacen); + VCALL_LOCALMEM1R(String, repeat); VCALL_LOCALMEM2R(String, insert); VCALL_LOCALMEM0R(String, capitalize); VCALL_LOCALMEM3R(String, split); @@ -283,6 +284,7 @@ struct _VariantCall { VCALL_LOCALMEM0R(String, sha1_buffer); VCALL_LOCALMEM0R(String, sha256_buffer); VCALL_LOCALMEM0R(String, empty); + VCALL_LOCALMEM1R(String, humanize_size); VCALL_LOCALMEM0R(String, is_abs_path); VCALL_LOCALMEM0R(String, is_rel_path); VCALL_LOCALMEM0R(String, get_base_dir); @@ -314,6 +316,10 @@ struct _VariantCall { static void _call_String_to_ascii(Variant &r_ret, Variant &p_self, const Variant **p_args) { String *s = reinterpret_cast<String *>(p_self._data._mem); + if (s->empty()) { + r_ret = PoolByteArray(); + return; + } CharString charstr = s->ascii(); PoolByteArray retval; @@ -329,6 +335,10 @@ struct _VariantCall { static void _call_String_to_utf8(Variant &r_ret, Variant &p_self, const Variant **p_args) { String *s = reinterpret_cast<String *>(p_self._data._mem); + if (s->empty()) { + r_ret = PoolByteArray(); + return; + } CharString charstr = s->utf8(); PoolByteArray retval; @@ -347,6 +357,8 @@ struct _VariantCall { VCALL_LOCALMEM0R(Vector2, is_normalized); VCALL_LOCALMEM1R(Vector2, distance_to); VCALL_LOCALMEM1R(Vector2, distance_squared_to); + VCALL_LOCALMEM1R(Vector2, posmod); + VCALL_LOCALMEM1R(Vector2, posmodv); VCALL_LOCALMEM1R(Vector2, project); VCALL_LOCALMEM1R(Vector2, angle_to); VCALL_LOCALMEM1R(Vector2, angle_to_point); @@ -370,6 +382,7 @@ struct _VariantCall { VCALL_LOCALMEM1R(Vector2, cross); VCALL_LOCALMEM0R(Vector2, abs); VCALL_LOCALMEM1R(Vector2, clamped); + VCALL_LOCALMEM0R(Vector2, sign); VCALL_LOCALMEM0R(Rect2, get_area); VCALL_LOCALMEM1R(Rect2, intersects); @@ -407,12 +420,15 @@ struct _VariantCall { VCALL_LOCALMEM0R(Vector3, round); VCALL_LOCALMEM1R(Vector3, distance_to); VCALL_LOCALMEM1R(Vector3, distance_squared_to); + VCALL_LOCALMEM1R(Vector3, posmod); + VCALL_LOCALMEM1R(Vector3, posmodv); VCALL_LOCALMEM1R(Vector3, project); VCALL_LOCALMEM1R(Vector3, angle_to); VCALL_LOCALMEM1R(Vector3, direction_to); VCALL_LOCALMEM1R(Vector3, slide); VCALL_LOCALMEM1R(Vector3, bounce); VCALL_LOCALMEM1R(Vector3, reflect); + VCALL_LOCALMEM0R(Vector3, sign); VCALL_LOCALMEM0R(Plane, normalized); VCALL_LOCALMEM0R(Plane, center); @@ -528,6 +544,7 @@ struct _VariantCall { VCALL_LOCALMEM2R(Array, bsearch); VCALL_LOCALMEM4R(Array, bsearch_custom); VCALL_LOCALMEM1R(Array, duplicate); + VCALL_LOCALMEM4R(Array, slice); VCALL_LOCALMEM0(Array, invert); VCALL_LOCALMEM0R(Array, max); VCALL_LOCALMEM0R(Array, min); @@ -536,7 +553,7 @@ struct _VariantCall { PoolByteArray *ba = reinterpret_cast<PoolByteArray *>(p_self._data._mem); String s; - if (ba->size() >= 0) { + if (ba->size() > 0) { PoolByteArray::Read r = ba->read(); CharString cs; cs.resize(ba->size() + 1); @@ -552,7 +569,7 @@ struct _VariantCall { PoolByteArray *ba = reinterpret_cast<PoolByteArray *>(p_self._data._mem); String s; - if (ba->size() >= 0) { + if (ba->size() > 0) { PoolByteArray::Read r = ba->read(); s.parse_utf8((const char *)r.ptr(), ba->size()); } @@ -563,14 +580,15 @@ struct _VariantCall { PoolByteArray *ba = reinterpret_cast<PoolByteArray *>(p_self._data._mem); PoolByteArray compressed; - Compression::Mode mode = (Compression::Mode)(int)(*p_args[0]); - - compressed.resize(Compression::get_max_compressed_buffer_size(ba->size(), mode)); - int result = Compression::compress(compressed.write().ptr(), ba->read().ptr(), ba->size(), mode); + if (ba->size() > 0) { + Compression::Mode mode = (Compression::Mode)(int)(*p_args[0]); - result = result >= 0 ? result : 0; - compressed.resize(result); + compressed.resize(Compression::get_max_compressed_buffer_size(ba->size(), mode)); + int result = Compression::compress(compressed.write().ptr(), ba->read().ptr(), ba->size(), mode); + result = result >= 0 ? result : 0; + compressed.resize(result); + } r_ret = compressed; } @@ -582,10 +600,9 @@ struct _VariantCall { int buffer_size = (int)(*p_args[0]); - if (buffer_size < 0) { + if (buffer_size <= 0) { r_ret = decompressed; - ERR_EXPLAIN("Decompression buffer size is less than zero"); - ERR_FAIL(); + ERR_FAIL_MSG("Decompression buffer size must be greater than zero."); } decompressed.resize(buffer_size); @@ -597,13 +614,14 @@ struct _VariantCall { r_ret = decompressed; } - static void _call_PoolByteArray_sha256_string(Variant &r_ret, Variant &p_self, const Variant **p_args) { + static void _call_PoolByteArray_hex_encode(Variant &r_ret, Variant &p_self, const Variant **p_args) { PoolByteArray *ba = reinterpret_cast<PoolByteArray *>(p_self._data._mem); + if (ba->size() == 0) { + r_ret = String(); + return; + } PoolByteArray::Read r = ba->read(); - String s; - unsigned char hash[32]; - CryptoCore::sha256((unsigned char *)r.ptr(), ba->size(), hash); - s = String::hex_encode_buffer(hash, 32); + String s = String::hex_encode_buffer(&r[0], ba->size()); r_ret = s; } @@ -749,6 +767,7 @@ struct _VariantCall { case Variant::VECTOR2: r_ret = reinterpret_cast<Transform2D *>(p_self._data._ptr)->xform(p_args[0]->operator Vector2()); return; case Variant::RECT2: r_ret = reinterpret_cast<Transform2D *>(p_self._data._ptr)->xform(p_args[0]->operator Rect2()); return; + case Variant::POOL_VECTOR2_ARRAY: r_ret = reinterpret_cast<Transform2D *>(p_self._data._ptr)->xform(p_args[0]->operator PoolVector2Array()); return; default: r_ret = Variant(); } } @@ -759,6 +778,7 @@ struct _VariantCall { case Variant::VECTOR2: r_ret = reinterpret_cast<Transform2D *>(p_self._data._ptr)->xform_inv(p_args[0]->operator Vector2()); return; case Variant::RECT2: r_ret = reinterpret_cast<Transform2D *>(p_self._data._ptr)->xform_inv(p_args[0]->operator Rect2()); return; + case Variant::POOL_VECTOR2_ARRAY: r_ret = reinterpret_cast<Transform2D *>(p_self._data._ptr)->xform_inv(p_args[0]->operator PoolVector2Array()); return; default: r_ret = Variant(); } } @@ -815,6 +835,7 @@ struct _VariantCall { case Variant::VECTOR3: r_ret = reinterpret_cast<Transform *>(p_self._data._ptr)->xform(p_args[0]->operator Vector3()); return; case Variant::PLANE: r_ret = reinterpret_cast<Transform *>(p_self._data._ptr)->xform(p_args[0]->operator Plane()); return; case Variant::AABB: r_ret = reinterpret_cast<Transform *>(p_self._data._ptr)->xform(p_args[0]->operator ::AABB()); return; + case Variant::POOL_VECTOR3_ARRAY: r_ret = reinterpret_cast<Transform *>(p_self._data._ptr)->xform(p_args[0]->operator ::PoolVector3Array()); return; default: r_ret = Variant(); } } @@ -826,6 +847,7 @@ struct _VariantCall { case Variant::VECTOR3: r_ret = reinterpret_cast<Transform *>(p_self._data._ptr)->xform_inv(p_args[0]->operator Vector3()); return; case Variant::PLANE: r_ret = reinterpret_cast<Transform *>(p_self._data._ptr)->xform_inv(p_args[0]->operator Plane()); return; case Variant::AABB: r_ret = reinterpret_cast<Transform *>(p_self._data._ptr)->xform_inv(p_args[0]->operator ::AABB()); return; + case Variant::POOL_VECTOR3_ARRAY: r_ret = reinterpret_cast<Transform *>(p_self._data._ptr)->xform_inv(p_args[0]->operator ::PoolVector3Array()); return; default: r_ret = Variant(); } } @@ -914,7 +936,7 @@ struct _VariantCall { static void Quat_init2(Variant &r_ret, const Variant **p_args) { - r_ret = Quat(((Vector3)(*p_args[0])), ((float)(*p_args[1]))); + r_ret = Quat(((Vector3)(*p_args[0])), ((real_t)(*p_args[1]))); } static void Quat_init3(Variant &r_ret, const Variant **p_args) { @@ -1523,6 +1545,7 @@ void register_variant_methods() { ADDFUNC2R(STRING, STRING, String, format, NIL, "values", STRING, "placeholder", varray("{_}")); ADDFUNC2R(STRING, STRING, String, replace, STRING, "what", STRING, "forwhat", varray()); ADDFUNC2R(STRING, STRING, String, replacen, STRING, "what", STRING, "forwhat", varray()); + ADDFUNC1R(STRING, STRING, String, repeat, INT, "count", varray()); ADDFUNC2R(STRING, STRING, String, insert, INT, "position", STRING, "what", varray()); ADDFUNC0R(STRING, STRING, String, capitalize, varray()); ADDFUNC3R(STRING, POOL_STRING_ARRAY, String, split, STRING, "delimiter", BOOL, "allow_empty", INT, "maxsplit", varray(true, 0)); @@ -1552,6 +1575,7 @@ void register_variant_methods() { ADDFUNC0R(STRING, POOL_BYTE_ARRAY, String, sha1_buffer, varray()); ADDFUNC0R(STRING, POOL_BYTE_ARRAY, String, sha256_buffer, varray()); ADDFUNC0R(STRING, BOOL, String, empty, varray()); + ADDFUNC1R(STRING, STRING, String, humanize_size, INT, "size", varray()); ADDFUNC0R(STRING, BOOL, String, is_abs_path, varray()); ADDFUNC0R(STRING, BOOL, String, is_rel_path, varray()); ADDFUNC0R(STRING, STRING, String, get_base_dir, varray()); @@ -1591,6 +1615,8 @@ void register_variant_methods() { ADDFUNC1R(VECTOR2, VECTOR2, Vector2, direction_to, VECTOR2, "b", varray()); ADDFUNC1R(VECTOR2, REAL, Vector2, distance_to, VECTOR2, "to", varray()); ADDFUNC1R(VECTOR2, REAL, Vector2, distance_squared_to, VECTOR2, "to", varray()); + ADDFUNC1R(VECTOR2, VECTOR2, Vector2, posmod, REAL, "mod", varray()); + ADDFUNC1R(VECTOR2, VECTOR2, Vector2, posmodv, VECTOR2, "modv", varray()); ADDFUNC1R(VECTOR2, VECTOR2, Vector2, project, VECTOR2, "b", varray()); ADDFUNC1R(VECTOR2, REAL, Vector2, angle_to, VECTOR2, "to", varray()); ADDFUNC1R(VECTOR2, REAL, Vector2, angle_to_point, VECTOR2, "to", varray()); @@ -1612,6 +1638,7 @@ void register_variant_methods() { ADDFUNC1R(VECTOR2, REAL, Vector2, cross, VECTOR2, "with", varray()); ADDFUNC0R(VECTOR2, VECTOR2, Vector2, abs, varray()); ADDFUNC1R(VECTOR2, VECTOR2, Vector2, clamped, REAL, "length", varray()); + ADDFUNC0R(VECTOR2, VECTOR2, Vector2, sign, varray()); ADDFUNC0R(RECT2, REAL, Rect2, get_area, varray()); ADDFUNC1R(RECT2, BOOL, Rect2, intersects, RECT2, "b", varray()); @@ -1650,11 +1677,14 @@ void register_variant_methods() { ADDFUNC0R(VECTOR3, VECTOR3, Vector3, round, varray()); ADDFUNC1R(VECTOR3, REAL, Vector3, distance_to, VECTOR3, "b", varray()); ADDFUNC1R(VECTOR3, REAL, Vector3, distance_squared_to, VECTOR3, "b", varray()); + ADDFUNC1R(VECTOR3, VECTOR3, Vector3, posmod, REAL, "mod", varray()); + ADDFUNC1R(VECTOR3, VECTOR3, Vector3, posmodv, VECTOR3, "modv", varray()); ADDFUNC1R(VECTOR3, VECTOR3, Vector3, project, VECTOR3, "b", varray()); ADDFUNC1R(VECTOR3, REAL, Vector3, angle_to, VECTOR3, "to", varray()); ADDFUNC1R(VECTOR3, VECTOR3, Vector3, slide, VECTOR3, "n", varray()); ADDFUNC1R(VECTOR3, VECTOR3, Vector3, bounce, VECTOR3, "n", varray()); ADDFUNC1R(VECTOR3, VECTOR3, Vector3, reflect, VECTOR3, "n", varray()); + ADDFUNC0R(VECTOR3, VECTOR3, Vector3, sign, varray()); ADDFUNC0R(PLANE, PLANE, Plane, normalized, varray()); ADDFUNC0R(PLANE, VECTOR3, Plane, center, varray()); @@ -1747,6 +1777,7 @@ void register_variant_methods() { ADDFUNC4R(ARRAY, INT, Array, bsearch_custom, NIL, "value", OBJECT, "obj", STRING, "func", BOOL, "before", varray(true)); ADDFUNC0NC(ARRAY, NIL, Array, invert, varray()); ADDFUNC1R(ARRAY, ARRAY, Array, duplicate, BOOL, "deep", varray(false)); + ADDFUNC4R(ARRAY, ARRAY, Array, slice, INT, "begin", INT, "end", INT, "step", BOOL, "deep", varray(1, false)); ADDFUNC0R(ARRAY, NIL, Array, max, varray()); ADDFUNC0R(ARRAY, NIL, Array, min, varray()); @@ -1763,7 +1794,7 @@ void register_variant_methods() { ADDFUNC0R(POOL_BYTE_ARRAY, STRING, PoolByteArray, get_string_from_ascii, varray()); ADDFUNC0R(POOL_BYTE_ARRAY, STRING, PoolByteArray, get_string_from_utf8, varray()); - ADDFUNC0R(POOL_BYTE_ARRAY, STRING, PoolByteArray, sha256_string, varray()); + ADDFUNC0R(POOL_BYTE_ARRAY, STRING, PoolByteArray, hex_encode, varray()); ADDFUNC1R(POOL_BYTE_ARRAY, POOL_BYTE_ARRAY, PoolByteArray, compress, INT, "compression_mode", varray(0)); ADDFUNC2R(POOL_BYTE_ARRAY, POOL_BYTE_ARRAY, PoolByteArray, decompress, INT, "buffer_size", INT, "compression_mode", varray(0)); @@ -1947,6 +1978,9 @@ void register_variant_methods() { _VariantCall::add_variant_constant(Variant::VECTOR3, "FORWARD", Vector3(0, 0, -1)); _VariantCall::add_variant_constant(Variant::VECTOR3, "BACK", Vector3(0, 0, 1)); + _VariantCall::add_constant(Variant::VECTOR2, "AXIS_X", Vector2::AXIS_X); + _VariantCall::add_constant(Variant::VECTOR2, "AXIS_Y", Vector2::AXIS_Y); + _VariantCall::add_variant_constant(Variant::VECTOR2, "ZERO", Vector2(0, 0)); _VariantCall::add_variant_constant(Variant::VECTOR2, "ONE", Vector2(1, 1)); _VariantCall::add_variant_constant(Variant::VECTOR2, "INF", Vector2(Math_INF, Math_INF)); @@ -1955,19 +1989,27 @@ void register_variant_methods() { _VariantCall::add_variant_constant(Variant::VECTOR2, "UP", Vector2(0, -1)); _VariantCall::add_variant_constant(Variant::VECTOR2, "DOWN", Vector2(0, 1)); - _VariantCall::add_variant_constant(Variant::TRANSFORM2D, "IDENTITY", Transform2D(1, 0, 0, 1, 0, 0)); + _VariantCall::add_variant_constant(Variant::TRANSFORM2D, "IDENTITY", Transform2D()); _VariantCall::add_variant_constant(Variant::TRANSFORM2D, "FLIP_X", Transform2D(-1, 0, 0, 1, 0, 0)); _VariantCall::add_variant_constant(Variant::TRANSFORM2D, "FLIP_Y", Transform2D(1, 0, 0, -1, 0, 0)); - Transform identity_transform, transform_x, transform_y, transform_z; - identity_transform.set(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0); + Transform identity_transform = Transform(); + Transform flip_x_transform = Transform(-1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0); + Transform flip_y_transform = Transform(1, 0, 0, 0, -1, 0, 0, 0, 1, 0, 0, 0); + Transform flip_z_transform = Transform(1, 0, 0, 0, 1, 0, 0, 0, -1, 0, 0, 0); _VariantCall::add_variant_constant(Variant::TRANSFORM, "IDENTITY", identity_transform); - transform_x.set(-1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0); - _VariantCall::add_variant_constant(Variant::TRANSFORM, "FLIP_X", transform_x); - transform_y.set(1, 0, 0, 0, -1, 0, 0, 0, 1, 0, 0, 0); - _VariantCall::add_variant_constant(Variant::TRANSFORM, "FLIP_Y", transform_y); - transform_z.set(1, 0, 0, 0, 1, 0, 0, 0, -1, 0, 0, 0); - _VariantCall::add_variant_constant(Variant::TRANSFORM, "FLIP_Z", transform_z); + _VariantCall::add_variant_constant(Variant::TRANSFORM, "FLIP_X", flip_x_transform); + _VariantCall::add_variant_constant(Variant::TRANSFORM, "FLIP_Y", flip_y_transform); + _VariantCall::add_variant_constant(Variant::TRANSFORM, "FLIP_Z", flip_z_transform); + + Basis identity_basis = Basis(); + Basis flip_x_basis = Basis(-1, 0, 0, 0, 1, 0, 0, 0, 1); + Basis flip_y_basis = Basis(1, 0, 0, 0, -1, 0, 0, 0, 1); + Basis flip_z_basis = Basis(1, 0, 0, 0, 1, 0, 0, 0, -1); + _VariantCall::add_variant_constant(Variant::BASIS, "IDENTITY", identity_basis); + _VariantCall::add_variant_constant(Variant::BASIS, "FLIP_X", flip_x_basis); + _VariantCall::add_variant_constant(Variant::BASIS, "FLIP_Y", flip_y_basis); + _VariantCall::add_variant_constant(Variant::BASIS, "FLIP_Z", flip_z_basis); _VariantCall::add_variant_constant(Variant::PLANE, "PLANE_YZ", Plane(Vector3(1, 0, 0), 0)); _VariantCall::add_variant_constant(Variant::PLANE, "PLANE_XZ", Plane(Vector3(0, 1, 0), 0)); diff --git a/core/variant_parser.cpp b/core/variant_parser.cpp index 07212ec669..fe2c981c3c 100644 --- a/core/variant_parser.cpp +++ b/core/variant_parser.cpp @@ -1522,7 +1522,7 @@ Error VariantParser::parse_tag_assign_eof(Stream *p_stream, int &line, String &r return err; if (tk.type != TK_STRING) { r_err_str = "Error reading quoted string"; - return err; + return ERR_INVALID_DATA; } what = tk.value; |