diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/input/input_event.cpp | 22 | ||||
-rw-r--r-- | core/input/input_event.h | 6 | ||||
-rw-r--r-- | core/input/input_map.cpp | 1 | ||||
-rw-r--r-- | core/math/math_funcs.h | 4 | ||||
-rw-r--r-- | core/math/random_pcg.cpp | 2 | ||||
-rw-r--r-- | core/string/optimized_translation.cpp | 1 | ||||
-rw-r--r-- | core/string/ustring.cpp | 65 | ||||
-rw-r--r-- | core/string/ustring.h | 1 | ||||
-rw-r--r-- | core/variant/callable.cpp | 20 | ||||
-rw-r--r-- | core/variant/callable.h | 3 | ||||
-rw-r--r-- | core/variant/variant_call.cpp | 79 | ||||
-rw-r--r-- | core/variant/variant_op.cpp | 16 | ||||
-rw-r--r-- | core/variant/variant_utility.cpp | 5 |
13 files changed, 208 insertions, 17 deletions
diff --git a/core/input/input_event.cpp b/core/input/input_event.cpp index 99cc51b95e..46629d742e 100644 --- a/core/input/input_event.cpp +++ b/core/input/input_event.cpp @@ -545,12 +545,12 @@ bool InputEventMouseButton::is_pressed() const { return pressed; } -void InputEventMouseButton::set_doubleclick(bool p_doubleclick) { - doubleclick = p_doubleclick; +void InputEventMouseButton::set_double_click(bool p_double_click) { + double_click = p_double_click; } -bool InputEventMouseButton::is_doubleclick() const { - return doubleclick; +bool InputEventMouseButton::is_double_click() const { + return double_click; } Ref<InputEvent> InputEventMouseButton::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const { @@ -569,7 +569,7 @@ Ref<InputEvent> InputEventMouseButton::xformed_by(const Transform2D &p_xform, co mb->set_button_mask(get_button_mask()); mb->set_pressed(pressed); - mb->set_doubleclick(doubleclick); + mb->set_double_click(double_click); mb->set_factor(factor); mb->set_button_index(button_index); @@ -636,7 +636,7 @@ String InputEventMouseButton::as_text() const { } // Double Click - if (doubleclick) { + if (double_click) { full_string += " (" + RTR("Double Click") + ")"; } @@ -645,7 +645,7 @@ String InputEventMouseButton::as_text() const { String InputEventMouseButton::to_string() { String p = is_pressed() ? "true" : "false"; - String d = doubleclick ? "true" : "false"; + String d = double_click ? "true" : "false"; int idx = get_button_index(); String button_string = itos(idx); @@ -671,7 +671,7 @@ String InputEventMouseButton::to_string() { // Work around the fact vformat can only take 5 substitutions but 6 need to be passed. String index_and_mods = vformat("button_index=%s mods=%s", button_index, mods); - return vformat("InputEventMouseButton: %s pressed=%s position=(%s) button_mask=%s doubleclick=%s", index_and_mods, p, String(get_position()), itos(get_button_mask()), d); + return vformat("InputEventMouseButton: %s pressed=%s position=(%s) button_mask=%s double_click=%s", index_and_mods, p, String(get_position()), itos(get_button_mask()), d); } void InputEventMouseButton::_bind_methods() { @@ -684,13 +684,13 @@ void InputEventMouseButton::_bind_methods() { ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventMouseButton::set_pressed); // ClassDB::bind_method(D_METHOD("is_pressed"), &InputEventMouseButton::is_pressed); - ClassDB::bind_method(D_METHOD("set_doubleclick", "doubleclick"), &InputEventMouseButton::set_doubleclick); - ClassDB::bind_method(D_METHOD("is_doubleclick"), &InputEventMouseButton::is_doubleclick); + ClassDB::bind_method(D_METHOD("set_double_click", "double_click"), &InputEventMouseButton::set_double_click); + ClassDB::bind_method(D_METHOD("is_double_click"), &InputEventMouseButton::is_double_click); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "factor"), "set_factor", "get_factor"); ADD_PROPERTY(PropertyInfo(Variant::INT, "button_index"), "set_button_index", "get_button_index"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed"); - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "doubleclick"), "set_doubleclick", "is_doubleclick"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "double_click"), "set_double_click", "is_double_click"); } /////////////////////////////////// diff --git a/core/input/input_event.h b/core/input/input_event.h index 94aa68db33..5a33ee7b9c 100644 --- a/core/input/input_event.h +++ b/core/input/input_event.h @@ -294,7 +294,7 @@ class InputEventMouseButton : public InputEventMouse { float factor = 1; int button_index = 0; bool pressed = false; //otherwise released - bool doubleclick = false; //last even less than doubleclick time + bool double_click = false; //last even less than double click time protected: static void _bind_methods(); @@ -309,8 +309,8 @@ public: void set_pressed(bool p_pressed); virtual bool is_pressed() const override; - void set_doubleclick(bool p_doubleclick); - bool is_doubleclick() const; + void set_double_click(bool p_double_click); + bool is_double_click() const; virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override; virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const override; diff --git a/core/input/input_map.cpp b/core/input/input_map.cpp index aab4e6593c..7421909650 100644 --- a/core/input/input_map.cpp +++ b/core/input/input_map.cpp @@ -474,6 +474,7 @@ const OrderedHashMap<String, List<Ref<InputEvent>>> &InputMap::get_builtins() { inputs = List<Ref<InputEvent>>(); inputs.push_back(InputEventKey::create_reference(KEY_TAB)); + inputs.push_back(InputEventKey::create_reference(KEY_ENTER)); default_builtin_cache.insert("ui_text_completion_accept", inputs); // Newlines diff --git a/core/math/math_funcs.h b/core/math/math_funcs.h index 8cf13efdb6..c0d7649b65 100644 --- a/core/math/math_funcs.h +++ b/core/math/math_funcs.h @@ -218,8 +218,8 @@ public: return value; } - static _ALWAYS_INLINE_ int posmod(int p_x, int p_y) { - int value = p_x % p_y; + static _ALWAYS_INLINE_ int64_t posmod(int64_t p_x, int64_t p_y) { + int64_t value = p_x % p_y; if ((value < 0 && p_y > 0) || (value > 0 && p_y < 0)) { value += p_y; } diff --git a/core/math/random_pcg.cpp b/core/math/random_pcg.cpp index 1152c4e834..681c2a9717 100644 --- a/core/math/random_pcg.cpp +++ b/core/math/random_pcg.cpp @@ -39,7 +39,7 @@ RandomPCG::RandomPCG(uint64_t p_seed, uint64_t p_inc) : } void RandomPCG::randomize() { - seed((OS::get_singleton()->get_unix_time() + OS::get_singleton()->get_ticks_usec()) * pcg.state + PCG_DEFAULT_INC_64); + seed(((uint64_t)OS::get_singleton()->get_unix_time() + OS::get_singleton()->get_ticks_usec()) * pcg.state + PCG_DEFAULT_INC_64); } double RandomPCG::random(double p_from, double p_to) { diff --git a/core/string/optimized_translation.cpp b/core/string/optimized_translation.cpp index 53d0a8924d..268562d971 100644 --- a/core/string/optimized_translation.cpp +++ b/core/string/optimized_translation.cpp @@ -46,6 +46,7 @@ void OptimizedTranslation::generate(const Ref<Translation> &p_from) { // This method compresses a Translation instance. // Right now, it doesn't handle context or plurals, so Translation subclasses using plurals or context (i.e TranslationPO) shouldn't be compressed. #ifdef TOOLS_ENABLED + ERR_FAIL_COND(p_from.is_null()); List<StringName> keys; p_from->get_message_list(&keys); diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp index a3bbb5ac18..bdb66526a4 100644 --- a/core/string/ustring.cpp +++ b/core/string/ustring.cpp @@ -240,6 +240,71 @@ String String::word_wrap(int p_chars_per_line) const { return ret; } +Error String::parse_url(String &r_scheme, String &r_host, int &r_port, String &r_path) const { + // Splits the URL into scheme, host, port, path. Strip credentials when present. + String base = *this; + r_scheme = ""; + r_host = ""; + r_port = 0; + r_path = ""; + int pos = base.find("://"); + // Scheme + if (pos != -1) { + r_scheme = base.substr(0, pos + 3).to_lower(); + base = base.substr(pos + 3, base.length() - pos - 3); + } + pos = base.find("/"); + // Path + if (pos != -1) { + r_path = base.substr(pos, base.length() - pos); + base = base.substr(0, pos); + } + // Host + pos = base.find("@"); + if (pos != -1) { + // Strip credentials + base = base.substr(pos + 1, base.length() - pos - 1); + } + if (base.begins_with("[")) { + // Literal IPv6 + pos = base.rfind("]"); + if (pos == -1) { + return ERR_INVALID_PARAMETER; + } + r_host = base.substr(1, pos - 1); + base = base.substr(pos + 1, base.length() - pos - 1); + } else { + // Anything else + if (base.get_slice_count(":") > 1) { + return ERR_INVALID_PARAMETER; + } + pos = base.rfind(":"); + if (pos == -1) { + r_host = base; + base = ""; + } else { + r_host = base.substr(0, pos); + base = base.substr(pos, base.length() - pos); + } + } + if (r_host.is_empty()) { + return ERR_INVALID_PARAMETER; + } + r_host = r_host.to_lower(); + // Port + if (base.begins_with(":")) { + base = base.substr(1, base.length() - 1); + if (!base.is_valid_integer()) { + return ERR_INVALID_PARAMETER; + } + r_port = base.to_int(); + if (r_port < 1 || r_port > 65535) { + return ERR_INVALID_PARAMETER; + } + } + return OK; +} + void String::copy_from(const char *p_cstr) { // copy Latin-1 encoded c-string directly if (!p_cstr) { diff --git a/core/string/ustring.h b/core/string/ustring.h index 1e362d7683..a56845deff 100644 --- a/core/string/ustring.h +++ b/core/string/ustring.h @@ -416,6 +416,7 @@ public: String c_unescape() const; String json_escape() const; String word_wrap(int p_chars_per_line) const; + Error parse_url(String &r_scheme, String &r_host, int &r_port, String &r_path) const; String property_name_encode() const; diff --git a/core/variant/callable.cpp b/core/variant/callable.cpp index a1d9c5ed2f..e06b3e07ef 100644 --- a/core/variant/callable.cpp +++ b/core/variant/callable.cpp @@ -54,6 +54,20 @@ void Callable::call(const Variant **p_arguments, int p_argcount, Variant &r_retu } } +void Callable::rpc(int p_id, const Variant **p_arguments, int p_argcount, CallError &r_call_error) const { + if (is_null()) { + r_call_error.error = CallError::CALL_ERROR_INSTANCE_IS_NULL; + r_call_error.argument = 0; + r_call_error.expected = 0; + } else if (!is_custom()) { + r_call_error.error = CallError::CALL_ERROR_INVALID_METHOD; + r_call_error.argument = 0; + r_call_error.expected = 0; + } else { + custom->rpc(p_id, p_arguments, p_argcount, r_call_error); + } +} + Callable Callable::bind(const Variant **p_arguments, int p_argcount) const { Vector<Variant> args; args.resize(p_argcount); @@ -283,6 +297,12 @@ Callable::~Callable() { } } +void CallableCustom::rpc(int p_peer_id, const Variant **p_arguments, int p_argcount, Callable::CallError &r_call_error) const { + r_call_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; + r_call_error.argument = 0; + r_call_error.expected = 0; +} + const Callable *CallableCustom::get_base_comparator() const { return nullptr; } diff --git a/core/variant/callable.h b/core/variant/callable.h index 090fd888e2..d91bebfa5f 100644 --- a/core/variant/callable.h +++ b/core/variant/callable.h @@ -70,6 +70,8 @@ public: void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, CallError &r_call_error) const; void call_deferred(const Variant **p_arguments, int p_argcount) const; + void rpc(int p_id, const Variant **p_arguments, int p_argcount, CallError &r_call_error) const; + _FORCE_INLINE_ bool is_null() const { return method == StringName() && object == 0; } @@ -124,6 +126,7 @@ public: virtual CompareLessFunc get_compare_less_func() const = 0; virtual ObjectID get_object() const = 0; //must always be able to provide an object virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const = 0; + virtual void rpc(int p_peer_id, const Variant **p_arguments, int p_argcount, Callable::CallError &r_call_error) const; virtual const Callable *get_base_comparator() const; CallableCustom(); diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp index deaccc6304..455e924568 100644 --- a/core/variant/variant_call.cpp +++ b/core/variant/variant_call.cpp @@ -493,6 +493,58 @@ static _FORCE_INLINE_ void vc_ptrcall(void (*method)(T *, P...), void *p_base, c } \ }; +#define VARARG_CLASS1(m_class, m_method_name, m_method_ptr, m_arg_type) \ + struct Method_##m_class##_##m_method_name { \ + static void call(Variant *base, const Variant **p_args, int p_argcount, Variant &r_ret, const Vector<Variant> &p_defvals, Callable::CallError &r_error) { \ + m_method_ptr(base, p_args, p_argcount, r_ret, r_error); \ + } \ + static void validated_call(Variant *base, const Variant **p_args, int p_argcount, Variant *r_ret) { \ + Callable::CallError ce; \ + m_method_ptr(base, p_args, p_argcount, *r_ret, ce); \ + } \ + static void ptrcall(void *p_base, const void **p_args, void *r_ret, int p_argcount) { \ + LocalVector<Variant> vars; \ + vars.resize(p_argcount); \ + LocalVector<const Variant *> vars_ptrs; \ + vars_ptrs.resize(p_argcount); \ + for (int i = 0; i < p_argcount; i++) { \ + vars[i] = PtrToArg<Variant>::convert(p_args[i]); \ + vars_ptrs[i] = &vars[i]; \ + } \ + Variant base = PtrToArg<m_class>::convert(p_base); \ + Variant ret; \ + Callable::CallError ce; \ + m_method_ptr(&base, (const Variant **)&vars_ptrs[0], p_argcount, ret, ce); \ + } \ + static int get_argument_count() { \ + return 1; \ + } \ + static Variant::Type get_argument_type(int p_arg) { \ + return m_arg_type; \ + } \ + static Variant::Type get_return_type() { \ + return Variant::NIL; \ + } \ + static bool has_return_type() { \ + return false; \ + } \ + static bool is_const() { \ + return true; \ + } \ + static bool is_static() { \ + return false; \ + } \ + static bool is_vararg() { \ + return true; \ + } \ + static Variant::Type get_base_type() { \ + return GetTypeInfo<m_class>::VARIANT_TYPE; \ + } \ + static StringName get_name() { \ + return #m_method_name; \ + } \ + }; + struct _VariantCall { static String func_PackedByteArray_get_string_from_ascii(PackedByteArray *p_instance) { String s; @@ -792,6 +844,27 @@ struct _VariantCall { callable->call_deferred(p_args, p_argcount); } + static void func_Callable_rpc(Variant *v, const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error) { + Callable *callable = VariantGetInternalPtr<Callable>::get_ptr(v); + callable->rpc(0, p_args, p_argcount, r_error); + } + + static void func_Callable_rpc_id(Variant *v, const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error) { + if (p_argcount == 0) { + r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; + r_error.argument = 0; + r_error.expected = 1; + + } else if (p_args[0]->get_type() != Variant::INT) { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.argument = 0; + r_error.expected = Variant::INT; + } else { + Callable *callable = VariantGetInternalPtr<Callable>::get_ptr(v); + callable->rpc(*p_args[0], &p_args[1], p_argcount - 1, r_error); + } + } + static void func_Callable_bind(Variant *v, const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error) { Callable *callable = VariantGetInternalPtr<Callable>::get_ptr(v); r_ret = callable->bind(p_args, p_argcount); @@ -1219,6 +1292,10 @@ Variant Variant::get_constant_value(Variant::Type p_type, const StringName &p_va VARARG_CLASS(m_type, m_name, m_method, m_has_return, m_ret_type) \ register_builtin_method<Method_##m_type##_##m_name>(sarray(), Vector<Variant>()); +#define bind_custom1(m_type, m_name, m_method, m_arg_type, m_arg_name) \ + VARARG_CLASS1(m_type, m_name, m_method, m_arg_type) \ + register_builtin_method<Method_##m_type##_##m_name>(sarray(m_arg_name), Vector<Variant>()); + static void _register_variant_builtin_methods() { _VariantCall::constant_data = memnew_arr(_VariantCall::ConstantData, Variant::VARIANT_MAX); builtin_method_info = memnew_arr(BuiltinMethodMap, Variant::VARIANT_MAX); @@ -1532,6 +1609,8 @@ static void _register_variant_builtin_methods() { bind_custom(Callable, call, _VariantCall::func_Callable_call, true, Variant); bind_custom(Callable, call_deferred, _VariantCall::func_Callable_call_deferred, false, Variant); + bind_custom(Callable, rpc, _VariantCall::func_Callable_rpc, false, Variant); + bind_custom1(Callable, rpc_id, _VariantCall::func_Callable_rpc_id, Variant::INT, "peer_id"); bind_custom(Callable, bind, _VariantCall::func_Callable_bind, true, Callable); /* Signal */ diff --git a/core/variant/variant_op.cpp b/core/variant/variant_op.cpp index 6cbc98d14d..8cfa793c0e 100644 --- a/core/variant/variant_op.cpp +++ b/core/variant/variant_op.cpp @@ -257,6 +257,14 @@ public: static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) { const A &a = *VariantGetInternalPtr<A>::get_ptr(&p_left); const B &b = *VariantGetInternalPtr<B>::get_ptr(&p_right); + +#if defined(DEBUG_ENABLED) + if (b < 0 || a < 0) { + *r_ret = "Invalid operands for bit shifting. Only positive operands are supported."; + r_valid = false; + return; + } +#endif *r_ret = a << b; r_valid = true; } @@ -276,6 +284,14 @@ public: static void evaluate(const Variant &p_left, const Variant &p_right, Variant *r_ret, bool &r_valid) { const A &a = *VariantGetInternalPtr<A>::get_ptr(&p_left); const B &b = *VariantGetInternalPtr<B>::get_ptr(&p_right); + +#if defined(DEBUG_ENABLED) + if (b < 0 || a < 0) { + *r_ret = "Invalid operands for bit shifting. Only positive operands are supported."; + r_valid = false; + return; + } +#endif *r_ret = a >> b; r_valid = true; } diff --git a/core/variant/variant_utility.cpp b/core/variant/variant_utility.cpp index f154ab1ed6..553f2b23a2 100644 --- a/core/variant/variant_utility.cpp +++ b/core/variant/variant_utility.cpp @@ -93,6 +93,10 @@ struct VariantUtilityFunctions { return Math::fposmod(b, r); } + static inline int64_t posmod(int64_t b, int64_t r) { + return Math::posmod(b, r); + } + static inline double floor(double x) { return Math::floor(x); } @@ -1154,6 +1158,7 @@ void Variant::_register_variant_utility_functions() { FUNCBINDR(sqrt, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(fmod, sarray("x", "y"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(fposmod, sarray("x", "y"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(posmod, sarray("x", "y"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(floor, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(ceil, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(round, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); |