diff options
Diffstat (limited to 'core')
71 files changed, 827 insertions, 411 deletions
diff --git a/core/SCsub b/core/SCsub index 166b7083e4..6389cd176c 100644 --- a/core/SCsub +++ b/core/SCsub @@ -47,15 +47,11 @@ env_thirdparty.disable_warnings() thirdparty_misc_dir = "#thirdparty/misc/" thirdparty_misc_sources = [ # C sources - "base64.c", "fastlz.c", - "sha256.c", "smaz.c", # C++ sources - "aes256.cpp", "hq2x.cpp", - "md5.cpp", "pcg.cpp", "triangulator.cpp", "clipper.cpp", diff --git a/core/array.cpp b/core/array.cpp index 65934d6ec9..a334af2c04 100644 --- a/core/array.cpp +++ b/core/array.cpp @@ -133,12 +133,18 @@ void Array::erase(const Variant &p_value) { } Variant Array::front() const { - ERR_FAIL_COND_V(_p->array.size() == 0, Variant()); + if (_p->array.size() == 0) { + ERR_EXPLAIN("Can't take value from empty array"); + ERR_FAIL_V(Variant()); + } return operator[](0); } Variant Array::back() const { - ERR_FAIL_COND_V(_p->array.size() == 0, Variant()); + if (_p->array.size() == 0) { + ERR_EXPLAIN("Can't take value from empty array"); + ERR_FAIL_V(Variant()); + } return operator[](_p->array.size() - 1); } @@ -165,8 +171,8 @@ int Array::rfind(const Variant &p_value, int p_from) const { if (_p->array[i] == p_value) { return i; - }; - }; + } + } return -1; } @@ -186,8 +192,8 @@ int Array::count(const Variant &p_value) const { if (_p->array[i] == p_value) { amount++; - }; - }; + } + } return amount; } diff --git a/core/bind/core_bind.cpp b/core/bind/core_bind.cpp index 8a898f3b53..382ab31f6d 100644 --- a/core/bind/core_bind.cpp +++ b/core/bind/core_bind.cpp @@ -34,13 +34,12 @@ #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" #include "core/project_settings.h" -#include "thirdparty/misc/base64.h" - /** * Time constants borrowed from loc_time.h */ @@ -76,7 +75,7 @@ RES _ResourceLoader::load(const String &p_path, const String &p_type_hint, bool if (err != OK) { ERR_EXPLAIN("Error loading resource: '" + p_path + "'"); - ERR_FAIL_COND_V(err != OK, ret); + ERR_FAIL_V(ret); } return ret; } @@ -149,8 +148,10 @@ _ResourceLoader::_ResourceLoader() { } Error _ResourceSaver::save(const String &p_path, const RES &p_resource, SaverFlags p_flags) { - - ERR_FAIL_COND_V(p_resource.is_null(), ERR_INVALID_PARAMETER); + if (p_resource.is_null()) { + ERR_EXPLAIN("Can't save empty resource to path: " + String(p_path)) + ERR_FAIL_V(ERR_INVALID_PARAMETER); + } return ResourceSaver::save(p_path, p_resource, p_flags); } @@ -246,11 +247,11 @@ PoolStringArray _OS::get_connected_midi_inputs() { } void _OS::open_midi_inputs() { - return OS::get_singleton()->open_midi_inputs(); + OS::get_singleton()->open_midi_inputs(); } void _OS::close_midi_inputs() { - return OS::get_singleton()->close_midi_inputs(); + OS::get_singleton()->close_midi_inputs(); } void _OS::set_video_mode(const Size2 &p_size, bool p_fullscreen, bool p_resizeable, int p_screen) { @@ -1317,6 +1318,26 @@ void _OS::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "window_position"), "set_window_position", "get_window_position"); ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "window_size"), "set_window_size", "get_window_size"); + // Those default values need to be specified for the docs generator, + // to avoid using values from the documentation writer's own OS instance. + ADD_PROPERTY_DEFAULT("clipboard", ""); + ADD_PROPERTY_DEFAULT("current_screen", 0); + ADD_PROPERTY_DEFAULT("exit_code", 0); + ADD_PROPERTY_DEFAULT("vsync_enabled", true); + ADD_PROPERTY_DEFAULT("low_processor_usage_mode", false); + ADD_PROPERTY_DEFAULT("keep_screen_on", true); + ADD_PROPERTY_DEFAULT("min_window_size", Vector2()); + ADD_PROPERTY_DEFAULT("max_window_size", Vector2()); + ADD_PROPERTY_DEFAULT("screen_orientation", 0); + ADD_PROPERTY_DEFAULT("window_borderless", false); + ADD_PROPERTY_DEFAULT("window_per_pixel_transparency_enabled", false); + ADD_PROPERTY_DEFAULT("window_fullscreen", false); + ADD_PROPERTY_DEFAULT("window_maximized", false); + ADD_PROPERTY_DEFAULT("window_minimized", false); + ADD_PROPERTY_DEFAULT("window_resizable", true); + ADD_PROPERTY_DEFAULT("window_position", Vector2()); + ADD_PROPERTY_DEFAULT("window_size", Vector2()); + BIND_ENUM_CONSTANT(VIDEO_DRIVER_GLES2); BIND_ENUM_CONSTANT(VIDEO_DRIVER_GLES3); @@ -1524,6 +1545,11 @@ bool _Geometry::is_polygon_clockwise(const Vector<Vector2> &p_polygon) { return Geometry::is_polygon_clockwise(p_polygon); } +bool _Geometry::is_point_in_polygon(const Point2 &p_point, const Vector<Vector2> &p_polygon) { + + return Geometry::is_point_in_polygon(p_point, p_polygon); +} + Vector<int> _Geometry::triangulate_polygon(const Vector<Vector2> &p_polygon) { return Geometry::triangulate_polygon(p_polygon); @@ -1706,6 +1732,7 @@ void _Geometry::_bind_methods() { ClassDB::bind_method(D_METHOD("point_is_inside_triangle", "point", "a", "b", "c"), &_Geometry::point_is_inside_triangle); ClassDB::bind_method(D_METHOD("is_polygon_clockwise", "polygon"), &_Geometry::is_polygon_clockwise); + ClassDB::bind_method(D_METHOD("is_point_in_polygon", "point", "polygon"), &_Geometry::is_point_in_polygon); ClassDB::bind_method(D_METHOD("triangulate_polygon", "polygon"), &_Geometry::triangulate_polygon); ClassDB::bind_method(D_METHOD("triangulate_delaunay_2d", "points"), &_Geometry::triangulate_delaunay_2d); ClassDB::bind_method(D_METHOD("convex_hull_2d", "points"), &_Geometry::convex_hull_2d); @@ -2240,7 +2267,7 @@ bool _Directory::current_is_dir() const { void _Directory::list_dir_end() { ERR_FAIL_COND(!d); - return d->list_dir_end(); + d->list_dir_end(); } int _Directory::get_drive_count() { @@ -2410,7 +2437,8 @@ String _Marshalls::variant_to_base64(const Variant &p_var, bool p_full_objects) b64buff.resize(b64len); PoolVector<uint8_t>::Write w64 = b64buff.write(); - int strlen = base64_encode((char *)(&w64[0]), (char *)(&w[0]), len); + size_t strlen = 0; + ERR_FAIL_COND_V(CryptoCore::b64_encode(&w64[0], b64len, &strlen, &w[0], len) != OK, String()); //OS::get_singleton()->print("len is %i, vector size is %i\n", b64len, strlen); w64[strlen] = 0; String ret = (char *)&w64[0]; @@ -2427,7 +2455,8 @@ Variant _Marshalls::base64_to_variant(const String &p_str, bool p_allow_objects) buf.resize(strlen / 4 * 3 + 1); PoolVector<uint8_t>::Write w = buf.write(); - int len = base64_decode((char *)(&w[0]), (char *)cstr.get_data(), strlen); + size_t len = 0; + ERR_FAIL_COND_V(CryptoCore::b64_decode(&w[0], buf.size(), &len, (unsigned char *)cstr.get_data(), strlen) != OK, Variant()); Variant v; Error err = decode_variant(v, &w[0], len, NULL, p_allow_objects); @@ -2446,7 +2475,8 @@ String _Marshalls::raw_to_base64(const PoolVector<uint8_t> &p_arr) { b64buff.resize(b64len); PoolVector<uint8_t>::Write w64 = b64buff.write(); - int strlen = base64_encode((char *)(&w64[0]), (char *)(&r[0]), len); + size_t strlen = 0; + ERR_FAIL_COND_V(CryptoCore::b64_encode(&w64[0], b64len, &strlen, &r[0], len) != OK, String()); w64[strlen] = 0; String ret = (char *)&w64[0]; @@ -2458,17 +2488,16 @@ PoolVector<uint8_t> _Marshalls::base64_to_raw(const String &p_str) { int strlen = p_str.length(); CharString cstr = p_str.ascii(); - int arr_len; + size_t arr_len = 0; PoolVector<uint8_t> buf; { buf.resize(strlen / 4 * 3 + 1); PoolVector<uint8_t>::Write w = buf.write(); - arr_len = base64_decode((char *)(&w[0]), (char *)cstr.get_data(), strlen); - }; + ERR_FAIL_COND_V(CryptoCore::b64_decode(&w[0], buf.size(), &arr_len, (unsigned char *)cstr.get_data(), strlen) != OK, PoolVector<uint8_t>()); + } buf.resize(arr_len); - // conversion from PoolVector<uint8_t> to raw array? return buf; }; @@ -2482,7 +2511,8 @@ String _Marshalls::utf8_to_base64(const String &p_str) { b64buff.resize(b64len); PoolVector<uint8_t>::Write w64 = b64buff.write(); - int strlen = base64_encode((char *)(&w64[0]), (char *)cstr.get_data(), len); + size_t strlen = 0; + ERR_FAIL_COND_V(CryptoCore::b64_encode(&w64[0], b64len, &strlen, (unsigned char *)cstr.get_data(), len) != OK, String()); w64[strlen] = 0; String ret = (char *)&w64[0]; @@ -2499,7 +2529,8 @@ String _Marshalls::base64_to_utf8(const String &p_str) { buf.resize(strlen / 4 * 3 + 1 + 1); PoolVector<uint8_t>::Write w = buf.write(); - int len = base64_decode((char *)(&w[0]), (char *)cstr.get_data(), strlen); + size_t len = 0; + ERR_FAIL_COND_V(CryptoCore::b64_decode(&w[0], buf.size(), &len, (unsigned char *)cstr.get_data(), strlen) != OK, String()); w[len] = 0; String ret = String::utf8((char *)&w[0]); @@ -2674,6 +2705,8 @@ Variant _Thread::wait_to_finish() { target_method = StringName(); target_instance = NULL; userdata = Variant(); + if (thread) + memdelete(thread); thread = NULL; return r; diff --git a/core/bind/core_bind.h b/core/bind/core_bind.h index 7d0c158f51..3be5a08752 100644 --- a/core/bind/core_bind.h +++ b/core/bind/core_bind.h @@ -408,6 +408,7 @@ public: int get_uv84_normal_bit(const Vector3 &p_vector); bool is_polygon_clockwise(const Vector<Vector2> &p_polygon); + bool is_point_in_polygon(const Point2 &p_point, const Vector<Vector2> &p_polygon); Vector<int> triangulate_polygon(const Vector<Vector2> &p_polygon); Vector<int> triangulate_delaunay_2d(const Vector<Vector2> &p_points); Vector<Point2> convex_hull_2d(const Vector<Point2> &p_points); @@ -804,6 +805,9 @@ public: void set_result(const Variant &p_result); Variant get_result() const; + + JSONParseResult() : + error_line(-1) {} }; class _JSON : public Object { diff --git a/core/class_db.cpp b/core/class_db.cpp index ec07ee98e2..2cbf53ba0b 100644 --- a/core/class_db.cpp +++ b/core/class_db.cpp @@ -545,6 +545,11 @@ bool ClassDB::can_instance(const StringName &p_class) { ClassInfo *ti = classes.getptr(p_class); ERR_FAIL_COND_V(!ti, false); +#ifdef TOOLS_ENABLED + if (ti->api == API_EDITOR && !Engine::get_singleton()->is_editor_hint()) { + return false; + } +#endif return (!ti->disabled && ti->creation_func != NULL); } @@ -552,7 +557,7 @@ void ClassDB::_add_class2(const StringName &p_class, const StringName &p_inherit OBJTYPE_WLOCK; - StringName name = p_class; + const StringName &name = p_class; ERR_FAIL_COND(classes.has(name)); @@ -920,7 +925,7 @@ void ClassDB::add_property(StringName p_class, const PropertyInfo &p_pinfo, cons #ifdef DEBUG_METHODS_ENABLED if (!mb_set) { ERR_EXPLAIN("Invalid Setter: " + p_class + "::" + p_setter + " for property: " + p_pinfo.name); - ERR_FAIL_COND(!mb_set); + ERR_FAIL(); } else { int exp_args = 1 + (p_index >= 0 ? 1 : 0); if (mb_set->get_argument_count() != exp_args) { @@ -939,7 +944,7 @@ void ClassDB::add_property(StringName p_class, const PropertyInfo &p_pinfo, cons if (!mb_get) { ERR_EXPLAIN("Invalid Getter: " + p_class + "::" + p_getter + " for property: " + p_pinfo.name); - ERR_FAIL_COND(!mb_get); + ERR_FAIL(); } else { int exp_args = 0 + (p_index >= 0 ? 1 : 0); @@ -980,6 +985,13 @@ void ClassDB::add_property(StringName p_class, const PropertyInfo &p_pinfo, cons type->property_setget[p_pinfo.name] = psg; } +void ClassDB::set_property_default_value(StringName p_class, const StringName &p_name, const Variant &p_default) { + if (!default_values.has(p_class)) { + default_values[p_class] = HashMap<StringName, Variant>(); + } + default_values[p_class][p_name] = p_default; +} + void ClassDB::get_property_list(StringName p_class, List<PropertyInfo> *p_list, bool p_no_inheritance, const Object *p_validator) { OBJTYPE_RLOCK; @@ -1383,37 +1395,60 @@ void ClassDB::get_extensions_for_type(const StringName &p_class, List<String> *p } HashMap<StringName, HashMap<StringName, Variant> > ClassDB::default_values; +Set<StringName> ClassDB::default_values_cached; -Variant ClassDB::class_get_default_property_value(const StringName &p_class, const StringName &p_property) { +Variant ClassDB::class_get_default_property_value(const StringName &p_class, const StringName &p_property, bool *r_valid) { - if (!default_values.has(p_class)) { + if (!default_values_cached.has(p_class)) { - default_values[p_class] = HashMap<StringName, Variant>(); + if (!default_values.has(p_class)) { + default_values[p_class] = HashMap<StringName, Variant>(); + } + + Object *c = NULL; + bool cleanup_c = false; - if (ClassDB::can_instance(p_class)) { + if (Engine::get_singleton()->has_singleton(p_class)) { + c = Engine::get_singleton()->get_singleton_object(p_class); + cleanup_c = false; + } else if (ClassDB::can_instance(p_class)) { + c = ClassDB::instance(p_class); + cleanup_c = true; + } + + if (c) { - Object *c = ClassDB::instance(p_class); List<PropertyInfo> plist; c->get_property_list(&plist); for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) { if (E->get().usage & (PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_EDITOR)) { - Variant v = c->get(E->get().name); - default_values[p_class][E->get().name] = v; + if (!default_values[p_class].has(E->get().name)) { + Variant v = c->get(E->get().name); + default_values[p_class][E->get().name] = v; + } } } - memdelete(c); + + if (cleanup_c) { + memdelete(c); + } } + + default_values_cached.insert(p_class); } if (!default_values.has(p_class)) { + if (r_valid != NULL) *r_valid = false; return Variant(); } if (!default_values[p_class].has(p_property)) { + if (r_valid != NULL) *r_valid = false; return Variant(); } + if (r_valid != NULL) *r_valid = true; return default_values[p_class][p_property]; } @@ -1424,6 +1459,12 @@ void ClassDB::init() { lock = RWLock::create(); } +void ClassDB::cleanup_defaults() { + + default_values.clear(); + default_values_cached.clear(); +} + void ClassDB::cleanup() { //OBJTYPE_LOCK; hah not here @@ -1443,7 +1484,6 @@ void ClassDB::cleanup() { classes.clear(); resource_base_extensions.clear(); compat_classes.clear(); - default_values.clear(); memdelete(lock); } diff --git a/core/class_db.h b/core/class_db.h index efa1a46866..237ae9b806 100644 --- a/core/class_db.h +++ b/core/class_db.h @@ -162,6 +162,7 @@ public: static void _add_class2(const StringName &p_class, const StringName &p_inherits); static HashMap<StringName, HashMap<StringName, Variant> > default_values; + static Set<StringName> default_values_cached; public: // DO NOT USE THIS!!!!!! NEEDS TO BE PUBLIC BUT DO NOT USE NO MATTER WHAT!!! @@ -329,6 +330,7 @@ public: static void add_property_group(StringName p_class, const String &p_name, const String &p_prefix = ""); static void add_property(StringName p_class, const PropertyInfo &p_pinfo, const StringName &p_setter, const StringName &p_getter, int p_index = -1); + static void set_property_default_value(StringName p_class, const StringName &p_name, const Variant &p_default); static void get_property_list(StringName p_class, List<PropertyInfo> *p_list, bool p_no_inheritance = false, const Object *p_validator = NULL); static bool set_property(Object *p_object, const StringName &p_property, const Variant &p_value, bool *r_valid = NULL); static bool get_property(Object *p_object, const StringName &p_property, Variant &r_value); @@ -355,7 +357,7 @@ public: static void get_enum_list(const StringName &p_class, List<StringName> *p_enums, bool p_no_inheritance = false); static void get_enum_constants(const StringName &p_class, const StringName &p_enum, List<StringName> *p_constants, bool p_no_inheritance = false); - static Variant class_get_default_property_value(const StringName &p_class, const StringName &p_property); + static Variant class_get_default_property_value(const StringName &p_class, const StringName &p_property, bool *r_valid = NULL); static StringName get_category(const StringName &p_node); @@ -373,6 +375,7 @@ public: static void set_current_api(APIType p_api); static APIType get_current_api(); + static void cleanup_defaults(); static void cleanup(); }; diff --git a/core/color.cpp b/core/color.cpp index 8959fce4e3..1843532124 100644 --- a/core/color.cpp +++ b/core/color.cpp @@ -388,9 +388,8 @@ bool Color::html_is_valid(const String &p_color) { return false; } - int a = 255; if (alpha) { - a = _parse_col(color, 0); + int a = _parse_col(color, 0); if (a < 0) { return false; } diff --git a/core/color.h b/core/color.h index b2148e1357..77f95b5dc9 100644 --- a/core/color.h +++ b/core/color.h @@ -195,7 +195,7 @@ struct Color { static Color named(const String &p_name); String to_html(bool p_alpha = true) const; Color from_hsv(float p_h, float p_s, float p_v, float p_a) const; - static Color from_rgbe9995(uint32_t p_color); + static Color from_rgbe9995(uint32_t p_rgbe); _FORCE_INLINE_ bool operator<(const Color &p_color) const; //used in set keys operator String() const; diff --git a/core/command_queue_mt.h b/core/command_queue_mt.h index 798fa4394d..3789eda5db 100644 --- a/core/command_queue_mt.h +++ b/core/command_queue_mt.h @@ -346,7 +346,7 @@ class CommandQueueMT { } return NULL; } - } else if (write_ptr >= dealloc_ptr) { + } else { // ahead of dealloc_ptr, check that there is room if ((COMMAND_MEM_SIZE - write_ptr) < alloc_size + sizeof(uint32_t)) { diff --git a/core/engine.cpp b/core/engine.cpp index 50822244cf..2d8473fbd9 100644 --- a/core/engine.cpp +++ b/core/engine.cpp @@ -197,8 +197,10 @@ 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); - ERR_EXPLAIN("Failed to retrieve non-existent singleton '" + p_name + "'"); - ERR_FAIL_COND_V(!E, NULL); + if (!E) { + ERR_EXPLAIN("Failed to retrieve non-existent singleton '" + p_name + "'"); + ERR_FAIL_V(NULL); + } return E->get(); }; diff --git a/core/error_list.h b/core/error_list.h index 304861da4e..dc5a5e68dd 100644 --- a/core/error_list.h +++ b/core/error_list.h @@ -39,7 +39,7 @@ */ enum Error { - OK, + OK, // (0) FAILED, ///< Generic fail error ERR_UNAVAILABLE, ///< What is requested is unsupported/unavailable ERR_UNCONFIGURED, ///< The object being used hasn't been properly set up yet @@ -69,12 +69,12 @@ enum Error { ERR_CONNECTION_ERROR, ERR_CANT_ACQUIRE_RESOURCE, ERR_CANT_FORK, - ERR_INVALID_DATA, ///< Data passed is invalid (30) + ERR_INVALID_DATA, ///< Data passed is invalid (30) ERR_INVALID_PARAMETER, ///< Parameter passed is invalid ERR_ALREADY_EXISTS, ///< When adding, item already exists - ERR_DOES_NOT_EXIST, ///< When retrieving/erasing, it item does not exist + ERR_DOES_NOT_EXIST, ///< When retrieving/erasing, if item does not exist ERR_DATABASE_CANT_READ, ///< database is full - ERR_DATABASE_CANT_WRITE, ///< database is full (35) + ERR_DATABASE_CANT_WRITE, ///< database is full (35) ERR_COMPILATION_FAILED, ERR_METHOD_NOT_FOUND, ERR_LINK_FAILED, diff --git a/core/error_macros.h b/core/error_macros.h index f72e987e23..69874e280b 100644 --- a/core/error_macros.h +++ b/core/error_macros.h @@ -136,8 +136,8 @@ extern bool _err_error_exists; if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \ _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \ return; \ - } else \ - _err_error_exists = false; \ + } \ + _err_error_exists = false; \ } while (0); // (*) /** An index has failed if m_index<0 or m_index >=m_size, the function exits. @@ -150,8 +150,8 @@ extern bool _err_error_exists; if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \ _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \ return m_retval; \ - } else \ - _err_error_exists = false; \ + } \ + _err_error_exists = false; \ } while (0); // (*) /** An index has failed if m_index >=m_size, the function exits. @@ -164,8 +164,8 @@ extern bool _err_error_exists; if (unlikely((m_index) >= (m_size))) { \ _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \ return m_retval; \ - } else \ - _err_error_exists = false; \ + } \ + _err_error_exists = false; \ } while (0); // (*) /** Use this one if there is no sensible fallback, that is, the error is unrecoverable. @@ -188,8 +188,8 @@ extern bool _err_error_exists; if (unlikely(!m_param)) { \ _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter ' " _STR(m_param) " ' is null."); \ return; \ - } else \ - _err_error_exists = false; \ + } \ + _err_error_exists = false; \ } #define ERR_FAIL_NULL_V(m_param, m_retval) \ @@ -197,8 +197,8 @@ extern bool _err_error_exists; if (unlikely(!m_param)) { \ _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter ' " _STR(m_param) " ' is null."); \ return m_retval; \ - } else \ - _err_error_exists = false; \ + } \ + _err_error_exists = false; \ } /** An error condition happened (m_cond tested true) (WARNING this is the opposite as assert(). @@ -210,8 +210,8 @@ extern bool _err_error_exists; if (unlikely(m_cond)) { \ _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition ' " _STR(m_cond) " ' is true."); \ return; \ - } else \ - _err_error_exists = false; \ + } \ + _err_error_exists = false; \ } /** Use this one if there is no sensible fallback, that is, the error is unrecoverable. @@ -236,8 +236,8 @@ extern bool _err_error_exists; if (unlikely(m_cond)) { \ _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition ' " _STR(m_cond) " ' is true. returned: " _STR(m_retval)); \ return m_retval; \ - } else \ - _err_error_exists = false; \ + } \ + _err_error_exists = false; \ } /** An error condition happened (m_cond tested true) (WARNING this is the opposite as assert(). @@ -249,8 +249,8 @@ extern bool _err_error_exists; if (unlikely(m_cond)) { \ _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition ' " _STR(m_cond) " ' is true. Continuing..:"); \ continue; \ - } else \ - _err_error_exists = false; \ + } \ + _err_error_exists = false; \ } /** An error condition happened (m_cond tested true) (WARNING this is the opposite as assert(). @@ -262,8 +262,8 @@ extern bool _err_error_exists; if (unlikely(m_cond)) { \ _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition ' " _STR(m_cond) " ' is true. Breaking..:"); \ break; \ - } else \ - _err_error_exists = false; \ + } \ + _err_error_exists = false; \ } /** Print an error string and return diff --git a/core/global_constants.cpp b/core/global_constants.cpp index 671b3c545b..5bfdc8ab8f 100644 --- a/core/global_constants.cpp +++ b/core/global_constants.cpp @@ -486,47 +486,55 @@ void register_global_constants() { // error list - BIND_GLOBAL_ENUM_CONSTANT(OK); - BIND_GLOBAL_ENUM_CONSTANT(FAILED); ///< Generic fail error - BIND_GLOBAL_ENUM_CONSTANT(ERR_UNAVAILABLE); ///< What is requested is unsupported/unavailable - BIND_GLOBAL_ENUM_CONSTANT(ERR_UNCONFIGURED); ///< The object being used hasn't been properly set up yet - BIND_GLOBAL_ENUM_CONSTANT(ERR_UNAUTHORIZED); ///< Missing credentials for requested resource - BIND_GLOBAL_ENUM_CONSTANT(ERR_PARAMETER_RANGE_ERROR); ///< Parameter given out of range - BIND_GLOBAL_ENUM_CONSTANT(ERR_OUT_OF_MEMORY); ///< Out of memory + BIND_GLOBAL_ENUM_CONSTANT(OK); // (0) + BIND_GLOBAL_ENUM_CONSTANT(FAILED); + BIND_GLOBAL_ENUM_CONSTANT(ERR_UNAVAILABLE); + BIND_GLOBAL_ENUM_CONSTANT(ERR_UNCONFIGURED); + BIND_GLOBAL_ENUM_CONSTANT(ERR_UNAUTHORIZED); + BIND_GLOBAL_ENUM_CONSTANT(ERR_PARAMETER_RANGE_ERROR); // (5) + BIND_GLOBAL_ENUM_CONSTANT(ERR_OUT_OF_MEMORY); BIND_GLOBAL_ENUM_CONSTANT(ERR_FILE_NOT_FOUND); BIND_GLOBAL_ENUM_CONSTANT(ERR_FILE_BAD_DRIVE); BIND_GLOBAL_ENUM_CONSTANT(ERR_FILE_BAD_PATH); - BIND_GLOBAL_ENUM_CONSTANT(ERR_FILE_NO_PERMISSION); + BIND_GLOBAL_ENUM_CONSTANT(ERR_FILE_NO_PERMISSION); // (10) BIND_GLOBAL_ENUM_CONSTANT(ERR_FILE_ALREADY_IN_USE); BIND_GLOBAL_ENUM_CONSTANT(ERR_FILE_CANT_OPEN); BIND_GLOBAL_ENUM_CONSTANT(ERR_FILE_CANT_WRITE); BIND_GLOBAL_ENUM_CONSTANT(ERR_FILE_CANT_READ); - BIND_GLOBAL_ENUM_CONSTANT(ERR_FILE_UNRECOGNIZED); + BIND_GLOBAL_ENUM_CONSTANT(ERR_FILE_UNRECOGNIZED); // (15) BIND_GLOBAL_ENUM_CONSTANT(ERR_FILE_CORRUPT); BIND_GLOBAL_ENUM_CONSTANT(ERR_FILE_MISSING_DEPENDENCIES); BIND_GLOBAL_ENUM_CONSTANT(ERR_FILE_EOF); - BIND_GLOBAL_ENUM_CONSTANT(ERR_CANT_OPEN); ///< Can't open a resource/socket/file - BIND_GLOBAL_ENUM_CONSTANT(ERR_CANT_CREATE); - BIND_GLOBAL_ENUM_CONSTANT(ERR_PARSE_ERROR); + BIND_GLOBAL_ENUM_CONSTANT(ERR_CANT_OPEN); + BIND_GLOBAL_ENUM_CONSTANT(ERR_CANT_CREATE); // (20) BIND_GLOBAL_ENUM_CONSTANT(ERR_QUERY_FAILED); BIND_GLOBAL_ENUM_CONSTANT(ERR_ALREADY_IN_USE); - BIND_GLOBAL_ENUM_CONSTANT(ERR_LOCKED); ///< resource is locked + BIND_GLOBAL_ENUM_CONSTANT(ERR_LOCKED); BIND_GLOBAL_ENUM_CONSTANT(ERR_TIMEOUT); + BIND_GLOBAL_ENUM_CONSTANT(ERR_CANT_CONNECT); // (25) + BIND_GLOBAL_ENUM_CONSTANT(ERR_CANT_RESOLVE); + BIND_GLOBAL_ENUM_CONSTANT(ERR_CONNECTION_ERROR); BIND_GLOBAL_ENUM_CONSTANT(ERR_CANT_ACQUIRE_RESOURCE); - BIND_GLOBAL_ENUM_CONSTANT(ERR_INVALID_DATA); ///< Data passed is invalid - BIND_GLOBAL_ENUM_CONSTANT(ERR_INVALID_PARAMETER); ///< Parameter passed is invalid - BIND_GLOBAL_ENUM_CONSTANT(ERR_ALREADY_EXISTS); ///< When adding ), item already exists - BIND_GLOBAL_ENUM_CONSTANT(ERR_DOES_NOT_EXIST); ///< When retrieving/erasing ), it item does not exist - BIND_GLOBAL_ENUM_CONSTANT(ERR_DATABASE_CANT_READ); ///< database is full - BIND_GLOBAL_ENUM_CONSTANT(ERR_DATABASE_CANT_WRITE); ///< database is full + BIND_GLOBAL_ENUM_CONSTANT(ERR_CANT_FORK); + BIND_GLOBAL_ENUM_CONSTANT(ERR_INVALID_DATA); // (30) + BIND_GLOBAL_ENUM_CONSTANT(ERR_INVALID_PARAMETER); + BIND_GLOBAL_ENUM_CONSTANT(ERR_ALREADY_EXISTS); + BIND_GLOBAL_ENUM_CONSTANT(ERR_DOES_NOT_EXIST); + BIND_GLOBAL_ENUM_CONSTANT(ERR_DATABASE_CANT_READ); + BIND_GLOBAL_ENUM_CONSTANT(ERR_DATABASE_CANT_WRITE); // (35) BIND_GLOBAL_ENUM_CONSTANT(ERR_COMPILATION_FAILED); BIND_GLOBAL_ENUM_CONSTANT(ERR_METHOD_NOT_FOUND); BIND_GLOBAL_ENUM_CONSTANT(ERR_LINK_FAILED); BIND_GLOBAL_ENUM_CONSTANT(ERR_SCRIPT_FAILED); - BIND_GLOBAL_ENUM_CONSTANT(ERR_CYCLIC_LINK); + BIND_GLOBAL_ENUM_CONSTANT(ERR_CYCLIC_LINK); // (40) + BIND_GLOBAL_ENUM_CONSTANT(ERR_INVALID_DECLARATION); + BIND_GLOBAL_ENUM_CONSTANT(ERR_DUPLICATE_SYMBOL); + BIND_GLOBAL_ENUM_CONSTANT(ERR_PARSE_ERROR); BIND_GLOBAL_ENUM_CONSTANT(ERR_BUSY); - BIND_GLOBAL_ENUM_CONSTANT(ERR_HELP); ///< user requested help!! - BIND_GLOBAL_ENUM_CONSTANT(ERR_BUG); ///< a bug in the software certainly happened ), due to a double check failing or unexpected behavior. + BIND_GLOBAL_ENUM_CONSTANT(ERR_SKIP); // (45) + BIND_GLOBAL_ENUM_CONSTANT(ERR_HELP); + BIND_GLOBAL_ENUM_CONSTANT(ERR_BUG); + BIND_GLOBAL_ENUM_CONSTANT(ERR_PRINTER_ON_FIRE); BIND_GLOBAL_ENUM_CONSTANT(PROPERTY_HINT_NONE); BIND_GLOBAL_ENUM_CONSTANT(PROPERTY_HINT_RANGE); diff --git a/core/hash_map.h b/core/hash_map.h index 31332991de..1513d7a65b 100644 --- a/core/hash_map.h +++ b/core/hash_map.h @@ -208,7 +208,10 @@ private: /* if element doesn't exist, create it */ Element *e = memnew(Element); - ERR_FAIL_COND_V(!e, NULL); /* out of memory */ + if (!e) { + ERR_EXPLAIN("Out of memory"); + ERR_FAIL_V(NULL); + } uint32_t hash = Hasher::hash(p_key); uint32_t index = hash & ((1 << hash_table_power) - 1); e->next = hash_table[index]; @@ -495,8 +498,10 @@ public: } else { /* get the next key */ const Element *e = get_element(*p_key); - ERR_FAIL_COND_V(!e, NULL); /* invalid key supplied */ - + if (!e) { + ERR_EXPLAIN("Invalid key supplied") + ERR_FAIL_V(NULL); + } 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 c85d7f6bcc..18a3aae88f 100644 --- a/core/image.cpp +++ b/core/image.cpp @@ -1372,6 +1372,7 @@ void Image::shrink_x2() { int new_size = data.size() - ofs; new_img.resize(new_size); + ERR_FAIL_COND(new_img.size() == 0); { PoolVector<uint8_t>::Write w = new_img.write(); @@ -1391,6 +1392,7 @@ void Image::shrink_x2() { ERR_FAIL_COND(!_can_modify(format)); int ps = get_format_pixel_size(format); new_img.resize((width / 2) * (height / 2) * ps); + ERR_FAIL_COND(new_img.size() == 0); { PoolVector<uint8_t>::Write w = new_img.write(); @@ -1464,7 +1466,10 @@ Error Image::generate_mipmaps(bool p_renormalize) { ERR_FAIL_V(ERR_UNAVAILABLE); } - ERR_FAIL_COND_V(width == 0 || height == 0, ERR_UNCONFIGURED); + if (width == 0 || height == 0) { + ERR_EXPLAIN("Cannot generate mipmaps with width or height equal to 0."); + ERR_FAIL_V(ERR_UNCONFIGURED); + } int mmcount; @@ -2402,7 +2407,7 @@ Color Image::get_pixel(int p_x, int p_y) const { #ifdef DEBUG_ENABLED if (!ptr) { ERR_EXPLAIN("Image must be locked with 'lock()' before using get_pixel()"); - ERR_FAIL_COND_V(!ptr, Color()); + ERR_FAIL_V(Color()); } ERR_FAIL_INDEX_V(p_x, width, Color()); @@ -2532,7 +2537,7 @@ Color Image::get_pixel(int p_x, int p_y) const { } void Image::set_pixelv(const Point2 &p_dst, const Color &p_color) { - return set_pixel(p_dst.x, p_dst.y, p_color); + set_pixel(p_dst.x, p_dst.y, p_color); } void Image::set_pixel(int p_x, int p_y, const Color &p_color) { @@ -2541,7 +2546,7 @@ void Image::set_pixel(int p_x, int p_y, const Color &p_color) { #ifdef DEBUG_ENABLED if (!ptr) { ERR_EXPLAIN("Image must be locked with 'lock()' before using set_pixel()"); - ERR_FAIL_COND(!ptr); + ERR_FAIL(); } ERR_FAIL_INDEX(p_x, width); diff --git a/core/image.h b/core/image.h index 752ef20208..cc796789cd 100644 --- a/core/image.h +++ b/core/image.h @@ -350,7 +350,7 @@ public: Color get_pixelv(const Point2 &p_src) const; Color get_pixel(int p_x, int p_y) const; - void set_pixelv(const Point2 &p_dest, const Color &p_color); + void set_pixelv(const Point2 &p_dst, const Color &p_color); void set_pixel(int p_x, int p_y, const Color &p_color); void copy_internals_from(const Ref<Image> &p_image) { diff --git a/core/input_map.cpp b/core/input_map.cpp index 012c6a7c4f..04911787a8 100644 --- a/core/input_map.cpp +++ b/core/input_map.cpp @@ -194,7 +194,7 @@ bool InputMap::event_get_action_status(const Ref<InputEvent> &p_event, const Str Map<StringName, Action>::Element *E = input_map.find(p_action); if (!E) { ERR_EXPLAIN("Request for nonexistent InputMap action: " + String(p_action)); - ERR_FAIL_COND_V(!E, false); + ERR_FAIL_V(false); } Ref<InputEventAction> input_event_action = p_event; diff --git a/core/io/config_file.cpp b/core/io/config_file.cpp index 871e21df3e..f7fb72c089 100644 --- a/core/io/config_file.cpp +++ b/core/io/config_file.cpp @@ -30,7 +30,7 @@ #include "config_file.h" -#include "core/os/file_access.h" +#include "core/io/file_access_encrypted.h" #include "core/os/keyboard.h" #include "core/variant_parser.h" @@ -137,6 +137,48 @@ Error ConfigFile::save(const String &p_path) { return err; } + return _internal_save(file); +} + +Error ConfigFile::save_encrypted(const String &p_path, const Vector<uint8_t> &p_key) { + + Error err; + FileAccess *f = FileAccess::open(p_path, FileAccess::WRITE, &err); + + if (err) + return err; + + FileAccessEncrypted *fae = memnew(FileAccessEncrypted); + err = fae->open_and_parse(f, p_key, FileAccessEncrypted::MODE_WRITE_AES256); + if (err) { + memdelete(fae); + memdelete(f); + return err; + } + return _internal_save(fae); +} + +Error ConfigFile::save_encrypted_pass(const String &p_path, const String &p_pass) { + + Error err; + FileAccess *f = FileAccess::open(p_path, FileAccess::WRITE, &err); + + if (err) + return err; + + FileAccessEncrypted *fae = memnew(FileAccessEncrypted); + err = fae->open_and_parse_password(f, p_pass, FileAccessEncrypted::MODE_WRITE_AES256); + if (err) { + memdelete(fae); + memdelete(f); + return err; + } + + return _internal_save(fae); +} + +Error ConfigFile::_internal_save(FileAccess *file) { + for (OrderedHashMap<String, OrderedHashMap<String, Variant> >::Element E = values.front(); E; E = E.next()) { if (E != values.front()) @@ -164,6 +206,48 @@ Error ConfigFile::load(const String &p_path) { if (!f) return ERR_CANT_OPEN; + return _internal_load(p_path, f); +} + +Error ConfigFile::load_encrypted(const String &p_path, const Vector<uint8_t> &p_key) { + + Error err; + FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err); + + if (err) + return err; + + FileAccessEncrypted *fae = memnew(FileAccessEncrypted); + err = fae->open_and_parse(f, p_key, FileAccessEncrypted::MODE_READ); + if (err) { + memdelete(fae); + memdelete(f); + return err; + } + return _internal_load(p_path, fae); +} + +Error ConfigFile::load_encrypted_pass(const String &p_path, const String &p_pass) { + + Error err; + FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err); + + if (err) + return err; + + FileAccessEncrypted *fae = memnew(FileAccessEncrypted); + err = fae->open_and_parse_password(f, p_pass, FileAccessEncrypted::MODE_READ); + if (err) { + memdelete(fae); + memdelete(f); + return err; + } + + return _internal_load(p_path, fae); +} + +Error ConfigFile::_internal_load(const String &p_path, FileAccess *f) { + VariantParser::StreamFile stream; stream.f = f; @@ -182,7 +266,7 @@ Error ConfigFile::load(const String &p_path) { next_tag.fields.clear(); next_tag.name = String(); - err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, NULL, true); + Error err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, NULL, true); if (err == ERR_FILE_EOF) { memdelete(f); return OK; @@ -215,6 +299,12 @@ void ConfigFile::_bind_methods() { ClassDB::bind_method(D_METHOD("load", "path"), &ConfigFile::load); ClassDB::bind_method(D_METHOD("save", "path"), &ConfigFile::save); + + ClassDB::bind_method(D_METHOD("load_encrypted", "path", "key"), &ConfigFile::load_encrypted); + ClassDB::bind_method(D_METHOD("load_encrypted_pass", "path", "pass"), &ConfigFile::load_encrypted_pass); + + ClassDB::bind_method(D_METHOD("save_encrypted", "path", "key"), &ConfigFile::save_encrypted); + ClassDB::bind_method(D_METHOD("save_encrypted_pass", "path", "pass"), &ConfigFile::save_encrypted_pass); } ConfigFile::ConfigFile() { diff --git a/core/io/config_file.h b/core/io/config_file.h index 36e5c0ca7d..3ab6fef868 100644 --- a/core/io/config_file.h +++ b/core/io/config_file.h @@ -32,6 +32,7 @@ #define CONFIG_FILE_H #include "core/ordered_hash_map.h" +#include "core/os/file_access.h" #include "core/reference.h" class ConfigFile : public Reference { @@ -42,6 +43,8 @@ class ConfigFile : public Reference { PoolStringArray _get_sections() const; PoolStringArray _get_section_keys(const String &p_section) const; + Error _internal_load(const String &p_path, FileAccess *f); + Error _internal_save(FileAccess *file); protected: static void _bind_methods(); @@ -61,6 +64,12 @@ public: Error save(const String &p_path); Error load(const String &p_path); + Error load_encrypted(const String &p_path, const Vector<uint8_t> &p_key); + Error load_encrypted_pass(const String &p_path, const String &p_pass); + + Error save_encrypted(const String &p_path, const Vector<uint8_t> &p_key); + Error save_encrypted_pass(const String &p_path, const String &p_pass); + ConfigFile(); }; diff --git a/core/io/file_access_buffered.cpp b/core/io/file_access_buffered.cpp index 93eaeb08c5..15523a49a9 100644 --- a/core/io/file_access_buffered.cpp +++ b/core/io/file_access_buffered.cpp @@ -35,79 +35,79 @@ Error FileAccessBuffered::set_error(Error p_error) const { return (last_error = p_error); -}; +} void FileAccessBuffered::set_cache_size(int p_size) { cache_size = p_size; -}; +} int FileAccessBuffered::get_cache_size() { return cache_size; -}; +} int FileAccessBuffered::cache_data_left() const { if (file.offset >= file.size) { return 0; - }; + } if (cache.offset == -1 || file.offset < cache.offset || file.offset >= cache.offset + cache.buffer.size()) { return read_data_block(file.offset, cache_size); + } - } else { - - return cache.buffer.size() - (file.offset - cache.offset); - }; - - return 0; -}; + return cache.buffer.size() - (file.offset - cache.offset); +} void FileAccessBuffered::seek(size_t p_position) { file.offset = p_position; -}; +} void FileAccessBuffered::seek_end(int64_t p_position) { file.offset = file.size + p_position; -}; +} size_t FileAccessBuffered::get_position() const { return file.offset; -}; +} size_t FileAccessBuffered::get_len() const { return file.size; -}; +} bool FileAccessBuffered::eof_reached() const { return file.offset > file.size; -}; +} uint8_t FileAccessBuffered::get_8() const { - - ERR_FAIL_COND_V(!file.open, 0); + if (!file.open) { + ERR_EXPLAIN("Can't get data, when file is not opened."); + ERR_FAIL_V(0); + } uint8_t byte = 0; if (cache_data_left() >= 1) { byte = cache.buffer[file.offset - cache.offset]; - }; + } ++file.offset; return byte; -}; +} int FileAccessBuffered::get_buffer(uint8_t *p_dest, int p_length) const { - - ERR_FAIL_COND_V(!file.open, -1); + if (!file.open) { + ERR_EXPLAIN("Can't get buffer, when file is not opened."); + ERR_FAIL_V(-1); + } if (p_length > cache_size) { @@ -124,16 +124,16 @@ int FileAccessBuffered::get_buffer(uint8_t *p_dest, int p_length) const { p_length -= size; file.offset += size; total_read += size; - }; + } int err = read_data_block(file.offset, p_length, p_dest); if (err >= 0) { total_read += err; file.offset += err; - }; + } return total_read; - }; + } int to_read = p_length; int total_read = 0; @@ -143,10 +143,10 @@ int FileAccessBuffered::get_buffer(uint8_t *p_dest, int p_length) const { if (left == 0) { file.offset += to_read; return total_read; - }; + } if (left < 0) { return left; - }; + } int r = MIN(left, to_read); //PoolVector<uint8_t>::Read read = cache.buffer.read(); @@ -156,25 +156,25 @@ int FileAccessBuffered::get_buffer(uint8_t *p_dest, int p_length) const { file.offset += r; total_read += r; to_read -= r; - }; + } return p_length; -}; +} bool FileAccessBuffered::is_open() const { return file.open; -}; +} Error FileAccessBuffered::get_error() const { return last_error; -}; +} FileAccessBuffered::FileAccessBuffered() { cache_size = DEFAULT_CACHE_SIZE; -}; +} FileAccessBuffered::~FileAccessBuffered() { } diff --git a/core/io/file_access_buffered_fa.h b/core/io/file_access_buffered_fa.h index 24b40cbce8..6e806e7b3f 100644 --- a/core/io/file_access_buffered_fa.h +++ b/core/io/file_access_buffered_fa.h @@ -40,7 +40,10 @@ class FileAccessBufferedFA : public FileAccessBuffered { int read_data_block(int p_offset, int p_size, uint8_t *p_dest = 0) const { - ERR_FAIL_COND_V(!f.is_open(), -1); + if (!f.is_open()) { + ERR_EXPLAIN("Can't read data block, when file is not opened."); + ERR_FAIL_V(-1); + } ((T *)&f)->seek(p_offset); diff --git a/core/io/file_access_encrypted.cpp b/core/io/file_access_encrypted.cpp index 7dea749a43..ccee6aeb15 100644 --- a/core/io/file_access_encrypted.cpp +++ b/core/io/file_access_encrypted.cpp @@ -30,13 +30,11 @@ #include "file_access_encrypted.h" +#include "core/math/crypto_core.h" #include "core/os/copymem.h" #include "core/print_string.h" #include "core/variant.h" -#include "thirdparty/misc/aes256.h" -#include "thirdparty/misc/md5.h" - #include <stdio.h> #define COMP_MAGIC 0x43454447 @@ -83,25 +81,21 @@ Error FileAccessEncrypted::open_and_parse(FileAccess *p_base, const Vector<uint8 uint32_t blen = p_base->get_buffer(data.ptrw(), ds); ERR_FAIL_COND_V(blen != ds, ERR_FILE_CORRUPT); - aes256_context ctx; - aes256_init(&ctx, key.ptrw()); + CryptoCore::AESContext ctx; + ctx.set_decode_key(key.ptrw(), 256); for (size_t i = 0; i < ds; i += 16) { - aes256_decrypt_ecb(&ctx, &data.write[i]); + ctx.decrypt_ecb(&data.write[i], &data.write[i]); } - aes256_done(&ctx); - data.resize(length); - MD5_CTX md5; - MD5Init(&md5); - MD5Update(&md5, (uint8_t *)data.ptr(), data.size()); - MD5Final(&md5); + 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(md5.digest) != String::md5(md5d), ERR_FILE_CORRUPT); + ERR_FAIL_COND_V(String::md5(hash) != String::md5(md5d), ERR_FILE_CORRUPT); file = p_base; } @@ -140,10 +134,8 @@ void FileAccessEncrypted::close() { len += 16 - (len % 16); } - MD5_CTX md5; - MD5Init(&md5); - MD5Update(&md5, (uint8_t *)data.ptr(), data.size()); - MD5Final(&md5); + unsigned char hash[16]; + ERR_FAIL_COND(CryptoCore::md5(data.ptr(), data.size(), hash) != OK); // Bug? compressed.resize(len); zeromem(compressed.ptrw(), len); @@ -151,20 +143,18 @@ void FileAccessEncrypted::close() { compressed.write[i] = data[i]; } - aes256_context ctx; - aes256_init(&ctx, key.ptrw()); + CryptoCore::AESContext ctx; + ctx.set_encode_key(key.ptrw(), 256); for (size_t i = 0; i < len; i += 16) { - aes256_encrypt_ecb(&ctx, &compressed.write[i]); + ctx.encrypt_ecb(&compressed.write[i], &compressed.write[i]); } - aes256_done(&ctx); - file->store_32(COMP_MAGIC); file->store_32(mode); - file->store_buffer(md5.digest, 16); + file->store_buffer(hash, 16); file->store_64(data.size()); file->store_buffer(compressed.ptr(), compressed.size()); diff --git a/core/io/file_access_network.cpp b/core/io/file_access_network.cpp index 5dd167c581..d1c7f5c334 100644 --- a/core/io/file_access_network.cpp +++ b/core/io/file_access_network.cpp @@ -435,7 +435,6 @@ int FileAccessNetwork::get_buffer(uint8_t *p_dst, int p_length) const { _queue_page(page + j); } - buff = pages.write[page].buffer.ptrw(); //queue pages buffer_mutex->unlock(); } diff --git a/core/io/http_client.cpp b/core/io/http_client.cpp index 891fb7b0ca..170bef4430 100644 --- a/core/io/http_client.cpp +++ b/core/io/http_client.cpp @@ -482,8 +482,6 @@ Error HTTPClient::poll() { return OK; } } - // Wait for response - return OK; } break; case STATUS_DISCONNECTED: { return ERR_UNCONFIGURED; @@ -775,7 +773,7 @@ Dictionary HTTPClient::_get_response_headers_as_dictionary() { get_response_headers(&rh); Dictionary ret; for (const List<String>::Element *E = rh.front(); E; E = E->next()) { - String s = E->get(); + const String &s = E->get(); int sp = s.find(":"); if (sp == -1) continue; diff --git a/core/io/json.cpp b/core/io/json.cpp index c211ca2ed4..4e729cb355 100644 --- a/core/io/json.cpp +++ b/core/io/json.cpp @@ -347,8 +347,6 @@ Error JSON::_parse_value(Variant &value, Token &token, const CharType *p_str, in r_err_str = "Expected value, got " + String(tk_name[token.type]) + "."; return ERR_PARSE_ERROR; } - - return ERR_PARSE_ERROR; } Error JSON::_parse_array(Array &array, const CharType *p_str, int &index, int p_len, int &line, String &r_err_str) { diff --git a/core/io/marshalls.cpp b/core/io/marshalls.cpp index 7494603462..17a3f52a65 100644 --- a/core/io/marshalls.cpp +++ b/core/io/marshalls.cpp @@ -37,13 +37,11 @@ #include <limits.h> #include <stdio.h> -#define _S(a) ((int32_t)a) -#define ERR_FAIL_ADD_OF(a, b, err) ERR_FAIL_COND_V(_S(b) < 0 || _S(a) < 0 || _S(a) > INT_MAX - _S(b), err) -#define ERR_FAIL_MUL_OF(a, b, err) ERR_FAIL_COND_V(_S(a) < 0 || _S(b) <= 0 || _S(a) > INT_MAX / _S(b), err) - void EncodedObjectAsID::_bind_methods() { ClassDB::bind_method(D_METHOD("set_object_id", "id"), &EncodedObjectAsID::set_object_id); ClassDB::bind_method(D_METHOD("get_object_id"), &EncodedObjectAsID::get_object_id); + + ADD_PROPERTY(PropertyInfo(Variant::INT, "object_id"), "set_object_id", "get_object_id"); } void EncodedObjectAsID::set_object_id(ObjectID p_id) { @@ -59,6 +57,10 @@ EncodedObjectAsID::EncodedObjectAsID() : id(0) { } +#define _S(a) ((int32_t)a) +#define ERR_FAIL_ADD_OF(a, b, err) ERR_FAIL_COND_V(_S(b) < 0 || _S(a) < 0 || _S(a) > INT_MAX - _S(b), err) +#define ERR_FAIL_MUL_OF(a, b, err) ERR_FAIL_COND_V(_S(a) < 0 || _S(b) <= 0 || _S(a) > INT_MAX / _S(b), err) + #define ENCODE_MASK 0xFF #define ENCODE_FLAG_64 1 << 16 #define ENCODE_FLAG_OBJECT_AS_ID 1 << 16 @@ -681,8 +683,6 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int if (r_len) (*r_len) += adv; - len -= adv; - buf += adv; } r_variant = varray; @@ -719,8 +719,6 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int if (r_len) (*r_len) += adv; - len -= adv; - buf += adv; } r_variant = varray; @@ -758,8 +756,6 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int if (r_len) (*r_len) += adv; - len -= adv; - buf += adv; } r_variant = carray; @@ -1092,7 +1088,6 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo if (!obj) { if (buf) { encode_uint32(0, buf); - buf += 4; } r_len += 4; diff --git a/core/io/multiplayer_api.cpp b/core/io/multiplayer_api.cpp index 2779837190..33dc4dbde4 100644 --- a/core/io/multiplayer_api.cpp +++ b/core/io/multiplayer_api.cpp @@ -874,6 +874,7 @@ void MultiplayerAPI::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_object_decoding"), "set_allow_object_decoding", "is_object_decoding_allowed"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "refuse_new_network_connections"), "set_refuse_new_network_connections", "is_refusing_new_network_connections"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "network_peer", PROPERTY_HINT_RESOURCE_TYPE, "NetworkedMultiplayerPeer", 0), "set_network_peer", "get_network_peer"); + ADD_PROPERTY_DEFAULT("refuse_new_network_connections", false); ADD_SIGNAL(MethodInfo("network_peer_connected", PropertyInfo(Variant::INT, "id"))); ADD_SIGNAL(MethodInfo("network_peer_disconnected", PropertyInfo(Variant::INT, "id"))); diff --git a/core/io/multiplayer_api.h b/core/io/multiplayer_api.h index 779dd043bd..5258dde5d7 100644 --- a/core/io/multiplayer_api.h +++ b/core/io/multiplayer_api.h @@ -77,7 +77,7 @@ protected: void _process_raw(int p_from, const uint8_t *p_packet, int p_packet_len); void _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); - bool _send_confirm_path(NodePath p_path, PathSentCache *psc, int p_from); + bool _send_confirm_path(NodePath p_path, PathSentCache *psc, int p_target); public: enum NetworkCommands { diff --git a/core/io/pck_packer.cpp b/core/io/pck_packer.cpp index 8920bbfb81..c16d89d695 100644 --- a/core/io/pck_packer.cpp +++ b/core/io/pck_packer.cpp @@ -63,10 +63,11 @@ void PCKPacker::_bind_methods() { Error PCKPacker::pck_start(const String &p_file, int p_alignment) { file = FileAccess::open(p_file, FileAccess::WRITE); - if (file == NULL) { - return ERR_CANT_CREATE; - }; + if (!file) { + ERR_EXPLAIN("Can't open file to write: " + String(p_file)); + ERR_FAIL_V(ERR_CANT_CREATE); + } alignment = p_alignment; @@ -109,10 +110,7 @@ Error PCKPacker::add_file(const String &p_file, const String &p_src) { Error PCKPacker::flush(bool p_verbose) { - if (!file) { - ERR_FAIL_COND_V(!file, ERR_INVALID_PARAMETER); - return ERR_INVALID_PARAMETER; - }; + ERR_FAIL_COND_V(!file, ERR_INVALID_PARAMETER); // write the index diff --git a/core/io/resource_format_binary.cpp b/core/io/resource_format_binary.cpp index aef2dcfff3..688dfc21e5 100644 --- a/core/io/resource_format_binary.cpp +++ b/core/io/resource_format_binary.cpp @@ -720,7 +720,7 @@ Error ResourceInteractiveLoaderBinary::poll() { error = ERR_FILE_CORRUPT; memdelete(obj); //bye ERR_EXPLAIN(local_path + ":Resource type in resource field not a resource, type is: " + obj->get_class()); - ERR_FAIL_COND_V(!r, ERR_FILE_CORRUPT); + ERR_FAIL_V(ERR_FILE_CORRUPT); } RES res = RES(r); @@ -1694,7 +1694,7 @@ void ResourceFormatSaverBinaryInstance::_find_resources(const Variant &p_variant int len = varray.size(); for (int i = 0; i < len; i++) { - Variant v = varray.get(i); + const Variant &v = varray.get(i); _find_resources(v); } diff --git a/core/io/resource_importer.cpp b/core/io/resource_importer.cpp index 4a58d37ca5..63d7ba547c 100644 --- a/core/io/resource_importer.cpp +++ b/core/io/resource_importer.cpp @@ -161,7 +161,8 @@ void ResourceFormatImporter::get_recognized_extensions(List<String> *p_extension void ResourceFormatImporter::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const { if (p_type == "") { - return get_recognized_extensions(p_extensions); + get_recognized_extensions(p_extensions); + return; } Set<String> found; @@ -347,7 +348,7 @@ void ResourceFormatImporter::get_dependencies(const String &p_path, List<String> return; } - return ResourceLoader::get_dependencies(pat.path, p_dependencies, p_add_types); + ResourceLoader::get_dependencies(pat.path, p_dependencies, p_add_types); } Ref<ResourceImporter> ResourceFormatImporter::get_importer_by_name(const String &p_name) const { diff --git a/core/io/resource_loader.cpp b/core/io/resource_loader.cpp index 56d3b8b133..a29b9d1ddb 100644 --- a/core/io/resource_loader.cpp +++ b/core/io/resource_loader.cpp @@ -207,8 +207,6 @@ RES ResourceFormatLoader::load(const String &p_path, const String &p_original_pa ERR_FAIL_COND_V(err != OK, RES()); } - - return RES(); } void ResourceFormatLoader::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) { @@ -283,7 +281,6 @@ RES ResourceLoader::_load(const String &p_path, const String &p_original_path, c ERR_EXPLAIN("No loader found for resource: " + p_path); } ERR_FAIL_V(RES()); - return RES(); } bool ResourceLoader::_add_to_loading_map(const String &p_path) { @@ -543,7 +540,6 @@ Ref<ResourceInteractiveLoader> ResourceLoader::load_interactive(const String &p_ ERR_EXPLAIN("No loader found for resource: " + path); } ERR_FAIL_V(Ref<ResourceInteractiveLoader>()); - return Ref<ResourceInteractiveLoader>(); } void ResourceLoader::add_resource_format_loader(Ref<ResourceFormatLoader> p_format_loader, bool p_at_front) { diff --git a/core/io/stream_peer_ssl.cpp b/core/io/stream_peer_ssl.cpp index 254ae84bf5..ccce48ccd7 100644 --- a/core/io/stream_peer_ssl.cpp +++ b/core/io/stream_peer_ssl.cpp @@ -39,7 +39,9 @@ StreamPeerSSL *(*StreamPeerSSL::_create)() = NULL; StreamPeerSSL *StreamPeerSSL::create() { - return _create(); + if (_create) + return _create(); + return NULL; } StreamPeerSSL::LoadCertsFromMemory StreamPeerSSL::load_certs_func = NULL; diff --git a/core/io/stream_peer_tcp.cpp b/core/io/stream_peer_tcp.cpp index 45f3e46e35..bcdae343b8 100644 --- a/core/io/stream_peer_tcp.cpp +++ b/core/io/stream_peer_tcp.cpp @@ -352,7 +352,6 @@ void StreamPeerTCP::_bind_methods() { StreamPeerTCP::StreamPeerTCP() : _sock(Ref<NetSocket>(NetSocket::create())), status(STATUS_NONE), - peer_host(IP_Address()), peer_port(0) { } diff --git a/core/io/tcp_server.cpp b/core/io/tcp_server.cpp index 6599c4eb5b..be87f47d50 100644 --- a/core/io/tcp_server.cpp +++ b/core/io/tcp_server.cpp @@ -83,11 +83,7 @@ bool TCP_Server::is_connection_available() const { return false; Error err = _sock->poll(NetSocket::POLL_TYPE_IN, 0); - if (err != OK) { - return false; - } - - return true; + return (err == OK); } Ref<StreamPeerTCP> TCP_Server::take_connection() { diff --git a/core/math/SCsub b/core/math/SCsub index 1c5f954470..4e76efceff 100644 --- a/core/math/SCsub +++ b/core/math/SCsub @@ -2,4 +2,37 @@ Import('env') -env.add_source_files(env.core_sources, "*.cpp") +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(CPPFLAGS=['-DMBEDTLS_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.add_source_files(env.core_sources, "*.cpp") diff --git a/core/math/a_star.cpp b/core/math/a_star.cpp index 7ce3824505..b61119d8df 100644 --- a/core/math/a_star.cpp +++ b/core/math/a_star.cpp @@ -216,6 +216,8 @@ int AStar::get_closest_point(const Vector3 &p_point) const { for (const Map<int, Point *>::Element *E = points.front(); E; E = E->next()) { + if (!E->get()->enabled) + continue; //Disabled points should not be considered real_t d = p_point.distance_squared_to(E->get()->pos); if (closest_id < 0 || d < closest_dist) { closest_dist = d; @@ -234,6 +236,10 @@ Vector3 AStar::get_closest_position_in_segment(const Vector3 &p_point) const { for (const Set<Segment>::Element *E = segments.front(); E; E = E->next()) { + if (!(E->get().from_point->enabled && E->get().to_point->enabled)) { + continue; + } + Vector3 segment[2] = { E->get().from_point->pos, E->get().to_point->pos, diff --git a/core/math/bsp_tree.cpp b/core/math/bsp_tree.cpp index d7e6e82cd9..a12f9fee2e 100644 --- a/core/math/bsp_tree.cpp +++ b/core/math/bsp_tree.cpp @@ -142,7 +142,7 @@ int BSP_Tree::_get_points_inside(int p_node, const Vector3 *p_points, int *p_ind } return _get_points_inside(node->over, p_points, p_indices, p_center, p_half_extents, p_indices_count); - } else if (dist_min <= 0) { //all points behind plane + } else { //all points behind plane if (node->under == UNDER_LEAF) { @@ -150,8 +150,6 @@ int BSP_Tree::_get_points_inside(int p_node, const Vector3 *p_points, int *p_ind } return _get_points_inside(node->under, p_points, p_indices, p_center, p_half_extents, p_indices_count); } - - return 0; } int BSP_Tree::get_points_inside(const Vector3 *p_points, int p_point_count) const { @@ -271,8 +269,6 @@ bool BSP_Tree::point_is_inside(const Vector3 &p_point) const { ERR_FAIL_COND_V(idx < MAX_NODES && idx >= node_count, false); #endif } - - return false; } static int _bsp_find_best_half_plane(const Face3 *p_faces, const Vector<int> &p_indices, real_t p_tolerance) { diff --git a/core/math/camera_matrix.cpp b/core/math/camera_matrix.cpp index f615cc8c65..8b3b6c82f3 100644 --- a/core/math/camera_matrix.cpp +++ b/core/math/camera_matrix.cpp @@ -507,21 +507,21 @@ void CameraMatrix::set_light_bias() { real_t *m = &matrix[0][0]; - m[0] = 0.5, - m[1] = 0.0, - m[2] = 0.0, - m[3] = 0.0, - m[4] = 0.0, - m[5] = 0.5, - m[6] = 0.0, - m[7] = 0.0, - m[8] = 0.0, - m[9] = 0.0, - m[10] = 0.5, - m[11] = 0.0, - m[12] = 0.5, - m[13] = 0.5, - m[14] = 0.5, + m[0] = 0.5; + m[1] = 0.0; + m[2] = 0.0; + m[3] = 0.0; + m[4] = 0.0; + m[5] = 0.5; + m[6] = 0.0; + m[7] = 0.0; + m[8] = 0.0; + m[9] = 0.0; + m[10] = 0.5; + m[11] = 0.0; + m[12] = 0.5; + m[13] = 0.5; + m[14] = 0.5; m[15] = 1.0; } @@ -529,21 +529,21 @@ void CameraMatrix::set_light_atlas_rect(const Rect2 &p_rect) { real_t *m = &matrix[0][0]; - m[0] = p_rect.size.width, - m[1] = 0.0, - m[2] = 0.0, - m[3] = 0.0, - m[4] = 0.0, - m[5] = p_rect.size.height, - m[6] = 0.0, - m[7] = 0.0, - m[8] = 0.0, - m[9] = 0.0, - m[10] = 1.0, - m[11] = 0.0, - m[12] = p_rect.position.x, - m[13] = p_rect.position.y, - m[14] = 0.0, + m[0] = p_rect.size.width; + m[1] = 0.0; + m[2] = 0.0; + m[3] = 0.0; + m[4] = 0.0; + m[5] = p_rect.size.height; + m[6] = 0.0; + m[7] = 0.0; + m[8] = 0.0; + m[9] = 0.0; + m[10] = 1.0; + m[11] = 0.0; + m[12] = p_rect.position.x; + m[13] = p_rect.position.y; + m[14] = 0.0; m[15] = 1.0; } diff --git a/core/math/crypto_core.cpp b/core/math/crypto_core.cpp new file mode 100644 index 0000000000..6449d94db8 --- /dev/null +++ b/core/math/crypto_core.cpp @@ -0,0 +1,146 @@ +/*************************************************************************/ +/* crypto_core.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_core.h" + +#include <mbedtls/aes.h> +#include <mbedtls/base64.h> +#include <mbedtls/md5.h> +#include <mbedtls/sha1.h> +#include <mbedtls/sha256.h> + +// MD5 +CryptoCore::MD5Context::MD5Context() { + ctx = memalloc(sizeof(mbedtls_md5_context)); + mbedtls_md5_init((mbedtls_md5_context *)ctx); +} + +CryptoCore::MD5Context::~MD5Context() { + mbedtls_md5_free((mbedtls_md5_context *)ctx); + memfree((mbedtls_md5_context *)ctx); +} + +Error CryptoCore::MD5Context::start() { + int ret = mbedtls_md5_starts_ret((mbedtls_md5_context *)ctx); + return ret ? FAILED : OK; +} + +Error CryptoCore::MD5Context::update(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; +} + +Error CryptoCore::MD5Context::finish(unsigned char r_hash[16]) { + int ret = mbedtls_md5_finish_ret((mbedtls_md5_context *)ctx, r_hash); + return ret ? FAILED : OK; +} + +// SHA256 +CryptoCore::SHA256Context::SHA256Context() { + ctx = memalloc(sizeof(mbedtls_sha256_context)); + mbedtls_sha256_init((mbedtls_sha256_context *)ctx); +} + +CryptoCore::SHA256Context::~SHA256Context() { + mbedtls_sha256_free((mbedtls_sha256_context *)ctx); + memfree((mbedtls_sha256_context *)ctx); +} + +Error CryptoCore::SHA256Context::start() { + int ret = mbedtls_sha256_starts_ret((mbedtls_sha256_context *)ctx, 0); + return ret ? FAILED : OK; +} + +Error CryptoCore::SHA256Context::update(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]) { + int ret = mbedtls_sha256_finish_ret((mbedtls_sha256_context *)ctx, r_hash); + return ret ? FAILED : OK; +} + +// AES256 +CryptoCore::AESContext::AESContext() { + ctx = memalloc(sizeof(mbedtls_aes_context)); + mbedtls_aes_init((mbedtls_aes_context *)ctx); +} + +CryptoCore::AESContext::~AESContext() { + mbedtls_aes_free((mbedtls_aes_context *)ctx); + memfree((mbedtls_aes_context *)ctx); +} + +Error CryptoCore::AESContext::set_encode_key(const uint8_t *p_key, size_t p_bits) { + int ret = mbedtls_aes_setkey_enc((mbedtls_aes_context *)ctx, p_key, p_bits); + return ret ? FAILED : OK; +} + +Error CryptoCore::AESContext::set_decode_key(const uint8_t *p_key, size_t p_bits) { + int ret = mbedtls_aes_setkey_dec((mbedtls_aes_context *)ctx, p_key, p_bits); + return ret ? FAILED : OK; +} + +Error CryptoCore::AESContext::encrypt_ecb(const uint8_t p_src[16], uint8_t r_dst[16]) { + int ret = mbedtls_aes_crypt_ecb((mbedtls_aes_context *)ctx, MBEDTLS_AES_ENCRYPT, p_src, r_dst); + return ret ? FAILED : OK; +} + +Error CryptoCore::AESContext::decrypt_ecb(const uint8_t p_src[16], uint8_t r_dst[16]) { + int ret = mbedtls_aes_crypt_ecb((mbedtls_aes_context *)ctx, MBEDTLS_AES_DECRYPT, p_src, r_dst); + return ret ? FAILED : OK; +} + +// CryptoCore +Error CryptoCore::b64_encode(uint8_t *r_dst, int p_dst_len, size_t *r_len, const uint8_t *p_src, int p_src_len) { + int ret = mbedtls_base64_encode(r_dst, p_dst_len, r_len, p_src, p_src_len); + return ret ? FAILED : OK; +} + +Error CryptoCore::b64_decode(uint8_t *r_dst, int p_dst_len, size_t *r_len, const uint8_t *p_src, int p_src_len) { + int ret = mbedtls_base64_decode(r_dst, p_dst_len, r_len, p_src, p_src_len); + return ret ? FAILED : OK; +} + +Error CryptoCore::md5(const uint8_t *p_src, int p_src_len, unsigned char r_hash[16]) { + int ret = mbedtls_md5_ret(p_src, p_src_len, r_hash); + return ret ? FAILED : OK; +} + +Error CryptoCore::sha1(const uint8_t *p_src, int p_src_len, unsigned char r_hash[20]) { + int ret = mbedtls_sha1_ret(p_src, p_src_len, r_hash); + return ret ? FAILED : OK; +} + +Error CryptoCore::sha256(const uint8_t *p_src, int p_src_len, unsigned char r_hash[32]) { + int ret = mbedtls_sha256_ret(p_src, p_src_len, r_hash, 0); + return ret ? FAILED : OK; +} diff --git a/core/math/crypto_core.h b/core/math/crypto_core.h new file mode 100644 index 0000000000..1cb3c86e18 --- /dev/null +++ b/core/math/crypto_core.h @@ -0,0 +1,89 @@ +/*************************************************************************/ +/* crypto_core.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_CORE_H +#define CRYPTO_CORE_H + +#include "core/reference.h" + +class CryptoCore { + +public: + class MD5Context { + + private: + void *ctx; // To include, or not to include... + + public: + MD5Context(); + ~MD5Context(); + + Error start(); + Error update(uint8_t *p_src, size_t p_len); + Error finish(unsigned char r_hash[16]); + }; + + class SHA256Context { + + private: + void *ctx; // To include, or not to include... + + public: + SHA256Context(); + ~SHA256Context(); + + Error start(); + Error update(uint8_t *p_src, size_t p_len); + Error finish(unsigned char r_hash[16]); + }; + + class AESContext { + + private: + void *ctx; // To include, or not to include... + + public: + AESContext(); + ~AESContext(); + + Error set_encode_key(const uint8_t *p_key, size_t p_bits); + Error set_decode_key(const uint8_t *p_key, size_t p_bits); + Error encrypt_ecb(const uint8_t p_src[16], uint8_t r_dst[16]); + Error decrypt_ecb(const uint8_t p_src[16], uint8_t r_dst[16]); + }; + + static Error b64_encode(uint8_t *r_dst, int p_dst_len, size_t *r_len, const uint8_t *p_src, int p_src_len); + static Error b64_decode(uint8_t *r_dst, int p_dst_len, size_t *r_len, const uint8_t *p_src, int p_src_len); + + static Error md5(const uint8_t *p_src, int p_src_len, unsigned char r_hash[16]); + static Error sha1(const uint8_t *p_src, int p_src_len, unsigned char r_hash[20]); + static Error sha256(const uint8_t *p_src, int p_src_len, unsigned char r_hash[32]); +}; +#endif // CRYPTO_CORE_H diff --git a/core/math/expression.cpp b/core/math/expression.cpp index e484e9194d..b52658e2cf 100644 --- a/core/math/expression.cpp +++ b/core/math/expression.cpp @@ -1794,7 +1794,7 @@ Expression::ENode *Expression::_parse_expression() { if (next_op == -1) { _set_error("Yet another parser bug...."); - ERR_FAIL_COND_V(next_op == -1, NULL); + ERR_FAIL_V(NULL); } // OK! create operator.. diff --git a/core/math/math_funcs.cpp b/core/math/math_funcs.cpp index 5b5fd8e283..7a2e74a413 100644 --- a/core/math/math_funcs.cpp +++ b/core/math/math_funcs.cpp @@ -161,8 +161,6 @@ uint32_t Math::larger_prime(uint32_t p_val) { return primes[idx]; idx++; } - - return 0; } double Math::random(double from, double to) { diff --git a/core/math/random_number_generator.cpp b/core/math/random_number_generator.cpp index 6add00c1d8..54a88d5cd8 100644 --- a/core/math/random_number_generator.cpp +++ b/core/math/random_number_generator.cpp @@ -30,8 +30,7 @@ #include "random_number_generator.h" -RandomNumberGenerator::RandomNumberGenerator() : - randbase() {} +RandomNumberGenerator::RandomNumberGenerator() {} void RandomNumberGenerator::_bind_methods() { ClassDB::bind_method(D_METHOD("set_seed", "seed"), &RandomNumberGenerator::set_seed); diff --git a/core/math/random_number_generator.h b/core/math/random_number_generator.h index a6182a4b33..9b54ea9b2e 100644 --- a/core/math/random_number_generator.h +++ b/core/math/random_number_generator.h @@ -47,7 +47,7 @@ public: _FORCE_INLINE_ uint64_t get_seed() { return randbase.get_seed(); } - _FORCE_INLINE_ void randomize() { return randbase.randomize(); } + _FORCE_INLINE_ void randomize() { randbase.randomize(); } _FORCE_INLINE_ uint32_t randi() { return randbase.rand(); } diff --git a/core/math/triangle_mesh.h b/core/math/triangle_mesh.h index ee7bf0f6b5..8b01080852 100644 --- a/core/math/triangle_mesh.h +++ b/core/math/triangle_mesh.h @@ -97,7 +97,7 @@ public: PoolVector<Triangle> get_triangles() const { return triangles; } PoolVector<Vector3> get_vertices() const { return vertices; } - void get_indices(PoolVector<int> *p_triangles_indices) const; + void get_indices(PoolVector<int> *r_triangles_indices) const; void create(const PoolVector<Vector3> &p_faces); TriangleMesh(); diff --git a/core/node_path.cpp b/core/node_path.cpp index 07ff765516..a4b7cbe2eb 100644 --- a/core/node_path.cpp +++ b/core/node_path.cpp @@ -357,7 +357,7 @@ NodePath::NodePath(const String &p_path) { String path = p_path; Vector<StringName> subpath; - int absolute = (path[0] == '/') ? 1 : 0; + bool absolute = (path[0] == '/'); bool last_is_slash = true; bool has_slashes = false; int slices = 0; @@ -387,7 +387,7 @@ NodePath::NodePath(const String &p_path) { path = path.substr(0, subpath_pos); } - for (int i = absolute; i < path.length(); i++) { + for (int i = (int)absolute; i < path.length(); i++) { if (path[i] == '/') { @@ -407,7 +407,7 @@ NodePath::NodePath(const String &p_path) { data = memnew(Data); data->refcount.init(); - data->absolute = absolute ? true : false; + data->absolute = absolute; data->has_slashes = has_slashes; data->subpath = subpath; data->hash_cache_valid = false; @@ -416,10 +416,10 @@ NodePath::NodePath(const String &p_path) { return; data->path.resize(slices); last_is_slash = true; - int from = absolute; + int from = (int)absolute; int slice = 0; - for (int i = absolute; i < path.length() + 1; i++) { + for (int i = (int)absolute; i < path.length() + 1; i++) { if (path[i] == '/' || path[i] == 0) { diff --git a/core/object.cpp b/core/object.cpp index 64f55f08a9..3367d6b6c3 100644 --- a/core/object.cpp +++ b/core/object.cpp @@ -474,7 +474,6 @@ void Object::set(const StringName &p_name, const Variant &p_value, bool *r_valid if (r_valid) *r_valid = false; - return; } Variant Object::get(const StringName &p_name, bool *r_valid) const { @@ -743,13 +742,11 @@ void Object::call_multilevel(const StringName &p_method, const Variant **p_args, if (Object::cast_to<Reference>(this)) { ERR_EXPLAIN("Can't 'free' a reference."); ERR_FAIL(); - return; } if (_lock_index.get() > 1) { ERR_EXPLAIN("Object is locked and can't be freed."); ERR_FAIL(); - return; } #endif @@ -810,11 +807,7 @@ bool Object::has_method(const StringName &p_method) const { MethodBind *method = ClassDB::get_method(get_class_name(), p_method); - if (method) { - return true; - } - - return false; + return method != NULL; } Variant Object::getvar(const Variant &p_key, bool *r_valid) const { @@ -1472,7 +1465,7 @@ Error Object::connect(const StringName &p_signal, Object *p_to_object, const Str 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_COND_V(!signal_is_valid, ERR_INVALID_PARAMETER); + ERR_FAIL_V(ERR_INVALID_PARAMETER); } signal_map[p_signal] = Signal(); s = &signal_map[p_signal]; @@ -1485,7 +1478,7 @@ Error Object::connect(const StringName &p_signal, Object *p_to_object, const Str return OK; } else { ERR_EXPLAIN("Signal '" + p_signal + "' is already connected to given method '" + p_to_method + "' in that object."); - ERR_FAIL_COND_V(s->slot_map.has(target), ERR_INVALID_PARAMETER); + ERR_FAIL_V(ERR_INVALID_PARAMETER); } } @@ -1522,7 +1515,7 @@ bool Object::is_connected(const StringName &p_signal, Object *p_to_object, const return false; ERR_EXPLAIN("Nonexistent signal: " + p_signal); - ERR_FAIL_COND_V(!s, false); + ERR_FAIL_V(false); } Signal::Target target(p_to_object->get_instance_id(), p_to_method); @@ -1542,11 +1535,11 @@ void Object::_disconnect(const StringName &p_signal, Object *p_to_object, const Signal *s = signal_map.getptr(p_signal); if (!s) { ERR_EXPLAIN("Nonexistent signal: " + p_signal); - ERR_FAIL_COND(!s); + ERR_FAIL(); } if (s->lock > 0) { ERR_EXPLAIN("Attempt to disconnect signal '" + p_signal + "' while emitting (locks: " + itos(s->lock) + ")"); - ERR_FAIL_COND(s->lock > 0); + ERR_FAIL(); } Signal::Target target(p_to_object->get_instance_id(), p_to_method); @@ -1689,7 +1682,7 @@ void Object::clear_internal_resource_paths() { void Object::_bind_methods() { ClassDB::bind_method(D_METHOD("get_class"), &Object::get_class); - ClassDB::bind_method(D_METHOD("is_class", "type"), &Object::is_class); + ClassDB::bind_method(D_METHOD("is_class", "class"), &Object::is_class); ClassDB::bind_method(D_METHOD("set", "property", "value"), &Object::_set_bind); ClassDB::bind_method(D_METHOD("get", "property"), &Object::_get_bind); ClassDB::bind_method(D_METHOD("set_indexed", "property", "value"), &Object::_set_indexed_bind); @@ -1709,14 +1702,8 @@ void Object::_bind_methods() { ClassDB::bind_method(D_METHOD("has_meta", "name"), &Object::has_meta); ClassDB::bind_method(D_METHOD("get_meta_list"), &Object::_get_meta_list_bind); - //todo reimplement this per language so all 5 arguments can be called - - //ClassDB::bind_method(D_METHOD("call","method","arg1","arg2","arg3","arg4"),&Object::_call_bind,DEFVAL(Variant()),DEFVAL(Variant()),DEFVAL(Variant()),DEFVAL(Variant())); - //ClassDB::bind_method(D_METHOD("call_deferred","method","arg1","arg2","arg3","arg4"),&Object::_call_deferred_bind,DEFVAL(Variant()),DEFVAL(Variant()),DEFVAL(Variant()),DEFVAL(Variant())); - ClassDB::bind_method(D_METHOD("add_user_signal", "signal", "arguments"), &Object::_add_user_signal, DEFVAL(Array())); ClassDB::bind_method(D_METHOD("has_user_signal", "signal"), &Object::_has_user_signal); - //ClassDB::bind_method(D_METHOD("emit_signal","signal","arguments"),&Object::_emit_signal,DEFVAL(Array())); { MethodInfo mi; diff --git a/core/object.h b/core/object.h index 4394c1c3da..1e0b22c086 100644 --- a/core/object.h +++ b/core/object.h @@ -130,6 +130,7 @@ enum PropertyUsageFlags { #define ADD_SIGNAL(m_signal) ClassDB::add_signal(get_class_static(), m_signal) #define ADD_PROPERTY(m_property, m_setter, m_getter) ClassDB::add_property(get_class_static(), m_property, _scs_create(m_setter), _scs_create(m_getter)) #define ADD_PROPERTYI(m_property, m_setter, m_getter, m_index) ClassDB::add_property(get_class_static(), m_property, _scs_create(m_setter), _scs_create(m_getter), m_index) +#define ADD_PROPERTY_DEFAULT(m_property, m_default) ClassDB::set_property_default_value(get_class_static(), m_property, m_default) #define ADD_GROUP(m_name, m_prefix) ClassDB::add_property_group(get_class_static(), m_name, m_prefix) struct PropertyInfo { diff --git a/core/os/dir_access.cpp b/core/os/dir_access.cpp index d81c30f33a..0cdb5b41b7 100644 --- a/core/os/dir_access.cpp +++ b/core/os/dir_access.cpp @@ -43,8 +43,6 @@ String DirAccess::_get_root_path() const { case ACCESS_USERDATA: return OS::get_singleton()->get_user_data_dir(); default: return ""; } - - return ""; } String DirAccess::_get_root_string() const { @@ -54,8 +52,6 @@ String DirAccess::_get_root_string() const { case ACCESS_USERDATA: return "user://"; default: return ""; } - - return ""; } int DirAccess::get_current_drive() { @@ -373,12 +369,12 @@ Error DirAccess::_copy_dir(DirAccess *p_target_da, String p_to, int p_chmod_flag if (current_is_dir()) dirs.push_back(n); else { - String rel_path = n; + const String &rel_path = n; if (!n.is_rel_path()) { list_dir_end(); return ERR_BUG; } - Error err = copy(get_current_dir() + "/" + n, p_to + rel_path, p_chmod_flags); + Error err = copy(get_current_dir().plus_file(n), p_to + rel_path, p_chmod_flags); if (err) { list_dir_end(); return err; diff --git a/core/os/file_access.cpp b/core/os/file_access.cpp index 913caf6584..7509050b2b 100644 --- a/core/os/file_access.cpp +++ b/core/os/file_access.cpp @@ -32,12 +32,10 @@ #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" -#include "thirdparty/misc/md5.h" -#include "thirdparty/misc/sha256.h" - FileAccess::CreateFunc FileAccess::create_func[ACCESS_MAX] = { 0, 0 }; FileAccess::FileCloseFailNotify FileAccess::close_fail_notify = NULL; @@ -601,7 +599,8 @@ 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_FAIL_COND_V(!f, Vector<uint8_t>()); + ERR_EXPLAIN("Can't open file from path: " + String(p_path)); + ERR_FAIL_V(Vector<uint8_t>()); } Vector<uint8_t> data; data.resize(f->get_len()); @@ -621,7 +620,8 @@ String FileAccess::get_file_as_string(const String &p_path, Error *r_error) { if (r_error) { return String(); } - ERR_FAIL_COND_V(err != OK, String()); + ERR_EXPLAIN("Can't get file as string from path: " + String(p_path)); + ERR_FAIL_V(String()); } String ret; @@ -635,8 +635,8 @@ String FileAccess::get_md5(const String &p_file) { if (!f) return String(); - MD5_CTX md5; - MD5Init(&md5); + CryptoCore::MD5Context ctx; + ctx.start(); unsigned char step[32768]; @@ -645,24 +645,24 @@ String FileAccess::get_md5(const String &p_file) { int br = f->get_buffer(step, 32768); if (br > 0) { - MD5Update(&md5, step, br); + ctx.update(step, br); } if (br < 4096) break; } - MD5Final(&md5); - - String ret = String::md5(md5.digest); + unsigned char hash[16]; + ctx.finish(hash); memdelete(f); - return ret; + + return String::md5(hash); } String FileAccess::get_multiple_md5(const Vector<String> &p_file) { - MD5_CTX md5; - MD5Init(&md5); + CryptoCore::MD5Context ctx; + ctx.start(); for (int i = 0; i < p_file.size(); i++) { FileAccess *f = FileAccess::open(p_file[i], READ); @@ -675,7 +675,7 @@ String FileAccess::get_multiple_md5(const Vector<String> &p_file) { int br = f->get_buffer(step, 32768); if (br > 0) { - MD5Update(&md5, step, br); + ctx.update(step, br); } if (br < 4096) break; @@ -683,11 +683,10 @@ String FileAccess::get_multiple_md5(const Vector<String> &p_file) { memdelete(f); } - MD5Final(&md5); + unsigned char hash[16]; + ctx.finish(hash); - String ret = String::md5(md5.digest); - - return ret; + return String::md5(hash); } String FileAccess::get_sha256(const String &p_file) { @@ -696,8 +695,8 @@ String FileAccess::get_sha256(const String &p_file) { if (!f) return String(); - sha256_context sha256; - sha256_init(&sha256); + CryptoCore::SHA256Context ctx; + ctx.start(); unsigned char step[32768]; @@ -706,15 +705,14 @@ String FileAccess::get_sha256(const String &p_file) { int br = f->get_buffer(step, 32768); if (br > 0) { - sha256_hash(&sha256, step, br); + ctx.update(step, br); } if (br < 4096) break; } unsigned char hash[32]; - - sha256_done(&sha256, hash); + ctx.finish(hash); memdelete(f); return String::hex_encode_buffer(hash, 32); diff --git a/core/os/input_event.cpp b/core/os/input_event.cpp index 9c5066da3d..a40a50cfce 100644 --- a/core/os/input_event.cpp +++ b/core/os/input_event.cpp @@ -314,7 +314,7 @@ bool InputEventKey::action_match(const Ref<InputEvent> &p_event, bool *p_pressed if (p_pressed != NULL) *p_pressed = key->is_pressed(); if (p_strength != NULL) - *p_strength = (*p_pressed) ? 1.0f : 0.0f; + *p_strength = (p_pressed != NULL && *p_pressed) ? 1.0f : 0.0f; } return match; } @@ -483,7 +483,7 @@ bool InputEventMouseButton::action_match(const Ref<InputEvent> &p_event, bool *p if (p_pressed != NULL) *p_pressed = mb->is_pressed(); if (p_strength != NULL) - *p_strength = (*p_pressed) ? 1.0f : 0.0f; + *p_strength = (p_pressed != NULL && *p_pressed) ? 1.0f : 0.0f; } return match; @@ -795,7 +795,7 @@ bool InputEventJoypadButton::action_match(const Ref<InputEvent> &p_event, bool * if (p_pressed != NULL) *p_pressed = jb->is_pressed(); if (p_strength != NULL) - *p_strength = (*p_pressed) ? 1.0f : 0.0f; + *p_strength = (p_pressed != NULL && *p_pressed) ? 1.0f : 0.0f; } return match; @@ -1041,7 +1041,7 @@ bool InputEventAction::action_match(const Ref<InputEvent> &p_event, bool *p_pres if (p_pressed != NULL) *p_pressed = act->pressed; if (p_strength != NULL) - *p_strength = (*p_pressed) ? 1.0f : 0.0f; + *p_strength = (p_pressed != NULL && *p_pressed) ? 1.0f : 0.0f; } return match; } diff --git a/core/os/main_loop.cpp b/core/os/main_loop.cpp index 895ce14ae9..9946ced2f3 100644 --- a/core/os/main_loop.cpp +++ b/core/os/main_loop.cpp @@ -44,9 +44,9 @@ void MainLoop::_bind_methods() { BIND_VMETHOD(MethodInfo("_input_event", PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent"))); BIND_VMETHOD(MethodInfo("_input_text", PropertyInfo(Variant::STRING, "text"))); BIND_VMETHOD(MethodInfo("_initialize")); - BIND_VMETHOD(MethodInfo("_iteration", PropertyInfo(Variant::REAL, "delta"))); - BIND_VMETHOD(MethodInfo("_idle", PropertyInfo(Variant::REAL, "delta"))); - BIND_VMETHOD(MethodInfo("_drop_files", PropertyInfo(Variant::POOL_STRING_ARRAY, "files"), PropertyInfo(Variant::INT, "screen"))); + BIND_VMETHOD(MethodInfo(Variant::BOOL, "_iteration", PropertyInfo(Variant::REAL, "delta"))); + BIND_VMETHOD(MethodInfo(Variant::BOOL, "_idle", PropertyInfo(Variant::REAL, "delta"))); + BIND_VMETHOD(MethodInfo("_drop_files", PropertyInfo(Variant::POOL_STRING_ARRAY, "files"), PropertyInfo(Variant::INT, "from_screen"))); BIND_VMETHOD(MethodInfo("_finalize")); BIND_CONSTANT(NOTIFICATION_WM_MOUSE_ENTER); diff --git a/core/os/memory.h b/core/os/memory.h index f3ca9fc614..e073b11e76 100644 --- a/core/os/memory.h +++ b/core/os/memory.h @@ -66,7 +66,7 @@ public: class DefaultAllocator { public: _FORCE_INLINE_ static void *alloc(size_t p_memory) { return Memory::alloc_static(p_memory, false); } - _FORCE_INLINE_ static void free(void *p_ptr) { return Memory::free_static(p_ptr, false); } + _FORCE_INLINE_ static void free(void *p_ptr) { Memory::free_static(p_ptr, false); } }; void *operator new(size_t p_size, const char *p_description); ///< operator new that takes a description and uses MemoryStaticPool diff --git a/core/os/os.cpp b/core/os/os.cpp index 1a3c9ac5f8..925154af7d 100644 --- a/core/os/os.cpp +++ b/core/os/os.cpp @@ -51,6 +51,35 @@ uint32_t OS::get_ticks_msec() const { return get_ticks_usec() / 1000; } +String OS::get_iso_date_time(bool local) const { + OS::Date date = get_date(local); + OS::Time time = get_time(local); + + String timezone; + if (!local) { + TimeZoneInfo zone = get_time_zone_info(); + if (zone.bias >= 0) { + timezone = "+"; + } + timezone = timezone + itos(zone.bias / 60).pad_zeros(2) + itos(zone.bias % 60).pad_zeros(2); + } else { + timezone = "Z"; + } + + return itos(date.year).pad_zeros(2) + + "-" + + itos(date.month).pad_zeros(2) + + "-" + + itos(date.day).pad_zeros(2) + + "T" + + itos(time.hour).pad_zeros(2) + + ":" + + itos(time.min).pad_zeros(2) + + ":" + + itos(time.sec).pad_zeros(2) + + timezone; +} + uint64_t OS::get_splash_tick_msec() const { return _msec_splash; } @@ -239,7 +268,8 @@ void OS::print_all_resources(String p_to_file) { _OSPRF = FileAccess::open(p_to_file, FileAccess::WRITE, &err); if (err != OK) { _OSPRF = NULL; - ERR_FAIL_COND(err != OK); + ERR_EXPLAIN("Can't print all resources to file: " + String(p_to_file)); + ERR_FAIL(); } } @@ -759,6 +789,8 @@ OS::OS() { } OS::~OS() { + if (last_error) + memfree(last_error); memdelete(_logger); singleton = NULL; } diff --git a/core/os/os.h b/core/os/os.h index 1b19ddff26..2224d3b006 100644 --- a/core/os/os.h +++ b/core/os/os.h @@ -336,6 +336,7 @@ public: virtual Date get_date(bool local = false) const = 0; virtual Time get_time(bool local = false) const = 0; virtual TimeZoneInfo get_time_zone_info() const = 0; + virtual String get_iso_date_time(bool local = false) const; virtual uint64_t get_unix_time() const; virtual uint64_t get_system_time_secs() const; virtual uint64_t get_system_time_msecs() const; diff --git a/core/packed_data_container.cpp b/core/packed_data_container.cpp index fa60be64a7..54bf12b314 100644 --- a/core/packed_data_container.cpp +++ b/core/packed_data_container.cpp @@ -224,7 +224,8 @@ uint32_t PackedDataContainer::_pack(const Variant &p_data, Vector<uint8_t> &tmpd string_cache[s] = tmpdata.size(); - }; //fallthrough + FALLTHROUGH; + }; case Variant::NIL: case Variant::BOOL: case Variant::INT: diff --git a/core/pool_allocator.cpp b/core/pool_allocator.cpp index 11a3be89bd..094352b5cc 100644 --- a/core/pool_allocator.cpp +++ b/core/pool_allocator.cpp @@ -206,8 +206,8 @@ PoolAllocator::ID PoolAllocator::alloc(int p_size) { if (!find_hole(&new_entry_indices_pos, size_to_alloc)) { mt_unlock(); - ERR_PRINT("memory can't be compacted further"); - return POOL_ALLOCATOR_INVALID_ID; + ERR_EXPLAIN("Memory can't be compacted further"); + ERR_FAIL_V(POOL_ALLOCATOR_INVALID_ID); } } @@ -217,7 +217,8 @@ PoolAllocator::ID PoolAllocator::alloc(int p_size) { if (!found_free_entry) { mt_unlock(); - ERR_FAIL_COND_V(!found_free_entry, POOL_ALLOCATOR_INVALID_ID); + ERR_EXPLAIN("No free entry found in PoolAllocator"); + ERR_FAIL_V(POOL_ALLOCATOR_INVALID_ID); } /* move all entry indices up, make room for this one */ diff --git a/core/pool_vector.h b/core/pool_vector.h index 102a620f17..338de966f6 100644 --- a/core/pool_vector.h +++ b/core/pool_vector.h @@ -411,8 +411,8 @@ public: p_to = size() + p_to; } - CRASH_BAD_INDEX(p_from, size()); - CRASH_BAD_INDEX(p_to, size()); + ERR_FAIL_INDEX_V(p_from, size(), PoolVector<T>()); + ERR_FAIL_INDEX_V(p_to, size(), PoolVector<T>()); PoolVector<T> slice; int span = 1 + p_to - p_from; @@ -511,6 +511,8 @@ const 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); + if (alloc == NULL) { if (p_size == 0) diff --git a/core/project_settings.cpp b/core/project_settings.cpp index 0508806a35..fc1a74801d 100644 --- a/core/project_settings.cpp +++ b/core/project_settings.cpp @@ -566,7 +566,7 @@ Error ProjectSettings::_load_settings_text(const String p_path) { 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_COND_V(config_version > CONFIG_VERSION, ERR_FILE_CANT_OPEN); + ERR_FAIL_V(ERR_FILE_CANT_OPEN); } } else { if (section == String()) { @@ -863,8 +863,6 @@ Error ProjectSettings::save_custom(const String &p_path, const CustomMap &p_cust ERR_EXPLAIN("Unknown config file format: " + p_path); ERR_FAIL_V(ERR_FILE_UNRECOGNIZED); } - - return OK; } Variant _GLOBAL_DEF(const String &p_var, const Variant &p_default, bool p_restart_if_changed) { diff --git a/core/register_core_types.cpp b/core/register_core_types.cpp index 135df4e5bd..af863dd385 100644 --- a/core/register_core_types.cpp +++ b/core/register_core_types.cpp @@ -273,6 +273,7 @@ void unregister_core_types() { ResourceLoader::finalize(); + ClassDB::cleanup_defaults(); ObjectDB::cleanup(); unregister_variant_methods(); diff --git a/core/safe_refcount.h b/core/safe_refcount.h index f6b8f80271..54f540b0c7 100644 --- a/core/safe_refcount.h +++ b/core/safe_refcount.h @@ -189,11 +189,7 @@ public: _ALWAYS_INLINE_ bool unref() { // true if must be disposed of - if (atomic_decrement(&count) == 0) { - return true; - } - - return false; + return atomic_decrement(&count) == 0; } _ALWAYS_INLINE_ uint32_t get() const { // nothrow diff --git a/core/undo_redo.cpp b/core/undo_redo.cpp index f7ca6d3bde..f0c2b8eb9b 100644 --- a/core/undo_redo.cpp +++ b/core/undo_redo.cpp @@ -336,6 +336,7 @@ bool UndoRedo::redo() { _process_operation_list(actions.write[current_action].do_ops.front()); version++; + emit_signal("version_changed"); return true; } @@ -348,6 +349,8 @@ bool UndoRedo::undo() { _process_operation_list(actions.write[current_action].undo_ops.front()); current_action--; version--; + emit_signal("version_changed"); + return true; } @@ -359,18 +362,30 @@ void UndoRedo::clear_history(bool p_increase_version) { while (actions.size()) _pop_history_tail(); - if (p_increase_version) + if (p_increase_version) { version++; + emit_signal("version_changed"); + } } String UndoRedo::get_current_action_name() const { ERR_FAIL_COND_V(action_level > 0, ""); if (current_action < 0) - return ""; //nothing to redo + return ""; return actions[current_action].name; } +bool UndoRedo::has_undo() { + + return current_action >= 0; +} + +bool UndoRedo::has_redo() { + + return (current_action + 1) < actions.size(); +} + uint64_t UndoRedo::get_version() const { return version; @@ -523,10 +538,14 @@ void UndoRedo::_bind_methods() { ClassDB::bind_method(D_METHOD("add_undo_reference", "object"), &UndoRedo::add_undo_reference); ClassDB::bind_method(D_METHOD("clear_history", "increase_version"), &UndoRedo::clear_history, DEFVAL(true)); ClassDB::bind_method(D_METHOD("get_current_action_name"), &UndoRedo::get_current_action_name); + ClassDB::bind_method(D_METHOD("has_undo"), &UndoRedo::has_undo); + ClassDB::bind_method(D_METHOD("has_redo"), &UndoRedo::has_redo); ClassDB::bind_method(D_METHOD("get_version"), &UndoRedo::get_version); ClassDB::bind_method(D_METHOD("redo"), &UndoRedo::redo); ClassDB::bind_method(D_METHOD("undo"), &UndoRedo::undo); + ADD_SIGNAL(MethodInfo("version_changed")); + BIND_ENUM_CONSTANT(MERGE_DISABLE); BIND_ENUM_CONSTANT(MERGE_ENDS); BIND_ENUM_CONSTANT(MERGE_ALL); diff --git a/core/undo_redo.h b/core/undo_redo.h index e2cc6c659b..276d00d9af 100644 --- a/core/undo_redo.h +++ b/core/undo_redo.h @@ -118,6 +118,9 @@ public: String get_current_action_name() const; void clear_history(bool p_increase_version = true); + bool has_undo(); + bool has_redo(); + uint64_t get_version() const; void set_commit_notify_callback(CommitNotifyCallback p_callback, void *p_ud); diff --git a/core/ustring.cpp b/core/ustring.cpp index 686aa6f8e3..2b312191e2 100644 --- a/core/ustring.cpp +++ b/core/ustring.cpp @@ -31,6 +31,7 @@ #include "ustring.h" #include "core/color.h" +#include "core/math/crypto_core.h" #include "core/math/math_funcs.h" #include "core/os/memory.h" #include "core/print_string.h" @@ -38,9 +39,6 @@ #include "core/ucaps.h" #include "core/variant.h" -#include "thirdparty/misc/md5.h" -#include "thirdparty/misc/sha256.h" - #include <wchar.h> #ifndef NO_USE_STDLIB @@ -481,8 +479,6 @@ signed char String::nocasecmp_to(const String &p_str) const { this_str++; that_str++; } - - return 0; //should never reach anyway } signed char String::casecmp_to(const String &p_str) const { @@ -513,8 +509,6 @@ signed char String::casecmp_to(const String &p_str) const { this_str++; that_str++; } - - return 0; //should never reach anyway } signed char String::naturalnocasecmp_to(const String &p_str) const { @@ -731,8 +725,6 @@ String String::get_slicec(CharType p_splitter, int p_slice) const { i++; } - - return String(); //no find! } Vector<String> String::split_spaces() const { @@ -2260,54 +2252,42 @@ uint64_t String::hash64() const { String String::md5_text() const { CharString cs = utf8(); - MD5_CTX ctx; - MD5Init(&ctx); - MD5Update(&ctx, (unsigned char *)cs.ptr(), cs.length()); - MD5Final(&ctx); - return String::md5(ctx.digest); + unsigned char hash[16]; + CryptoCore::md5((unsigned char *)cs.ptr(), cs.length(), hash); + return String::hex_encode_buffer(hash, 16); } String String::sha256_text() const { CharString cs = utf8(); unsigned char hash[32]; - sha256_context ctx; - sha256_init(&ctx); - sha256_hash(&ctx, (unsigned char *)cs.ptr(), cs.length()); - sha256_done(&ctx, hash); + CryptoCore::sha256((unsigned char *)cs.ptr(), cs.length(), hash); return String::hex_encode_buffer(hash, 32); } Vector<uint8_t> String::md5_buffer() const { CharString cs = utf8(); - MD5_CTX ctx; - MD5Init(&ctx); - MD5Update(&ctx, (unsigned char *)cs.ptr(), cs.length()); - MD5Final(&ctx); + unsigned char hash[16]; + CryptoCore::md5((unsigned char *)cs.ptr(), cs.length(), hash); Vector<uint8_t> ret; ret.resize(16); for (int i = 0; i < 16; i++) { - ret.write[i] = ctx.digest[i]; - }; - + ret.write[i] = hash[i]; + } return ret; }; Vector<uint8_t> String::sha256_buffer() const { CharString cs = utf8(); unsigned char hash[32]; - sha256_context ctx; - sha256_init(&ctx); - sha256_hash(&ctx, (unsigned char *)cs.ptr(), cs.length()); - sha256_done(&ctx, hash); + CryptoCore::sha256((unsigned char *)cs.ptr(), cs.length(), hash); Vector<uint8_t> ret; ret.resize(32); for (int i = 0; i < 32; i++) { ret.write[i] = hash[i]; } - return ret; } @@ -3799,11 +3779,7 @@ bool String::is_valid_filename() const { return false; } - if (find(":") != -1 || find("/") != -1 || find("\\") != -1 || find("?") != -1 || find("*") != -1 || find("\"") != -1 || find("|") != -1 || find("%") != -1 || find("<") != -1 || find(">") != -1) { - return false; - } else { - return true; - } + return !(find(":") != -1 || find("/") != -1 || find("\\") != -1 || find("?") != -1 || find("*") != -1 || find("\"") != -1 || find("|") != -1 || find("%") != -1 || find("<") != -1 || find(">") != -1); } bool String::is_valid_ip_address() const { @@ -3941,7 +3917,6 @@ String String::percent_decode() const { uint8_t a = LOWERCASE(cs[i + 1]); uint8_t b = LOWERCASE(cs[i + 2]); - c = 0; if (a >= '0' && a <= '9') c = (a - '0') << 4; else if (a >= 'a' && a <= 'f') diff --git a/core/ustring.h b/core/ustring.h index ecf934a26b..a32daabb91 100644 --- a/core/ustring.h +++ b/core/ustring.h @@ -404,8 +404,6 @@ _FORCE_INLINE_ bool is_str_less(const L *l_ptr, const R *r_ptr) { l_ptr++; r_ptr++; } - - CRASH_COND(true); // unreachable } /* end of namespace */ diff --git a/core/variant.cpp b/core/variant.cpp index 6eadf59fce..5b51a4e513 100644 --- a/core/variant.cpp +++ b/core/variant.cpp @@ -709,7 +709,7 @@ bool Variant::is_zero() const { // atomic types case BOOL: { - return _data._bool == false; + return !(_data._bool); } break; case INT: { @@ -1171,8 +1171,6 @@ Variant::operator signed int() const { return 0; } } - - return 0; } Variant::operator unsigned int() const { @@ -1188,8 +1186,6 @@ Variant::operator unsigned int() const { return 0; } } - - return 0; } Variant::operator int64_t() const { @@ -1206,8 +1202,6 @@ Variant::operator int64_t() const { return 0; } } - - return 0; } /* @@ -1244,8 +1238,6 @@ Variant::operator uint64_t() const { return 0; } } - - return 0; } #ifdef NEED_LONG_INT @@ -1300,8 +1292,6 @@ Variant::operator signed short() const { return 0; } } - - return 0; } Variant::operator unsigned short() const { @@ -1317,8 +1307,6 @@ Variant::operator unsigned short() const { return 0; } } - - return 0; } Variant::operator signed char() const { @@ -1334,8 +1322,6 @@ Variant::operator signed char() const { return 0; } } - - return 0; } Variant::operator unsigned char() const { @@ -1351,8 +1337,6 @@ Variant::operator unsigned char() const { return 0; } } - - return 0; } Variant::operator CharType() const { @@ -1374,8 +1358,6 @@ Variant::operator float() const { return 0; } } - - return 0; } Variant::operator double() const { @@ -1391,8 +1373,6 @@ Variant::operator double() const { return 0; } } - - return true; } Variant::operator StringName() const { diff --git a/core/variant.h b/core/variant.h index 5151262f27..a8e99c13f1 100644 --- a/core/variant.h +++ b/core/variant.h @@ -248,8 +248,8 @@ public: Variant(unsigned short p_short); Variant(signed char p_char); // real one Variant(unsigned char p_char); - Variant(int64_t p_char); // real one - Variant(uint64_t p_char); + Variant(int64_t p_int); // real one + Variant(uint64_t p_int); Variant(float p_float); Variant(double p_double); Variant(const String &p_string); @@ -262,11 +262,11 @@ public: Variant(const Plane &p_plane); Variant(const ::AABB &p_aabb); Variant(const Quat &p_quat); - Variant(const Basis &p_transform); + Variant(const Basis &p_matrix); Variant(const Transform2D &p_transform); Variant(const Transform &p_transform); Variant(const Color &p_color); - Variant(const NodePath &p_path); + Variant(const NodePath &p_node_path); Variant(const RefPtr &p_resource); Variant(const RID &p_rid); Variant(const Object *p_object); @@ -283,17 +283,17 @@ public: Variant(const PoolVector<Face3> &p_face_array); Variant(const Vector<Variant> &p_array); - Variant(const Vector<uint8_t> &p_raw_array); - Variant(const Vector<int> &p_int_array); - Variant(const Vector<real_t> &p_real_array); - Variant(const Vector<String> &p_string_array); - Variant(const Vector<StringName> &p_string_array); - Variant(const Vector<Vector3> &p_vector3_array); - Variant(const Vector<Color> &p_color_array); + Variant(const Vector<uint8_t> &p_array); + Variant(const Vector<int> &p_array); + Variant(const Vector<real_t> &p_array); + Variant(const Vector<String> &p_array); + Variant(const Vector<StringName> &p_array); + Variant(const Vector<Vector3> &p_array); + Variant(const Vector<Color> &p_array); Variant(const Vector<Plane> &p_array); // helper Variant(const Vector<RID> &p_array); // helper Variant(const Vector<Vector2> &p_array); // helper - Variant(const PoolVector<Vector2> &p_array); // helper + Variant(const PoolVector<Vector2> &p_vector2_array); // helper Variant(const IP_Address &p_address); diff --git a/core/variant_call.cpp b/core/variant_call.cpp index dc28f1ca02..811008e7c8 100644 --- a/core/variant_call.cpp +++ b/core/variant_call.cpp @@ -33,10 +33,10 @@ #include "core/color_names.inc" #include "core/core_string_names.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" -#include "thirdparty/misc/sha256.h" typedef void (*VariantFunc)(Variant &r_ret, Variant &p_self, const Variant **p_args); typedef void (*VariantConstructFunc)(Variant &r_ret, const Variant **p_args); @@ -598,10 +598,7 @@ struct _VariantCall { PoolByteArray::Read r = ba->read(); String s; unsigned char hash[32]; - sha256_context sha256; - sha256_init(&sha256); - sha256_hash(&sha256, (unsigned char *)r.ptr(), ba->size()); - sha256_done(&sha256, hash); + CryptoCore::sha256((unsigned char *)r.ptr(), ba->size(), hash); s = String::hex_encode_buffer(hash, 32); r_ret = s; } diff --git a/core/variant_op.cpp b/core/variant_op.cpp index f3c9bcaa7e..d677c7776a 100644 --- a/core/variant_op.cpp +++ b/core/variant_op.cpp @@ -2183,7 +2183,8 @@ void Variant::set(const Variant &p_index, const Variant &p_value, bool *r_valid) return; } - return obj->set(p_index, p_value, r_valid); + obj->set(p_index, p_value, r_valid); + return; } } break; case DICTIONARY: { diff --git a/core/variant_parser.cpp b/core/variant_parser.cpp index d7371b0434..d5513bc2d7 100644 --- a/core/variant_parser.cpp +++ b/core/variant_parser.cpp @@ -436,8 +436,6 @@ Error VariantParser::_parse_enginecfg(Stream *p_stream, Vector<String> &strings, line++; } } - - return OK; } template <class T> @@ -799,8 +797,6 @@ Error VariantParser::parse_value(Token &token, Variant &value, Stream *p_stream, } } - return OK; - } else if (id == "Resource" || id == "SubResource" || id == "ExtResource") { get_token(p_stream, token, line, r_err_str); @@ -864,8 +860,6 @@ Error VariantParser::parse_value(Token &token, Variant &value, Stream *p_stream, return ERR_PARSE_ERROR; } } - - return OK; #ifndef DISABLE_DEPRECATED } else if (id == "InputEvent") { @@ -1256,8 +1250,6 @@ Error VariantParser::parse_value(Token &token, Variant &value, Stream *p_stream, r_err_str = "Expected value, got " + String(tk_name[token.type]) + "."; return ERR_PARSE_ERROR; } - - return ERR_PARSE_ERROR; } Error VariantParser::_parse_array(Array &array, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser) { @@ -1301,8 +1293,6 @@ Error VariantParser::_parse_array(Array &array, Stream *p_stream, int &line, Str array.push_back(v); need_comma = true; } - - return OK; } Error VariantParser::_parse_dictionary(Dictionary &object, Stream *p_stream, int &line, String &r_err_str, ResourceParser *p_res_parser) { @@ -1372,8 +1362,6 @@ Error VariantParser::_parse_dictionary(Dictionary &object, Stream *p_stream, int at_key = true; } } - - return OK; } Error VariantParser::_parse_tag(Token &token, Stream *p_stream, int &line, String &r_err_str, Tag &r_tag, ResourceParser *p_res_parser, bool p_simple_tag) { @@ -1557,8 +1545,6 @@ Error VariantParser::parse_tag_assign_eof(Stream *p_stream, int &line, String &r line++; } } - - return OK; } Error VariantParser::parse(Stream *p_stream, Variant &r_ret, String &r_err_str, int &r_err_line, ResourceParser *p_res_parser) { |