diff options
Diffstat (limited to 'core')
44 files changed, 563 insertions, 139 deletions
diff --git a/core/config/engine.cpp b/core/config/engine.cpp index 24080c056a..203f8c3882 100644 --- a/core/config/engine.cpp +++ b/core/config/engine.cpp @@ -110,7 +110,6 @@ Dictionary Engine::get_version_info() const { dict["hex"] = VERSION_HEX; dict["status"] = VERSION_STATUS; dict["build"] = VERSION_BUILD; - dict["year"] = VERSION_YEAR; String hash = String(VERSION_HASH); dict["hash"] = hash.is_empty() ? String("unknown") : hash; diff --git a/core/core_constants.cpp b/core/core_constants.cpp index 3b96fc20c6..aaabbabfd9 100644 --- a/core/core_constants.cpp +++ b/core/core_constants.cpp @@ -507,6 +507,10 @@ void register_global_constants() { BIND_CORE_BITFIELD_CLASS_FLAG(KeyModifierMask, KEY_MASK, KPAD); BIND_CORE_BITFIELD_CLASS_FLAG(KeyModifierMask, KEY_MASK, GROUP_SWITCH); + BIND_CORE_ENUM_CLASS_CONSTANT(KeyLocation, KEY_LOCATION, UNSPECIFIED); + BIND_CORE_ENUM_CLASS_CONSTANT(KeyLocation, KEY_LOCATION, LEFT); + BIND_CORE_ENUM_CLASS_CONSTANT(KeyLocation, KEY_LOCATION, RIGHT); + BIND_CORE_ENUM_CLASS_CONSTANT(MouseButton, MOUSE_BUTTON, NONE); BIND_CORE_ENUM_CLASS_CONSTANT(MouseButton, MOUSE_BUTTON, LEFT); BIND_CORE_ENUM_CLASS_CONSTANT(MouseButton, MOUSE_BUTTON, RIGHT); diff --git a/core/debugger/remote_debugger.cpp b/core/debugger/remote_debugger.cpp index ce675d6b06..d3b0039e72 100644 --- a/core/debugger/remote_debugger.cpp +++ b/core/debugger/remote_debugger.cpp @@ -36,6 +36,7 @@ #include "core/debugger/engine_profiler.h" #include "core/debugger/script_debugger.h" #include "core/input/input.h" +#include "core/io/resource_loader.h" #include "core/object/script_language.h" #include "core/os/os.h" @@ -513,8 +514,9 @@ void RemoteDebugger::debug(bool p_can_continue, bool p_is_error_breakpoint) { _send_stack_vars(globals, globals_vals, 2); } else if (command == "reload_scripts") { + script_paths_to_reload = data; + } else if (command == "reload_all_scripts") { reload_all_scripts = true; - } else if (command == "breakpoint") { ERR_FAIL_COND(data.size() < 3); bool set = data[2]; @@ -589,19 +591,36 @@ void RemoteDebugger::poll_events(bool p_is_idle) { } // Reload scripts during idle poll only. - if (p_is_idle && reload_all_scripts) { - for (int i = 0; i < ScriptServer::get_language_count(); i++) { - ScriptServer::get_language(i)->reload_all_scripts(); + if (p_is_idle) { + if (reload_all_scripts) { + for (int i = 0; i < ScriptServer::get_language_count(); i++) { + ScriptServer::get_language(i)->reload_all_scripts(); + } + reload_all_scripts = false; + } else if (!script_paths_to_reload.is_empty()) { + Array scripts_to_reload; + for (int i = 0; i < script_paths_to_reload.size(); ++i) { + String path = script_paths_to_reload[i]; + Error err = OK; + Ref<Script> script = ResourceLoader::load(path, "", ResourceFormatLoader::CACHE_MODE_REUSE, &err); + ERR_CONTINUE_MSG(err != OK, vformat("Could not reload script '%s': %s", path, error_names[err])); + ERR_CONTINUE_MSG(script.is_null(), vformat("Could not reload script '%s': Not a script!", path, error_names[err])); + scripts_to_reload.push_back(script); + } + for (int i = 0; i < ScriptServer::get_language_count(); i++) { + ScriptServer::get_language(i)->reload_scripts(scripts_to_reload, true); + } } - reload_all_scripts = false; + script_paths_to_reload.clear(); } } Error RemoteDebugger::_core_capture(const String &p_cmd, const Array &p_data, bool &r_captured) { r_captured = true; if (p_cmd == "reload_scripts") { + script_paths_to_reload = p_data; + } else if (p_cmd == "reload_all_scripts") { reload_all_scripts = true; - } else if (p_cmd == "breakpoint") { ERR_FAIL_COND_V(p_data.size() < 3, ERR_INVALID_DATA); bool set = p_data[2]; diff --git a/core/debugger/remote_debugger.h b/core/debugger/remote_debugger.h index 7c399178c6..519a90e7cc 100644 --- a/core/debugger/remote_debugger.h +++ b/core/debugger/remote_debugger.h @@ -74,6 +74,7 @@ private: int warn_count = 0; int last_reset = 0; bool reload_all_scripts = false; + Array script_paths_to_reload; // Make handlers and send_message thread safe. Mutex mutex; diff --git a/core/extension/gdextension.cpp b/core/extension/gdextension.cpp index ce01531b5c..2904e54b22 100644 --- a/core/extension/gdextension.cpp +++ b/core/extension/gdextension.cpp @@ -653,6 +653,8 @@ void GDExtension::_unregister_extension_class(GDExtensionClassLibraryPtr p_libra if (!ext->is_reloading) { self->extension_classes.erase(class_name); } + + GDExtensionEditorHelp::remove_class(class_name); #else self->extension_classes.erase(class_name); #endif @@ -1196,4 +1198,17 @@ void GDExtensionEditorPlugins::remove_extension_class(const StringName &p_class_ extension_classes.erase(p_class_name); } } + +GDExtensionEditorHelp::EditorHelpLoadXmlBufferFunc GDExtensionEditorHelp::editor_help_load_xml_buffer = nullptr; +GDExtensionEditorHelp::EditorHelpRemoveClassFunc GDExtensionEditorHelp::editor_help_remove_class = nullptr; + +void GDExtensionEditorHelp::load_xml_buffer(const uint8_t *p_buffer, int p_size) { + ERR_FAIL_NULL(editor_help_load_xml_buffer); + editor_help_load_xml_buffer(p_buffer, p_size); +} + +void GDExtensionEditorHelp::remove_class(const String &p_class) { + ERR_FAIL_NULL(editor_help_remove_class); + editor_help_remove_class(p_class); +} #endif // TOOLS_ENABLED diff --git a/core/extension/gdextension.h b/core/extension/gdextension.h index 0b39581751..588efed017 100644 --- a/core/extension/gdextension.h +++ b/core/extension/gdextension.h @@ -197,6 +197,26 @@ public: return extension_classes; } }; + +class GDExtensionEditorHelp { +protected: + friend class EditorHelp; + + // Similarly to EditorNode above, we need to be able to ask EditorHelp to parse + // new documentation data. Note though that, differently from EditorHelp, this + // is initialized even _before_ it gets instantiated, as we need to rely on + // this method while initializing the engine. + typedef void (*EditorHelpLoadXmlBufferFunc)(const uint8_t *p_buffer, int p_size); + static EditorHelpLoadXmlBufferFunc editor_help_load_xml_buffer; + + typedef void (*EditorHelpRemoveClassFunc)(const String &p_class); + static EditorHelpRemoveClassFunc editor_help_remove_class; + +public: + static void load_xml_buffer(const uint8_t *p_buffer, int p_size); + static void remove_class(const String &p_class); +}; + #endif // TOOLS_ENABLED #endif // GDEXTENSION_H diff --git a/core/extension/gdextension_interface.cpp b/core/extension/gdextension_interface.cpp index e02e7aa701..88572f24f0 100644 --- a/core/extension/gdextension_interface.cpp +++ b/core/extension/gdextension_interface.cpp @@ -42,6 +42,8 @@ #include "core/variant/variant.h" #include "core/version.h" +#include <string.h> + class CallableCustomExtension : public CallableCustom { void *userdata; void *token; @@ -1373,6 +1375,19 @@ static void gdextension_editor_remove_plugin(GDExtensionConstStringNamePtr p_cla #endif } +static void gdextension_editor_help_load_xml_from_utf8_chars_and_len(const char *p_data, GDExtensionInt p_size) { +#ifdef TOOLS_ENABLED + GDExtensionEditorHelp::load_xml_buffer((const uint8_t *)p_data, p_size); +#endif +} + +static void gdextension_editor_help_load_xml_from_utf8_chars(const char *p_data) { +#ifdef TOOLS_ENABLED + size_t len = strlen(p_data); + gdextension_editor_help_load_xml_from_utf8_chars_and_len(p_data, len); +#endif +} + #define REGISTER_INTERFACE_FUNC(m_name) GDExtension::register_interface_function(#m_name, (GDExtensionInterfaceFunctionPtr)&gdextension_##m_name) void gdextension_setup_interface() { @@ -1516,6 +1531,8 @@ void gdextension_setup_interface() { REGISTER_INTERFACE_FUNC(classdb_get_class_tag); REGISTER_INTERFACE_FUNC(editor_add_plugin); REGISTER_INTERFACE_FUNC(editor_remove_plugin); + REGISTER_INTERFACE_FUNC(editor_help_load_xml_from_utf8_chars); + REGISTER_INTERFACE_FUNC(editor_help_load_xml_from_utf8_chars_and_len); } #undef REGISTER_INTERFACE_FUNCTION diff --git a/core/extension/gdextension_interface.h b/core/extension/gdextension_interface.h index d58f0226d8..8fda11c651 100644 --- a/core/extension/gdextension_interface.h +++ b/core/extension/gdextension_interface.h @@ -2617,6 +2617,31 @@ typedef void (*GDExtensionInterfaceEditorAddPlugin)(GDExtensionConstStringNamePt */ typedef void (*GDExtensionInterfaceEditorRemovePlugin)(GDExtensionConstStringNamePtr p_class_name); +/** + * @name editor_help_load_xml_from_utf8_chars + * @since 4.3 + * + * Loads new XML-formatted documentation data in the editor. + * + * The provided pointer can be immediately freed once the function returns. + * + * @param p_data A pointer to an UTF-8 encoded C string (null terminated). + */ +typedef void (*GDExtensionsInterfaceEditorHelpLoadXmlFromUtf8Chars)(const char *p_data); + +/** + * @name editor_help_load_xml_from_utf8_chars_and_len + * @since 4.3 + * + * Loads new XML-formatted documentation data in the editor. + * + * The provided pointer can be immediately freed once the function returns. + * + * @param p_data A pointer to an UTF-8 encoded C string. + * @param p_size The number of bytes (not code units). + */ +typedef void (*GDExtensionsInterfaceEditorHelpLoadXmlFromUtf8CharsAndLen)(const char *p_data, GDExtensionInt p_size); + #ifdef __cplusplus } #endif diff --git a/core/input/input.cpp b/core/input/input.cpp index 2ba4b1d1e8..4e33d3087d 100644 --- a/core/input/input.cpp +++ b/core/input/input.cpp @@ -865,6 +865,8 @@ Point2i Input::warp_mouse_motion(const Ref<InputEventMouseMotion> &p_motion, con } void Input::action_press(const StringName &p_action, float p_strength) { + ERR_FAIL_COND_MSG(!InputMap::get_singleton()->has_action(p_action), InputMap::get_singleton()->suggest_actions(p_action)); + // Create or retrieve existing action. ActionState &action_state = action_states[p_action]; @@ -879,6 +881,8 @@ void Input::action_press(const StringName &p_action, float p_strength) { } void Input::action_release(const StringName &p_action) { + ERR_FAIL_COND_MSG(!InputMap::get_singleton()->has_action(p_action), InputMap::get_singleton()->suggest_actions(p_action)); + // Create or retrieve existing action. ActionState &action_state = action_states[p_action]; action_state.cache.pressed = 0; diff --git a/core/input/input_builders.py b/core/input/input_builders.py index e98e2441e2..94c566493e 100644 --- a/core/input/input_builders.py +++ b/core/input/input_builders.py @@ -45,10 +45,10 @@ def make_default_controller_mappings(target, source, env): platform_mappings[current_platform][guid] = line platform_variables = { - "Linux": "#if LINUXBSD_ENABLED", + "Linux": "#ifdef LINUXBSD_ENABLED", "Windows": "#ifdef WINDOWS_ENABLED", "Mac OS X": "#ifdef MACOS_ENABLED", - "Android": "#if defined(__ANDROID__)", + "Android": "#ifdef ANDROID_ENABLED", "iOS": "#ifdef IOS_ENABLED", "Web": "#ifdef WEB_ENABLED", } diff --git a/core/input/input_event.cpp b/core/input/input_event.cpp index e99dd04599..89ffcecf50 100644 --- a/core/input/input_event.cpp +++ b/core/input/input_event.cpp @@ -364,6 +364,15 @@ char32_t InputEventKey::get_unicode() const { return unicode; } +void InputEventKey::set_location(KeyLocation p_key_location) { + location = p_key_location; + emit_changed(); +} + +KeyLocation InputEventKey::get_location() const { + return location; +} + void InputEventKey::set_echo(bool p_enable) { echo = p_enable; emit_changed(); @@ -436,6 +445,23 @@ String InputEventKey::as_text_key_label() const { return mods_text.is_empty() ? kc : mods_text + "+" + kc; } +String InputEventKey::as_text_location() const { + String loc; + + switch (location) { + case KeyLocation::LEFT: + loc = "left"; + break; + case KeyLocation::RIGHT: + loc = "right"; + break; + default: + break; + } + + return loc; +} + String InputEventKey::as_text() const { String kc; @@ -464,6 +490,11 @@ String InputEventKey::to_string() { String kc = ""; String physical = "false"; + String loc = as_text_location(); + if (loc.is_empty()) { + loc = "unspecified"; + } + if (keycode == Key::NONE && physical_keycode == Key::NONE && unicode != 0) { kc = "U+" + String::num_uint64(unicode, 16) + " (" + String::chr(unicode) + ")"; } else if (keycode != Key::NONE) { @@ -478,7 +509,7 @@ String InputEventKey::to_string() { String mods = InputEventWithModifiers::as_text(); mods = mods.is_empty() ? "none" : mods; - return vformat("InputEventKey: keycode=%s, mods=%s, physical=%s, pressed=%s, echo=%s", kc, mods, physical, p, e); + return vformat("InputEventKey: keycode=%s, mods=%s, physical=%s, location=%s, pressed=%s, echo=%s", kc, mods, physical, loc, p, e); } Ref<InputEventKey> InputEventKey::create_reference(Key p_keycode, bool p_physical) { @@ -531,6 +562,9 @@ bool InputEventKey::action_match(const Ref<InputEvent> &p_event, bool p_exact_ma match = keycode == key->keycode; } else if (physical_keycode != Key::NONE) { match = physical_keycode == key->physical_keycode; + if (location != KeyLocation::UNSPECIFIED) { + match &= location == key->location; + } } else { match = false; } @@ -572,6 +606,9 @@ bool InputEventKey::is_match(const Ref<InputEvent> &p_event, bool p_exact_match) return (keycode == key->keycode) && (!p_exact_match || get_modifiers_mask() == key->get_modifiers_mask()); } else if (physical_keycode != Key::NONE) { + if (location != KeyLocation::UNSPECIFIED && location != key->location) { + return false; + } return (physical_keycode == key->physical_keycode) && (!p_exact_match || get_modifiers_mask() == key->get_modifiers_mask()); } else { @@ -594,6 +631,9 @@ void InputEventKey::_bind_methods() { ClassDB::bind_method(D_METHOD("set_unicode", "unicode"), &InputEventKey::set_unicode); ClassDB::bind_method(D_METHOD("get_unicode"), &InputEventKey::get_unicode); + ClassDB::bind_method(D_METHOD("set_location", "location"), &InputEventKey::set_location); + ClassDB::bind_method(D_METHOD("get_location"), &InputEventKey::get_location); + ClassDB::bind_method(D_METHOD("set_echo", "echo"), &InputEventKey::set_echo); ClassDB::bind_method(D_METHOD("get_keycode_with_modifiers"), &InputEventKey::get_keycode_with_modifiers); @@ -603,12 +643,14 @@ void InputEventKey::_bind_methods() { ClassDB::bind_method(D_METHOD("as_text_keycode"), &InputEventKey::as_text_keycode); ClassDB::bind_method(D_METHOD("as_text_physical_keycode"), &InputEventKey::as_text_physical_keycode); ClassDB::bind_method(D_METHOD("as_text_key_label"), &InputEventKey::as_text_key_label); + ClassDB::bind_method(D_METHOD("as_text_location"), &InputEventKey::as_text_location); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed"); ADD_PROPERTY(PropertyInfo(Variant::INT, "keycode"), "set_keycode", "get_keycode"); ADD_PROPERTY(PropertyInfo(Variant::INT, "physical_keycode"), "set_physical_keycode", "get_physical_keycode"); ADD_PROPERTY(PropertyInfo(Variant::INT, "key_label"), "set_key_label", "get_key_label"); ADD_PROPERTY(PropertyInfo(Variant::INT, "unicode"), "set_unicode", "get_unicode"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "location", PROPERTY_HINT_ENUM, "Unspecified,Left,Right"), "set_location", "get_location"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "echo"), "set_echo", "is_echo"); } diff --git a/core/input/input_event.h b/core/input/input_event.h index ed7ccf0a9f..61a53116e9 100644 --- a/core/input/input_event.h +++ b/core/input/input_event.h @@ -157,6 +157,7 @@ class InputEventKey : public InputEventWithModifiers { Key physical_keycode = Key::NONE; Key key_label = Key::NONE; uint32_t unicode = 0; ///unicode + KeyLocation location = KeyLocation::UNSPECIFIED; bool echo = false; /// true if this is an echo key @@ -178,6 +179,9 @@ public: void set_unicode(char32_t p_unicode); char32_t get_unicode() const; + void set_location(KeyLocation p_key_location); + KeyLocation get_location() const; + void set_echo(bool p_enable); virtual bool is_echo() const override; @@ -193,6 +197,7 @@ public: virtual String as_text_physical_keycode() const; virtual String as_text_keycode() const; virtual String as_text_key_label() const; + virtual String as_text_location() const; virtual String as_text() const override; virtual String to_string() override; diff --git a/core/io/file_access_memory.cpp b/core/io/file_access_memory.cpp index 0983920b94..da02c883e8 100644 --- a/core/io/file_access_memory.cpp +++ b/core/io/file_access_memory.cpp @@ -119,7 +119,7 @@ uint64_t FileAccessMemory::get_length() const { } bool FileAccessMemory::eof_reached() const { - return pos > length; + return pos >= length; } uint8_t FileAccessMemory::get_8() const { diff --git a/core/io/packet_peer.cpp b/core/io/packet_peer.cpp index 48806fba73..0329ace313 100644 --- a/core/io/packet_peer.cpp +++ b/core/io/packet_peer.cpp @@ -318,9 +318,9 @@ int PacketPeerStream::get_output_buffer_max_size() const { } PacketPeerStream::PacketPeerStream() { - int rbsize = GLOBAL_GET("network/limits/packet_peer_stream/max_buffer_po2"); + int64_t rbsize = GLOBAL_GET("network/limits/packet_peer_stream/max_buffer_po2"); ring_buffer.resize(rbsize); - input_buffer.resize(1 << rbsize); - output_buffer.resize(1 << rbsize); + input_buffer.resize(int64_t(1) << rbsize); + output_buffer.resize(int64_t(1) << rbsize); } diff --git a/core/math/basis.cpp b/core/math/basis.cpp index cd8c87b158..1ff6cdd588 100644 --- a/core/math/basis.cpp +++ b/core/math/basis.cpp @@ -722,7 +722,7 @@ Basis::operator String() const { Quaternion Basis::get_quaternion() const { #ifdef MATH_CHECKS - ERR_FAIL_COND_V_MSG(!is_rotation(), Quaternion(), "Basis must be normalized in order to be casted to a Quaternion. Use get_rotation_quaternion() or call orthonormalized() if the Basis contains linearly independent vectors."); + ERR_FAIL_COND_V_MSG(!is_rotation(), Quaternion(), "Basis " + operator String() + " must be normalized in order to be casted to a Quaternion. Use get_rotation_quaternion() or call orthonormalized() if the Basis contains linearly independent vectors."); #endif /* Allow getting a quaternion from an unnormalized transform */ Basis m = *this; @@ -849,7 +849,7 @@ void Basis::set_quaternion(const Quaternion &p_quaternion) { void Basis::set_axis_angle(const Vector3 &p_axis, real_t p_angle) { // Rotation matrix from axis and angle, see https://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_angle #ifdef MATH_CHECKS - ERR_FAIL_COND_MSG(!p_axis.is_normalized(), "The axis Vector3 must be normalized."); + ERR_FAIL_COND_MSG(!p_axis.is_normalized(), "The axis Vector3 " + p_axis.operator String() + " must be normalized."); #endif Vector3 axis_sq(p_axis.x * p_axis.x, p_axis.y * p_axis.y, p_axis.z * p_axis.z); real_t cosine = Math::cos(p_angle); diff --git a/core/math/geometry_2d.h b/core/math/geometry_2d.h index b37fce9e9c..9907d579a5 100644 --- a/core/math/geometry_2d.h +++ b/core/math/geometry_2d.h @@ -119,6 +119,10 @@ public: } } + static real_t get_distance_to_segment(const Vector2 &p_point, const Vector2 *p_segment) { + return p_point.distance_to(get_closest_point_to_segment(p_point, p_segment)); + } + static bool is_point_in_triangle(const Vector2 &s, const Vector2 &a, const Vector2 &b, const Vector2 &c) { Vector2 an = a - s; Vector2 bn = b - s; @@ -249,6 +253,28 @@ public: return -1; } + static bool segment_intersects_rect(const Vector2 &p_from, const Vector2 &p_to, const Rect2 &p_rect) { + if (p_rect.has_point(p_from) || p_rect.has_point(p_to)) { + return true; + } + + const Vector2 rect_points[4] = { + p_rect.position, + p_rect.position + Vector2(p_rect.size.x, 0), + p_rect.position + p_rect.size, + p_rect.position + Vector2(0, p_rect.size.y) + }; + + // Check if any of the rect's edges intersect the segment. + for (int i = 0; i < 4; i++) { + if (segment_intersects_segment(p_from, p_to, rect_points[i], rect_points[(i + 1) % 4], nullptr)) { + return true; + } + } + + return false; + } + enum PolyBooleanOperation { OPERATION_UNION, OPERATION_DIFFERENCE, diff --git a/core/math/quaternion.cpp b/core/math/quaternion.cpp index e4ad17c8ef..cbaaa1371a 100644 --- a/core/math/quaternion.cpp +++ b/core/math/quaternion.cpp @@ -41,7 +41,7 @@ real_t Quaternion::angle_to(const Quaternion &p_to) const { Vector3 Quaternion::get_euler(EulerOrder p_order) const { #ifdef MATH_CHECKS - ERR_FAIL_COND_V_MSG(!is_normalized(), Vector3(0, 0, 0), "The quaternion must be normalized."); + ERR_FAIL_COND_V_MSG(!is_normalized(), Vector3(0, 0, 0), "The quaternion " + operator String() + " must be normalized."); #endif return Basis(*this).get_euler(p_order); } @@ -88,7 +88,7 @@ bool Quaternion::is_normalized() const { Quaternion Quaternion::inverse() const { #ifdef MATH_CHECKS - ERR_FAIL_COND_V_MSG(!is_normalized(), Quaternion(), "The quaternion must be normalized."); + ERR_FAIL_COND_V_MSG(!is_normalized(), Quaternion(), "The quaternion " + operator String() + " must be normalized."); #endif return Quaternion(-x, -y, -z, w); } @@ -112,8 +112,8 @@ Quaternion Quaternion::exp() const { Quaternion Quaternion::slerp(const Quaternion &p_to, const real_t &p_weight) const { #ifdef MATH_CHECKS - ERR_FAIL_COND_V_MSG(!is_normalized(), Quaternion(), "The start quaternion must be normalized."); - ERR_FAIL_COND_V_MSG(!p_to.is_normalized(), Quaternion(), "The end quaternion must be normalized."); + ERR_FAIL_COND_V_MSG(!is_normalized(), Quaternion(), "The start quaternion " + operator String() + " must be normalized."); + ERR_FAIL_COND_V_MSG(!p_to.is_normalized(), Quaternion(), "The end quaternion " + p_to.operator String() + " must be normalized."); #endif Quaternion to1; real_t omega, cosom, sinom, scale0, scale1; @@ -153,8 +153,8 @@ Quaternion Quaternion::slerp(const Quaternion &p_to, const real_t &p_weight) con Quaternion Quaternion::slerpni(const Quaternion &p_to, const real_t &p_weight) const { #ifdef MATH_CHECKS - ERR_FAIL_COND_V_MSG(!is_normalized(), Quaternion(), "The start quaternion must be normalized."); - ERR_FAIL_COND_V_MSG(!p_to.is_normalized(), Quaternion(), "The end quaternion must be normalized."); + ERR_FAIL_COND_V_MSG(!is_normalized(), Quaternion(), "The start quaternion " + operator String() + " must be normalized."); + ERR_FAIL_COND_V_MSG(!p_to.is_normalized(), Quaternion(), "The end quaternion " + p_to.operator String() + " must be normalized."); #endif const Quaternion &from = *this; @@ -177,8 +177,8 @@ Quaternion Quaternion::slerpni(const Quaternion &p_to, const real_t &p_weight) c Quaternion Quaternion::spherical_cubic_interpolate(const Quaternion &p_b, const Quaternion &p_pre_a, const Quaternion &p_post_b, const real_t &p_weight) const { #ifdef MATH_CHECKS - ERR_FAIL_COND_V_MSG(!is_normalized(), Quaternion(), "The start quaternion must be normalized."); - ERR_FAIL_COND_V_MSG(!p_b.is_normalized(), Quaternion(), "The end quaternion must be normalized."); + ERR_FAIL_COND_V_MSG(!is_normalized(), Quaternion(), "The start quaternion " + operator String() + " must be normalized."); + ERR_FAIL_COND_V_MSG(!p_b.is_normalized(), Quaternion(), "The end quaternion " + p_b.operator String() + " must be normalized."); #endif Quaternion from_q = *this; Quaternion pre_q = p_pre_a; @@ -228,8 +228,8 @@ Quaternion Quaternion::spherical_cubic_interpolate(const Quaternion &p_b, const Quaternion Quaternion::spherical_cubic_interpolate_in_time(const Quaternion &p_b, const Quaternion &p_pre_a, const Quaternion &p_post_b, const real_t &p_weight, const real_t &p_b_t, const real_t &p_pre_a_t, const real_t &p_post_b_t) const { #ifdef MATH_CHECKS - ERR_FAIL_COND_V_MSG(!is_normalized(), Quaternion(), "The start quaternion must be normalized."); - ERR_FAIL_COND_V_MSG(!p_b.is_normalized(), Quaternion(), "The end quaternion must be normalized."); + ERR_FAIL_COND_V_MSG(!is_normalized(), Quaternion(), "The start quaternion " + operator String() + " must be normalized."); + ERR_FAIL_COND_V_MSG(!p_b.is_normalized(), Quaternion(), "The end quaternion " + p_b.operator String() + " must be normalized."); #endif Quaternion from_q = *this; Quaternion pre_q = p_pre_a; @@ -294,7 +294,7 @@ real_t Quaternion::get_angle() const { Quaternion::Quaternion(const Vector3 &p_axis, real_t p_angle) { #ifdef MATH_CHECKS - ERR_FAIL_COND_MSG(!p_axis.is_normalized(), "The axis Vector3 must be normalized."); + ERR_FAIL_COND_MSG(!p_axis.is_normalized(), "The axis Vector3 " + p_axis.operator String() + " must be normalized."); #endif real_t d = p_axis.length(); if (d == 0) { diff --git a/core/math/quaternion.h b/core/math/quaternion.h index ea952304a5..f8133df559 100644 --- a/core/math/quaternion.h +++ b/core/math/quaternion.h @@ -33,8 +33,7 @@ #include "core/math/math_funcs.h" #include "core/math/vector3.h" - -class String; +#include "core/string/ustring.h" struct _NO_DISCARD_ Quaternion { union { @@ -90,7 +89,7 @@ struct _NO_DISCARD_ Quaternion { _FORCE_INLINE_ Vector3 xform(const Vector3 &v) const { #ifdef MATH_CHECKS - ERR_FAIL_COND_V_MSG(!is_normalized(), v, "The quaternion must be normalized."); + ERR_FAIL_COND_V_MSG(!is_normalized(), v, "The quaternion " + operator String() + " must be normalized."); #endif Vector3 u(x, y, z); Vector3 uv = u.cross(v); diff --git a/core/math/vector2.cpp b/core/math/vector2.cpp index df8c804243..9982415b97 100644 --- a/core/math/vector2.cpp +++ b/core/math/vector2.cpp @@ -162,7 +162,7 @@ Vector2 Vector2::move_toward(const Vector2 &p_to, const real_t p_delta) const { // slide returns the component of the vector along the given plane, specified by its normal vector. Vector2 Vector2::slide(const Vector2 &p_normal) const { #ifdef MATH_CHECKS - ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector2(), "The normal Vector2 must be normalized."); + ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector2(), "The normal Vector2 " + p_normal.operator String() + "must be normalized."); #endif return *this - p_normal * this->dot(p_normal); } @@ -173,7 +173,7 @@ Vector2 Vector2::bounce(const Vector2 &p_normal) const { Vector2 Vector2::reflect(const Vector2 &p_normal) const { #ifdef MATH_CHECKS - ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector2(), "The normal Vector2 must be normalized."); + ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector2(), "The normal Vector2 " + p_normal.operator String() + "must be normalized."); #endif return 2.0f * p_normal * this->dot(p_normal) - *this; } diff --git a/core/math/vector3.h b/core/math/vector3.h index 18943a820f..7551b21493 100644 --- a/core/math/vector3.h +++ b/core/math/vector3.h @@ -33,8 +33,8 @@ #include "core/error/error_macros.h" #include "core/math/math_funcs.h" +#include "core/string/ustring.h" -class String; struct Basis; struct Vector2; struct Vector3i; @@ -512,7 +512,7 @@ void Vector3::zero() { // slide returns the component of the vector along the given plane, specified by its normal vector. Vector3 Vector3::slide(const Vector3 &p_normal) const { #ifdef MATH_CHECKS - ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector3(), "The normal Vector3 must be normalized."); + ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector3(), "The normal Vector3 " + p_normal.operator String() + " must be normalized."); #endif return *this - p_normal * this->dot(p_normal); } @@ -523,7 +523,7 @@ Vector3 Vector3::bounce(const Vector3 &p_normal) const { Vector3 Vector3::reflect(const Vector3 &p_normal) const { #ifdef MATH_CHECKS - ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector3(), "The normal Vector3 must be normalized."); + ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector3(), "The normal Vector3 " + p_normal.operator String() + " must be normalized."); #endif return 2.0f * p_normal * this->dot(p_normal) - *this; } diff --git a/core/object/class_db.cpp b/core/object/class_db.cpp index bf1bd0de93..45fbb19f88 100644 --- a/core/object/class_db.cpp +++ b/core/object/class_db.cpp @@ -31,6 +31,7 @@ #include "class_db.h" #include "core/config/engine.h" +#include "core/core_string_names.h" #include "core/io/resource_loader.h" #include "core/object/script_language.h" #include "core/os/mutex.h" @@ -1299,6 +1300,12 @@ bool ClassDB::get_property(Object *p_object, const StringName &p_property, Varia check = check->inherits_ptr; } + // The "free()" method is special, so we assume it exists and return a Callable. + if (p_property == CoreStringNames::get_singleton()->_free) { + r_value = Callable(p_object, p_property); + return true; + } + return false; } diff --git a/core/object/object.cpp b/core/object/object.cpp index 5a776e2106..3901c4835d 100644 --- a/core/object/object.cpp +++ b/core/object/object.cpp @@ -728,7 +728,7 @@ Variant Object::callp(const StringName &p_method, const Variant **p_args, int p_ r_error.expected = 0; return Variant(); } - if (Object::cast_to<RefCounted>(this)) { + if (is_ref_counted()) { r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; ERR_FAIL_V_MSG(Variant(), "Can't 'free' a reference."); } diff --git a/core/object/script_language.h b/core/object/script_language.h index 66106bf139..bb714d5bc3 100644 --- a/core/object/script_language.h +++ b/core/object/script_language.h @@ -371,6 +371,7 @@ public: virtual Vector<StackInfo> debug_get_current_stack_info() { return Vector<StackInfo>(); } virtual void reload_all_scripts() = 0; + virtual void reload_scripts(const Array &p_scripts, bool p_soft_reload) = 0; virtual void reload_tool_script(const Ref<Script> &p_script, bool p_soft_reload) = 0; /* LOADER FUNCTIONS */ diff --git a/core/object/script_language_extension.h b/core/object/script_language_extension.h index 8b01667519..5b10739486 100644 --- a/core/object/script_language_extension.h +++ b/core/object/script_language_extension.h @@ -562,6 +562,7 @@ public: } EXBIND0(reload_all_scripts) + EXBIND2(reload_scripts, const Array &, bool) EXBIND2(reload_tool_script, const Ref<Script> &, bool) /* LOADER FUNCTIONS */ diff --git a/core/object/undo_redo.cpp b/core/object/undo_redo.cpp index 3c1d4ef95e..569e6ae19f 100644 --- a/core/object/undo_redo.cpp +++ b/core/object/undo_redo.cpp @@ -316,6 +316,14 @@ void UndoRedo::commit_action(bool p_execute) { _redo(p_execute); // perform action committing--; + if (max_steps > 0) { + // Clear early steps. + + while (actions.size() > max_steps) { + _pop_history_tail(); + } + } + if (add_message && callback && actions.size() > 0) { callback(callback_ud, actions[actions.size() - 1].name); } @@ -473,6 +481,14 @@ uint64_t UndoRedo::get_version() const { return version; } +void UndoRedo::set_max_steps(int p_max_steps) { + max_steps = p_max_steps; +} + +int UndoRedo::get_max_steps() const { + return max_steps; +} + void UndoRedo::set_commit_notify_callback(CommitNotifyCallback p_callback, void *p_ud) { callback = p_callback; callback_ud = p_ud; @@ -517,9 +533,13 @@ void UndoRedo::_bind_methods() { 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("set_max_steps", "max_steps"), &UndoRedo::set_max_steps); + ClassDB::bind_method(D_METHOD("get_max_steps"), &UndoRedo::get_max_steps); ClassDB::bind_method(D_METHOD("redo"), &UndoRedo::redo); ClassDB::bind_method(D_METHOD("undo"), &UndoRedo::undo); + ADD_PROPERTY(PropertyInfo(Variant::INT, "max_steps", PROPERTY_HINT_RANGE, "0,50,1,or_greater"), "set_max_steps", "get_max_steps"); + ADD_SIGNAL(MethodInfo("version_changed")); BIND_ENUM_CONSTANT(MERGE_DISABLE); diff --git a/core/object/undo_redo.h b/core/object/undo_redo.h index b3a3322e4b..62aebff877 100644 --- a/core/object/undo_redo.h +++ b/core/object/undo_redo.h @@ -80,6 +80,7 @@ private: int current_action = -1; bool force_keep_in_merge_ends = false; int action_level = 0; + int max_steps = 0; MergeMode merge_mode = MERGE_DISABLE; bool merging = false; uint64_t version = 1; @@ -135,6 +136,9 @@ public: uint64_t get_version() const; + void set_max_steps(int p_max_steps); + int get_max_steps() const; + void set_commit_notify_callback(CommitNotifyCallback p_callback, void *p_ud); void set_method_notify_callback(MethodNotifyCallback p_method_callback, void *p_ud); diff --git a/core/object/worker_thread_pool.cpp b/core/object/worker_thread_pool.cpp index 8e8a2ef06b..e2ab473b01 100644 --- a/core/object/worker_thread_pool.cpp +++ b/core/object/worker_thread_pool.cpp @@ -47,6 +47,7 @@ WorkerThreadPool *WorkerThreadPool::singleton = nullptr; thread_local CommandQueueMT *WorkerThreadPool::flushing_cmd_queue = nullptr; void WorkerThreadPool::_process_task(Task *p_task) { +#ifdef THREADS_ENABLED int pool_thread_index = thread_ids[Thread::get_caller_id()]; ThreadData &curr_thread = threads[pool_thread_index]; Task *prev_task = nullptr; // In case this is recursively called. @@ -69,6 +70,7 @@ void WorkerThreadPool::_process_task(Task *p_task) { curr_thread.current_task = p_task; task_mutex.unlock(); } +#endif if (p_task->group) { // Handling a group @@ -143,6 +145,7 @@ void WorkerThreadPool::_process_task(Task *p_task) { } } +#ifdef THREADS_ENABLED { curr_thread.current_task = prev_task; if (p_task->low_priority) { @@ -159,6 +162,7 @@ void WorkerThreadPool::_process_task(Task *p_task) { } set_current_thread_safe_for_nodes(safe_for_nodes_backup); +#endif } void WorkerThreadPool::_thread_function(void *p_user) { @@ -542,6 +546,7 @@ bool WorkerThreadPool::is_group_task_completed(GroupID p_group) const { } void WorkerThreadPool::wait_for_group_task_completion(GroupID p_group) { +#ifdef THREADS_ENABLED task_mutex.lock(); Group **groupp = groups.getptr(p_group); task_mutex.unlock(); @@ -574,6 +579,7 @@ void WorkerThreadPool::wait_for_group_task_completion(GroupID p_group) { task_mutex.lock(); // This mutex is needed when Physics 2D and/or 3D is selected to run on a separate thread. groups.erase(p_group); task_mutex.unlock(); +#endif } int WorkerThreadPool::get_thread_index() { diff --git a/core/os/condition_variable.h b/core/os/condition_variable.h index 6a49ced31b..2b6b272e18 100644 --- a/core/os/condition_variable.h +++ b/core/os/condition_variable.h @@ -33,6 +33,8 @@ #include "core/os/mutex.h" +#ifdef THREADS_ENABLED + #ifdef MINGW_ENABLED #define MINGW_STDTHREAD_REDUNDANCY_WARNING #include "thirdparty/mingw-std-threads/mingw.condition_variable.h" @@ -66,4 +68,16 @@ public: } }; +#else // No threads. + +class ConditionVariable { +public: + template <class BinaryMutexT> + void wait(const MutexLock<BinaryMutexT> &p_lock) const {} + void notify_one() const {} + void notify_all() const {} +}; + +#endif // THREADS_ENABLED + #endif // CONDITION_VARIABLE_H diff --git a/core/os/keyboard.h b/core/os/keyboard.h index 785972d31d..2051973336 100644 --- a/core/os/keyboard.h +++ b/core/os/keyboard.h @@ -260,6 +260,12 @@ enum class KeyModifierMask { GROUP_SWITCH = (1 << 30) }; +enum class KeyLocation { + UNSPECIFIED, + LEFT, + RIGHT +}; + // To avoid having unnecessary operators, only define the ones that are needed. constexpr Key operator-(uint32_t a, Key b) { diff --git a/core/os/mutex.cpp b/core/os/mutex.cpp index 5d4e457c5f..9a8a2a2961 100644 --- a/core/os/mutex.cpp +++ b/core/os/mutex.cpp @@ -40,7 +40,11 @@ void _global_unlock() { _global_mutex.unlock(); } +#ifdef THREADS_ENABLED + template class MutexImpl<THREADING_NAMESPACE::recursive_mutex>; template class MutexImpl<THREADING_NAMESPACE::mutex>; template class MutexLock<MutexImpl<THREADING_NAMESPACE::recursive_mutex>>; template class MutexLock<MutexImpl<THREADING_NAMESPACE::mutex>>; + +#endif diff --git a/core/os/mutex.h b/core/os/mutex.h index 03af48ca7c..69f494d9cd 100644 --- a/core/os/mutex.h +++ b/core/os/mutex.h @@ -43,6 +43,8 @@ #define THREADING_NAMESPACE std #endif +#ifdef THREADS_ENABLED + template <class MutexT> class MutexLock; @@ -125,8 +127,8 @@ class MutexLock { THREADING_NAMESPACE::unique_lock<typename MutexT::StdMutexType> lock; public: - _ALWAYS_INLINE_ explicit MutexLock(const MutexT &p_mutex) : - lock(p_mutex.mutex){}; + explicit MutexLock(const MutexT &p_mutex) : + lock(p_mutex.mutex) {} }; // This specialization is needed so manual locking and MutexLock can be used @@ -155,4 +157,38 @@ extern template class MutexImpl<THREADING_NAMESPACE::mutex>; extern template class MutexLock<MutexImpl<THREADING_NAMESPACE::recursive_mutex>>; extern template class MutexLock<MutexImpl<THREADING_NAMESPACE::mutex>>; +#else // No threads. + +class MutexImpl { + mutable THREADING_NAMESPACE::mutex mutex; + +public: + void lock() const {} + void unlock() const {} + bool try_lock() const { return true; } +}; + +template <int Tag> +class SafeBinaryMutex : public MutexImpl { + static thread_local uint32_t count; +}; + +template <class MutexT> +class MutexLock { +public: + MutexLock(const MutexT &p_mutex) {} +}; + +template <int Tag> +class MutexLock<SafeBinaryMutex<Tag>> { +public: + MutexLock(const SafeBinaryMutex<Tag> &p_mutex) {} + ~MutexLock() {} +}; + +using Mutex = MutexImpl; +using BinaryMutex = MutexImpl; + +#endif // THREADS_ENABLED + #endif // MUTEX_H diff --git a/core/os/os.cpp b/core/os/os.cpp index 26ae286979..d5d9988cc1 100644 --- a/core/os/os.cpp +++ b/core/os/os.cpp @@ -504,6 +504,12 @@ bool OS::has_feature(const String &p_feature) { } #endif +#ifdef THREADS_ENABLED + if (p_feature == "threads") { + return true; + } +#endif + if (_check_internal_feature_support(p_feature)) { return true; } diff --git a/core/os/semaphore.h b/core/os/semaphore.h index b8ae35b86b..19ef1dedc0 100644 --- a/core/os/semaphore.h +++ b/core/os/semaphore.h @@ -31,6 +31,10 @@ #ifndef SEMAPHORE_H #define SEMAPHORE_H +#include <cstdint> + +#ifdef THREADS_ENABLED + #include "core/error/error_list.h" #include "core/typedefs.h" #ifdef DEBUG_ENABLED @@ -132,4 +136,17 @@ public: #endif }; +#else // No threads. + +class Semaphore { +public: + void post(uint32_t p_count = 1) const {} + void wait() const {} + bool try_wait() const { + return true; + } +}; + +#endif // THREADS_ENABLED + #endif // SEMAPHORE_H diff --git a/core/os/spin_lock.h b/core/os/spin_lock.h index 93ea782b60..d386cd5890 100644 --- a/core/os/spin_lock.h +++ b/core/os/spin_lock.h @@ -33,6 +33,25 @@ #include "core/typedefs.h" +#if defined(__APPLE__) + +#include <os/lock.h> + +class SpinLock { + mutable os_unfair_lock _lock = OS_UNFAIR_LOCK_INIT; + +public: + _ALWAYS_INLINE_ void lock() const { + os_unfair_lock_lock(&_lock); + } + + _ALWAYS_INLINE_ void unlock() const { + os_unfair_lock_unlock(&_lock); + } +}; + +#else + #include <atomic> class SpinLock { @@ -49,4 +68,6 @@ public: } }; +#endif // __APPLE__ + #endif // SPIN_LOCK_H diff --git a/core/os/thread.cpp b/core/os/thread.cpp index 2ba90ba42c..afc74364f6 100644 --- a/core/os/thread.cpp +++ b/core/os/thread.cpp @@ -33,19 +33,22 @@ #include "thread.h" +#ifdef THREADS_ENABLED #include "core/object/script_language.h" #include "core/templates/safe_refcount.h" -Thread::PlatformFunctions Thread::platform_functions; - SafeNumeric<uint64_t> Thread::id_counter(1); // The first value after .increment() is 2, hence by default the main thread ID should be 1. thread_local Thread::ID Thread::caller_id = Thread::UNASSIGNED_ID; +#endif + +Thread::PlatformFunctions Thread::platform_functions; void Thread::_set_platform_functions(const PlatformFunctions &p_functions) { platform_functions = p_functions; } +#ifdef THREADS_ENABLED void Thread::callback(ID p_caller_id, const Settings &p_settings, Callback p_callback, void *p_userdata) { Thread::caller_id = p_caller_id; if (platform_functions.set_priority) { @@ -107,4 +110,6 @@ Thread::~Thread() { } } +#endif // THREADS_ENABLED + #endif // PLATFORM_THREAD_OVERRIDE diff --git a/core/os/thread.h b/core/os/thread.h index cc954678f9..a0ecc24c91 100644 --- a/core/os/thread.h +++ b/core/os/thread.h @@ -53,6 +53,8 @@ class String; +#ifdef THREADS_ENABLED + class Thread { public: typedef void (*Callback)(void *p_userdata); @@ -86,6 +88,8 @@ public: private: friend class Main; + static PlatformFunctions platform_functions; + ID id = UNASSIGNED_ID; static SafeNumeric<uint64_t> id_counter; static thread_local ID caller_id; @@ -93,8 +97,6 @@ private: static void callback(ID p_caller_id, const Settings &p_settings, Thread::Callback p_callback, void *p_userdata); - static PlatformFunctions platform_functions; - static void make_main_thread() { caller_id = MAIN_ID; } static void release_main_thread() { caller_id = UNASSIGNED_ID; } @@ -125,6 +127,64 @@ public: ~Thread(); }; +#else // No threads. + +class Thread { +public: + typedef void (*Callback)(void *p_userdata); + + typedef uint64_t ID; + + enum : ID { + UNASSIGNED_ID = 0, + MAIN_ID = 1 + }; + + enum Priority { + PRIORITY_LOW, + PRIORITY_NORMAL, + PRIORITY_HIGH + }; + + struct Settings { + Priority priority; + Settings() { priority = PRIORITY_NORMAL; } + }; + + struct PlatformFunctions { + Error (*set_name)(const String &) = nullptr; + void (*set_priority)(Thread::Priority) = nullptr; + void (*init)() = nullptr; + void (*wrapper)(Thread::Callback, void *) = nullptr; + void (*term)() = nullptr; + }; + +private: + friend class Main; + + static PlatformFunctions platform_functions; + + static void make_main_thread() {} + static void release_main_thread() {} + +public: + static void _set_platform_functions(const PlatformFunctions &p_functions); + + _FORCE_INLINE_ ID get_id() const { return 0; } + _FORCE_INLINE_ static ID get_caller_id() { return MAIN_ID; } + _FORCE_INLINE_ static ID get_main_id() { return MAIN_ID; } + + _FORCE_INLINE_ static bool is_main_thread() { return true; } + + static Error set_name(const String &p_name) { return ERR_UNAVAILABLE; } + + void start(Thread::Callback p_callback, void *p_user, const Settings &p_settings = Settings()) {} + bool is_started() const { return false; } + void wait_to_finish() {} +}; + +#endif // THREADS_ENABLED + #endif // THREAD_H #endif // PLATFORM_THREAD_OVERRIDE diff --git a/core/templates/cowdata.h b/core/templates/cowdata.h index 46d9797d6c..d43cf8107f 100644 --- a/core/templates/cowdata.h +++ b/core/templates/cowdata.h @@ -46,7 +46,7 @@ class CharString; template <class T, class V> class VMap; -SAFE_NUMERIC_TYPE_PUN_GUARANTEES(uint32_t) +SAFE_NUMERIC_TYPE_PUN_GUARANTEES(uint64_t) // Silence a false positive warning (see GH-52119). #if defined(__GNUC__) && !defined(__clang__) @@ -64,45 +64,71 @@ class CowData { template <class TV, class VV> friend class VMap; +public: + typedef int64_t Size; + typedef uint64_t USize; + static constexpr USize MAX_INT = INT64_MAX; + private: + // Function to find the next power of 2 to an integer. + static _FORCE_INLINE_ USize next_po2(USize x) { + if (x == 0) { + return 0; + } + + --x; + x |= x >> 1; + x |= x >> 2; + x |= x >> 4; + x |= x >> 8; + x |= x >> 16; + if (sizeof(USize) == 8) { + x |= x >> 32; + } + + return ++x; + } + + static constexpr USize ALLOC_PAD = sizeof(USize) * 2; // For size and atomic refcount. + mutable T *_ptr = nullptr; // internal helpers - _FORCE_INLINE_ SafeNumeric<uint32_t> *_get_refcount() const { + _FORCE_INLINE_ SafeNumeric<USize> *_get_refcount() const { if (!_ptr) { return nullptr; } - return reinterpret_cast<SafeNumeric<uint32_t> *>(_ptr) - 2; + return reinterpret_cast<SafeNumeric<USize> *>(_ptr) - 2; } - _FORCE_INLINE_ uint32_t *_get_size() const { + _FORCE_INLINE_ USize *_get_size() const { if (!_ptr) { return nullptr; } - return reinterpret_cast<uint32_t *>(_ptr) - 1; + return reinterpret_cast<USize *>(_ptr) - 1; } - _FORCE_INLINE_ size_t _get_alloc_size(size_t p_elements) const { - return next_power_of_2(p_elements * sizeof(T)); + _FORCE_INLINE_ USize _get_alloc_size(USize p_elements) const { + return next_po2(p_elements * sizeof(T)); } - _FORCE_INLINE_ bool _get_alloc_size_checked(size_t p_elements, size_t *out) const { + _FORCE_INLINE_ bool _get_alloc_size_checked(USize p_elements, USize *out) const { if (unlikely(p_elements == 0)) { *out = 0; return true; } -#if defined(__GNUC__) - size_t o; - size_t p; +#if defined(__GNUC__) && defined(IS_32_BIT) + USize o; + USize p; if (__builtin_mul_overflow(p_elements, sizeof(T), &o)) { *out = 0; return false; } - *out = next_power_of_2(o); - if (__builtin_add_overflow(o, static_cast<size_t>(32), &p)) { + *out = next_po2(o); + if (__builtin_add_overflow(o, static_cast<USize>(32), &p)) { return false; // No longer allocated here. } #else @@ -116,7 +142,7 @@ private: void _unref(void *p_data); void _ref(const CowData *p_from); void _ref(const CowData &p_from); - uint32_t _copy_on_write(); + USize _copy_on_write(); public: void operator=(const CowData<T> &p_from) { _ref(p_from); } @@ -130,8 +156,8 @@ public: return _ptr; } - _FORCE_INLINE_ int size() const { - uint32_t *size = (uint32_t *)_get_size(); + _FORCE_INLINE_ Size size() const { + USize *size = (USize *)_get_size(); if (size) { return *size; } else { @@ -142,42 +168,42 @@ public: _FORCE_INLINE_ void clear() { resize(0); } _FORCE_INLINE_ bool is_empty() const { return _ptr == nullptr; } - _FORCE_INLINE_ void set(int p_index, const T &p_elem) { + _FORCE_INLINE_ void set(Size p_index, const T &p_elem) { ERR_FAIL_INDEX(p_index, size()); _copy_on_write(); _ptr[p_index] = p_elem; } - _FORCE_INLINE_ T &get_m(int p_index) { + _FORCE_INLINE_ T &get_m(Size p_index) { CRASH_BAD_INDEX(p_index, size()); _copy_on_write(); return _ptr[p_index]; } - _FORCE_INLINE_ const T &get(int p_index) const { + _FORCE_INLINE_ const T &get(Size p_index) const { CRASH_BAD_INDEX(p_index, size()); return _ptr[p_index]; } template <bool p_ensure_zero = false> - Error resize(int p_size); + Error resize(Size p_size); - _FORCE_INLINE_ void remove_at(int p_index) { + _FORCE_INLINE_ void remove_at(Size p_index) { ERR_FAIL_INDEX(p_index, size()); T *p = ptrw(); - int len = size(); - for (int i = p_index; i < len - 1; i++) { + Size len = size(); + for (Size i = p_index; i < len - 1; i++) { p[i] = p[i + 1]; } resize(len - 1); } - Error insert(int p_pos, const T &p_val) { + Error insert(Size p_pos, const T &p_val) { ERR_FAIL_INDEX_V(p_pos, size() + 1, ERR_INVALID_PARAMETER); resize(size() + 1); - for (int i = (size() - 1); i > p_pos; i--) { + for (Size i = (size() - 1); i > p_pos; i--) { set(i, get(i - 1)); } set(p_pos, p_val); @@ -185,9 +211,9 @@ public: return OK; } - int find(const T &p_val, int p_from = 0) const; - int rfind(const T &p_val, int p_from = -1) const; - int count(const T &p_val) const; + Size find(const T &p_val, Size p_from = 0) const; + Size rfind(const T &p_val, Size p_from = -1) const; + Size count(const T &p_val) const; _FORCE_INLINE_ CowData() {} _FORCE_INLINE_ ~CowData(); @@ -200,7 +226,7 @@ void CowData<T>::_unref(void *p_data) { return; } - SafeNumeric<uint32_t> *refc = _get_refcount(); + SafeNumeric<USize> *refc = _get_refcount(); if (refc->decrement() > 0) { return; // still in use @@ -208,35 +234,36 @@ void CowData<T>::_unref(void *p_data) { // clean up if (!std::is_trivially_destructible<T>::value) { - uint32_t *count = _get_size(); + USize *count = _get_size(); T *data = (T *)(count + 1); - for (uint32_t i = 0; i < *count; ++i) { + for (USize i = 0; i < *count; ++i) { // call destructors data[i].~T(); } } // free mem - Memory::free_static((uint8_t *)p_data, true); + Memory::free_static(((uint8_t *)p_data) - ALLOC_PAD, false); } template <class T> -uint32_t CowData<T>::_copy_on_write() { +typename CowData<T>::USize CowData<T>::_copy_on_write() { if (!_ptr) { return 0; } - SafeNumeric<uint32_t> *refc = _get_refcount(); + SafeNumeric<USize> *refc = _get_refcount(); - uint32_t rc = refc->get(); + USize rc = refc->get(); if (unlikely(rc > 1)) { /* in use by more than me */ - uint32_t current_size = *_get_size(); + USize current_size = *_get_size(); - uint32_t *mem_new = (uint32_t *)Memory::alloc_static(_get_alloc_size(current_size), true); + USize *mem_new = (USize *)Memory::alloc_static(_get_alloc_size(current_size) + ALLOC_PAD, false); + mem_new += 2; - new (mem_new - 2) SafeNumeric<uint32_t>(1); //refcount + new (mem_new - 2) SafeNumeric<USize>(1); //refcount *(mem_new - 1) = current_size; //size T *_data = (T *)(mem_new); @@ -246,7 +273,7 @@ uint32_t CowData<T>::_copy_on_write() { memcpy(mem_new, _ptr, current_size * sizeof(T)); } else { - for (uint32_t i = 0; i < current_size; i++) { + for (USize i = 0; i < current_size; i++) { memnew_placement(&_data[i], T(_ptr[i])); } } @@ -261,10 +288,10 @@ uint32_t CowData<T>::_copy_on_write() { template <class T> template <bool p_ensure_zero> -Error CowData<T>::resize(int p_size) { +Error CowData<T>::resize(Size p_size) { ERR_FAIL_COND_V(p_size < 0, ERR_INVALID_PARAMETER); - int current_size = size(); + Size current_size = size(); if (p_size == current_size) { return OK; @@ -278,27 +305,29 @@ Error CowData<T>::resize(int p_size) { } // possibly changing size, copy on write - uint32_t rc = _copy_on_write(); + USize rc = _copy_on_write(); - size_t current_alloc_size = _get_alloc_size(current_size); - size_t alloc_size; + USize current_alloc_size = _get_alloc_size(current_size); + USize alloc_size; ERR_FAIL_COND_V(!_get_alloc_size_checked(p_size, &alloc_size), ERR_OUT_OF_MEMORY); if (p_size > current_size) { if (alloc_size != current_alloc_size) { if (current_size == 0) { // alloc from scratch - uint32_t *ptr = (uint32_t *)Memory::alloc_static(alloc_size, true); + USize *ptr = (USize *)Memory::alloc_static(alloc_size + ALLOC_PAD, false); + ptr += 2; ERR_FAIL_NULL_V(ptr, ERR_OUT_OF_MEMORY); *(ptr - 1) = 0; //size, currently none - new (ptr - 2) SafeNumeric<uint32_t>(1); //refcount + new (ptr - 2) SafeNumeric<USize>(1); //refcount _ptr = (T *)ptr; } else { - uint32_t *_ptrnew = (uint32_t *)Memory::realloc_static(_ptr, alloc_size, true); + USize *_ptrnew = (USize *)Memory::realloc_static(((uint8_t *)_ptr) - ALLOC_PAD, alloc_size + ALLOC_PAD, false); ERR_FAIL_NULL_V(_ptrnew, ERR_OUT_OF_MEMORY); - new (_ptrnew - 2) SafeNumeric<uint32_t>(rc); //refcount + _ptrnew += 2; + new (_ptrnew - 2) SafeNumeric<USize>(rc); //refcount _ptr = (T *)(_ptrnew); } @@ -307,7 +336,7 @@ Error CowData<T>::resize(int p_size) { // construct the newly created elements if (!std::is_trivially_constructible<T>::value) { - for (int i = *_get_size(); i < p_size; i++) { + for (Size i = *_get_size(); i < p_size; i++) { memnew_placement(&_ptr[i], T); } } else if (p_ensure_zero) { @@ -319,16 +348,17 @@ Error CowData<T>::resize(int p_size) { } else if (p_size < current_size) { if (!std::is_trivially_destructible<T>::value) { // deinitialize no longer needed elements - for (uint32_t i = p_size; i < *_get_size(); i++) { + for (USize i = p_size; i < *_get_size(); i++) { T *t = &_ptr[i]; t->~T(); } } if (alloc_size != current_alloc_size) { - uint32_t *_ptrnew = (uint32_t *)Memory::realloc_static(_ptr, alloc_size, true); + USize *_ptrnew = (USize *)Memory::realloc_static(((uint8_t *)_ptr) - ALLOC_PAD, alloc_size + ALLOC_PAD, false); ERR_FAIL_NULL_V(_ptrnew, ERR_OUT_OF_MEMORY); - new (_ptrnew - 2) SafeNumeric<uint32_t>(rc); //refcount + _ptrnew += 2; + new (_ptrnew - 2) SafeNumeric<USize>(rc); //refcount _ptr = (T *)(_ptrnew); } @@ -340,14 +370,14 @@ Error CowData<T>::resize(int p_size) { } template <class T> -int CowData<T>::find(const T &p_val, int p_from) const { - int ret = -1; +typename CowData<T>::Size CowData<T>::find(const T &p_val, Size p_from) const { + Size ret = -1; if (p_from < 0 || size() == 0) { return ret; } - for (int i = p_from; i < size(); i++) { + for (Size i = p_from; i < size(); i++) { if (get(i) == p_val) { ret = i; break; @@ -358,8 +388,8 @@ int CowData<T>::find(const T &p_val, int p_from) const { } template <class T> -int CowData<T>::rfind(const T &p_val, int p_from) const { - const int s = size(); +typename CowData<T>::Size CowData<T>::rfind(const T &p_val, Size p_from) const { + const Size s = size(); if (p_from < 0) { p_from = s + p_from; @@ -368,7 +398,7 @@ int CowData<T>::rfind(const T &p_val, int p_from) const { p_from = s - 1; } - for (int i = p_from; i >= 0; i--) { + for (Size i = p_from; i >= 0; i--) { if (get(i) == p_val) { return i; } @@ -377,9 +407,9 @@ int CowData<T>::rfind(const T &p_val, int p_from) const { } template <class T> -int CowData<T>::count(const T &p_val) const { - int amount = 0; - for (int i = 0; i < size(); i++) { +typename CowData<T>::Size CowData<T>::count(const T &p_val) const { + Size amount = 0; + for (Size i = 0; i < size(); i++) { if (get(i) == p_val) { amount++; } diff --git a/core/templates/ring_buffer.h b/core/templates/ring_buffer.h index d878894946..54148a59bf 100644 --- a/core/templates/ring_buffer.h +++ b/core/templates/ring_buffer.h @@ -197,7 +197,7 @@ public: int old_size = size(); int new_size = 1 << p_power; int mask = new_size - 1; - data.resize(1 << p_power); + data.resize(int64_t(1) << int64_t(p_power)); if (old_size < new_size && read_pos > write_pos) { for (int i = 0; i < write_pos; i++) { data.write[(old_size + i) & mask] = data[i]; diff --git a/core/templates/vector.h b/core/templates/vector.h index d8bac0870f..361a7f56d5 100644 --- a/core/templates/vector.h +++ b/core/templates/vector.h @@ -48,7 +48,7 @@ template <class T> class VectorWriteProxy { public: - _FORCE_INLINE_ T &operator[](int p_index) { + _FORCE_INLINE_ T &operator[](typename CowData<T>::Size p_index) { CRASH_BAD_INDEX(p_index, ((Vector<T> *)(this))->_cowdata.size()); return ((Vector<T> *)(this))->_cowdata.ptrw()[p_index]; @@ -61,6 +61,7 @@ class Vector { public: VectorWriteProxy<T> write; + typedef typename CowData<T>::Size Size; private: CowData<T> _cowdata; @@ -70,9 +71,9 @@ public: _FORCE_INLINE_ bool append(const T &p_elem) { return push_back(p_elem); } //alias void fill(T p_elem); - void remove_at(int p_index) { _cowdata.remove_at(p_index); } + void remove_at(Size p_index) { _cowdata.remove_at(p_index); } _FORCE_INLINE_ bool erase(const T &p_val) { - int idx = find(p_val); + Size idx = find(p_val); if (idx >= 0) { remove_at(idx); return true; @@ -87,17 +88,17 @@ public: _FORCE_INLINE_ void clear() { resize(0); } _FORCE_INLINE_ bool is_empty() const { return _cowdata.is_empty(); } - _FORCE_INLINE_ T get(int p_index) { return _cowdata.get(p_index); } - _FORCE_INLINE_ const T &get(int p_index) const { return _cowdata.get(p_index); } - _FORCE_INLINE_ void set(int p_index, const T &p_elem) { _cowdata.set(p_index, p_elem); } - _FORCE_INLINE_ int size() const { return _cowdata.size(); } - Error resize(int p_size) { return _cowdata.resize(p_size); } - Error resize_zeroed(int p_size) { return _cowdata.template resize<true>(p_size); } - _FORCE_INLINE_ const T &operator[](int p_index) const { return _cowdata.get(p_index); } - Error insert(int p_pos, T p_val) { return _cowdata.insert(p_pos, p_val); } - int find(const T &p_val, int p_from = 0) const { return _cowdata.find(p_val, p_from); } - int rfind(const T &p_val, int p_from = -1) const { return _cowdata.rfind(p_val, p_from); } - int count(const T &p_val) const { return _cowdata.count(p_val); } + _FORCE_INLINE_ T get(Size p_index) { return _cowdata.get(p_index); } + _FORCE_INLINE_ const T &get(Size p_index) const { return _cowdata.get(p_index); } + _FORCE_INLINE_ void set(Size p_index, const T &p_elem) { _cowdata.set(p_index, p_elem); } + _FORCE_INLINE_ Size size() const { return _cowdata.size(); } + Error resize(Size p_size) { return _cowdata.resize(p_size); } + Error resize_zeroed(Size p_size) { return _cowdata.template resize<true>(p_size); } + _FORCE_INLINE_ const T &operator[](Size p_index) const { return _cowdata.get(p_index); } + Error insert(Size p_pos, T p_val) { return _cowdata.insert(p_pos, p_val); } + Size find(const T &p_val, Size p_from = 0) const { return _cowdata.find(p_val, p_from); } + Size rfind(const T &p_val, Size p_from = -1) const { return _cowdata.rfind(p_val, p_from); } + Size count(const T &p_val) const { return _cowdata.count(p_val); } void append_array(Vector<T> p_other); @@ -109,7 +110,7 @@ public: template <class Comparator, bool Validate = SORT_ARRAY_VALIDATE_ENABLED, class... Args> void sort_custom(Args &&...args) { - int len = _cowdata.size(); + Size len = _cowdata.size(); if (len == 0) { return; } @@ -119,12 +120,12 @@ public: sorter.sort(data, len); } - int bsearch(const T &p_value, bool p_before) { + Size bsearch(const T &p_value, bool p_before) { return bsearch_custom<_DefaultComparator<T>>(p_value, p_before); } template <class Comparator, class Value, class... Args> - int bsearch_custom(const Value &p_value, bool p_before, Args &&...args) { + Size bsearch_custom(const Value &p_value, bool p_before, Args &&...args) { SearchArray<T, Comparator> search{ args... }; return search.bisect(ptrw(), size(), p_value, p_before); } @@ -134,7 +135,7 @@ public: } void ordered_insert(const T &p_val) { - int i; + Size i; for (i = 0; i < _cowdata.size(); i++) { if (p_val < operator[](i)) { break; @@ -157,28 +158,28 @@ public: return ret; } - Vector<T> slice(int p_begin, int p_end = INT_MAX) const { + Vector<T> slice(Size p_begin, Size p_end = CowData<T>::MAX_INT) const { Vector<T> result; - const int s = size(); + const Size s = size(); - int begin = CLAMP(p_begin, -s, s); + Size begin = CLAMP(p_begin, -s, s); if (begin < 0) { begin += s; } - int end = CLAMP(p_end, -s, s); + Size end = CLAMP(p_end, -s, s); if (end < 0) { end += s; } ERR_FAIL_COND_V(begin > end, result); - int result_size = end - begin; + Size result_size = end - begin; result.resize(result_size); const T *const r = ptr(); T *const w = result.ptrw(); - for (int i = 0; i < result_size; ++i) { + for (Size i = 0; i < result_size; ++i) { w[i] = r[begin + i]; } @@ -186,11 +187,11 @@ public: } bool operator==(const Vector<T> &p_arr) const { - int s = size(); + Size s = size(); if (s != p_arr.size()) { return false; } - for (int i = 0; i < s; i++) { + for (Size i = 0; i < s; i++) { if (operator[](i) != p_arr[i]) { return false; } @@ -199,11 +200,11 @@ public: } bool operator!=(const Vector<T> &p_arr) const { - int s = size(); + Size s = size(); if (s != p_arr.size()) { return true; } - for (int i = 0; i < s; i++) { + for (Size i = 0; i < s; i++) { if (operator[](i) != p_arr[i]) { return true; } @@ -280,7 +281,7 @@ public: Error err = _cowdata.resize(p_init.size()); ERR_FAIL_COND(err); - int i = 0; + Size i = 0; for (const T &element : p_init) { _cowdata.set(i++, element); } @@ -292,7 +293,7 @@ public: template <class T> void Vector<T>::reverse() { - for (int i = 0; i < size() / 2; i++) { + for (Size i = 0; i < size() / 2; i++) { T *p = ptrw(); SWAP(p[i], p[size() - i - 1]); } @@ -300,13 +301,13 @@ void Vector<T>::reverse() { template <class T> void Vector<T>::append_array(Vector<T> p_other) { - const int ds = p_other.size(); + const Size ds = p_other.size(); if (ds == 0) { return; } - const int bs = size(); + const Size bs = size(); resize(bs + ds); - for (int i = 0; i < ds; ++i) { + for (Size i = 0; i < ds; ++i) { ptrw()[bs + i] = p_other[i]; } } @@ -323,7 +324,7 @@ bool Vector<T>::push_back(T p_elem) { template <class T> void Vector<T>::fill(T p_elem) { T *p = ptrw(); - for (int i = 0; i < size(); i++) { + for (Size i = 0; i < size(); i++) { p[i] = p_elem; } } diff --git a/core/typedefs.h b/core/typedefs.h index 8b04960b9a..8807ee3c99 100644 --- a/core/typedefs.h +++ b/core/typedefs.h @@ -93,6 +93,7 @@ #undef OK #undef CONNECT_DEFERRED // override from Windows SDK, clashes with Object enum #undef MemoryBarrier +#undef MONO_FONT #endif // Make room for our constexpr's below by overriding potential system-specific macros. diff --git a/core/variant/binder_common.h b/core/variant/binder_common.h index 34b54f1d00..c9f5ae7fc6 100644 --- a/core/variant/binder_common.h +++ b/core/variant/binder_common.h @@ -176,6 +176,7 @@ VARIANT_ENUM_CAST(Variant::Operator); VARIANT_ENUM_CAST(Key); VARIANT_BITFIELD_CAST(KeyModifierMask); +VARIANT_ENUM_CAST(KeyLocation); static inline Key &operator|=(Key &a, BitField<KeyModifierMask> b) { a = static_cast<Key>(static_cast<int>(a) | static_cast<int>(b.operator int64_t())); diff --git a/core/variant/variant.h b/core/variant/variant.h index 602d287f22..429b346896 100644 --- a/core/variant/variant.h +++ b/core/variant/variant.h @@ -502,6 +502,7 @@ public: VARIANT_ENUM_CLASS_CONSTRUCTOR(JoyAxis) VARIANT_ENUM_CLASS_CONSTRUCTOR(JoyButton) VARIANT_ENUM_CLASS_CONSTRUCTOR(Key) + VARIANT_ENUM_CLASS_CONSTRUCTOR(KeyLocation) VARIANT_ENUM_CLASS_CONSTRUCTOR(MIDIMessage) VARIANT_ENUM_CLASS_CONSTRUCTOR(MouseButton) diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp index 03836985f3..90f7e7b44a 100644 --- a/core/variant/variant_call.cpp +++ b/core/variant/variant_call.cpp @@ -666,7 +666,7 @@ struct _VariantCall { CharString cs; cs.resize(p_instance->size() + 1); memcpy(cs.ptrw(), r, p_instance->size()); - cs[p_instance->size()] = 0; + cs[(int)p_instance->size()] = 0; s = cs.get_data(); } diff --git a/core/version.h b/core/version.h index 5ddb09284e..abb81312ac 100644 --- a/core/version.h +++ b/core/version.h @@ -33,6 +33,12 @@ #include "core/version_generated.gen.h" +// Copied from typedefs.h to stay lean. +#ifndef _STR +#define _STR(m_x) #m_x +#define _MKSTR(m_x) _STR(m_x) +#endif + // Godot versions are of the form <major>.<minor> for the initial release, // and then <major>.<minor>.<patch> for subsequent bugfix releases where <patch> != 0 // That's arbitrary, but we find it pretty and it's the current policy. |