diff options
Diffstat (limited to 'core')
208 files changed, 6041 insertions, 4942 deletions
diff --git a/core/SCsub b/core/SCsub index d4462fa546..43deff3ad5 100644 --- a/core/SCsub +++ b/core/SCsub @@ -85,7 +85,7 @@ if env["builtin_zlib"]: env_thirdparty.Prepend(CPPPATH=[thirdparty_zlib_dir]) # Needs to be available in main env too env.Prepend(CPPPATH=[thirdparty_zlib_dir]) - if env["target"] == "debug": + if env.dev_build: env_thirdparty.Append(CPPDEFINES=["ZLIB_DEBUG"]) env_thirdparty.add_source_files(thirdparty_obj, thirdparty_zlib_sources) diff --git a/core/config/engine.cpp b/core/config/engine.cpp index e1da9eb44e..7aa5f4d06d 100644 --- a/core/config/engine.cpp +++ b/core/config/engine.cpp @@ -33,7 +33,10 @@ #include "core/authors.gen.h" #include "core/config/project_settings.h" #include "core/donors.gen.h" +#include "core/io/json.h" #include "core/license.gen.h" +#include "core/os/os.h" +#include "core/variant/typed_array.h" #include "core/version.h" void Engine::set_physics_ticks_per_second(int p_ips) { @@ -45,6 +48,15 @@ int Engine::get_physics_ticks_per_second() const { return ips; } +void Engine::set_max_physics_steps_per_frame(int p_max_physics_steps) { + ERR_FAIL_COND_MSG(p_max_physics_steps <= 0, "Maximum number of physics steps per frame must be greater than 0."); + max_physics_steps_per_frame = p_max_physics_steps; +} + +int Engine::get_max_physics_steps_per_frame() const { + return max_physics_steps_per_frame; +} + void Engine::set_physics_jitter_fix(double p_threshold) { if (p_threshold < 0) { p_threshold = 0; @@ -56,12 +68,12 @@ double Engine::get_physics_jitter_fix() const { return physics_jitter_fix; } -void Engine::set_target_fps(int p_fps) { - _target_fps = p_fps > 0 ? p_fps : 0; +void Engine::set_max_fps(int p_fps) { + _max_fps = p_fps > 0 ? p_fps : 0; } -int Engine::get_target_fps() const { - return _target_fps; +int Engine::get_max_fps() const { + return _max_fps; } uint64_t Engine::get_frames_drawn() { @@ -134,8 +146,8 @@ Dictionary Engine::get_author_info() const { return dict; } -Array Engine::get_copyright_info() const { - Array components; +TypedArray<Dictionary> Engine::get_copyright_info() const { + TypedArray<Dictionary> components; for (int component_index = 0; component_index < COPYRIGHT_INFO_COUNT; component_index++) { const ComponentCopyright &cp_info = COPYRIGHT_INFO[component_index]; Dictionary component_dict; @@ -188,11 +200,11 @@ String Engine::get_architecture_name() const { #elif defined(__i386) || defined(__i386__) || defined(_M_IX86) return "x86_32"; -#elif defined(__aarch64__) || defined(_M_ARM64) +#elif defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC) return "arm64"; -#elif defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7S__) - return "armv7"; +#elif defined(__arm__) || defined(_M_ARM) + return "arm32"; #elif defined(__riscv) #if __riscv_xlen == 8 @@ -307,6 +319,43 @@ Engine::Engine() { singleton = this; } +void Engine::startup_begin() { + startup_benchmark_total_from = OS::get_singleton()->get_ticks_usec(); +} + +void Engine::startup_benchmark_begin_measure(const String &p_what) { + startup_benchmark_section = p_what; + startup_benchmark_from = OS::get_singleton()->get_ticks_usec(); +} +void Engine::startup_benchmark_end_measure() { + uint64_t total = OS::get_singleton()->get_ticks_usec() - startup_benchmark_from; + double total_f = double(total) / double(1000000); + + startup_benchmark_json[startup_benchmark_section] = total_f; +} + +void Engine::startup_dump(const String &p_to_file) { + uint64_t total = OS::get_singleton()->get_ticks_usec() - startup_benchmark_total_from; + double total_f = double(total) / double(1000000); + startup_benchmark_json["total_time"] = total_f; + + if (!p_to_file.is_empty()) { + Ref<FileAccess> f = FileAccess::open(p_to_file, FileAccess::WRITE); + if (f.is_valid()) { + Ref<JSON> json; + json.instantiate(); + f->store_string(json->stringify(startup_benchmark_json, "\t", false, true)); + } + } else { + List<Variant> keys; + startup_benchmark_json.get_key_list(&keys); + print_line("STARTUP BENCHMARK:"); + for (const Variant &K : keys) { + print_line("\t-", K, ": ", startup_benchmark_json[K], +" sec."); + } + } +} + Engine::Singleton::Singleton(const StringName &p_name, Object *p_ptr, const StringName &p_class_name) : name(p_name), ptr(p_ptr), diff --git a/core/config/engine.h b/core/config/engine.h index 649be23717..1b179c5727 100644 --- a/core/config/engine.h +++ b/core/config/engine.h @@ -36,6 +36,9 @@ #include "core/templates/list.h" #include "core/templates/vector.h" +template <typename T> +class TypedArray; + class Engine { public: struct Singleton { @@ -57,9 +60,10 @@ private: int ips = 60; double physics_jitter_fix = 0.5; double _fps = 1; - int _target_fps = 0; + int _max_fps = 0; double _time_scale = 1.0; uint64_t _physics_frames = 0; + int max_physics_steps_per_frame = 8; double _physics_interpolation_fraction = 0.0f; bool abort_on_gpu_errors = false; bool use_validation_layers = false; @@ -79,17 +83,25 @@ private: String write_movie_path; String shader_cache_path; + Dictionary startup_benchmark_json; + String startup_benchmark_section; + uint64_t startup_benchmark_from = 0; + uint64_t startup_benchmark_total_from = 0; + public: static Engine *get_singleton(); virtual void set_physics_ticks_per_second(int p_ips); virtual int get_physics_ticks_per_second() const; + virtual void set_max_physics_steps_per_frame(int p_max_physics_steps); + virtual int get_max_physics_steps_per_frame() const; + void set_physics_jitter_fix(double p_threshold); double get_physics_jitter_fix() const; - virtual void set_target_fps(int p_fps); - virtual int get_target_fps() const; + virtual void set_max_fps(int p_fps); + virtual int get_max_fps() const; virtual double get_frames_per_second() const { return _fps; } @@ -134,7 +146,7 @@ public: Dictionary get_version_info() const; Dictionary get_author_info() const; - Array get_copyright_info() const; + TypedArray<Dictionary> get_copyright_info() const; Dictionary get_donor_info() const; Dictionary get_license_info() const; String get_license_text() const; @@ -151,6 +163,11 @@ public: bool is_validation_layers_enabled() const; int32_t get_gpu_index() const; + void startup_begin(); + void startup_benchmark_begin_measure(const String &p_what); + void startup_benchmark_end_measure(); + void startup_dump(const String &p_to_file); + Engine(); virtual ~Engine() {} }; diff --git a/core/config/project_settings.cpp b/core/config/project_settings.cpp index 5c4bcc687a..7aaa9a46b7 100644 --- a/core/config/project_settings.cpp +++ b/core/config/project_settings.cpp @@ -41,7 +41,10 @@ #include "core/os/keyboard.h" #include "core/variant/variant_parser.h" #include "core/version.h" + +#ifdef TOOLS_ENABLED #include "modules/modules_enabled.gen.h" // For mono. +#endif // TOOLS_ENABLED const String ProjectSettings::PROJECT_DATA_DIR_NAME_SUFFIX = "godot"; @@ -72,13 +75,14 @@ String ProjectSettings::get_safe_project_name() const { } String ProjectSettings::get_imported_files_path() const { - return get_project_data_path().plus_file("imported"); + return get_project_data_path().path_join("imported"); } +#ifdef TOOLS_ENABLED // Returns the features that a project must have when opened with this build of Godot. // This is used by the project manager to provide the initial_settings for config/features. const PackedStringArray ProjectSettings::get_required_features() { - PackedStringArray features = PackedStringArray(); + PackedStringArray features; features.append(VERSION_BRANCH); #ifdef REAL_T_IS_DOUBLE features.append("Double Precision"); @@ -97,19 +101,28 @@ const PackedStringArray ProjectSettings::_get_supported_features() { features.append(VERSION_BRANCH "." _MKSTR(VERSION_PATCH)); features.append(VERSION_FULL_CONFIG); features.append(VERSION_FULL_BUILD); - // For now, assume Vulkan is always supported. - // This should be removed if it's possible to build the editor without Vulkan. - features.append("Vulkan Clustered"); - features.append("Vulkan Mobile"); + +#ifdef VULKAN_ENABLED + features.append("Forward Plus"); + features.append("Mobile"); +#endif + +#ifdef GLES3_ENABLED + features.append("GL Compatibility"); +#endif return features; } // Returns the features that this project needs but this build of Godot lacks. const PackedStringArray ProjectSettings::get_unsupported_features(const PackedStringArray &p_project_features) { - PackedStringArray unsupported_features = PackedStringArray(); + PackedStringArray unsupported_features; PackedStringArray supported_features = singleton->_get_supported_features(); for (int i = 0; i < p_project_features.size(); i++) { if (!supported_features.has(p_project_features[i])) { + // Temporary compatibility code to ease upgrade to 4.0 beta 2+. + if (p_project_features[i].begins_with("Vulkan")) { + continue; + } unsupported_features.append(p_project_features[i]); } } @@ -137,10 +150,26 @@ const PackedStringArray ProjectSettings::_trim_to_supported_features(const Packe features.sort(); return features; } +#endif // TOOLS_ENABLED String ProjectSettings::localize_path(const String &p_path) const { - if (resource_path.is_empty() || p_path.begins_with("res://") || p_path.begins_with("user://") || - (p_path.is_absolute_path() && !p_path.begins_with(resource_path))) { + if (resource_path.is_empty() || (p_path.is_absolute_path() && !p_path.begins_with(resource_path))) { + return p_path.simplify_path(); + } + + // Check if we have a special path (like res://) or a protocol identifier. + int p = p_path.find("://"); + bool found = false; + if (p > 0) { + found = true; + for (int i = 0; i < p; i++) { + if (!is_ascii_alphanumeric_char(p_path[i])) { + found = false; + break; + } + } + } + if (found) { return p_path.simplify_path(); } @@ -157,12 +186,12 @@ String ProjectSettings::localize_path(const String &p_path) const { // in an absolute path that just happens to contain this string but points to a // different folder (e.g. "/my/project" as resource_path would be contained in // "/my/project_data", even though the latter is not part of res://. - // `plus_file("")` is an easy way to ensure we have a trailing '/'. - const String res_path = resource_path.plus_file(""); + // `path_join("")` is an easy way to ensure we have a trailing '/'. + const String res_path = resource_path.path_join(""); // DirAccess::get_current_dir() is not guaranteed to return a path that with a trailing '/', // so we must make sure we have it as well in order to compare with 'res_path'. - cwd = cwd.plus_file(""); + cwd = cwd.path_join(""); if (!cwd.begins_with(res_path)) { return p_path; @@ -442,6 +471,15 @@ void ProjectSettings::_convert_to_last_version(int p_from_version) { * If nothing was found, error out. */ Error ProjectSettings::_setup(const String &p_path, const String &p_main_pack, bool p_upwards, bool p_ignore_override) { + if (!OS::get_singleton()->get_resource_dir().is_empty()) { + // OS will call ProjectSettings->get_resource_path which will be empty if not overridden! + // If the OS would rather use a specific location, then it will not be empty. + resource_path = OS::get_singleton()->get_resource_dir().replace("\\", "/"); + if (!resource_path.is_empty() && resource_path[resource_path.length() - 1] == '/') { + resource_path = resource_path.substr(0, resource_path.length() - 1); // Chop end. + } + } + // If looking for files in a network client, use it directly if (FileAccessNetworkClient::get_singleton()) { @@ -463,7 +501,7 @@ Error ProjectSettings::_setup(const String &p_path, const String &p_main_pack, b if (err == OK && !p_ignore_override) { // Load override from location of the main pack // Optional, we don't mind if it fails - _load_settings_text(p_main_pack.get_base_dir().plus_file("override.cfg")); + _load_settings_text(p_main_pack.get_base_dir().path_join("override.cfg")); } return err; } @@ -491,14 +529,14 @@ Error ProjectSettings::_setup(const String &p_path, const String &p_main_pack, b #ifdef MACOS_ENABLED if (!found) { // Attempt to load PCK from macOS .app bundle resources. - found = _load_resource_pack(OS::get_singleton()->get_bundle_resource_dir().plus_file(exec_basename + ".pck")) || _load_resource_pack(OS::get_singleton()->get_bundle_resource_dir().plus_file(exec_filename + ".pck")); + found = _load_resource_pack(OS::get_singleton()->get_bundle_resource_dir().path_join(exec_basename + ".pck")) || _load_resource_pack(OS::get_singleton()->get_bundle_resource_dir().path_join(exec_filename + ".pck")); } #endif if (!found) { // Try to load data pack at the location of the executable. // As mentioned above, we have two potential names to attempt. - found = _load_resource_pack(exec_dir.plus_file(exec_basename + ".pck")) || _load_resource_pack(exec_dir.plus_file(exec_filename + ".pck")); + found = _load_resource_pack(exec_dir.path_join(exec_basename + ".pck")) || _load_resource_pack(exec_dir.path_join(exec_filename + ".pck")); } if (!found) { @@ -514,7 +552,7 @@ Error ProjectSettings::_setup(const String &p_path, const String &p_main_pack, b // Load overrides from the PCK and the executable location. // Optional, we don't mind if either fails. _load_settings_text("res://override.cfg"); - _load_settings_text(exec_path.get_base_dir().plus_file("override.cfg")); + _load_settings_text(exec_path.get_base_dir().path_join("override.cfg")); } return err; } @@ -524,13 +562,6 @@ Error ProjectSettings::_setup(const String &p_path, const String &p_main_pack, b // (Only Android -when reading from pck- and iOS use this.) if (!OS::get_singleton()->get_resource_dir().is_empty()) { - // OS will call ProjectSettings->get_resource_path which will be empty if not overridden! - // If the OS would rather use a specific location, then it will not be empty. - resource_path = OS::get_singleton()->get_resource_dir().replace("\\", "/"); - if (!resource_path.is_empty() && resource_path[resource_path.length() - 1] == '/') { - resource_path = resource_path.substr(0, resource_path.length() - 1); // Chop end. - } - Error err = _load_settings_text_or_binary("res://project.godot", "res://project.binary"); if (err == OK && !p_ignore_override) { // Optional, we don't mind if it fails. @@ -554,10 +585,10 @@ Error ProjectSettings::_setup(const String &p_path, const String &p_main_pack, b // Set the resource path early so things can be resolved when loading. resource_path = current_dir; resource_path = resource_path.replace("\\", "/"); // Windows path to Unix path just in case. - err = _load_settings_text_or_binary(current_dir.plus_file("project.godot"), current_dir.plus_file("project.binary")); + err = _load_settings_text_or_binary(current_dir.path_join("project.godot"), current_dir.path_join("project.binary")); if (err == OK && !p_ignore_override) { // Optional, we don't mind if it fails. - _load_settings_text(current_dir.plus_file("override.cfg")); + _load_settings_text(current_dir.path_join("override.cfg")); found = true; break; } @@ -588,7 +619,7 @@ Error ProjectSettings::_setup(const String &p_path, const String &p_main_pack, b Error ProjectSettings::setup(const String &p_path, const String &p_main_pack, bool p_upwards, bool p_ignore_override) { Error err = _setup(p_path, p_main_pack, p_upwards, p_ignore_override); if (err == OK) { - String custom_settings = GLOBAL_DEF("application/config/project_settings_override", ""); + String custom_settings = GLOBAL_GET("application/config/project_settings_override"); if (!custom_settings.is_empty()) { _load_settings_text(custom_settings); } @@ -683,7 +714,7 @@ Error ProjectSettings::_load_settings_text(const String &p_path) { // If we're loading a project.godot from source code, we can operate some // ProjectSettings conversions if need be. _convert_to_last_version(config_version); - last_save_time = FileAccess::get_modified_time(get_resource_path().plus_file("project.godot")); + last_save_time = FileAccess::get_modified_time(get_resource_path().path_join("project.godot")); return OK; } ERR_FAIL_COND_V_MSG(err != OK, err, "Error parsing " + p_path + " at line " + itos(lines) + ": " + error_text + " File might be corrupted."); @@ -762,14 +793,14 @@ void ProjectSettings::clear(const String &p_name) { } Error ProjectSettings::save() { - Error error = save_custom(get_resource_path().plus_file("project.godot")); + Error error = save_custom(get_resource_path().path_join("project.godot")); if (error == OK) { - last_save_time = FileAccess::get_modified_time(get_resource_path().plus_file("project.godot")); + last_save_time = FileAccess::get_modified_time(get_resource_path().path_join("project.godot")); } return error; } -Error ProjectSettings::_save_settings_binary(const String &p_file, const RBMap<String, List<String>> &props, const CustomMap &p_custom, const String &p_custom_features) { +Error ProjectSettings::_save_settings_binary(const String &p_file, const RBMap<String, List<String>> &p_props, const CustomMap &p_custom, const String &p_custom_features) { Error err; Ref<FileAccess> file = FileAccess::open(p_file, FileAccess::WRITE, &err); ERR_FAIL_COND_V_MSG(err != OK, err, "Couldn't save project.binary at " + p_file + "."); @@ -779,7 +810,7 @@ Error ProjectSettings::_save_settings_binary(const String &p_file, const RBMap<S int count = 0; - for (const KeyValue<String, List<String>> &E : props) { + for (const KeyValue<String, List<String>> &E : p_props) { count += E.value.size(); } @@ -805,7 +836,7 @@ Error ProjectSettings::_save_settings_binary(const String &p_file, const RBMap<S file->store_32(count); //store how many properties are saved } - for (const KeyValue<String, List<String>> &E : props) { + for (const KeyValue<String, List<String>> &E : p_props) { for (const String &key : E.value) { String k = key; if (!E.key.is_empty()) { @@ -837,7 +868,7 @@ Error ProjectSettings::_save_settings_binary(const String &p_file, const RBMap<S return OK; } -Error ProjectSettings::_save_settings_text(const String &p_file, const RBMap<String, List<String>> &props, const CustomMap &p_custom, const String &p_custom_features) { +Error ProjectSettings::_save_settings_text(const String &p_file, const RBMap<String, List<String>> &p_props, const CustomMap &p_custom, const String &p_custom_features) { Error err; Ref<FileAccess> file = FileAccess::open(p_file, FileAccess::WRITE, &err); @@ -858,8 +889,8 @@ Error ProjectSettings::_save_settings_text(const String &p_file, const RBMap<Str } file->store_string("\n"); - for (const KeyValue<String, List<String>> &E : props) { - if (E.key != props.begin()->key) { + for (const KeyValue<String, List<String>> &E : p_props) { + if (E.key != p_props.begin()->key) { file->store_string("\n"); } @@ -895,13 +926,14 @@ Error ProjectSettings::_save_custom_bnd(const String &p_file) { // add other par Error ProjectSettings::save_custom(const String &p_path, const CustomMap &p_custom, const Vector<String> &p_custom_features, bool p_merge_with_current) { ERR_FAIL_COND_V_MSG(p_path.is_empty(), ERR_INVALID_PARAMETER, "Project settings save path cannot be empty."); +#ifdef TOOLS_ENABLED PackedStringArray project_features = get_setting("application/config/features"); // If there is no feature list currently present, force one to generate. if (project_features.is_empty()) { project_features = ProjectSettings::get_required_features(); } // Check the rendering API. - const String rendering_api = has_setting("rendering/quality/driver/driver_name") ? (String)get_setting("rendering/quality/driver/driver_name") : String(); + const String rendering_api = has_setting("rendering/renderer/rendering_method") ? (String)get_setting("rendering/renderer/rendering_method") : String(); if (!rendering_api.is_empty()) { // Add the rendering API as a project feature if it doesn't already exist. if (!project_features.has(rendering_api)) { @@ -909,7 +941,7 @@ Error ProjectSettings::save_custom(const String &p_path, const CustomMap &p_cust } } // Check for the existence of a csproj file. - if (FileAccess::exists(get_resource_path().plus_file(get_safe_project_name() + ".csproj"))) { + if (FileAccess::exists(get_resource_path().path_join(get_safe_project_name() + ".csproj"))) { // If there is a csproj file, add the C# feature if it doesn't already exist. if (!project_features.has("C#")) { project_features.append("C#"); @@ -922,6 +954,7 @@ Error ProjectSettings::save_custom(const String &p_path, const CustomMap &p_cust } project_features = _trim_to_supported_features(project_features); set_setting("application/config/features", project_features); +#endif // TOOLS_ENABLED RBSet<_VCSort> vclist; @@ -962,7 +995,7 @@ Error ProjectSettings::save_custom(const String &p_path, const CustomMap &p_cust vclist.insert(vc); } - RBMap<String, List<String>> props; + RBMap<String, List<String>> save_props; for (const _VCSort &E : vclist) { String category = E.name; @@ -976,24 +1009,24 @@ Error ProjectSettings::save_custom(const String &p_path, const CustomMap &p_cust category = category.substr(0, div); name = name.substr(div + 1, name.size()); } - props[category].push_back(name); + save_props[category].push_back(name); } - String custom_features; + String save_features; for (int i = 0; i < p_custom_features.size(); i++) { if (i > 0) { - custom_features += ","; + save_features += ","; } String f = p_custom_features[i].strip_edges().replace("\"", ""); - custom_features += f; + save_features += f; } if (p_path.ends_with(".godot") || p_path.ends_with("override.cfg")) { - return _save_settings_text(p_path, props, p_custom, custom_features); + return _save_settings_text(p_path, save_props, p_custom, save_features); } else if (p_path.ends_with(".binary")) { - return _save_settings_binary(p_path, props, p_custom, custom_features); + return _save_settings_binary(p_path, save_props, p_custom, save_features); } else { ERR_FAIL_V_MSG(ERR_FILE_UNRECOGNIZED, "Unknown config file format: " + p_path + "."); } @@ -1004,7 +1037,7 @@ Variant _GLOBAL_DEF(const String &p_var, const Variant &p_default, bool p_restar if (!ProjectSettings::get_singleton()->has_setting(p_var)) { ProjectSettings::get_singleton()->set(p_var, p_default); } - ret = ProjectSettings::get_singleton()->get(p_var); + ret = GLOBAL_GET(p_var); ProjectSettings::get_singleton()->set_initial_value(p_var, p_default); ProjectSettings::get_singleton()->set_builtin_order(p_var); @@ -1070,7 +1103,7 @@ bool ProjectSettings::is_using_datapack() const { return using_datapack; } -bool ProjectSettings::property_can_revert(const String &p_name) { +bool ProjectSettings::_property_can_revert(const StringName &p_name) const { if (!props.has(p_name)) { return false; } @@ -1078,12 +1111,13 @@ bool ProjectSettings::property_can_revert(const String &p_name) { return props[p_name].initial != props[p_name].variant; } -Variant ProjectSettings::property_get_revert(const String &p_name) { +bool ProjectSettings::_property_get_revert(const StringName &p_name, Variant &r_property) const { if (!props.has(p_name)) { - return Variant(); + return false; } - return props[p_name].initial; + r_property = props[p_name].initial; + return true; } void ProjectSettings::set_setting(const String &p_setting, const Variant &p_value) { @@ -1129,13 +1163,12 @@ void ProjectSettings::_bind_methods() { ClassDB::bind_method(D_METHOD("get_order", "name"), &ProjectSettings::get_order); ClassDB::bind_method(D_METHOD("set_initial_value", "name", "value"), &ProjectSettings::set_initial_value); ClassDB::bind_method(D_METHOD("add_property_info", "hint"), &ProjectSettings::_add_property_info_bind); + ClassDB::bind_method(D_METHOD("set_restart_if_changed", "name", "restart"), &ProjectSettings::set_restart_if_changed); ClassDB::bind_method(D_METHOD("clear", "name"), &ProjectSettings::clear); ClassDB::bind_method(D_METHOD("localize_path", "path"), &ProjectSettings::localize_path); ClassDB::bind_method(D_METHOD("globalize_path", "path"), &ProjectSettings::globalize_path); ClassDB::bind_method(D_METHOD("save"), &ProjectSettings::save); ClassDB::bind_method(D_METHOD("load_resource_pack", "pack", "replace_files", "offset"), &ProjectSettings::_load_resource_pack, DEFVAL(true), DEFVAL(0)); - ClassDB::bind_method(D_METHOD("property_can_revert", "name"), &ProjectSettings::property_can_revert); - ClassDB::bind_method(D_METHOD("property_get_revert", "name"), &ProjectSettings::property_get_revert); ClassDB::bind_method(D_METHOD("save_custom", "file"), &ProjectSettings::_save_custom_bnd); } @@ -1183,16 +1216,26 @@ ProjectSettings::ProjectSettings() { GLOBAL_DEF("application/config/custom_user_dir_name", ""); GLOBAL_DEF("application/config/project_settings_override", ""); - GLOBAL_DEF_BASIC("display/window/size/viewport_width", 1024); + // The default window size is tuned to: + // - Have a 16:9 aspect ratio, + // - Have both dimensions divisible by 8 to better play along with video recording, + // - Be displayable correctly in windowed mode on a 1366×768 display (tested on Windows 10 with default settings). + GLOBAL_DEF_BASIC("display/window/size/viewport_width", 1152); custom_prop_info["display/window/size/viewport_width"] = PropertyInfo(Variant::INT, "display/window/size/viewport_width", PROPERTY_HINT_RANGE, "0,7680,1,or_greater"); // 8K resolution - GLOBAL_DEF_BASIC("display/window/size/viewport_height", 600); + GLOBAL_DEF_BASIC("display/window/size/viewport_height", 648); custom_prop_info["display/window/size/viewport_height"] = PropertyInfo(Variant::INT, "display/window/size/viewport_height", PROPERTY_HINT_RANGE, "0,4320,1,or_greater"); // 8K resolution + GLOBAL_DEF_BASIC("display/window/size/mode", 0); + custom_prop_info["display/window/size/mode"] = PropertyInfo(Variant::INT, "display/window/size/mode", PROPERTY_HINT_ENUM, "Windowed,Minimized,Maximized,Fullscreen,Exclusive Fullscreen"); + GLOBAL_DEF_BASIC("display/window/size/resizable", true); GLOBAL_DEF_BASIC("display/window/size/borderless", false); - GLOBAL_DEF_BASIC("display/window/size/fullscreen", false); GLOBAL_DEF("display/window/size/always_on_top", false); + GLOBAL_DEF("display/window/size/transparent", false); + GLOBAL_DEF("display/window/size/extend_to_title", false); + GLOBAL_DEF("display/window/size/no_focus", false); + GLOBAL_DEF("display/window/size/window_width_override", 0); custom_prop_info["display/window/size/window_width_override"] = PropertyInfo(Variant::INT, "display/window/size/window_width_override", PROPERTY_HINT_RANGE, "0,7680,1,or_greater"); // 8K resolution GLOBAL_DEF("display/window/size/window_height_override", 0); @@ -1203,12 +1246,12 @@ ProjectSettings::ProjectSettings() { GLOBAL_DEF_BASIC("audio/buses/default_bus_layout", "res://default_bus_layout.tres"); custom_prop_info["audio/buses/default_bus_layout"] = PropertyInfo(Variant::STRING, "audio/buses/default_bus_layout", PROPERTY_HINT_FILE, "*.tres"); - GLOBAL_DEF_RST("audio/general/2d_panning_strength", 1.0f); - custom_prop_info["audio/general/2d_panning_strength"] = PropertyInfo(Variant::FLOAT, "audio/general/2d_panning_strength", PROPERTY_HINT_RANGE, "0,4,0.01"); - GLOBAL_DEF_RST("audio/general/3d_panning_strength", 1.0f); - custom_prop_info["audio/general/3d_panning_strength"] = PropertyInfo(Variant::FLOAT, "audio/general/3d_panning_strength", PROPERTY_HINT_RANGE, "0,4,0.01"); + GLOBAL_DEF_RST("audio/general/2d_panning_strength", 0.5f); + custom_prop_info["audio/general/2d_panning_strength"] = PropertyInfo(Variant::FLOAT, "audio/general/2d_panning_strength", PROPERTY_HINT_RANGE, "0,2,0.01"); + GLOBAL_DEF_RST("audio/general/3d_panning_strength", 0.5f); + custom_prop_info["audio/general/3d_panning_strength"] = PropertyInfo(Variant::FLOAT, "audio/general/3d_panning_strength", PROPERTY_HINT_RANGE, "0,2,0.01"); - PackedStringArray extensions = PackedStringArray(); + PackedStringArray extensions; extensions.push_back("gd"); if (Engine::get_singleton()->has_singleton("GodotSharp")) { extensions.push_back("cs"); @@ -1233,6 +1276,7 @@ ProjectSettings::ProjectSettings() { GLOBAL_DEF("physics/2d/run_on_separate_thread", false); GLOBAL_DEF("physics/3d/run_on_separate_thread", false); + GLOBAL_DEF("debug/disable_touch", false); GLOBAL_DEF("debug/settings/profiler/max_functions", 16384); custom_prop_info["debug/settings/profiler/max_functions"] = PropertyInfo(Variant::INT, "debug/settings/profiler/max_functions", PROPERTY_HINT_RANGE, "128,65535,1"); @@ -1249,6 +1293,22 @@ ProjectSettings::ProjectSettings() { GLOBAL_DEF("compression/formats/gzip/compression_level", Compression::gzip_level); custom_prop_info["compression/formats/gzip/compression_level"] = PropertyInfo(Variant::INT, "compression/formats/gzip/compression_level", PROPERTY_HINT_RANGE, "-1,9,1"); + GLOBAL_DEF("debug/settings/crash_handler/message", + String("Please include this when reporting the bug to the project developer.")); + GLOBAL_DEF("debug/settings/crash_handler/message.editor", + String("Please include this when reporting the bug on: https://github.com/godotengine/godot/issues")); + GLOBAL_DEF_RST("rendering/occlusion_culling/bvh_build_quality", 2); + GLOBAL_DEF("memory/limits/multithreaded_server/rid_pool_prealloc", 60); + GLOBAL_DEF_RST("internationalization/rendering/force_right_to_left_layout_direction", false); + + GLOBAL_DEF("gui/timers/incremental_search_max_interval_msec", 2000); + ProjectSettings::get_singleton()->set_custom_property_info("gui/timers/incremental_search_max_interval_msec", PropertyInfo(Variant::INT, "gui/timers/incremental_search_max_interval_msec", PROPERTY_HINT_RANGE, "0,10000,1,or_greater")); // No negative numbers. + + GLOBAL_DEF("rendering/rendering_device/staging_buffer/block_size_kb", 256); + GLOBAL_DEF("rendering/rendering_device/staging_buffer/max_size_mb", 128); + GLOBAL_DEF("rendering/rendering_device/staging_buffer/texture_upload_region_size_px", 64); + GLOBAL_DEF("rendering/rendering_device/vulkan/max_descriptors_per_pool", 64); + // These properties will not show up in the dialog nor in the documentation. If you want to exclude whole groups, see _get_property_list() method. GLOBAL_DEF_INTERNAL("application/config/features", PackedStringArray()); GLOBAL_DEF_INTERNAL("internationalization/locale/translation_remaps", PackedStringArray()); diff --git a/core/config/project_settings.h b/core/config/project_settings.h index c3992a4db2..960dfe0395 100644 --- a/core/config/project_settings.h +++ b/core/config/project_settings.h @@ -45,11 +45,14 @@ public: static const String PROJECT_DATA_DIR_NAME_SUFFIX; enum { - //properties that are not for built in values begin from this value, so builtin ones are displayed first + // Properties that are not for built in values begin from this value, so builtin ones are displayed first. NO_BUILTIN_ORDER_BASE = 1 << 16 }; + +#ifdef TOOLS_ENABLED const static PackedStringArray get_required_features(); const static PackedStringArray get_unsupported_features(const PackedStringArray &p_project_features); +#endif // TOOLS_ENABLED struct AutoloadInfo { StringName name; @@ -102,6 +105,8 @@ protected: bool _set(const StringName &p_name, const Variant &p_value); bool _get(const StringName &p_name, Variant &r_ret) const; void _get_property_list(List<PropertyInfo> *p_list) const; + bool _property_can_revert(const StringName &p_name) const; + bool _property_get_revert(const StringName &p_name, Variant &r_property) const; static ProjectSettings *singleton; @@ -114,8 +119,10 @@ protected: Error _save_custom_bnd(const String &p_file); +#ifdef TOOLS_ENABLED const static PackedStringArray _get_supported_features(); const static PackedStringArray _trim_to_supported_features(const PackedStringArray &p_project_features); +#endif // TOOLS_ENABLED void _convert_to_last_version(int p_from_version); @@ -147,9 +154,6 @@ public: void set_ignore_value_in_docs(const String &p_name, bool p_ignore); bool get_ignore_value_in_docs(const String &p_name) const; - bool property_can_revert(const String &p_name); - Variant property_get_revert(const String &p_name); - String get_project_data_dir_name() const; String get_project_data_path() const; String get_resource_path() const; diff --git a/core/core_bind.cpp b/core/core_bind.cpp index 630bd68e65..1fe34cb4fd 100644 --- a/core/core_bind.cpp +++ b/core/core_bind.cpp @@ -39,6 +39,7 @@ #include "core/math/geometry_2d.h" #include "core/math/geometry_3d.h" #include "core/os/keyboard.h" +#include "core/variant/typed_array.h" namespace core_bind { @@ -252,6 +253,10 @@ Error OS::shell_open(String p_uri) { return ::OS::get_singleton()->shell_open(p_uri); } +String OS::read_string_from_stdin(bool p_block) { + return ::OS::get_singleton()->get_stdin_string(true); +} + int OS::execute(const String &p_path, const Vector<String> &p_arguments, Array r_output, bool p_read_stderr, bool p_open_console) { List<String> args; for (int i = 0; i < p_arguments.size(); i++) { @@ -321,6 +326,18 @@ String OS::get_name() const { return ::OS::get_singleton()->get_name(); } +String OS::get_distribution_name() const { + return ::OS::get_singleton()->get_distribution_name(); +} + +String OS::get_version() const { + return ::OS::get_singleton()->get_version(); +} + +Vector<String> OS::get_video_adapter_driver_info() const { + return ::OS::get_singleton()->get_video_adapter_driver_info(); +} + Vector<String> OS::get_cmdline_args() { List<String> cmdline = ::OS::get_singleton()->get_cmdline_args(); Vector<String> cmdlinev; @@ -416,10 +433,6 @@ void OS::delay_msec(int p_msec) const { ::OS::get_singleton()->delay_usec(int64_t(p_msec) * 1000); } -bool OS::can_use_threads() const { - return ::OS::get_singleton()->can_use_threads(); -} - bool OS::is_userfs_persistent() const { return ::OS::get_singleton()->is_userfs_persistent(); } @@ -436,118 +449,6 @@ bool OS::is_stdout_verbose() const { return ::OS::get_singleton()->is_stdout_verbose(); } -void OS::dump_memory_to_file(const String &p_file) { - ::OS::get_singleton()->dump_memory_to_file(p_file.utf8().get_data()); -} - -struct OSCoreBindImg { - String path; - Size2 size; - int fmt = 0; - ObjectID id; - int vram = 0; - bool operator<(const OSCoreBindImg &p_img) const { return vram == p_img.vram ? id < p_img.id : vram > p_img.vram; } -}; - -void OS::print_all_textures_by_size() { - List<OSCoreBindImg> imgs; - uint64_t total = 0; - { - List<Ref<Resource>> rsrc; - ResourceCache::get_cached_resources(&rsrc); - - for (Ref<Resource> &res : rsrc) { - if (!res->is_class("Texture")) { - continue; - } - - Size2 size = res->call("get_size"); - int fmt = res->call("get_format"); - - OSCoreBindImg img; - img.size = size; - img.fmt = fmt; - img.path = res->get_path(); - img.vram = Image::get_image_data_size(img.size.width, img.size.height, Image::Format(img.fmt)); - img.id = res->get_instance_id(); - total += img.vram; - imgs.push_back(img); - } - } - - imgs.sort(); - - if (imgs.size() == 0) { - print_line("No textures seem used in this project."); - } else { - print_line("Textures currently in use, sorted by VRAM usage:\n" - "Path - VRAM usage (Dimensions)"); - } - - for (const OSCoreBindImg &img : imgs) { - print_line(vformat("%s - %s %s", - img.path, - String::humanize_size(img.vram), - img.size)); - } - - print_line(vformat("Total VRAM usage: %s.", String::humanize_size(total))); -} - -void OS::print_resources_by_type(const Vector<String> &p_types) { - ERR_FAIL_COND_MSG(p_types.size() == 0, - "At least one type should be provided to print resources by type."); - - print_line(vformat("Resources currently in use for the following types: %s", p_types)); - - RBMap<String, int> type_count; - List<Ref<Resource>> resources; - ResourceCache::get_cached_resources(&resources); - - for (const Ref<Resource> &r : resources) { - bool found = false; - - for (int i = 0; i < p_types.size(); i++) { - if (r->is_class(p_types[i])) { - found = true; - } - } - if (!found) { - continue; - } - - if (!type_count.has(r->get_class())) { - type_count[r->get_class()] = 0; - } - - type_count[r->get_class()]++; - - print_line(vformat("%s: %s", r->get_class(), r->get_path())); - - List<StringName> metas; - r->get_meta_list(&metas); - for (const StringName &meta : metas) { - print_line(vformat(" %s: %s", meta, r->get_meta(meta))); - } - } - - for (const KeyValue<String, int> &E : type_count) { - print_line(vformat("%s count: %d", E.key, E.value)); - } -} - -void OS::print_all_resources(const String &p_to_file) { - ::OS::get_singleton()->print_all_resources(p_to_file); -} - -void OS::print_resources_in_use(bool p_short) { - ::OS::get_singleton()->print_resources_in_use(p_short); -} - -void OS::dump_resources_to_file(const String &p_file) { - ::OS::get_singleton()->dump_resources_to_file(p_file.utf8().get_data()); -} - Error OS::move_to_trash(const String &p_path) const { return ::OS::get_singleton()->move_to_trash(p_path); } @@ -633,6 +534,7 @@ void OS::_bind_methods() { ClassDB::bind_method(D_METHOD("get_system_fonts"), &OS::get_system_fonts); ClassDB::bind_method(D_METHOD("get_system_font_path", "font_name", "bold", "italic"), &OS::get_system_font_path, DEFVAL(false), DEFVAL(false)); ClassDB::bind_method(D_METHOD("get_executable_path"), &OS::get_executable_path); + ClassDB::bind_method(D_METHOD("read_string_from_stdin", "block"), &OS::read_string_from_stdin, DEFVAL(true)); ClassDB::bind_method(D_METHOD("execute", "path", "arguments", "output", "read_stderr", "open_console"), &OS::execute, DEFVAL(Array()), DEFVAL(false), DEFVAL(false)); ClassDB::bind_method(D_METHOD("create_process", "path", "arguments", "open_console"), &OS::create_process, DEFVAL(false)); ClassDB::bind_method(D_METHOD("create_instance", "arguments"), &OS::create_instance); @@ -646,9 +548,13 @@ void OS::_bind_methods() { ClassDB::bind_method(D_METHOD("has_environment", "variable"), &OS::has_environment); ClassDB::bind_method(D_METHOD("get_name"), &OS::get_name); + ClassDB::bind_method(D_METHOD("get_distribution_name"), &OS::get_distribution_name); + ClassDB::bind_method(D_METHOD("get_version"), &OS::get_version); ClassDB::bind_method(D_METHOD("get_cmdline_args"), &OS::get_cmdline_args); ClassDB::bind_method(D_METHOD("get_cmdline_user_args"), &OS::get_cmdline_user_args); + ClassDB::bind_method(D_METHOD("get_video_adapter_driver_info"), &OS::get_video_adapter_driver_info); + ClassDB::bind_method(D_METHOD("set_restart_on_exit", "restart", "arguments"), &OS::set_restart_on_exit, DEFVAL(Vector<String>())); ClassDB::bind_method(D_METHOD("is_restart_on_exit_set"), &OS::is_restart_on_exit_set); ClassDB::bind_method(D_METHOD("get_restart_on_exit_arguments"), &OS::get_restart_on_exit_arguments); @@ -662,15 +568,8 @@ void OS::_bind_methods() { ClassDB::bind_method(D_METHOD("is_userfs_persistent"), &OS::is_userfs_persistent); ClassDB::bind_method(D_METHOD("is_stdout_verbose"), &OS::is_stdout_verbose); - ClassDB::bind_method(D_METHOD("can_use_threads"), &OS::can_use_threads); - ClassDB::bind_method(D_METHOD("is_debug_build"), &OS::is_debug_build); - ClassDB::bind_method(D_METHOD("dump_memory_to_file", "file"), &OS::dump_memory_to_file); - ClassDB::bind_method(D_METHOD("dump_resources_to_file", "file"), &OS::dump_resources_to_file); - ClassDB::bind_method(D_METHOD("print_resources_in_use", "short"), &OS::print_resources_in_use, DEFVAL(false)); - ClassDB::bind_method(D_METHOD("print_all_resources", "tofile"), &OS::print_all_resources, DEFVAL("")); - ClassDB::bind_method(D_METHOD("get_static_memory_usage"), &OS::get_static_memory_usage); ClassDB::bind_method(D_METHOD("get_static_memory_peak_usage"), &OS::get_static_memory_peak_usage); @@ -682,9 +581,6 @@ void OS::_bind_methods() { ClassDB::bind_method(D_METHOD("get_cache_dir"), &OS::get_cache_dir); ClassDB::bind_method(D_METHOD("get_unique_id"), &OS::get_unique_id); - ClassDB::bind_method(D_METHOD("print_all_textures_by_size"), &OS::print_all_textures_by_size); - ClassDB::bind_method(D_METHOD("print_resources_by_type", "types"), &OS::print_resources_by_type); - ClassDB::bind_method(D_METHOD("get_keycode_string", "code"), &OS::get_keycode_string); ClassDB::bind_method(D_METHOD("is_keycode_unicode", "code"), &OS::is_keycode_unicode); ClassDB::bind_method(D_METHOD("find_keycode_from_string", "string"), &OS::find_keycode_from_string); @@ -709,8 +605,8 @@ void OS::_bind_methods() { ADD_PROPERTY_DEFAULT("low_processor_usage_mode", false); ADD_PROPERTY_DEFAULT("low_processor_usage_mode_sleep_usec", 6900); - BIND_ENUM_CONSTANT(VIDEO_DRIVER_VULKAN); - BIND_ENUM_CONSTANT(VIDEO_DRIVER_OPENGL_3); + BIND_ENUM_CONSTANT(RENDERING_DRIVER_VULKAN); + BIND_ENUM_CONSTANT(RENDERING_DRIVER_OPENGL3); BIND_ENUM_CONSTANT(DAY_SUNDAY); BIND_ENUM_CONSTANT(DAY_MONDAY); @@ -818,7 +714,18 @@ Vector<Point2> Geometry2D::convex_hull(const Vector<Point2> &p_points) { return ::Geometry2D::convex_hull(p_points); } -Array Geometry2D::merge_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) { +TypedArray<PackedVector2Array> Geometry2D::decompose_polygon_in_convex(const Vector<Vector2> &p_polygon) { + Vector<Vector<Point2>> decomp = ::Geometry2D::decompose_polygon_in_convex(p_polygon); + + TypedArray<PackedVector2Array> ret; + + for (int i = 0; i < decomp.size(); ++i) { + ret.push_back(decomp[i]); + } + return ret; +} + +TypedArray<PackedVector2Array> Geometry2D::merge_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) { Vector<Vector<Point2>> polys = ::Geometry2D::merge_polygons(p_polygon_a, p_polygon_b); Array ret; @@ -829,10 +736,10 @@ Array Geometry2D::merge_polygons(const Vector<Vector2> &p_polygon_a, const Vecto return ret; } -Array Geometry2D::clip_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) { +TypedArray<PackedVector2Array> Geometry2D::clip_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) { Vector<Vector<Point2>> polys = ::Geometry2D::clip_polygons(p_polygon_a, p_polygon_b); - Array ret; + TypedArray<PackedVector2Array> ret; for (int i = 0; i < polys.size(); ++i) { ret.push_back(polys[i]); @@ -840,7 +747,7 @@ Array Geometry2D::clip_polygons(const Vector<Vector2> &p_polygon_a, const Vector return ret; } -Array Geometry2D::intersect_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) { +TypedArray<PackedVector2Array> Geometry2D::intersect_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) { Vector<Vector<Point2>> polys = ::Geometry2D::intersect_polygons(p_polygon_a, p_polygon_b); Array ret; @@ -851,7 +758,7 @@ Array Geometry2D::intersect_polygons(const Vector<Vector2> &p_polygon_a, const V return ret; } -Array Geometry2D::exclude_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) { +TypedArray<PackedVector2Array> Geometry2D::exclude_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) { Vector<Vector<Point2>> polys = ::Geometry2D::exclude_polygons(p_polygon_a, p_polygon_b); Array ret; @@ -862,7 +769,7 @@ Array Geometry2D::exclude_polygons(const Vector<Vector2> &p_polygon_a, const Vec return ret; } -Array Geometry2D::clip_polyline_with_polygon(const Vector<Vector2> &p_polyline, const Vector<Vector2> &p_polygon) { +TypedArray<PackedVector2Array> Geometry2D::clip_polyline_with_polygon(const Vector<Vector2> &p_polyline, const Vector<Vector2> &p_polygon) { Vector<Vector<Point2>> polys = ::Geometry2D::clip_polyline_with_polygon(p_polyline, p_polygon); Array ret; @@ -873,7 +780,7 @@ Array Geometry2D::clip_polyline_with_polygon(const Vector<Vector2> &p_polyline, return ret; } -Array Geometry2D::intersect_polyline_with_polygon(const Vector<Vector2> &p_polyline, const Vector<Vector2> &p_polygon) { +TypedArray<PackedVector2Array> Geometry2D::intersect_polyline_with_polygon(const Vector<Vector2> &p_polyline, const Vector<Vector2> &p_polygon) { Vector<Vector<Point2>> polys = ::Geometry2D::intersect_polyline_with_polygon(p_polyline, p_polygon); Array ret; @@ -884,7 +791,7 @@ Array Geometry2D::intersect_polyline_with_polygon(const Vector<Vector2> &p_polyl return ret; } -Array Geometry2D::offset_polygon(const Vector<Vector2> &p_polygon, real_t p_delta, PolyJoinType p_join_type) { +TypedArray<PackedVector2Array> Geometry2D::offset_polygon(const Vector<Vector2> &p_polygon, real_t p_delta, PolyJoinType p_join_type) { Vector<Vector<Point2>> polys = ::Geometry2D::offset_polygon(p_polygon, p_delta, ::Geometry2D::PolyJoinType(p_join_type)); Array ret; @@ -895,7 +802,7 @@ Array Geometry2D::offset_polygon(const Vector<Vector2> &p_polygon, real_t p_delt return ret; } -Array Geometry2D::offset_polyline(const Vector<Vector2> &p_polygon, real_t p_delta, PolyJoinType p_join_type, PolyEndType p_end_type) { +TypedArray<PackedVector2Array> Geometry2D::offset_polyline(const Vector<Vector2> &p_polygon, real_t p_delta, PolyJoinType p_join_type, PolyEndType p_end_type) { Vector<Vector<Point2>> polys = ::Geometry2D::offset_polyline(p_polygon, p_delta, ::Geometry2D::PolyJoinType(p_join_type), ::Geometry2D::PolyEndType(p_end_type)); Array ret; @@ -919,14 +826,13 @@ Dictionary Geometry2D::make_atlas(const Vector<Size2> &p_rects) { ::Geometry2D::make_atlas(rects, result, size); - Size2 r_size = size; Vector<Point2> r_result; for (int i = 0; i < result.size(); i++) { r_result.push_back(result[i]); } ret["points"] = r_result; - ret["size"] = r_size; + ret["size"] = size; return ret; } @@ -950,6 +856,7 @@ void Geometry2D::_bind_methods() { ClassDB::bind_method(D_METHOD("triangulate_polygon", "polygon"), &Geometry2D::triangulate_polygon); ClassDB::bind_method(D_METHOD("triangulate_delaunay", "points"), &Geometry2D::triangulate_delaunay); ClassDB::bind_method(D_METHOD("convex_hull", "points"), &Geometry2D::convex_hull); + ClassDB::bind_method(D_METHOD("decompose_polygon_in_convex", "polygon"), &Geometry2D::decompose_polygon_in_convex); ClassDB::bind_method(D_METHOD("merge_polygons", "polygon_a", "polygon_b"), &Geometry2D::merge_polygons); ClassDB::bind_method(D_METHOD("clip_polygons", "polygon_a", "polygon_b"), &Geometry2D::clip_polygons); @@ -988,16 +895,19 @@ Geometry3D *Geometry3D::get_singleton() { return singleton; } -Vector<Plane> Geometry3D::build_box_planes(const Vector3 &p_extents) { - return ::Geometry3D::build_box_planes(p_extents); +TypedArray<Plane> Geometry3D::build_box_planes(const Vector3 &p_extents) { + Variant ret = ::Geometry3D::build_box_planes(p_extents); + return ret; } -Vector<Plane> Geometry3D::build_cylinder_planes(float p_radius, float p_height, int p_sides, Vector3::Axis p_axis) { - return ::Geometry3D::build_cylinder_planes(p_radius, p_height, p_sides, p_axis); +TypedArray<Plane> Geometry3D::build_cylinder_planes(float p_radius, float p_height, int p_sides, Vector3::Axis p_axis) { + Variant ret = ::Geometry3D::build_cylinder_planes(p_radius, p_height, p_sides, p_axis); + return ret; } -Vector<Plane> Geometry3D::build_capsule_planes(float p_radius, float p_height, int p_sides, int p_lats, Vector3::Axis p_axis) { - return ::Geometry3D::build_capsule_planes(p_radius, p_height, p_sides, p_lats, p_axis); +TypedArray<Plane> Geometry3D::build_capsule_planes(float p_radius, float p_height, int p_sides, int p_lats, Vector3::Axis p_axis) { + Variant ret = ::Geometry3D::build_capsule_planes(p_radius, p_height, p_sides, p_lats, p_axis); + return ret; } Vector<Vector3> Geometry3D::get_closest_points_between_segments(const Vector3 &p1, const Vector3 &p2, const Vector3 &q1, const Vector3 &q2) { @@ -1098,636 +1008,6 @@ void Geometry3D::_bind_methods() { ClassDB::bind_method(D_METHOD("clip_polygon", "points", "plane"), &Geometry3D::clip_polygon); } -////// File ////// - -Error File::open_encrypted(const String &p_path, ModeFlags p_mode_flags, const Vector<uint8_t> &p_key) { - Error err = open(p_path, p_mode_flags); - if (err) { - return err; - } - - Ref<FileAccessEncrypted> fae; - fae.instantiate(); - err = fae->open_and_parse(f, p_key, (p_mode_flags == WRITE) ? FileAccessEncrypted::MODE_WRITE_AES256 : FileAccessEncrypted::MODE_READ); - if (err) { - close(); - return err; - } - f = fae; - return OK; -} - -Error File::open_encrypted_pass(const String &p_path, ModeFlags p_mode_flags, const String &p_pass) { - Error err = open(p_path, p_mode_flags); - if (err) { - return err; - } - - Ref<FileAccessEncrypted> fae; - fae.instantiate(); - err = fae->open_and_parse_password(f, p_pass, (p_mode_flags == WRITE) ? FileAccessEncrypted::MODE_WRITE_AES256 : FileAccessEncrypted::MODE_READ); - if (err) { - close(); - return err; - } - - f = fae; - return OK; -} - -Error File::open_compressed(const String &p_path, ModeFlags p_mode_flags, CompressionMode p_compress_mode) { - Ref<FileAccessCompressed> fac; - fac.instantiate(); - fac->configure("GCPF", (Compression::Mode)p_compress_mode); - - Error err = fac->_open(p_path, p_mode_flags); - - if (err) { - return err; - } - - f = fac; - return OK; -} - -Error File::open(const String &p_path, ModeFlags p_mode_flags) { - Error err; - f = FileAccess::open(p_path, p_mode_flags, &err); - if (f.is_valid()) { - f->set_big_endian(big_endian); - } - return err; -} - -void File::flush() { - ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before flushing."); - f->flush(); -} - -void File::close() { - ERR_FAIL_COND_MSG(f.is_null(), "File must be opened."); - f.unref(); -} - -bool File::is_open() const { - return f != nullptr; -} - -String File::get_path() const { - ERR_FAIL_COND_V_MSG(f.is_null(), "", "File must be opened before use, or is lacking read-write permission."); - return f->get_path(); -} - -String File::get_path_absolute() const { - ERR_FAIL_COND_V_MSG(f.is_null(), "", "File must be opened before use, or is lacking read-write permission."); - return f->get_path_absolute(); -} - -void File::seek(int64_t p_position) { - ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use, or is lacking read-write permission."); - ERR_FAIL_COND_MSG(p_position < 0, "Seek position must be a positive integer."); - f->seek(p_position); -} - -void File::seek_end(int64_t p_position) { - ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use, or is lacking read-write permission."); - f->seek_end(p_position); -} - -uint64_t File::get_position() const { - ERR_FAIL_COND_V_MSG(f.is_null(), 0, "File must be opened before use, or is lacking read-write permission."); - return f->get_position(); -} - -uint64_t File::get_length() const { - ERR_FAIL_COND_V_MSG(f.is_null(), 0, "File must be opened before use, or is lacking read-write permission."); - return f->get_length(); -} - -bool File::eof_reached() const { - ERR_FAIL_COND_V_MSG(f.is_null(), false, "File must be opened before use, or is lacking read-write permission."); - return f->eof_reached(); -} - -uint8_t File::get_8() const { - ERR_FAIL_COND_V_MSG(f.is_null(), 0, "File must be opened before use, or is lacking read-write permission."); - return f->get_8(); -} - -uint16_t File::get_16() const { - ERR_FAIL_COND_V_MSG(f.is_null(), 0, "File must be opened before use, or is lacking read-write permission."); - return f->get_16(); -} - -uint32_t File::get_32() const { - ERR_FAIL_COND_V_MSG(f.is_null(), 0, "File must be opened before use, or is lacking read-write permission."); - return f->get_32(); -} - -uint64_t File::get_64() const { - ERR_FAIL_COND_V_MSG(f.is_null(), 0, "File must be opened before use, or is lacking read-write permission."); - return f->get_64(); -} - -float File::get_float() const { - ERR_FAIL_COND_V_MSG(f.is_null(), 0, "File must be opened before use, or is lacking read-write permission."); - return f->get_float(); -} - -double File::get_double() const { - ERR_FAIL_COND_V_MSG(f.is_null(), 0, "File must be opened before use, or is lacking read-write permission."); - return f->get_double(); -} - -real_t File::get_real() const { - ERR_FAIL_COND_V_MSG(f.is_null(), 0, "File must be opened before use, or is lacking read-write permission."); - return f->get_real(); -} - -Vector<uint8_t> File::get_buffer(int64_t p_length) const { - Vector<uint8_t> data; - ERR_FAIL_COND_V_MSG(f.is_null(), data, "File must be opened before use, or is lacking read-write permission."); - - ERR_FAIL_COND_V_MSG(p_length < 0, data, "Length of buffer cannot be smaller than 0."); - if (p_length == 0) { - return data; - } - - Error err = data.resize(p_length); - ERR_FAIL_COND_V_MSG(err != OK, data, "Can't resize data to " + itos(p_length) + " elements."); - - uint8_t *w = data.ptrw(); - int64_t len = f->get_buffer(&w[0], p_length); - - if (len < p_length) { - data.resize(len); - } - - return data; -} - -String File::get_as_text(bool p_skip_cr) const { - ERR_FAIL_COND_V_MSG(f.is_null(), String(), "File must be opened before use, or is lacking read-write permission."); - - uint64_t original_pos = f->get_position(); - const_cast<FileAccess *>(*f)->seek(0); - - String text = f->get_as_utf8_string(p_skip_cr); - - const_cast<FileAccess *>(*f)->seek(original_pos); - - return text; -} - -String File::get_md5(const String &p_path) const { - return FileAccess::get_md5(p_path); -} - -String File::get_sha256(const String &p_path) const { - return FileAccess::get_sha256(p_path); -} - -String File::get_line() const { - ERR_FAIL_COND_V_MSG(f.is_null(), String(), "File must be opened before use, or is lacking read-write permission."); - return f->get_line(); -} - -Vector<String> File::get_csv_line(const String &p_delim) const { - ERR_FAIL_COND_V_MSG(f.is_null(), Vector<String>(), "File must be opened before use, or is lacking read-write permission."); - return f->get_csv_line(p_delim); -} - -/**< use this for files WRITTEN in _big_ endian machines (i.e. amiga/mac) - * It's not about the current CPU type but file formats. - * These flags get reset to false (little endian) on each open - */ - -void File::set_big_endian(bool p_big_endian) { - big_endian = p_big_endian; - if (f.is_valid()) { - f->set_big_endian(p_big_endian); - } -} - -bool File::is_big_endian() { - return big_endian; -} - -Error File::get_error() const { - if (f.is_null()) { - return ERR_UNCONFIGURED; - } - return f->get_error(); -} - -void File::store_8(uint8_t p_dest) { - ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use, or is lacking read-write permission."); - - f->store_8(p_dest); -} - -void File::store_16(uint16_t p_dest) { - ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use, or is lacking read-write permission."); - - f->store_16(p_dest); -} - -void File::store_32(uint32_t p_dest) { - ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use, or is lacking read-write permission."); - - f->store_32(p_dest); -} - -void File::store_64(uint64_t p_dest) { - ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use, or is lacking read-write permission."); - - f->store_64(p_dest); -} - -void File::store_float(float p_dest) { - ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use, or is lacking read-write permission."); - - f->store_float(p_dest); -} - -void File::store_double(double p_dest) { - ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use, or is lacking read-write permission."); - - f->store_double(p_dest); -} - -void File::store_real(real_t p_real) { - ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use, or is lacking read-write permission."); - - f->store_real(p_real); -} - -void File::store_string(const String &p_string) { - ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use, or is lacking read-write permission."); - - f->store_string(p_string); -} - -void File::store_pascal_string(const String &p_string) { - ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use, or is lacking read-write permission."); - - f->store_pascal_string(p_string); -} - -String File::get_pascal_string() { - ERR_FAIL_COND_V_MSG(f.is_null(), "", "File must be opened before use, or is lacking read-write permission."); - - return f->get_pascal_string(); -} - -void File::store_line(const String &p_string) { - ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use, or is lacking read-write permission."); - f->store_line(p_string); -} - -void File::store_csv_line(const Vector<String> &p_values, const String &p_delim) { - ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use, or is lacking read-write permission."); - f->store_csv_line(p_values, p_delim); -} - -void File::store_buffer(const Vector<uint8_t> &p_buffer) { - ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use, or is lacking read-write permission."); - - uint64_t len = p_buffer.size(); - if (len == 0) { - return; - } - - const uint8_t *r = p_buffer.ptr(); - - f->store_buffer(&r[0], len); -} - -bool File::file_exists(const String &p_name) { - return FileAccess::exists(p_name); -} - -void File::store_var(const Variant &p_var, bool p_full_objects) { - ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use, or is lacking read-write permission."); - int len; - Error err = encode_variant(p_var, nullptr, len, p_full_objects); - ERR_FAIL_COND_MSG(err != OK, "Error when trying to encode Variant."); - - Vector<uint8_t> buff; - buff.resize(len); - - uint8_t *w = buff.ptrw(); - err = encode_variant(p_var, &w[0], len, p_full_objects); - ERR_FAIL_COND_MSG(err != OK, "Error when trying to encode Variant."); - - store_32(len); - store_buffer(buff); -} - -Variant File::get_var(bool p_allow_objects) const { - ERR_FAIL_COND_V_MSG(f.is_null(), Variant(), "File must be opened before use, or is lacking read-write permission."); - uint32_t len = get_32(); - Vector<uint8_t> buff = get_buffer(len); - ERR_FAIL_COND_V((uint32_t)buff.size() != len, Variant()); - - const uint8_t *r = buff.ptr(); - - Variant v; - Error err = decode_variant(v, &r[0], len, nullptr, p_allow_objects); - ERR_FAIL_COND_V_MSG(err != OK, Variant(), "Error when trying to encode Variant."); - - return v; -} - -uint64_t File::get_modified_time(const String &p_file) const { - return FileAccess::get_modified_time(p_file); -} - -void File::_bind_methods() { - ClassDB::bind_method(D_METHOD("open_encrypted", "path", "mode_flags", "key"), &File::open_encrypted); - ClassDB::bind_method(D_METHOD("open_encrypted_with_pass", "path", "mode_flags", "pass"), &File::open_encrypted_pass); - ClassDB::bind_method(D_METHOD("open_compressed", "path", "mode_flags", "compression_mode"), &File::open_compressed, DEFVAL(0)); - - ClassDB::bind_method(D_METHOD("open", "path", "flags"), &File::open); - ClassDB::bind_method(D_METHOD("flush"), &File::flush); - ClassDB::bind_method(D_METHOD("close"), &File::close); - ClassDB::bind_method(D_METHOD("get_path"), &File::get_path); - ClassDB::bind_method(D_METHOD("get_path_absolute"), &File::get_path_absolute); - ClassDB::bind_method(D_METHOD("is_open"), &File::is_open); - ClassDB::bind_method(D_METHOD("seek", "position"), &File::seek); - ClassDB::bind_method(D_METHOD("seek_end", "position"), &File::seek_end, DEFVAL(0)); - ClassDB::bind_method(D_METHOD("get_position"), &File::get_position); - ClassDB::bind_method(D_METHOD("get_length"), &File::get_length); - ClassDB::bind_method(D_METHOD("eof_reached"), &File::eof_reached); - ClassDB::bind_method(D_METHOD("get_8"), &File::get_8); - ClassDB::bind_method(D_METHOD("get_16"), &File::get_16); - ClassDB::bind_method(D_METHOD("get_32"), &File::get_32); - ClassDB::bind_method(D_METHOD("get_64"), &File::get_64); - ClassDB::bind_method(D_METHOD("get_float"), &File::get_float); - ClassDB::bind_method(D_METHOD("get_double"), &File::get_double); - ClassDB::bind_method(D_METHOD("get_real"), &File::get_real); - ClassDB::bind_method(D_METHOD("get_buffer", "length"), &File::get_buffer); - ClassDB::bind_method(D_METHOD("get_line"), &File::get_line); - ClassDB::bind_method(D_METHOD("get_csv_line", "delim"), &File::get_csv_line, DEFVAL(",")); - ClassDB::bind_method(D_METHOD("get_as_text", "skip_cr"), &File::get_as_text, DEFVAL(false)); - ClassDB::bind_method(D_METHOD("get_md5", "path"), &File::get_md5); - ClassDB::bind_method(D_METHOD("get_sha256", "path"), &File::get_sha256); - ClassDB::bind_method(D_METHOD("is_big_endian"), &File::is_big_endian); - ClassDB::bind_method(D_METHOD("set_big_endian", "big_endian"), &File::set_big_endian); - ClassDB::bind_method(D_METHOD("get_error"), &File::get_error); - ClassDB::bind_method(D_METHOD("get_var", "allow_objects"), &File::get_var, DEFVAL(false)); - - ClassDB::bind_method(D_METHOD("store_8", "value"), &File::store_8); - ClassDB::bind_method(D_METHOD("store_16", "value"), &File::store_16); - ClassDB::bind_method(D_METHOD("store_32", "value"), &File::store_32); - ClassDB::bind_method(D_METHOD("store_64", "value"), &File::store_64); - ClassDB::bind_method(D_METHOD("store_float", "value"), &File::store_float); - ClassDB::bind_method(D_METHOD("store_double", "value"), &File::store_double); - ClassDB::bind_method(D_METHOD("store_real", "value"), &File::store_real); - ClassDB::bind_method(D_METHOD("store_buffer", "buffer"), &File::store_buffer); - ClassDB::bind_method(D_METHOD("store_line", "line"), &File::store_line); - ClassDB::bind_method(D_METHOD("store_csv_line", "values", "delim"), &File::store_csv_line, DEFVAL(",")); - ClassDB::bind_method(D_METHOD("store_string", "string"), &File::store_string); - ClassDB::bind_method(D_METHOD("store_var", "value", "full_objects"), &File::store_var, DEFVAL(false)); - - ClassDB::bind_method(D_METHOD("store_pascal_string", "string"), &File::store_pascal_string); - ClassDB::bind_method(D_METHOD("get_pascal_string"), &File::get_pascal_string); - - ClassDB::bind_static_method("File", D_METHOD("file_exists", "path"), &File::file_exists); - ClassDB::bind_method(D_METHOD("get_modified_time", "file"), &File::get_modified_time); - - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "big_endian"), "set_big_endian", "is_big_endian"); - - BIND_ENUM_CONSTANT(READ); - BIND_ENUM_CONSTANT(WRITE); - BIND_ENUM_CONSTANT(READ_WRITE); - BIND_ENUM_CONSTANT(WRITE_READ); - - BIND_ENUM_CONSTANT(COMPRESSION_FASTLZ); - BIND_ENUM_CONSTANT(COMPRESSION_DEFLATE); - BIND_ENUM_CONSTANT(COMPRESSION_ZSTD); - BIND_ENUM_CONSTANT(COMPRESSION_GZIP); -} - -////// Directory ////// - -Error Directory::open(const String &p_path) { - Error err; - Ref<DirAccess> alt = DirAccess::open(p_path, &err); - if (alt.is_null()) { - return err; - } - d = alt; - dir_open = true; - - return OK; -} - -bool Directory::is_open() const { - return d.is_valid() && dir_open; -} - -Error Directory::list_dir_begin() { - ERR_FAIL_COND_V_MSG(!is_open(), ERR_UNCONFIGURED, "Directory must be opened before use."); - return d->list_dir_begin(); -} - -String Directory::get_next() { - ERR_FAIL_COND_V_MSG(!is_open(), "", "Directory must be opened before use."); - - String next = d->get_next(); - while (!next.is_empty() && ((!include_navigational && (next == "." || next == "..")) || (!include_hidden && d->current_is_hidden()))) { - next = d->get_next(); - } - return next; -} - -bool Directory::current_is_dir() const { - ERR_FAIL_COND_V_MSG(!is_open(), false, "Directory must be opened before use."); - return d->current_is_dir(); -} - -void Directory::list_dir_end() { - ERR_FAIL_COND_MSG(!is_open(), "Directory must be opened before use."); - d->list_dir_end(); -} - -PackedStringArray Directory::get_files() { - return _get_contents(false); -} - -PackedStringArray Directory::get_directories() { - return _get_contents(true); -} - -PackedStringArray Directory::_get_contents(bool p_directories) { - PackedStringArray ret; - ERR_FAIL_COND_V_MSG(!is_open(), ret, "Directory must be opened before use."); - - list_dir_begin(); - String s = get_next(); - while (!s.is_empty()) { - if (current_is_dir() == p_directories) { - ret.append(s); - } - s = get_next(); - } - - ret.sort(); - return ret; -} - -void Directory::set_include_navigational(bool p_enable) { - include_navigational = p_enable; -} - -bool Directory::get_include_navigational() const { - return include_navigational; -} - -void Directory::set_include_hidden(bool p_enable) { - include_hidden = p_enable; -} - -bool Directory::get_include_hidden() const { - return include_hidden; -} - -int Directory::get_drive_count() { - ERR_FAIL_COND_V_MSG(!is_open(), 0, "Directory must be opened before use."); - return d->get_drive_count(); -} - -String Directory::get_drive(int p_drive) { - ERR_FAIL_COND_V_MSG(!is_open(), "", "Directory must be opened before use."); - return d->get_drive(p_drive); -} - -int Directory::get_current_drive() { - ERR_FAIL_COND_V_MSG(!is_open(), 0, "Directory must be opened before use."); - return d->get_current_drive(); -} - -Error Directory::change_dir(String p_dir) { - ERR_FAIL_COND_V_MSG(d.is_null(), ERR_UNCONFIGURED, "Directory is not configured properly."); - Error err = d->change_dir(p_dir); - - if (err != OK) { - return err; - } - dir_open = true; - - return OK; -} - -String Directory::get_current_dir() { - ERR_FAIL_COND_V_MSG(!is_open(), "", "Directory must be opened before use."); - return d->get_current_dir(); -} - -Error Directory::make_dir(String p_dir) { - ERR_FAIL_COND_V_MSG(d.is_null(), ERR_UNCONFIGURED, "Directory is not configured properly."); - if (!p_dir.is_relative_path()) { - Ref<DirAccess> da = DirAccess::create_for_path(p_dir); - return da->make_dir(p_dir); - } - return d->make_dir(p_dir); -} - -Error Directory::make_dir_recursive(String p_dir) { - ERR_FAIL_COND_V_MSG(d.is_null(), ERR_UNCONFIGURED, "Directory is not configured properly."); - if (!p_dir.is_relative_path()) { - Ref<DirAccess> da = DirAccess::create_for_path(p_dir); - return da->make_dir_recursive(p_dir); - } - return d->make_dir_recursive(p_dir); -} - -bool Directory::file_exists(String p_file) { - ERR_FAIL_COND_V_MSG(d.is_null(), false, "Directory is not configured properly."); - if (!p_file.is_relative_path()) { - return FileAccess::exists(p_file); - } - return d->file_exists(p_file); -} - -bool Directory::dir_exists(String p_dir) { - ERR_FAIL_COND_V_MSG(d.is_null(), false, "Directory is not configured properly."); - if (!p_dir.is_relative_path()) { - return DirAccess::exists(p_dir); - } - return d->dir_exists(p_dir); -} - -uint64_t Directory::get_space_left() { - ERR_FAIL_COND_V_MSG(d.is_null(), 0, "Directory must be opened before use."); - return d->get_space_left() / 1024 * 1024; // Truncate to closest MiB. -} - -Error Directory::copy(String p_from, String p_to) { - ERR_FAIL_COND_V_MSG(!is_open(), ERR_UNCONFIGURED, "Directory must be opened before use."); - return d->copy(p_from, p_to); -} - -Error Directory::rename(String p_from, String p_to) { - ERR_FAIL_COND_V_MSG(!is_open(), ERR_UNCONFIGURED, "Directory must be opened before use."); - ERR_FAIL_COND_V_MSG(p_from.is_empty() || p_from == "." || p_from == "..", ERR_INVALID_PARAMETER, "Invalid path to rename."); - - if (!p_from.is_relative_path()) { - Ref<DirAccess> da = DirAccess::create_for_path(p_from); - ERR_FAIL_COND_V_MSG(!da->file_exists(p_from) && !da->dir_exists(p_from), ERR_DOES_NOT_EXIST, "File or directory does not exist."); - return da->rename(p_from, p_to); - } - - ERR_FAIL_COND_V_MSG(!d->file_exists(p_from) && !d->dir_exists(p_from), ERR_DOES_NOT_EXIST, "File or directory does not exist."); - return d->rename(p_from, p_to); -} - -Error Directory::remove(String p_name) { - ERR_FAIL_COND_V_MSG(!is_open(), ERR_UNCONFIGURED, "Directory must be opened before use."); - if (!p_name.is_relative_path()) { - Ref<DirAccess> da = DirAccess::create_for_path(p_name); - return da->remove(p_name); - } - - return d->remove(p_name); -} - -void Directory::_bind_methods() { - ClassDB::bind_method(D_METHOD("open", "path"), &Directory::open); - ClassDB::bind_method(D_METHOD("list_dir_begin"), &Directory::list_dir_begin, DEFVAL(false), DEFVAL(false)); - ClassDB::bind_method(D_METHOD("get_next"), &Directory::get_next); - ClassDB::bind_method(D_METHOD("current_is_dir"), &Directory::current_is_dir); - ClassDB::bind_method(D_METHOD("list_dir_end"), &Directory::list_dir_end); - ClassDB::bind_method(D_METHOD("get_files"), &Directory::get_files); - ClassDB::bind_method(D_METHOD("get_directories"), &Directory::get_directories); - ClassDB::bind_method(D_METHOD("get_drive_count"), &Directory::get_drive_count); - ClassDB::bind_method(D_METHOD("get_drive", "idx"), &Directory::get_drive); - ClassDB::bind_method(D_METHOD("get_current_drive"), &Directory::get_current_drive); - ClassDB::bind_method(D_METHOD("change_dir", "todir"), &Directory::change_dir); - ClassDB::bind_method(D_METHOD("get_current_dir"), &Directory::get_current_dir); - ClassDB::bind_method(D_METHOD("make_dir", "path"), &Directory::make_dir); - ClassDB::bind_method(D_METHOD("make_dir_recursive", "path"), &Directory::make_dir_recursive); - ClassDB::bind_method(D_METHOD("file_exists", "path"), &Directory::file_exists); - ClassDB::bind_method(D_METHOD("dir_exists", "path"), &Directory::dir_exists); - ClassDB::bind_method(D_METHOD("get_space_left"), &Directory::get_space_left); - ClassDB::bind_method(D_METHOD("copy", "from", "to"), &Directory::copy); - ClassDB::bind_method(D_METHOD("rename", "from", "to"), &Directory::rename); - ClassDB::bind_method(D_METHOD("remove", "path"), &Directory::remove); - - ClassDB::bind_method(D_METHOD("set_include_navigational", "enable"), &Directory::set_include_navigational); - ClassDB::bind_method(D_METHOD("get_include_navigational"), &Directory::get_include_navigational); - ClassDB::bind_method(D_METHOD("set_include_hidden", "enable"), &Directory::set_include_hidden); - ClassDB::bind_method(D_METHOD("get_include_hidden"), &Directory::get_include_hidden); - - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "include_navigational"), "set_include_navigational", "get_include_navigational"); - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "include_hidden"), "set_include_hidden", "get_include_hidden"); -} - -Directory::Directory() { - d = DirAccess::create(DirAccess::ACCESS_RESOURCES); -} - ////// Marshalls ////// Marshalls *Marshalls::singleton = nullptr; @@ -2022,10 +1302,10 @@ Dictionary ClassDB::get_signal(StringName p_class, StringName p_signal) const { } } -Array ClassDB::get_signal_list(StringName p_class, bool p_no_inheritance) const { +TypedArray<Dictionary> ClassDB::get_signal_list(StringName p_class, bool p_no_inheritance) const { List<MethodInfo> signals; ::ClassDB::get_signal_list(p_class, &signals, p_no_inheritance); - Array ret; + TypedArray<Dictionary> ret; for (const MethodInfo &E : signals) { ret.push_back(E.operator Dictionary()); @@ -2034,10 +1314,10 @@ Array ClassDB::get_signal_list(StringName p_class, bool p_no_inheritance) const return ret; } -Array ClassDB::get_property_list(StringName p_class, bool p_no_inheritance) const { +TypedArray<Dictionary> ClassDB::get_property_list(StringName p_class, bool p_no_inheritance) const { List<PropertyInfo> plist; ::ClassDB::get_property_list(p_class, &plist, p_no_inheritance); - Array ret; + TypedArray<Dictionary> ret; for (const PropertyInfo &E : plist) { ret.push_back(E.operator Dictionary()); } @@ -2066,10 +1346,10 @@ bool ClassDB::has_method(StringName p_class, StringName p_method, bool p_no_inhe return ::ClassDB::has_method(p_class, p_method, p_no_inheritance); } -Array ClassDB::get_method_list(StringName p_class, bool p_no_inheritance) const { +TypedArray<Dictionary> ClassDB::get_method_list(StringName p_class, bool p_no_inheritance) const { List<MethodInfo> methods; ::ClassDB::get_method_list(p_class, &methods, p_no_inheritance); - Array ret; + TypedArray<Dictionary> ret; for (const MethodInfo &E : methods) { #ifdef DEBUG_METHODS_ENABLED @@ -2197,6 +1477,14 @@ int Engine::get_physics_ticks_per_second() const { return ::Engine::get_singleton()->get_physics_ticks_per_second(); } +void Engine::set_max_physics_steps_per_frame(int p_max_physics_steps) { + ::Engine::get_singleton()->set_max_physics_steps_per_frame(p_max_physics_steps); +} + +int Engine::get_max_physics_steps_per_frame() const { + return ::Engine::get_singleton()->get_max_physics_steps_per_frame(); +} + void Engine::set_physics_jitter_fix(double p_threshold) { ::Engine::get_singleton()->set_physics_jitter_fix(p_threshold); } @@ -2209,12 +1497,12 @@ double Engine::get_physics_interpolation_fraction() const { return ::Engine::get_singleton()->get_physics_interpolation_fraction(); } -void Engine::set_target_fps(int p_fps) { - ::Engine::get_singleton()->set_target_fps(p_fps); +void Engine::set_max_fps(int p_fps) { + ::Engine::get_singleton()->set_max_fps(p_fps); } -int Engine::get_target_fps() const { - return ::Engine::get_singleton()->get_target_fps(); +int Engine::get_max_fps() const { + return ::Engine::get_singleton()->get_max_fps(); } double Engine::get_frames_per_second() const { @@ -2254,7 +1542,7 @@ Dictionary Engine::get_author_info() const { return ::Engine::get_singleton()->get_author_info(); } -Array Engine::get_copyright_info() const { +TypedArray<Dictionary> Engine::get_copyright_info() const { return ::Engine::get_singleton()->get_copyright_info(); } @@ -2348,11 +1636,13 @@ bool Engine::is_printing_error_messages() const { void Engine::_bind_methods() { ClassDB::bind_method(D_METHOD("set_physics_ticks_per_second", "physics_ticks_per_second"), &Engine::set_physics_ticks_per_second); ClassDB::bind_method(D_METHOD("get_physics_ticks_per_second"), &Engine::get_physics_ticks_per_second); + ClassDB::bind_method(D_METHOD("set_max_physics_steps_per_frame", "max_physics_steps"), &Engine::set_max_physics_steps_per_frame); + ClassDB::bind_method(D_METHOD("get_max_physics_steps_per_frame"), &Engine::get_max_physics_steps_per_frame); ClassDB::bind_method(D_METHOD("set_physics_jitter_fix", "physics_jitter_fix"), &Engine::set_physics_jitter_fix); ClassDB::bind_method(D_METHOD("get_physics_jitter_fix"), &Engine::get_physics_jitter_fix); ClassDB::bind_method(D_METHOD("get_physics_interpolation_fraction"), &Engine::get_physics_interpolation_fraction); - ClassDB::bind_method(D_METHOD("set_target_fps", "target_fps"), &Engine::set_target_fps); - ClassDB::bind_method(D_METHOD("get_target_fps"), &Engine::get_target_fps); + ClassDB::bind_method(D_METHOD("set_max_fps", "max_fps"), &Engine::set_max_fps); + ClassDB::bind_method(D_METHOD("get_max_fps"), &Engine::get_max_fps); ClassDB::bind_method(D_METHOD("set_time_scale", "time_scale"), &Engine::set_time_scale); ClassDB::bind_method(D_METHOD("get_time_scale"), &Engine::get_time_scale); @@ -2395,7 +1685,8 @@ void Engine::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::BOOL, "print_error_messages"), "set_print_error_messages", "is_printing_error_messages"); ADD_PROPERTY(PropertyInfo(Variant::INT, "physics_ticks_per_second"), "set_physics_ticks_per_second", "get_physics_ticks_per_second"); - ADD_PROPERTY(PropertyInfo(Variant::INT, "target_fps"), "set_target_fps", "get_target_fps"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "max_physics_steps_per_frame"), "set_max_physics_steps_per_frame", "get_max_physics_steps_per_frame"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "max_fps"), "set_max_fps", "get_max_fps"); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "time_scale"), "set_time_scale", "get_time_scale"); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "physics_jitter_fix"), "set_physics_jitter_fix", "get_physics_jitter_fix"); } diff --git a/core/core_bind.h b/core/core_bind.h index 79230bd685..748ecb4929 100644 --- a/core/core_bind.h +++ b/core/core_bind.h @@ -32,9 +32,6 @@ #define CORE_BIND_H #include "core/debugger/engine_profiler.h" -#include "core/io/compression.h" -#include "core/io/dir_access.h" -#include "core/io/file_access.h" #include "core/io/image.h" #include "core/io/resource_loader.h" #include "core/io/resource_saver.h" @@ -45,6 +42,8 @@ #include "core/templates/safe_refcount.h" class MainLoop; +template <typename T> +class TypedArray; namespace core_bind { @@ -125,9 +124,9 @@ protected: static OS *singleton; public: - enum VideoDriver { - VIDEO_DRIVER_VULKAN, - VIDEO_DRIVER_OPENGL_3, + enum RenderingDriver { + RENDERING_DRIVER_VULKAN, + RENDERING_DRIVER_OPENGL3, }; enum Weekday { @@ -173,6 +172,7 @@ public: Vector<String> get_system_fonts() const; String get_system_font_path(const String &p_font_name, bool p_bold = false, bool p_italic = false) const; String get_executable_path() const; + String read_string_from_stdin(bool p_block = true); int execute(const String &p_path, const Vector<String> &p_arguments, Array r_output = Array(), bool p_read_stderr = false, bool p_open_console = false); int create_process(const String &p_path, const Vector<String> &p_arguments, bool p_open_console = false); int create_instance(const Vector<String> &p_arguments); @@ -191,22 +191,18 @@ public: bool set_environment(const String &p_var, const String &p_value) const; String get_name() const; + String get_distribution_name() const; + String get_version() const; Vector<String> get_cmdline_args(); Vector<String> get_cmdline_user_args(); + Vector<String> get_video_adapter_driver_info() const; + String get_locale() const; String get_locale_language() const; String get_model_name() const; - void dump_memory_to_file(const String &p_file); - void dump_resources_to_file(const String &p_file); - - void print_resources_in_use(bool p_short = false); - void print_all_resources(const String &p_to_file); - void print_all_textures_by_size(); - void print_resources_by_type(const Vector<String> &p_types); - bool is_debug_build() const; String get_unique_id() const; @@ -225,8 +221,6 @@ public: uint64_t get_ticks_msec() const; uint64_t get_ticks_usec() const; - bool can_use_threads() const; - bool is_userfs_persistent() const; bool is_stdout_verbose() const; @@ -293,6 +287,7 @@ public: Vector<int> triangulate_polygon(const Vector<Vector2> &p_polygon); Vector<int> triangulate_delaunay(const Vector<Vector2> &p_points); Vector<Point2> convex_hull(const Vector<Point2> &p_points); + TypedArray<PackedVector2Array> decompose_polygon_in_convex(const Vector<Vector2> &p_polygon); enum PolyBooleanOperation { OPERATION_UNION, @@ -301,14 +296,14 @@ public: OPERATION_XOR }; // 2D polygon boolean operations. - Array merge_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b); // Union (add). - Array clip_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b); // Difference (subtract). - Array intersect_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b); // Common area (multiply). - Array exclude_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b); // All but common area (xor). + TypedArray<PackedVector2Array> merge_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b); // Union (add). + TypedArray<PackedVector2Array> clip_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b); // Difference (subtract). + TypedArray<PackedVector2Array> intersect_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b); // Common area (multiply). + TypedArray<PackedVector2Array> exclude_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b); // All but common area (xor). // 2D polyline vs polygon operations. - Array clip_polyline_with_polygon(const Vector<Vector2> &p_polyline, const Vector<Vector2> &p_polygon); // Cut. - Array intersect_polyline_with_polygon(const Vector<Vector2> &p_polyline, const Vector<Vector2> &p_polygon); // Chop. + TypedArray<PackedVector2Array> clip_polyline_with_polygon(const Vector<Vector2> &p_polyline, const Vector<Vector2> &p_polygon); // Cut. + TypedArray<PackedVector2Array> intersect_polyline_with_polygon(const Vector<Vector2> &p_polyline, const Vector<Vector2> &p_polygon); // Chop. // 2D offset polygons/polylines. enum PolyJoinType { @@ -323,8 +318,8 @@ public: END_SQUARE, END_ROUND }; - Array offset_polygon(const Vector<Vector2> &p_polygon, real_t p_delta, PolyJoinType p_join_type = JOIN_SQUARE); - Array offset_polyline(const Vector<Vector2> &p_polygon, real_t p_delta, PolyJoinType p_join_type = JOIN_SQUARE, PolyEndType p_end_type = END_SQUARE); + TypedArray<PackedVector2Array> offset_polygon(const Vector<Vector2> &p_polygon, real_t p_delta, PolyJoinType p_join_type = JOIN_SQUARE); + TypedArray<PackedVector2Array> offset_polyline(const Vector<Vector2> &p_polygon, real_t p_delta, PolyJoinType p_join_type = JOIN_SQUARE, PolyEndType p_end_type = END_SQUARE); Dictionary make_atlas(const Vector<Size2> &p_rects); @@ -341,9 +336,9 @@ protected: public: static Geometry3D *get_singleton(); - Vector<Plane> build_box_planes(const Vector3 &p_extents); - Vector<Plane> build_cylinder_planes(float p_radius, float p_height, int p_sides, Vector3::Axis p_axis = Vector3::AXIS_Z); - Vector<Plane> build_capsule_planes(float p_radius, float p_height, int p_sides, int p_lats, Vector3::Axis p_axis = Vector3::AXIS_Z); + TypedArray<Plane> build_box_planes(const Vector3 &p_extents); + TypedArray<Plane> build_cylinder_planes(float p_radius, float p_height, int p_sides, Vector3::Axis p_axis = Vector3::AXIS_Z); + TypedArray<Plane> build_capsule_planes(float p_radius, float p_height, int p_sides, int p_lats, Vector3::Axis p_axis = Vector3::AXIS_Z); Vector<Vector3> get_closest_points_between_segments(const Vector3 &p1, const Vector3 &p2, const Vector3 &q1, const Vector3 &q2); Vector3 get_closest_point_to_segment(const Vector3 &p_point, const Vector3 &p_a, const Vector3 &p_b); Vector3 get_closest_point_to_segment_uncapped(const Vector3 &p_point, const Vector3 &p_a, const Vector3 &p_b); @@ -359,156 +354,6 @@ public: Geometry3D() { singleton = this; } }; -class File : public RefCounted { - GDCLASS(File, RefCounted); - - Ref<FileAccess> f; - bool big_endian = false; - -protected: - static void _bind_methods(); - -public: - enum ModeFlags { - READ = 1, - WRITE = 2, - READ_WRITE = 3, - WRITE_READ = 7, - }; - - enum CompressionMode { - COMPRESSION_FASTLZ = Compression::MODE_FASTLZ, - COMPRESSION_DEFLATE = Compression::MODE_DEFLATE, - COMPRESSION_ZSTD = Compression::MODE_ZSTD, - COMPRESSION_GZIP = Compression::MODE_GZIP - }; - - Error open_encrypted(const String &p_path, ModeFlags p_mode_flags, const Vector<uint8_t> &p_key); - Error open_encrypted_pass(const String &p_path, ModeFlags p_mode_flags, const String &p_pass); - Error open_compressed(const String &p_path, ModeFlags p_mode_flags, CompressionMode p_compress_mode = COMPRESSION_FASTLZ); - - Error open(const String &p_path, ModeFlags p_mode_flags); // open a file. - void flush(); // Flush a file (write its buffer to disk). - void close(); // Close a file. - bool is_open() const; // True when file is open. - - String get_path() const; // Returns the path for the current open file. - String get_path_absolute() const; // Returns the absolute path for the current open file. - - void seek(int64_t p_position); // Seek to a given position. - void seek_end(int64_t p_position = 0); // Seek from the end of file. - uint64_t get_position() const; // Get position in the file. - uint64_t get_length() const; // Get size of the file. - - bool eof_reached() const; // Reading passed EOF. - - uint8_t get_8() const; // Get a byte. - uint16_t get_16() const; // Get 16 bits uint. - uint32_t get_32() const; // Get 32 bits uint. - uint64_t get_64() const; // Get 64 bits uint. - - float get_float() const; - double get_double() const; - real_t get_real() const; - - Variant get_var(bool p_allow_objects = false) const; - - Vector<uint8_t> get_buffer(int64_t p_length) const; // Get an array of bytes. - String get_line() const; - Vector<String> get_csv_line(const String &p_delim = ",") const; - String get_as_text(bool p_skip_cr = false) const; - String get_md5(const String &p_path) const; - String get_sha256(const String &p_path) const; - - /* - * Use this for files WRITTEN in _big_ endian machines (ie, amiga/mac). - * It's not about the current CPU type but file formats. - * This flag gets reset to `false` (little endian) on each open. - */ - void set_big_endian(bool p_big_endian); - bool is_big_endian(); - - Error get_error() const; // Get last error. - - void store_8(uint8_t p_dest); // Store a byte. - void store_16(uint16_t p_dest); // Store 16 bits uint. - void store_32(uint32_t p_dest); // Store 32 bits uint. - void store_64(uint64_t p_dest); // Store 64 bits uint. - - void store_float(float p_dest); - void store_double(double p_dest); - void store_real(real_t p_real); - - void store_string(const String &p_string); - void store_line(const String &p_string); - void store_csv_line(const Vector<String> &p_values, const String &p_delim = ","); - - virtual void store_pascal_string(const String &p_string); - virtual String get_pascal_string(); - - void store_buffer(const Vector<uint8_t> &p_buffer); // Store an array of bytes. - - void store_var(const Variant &p_var, bool p_full_objects = false); - - static bool file_exists(const String &p_name); // Return true if a file exists. - - uint64_t get_modified_time(const String &p_file) const; - - File() {} -}; - -class Directory : public RefCounted { - GDCLASS(Directory, RefCounted); - Ref<DirAccess> d; - - bool dir_open = false; - bool include_navigational = false; - bool include_hidden = false; - -protected: - static void _bind_methods(); - -public: - Error open(const String &p_path); - - bool is_open() const; - - Error list_dir_begin(); - String get_next(); - bool current_is_dir() const; - void list_dir_end(); - - PackedStringArray get_files(); - PackedStringArray get_directories(); - PackedStringArray _get_contents(bool p_directories); - - void set_include_navigational(bool p_enable); - bool get_include_navigational() const; - void set_include_hidden(bool p_enable); - bool get_include_hidden() const; - - int get_drive_count(); - String get_drive(int p_drive); - int get_current_drive(); - - Error change_dir(String p_dir); // Can be relative or absolute, return false on success. - String get_current_dir(); // Return current dir location. - - Error make_dir(String p_dir); - Error make_dir_recursive(String p_dir); - - bool file_exists(String p_file); - bool dir_exists(String p_dir); - - uint64_t get_space_left(); - - Error copy(String p_from, String p_to); - Error rename(String p_from, String p_to); - Error remove(String p_name); - - Directory(); -}; - class Marshalls : public Object { GDCLASS(Marshalls, Object); @@ -602,15 +447,15 @@ public: bool has_signal(StringName p_class, StringName p_signal) const; Dictionary get_signal(StringName p_class, StringName p_signal) const; - Array get_signal_list(StringName p_class, bool p_no_inheritance = false) const; + TypedArray<Dictionary> get_signal_list(StringName p_class, bool p_no_inheritance = false) const; - Array get_property_list(StringName p_class, bool p_no_inheritance = false) const; + TypedArray<Dictionary> get_property_list(StringName p_class, bool p_no_inheritance = false) const; Variant get_property(Object *p_object, const StringName &p_property) const; Error set_property(Object *p_object, const StringName &p_property, const Variant &p_value) const; bool has_method(StringName p_class, StringName p_method, bool p_no_inheritance = false) const; - Array get_method_list(StringName p_class, bool p_no_inheritance = false) const; + TypedArray<Dictionary> get_method_list(StringName p_class, bool p_no_inheritance = false) const; PackedStringArray get_integer_constant_list(const StringName &p_class, bool p_no_inheritance = false) const; bool has_integer_constant(const StringName &p_class, const StringName &p_name) const; @@ -641,12 +486,15 @@ public: void set_physics_ticks_per_second(int p_ips); int get_physics_ticks_per_second() const; + void set_max_physics_steps_per_frame(int p_max_physics_steps); + int get_max_physics_steps_per_frame() const; + void set_physics_jitter_fix(double p_threshold); double get_physics_jitter_fix() const; double get_physics_interpolation_fraction() const; - void set_target_fps(int p_fps); - int get_target_fps() const; + void set_max_fps(int p_fps); + int get_max_fps() const; double get_frames_per_second() const; uint64_t get_physics_frames() const; @@ -661,7 +509,7 @@ public: Dictionary get_version_info() const; Dictionary get_author_info() const; - Array get_copyright_info() const; + TypedArray<Dictionary> get_copyright_info() const; Dictionary get_donor_info() const; Dictionary get_license_info() const; String get_license_text() const; @@ -733,7 +581,7 @@ VARIANT_ENUM_CAST(core_bind::ResourceLoader::CacheMode); VARIANT_BITFIELD_CAST(core_bind::ResourceSaver::SaverFlags); -VARIANT_ENUM_CAST(core_bind::OS::VideoDriver); +VARIANT_ENUM_CAST(core_bind::OS::RenderingDriver); VARIANT_ENUM_CAST(core_bind::OS::Weekday); VARIANT_ENUM_CAST(core_bind::OS::Month); VARIANT_ENUM_CAST(core_bind::OS::SystemDir); @@ -742,9 +590,6 @@ VARIANT_ENUM_CAST(core_bind::Geometry2D::PolyBooleanOperation); VARIANT_ENUM_CAST(core_bind::Geometry2D::PolyJoinType); VARIANT_ENUM_CAST(core_bind::Geometry2D::PolyEndType); -VARIANT_ENUM_CAST(core_bind::File::ModeFlags); -VARIANT_ENUM_CAST(core_bind::File::CompressionMode); - VARIANT_ENUM_CAST(core_bind::Thread::Priority); #endif // CORE_BIND_H diff --git a/core/core_builders.py b/core/core_builders.py index b07daa80ae..b0a3b85d58 100644 --- a/core/core_builders.py +++ b/core/core_builders.py @@ -2,6 +2,7 @@ All such functions are invoked in a subprocess on Windows to prevent build flakiness. """ +import zlib from platform_methods import subprocess_main @@ -33,7 +34,6 @@ def make_certs_header(target, source, env): g = open(dst, "w", encoding="utf-8") buf = f.read() decomp_size = len(buf) - import zlib # Use maximum zlib compression level to further reduce file size # (at the cost of initial build times). @@ -208,7 +208,7 @@ def make_license_header(target, source, env): from collections import OrderedDict - projects = OrderedDict() + projects: dict = OrderedDict() license_list = [] with open(src_copyright, "r", encoding="utf-8") as copyright_file: @@ -230,7 +230,7 @@ def make_license_header(target, source, env): part = {} reader.next_line() - data_list = [] + data_list: list = [] for project in iter(projects.values()): for part in project: part["file_index"] = len(data_list) diff --git a/core/core_constants.cpp b/core/core_constants.cpp index dc0ab72a86..628740ed32 100644 --- a/core/core_constants.cpp +++ b/core/core_constants.cpp @@ -167,6 +167,13 @@ void register_global_constants() { BIND_CORE_ENUM_CONSTANT(INLINE_ALIGNMENT_IMAGE_MASK); BIND_CORE_ENUM_CONSTANT(INLINE_ALIGNMENT_TEXT_MASK); + BIND_CORE_ENUM_CLASS_CONSTANT(EulerOrder, EULER_ORDER, XYZ); + BIND_CORE_ENUM_CLASS_CONSTANT(EulerOrder, EULER_ORDER, XZY); + BIND_CORE_ENUM_CLASS_CONSTANT(EulerOrder, EULER_ORDER, YXZ); + BIND_CORE_ENUM_CLASS_CONSTANT(EulerOrder, EULER_ORDER, YZX); + BIND_CORE_ENUM_CLASS_CONSTANT(EulerOrder, EULER_ORDER, ZXY); + BIND_CORE_ENUM_CLASS_CONSTANT(EulerOrder, EULER_ORDER, ZYX); + BIND_CORE_ENUM_CLASS_CONSTANT(Key, KEY, NONE); BIND_CORE_ENUM_CLASS_CONSTANT(Key, KEY, SPECIAL); BIND_CORE_ENUM_CLASS_CONSTANT(Key, KEY, ESCAPE); @@ -433,11 +440,11 @@ void register_global_constants() { BIND_CORE_ENUM_CLASS_CONSTANT_CUSTOM(KeyModifierMask, KEY_CODE_MASK, CODE_MASK); BIND_CORE_ENUM_CLASS_CONSTANT_CUSTOM(KeyModifierMask, KEY_MODIFIER_MASK, MODIFIER_MASK); + BIND_CORE_ENUM_CLASS_CONSTANT(KeyModifierMask, KEY_MASK, CMD_OR_CTRL); BIND_CORE_ENUM_CLASS_CONSTANT(KeyModifierMask, KEY_MASK, SHIFT); BIND_CORE_ENUM_CLASS_CONSTANT(KeyModifierMask, KEY_MASK, ALT); BIND_CORE_ENUM_CLASS_CONSTANT(KeyModifierMask, KEY_MASK, META); BIND_CORE_ENUM_CLASS_CONSTANT(KeyModifierMask, KEY_MASK, CTRL); - BIND_CORE_ENUM_CLASS_CONSTANT_NO_VAL(KeyModifierMask, KEY_MASK, CMD); BIND_CORE_ENUM_CLASS_CONSTANT(KeyModifierMask, KEY_MASK, KPAD); BIND_CORE_ENUM_CLASS_CONSTANT(KeyModifierMask, KEY_MASK, GROUP_SWITCH); @@ -588,8 +595,6 @@ void register_global_constants() { BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_EXPRESSION); BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_PLACEHOLDER_TEXT); BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_COLOR_NO_ALPHA); - BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_IMAGE_COMPRESS_LOSSY); - BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_IMAGE_COMPRESS_LOSSLESS); BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_OBJECT_ID); BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_TYPE_STRING); @@ -612,6 +617,8 @@ void register_global_constants() { BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_LOCALE_ID); BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_LOCALIZABLE_STRING); BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_NODE_TYPE); + BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_HIDE_QUATERNION_EDIT); + BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_PASSWORD); BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_MAX); BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_NONE); @@ -644,6 +651,7 @@ void register_global_constants() { BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_DEFERRED_SET_RESOURCE); BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT); BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_EDITOR_BASIC_SETTING); + BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_READ_ONLY); BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_ARRAY); BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_DEFAULT); diff --git a/core/debugger/remote_debugger.cpp b/core/debugger/remote_debugger.cpp index 23ee977df4..a2e3fab82c 100644 --- a/core/debugger/remote_debugger.cpp +++ b/core/debugger/remote_debugger.cpp @@ -39,89 +39,6 @@ #include "core/object/script_language.h" #include "core/os/os.h" -class RemoteDebugger::MultiplayerProfiler : public EngineProfiler { - struct BandwidthFrame { - uint32_t timestamp; - int packet_size; - }; - - int bandwidth_in_ptr = 0; - Vector<BandwidthFrame> bandwidth_in; - int bandwidth_out_ptr = 0; - Vector<BandwidthFrame> bandwidth_out; - uint64_t last_bandwidth_time = 0; - - int bandwidth_usage(const Vector<BandwidthFrame> &p_buffer, int p_pointer) { - ERR_FAIL_COND_V(p_buffer.size() == 0, 0); - int total_bandwidth = 0; - - uint64_t timestamp = OS::get_singleton()->get_ticks_msec(); - uint64_t final_timestamp = timestamp - 1000; - - int i = (p_pointer + p_buffer.size() - 1) % p_buffer.size(); - - while (i != p_pointer && p_buffer[i].packet_size > 0) { - if (p_buffer[i].timestamp < final_timestamp) { - return total_bandwidth; - } - total_bandwidth += p_buffer[i].packet_size; - i = (i + p_buffer.size() - 1) % p_buffer.size(); - } - - ERR_FAIL_COND_V_MSG(i == p_pointer, total_bandwidth, "Reached the end of the bandwidth profiler buffer, values might be inaccurate."); - return total_bandwidth; - } - -public: - void toggle(bool p_enable, const Array &p_opts) { - if (!p_enable) { - bandwidth_in.clear(); - bandwidth_out.clear(); - } else { - bandwidth_in_ptr = 0; - bandwidth_in.resize(16384); // ~128kB - for (int i = 0; i < bandwidth_in.size(); ++i) { - bandwidth_in.write[i].packet_size = -1; - } - bandwidth_out_ptr = 0; - bandwidth_out.resize(16384); // ~128kB - for (int i = 0; i < bandwidth_out.size(); ++i) { - bandwidth_out.write[i].packet_size = -1; - } - } - } - - void add(const Array &p_data) { - ERR_FAIL_COND(p_data.size() < 3); - const String inout = p_data[0]; - int time = p_data[1]; - int size = p_data[2]; - if (inout == "in") { - bandwidth_in.write[bandwidth_in_ptr].timestamp = time; - bandwidth_in.write[bandwidth_in_ptr].packet_size = size; - bandwidth_in_ptr = (bandwidth_in_ptr + 1) % bandwidth_in.size(); - } else if (inout == "out") { - bandwidth_out.write[bandwidth_out_ptr].timestamp = time; - bandwidth_out.write[bandwidth_out_ptr].packet_size = size; - bandwidth_out_ptr = (bandwidth_out_ptr + 1) % bandwidth_out.size(); - } - } - - void tick(double p_frame_time, double p_process_time, double p_physics_time, double p_physics_frame_time) { - uint64_t pt = OS::get_singleton()->get_ticks_msec(); - if (pt - last_bandwidth_time > 200) { - last_bandwidth_time = pt; - int incoming_bandwidth = bandwidth_usage(bandwidth_in, bandwidth_in_ptr); - int outgoing_bandwidth = bandwidth_usage(bandwidth_out, bandwidth_out_ptr); - - Array arr; - arr.push_back(incoming_bandwidth); - arr.push_back(outgoing_bandwidth); - EngineDebugger::get_singleton()->send_message("multiplayer:bandwidth", arr); - } - } -}; - class RemoteDebugger::PerformanceProfiler : public EngineProfiler { Object *performance = nullptr; int last_perf_time = 0; @@ -659,10 +576,6 @@ RemoteDebugger::RemoteDebugger(Ref<RemoteDebuggerPeer> p_peer) { max_errors_per_second = GLOBAL_GET("network/limits/debugger/max_errors_per_second"); max_warnings_per_second = GLOBAL_GET("network/limits/debugger/max_warnings_per_second"); - // Multiplayer Profiler - multiplayer_profiler.instantiate(); - multiplayer_profiler->bind("multiplayer"); - // Performance Profiler Object *perf = Engine::get_singleton()->get_singleton_object("Performance"); if (perf) { diff --git a/core/debugger/remote_debugger.h b/core/debugger/remote_debugger.h index fe4bbe86ea..944229d361 100644 --- a/core/debugger/remote_debugger.h +++ b/core/debugger/remote_debugger.h @@ -50,10 +50,8 @@ public: private: typedef DebuggerMarshalls::OutputError ErrorMessage; - class MultiplayerProfiler; class PerformanceProfiler; - Ref<MultiplayerProfiler> multiplayer_profiler; Ref<PerformanceProfiler> performance_profiler; Ref<RemoteDebuggerPeer> peer; diff --git a/core/debugger/remote_debugger_peer.cpp b/core/debugger/remote_debugger_peer.cpp index e9362b4ea4..525362ec7c 100644 --- a/core/debugger/remote_debugger_peer.cpp +++ b/core/debugger/remote_debugger_peer.cpp @@ -79,10 +79,8 @@ RemoteDebuggerPeerTCP::RemoteDebuggerPeerTCP(Ref<StreamPeerTCP> p_tcp) { tcp_client = p_tcp; if (tcp_client.is_valid()) { // Attaching to an already connected stream. connected = true; -#ifndef NO_THREADS running = true; thread.start(_thread_func, this); -#endif } else { tcp_client.instantiate(); } @@ -183,10 +181,8 @@ Error RemoteDebuggerPeerTCP::connect_to_host(const String &p_host, uint16_t p_po return FAILED; } connected = true; -#ifndef NO_THREADS running = true; thread.start(_thread_func, this); -#endif return OK; } @@ -208,9 +204,7 @@ void RemoteDebuggerPeerTCP::_thread_func(void *p_ud) { } void RemoteDebuggerPeerTCP::poll() { -#ifdef NO_THREADS - _poll(); -#endif + // Nothing to do, polling is done in thread. } void RemoteDebuggerPeerTCP::_poll() { diff --git a/core/debugger/remote_debugger_peer.h b/core/debugger/remote_debugger_peer.h index 473fd8d712..2ab641d537 100644 --- a/core/debugger/remote_debugger_peer.h +++ b/core/debugger/remote_debugger_peer.h @@ -43,12 +43,12 @@ protected: public: virtual bool is_peer_connected() = 0; + virtual int get_max_message_size() const = 0; virtual bool has_message() = 0; virtual Error put_message(const Array &p_arr) = 0; virtual Array get_message() = 0; virtual void close() = 0; virtual void poll() = 0; - virtual int get_max_message_size() const = 0; virtual bool can_block() const { return true; } // If blocking io is allowed on main thread (debug). RemoteDebuggerPeer(); @@ -81,12 +81,12 @@ public: Error connect_to_host(const String &p_host, uint16_t p_port); - void poll() override; bool is_peer_connected() override; + int get_max_message_size() const override; bool has_message() override; - Array get_message() override; Error put_message(const Array &p_arr) override; - int get_max_message_size() const override; + Array get_message() override; + void poll() override; void close() override; RemoteDebuggerPeerTCP(Ref<StreamPeerTCP> p_stream = Ref<StreamPeerTCP>()); diff --git a/core/doc_data.h b/core/doc_data.h index 1d8d2483e0..d5fbe37c96 100644 --- a/core/doc_data.h +++ b/core/doc_data.h @@ -32,7 +32,6 @@ #define DOC_DATA_H #include "core/io/xml_parser.h" -#include "core/templates/rb_map.h" #include "core/variant/variant.h" struct ScriptMemberInfo { @@ -58,6 +57,27 @@ public: } return name < p_arg.name; } + static ArgumentDoc from_dict(const Dictionary &p_dict) { + ArgumentDoc doc; + + if (p_dict.has("name")) { + doc.name = p_dict["name"]; + } + + if (p_dict.has("type")) { + doc.type = p_dict["type"]; + } + + if (p_dict.has("enumeration")) { + doc.enumeration = p_dict["enumeration"]; + } + + if (p_dict.has("default_value")) { + doc.default_value = p_dict["default_value"]; + } + + return doc; + } }; struct MethodDoc { @@ -66,6 +86,8 @@ public: String return_enum; String qualifiers; String description; + bool is_deprecated = false; + bool is_experimental = false; Vector<ArgumentDoc> arguments; Vector<int> errors_returned; bool operator<(const MethodDoc &p_method) const { @@ -96,6 +118,55 @@ public: } return name < p_method.name; } + static MethodDoc from_dict(const Dictionary &p_dict) { + MethodDoc doc; + + if (p_dict.has("name")) { + doc.name = p_dict["name"]; + } + + if (p_dict.has("return_type")) { + doc.return_type = p_dict["return_type"]; + } + + if (p_dict.has("return_enum")) { + doc.return_enum = p_dict["return_enum"]; + } + + if (p_dict.has("qualifiers")) { + doc.qualifiers = p_dict["qualifiers"]; + } + + if (p_dict.has("description")) { + doc.description = p_dict["description"]; + } + + if (p_dict.has("is_deprecated")) { + doc.is_deprecated = p_dict["is_deprecated"]; + } + + if (p_dict.has("is_experimental")) { + doc.is_experimental = p_dict["is_experimental"]; + } + + Array arguments; + if (p_dict.has("arguments")) { + arguments = p_dict["arguments"]; + } + for (int i = 0; i < arguments.size(); i++) { + doc.arguments.push_back(ArgumentDoc::from_dict(arguments[i])); + } + + Array errors_returned; + if (p_dict.has("errors_returned")) { + errors_returned = p_dict["errors_returned"]; + } + for (int i = 0; i < errors_returned.size(); i++) { + doc.errors_returned.push_back(errors_returned[i]); + } + + return doc; + } }; struct ConstantDoc { @@ -105,9 +176,48 @@ public: String enumeration; bool is_bitfield = false; String description; + bool is_deprecated = false; + bool is_experimental = false; bool operator<(const ConstantDoc &p_const) const { return name < p_const.name; } + static ConstantDoc from_dict(const Dictionary &p_dict) { + ConstantDoc doc; + + if (p_dict.has("name")) { + doc.name = p_dict["name"]; + } + + if (p_dict.has("value")) { + doc.value = p_dict["value"]; + } + + if (p_dict.has("is_value_valid")) { + doc.is_value_valid = p_dict["is_value_valid"]; + } + + if (p_dict.has("enumeration")) { + doc.enumeration = p_dict["enumeration"]; + } + + if (p_dict.has("is_bitfield")) { + doc.is_bitfield = p_dict["is_bitfield"]; + } + + if (p_dict.has("description")) { + doc.description = p_dict["description"]; + } + + if (p_dict.has("is_deprecated")) { + doc.is_deprecated = p_dict["is_deprecated"]; + } + + if (p_dict.has("is_experimental")) { + doc.is_experimental = p_dict["is_experimental"]; + } + + return doc; + } }; struct EnumDoc { @@ -115,6 +225,31 @@ public: bool is_bitfield = false; String description; Vector<DocData::ConstantDoc> values; + static EnumDoc from_dict(const Dictionary &p_dict) { + EnumDoc doc; + + if (p_dict.has("name")) { + doc.name = p_dict["name"]; + } + + if (p_dict.has("is_bitfield")) { + doc.is_bitfield = p_dict["is_bitfield"]; + } + + if (p_dict.has("description")) { + doc.description = p_dict["description"]; + } + + Array values; + if (p_dict.has("values")) { + values = p_dict["values"]; + } + for (int i = 0; i < values.size(); i++) { + doc.values.push_back(ConstantDoc::from_dict(values[i])); + } + + return doc; + } }; struct PropertyDoc { @@ -126,9 +261,60 @@ public: String default_value; bool overridden = false; String overrides; + bool is_deprecated = false; + bool is_experimental = false; bool operator<(const PropertyDoc &p_prop) const { return name < p_prop.name; } + static PropertyDoc from_dict(const Dictionary &p_dict) { + PropertyDoc doc; + + if (p_dict.has("name")) { + doc.name = p_dict["name"]; + } + + if (p_dict.has("type")) { + doc.type = p_dict["type"]; + } + + if (p_dict.has("enumeration")) { + doc.enumeration = p_dict["enumeration"]; + } + + if (p_dict.has("description")) { + doc.description = p_dict["description"]; + } + + if (p_dict.has("setter")) { + doc.setter = p_dict["setter"]; + } + + if (p_dict.has("getter")) { + doc.getter = p_dict["getter"]; + } + + if (p_dict.has("default_value")) { + doc.default_value = p_dict["default_value"]; + } + + if (p_dict.has("overridden")) { + doc.overridden = p_dict["overridden"]; + } + + if (p_dict.has("overrides")) { + doc.overrides = p_dict["overrides"]; + } + + if (p_dict.has("is_deprecated")) { + doc.is_deprecated = p_dict["is_deprecated"]; + } + + if (p_dict.has("is_experimental")) { + doc.is_experimental = p_dict["is_experimental"]; + } + + return doc; + } }; struct ThemeItemDoc { @@ -144,11 +330,49 @@ public: } return data_type < p_theme_item.data_type; } + static ThemeItemDoc from_dict(const Dictionary &p_dict) { + ThemeItemDoc doc; + + if (p_dict.has("name")) { + doc.name = p_dict["name"]; + } + + if (p_dict.has("type")) { + doc.type = p_dict["type"]; + } + + if (p_dict.has("data_type")) { + doc.data_type = p_dict["data_type"]; + } + + if (p_dict.has("description")) { + doc.description = p_dict["description"]; + } + + if (p_dict.has("default_value")) { + doc.default_value = p_dict["default_value"]; + } + + return doc; + } }; struct TutorialDoc { String link; String title; + static TutorialDoc from_dict(const Dictionary &p_dict) { + TutorialDoc doc; + + if (p_dict.has("link")) { + doc.link = p_dict["link"]; + } + + if (p_dict.has("title")) { + doc.title = p_dict["title"]; + } + + return doc; + } }; struct ClassDoc { @@ -167,11 +391,134 @@ public: Vector<PropertyDoc> properties; Vector<MethodDoc> annotations; Vector<ThemeItemDoc> theme_properties; + bool is_deprecated = false; + bool is_experimental = false; bool is_script_doc = false; String script_path; bool operator<(const ClassDoc &p_class) const { return name < p_class.name; } + static ClassDoc from_dict(const Dictionary &p_dict) { + ClassDoc doc; + + if (p_dict.has("name")) { + doc.name = p_dict["name"]; + } + + if (p_dict.has("inherits")) { + doc.inherits = p_dict["inherits"]; + } + + if (p_dict.has("category")) { + doc.category = p_dict["category"]; + } + + if (p_dict.has("brief_description")) { + doc.brief_description = p_dict["brief_description"]; + } + + if (p_dict.has("description")) { + doc.description = p_dict["description"]; + } + + Array tutorials; + if (p_dict.has("tutorials")) { + tutorials = p_dict["tutorials"]; + } + for (int i = 0; i < tutorials.size(); i++) { + doc.tutorials.push_back(TutorialDoc::from_dict(tutorials[i])); + } + + Array constructors; + if (p_dict.has("constructors")) { + constructors = p_dict["constructors"]; + } + for (int i = 0; i < constructors.size(); i++) { + doc.constructors.push_back(MethodDoc::from_dict(constructors[i])); + } + + Array methods; + if (p_dict.has("methods")) { + methods = p_dict["methods"]; + } + for (int i = 0; i < methods.size(); i++) { + doc.methods.push_back(MethodDoc::from_dict(methods[i])); + } + + Array operators; + if (p_dict.has("operators")) { + operators = p_dict["operators"]; + } + for (int i = 0; i < operators.size(); i++) { + doc.operators.push_back(MethodDoc::from_dict(operators[i])); + } + + Array signals; + if (p_dict.has("signals")) { + signals = p_dict["signals"]; + } + for (int i = 0; i < signals.size(); i++) { + doc.signals.push_back(MethodDoc::from_dict(signals[i])); + } + + Array constants; + if (p_dict.has("constants")) { + constants = p_dict["constants"]; + } + for (int i = 0; i < constants.size(); i++) { + doc.constants.push_back(ConstantDoc::from_dict(constants[i])); + } + + Dictionary enums; + if (p_dict.has("enums")) { + enums = p_dict["enums"]; + } + for (int i = 0; i < enums.size(); i++) { + doc.enums[enums.get_key_at_index(i)] = enums.get_value_at_index(i); + } + + Array properties; + if (p_dict.has("properties")) { + properties = p_dict["properties"]; + } + for (int i = 0; i < properties.size(); i++) { + doc.properties.push_back(PropertyDoc::from_dict(properties[i])); + } + + Array annotations; + if (p_dict.has("annotations")) { + annotations = p_dict["annotations"]; + } + for (int i = 0; i < annotations.size(); i++) { + doc.annotations.push_back(MethodDoc::from_dict(annotations[i])); + } + + Array theme_properties; + if (p_dict.has("theme_properties")) { + theme_properties = p_dict["theme_properties"]; + } + for (int i = 0; i < theme_properties.size(); i++) { + doc.theme_properties.push_back(ThemeItemDoc::from_dict(theme_properties[i])); + } + + if (p_dict.has("is_deprecated")) { + doc.is_deprecated = p_dict["is_deprecated"]; + } + + if (p_dict.has("is_experimental")) { + doc.is_experimental = p_dict["is_experimental"]; + } + + if (p_dict.has("is_script_doc")) { + doc.is_script_doc = p_dict["is_script_doc"]; + } + + if (p_dict.has("script_path")) { + doc.script_path = p_dict["script_path"]; + } + + return doc; + } }; static void return_doc_from_retinfo(DocData::MethodDoc &p_method, const PropertyInfo &p_retinfo); diff --git a/core/error/error_macros.h b/core/error/error_macros.h index 2cfb5421c6..6901548cca 100644 --- a/core/error/error_macros.h +++ b/core/error/error_macros.h @@ -137,8 +137,7 @@ void _err_flush_stdout(); ((void)0) /** - * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0. - * If not, prints `m_msg`, notifies in the editor, and the current function returns. + * Same as `ERR_FAIL_INDEX_MSG` but also notifies the editor. */ #define ERR_FAIL_INDEX_EDMSG(m_index, m_size, m_msg) \ if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \ @@ -173,8 +172,7 @@ void _err_flush_stdout(); ((void)0) /** - * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0. - * If not, prints `m_msg`, notifies in the editor, and the current function returns `m_retval`. + * Same as `ERR_FAIL_INDEX_V_MSG` but also notifies the editor. */ #define ERR_FAIL_INDEX_V_EDMSG(m_index, m_size, m_retval, m_msg) \ if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \ @@ -240,9 +238,9 @@ void _err_flush_stdout(); return; \ } else \ ((void)0) + /** - * Ensures an unsigned integer index `m_index` is less than `m_size`. - * If not, prints `m_msg`, notifies in the editor, and the current function returns. + * Same as `ERR_FAIL_UNSIGNED_INDEX_MSG` but also notifies the editor. */ #define ERR_FAIL_UNSIGNED_INDEX_EDMSG(m_index, m_size, m_msg) \ if (unlikely((m_index) >= (m_size))) { \ @@ -277,8 +275,7 @@ void _err_flush_stdout(); ((void)0) /** - * Ensures an unsigned integer index `m_index` is less than `m_size`. - * If not, prints `m_msg`, notifies in the editor, and the current function returns `m_retval`. + * Same as `ERR_FAIL_UNSIGNED_INDEX_V_EDMSG` but also notifies the editor. */ #define ERR_FAIL_UNSIGNED_INDEX_V_EDMSG(m_index, m_size, m_retval, m_msg) \ if (unlikely((m_index) >= (m_size))) { \ @@ -346,8 +343,7 @@ void _err_flush_stdout(); ((void)0) /** - * Ensures a pointer `m_param` is not null. - * If it is null, prints `m_msg`, notifies in the editor, and the current function returns. + * Same as `ERR_FAIL_NULL_MSG` but also notifies the editor. */ #define ERR_FAIL_NULL_EDMSG(m_param, m_msg) \ if (unlikely(m_param == nullptr)) { \ @@ -382,8 +378,7 @@ void _err_flush_stdout(); ((void)0) /** - * Ensures a pointer `m_param` is not null. - * If it is null, prints `m_msg`, notifies in the editor, and the current function returns `m_retval`. + * Same as `ERR_FAIL_NULL_V_MSG` but also notifies the editor. */ #define ERR_FAIL_NULL_V_EDMSG(m_param, m_retval, m_msg) \ if (unlikely(m_param == nullptr)) { \ @@ -423,11 +418,7 @@ void _err_flush_stdout(); ((void)0) /** - * Ensures `m_cond` is false. - * If `m_cond` is true, prints `m_msg`, notifies in the editor, and the current function returns. - * - * If checking for null use ERR_FAIL_NULL_MSG instead. - * If checking index bounds use ERR_FAIL_INDEX_MSG instead. + * Same as `ERR_FAIL_COND_MSG` but also notifies the editor. */ #define ERR_FAIL_COND_EDMSG(m_cond, m_msg) \ if (unlikely(m_cond)) { \ @@ -467,11 +458,7 @@ void _err_flush_stdout(); ((void)0) /** - * Ensures `m_cond` is false. - * If `m_cond` is true, prints `m_msg`, notifies in the editor, and the current function returns `m_retval`. - * - * If checking for null use ERR_FAIL_NULL_V_MSG instead. - * If checking index bounds use ERR_FAIL_INDEX_V_MSG instead. + * Same as `ERR_FAIL_COND_V_MSG` but also notifies the editor. */ #define ERR_FAIL_COND_V_EDMSG(m_cond, m_retval, m_msg) \ if (unlikely(m_cond)) { \ @@ -506,8 +493,7 @@ void _err_flush_stdout(); ((void)0) /** - * Ensures `m_cond` is false. - * If `m_cond` is true, prints `m_msg`, notifies in the editor, and the current loop continues. + * Same as `ERR_CONTINUE_MSG` but also notifies the editor. */ #define ERR_CONTINUE_EDMSG(m_cond, m_msg) \ if (unlikely(m_cond)) { \ @@ -542,8 +528,7 @@ void _err_flush_stdout(); ((void)0) /** - * Ensures `m_cond` is false. - * If `m_cond` is true, prints `m_msg`, notifies in the editor, and the current loop breaks. + * Same as `ERR_BREAK_MSG` but also notifies the editor. */ #define ERR_BREAK_EDMSG(m_cond, m_msg) \ if (unlikely(m_cond)) { \ @@ -613,10 +598,7 @@ void _err_flush_stdout(); ((void)0) /** - * Try using `ERR_FAIL_COND_MSG`. - * Only use this macro if more complex error detection or recovery is required. - * - * Prints `m_msg`, notifies in the editor, and the current function returns. + * Same as `ERR_FAIL_MSG` but also notifies the editor. */ #define ERR_FAIL_EDMSG(m_msg) \ if (true) { \ @@ -653,10 +635,7 @@ void _err_flush_stdout(); ((void)0) /** - * Try using `ERR_FAIL_COND_V_MSG`. - * Only use this macro if more complex error detection or recovery is required. - * - * Prints `m_msg`, notifies in the editor, and the current function returns `m_retval`. + * Same as `ERR_FAIL_V_MSG` but also notifies the editor. */ #define ERR_FAIL_V_EDMSG(m_retval, m_msg) \ if (true) { \ @@ -666,7 +645,7 @@ void _err_flush_stdout(); ((void)0) /** - * Try using `ERR_FAIL_COND_MSG`, `ERR_FAIL_COND_V_MSG`, `ERR_CONTINUE_MSG` or ERR_BREAK_MSG. + * Try using `ERR_FAIL_COND_MSG`, `ERR_FAIL_COND_V_MSG`, `ERR_CONTINUE_MSG` or `ERR_BREAK_MSG`. * Only use this macro at the start of a function that has not been implemented yet, or * if more complex error detection or recovery is required. * @@ -676,14 +655,10 @@ void _err_flush_stdout(); _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg) /** - * Try using `ERR_FAIL_COND_MSG`, `ERR_FAIL_COND_V_MSG`, `ERR_CONTINUE_MSG` or ERR_BREAK_MSG. - * Only use this macro at the start of a function that has not been implemented yet, or - * if more complex error detection or recovery is required. - * - * Prints `m_msg` and notifies the editor. + * Same as `ERR_PRINT` but also notifies the editor. */ #define ERR_PRINT_ED(m_msg) \ - _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg, ) + _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg, true) /** * Prints `m_msg` once during the application lifetime. @@ -699,7 +674,7 @@ void _err_flush_stdout(); ((void)0) /** - * Prints `m_msg` and notifies the editor once during the application lifetime. + * Same as `ERR_PRINT_ONCE` but also notifies the editor. */ #define ERR_PRINT_ONCE_ED(m_msg) \ if (true) { \ @@ -722,9 +697,7 @@ void _err_flush_stdout(); _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg, false, ERR_HANDLER_WARNING) /** - * Prints `m_msg` and notifies the editor. - * - * If warning about deprecated usage, use `WARN_DEPRECATED` or `WARN_DEPRECATED_MSG` instead. + * Same as `WARN_PRINT` but also notifies the editor. */ #define WARN_PRINT_ED(m_msg) \ _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg, true, ERR_HANDLER_WARNING) @@ -745,9 +718,7 @@ void _err_flush_stdout(); ((void)0) /** - * Prints `m_msg` and notifies the editor once during the application lifetime. - * - * If warning about deprecated usage, use `WARN_DEPRECATED` or `WARN_DEPRECATED_MSG` instead. + * Same as `WARN_PRINT_ONCE` but also notifies the editor. */ #define WARN_PRINT_ONCE_ED(m_msg) \ if (true) { \ diff --git a/core/extension/SCsub b/core/extension/SCsub index 23727c1b76..a8f68a1533 100644 --- a/core/extension/SCsub +++ b/core/extension/SCsub @@ -3,10 +3,15 @@ Import("env") import make_wrappers +import make_interface_dumper from platform_methods import run_in_subprocess env.CommandNoCache(["ext_wrappers.gen.inc"], "make_wrappers.py", run_in_subprocess(make_wrappers.run)) - +env.CommandNoCache( + "gdnative_interface_dump.gen.h", + ["gdnative_interface.h", "make_interface_dumper.py"], + run_in_subprocess(make_interface_dumper.run), +) env_extension = env.Clone() diff --git a/core/extension/extension_api_dump.cpp b/core/extension/extension_api_dump.cpp index 867b1fc637..a2c507e350 100644 --- a/core/extension/extension_api_dump.cpp +++ b/core/extension/extension_api_dump.cpp @@ -29,6 +29,7 @@ /*************************************************************************/ #include "extension_api_dump.h" + #include "core/config/engine.h" #include "core/core_constants.h" #include "core/io/file_access.h" @@ -38,7 +39,15 @@ #ifdef TOOLS_ENABLED -static String get_type_name(const PropertyInfo &p_info) { +static String get_builtin_or_variant_type_name(const Variant::Type p_type) { + if (p_type == Variant::NIL) { + return "Variant"; + } else { + return Variant::get_type_name(p_type); + } +} + +static String get_property_info_type_name(const PropertyInfo &p_info) { if (p_info.type == Variant::INT && (p_info.hint == PROPERTY_HINT_INT_IS_POINTER)) { if (p_info.hint_string.is_empty()) { return "void*"; @@ -46,12 +55,18 @@ static String get_type_name(const PropertyInfo &p_info) { return p_info.hint_string + "*"; } } + if (p_info.type == Variant::ARRAY && (p_info.hint == PROPERTY_HINT_ARRAY_TYPE)) { + return String("typedarray::") + p_info.hint_string; + } if (p_info.type == Variant::INT && (p_info.usage & (PROPERTY_USAGE_CLASS_IS_ENUM))) { return String("enum::") + String(p_info.class_name); } if (p_info.type == Variant::INT && (p_info.usage & (PROPERTY_USAGE_CLASS_IS_BITFIELD))) { return String("bitfield::") + String(p_info.class_name); } + if (p_info.type == Variant::INT && (p_info.usage & PROPERTY_USAGE_ARRAY)) { + return "int"; + } if (p_info.class_name != StringName()) { return p_info.class_name; } @@ -64,7 +79,7 @@ static String get_type_name(const PropertyInfo &p_info) { if (p_info.type == Variant::NIL) { return "void"; } - return Variant::get_type_name(p_info.type); + return get_builtin_or_variant_type_name(p_info.type); } Dictionary NativeExtensionAPIDump::generate_extension_api() { @@ -88,6 +103,7 @@ Dictionary NativeExtensionAPIDump::generate_extension_api() { } const uint32_t vec3_elems = 3; + const uint32_t vec4_elems = 4; const uint32_t ptrsize_32 = 4; const uint32_t ptrsize_64 = 8; static const char *build_config_name[4] = { "float_32", "float_64", "double_32", "double_64" }; @@ -138,7 +154,7 @@ Dictionary NativeExtensionAPIDump::generate_extension_api() { { Variant::AABB, (vec3_elems * 2) * sizeof(float), (vec3_elems * 2) * sizeof(float), (vec3_elems * 2) * sizeof(double), (vec3_elems * 2) * sizeof(double) }, { Variant::BASIS, (vec3_elems * 3) * sizeof(float), (vec3_elems * 3) * sizeof(float), (vec3_elems * 3) * sizeof(double), (vec3_elems * 3) * sizeof(double) }, { Variant::TRANSFORM3D, (vec3_elems * 4) * sizeof(float), (vec3_elems * 4) * sizeof(float), (vec3_elems * 4) * sizeof(double), (vec3_elems * 4) * sizeof(double) }, - { Variant::PROJECTION, 4 * 4 * sizeof(float), 4 * 4 * sizeof(float), 4 * 4 * sizeof(double), 4 * 4 * sizeof(double) }, + { Variant::PROJECTION, (vec4_elems * 4) * sizeof(float), (vec4_elems * 4) * sizeof(float), (vec4_elems * 4) * sizeof(double), (vec4_elems * 4) * sizeof(double) }, { Variant::COLOR, 4 * sizeof(float), 4 * sizeof(float), 4 * sizeof(float), 4 * sizeof(float) }, { Variant::STRING_NAME, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 }, { Variant::NODE_PATH, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 }, @@ -211,7 +227,7 @@ Dictionary NativeExtensionAPIDump::generate_extension_api() { String name = t == Variant::VARIANT_MAX ? String("Variant") : Variant::get_type_name(t); Dictionary d2; d2["name"] = name; - uint32_t size; + uint32_t size = 0; switch (i) { case 0: size = type_size_array[j].size_32_bits_real_float; @@ -284,6 +300,10 @@ Dictionary NativeExtensionAPIDump::generate_extension_api() { { Variant::BASIS, "z", vec3_elems * 2 * sizeof(float), vec3_elems * 2 * sizeof(float), vec3_elems * 2 * sizeof(double), vec3_elems * 2 * sizeof(double) }, { Variant::TRANSFORM3D, "basis", 0, 0, 0, 0 }, { Variant::TRANSFORM3D, "origin", (vec3_elems * 3) * sizeof(float), (vec3_elems * 3) * sizeof(float), (vec3_elems * 3) * sizeof(double), (vec3_elems * 3) * sizeof(double) }, + { Variant::PROJECTION, "x", 0, 0, 0, 0 }, + { Variant::PROJECTION, "y", vec4_elems * sizeof(float), vec4_elems * sizeof(float), vec4_elems * sizeof(double), vec4_elems * sizeof(double) }, + { Variant::PROJECTION, "z", vec4_elems * 2 * sizeof(float), vec4_elems * 2 * sizeof(float), vec4_elems * 2 * sizeof(double), vec4_elems * 2 * sizeof(double) }, + { Variant::PROJECTION, "w", vec4_elems * 3 * sizeof(float), vec4_elems * 3 * sizeof(float), vec4_elems * 3 * sizeof(double), vec4_elems * 3 * sizeof(double) }, { Variant::COLOR, "r", 0, 0, 0, 0 }, { Variant::COLOR, "g", sizeof(float), sizeof(float), sizeof(float), sizeof(float) }, { Variant::COLOR, "b", 2 * sizeof(float), 2 * sizeof(float), 2 * sizeof(float), 2 * sizeof(float) }, @@ -322,7 +342,7 @@ Dictionary NativeExtensionAPIDump::generate_extension_api() { last_type = t; } Dictionary d3; - uint32_t offset; + uint32_t offset = 0; switch (i) { case 0: offset = member_offset_array[idx].offset_32_bits_real_float; @@ -419,8 +439,7 @@ Dictionary NativeExtensionAPIDump::generate_extension_api() { Dictionary arg; String argname = vararg ? "arg" + itos(i + 1) : Variant::get_utility_function_argument_name(name, i); arg["name"] = argname; - Variant::Type argtype = Variant::get_utility_function_argument_type(name, i); - arg["type"] = argtype == Variant::NIL ? String("Variant") : Variant::get_type_name(argtype); + arg["type"] = get_builtin_or_variant_type_name(Variant::get_utility_function_argument_type(name, i)); //no default value support in utility functions arguments.push_back(arg); } @@ -450,11 +469,10 @@ Dictionary NativeExtensionAPIDump::generate_extension_api() { Dictionary d; d["name"] = Variant::get_type_name(type); if (Variant::has_indexing(type)) { - Variant::Type index_type = Variant::get_indexed_element_type(type); - d["indexing_return_type"] = index_type == Variant::NIL ? String("Variant") : Variant::get_type_name(index_type); + d["indexing_return_type"] = get_builtin_or_variant_type_name(Variant::get_indexed_element_type(type)); } - d["is_keyed"] = Variant::ValidatedKeyedSetter(type); + d["is_keyed"] = Variant::is_keyed(type); { //members @@ -465,7 +483,7 @@ Dictionary NativeExtensionAPIDump::generate_extension_api() { for (const StringName &member_name : member_names) { Dictionary d2; d2["name"] = String(member_name); - d2["type"] = Variant::get_type_name(Variant::get_member_type(type, member_name)); + d2["type"] = get_builtin_or_variant_type_name(Variant::get_member_type(type, member_name)); members.push_back(d2); } if (members.size()) { @@ -482,7 +500,7 @@ Dictionary NativeExtensionAPIDump::generate_extension_api() { Dictionary d2; d2["name"] = String(constant_name); Variant constant = Variant::get_constant_value(type, constant_name); - d2["type"] = Variant::get_type_name(constant.get_type()); + d2["type"] = get_builtin_or_variant_type_name(constant.get_type()); d2["value"] = constant.get_construct_string(); constants.push_back(d2); } @@ -533,9 +551,9 @@ Dictionary NativeExtensionAPIDump::generate_extension_api() { Dictionary d2; d2["name"] = Variant::get_operator_name(Variant::Operator(k)); if (k != Variant::OP_NEGATE && k != Variant::OP_POSITIVE && k != Variant::OP_NOT && k != Variant::OP_BIT_NEGATE) { - d2["right_type"] = Variant::get_type_name(Variant::Type(j)); + d2["right_type"] = get_builtin_or_variant_type_name(Variant::Type(j)); } - d2["return_type"] = Variant::get_type_name(Variant::get_operator_return_type(Variant::Operator(k), type, Variant::Type(j))); + d2["return_type"] = get_builtin_or_variant_type_name(Variant::get_operator_return_type(Variant::Operator(k), type, Variant::Type(j))); operators.push_back(d2); } } @@ -569,8 +587,7 @@ Dictionary NativeExtensionAPIDump::generate_extension_api() { for (int j = 0; j < argcount; j++) { Dictionary d3; d3["name"] = Variant::get_builtin_method_argument_name(type, method_name, j); - Variant::Type argtype = Variant::get_builtin_method_argument_type(type, method_name, j); - d3["type"] = argtype == Variant::NIL ? String("Variant") : Variant::get_type_name(argtype); + d3["type"] = get_builtin_or_variant_type_name(Variant::get_builtin_method_argument_type(type, method_name, j)); if (j >= (argcount - default_args.size())) { int dargidx = j - (argcount - default_args.size()); @@ -602,7 +619,7 @@ Dictionary NativeExtensionAPIDump::generate_extension_api() { for (int k = 0; k < argcount; k++) { Dictionary d3; d3["name"] = Variant::get_constructor_argument_name(type, j, k); - d3["type"] = Variant::get_type_name(Variant::get_constructor_argument_type(type, j, k)); + d3["type"] = get_builtin_or_variant_type_name(Variant::get_constructor_argument_type(type, j, k)); arguments.push_back(d3); } if (arguments.size()) { @@ -730,7 +747,7 @@ Dictionary NativeExtensionAPIDump::generate_extension_api() { d3["name"] = pinfo.name; } - d3["type"] = get_type_name(pinfo); + d3["type"] = get_property_info_type_name(pinfo); if (i == -1) { d2["return_value"] = d3; @@ -773,7 +790,7 @@ Dictionary NativeExtensionAPIDump::generate_extension_api() { if (i >= 0) { d3["name"] = pinfo.name; } - d3["type"] = get_type_name(pinfo); + d3["type"] = get_property_info_type_name(pinfo); if (method->get_argument_meta(i) > 0) { static const char *argmeta[11] = { "none", "int8", "int16", "int32", "int64", "uint8", "uint16", "uint32", "uint64", "float", "double" }; @@ -820,7 +837,7 @@ Dictionary NativeExtensionAPIDump::generate_extension_api() { for (int i = 0; i < F.arguments.size(); i++) { Dictionary d3; d3["name"] = F.arguments[i].name; - d3["type"] = get_type_name(F.arguments[i]); + d3["type"] = get_property_info_type_name(F.arguments[i]); arguments.push_back(d3); } if (arguments.size()) { @@ -840,15 +857,19 @@ Dictionary NativeExtensionAPIDump::generate_extension_api() { List<PropertyInfo> property_list; ClassDB::get_property_list(class_name, &property_list, true); for (const PropertyInfo &F : property_list) { - if (F.usage & PROPERTY_USAGE_CATEGORY || F.usage & PROPERTY_USAGE_GROUP || F.usage & PROPERTY_USAGE_SUBGROUP) { + if (F.usage & PROPERTY_USAGE_CATEGORY || F.usage & PROPERTY_USAGE_GROUP || F.usage & PROPERTY_USAGE_SUBGROUP || (F.type == Variant::NIL && F.usage & PROPERTY_USAGE_ARRAY)) { continue; //not real properties } if (F.name.begins_with("_")) { continue; //hidden property } + if (F.name.find("/") >= 0) { + // Ignore properties with '/' (slash) in the name. These are only meant for use in the inspector. + continue; + } StringName property_name = F.name; Dictionary d2; - d2["type"] = get_type_name(F); + d2["type"] = get_property_info_type_name(F); d2["name"] = String(property_name); d2["setter"] = ClassDB::get_property_setter(class_name, F.name); d2["getter"] = ClassDB::get_property_getter(class_name, F.name); @@ -918,9 +939,9 @@ void NativeExtensionAPIDump::generate_extension_json_file(const String &p_path) Ref<JSON> json; json.instantiate(); - String text = json->stringify(api, "\t", false); + String text = json->stringify(api, "\t", false) + "\n"; Ref<FileAccess> fa = FileAccess::open(p_path, FileAccess::WRITE); - CharString cs = text.ascii(); - fa->store_buffer((const uint8_t *)cs.ptr(), cs.length()); + fa->store_string(text); } -#endif + +#endif // TOOLS_ENABLED diff --git a/core/extension/gdnative_interface.cpp b/core/extension/gdnative_interface.cpp index ef0b590030..864f2fa86b 100644 --- a/core/extension/gdnative_interface.cpp +++ b/core/extension/gdnative_interface.cpp @@ -61,8 +61,9 @@ static void gdnative_print_script_error(const char *p_description, const char *p _err_print_error(p_function, p_file, p_line, p_description, false, ERR_HANDLER_SCRIPT); } -uint64_t gdnative_get_native_struct_size(const char *p_name) { - return ClassDB::get_native_struct_size(p_name); +uint64_t gdnative_get_native_struct_size(const GDNativeStringNamePtr p_name) { + const StringName name = *reinterpret_cast<const StringName *>(p_name); + return ClassDB::get_native_struct_size(name); } // Variant functions @@ -81,11 +82,11 @@ static void gdnative_variant_destroy(GDNativeVariantPtr p_self) { static void gdnative_variant_call(GDNativeVariantPtr p_self, const GDNativeStringNamePtr p_method, const GDNativeVariantPtr *p_args, const GDNativeInt p_argcount, GDNativeVariantPtr r_return, GDNativeCallError *r_error) { Variant *self = (Variant *)p_self; - const StringName *method = (const StringName *)p_method; + const StringName method = *reinterpret_cast<const StringName *>(p_method); const Variant **args = (const Variant **)p_args; Variant ret; Callable::CallError error; - self->callp(*method, args, p_argcount, ret, error); + self->callp(method, args, p_argcount, ret, error); memnew_placement(r_return, Variant(ret)); if (r_error) { @@ -97,11 +98,11 @@ static void gdnative_variant_call(GDNativeVariantPtr p_self, const GDNativeStrin static void gdnative_variant_call_static(GDNativeVariantType p_type, const GDNativeStringNamePtr p_method, const GDNativeVariantPtr *p_args, const GDNativeInt p_argcount, GDNativeVariantPtr r_return, GDNativeCallError *r_error) { Variant::Type type = (Variant::Type)p_type; - const StringName *method = (const StringName *)p_method; + const StringName method = *reinterpret_cast<const StringName *>(p_method); const Variant **args = (const Variant **)p_args; Variant ret; Callable::CallError error; - Variant::call_static(type, *method, args, p_argcount, ret, error); + Variant::call_static(type, method, args, p_argcount, ret, error); memnew_placement(r_return, Variant(ret)); if (r_error) { @@ -251,27 +252,6 @@ static GDNativeBool gdnative_variant_booleanize(const GDNativeVariantPtr p_self) return self->booleanize(); } -static void gdnative_variant_sub(const GDNativeVariantPtr p_a, const GDNativeVariantPtr p_b, GDNativeVariantPtr r_dst) { - const Variant *a = (const Variant *)p_a; - const Variant *b = (const Variant *)p_b; - memnew_placement(r_dst, Variant); - Variant::sub(*a, *b, *(Variant *)r_dst); -} - -static void gdnative_variant_blend(const GDNativeVariantPtr p_a, const GDNativeVariantPtr p_b, float p_c, GDNativeVariantPtr r_dst) { - const Variant *a = (const Variant *)p_a; - const Variant *b = (const Variant *)p_b; - memnew_placement(r_dst, Variant); - Variant::blend(*a, *b, p_c, *(Variant *)r_dst); -} - -static void gdnative_variant_interpolate(const GDNativeVariantPtr p_a, const GDNativeVariantPtr p_b, float p_c, GDNativeVariantPtr r_dst) { - const Variant *a = (const Variant *)p_a; - const Variant *b = (const Variant *)p_b; - memnew_placement(r_dst, Variant); - Variant::interpolate(*a, *b, p_c, *(Variant *)r_dst); -} - static void gdnative_variant_duplicate(const GDNativeVariantPtr p_self, GDNativeVariantPtr r_ret, GDNativeBool p_deep) { const Variant *self = (const Variant *)p_self; memnew_placement(r_ret, Variant(self->duplicate(p_deep))); @@ -490,11 +470,11 @@ static GDNativeTypeFromVariantConstructorFunc gdnative_get_type_from_variant_con static GDNativePtrOperatorEvaluator gdnative_variant_get_ptr_operator_evaluator(GDNativeVariantOperator p_operator, GDNativeVariantType p_type_a, GDNativeVariantType p_type_b) { return (GDNativePtrOperatorEvaluator)Variant::get_ptr_operator_evaluator(Variant::Operator(p_operator), Variant::Type(p_type_a), Variant::Type(p_type_b)); } -static GDNativePtrBuiltInMethod gdnative_variant_get_ptr_builtin_method(GDNativeVariantType p_type, const char *p_method, GDNativeInt p_hash) { - StringName method = p_method; +static GDNativePtrBuiltInMethod gdnative_variant_get_ptr_builtin_method(GDNativeVariantType p_type, const GDNativeStringNamePtr p_method, GDNativeInt p_hash) { + const StringName method = *reinterpret_cast<const StringName *>(p_method); uint32_t hash = Variant::get_builtin_method_hash(Variant::Type(p_type), method); if (hash != p_hash) { - ERR_PRINT_ONCE("Error getting method " + String(method) + ", hash mismatch."); + ERR_PRINT_ONCE("Error getting method " + method + ", hash mismatch."); return nullptr; } @@ -518,11 +498,13 @@ static void gdnative_variant_construct(GDNativeVariantType p_type, GDNativeVaria r_error->expected = error.expected; } } -static GDNativePtrSetter gdnative_variant_get_ptr_setter(GDNativeVariantType p_type, const char *p_member) { - return (GDNativePtrSetter)Variant::get_member_ptr_setter(Variant::Type(p_type), p_member); +static GDNativePtrSetter gdnative_variant_get_ptr_setter(GDNativeVariantType p_type, const GDNativeStringNamePtr p_member) { + const StringName member = *reinterpret_cast<const StringName *>(p_member); + return (GDNativePtrSetter)Variant::get_member_ptr_setter(Variant::Type(p_type), member); } -static GDNativePtrGetter gdnative_variant_get_ptr_getter(GDNativeVariantType p_type, const char *p_member) { - return (GDNativePtrGetter)Variant::get_member_ptr_getter(Variant::Type(p_type), p_member); +static GDNativePtrGetter gdnative_variant_get_ptr_getter(GDNativeVariantType p_type, const GDNativeStringNamePtr p_member) { + const StringName member = *reinterpret_cast<const StringName *>(p_member); + return (GDNativePtrGetter)Variant::get_member_ptr_getter(Variant::Type(p_type), member); } static GDNativePtrIndexedSetter gdnative_variant_get_ptr_indexed_setter(GDNativeVariantType p_type) { return (GDNativePtrIndexedSetter)Variant::get_member_ptr_indexed_setter(Variant::Type(p_type)); @@ -539,14 +521,15 @@ static GDNativePtrKeyedGetter gdnative_variant_get_ptr_keyed_getter(GDNativeVari static GDNativePtrKeyedChecker gdnative_variant_get_ptr_keyed_checker(GDNativeVariantType p_type) { return (GDNativePtrKeyedChecker)Variant::get_member_ptr_keyed_checker(Variant::Type(p_type)); } -static void gdnative_variant_get_constant_value(GDNativeVariantType p_type, const char *p_constant, GDNativeVariantPtr r_ret) { - memnew_placement(r_ret, Variant(Variant::get_constant_value(Variant::Type(p_type), p_constant))); +static void gdnative_variant_get_constant_value(GDNativeVariantType p_type, const GDNativeStringNamePtr p_constant, GDNativeVariantPtr r_ret) { + StringName constant = *reinterpret_cast<const StringName *>(p_constant); + memnew_placement(r_ret, Variant(Variant::get_constant_value(Variant::Type(p_type), constant))); } -static GDNativePtrUtilityFunction gdnative_variant_get_ptr_utility_function(const char *p_function, GDNativeInt p_hash) { - StringName function = p_function; +static GDNativePtrUtilityFunction gdnative_variant_get_ptr_utility_function(const GDNativeStringNamePtr p_function, GDNativeInt p_hash) { + StringName function = *reinterpret_cast<const StringName *>(p_function); uint32_t hash = Variant::get_utility_function_hash(function); if (hash != p_hash) { - ERR_PRINT_ONCE("Error getting utility function " + String(function) + ", hash mismatch."); + ERR_PRINT_ONCE("Error getting utility function " + function + ", hash mismatch."); return nullptr; } return (GDNativePtrUtilityFunction)Variant::get_ptr_utility_function(function); @@ -580,7 +563,7 @@ static void gdnative_string_new_with_utf32_chars(GDNativeStringPtr r_dest, const static void gdnative_string_new_with_wide_chars(GDNativeStringPtr r_dest, const wchar_t *p_contents) { String *dest = (String *)r_dest; - if (sizeof(wchar_t) == 2) { + if constexpr (sizeof(wchar_t) == 2) { // wchar_t is 16 bit, parse. memnew_placement(dest, String); dest->parse_utf16((const char16_t *)p_contents); @@ -617,7 +600,7 @@ static void gdnative_string_new_with_utf32_chars_and_len(GDNativeStringPtr r_des static void gdnative_string_new_with_wide_chars_and_len(GDNativeStringPtr r_dest, const wchar_t *p_contents, const GDNativeInt p_size) { String *dest = (String *)r_dest; - if (sizeof(wchar_t) == 2) { + if constexpr (sizeof(wchar_t) == 2) { // wchar_t is 16 bit, parse. memnew_placement(dest, String); dest->parse_utf16((const char16_t *)p_contents, p_size); @@ -676,7 +659,7 @@ static GDNativeInt gdnative_string_to_utf32_chars(const GDNativeStringPtr p_self return len; } static GDNativeInt gdnative_string_to_wide_chars(const GDNativeStringPtr p_self, wchar_t *r_text, GDNativeInt p_max_write_length) { - if (sizeof(wchar_t) == 4) { + if constexpr (sizeof(wchar_t) == 4) { return gdnative_string_to_utf32_chars(p_self, (char32_t *)r_text, p_max_write_length); } else { return gdnative_string_to_utf16_chars(p_self, (char16_t *)r_text, p_max_write_length); @@ -857,8 +840,9 @@ static void gdnative_object_destroy(GDNativeObjectPtr p_o) { memdelete((Object *)p_o); } -static GDNativeObjectPtr gdnative_global_get_singleton(const char *p_name) { - return (GDNativeObjectPtr)Engine::get_singleton()->get_singleton_object(String(p_name)); +static GDNativeObjectPtr gdnative_global_get_singleton(const GDNativeStringNamePtr p_name) { + const StringName name = *reinterpret_cast<const StringName *>(p_name); + return (GDNativeObjectPtr)Engine::get_singleton()->get_singleton_object(name); } static void *gdnative_object_get_instance_binding(GDNativeObjectPtr p_object, void *p_token, const GDNativeInstanceBindingCallbacks *p_callbacks) { @@ -871,9 +855,10 @@ static void gdnative_object_set_instance_binding(GDNativeObjectPtr p_object, voi o->set_instance_binding(p_token, p_binding, p_callbacks); } -static void gdnative_object_set_instance(GDNativeObjectPtr p_object, const char *p_classname, GDExtensionClassInstancePtr p_instance) { +static void gdnative_object_set_instance(GDNativeObjectPtr p_object, const GDNativeStringNamePtr p_classname, GDExtensionClassInstancePtr p_instance) { + const StringName classname = *reinterpret_cast<const StringName *>(p_classname); Object *o = (Object *)p_object; - ClassDB::set_object_extension_instance(o, p_classname, p_instance); + ClassDB::set_object_extension_instance(o, classname, p_instance); } static GDNativeObjectPtr gdnative_object_get_instance_from_id(GDObjectInstanceID p_instance_id) { @@ -901,23 +886,26 @@ static GDNativeScriptInstancePtr gdnative_script_instance_create(const GDNativeE return reinterpret_cast<GDNativeScriptInstancePtr>(script_instance_extension); } -static GDNativeMethodBindPtr gdnative_classdb_get_method_bind(const char *p_classname, const char *p_methodname, GDNativeInt p_hash) { - MethodBind *mb = ClassDB::get_method(StringName(p_classname), StringName(p_methodname)); +static GDNativeMethodBindPtr gdnative_classdb_get_method_bind(const GDNativeStringNamePtr p_classname, const GDNativeStringNamePtr p_methodname, GDNativeInt p_hash) { + const StringName classname = *reinterpret_cast<const StringName *>(p_classname); + const StringName methodname = *reinterpret_cast<const StringName *>(p_methodname); + MethodBind *mb = ClassDB::get_method(classname, methodname); ERR_FAIL_COND_V(!mb, nullptr); if (mb->get_hash() != p_hash) { - ERR_PRINT_ONCE("Hash mismatch for method '" + String(p_classname) + "." + String(p_methodname) + "'."); + ERR_PRINT("Hash mismatch for method '" + classname + "." + methodname + "'."); return nullptr; } - // MethodBind *mb = ClassDB::get_method("Node", "get_name"); return (GDNativeMethodBindPtr)mb; } -static GDNativeObjectPtr gdnative_classdb_construct_object(const char *p_classname) { - return (GDNativeObjectPtr)ClassDB::instantiate(p_classname); +static GDNativeObjectPtr gdnative_classdb_construct_object(const GDNativeStringNamePtr p_classname) { + const StringName classname = *reinterpret_cast<const StringName *>(p_classname); + return (GDNativeObjectPtr)ClassDB::instantiate(classname); } -static void *gdnative_classdb_get_class_tag(const char *p_classname) { - ClassDB::ClassInfo *class_info = ClassDB::classes.getptr(p_classname); +static void *gdnative_classdb_get_class_tag(const GDNativeStringNamePtr p_classname) { + const StringName classname = *reinterpret_cast<const StringName *>(p_classname); + ClassDB::ClassInfo *class_info = ClassDB::classes.getptr(classname); return class_info ? class_info->class_ptr : nullptr; } @@ -970,9 +958,6 @@ void gdnative_setup_interface(GDNativeInterface *p_interface) { gdni.variant_recursive_hash = gdnative_variant_recursive_hash; gdni.variant_hash_compare = gdnative_variant_hash_compare; gdni.variant_booleanize = gdnative_variant_booleanize; - gdni.variant_sub = gdnative_variant_sub; - gdni.variant_blend = gdnative_variant_blend; - gdni.variant_interpolate = gdnative_variant_interpolate; gdni.variant_duplicate = gdnative_variant_duplicate; gdni.variant_stringify = gdnative_variant_stringify; diff --git a/core/extension/gdnative_interface.h b/core/extension/gdnative_interface.h index 041a6e5112..50410c4857 100644 --- a/core/extension/gdnative_interface.h +++ b/core/extension/gdnative_interface.h @@ -109,6 +109,7 @@ typedef enum { GDNATIVE_VARIANT_OP_LESS_EQUAL, GDNATIVE_VARIANT_OP_GREATER, GDNATIVE_VARIANT_OP_GREATER_EQUAL, + /* mathematic */ GDNATIVE_VARIANT_OP_ADD, GDNATIVE_VARIANT_OP_SUBTRACT, @@ -118,6 +119,7 @@ typedef enum { GDNATIVE_VARIANT_OP_POSITIVE, GDNATIVE_VARIANT_OP_MODULE, GDNATIVE_VARIANT_OP_POWER, + /* bitwise */ GDNATIVE_VARIANT_OP_SHIFT_LEFT, GDNATIVE_VARIANT_OP_SHIFT_RIGHT, @@ -125,11 +127,13 @@ typedef enum { GDNATIVE_VARIANT_OP_BIT_OR, GDNATIVE_VARIANT_OP_BIT_XOR, GDNATIVE_VARIANT_OP_BIT_NEGATE, + /* logic */ GDNATIVE_VARIANT_OP_AND, GDNATIVE_VARIANT_OP_OR, GDNATIVE_VARIANT_OP_XOR, GDNATIVE_VARIANT_OP_NOT, + /* containment */ GDNATIVE_VARIANT_OP_IN, GDNATIVE_VARIANT_OP_MAX @@ -152,11 +156,11 @@ typedef uint64_t GDObjectInstanceID; typedef enum { GDNATIVE_CALL_OK, GDNATIVE_CALL_ERROR_INVALID_METHOD, - GDNATIVE_CALL_ERROR_INVALID_ARGUMENT, /* expected is variant type */ - GDNATIVE_CALL_ERROR_TOO_MANY_ARGUMENTS, /* expected is number of arguments */ - GDNATIVE_CALL_ERROR_TOO_FEW_ARGUMENTS, /* expected is number of arguments */ + GDNATIVE_CALL_ERROR_INVALID_ARGUMENT, // Expected a different variant type. + GDNATIVE_CALL_ERROR_TOO_MANY_ARGUMENTS, // Expected lower number of arguments. + GDNATIVE_CALL_ERROR_TOO_FEW_ARGUMENTS, // Expected higher number of arguments. GDNATIVE_CALL_ERROR_INSTANCE_IS_NULL, - GDNATIVE_CALL_ERROR_METHOD_NOT_CONST, /* used for const call */ + GDNATIVE_CALL_ERROR_METHOD_NOT_CONST, // Used for const call. } GDNativeCallErrorType; typedef struct { @@ -201,51 +205,60 @@ typedef GDNativeBool (*GDNativeExtensionClassGet)(GDExtensionClassInstancePtr p_ typedef uint64_t (*GDNativeExtensionClassGetRID)(GDExtensionClassInstancePtr p_instance); typedef struct { - uint32_t type; - const char *name; - const char *class_name; - uint32_t hint; - const char *hint_string; - uint32_t usage; + GDNativeVariantType type; + GDNativeStringNamePtr name; + GDNativeStringNamePtr class_name; + uint32_t hint; // Bitfield of `PropertyHint` (defined in `extension_api.json`). + GDNativeStringPtr hint_string; + uint32_t usage; // Bitfield of `PropertyUsageFlags` (defined in `extension_api.json`). } GDNativePropertyInfo; typedef struct { - const char *name; + GDNativeStringNamePtr name; GDNativePropertyInfo return_value; - uint32_t flags; // From GDNativeExtensionClassMethodFlags + uint32_t flags; // Bitfield of `GDNativeExtensionClassMethodFlags`. int32_t id; - GDNativePropertyInfo *arguments; + + /* Arguments: `default_arguments` is an array of size `argument_count`. */ uint32_t argument_count; - GDNativeVariantPtr default_arguments; + GDNativePropertyInfo *arguments; + + /* Default arguments: `default_arguments` is an array of size `default_argument_count`. */ uint32_t default_argument_count; + GDNativeVariantPtr *default_arguments; } GDNativeMethodInfo; typedef const GDNativePropertyInfo *(*GDNativeExtensionClassGetPropertyList)(GDExtensionClassInstancePtr p_instance, uint32_t *r_count); typedef void (*GDNativeExtensionClassFreePropertyList)(GDExtensionClassInstancePtr p_instance, const GDNativePropertyInfo *p_list); +typedef GDNativeBool (*GDNativeExtensionClassPropertyCanRevert)(GDExtensionClassInstancePtr p_instance, const GDNativeStringNamePtr p_name); +typedef GDNativeBool (*GDNativeExtensionClassPropertyGetRevert)(GDExtensionClassInstancePtr p_instance, const GDNativeStringNamePtr p_name, GDNativeVariantPtr r_ret); typedef void (*GDNativeExtensionClassNotification)(GDExtensionClassInstancePtr p_instance, int32_t p_what); -typedef void (*GDNativeExtensionClassToString)(GDExtensionClassInstancePtr p_instance, GDNativeStringPtr p_out); +typedef void (*GDNativeExtensionClassToString)(GDExtensionClassInstancePtr p_instance, GDNativeBool *r_is_valid, GDNativeStringPtr p_out); typedef void (*GDNativeExtensionClassReference)(GDExtensionClassInstancePtr p_instance); typedef void (*GDNativeExtensionClassUnreference)(GDExtensionClassInstancePtr p_instance); typedef void (*GDNativeExtensionClassCallVirtual)(GDExtensionClassInstancePtr p_instance, const GDNativeTypePtr *p_args, GDNativeTypePtr r_ret); typedef GDNativeObjectPtr (*GDNativeExtensionClassCreateInstance)(void *p_userdata); typedef void (*GDNativeExtensionClassFreeInstance)(void *p_userdata, GDExtensionClassInstancePtr p_instance); -typedef void (*GDNativeExtensionClassObjectInstance)(GDExtensionClassInstancePtr p_instance, GDNativeObjectPtr p_object_instance); -typedef GDNativeExtensionClassCallVirtual (*GDNativeExtensionClassGetVirtual)(void *p_userdata, const char *p_name); +typedef GDNativeExtensionClassCallVirtual (*GDNativeExtensionClassGetVirtual)(void *p_userdata, const GDNativeStringNamePtr p_name); typedef struct { + GDNativeBool is_virtual; + GDNativeBool is_abstract; GDNativeExtensionClassSet set_func; GDNativeExtensionClassGet get_func; GDNativeExtensionClassGetPropertyList get_property_list_func; GDNativeExtensionClassFreePropertyList free_property_list_func; + GDNativeExtensionClassPropertyCanRevert property_can_revert_func; + GDNativeExtensionClassPropertyGetRevert property_get_revert_func; GDNativeExtensionClassNotification notification_func; GDNativeExtensionClassToString to_string_func; GDNativeExtensionClassReference reference_func; GDNativeExtensionClassUnreference unreference_func; - GDNativeExtensionClassCreateInstance create_instance_func; /* this one is mandatory */ - GDNativeExtensionClassFreeInstance free_instance_func; /* this one is mandatory */ - GDNativeExtensionClassGetVirtual get_virtual_func; + GDNativeExtensionClassCreateInstance create_instance_func; // (Default) constructor; mandatory. If the class is not instantiable, consider making it virtual or abstract. + GDNativeExtensionClassFreeInstance free_instance_func; // Destructor; mandatory. + GDNativeExtensionClassGetVirtual get_virtual_func; // Queries a virtual function by name and returns a callback to invoke the requested virtual function. GDNativeExtensionClassGetRID get_rid_func; - void *class_userdata; + void *class_userdata; // Per-class user data, later accessible in instance bindings. } GDNativeExtensionClassCreationInfo; typedef void *GDNativeExtensionClassLibraryPtr; @@ -279,29 +292,33 @@ typedef enum { typedef void (*GDNativeExtensionClassMethodCall)(void *method_userdata, GDExtensionClassInstancePtr p_instance, const GDNativeVariantPtr *p_args, const GDNativeInt p_argument_count, GDNativeVariantPtr r_return, GDNativeCallError *r_error); typedef void (*GDNativeExtensionClassMethodPtrCall)(void *method_userdata, GDExtensionClassInstancePtr p_instance, const GDNativeTypePtr *p_args, GDNativeTypePtr r_ret); -/* passing -1 as argument in the following functions refers to the return type */ -typedef GDNativeVariantType (*GDNativeExtensionClassMethodGetArgumentType)(void *p_method_userdata, int32_t p_argument); -typedef void (*GDNativeExtensionClassMethodGetArgumentInfo)(void *p_method_userdata, int32_t p_argument, GDNativePropertyInfo *r_info); -typedef GDNativeExtensionClassMethodArgumentMetadata (*GDNativeExtensionClassMethodGetArgumentMetadata)(void *p_method_userdata, int32_t p_argument); - typedef struct { - const char *name; + GDNativeStringNamePtr name; void *method_userdata; GDNativeExtensionClassMethodCall call_func; GDNativeExtensionClassMethodPtrCall ptrcall_func; - uint32_t method_flags; /* GDNativeExtensionClassMethodFlags */ - uint32_t argument_count; + uint32_t method_flags; // Bitfield of `GDNativeExtensionClassMethodFlags`. + + /* If `has_return_value` is false, `return_value_info` and `return_value_metadata` are ignored. */ GDNativeBool has_return_value; - GDNativeExtensionClassMethodGetArgumentType get_argument_type_func; - GDNativeExtensionClassMethodGetArgumentInfo get_argument_info_func; /* name and hint information for the argument can be omitted in release builds. Class name should always be present if it applies. */ - GDNativeExtensionClassMethodGetArgumentMetadata get_argument_metadata_func; + GDNativePropertyInfo *return_value_info; + GDNativeExtensionClassMethodArgumentMetadata return_value_metadata; + + /* Arguments: `arguments_info` and `arguments_metadata` are array of size `argument_count`. + * Name and hint information for the argument can be omitted in release builds. Class name should always be present if it applies. + */ + uint32_t argument_count; + GDNativePropertyInfo *arguments_info; + GDNativeExtensionClassMethodArgumentMetadata *arguments_metadata; + + /* Default arguments: `default_arguments` is an array of size `default_argument_count`. */ uint32_t default_argument_count; GDNativeVariantPtr *default_arguments; } GDNativeExtensionClassMethodInfo; /* SCRIPT INSTANCE EXTENSION */ -typedef void *GDNativeExtensionScriptInstanceDataPtr; // Pointer to custom ScriptInstance native implementation +typedef void *GDNativeExtensionScriptInstanceDataPtr; // Pointer to custom ScriptInstance native implementation. typedef GDNativeBool (*GDNativeExtensionScriptInstanceSet)(GDNativeExtensionScriptInstanceDataPtr p_instance, const GDNativeStringNamePtr p_name, const GDNativeVariantPtr p_value); typedef GDNativeBool (*GDNativeExtensionScriptInstanceGet)(GDNativeExtensionScriptInstanceDataPtr p_instance, const GDNativeStringNamePtr p_name, GDNativeVariantPtr r_ret); @@ -309,6 +326,9 @@ typedef const GDNativePropertyInfo *(*GDNativeExtensionScriptInstanceGetProperty typedef void (*GDNativeExtensionScriptInstanceFreePropertyList)(GDNativeExtensionScriptInstanceDataPtr p_instance, const GDNativePropertyInfo *p_list); typedef GDNativeVariantType (*GDNativeExtensionScriptInstanceGetPropertyType)(GDNativeExtensionScriptInstanceDataPtr p_instance, const GDNativeStringNamePtr p_name, GDNativeBool *r_is_valid); +typedef GDNativeBool (*GDNativeExtensionScriptInstancePropertyCanRevert)(GDNativeExtensionScriptInstanceDataPtr p_instance, const GDNativeStringNamePtr p_name); +typedef GDNativeBool (*GDNativeExtensionScriptInstancePropertyGetRevert)(GDNativeExtensionScriptInstanceDataPtr p_instance, const GDNativeStringNamePtr p_name, GDNativeVariantPtr r_ret); + typedef GDNativeObjectPtr (*GDNativeExtensionScriptInstanceGetOwner)(GDNativeExtensionScriptInstanceDataPtr p_instance); typedef void (*GDNativeExtensionScriptInstancePropertyStateAdd)(const GDNativeStringNamePtr p_name, const GDNativeVariantPtr p_value, void *p_userdata); typedef void (*GDNativeExtensionScriptInstanceGetPropertyState)(GDNativeExtensionScriptInstanceDataPtr p_instance, GDNativeExtensionScriptInstancePropertyStateAdd p_add_func, void *p_userdata); @@ -320,7 +340,7 @@ typedef GDNativeBool (*GDNativeExtensionScriptInstanceHasMethod)(GDNativeExtensi typedef void (*GDNativeExtensionScriptInstanceCall)(GDNativeExtensionScriptInstanceDataPtr p_self, const GDNativeStringNamePtr p_method, const GDNativeVariantPtr *p_args, const GDNativeInt p_argument_count, GDNativeVariantPtr r_return, GDNativeCallError *r_error); typedef void (*GDNativeExtensionScriptInstanceNotification)(GDNativeExtensionScriptInstanceDataPtr p_instance, int32_t p_what); -typedef const char *(*GDNativeExtensionScriptInstanceToString)(GDNativeExtensionScriptInstanceDataPtr p_instance, GDNativeBool *r_is_valid); +typedef void (*GDNativeExtensionScriptInstanceToString)(GDNativeExtensionScriptInstanceDataPtr p_instance, GDNativeBool *r_is_valid, GDNativeStringPtr r_out); typedef void (*GDNativeExtensionScriptInstanceRefCountIncremented)(GDNativeExtensionScriptInstanceDataPtr p_instance); typedef GDNativeBool (*GDNativeExtensionScriptInstanceRefCountDecremented)(GDNativeExtensionScriptInstanceDataPtr p_instance); @@ -341,13 +361,16 @@ typedef struct { GDNativeExtensionScriptInstanceGet get_func; GDNativeExtensionScriptInstanceGetPropertyList get_property_list_func; GDNativeExtensionScriptInstanceFreePropertyList free_property_list_func; - GDNativeExtensionScriptInstanceGetPropertyType get_property_type_func; + + GDNativeExtensionScriptInstancePropertyCanRevert property_can_revert_func; + GDNativeExtensionScriptInstancePropertyGetRevert property_get_revert_func; GDNativeExtensionScriptInstanceGetOwner get_owner_func; GDNativeExtensionScriptInstanceGetPropertyState get_property_state_func; GDNativeExtensionScriptInstanceGetMethodList get_method_list_func; GDNativeExtensionScriptInstanceFreeMethodList free_method_list_func; + GDNativeExtensionScriptInstanceGetPropertyType get_property_type_func; GDNativeExtensionScriptInstanceHasMethod has_method_func; @@ -381,6 +404,7 @@ typedef struct { const char *version_string; /* GODOT CORE */ + void *(*mem_alloc)(size_t p_bytes); void *(*mem_realloc)(void *p_ptr, size_t p_bytes); void (*mem_free)(void *p_ptr); @@ -389,7 +413,7 @@ typedef struct { void (*print_warning)(const char *p_description, const char *p_function, const char *p_file, int32_t p_line); void (*print_script_error)(const char *p_description, const char *p_function, const char *p_file, int32_t p_line); - uint64_t (*get_native_struct_size)(const char *p_name); + uint64_t (*get_native_struct_size)(const GDNativeStringNamePtr p_name); /* GODOT VARIANT */ @@ -417,9 +441,6 @@ typedef struct { GDNativeInt (*variant_recursive_hash)(const GDNativeVariantPtr p_self, GDNativeInt p_recursion_count); GDNativeBool (*variant_hash_compare)(const GDNativeVariantPtr p_self, const GDNativeVariantPtr p_other); GDNativeBool (*variant_booleanize)(const GDNativeVariantPtr p_self); - void (*variant_sub)(const GDNativeVariantPtr p_a, const GDNativeVariantPtr p_b, GDNativeVariantPtr r_dst); - void (*variant_blend)(const GDNativeVariantPtr p_a, const GDNativeVariantPtr p_b, float p_c, GDNativeVariantPtr r_dst); - void (*variant_interpolate)(const GDNativeVariantPtr p_a, const GDNativeVariantPtr p_b, float p_c, GDNativeVariantPtr r_dst); void (*variant_duplicate)(const GDNativeVariantPtr p_self, GDNativeVariantPtr r_ret, GDNativeBool p_deep); void (*variant_stringify)(const GDNativeVariantPtr p_self, GDNativeStringPtr r_ret); @@ -435,22 +456,21 @@ typedef struct { GDNativeVariantFromTypeConstructorFunc (*get_variant_from_type_constructor)(GDNativeVariantType p_type); GDNativeTypeFromVariantConstructorFunc (*get_variant_to_type_constructor)(GDNativeVariantType p_type); GDNativePtrOperatorEvaluator (*variant_get_ptr_operator_evaluator)(GDNativeVariantOperator p_operator, GDNativeVariantType p_type_a, GDNativeVariantType p_type_b); - GDNativePtrBuiltInMethod (*variant_get_ptr_builtin_method)(GDNativeVariantType p_type, const char *p_method, GDNativeInt p_hash); + GDNativePtrBuiltInMethod (*variant_get_ptr_builtin_method)(GDNativeVariantType p_type, const GDNativeStringNamePtr p_method, GDNativeInt p_hash); GDNativePtrConstructor (*variant_get_ptr_constructor)(GDNativeVariantType p_type, int32_t p_constructor); GDNativePtrDestructor (*variant_get_ptr_destructor)(GDNativeVariantType p_type); void (*variant_construct)(GDNativeVariantType p_type, GDNativeVariantPtr p_base, const GDNativeVariantPtr *p_args, int32_t p_argument_count, GDNativeCallError *r_error); - GDNativePtrSetter (*variant_get_ptr_setter)(GDNativeVariantType p_type, const char *p_member); - GDNativePtrGetter (*variant_get_ptr_getter)(GDNativeVariantType p_type, const char *p_member); + GDNativePtrSetter (*variant_get_ptr_setter)(GDNativeVariantType p_type, const GDNativeStringNamePtr p_member); + GDNativePtrGetter (*variant_get_ptr_getter)(GDNativeVariantType p_type, const GDNativeStringNamePtr p_member); GDNativePtrIndexedSetter (*variant_get_ptr_indexed_setter)(GDNativeVariantType p_type); GDNativePtrIndexedGetter (*variant_get_ptr_indexed_getter)(GDNativeVariantType p_type); GDNativePtrKeyedSetter (*variant_get_ptr_keyed_setter)(GDNativeVariantType p_type); GDNativePtrKeyedGetter (*variant_get_ptr_keyed_getter)(GDNativeVariantType p_type); GDNativePtrKeyedChecker (*variant_get_ptr_keyed_checker)(GDNativeVariantType p_type); - void (*variant_get_constant_value)(GDNativeVariantType p_type, const char *p_constant, GDNativeVariantPtr r_ret); - GDNativePtrUtilityFunction (*variant_get_ptr_utility_function)(const char *p_function, GDNativeInt p_hash); + void (*variant_get_constant_value)(GDNativeVariantType p_type, const GDNativeStringNamePtr p_constant, GDNativeVariantPtr r_ret); + GDNativePtrUtilityFunction (*variant_get_ptr_utility_function)(const GDNativeStringNamePtr p_function, GDNativeInt p_hash); /* extra utilities */ - void (*string_new_with_latin1_chars)(GDNativeStringPtr r_dest, const char *p_contents); void (*string_new_with_utf8_chars)(GDNativeStringPtr r_dest, const char *p_contents); void (*string_new_with_utf16_chars)(GDNativeStringPtr r_dest, const char16_t *p_contents); @@ -461,6 +481,7 @@ typedef struct { void (*string_new_with_utf16_chars_and_len)(GDNativeStringPtr r_dest, const char16_t *p_contents, const GDNativeInt p_size); void (*string_new_with_utf32_chars_and_len)(GDNativeStringPtr r_dest, const char32_t *p_contents, const GDNativeInt p_size); void (*string_new_with_wide_chars_and_len)(GDNativeStringPtr r_dest, const wchar_t *p_contents, const GDNativeInt p_size); + /* Information about the following functions: * - The return value is the resulting encoded string length. * - The length returned is in characters, not in bytes. It also does not include a trailing zero. @@ -516,12 +537,12 @@ typedef struct { void (*object_method_bind_call)(const GDNativeMethodBindPtr p_method_bind, GDNativeObjectPtr p_instance, const GDNativeVariantPtr *p_args, GDNativeInt p_arg_count, GDNativeVariantPtr r_ret, GDNativeCallError *r_error); void (*object_method_bind_ptrcall)(const GDNativeMethodBindPtr p_method_bind, GDNativeObjectPtr p_instance, const GDNativeTypePtr *p_args, GDNativeTypePtr r_ret); void (*object_destroy)(GDNativeObjectPtr p_o); - GDNativeObjectPtr (*global_get_singleton)(const char *p_name); + GDNativeObjectPtr (*global_get_singleton)(const GDNativeStringNamePtr p_name); void *(*object_get_instance_binding)(GDNativeObjectPtr p_o, void *p_token, const GDNativeInstanceBindingCallbacks *p_callbacks); void (*object_set_instance_binding)(GDNativeObjectPtr p_o, void *p_token, void *p_binding, const GDNativeInstanceBindingCallbacks *p_callbacks); - void (*object_set_instance)(GDNativeObjectPtr p_o, const char *p_classname, GDExtensionClassInstancePtr p_instance); /* p_classname should be a registered extension class and should extend the p_o object's class. */ + void (*object_set_instance)(GDNativeObjectPtr p_o, const GDNativeStringNamePtr p_classname, GDExtensionClassInstancePtr p_instance); /* p_classname should be a registered extension class and should extend the p_o object's class. */ GDNativeObjectPtr (*object_cast_to)(const GDNativeObjectPtr p_object, void *p_class_tag); GDNativeObjectPtr (*object_get_instance_from_id)(GDObjectInstanceID p_instance_id); @@ -532,20 +553,22 @@ typedef struct { GDNativeScriptInstancePtr (*script_instance_create)(const GDNativeExtensionScriptInstanceInfo *p_info, GDNativeExtensionScriptInstanceDataPtr p_instance_data); /* CLASSDB */ - GDNativeObjectPtr (*classdb_construct_object)(const char *p_classname); /* The passed class must be a built-in godot class, or an already-registered extension class. In both case, object_set_instance should be called to fully initialize the object. */ - GDNativeMethodBindPtr (*classdb_get_method_bind)(const char *p_classname, const char *p_methodname, GDNativeInt p_hash); - void *(*classdb_get_class_tag)(const char *p_classname); + + GDNativeObjectPtr (*classdb_construct_object)(const GDNativeStringNamePtr p_classname); /* The passed class must be a built-in godot class, or an already-registered extension class. In both case, object_set_instance should be called to fully initialize the object. */ + GDNativeMethodBindPtr (*classdb_get_method_bind)(const GDNativeStringNamePtr p_classname, const GDNativeStringNamePtr p_methodname, GDNativeInt p_hash); + void *(*classdb_get_class_tag)(const GDNativeStringNamePtr p_classname); /* CLASSDB EXTENSION */ - void (*classdb_register_extension_class)(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name, const char *p_parent_class_name, const GDNativeExtensionClassCreationInfo *p_extension_funcs); - void (*classdb_register_extension_class_method)(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name, const GDNativeExtensionClassMethodInfo *p_method_info); - void (*classdb_register_extension_class_integer_constant)(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name, const char *p_enum_name, const char *p_constant_name, GDNativeInt p_constant_value, GDNativeBool p_is_bitfield); - void (*classdb_register_extension_class_property)(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name, const GDNativePropertyInfo *p_info, const char *p_setter, const char *p_getter); - void (*classdb_register_extension_class_property_group)(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name, const char *p_group_name, const char *p_prefix); - void (*classdb_register_extension_class_property_subgroup)(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name, const char *p_subgroup_name, const char *p_prefix); - void (*classdb_register_extension_class_signal)(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name, const char *p_signal_name, const GDNativePropertyInfo *p_argument_info, GDNativeInt p_argument_count); - void (*classdb_unregister_extension_class)(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name); /* Unregistering a parent class before a class that inherits it will result in failure. Inheritors must be unregistered first. */ + /* Provided parameters for `classdb_register_extension_*` can be safely freed once the function returns. */ + void (*classdb_register_extension_class)(const GDNativeExtensionClassLibraryPtr p_library, const GDNativeStringNamePtr p_class_name, const GDNativeStringNamePtr p_parent_class_name, const GDNativeExtensionClassCreationInfo *p_extension_funcs); + void (*classdb_register_extension_class_method)(const GDNativeExtensionClassLibraryPtr p_library, const GDNativeStringNamePtr p_class_name, const GDNativeExtensionClassMethodInfo *p_method_info); + void (*classdb_register_extension_class_integer_constant)(const GDNativeExtensionClassLibraryPtr p_library, const GDNativeStringNamePtr p_class_name, const GDNativeStringNamePtr p_enum_name, const GDNativeStringNamePtr p_constant_name, GDNativeInt p_constant_value, GDNativeBool p_is_bitfield); + void (*classdb_register_extension_class_property)(const GDNativeExtensionClassLibraryPtr p_library, const GDNativeStringNamePtr p_class_name, const GDNativePropertyInfo *p_info, const GDNativeStringNamePtr p_setter, const GDNativeStringNamePtr p_getter); + void (*classdb_register_extension_class_property_group)(const GDNativeExtensionClassLibraryPtr p_library, const GDNativeStringNamePtr p_class_name, const GDNativeStringPtr p_group_name, const GDNativeStringPtr p_prefix); + void (*classdb_register_extension_class_property_subgroup)(const GDNativeExtensionClassLibraryPtr p_library, const GDNativeStringNamePtr p_class_name, const GDNativeStringPtr p_subgroup_name, const GDNativeStringPtr p_prefix); + void (*classdb_register_extension_class_signal)(const GDNativeExtensionClassLibraryPtr p_library, const GDNativeStringNamePtr p_class_name, const GDNativeStringNamePtr p_signal_name, const GDNativePropertyInfo *p_argument_info, GDNativeInt p_argument_count); + void (*classdb_unregister_extension_class)(const GDNativeExtensionClassLibraryPtr p_library, const GDNativeStringNamePtr p_class_name); /* Unregistering a parent class before a class that inherits it will result in failure. Inheritors must be unregistered first. */ void (*get_library_path)(const GDNativeExtensionClassLibraryPtr p_library, GDNativeStringPtr r_path); @@ -573,9 +596,10 @@ typedef struct { } GDNativeInitialization; /* Define a C function prototype that implements the function below and expose it to dlopen() (or similar). - * It will be called on initialization. The name must be an unique one specified in the .gdextension config file. + * This is the entry point of the GDExtension library and will be called on initialization. + * It can be used to set up different init levels, which are called during various stages of initialization/shutdown. + * The function name must be a unique one specified in the .gdextension config file. */ - typedef GDNativeBool (*GDNativeInitializationFunction)(const GDNativeInterface *p_interface, const GDNativeExtensionClassLibraryPtr p_library, GDNativeInitialization *r_initialization); #ifdef __cplusplus diff --git a/core/extension/make_interface_dumper.py b/core/extension/make_interface_dumper.py new file mode 100644 index 0000000000..cc85c728d5 --- /dev/null +++ b/core/extension/make_interface_dumper.py @@ -0,0 +1,47 @@ +def run(target, source, env): + src = source[0] + dst = target[0] + f = open(src, "r", encoding="utf-8") + g = open(dst, "w", encoding="utf-8") + + g.write( + """/* THIS FILE IS GENERATED DO NOT EDIT */ +#ifndef GDNATIVE_INTERFACE_DUMP_H +#define GDNATIVE_INTERFACE_DUMP_H + +#ifdef TOOLS_ENABLED + +#include "core/io/file_access.h" +#include "core/string/ustring.h" + +class GDNativeInterfaceDump { + private: + static constexpr char const *gdnative_interface_dump =""" + ) + for line in f: + g.write('"' + line.rstrip().replace('"', '\\"') + '\\n"\n') + g.write(";\n") + + g.write( + """ + public: + static void generate_gdnative_interface_file(const String &p_path) { + Ref<FileAccess> fa = FileAccess::open(p_path, FileAccess::WRITE); + CharString cs(gdnative_interface_dump); + fa->store_buffer((const uint8_t *)cs.ptr(), cs.length()); + }; +}; + +#endif // TOOLS_ENABLED + +#endif // GDNATIVE_INTERFACE_DUMP_H +""" + ) + g.close() + f.close() + + +if __name__ == "__main__": + from platform_methods import subprocess_main + + subprocess_main(globals()) diff --git a/core/extension/make_wrappers.py b/core/extension/make_wrappers.py index 862d313fba..1e4634ad2c 100644 --- a/core/extension/make_wrappers.py +++ b/core/extension/make_wrappers.py @@ -1,4 +1,60 @@ -proto = """ +proto_mod = """ +#define MODBIND$VER($RETTYPE m_name$ARG) \\ +virtual $RETVAL _##m_name($FUNCARGS) $CONST; \\ +_FORCE_INLINE_ virtual $RETVAL m_name($FUNCARGS) $CONST override { \\ + $RETX _##m_name($CALLARGS);\\ +} +""" + + +def generate_mod_version(argcount, const=False, returns=False): + s = proto_mod + sproto = str(argcount) + method_info = "" + if returns: + sproto += "R" + s = s.replace("$RETTYPE", "m_ret, ") + s = s.replace("$RETVAL", "m_ret") + s = s.replace("$RETX", "return") + + else: + s = s.replace("$RETTYPE", "") + s = s.replace("$RETVAL", "void") + s = s.replace("$RETX", "") + + if const: + sproto += "C" + s = s.replace("$CONST", "const") + else: + s = s.replace("$CONST", "") + + s = s.replace("$VER", sproto) + argtext = "" + funcargs = "" + callargs = "" + + for i in range(argcount): + if i > 0: + funcargs += ", " + callargs += ", " + + argtext += ", m_type" + str(i + 1) + funcargs += "m_type" + str(i + 1) + " arg" + str(i + 1) + callargs += "arg" + str(i + 1) + + if argcount: + s = s.replace("$ARG", argtext) + s = s.replace("$FUNCARGS", funcargs) + s = s.replace("$CALLARGS", callargs) + else: + s = s.replace("$ARG", "") + s = s.replace("$FUNCARGS", funcargs) + s = s.replace("$CALLARGS", callargs) + + return s + + +proto_ex = """ #define EXBIND$VER($RETTYPE m_name$ARG) \\ GDVIRTUAL$VER($RETTYPE_##m_name$ARG)\\ virtual $RETVAL m_name($FUNCARGS) $CONST override { \\ @@ -9,8 +65,8 @@ virtual $RETVAL m_name($FUNCARGS) $CONST override { \\ """ -def generate_version(argcount, const=False, returns=False): - s = proto +def generate_ex_version(argcount, const=False, returns=False): + s = proto_ex sproto = str(argcount) method_info = "" if returns: @@ -63,25 +119,28 @@ def generate_version(argcount, const=False, returns=False): def run(target, source, env): - max_versions = 12 txt = """ #ifndef GDEXTENSION_WRAPPERS_GEN_H #define GDEXTENSION_WRAPPERS_GEN_H - - """ for i in range(max_versions + 1): + txt += "\n/* Extension Wrapper " + str(i) + " Arguments */\n" + txt += generate_ex_version(i, False, False) + txt += generate_ex_version(i, False, True) + txt += generate_ex_version(i, True, False) + txt += generate_ex_version(i, True, True) - txt += "/* " + str(i) + " Arguments */\n\n" - txt += generate_version(i, False, False) - txt += generate_version(i, False, True) - txt += generate_version(i, True, False) - txt += generate_version(i, True, True) + for i in range(max_versions + 1): + txt += "\n/* Module Wrapper " + str(i) + " Arguments */\n" + txt += generate_mod_version(i, False, False) + txt += generate_mod_version(i, False, True) + txt += generate_mod_version(i, True, False) + txt += generate_mod_version(i, True, True) - txt += "#endif" + txt += "\n#endif\n" with open(target[0], "w") as f: f.write(txt) diff --git a/core/extension/native_extension.cpp b/core/extension/native_extension.cpp index a085df874e..83a2e80793 100644 --- a/core/extension/native_extension.cpp +++ b/core/extension/native_extension.cpp @@ -36,32 +36,43 @@ #include "core/os/os.h" String NativeExtension::get_extension_list_config_file() { - return ProjectSettings::get_singleton()->get_project_data_path().plus_file("extension_list.cfg"); + return ProjectSettings::get_singleton()->get_project_data_path().path_join("extension_list.cfg"); } class NativeExtensionMethodBind : public MethodBind { GDNativeExtensionClassMethodCall call_func; GDNativeExtensionClassMethodPtrCall ptrcall_func; - GDNativeExtensionClassMethodGetArgumentType get_argument_type_func; - GDNativeExtensionClassMethodGetArgumentInfo get_argument_info_func; - GDNativeExtensionClassMethodGetArgumentMetadata get_argument_metadata_func; void *method_userdata; bool vararg; + PropertyInfo return_value_info; + GodotTypeInfo::Metadata return_value_metadata; + List<PropertyInfo> arguments_info; + List<GodotTypeInfo::Metadata> arguments_metadata; protected: virtual Variant::Type _gen_argument_type(int p_arg) const override { - return Variant::Type(get_argument_type_func(method_userdata, p_arg)); + if (p_arg < 0) { + return return_value_info.type; + } else { + return arguments_info[p_arg].type; + } } virtual PropertyInfo _gen_argument_type_info(int p_arg) const override { - GDNativePropertyInfo pinfo; - get_argument_info_func(method_userdata, p_arg, &pinfo); - return PropertyInfo(pinfo); + if (p_arg < 0) { + return return_value_info; + } else { + return arguments_info[p_arg]; + } } public: #ifdef DEBUG_METHODS_ENABLED virtual GodotTypeInfo::Metadata get_argument_meta(int p_arg) const override { - return GodotTypeInfo::Metadata(get_argument_metadata_func(method_userdata, p_arg)); + if (p_arg < 0) { + return return_value_metadata; + } else { + return arguments_metadata[p_arg]; + } } #endif @@ -89,10 +100,17 @@ public: method_userdata = p_method_info->method_userdata; call_func = p_method_info->call_func; ptrcall_func = p_method_info->ptrcall_func; - get_argument_type_func = p_method_info->get_argument_type_func; - get_argument_info_func = p_method_info->get_argument_info_func; - get_argument_metadata_func = p_method_info->get_argument_metadata_func; - set_name(p_method_info->name); + set_name(*reinterpret_cast<StringName *>(p_method_info->name)); + + if (p_method_info->has_return_value) { + return_value_info = PropertyInfo(*p_method_info->return_value_info); + return_value_metadata = GodotTypeInfo::Metadata(p_method_info->return_value_metadata); + } + + for (uint32_t i = 0; i < p_method_info->argument_count; i++) { + arguments_info.push_back(PropertyInfo(p_method_info->arguments_info[i])); + arguments_metadata.push_back(GodotTypeInfo::Metadata(p_method_info->arguments_metadata[i])); + } set_hint_flags(p_method_info->method_flags); @@ -117,15 +135,15 @@ public: static GDNativeInterface gdnative_interface; -void NativeExtension::_register_extension_class(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name, const char *p_parent_class_name, const GDNativeExtensionClassCreationInfo *p_extension_funcs) { +void NativeExtension::_register_extension_class(const GDNativeExtensionClassLibraryPtr p_library, const GDNativeStringNamePtr p_class_name, const GDNativeStringNamePtr p_parent_class_name, const GDNativeExtensionClassCreationInfo *p_extension_funcs) { NativeExtension *self = static_cast<NativeExtension *>(p_library); - StringName class_name = p_class_name; + StringName class_name = *reinterpret_cast<StringName *>(p_class_name); + StringName parent_class_name = *reinterpret_cast<StringName *>(p_parent_class_name); ERR_FAIL_COND_MSG(!String(class_name).is_valid_identifier(), "Attempt to register extension class '" + class_name + "', which is not a valid class identifier."); ERR_FAIL_COND_MSG(ClassDB::class_exists(class_name), "Attempt to register extension class '" + class_name + "', which appears to be already registered."); Extension *parent_extension = nullptr; - StringName parent_class_name = p_parent_class_name; if (self->extension_classes.has(parent_class_name)) { parent_extension = &self->extension_classes[parent_class_name]; @@ -152,10 +170,14 @@ void NativeExtension::_register_extension_class(const GDNativeExtensionClassLibr extension->native_extension.parent_class_name = parent_class_name; extension->native_extension.class_name = class_name; extension->native_extension.editor_class = self->level_initialized == INITIALIZATION_LEVEL_EDITOR; + extension->native_extension.is_virtual = p_extension_funcs->is_virtual; + extension->native_extension.is_abstract = p_extension_funcs->is_abstract; extension->native_extension.set = p_extension_funcs->set_func; extension->native_extension.get = p_extension_funcs->get_func; extension->native_extension.get_property_list = p_extension_funcs->get_property_list_func; extension->native_extension.free_property_list = p_extension_funcs->free_property_list_func; + extension->native_extension.property_can_revert = p_extension_funcs->property_can_revert_func; + extension->native_extension.property_get_revert = p_extension_funcs->property_get_revert_func; extension->native_extension.notification = p_extension_funcs->notification_func; extension->native_extension.to_string = p_extension_funcs->to_string_func; extension->native_extension.reference = p_extension_funcs->reference_func; @@ -168,11 +190,11 @@ void NativeExtension::_register_extension_class(const GDNativeExtensionClassLibr ClassDB::register_extension_class(&extension->native_extension); } -void NativeExtension::_register_extension_class_method(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name, const GDNativeExtensionClassMethodInfo *p_method_info) { +void NativeExtension::_register_extension_class_method(const GDNativeExtensionClassLibraryPtr p_library, const GDNativeStringNamePtr p_class_name, const GDNativeExtensionClassMethodInfo *p_method_info) { NativeExtension *self = static_cast<NativeExtension *>(p_library); - StringName class_name = p_class_name; - StringName method_name = p_method_info->name; + StringName class_name = *reinterpret_cast<const StringName *>(p_class_name); + StringName method_name = *reinterpret_cast<const StringName *>(p_method_info->name); ERR_FAIL_COND_MSG(!self->extension_classes.has(class_name), "Attempt to register extension method '" + String(method_name) + "' for unexisting class '" + class_name + "'."); //Extension *extension = &self->extension_classes[class_name]; @@ -182,56 +204,63 @@ void NativeExtension::_register_extension_class_method(const GDNativeExtensionCl ClassDB::bind_method_custom(class_name, method); } -void NativeExtension::_register_extension_class_integer_constant(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name, const char *p_enum_name, const char *p_constant_name, GDNativeInt p_constant_value, GDNativeBool p_is_bitfield) { +void NativeExtension::_register_extension_class_integer_constant(const GDNativeExtensionClassLibraryPtr p_library, const GDNativeStringNamePtr p_class_name, const GDNativeStringNamePtr p_enum_name, const GDNativeStringNamePtr p_constant_name, GDNativeInt p_constant_value, GDNativeBool p_is_bitfield) { NativeExtension *self = static_cast<NativeExtension *>(p_library); - StringName class_name = p_class_name; - ERR_FAIL_COND_MSG(!self->extension_classes.has(class_name), "Attempt to register extension constant '" + String(p_constant_name) + "' for unexisting class '" + class_name + "'."); - - //Extension *extension = &self->extension_classes[class_name]; + StringName class_name = *reinterpret_cast<const StringName *>(p_class_name); + StringName enum_name = *reinterpret_cast<const StringName *>(p_enum_name); + StringName constant_name = *reinterpret_cast<const StringName *>(p_constant_name); + ERR_FAIL_COND_MSG(!self->extension_classes.has(class_name), "Attempt to register extension constant '" + constant_name + "' for unexisting class '" + class_name + "'."); - ClassDB::bind_integer_constant(class_name, p_enum_name, p_constant_name, p_constant_value, p_is_bitfield); + ClassDB::bind_integer_constant(class_name, enum_name, constant_name, p_constant_value, p_is_bitfield); } -void NativeExtension::_register_extension_class_property(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name, const GDNativePropertyInfo *p_info, const char *p_setter, const char *p_getter) { +void NativeExtension::_register_extension_class_property(const GDNativeExtensionClassLibraryPtr p_library, const GDNativeStringNamePtr p_class_name, const GDNativePropertyInfo *p_info, const GDNativeStringNamePtr p_setter, const GDNativeStringNamePtr p_getter) { NativeExtension *self = static_cast<NativeExtension *>(p_library); - StringName class_name = p_class_name; - String property_name = p_info->name; + StringName class_name = *reinterpret_cast<const StringName *>(p_class_name); + StringName setter = *reinterpret_cast<const StringName *>(p_setter); + StringName getter = *reinterpret_cast<const StringName *>(p_getter); + String property_name = *reinterpret_cast<const StringName *>(p_info->name); ERR_FAIL_COND_MSG(!self->extension_classes.has(class_name), "Attempt to register extension class property '" + property_name + "' for unexisting class '" + class_name + "'."); //Extension *extension = &self->extension_classes[class_name]; PropertyInfo pinfo(*p_info); - ClassDB::add_property(class_name, pinfo, p_setter, p_getter); + ClassDB::add_property(class_name, pinfo, setter, getter); } -void NativeExtension::_register_extension_class_property_group(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name, const char *p_group_name, const char *p_prefix) { +void NativeExtension::_register_extension_class_property_group(const GDNativeExtensionClassLibraryPtr p_library, const GDNativeStringNamePtr p_class_name, const GDNativeStringPtr p_group_name, const GDNativeStringPtr p_prefix) { NativeExtension *self = static_cast<NativeExtension *>(p_library); - StringName class_name = p_class_name; - ERR_FAIL_COND_MSG(!self->extension_classes.has(class_name), "Attempt to register extension class property group '" + String(p_group_name) + "' for unexisting class '" + class_name + "'."); + StringName class_name = *reinterpret_cast<const StringName *>(p_class_name); + String group_name = *reinterpret_cast<const String *>(p_group_name); + String prefix = *reinterpret_cast<const String *>(p_prefix); + ERR_FAIL_COND_MSG(!self->extension_classes.has(class_name), "Attempt to register extension class property group '" + group_name + "' for unexisting class '" + class_name + "'."); - ClassDB::add_property_group(class_name, p_group_name, p_prefix); + ClassDB::add_property_group(class_name, group_name, prefix); } -void NativeExtension::_register_extension_class_property_subgroup(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name, const char *p_subgroup_name, const char *p_prefix) { +void NativeExtension::_register_extension_class_property_subgroup(const GDNativeExtensionClassLibraryPtr p_library, const GDNativeStringNamePtr p_class_name, const GDNativeStringPtr p_subgroup_name, const GDNativeStringPtr p_prefix) { NativeExtension *self = static_cast<NativeExtension *>(p_library); - StringName class_name = p_class_name; - ERR_FAIL_COND_MSG(!self->extension_classes.has(class_name), "Attempt to register extension class property subgroup '" + String(p_subgroup_name) + "' for unexisting class '" + class_name + "'."); + StringName class_name = *reinterpret_cast<const StringName *>(p_class_name); + String subgroup_name = *reinterpret_cast<const String *>(p_subgroup_name); + String prefix = *reinterpret_cast<const String *>(p_prefix); + ERR_FAIL_COND_MSG(!self->extension_classes.has(class_name), "Attempt to register extension class property subgroup '" + subgroup_name + "' for unexisting class '" + class_name + "'."); - ClassDB::add_property_subgroup(class_name, p_subgroup_name, p_prefix); + ClassDB::add_property_subgroup(class_name, subgroup_name, prefix); } -void NativeExtension::_register_extension_class_signal(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name, const char *p_signal_name, const GDNativePropertyInfo *p_argument_info, GDNativeInt p_argument_count) { +void NativeExtension::_register_extension_class_signal(const GDNativeExtensionClassLibraryPtr p_library, const GDNativeStringNamePtr p_class_name, const GDNativeStringNamePtr p_signal_name, const GDNativePropertyInfo *p_argument_info, GDNativeInt p_argument_count) { NativeExtension *self = static_cast<NativeExtension *>(p_library); - StringName class_name = p_class_name; - ERR_FAIL_COND_MSG(!self->extension_classes.has(class_name), "Attempt to register extension class signal '" + String(p_signal_name) + "' for unexisting class '" + class_name + "'."); + StringName class_name = *reinterpret_cast<const StringName *>(p_class_name); + StringName signal_name = *reinterpret_cast<const StringName *>(p_signal_name); + ERR_FAIL_COND_MSG(!self->extension_classes.has(class_name), "Attempt to register extension class signal '" + signal_name + "' for unexisting class '" + class_name + "'."); MethodInfo s; - s.name = p_signal_name; + s.name = signal_name; for (int i = 0; i < p_argument_count; i++) { PropertyInfo arg(p_argument_info[i]); s.arguments.push_back(arg); @@ -239,10 +268,10 @@ void NativeExtension::_register_extension_class_signal(const GDNativeExtensionCl ClassDB::add_signal(class_name, s); } -void NativeExtension::_unregister_extension_class(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name) { +void NativeExtension::_unregister_extension_class(const GDNativeExtensionClassLibraryPtr p_library, const GDNativeStringNamePtr p_class_name) { NativeExtension *self = static_cast<NativeExtension *>(p_library); - StringName class_name = p_class_name; + StringName class_name = *reinterpret_cast<const StringName *>(p_class_name); ERR_FAIL_COND_MSG(!self->extension_classes.has(class_name), "Attempt to unregister unexisting extension class '" + class_name + "'."); Extension *ext = &self->extension_classes[class_name]; ERR_FAIL_COND_MSG(ext->native_extension.children.size(), "Attempt to unregister class '" + class_name + "' while other extension classes inherit from it."); @@ -419,7 +448,7 @@ Ref<Resource> NativeExtensionResourceLoader::load(const String &p_path, const St } if (!library_path.is_resource_file() && !library_path.is_absolute_path()) { - library_path = p_path.get_base_dir().plus_file(library_path); + library_path = p_path.get_base_dir().path_join(library_path); } Ref<NativeExtension> lib; diff --git a/core/extension/native_extension.h b/core/extension/native_extension.h index b7238d2899..70f6f9f039 100644 --- a/core/extension/native_extension.h +++ b/core/extension/native_extension.h @@ -47,14 +47,14 @@ class NativeExtension : public Resource { HashMap<StringName, Extension> extension_classes; - static void _register_extension_class(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name, const char *p_parent_class_name, const GDNativeExtensionClassCreationInfo *p_extension_funcs); - static void _register_extension_class_method(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name, const GDNativeExtensionClassMethodInfo *p_method_info); - static void _register_extension_class_integer_constant(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name, const char *p_enum_name, const char *p_constant_name, GDNativeInt p_constant_value, GDNativeBool p_is_bitfield); - static void _register_extension_class_property(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name, const GDNativePropertyInfo *p_info, const char *p_setter, const char *p_getter); - static void _register_extension_class_property_group(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name, const char *p_group_name, const char *p_prefix); - static void _register_extension_class_property_subgroup(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name, const char *p_subgroup_name, const char *p_prefix); - static void _register_extension_class_signal(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name, const char *p_signal_name, const GDNativePropertyInfo *p_argument_info, GDNativeInt p_argument_count); - static void _unregister_extension_class(const GDNativeExtensionClassLibraryPtr p_library, const char *p_class_name); + static void _register_extension_class(const GDNativeExtensionClassLibraryPtr p_library, const GDNativeStringNamePtr p_class_name, const GDNativeStringNamePtr p_parent_class_name, const GDNativeExtensionClassCreationInfo *p_extension_funcs); + static void _register_extension_class_method(const GDNativeExtensionClassLibraryPtr p_library, const GDNativeStringNamePtr p_class_name, const GDNativeExtensionClassMethodInfo *p_method_info); + static void _register_extension_class_integer_constant(const GDNativeExtensionClassLibraryPtr p_library, const GDNativeStringNamePtr p_class_name, const GDNativeStringNamePtr p_enum_name, const GDNativeStringNamePtr p_constant_name, GDNativeInt p_constant_value, GDNativeBool p_is_bitfield); + static void _register_extension_class_property(const GDNativeExtensionClassLibraryPtr p_library, const GDNativeStringNamePtr p_class_name, const GDNativePropertyInfo *p_info, const GDNativeStringNamePtr p_setter, const GDNativeStringNamePtr p_getter); + static void _register_extension_class_property_group(const GDNativeExtensionClassLibraryPtr p_library, const GDNativeStringNamePtr p_class_name, const GDNativeStringNamePtr p_group_name, const GDNativeStringNamePtr p_prefix); + static void _register_extension_class_property_subgroup(const GDNativeExtensionClassLibraryPtr p_library, const GDNativeStringNamePtr p_class_name, const GDNativeStringNamePtr p_subgroup_name, const GDNativeStringNamePtr p_prefix); + static void _register_extension_class_signal(const GDNativeExtensionClassLibraryPtr p_library, const GDNativeStringNamePtr p_class_name, const GDNativeStringNamePtr p_signal_name, const GDNativePropertyInfo *p_argument_info, GDNativeInt p_argument_count); + static void _unregister_extension_class(const GDNativeExtensionClassLibraryPtr p_library, const GDNativeStringNamePtr p_class_name); static void _get_library_path(const GDNativeExtensionClassLibraryPtr p_library, GDNativeStringPtr r_path); GDNativeInitialization initialization; diff --git a/core/input/gamecontrollerdb.txt b/core/input/gamecontrollerdb.txt index f5874a3dec..774d1be6b5 100644 --- a/core/input/gamecontrollerdb.txt +++ b/core/input/gamecontrollerdb.txt @@ -19,7 +19,9 @@ 03000000801000000900000000000000,8BitDo F30 Arcade Stick,a:b0,b:b1,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b6,lefttrigger:b8,leftx:a0,lefty:a1,rightshoulder:b7,righttrigger:b9,rightx:a2,righty:a5,start:b11,x:b3,y:b4,platform:Windows, 03000000c82d00001038000000000000,8BitDo F30 Pro,a:b1,b:b0,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b2,leftshoulder:b6,leftstick:b13,lefttrigger:b8,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b14,righttrigger:b9,rightx:a2,righty:a5,start:b11,x:b4,y:b3,platform:Windows, 03000000c82d00000090000000000000,8BitDo FC30 Pro,a:b1,b:b0,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b2,leftshoulder:b6,leftstick:b13,lefttrigger:b8,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b14,righttrigger:b9,rightx:a3,righty:a4,start:b11,x:b4,y:b3,platform:Windows, +03000000c82d00001251000000000000,8BitDo Lite 2,a:b1,b:b0,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b6,leftstick:b13,lefttrigger:b8,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b14,righttrigger:b9,rightx:a3,righty:a4,start:b11,x:b4,y:b3,platform:Windows, 03000000c82d00001151000000000000,8BitDo Lite SE,a:b1,b:b0,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b6,leftstick:b13,lefttrigger:b8,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b14,righttrigger:b9,rightx:a3,righty:a4,start:b11,x:b4,y:b3,platform:Windows, +03000000c82d00000150000000000000,8BitDo M30,a:b1,b:b0,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftx:a0,lefty:a1,rightshoulder:b6,righttrigger:b7,rightx:a3,righty:a5,start:b11,x:b4,y:b3,platform:Windows, 03000000c82d00000151000000000000,8BitDo M30,a:b0,b:b1,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftx:a0,lefty:a2,rightshoulder:b6,righttrigger:b7,rightx:a3,righty:a5,start:b11,x:b3,y:b4,platform:Windows, 03000000c82d00000650000000000000,8BitDo M30,a:b0,b:b1,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b2,leftshoulder:b8,lefttrigger:b9,leftx:a0,lefty:a1,rightshoulder:b6,righttrigger:b7,start:b11,x:b3,y:b4,platform:Windows, 03000000c82d00005106000000000000,8BitDo M30,a:b0,b:b1,back:b10,dpdown:+a2,dpleft:-a0,dpright:+a0,dpup:-a2,guide:b2,leftshoulder:b8,lefttrigger:b9,rightshoulder:b6,righttrigger:b7,start:b11,x:b3,y:b4,platform:Windows, @@ -67,6 +69,7 @@ 03000000c82d00000121000000000000,8BitDo SN30 Pro for Android,a:b0,b:b1,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b6,leftstick:b13,lefttrigger:b8,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b14,righttrigger:b9,rightx:a3,righty:a4,start:b11,x:b3,y:b4,platform:Windows, 03000000c82d00000260000000000000,8BitDo SN30 Pro+,a:b1,b:b0,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b2,leftshoulder:b6,leftstick:b13,lefttrigger:b8,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b14,righttrigger:b9,rightx:a2,righty:a3,start:b11,x:b4,y:b3,platform:Windows, 03000000c82d00000261000000000000,8BitDo SN30 Pro+,a:b1,b:b0,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b2,leftshoulder:b6,leftstick:b13,lefttrigger:b8,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b14,righttrigger:b9,rightx:a2,righty:a3,start:b11,x:b4,y:b3,platform:Windows, +03000000c82d00001330000000000000,8BitDo Ultimate Wireless,a:b0,b:b1,x:b3,y:b4,back:b10,guide:b12,start:b11,leftstick:b13,rightstick:b14,leftshoulder:b6,rightshoulder:b7,dpup:h0.1,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:b8,righttrigger:b9,paddle1:b23,paddle2:b19,platform:Windows, 03000000a00500003232000000000000,8BitDo Zero,a:b0,b:b1,back:b10,dpdown:+a2,dpleft:-a0,dpright:+a0,dpup:-a2,leftshoulder:b6,rightshoulder:b7,start:b11,x:b3,y:b4,platform:Windows, 03000000c82d00001890000000000000,8BitDo Zero 2,a:b1,b:b0,back:b10,dpdown:+a1,dpleft:-a0,dpright:+a0,dpup:-a1,leftshoulder:b6,rightshoulder:b7,start:b11,x:b4,y:b3,platform:Windows, 03000000c82d00003032000000000000,8BitDo Zero 2,a:b1,b:b0,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b6,leftx:a0,lefty:a1,rightshoulder:b7,rightx:a2,righty:a3,start:b11,x:b4,y:b3,platform:Windows, @@ -98,6 +101,8 @@ 03000000869800002500000000000000,Astro C40 TR PS4 Controller,a:b1,b:b2,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b4,leftstick:b10,lefttrigger:a3,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:a4,rightx:a2,righty:a5,start:b9,x:b0,y:b3,platform:Windows, 03000000a30c00002700000000000000,Astro City Mini,a:b2,b:b1,back:b8,dpdown:+a4,dpleft:-a3,dpright:+a3,dpup:-a4,rightshoulder:b4,righttrigger:b5,start:b9,x:b3,y:b0,platform:Windows, 03000000a30c00002800000000000000,Astro City Mini,a:b2,b:b1,back:b8,leftx:a3,lefty:a4,rightshoulder:b4,righttrigger:b5,start:b9,x:b3,y:b0,platform:Windows, +03000000050b00000579000000000000,ASUS ROG Kunai 3,a:b0,b:b1,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b6,leftstick:b13,lefttrigger:b8,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b14,righttrigger:b9,rightx:a3,righty:a4,start:b11,x:b3,y:b4,platform:Windows, +03000000050b00000679000000000000,ASUS ROG Kunai 3,a:b0,b:b1,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b6,leftstick:b13,lefttrigger:b8,leftx:a0,lefty:a1,misc1:b15,rightshoulder:b7,rightstick:b14,righttrigger:b9,rightx:a3,righty:a4,start:b11,x:b3,y:b4,platform:Windows, 03000000e4150000103f000000000000,Batarang,a:b0,b:b1,back:b6,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b4,leftstick:b8,lefttrigger:b10,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b9,righttrigger:b11,rightx:a3,righty:a4,start:b7,x:b2,y:b3,platform:Windows, 03000000d6200000e557000000000000,Batarang PlayStation Controller,a:b1,b:b2,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b0,y:b3,platform:Windows, 03000000c01100001352000000000000,Battalife Joystick,a:b6,b:b7,back:b2,leftshoulder:b0,leftx:a0,lefty:a1,rightshoulder:b1,start:b3,x:b4,y:b5,platform:Windows, @@ -211,6 +216,7 @@ 03000000300f00000b01000000000000,GGE909 Recoil,a:b2,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a3,righty:a2,start:b9,x:b3,y:b0,platform:Windows, 03000000f0250000c283000000000000,Gioteck PlayStation Controller,a:b2,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b3,y:b0,platform:Windows, 03000000f025000021c1000000000000,Gioteck PS3 Controller,a:b2,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b3,y:b0,platform:Windows, +03000000f025000031c1000000000000,Gioteck PS3 Controller,a:b2,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b3,y:b0,platform:Windows, 03000000f0250000c383000000000000,Gioteck VX2 PlayStation Controller,a:b2,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b3,y:b0,platform:Windows, 03000000f0250000c483000000000000,Gioteck VX2 PlayStation Controller,a:b2,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b3,y:b0,platform:Windows, 030000004f04000026b3000000000000,GP XID,a:b0,b:b1,back:b6,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b4,leftstick:b8,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b9,righttrigger:a5,rightx:a3,righty:a4,start:b7,x:b2,y:b3,platform:Windows, @@ -276,7 +282,7 @@ 030000000d0f00005c00000000000000,Hori Real Arcade Pro V4,a:b1,b:b2,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b0,y:b3,platform:Windows, 030000000d0f0000af00000000000000,Hori Real Arcade Pro VHS,a:b1,b:b2,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b4,leftstick:b10,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:a5,rightx:a3,righty:a4,start:b9,x:b0,y:b3,platform:Windows, 030000000d0f00001b00000000000000,Hori Real Arcade Pro VX,a:b0,b:b1,back:b6,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b4,lefttrigger:b8,leftx:a0,lefty:a1,rightshoulder:b5,righttrigger:b9,rightx:a3,righty:a4,start:b7,x:b2,y:b3,platform:Windows, -03000000ad1b000002f5000000000000,Hori Real Arcade Pro VX,a:b0,b:b1,back:b7,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b4,leftstick:b07,lefttrigger:b10,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b08,righttrigger:b11,rightx:a2,righty:a5,start:b6,x:b2,y:b3,platform:Windows, +03000000ad1b000002f5000000000000,Hori Real Arcade Pro VX,a:b0,b:b1,back:b7,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b4,lefttrigger:b10,leftx:a0,lefty:a1,rightshoulder:b5,righttrigger:b11,rightx:a2,righty:a5,start:b6,x:b2,y:b3,platform:Windows, 030000000d0f00009c00000000000000,Hori TAC Pro,a:b1,b:b2,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b4,leftstick:b10,lefttrigger:a3,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:a4,rightx:a2,righty:a5,start:b9,x:b0,y:b3,platform:Windows, 030000000d0f0000c900000000000000,Hori Taiko Controller,a:b1,b:b2,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b0,y:b3,platform:Windows, 030000000d0f0000c100000000000000,Horipad,a:b1,b:b2,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b0,y:b3,platform:Windows, @@ -363,6 +369,7 @@ 030000002a0600001024000000000000,Matricom,a:b0,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a3,righty:a4,start:b9,x:b2,y:b3,platform:Windows, 030000009f000000adbb000000000000,MaxJoypad Virtual Controller,a:b1,b:b2,back:b9,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b4,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,righttrigger:b7,rightx:a2,righty:a3,start:b8,x:b3,y:b0,platform:Windows, 03000000250900000128000000000000,Mayflash Arcade Stick,a:b1,b:b2,back:b8,leftshoulder:b0,lefttrigger:b4,leftx:a0,lefty:a1,rightshoulder:b3,righttrigger:b7,start:b9,x:b5,y:b6,platform:Windows, +03000000242f00003700000000000000,Mayflash F101,a:b1,b:b2,back:b8,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b4,lefttrigger:b6,rightshoulder:b5,righttrigger:b7,start:b9,x:b0,y:b3,platform:Windows, 03000000790000003018000000000000,Mayflash F300 Arcade Joystick,a:b1,b:b2,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b4,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,righttrigger:b7,start:b9,x:b0,y:b3,platform:Windows, 03000000242f00003900000000000000,Mayflash F300 Elite Arcade Joystick,a:b1,b:b2,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a5,start:b9,x:b0,y:b3,platform:Windows, 03000000790000004418000000000000,Mayflash GameCube Controller,a:b1,b:b2,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,lefttrigger:a3,leftx:a0,lefty:a1,rightshoulder:b7,righttrigger:a4,rightx:a5,righty:a2,start:b9,x:b0,y:b3,platform:Windows, @@ -435,6 +442,7 @@ 03000000790000002201000000000000,PC Controller,a:b2,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b3,y:b0,platform:Windows, 030000006f0e00008501000000000000,PDP Fightpad Pro,a:b2,b:b3,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b1,y:b0,platform:Windows, 030000006f0e00000901000000000000,PDP Versus Fighting,a:b1,b:b2,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b4,lefttrigger:b6,rightshoulder:b5,righttrigger:b7,start:b9,x:b0,y:b3,platform:Windows, +030000008f0e00004100000000000000,PlaySega,a:b1,b:b0,dpdown:+a1,dpleft:-a0,dpright:+a0,dpup:-a1,leftshoulder:b6,lefttrigger:b7,rightshoulder:b5,righttrigger:b2,start:b8,x:b4,y:b3,platform:Windows, 03000000e30500009605000000000000,PlayStation Adapter,a:b2,b:b1,back:b9,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b6,leftstick:b10,lefttrigger:b4,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b11,righttrigger:b5,rightx:a2,righty:a3,start:b8,x:b3,y:b0,platform:Windows, 030000004c050000da0c000000000000,PlayStation Classic Controller,a:b2,b:b1,back:b8,dpdown:+a1,dpleft:-a0,dpright:+a0,dpup:-a1,leftshoulder:b6,lefttrigger:b4,rightshoulder:b7,righttrigger:b5,start:b9,x:b3,y:b0,platform:Windows, 03000000632500002306000000000000,PlayStation Controller,a:b0,b:b1,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b6,leftstick:b13,lefttrigger:b8,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b14,righttrigger:b9,rightx:a2,righty:a3,start:b11,x:b3,y:b4,platform:Windows, @@ -459,6 +467,7 @@ 03000000666600006706000000000000,PS2 Controller,a:b2,b:b1,back:b8,dpdown:b14,dpleft:b15,dpright:b13,dpup:b12,leftshoulder:b6,leftstick:b9,lefttrigger:b4,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b10,righttrigger:b5,rightx:a2,righty:a3,start:b11,x:b3,y:b0,platform:Windows, 030000006b1400000303000000000000,PS2 Controller,a:b0,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b2,y:b3,platform:Windows, 030000009d0d00001330000000000000,PS2 Controller,a:b0,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b4,leftstick:b10,lefttrigger:b5,leftx:a0,lefty:a1,rightshoulder:b6,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b2,y:b3,platform:Windows, +03000000151a00006222000000000000,PS2 Dual Plus Adapter,a:b2,b:b1,back:b9,dpdown:b14,dpleft:b15,dpright:b13,dpup:b12,leftshoulder:b6,leftstick:b10,lefttrigger:b4,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b11,righttrigger:b5,rightx:a2,righty:a3,start:b8,x:b3,y:b0,platform:Windows, 03000000120a00000100000000000000,PS3 Controller,a:b0,b:b1,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b6,leftstick:b13,lefttrigger:b8,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b14,righttrigger:b9,rightx:a2,righty:a5,start:b11,x:b3,y:b4,platform:Windows, 03000000120c00001307000000000000,PS3 Controller,a:b1,b:b2,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b4,leftstick:b10,lefttrigger:a3,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:a4,rightx:a2,righty:a5,start:b9,x:b0,y:b3,platform:Windows, 03000000120c00001cf1000000000000,PS3 Controller,a:b1,b:b2,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b4,leftstick:b10,lefttrigger:a3,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:a4,rightx:a2,righty:a5,start:b9,x:b0,y:b3,platform:Windows, @@ -649,6 +658,7 @@ 030000004f04000003d0000000000000,ThrustMaster Run N Drive PlayStation Controller,a:b1,b:b2,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b7,leftshoulder:a3,leftstick:b10,lefttrigger:b4,leftx:a0,lefty:a1,rightshoulder:a4,rightstick:b11,righttrigger:b5,rightx:a2,righty:a5,start:b9,x:b0,y:b3,platform:Windows, 030000004f04000009d0000000000000,ThrustMaster Run N Drive PlayStation Controller,a:b1,b:b2,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b0,y:b3,platform:Windows, 030000006d04000088ca000000000000,Thunderpad,a:b0,b:b1,back:b7,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b5,leftstick:b8,lefttrigger:b10,leftx:a0,lefty:a1,rightshoulder:b4,rightstick:b9,righttrigger:b11,rightx:a3,righty:a4,start:b6,x:b2,y:b3,platform:Windows, +03000000666600000288000000000000,TigerGame PlayStation Adapter,a:b2,b:b1,back:b9,dpdown:b14,dpleft:b15,dpright:b13,dpup:b12,leftshoulder:b6,leftstick:b10,lefttrigger:b4,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b11,righttrigger:b5,rightx:a2,righty:a3,start:b8,x:b3,y:b0,platform:Windows, 03000000666600000488000000000000,TigerGame PlayStation Adapter,a:b2,b:b1,back:b9,dpdown:b14,dpleft:b15,dpright:b13,dpup:b12,leftshoulder:b6,leftstick:b10,lefttrigger:b4,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b11,righttrigger:b5,rightx:a2,righty:a3,start:b8,x:b3,y:b0,platform:Windows, 030000004f04000007d0000000000000,TMini,a:b1,b:b2,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b0,y:b3,platform:Windows, 03000000571d00002100000000000000,Tomee NES Controller Adapter,a:b1,b:b0,back:b2,dpdown:+a4,dpleft:-a0,dpright:+a0,dpup:-a4,start:b3,platform:Windows, @@ -667,7 +677,6 @@ 03000000300f00000701000000000000,USB Controller,a:b2,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a3,righty:a2,start:b9,x:b3,y:b0,platform:Windows, 03000000341a00002308000000000000,USB Controller,a:b0,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b2,y:b3,platform:Windows, 03000000666600000188000000000000,USB Controller,a:b2,b:b1,back:b9,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b6,leftstick:b10,lefttrigger:b4,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b11,righttrigger:b5,rightx:a2,righty:a3,start:b8,x:b3,y:b0,platform:Windows, -03000000666600000288000000000000,USB Controller,a:b2,b:b1,back:b9,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b6,leftstick:b10,lefttrigger:b4,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b11,righttrigger:b5,rightx:a2,righty:a3,start:b8,x:b3,y:b0,platform:Windows, 030000006b1400000203000000000000,USB Controller,a:b0,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b2,y:b3,platform:Windows, 03000000790000000a00000000000000,USB Controller,a:b2,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a4,start:b9,x:b3,y:b0,platform:Windows, 03000000b404000081c6000000000000,USB Controller,a:b2,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a5,start:b9,x:b3,y:b0,platform:Windows, @@ -731,11 +740,14 @@ 03000000341a00000608000000000000,Xeox,a:b0,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b2,y:b3,platform:Windows, 03000000450c00002043000000000000,Xeox SL6556BK,a:b0,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b2,y:b3,platform:Windows, 030000006f0e00000300000000000000,XGear,a:b2,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b6,leftstick:b10,lefttrigger:b4,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b11,righttrigger:b5,rightx:a5,righty:a2,start:b9,x:b3,y:b0,platform:Windows, -03000000172700004431000000000000,XiaoMi Controller,a:b0,b:b1,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b20,leftshoulder:b6,leftstick:b13,lefttrigger:b8,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b14,righttrigger:a7,rightx:a2,righty:a5,start:b11,x:b3,y:b4,platform:Windows, +03000000172700004431000000000000,Xiaomi Controller,a:b0,b:b1,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b20,leftshoulder:b6,leftstick:b13,lefttrigger:b8,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b14,righttrigger:a7,rightx:a2,righty:a5,start:b11,x:b3,y:b4,platform:Windows, +03000000bc2000005060000000000000,Xiaomi XMGP01YM,a:b0,b:b1,x:b3,y:b4,back:b10,guide:b12,start:b11,leftstick:b13,rightstick:b14,leftshoulder:b6,rightshoulder:b7,dpup:h0.1,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,leftx:a0,-lefty:-a1,+lefty:+a2,rightx:a3,-righty:-a4,+righty:+a5,lefttrigger:b8,righttrigger:b9,platform:Windows, +03000000172700003350000000000000,Xiaomi XMGP01YM,a:b0,b:b1,x:b3,y:b4,back:b10,guide:b12,start:b11,leftstick:b13,rightstick:b14,leftshoulder:b6,rightshoulder:b7,dpup:h0.1,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,leftx:a0,lefty:a1,rightx:a3,righty:a4,platform:Windows, 03000000786901006e70000000000000,XInput Controller,a:b0,b:b1,back:b6,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b10,leftshoulder:b4,leftstick:b8,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b9,righttrigger:a5,rightx:a3,righty:a4,start:b7,x:b2,y:b3,platform:Windows, xinput,XInput Controller,a:b0,b:b1,back:b6,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b10,leftshoulder:b4,leftstick:b8,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b9,righttrigger:a5,rightx:a3,righty:a4,start:b7,x:b2,y:b3,platform:Windows, 030000007d0400000340000000000000,Xterminator Digital Gamepad,a:b0,b:b1,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:-a4,lefttrigger:+a4,leftx:a0,lefty:a1,paddle1:b7,paddle2:b6,rightshoulder:b5,rightstick:b9,righttrigger:b2,rightx:a3,righty:a5,start:b8,x:b3,y:b4,platform:Windows, 03000000790000004f18000000000000,ZDT Android Controller,a:b0,b:b1,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b6,leftstick:b13,lefttrigger:b8,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b14,righttrigger:b9,rightx:a3,righty:a4,start:b11,x:b3,y:b4,platform:Windows, +03000000120c00000500000000000000,Zeroplus Adapter,a:b2,b:b1,back:b11,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b6,leftstick:b9,lefttrigger:b4,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b0,righttrigger:b5,rightx:a3,righty:a2,start:b8,x:b3,y:b0,platform:Windows, 03000000120c0000101e000000000000,Zeroplus P4 Gamepad,a:b1,b:b2,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b4,leftstick:b10,lefttrigger:a3,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:a4,rightx:a2,righty:a5,start:b9,x:b0,y:b3,platform:Windows, # Mac OS X @@ -746,6 +758,7 @@ xinput,XInput Controller,a:b0,b:b1,back:b6,dpdown:h0.4,dpleft:h0.8,dpright:h0.2, 03000000c82d00000090000001000000,8BitDo FC30 Pro,a:b1,b:b0,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b6,leftstick:b13,lefttrigger:a4,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b14,righttrigger:a5,rightx:a2,righty:a3,start:b11,x:b4,y:b3,platform:Mac OS X, 03000000c82d00001038000000010000,8BitDo FC30 Pro,a:b1,b:b0,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b6,leftstick:b13,lefttrigger:a5,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b14,righttrigger:a4,rightx:a2,righty:a3,start:b11,x:b4,y:b3,platform:Mac OS X, 03000000c82d00001251000000010000,8BitDo Lite 2,a:b1,b:b0,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b6,leftstick:b13,lefttrigger:a5,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b14,righttrigger:a4,rightx:a2,righty:a3,start:b11,x:b4,y:b3,platform:Mac OS X, +03000000c82d00001251000000020000,8BitDo Lite 2,a:b1,b:b0,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b6,leftstick:b13,lefttrigger:a5,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b14,righttrigger:a4,rightx:a2,righty:a3,start:b11,x:b4,y:b3,platform:Mac OS X, 03000000c82d00001151000000010000,8BitDo Lite SE,a:b1,b:b0,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b6,leftstick:b13,lefttrigger:a5,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b14,righttrigger:a4,rightx:a2,righty:a3,start:b11,x:b4,y:b3,platform:Mac OS X, 03000000c82d00001151000000020000,8BitDo Lite SE,a:b1,b:b0,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b6,leftstick:b13,lefttrigger:a5,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b14,righttrigger:a4,rightx:a2,righty:a3,start:b11,x:b4,y:b3,platform:Mac OS X, 03000000a30c00002400000006020000,8BitDo M30,a:b2,b:b1,dpdown:+a4,dpleft:-a3,dpright:+a3,dpup:-a4,guide:b9,leftshoulder:b6,lefttrigger:b5,rightshoulder:b4,righttrigger:b7,start:b8,x:b3,y:b0,platform:Mac OS X, @@ -787,6 +800,8 @@ xinput,XInput Controller,a:b0,b:b1,back:b6,dpdown:h0.4,dpleft:h0.8,dpright:h0.2, 03000000a30c00002700000003030000,Astro City Mini,a:b2,b:b1,back:b8,dpdown:+a4,dpleft:-a3,dpright:+a3,dpup:-a4,rightshoulder:b4,righttrigger:b5,start:b9,x:b3,y:b0,platform:Mac OS X, 03000000a30c00002800000003030000,Astro City Mini,a:b2,b:b1,back:b8,leftx:a3,lefty:a4,rightshoulder:b4,righttrigger:b5,start:b9,x:b3,y:b0,platform:Mac OS X, 03000000050b00000045000031000000,ASUS Gamepad,a:b0,b:b1,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b4,leftstick:b7,lefttrigger:a5,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b8,righttrigger:a4,rightx:a2,righty:a3,start:b9,x:b2,y:b3,platform:Mac OS X, +03000000050b00000579000000010000,ASUS ROG Kunai 3,a:b0,b:b1,back:b12,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b14,leftshoulder:b6,leftstick:b15,lefttrigger:a5,leftx:a0,lefty:a1,misc1:b42,paddle1:b9,paddle2:b11,rightshoulder:b7,rightstick:b16,righttrigger:a4,rightx:a2,righty:a3,start:b13,x:b3,y:b4,platform:Mac OS X, +03000000050b00000679000000010000,ASUS ROG Kunai 3,a:b0,b:b1,back:b12,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b14,leftshoulder:b6,leftstick:b15,lefttrigger:a5,leftx:a0,lefty:a1,misc1:b23,rightshoulder:b7,rightstick:b16,righttrigger:a4,rightx:a2,righty:a3,start:b13,x:b3,y:b4,platform:Mac OS X, 03000000c62400001a89000000010000,BDA MOGA XP5-X Plus,a:b0,b:b1,back:b12,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b14,leftshoulder:b6,leftstick:b15,lefttrigger:a5,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b16,righttrigger:a4,rightx:a2,righty:a3,start:b13,x:b3,y:b4,platform:Mac OS X, 03000000c62400001b89000000010000,BDA MOGA XP5-X Plus,a:b0,b:b1,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b6,leftstick:b13,lefttrigger:a5,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b14,righttrigger:a4,rightx:a2,righty:a3,start:b11,x:b3,y:b4,platform:Mac OS X, 03000000d62000002a79000000010000,BDA PS4 Fightpad,a:b1,b:b2,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b4,leftstick:b10,lefttrigger:a3,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:a4,rightx:a2,righty:a5,start:b9,x:b0,y:b3,platform:Mac OS X, @@ -973,6 +988,8 @@ xinput,XInput Controller,a:b0,b:b1,back:b6,dpdown:h0.4,dpleft:h0.8,dpright:h0.2, 03000000021000000090000011010000,8BitDo FC30 Pro,a:b1,b:b0,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b6,leftstick:b13,lefttrigger:b8,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b14,righttrigger:b9,rightx:a2,righty:a3,start:b11,x:b4,y:b3,platform:Linux, 03000000c82d00000090000011010000,8BitDo FC30 Pro,a:b1,b:b0,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b6,leftstick:b13,lefttrigger:a4,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b14,righttrigger:a5,rightx:a2,righty:a3,start:b11,x:b4,y:b3,platform:Linux, 05000000c82d00001038000000010000,8BitDo FC30 Pro,a:b1,b:b0,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b6,leftstick:b13,lefttrigger:b8,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b14,righttrigger:b9,rightx:a2,righty:a3,start:b11,x:b4,y:b3,platform:Linux, +03000000c82d00001251000011010000,8BitDo Lite 2,a:b1,b:b0,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b6,leftstick:b13,lefttrigger:a5,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b14,righttrigger:a5,rightx:a2,righty:a3,start:b11,x:b4,y:b3,platform:Linux, +05000000c82d00001251000000010000,8BitDo Lite 2,a:b1,b:b0,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b6,leftstick:b13,lefttrigger:a5,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b14,righttrigger:a4,rightx:a2,righty:a3,start:b11,x:b4,y:b3,platform:Linux, 03000000c82d00001151000011010000,8BitDo Lite SE,a:b1,b:b0,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b6,leftstick:b13,lefttrigger:b8,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b14,righttrigger:b9,rightx:a2,righty:a3,start:b11,x:b4,y:b3,platform:Linux, 05000000c82d00001151000000010000,8BitDo Lite SE,a:b1,b:b0,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b6,leftstick:b13,lefttrigger:b8,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b14,righttrigger:b9,rightx:a2,righty:a3,start:b11,x:b4,y:b3,platform:Linux, 03000000c82d00000151000000010000,8BitDo M30,a:b0,b:b1,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftx:a0,lefty:a1,rightshoulder:b6,righttrigger:b7,rightx:a2,righty:a3,start:b11,x:b3,y:b4,platform:Linux, @@ -1032,13 +1049,15 @@ xinput,XInput Controller,a:b0,b:b1,back:b6,dpdown:h0.4,dpleft:h0.8,dpright:h0.2, 03000000790000003018000011010000,Arcade Fightstick F300,a:b1,b:b2,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b4,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,righttrigger:b7,start:b9,x:b0,y:b3,platform:Linux, 03000000a30c00002700000011010000,Astro City Mini,a:b2,b:b1,back:b8,dpdown:+a1,dpleft:-a0,dpright:+a0,dpup:-a1,rightshoulder:b4,righttrigger:b5,start:b9,x:b3,y:b0,platform:Linux, 03000000a30c00002800000011010000,Astro City Mini,a:b2,b:b1,back:b8,leftx:a0,lefty:a1,rightshoulder:b4,righttrigger:b5,start:b9,x:b3,y:b0,platform:Linux, -05000000050b00000045000031000000,Asus Gamepad,a:b0,b:b1,back:b9,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b6,leftshoulder:b4,leftstick:b7,lefttrigger:a5,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b8,righttrigger:a4,rightx:a2,righty:a3,start:b10,x:b2,y:b3,platform:Linux, -05000000050b00000045000040000000,Asus Gamepad,a:b0,b:b1,back:b9,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b6,leftshoulder:b4,leftstick:b7,lefttrigger:a5,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b8,righttrigger:a4,rightx:a2,righty:a3,start:b10,x:b2,y:b3,platform:Linux, -03000000503200000110000000000000,Atari Classic Controller,a:b0,back:b2,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b4,start:b3,x:b1,platform:Linux, -03000000503200000110000011010000,Atari Classic Controller,a:b0,back:b2,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b4,start:b3,x:b1,platform:Linux, -05000000503200000110000000000000,Atari Classic Controller,a:b0,back:b2,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b4,start:b3,x:b1,platform:Linux, -05000000503200000110000044010000,Atari Classic Controller,a:b0,back:b2,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b4,start:b3,x:b1,platform:Linux, -05000000503200000110000046010000,Atari Classic Controller,a:b0,back:b2,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b4,start:b3,x:b1,platform:Linux, +05000000050b00000045000031000000,ASUS Gamepad,a:b0,b:b1,back:b9,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b6,leftshoulder:b4,leftstick:b7,lefttrigger:a5,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b8,righttrigger:a4,rightx:a2,righty:a3,start:b10,x:b2,y:b3,platform:Linux, +05000000050b00000045000040000000,ASUS Gamepad,a:b0,b:b1,back:b9,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b6,leftshoulder:b4,leftstick:b7,lefttrigger:a5,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b8,righttrigger:a4,rightx:a2,righty:a3,start:b10,x:b2,y:b3,platform:Linux, +03000000050b00000579000011010000,ASUS ROG Kunai 3,a:b0,b:b1,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b6,leftstick:b13,lefttrigger:a5,leftx:a0,lefty:a1,misc1:b36,paddle1:b52,paddle2:b53,rightshoulder:b7,rightstick:b14,righttrigger:a4,rightx:a2,righty:a3,start:b11,x:b3,y:b4,platform:Linux, +05000000050b00000679000000010000,ASUS ROG Kunai 3,a:b0,b:b1,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b6,leftstick:b13,lefttrigger:a5,leftx:a0,lefty:a1,misc1:b21,paddle1:b22,paddle2:b23,rightshoulder:b7,rightstick:b14,righttrigger:a4,rightx:a2,righty:a3,start:b11,x:b3,y:b4,platform:Linux, +03000000503200000110000000000000,Atari Classic Controller,a:b0,b:b1,back:b2,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b4,start:b3,platform:Linux, +03000000503200000110000011010000,Atari Classic Controller,a:b0,b:b1,back:b2,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b4,start:b3,platform:Linux, +05000000503200000110000000000000,Atari Classic Controller,a:b0,b:b1,back:b2,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b4,start:b3,platform:Linux, +05000000503200000110000044010000,Atari Classic Controller,a:b0,b:b1,back:b2,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b4,start:b3,platform:Linux, +05000000503200000110000046010000,Atari Classic Controller,a:b0,b:b1,back:b2,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b4,start:b3,platform:Linux, 03000000503200000210000000000000,Atari Controller,a:b0,b:b1,back:b9,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b10,leftshoulder:b4,leftstick:b6,lefttrigger:a5,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b7,righttrigger:a4,rightx:a2,righty:a3,start:b8,x:b2,y:b3,platform:Linux, 03000000503200000210000011010000,Atari Controller,a:b0,b:b1,back:b6,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b8,leftshoulder:b4,leftstick:b9,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b10,righttrigger:a5,rightx:a3,righty:a4,start:b7,x:b3,y:b2,platform:Linux, 05000000503200000210000000000000,Atari Controller,a:b0,b:b1,back:b6,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b8,leftshoulder:b4,leftstick:b9,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b10,righttrigger:a5,rightx:a3,righty:a4,start:b7,x:b3,y:b2,platform:Linux, @@ -1223,18 +1242,18 @@ xinput,XInput Controller,a:b0,b:b1,back:b6,dpdown:h0.4,dpleft:h0.8,dpright:h0.2, 060000007e0500003713000000000000,Nintendo 3DS,a:b0,b:b1,back:b8,dpdown:b11,dpleft:b12,dpright:b13,dpup:b10,leftshoulder:b4,leftx:a0,lefty:a1,rightshoulder:b5,rightx:a2,righty:a3,start:b9,x:b3,y:b2,platform:Linux, 030000007e0500003703000000016800,Nintendo GameCube Controller,a:b0,b:b2,dpdown:b6,dpleft:b4,dpright:b5,dpup:b7,lefttrigger:a4,leftx:a0,lefty:a1~,rightshoulder:b9,righttrigger:a5,rightx:a2,righty:a3~,start:b8,x:b1,y:b3,platform:Linux, 03000000790000004618000010010000,Nintendo GameCube Controller Adapter,a:b1,b:b0,dpdown:b14,dpleft:b15,dpright:b13,dpup:b12,lefttrigger:b4,leftx:a0,lefty:a1,rightshoulder:b7,righttrigger:b5,rightx:a5~,righty:a2~,start:b9,x:b2,y:b3,platform:Linux, -060000007e0500000620000000000000,Nintendo Switch Combined Joy-Cons,a:b0,b:b1,back:b9,dpdown:b15,dpleft:b16,dpright:b17,dpup:b14,leftshoulder:b5,leftstick:b12,lefttrigger:b7,leftx:a0,lefty:a1,rightshoulder:b6,rightstick:b13,righttrigger:b8,rightx:a2,righty:a3,start:b10,x:b3,y:b2,platform:Linux, -060000007e0500000820000000000000,Nintendo Switch Combined Joy-Cons,a:b0,b:b1,back:b9,dpdown:b15,dpleft:b16,dpright:b17,dpup:b14,guide:b11,leftshoulder:b5,leftstick:b12,lefttrigger:b7,leftx:a0,lefty:a1,rightshoulder:b6,rightstick:b13,righttrigger:b8,rightx:a2,righty:a3,start:b10,x:b3,y:b2,platform:Linux, -050000004c69632050726f20436f6e00,Nintendo Switch Controller,a:b0,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b2,y:b3,platform:Linux, +060000007e0500000620000000000000,Nintendo Switch Combined Joy-Cons,a:b0,b:b1,back:b9,dpdown:b15,dpleft:b16,dpright:b17,dpup:b14,guide:b11,leftshoulder:b5,leftstick:b12,lefttrigger:b7,leftx:a0,lefty:a1,misc1:b4,rightshoulder:b6,rightstick:b13,righttrigger:b8,rightx:a2,righty:a3,start:b10,x:b3,y:b2,platform:Linux, +060000007e0500000820000000000000,Nintendo Switch Combined Joy-Cons,a:b0,b:b1,back:b9,dpdown:b15,dpleft:b16,dpright:b17,dpup:b14,guide:b11,leftshoulder:b5,leftstick:b12,lefttrigger:b7,leftx:a0,lefty:a1,misc1:b4,rightshoulder:b6,rightstick:b13,righttrigger:b8,rightx:a2,righty:a3,start:b10,x:b3,y:b2,platform:Linux, +050000004c69632050726f20436f6e00,Nintendo Switch Controller,a:b0,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,misc1:b13,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b2,y:b3,platform:Linux, 050000007e0500000620000001800000,Nintendo Switch Left Joy-Con,a:b16,b:b15,back:b4,leftshoulder:b6,leftstick:b12,leftx:a1,lefty:a0~,rightshoulder:b8,start:b9,x:b14,y:b17,platform:Linux, 03000000d620000013a7000011010000,Nintendo Switch PowerA Controller,a:b1,b:b2,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b0,y:b3,platform:Linux, 03000000d620000011a7000011010000,Nintendo Switch PowerA Core Plus Controller,a:b1,b:b2,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b0,y:b3,platform:Linux, 030000007e0500000920000011810000,Nintendo Switch Pro Controller,a:b0,b:b1,back:b9,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b11,leftshoulder:b5,leftstick:b12,lefttrigger:b7,leftx:a0,lefty:a1,misc1:b4,rightshoulder:b6,rightstick:b13,righttrigger:b8,rightx:a2,righty:a3,start:b10,x:b3,y:b2,platform:Linux, 050000007e0500000920000001000000,Nintendo Switch Pro Controller,a:b0,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,misc1:b4,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b2,y:b3,platform:Linux, -050000007e0500000920000001800000,Nintendo Switch Pro Controller,a:b0,b:b1,back:b9,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b11,leftshoulder:b5,leftstick:b12,lefttrigger:b7,leftx:a0,lefty:a1,rightshoulder:b6,rightstick:b13,righttrigger:b8,rightx:a2,righty:a3,start:b10,x:b3,y:b2,platform:Linux, +050000007e0500000920000001800000,Nintendo Switch Pro Controller,a:b0,b:b1,back:b9,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b11,leftshoulder:b5,leftstick:b12,lefttrigger:b7,leftx:a0,lefty:a1,misc1:b4,rightshoulder:b6,rightstick:b13,righttrigger:b8,rightx:a2,righty:a3,start:b10,x:b3,y:b2,platform:Linux, 050000007e0500000720000001800000,Nintendo Switch Right Joy-Con,a:b1,b:b2,back:b9,leftshoulder:b4,leftstick:b10,leftx:a1~,lefty:a0,rightshoulder:b6,start:b8,x:b0,y:b3,platform:Linux, 05000000010000000100000003000000,Nintendo Wii Remote,a:b0,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b10,leftshoulder:b4,leftstick:b11,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b12,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b2,y:b3,platform:Linux, -050000007e0500003003000001000000,Nintendo Wii Remote Pro Controller,a:b0,b:b1,back:b8,dpdown:b14,dpleft:b15,dpright:b16,dpup:b13,guide:b10,leftshoulder:b4,leftstick:b11,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b12,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b3,y:b2,platform:Linux, +050000007e0500003003000001000000,Nintendo Wii U Pro Controller,a:b0,b:b1,back:b8,dpdown:b14,dpleft:b15,dpright:b16,dpup:b13,guide:b10,leftshoulder:b4,leftstick:b11,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b12,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b3,y:b2,platform:Linux, 030000000d0500000308000010010000,Nostromo n45 Dual Analog,a:b0,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b9,leftshoulder:b4,leftstick:b12,lefttrigger:b5,leftx:a0,lefty:a1,rightshoulder:b6,rightstick:b11,righttrigger:b7,rightx:a3,righty:a2,start:b10,x:b2,y:b3,platform:Linux, 050000007e0500001920000001000000,NSO N64 Controller,+rightx:b8,+righty:b7,-rightx:b3,-righty:b2,a:b1,b:b0,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b4,lefttrigger:b6,leftx:a0,lefty:a1,misc1:b13,rightshoulder:b5,righttrigger:b10,start:b9,platform:Linux, 050000007e0500001720000001000000,NSO SNES Controller,a:b0,b:b1,back:b9,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b11,leftshoulder:b5,leftstick:b12,lefttrigger:b7,leftx:a0,lefty:a1,rightshoulder:b6,rightstick:b13,righttrigger:b8,rightx:a2,righty:a3,start:b10,x:b3,y:b2,platform:Linux, @@ -1242,8 +1261,8 @@ xinput,XInput Controller,a:b0,b:b1,back:b6,dpdown:h0.4,dpleft:h0.8,dpright:h0.2, 03000000550900001472000011010000,NVIDIA Controller v01.04,a:b0,b:b1,back:b14,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b16,leftshoulder:b4,leftstick:b7,lefttrigger:a3,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b8,righttrigger:a4,rightx:a2,righty:a5,start:b6,x:b2,y:b3,platform:Linux, 05000000550900001472000001000000,NVIDIA Controller v01.04,a:b0,b:b1,back:b14,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b16,leftshoulder:b4,leftstick:b7,lefttrigger:a3,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b8,righttrigger:a4,rightx:a2,righty:a5,start:b6,x:b2,y:b3,platform:Linux, 03000000451300000830000010010000,NYKO CORE,a:b1,b:b2,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a5,start:b9,x:b0,y:b3,platform:Linux, -19000000010000000100000001010000,odroidgo2 joypad,a:b1,b:b0,dpdown:b7,dpleft:b8,dpright:b9,dpup:b6,guide:b10,leftshoulder:b4,leftstick:b12,lefttrigger:b11,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b13,righttrigger:b14,start:b15,x:b2,y:b3,platform:Linux, -19000000010000000200000011000000,odroidgo2 joypad v11,a:b1,b:b0,dpdown:b9,dpleft:b10,dpright:b11,dpup:b8,guide:b12,leftshoulder:b4,leftstick:b14,lefttrigger:b13,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b15,righttrigger:b16,start:b17,x:b2,y:b3,platform:Linux, +19000000010000000100000001010000,ODROID Go 2,a:b1,b:b0,dpdown:b7,dpleft:b8,dpright:b9,dpup:b6,guide:b10,leftshoulder:b4,leftstick:b12,lefttrigger:b11,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b13,righttrigger:b14,start:b15,x:b2,y:b3,platform:Linux, +19000000010000000200000011000000,ODROID Go 2,a:b1,b:b0,dpdown:b9,dpleft:b10,dpright:b11,dpup:b8,guide:b12,leftshoulder:b4,leftstick:b14,lefttrigger:b13,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b15,righttrigger:b16,start:b17,x:b2,y:b3,platform:Linux, 03000000c0160000dc27000001010000,OnyxSoft Dual JoyDivision,a:b0,b:b1,dpdown:+a1,dpleft:-a0,dpright:+a0,dpup:-a1,leftshoulder:b4,rightshoulder:b5,start:b6,x:b2,y:b3,platform:Linux, 05000000362800000100000002010000,OUYA Controller,a:b0,b:b3,dpdown:b9,dpleft:b10,dpright:b11,dpup:b8,guide:b14,leftshoulder:b4,leftstick:b6,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b7,righttrigger:a5,rightx:a3,righty:a4,x:b1,y:b2,platform:Linux, 05000000362800000100000003010000,OUYA Controller,a:b0,b:b3,dpdown:b9,dpleft:b10,dpright:b11,dpup:b8,guide:b14,leftshoulder:b4,leftstick:b6,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b7,righttrigger:a5,rightx:a3,righty:a4,x:b1,y:b2,platform:Linux, @@ -1383,6 +1402,7 @@ xinput,XInput Controller,a:b0,b:b1,back:b6,dpdown:h0.4,dpleft:h0.8,dpright:h0.2, 05000000de2800000212000001000000,Steam Controller,a:b0,b:b1,back:b6,dpdown:b14,dpleft:b15,dpright:b13,dpup:b12,guide:b8,leftshoulder:b4,leftstick:b9,lefttrigger:a2,leftx:a0,lefty:a1,paddle1:b11,paddle2:b10,rightshoulder:b5,righttrigger:a3,start:b7,x:b2,y:b3,platform:Linux, 05000000de2800000511000001000000,Steam Controller,a:b0,b:b1,back:b6,dpdown:b14,dpleft:b15,dpright:b13,dpup:b12,guide:b8,leftshoulder:b4,leftstick:b9,lefttrigger:a2,leftx:a0,lefty:a1,paddle1:b11,paddle2:b10,rightshoulder:b5,righttrigger:a3,start:b7,x:b2,y:b3,platform:Linux, 05000000de2800000611000001000000,Steam Controller,a:b0,b:b1,back:b6,dpdown:b14,dpleft:b15,dpright:b13,dpup:b12,guide:b8,leftshoulder:b4,leftstick:b9,lefttrigger:a2,leftx:a0,lefty:a1,paddle1:b11,paddle2:b10,rightshoulder:b5,righttrigger:a3,start:b7,x:b2,y:b3,platform:Linux, +03000000de2800000512000010010000,Steam Deck,a:b3,b:b4,back:b11,dpdown:b17,dpleft:b18,dpright:b19,dpup:b16,guide:b13,leftshoulder:b7,leftstick:b14,lefttrigger:a9,leftx:a0,lefty:a1,rightshoulder:b8,rightstick:b15,righttrigger:a8,rightx:a2,righty:a3,start:b12,x:b5,y:b6,platform:Linux, 03000000de280000ff11000001000000,Steam Virtual Gamepad,a:b0,b:b1,back:b6,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b8,leftshoulder:b4,leftstick:b9,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b10,righttrigger:a5,rightx:a3,righty:a4,start:b7,x:b2,y:b3,platform:Linux, 050000004e696d6275732b0000000000,SteelSeries Nimbus Plus,a:b0,b:b1,back:b10,guide:b11,leftshoulder:b4,leftstick:b8,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b9,righttrigger:b7,rightx:a2,righty:a3,start:b12,x:b2,y:b3,platform:Linux, 03000000381000003014000075010000,SteelSeries Stratus Duo,a:b0,b:b1,back:b6,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b8,leftshoulder:b4,leftstick:b9,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b10,righttrigger:a5,rightx:a3,righty:a4,start:b7,x:b2,y:b3,platform:Linux, @@ -1417,6 +1437,10 @@ xinput,XInput Controller,a:b0,b:b1,back:b6,dpdown:h0.4,dpleft:h0.8,dpright:h0.2, 03000000d814000007cd000011010000,Toodles 2008 Chimp PC PS3,a:b0,b:b1,back:b8,leftshoulder:b4,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,righttrigger:b7,start:b9,x:b3,y:b2,platform:Linux, 030000005e0400008e02000070050000,Torid,a:b0,b:b1,back:b6,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b8,leftshoulder:b4,leftstick:b9,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b10,righttrigger:a5,rightx:a3,righty:a4,start:b7,x:b2,y:b3,platform:Linux, 03000000c01100000591000011010000,Torid,a:b2,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b3,y:b0,platform:Linux, +03000000680a00000300000003000000,TRBot Virtual Joypad,a:b11,b:b12,back:b15,dpdown:b6,dpleft:b3,dpright:b4,dpup:b5,leftshoulder:b17,leftstick:b21,lefttrigger:a5,leftx:a0,lefty:a1,rightshoulder:b18,rightstick:b22,righttrigger:a2,rightx:a3,righty:a4,start:b16,x:b13,y:b14,platform:Linux, +03000000780300000300000003000000,TRBot Virtual Joypad,a:b11,b:b12,back:b15,dpdown:b6,dpleft:b3,dpright:b4,dpup:b5,leftshoulder:b17,leftstick:b21,lefttrigger:a5,leftx:a0,lefty:a1,rightshoulder:b18,rightstick:b22,righttrigger:a2,rightx:a3,righty:a4,start:b16,x:b13,y:b14,platform:Linux, +03000000e00d00000300000003000000,TRBot Virtual Joypad,a:b11,b:b12,back:b15,dpdown:b6,dpleft:b3,dpright:b4,dpup:b5,leftshoulder:b17,leftstick:b21,lefttrigger:a5,leftx:a0,lefty:a1,rightshoulder:b18,rightstick:b22,righttrigger:a2,rightx:a3,righty:a4,start:b16,x:b13,y:b14,platform:Linux, +03000000f00600000300000003000000,TRBot Virtual Joypad,a:b11,b:b12,back:b15,dpdown:b6,dpleft:b3,dpright:b4,dpup:b5,leftshoulder:b17,leftstick:b21,lefttrigger:a5,leftx:a0,lefty:a1,rightshoulder:b18,rightstick:b22,righttrigger:a2,rightx:a3,righty:a4,start:b16,x:b13,y:b14,platform:Linux, 030000005f140000c501000010010000,Trust Gamepad,a:b2,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b3,y:b0,platform:Linux, 03000000100800000100000010010000,Twin PS2 Adapter,a:b2,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b6,leftstick:b10,lefttrigger:b4,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b11,righttrigger:b5,rightx:a3,righty:a2,start:b9,x:b3,y:b0,platform:Linux, 03000000100800000300000010010000,USB Gamepad,a:b2,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b6,leftstick:b10,lefttrigger:b4,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b11,righttrigger:b5,rightx:a3,righty:a2,start:b9,x:b3,y:b0,platform:Linux, @@ -1448,8 +1472,10 @@ xinput,XInput Controller,a:b0,b:b1,back:b6,dpdown:h0.4,dpleft:h0.8,dpright:h0.2, 050000005e040000fd02000030110000,Xbox One Controller,a:b0,b:b1,back:b6,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b8,leftshoulder:b4,leftstick:b9,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b10,righttrigger:a5,rightx:a3,righty:a4,start:b7,x:b2,y:b3,platform:Linux, 060000005e040000120b000007050000,Xbox One Controller,a:b0,b:b1,back:b6,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b8,leftshoulder:b4,leftstick:b9,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b10,righttrigger:a5,rightx:a3,righty:a4,start:b7,x:b2,y:b3,platform:Linux, 050000005e040000e302000002090000,Xbox One Elite,a:b0,b:b1,back:b136,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b6,leftstick:b13,lefttrigger:a6,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b14,righttrigger:a5,rightx:a2,righty:a3,start:b11,x:b3,y:b4,platform:Linux, +050000005e040000220b000013050000,Xbox One Elite 2 Controller,a:b0,b:b1,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b6,leftstick:b13,lefttrigger:a5,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b14,righttrigger:a4,rightx:a2,righty:a3,start:b11,x:b3,y:b4,platform:Linux, 050000005e040000050b000002090000,Xbox One Elite Series 2,a:b0,b:b1,back:b136,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b6,leftstick:b13,lefttrigger:a6,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b14,righttrigger:a5,rightx:a2,righty:a3,start:b11,x:b3,y:b4,platform:Linux, 060000005e040000ea0200000b050000,Xbox One S Controller,a:b0,b:b1,back:b6,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b8,leftshoulder:b4,leftstick:b9,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b10,righttrigger:a5,rightx:a3,righty:a4,start:b7,x:b2,y:b3,platform:Linux, +060000005e040000ea0200000d050000,Xbox One S Controller,a:b0,b:b1,back:b6,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b8,leftshoulder:b4,leftstick:b9,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b10,righttrigger:a5,rightx:a3,righty:a4,start:b7,x:b2,y:b3,platform:Linux, 030000005e040000120b000001050000,Xbox Series Controller,a:b0,b:b1,back:b6,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b8,leftshoulder:b4,leftstick:b9,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b10,righttrigger:a5,rightx:a3,righty:a4,start:b7,x:b2,y:b3,platform:Linux, 030000005e040000120b000005050000,Xbox Series Controller,a:b0,b:b1,back:b6,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b8,leftshoulder:b4,leftstick:b9,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b10,righttrigger:a5,rightx:a3,righty:a4,start:b7,x:b2,y:b3,platform:Linux, 030000005e040000120b00000d050000,Xbox Series Controller,a:b0,b:b1,back:b6,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b8,leftshoulder:b4,leftstick:b9,lefttrigger:a2,leftx:a0,lefty:a1,misc1:b11,rightshoulder:b5,rightstick:b10,righttrigger:a5,rightx:a3,righty:a4,start:b7,x:b2,y:b3,platform:Linux, @@ -1459,9 +1485,10 @@ xinput,XInput Controller,a:b0,b:b1,back:b6,dpdown:h0.4,dpleft:h0.8,dpright:h0.2, 050000005e040000130b000009050000,Xbox Series Controller,a:b0,b:b1,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b6,leftstick:b13,lefttrigger:a5,leftx:a0,lefty:a1,misc1:b15,rightshoulder:b7,rightstick:b14,righttrigger:a4,rightx:a2,righty:a3,start:b11,x:b3,y:b4,platform:Linux, 050000005e040000130b000013050000,Xbox Series Controller,a:b0,b:b1,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b6,leftstick:b13,lefttrigger:a5,leftx:a0,lefty:a1,misc1:b15,rightshoulder:b7,rightstick:b14,righttrigger:a4,rightx:a2,righty:a3,start:b11,x:b3,y:b4,platform:Linux, 060000005e040000120b00000b050000,Xbox Series Controller,a:b0,b:b1,back:b6,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b8,leftshoulder:b4,leftstick:b9,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b10,righttrigger:a5,rightx:a3,righty:a4,start:b7,x:b2,y:b3,platform:Linux, +030000005e040000120b000007050000,Xbox Series X Controller,a:b0,b:b1,back:b6,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b8,leftshoulder:b4,leftstick:b9,lefttrigger:a2,leftx:a0,lefty:a1,misc1:b11,rightshoulder:b5,rightstick:b10,righttrigger:a5,rightx:a3,righty:a4,start:b7,x:b2,y:b3,platform:Linux, 050000005e040000130b000011050000,Xbox Series X Controller,a:b0,b:b1,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b6,leftstick:b13,lefttrigger:a5,leftx:a0,lefty:a1,misc1:b15,rightshoulder:b7,rightstick:b14,righttrigger:a4,rightx:a2,righty:a3,start:b11,x:b3,y:b4,platform:Linux, -030000005e040000120b000007050000,Xbox Series X Controller,a:b0,b:b1,x:b2,y:b3,back:b6,guide:b8,start:b7,leftstick:b9,rightstick:b10,leftshoulder:b4,rightshoulder:b5,dpup:h0.1,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,misc1:b11,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:a2,righttrigger:a5,platform:Linux, 050000005e040000130b000007050000,Xbox Wireless Controller,a:b0,b:b1,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b6,leftstick:b13,lefttrigger:a5,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b14,righttrigger:a4,rightx:a2,righty:a3,start:b11,x:b3,y:b4,platform:Linux, +050000005e040000200b000013050000,Xbox Wireless Controller,a:b0,b:b1,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b12,leftshoulder:b6,leftstick:b13,lefttrigger:a5,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b14,righttrigger:a4,rightx:a2,righty:a3,start:b11,x:b3,y:b4,platform:Linux, 03000000450c00002043000010010000,XEOX SL6556 BK,a:b0,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b2,y:b3,platform:Linux, 05000000172700004431000029010000,XiaoMi Controller,a:b0,b:b1,back:b10,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b20,leftshoulder:b6,leftstick:b13,lefttrigger:a7,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b14,righttrigger:a6,rightx:a2,righty:a5,start:b11,x:b3,y:b4,platform:Linux, 03000000c0160000e105000001010000,XinMo Dual Arcade,a:b4,b:b3,back:b6,dpdown:b12,dpleft:b13,dpright:b14,dpup:b11,guide:b9,leftshoulder:b2,leftx:a0,lefty:a1,rightshoulder:b5,start:b7,x:b1,y:b0,platform:Linux, @@ -1479,8 +1506,12 @@ xinput,XInput Controller,a:b0,b:b1,back:b6,dpdown:h0.4,dpleft:h0.8,dpright:h0.2, 33313433353539306634656436353432,8BitDo Dogbone,a:b1,b:b0,back:b15,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftx:a0,lefty:a1,rightx:a2,righty:a3,start:b6,x:b2,y:b3,platform:Android, 38426974446f20446f67626f6e65204d,8BitDo Dogbone,a:b1,b:b0,back:b15,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftx:a0,lefty:a1,rightx:a2,righty:a3,start:b6,platform:Android, 34343439373236623466343934376233,8BitDo FC30 Pro,a:b0,b:b1,back:b4,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b3,leftstick:b28,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b4,rightstick:b29,righttrigger:b7,start:b5,x:b30,y:b2,platform:Android, +38426974446f2038426974446f204c69,8BitDo Lite,a:b1,b:b0,back:b15,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b5,leftshoulder:b9,leftstick:b7,lefttrigger:b17,leftx:a0,lefty:a1,rightshoulder:b10,rightstick:b8,righttrigger:b18,rightx:a2,righty:a3,start:b6,x:b3,y:b2,platform:Android, +30643332373663313263316637356631,8BitDo Lite 2,a:b1,b:b0,back:b15,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b5,leftshoulder:b9,leftstick:b7,lefttrigger:b17,leftx:a0,lefty:a1,rightshoulder:b10,rightstick:b8,righttrigger:b18,rightx:a2,righty:a3,start:b6,x:b3,y:b2,platform:Android, +38426974446f204c6974652032000000,8BitDo Lite 2,a:b1,b:b0,back:b15,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b5,leftshoulder:b9,leftstick:b7,lefttrigger:b17,leftx:a0,lefty:a1,rightshoulder:b10,rightstick:b8,righttrigger:b18,rightx:a2,righty:a3,start:b6,x:b3,y:b2,platform:Android, +62656331626461363634633735353032,8BitDo Lite 2,a:b1,b:b0,back:b15,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b5,leftshoulder:b9,leftstick:b7,lefttrigger:b17,leftx:a0,lefty:a1,rightshoulder:b10,rightstick:b8,righttrigger:b18,rightx:a2,righty:a3,start:b6,x:b3,y:b2,platform:Android, 38393936616436383062666232653338,8BitDo Lite SE,a:b1,b:b0,back:b15,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b9,leftstick:b7,lefttrigger:b17,leftx:a0,lefty:a1,rightshoulder:b10,rightstick:b8,righttrigger:b18,rightx:a2,righty:a3,start:b6,x:b3,y:b2,platform:Android, -38426974446f2038426974446f204c69,8BitDo Lite SE,a:b1,b:b0,back:b15,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b5,leftshoulder:b9,leftstick:b7,lefttrigger:b17,leftx:a0,lefty:a1,rightshoulder:b10,rightstick:b8,righttrigger:b18,rightx:a2,righty:a3,start:b6,x:b3,y:b2,platform:Android, +38426974446f204c6974652053450000,8BitDo Lite SE,a:b1,b:b0,back:b15,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,guide:b5,leftshoulder:b9,leftstick:b7,lefttrigger:b17,leftx:a0,lefty:a1,rightshoulder:b10,rightstick:b8,righttrigger:b18,rightx:a2,righty:a3,start:b6,x:b3,y:b2,platform:Android, 39356430616562366466646636643435,8BitDo Lite SE,a:b1,b:b0,back:b15,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b9,leftstick:b7,lefttrigger:b17,leftx:a0,lefty:a1,rightshoulder:b10,rightstick:b8,righttrigger:b18,rightx:a2,righty:a3,start:b6,x:b3,y:b2,platform:Android, 05000000c82d000006500000ffff3f00,8BitDo M30,a:b1,b:b0,back:b4,dpdown:+a1,dpleft:-a0,dpright:+a0,dpup:-a1,guide:b17,leftshoulder:b9,lefttrigger:a5,rightshoulder:b10,righttrigger:a4,start:b6,x:b3,y:b2,platform:Android, 05000000c82d000051060000ffff3f00,8BitDo M30,a:b1,b:b0,back:b4,dpdown:+a1,dpleft:-a0,dpright:+a0,dpup:-a1,guide:b17,leftshoulder:b9,lefttrigger:a4,rightshoulder:b10,righttrigger:a5,start:b6,x:b3,y:b2,platform:Android, diff --git a/core/input/godotcontrollerdb.txt b/core/input/godotcontrollerdb.txt index 7f3570729a..e11099f380 100644 --- a/core/input/godotcontrollerdb.txt +++ b/core/input/godotcontrollerdb.txt @@ -7,31 +7,32 @@ __XINPUT_DEVICE__,XInput Gamepad,a:b12,b:b13,x:b14,y:b15,start:b4,back:b5,leftst # Android Default Android Gamepad,Default Controller,leftx:a0,lefty:a1,dpdown:h0.4,rightstick:b8,rightshoulder:b10,rightx:a2,start:b6,righty:a3,dpleft:h0.8,lefttrigger:a4,x:b2,dpup:h0.1,back:b4,leftstick:b7,leftshoulder:b9,y:b3,a:b0,dpright:h0.2,righttrigger:a5,b:b1,platform:Android, -# Javascript -standard,Standard Gamepad Mapping,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a6,righttrigger:a7,a:b0,b:b1,x:b2,y:b3,leftshoulder:b4,rightshoulder:b5,back:b8,start:b9,leftstick:b10,rightstick:b11,dpup:b12,dpdown:b13,dpleft:b14,dpright:b15,guide:b16,leftstick:b10,rightstick:b11,platform:Javascript, -Linux24c6581a,PowerA Xbox One Cabled,a:b0,b:b1,y:b3,x:b2,start:b7,guide:b8,back:b6,leftstick:b9,rightstick:b10,leftshoulder:b4,rightshoulder:b5,dpup:-a7,dpleft:-a6,dpdown:+a7,dpright:+a6,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:a2,righttrigger:a5,platform:Javascript, -Linux0e6f0301,Logic 3 Controller (xbox compatible),a:b0,b:b1,y:b3,x:b2,start:b7,guide:b8,back:b6,leftstick:b9,rightstick:b10,leftshoulder:b4,rightshoulder:b5,dpup:-a7,dpleft:-a6,dpdown:+a7,dpright:+a6,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:a2,righttrigger:a5,platform:Javascript, -Linux045e028e,Microsoft X-Box 360 pad,a:b0,b:b1,y:b3,x:b2,start:b7,guide:b8,back:b6,leftstick:b9,rightstick:b10,leftshoulder:b4,rightshoulder:b5,dpup:-a7,dpleft:-a6,dpdown:+a7,dpright:+a6,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:a2,righttrigger:a5,platform:Javascript, -Linux045e02d1,Microsoft X-Box One pad,a:b0,b:b1,y:b3,x:b2,start:b7,guide:b8,back:b6,leftstick:b9,rightstick:b10,leftshoulder:b4,rightshoulder:b5,dpup:-a7,dpleft:-a6,dpdown:+a7,dpright:+a6,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:a2,righttrigger:a5,platform:Javascript, -Linux045e02ea,Microsoft X-Box One S pad,a:b0,b:b1,y:b3,x:b2,start:b7,guide:b8,back:b6,leftstick:b9,rightstick:b10,leftshoulder:b4,rightshoulder:b5,dpup:-a7,dpleft:-a6,dpdown:+a7,dpright:+a6,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:a2,righttrigger:a5,platform:Javascript, -Linux045e0b12,Microsoft X-Box Series X pad,a:b0,b:b1,y:b3,x:b2,start:b7,guide:b8,back:b6,leftstick:b9,rightstick:b10,leftshoulder:b4,rightshoulder:b5,dpup:-a7,dpleft:-a6,dpdown:+a7,dpright:+a6,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:a2,righttrigger:a5,platform:Javascript, -Linux044fb315,Thrustmaster dual analog 3.2,a:b0,b:b2,y:b3,x:b1,start:b9,back:b8,leftstick:b10,rightstick:b11,leftshoulder:b4,rightshoulder:b6,dpup:-a5,dpleft:-a4,dpdown:+a5,dpright:+a4,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:b5,righttrigger:b7,platform:Javascript, -Linux0e8f0003,PS3 Controller,a:b2,b:b1,back:b8,dpdown:+a5,dpleft:-a4,dpright:+a4,dpup:-a5,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b3,y:b0,platform:Javascript, -MacOSX24c6581a,PowerA Xbox One Cabled,a:b11,b:b12,y:b14,x:b13,start:b4,back:b5,leftstick:b6,rightstick:b7,leftshoulder:b8,rightshoulder:b9,dpup:b0,dpleft:b2,dpdown:b1,dpright:b3,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,platform:Javascript -MacOSX045e028e,Xbox 360 Wired Controller,a:b11,b:b12,y:b14,x:b13,start:b4,back:b5,leftstick:b6,rightstick:b7,leftshoulder:b8,rightshoulder:b9,dpup:b0,dpleft:b2,dpdown:b1,dpright:b3,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,platform:Javascript -MacOSX045e02d1,Xbox One Controller,a:b11,b:b12,y:b14,x:b13,start:b4,back:b5,leftstick:b6,rightstick:b7,leftshoulder:b8,rightshoulder:b9,dpup:b0,dpleft:b2,dpdown:b1,dpright:b3,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,platform:Javascript -MacOSX045e02ea,Xbox One S Controller,a:b11,b:b12,y:b14,x:b13,start:b4,back:b5,leftstick:b6,rightstick:b7,leftshoulder:b8,rightshoulder:b9,dpup:b0,dpleft:b2,dpdown:b1,dpright:b3,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,platform:Javascript -MacOSX045e0b12,Xbox Series X Controller,a:b11,b:b12,y:b14,x:b13,start:b4,back:b5,leftstick:b6,rightstick:b7,leftshoulder:b8,rightshoulder:b9,dpup:b0,dpleft:b2,dpdown:b1,dpright:b3,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,platform:Javascript -Linux15320a14,Razer Wolverine Ultimate,a:b0,b:b1,y:b3,x:b2,start:b7,back:b6,leftstick:b9,rightstick:b10,leftshoulder:b4,rightshoulder:b5,dpup:-a7,dpleft:-a6,dpdown:+a7,dpright:+a6,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:a2,righttrigger:a5,platform:Javascript -Linux05832060,iBuffalo BSGP801,a:b1,b:b0,y:b2,x:b3,start:b7,back:b6,leftshoulder:b4,rightshoulder:b5,dpup:-a1,dpleft:-a0,dpdown:+a1,dpright:+a0,platform:Javascript -MacOSX05832060,iBuffalo BSGP801,a:b1,b:b0,y:b2,x:b3,start:b7,back:b6,leftshoulder:b4,rightshoulder:b5,dpup:-a1,dpleft:-a0,dpdown:+a1,dpright:+a0,platform:Javascript -Linux0e8f3013,HuiJia USB GamePad,a:b2,b:b1,y:b0,x:b3,start:b9,back:b8,leftshoulder:b6,rightshoulder:b7,dpup:-a1,dpleft:-a0,dpdown:+a1,dpright:+a0,platform:Javascript -Windows0e8f3013,HuiJia USB GamePad,a:b2,b:b1,y:b0,x:b3,start:b9,back:b8,leftshoulder:b6,rightshoulder:b7,dpup:-a1,dpleft:-a0,dpdown:+a1,dpright:+a0,platform:Javascript -MacOSX0e8f3013,HuiJia USB GamePad,a:b2,b:b1,y:b0,x:b3,start:b9,back:b8,leftshoulder:b6,rightshoulder:b7,dpup:-a4,dpleft:-a3,dpdown:+a4,dpright:+a3,platform:Javascript -Linux046dc216,046d-c216-Logitech Logitech Dual Action,a:b1,b:b2,y:b3,x:b0,start:b9,back:b8,leftstick:b10,rightstick:b11,leftshoulder:b4,rightshoulder:b5,dpup:-a5,dpleft:-a4,dpdown:+a5,dpright:+a4,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:b6,righttrigger:b7,platform:Javascript -Linux20d6a713,Bensussen Deutsch & Associates Inc.(BDA) NSW Wired controller,a:b1,b:b2,y:b3,x:b0,start:b9,back:b8,leftstick:b10,rightstick:b11,leftshoulder:b4,rightshoulder:b5,dpup:-a5,dpleft:-a4,dpdown:+a5,dpright:+a4,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:b6,righttrigger:b7,platform:Javascript -Linux054c05c4,Sony Computer Entertainment Wireless Controller,a:b0,b:b1,y:b2,x:b3,start:b9,back:b8,leftstick:b11,rightstick:b12,leftshoulder:b4,rightshoulder:b5,dpup:-a7,dpleft:-a6,dpdown:+a7,dpright:+a6,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:a2,righttrigger:a5,platform:Javascript -Linux18d19400,18d1-9400-Google LLC Stadia Controller rev. A,a:b0,b:b1,y:b3,x:b2,start:b7,back:b6,leftstick:b9,rightstick:b10,leftshoulder:b4,rightshoulder:b5,dpup:-a7,dpleft:-a6,dpdown:+a7,dpright:+a6,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a5,righttrigger:a4,platform:Javascript +# Web +standard,Standard Gamepad Mapping,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a6,righttrigger:a7,a:b0,b:b1,x:b2,y:b3,leftshoulder:b4,rightshoulder:b5,back:b8,start:b9,leftstick:b10,rightstick:b11,dpup:b12,dpdown:b13,dpleft:b14,dpright:b15,guide:b16,leftstick:b10,rightstick:b11,platform:Web, +Linux24c6581a,PowerA Xbox One Cabled,a:b0,b:b1,y:b3,x:b2,start:b7,guide:b8,back:b6,leftstick:b9,rightstick:b10,leftshoulder:b4,rightshoulder:b5,dpup:-a7,dpleft:-a6,dpdown:+a7,dpright:+a6,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:a2,righttrigger:a5,platform:Web, +Linux0e6f0301,Logic 3 Controller (xbox compatible),a:b0,b:b1,y:b3,x:b2,start:b7,guide:b8,back:b6,leftstick:b9,rightstick:b10,leftshoulder:b4,rightshoulder:b5,dpup:-a7,dpleft:-a6,dpdown:+a7,dpright:+a6,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:a2,righttrigger:a5,platform:Web, +Linux045e028e,Microsoft X-Box 360 pad,a:b0,b:b1,y:b3,x:b2,start:b7,guide:b8,back:b6,leftstick:b9,rightstick:b10,leftshoulder:b4,rightshoulder:b5,dpup:-a7,dpleft:-a6,dpdown:+a7,dpright:+a6,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:a2,righttrigger:a5,platform:Web, +Linux045e02d1,Microsoft X-Box One pad,a:b0,b:b1,y:b3,x:b2,start:b7,guide:b8,back:b6,leftstick:b9,rightstick:b10,leftshoulder:b4,rightshoulder:b5,dpup:-a7,dpleft:-a6,dpdown:+a7,dpright:+a6,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:a2,righttrigger:a5,platform:Web, +Linux045e02ea,Microsoft X-Box One S pad,a:b0,b:b1,y:b3,x:b2,start:b7,guide:b8,back:b6,leftstick:b9,rightstick:b10,leftshoulder:b4,rightshoulder:b5,dpup:-a7,dpleft:-a6,dpdown:+a7,dpright:+a6,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:a2,righttrigger:a5,platform:Web, +Linux045e0b12,Microsoft X-Box Series X pad,a:b0,b:b1,y:b3,x:b2,start:b7,guide:b8,back:b6,leftstick:b9,rightstick:b10,leftshoulder:b4,rightshoulder:b5,dpup:-a7,dpleft:-a6,dpdown:+a7,dpright:+a6,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:a2,righttrigger:a5,platform:Web, +Linux044fb315,Thrustmaster dual analog 3.2,a:b0,b:b2,y:b3,x:b1,start:b9,back:b8,leftstick:b10,rightstick:b11,leftshoulder:b4,rightshoulder:b6,dpup:-a5,dpleft:-a4,dpdown:+a5,dpright:+a4,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:b5,righttrigger:b7,platform:Web, +Linux0e8f0003,PS3 Controller,a:b2,b:b1,back:b8,dpdown:+a5,dpleft:-a4,dpright:+a4,dpup:-a5,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b3,y:b0,platform:Web, +MacOSX24c6581a,PowerA Xbox One Cabled,a:b11,b:b12,y:b14,x:b13,start:b4,back:b5,leftstick:b6,rightstick:b7,leftshoulder:b8,rightshoulder:b9,dpup:b0,dpleft:b2,dpdown:b1,dpright:b3,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,platform:Web +MacOSX045e028e,Xbox 360 Wired Controller,a:b11,b:b12,y:b14,x:b13,start:b4,back:b5,leftstick:b6,rightstick:b7,leftshoulder:b8,rightshoulder:b9,dpup:b0,dpleft:b2,dpdown:b1,dpright:b3,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,platform:Web +MacOSX045e02d1,Xbox One Controller,a:b11,b:b12,y:b14,x:b13,start:b4,back:b5,leftstick:b6,rightstick:b7,leftshoulder:b8,rightshoulder:b9,dpup:b0,dpleft:b2,dpdown:b1,dpright:b3,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,platform:Web +MacOSX045e02ea,Xbox One S Controller,a:b11,b:b12,y:b14,x:b13,start:b4,back:b5,leftstick:b6,rightstick:b7,leftshoulder:b8,rightshoulder:b9,dpup:b0,dpleft:b2,dpdown:b1,dpright:b3,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,platform:Web +MacOSX045e0b12,Xbox Series X Controller,a:b11,b:b12,y:b14,x:b13,start:b4,back:b5,leftstick:b6,rightstick:b7,leftshoulder:b8,rightshoulder:b9,dpup:b0,dpleft:b2,dpdown:b1,dpright:b3,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,platform:Web +Linux15320a14,Razer Wolverine Ultimate,a:b0,b:b1,y:b3,x:b2,start:b7,back:b6,leftstick:b9,rightstick:b10,leftshoulder:b4,rightshoulder:b5,dpup:-a7,dpleft:-a6,dpdown:+a7,dpright:+a6,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:a2,righttrigger:a5,platform:Web +Linux05832060,iBuffalo BSGP801,a:b1,b:b0,y:b2,x:b3,start:b7,back:b6,leftshoulder:b4,rightshoulder:b5,dpup:-a1,dpleft:-a0,dpdown:+a1,dpright:+a0,platform:Web +MacOSX05832060,iBuffalo BSGP801,a:b1,b:b0,y:b2,x:b3,start:b7,back:b6,leftshoulder:b4,rightshoulder:b5,dpup:-a1,dpleft:-a0,dpdown:+a1,dpright:+a0,platform:Web +Linux0e8f3013,HuiJia USB GamePad,a:b2,b:b1,y:b0,x:b3,start:b9,back:b8,leftshoulder:b6,rightshoulder:b7,dpup:-a1,dpleft:-a0,dpdown:+a1,dpright:+a0,platform:Web +Windows0e8f3013,HuiJia USB GamePad,a:b2,b:b1,y:b0,x:b3,start:b9,back:b8,leftshoulder:b6,rightshoulder:b7,dpup:-a1,dpleft:-a0,dpdown:+a1,dpright:+a0,platform:Web +MacOSX0e8f3013,HuiJia USB GamePad,a:b2,b:b1,y:b0,x:b3,start:b9,back:b8,leftshoulder:b6,rightshoulder:b7,dpup:-a4,dpleft:-a3,dpdown:+a4,dpright:+a3,platform:Web +Linux046dc216,046d-c216-Logitech Logitech Dual Action,a:b1,b:b2,y:b3,x:b0,start:b9,back:b8,leftstick:b10,rightstick:b11,leftshoulder:b4,rightshoulder:b5,dpup:-a5,dpleft:-a4,dpdown:+a5,dpright:+a4,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:b6,righttrigger:b7,platform:Web +Linux20d6a713,Bensussen Deutsch & Associates Inc.(BDA) NSW Wired controller,a:b1,b:b2,y:b3,x:b0,start:b9,back:b8,leftstick:b10,rightstick:b11,leftshoulder:b4,rightshoulder:b5,dpup:-a5,dpleft:-a4,dpdown:+a5,dpright:+a4,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:b6,righttrigger:b7,platform:Web +Linux054c05c4,Sony Computer Entertainment Wireless Controller,a:b0,b:b1,y:b2,x:b3,start:b9,back:b8,leftstick:b11,rightstick:b12,leftshoulder:b4,rightshoulder:b5,dpup:-a7,dpleft:-a6,dpdown:+a7,dpright:+a6,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:a2,righttrigger:a5,platform:Web +Linux18d19400,18d1-9400-Google LLC Stadia Controller rev. A,a:b0,b:b1,y:b3,x:b2,start:b7,back:b6,leftstick:b9,rightstick:b10,leftshoulder:b4,rightshoulder:b5,dpup:-a7,dpleft:-a6,dpdown:+a7,dpright:+a6,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a5,righttrigger:a4,platform:Web +Linux054c0268,054c-0268-Sony PLAYSTATION(R)3 Controller,a:b0,b:b1,y:b2,x:b3,start:b9,back:b8,leftstick:b11,rightstick:b12,leftshoulder:b4,rightshoulder:b5,dpup:b13,dpleft:b15,dpdown:b14,dpright:b16,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:a2,righttrigger:a5,platform:Web # UWP __UWP_GAMEPAD__,Xbox Controller,a:b2,b:b3,x:b4,y:b5,start:b0,back:b1,leftstick:b12,rightstick:b13,leftshoulder:b10,rightshoulder:b11,dpup:b6,dpdown:b7,dpleft:b8,dpright:b9,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,platform:UWP, diff --git a/core/input/input.cpp b/core/input/input.cpp index e08647f5ea..cb65d2569c 100644 --- a/core/input/input.cpp +++ b/core/input/input.cpp @@ -169,11 +169,10 @@ void Input::_bind_methods() { void Input::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const { String pf = p_function; - if (p_idx == 0 && - (pf == "is_action_pressed" || pf == "action_press" || pf == "action_release" || - pf == "is_action_just_pressed" || pf == "is_action_just_released" || - pf == "get_action_strength" || pf == "get_action_raw_strength" || - pf == "get_axis" || pf == "get_vector")) { + + if ((p_idx == 0 && (pf == "is_action_pressed" || pf == "action_press" || pf == "action_release" || pf == "is_action_just_pressed" || pf == "is_action_just_released" || pf == "get_action_strength" || pf == "get_action_raw_strength")) || + (p_idx < 2 && pf == "get_axis") || + (p_idx < 4 && pf == "get_vector")) { List<PropertyInfo> pinfo; ProjectSettings::get_singleton()->get_property_list(&pinfo); @@ -366,7 +365,6 @@ Vector2 Input::get_vector(const StringName &p_negative_x, const StringName &p_po // Inverse lerp length to map (p_deadzone, 1) to (0, 1). return vector * (Math::inverse_lerp(p_deadzone, 1.0f, length) / length); } - return vector; } float Input::get_joy_axis(int p_device, JoyAxis p_axis) const { @@ -521,6 +519,7 @@ void Input::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_em touch_event.instantiate(); touch_event->set_pressed(mb->is_pressed()); touch_event->set_position(mb->get_position()); + touch_event->set_double_tap(mb->is_double_click()); event_dispatch_function(touch_event); } } @@ -582,6 +581,7 @@ void Input::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_em button_event->set_global_position(st->get_position()); button_event->set_pressed(st->is_pressed()); button_event->set_button_index(MouseButton::LEFT); + button_event->set_double_click(st->is_double_tap()); if (st->is_pressed()) { button_event->set_button_mask(MouseButton(mouse_button_mask | MouseButton::MASK_LEFT)); } else { @@ -767,9 +767,6 @@ Point2i Input::warp_mouse_motion(const Ref<InputEventMouseMotion> &p_motion, con return rel_warped; } -void Input::iteration(float p_step) { -} - void Input::action_press(const StringName &p_action, float p_strength) { Action action; @@ -974,11 +971,9 @@ void Input::joy_axis(int p_device, JoyAxis p_axis, float p_value) { if (map.type == TYPE_BUTTON) { bool pressed = map.value > 0.5; - if (pressed == joy_buttons_pressed.has(_combine_device((JoyButton)map.index, p_device))) { - // Button already pressed or released; so ignore. - return; + if (pressed != joy_buttons_pressed.has(_combine_device((JoyButton)map.index, p_device))) { + _button_event(p_device, (JoyButton)map.index, pressed); } - _button_event(p_device, (JoyButton)map.index, pressed); // Ensure opposite D-Pad button is also released. switch ((JoyButton)map.index) { @@ -1129,7 +1124,7 @@ Input::JoyEvent Input::_get_mapped_axis_event(const JoyDeviceMapping &mapping, J value = -value; } if (binding.input.axis.range == FULL_AXIS || - (binding.input.axis.range == POSITIVE_HALF_AXIS && value > 0) || + (binding.input.axis.range == POSITIVE_HALF_AXIS && value >= 0) || (binding.input.axis.range == NEGATIVE_HALF_AXIS && value < 0)) { event.type = binding.outputType; float shifted_positive_value = 0; @@ -1315,7 +1310,7 @@ void Input::parse_mapping(String p_mapping) { JoyButton output_button = _get_output_button(output); JoyAxis output_axis = _get_output_axis(output); ERR_CONTINUE_MSG(output_button == JoyButton::INVALID && output_axis == JoyAxis::INVALID, - vformat("Unrecognised output string \"%s\" in mapping:\n%s", output, p_mapping)); + vformat("Unrecognized output string \"%s\" in mapping:\n%s", output, p_mapping)); ERR_CONTINUE_MSG(output_button != JoyButton::INVALID && output_axis != JoyAxis::INVALID, vformat("Output string \"%s\" matched both button and axis in mapping:\n%s", output, p_mapping)); diff --git a/core/input/input.h b/core/input/input.h index 3ad8c91ddf..a07174b887 100644 --- a/core/input/input.h +++ b/core/input/input.h @@ -114,6 +114,15 @@ private: int mouse_from_touch_index = -1; + struct VibrationInfo { + float weak_magnitude; + float strong_magnitude; + float duration; // Duration in seconds + uint64_t timestamp; + }; + + HashMap<int, VibrationInfo> joy_vibration; + struct VelocityTrack { uint64_t last_tick = 0; Vector2 velocity; @@ -226,15 +235,6 @@ private: EventDispatchFunc event_dispatch_function = nullptr; protected: - struct VibrationInfo { - float weak_magnitude; - float strong_magnitude; - float duration; // Duration in seconds - uint64_t timestamp; - }; - - HashMap<int, VibrationInfo> joy_vibration; - static void _bind_methods(); public: @@ -295,8 +295,6 @@ public: void action_press(const StringName &p_action, float p_strength = 1.f); void action_release(const StringName &p_action); - void iteration(float p_step); - void set_emulate_touch_from_mouse(bool p_emulate); bool is_emulating_touch_from_mouse() const; void ensure_touch_mouse_raised(); diff --git a/core/input/input_builders.py b/core/input/input_builders.py index c0dac26f02..a7729c9af2 100644 --- a/core/input/input_builders.py +++ b/core/input/input_builders.py @@ -16,7 +16,7 @@ def make_default_controller_mappings(target, source, env): g.write('#include "core/input/default_controller_mappings.h"\n') # ensure mappings have a consistent order - platform_mappings = OrderedDict() + platform_mappings: dict = OrderedDict() for src_path in source: with open(src_path, "r") as f: # read mapping file and skip header @@ -50,7 +50,7 @@ def make_default_controller_mappings(target, source, env): "Mac OS X": "#ifdef MACOS_ENABLED", "Android": "#if defined(__ANDROID__)", "iOS": "#ifdef IOS_ENABLED", - "Javascript": "#ifdef JAVASCRIPT_ENABLED", + "Web": "#ifdef WEB_ENABLED", "UWP": "#ifdef UWP_ENABLED", } diff --git a/core/input/input_event.cpp b/core/input/input_event.cpp index 8ad2193fca..0a32b4bf68 100644 --- a/core/input/input_event.cpp +++ b/core/input/input_event.cpp @@ -142,13 +142,33 @@ int64_t InputEventFromWindow::get_window_id() const { /////////////////////////////////// -void InputEventWithModifiers::set_store_command(bool p_enabled) { - store_command = p_enabled; +void InputEventWithModifiers::set_command_or_control_autoremap(bool p_enabled) { + command_or_control_autoremap = p_enabled; + if (command_or_control_autoremap) { +#ifdef MACOS_ENABLED + ctrl_pressed = false; + meta_pressed = true; +#else + ctrl_pressed = true; + meta_pressed = false; +#endif + } else { + ctrl_pressed = false; + meta_pressed = false; + } emit_changed(); } -bool InputEventWithModifiers::is_storing_command() const { - return store_command; +bool InputEventWithModifiers::is_command_or_control_autoremap() const { + return command_or_control_autoremap; +} + +bool InputEventWithModifiers::is_command_or_control_pressed() const { +#ifdef MACOS_ENABLED + return meta_pressed; +#else + return ctrl_pressed; +#endif } void InputEventWithModifiers::set_shift_pressed(bool p_enabled) { @@ -170,6 +190,7 @@ bool InputEventWithModifiers::is_alt_pressed() const { } void InputEventWithModifiers::set_ctrl_pressed(bool p_enabled) { + ERR_FAIL_COND_MSG(command_or_control_autoremap, "Command/Control autoremaping is enabled, cannot set Control directly!"); ctrl_pressed = p_enabled; emit_changed(); } @@ -179,6 +200,7 @@ bool InputEventWithModifiers::is_ctrl_pressed() const { } void InputEventWithModifiers::set_meta_pressed(bool p_enabled) { + ERR_FAIL_COND_MSG(command_or_control_autoremap, "Command/Control autoremaping is enabled, cannot set Meta directly!"); meta_pressed = p_enabled; emit_changed(); } @@ -187,15 +209,6 @@ bool InputEventWithModifiers::is_meta_pressed() const { return meta_pressed; } -void InputEventWithModifiers::set_command_pressed(bool p_enabled) { - command_pressed = p_enabled; - emit_changed(); -} - -bool InputEventWithModifiers::is_command_pressed() const { - return command_pressed; -} - void InputEventWithModifiers::set_modifiers_from_event(const InputEventWithModifiers *event) { set_alt_pressed(event->is_alt_pressed()); set_shift_pressed(event->is_shift_pressed()); @@ -217,6 +230,13 @@ Key InputEventWithModifiers::get_modifiers_mask() const { if (is_meta_pressed()) { mask |= KeyModifierMask::META; } + if (is_command_or_control_autoremap()) { +#ifdef MACOS_ENABLED + mask |= KeyModifierMask::META; +#else + mask |= KeyModifierMask::CTRL; +#endif + } return mask; } @@ -248,8 +268,10 @@ String InputEventWithModifiers::to_string() { } void InputEventWithModifiers::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_store_command", "enable"), &InputEventWithModifiers::set_store_command); - ClassDB::bind_method(D_METHOD("is_storing_command"), &InputEventWithModifiers::is_storing_command); + ClassDB::bind_method(D_METHOD("set_command_or_control_autoremap", "enable"), &InputEventWithModifiers::set_command_or_control_autoremap); + ClassDB::bind_method(D_METHOD("is_command_or_control_autoremap"), &InputEventWithModifiers::is_command_or_control_autoremap); + + ClassDB::bind_method(D_METHOD("is_command_or_control_pressed"), &InputEventWithModifiers::is_command_or_control_pressed); ClassDB::bind_method(D_METHOD("set_alt_pressed", "pressed"), &InputEventWithModifiers::set_alt_pressed); ClassDB::bind_method(D_METHOD("is_alt_pressed"), &InputEventWithModifiers::is_alt_pressed); @@ -263,35 +285,25 @@ void InputEventWithModifiers::_bind_methods() { ClassDB::bind_method(D_METHOD("set_meta_pressed", "pressed"), &InputEventWithModifiers::set_meta_pressed); ClassDB::bind_method(D_METHOD("is_meta_pressed"), &InputEventWithModifiers::is_meta_pressed); - ClassDB::bind_method(D_METHOD("set_command_pressed", "pressed"), &InputEventWithModifiers::set_command_pressed); - ClassDB::bind_method(D_METHOD("is_command_pressed"), &InputEventWithModifiers::is_command_pressed); - - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "store_command"), "set_store_command", "is_storing_command"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "command_or_control_autoremap"), "set_command_or_control_autoremap", "is_command_or_control_autoremap"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "alt_pressed"), "set_alt_pressed", "is_alt_pressed"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shift_pressed"), "set_shift_pressed", "is_shift_pressed"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ctrl_pressed"), "set_ctrl_pressed", "is_ctrl_pressed"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "meta_pressed"), "set_meta_pressed", "is_meta_pressed"); - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "command_pressed"), "set_command_pressed", "is_command_pressed"); } -void InputEventWithModifiers::_validate_property(PropertyInfo &property) const { - if (store_command) { - // If we only want to Store "Command". -#ifdef APPLE_STYLE_KEYS - // Don't store "Meta" on Mac. - if (property.name == "meta_pressed") { - property.usage ^= PROPERTY_USAGE_STORAGE; +void InputEventWithModifiers::_validate_property(PropertyInfo &p_property) const { + if (command_or_control_autoremap) { + // Cannot be used with Meta/Command or Control! + if (p_property.name == "meta_pressed") { + p_property.usage ^= PROPERTY_USAGE_STORAGE; } -#else - // Don't store "Ctrl". - if (property.name == "ctrl_pressed") { - property.usage ^= PROPERTY_USAGE_STORAGE; + if (p_property.name == "ctrl_pressed") { + p_property.usage ^= PROPERTY_USAGE_STORAGE; } -#endif } else { - // We don't want to store command, only ctrl or meta (on mac). - if (property.name == "command_pressed") { - property.usage ^= PROPERTY_USAGE_STORAGE; + if (p_property.name == "command_or_control_autoremap") { + p_property.usage ^= PROPERTY_USAGE_STORAGE; } } } @@ -399,14 +411,18 @@ Ref<InputEventKey> InputEventKey::create_reference(Key p_keycode) { if ((p_keycode & KeyModifierMask::ALT) != Key::NONE) { ie->set_alt_pressed(true); } - if ((p_keycode & KeyModifierMask::CTRL) != Key::NONE) { - ie->set_ctrl_pressed(true); - } - if ((p_keycode & KeyModifierMask::CMD) != Key::NONE) { - ie->set_command_pressed(true); - } - if ((p_keycode & KeyModifierMask::META) != Key::NONE) { - ie->set_meta_pressed(true); + if ((p_keycode & KeyModifierMask::CMD_OR_CTRL) != Key::NONE) { + ie->set_command_or_control_autoremap(true); + if ((p_keycode & KeyModifierMask::CTRL) != Key::NONE || (p_keycode & KeyModifierMask::META) != Key::NONE) { + WARN_PRINT("Invalid Key Modifiers: Command or Control autoremapping is enabled, Meta and Control values are ignored!"); + } + } else { + if ((p_keycode & KeyModifierMask::CTRL) != Key::NONE) { + ie->set_ctrl_pressed(true); + } + if ((p_keycode & KeyModifierMask::META) != Key::NONE) { + ie->set_meta_pressed(true); + } } return ie; @@ -433,11 +449,11 @@ bool InputEventKey::action_match(const Ref<InputEvent> &p_event, bool p_exact_ma match &= action_mask == key_mask; } if (match) { - bool pressed = key->is_pressed(); + bool key_pressed = key->is_pressed(); if (r_pressed != nullptr) { - *r_pressed = pressed; + *r_pressed = key_pressed; } - float strength = pressed ? 1.0f : 0.0f; + float strength = key_pressed ? 1.0f : 0.0f; if (r_strength != nullptr) { *r_strength = strength; } @@ -594,20 +610,20 @@ bool InputEventMouseButton::action_match(const Ref<InputEvent> &p_event, bool p_ } bool match = button_index == mb->button_index; - Key action_mask = get_modifiers_mask(); - Key button_mask = mb->get_modifiers_mask(); + Key action_modifiers_mask = get_modifiers_mask(); + Key button_modifiers_mask = mb->get_modifiers_mask(); if (mb->is_pressed()) { - match &= (action_mask & button_mask) == action_mask; + match &= (action_modifiers_mask & button_modifiers_mask) == action_modifiers_mask; } if (p_exact_match) { - match &= action_mask == button_mask; + match &= action_modifiers_mask == button_modifiers_mask; } if (match) { - bool pressed = mb->is_pressed(); + bool mb_pressed = mb->is_pressed(); if (r_pressed != nullptr) { - *r_pressed = pressed; + *r_pressed = mb_pressed; } - float strength = pressed ? 1.0f : 0.0f; + float strength = mb_pressed ? 1.0f : 0.0f; if (r_strength != nullptr) { *r_strength = strength; } @@ -792,9 +808,9 @@ String InputEventMouseMotion::as_text() const { } String InputEventMouseMotion::to_string() { - MouseButton button_mask = get_button_mask(); - String button_mask_string = itos((int64_t)button_mask); - switch (button_mask) { + MouseButton mouse_button_mask = get_button_mask(); + String button_mask_string = itos((int64_t)mouse_button_mask); + switch (mouse_button_mask) { case MouseButton::MASK_LEFT: button_mask_string += vformat(" (%s)", TTRGET(_mouse_button_descriptions[(size_t)MouseButton::LEFT - 1])); break; @@ -1029,11 +1045,11 @@ bool InputEventJoypadButton::action_match(const Ref<InputEvent> &p_event, bool p bool match = button_index == jb->button_index; if (match) { - bool pressed = jb->is_pressed(); + bool jb_pressed = jb->is_pressed(); if (r_pressed != nullptr) { - *r_pressed = pressed; + *r_pressed = jb_pressed; } - float strength = pressed ? 1.0f : 0.0f; + float strength = jb_pressed ? 1.0f : 0.0f; if (r_strength != nullptr) { *r_strength = strength; } @@ -1146,6 +1162,13 @@ bool InputEventScreenTouch::is_pressed() const { return pressed; } +void InputEventScreenTouch::set_double_tap(bool p_double_tap) { + double_tap = p_double_tap; +} +bool InputEventScreenTouch::is_double_tap() const { + return double_tap; +} + Ref<InputEvent> InputEventScreenTouch::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const { Ref<InputEventScreenTouch> st; st.instantiate(); @@ -1154,6 +1177,7 @@ Ref<InputEvent> InputEventScreenTouch::xformed_by(const Transform2D &p_xform, co st->set_index(index); st->set_position(p_xform.xform(pos + p_local_ofs)); st->set_pressed(pressed); + st->set_double_tap(double_tap); return st; } @@ -1166,7 +1190,8 @@ String InputEventScreenTouch::as_text() const { String InputEventScreenTouch::to_string() { String p = pressed ? "true" : "false"; - return vformat("InputEventScreenTouch: index=%d, pressed=%s, position=(%s)", index, p, String(get_position())); + String double_tap_string = double_tap ? "true" : "false"; + return vformat("InputEventScreenTouch: index=%d, pressed=%s, position=(%s), double_tap=%s", index, p, String(get_position()), double_tap_string); } void InputEventScreenTouch::_bind_methods() { @@ -1179,9 +1204,13 @@ void InputEventScreenTouch::_bind_methods() { ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &InputEventScreenTouch::set_pressed); //ClassDB::bind_method(D_METHOD("is_pressed"),&InputEventScreenTouch::is_pressed); + ClassDB::bind_method(D_METHOD("set_double_tap", "double_tap"), &InputEventScreenTouch::set_double_tap); + ClassDB::bind_method(D_METHOD("is_double_tap"), &InputEventScreenTouch::is_double_tap); + ADD_PROPERTY(PropertyInfo(Variant::INT, "index"), "set_index", "get_index"); ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position", PROPERTY_HINT_NONE, "suffix:px"), "set_position", "get_position"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "double_tap"), "set_double_tap", "is_double_tap"); } /////////////////////////////////// @@ -1309,7 +1338,7 @@ bool InputEventAction::is_match(const Ref<InputEvent> &p_event, bool p_exact_mat return false; } - return p_event->is_action(action); + return p_event->is_action(action, p_exact_match); } bool InputEventAction::is_action(const StringName &p_action) const { @@ -1324,16 +1353,16 @@ bool InputEventAction::action_match(const Ref<InputEvent> &p_event, bool p_exact bool match = action == act->action; if (match) { - bool pressed = act->pressed; + bool act_pressed = act->pressed; if (r_pressed != nullptr) { - *r_pressed = pressed; + *r_pressed = act_pressed; } - float strength = pressed ? 1.0f : 0.0f; + float act_strength = act_pressed ? 1.0f : 0.0f; if (r_strength != nullptr) { - *r_strength = strength; + *r_strength = act_strength; } if (r_raw_strength != nullptr) { - *r_raw_strength = strength; + *r_raw_strength = act_strength; } } return match; @@ -1533,7 +1562,7 @@ String InputEventMIDI::as_text() const { } String InputEventMIDI::to_string() { - return vformat("InputEventMIDI: channel=%d, message=%d, pitch=%d, velocity=%d, pressure=%d", channel, message, pitch, velocity, pressure); + return vformat("InputEventMIDI: channel=%d, message=%d, pitch=%d, velocity=%d, pressure=%d, controller_number=%d, controller_value=%d", channel, message, pitch, velocity, pressure, controller_number, controller_value); } void InputEventMIDI::_bind_methods() { diff --git a/core/input/input_event.h b/core/input/input_event.h index 59a2df497c..adbcb7cf68 100644 --- a/core/input/input_event.h +++ b/core/input/input_event.h @@ -107,32 +107,22 @@ public: class InputEventWithModifiers : public InputEventFromWindow { GDCLASS(InputEventWithModifiers, InputEventFromWindow); - bool store_command = true; + bool command_or_control_autoremap = false; bool shift_pressed = false; bool alt_pressed = false; -#ifdef APPLE_STYLE_KEYS - union { - bool command_pressed; - bool meta_pressed = false; //< windows/mac key - }; - + bool meta_pressed = false; // "Command" on macOS, "Meta/Win" key on other platforms. bool ctrl_pressed = false; -#else - union { - bool command_pressed; //< windows/mac key - bool ctrl_pressed = false; - }; - bool meta_pressed = false; //< windows/mac key -#endif protected: static void _bind_methods(); - virtual void _validate_property(PropertyInfo &property) const override; + void _validate_property(PropertyInfo &p_property) const; public: - void set_store_command(bool p_enabled); - bool is_storing_command() const; + void set_command_or_control_autoremap(bool p_enabled); + bool is_command_or_control_autoremap() const; + + bool is_command_or_control_pressed() const; void set_shift_pressed(bool p_pressed); bool is_shift_pressed() const; @@ -146,9 +136,6 @@ public: void set_meta_pressed(bool p_pressed); bool is_meta_pressed() const; - void set_command_pressed(bool p_pressed); - bool is_command_pressed() const; - void set_modifiers_from_event(const InputEventWithModifiers *event); Key get_modifiers_mask() const; @@ -366,6 +353,7 @@ class InputEventScreenTouch : public InputEventFromWindow { int index = 0; Vector2 pos; bool pressed = false; + bool double_tap = false; protected: static void _bind_methods(); @@ -380,6 +368,9 @@ public: void set_pressed(bool p_pressed); virtual bool is_pressed() const override; + void set_double_tap(bool p_double_tap); + bool is_double_tap() const; + virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override; virtual String as_text() const override; virtual String to_string() override; diff --git a/core/input/input_map.cpp b/core/input/input_map.cpp index 942c5248df..1c7d71e309 100644 --- a/core/input/input_map.cpp +++ b/core/input/input_map.cpp @@ -34,6 +34,7 @@ #include "core/input/input.h" #include "core/os/keyboard.h" #include "core/os/os.h" +#include "core/variant/typed_array.h" InputMap *InputMap::singleton = nullptr; @@ -99,8 +100,8 @@ void InputMap::erase_action(const StringName &p_action) { input_map.erase(p_action); } -Array InputMap::_get_actions() { - Array ret; +TypedArray<StringName> InputMap::_get_actions() { + TypedArray<StringName> ret; List<StringName> actions = get_actions(); if (actions.is_empty()) { return ret; @@ -190,8 +191,8 @@ void InputMap::action_erase_events(const StringName &p_action) { input_map[p_action].inputs.clear(); } -Array InputMap::_action_get_events(const StringName &p_action) { - Array ret; +TypedArray<InputEvent> InputMap::_action_get_events(const StringName &p_action) { + TypedArray<InputEvent> ret; const List<Ref<InputEvent>> *al = action_get_events(p_action); if (al) { for (const List<Ref<InputEvent>>::Element *E = al->front(); E; E = E->next()) { @@ -256,7 +257,7 @@ void InputMap::load_from_project_settings() { String name = pi.name.substr(pi.name.find("/") + 1, pi.name.length()); - Dictionary action = ProjectSettings::get_singleton()->get(pi.name); + Dictionary action = GLOBAL_GET(pi.name); float deadzone = action.has("deadzone") ? (float)action["deadzone"] : 0.5f; Array events = action["events"]; @@ -330,12 +331,18 @@ static const _BuiltinActionDisplayName _builtin_action_display_names[] = { { "ui_text_caret_document_start.macos", TTRC("Caret Document Start") }, { "ui_text_caret_document_end", TTRC("Caret Document End") }, { "ui_text_caret_document_end.macos", TTRC("Caret Document End") }, + { "ui_text_caret_add_below", TTRC("Caret Add Below") }, + { "ui_text_caret_add_below.macos", TTRC("Caret Add Below") }, + { "ui_text_caret_add_above", TTRC("Caret Add Above") }, + { "ui_text_caret_add_above.macos", TTRC("Caret Add Above") }, { "ui_text_scroll_up", TTRC("Scroll Up") }, { "ui_text_scroll_up.macos", TTRC("Scroll Up") }, { "ui_text_scroll_down", TTRC("Scroll Down") }, { "ui_text_scroll_down.macos", TTRC("Scroll Down") }, { "ui_text_select_all", TTRC("Select All") }, { "ui_text_select_word_under_caret", TTRC("Select Word Under Caret") }, + { "ui_text_add_selection_for_next_occurrence", TTRC("Add Selection for Next Occurrence") }, + { "ui_text_clear_carets_and_selection", TTRC("Clear Carets and Selection") }, { "ui_text_toggle_insert_mode", TTRC("Toggle Insert Mode") }, { "ui_text_submit", TTRC("Text Submitted") }, { "ui_graph_duplicate", TTRC("Duplicate Nodes") }, @@ -428,27 +435,27 @@ const HashMap<String, List<Ref<InputEvent>>> &InputMap::get_builtins() { // ///// UI basic Shortcuts ///// inputs = List<Ref<InputEvent>>(); - inputs.push_back(InputEventKey::create_reference(Key::X | KeyModifierMask::CMD)); + inputs.push_back(InputEventKey::create_reference(Key::X | KeyModifierMask::CMD_OR_CTRL)); inputs.push_back(InputEventKey::create_reference(Key::KEY_DELETE | KeyModifierMask::SHIFT)); default_builtin_cache.insert("ui_cut", inputs); inputs = List<Ref<InputEvent>>(); - inputs.push_back(InputEventKey::create_reference(Key::C | KeyModifierMask::CMD)); - inputs.push_back(InputEventKey::create_reference(Key::INSERT | KeyModifierMask::CMD)); + inputs.push_back(InputEventKey::create_reference(Key::C | KeyModifierMask::CMD_OR_CTRL)); + inputs.push_back(InputEventKey::create_reference(Key::INSERT | KeyModifierMask::CMD_OR_CTRL)); default_builtin_cache.insert("ui_copy", inputs); inputs = List<Ref<InputEvent>>(); - inputs.push_back(InputEventKey::create_reference(Key::V | KeyModifierMask::CMD)); + inputs.push_back(InputEventKey::create_reference(Key::V | KeyModifierMask::CMD_OR_CTRL)); inputs.push_back(InputEventKey::create_reference(Key::INSERT | KeyModifierMask::SHIFT)); default_builtin_cache.insert("ui_paste", inputs); inputs = List<Ref<InputEvent>>(); - inputs.push_back(InputEventKey::create_reference(Key::Z | KeyModifierMask::CMD)); + inputs.push_back(InputEventKey::create_reference(Key::Z | KeyModifierMask::CMD_OR_CTRL)); default_builtin_cache.insert("ui_undo", inputs); inputs = List<Ref<InputEvent>>(); - inputs.push_back(InputEventKey::create_reference(Key::Z | KeyModifierMask::CMD | KeyModifierMask::SHIFT)); - inputs.push_back(InputEventKey::create_reference(Key::Y | KeyModifierMask::CMD)); + inputs.push_back(InputEventKey::create_reference(Key::Z | KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT)); + inputs.push_back(InputEventKey::create_reference(Key::Y | KeyModifierMask::CMD_OR_CTRL)); default_builtin_cache.insert("ui_redo", inputs); // ///// UI Text Input Shortcuts ///// @@ -473,13 +480,13 @@ const HashMap<String, List<Ref<InputEvent>>> &InputMap::get_builtins() { inputs = List<Ref<InputEvent>>(); - inputs.push_back(InputEventKey::create_reference(Key::ENTER | KeyModifierMask::CMD)); - inputs.push_back(InputEventKey::create_reference(Key::KP_ENTER | KeyModifierMask::CMD)); + inputs.push_back(InputEventKey::create_reference(Key::ENTER | KeyModifierMask::CMD_OR_CTRL)); + inputs.push_back(InputEventKey::create_reference(Key::KP_ENTER | KeyModifierMask::CMD_OR_CTRL)); default_builtin_cache.insert("ui_text_newline_blank", inputs); inputs = List<Ref<InputEvent>>(); - inputs.push_back(InputEventKey::create_reference(Key::ENTER | KeyModifierMask::SHIFT | KeyModifierMask::CMD)); - inputs.push_back(InputEventKey::create_reference(Key::KP_ENTER | KeyModifierMask::SHIFT | KeyModifierMask::CMD)); + inputs.push_back(InputEventKey::create_reference(Key::ENTER | KeyModifierMask::SHIFT | KeyModifierMask::CMD_OR_CTRL)); + inputs.push_back(InputEventKey::create_reference(Key::KP_ENTER | KeyModifierMask::SHIFT | KeyModifierMask::CMD_OR_CTRL)); default_builtin_cache.insert("ui_text_newline_above", inputs); // Indentation @@ -498,7 +505,7 @@ const HashMap<String, List<Ref<InputEvent>>> &InputMap::get_builtins() { default_builtin_cache.insert("ui_text_backspace", inputs); inputs = List<Ref<InputEvent>>(); - inputs.push_back(InputEventKey::create_reference(Key::BACKSPACE | KeyModifierMask::CMD)); + inputs.push_back(InputEventKey::create_reference(Key::BACKSPACE | KeyModifierMask::CMD_OR_CTRL)); default_builtin_cache.insert("ui_text_backspace_word", inputs); inputs = List<Ref<InputEvent>>(); @@ -509,7 +516,7 @@ const HashMap<String, List<Ref<InputEvent>>> &InputMap::get_builtins() { default_builtin_cache.insert("ui_text_backspace_all_to_left", inputs); inputs = List<Ref<InputEvent>>(); - inputs.push_back(InputEventKey::create_reference(Key::BACKSPACE | KeyModifierMask::CMD)); + inputs.push_back(InputEventKey::create_reference(Key::BACKSPACE | KeyModifierMask::CMD_OR_CTRL)); default_builtin_cache.insert("ui_text_backspace_all_to_left.macos", inputs); inputs = List<Ref<InputEvent>>(); @@ -517,7 +524,7 @@ const HashMap<String, List<Ref<InputEvent>>> &InputMap::get_builtins() { default_builtin_cache.insert("ui_text_delete", inputs); inputs = List<Ref<InputEvent>>(); - inputs.push_back(InputEventKey::create_reference(Key::KEY_DELETE | KeyModifierMask::CMD)); + inputs.push_back(InputEventKey::create_reference(Key::KEY_DELETE | KeyModifierMask::CMD_OR_CTRL)); default_builtin_cache.insert("ui_text_delete_word", inputs); inputs = List<Ref<InputEvent>>(); @@ -528,7 +535,7 @@ const HashMap<String, List<Ref<InputEvent>>> &InputMap::get_builtins() { default_builtin_cache.insert("ui_text_delete_all_to_right", inputs); inputs = List<Ref<InputEvent>>(); - inputs.push_back(InputEventKey::create_reference(Key::KEY_DELETE | KeyModifierMask::CMD)); + inputs.push_back(InputEventKey::create_reference(Key::KEY_DELETE | KeyModifierMask::CMD_OR_CTRL)); default_builtin_cache.insert("ui_text_delete_all_to_right.macos", inputs); // Text Caret Movement Left/Right @@ -538,7 +545,7 @@ const HashMap<String, List<Ref<InputEvent>>> &InputMap::get_builtins() { default_builtin_cache.insert("ui_text_caret_left", inputs); inputs = List<Ref<InputEvent>>(); - inputs.push_back(InputEventKey::create_reference(Key::LEFT | KeyModifierMask::CMD)); + inputs.push_back(InputEventKey::create_reference(Key::LEFT | KeyModifierMask::CMD_OR_CTRL)); default_builtin_cache.insert("ui_text_caret_word_left", inputs); inputs = List<Ref<InputEvent>>(); @@ -550,7 +557,7 @@ const HashMap<String, List<Ref<InputEvent>>> &InputMap::get_builtins() { default_builtin_cache.insert("ui_text_caret_right", inputs); inputs = List<Ref<InputEvent>>(); - inputs.push_back(InputEventKey::create_reference(Key::RIGHT | KeyModifierMask::CMD)); + inputs.push_back(InputEventKey::create_reference(Key::RIGHT | KeyModifierMask::CMD_OR_CTRL)); default_builtin_cache.insert("ui_text_caret_word_right", inputs); inputs = List<Ref<InputEvent>>(); @@ -575,7 +582,7 @@ const HashMap<String, List<Ref<InputEvent>>> &InputMap::get_builtins() { inputs = List<Ref<InputEvent>>(); inputs.push_back(InputEventKey::create_reference(Key::A | KeyModifierMask::CTRL)); - inputs.push_back(InputEventKey::create_reference(Key::LEFT | KeyModifierMask::CMD)); + inputs.push_back(InputEventKey::create_reference(Key::LEFT | KeyModifierMask::CMD_OR_CTRL)); default_builtin_cache.insert("ui_text_caret_line_start.macos", inputs); inputs = List<Ref<InputEvent>>(); @@ -584,7 +591,7 @@ const HashMap<String, List<Ref<InputEvent>>> &InputMap::get_builtins() { inputs = List<Ref<InputEvent>>(); inputs.push_back(InputEventKey::create_reference(Key::E | KeyModifierMask::CTRL)); - inputs.push_back(InputEventKey::create_reference(Key::RIGHT | KeyModifierMask::CMD)); + inputs.push_back(InputEventKey::create_reference(Key::RIGHT | KeyModifierMask::CMD_OR_CTRL)); default_builtin_cache.insert("ui_text_caret_line_end.macos", inputs); // Text Caret Movement Page Up/Down @@ -600,50 +607,76 @@ const HashMap<String, List<Ref<InputEvent>>> &InputMap::get_builtins() { // Text Caret Movement Document Start/End inputs = List<Ref<InputEvent>>(); - inputs.push_back(InputEventKey::create_reference(Key::HOME | KeyModifierMask::CMD)); + inputs.push_back(InputEventKey::create_reference(Key::HOME | KeyModifierMask::CMD_OR_CTRL)); default_builtin_cache.insert("ui_text_caret_document_start", inputs); inputs = List<Ref<InputEvent>>(); - inputs.push_back(InputEventKey::create_reference(Key::UP | KeyModifierMask::CMD)); + inputs.push_back(InputEventKey::create_reference(Key::UP | KeyModifierMask::CMD_OR_CTRL)); default_builtin_cache.insert("ui_text_caret_document_start.macos", inputs); inputs = List<Ref<InputEvent>>(); - inputs.push_back(InputEventKey::create_reference(Key::END | KeyModifierMask::CMD)); + inputs.push_back(InputEventKey::create_reference(Key::END | KeyModifierMask::CMD_OR_CTRL)); default_builtin_cache.insert("ui_text_caret_document_end", inputs); inputs = List<Ref<InputEvent>>(); - inputs.push_back(InputEventKey::create_reference(Key::DOWN | KeyModifierMask::CMD)); + inputs.push_back(InputEventKey::create_reference(Key::DOWN | KeyModifierMask::CMD_OR_CTRL)); default_builtin_cache.insert("ui_text_caret_document_end.macos", inputs); + // Text Caret Addition Below/Above + + inputs = List<Ref<InputEvent>>(); + inputs.push_back(InputEventKey::create_reference(Key::DOWN | KeyModifierMask::SHIFT | KeyModifierMask::CMD_OR_CTRL)); + default_builtin_cache.insert("ui_text_caret_add_below", inputs); + + inputs = List<Ref<InputEvent>>(); + inputs.push_back(InputEventKey::create_reference(Key::L | KeyModifierMask::SHIFT | KeyModifierMask::CMD_OR_CTRL)); + default_builtin_cache.insert("ui_text_caret_add_below.macos", inputs); + + inputs = List<Ref<InputEvent>>(); + inputs.push_back(InputEventKey::create_reference(Key::UP | KeyModifierMask::SHIFT | KeyModifierMask::CMD_OR_CTRL)); + default_builtin_cache.insert("ui_text_caret_add_above", inputs); + + inputs = List<Ref<InputEvent>>(); + inputs.push_back(InputEventKey::create_reference(Key::O | KeyModifierMask::SHIFT | KeyModifierMask::CMD_OR_CTRL)); + default_builtin_cache.insert("ui_text_caret_add_above.macos", inputs); + // Text Scrolling inputs = List<Ref<InputEvent>>(); - inputs.push_back(InputEventKey::create_reference(Key::UP | KeyModifierMask::CMD)); + inputs.push_back(InputEventKey::create_reference(Key::UP | KeyModifierMask::CMD_OR_CTRL)); default_builtin_cache.insert("ui_text_scroll_up", inputs); inputs = List<Ref<InputEvent>>(); - inputs.push_back(InputEventKey::create_reference(Key::UP | KeyModifierMask::CMD | KeyModifierMask::ALT)); + inputs.push_back(InputEventKey::create_reference(Key::UP | KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::ALT)); default_builtin_cache.insert("ui_text_scroll_up.macos", inputs); inputs = List<Ref<InputEvent>>(); - inputs.push_back(InputEventKey::create_reference(Key::DOWN | KeyModifierMask::CMD)); + inputs.push_back(InputEventKey::create_reference(Key::DOWN | KeyModifierMask::CMD_OR_CTRL)); default_builtin_cache.insert("ui_text_scroll_down", inputs); inputs = List<Ref<InputEvent>>(); - inputs.push_back(InputEventKey::create_reference(Key::DOWN | KeyModifierMask::CMD | KeyModifierMask::ALT)); + inputs.push_back(InputEventKey::create_reference(Key::DOWN | KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::ALT)); default_builtin_cache.insert("ui_text_scroll_down.macos", inputs); // Text Misc inputs = List<Ref<InputEvent>>(); - inputs.push_back(InputEventKey::create_reference(Key::A | KeyModifierMask::CMD)); + inputs.push_back(InputEventKey::create_reference(Key::A | KeyModifierMask::CMD_OR_CTRL)); default_builtin_cache.insert("ui_text_select_all", inputs); inputs = List<Ref<InputEvent>>(); - inputs.push_back(InputEventKey::create_reference(Key::D | KeyModifierMask::CMD)); + inputs.push_back(InputEventKey::create_reference(Key::G | KeyModifierMask::ALT)); default_builtin_cache.insert("ui_text_select_word_under_caret", inputs); inputs = List<Ref<InputEvent>>(); + inputs.push_back(InputEventKey::create_reference(Key::D | KeyModifierMask::CMD_OR_CTRL)); + default_builtin_cache.insert("ui_text_add_selection_for_next_occurrence", inputs); + + inputs = List<Ref<InputEvent>>(); + inputs.push_back(InputEventKey::create_reference(Key::ESCAPE)); + default_builtin_cache.insert("ui_text_clear_carets_and_selection", inputs); + + inputs = List<Ref<InputEvent>>(); inputs.push_back(InputEventKey::create_reference(Key::INSERT)); default_builtin_cache.insert("ui_text_toggle_insert_mode", inputs); @@ -659,7 +692,7 @@ const HashMap<String, List<Ref<InputEvent>>> &InputMap::get_builtins() { // ///// UI Graph Shortcuts ///// inputs = List<Ref<InputEvent>>(); - inputs.push_back(InputEventKey::create_reference(Key::D | KeyModifierMask::CMD)); + inputs.push_back(InputEventKey::create_reference(Key::D | KeyModifierMask::CMD_OR_CTRL)); default_builtin_cache.insert("ui_graph_duplicate", inputs); inputs = List<Ref<InputEvent>>(); @@ -680,7 +713,7 @@ const HashMap<String, List<Ref<InputEvent>>> &InputMap::get_builtins() { default_builtin_cache.insert("ui_filedialog_show_hidden", inputs); inputs = List<Ref<InputEvent>>(); - inputs.push_back(InputEventKey::create_reference(Key::QUOTELEFT | KeyModifierMask::CMD)); + inputs.push_back(InputEventKey::create_reference(Key::QUOTELEFT | KeyModifierMask::CMD_OR_CTRL)); default_builtin_cache.insert("ui_swap_input_direction", inputs); return default_builtin_cache; diff --git a/core/input/input_map.h b/core/input/input_map.h index 2400a4a3f7..414a06b2f1 100644 --- a/core/input/input_map.h +++ b/core/input/input_map.h @@ -36,6 +36,9 @@ #include "core/object/object.h" #include "core/templates/hash_map.h" +template <typename T> +class TypedArray; + class InputMap : public Object { GDCLASS(InputMap, Object); @@ -60,8 +63,8 @@ private: List<Ref<InputEvent>>::Element *_find_event(Action &p_action, const Ref<InputEvent> &p_event, bool p_exact_match = false, bool *r_pressed = nullptr, float *r_strength = nullptr, float *r_raw_strength = nullptr) const; - Array _action_get_events(const StringName &p_action); - Array _get_actions(); + TypedArray<InputEvent> _action_get_events(const StringName &p_action); + TypedArray<StringName> _get_actions(); protected: static void _bind_methods(); diff --git a/core/io/config_file.cpp b/core/io/config_file.cpp index ae421654ca..f84a95347a 100644 --- a/core/io/config_file.cpp +++ b/core/io/config_file.cpp @@ -32,6 +32,7 @@ #include "core/io/file_access_encrypted.h" #include "core/os/keyboard.h" +#include "core/string/string_builder.h" #include "core/variant/variant_parser.h" PackedStringArray ConfigFile::_get_sections() const { @@ -130,6 +131,28 @@ void ConfigFile::erase_section_key(const String &p_section, const String &p_key) } } +String ConfigFile::encode_to_text() const { + StringBuilder sb; + bool first = true; + for (const KeyValue<String, HashMap<String, Variant>> &E : values) { + if (first) { + first = false; + } else { + sb.append("\n"); + } + if (!E.key.is_empty()) { + sb.append("[" + E.key + "]\n\n"); + } + + for (const KeyValue<String, Variant> &F : E.value) { + String vstr; + VariantWriter::write_to_string(F.value, vstr); + sb.append(F.key.property_name_encode() + "=" + vstr + "\n"); + } + } + return sb.as_string(); +} + Error ConfigFile::save(const String &p_path) { Error err; Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::WRITE, &err); @@ -295,6 +318,7 @@ Error ConfigFile::_parse(const String &p_path, VariantParser::Stream *p_stream) void ConfigFile::clear() { values.clear(); } + void ConfigFile::_bind_methods() { ClassDB::bind_method(D_METHOD("set_value", "section", "key", "value"), &ConfigFile::set_value); ClassDB::bind_method(D_METHOD("get_value", "section", "key", "default"), &ConfigFile::get_value, DEFVAL(Variant())); @@ -312,6 +336,8 @@ void ConfigFile::_bind_methods() { ClassDB::bind_method(D_METHOD("parse", "data"), &ConfigFile::parse); ClassDB::bind_method(D_METHOD("save", "path"), &ConfigFile::save); + ClassDB::bind_method(D_METHOD("encode_to_text"), &ConfigFile::encode_to_text); + BIND_METHOD_ERR_RETURN_DOC("load", ERR_FILE_CANT_OPEN); ClassDB::bind_method(D_METHOD("load_encrypted", "path", "key"), &ConfigFile::load_encrypted); diff --git a/core/io/config_file.h b/core/io/config_file.h index 3b07ec52f5..f6209492b7 100644 --- a/core/io/config_file.h +++ b/core/io/config_file.h @@ -68,6 +68,8 @@ public: Error load(const String &p_path); Error parse(const String &p_data); + String encode_to_text() const; // used by exporter + void clear(); Error load_encrypted(const String &p_path, const Vector<uint8_t> &p_key); diff --git a/core/io/dir_access.cpp b/core/io/dir_access.cpp index f82d6f077f..7eb50d2261 100644 --- a/core/io/dir_access.cpp +++ b/core/io/dir_access.cpp @@ -36,6 +36,8 @@ #include "core/os/os.h" #include "core/templates/local_vector.h" +thread_local Error DirAccess::last_dir_open_error = OK; + String DirAccess::_get_root_path() const { switch (_access_type) { case ACCESS_RESOURCES: @@ -106,7 +108,7 @@ static Error _erase_recursive(DirAccess *da) { if (err) { return err; } - err = da->remove(da->get_current_dir().plus_file(E)); + err = da->remove(da->get_current_dir().path_join(E)); if (err) { return err; } @@ -116,7 +118,7 @@ static Error _erase_recursive(DirAccess *da) { } for (const String &E : files) { - Error err = da->remove(da->get_current_dir().plus_file(E)); + Error err = da->remove(da->get_current_dir().path_join(E)); if (err) { return err; } @@ -138,7 +140,7 @@ Error DirAccess::make_dir_recursive(String p_dir) { if (p_dir.is_relative_path()) { //append current - full_dir = get_current_dir().plus_file(p_dir); + full_dir = get_current_dir().path_join(p_dir); } else { full_dir = p_dir; @@ -172,7 +174,7 @@ Error DirAccess::make_dir_recursive(String p_dir) { String curpath = base; for (int i = 0; i < subdirs.size(); i++) { - curpath = curpath.plus_file(subdirs[i]); + curpath = curpath.path_join(subdirs[i]); Error err = make_dir(curpath); if (err != OK && err != ERR_ALREADY_EXISTS) { ERR_FAIL_V_MSG(err, "Could not create directory: " + curpath); @@ -249,6 +251,61 @@ Ref<DirAccess> DirAccess::open(const String &p_path, Error *r_error) { return da; } +Ref<DirAccess> DirAccess::_open(const String &p_path) { + Error err = OK; + Ref<DirAccess> da = open(p_path, &err); + last_dir_open_error = err; + if (err) { + return Ref<DirAccess>(); + } + return da; +} + +int DirAccess::_get_drive_count() { + Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); + return d->get_drive_count(); +} + +String DirAccess::get_drive_name(int p_idx) { + Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); + return d->get_drive(p_idx); +} + +Error DirAccess::make_dir_absolute(const String &p_dir) { + Ref<DirAccess> d = DirAccess::create_for_path(p_dir); + return d->make_dir(p_dir); +} + +Error DirAccess::make_dir_recursive_absolute(const String &p_dir) { + Ref<DirAccess> d = DirAccess::create_for_path(p_dir); + return d->make_dir_recursive(p_dir); +} + +bool DirAccess::dir_exists_absolute(const String &p_dir) { + Ref<DirAccess> d = DirAccess::create_for_path(p_dir); + return d->dir_exists(p_dir); +} + +Error DirAccess::copy_absolute(const String &p_from, const String &p_to, int p_chmod_flags) { + Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); + // Support copying from res:// to user:// etc. + String from = ProjectSettings::get_singleton()->globalize_path(p_from); + String to = ProjectSettings::get_singleton()->globalize_path(p_to); + return d->copy(from, to, p_chmod_flags); +} + +Error DirAccess::rename_absolute(const String &p_from, const String &p_to) { + Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); + String from = ProjectSettings::get_singleton()->globalize_path(p_from); + String to = ProjectSettings::get_singleton()->globalize_path(p_to); + return d->rename(from, to); +} + +Error DirAccess::remove_absolute(const String &p_path) { + Ref<DirAccess> d = DirAccess::create_for_path(p_path); + return d->remove(p_path); +} + Ref<DirAccess> DirAccess::create(AccessType p_access) { Ref<DirAccess> da = create_func[p_access] ? create_func[p_access]() : nullptr; if (da.is_valid()) { @@ -266,6 +323,10 @@ Ref<DirAccess> DirAccess::create(AccessType p_access) { return da; } +Error DirAccess::get_open_error() { + return last_dir_open_error; +} + String DirAccess::get_full_path(const String &p_path, AccessType p_access) { Ref<DirAccess> d = DirAccess::create(p_access); if (d.is_null()) { @@ -290,7 +351,7 @@ Error DirAccess::copy(String p_from, String p_to, int p_chmod_flags) { const size_t copy_buffer_limit = 65536; // 64 KB fsrc->seek_end(0); - int size = fsrc->get_position(); + uint64_t size = fsrc->get_position(); fsrc->seek(0); err = OK; size_t buffer_size = MIN(size * sizeof(uint8_t), copy_buffer_limit); @@ -354,8 +415,8 @@ Error DirAccess::_copy_dir(Ref<DirAccess> &p_target_da, String p_to, int p_chmod String n = get_next(); while (!n.is_empty()) { if (n != "." && n != "..") { - if (p_copy_links && is_link(get_current_dir().plus_file(n))) { - create_link(read_link(get_current_dir().plus_file(n)), p_to + n); + if (p_copy_links && is_link(get_current_dir().path_join(n))) { + create_link(read_link(get_current_dir().path_join(n)), p_to + n); } else if (current_is_dir()) { dirs.push_back(n); } else { @@ -364,7 +425,7 @@ Error DirAccess::_copy_dir(Ref<DirAccess> &p_target_da, String p_to, int p_chmod list_dir_end(); return ERR_BUG; } - Error err = copy(get_current_dir().plus_file(n), p_to + rel_path, p_chmod_flags); + Error err = copy(get_current_dir().path_join(n), p_to + rel_path, p_chmod_flags); if (err) { list_dir_end(); return err; @@ -424,3 +485,104 @@ bool DirAccess::exists(String p_dir) { Ref<DirAccess> da = DirAccess::create_for_path(p_dir); return da->change_dir(p_dir) == OK; } + +PackedStringArray DirAccess::get_files() { + return _get_contents(false); +} + +PackedStringArray DirAccess::get_files_at(const String &p_path) { + Ref<DirAccess> da = DirAccess::open(p_path); + ERR_FAIL_COND_V_MSG(da.is_null(), PackedStringArray(), vformat("Couldn't open directory at path \"%s\".", p_path)); + return da->get_files(); +} + +PackedStringArray DirAccess::get_directories() { + return _get_contents(true); +} + +PackedStringArray DirAccess::get_directories_at(const String &p_path) { + Ref<DirAccess> da = DirAccess::open(p_path); + ERR_FAIL_COND_V_MSG(da.is_null(), PackedStringArray(), vformat("Couldn't open directory at path \"%s\".", p_path)); + return da->get_directories(); +} + +PackedStringArray DirAccess::_get_contents(bool p_directories) { + PackedStringArray ret; + + list_dir_begin(); + String s = _get_next(); + while (!s.is_empty()) { + if (current_is_dir() == p_directories) { + ret.append(s); + } + s = _get_next(); + } + + ret.sort(); + return ret; +} + +String DirAccess::_get_next() { + String next = get_next(); + while (!next.is_empty() && ((!include_navigational && (next == "." || next == "..")) || (!include_hidden && current_is_hidden()))) { + next = get_next(); + } + return next; +} + +void DirAccess::set_include_navigational(bool p_enable) { + include_navigational = p_enable; +} + +bool DirAccess::get_include_navigational() const { + return include_navigational; +} + +void DirAccess::set_include_hidden(bool p_enable) { + include_hidden = p_enable; +} + +bool DirAccess::get_include_hidden() const { + return include_hidden; +} + +void DirAccess::_bind_methods() { + ClassDB::bind_static_method("DirAccess", D_METHOD("open", "path"), &DirAccess::_open); + ClassDB::bind_static_method("DirAccess", D_METHOD("get_open_error"), &DirAccess::get_open_error); + + ClassDB::bind_method(D_METHOD("list_dir_begin"), &DirAccess::list_dir_begin, DEFVAL(false), DEFVAL(false)); + ClassDB::bind_method(D_METHOD("get_next"), &DirAccess::_get_next); + ClassDB::bind_method(D_METHOD("current_is_dir"), &DirAccess::current_is_dir); + ClassDB::bind_method(D_METHOD("list_dir_end"), &DirAccess::list_dir_end); + ClassDB::bind_method(D_METHOD("get_files"), &DirAccess::get_files); + ClassDB::bind_static_method("DirAccess", D_METHOD("get_files_at", "path"), &DirAccess::get_files_at); + ClassDB::bind_method(D_METHOD("get_directories"), &DirAccess::get_directories); + ClassDB::bind_static_method("DirAccess", D_METHOD("get_directories_at", "path"), &DirAccess::get_directories_at); + ClassDB::bind_static_method("DirAccess", D_METHOD("get_drive_count"), &DirAccess::_get_drive_count); + ClassDB::bind_static_method("DirAccess", D_METHOD("get_drive_name", "idx"), &DirAccess::get_drive_name); + ClassDB::bind_method(D_METHOD("get_current_drive"), &DirAccess::get_current_drive); + ClassDB::bind_method(D_METHOD("change_dir", "to_dir"), &DirAccess::change_dir); + ClassDB::bind_method(D_METHOD("get_current_dir", "include_drive"), &DirAccess::get_current_dir, DEFVAL(true)); + ClassDB::bind_method(D_METHOD("make_dir", "path"), &DirAccess::make_dir); + ClassDB::bind_static_method("DirAccess", D_METHOD("make_dir_absolute", "path"), &DirAccess::make_dir_absolute); + ClassDB::bind_method(D_METHOD("make_dir_recursive", "path"), &DirAccess::make_dir_recursive); + ClassDB::bind_static_method("DirAccess", D_METHOD("make_dir_recursive_absolute", "path"), &DirAccess::make_dir_recursive_absolute); + ClassDB::bind_method(D_METHOD("file_exists", "path"), &DirAccess::file_exists); + ClassDB::bind_method(D_METHOD("dir_exists", "path"), &DirAccess::dir_exists); + ClassDB::bind_static_method("DirAccess", D_METHOD("dir_exists_absolute", "path"), &DirAccess::dir_exists_absolute); + ClassDB::bind_method(D_METHOD("get_space_left"), &DirAccess::get_space_left); + ClassDB::bind_method(D_METHOD("copy", "from", "to", "chmod_flags"), &DirAccess::copy, DEFVAL(-1)); + ClassDB::bind_static_method("DirAccess", D_METHOD("copy_absolute", "from", "to", "chmod_flags"), &DirAccess::copy_absolute, DEFVAL(-1)); + ClassDB::bind_method(D_METHOD("rename", "from", "to"), &DirAccess::rename); + ClassDB::bind_static_method("DirAccess", D_METHOD("rename_absolute", "from", "to"), &DirAccess::rename_absolute); + ClassDB::bind_method(D_METHOD("remove", "path"), &DirAccess::remove); + ClassDB::bind_static_method("DirAccess", D_METHOD("remove_absolute", "path"), &DirAccess::remove_absolute); + + ClassDB::bind_method(D_METHOD("set_include_navigational", "enable"), &DirAccess::set_include_navigational); + ClassDB::bind_method(D_METHOD("get_include_navigational"), &DirAccess::get_include_navigational); + ClassDB::bind_method(D_METHOD("set_include_hidden", "enable"), &DirAccess::set_include_hidden); + ClassDB::bind_method(D_METHOD("get_include_hidden"), &DirAccess::get_include_hidden); + + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "include_navigational"), "set_include_navigational", "get_include_navigational"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "include_hidden"), "set_include_hidden", "get_include_hidden"); +} diff --git a/core/io/dir_access.h b/core/io/dir_access.h index d5318dfb45..bd24214e73 100644 --- a/core/io/dir_access.h +++ b/core/io/dir_access.h @@ -37,6 +37,8 @@ //@ TODO, excellent candidate for THREAD_SAFE MACRO, should go through all these and add THREAD_SAFE where it applies class DirAccess : public RefCounted { + GDCLASS(DirAccess, RefCounted); + public: enum AccessType { ACCESS_RESOURCES, @@ -50,12 +52,20 @@ public: private: AccessType _access_type = ACCESS_FILESYSTEM; static CreateFunc create_func[ACCESS_MAX]; ///< set this to instance a filesystem object + static Ref<DirAccess> _open(const String &p_path); Error _copy_dir(Ref<DirAccess> &p_target_da, String p_to, int p_chmod_flags, bool p_copy_links); + PackedStringArray _get_contents(bool p_directories); + + thread_local static Error last_dir_open_error; + bool include_navigational = false; + bool include_hidden = false; protected: + static void _bind_methods(); + String _get_root_path() const; - String _get_root_string() const; + virtual String _get_root_string() const; AccessType get_access_type() const; String fix_path(String p_path) const; @@ -118,6 +128,7 @@ public: static Ref<DirAccess> create_for_path(const String &p_path); static Ref<DirAccess> create(AccessType p_access); + static Error get_open_error(); template <class T> static void make_default(AccessType p_access) { @@ -126,6 +137,28 @@ public: static Ref<DirAccess> open(const String &p_path, Error *r_error = nullptr); + static int _get_drive_count(); + static String get_drive_name(int p_idx); + + static Error make_dir_absolute(const String &p_dir); + static Error make_dir_recursive_absolute(const String &p_dir); + static bool dir_exists_absolute(const String &p_dir); + + static Error copy_absolute(const String &p_from, const String &p_to, int p_chmod_flags = -1); + static Error rename_absolute(const String &p_from, const String &p_to); + static Error remove_absolute(const String &p_path); + + PackedStringArray get_files(); + static PackedStringArray get_files_at(const String &p_path); + PackedStringArray get_directories(); + static PackedStringArray get_directories_at(const String &p_path); + String _get_next(); + + void set_include_navigational(bool p_enable); + bool get_include_navigational() const; + void set_include_hidden(bool p_enable); + bool get_include_hidden() const; + DirAccess() {} virtual ~DirAccess() {} }; diff --git a/core/io/file_access.cpp b/core/io/file_access.cpp index 8ed3d40c22..cb25564342 100644 --- a/core/io/file_access.cpp +++ b/core/io/file_access.cpp @@ -32,6 +32,8 @@ #include "core/config/project_settings.h" #include "core/crypto/crypto_core.h" +#include "core/io/file_access_compressed.h" +#include "core/io/file_access_encrypted.h" #include "core/io/file_access_pack.h" #include "core/io/marshalls.h" #include "core/os/os.h" @@ -41,6 +43,7 @@ FileAccess::CreateFunc FileAccess::create_func[ACCESS_MAX] = { nullptr, nullptr FileAccess::FileCloseFailNotify FileAccess::close_fail_notify = nullptr; bool FileAccess::backup_save = false; +thread_local Error FileAccess::last_file_open_error = OK; Ref<FileAccess> FileAccess::create(AccessType p_access) { ERR_FAIL_INDEX_V(p_access, ACCESS_MAX, nullptr); @@ -81,7 +84,7 @@ Ref<FileAccess> FileAccess::create_for_path(const String &p_path) { } Error FileAccess::reopen(const String &p_path, int p_mode_flags) { - return _open(p_path, p_mode_flags); + return open_internal(p_path, p_mode_flags); } Ref<FileAccess> FileAccess::open(const String &p_path, int p_mode_flags, Error *r_error) { @@ -99,7 +102,7 @@ Ref<FileAccess> FileAccess::open(const String &p_path, int p_mode_flags, Error * } ret = create_for_path(p_path); - Error err = ret->_open(p_path, p_mode_flags); + Error err = ret->open_internal(p_path, p_mode_flags); if (r_error) { *r_error = err; @@ -111,10 +114,74 @@ Ref<FileAccess> FileAccess::open(const String &p_path, int p_mode_flags, Error * return ret; } +Ref<FileAccess> FileAccess::_open(const String &p_path, ModeFlags p_mode_flags) { + Error err = OK; + Ref<FileAccess> fa = open(p_path, p_mode_flags, &err); + last_file_open_error = err; + if (err) { + return Ref<FileAccess>(); + } + return fa; +} + +Ref<FileAccess> FileAccess::open_encrypted(const String &p_path, ModeFlags p_mode_flags, const Vector<uint8_t> &p_key) { + Ref<FileAccess> fa = _open(p_path, p_mode_flags); + if (fa.is_null()) { + return fa; + } + + Ref<FileAccessEncrypted> fae; + fae.instantiate(); + Error err = fae->open_and_parse(fa, p_key, (p_mode_flags == WRITE) ? FileAccessEncrypted::MODE_WRITE_AES256 : FileAccessEncrypted::MODE_READ); + if (err) { + last_file_open_error = err; + return Ref<FileAccess>(); + } + return fae; +} + +Ref<FileAccess> FileAccess::open_encrypted_pass(const String &p_path, ModeFlags p_mode_flags, const String &p_pass) { + Ref<FileAccess> fa = _open(p_path, p_mode_flags); + if (fa.is_null()) { + return fa; + } + + Ref<FileAccessEncrypted> fae; + fae.instantiate(); + Error err = fae->open_and_parse_password(fa, p_pass, (p_mode_flags == WRITE) ? FileAccessEncrypted::MODE_WRITE_AES256 : FileAccessEncrypted::MODE_READ); + if (err) { + last_file_open_error = err; + return Ref<FileAccess>(); + } + return fae; +} + +Ref<FileAccess> FileAccess::open_compressed(const String &p_path, ModeFlags p_mode_flags, CompressionMode p_compress_mode) { + Ref<FileAccessCompressed> fac; + fac.instantiate(); + fac->configure("GCPF", (Compression::Mode)p_compress_mode); + Error err = fac->open_internal(p_path, p_mode_flags); + + if (err) { + last_file_open_error = err; + return Ref<FileAccess>(); + } + + return fac; +} + +Error FileAccess::get_open_error() { + return last_file_open_error; +} + FileAccess::CreateFunc FileAccess::get_create_func(AccessType p_access) { return create_func[p_access]; } +FileAccess::AccessType FileAccess::get_access_type() const { + return _access_type; +} + String FileAccess::fix_path(const String &p_path) const { //helper used by file accesses that use a single filesystem @@ -223,6 +290,20 @@ real_t FileAccess::get_real() const { } } +Variant FileAccess::get_var(bool p_allow_objects) const { + uint32_t len = get_32(); + Vector<uint8_t> buff = _get_buffer(len); + ERR_FAIL_COND_V((uint32_t)buff.size() != len, Variant()); + + const uint8_t *r = buff.ptr(); + + Variant v; + Error err = decode_variant(v, &r[0], len, nullptr, p_allow_objects); + ERR_FAIL_COND_V_MSG(err != OK, Variant(), "Error when trying to encode Variant."); + + return v; +} + double FileAccess::get_double() const { MarshallDouble m; m.l = get_64(); @@ -366,6 +447,17 @@ Vector<String> FileAccess::get_csv_line(const String &p_delim) const { return strings; } +String FileAccess::get_as_text(bool p_skip_cr) const { + uint64_t original_pos = get_position(); + const_cast<FileAccess *>(this)->seek(0); + + String text = get_as_utf8_string(p_skip_cr); + + const_cast<FileAccess *>(this)->seek(original_pos); + + return text; +} + uint64_t FileAccess::get_buffer(uint8_t *p_dst, uint64_t p_length) const { ERR_FAIL_COND_V(!p_dst && p_length > 0, -1); @@ -377,6 +469,27 @@ uint64_t FileAccess::get_buffer(uint8_t *p_dst, uint64_t p_length) const { return i; } +Vector<uint8_t> FileAccess::_get_buffer(int64_t p_length) const { + Vector<uint8_t> data; + + ERR_FAIL_COND_V_MSG(p_length < 0, data, "Length of buffer cannot be smaller than 0."); + if (p_length == 0) { + return data; + } + + Error err = data.resize(p_length); + ERR_FAIL_COND_V_MSG(err != OK, data, "Can't resize data to " + itos(p_length) + " elements."); + + uint8_t *w = data.ptrw(); + int64_t len = get_buffer(&w[0], p_length); + + if (len < p_length) { + data.resize(len); + } + + return data; +} + String FileAccess::get_as_utf8_string(bool p_skip_cr) const { Vector<uint8_t> sourcef; uint64_t len = get_length(); @@ -435,7 +548,7 @@ void FileAccess::store_64(uint64_t p_dest) { } void FileAccess::store_real(real_t p_real) { - if (sizeof(real_t) == 4) { + if constexpr (sizeof(real_t) == 4) { store_float(p_real); } else { store_double(p_real); @@ -550,6 +663,33 @@ void FileAccess::store_buffer(const uint8_t *p_src, uint64_t p_length) { } } +void FileAccess::_store_buffer(const Vector<uint8_t> &p_buffer) { + uint64_t len = p_buffer.size(); + if (len == 0) { + return; + } + + const uint8_t *r = p_buffer.ptr(); + + store_buffer(&r[0], len); +} + +void FileAccess::store_var(const Variant &p_var, bool p_full_objects) { + int len; + Error err = encode_variant(p_var, nullptr, len, p_full_objects); + ERR_FAIL_COND_MSG(err != OK, "Error when trying to encode Variant."); + + Vector<uint8_t> buff; + buff.resize(len); + + uint8_t *w = buff.ptrw(); + err = encode_variant(p_var, &w[0], len, p_full_objects); + ERR_FAIL_COND_MSG(err != OK, "Error when trying to encode Variant."); + + store_32(len); + _store_buffer(buff); +} + Vector<uint8_t> FileAccess::get_file_as_array(const String &p_path, Error *r_error) { Ref<FileAccess> f = FileAccess::open(p_path, READ, r_error); if (f.is_null()) { @@ -662,3 +802,69 @@ String FileAccess::get_sha256(const String &p_file) { return String::hex_encode_buffer(hash, 32); } + +void FileAccess::_bind_methods() { + ClassDB::bind_static_method("FileAccess", D_METHOD("open", "path", "flags"), &FileAccess::_open); + ClassDB::bind_static_method("FileAccess", D_METHOD("open_encrypted", "path", "mode_flags", "key"), &FileAccess::open_encrypted); + ClassDB::bind_static_method("FileAccess", D_METHOD("open_encrypted_with_pass", "path", "mode_flags", "pass"), &FileAccess::open_encrypted_pass); + ClassDB::bind_static_method("FileAccess", D_METHOD("open_compressed", "path", "mode_flags", "compression_mode"), &FileAccess::open_compressed, DEFVAL(0)); + ClassDB::bind_static_method("FileAccess", D_METHOD("get_open_error"), &FileAccess::get_open_error); + + ClassDB::bind_method(D_METHOD("flush"), &FileAccess::flush); + ClassDB::bind_method(D_METHOD("get_path"), &FileAccess::get_path); + ClassDB::bind_method(D_METHOD("get_path_absolute"), &FileAccess::get_path_absolute); + ClassDB::bind_method(D_METHOD("is_open"), &FileAccess::is_open); + ClassDB::bind_method(D_METHOD("seek", "position"), &FileAccess::seek); + ClassDB::bind_method(D_METHOD("seek_end", "position"), &FileAccess::seek_end, DEFVAL(0)); + ClassDB::bind_method(D_METHOD("get_position"), &FileAccess::get_position); + ClassDB::bind_method(D_METHOD("get_length"), &FileAccess::get_length); + ClassDB::bind_method(D_METHOD("eof_reached"), &FileAccess::eof_reached); + ClassDB::bind_method(D_METHOD("get_8"), &FileAccess::get_8); + ClassDB::bind_method(D_METHOD("get_16"), &FileAccess::get_16); + ClassDB::bind_method(D_METHOD("get_32"), &FileAccess::get_32); + ClassDB::bind_method(D_METHOD("get_64"), &FileAccess::get_64); + ClassDB::bind_method(D_METHOD("get_float"), &FileAccess::get_float); + ClassDB::bind_method(D_METHOD("get_double"), &FileAccess::get_double); + ClassDB::bind_method(D_METHOD("get_real"), &FileAccess::get_real); + ClassDB::bind_method(D_METHOD("get_buffer", "length"), &FileAccess::_get_buffer); + ClassDB::bind_method(D_METHOD("get_line"), &FileAccess::get_line); + ClassDB::bind_method(D_METHOD("get_csv_line", "delim"), &FileAccess::get_csv_line, DEFVAL(",")); + ClassDB::bind_method(D_METHOD("get_as_text", "skip_cr"), &FileAccess::get_as_text, DEFVAL(false)); + ClassDB::bind_static_method("FileAccess", D_METHOD("get_md5", "path"), &FileAccess::get_md5); + ClassDB::bind_static_method("FileAccess", D_METHOD("get_sha256", "path"), &FileAccess::get_sha256); + ClassDB::bind_method(D_METHOD("is_big_endian"), &FileAccess::is_big_endian); + ClassDB::bind_method(D_METHOD("set_big_endian", "big_endian"), &FileAccess::set_big_endian); + ClassDB::bind_method(D_METHOD("get_error"), &FileAccess::get_error); + ClassDB::bind_method(D_METHOD("get_var", "allow_objects"), &FileAccess::get_var, DEFVAL(false)); + + ClassDB::bind_method(D_METHOD("store_8", "value"), &FileAccess::store_8); + ClassDB::bind_method(D_METHOD("store_16", "value"), &FileAccess::store_16); + ClassDB::bind_method(D_METHOD("store_32", "value"), &FileAccess::store_32); + ClassDB::bind_method(D_METHOD("store_64", "value"), &FileAccess::store_64); + ClassDB::bind_method(D_METHOD("store_float", "value"), &FileAccess::store_float); + ClassDB::bind_method(D_METHOD("store_double", "value"), &FileAccess::store_double); + ClassDB::bind_method(D_METHOD("store_real", "value"), &FileAccess::store_real); + ClassDB::bind_method(D_METHOD("store_buffer", "buffer"), &FileAccess::_store_buffer); + ClassDB::bind_method(D_METHOD("store_line", "line"), &FileAccess::store_line); + ClassDB::bind_method(D_METHOD("store_csv_line", "values", "delim"), &FileAccess::store_csv_line, DEFVAL(",")); + ClassDB::bind_method(D_METHOD("store_string", "string"), &FileAccess::store_string); + ClassDB::bind_method(D_METHOD("store_var", "value", "full_objects"), &FileAccess::store_var, DEFVAL(false)); + + ClassDB::bind_method(D_METHOD("store_pascal_string", "string"), &FileAccess::store_pascal_string); + ClassDB::bind_method(D_METHOD("get_pascal_string"), &FileAccess::get_pascal_string); + + ClassDB::bind_static_method("FileAccess", D_METHOD("file_exists", "path"), &FileAccess::exists); + ClassDB::bind_static_method("FileAccess", D_METHOD("get_modified_time", "file"), &FileAccess::get_modified_time); + + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "big_endian"), "set_big_endian", "is_big_endian"); + + BIND_ENUM_CONSTANT(READ); + BIND_ENUM_CONSTANT(WRITE); + BIND_ENUM_CONSTANT(READ_WRITE); + BIND_ENUM_CONSTANT(WRITE_READ); + + BIND_ENUM_CONSTANT(COMPRESSION_FASTLZ); + BIND_ENUM_CONSTANT(COMPRESSION_DEFLATE); + BIND_ENUM_CONSTANT(COMPRESSION_ZSTD); + BIND_ENUM_CONSTANT(COMPRESSION_GZIP); +} diff --git a/core/io/file_access.h b/core/io/file_access.h index 3386800686..8ca44306a0 100644 --- a/core/io/file_access.h +++ b/core/io/file_access.h @@ -31,6 +31,7 @@ #ifndef FILE_ACCESS_H #define FILE_ACCESS_H +#include "core/io/compression.h" #include "core/math/math_defs.h" #include "core/object/ref_counted.h" #include "core/os/memory.h" @@ -42,6 +43,8 @@ */ class FileAccess : public RefCounted { + GDCLASS(FileAccess, RefCounted); + public: enum AccessType { ACCESS_RESOURCES, @@ -50,6 +53,20 @@ public: ACCESS_MAX }; + enum ModeFlags { + READ = 1, + WRITE = 2, + READ_WRITE = 3, + WRITE_READ = 7, + }; + + enum CompressionMode { + COMPRESSION_FASTLZ = Compression::MODE_FASTLZ, + COMPRESSION_DEFLATE = Compression::MODE_DEFLATE, + COMPRESSION_ZSTD = Compression::MODE_ZSTD, + COMPRESSION_GZIP = Compression::MODE_GZIP + }; + typedef void (*FileCloseFailNotify)(const String &); typedef Ref<FileAccess> (*CreateFunc)(); @@ -60,14 +77,19 @@ public: virtual Error _set_unix_permissions(const String &p_file, uint32_t p_permissions) = 0; protected: + static void _bind_methods(); + + AccessType get_access_type() const; String fix_path(const String &p_path) const; - virtual Error _open(const String &p_path, int p_mode_flags) = 0; ///< open a file + virtual Error open_internal(const String &p_path, int p_mode_flags) = 0; ///< open a file virtual uint64_t _get_modified_time(const String &p_file) = 0; + virtual void _set_access_type(AccessType p_access); static FileCloseFailNotify close_fail_notify; private: static bool backup_save; + thread_local static Error last_file_open_error; AccessType _access_type = ACCESS_FILESYSTEM; static CreateFunc create_func[ACCESS_MAX]; /** default file access creation function for a platform */ @@ -76,18 +98,11 @@ private: return memnew(T); } + static Ref<FileAccess> _open(const String &p_path, ModeFlags p_mode_flags); + public: static void set_file_close_fail_notify_callback(FileCloseFailNotify p_cbk) { close_fail_notify = p_cbk; } - virtual void _set_access_type(AccessType p_access); - - enum ModeFlags { - READ = 1, - WRITE = 2, - READ_WRITE = 3, - WRITE_READ = 7, - }; - virtual bool is_open() const = 0; ///< true when file is open virtual String get_path() const { return ""; } /// returns the path for the current open file @@ -109,10 +124,14 @@ public: virtual double get_double() const; virtual real_t get_real() const; + Variant get_var(bool p_allow_objects = false) const; + virtual uint64_t get_buffer(uint8_t *p_dst, uint64_t p_length) const; ///< get an array of bytes + Vector<uint8_t> _get_buffer(int64_t p_length) const; virtual String get_line() const; virtual String get_token() const; virtual Vector<String> get_csv_line(const String &p_delim = ",") const; + String get_as_text(bool p_skip_cr = false) const; virtual String get_as_utf8_string(bool p_skip_cr = false) const; /** @@ -143,6 +162,9 @@ public: virtual String get_pascal_string(); virtual void store_buffer(const uint8_t *p_src, uint64_t p_length); ///< store an array of bytes + void _store_buffer(const Vector<uint8_t> &p_buffer); + + void store_var(const Variant &p_var, bool p_full_objects = false); virtual bool file_exists(const String &p_name) = 0; ///< return true if a file exists @@ -151,6 +173,12 @@ public: static Ref<FileAccess> create(AccessType p_access); /// Create a file access (for the current platform) this is the only portable way of accessing files. static Ref<FileAccess> create_for_path(const String &p_path); static Ref<FileAccess> open(const String &p_path, int p_mode_flags, Error *r_error = nullptr); /// Create a file access (for the current platform) this is the only portable way of accessing files. + + static Ref<FileAccess> open_encrypted(const String &p_path, ModeFlags p_mode_flags, const Vector<uint8_t> &p_key); + static Ref<FileAccess> open_encrypted_pass(const String &p_path, ModeFlags p_mode_flags, const String &p_pass); + static Ref<FileAccess> open_compressed(const String &p_path, ModeFlags p_mode_flags, CompressionMode p_compress_mode = COMPRESSION_FASTLZ); + static Error get_open_error(); + static CreateFunc get_create_func(AccessType p_access); static bool exists(const String &p_name); ///< return true if a file exists static uint64_t get_modified_time(const String &p_file); @@ -176,4 +204,7 @@ public: virtual ~FileAccess() {} }; +VARIANT_ENUM_CAST(FileAccess::CompressionMode); +VARIANT_ENUM_CAST(FileAccess::ModeFlags); + #endif // FILE_ACCESS_H diff --git a/core/io/file_access_compressed.cpp b/core/io/file_access_compressed.cpp index 1d0a718166..d2c8a88269 100644 --- a/core/io/file_access_compressed.cpp +++ b/core/io/file_access_compressed.cpp @@ -95,7 +95,7 @@ Error FileAccessCompressed::open_after_magic(Ref<FileAccess> p_base) { return ret == -1 ? ERR_FILE_CORRUPT : OK; } -Error FileAccessCompressed::_open(const String &p_path, int p_mode_flags) { +Error FileAccessCompressed::open_internal(const String &p_path, int p_mode_flags) { ERR_FAIL_COND_V(p_mode_flags == READ_WRITE, ERR_UNAVAILABLE); _close(); diff --git a/core/io/file_access_compressed.h b/core/io/file_access_compressed.h index e41491a92c..ee114c2c65 100644 --- a/core/io/file_access_compressed.h +++ b/core/io/file_access_compressed.h @@ -70,7 +70,7 @@ public: Error open_after_magic(Ref<FileAccess> p_base); - virtual Error _open(const String &p_path, int p_mode_flags) override; ///< open a file + virtual Error open_internal(const String &p_path, int p_mode_flags) override; ///< open a file virtual bool is_open() const override; ///< true when file is open virtual void seek(uint64_t p_position) override; ///< seek to a given position diff --git a/core/io/file_access_encrypted.cpp b/core/io/file_access_encrypted.cpp index d1b014a0be..e7b2a2dfee 100644 --- a/core/io/file_access_encrypted.cpp +++ b/core/io/file_access_encrypted.cpp @@ -102,16 +102,16 @@ Error FileAccessEncrypted::open_and_parse(Ref<FileAccess> p_base, const Vector<u Error FileAccessEncrypted::open_and_parse_password(Ref<FileAccess> p_base, const String &p_key, Mode p_mode) { String cs = p_key.md5_text(); ERR_FAIL_COND_V(cs.length() != 32, ERR_INVALID_PARAMETER); - Vector<uint8_t> key; - key.resize(32); + Vector<uint8_t> key_md5; + key_md5.resize(32); for (int i = 0; i < 32; i++) { - key.write[i] = cs[i]; + key_md5.write[i] = cs[i]; } - return open_and_parse(p_base, key, p_mode); + return open_and_parse(p_base, key_md5, p_mode); } -Error FileAccessEncrypted::_open(const String &p_path, int p_mode_flags) { +Error FileAccessEncrypted::open_internal(const String &p_path, int p_mode_flags) { return OK; } diff --git a/core/io/file_access_encrypted.h b/core/io/file_access_encrypted.h index 6200f87a7a..6b4588841d 100644 --- a/core/io/file_access_encrypted.h +++ b/core/io/file_access_encrypted.h @@ -60,7 +60,7 @@ public: Error open_and_parse(Ref<FileAccess> p_base, const Vector<uint8_t> &p_key, Mode p_mode, bool p_with_magic = true); Error open_and_parse_password(Ref<FileAccess> p_base, const String &p_key, Mode p_mode); - virtual Error _open(const String &p_path, int p_mode_flags) override; ///< open a file + virtual Error open_internal(const String &p_path, int p_mode_flags) override; ///< open a file virtual bool is_open() const override; ///< true when file is open virtual String get_path() const override; /// returns the path for the current open file diff --git a/core/io/file_access_memory.cpp b/core/io/file_access_memory.cpp index 499d001234..21ded4247f 100644 --- a/core/io/file_access_memory.cpp +++ b/core/io/file_access_memory.cpp @@ -78,7 +78,7 @@ Error FileAccessMemory::open_custom(const uint8_t *p_data, uint64_t p_len) { return OK; } -Error FileAccessMemory::_open(const String &p_path, int p_mode_flags) { +Error FileAccessMemory::open_internal(const String &p_path, int p_mode_flags) { ERR_FAIL_COND_V(!files, ERR_FILE_NOT_FOUND); String name = fix_path(p_path); diff --git a/core/io/file_access_memory.h b/core/io/file_access_memory.h index f2bd2aa832..b1f408eb98 100644 --- a/core/io/file_access_memory.h +++ b/core/io/file_access_memory.h @@ -45,7 +45,7 @@ public: static void cleanup(); virtual Error open_custom(const uint8_t *p_data, uint64_t p_len); ///< open a file - virtual Error _open(const String &p_path, int p_mode_flags) override; ///< open a file + virtual Error open_internal(const String &p_path, int p_mode_flags) override; ///< open a file virtual bool is_open() const override; ///< true when file is open virtual void seek(uint64_t p_position) override; ///< seek to a given position diff --git a/core/io/file_access_network.cpp b/core/io/file_access_network.cpp index 1365b4b593..87f3f66597 100644 --- a/core/io/file_access_network.cpp +++ b/core/io/file_access_network.cpp @@ -137,12 +137,12 @@ void FileAccessNetworkClient::_thread_func() { int64_t offset = get_64(); int32_t len = get_32(); - Vector<uint8_t> block; - block.resize(len); - client->get_data(block.ptrw(), len); + Vector<uint8_t> resp_block; + resp_block.resize(len); + client->get_data(resp_block.ptrw(), len); if (fa) { //may have been queued - fa->_set_block(offset, block); + fa->_set_block(offset, resp_block); } } break; @@ -252,7 +252,7 @@ void FileAccessNetwork::_respond(uint64_t p_len, Error p_status) { pages.resize(pc); } -Error FileAccessNetwork::_open(const String &p_path, int p_mode_flags) { +Error FileAccessNetwork::open_internal(const String &p_path, int p_mode_flags) { ERR_FAIL_COND_V(p_mode_flags != READ, ERR_UNAVAILABLE); _close(); diff --git a/core/io/file_access_network.h b/core/io/file_access_network.h index ceadc715a1..ee92d3b9db 100644 --- a/core/io/file_access_network.h +++ b/core/io/file_access_network.h @@ -132,7 +132,7 @@ public: RESPONSE_GET_MODTIME, }; - virtual Error _open(const String &p_path, int p_mode_flags) override; ///< open a file + virtual Error open_internal(const String &p_path, int p_mode_flags) override; ///< open a file virtual bool is_open() const override; ///< true when file is open virtual void seek(uint64_t p_position) override; ///< seek to a given position diff --git a/core/io/file_access_pack.cpp b/core/io/file_access_pack.cpp index 595a6e9873..dfcce30ab5 100644 --- a/core/io/file_access_pack.cpp +++ b/core/io/file_access_pack.cpp @@ -259,7 +259,7 @@ Ref<FileAccess> PackedSourcePCK::get_file(const String &p_path, PackedData::Pack ////////////////////////////////////////////////////////////////// -Error FileAccessPack::_open(const String &p_path, int p_mode_flags) { +Error FileAccessPack::open_internal(const String &p_path, int p_mode_flags) { ERR_FAIL_V(ERR_UNAVAILABLE); return ERR_UNAVAILABLE; } @@ -520,7 +520,7 @@ String DirAccessPack::get_current_dir(bool p_include_drive) const { while (pd->parent) { pd = pd->parent; - p = pd->name.plus_file(p); + p = pd->name.path_join(p); } return "res://" + p; diff --git a/core/io/file_access_pack.h b/core/io/file_access_pack.h index 023758ac0f..4b9b49a161 100644 --- a/core/io/file_access_pack.h +++ b/core/io/file_access_pack.h @@ -148,7 +148,7 @@ class FileAccessPack : public FileAccess { uint64_t off; Ref<FileAccess> f; - virtual Error _open(const String &p_path, int p_mode_flags) override; + virtual Error open_internal(const String &p_path, int p_mode_flags) override; virtual uint64_t _get_modified_time(const String &p_file) override { return 0; } virtual uint32_t _get_unix_permissions(const String &p_file) override { return 0; } virtual Error _set_unix_permissions(const String &p_file, uint32_t p_permissions) override { return FAILED; } diff --git a/core/io/file_access_zip.cpp b/core/io/file_access_zip.cpp index 17f2335a8e..72503851c1 100644 --- a/core/io/file_access_zip.cpp +++ b/core/io/file_access_zip.cpp @@ -234,7 +234,7 @@ ZipArchive::~ZipArchive() { packages.clear(); } -Error FileAccessZip::_open(const String &p_path, int p_mode_flags) { +Error FileAccessZip::open_internal(const String &p_path, int p_mode_flags) { _close(); ERR_FAIL_COND_V(p_mode_flags & FileAccess::WRITE, FAILED); @@ -337,7 +337,7 @@ bool FileAccessZip::file_exists(const String &p_name) { } FileAccessZip::FileAccessZip(const String &p_path, const PackedData::PackedFile &p_file) { - _open(p_path, FileAccess::READ); + open_internal(p_path, FileAccess::READ); } FileAccessZip::~FileAccessZip() { diff --git a/core/io/file_access_zip.h b/core/io/file_access_zip.h index 74a48192f3..6d61b9a291 100644 --- a/core/io/file_access_zip.h +++ b/core/io/file_access_zip.h @@ -85,7 +85,7 @@ class FileAccessZip : public FileAccess { void _close(); public: - virtual Error _open(const String &p_path, int p_mode_flags) override; ///< open a file + virtual Error open_internal(const String &p_path, int p_mode_flags) override; ///< open a file virtual bool is_open() const override; ///< true when file is open virtual void seek(uint64_t p_position) override; ///< seek to a given position diff --git a/core/io/http_client.cpp b/core/io/http_client.cpp index 52b1120b2a..93a310e83b 100644 --- a/core/io/http_client.cpp +++ b/core/io/http_client.cpp @@ -138,7 +138,7 @@ PackedStringArray HTTPClient::_get_response_headers() { } void HTTPClient::_bind_methods() { - ClassDB::bind_method(D_METHOD("connect_to_host", "host", "port", "use_ssl", "verify_host"), &HTTPClient::connect_to_host, DEFVAL(-1), DEFVAL(false), DEFVAL(true)); + ClassDB::bind_method(D_METHOD("connect_to_host", "host", "port", "use_tls", "verify_host"), &HTTPClient::connect_to_host, DEFVAL(-1), DEFVAL(false), DEFVAL(true)); ClassDB::bind_method(D_METHOD("set_connection", "connection"), &HTTPClient::set_connection); ClassDB::bind_method(D_METHOD("get_connection"), &HTTPClient::get_connection); ClassDB::bind_method(D_METHOD("request_raw", "method", "url", "headers", "body"), &HTTPClient::_request_raw); @@ -190,7 +190,7 @@ void HTTPClient::_bind_methods() { BIND_ENUM_CONSTANT(STATUS_REQUESTING); // Request in progress BIND_ENUM_CONSTANT(STATUS_BODY); // Request resulted in body which must be read BIND_ENUM_CONSTANT(STATUS_CONNECTION_ERROR); - BIND_ENUM_CONSTANT(STATUS_SSL_HANDSHAKE_ERROR); + BIND_ENUM_CONSTANT(STATUS_TLS_HANDSHAKE_ERROR); BIND_ENUM_CONSTANT(RESPONSE_CONTINUE); BIND_ENUM_CONSTANT(RESPONSE_SWITCHING_PROTOCOLS); diff --git a/core/io/http_client.h b/core/io/http_client.h index de6045f647..0524b010f4 100644 --- a/core/io/http_client.h +++ b/core/io/http_client.h @@ -138,7 +138,7 @@ public: STATUS_REQUESTING, // Request in progress STATUS_BODY, // Request resulted in body, which must be read STATUS_CONNECTION_ERROR, - STATUS_SSL_HANDSHAKE_ERROR, + STATUS_TLS_HANDSHAKE_ERROR, }; @@ -168,7 +168,7 @@ public: Error verify_headers(const Vector<String> &p_headers); virtual Error request(Method p_method, const String &p_url, const Vector<String> &p_headers, const uint8_t *p_body, int p_body_size) = 0; - virtual Error connect_to_host(const String &p_host, int p_port = -1, bool p_ssl = false, bool p_verify_host = true) = 0; + virtual Error connect_to_host(const String &p_host, int p_port = -1, bool p_tls = false, bool p_verify_host = true) = 0; virtual void set_connection(const Ref<StreamPeer> &p_connection) = 0; virtual Ref<StreamPeer> get_connection() const = 0; diff --git a/core/io/http_client_tcp.cpp b/core/io/http_client_tcp.cpp index d983d86b99..aff79320ca 100644 --- a/core/io/http_client_tcp.cpp +++ b/core/io/http_client_tcp.cpp @@ -28,18 +28,18 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#ifndef JAVASCRIPT_ENABLED +#ifndef WEB_ENABLED #include "http_client_tcp.h" -#include "core/io/stream_peer_ssl.h" +#include "core/io/stream_peer_tls.h" #include "core/version.h" HTTPClient *HTTPClientTCP::_create_func() { return memnew(HTTPClientTCP); } -Error HTTPClientTCP::connect_to_host(const String &p_host, int p_port, bool p_ssl, bool p_verify_host) { +Error HTTPClientTCP::connect_to_host(const String &p_host, int p_port, bool p_tls, bool p_verify_host) { close(); conn_port = p_port; @@ -47,21 +47,21 @@ Error HTTPClientTCP::connect_to_host(const String &p_host, int p_port, bool p_ss ip_candidates.clear(); - ssl = p_ssl; - ssl_verify_host = p_verify_host; + tls = p_tls; + tls_verify_host = p_verify_host; String host_lower = conn_host.to_lower(); if (host_lower.begins_with("http://")) { conn_host = conn_host.substr(7, conn_host.length() - 7); } else if (host_lower.begins_with("https://")) { - ssl = true; + tls = true; conn_host = conn_host.substr(8, conn_host.length() - 8); } ERR_FAIL_COND_V(conn_host.length() < HOST_MIN_LEN, ERR_INVALID_PARAMETER); if (conn_port < 0) { - if (ssl) { + if (tls) { conn_port = PORT_HTTPS; } else { conn_port = PORT_HTTP; @@ -70,11 +70,11 @@ Error HTTPClientTCP::connect_to_host(const String &p_host, int p_port, bool p_ss connection = tcp_connection; - if (ssl && https_proxy_port != -1) { + if (tls && https_proxy_port != -1) { proxy_client.instantiate(); // Needs proxy negotiation. server_host = https_proxy_host; server_port = https_proxy_port; - } else if (!ssl && http_proxy_port != -1) { + } else if (!tls && http_proxy_port != -1) { server_host = http_proxy_host; server_port = http_proxy_port; } else { @@ -94,6 +94,10 @@ Error HTTPClientTCP::connect_to_host(const String &p_host, int p_port, bool p_ss } else { // Host contains hostname and needs to be resolved to IP. resolving = IP::get_singleton()->resolve_hostname_queue_item(server_host); + if (resolving == IP::RESOLVER_INVALID_ID) { + status = STATUS_CANT_RESOLVE; + return ERR_CANT_RESOLVE; + } status = STATUS_RESOLVING; } @@ -103,9 +107,9 @@ Error HTTPClientTCP::connect_to_host(const String &p_host, int p_port, bool p_ss void HTTPClientTCP::set_connection(const Ref<StreamPeer> &p_connection) { ERR_FAIL_COND_MSG(p_connection.is_null(), "Connection is not a reference to a valid StreamPeer object."); - if (ssl) { - ERR_FAIL_NULL_MSG(Object::cast_to<StreamPeerSSL>(p_connection.ptr()), - "Connection is not a reference to a valid StreamPeerSSL object."); + if (tls) { + ERR_FAIL_NULL_MSG(Object::cast_to<StreamPeerTLS>(p_connection.ptr()), + "Connection is not a reference to a valid StreamPeerTLS object."); } if (connection == p_connection) { @@ -152,7 +156,7 @@ Error HTTPClientTCP::request(Method p_method, const String &p_url, const Vector< } String uri = p_url; - if (!ssl && http_proxy_port != -1) { + if (!tls && http_proxy_port != -1) { uri = vformat("http://%s:%d%s", conn_host, conn_port, p_url); } @@ -177,7 +181,7 @@ Error HTTPClientTCP::request(Method p_method, const String &p_url, const Vector< } } if (add_host) { - if ((ssl && conn_port == PORT_HTTPS) || (!ssl && conn_port == PORT_HTTP)) { + if ((tls && conn_port == PORT_HTTPS) || (!tls && conn_port == PORT_HTTP)) { // Don't append the standard ports. request += "Host: " + conn_host + "\r\n"; } else { @@ -312,7 +316,7 @@ Error HTTPClientTCP::poll() { return OK; } break; case StreamPeerTCP::STATUS_CONNECTED: { - if (ssl && proxy_client.is_valid()) { + if (tls && proxy_client.is_valid()) { Error err = proxy_client->poll(); if (err == ERR_UNCONFIGURED) { proxy_client->set_connection(tcp_connection); @@ -353,42 +357,42 @@ Error HTTPClientTCP::poll() { return ERR_CANT_CONNECT; } break; } - } else if (ssl) { - Ref<StreamPeerSSL> ssl; + } else if (tls) { + Ref<StreamPeerTLS> tls_conn; if (!handshaking) { - // Connect the StreamPeerSSL and start handshaking. - ssl = Ref<StreamPeerSSL>(StreamPeerSSL::create()); - ssl->set_blocking_handshake_enabled(false); - Error err = ssl->connect_to_stream(tcp_connection, ssl_verify_host, conn_host); + // Connect the StreamPeerTLS and start handshaking. + tls_conn = Ref<StreamPeerTLS>(StreamPeerTLS::create()); + tls_conn->set_blocking_handshake_enabled(false); + Error err = tls_conn->connect_to_stream(tcp_connection, tls_verify_host, conn_host); if (err != OK) { close(); - status = STATUS_SSL_HANDSHAKE_ERROR; + status = STATUS_TLS_HANDSHAKE_ERROR; return ERR_CANT_CONNECT; } - connection = ssl; + connection = tls_conn; handshaking = true; } else { - // We are already handshaking, which means we can use your already active SSL connection. - ssl = static_cast<Ref<StreamPeerSSL>>(connection); - if (ssl.is_null()) { + // We are already handshaking, which means we can use your already active TLS connection. + tls_conn = static_cast<Ref<StreamPeerTLS>>(connection); + if (tls_conn.is_null()) { close(); - status = STATUS_SSL_HANDSHAKE_ERROR; + status = STATUS_TLS_HANDSHAKE_ERROR; return ERR_CANT_CONNECT; } - ssl->poll(); // Try to finish the handshake. + tls_conn->poll(); // Try to finish the handshake. } - if (ssl->get_status() == StreamPeerSSL::STATUS_CONNECTED) { + if (tls_conn->get_status() == StreamPeerTLS::STATUS_CONNECTED) { // Handshake has been successful. handshaking = false; ip_candidates.clear(); status = STATUS_CONNECTED; return OK; - } else if (ssl->get_status() != StreamPeerSSL::STATUS_HANDSHAKING) { + } else if (tls_conn->get_status() != StreamPeerTLS::STATUS_HANDSHAKING) { // Handshake has failed. close(); - status = STATUS_SSL_HANDSHAKE_ERROR; + status = STATUS_TLS_HANDSHAKE_ERROR; return ERR_CANT_CONNECT; } // ... we will need to poll more for handshake to finish. @@ -417,10 +421,10 @@ Error HTTPClientTCP::poll() { case STATUS_BODY: case STATUS_CONNECTED: { // Check if we are still connected. - if (ssl) { - Ref<StreamPeerSSL> tmp = connection; + if (tls) { + Ref<StreamPeerTLS> tmp = connection; tmp->poll(); - if (tmp->get_status() != StreamPeerSSL::STATUS_CONNECTED) { + if (tmp->get_status() != StreamPeerTLS::STATUS_CONNECTED) { status = STATUS_CONNECTION_ERROR; return ERR_CONNECTION_ERROR; } @@ -544,7 +548,7 @@ Error HTTPClientTCP::poll() { return ERR_UNCONFIGURED; } break; case STATUS_CONNECTION_ERROR: - case STATUS_SSL_HANDSHAKE_ERROR: { + case STATUS_TLS_HANDSHAKE_ERROR: { return ERR_CONNECTION_ERROR; } break; case STATUS_CANT_CONNECT: { @@ -788,4 +792,4 @@ HTTPClientTCP::HTTPClientTCP() { HTTPClient *(*HTTPClient::_create)() = HTTPClientTCP::_create_func; -#endif // #ifndef JAVASCRIPT_ENABLED +#endif // WEB_ENABLED diff --git a/core/io/http_client_tcp.h b/core/io/http_client_tcp.h index c10e0b1eca..744c15f7ab 100644 --- a/core/io/http_client_tcp.h +++ b/core/io/http_client_tcp.h @@ -46,8 +46,8 @@ private: String http_proxy_host; int https_proxy_port = -1; // Proxy server for https requests. String https_proxy_host; - bool ssl = false; - bool ssl_verify_host = false; + bool tls = false; + bool tls_verify_host = false; bool blocking = false; bool handshaking = false; bool head_request = false; @@ -79,7 +79,7 @@ public: Error request(Method p_method, const String &p_url, const Vector<String> &p_headers, const uint8_t *p_body, int p_body_size) override; - Error connect_to_host(const String &p_host, int p_port = -1, bool p_ssl = false, bool p_verify_host = true) override; + Error connect_to_host(const String &p_host, int p_port = -1, bool p_tls = false, bool p_verify_host = true) override; void set_connection(const Ref<StreamPeer> &p_connection) override; Ref<StreamPeer> get_connection() const override; void close() override; diff --git a/core/io/image.cpp b/core/io/image.cpp index 2d87523ca4..21146dd80c 100644 --- a/core/io/image.cpp +++ b/core/io/image.cpp @@ -416,8 +416,8 @@ int Image::get_height() const { return height; } -Vector2i Image::get_size() const { - return Vector2i(width, height); +Size2i Image::get_size() const { + return Size2i(width, height); } bool Image::has_mipmaps() const { @@ -444,7 +444,7 @@ static void _convert(int p_width, int p_height, const uint8_t *p_src, uint8_t *p uint8_t rgba[4] = { 0, 0, 0, 255 }; - if (read_gray) { + if constexpr (read_gray) { rgba[0] = rofs[0]; rgba[1] = rofs[0]; rgba[2] = rofs[0]; @@ -454,11 +454,11 @@ static void _convert(int p_width, int p_height, const uint8_t *p_src, uint8_t *p } } - if (read_alpha || write_alpha) { + if constexpr (read_alpha || write_alpha) { rgba[3] = read_alpha ? rofs[read_bytes] : 255; } - if (write_gray) { + if constexpr (write_gray) { //TODO: not correct grayscale, should use fixed point version of actual weights wofs[0] = uint8_t((uint16_t(rgba[0]) + uint16_t(rgba[1]) + uint16_t(rgba[2])) / 3); } else { @@ -467,7 +467,7 @@ static void _convert(int p_width, int p_height, const uint8_t *p_src, uint8_t *p } } - if (write_alpha) { + if constexpr (write_alpha) { wofs[write_bytes] = rgba[3]; } } @@ -640,7 +640,7 @@ static void _scale_cubic(const uint8_t *__restrict p_src, uint8_t *__restrict p_ double xfac = (double)width / p_dst_width; double yfac = (double)height / p_dst_height; // coordinates of source points and coefficients - double ox, oy, dx, dy, k1, k2; + double ox, oy, dx, dy; int ox1, oy1, ox2, oy2; // destination pixel values // width and height decreased by 1 @@ -671,7 +671,7 @@ static void _scale_cubic(const uint8_t *__restrict p_src, uint8_t *__restrict p_ for (int n = -1; n < 3; n++) { // get Y coefficient - k1 = _bicubic_interp_kernel(dy - (double)n); + [[maybe_unused]] double k1 = _bicubic_interp_kernel(dy - (double)n); oy2 = oy1 + n; if (oy2 < 0) { @@ -683,7 +683,7 @@ static void _scale_cubic(const uint8_t *__restrict p_src, uint8_t *__restrict p_ for (int m = -1; m < 3; m++) { // get X coefficient - k2 = k1 * _bicubic_interp_kernel((double)m - dx); + [[maybe_unused]] double k2 = k1 * _bicubic_interp_kernel((double)m - dx); ox2 = ox1 + m; if (ox2 < 0) { @@ -697,7 +697,7 @@ static void _scale_cubic(const uint8_t *__restrict p_src, uint8_t *__restrict p_ const T *__restrict p = ((T *)p_src) + (oy2 * p_src_width + ox2) * CC; for (int i = 0; i < CC; i++) { - if (sizeof(T) == 2) { //half float + if constexpr (sizeof(T) == 2) { //half float color[i] = Math::half_to_float(p[i]); } else { color[i] += p[i] * k2; @@ -707,9 +707,9 @@ static void _scale_cubic(const uint8_t *__restrict p_src, uint8_t *__restrict p_ } for (int i = 0; i < CC; i++) { - if (sizeof(T) == 1) { //byte + if constexpr (sizeof(T) == 1) { //byte dst[i] = CLAMP(Math::fast_ftoi(color[i]), 0, 255); - } else if (sizeof(T) == 2) { //half float + } else if constexpr (sizeof(T) == 2) { //half float dst[i] = Math::make_half_float(color[i]); } else { dst[i] = color[i]; @@ -758,7 +758,7 @@ static void _scale_bilinear(const uint8_t *__restrict p_src, uint8_t *__restrict src_xofs_right *= CC; for (uint32_t l = 0; l < CC; l++) { - if (sizeof(T) == 1) { //uint8 + if constexpr (sizeof(T) == 1) { //uint8 uint32_t p00 = p_src[y_ofs_up + src_xofs_left + l] << FRAC_BITS; uint32_t p10 = p_src[y_ofs_up + src_xofs_right + l] << FRAC_BITS; uint32_t p01 = p_src[y_ofs_down + src_xofs_left + l] << FRAC_BITS; @@ -769,7 +769,7 @@ static void _scale_bilinear(const uint8_t *__restrict p_src, uint8_t *__restrict uint32_t interp = interp_up + (((interp_down - interp_up) * src_yofs_frac) >> FRAC_BITS); interp >>= FRAC_BITS; p_dst[i * p_dst_width * CC + j * CC + l] = uint8_t(interp); - } else if (sizeof(T) == 2) { //half float + } else if constexpr (sizeof(T) == 2) { //half float float xofs_frac = float(src_xofs_frac) / (1 << FRAC_BITS); float yofs_frac = float(src_yofs_frac) / (1 << FRAC_BITS); @@ -786,7 +786,7 @@ static void _scale_bilinear(const uint8_t *__restrict p_src, uint8_t *__restrict float interp = interp_up + ((interp_down - interp_up) * yofs_frac); dst[i * p_dst_width * CC + j * CC + l] = Math::make_half_float(interp); - } else if (sizeof(T) == 4) { //float + } else if constexpr (sizeof(T) == 4) { //float float xofs_frac = float(src_xofs_frac) / (1 << FRAC_BITS); float yofs_frac = float(src_yofs_frac) / (1 << FRAC_BITS); @@ -877,7 +877,7 @@ static void _scale_lanczos(const uint8_t *__restrict p_src, uint8_t *__restrict const T *__restrict src_data = ((const T *)p_src) + (buffer_y * src_width + target_x) * CC; for (uint32_t i = 0; i < CC; i++) { - if (sizeof(T) == 2) { //half float + if constexpr (sizeof(T) == 2) { //half float pixel[i] += Math::half_to_float(src_data[i]) * lanczos_val; } else { pixel[i] += src_data[i] * lanczos_val; @@ -934,9 +934,9 @@ static void _scale_lanczos(const uint8_t *__restrict p_src, uint8_t *__restrict for (uint32_t i = 0; i < CC; i++) { pixel[i] /= weight; - if (sizeof(T) == 1) { //byte + if constexpr (sizeof(T) == 1) { //byte dst_data[i] = CLAMP(Math::fast_ftoi(pixel[i]), 0, 255); - } else if (sizeof(T) == 2) { //half float + } else if constexpr (sizeof(T) == 2) { //half float dst_data[i] = Math::make_half_float(pixel[i]); } else { // float dst_data[i] = pixel[i]; @@ -982,7 +982,7 @@ void Image::resize_to_po2(bool p_square, Interpolation p_interpolation) { } void Image::resize(int p_width, int p_height, Interpolation p_interpolation) { - ERR_FAIL_COND_MSG(data.size() == 0, "Cannot resize image before creating it, use create() or create_from_data() first."); + ERR_FAIL_COND_MSG(data.size() == 0, "Cannot resize image before creating it, use set_data() first."); ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot resize in compressed or custom image formats."); bool mipmap_aware = p_interpolation == INTERPOLATE_TRILINEAR /* || p_interpolation == INTERPOLATE_TRICUBIC */; @@ -1017,7 +1017,7 @@ void Image::resize(int p_width, int p_height, Interpolation p_interpolation) { } bool interpolate_mipmaps = mipmap_aware && mip1 != mip2; if (interpolate_mipmaps) { - dst2.create(p_width, p_height, false, format); + dst2.initialize_data(p_width, p_height, false, format); } bool had_mipmaps = mipmaps; @@ -1341,78 +1341,126 @@ void Image::crop(int p_width, int p_height) { void Image::rotate_90(ClockDirection p_direction) { ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot rotate in compressed or custom image formats."); - ERR_FAIL_COND_MSG(width <= 1, "The Image width specified (" + itos(width) + " pixels) must be greater than 1 pixels."); - ERR_FAIL_COND_MSG(height <= 1, "The Image height specified (" + itos(height) + " pixels) must be greater than 1 pixels."); - - int saved_width = height; - int saved_height = width; - - if (width != height) { - int n = MAX(width, height); - resize(n, n, INTERPOLATE_NEAREST); - } + ERR_FAIL_COND_MSG(width <= 0, "The Image width specified (" + itos(width) + " pixels) must be greater than 0 pixels."); + ERR_FAIL_COND_MSG(height <= 0, "The Image height specified (" + itos(height) + " pixels) must be greater than 0 pixels."); bool used_mipmaps = has_mipmaps(); if (used_mipmaps) { clear_mipmaps(); } + // In-place 90 degrees rotation by following the permutation cycles. { - uint8_t *w = data.ptrw(); - uint8_t src[16]; - uint8_t dst[16]; + // Explanation by example (clockwise): + // + // abc da + // def -> eb + // fc + // + // In memory: + // 012345 012345 + // abcdef -> daebfc + // + // Permutation cycles: + // (0 --a--> 1 --b--> 3 --d--> 0) + // (2 --c--> 5 --f--> 4 --e--> 2) + // + // Applying cycles (backwards): + // 0->s s=a (store) + // 3->0 abcdef -> dbcdef + // 1->3 dbcdef -> dbcbef + // s->1 dbcbef -> dacbef + // + // 2->s s=c + // 4->2 dacbef -> daebef + // 5->4 daebef -> daebff + // s->5 daebff -> daebfc + + const int w = width; + const int h = height; + const int size = w * h; + + uint8_t *data_ptr = data.ptrw(); uint32_t pixel_size = get_format_pixel_size(format); - // Flip. + uint8_t single_pixel_buffer[16]; - if (p_direction == CLOCKWISE) { - for (int y = 0; y < height / 2; y++) { - for (int x = 0; x < width; x++) { - _get_pixelb(x, y, pixel_size, w, src); - _get_pixelb(x, height - y - 1, pixel_size, w, dst); +#define PREV_INDEX_IN_CYCLE(index) (p_direction == CLOCKWISE) ? ((h - 1 - (index % h)) * w + (index / h)) : ((index % h) * w + (w - 1 - (index / h))) - _put_pixelb(x, height - y - 1, pixel_size, w, src); - _put_pixelb(x, y, pixel_size, w, dst); + if (w == h) { // Square case, 4-length cycles only (plus irrelevant thus skipped 1-length cycle in the middle for odd-sized squares). + for (int y = 0; y < h / 2; y++) { + for (int x = 0; x < (w + 1) / 2; x++) { + int current = y * w + x; + memcpy(single_pixel_buffer, data_ptr + current * pixel_size, pixel_size); + for (int i = 0; i < 3; i++) { + int prev = PREV_INDEX_IN_CYCLE(current); + memcpy(data_ptr + current * pixel_size, data_ptr + prev * pixel_size, pixel_size); + current = prev; + } + memcpy(data_ptr + current * pixel_size, single_pixel_buffer, pixel_size); } } - } else { - for (int y = 0; y < height; y++) { - for (int x = 0; x < width / 2; x++) { - _get_pixelb(x, y, pixel_size, w, src); - _get_pixelb(width - x - 1, y, pixel_size, w, dst); + } else { // Rectangular case (w != h), kinda unpredictable cycles. + int permuted_pixels_count = 0; + + for (int i = 0; i < size; i++) { + int prev = PREV_INDEX_IN_CYCLE(i); + if (prev == i) { + // 1-length cycle, pixel remains at the same index. + permuted_pixels_count++; + continue; + } - _put_pixelb(width - x - 1, y, pixel_size, w, src); - _put_pixelb(x, y, pixel_size, w, dst); + // Check whether we already processed this cycle. + // We iterate over it and if we'll find an index smaller than `i` then we already + // processed this cycle because we always start at the smallest index in the cycle. + // TODO: Improve this naive approach, can be done better. + while (prev > i) { + prev = PREV_INDEX_IN_CYCLE(prev); + } + if (prev < i) { + continue; } - } - } - // Transpose. + // Save the in-cycle pixel with the smallest index (`i`). + memcpy(single_pixel_buffer, data_ptr + i * pixel_size, pixel_size); - for (int y = 0; y < height; y++) { - for (int x = 0; x < width; x++) { - if (x < y) { - _get_pixelb(x, y, pixel_size, w, src); - _get_pixelb(y, x, pixel_size, w, dst); + // Overwrite pixels one by one by the preceding pixel in the cycle. + int current = i; + prev = PREV_INDEX_IN_CYCLE(current); + while (prev != i) { + memcpy(data_ptr + current * pixel_size, data_ptr + prev * pixel_size, pixel_size); + permuted_pixels_count++; + + current = prev; + prev = PREV_INDEX_IN_CYCLE(current); + }; - _put_pixelb(y, x, pixel_size, w, src); - _put_pixelb(x, y, pixel_size, w, dst); + // Overwrite the remaining pixel in the cycle by the saved pixel with the smallest index. + memcpy(data_ptr + current * pixel_size, single_pixel_buffer, pixel_size); + permuted_pixels_count++; + + if (permuted_pixels_count == size) { + break; } } + + width = h; + height = w; } + +#undef PREV_INDEX_IN_CYCLE } - if (saved_width != saved_height) { - resize(saved_width, saved_height, INTERPOLATE_NEAREST); - } else if (used_mipmaps) { + if (used_mipmaps) { generate_mipmaps(); } } void Image::rotate_180() { ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot rotate in compressed or custom image formats."); - ERR_FAIL_COND_MSG(width <= 1, "The Image width specified (" + itos(width) + " pixels) must be greater than 1 pixels."); - ERR_FAIL_COND_MSG(height <= 1, "The Image height specified (" + itos(height) + " pixels) must be greater than 1 pixels."); + ERR_FAIL_COND_MSG(width <= 0, "The Image width specified (" + itos(width) + " pixels) must be greater than 0 pixels."); + ERR_FAIL_COND_MSG(height <= 0, "The Image height specified (" + itos(height) + " pixels) must be greater than 0 pixels."); bool used_mipmaps = has_mipmaps(); if (used_mipmaps) { @@ -1420,19 +1468,21 @@ void Image::rotate_180() { } { - uint8_t *w = data.ptrw(); - uint8_t src[16]; - uint8_t dst[16]; + uint8_t *data_ptr = data.ptrw(); uint32_t pixel_size = get_format_pixel_size(format); - for (int y = 0; y < height / 2; y++) { - for (int x = 0; x < width; x++) { - _get_pixelb(x, y, pixel_size, w, src); - _get_pixelb(width - x - 1, height - y - 1, pixel_size, w, dst); + uint8_t single_pixel_buffer[16]; - _put_pixelb(width - x - 1, height - y - 1, pixel_size, w, src); - _put_pixelb(x, y, pixel_size, w, dst); - } + uint8_t *from_begin_ptr = data_ptr; + uint8_t *from_end_ptr = data_ptr + (width * height - 1) * pixel_size; + + while (from_begin_ptr < from_end_ptr) { + memcpy(single_pixel_buffer, from_begin_ptr, pixel_size); + memcpy(from_begin_ptr, from_end_ptr, pixel_size); + memcpy(from_end_ptr, single_pixel_buffer, pixel_size); + + from_begin_ptr += pixel_size; + from_end_ptr -= pixel_size; } } @@ -2016,9 +2066,7 @@ Error Image::generate_mipmap_roughness(RoughnessChannel p_roughness_channel, con uint8_t* wr = imgdata.ptrw(); memcpy(wr.ptr(), ptr, size); wr = uint8_t*(); - Ref<Image> im; - im.instantiate(); - im->create(w, h, false, format, imgdata); + Ref<Image> im = Image::create_from_data(w, h, false, format, imgdata); im->save_png("res://mipmap_" + itos(i) + ".png"); } #endif @@ -2051,7 +2099,25 @@ Vector<uint8_t> Image::get_data() const { return data; } -void Image::create(int p_width, int p_height, bool p_use_mipmaps, Format p_format) { +Ref<Image> Image::create_empty(int p_width, int p_height, bool p_use_mipmaps, Format p_format) { + Ref<Image> image; + image.instantiate(); + image->initialize_data(p_width, p_height, p_use_mipmaps, p_format); + return image; +} + +Ref<Image> Image::create_from_data(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const Vector<uint8_t> &p_data) { + Ref<Image> image; + image.instantiate(); + image->initialize_data(p_width, p_height, p_use_mipmaps, p_format, p_data); + return image; +} + +void Image::set_data(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const Vector<uint8_t> &p_data) { + initialize_data(p_width, p_height, p_use_mipmaps, p_format, p_data); +} + +void Image::initialize_data(int p_width, int p_height, bool p_use_mipmaps, Format p_format) { ERR_FAIL_COND_MSG(p_width <= 0, "The Image width specified (" + itos(p_width) + " pixels) must be greater than 0 pixels."); ERR_FAIL_COND_MSG(p_height <= 0, "The Image height specified (" + itos(p_height) + " pixels) must be greater than 0 pixels."); ERR_FAIL_COND_MSG(p_width > MAX_WIDTH, @@ -2077,7 +2143,7 @@ void Image::create(int p_width, int p_height, bool p_use_mipmaps, Format p_forma format = p_format; } -void Image::create(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const Vector<uint8_t> &p_data) { +void Image::initialize_data(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const Vector<uint8_t> &p_data) { ERR_FAIL_COND_MSG(p_width <= 0, "The Image width specified (" + itos(p_width) + " pixels) must be greater than 0 pixels."); ERR_FAIL_COND_MSG(p_height <= 0, "The Image height specified (" + itos(p_height) + " pixels) must be greater than 0 pixels."); ERR_FAIL_COND_MSG(p_width > MAX_WIDTH, @@ -2115,7 +2181,7 @@ void Image::create(int p_width, int p_height, bool p_use_mipmaps, Format p_forma mipmaps = p_use_mipmaps; } -void Image::create(const char **p_xpm) { +void Image::initialize_data(const char **p_xpm) { int size_width = 0; int size_height = 0; int pixelchars = 0; @@ -2230,7 +2296,7 @@ void Image::create(const char **p_xpm) { } if (line == colormap_size) { status = READING_PIXELS; - create(size_width, size_height, false, has_alpha ? FORMAT_RGBA8 : FORMAT_RGB8); + initialize_data(size_width, size_height, false, has_alpha ? FORMAT_RGBA8 : FORMAT_RGB8); data_write = data.ptrw(); pixel_size = has_alpha ? 4 : 3; } @@ -2559,7 +2625,7 @@ Image::Image(const char **p_xpm) { mipmaps = false; format = FORMAT_L8; - create(p_xpm); + initialize_data(p_xpm); } Image::Image(int p_width, int p_height, bool p_use_mipmaps, Format p_format) { @@ -2568,7 +2634,7 @@ Image::Image(int p_width, int p_height, bool p_use_mipmaps, Format p_format) { mipmaps = p_use_mipmaps; format = FORMAT_L8; - create(p_width, p_height, p_use_mipmaps, p_format); + initialize_data(p_width, p_height, p_use_mipmaps, p_format); } Image::Image(int p_width, int p_height, bool p_mipmaps, Format p_format, const Vector<uint8_t> &p_data) { @@ -2577,7 +2643,7 @@ Image::Image(int p_width, int p_height, bool p_mipmaps, Format p_format, const V mipmaps = p_mipmaps; format = FORMAT_L8; - create(p_width, p_height, p_mipmaps, p_format, p_data); + initialize_data(p_width, p_height, p_mipmaps, p_format, p_data); } Rect2i Image::get_used_rect() const { @@ -2620,9 +2686,9 @@ Rect2i Image::get_used_rect() const { } } -Ref<Image> Image::get_rect(const Rect2i &p_area) const { - Ref<Image> img = memnew(Image(p_area.size.x, p_area.size.y, mipmaps, format)); - img->blit_rect(Ref<Image>((Image *)this), p_area, Point2i(0, 0)); +Ref<Image> Image::get_region(const Rect2i &p_region) const { + Ref<Image> img = memnew(Image(p_region.size.x, p_region.size.y, mipmaps, format)); + img->blit_rect(Ref<Image>((Image *)this), p_region, Point2i(0, 0)); return img; } @@ -2671,7 +2737,7 @@ void Image::blit_rect(const Ref<Image> &p_src, const Rect2i &p_src_rect, const P Rect2i src_rect; Rect2i dest_rect; _get_clipped_src_and_dest_rects(p_src, p_src_rect, p_dest, src_rect, dest_rect); - if (src_rect.has_no_area() || dest_rect.has_no_area()) { + if (!src_rect.has_area() || !dest_rect.has_area()) { return; } @@ -2717,7 +2783,7 @@ void Image::blit_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, co Rect2i src_rect; Rect2i dest_rect; _get_clipped_src_and_dest_rects(p_src, p_src_rect, p_dest, src_rect, dest_rect); - if (src_rect.has_no_area() || dest_rect.has_no_area()) { + if (!src_rect.has_area() || !dest_rect.has_area()) { return; } @@ -2762,7 +2828,7 @@ void Image::blend_rect(const Ref<Image> &p_src, const Rect2i &p_src_rect, const Rect2i src_rect; Rect2i dest_rect; _get_clipped_src_and_dest_rects(p_src, p_src_rect, p_dest, src_rect, dest_rect); - if (src_rect.has_no_area() || dest_rect.has_no_area()) { + if (!src_rect.has_area() || !dest_rect.has_area()) { return; } @@ -2802,7 +2868,7 @@ void Image::blend_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, c Rect2i src_rect; Rect2i dest_rect; _get_clipped_src_and_dest_rects(p_src, p_src_rect, p_dest, src_rect, dest_rect); - if (src_rect.has_no_area() || dest_rect.has_no_area()) { + if (!src_rect.has_area() || !dest_rect.has_area()) { return; } @@ -2846,6 +2912,9 @@ void Image::_repeat_pixel_over_subsequent_memory(uint8_t *p_pixel, int p_pixel_s } void Image::fill(const Color &p_color) { + if (data.size() == 0) { + return; + } ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot fill in compressed or custom image formats."); uint8_t *dst_data_ptr = data.ptrw(); @@ -2859,10 +2928,13 @@ void Image::fill(const Color &p_color) { } void Image::fill_rect(const Rect2i &p_rect, const Color &p_color) { + if (data.size() == 0) { + return; + } ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot fill rect in compressed or custom image formats."); Rect2i r = Rect2i(0, 0, width, height).intersection(p_rect.abs()); - if (r.has_no_area()) { + if (!r.has_area()) { return; } @@ -2931,7 +3003,7 @@ void Image::_set_data(const Dictionary &p_data) { ERR_FAIL_COND(ddformat == FORMAT_MAX); - create(dwidth, dheight, dmipmaps, ddformat, ddata); + initialize_data(dwidth, dheight, dmipmaps, ddformat, ddata); } Dictionary Image::_get_data() const { @@ -3294,8 +3366,9 @@ void Image::_bind_methods() { ClassDB::bind_method(D_METHOD("generate_mipmaps", "renormalize"), &Image::generate_mipmaps, DEFVAL(false)); ClassDB::bind_method(D_METHOD("clear_mipmaps"), &Image::clear_mipmaps); - ClassDB::bind_method(D_METHOD("create", "width", "height", "use_mipmaps", "format"), &Image::create_empty); - ClassDB::bind_method(D_METHOD("create_from_data", "width", "height", "use_mipmaps", "format", "data"), &Image::create_from_data); + ClassDB::bind_static_method("Image", D_METHOD("create", "width", "height", "use_mipmaps", "format"), &Image::create_empty); + ClassDB::bind_static_method("Image", D_METHOD("create_from_data", "width", "height", "use_mipmaps", "format", "data"), &Image::create_from_data); + ClassDB::bind_method(D_METHOD("set_data", "width", "height", "use_mipmaps", "format", "data"), &Image::set_data); ClassDB::bind_method(D_METHOD("is_empty"), &Image::is_empty); @@ -3339,7 +3412,7 @@ void Image::_bind_methods() { ClassDB::bind_method(D_METHOD("fill_rect", "rect", "color"), &Image::fill_rect); ClassDB::bind_method(D_METHOD("get_used_rect"), &Image::get_used_rect); - ClassDB::bind_method(D_METHOD("get_rect", "rect"), &Image::get_rect); + ClassDB::bind_method(D_METHOD("get_region", "region"), &Image::get_region); ClassDB::bind_method(D_METHOD("copy_from", "src"), &Image::copy_internals_from); @@ -3460,9 +3533,7 @@ Ref<Image> Image::rgbe_to_srgb() { ERR_FAIL_COND_V(format != FORMAT_RGBE9995, Ref<Image>()); - Ref<Image> new_image; - new_image.instantiate(); - new_image->create(width, height, false, Image::FORMAT_RGB8); + Ref<Image> new_image = create_empty(width, height, false, Image::FORMAT_RGB8); for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { @@ -3503,6 +3574,7 @@ Ref<Image> Image::get_image_from_mipmap(int p_mipamp) const { void Image::bump_map_to_normal_map(float bump_scale) { ERR_FAIL_COND(!_can_modify(format)); + clear_mipmaps(); convert(Image::FORMAT_RF); Vector<uint8_t> result_image; //rgba output @@ -3869,15 +3941,15 @@ Dictionary Image::compute_image_metrics(const Ref<Image> p_compared_image, bool double image_metric_max, image_metric_mean, image_metric_mean_squared, image_metric_root_mean_squared, image_metric_peak_snr = 0.0; const bool average_component_error = true; - const uint32_t width = MIN(compared_image->get_width(), source_image->get_width()); - const uint32_t height = MIN(compared_image->get_height(), source_image->get_height()); + const uint32_t w = MIN(compared_image->get_width(), source_image->get_width()); + const uint32_t h = MIN(compared_image->get_height(), source_image->get_height()); // Histogram approach originally due to Charles Bloom. double hist[256]; memset(hist, 0, sizeof(hist)); - for (uint32_t y = 0; y < height; y++) { - for (uint32_t x = 0; x < width; x++) { + for (uint32_t y = 0; y < h; y++) { + for (uint32_t x = 0; x < w; x++) { const Color color_a = compared_image->get_pixel(x, y); const Color color_b = source_image->get_pixel(x, y); @@ -3922,7 +3994,7 @@ Dictionary Image::compute_image_metrics(const Ref<Image> p_compared_image, bool } // See http://richg42.blogspot.com/2016/09/how-to-compute-psnr-from-old-berkeley.html - double total_values = width * height; + double total_values = w * h; if (average_component_error) { total_values *= 4; diff --git a/core/io/image.h b/core/io/image.h index 9d415423be..62df81e7c8 100644 --- a/core/io/image.h +++ b/core/io/image.h @@ -209,7 +209,7 @@ private: public: int get_width() const; ///< Get image width int get_height() const; ///< Get image height - Vector2i get_size() const; + Size2i get_size() const; bool has_mipmaps() const; int get_mipmap_count() const; @@ -279,12 +279,12 @@ public: void normalize(); //for normal maps /** - * Create a new image of a given size and format. Current image will be lost + * Creates new internal image data of a given size and format. Current image will be lost. */ - void create(int p_width, int p_height, bool p_use_mipmaps, Format p_format); - void create(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const Vector<uint8_t> &p_data); + void initialize_data(int p_width, int p_height, bool p_use_mipmaps, Format p_format); + void initialize_data(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const Vector<uint8_t> &p_data); + void initialize_data(const char **p_xpm); - void create(const char **p_xpm); /** * returns true when the image is empty (0,0) in size */ @@ -303,13 +303,9 @@ public: Error save_webp(const String &p_path, const bool p_lossy = false, const float p_quality = 0.75f) const; Vector<uint8_t> save_webp_to_buffer(const bool p_lossy = false, const float p_quality = 0.75f) const; - void create_empty(int p_width, int p_height, bool p_use_mipmaps, Format p_format) { - create(p_width, p_height, p_use_mipmaps, p_format); - } - - void create_from_data(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const Vector<uint8_t> &p_data) { - create(p_width, p_height, p_use_mipmaps, p_format, p_data); - } + static Ref<Image> create_empty(int p_width, int p_height, bool p_use_mipmaps, Format p_format); + static Ref<Image> create_from_data(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const Vector<uint8_t> &p_data); + void set_data(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const Vector<uint8_t> &p_data); /** * create an empty image @@ -381,7 +377,7 @@ public: void fill_rect(const Rect2i &p_rect, const Color &p_color); Rect2i get_used_rect() const; - Ref<Image> get_rect(const Rect2i &p_area) const; + Ref<Image> get_region(const Rect2i &p_area) const; static void set_compress_bc_func(void (*p_compress_func)(Image *, float, UsedChannels)); static void set_compress_bptc_func(void (*p_compress_func)(Image *, float, UsedChannels)); diff --git a/core/io/image_loader.cpp b/core/io/image_loader.cpp index 9cf7c9caba..6c00ca4b67 100644 --- a/core/io/image_loader.cpp +++ b/core/io/image_loader.cpp @@ -32,6 +32,12 @@ #include "core/string/print_string.h" +void ImageFormatLoader::_bind_methods() { + BIND_BITFIELD_FLAG(FLAG_NONE); + BIND_BITFIELD_FLAG(FLAG_FORCE_LINEAR); + BIND_BITFIELD_FLAG(FLAG_CONVERT_COLORS); +} + bool ImageFormatLoader::recognize(const String &p_extension) const { List<String> extensions; get_recognized_extensions(&extensions); @@ -44,7 +50,37 @@ bool ImageFormatLoader::recognize(const String &p_extension) const { return false; } -Error ImageLoader::load_image(String p_file, Ref<Image> p_image, Ref<FileAccess> p_custom, bool p_force_linear, float p_scale) { +Error ImageFormatLoaderExtension::load_image(Ref<Image> p_image, Ref<FileAccess> p_fileaccess, BitField<ImageFormatLoader::LoaderFlags> p_flags, float p_scale) { + Error err = ERR_UNAVAILABLE; + GDVIRTUAL_CALL(_load_image, p_image, p_fileaccess, p_flags, p_scale, err); + return err; +} + +void ImageFormatLoaderExtension::get_recognized_extensions(List<String> *p_extension) const { + PackedStringArray ext; + if (GDVIRTUAL_CALL(_get_recognized_extensions, ext)) { + for (int i = 0; i < ext.size(); i++) { + p_extension->push_back(ext[i]); + } + } +} + +void ImageFormatLoaderExtension::add_format_loader() { + ImageLoader::add_image_format_loader(this); +} + +void ImageFormatLoaderExtension::remove_format_loader() { + ImageLoader::remove_image_format_loader(this); +} + +void ImageFormatLoaderExtension::_bind_methods() { + GDVIRTUAL_BIND(_get_recognized_extensions); + GDVIRTUAL_BIND(_load_image, "image", "fileaccess", "flags", "scale"); + ClassDB::bind_method(D_METHOD("add_format_loader"), &ImageFormatLoaderExtension::add_format_loader); + ClassDB::bind_method(D_METHOD("remove_format_loader"), &ImageFormatLoaderExtension::remove_format_loader); +} + +Error ImageLoader::load_image(String p_file, Ref<Image> p_image, Ref<FileAccess> p_custom, BitField<ImageFormatLoader::LoaderFlags> p_flags, float p_scale) { ERR_FAIL_COND_V_MSG(p_image.is_null(), ERR_INVALID_PARAMETER, "It's not a reference to a valid Image object."); Ref<FileAccess> f = p_custom; @@ -60,7 +96,7 @@ Error ImageLoader::load_image(String p_file, Ref<Image> p_image, Ref<FileAccess> if (!loader[i]->recognize(extension)) { continue; } - Error err = loader[i]->load_image(p_image, f, p_force_linear, p_scale); + Error err = loader.write[i]->load_image(p_image, f, p_flags, p_scale); if (err != OK) { ERR_PRINT("Error loading image: " + p_file); } @@ -79,7 +115,7 @@ void ImageLoader::get_recognized_extensions(List<String> *p_extensions) { } } -ImageFormatLoader *ImageLoader::recognize(const String &p_extension) { +Ref<ImageFormatLoader> ImageLoader::recognize(const String &p_extension) { for (int i = 0; i < loader.size(); i++) { if (loader[i]->recognize(p_extension)) { return loader[i]; @@ -89,17 +125,17 @@ ImageFormatLoader *ImageLoader::recognize(const String &p_extension) { return nullptr; } -Vector<ImageFormatLoader *> ImageLoader::loader; +Vector<Ref<ImageFormatLoader>> ImageLoader::loader; -void ImageLoader::add_image_format_loader(ImageFormatLoader *p_loader) { +void ImageLoader::add_image_format_loader(Ref<ImageFormatLoader> p_loader) { loader.push_back(p_loader); } -void ImageLoader::remove_image_format_loader(ImageFormatLoader *p_loader) { +void ImageLoader::remove_image_format_loader(Ref<ImageFormatLoader> p_loader) { loader.erase(p_loader); } -const Vector<ImageFormatLoader *> &ImageLoader::get_image_format_loaders() { +const Vector<Ref<ImageFormatLoader>> &ImageLoader::get_image_format_loaders() { return loader; } @@ -152,7 +188,7 @@ Ref<Resource> ResourceFormatLoaderImage::load(const String &p_path, const String Ref<Image> image; image.instantiate(); - Error err = ImageLoader::loader[idx]->load_image(image, f, false, 1.0); + Error err = ImageLoader::loader.write[idx]->load_image(image, f); if (err != OK) { if (r_error) { diff --git a/core/io/image_loader.h b/core/io/image_loader.h index c91d382c25..f70fdf22aa 100644 --- a/core/io/image_loader.h +++ b/core/io/image_loader.h @@ -31,20 +31,34 @@ #ifndef IMAGE_LOADER_H #define IMAGE_LOADER_H +#include "core/core_bind.h" #include "core/io/file_access.h" #include "core/io/image.h" #include "core/io/resource_loader.h" +#include "core/object/gdvirtual.gen.inc" #include "core/string/ustring.h" #include "core/templates/list.h" +#include "core/variant/binder_common.h" class ImageLoader; -class ImageFormatLoader { +class ImageFormatLoader : public RefCounted { + GDCLASS(ImageFormatLoader, RefCounted); + friend class ImageLoader; friend class ResourceFormatLoaderImage; +public: + enum LoaderFlags { + FLAG_NONE = 0, + FLAG_FORCE_LINEAR = 1, + FLAG_CONVERT_COLORS = 2, + }; + protected: - virtual Error load_image(Ref<Image> p_image, Ref<FileAccess> p_fileaccess, bool p_force_linear, float p_scale) = 0; + static void _bind_methods(); + + virtual Error load_image(Ref<Image> p_image, Ref<FileAccess> p_fileaccess, BitField<ImageFormatLoader::LoaderFlags> p_flags = FLAG_NONE, float p_scale = 1.0) = 0; virtual void get_recognized_extensions(List<String> *p_extensions) const = 0; bool recognize(const String &p_extension) const; @@ -52,20 +66,39 @@ public: virtual ~ImageFormatLoader() {} }; +VARIANT_BITFIELD_CAST(ImageFormatLoader::LoaderFlags); + +class ImageFormatLoaderExtension : public ImageFormatLoader { + GDCLASS(ImageFormatLoaderExtension, ImageFormatLoader); + +protected: + static void _bind_methods(); + +public: + virtual Error load_image(Ref<Image> p_image, Ref<FileAccess> p_fileaccess, BitField<ImageFormatLoader::LoaderFlags> p_flags = FLAG_NONE, float p_scale = 1.0) override; + virtual void get_recognized_extensions(List<String> *p_extensions) const override; + + void add_format_loader(); + void remove_format_loader(); + + GDVIRTUAL0RC(PackedStringArray, _get_recognized_extensions); + GDVIRTUAL4R(Error, _load_image, Ref<Image>, Ref<FileAccess>, BitField<ImageFormatLoader::LoaderFlags>, float); +}; + class ImageLoader { - static Vector<ImageFormatLoader *> loader; + static Vector<Ref<ImageFormatLoader>> loader; friend class ResourceFormatLoaderImage; protected: public: - static Error load_image(String p_file, Ref<Image> p_image, Ref<FileAccess> p_custom = Ref<FileAccess>(), bool p_force_linear = false, float p_scale = 1.0); + static Error load_image(String p_file, Ref<Image> p_image, Ref<FileAccess> p_custom = Ref<FileAccess>(), BitField<ImageFormatLoader::LoaderFlags> p_flags = ImageFormatLoader::FLAG_NONE, float p_scale = 1.0); static void get_recognized_extensions(List<String> *p_extensions); - static ImageFormatLoader *recognize(const String &p_extension); + static Ref<ImageFormatLoader> recognize(const String &p_extension); - static void add_image_format_loader(ImageFormatLoader *p_loader); - static void remove_image_format_loader(ImageFormatLoader *p_loader); + static void add_image_format_loader(Ref<ImageFormatLoader> p_loader); + static void remove_image_format_loader(Ref<ImageFormatLoader> p_loader); - static const Vector<ImageFormatLoader *> &get_image_format_loaders(); + static const Vector<Ref<ImageFormatLoader>> &get_image_format_loaders(); static void cleanup(); }; diff --git a/core/io/ip.cpp b/core/io/ip.cpp index 25e3bef5fc..a4d8dc3d5b 100644 --- a/core/io/ip.cpp +++ b/core/io/ip.cpp @@ -33,6 +33,7 @@ #include "core/os/semaphore.h" #include "core/os/thread.h" #include "core/templates/hash_map.h" +#include "core/variant/typed_array.h" VARIANT_ENUM_CAST(IP::ResolverStatus); @@ -124,11 +125,11 @@ struct _IP_ResolverPrivate { }; IPAddress IP::resolve_hostname(const String &p_hostname, IP::Type p_type) { - const Array addresses = resolve_hostname_addresses(p_hostname, p_type); - return addresses.size() ? addresses[0].operator IPAddress() : IPAddress(); + const PackedStringArray addresses = resolve_hostname_addresses(p_hostname, p_type); + return addresses.size() ? (IPAddress)addresses[0] : IPAddress(); } -Array IP::resolve_hostname_addresses(const String &p_hostname, Type p_type) { +PackedStringArray IP::resolve_hostname_addresses(const String &p_hostname, Type p_type) { List<IPAddress> res; String key = _IP_ResolverPrivate::get_cache_key(p_hostname, p_type); @@ -148,7 +149,7 @@ Array IP::resolve_hostname_addresses(const String &p_hostname, Type p_type) { } resolver->mutex.unlock(); - Array result; + PackedStringArray result; for (int i = 0; i < res.size(); ++i) { result.push_back(String(res[i])); } @@ -254,8 +255,8 @@ void IP::clear_cache(const String &p_hostname) { } } -Array IP::_get_local_addresses() const { - Array addresses; +PackedStringArray IP::_get_local_addresses() const { + PackedStringArray addresses; List<IPAddress> ip_addresses; get_local_addresses(&ip_addresses); for (const IPAddress &E : ip_addresses) { @@ -265,8 +266,8 @@ Array IP::_get_local_addresses() const { return addresses; } -Array IP::_get_local_interfaces() const { - Array results; +TypedArray<Dictionary> IP::_get_local_interfaces() const { + TypedArray<Dictionary> results; HashMap<String, Interface_Info> interfaces; get_local_interfaces(&interfaces); for (KeyValue<String, Interface_Info> &E : interfaces) { diff --git a/core/io/ip.h b/core/io/ip.h index 4d83515e2b..f2d93a454d 100644 --- a/core/io/ip.h +++ b/core/io/ip.h @@ -34,6 +34,9 @@ #include "core/io/ip_address.h" #include "core/os/os.h" +template <typename T> +class TypedArray; + struct _IP_ResolverPrivate; class IP : public Object { @@ -68,8 +71,8 @@ protected: static IP *singleton; static void _bind_methods(); - Array _get_local_addresses() const; - Array _get_local_interfaces() const; + PackedStringArray _get_local_addresses() const; + TypedArray<Dictionary> _get_local_interfaces() const; static IP *(*_create)(); @@ -82,7 +85,7 @@ public: }; IPAddress resolve_hostname(const String &p_hostname, Type p_type = TYPE_ANY); - Array resolve_hostname_addresses(const String &p_hostname, Type p_type = TYPE_ANY); + PackedStringArray resolve_hostname_addresses(const String &p_hostname, Type p_type = TYPE_ANY); // async resolver hostname ResolverID resolve_hostname_queue_item(const String &p_hostname, Type p_type = TYPE_ANY); ResolverStatus get_resolve_item_status(ResolverID p_id) const; diff --git a/core/io/json.cpp b/core/io/json.cpp index 4c4d91f851..7e267d35d4 100644 --- a/core/io/json.cpp +++ b/core/io/json.cpp @@ -56,6 +56,8 @@ String JSON::_make_indent(const String &p_indent, int p_size) { } String JSON::_stringify(const Variant &p_var, const String &p_indent, int p_cur_indent, bool p_sort_keys, HashSet<const void *> &p_markers, bool p_full_precision) { + ERR_FAIL_COND_V_MSG(p_cur_indent > Variant::MAX_RECURSION_DEPTH, "...", "JSON structure is too deep. Bailing."); + String colon = ":"; String end_statement = ""; @@ -357,17 +359,22 @@ Error JSON::_get_token(const char32_t *p_str, int &index, int p_len, Token &r_to return ERR_PARSE_ERROR; } -Error JSON::_parse_value(Variant &value, Token &token, const char32_t *p_str, int &index, int p_len, int &line, String &r_err_str) { +Error JSON::_parse_value(Variant &value, Token &token, const char32_t *p_str, int &index, int p_len, int &line, int p_depth, String &r_err_str) { + if (p_depth > Variant::MAX_RECURSION_DEPTH) { + r_err_str = "JSON structure is too deep. Bailing."; + return ERR_OUT_OF_MEMORY; + } + if (token.type == TK_CURLY_BRACKET_OPEN) { Dictionary d; - Error err = _parse_object(d, p_str, index, p_len, line, r_err_str); + Error err = _parse_object(d, p_str, index, p_len, line, p_depth + 1, r_err_str); if (err) { return err; } value = d; } else if (token.type == TK_BRACKET_OPEN) { Array a; - Error err = _parse_array(a, p_str, index, p_len, line, r_err_str); + Error err = _parse_array(a, p_str, index, p_len, line, p_depth + 1, r_err_str); if (err) { return err; } @@ -396,7 +403,7 @@ Error JSON::_parse_value(Variant &value, Token &token, const char32_t *p_str, in return OK; } -Error JSON::_parse_array(Array &array, const char32_t *p_str, int &index, int p_len, int &line, String &r_err_str) { +Error JSON::_parse_array(Array &array, const char32_t *p_str, int &index, int p_len, int &line, int p_depth, String &r_err_str) { Token token; bool need_comma = false; @@ -421,7 +428,7 @@ Error JSON::_parse_array(Array &array, const char32_t *p_str, int &index, int p_ } Variant v; - err = _parse_value(v, token, p_str, index, p_len, line, r_err_str); + err = _parse_value(v, token, p_str, index, p_len, line, p_depth, r_err_str); if (err) { return err; } @@ -434,7 +441,7 @@ Error JSON::_parse_array(Array &array, const char32_t *p_str, int &index, int p_ return ERR_PARSE_ERROR; } -Error JSON::_parse_object(Dictionary &object, const char32_t *p_str, int &index, int p_len, int &line, String &r_err_str) { +Error JSON::_parse_object(Dictionary &object, const char32_t *p_str, int &index, int p_len, int &line, int p_depth, String &r_err_str) { bool at_key = true; String key; Token token; @@ -483,7 +490,7 @@ Error JSON::_parse_object(Dictionary &object, const char32_t *p_str, int &index, } Variant v; - err = _parse_value(v, token, p_str, index, p_len, line, r_err_str); + err = _parse_value(v, token, p_str, index, p_len, line, p_depth, r_err_str); if (err) { return err; } @@ -497,6 +504,10 @@ Error JSON::_parse_object(Dictionary &object, const char32_t *p_str, int &index, return ERR_PARSE_ERROR; } +void JSON::set_data(const Variant &p_data) { + data = p_data; +} + Error JSON::_parse_string(const String &p_json, Variant &r_ret, String &r_err_str, int &r_err_line) { const char32_t *str = p_json.ptr(); int idx = 0; @@ -510,7 +521,7 @@ Error JSON::_parse_string(const String &p_json, Variant &r_ret, String &r_err_st return err; } - err = _parse_value(r_ret, token, str, idx, len, r_err_line, r_err_str); + err = _parse_value(r_ret, token, str, idx, len, r_err_line, 0, r_err_str); // Check if EOF is reached // or it's a type of the next token. @@ -528,11 +539,6 @@ Error JSON::_parse_string(const String &p_json, Variant &r_ret, String &r_err_st return err; } -String JSON::stringify(const Variant &p_var, const String &p_indent, bool p_sort_keys, bool p_full_precision) { - HashSet<const void *> markers; - return _stringify(p_var, p_indent, 0, p_sort_keys, markers, p_full_precision); -} - Error JSON::parse(const String &p_json_string) { Error err = _parse_string(p_json_string, data, err_str, err_line); if (err == Error::OK) { @@ -541,11 +547,109 @@ Error JSON::parse(const String &p_json_string) { return err; } +String JSON::stringify(const Variant &p_var, const String &p_indent, bool p_sort_keys, bool p_full_precision) { + Ref<JSON> jason; + jason.instantiate(); + HashSet<const void *> markers; + return jason->_stringify(p_var, p_indent, 0, p_sort_keys, markers, p_full_precision); +} + +Variant JSON::parse_string(const String &p_json_string) { + Ref<JSON> jason; + jason.instantiate(); + Error error = jason->parse(p_json_string); + ERR_FAIL_COND_V_MSG(error != Error::OK, Variant(), vformat("Parse JSON failed. Error at line %d: %s", jason->get_error_line(), jason->get_error_message())); + return jason->get_data(); +} + void JSON::_bind_methods() { - ClassDB::bind_method(D_METHOD("stringify", "data", "indent", "sort_keys", "full_precision"), &JSON::stringify, DEFVAL(""), DEFVAL(true), DEFVAL(false)); + ClassDB::bind_static_method("JSON", D_METHOD("stringify", "data", "indent", "sort_keys", "full_precision"), &JSON::stringify, DEFVAL(""), DEFVAL(true), DEFVAL(false)); + ClassDB::bind_static_method("JSON", D_METHOD("parse_string", "json_string"), &JSON::parse_string); ClassDB::bind_method(D_METHOD("parse", "json_string"), &JSON::parse); ClassDB::bind_method(D_METHOD("get_data"), &JSON::get_data); + ClassDB::bind_method(D_METHOD("set_data", "data"), &JSON::set_data); ClassDB::bind_method(D_METHOD("get_error_line"), &JSON::get_error_line); ClassDB::bind_method(D_METHOD("get_error_message"), &JSON::get_error_message); + + ADD_PROPERTY(PropertyInfo(Variant::NIL, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NIL_IS_VARIANT), "set_data", "get_data"); // Ensures that it can be serialized as binary. +} + +//// + +//////////// + +Ref<Resource> ResourceFormatLoaderJSON::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) { + if (r_error) { + *r_error = ERR_FILE_CANT_OPEN; + } + + if (!FileAccess::exists(p_path)) { + *r_error = ERR_FILE_NOT_FOUND; + return Ref<Resource>(); + } + + Ref<JSON> json; + json.instantiate(); + + Error err = json->parse(FileAccess::get_file_as_string(p_path)); + if (err != OK) { + if (r_error) { + *r_error = err; + } + ERR_PRINT("Error parsing JSON file at '" + p_path + "', on line " + itos(json->get_error_line()) + ": " + json->get_error_message()); + return Ref<Resource>(); + } + + if (r_error) { + *r_error = OK; + } + + return json; +} + +void ResourceFormatLoaderJSON::get_recognized_extensions(List<String> *p_extensions) const { + p_extensions->push_back("json"); +} + +bool ResourceFormatLoaderJSON::handles_type(const String &p_type) const { + return (p_type == "JSON"); +} + +String ResourceFormatLoaderJSON::get_resource_type(const String &p_path) const { + String el = p_path.get_extension().to_lower(); + if (el == "json") { + return "JSON"; + } + return ""; +} + +Error ResourceFormatSaverJSON::save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags) { + Ref<JSON> json = p_resource; + ERR_FAIL_COND_V(json.is_null(), ERR_INVALID_PARAMETER); + + String source = JSON::stringify(json->get_data(), "\t", false, true); + + Error err; + Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::WRITE, &err); + + ERR_FAIL_COND_V_MSG(err, err, "Cannot save json '" + p_path + "'."); + + file->store_string(source); + if (file->get_error() != OK && file->get_error() != ERR_FILE_EOF) { + return ERR_CANT_CREATE; + } + + return OK; +} + +void ResourceFormatSaverJSON::get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const { + Ref<JSON> json = p_resource; + if (json.is_valid()) { + p_extensions->push_back("json"); + } +} + +bool ResourceFormatSaverJSON::recognize(const Ref<Resource> &p_resource) const { + return p_resource->get_class_name() == "JSON"; //only json, not inherited } diff --git a/core/io/json.h b/core/io/json.h index 6ba0a8c76b..829a5f922b 100644 --- a/core/io/json.h +++ b/core/io/json.h @@ -31,7 +31,9 @@ #ifndef JSON_H #define JSON_H -#include "core/object/ref_counted.h" +#include "core/io/resource.h" +#include "core/io/resource_loader.h" +#include "core/io/resource_saver.h" #include "core/variant/variant.h" class JSON : public RefCounted { @@ -72,21 +74,38 @@ class JSON : public RefCounted { static String _make_indent(const String &p_indent, int p_size); static String _stringify(const Variant &p_var, const String &p_indent, int p_cur_indent, bool p_sort_keys, HashSet<const void *> &p_markers, bool p_full_precision = false); static Error _get_token(const char32_t *p_str, int &index, int p_len, Token &r_token, int &line, String &r_err_str); - static Error _parse_value(Variant &value, Token &token, const char32_t *p_str, int &index, int p_len, int &line, String &r_err_str); - static Error _parse_array(Array &array, const char32_t *p_str, int &index, int p_len, int &line, String &r_err_str); - static Error _parse_object(Dictionary &object, const char32_t *p_str, int &index, int p_len, int &line, String &r_err_str); + static Error _parse_value(Variant &value, Token &token, const char32_t *p_str, int &index, int p_len, int &line, int p_depth, String &r_err_str); + static Error _parse_array(Array &array, const char32_t *p_str, int &index, int p_len, int &line, int p_depth, String &r_err_str); + static Error _parse_object(Dictionary &object, const char32_t *p_str, int &index, int p_len, int &line, int p_depth, String &r_err_str); static Error _parse_string(const String &p_json, Variant &r_ret, String &r_err_str, int &r_err_line); protected: static void _bind_methods(); public: - String stringify(const Variant &p_var, const String &p_indent = "", bool p_sort_keys = true, bool p_full_precision = false); Error parse(const String &p_json_string); + static String stringify(const Variant &p_var, const String &p_indent = "", bool p_sort_keys = true, bool p_full_precision = false); + static Variant parse_string(const String &p_json_string); inline Variant get_data() const { return data; } + void set_data(const Variant &p_data); inline int get_error_line() const { return err_line; } inline String get_error_message() const { return err_str; } }; +class ResourceFormatLoaderJSON : public ResourceFormatLoader { +public: + virtual Ref<Resource> load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE); + virtual void get_recognized_extensions(List<String> *p_extensions) const; + virtual bool handles_type(const String &p_type) const; + virtual String get_resource_type(const String &p_path) const; +}; + +class ResourceFormatSaverJSON : public ResourceFormatSaver { +public: + virtual Error save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags = 0); + virtual void get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const; + virtual bool recognize(const Ref<Resource> &p_resource) const; +}; + #endif // JSON_H diff --git a/core/io/logger.cpp b/core/io/logger.cpp index b0f74f8db5..288e53d075 100644 --- a/core/io/logger.cpp +++ b/core/io/logger.cpp @@ -87,7 +87,7 @@ void Logger::log_error(const char *p_function, const char *p_file, int p_line, c } else { logf_error("USER %s: %s\n", err_type, err_details); } - logf_error(" at: %s (%s:%i) - %s\n", p_function, p_file, p_line, p_code); + logf_error(" at: %s (%s:%i)\n", p_function, p_file, p_line); } void Logger::logf(const char *p_format, ...) { diff --git a/core/io/marshalls.cpp b/core/io/marshalls.cpp index 2f69c10218..9ba653e1a9 100644 --- a/core/io/marshalls.cpp +++ b/core/io/marshalls.cpp @@ -503,17 +503,17 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int ERR_FAIL_COND_V((size_t)len < sizeof(double) * 16, ERR_INVALID_DATA); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { - val.matrix[i][j] = decode_double(&buf[(i * 4 + j) * sizeof(double)]); + val.columns[i][j] = decode_double(&buf[(i * 4 + j) * sizeof(double)]); } } if (r_len) { (*r_len) += sizeof(double) * 16; } } else { - ERR_FAIL_COND_V((size_t)len < sizeof(float) * 62, ERR_INVALID_DATA); + ERR_FAIL_COND_V((size_t)len < sizeof(float) * 16, ERR_INVALID_DATA); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { - val.matrix[i][j] = decode_float(&buf[(i * 4 + j) * sizeof(float)]); + val.columns[i][j] = decode_float(&buf[(i * 4 + j) * sizeof(float)]); } } @@ -1450,7 +1450,7 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo Projection val = p_variant; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { - memcpy(&buf[(i * 4 + j) * sizeof(real_t)], &val.matrix[i][j], sizeof(real_t)); + memcpy(&buf[(i * 4 + j) * sizeof(real_t)], &val.columns[i][j], sizeof(real_t)); } } } diff --git a/core/io/packet_peer.cpp b/core/io/packet_peer.cpp index 0af236f766..1df779d034 100644 --- a/core/io/packet_peer.cpp +++ b/core/io/packet_peer.cpp @@ -152,42 +152,24 @@ void PacketPeer::_bind_methods() { /***************/ -int PacketPeerExtension::get_available_packet_count() const { - int count; - if (GDVIRTUAL_CALL(_get_available_packet_count, count)) { - return count; - } - WARN_PRINT_ONCE("PacketPeerExtension::_get_available_packet_count is unimplemented!"); - return -1; -} - Error PacketPeerExtension::get_packet(const uint8_t **r_buffer, int &r_buffer_size) { - int err; + Error err; if (GDVIRTUAL_CALL(_get_packet, r_buffer, &r_buffer_size, err)) { - return (Error)err; + return err; } WARN_PRINT_ONCE("PacketPeerExtension::_get_packet_native is unimplemented!"); return FAILED; } Error PacketPeerExtension::put_packet(const uint8_t *p_buffer, int p_buffer_size) { - int err; + Error err; if (GDVIRTUAL_CALL(_put_packet, p_buffer, p_buffer_size, err)) { - return (Error)err; + return err; } WARN_PRINT_ONCE("PacketPeerExtension::_put_packet_native is unimplemented!"); return FAILED; } -int PacketPeerExtension::get_max_packet_size() const { - int size; - if (GDVIRTUAL_CALL(_get_max_packet_size, size)) { - return size; - } - WARN_PRINT_ONCE("PacketPeerExtension::_get_max_packet_size is unimplemented!"); - return 0; -} - void PacketPeerExtension::_bind_methods() { GDVIRTUAL_BIND(_get_packet, "r_buffer", "r_buffer_size"); GDVIRTUAL_BIND(_put_packet, "p_buffer", "p_buffer_size"); diff --git a/core/io/packet_peer.h b/core/io/packet_peer.h index ec9d33aa5a..07045e62a6 100644 --- a/core/io/packet_peer.h +++ b/core/io/packet_peer.h @@ -35,6 +35,7 @@ #include "core/object/class_db.h" #include "core/templates/ring_buffer.h" +#include "core/extension/ext_wrappers.gen.inc" #include "core/object/gdvirtual.gen.inc" #include "core/object/script_language.h" #include "core/variant/native_ptr.h" @@ -84,16 +85,14 @@ protected: static void _bind_methods(); public: - virtual int get_available_packet_count() const override; virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override; ///< buffer is GONE after next get_packet + GDVIRTUAL2R(Error, _get_packet, GDNativeConstPtr<const uint8_t *>, GDNativePtr<int>); + virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override; - virtual int get_max_packet_size() const override; + GDVIRTUAL2R(Error, _put_packet, GDNativeConstPtr<const uint8_t>, int); - /* GDExtension */ - GDVIRTUAL0RC(int, _get_available_packet_count); - GDVIRTUAL2R(int, _get_packet, GDNativeConstPtr<const uint8_t *>, GDNativePtr<int>); - GDVIRTUAL2R(int, _put_packet, GDNativeConstPtr<const uint8_t>, int); - GDVIRTUAL0RC(int, _get_max_packet_size); + EXBIND0RC(int, get_available_packet_count); + EXBIND0RC(int, get_max_packet_size); }; class PacketPeerStream : public PacketPeer { diff --git a/core/io/pck_packer.cpp b/core/io/pck_packer.cpp index aa1b323db2..0118b4c6af 100644 --- a/core/io/pck_packer.cpp +++ b/core/io/pck_packer.cpp @@ -107,6 +107,8 @@ Error PCKPacker::pck_start(const String &p_file, int p_alignment, const String & } Error PCKPacker::add_file(const String &p_file, const String &p_src, bool p_encrypt) { + ERR_FAIL_COND_V_MSG(file.is_null(), ERR_INVALID_PARAMETER, "File must be opened before use."); + Ref<FileAccess> f = FileAccess::open(p_src, FileAccess::READ); if (f.is_null()) { return ERR_FILE_CANT_OPEN; diff --git a/core/io/resource.cpp b/core/io/resource.cpp index fec5ca5c7b..ab30fb1ca3 100644 --- a/core/io/resource.cpp +++ b/core/io/resource.cpp @@ -93,15 +93,14 @@ String Resource::get_path() const { String Resource::generate_scene_unique_id() { // Generate a unique enough hash, but still user-readable. // If it's not unique it does not matter because the saver will try again. - OS::Date date = OS::get_singleton()->get_date(); - OS::Time time = OS::get_singleton()->get_time(); + OS::DateTime dt = OS::get_singleton()->get_datetime(); uint32_t hash = hash_murmur3_one_32(OS::get_singleton()->get_ticks_usec()); - hash = hash_murmur3_one_32(date.year, hash); - hash = hash_murmur3_one_32(date.month, hash); - hash = hash_murmur3_one_32(date.day, hash); - hash = hash_murmur3_one_32(time.hour, hash); - hash = hash_murmur3_one_32(time.minute, hash); - hash = hash_murmur3_one_32(time.second, hash); + hash = hash_murmur3_one_32(dt.year, hash); + hash = hash_murmur3_one_32(dt.month, hash); + hash = hash_murmur3_one_32(dt.day, hash); + hash = hash_murmur3_one_32(dt.hour, hash); + hash = hash_murmur3_one_32(dt.minute, hash); + hash = hash_murmur3_one_32(dt.second, hash); hash = hash_murmur3_one_32(Math::rand(), hash); static constexpr uint32_t characters = 5; @@ -490,7 +489,7 @@ bool ResourceCache::has(const String &p_path) { Resource **res = resources.getptr(p_path); - if (res && (*res)->reference_get_count() == 0) { + if (res && (*res)->get_reference_count() == 0) { // This resource is in the process of being deleted, ignore its existence. (*res)->path_cache = String(); resources.erase(p_path); @@ -543,43 +542,3 @@ int ResourceCache::get_cached_resource_count() { return rc; } - -void ResourceCache::dump(const char *p_file, bool p_short) { -#ifdef DEBUG_ENABLED - lock.lock(); - - HashMap<String, int> type_count; - - Ref<FileAccess> f; - if (p_file) { - f = FileAccess::open(String::utf8(p_file), FileAccess::WRITE); - ERR_FAIL_COND_MSG(f.is_null(), "Cannot create file at path '" + String::utf8(p_file) + "'."); - } - - for (KeyValue<String, Resource *> &E : resources) { - Resource *r = E.value; - - if (!type_count.has(r->get_class())) { - type_count[r->get_class()] = 0; - } - - type_count[r->get_class()]++; - - if (!p_short) { - if (f.is_valid()) { - f->store_line(r->get_class() + ": " + r->get_path()); - } - } - } - - for (const KeyValue<String, int> &E : type_count) { - if (f.is_valid()) { - f->store_line(E.key + " count: " + itos(E.value)); - } - } - - lock.unlock(); -#else - WARN_PRINT("ResourceCache::dump only with in debug builds."); -#endif -} diff --git a/core/io/resource.h b/core/io/resource.h index a2cde87990..a76a3920be 100644 --- a/core/io/resource.h +++ b/core/io/resource.h @@ -167,7 +167,6 @@ public: static void reload_externals(); static bool has(const String &p_path); static Ref<Resource> get_ref(const String &p_path); - static void dump(const char *p_file = nullptr, bool p_short = false); static void get_cached_resources(List<Ref<Resource>> *p_resources); static int get_cached_resource_count(); }; diff --git a/core/io/resource_format_binary.cpp b/core/io/resource_format_binary.cpp index b731608b4f..4611528db7 100644 --- a/core/io/resource_format_binary.cpp +++ b/core/io/resource_format_binary.cpp @@ -107,7 +107,7 @@ void ResourceLoaderBinary::_advance_padding(uint32_t p_len) { static Error read_reals(real_t *dst, Ref<FileAccess> &f, size_t count) { if (f->real_is_double) { - if (sizeof(real_t) == 8) { + if constexpr (sizeof(real_t) == 8) { // Ideal case with double-precision f->get_buffer((uint8_t *)dst, count * sizeof(double)); #ifdef BIG_ENDIAN_ENABLED @@ -118,7 +118,7 @@ static Error read_reals(real_t *dst, Ref<FileAccess> &f, size_t count) { } } #endif - } else if (sizeof(real_t) == 4) { + } else if constexpr (sizeof(real_t) == 4) { // May be slower, but this is for compatibility. Eventually the data should be converted. for (size_t i = 0; i < count; ++i) { dst[i] = f->get_double(); @@ -127,7 +127,7 @@ static Error read_reals(real_t *dst, Ref<FileAccess> &f, size_t count) { ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "real_t size is neither 4 nor 8!"); } } else { - if (sizeof(real_t) == 4) { + if constexpr (sizeof(real_t) == 4) { // Ideal case with float-precision f->get_buffer((uint8_t *)dst, count * sizeof(float)); #ifdef BIG_ENDIAN_ENABLED @@ -138,7 +138,7 @@ static Error read_reals(real_t *dst, Ref<FileAccess> &f, size_t count) { } } #endif - } else if (sizeof(real_t) == 8) { + } else if constexpr (sizeof(real_t) == 8) { for (size_t i = 0; i < count; ++i) { dst[i] = f->get_float(); } @@ -169,10 +169,10 @@ StringName ResourceLoaderBinary::_get_string() { } Error ResourceLoaderBinary::parse_variant(Variant &r_v) { - uint32_t type = f->get_32(); - print_bl("find property of type: " + itos(type)); + uint32_t prop_type = f->get_32(); + print_bl("find property of type: " + itos(prop_type)); - switch (type) { + switch (prop_type) { case VARIANT_NIL: { r_v = Variant(); } break; @@ -327,22 +327,22 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) { } break; case VARIANT_PROJECTION: { Projection v; - v.matrix[0].x = f->get_real(); - v.matrix[0].y = f->get_real(); - v.matrix[0].z = f->get_real(); - v.matrix[0].w = f->get_real(); - v.matrix[1].x = f->get_real(); - v.matrix[1].y = f->get_real(); - v.matrix[1].z = f->get_real(); - v.matrix[1].w = f->get_real(); - v.matrix[2].x = f->get_real(); - v.matrix[2].y = f->get_real(); - v.matrix[2].z = f->get_real(); - v.matrix[2].w = f->get_real(); - v.matrix[3].x = f->get_real(); - v.matrix[3].y = f->get_real(); - v.matrix[3].z = f->get_real(); - v.matrix[3].w = f->get_real(); + v.columns[0].x = f->get_real(); + v.columns[0].y = f->get_real(); + v.columns[0].z = f->get_real(); + v.columns[0].w = f->get_real(); + v.columns[1].x = f->get_real(); + v.columns[1].y = f->get_real(); + v.columns[1].z = f->get_real(); + v.columns[1].w = f->get_real(); + v.columns[2].x = f->get_real(); + v.columns[2].y = f->get_real(); + v.columns[2].z = f->get_real(); + v.columns[2].w = f->get_real(); + v.columns[3].x = f->get_real(); + v.columns[3].y = f->get_real(); + v.columns[3].z = f->get_real(); + v.columns[3].w = f->get_real(); r_v = v; } break; case VARIANT_COLOR: { @@ -421,7 +421,7 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) { if (!path.contains("://") && path.is_relative_path()) { // path is relative to file being loaded, so convert to a resource path - path = ProjectSettings::get_singleton()->localize_path(res_path.get_base_dir().plus_file(path)); + path = ProjectSettings::get_singleton()->localize_path(res_path.get_base_dir().path_join(path)); } if (remaps.find(path)) { @@ -683,7 +683,7 @@ Error ResourceLoaderBinary::load() { if (!path.contains("://") && path.is_relative_path()) { // path is relative to file being loaded, so convert to a resource path - path = ProjectSettings::get_singleton()->localize_path(path.get_base_dir().plus_file(external_resources[i].path)); + path = ProjectSettings::get_singleton()->localize_path(path.get_base_dir().path_join(external_resources[i].path)); } external_resources.write[i].path = path; //remap happens here, not on load because on load it can actually be used for filesystem dock resource remap @@ -1042,7 +1042,14 @@ void ResourceLoaderBinary::open(Ref<FileAccess> p_f, bool p_no_resources, bool p // If a UID is found and the path is valid, it will be used, otherwise, it falls back to the path. er.path = ResourceUID::get_singleton()->get_id_path(er.uid); } else { +#ifdef TOOLS_ENABLED + // Silence a warning that can happen during the initial filesystem scan due to cache being regenerated. + if (ResourceLoader::get_resource_uid(res_path) != er.uid) { + WARN_PRINT(String(res_path + ": In external resource #" + itos(i) + ", invalid UUID: " + ResourceUID::get_singleton()->id_to_text(er.uid) + " - using text path instead: " + er.path).utf8().get_data()); + } +#else WARN_PRINT(String(res_path + ": In external resource #" + itos(i) + ", invalid UUID: " + ResourceUID::get_singleton()->id_to_text(er.uid) + " - using text path instead: " + er.path).utf8().get_data()); +#endif } } } @@ -1100,16 +1107,14 @@ String ResourceLoaderBinary::recognize(Ref<FileAccess> p_f) { uint32_t ver_major = f->get_32(); f->get_32(); // ver_minor - uint32_t ver_format = f->get_32(); + uint32_t ver_fmt = f->get_32(); - if (ver_format > FORMAT_VERSION || ver_major > VERSION_MAJOR) { + if (ver_fmt > FORMAT_VERSION || ver_major > VERSION_MAJOR) { f.unref(); return ""; } - String type = get_unicode_string(); - - return type; + return get_unicode_string(); } Ref<Resource> ResourceFormatLoaderBinary::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) { @@ -1206,7 +1211,7 @@ Error ResourceFormatLoaderBinary::rename_dependencies(const String &p_path, cons Ref<FileAccessCompressed> facw; facw.instantiate(); facw->configure("RSCC"); - err = facw->_open(p_path + ".depren", FileAccess::WRITE); + err = facw->open_internal(p_path + ".depren", FileAccess::WRITE); ERR_FAIL_COND_V_MSG(err, ERR_FILE_CORRUPT, "Cannot create file '" + p_path + ".depren'."); fw = facw; @@ -1329,7 +1334,7 @@ Error ResourceFormatLoaderBinary::rename_dependencies(const String &p_path, cons bool relative = false; if (!path.begins_with("res://")) { - path = local_path.plus_file(path).simplify_path(); + path = local_path.path_join(path).simplify_path(); relative = true; } @@ -1630,22 +1635,22 @@ void ResourceFormatSaverBinaryInstance::write_variant(Ref<FileAccess> f, const V case Variant::PROJECTION: { f->store_32(VARIANT_PROJECTION); Projection val = p_property; - f->store_real(val.matrix[0].x); - f->store_real(val.matrix[0].y); - f->store_real(val.matrix[0].z); - f->store_real(val.matrix[0].w); - f->store_real(val.matrix[1].x); - f->store_real(val.matrix[1].y); - f->store_real(val.matrix[1].z); - f->store_real(val.matrix[1].w); - f->store_real(val.matrix[2].x); - f->store_real(val.matrix[2].y); - f->store_real(val.matrix[2].z); - f->store_real(val.matrix[2].w); - f->store_real(val.matrix[3].x); - f->store_real(val.matrix[3].y); - f->store_real(val.matrix[3].z); - f->store_real(val.matrix[3].w); + f->store_real(val.columns[0].x); + f->store_real(val.columns[0].y); + f->store_real(val.columns[0].z); + f->store_real(val.columns[0].w); + f->store_real(val.columns[1].x); + f->store_real(val.columns[1].y); + f->store_real(val.columns[1].z); + f->store_real(val.columns[1].w); + f->store_real(val.columns[2].x); + f->store_real(val.columns[2].y); + f->store_real(val.columns[2].z); + f->store_real(val.columns[2].w); + f->store_real(val.columns[3].x); + f->store_real(val.columns[3].y); + f->store_real(val.columns[3].z); + f->store_real(val.columns[3].w); } break; case Variant::COLOR: { @@ -1986,7 +1991,7 @@ Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const Ref<Re fac.instantiate(); fac->configure("RSCC"); f = fac; - err = fac->_open(p_path, FileAccess::WRITE); + err = fac->open_internal(p_path, FileAccess::WRITE); } else { f = FileAccess::open(p_path, FileAccess::WRITE, &err); } @@ -2118,9 +2123,9 @@ Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const Ref<Re for (int i = 0; i < save_order.size(); i++) { save_unicode_string(f, save_order[i]->get_save_class()); - String path = save_order[i]->get_path(); - path = relative_paths ? local_path.path_to_file(path) : path; - save_unicode_string(f, path); + String res_path = save_order[i]->get_path(); + res_path = relative_paths ? local_path.path_to_file(res_path) : res_path; + save_unicode_string(f, res_path); ResourceUID::ID ruid = ResourceSaver::get_resource_id_for_path(save_order[i]->get_path(), false); f->store_64(ruid); } diff --git a/core/io/resource_importer.cpp b/core/io/resource_importer.cpp index e059fc842b..564b37e662 100644 --- a/core/io/resource_importer.cpp +++ b/core/io/resource_importer.cpp @@ -108,6 +108,15 @@ Error ResourceFormatImporter::_get_path_and_type(const String &p_path, PathAndTy } } +#ifdef TOOLS_ENABLED + if (r_path_and_type.metadata && !r_path_and_type.path.is_empty()) { + Dictionary meta = r_path_and_type.metadata; + if (meta.has("has_editor_variant")) { + r_path_and_type.path = r_path_and_type.path.get_basename() + ".editor." + r_path_and_type.path.get_extension(); + } + } +#endif + if (r_path_and_type.path.is_empty() || r_path_and_type.type.is_empty()) { return ERR_FILE_CORRUPT; } @@ -421,7 +430,7 @@ Ref<ResourceImporter> ResourceFormatImporter::get_importer_by_extension(const St } String ResourceFormatImporter::get_import_base_path(const String &p_for_file) const { - return ProjectSettings::get_singleton()->get_imported_files_path().plus_file(p_for_file.get_file() + "-" + p_for_file.md5_text()); + return ProjectSettings::get_singleton()->get_imported_files_path().path_join(p_for_file.get_file() + "-" + p_for_file.md5_text()); } bool ResourceFormatImporter::are_import_settings_valid(const String &p_path) const { diff --git a/core/io/resource_loader.cpp b/core/io/resource_loader.cpp index eccb397e2e..6219ea70e4 100644 --- a/core/io/resource_loader.cpp +++ b/core/io/resource_loader.cpp @@ -49,6 +49,11 @@ Ref<ResourceFormatLoader> ResourceLoader::loader[ResourceLoader::MAX_LOADERS]; int ResourceLoader::loader_count = 0; bool ResourceFormatLoader::recognize_path(const String &p_path, const String &p_for_type) const { + bool ret = false; + if (GDVIRTUAL_CALL(_recognize_path, p_path, p_for_type, ret)) { + return ret; + } + String extension = p_path.get_extension(); List<String> extensions; @@ -68,12 +73,9 @@ bool ResourceFormatLoader::recognize_path(const String &p_path, const String &p_ } bool ResourceFormatLoader::handles_type(const String &p_type) const { - bool success; - if (GDVIRTUAL_CALL(_handles_type, p_type, success)) { - return success; - } - - return false; + bool success = false; + GDVIRTUAL_CALL(_handles_type, p_type, success); + return success; } void ResourceFormatLoader::get_classes_used(const String &p_path, HashSet<StringName> *r_classes) { @@ -93,21 +95,14 @@ void ResourceFormatLoader::get_classes_used(const String &p_path, HashSet<String String ResourceFormatLoader::get_resource_type(const String &p_path) const { String ret; - - if (GDVIRTUAL_CALL(_get_resource_type, p_path, ret)) { - return ret; - } - - return ""; + GDVIRTUAL_CALL(_get_resource_type, p_path, ret); + return ret; } ResourceUID::ID ResourceFormatLoader::get_resource_uid(const String &p_path) const { - int64_t uid; - if (GDVIRTUAL_CALL(_get_resource_uid, p_path, uid)) { - return uid; - } - - return ResourceUID::INVALID_ID; + int64_t uid = ResourceUID::INVALID_ID; + GDVIRTUAL_CALL(_get_resource_uid, p_path, uid); + return uid; } void ResourceFormatLoader::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const { @@ -123,11 +118,11 @@ void ResourceLoader::get_recognized_extensions_for_type(const String &p_type, Li } bool ResourceFormatLoader::exists(const String &p_path) const { - bool success; + bool success = false; if (GDVIRTUAL_CALL(_exists, p_path, success)) { return success; } - return FileAccess::exists(p_path); //by default just check file + return FileAccess::exists(p_path); // By default just check file. } void ResourceFormatLoader::get_recognized_extensions(List<String> *p_extensions) const { @@ -175,12 +170,9 @@ Error ResourceFormatLoader::rename_dependencies(const String &p_path, const Hash deps_dict[E.key] = E.value; } - int64_t err; - if (GDVIRTUAL_CALL(_rename_dependencies, p_path, deps_dict, err)) { - return (Error)err; - } - - return OK; + int64_t err = OK; + GDVIRTUAL_CALL(_rename_dependencies, p_path, deps_dict, err); + return (Error)err; } void ResourceFormatLoader::_bind_methods() { @@ -189,6 +181,7 @@ void ResourceFormatLoader::_bind_methods() { BIND_ENUM_CONSTANT(CACHE_MODE_REPLACE); GDVIRTUAL_BIND(_get_recognized_extensions); + GDVIRTUAL_BIND(_recognize_path, "path", "type"); GDVIRTUAL_BIND(_handles_type, "type"); GDVIRTUAL_BIND(_get_resource_type, "path"); GDVIRTUAL_BIND(_get_resource_uid, "path"); @@ -908,7 +901,7 @@ void ResourceLoader::load_translation_remaps() { return; } - Dictionary remaps = ProjectSettings::get_singleton()->get("internationalization/locale/translation_remaps"); + Dictionary remaps = GLOBAL_GET("internationalization/locale/translation_remaps"); List<Variant> keys; remaps.get_key_list(&keys); for (const Variant &E : keys) { @@ -935,7 +928,7 @@ void ResourceLoader::load_path_remaps() { return; } - Vector<String> remaps = ProjectSettings::get_singleton()->get("path_remap/remapped_paths"); + Vector<String> remaps = GLOBAL_GET("path_remap/remapped_paths"); int rc = remaps.size(); ERR_FAIL_COND(rc & 1); //must be even const String *r = remaps.ptr(); diff --git a/core/io/resource_loader.h b/core/io/resource_loader.h index 91ba930176..243670b2d0 100644 --- a/core/io/resource_loader.h +++ b/core/io/resource_loader.h @@ -51,6 +51,7 @@ protected: static void _bind_methods(); GDVIRTUAL0RC(Vector<String>, _get_recognized_extensions) + GDVIRTUAL2RC(bool, _recognize_path, String, StringName) GDVIRTUAL1RC(bool, _handles_type, StringName) GDVIRTUAL1RC(String, _get_resource_type, String) GDVIRTUAL1RC(ResourceUID::ID, _get_resource_uid, String) diff --git a/core/io/resource_saver.cpp b/core/io/resource_saver.cpp index 274316f058..710afc614f 100644 --- a/core/io/resource_saver.cpp +++ b/core/io/resource_saver.cpp @@ -42,21 +42,15 @@ ResourceSavedCallback ResourceSaver::save_callback = nullptr; ResourceSaverGetResourceIDForPath ResourceSaver::save_get_id_for_path = nullptr; Error ResourceFormatSaver::save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags) { - int64_t res; - if (GDVIRTUAL_CALL(_save, p_resource, p_path, p_flags, res)) { - return (Error)res; - } - - return ERR_METHOD_NOT_FOUND; + int64_t res = ERR_METHOD_NOT_FOUND; + GDVIRTUAL_CALL(_save, p_resource, p_path, p_flags, res); + return (Error)res; } bool ResourceFormatSaver::recognize(const Ref<Resource> &p_resource) const { - bool success; - if (GDVIRTUAL_CALL(_recognize, p_resource, success)) { - return success; - } - - return false; + bool success = false; + GDVIRTUAL_CALL(_recognize, p_resource, success); + return success; } void ResourceFormatSaver::get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const { @@ -69,10 +63,31 @@ void ResourceFormatSaver::get_recognized_extensions(const Ref<Resource> &p_resou } } +bool ResourceFormatSaver::recognize_path(const Ref<Resource> &p_resource, const String &p_path) const { + bool ret = false; + if (GDVIRTUAL_CALL(_recognize_path, p_resource, p_path, ret)) { + return ret; + } + + String extension = p_path.get_extension(); + + List<String> extensions; + get_recognized_extensions(p_resource, &extensions); + + for (const String &E : extensions) { + if (E.nocasecmp_to(extension) == 0) { + return true; + } + } + + return false; +} + void ResourceFormatSaver::_bind_methods() { - GDVIRTUAL_BIND(_save, "path", "resource", "flags"); + GDVIRTUAL_BIND(_save, "resource", "path", "flags"); GDVIRTUAL_BIND(_recognize, "resource"); GDVIRTUAL_BIND(_get_recognized_extensions, "resource"); + GDVIRTUAL_BIND(_recognize_path, "resource", "path"); } Error ResourceSaver::save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags) { @@ -90,17 +105,7 @@ Error ResourceSaver::save(const Ref<Resource> &p_resource, const String &p_path, continue; } - List<String> extensions; - bool recognized = false; - saver[i]->get_recognized_extensions(p_resource, &extensions); - - for (const String &E : extensions) { - if (E.nocasecmp_to(extension) == 0) { - recognized = true; - } - } - - if (!recognized) { + if (!saver[i]->recognize_path(p_resource, path)) { continue; } diff --git a/core/io/resource_saver.h b/core/io/resource_saver.h index 4fee2bcfd1..5e48ce88c3 100644 --- a/core/io/resource_saver.h +++ b/core/io/resource_saver.h @@ -44,11 +44,13 @@ protected: GDVIRTUAL3R(int64_t, _save, Ref<Resource>, String, uint32_t) GDVIRTUAL1RC(bool, _recognize, Ref<Resource>) GDVIRTUAL1RC(Vector<String>, _get_recognized_extensions, Ref<Resource>) + GDVIRTUAL2RC(bool, _recognize_path, Ref<Resource>, String) public: virtual Error save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags = 0); virtual bool recognize(const Ref<Resource> &p_resource) const; virtual void get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const; + virtual bool recognize_path(const Ref<Resource> &p_resource, const String &p_path) const; virtual ~ResourceFormatSaver() {} }; diff --git a/core/io/resource_uid.cpp b/core/io/resource_uid.cpp index fc324a26da..ed5ce3b911 100644 --- a/core/io/resource_uid.cpp +++ b/core/io/resource_uid.cpp @@ -39,7 +39,7 @@ static constexpr uint32_t char_count = ('z' - 'a'); static constexpr uint32_t base = char_count + ('9' - '0'); String ResourceUID::get_cache_file() { - return ProjectSettings::get_singleton()->get_project_data_path().plus_file("uid_cache.bin"); + return ProjectSettings::get_singleton()->get_project_data_path().path_join("uid_cache.bin"); } String ResourceUID::id_to_text(ID p_id) const { @@ -113,7 +113,12 @@ void ResourceUID::set_id(ID p_id, const String &p_path) { MutexLock l(mutex); ERR_FAIL_COND(!unique_ids.has(p_id)); CharString cs = p_path.utf8(); - if (strcmp(cs.ptr(), unique_ids[p_id].cs.ptr()) != 0) { + const char *update_ptr = cs.ptr(); + const char *cached_ptr = unique_ids[p_id].cs.ptr(); + if (update_ptr == nullptr && cached_ptr == nullptr) { + return; // Both are empty strings. + } + if ((update_ptr == nullptr) != (cached_ptr == nullptr) || strcmp(update_ptr, cached_ptr) != 0) { unique_ids[p_id].cs = cs; unique_ids[p_id].saved_to_cache = false; //changed changed = true; diff --git a/core/io/stream_peer.cpp b/core/io/stream_peer.cpp index c65968ef03..053ff64069 100644 --- a/core/io/stream_peer.cpp +++ b/core/io/stream_peer.cpp @@ -410,48 +410,39 @@ void StreamPeer::_bind_methods() { //////////////////////////////// -int StreamPeerExtension::get_available_bytes() const { - int count; - if (GDVIRTUAL_CALL(_get_available_bytes, count)) { - return count; - } - WARN_PRINT_ONCE("StreamPeerExtension::_get_available_bytes is unimplemented!"); - return -1; -} - Error StreamPeerExtension::get_data(uint8_t *r_buffer, int p_bytes) { - int err; + Error err; int received = 0; if (GDVIRTUAL_CALL(_get_data, r_buffer, p_bytes, &received, err)) { - return (Error)err; + return err; } WARN_PRINT_ONCE("StreamPeerExtension::_get_data is unimplemented!"); return FAILED; } Error StreamPeerExtension::get_partial_data(uint8_t *r_buffer, int p_bytes, int &r_received) { - int err; + Error err; if (GDVIRTUAL_CALL(_get_partial_data, r_buffer, p_bytes, &r_received, err)) { - return (Error)err; + return err; } WARN_PRINT_ONCE("StreamPeerExtension::_get_partial_data is unimplemented!"); return FAILED; } Error StreamPeerExtension::put_data(const uint8_t *p_data, int p_bytes) { - int err; + Error err; int sent = 0; if (GDVIRTUAL_CALL(_put_data, p_data, p_bytes, &sent, err)) { - return (Error)err; + return err; } WARN_PRINT_ONCE("StreamPeerExtension::_put_data is unimplemented!"); return FAILED; } Error StreamPeerExtension::put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) { - int err; + Error err; if (GDVIRTUAL_CALL(_put_data, p_data, p_bytes, &r_sent, err)) { - return (Error)err; + return err; } WARN_PRINT_ONCE("StreamPeerExtension::_put_partial_data is unimplemented!"); return FAILED; diff --git a/core/io/stream_peer.h b/core/io/stream_peer.h index 4609e52aa2..108a8ce9d9 100644 --- a/core/io/stream_peer.h +++ b/core/io/stream_peer.h @@ -33,6 +33,7 @@ #include "core/object/ref_counted.h" +#include "core/extension/ext_wrappers.gen.inc" #include "core/object/gdvirtual.gen.inc" #include "core/object/script_language.h" #include "core/variant/native_ptr.h" @@ -104,16 +105,18 @@ protected: public: virtual Error put_data(const uint8_t *p_data, int p_bytes) override; + GDVIRTUAL3R(Error, _put_data, GDNativeConstPtr<const uint8_t>, int, GDNativePtr<int>); + virtual Error put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) override; + GDVIRTUAL3R(Error, _put_partial_data, GDNativeConstPtr<const uint8_t>, int, GDNativePtr<int>); + virtual Error get_data(uint8_t *p_buffer, int p_bytes) override; + GDVIRTUAL3R(Error, _get_data, GDNativePtr<uint8_t>, int, GDNativePtr<int>); + virtual Error get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received) override; - virtual int get_available_bytes() const override; + GDVIRTUAL3R(Error, _get_partial_data, GDNativePtr<uint8_t>, int, GDNativePtr<int>); - GDVIRTUAL3R(int, _put_data, GDNativeConstPtr<const uint8_t>, int, GDNativePtr<int>); - GDVIRTUAL3R(int, _put_partial_data, GDNativeConstPtr<const uint8_t>, int, GDNativePtr<int>); - GDVIRTUAL3R(int, _get_data, GDNativePtr<uint8_t>, int, GDNativePtr<int>); - GDVIRTUAL3R(int, _get_partial_data, GDNativePtr<uint8_t>, int, GDNativePtr<int>); - GDVIRTUAL0RC(int, _get_available_bytes); + EXBIND0RC(int, get_available_bytes); }; class StreamPeerBuffer : public StreamPeer { diff --git a/core/io/stream_peer_gzip.cpp b/core/io/stream_peer_gzip.cpp new file mode 100644 index 0000000000..ca8be2d62e --- /dev/null +++ b/core/io/stream_peer_gzip.cpp @@ -0,0 +1,209 @@ +/*************************************************************************/ +/* stream_peer_gzip.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 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 "core/io/stream_peer_gzip.h" + +#include "core/io/zip_io.h" +#include <zlib.h> + +void StreamPeerGZIP::_bind_methods() { + ClassDB::bind_method(D_METHOD("start_compression", "use_deflate", "buffer_size"), &StreamPeerGZIP::start_compression, DEFVAL(false), DEFVAL(65535)); + ClassDB::bind_method(D_METHOD("start_decompression", "use_deflate", "buffer_size"), &StreamPeerGZIP::start_decompression, DEFVAL(false), DEFVAL(65535)); + ClassDB::bind_method(D_METHOD("finish"), &StreamPeerGZIP::finish); + ClassDB::bind_method(D_METHOD("clear"), &StreamPeerGZIP::clear); +} + +StreamPeerGZIP::StreamPeerGZIP() { +} + +StreamPeerGZIP::~StreamPeerGZIP() { + _close(); +} + +void StreamPeerGZIP::_close() { + if (ctx) { + z_stream *strm = (z_stream *)ctx; + if (compressing) { + deflateEnd(strm); + } else { + inflateEnd(strm); + } + memfree(strm); + ctx = nullptr; + } +} + +void StreamPeerGZIP::clear() { + _close(); + rb.clear(); + buffer.clear(); +} + +Error StreamPeerGZIP::start_compression(bool p_is_deflate, int buffer_size) { + return _start(true, p_is_deflate, buffer_size); +} + +Error StreamPeerGZIP::start_decompression(bool p_is_deflate, int buffer_size) { + return _start(false, p_is_deflate, buffer_size); +} + +Error StreamPeerGZIP::_start(bool p_compress, bool p_is_deflate, int buffer_size) { + ERR_FAIL_COND_V(ctx != nullptr, ERR_ALREADY_IN_USE); + clear(); + compressing = p_compress; + rb.resize(nearest_shift(buffer_size - 1)); + buffer.resize(1024); + + // Create ctx. + ctx = memalloc(sizeof(z_stream)); + z_stream &strm = *(z_stream *)ctx; + strm.next_in = Z_NULL; + strm.avail_in = 0; + strm.zalloc = zipio_alloc; + strm.zfree = zipio_free; + strm.opaque = Z_NULL; + int window_bits = p_is_deflate ? 15 : (15 + 16); + int err = Z_OK; + int level = Z_DEFAULT_COMPRESSION; + if (compressing) { + err = deflateInit2(&strm, level, Z_DEFLATED, window_bits, 8, Z_DEFAULT_STRATEGY); + } else { + err = inflateInit2(&strm, window_bits); + } + ERR_FAIL_COND_V(err != Z_OK, FAILED); + return OK; +} + +Error StreamPeerGZIP::_process(uint8_t *p_dst, int p_dst_size, const uint8_t *p_src, int p_src_size, int &r_consumed, int &r_out, bool p_close) { + ERR_FAIL_COND_V(!ctx, ERR_UNCONFIGURED); + z_stream &strm = *(z_stream *)ctx; + strm.avail_in = p_src_size; + strm.avail_out = p_dst_size; + strm.next_in = (Bytef *)p_src; + strm.next_out = (Bytef *)p_dst; + int flush = p_close ? Z_FINISH : Z_NO_FLUSH; + if (compressing) { + int err = deflate(&strm, flush); + ERR_FAIL_COND_V(err != (p_close ? Z_STREAM_END : Z_OK), FAILED); + } else { + int err = inflate(&strm, flush); + ERR_FAIL_COND_V(err != Z_OK && err != Z_STREAM_END, FAILED); + } + r_out = p_dst_size - strm.avail_out; + r_consumed = p_src_size - strm.avail_in; + return OK; +} + +Error StreamPeerGZIP::put_data(const uint8_t *p_data, int p_bytes) { + int wrote = 0; + Error err = put_partial_data(p_data, p_bytes, wrote); + if (err != OK) { + return err; + } + ERR_FAIL_COND_V(p_bytes != wrote, ERR_OUT_OF_MEMORY); + return OK; +} + +Error StreamPeerGZIP::put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) { + ERR_FAIL_COND_V(!ctx, ERR_UNCONFIGURED); + ERR_FAIL_COND_V(p_bytes < 0, ERR_INVALID_PARAMETER); + + // Ensure we have enough space in temporary buffer. + if (buffer.size() < p_bytes) { + buffer.resize(p_bytes); + } + + r_sent = 0; + while (r_sent < p_bytes && rb.space_left() > 1024) { // Keep the ring buffer size meaningful. + int sent = 0; + int to_write = 0; + // Compress or decompress + Error err = _process(buffer.ptrw(), MIN(buffer.size(), rb.space_left()), p_data + r_sent, p_bytes - r_sent, sent, to_write); + if (err != OK) { + return err; + } + // When decompressing, we might need to do another round. + r_sent += sent; + + // We can't write more than this buffer is full. + if (sent == 0 && to_write == 0) { + return OK; + } + if (to_write) { + // Copy to ring buffer. + int wrote = rb.write(buffer.ptr(), to_write); + ERR_FAIL_COND_V(wrote != to_write, ERR_BUG); + } + } + return OK; +} + +Error StreamPeerGZIP::get_data(uint8_t *p_buffer, int p_bytes) { + int received = 0; + Error err = get_partial_data(p_buffer, p_bytes, received); + if (err != OK) { + return err; + } + ERR_FAIL_COND_V(p_bytes != received, ERR_UNAVAILABLE); + return OK; +} + +Error StreamPeerGZIP::get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received) { + ERR_FAIL_COND_V(p_bytes < 0, ERR_INVALID_PARAMETER); + + r_received = MIN(p_bytes, rb.data_left()); + if (r_received == 0) { + return OK; + } + int received = rb.read(p_buffer, r_received); + ERR_FAIL_COND_V(received != r_received, ERR_BUG); + return OK; +} + +int StreamPeerGZIP::get_available_bytes() const { + return rb.data_left(); +} + +Error StreamPeerGZIP::finish() { + ERR_FAIL_COND_V(!ctx || !compressing, ERR_UNAVAILABLE); + // Ensure we have enough space in temporary buffer. + if (buffer.size() < 1024) { + buffer.resize(1024); // 1024 should be more than enough. + } + int consumed = 0; + int to_write = 0; + Error err = _process(buffer.ptrw(), 1024, nullptr, 0, consumed, to_write, true); // compress + if (err != OK) { + return err; + } + int wrote = rb.write(buffer.ptr(), to_write); + ERR_FAIL_COND_V(wrote != to_write, ERR_OUT_OF_MEMORY); + return OK; +} diff --git a/core/io/stream_peer_gzip.h b/core/io/stream_peer_gzip.h new file mode 100644 index 0000000000..5bafdbca9b --- /dev/null +++ b/core/io/stream_peer_gzip.h @@ -0,0 +1,76 @@ +/*************************************************************************/ +/* stream_peer_gzip.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 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 STREAM_PEER_GZIP_H +#define STREAM_PEER_GZIP_H + +#include "core/io/stream_peer.h" + +#include "core/core_bind.h" +#include "core/io/compression.h" +#include "core/templates/ring_buffer.h" + +class StreamPeerGZIP : public StreamPeer { + GDCLASS(StreamPeerGZIP, StreamPeer); + +private: + void *ctx = nullptr; // Will hold our z_stream instance. + bool compressing = true; + + RingBuffer<uint8_t> rb; + Vector<uint8_t> buffer; + + Error _process(uint8_t *p_dst, int p_dst_size, const uint8_t *p_src, int p_src_size, int &r_consumed, int &r_out, bool p_close = false); + void _close(); + Error _start(bool p_compress, bool p_is_deflate, int buffer_size = 65535); + +protected: + static void _bind_methods(); + +public: + Error start_compression(bool p_is_deflate, int buffer_size = 65535); + Error start_decompression(bool p_is_deflate, int buffer_size = 65535); + + Error finish(); + void clear(); + + virtual Error put_data(const uint8_t *p_data, int p_bytes) override; + virtual Error put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) override; + + virtual Error get_data(uint8_t *p_buffer, int p_bytes) override; + virtual Error get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received) override; + + virtual int get_available_bytes() const override; + + StreamPeerGZIP(); + ~StreamPeerGZIP(); +}; + +#endif // STREAM_PEER_GZIP_H diff --git a/core/io/stream_peer_tcp.cpp b/core/io/stream_peer_tcp.cpp index ba79590c19..e035e1b613 100644 --- a/core/io/stream_peer_tcp.cpp +++ b/core/io/stream_peer_tcp.cpp @@ -256,9 +256,9 @@ void StreamPeerTCP::disconnect_from_host() { peer_port = 0; } -Error StreamPeerTCP::wait(NetSocket::PollType p_type, int timeout) { +Error StreamPeerTCP::wait(NetSocket::PollType p_type, int p_timeout) { ERR_FAIL_COND_V(_sock.is_null() || !_sock->is_open(), ERR_UNAVAILABLE); - return _sock->poll(p_type, timeout); + return _sock->poll(p_type, p_timeout); } Error StreamPeerTCP::put_data(const uint8_t *p_data, int p_bytes) { diff --git a/core/io/stream_peer_tcp.h b/core/io/stream_peer_tcp.h index 39c2e84346..778fb83374 100644 --- a/core/io/stream_peer_tcp.h +++ b/core/io/stream_peer_tcp.h @@ -79,7 +79,7 @@ public: Error poll(); // Wait or check for writable, readable. - Error wait(NetSocket::PollType p_type, int timeout = 0); + Error wait(NetSocket::PollType p_type, int p_timeout = 0); // Read/Write from StreamPeer Error put_data(const uint8_t *p_data, int p_bytes) override; diff --git a/core/io/stream_peer_ssl.cpp b/core/io/stream_peer_tls.cpp index 5b90fb52a6..b1adde018a 100644 --- a/core/io/stream_peer_ssl.cpp +++ b/core/io/stream_peer_tls.cpp @@ -1,5 +1,5 @@ /*************************************************************************/ -/* stream_peer_ssl.cpp */ +/* stream_peer_tls.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ @@ -28,42 +28,42 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#include "stream_peer_ssl.h" +#include "stream_peer_tls.h" #include "core/config/engine.h" -StreamPeerSSL *(*StreamPeerSSL::_create)() = nullptr; +StreamPeerTLS *(*StreamPeerTLS::_create)() = nullptr; -StreamPeerSSL *StreamPeerSSL::create() { +StreamPeerTLS *StreamPeerTLS::create() { if (_create) { return _create(); } return nullptr; } -bool StreamPeerSSL::available = false; +bool StreamPeerTLS::available = false; -bool StreamPeerSSL::is_available() { +bool StreamPeerTLS::is_available() { return available; } -void StreamPeerSSL::set_blocking_handshake_enabled(bool p_enabled) { +void StreamPeerTLS::set_blocking_handshake_enabled(bool p_enabled) { blocking_handshake = p_enabled; } -bool StreamPeerSSL::is_blocking_handshake_enabled() const { +bool StreamPeerTLS::is_blocking_handshake_enabled() const { return blocking_handshake; } -void StreamPeerSSL::_bind_methods() { - ClassDB::bind_method(D_METHOD("poll"), &StreamPeerSSL::poll); - ClassDB::bind_method(D_METHOD("accept_stream", "stream", "private_key", "certificate", "chain"), &StreamPeerSSL::accept_stream, DEFVAL(Ref<X509Certificate>())); - ClassDB::bind_method(D_METHOD("connect_to_stream", "stream", "validate_certs", "for_hostname", "valid_certificate"), &StreamPeerSSL::connect_to_stream, DEFVAL(false), DEFVAL(String()), DEFVAL(Ref<X509Certificate>())); - ClassDB::bind_method(D_METHOD("get_status"), &StreamPeerSSL::get_status); - ClassDB::bind_method(D_METHOD("get_stream"), &StreamPeerSSL::get_stream); - ClassDB::bind_method(D_METHOD("disconnect_from_stream"), &StreamPeerSSL::disconnect_from_stream); - ClassDB::bind_method(D_METHOD("set_blocking_handshake_enabled", "enabled"), &StreamPeerSSL::set_blocking_handshake_enabled); - ClassDB::bind_method(D_METHOD("is_blocking_handshake_enabled"), &StreamPeerSSL::is_blocking_handshake_enabled); +void StreamPeerTLS::_bind_methods() { + ClassDB::bind_method(D_METHOD("poll"), &StreamPeerTLS::poll); + ClassDB::bind_method(D_METHOD("accept_stream", "stream", "private_key", "certificate", "chain"), &StreamPeerTLS::accept_stream, DEFVAL(Ref<X509Certificate>())); + ClassDB::bind_method(D_METHOD("connect_to_stream", "stream", "validate_certs", "for_hostname", "valid_certificate"), &StreamPeerTLS::connect_to_stream, DEFVAL(false), DEFVAL(String()), DEFVAL(Ref<X509Certificate>())); + ClassDB::bind_method(D_METHOD("get_status"), &StreamPeerTLS::get_status); + ClassDB::bind_method(D_METHOD("get_stream"), &StreamPeerTLS::get_stream); + ClassDB::bind_method(D_METHOD("disconnect_from_stream"), &StreamPeerTLS::disconnect_from_stream); + ClassDB::bind_method(D_METHOD("set_blocking_handshake_enabled", "enabled"), &StreamPeerTLS::set_blocking_handshake_enabled); + ClassDB::bind_method(D_METHOD("is_blocking_handshake_enabled"), &StreamPeerTLS::is_blocking_handshake_enabled); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "blocking_handshake"), "set_blocking_handshake_enabled", "is_blocking_handshake_enabled"); diff --git a/core/io/stream_peer_ssl.h b/core/io/stream_peer_tls.h index fe68667adc..ed7334fab3 100644 --- a/core/io/stream_peer_ssl.h +++ b/core/io/stream_peer_tls.h @@ -1,5 +1,5 @@ /*************************************************************************/ -/* stream_peer_ssl.h */ +/* stream_peer_tls.h */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ @@ -28,17 +28,17 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#ifndef STREAM_PEER_SSL_H -#define STREAM_PEER_SSL_H +#ifndef STREAM_PEER_TLS_H +#define STREAM_PEER_TLS_H #include "core/crypto/crypto.h" #include "core/io/stream_peer.h" -class StreamPeerSSL : public StreamPeer { - GDCLASS(StreamPeerSSL, StreamPeer); +class StreamPeerTLS : public StreamPeer { + GDCLASS(StreamPeerTLS, StreamPeer); protected: - static StreamPeerSSL *(*_create)(); + static StreamPeerTLS *(*_create)(); static void _bind_methods(); static bool available; @@ -65,13 +65,13 @@ public: virtual void disconnect_from_stream() = 0; - static StreamPeerSSL *create(); + static StreamPeerTLS *create(); static bool is_available(); - StreamPeerSSL() {} + StreamPeerTLS() {} }; -VARIANT_ENUM_CAST(StreamPeerSSL::Status); +VARIANT_ENUM_CAST(StreamPeerTLS::Status); -#endif // STREAM_PEER_SSL_H +#endif // STREAM_PEER_TLS_H diff --git a/core/io/xml_parser.cpp b/core/io/xml_parser.cpp index 154b55f5e7..16b7f3a1f6 100644 --- a/core/io/xml_parser.cpp +++ b/core/io/xml_parser.cpp @@ -41,11 +41,11 @@ static inline bool _is_white_space(char c) { } //! sets the state that text was found. Returns true if set should be set -bool XMLParser::_set_text(char *start, char *end) { +bool XMLParser::_set_text(const char *start, const char *end) { // check if text is more than 2 characters, and if not, check if there is // only white space, so that this text won't be reported if (end - start < 3) { - char *p = start; + const char *p = start; for (; p != end; ++p) { if (!_is_white_space(*p)) { break; @@ -92,7 +92,7 @@ void XMLParser::_parse_closing_xml_element() { void XMLParser::_ignore_definition() { node_type = NODE_UNKNOWN; - char *F = P; + const char *F = P; // move until end marked with '>' reached while (*P && *P != '>') { next_char(); @@ -123,8 +123,8 @@ bool XMLParser::_parse_cdata() { return true; } - char *cDataBegin = P; - char *cDataEnd = nullptr; + const char *cDataBegin = P; + const char *cDataEnd = nullptr; // find end of CDATA while (*P && !cDataEnd) { @@ -137,7 +137,7 @@ bool XMLParser::_parse_cdata() { next_char(); } - if (cDataEnd) { + if (!cDataEnd) { cDataEnd = P; } node_name = String::utf8(cDataBegin, (int)(cDataEnd - cDataBegin)); @@ -152,9 +152,9 @@ void XMLParser::_parse_comment() { node_type = NODE_COMMENT; P += 1; - char *pEndOfInput = data + length; - char *pCommentBegin; - char *pCommentEnd; + const char *pEndOfInput = data + length; + const char *pCommentBegin; + const char *pCommentEnd; if (P + 1 < pEndOfInput && P[0] == '-' && P[1] == '-') { // Comment, use '-->' as end. @@ -293,7 +293,7 @@ void XMLParser::_parse_opening_xml_element() { } void XMLParser::_parse_current_node() { - char *start = P; + const char *start = P; node_offset = P - data; // more forward until '<' found @@ -458,15 +458,36 @@ bool XMLParser::is_empty() const { Error XMLParser::open_buffer(const Vector<uint8_t> &p_buffer) { ERR_FAIL_COND_V(p_buffer.size() == 0, ERR_INVALID_DATA); - if (data) { - memdelete_arr(data); + if (data_copy) { + memdelete_arr(data_copy); + data_copy = nullptr; } length = p_buffer.size(); - data = memnew_arr(char, length + 1); - memcpy(data, p_buffer.ptr(), length); - data[length] = 0; + data_copy = memnew_arr(char, length + 1); + memcpy(data_copy, p_buffer.ptr(), length); + data_copy[length] = 0; + data = data_copy; + P = data; + current_line = 0; + + return OK; +} + +Error XMLParser::_open_buffer(const uint8_t *p_buffer, size_t p_size) { + ERR_FAIL_COND_V(p_size == 0, ERR_INVALID_DATA); + ERR_FAIL_COND_V(!p_buffer, ERR_INVALID_DATA); + + if (data_copy) { + memdelete_arr(data_copy); + data_copy = nullptr; + } + + length = p_size; + data = (const char *)p_buffer; P = data; + current_line = 0; + return OK; } @@ -479,13 +500,15 @@ Error XMLParser::open(const String &p_path) { length = file->get_length(); ERR_FAIL_COND_V(length < 1, ERR_FILE_CORRUPT); - if (data) { - memdelete_arr(data); + if (data_copy) { + memdelete_arr(data_copy); + data_copy = nullptr; } - data = memnew_arr(char, length + 1); - file->get_buffer((uint8_t *)data, length); - data[length] = 0; + data_copy = memnew_arr(char, length + 1); + file->get_buffer((uint8_t *)data_copy, length); + data_copy[length] = 0; + data = data_copy; P = data; current_line = 0; @@ -512,8 +535,9 @@ void XMLParser::skip_section() { } void XMLParser::close() { - if (data) { + if (data_copy) { memdelete_arr(data); + data_copy = nullptr; } data = nullptr; length = 0; @@ -528,7 +552,8 @@ int XMLParser::get_current_line() const { } XMLParser::~XMLParser() { - if (data) { - memdelete_arr(data); + if (data_copy) { + memdelete_arr(data_copy); + data_copy = nullptr; } } diff --git a/core/io/xml_parser.h b/core/io/xml_parser.h index aea252ddc7..be44f771dc 100644 --- a/core/io/xml_parser.h +++ b/core/io/xml_parser.h @@ -65,8 +65,9 @@ public: }; private: - char *data = nullptr; - char *P = nullptr; + char *data_copy = nullptr; + const char *data = nullptr; + const char *P = nullptr; uint64_t length = 0; uint64_t current_line = 0; String node_name; @@ -81,7 +82,7 @@ private: Vector<Attribute> attributes; - bool _set_text(char *start, char *end); + bool _set_text(const char *start, const char *end); void _parse_closing_xml_element(); void _ignore_definition(); bool _parse_cdata(); @@ -118,6 +119,7 @@ public: Error open(const String &p_path); Error open_buffer(const Vector<uint8_t> &p_buffer); + Error _open_buffer(const uint8_t *p_buffer, size_t p_size); void close(); diff --git a/core/io/zip_io.cpp b/core/io/zip_io.cpp index e573e8de19..200e5f5e83 100644 --- a/core/io/zip_io.cpp +++ b/core/io/zip_io.cpp @@ -37,11 +37,17 @@ void *zipio_open(voidpf opaque, const char *p_fname, int mode) { String fname; fname.parse_utf8(p_fname); + int file_access_mode = 0; if (mode & ZLIB_FILEFUNC_MODE_WRITE) { - (*fa) = FileAccess::open(fname, FileAccess::WRITE); - } else { - (*fa) = FileAccess::open(fname, FileAccess::READ); + file_access_mode |= FileAccess::WRITE; } + if (mode & ZLIB_FILEFUNC_MODE_READ) { + file_access_mode |= FileAccess::READ; + } + if (mode & ZLIB_FILEFUNC_MODE_CREATE) { + file_access_mode |= FileAccess::WRITE_READ; + } + (*fa) = FileAccess::open(fname, file_access_mode); if (fa->is_null()) { return nullptr; diff --git a/core/math/a_star.cpp b/core/math/a_star.cpp index 41a0848d01..b4281820e2 100644 --- a/core/math/a_star.cpp +++ b/core/math/a_star.cpp @@ -209,8 +209,8 @@ bool AStar3D::has_point(int64_t p_id) const { return points.has(p_id); } -Array AStar3D::get_point_ids() { - Array point_list; +PackedInt64Array AStar3D::get_point_ids() { + PackedInt64Array point_list; for (OAHashMap<int64_t, Point *>::Iterator it = points.iter(); it.valid; it = points.next_iter(it)) { point_list.push_back(*(it.key)); @@ -605,7 +605,7 @@ Vector<int64_t> AStar2D::get_point_connections(int64_t p_id) { return astar.get_point_connections(p_id); } -Array AStar2D::get_point_ids() { +PackedInt64Array AStar2D::get_point_ids() { return astar.get_point_ids(); } diff --git a/core/math/a_star.h b/core/math/a_star.h index c1497d133f..a9e2a62bb2 100644 --- a/core/math/a_star.h +++ b/core/math/a_star.h @@ -133,7 +133,7 @@ public: void remove_point(int64_t p_id); bool has_point(int64_t p_id) const; Vector<int64_t> get_point_connections(int64_t p_id); - Array get_point_ids(); + PackedInt64Array get_point_ids(); void set_point_disabled(int64_t p_id, bool p_disabled = true); bool is_point_disabled(int64_t p_id) const; @@ -183,7 +183,7 @@ public: void remove_point(int64_t p_id); bool has_point(int64_t p_id) const; Vector<int64_t> get_point_connections(int64_t p_id); - Array get_point_ids(); + PackedInt64Array get_point_ids(); void set_point_disabled(int64_t p_id, bool p_disabled = true); bool is_point_disabled(int64_t p_id) const; diff --git a/core/math/a_star_grid_2d.cpp b/core/math/a_star_grid_2d.cpp new file mode 100644 index 0000000000..c30acf32bb --- /dev/null +++ b/core/math/a_star_grid_2d.cpp @@ -0,0 +1,589 @@ +/*************************************************************************/ +/* a_star_grid_2d.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 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 "a_star_grid_2d.h" + +#include "core/variant/typed_array.h" + +static real_t heuristic_euclidian(const Vector2i &p_from, const Vector2i &p_to) { + real_t dx = (real_t)ABS(p_to.x - p_from.x); + real_t dy = (real_t)ABS(p_to.y - p_from.y); + return (real_t)Math::sqrt(dx * dx + dy * dy); +} + +static real_t heuristic_manhattan(const Vector2i &p_from, const Vector2i &p_to) { + real_t dx = (real_t)ABS(p_to.x - p_from.x); + real_t dy = (real_t)ABS(p_to.y - p_from.y); + return dx + dy; +} + +static real_t heuristic_octile(const Vector2i &p_from, const Vector2i &p_to) { + real_t dx = (real_t)ABS(p_to.x - p_from.x); + real_t dy = (real_t)ABS(p_to.y - p_from.y); + real_t F = Math_SQRT2 - 1; + return (dx < dy) ? F * dx + dy : F * dy + dx; +} + +static real_t heuristic_chebyshev(const Vector2i &p_from, const Vector2i &p_to) { + real_t dx = (real_t)ABS(p_to.x - p_from.x); + real_t dy = (real_t)ABS(p_to.y - p_from.y); + return MAX(dx, dy); +} + +static real_t (*heuristics[AStarGrid2D::HEURISTIC_MAX])(const Vector2i &, const Vector2i &) = { heuristic_euclidian, heuristic_manhattan, heuristic_octile, heuristic_chebyshev }; + +void AStarGrid2D::set_size(const Size2i &p_size) { + ERR_FAIL_COND(p_size.x < 0 || p_size.y < 0); + if (p_size != size) { + size = p_size; + dirty = true; + } +} + +Size2i AStarGrid2D::get_size() const { + return size; +} + +void AStarGrid2D::set_offset(const Vector2 &p_offset) { + if (!offset.is_equal_approx(p_offset)) { + offset = p_offset; + dirty = true; + } +} + +Vector2 AStarGrid2D::get_offset() const { + return offset; +} + +void AStarGrid2D::set_cell_size(const Size2 &p_cell_size) { + if (!cell_size.is_equal_approx(p_cell_size)) { + cell_size = p_cell_size; + dirty = true; + } +} + +Size2 AStarGrid2D::get_cell_size() const { + return cell_size; +} + +void AStarGrid2D::update() { + points.clear(); + for (int64_t y = 0; y < size.y; y++) { + LocalVector<Point> line; + for (int64_t x = 0; x < size.x; x++) { + line.push_back(Point(Vector2i(x, y), offset + Vector2(x, y) * cell_size)); + } + points.push_back(line); + } + dirty = false; +} + +bool AStarGrid2D::is_in_bounds(int p_x, int p_y) const { + return p_x >= 0 && p_x < size.width && p_y >= 0 && p_y < size.height; +} + +bool AStarGrid2D::is_in_boundsv(const Vector2i &p_id) const { + return p_id.x >= 0 && p_id.x < size.width && p_id.y >= 0 && p_id.y < size.height; +} + +bool AStarGrid2D::is_dirty() const { + return dirty; +} + +void AStarGrid2D::set_jumping_enabled(bool p_enabled) { + jumping_enabled = p_enabled; +} + +bool AStarGrid2D::is_jumping_enabled() const { + return jumping_enabled; +} + +void AStarGrid2D::set_diagonal_mode(DiagonalMode p_diagonal_mode) { + ERR_FAIL_INDEX((int)p_diagonal_mode, (int)DIAGONAL_MODE_MAX); + diagonal_mode = p_diagonal_mode; +} + +AStarGrid2D::DiagonalMode AStarGrid2D::get_diagonal_mode() const { + return diagonal_mode; +} + +void AStarGrid2D::set_default_heuristic(Heuristic p_heuristic) { + ERR_FAIL_INDEX((int)p_heuristic, (int)HEURISTIC_MAX); + default_heuristic = p_heuristic; +} + +AStarGrid2D::Heuristic AStarGrid2D::get_default_heuristic() const { + return default_heuristic; +} + +void AStarGrid2D::set_point_solid(const Vector2i &p_id, bool p_solid) { + ERR_FAIL_COND_MSG(dirty, "Grid is not initialized. Call the update method."); + ERR_FAIL_COND_MSG(!is_in_boundsv(p_id), vformat("Can't set if point is disabled. Point out of bounds (%s/%s, %s/%s).", p_id.x, size.width, p_id.y, size.height)); + points[p_id.y][p_id.x].solid = p_solid; +} + +bool AStarGrid2D::is_point_solid(const Vector2i &p_id) const { + ERR_FAIL_COND_V_MSG(dirty, false, "Grid is not initialized. Call the update method."); + ERR_FAIL_COND_V_MSG(!is_in_boundsv(p_id), false, vformat("Can't get if point is disabled. Point out of bounds (%s/%s, %s/%s).", p_id.x, size.width, p_id.y, size.height)); + return points[p_id.y][p_id.x].solid; +} + +AStarGrid2D::Point *AStarGrid2D::_jump(Point *p_from, Point *p_to) { + if (!p_to || p_to->solid) { + return nullptr; + } + if (p_to == end) { + return p_to; + } + + int64_t from_x = p_from->id.x; + int64_t from_y = p_from->id.y; + + int64_t to_x = p_to->id.x; + int64_t to_y = p_to->id.y; + + int64_t dx = to_x - from_x; + int64_t dy = to_y - from_y; + + if (diagonal_mode == DIAGONAL_MODE_ALWAYS || diagonal_mode == DIAGONAL_MODE_AT_LEAST_ONE_WALKABLE) { + if (dx != 0 && dy != 0) { + if ((_is_walkable(to_x - dx, to_y + dy) && !_is_walkable(to_x - dx, to_y)) || (_is_walkable(to_x + dx, to_y - dy) && !_is_walkable(to_x, to_y - dy))) { + return p_to; + } + if (_jump(p_to, _get_point(to_x + dx, to_y)) != nullptr) { + return p_to; + } + if (_jump(p_to, _get_point(to_x, to_y + dy)) != nullptr) { + return p_to; + } + } else { + if (dx != 0) { + if ((_is_walkable(to_x + dx, to_y + 1) && !_is_walkable(to_x, to_y + 1)) || (_is_walkable(to_x + dx, to_y - 1) && !_is_walkable(to_x, to_y - 1))) { + return p_to; + } + } else { + if ((_is_walkable(to_x + 1, to_y + dy) && !_is_walkable(to_x + 1, to_y)) || (_is_walkable(to_x - 1, to_y + dy) && !_is_walkable(to_x - 1, to_y))) { + return p_to; + } + } + } + if (_is_walkable(to_x + dx, to_y + dy) && (diagonal_mode == DIAGONAL_MODE_ALWAYS || (_is_walkable(to_x + dx, to_y) || _is_walkable(to_x, to_y + dy)))) { + return _jump(p_to, _get_point(to_x + dx, to_y + dy)); + } + } else if (diagonal_mode == DIAGONAL_MODE_ONLY_IF_NO_OBSTACLES) { + if (dx != 0 && dy != 0) { + if ((_is_walkable(to_x + dx, to_y + dy) && !_is_walkable(to_x, to_y + dy)) || !_is_walkable(to_x + dx, to_y)) { + return p_to; + } + if (_jump(p_to, _get_point(to_x + dx, to_y)) != nullptr) { + return p_to; + } + if (_jump(p_to, _get_point(to_x, to_y + dy)) != nullptr) { + return p_to; + } + } else { + if (dx != 0) { + if ((_is_walkable(to_x, to_y + 1) && !_is_walkable(to_x - dx, to_y + 1)) || (_is_walkable(to_x, to_y - 1) && !_is_walkable(to_x - dx, to_y - 1))) { + return p_to; + } + } else { + if ((_is_walkable(to_x + 1, to_y) && !_is_walkable(to_x + 1, to_y - dy)) || (_is_walkable(to_x - 1, to_y) && !_is_walkable(to_x - 1, to_y - dy))) { + return p_to; + } + } + } + if (_is_walkable(to_x + dx, to_y + dy) && _is_walkable(to_x + dx, to_y) && _is_walkable(to_x, to_y + dy)) { + return _jump(p_to, _get_point(to_x + dx, to_y + dy)); + } + } else { // DIAGONAL_MODE_NEVER + if (dx != 0) { + if (!_is_walkable(to_x + dx, to_y)) { + return p_to; + } + if (_jump(p_to, _get_point(to_x, to_y + 1)) != nullptr) { + return p_to; + } + if (_jump(p_to, _get_point(to_x, to_y - 1)) != nullptr) { + return p_to; + } + } else { + if (!_is_walkable(to_x, to_y + dy)) { + return p_to; + } + if (_jump(p_to, _get_point(to_x + 1, to_y)) != nullptr) { + return p_to; + } + if (_jump(p_to, _get_point(to_x - 1, to_y)) != nullptr) { + return p_to; + } + } + if (_is_walkable(to_x + dx, to_y + dy) && _is_walkable(to_x + dx, to_y) && _is_walkable(to_x, to_y + dy)) { + return _jump(p_to, _get_point(to_x + dx, to_y + dy)); + } + } + return nullptr; +} + +void AStarGrid2D::_get_nbors(Point *p_point, List<Point *> &r_nbors) { + bool ts0 = false, td0 = false, + ts1 = false, td1 = false, + ts2 = false, td2 = false, + ts3 = false, td3 = false; + + Point *left = nullptr; + Point *right = nullptr; + Point *top = nullptr; + Point *bottom = nullptr; + + Point *top_left = nullptr; + Point *top_right = nullptr; + Point *bottom_left = nullptr; + Point *bottom_right = nullptr; + + { + bool has_left = false; + bool has_right = false; + + if (p_point->id.x - 1 >= 0) { + left = _get_point_unchecked(p_point->id.x - 1, p_point->id.y); + has_left = true; + } + if (p_point->id.x + 1 < size.width) { + right = _get_point_unchecked(p_point->id.x + 1, p_point->id.y); + has_right = true; + } + if (p_point->id.y - 1 >= 0) { + top = _get_point_unchecked(p_point->id.x, p_point->id.y - 1); + if (has_left) { + top_left = _get_point_unchecked(p_point->id.x - 1, p_point->id.y - 1); + } + if (has_right) { + top_right = _get_point_unchecked(p_point->id.x + 1, p_point->id.y - 1); + } + } + if (p_point->id.y + 1 < size.height) { + bottom = _get_point_unchecked(p_point->id.x, p_point->id.y + 1); + if (has_left) { + bottom_left = _get_point_unchecked(p_point->id.x - 1, p_point->id.y + 1); + } + if (has_right) { + bottom_right = _get_point_unchecked(p_point->id.x + 1, p_point->id.y + 1); + } + } + } + + if (top && !top->solid) { + r_nbors.push_back(top); + ts0 = true; + } + if (right && !right->solid) { + r_nbors.push_back(right); + ts1 = true; + } + if (bottom && !bottom->solid) { + r_nbors.push_back(bottom); + ts2 = true; + } + if (left && !left->solid) { + r_nbors.push_back(left); + ts3 = true; + } + + switch (diagonal_mode) { + case DIAGONAL_MODE_ALWAYS: { + td0 = true; + td1 = true; + td2 = true; + td3 = true; + } break; + case DIAGONAL_MODE_NEVER: { + } break; + case DIAGONAL_MODE_AT_LEAST_ONE_WALKABLE: { + td0 = ts3 || ts0; + td1 = ts0 || ts1; + td2 = ts1 || ts2; + td3 = ts2 || ts3; + } break; + case DIAGONAL_MODE_ONLY_IF_NO_OBSTACLES: { + td0 = ts3 && ts0; + td1 = ts0 && ts1; + td2 = ts1 && ts2; + td3 = ts2 && ts3; + } break; + default: + break; + } + + if (td0 && (top_left && !top_left->solid)) { + r_nbors.push_back(top_left); + } + if (td1 && (top_right && !top_right->solid)) { + r_nbors.push_back(top_right); + } + if (td2 && (bottom_right && !bottom_right->solid)) { + r_nbors.push_back(bottom_right); + } + if (td3 && (bottom_left && !bottom_left->solid)) { + r_nbors.push_back(bottom_left); + } +} + +bool AStarGrid2D::_solve(Point *p_begin_point, Point *p_end_point) { + pass++; + + if (p_end_point->solid) { + return false; + } + + bool found_route = false; + + Vector<Point *> open_list; + SortArray<Point *, SortPoints> sorter; + + p_begin_point->g_score = 0; + p_begin_point->f_score = _estimate_cost(p_begin_point->id, p_end_point->id); + open_list.push_back(p_begin_point); + end = p_end_point; + + while (!open_list.is_empty()) { + Point *p = open_list[0]; // The currently processed point. + + if (p == p_end_point) { + found_route = true; + break; + } + + sorter.pop_heap(0, open_list.size(), open_list.ptrw()); // Remove the current point from the open list. + open_list.remove_at(open_list.size() - 1); + p->closed_pass = pass; // Mark the point as closed. + + List<Point *> nbors; + _get_nbors(p, nbors); + for (List<Point *>::Element *E = nbors.front(); E; E = E->next()) { + Point *e = E->get(); // The neighbour point. + if (jumping_enabled) { + e = _jump(p, e); + if (!e || e->closed_pass == pass) { + continue; + } + } else { + if (e->solid || e->closed_pass == pass) { + continue; + } + } + + real_t tentative_g_score = p->g_score + _compute_cost(p->id, e->id); + bool new_point = false; + + if (e->open_pass != pass) { // The point wasn't inside the open list. + e->open_pass = pass; + open_list.push_back(e); + new_point = true; + } else if (tentative_g_score >= e->g_score) { // The new path is worse than the previous. + continue; + } + + e->prev_point = p; + e->g_score = tentative_g_score; + e->f_score = e->g_score + _estimate_cost(e->id, p_end_point->id); + + if (new_point) { // The position of the new points is already known. + sorter.push_heap(0, open_list.size() - 1, 0, e, open_list.ptrw()); + } else { + sorter.push_heap(0, open_list.find(e), 0, e, open_list.ptrw()); + } + } + } + + return found_route; +} + +real_t AStarGrid2D::_estimate_cost(const Vector2i &p_from_id, const Vector2i &p_to_id) { + real_t scost; + if (GDVIRTUAL_CALL(_estimate_cost, p_from_id, p_to_id, scost)) { + return scost; + } + return heuristics[default_heuristic](p_from_id, p_to_id); +} + +real_t AStarGrid2D::_compute_cost(const Vector2i &p_from_id, const Vector2i &p_to_id) { + real_t scost; + if (GDVIRTUAL_CALL(_compute_cost, p_from_id, p_to_id, scost)) { + return scost; + } + return heuristics[default_heuristic](p_from_id, p_to_id); +} + +void AStarGrid2D::clear() { + points.clear(); + size = Vector2i(); +} + +Vector<Vector2> AStarGrid2D::get_point_path(const Vector2i &p_from_id, const Vector2i &p_to_id) { + ERR_FAIL_COND_V_MSG(dirty, Vector<Vector2>(), "Grid is not initialized. Call the update method."); + ERR_FAIL_COND_V_MSG(!is_in_boundsv(p_from_id), Vector<Vector2>(), vformat("Can't get id path. Point out of bounds (%s/%s, %s/%s)", p_from_id.x, size.width, p_from_id.y, size.height)); + ERR_FAIL_COND_V_MSG(!is_in_boundsv(p_to_id), Vector<Vector2>(), vformat("Can't get id path. Point out of bounds (%s/%s, %s/%s)", p_to_id.x, size.width, p_to_id.y, size.height)); + + Point *a = _get_point(p_from_id.x, p_from_id.y); + Point *b = _get_point(p_to_id.x, p_to_id.y); + + if (a == b) { + Vector<Vector2> ret; + ret.push_back(a->pos); + return ret; + } + + Point *begin_point = a; + Point *end_point = b; + + bool found_route = _solve(begin_point, end_point); + if (!found_route) { + return Vector<Vector2>(); + } + + Point *p = end_point; + int64_t pc = 1; + while (p != begin_point) { + pc++; + p = p->prev_point; + } + + Vector<Vector2> path; + path.resize(pc); + + { + Vector2 *w = path.ptrw(); + + p = end_point; + int64_t idx = pc - 1; + while (p != begin_point) { + w[idx--] = p->pos; + p = p->prev_point; + } + + w[0] = p->pos; + } + + return path; +} + +TypedArray<Vector2i> AStarGrid2D::get_id_path(const Vector2i &p_from_id, const Vector2i &p_to_id) { + ERR_FAIL_COND_V_MSG(dirty, TypedArray<Vector2i>(), "Grid is not initialized. Call the update method."); + ERR_FAIL_COND_V_MSG(!is_in_boundsv(p_from_id), TypedArray<Vector2i>(), vformat("Can't get id path. Point out of bounds (%s/%s, %s/%s)", p_from_id.x, size.width, p_from_id.y, size.height)); + ERR_FAIL_COND_V_MSG(!is_in_boundsv(p_to_id), TypedArray<Vector2i>(), vformat("Can't get id path. Point out of bounds (%s/%s, %s/%s)", p_to_id.x, size.width, p_to_id.y, size.height)); + + Point *a = _get_point(p_from_id.x, p_from_id.y); + Point *b = _get_point(p_to_id.x, p_to_id.y); + + if (a == b) { + TypedArray<Vector2i> ret; + ret.push_back(a); + return ret; + } + + Point *begin_point = a; + Point *end_point = b; + + bool found_route = _solve(begin_point, end_point); + if (!found_route) { + return TypedArray<Vector2i>(); + } + + Point *p = end_point; + int64_t pc = 1; + while (p != begin_point) { + pc++; + p = p->prev_point; + } + + TypedArray<Vector2i> path; + path.resize(pc); + + { + p = end_point; + int64_t idx = pc - 1; + while (p != begin_point) { + path[idx--] = p->id; + p = p->prev_point; + } + + path[0] = p->id; + } + + return path; +} + +void AStarGrid2D::_bind_methods() { + ClassDB::bind_method(D_METHOD("set_size", "size"), &AStarGrid2D::set_size); + ClassDB::bind_method(D_METHOD("get_size"), &AStarGrid2D::get_size); + ClassDB::bind_method(D_METHOD("set_offset", "offset"), &AStarGrid2D::set_offset); + ClassDB::bind_method(D_METHOD("get_offset"), &AStarGrid2D::get_offset); + ClassDB::bind_method(D_METHOD("set_cell_size", "cell_size"), &AStarGrid2D::set_cell_size); + ClassDB::bind_method(D_METHOD("get_cell_size"), &AStarGrid2D::get_cell_size); + ClassDB::bind_method(D_METHOD("is_in_bounds", "x", "y"), &AStarGrid2D::is_in_bounds); + ClassDB::bind_method(D_METHOD("is_in_boundsv", "id"), &AStarGrid2D::is_in_boundsv); + ClassDB::bind_method(D_METHOD("is_dirty"), &AStarGrid2D::is_dirty); + ClassDB::bind_method(D_METHOD("update"), &AStarGrid2D::update); + ClassDB::bind_method(D_METHOD("set_jumping_enabled", "enabled"), &AStarGrid2D::set_jumping_enabled); + ClassDB::bind_method(D_METHOD("is_jumping_enabled"), &AStarGrid2D::is_jumping_enabled); + ClassDB::bind_method(D_METHOD("set_diagonal_mode", "mode"), &AStarGrid2D::set_diagonal_mode); + ClassDB::bind_method(D_METHOD("get_diagonal_mode"), &AStarGrid2D::get_diagonal_mode); + ClassDB::bind_method(D_METHOD("set_default_heuristic", "heuristic"), &AStarGrid2D::set_default_heuristic); + ClassDB::bind_method(D_METHOD("get_default_heuristic"), &AStarGrid2D::get_default_heuristic); + ClassDB::bind_method(D_METHOD("set_point_solid", "id", "solid"), &AStarGrid2D::set_point_solid, DEFVAL(true)); + ClassDB::bind_method(D_METHOD("is_point_solid", "id"), &AStarGrid2D::is_point_solid); + ClassDB::bind_method(D_METHOD("clear"), &AStarGrid2D::clear); + + ClassDB::bind_method(D_METHOD("get_point_path", "from_id", "to_id"), &AStarGrid2D::get_point_path); + ClassDB::bind_method(D_METHOD("get_id_path", "from_id", "to_id"), &AStarGrid2D::get_id_path); + + GDVIRTUAL_BIND(_estimate_cost, "from_id", "to_id") + GDVIRTUAL_BIND(_compute_cost, "from_id", "to_id") + + ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "size"), "set_size", "get_size"); + ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset"), "set_offset", "get_offset"); + ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "cell_size"), "set_cell_size", "get_cell_size"); + + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "jumping_enabled"), "set_jumping_enabled", "is_jumping_enabled"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "default_heuristic", PROPERTY_HINT_ENUM, "Euclidean,Manhattan,Octile,Chebyshev,Max"), "set_default_heuristic", "get_default_heuristic"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "diagonal_mode", PROPERTY_HINT_ENUM, "Never,Always,At Least One Walkable,Only If No Obstacles,Max"), "set_diagonal_mode", "get_diagonal_mode"); + + BIND_ENUM_CONSTANT(HEURISTIC_EUCLIDEAN); + BIND_ENUM_CONSTANT(HEURISTIC_MANHATTAN); + BIND_ENUM_CONSTANT(HEURISTIC_OCTILE); + BIND_ENUM_CONSTANT(HEURISTIC_CHEBYSHEV); + BIND_ENUM_CONSTANT(HEURISTIC_MAX); + + BIND_ENUM_CONSTANT(DIAGONAL_MODE_ALWAYS); + BIND_ENUM_CONSTANT(DIAGONAL_MODE_NEVER); + BIND_ENUM_CONSTANT(DIAGONAL_MODE_AT_LEAST_ONE_WALKABLE); + BIND_ENUM_CONSTANT(DIAGONAL_MODE_ONLY_IF_NO_OBSTACLES); + BIND_ENUM_CONSTANT(DIAGONAL_MODE_MAX); +} diff --git a/core/math/a_star_grid_2d.h b/core/math/a_star_grid_2d.h new file mode 100644 index 0000000000..1002f18738 --- /dev/null +++ b/core/math/a_star_grid_2d.h @@ -0,0 +1,178 @@ +/*************************************************************************/ +/* a_star_grid_2d.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 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 A_STAR_GRID_2D_H +#define A_STAR_GRID_2D_H + +#include "core/object/gdvirtual.gen.inc" +#include "core/object/ref_counted.h" +#include "core/object/script_language.h" +#include "core/templates/list.h" +#include "core/templates/local_vector.h" + +class AStarGrid2D : public RefCounted { + GDCLASS(AStarGrid2D, RefCounted); + +public: + enum DiagonalMode { + DIAGONAL_MODE_ALWAYS, + DIAGONAL_MODE_NEVER, + DIAGONAL_MODE_AT_LEAST_ONE_WALKABLE, + DIAGONAL_MODE_ONLY_IF_NO_OBSTACLES, + DIAGONAL_MODE_MAX, + }; + + enum Heuristic { + HEURISTIC_EUCLIDEAN, + HEURISTIC_MANHATTAN, + HEURISTIC_OCTILE, + HEURISTIC_CHEBYSHEV, + HEURISTIC_MAX, + }; + +private: + Size2i size; + Vector2 offset; + Size2 cell_size = Size2(1, 1); + bool dirty = false; + + bool jumping_enabled = false; + DiagonalMode diagonal_mode = DIAGONAL_MODE_ALWAYS; + Heuristic default_heuristic = HEURISTIC_EUCLIDEAN; + + struct Point { + Vector2i id; + + bool solid = false; + Vector2 pos; + + // Used for pathfinding. + Point *prev_point = nullptr; + real_t g_score = 0; + real_t f_score = 0; + uint64_t open_pass = 0; + uint64_t closed_pass = 0; + + Point() {} + + Point(const Vector2i &p_id, const Vector2 &p_pos) : + id(p_id), pos(p_pos) {} + }; + + struct SortPoints { + _FORCE_INLINE_ bool operator()(const Point *A, const Point *B) const { // Returns true when the Point A is worse than Point B. + if (A->f_score > B->f_score) { + return true; + } else if (A->f_score < B->f_score) { + return false; + } else { + return A->g_score < B->g_score; // If the f_costs are the same then prioritize the points that are further away from the start. + } + } + }; + + LocalVector<LocalVector<Point>> points; + Point *end = nullptr; + + uint64_t pass = 1; + +private: // Internal routines. + _FORCE_INLINE_ bool _is_walkable(int64_t p_x, int64_t p_y) const { + if (p_x >= 0 && p_y >= 0 && p_x < size.width && p_y < size.height) { + return !points[p_y][p_x].solid; + } + return false; + } + + _FORCE_INLINE_ Point *_get_point(int64_t p_x, int64_t p_y) { + if (p_x >= 0 && p_y >= 0 && p_x < size.width && p_y < size.height) { + return &points[p_y][p_x]; + } + return nullptr; + } + + _FORCE_INLINE_ Point *_get_point_unchecked(int64_t p_x, int64_t p_y) { + return &points[p_y][p_x]; + } + + void _get_nbors(Point *p_point, List<Point *> &r_nbors); + Point *_jump(Point *p_from, Point *p_to); + bool _solve(Point *p_begin_point, Point *p_end_point); + +protected: + static void _bind_methods(); + + virtual real_t _estimate_cost(const Vector2i &p_from_id, const Vector2i &p_to_id); + virtual real_t _compute_cost(const Vector2i &p_from_id, const Vector2i &p_to_id); + + GDVIRTUAL2RC(real_t, _estimate_cost, Vector2i, Vector2i) + GDVIRTUAL2RC(real_t, _compute_cost, Vector2i, Vector2i) + +public: + void set_size(const Size2i &p_size); + Size2i get_size() const; + + void set_offset(const Vector2 &p_offset); + Vector2 get_offset() const; + + void set_cell_size(const Size2 &p_cell_size); + Size2 get_cell_size() const; + + void update(); + + int get_width() const; + int get_height() const; + + bool is_in_bounds(int p_x, int p_y) const; + bool is_in_boundsv(const Vector2i &p_id) const; + bool is_dirty() const; + + void set_jumping_enabled(bool p_enabled); + bool is_jumping_enabled() const; + + void set_diagonal_mode(DiagonalMode p_diagonal_mode); + DiagonalMode get_diagonal_mode() const; + + void set_default_heuristic(Heuristic p_heuristic); + Heuristic get_default_heuristic() const; + + void set_point_solid(const Vector2i &p_id, bool p_solid = true); + bool is_point_solid(const Vector2i &p_id) const; + + void clear(); + + Vector<Vector2> get_point_path(const Vector2i &p_from, const Vector2i &p_to); + TypedArray<Vector2i> get_id_path(const Vector2i &p_from, const Vector2i &p_to); +}; + +VARIANT_ENUM_CAST(AStarGrid2D::DiagonalMode); +VARIANT_ENUM_CAST(AStarGrid2D::Heuristic); + +#endif // A_STAR_GRID_2D_H diff --git a/core/math/aabb.cpp b/core/math/aabb.cpp index 4c89be7f4d..fcf245d2ad 100644 --- a/core/math/aabb.cpp +++ b/core/math/aabb.cpp @@ -30,7 +30,7 @@ #include "aabb.h" -#include "core/string/print_string.h" +#include "core/string/ustring.h" #include "core/variant/variant.h" real_t AABB::get_volume() const { @@ -76,6 +76,10 @@ bool AABB::is_equal_approx(const AABB &p_aabb) const { return position.is_equal_approx(p_aabb.position) && size.is_equal_approx(p_aabb.size); } +bool AABB::is_finite() const { + return position.is_finite() && size.is_finite(); +} + AABB AABB::intersection(const AABB &p_aabb) const { #ifdef MATH_CHECKS if (unlikely(size.x < 0 || size.y < 0 || size.z < 0 || p_aabb.size.x < 0 || p_aabb.size.y < 0 || p_aabb.size.z < 0)) { @@ -403,6 +407,7 @@ Variant AABB::intersects_segment_bind(const Vector3 &p_from, const Vector3 &p_to } return Variant(); } + Variant AABB::intersects_ray_bind(const Vector3 &p_from, const Vector3 &p_dir) const { Vector3 inters; if (intersects_ray(p_from, p_dir, &inters)) { diff --git a/core/math/aabb.h b/core/math/aabb.h index e88ba33531..9d5837ad37 100644 --- a/core/math/aabb.h +++ b/core/math/aabb.h @@ -31,7 +31,6 @@ #ifndef AABB_H #define AABB_H -#include "core/math/math_defs.h" #include "core/math/plane.h" #include "core/math/vector3.h" @@ -47,12 +46,12 @@ struct _NO_DISCARD_ AABB { Vector3 size; real_t get_volume() const; - _FORCE_INLINE_ bool has_no_volume() const { - return (size.x <= 0 || size.y <= 0 || size.z <= 0); + _FORCE_INLINE_ bool has_volume() const { + return size.x > 0.0f && size.y > 0.0f && size.z > 0.0f; } - _FORCE_INLINE_ bool has_no_surface() const { - return (size.x <= 0 && size.y <= 0 && size.z <= 0); + _FORCE_INLINE_ bool has_surface() const { + return size.x > 0.0f || size.y > 0.0f || size.z > 0.0f; } const Vector3 &get_position() const { return position; } @@ -64,6 +63,7 @@ struct _NO_DISCARD_ AABB { bool operator!=(const AABB &p_rval) const; bool is_equal_approx(const AABB &p_aabb) const; + bool is_finite() const; _FORCE_INLINE_ bool intersects(const AABB &p_aabb) const; /// Both AABBs overlap _FORCE_INLINE_ bool intersects_inclusive(const AABB &p_aabb) const; /// Both AABBs (or their faces) overlap _FORCE_INLINE_ bool encloses(const AABB &p_aabb) const; /// p_aabb is completely inside this @@ -101,7 +101,7 @@ struct _NO_DISCARD_ AABB { _FORCE_INLINE_ void expand_to(const Vector3 &p_vector); /** expand to contain a point if necessary */ _FORCE_INLINE_ AABB abs() const { - return AABB(Vector3(position.x + MIN(size.x, 0), position.y + MIN(size.y, 0), position.z + MIN(size.z, 0)), size.abs()); + return AABB(Vector3(position.x + MIN(size.x, (real_t)0), position.y + MIN(size.y, (real_t)0), position.z + MIN(size.z, (real_t)0)), size.abs()); } Variant intersects_segment_bind(const Vector3 &p_from, const Vector3 &p_to) const; diff --git a/core/math/audio_frame.h b/core/math/audio_frame.h index b3d63c0094..d06f9bef1e 100644 --- a/core/math/audio_frame.h +++ b/core/math/audio_frame.h @@ -34,7 +34,7 @@ #include "core/math/vector2.h" #include "core/typedefs.h" -static inline float undenormalise(volatile float f) { +static inline float undenormalize(volatile float f) { union { uint32_t i; float f; @@ -48,7 +48,7 @@ static inline float undenormalise(volatile float f) { } static const float AUDIO_PEAK_OFFSET = 0.0000000001f; -static const float AUDIO_MIN_PEAK_DB = -200.0f; // linear2db(AUDIO_PEAK_OFFSET) +static const float AUDIO_MIN_PEAK_DB = -200.0f; // linear_to_db(AUDIO_PEAK_OFFSET) struct AudioFrame { //left and right samples @@ -101,9 +101,9 @@ struct AudioFrame { r /= p_sample; } - _ALWAYS_INLINE_ void undenormalise() { - l = ::undenormalise(l); - r = ::undenormalise(r); + _ALWAYS_INLINE_ void undenormalize() { + l = ::undenormalize(l); + r = ::undenormalize(r); } _FORCE_INLINE_ AudioFrame lerp(const AudioFrame &p_b, float p_t) const { diff --git a/core/math/basis.cpp b/core/math/basis.cpp index f8e7c47107..d7bb025b69 100644 --- a/core/math/basis.cpp +++ b/core/math/basis.cpp @@ -31,7 +31,7 @@ #include "basis.h" #include "core/math/math_funcs.h" -#include "core/string/print_string.h" +#include "core/string/ustring.h" #define cofac(row1, col1, row2, col2) \ (rows[row1][col1] * rows[row2][col2] - rows[row1][col2] * rows[row2][col1]) @@ -142,8 +142,8 @@ bool Basis::is_symmetric() const { #endif Basis Basis::diagonalize() { -//NOTE: only implemented for symmetric matrices -//with the Jacobi iterative method +// NOTE: only implemented for symmetric matrices +// with the Jacobi iterative method #ifdef MATH_CHECKS ERR_FAIL_COND_V(!is_symmetric(), Basis()); #endif @@ -453,7 +453,7 @@ void Basis::get_rotation_axis_angle_local(Vector3 &p_axis, real_t &p_angle) cons Vector3 Basis::get_euler(EulerOrder p_order) const { switch (p_order) { - case EULER_ORDER_XYZ: { + case EulerOrder::XYZ: { // Euler angles in XYZ convention. // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix // @@ -487,8 +487,8 @@ Vector3 Basis::get_euler(EulerOrder p_order) const { euler.z = 0.0f; } return euler; - } break; - case EULER_ORDER_XZY: { + } + case EulerOrder::XZY: { // Euler angles in XZY convention. // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix // @@ -516,8 +516,8 @@ Vector3 Basis::get_euler(EulerOrder p_order) const { euler.z = -Math_PI / 2.0f; } return euler; - } break; - case EULER_ORDER_YXZ: { + } + case EulerOrder::YXZ: { // Euler angles in YXZ convention. // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix // @@ -554,8 +554,8 @@ Vector3 Basis::get_euler(EulerOrder p_order) const { } return euler; - } break; - case EULER_ORDER_YZX: { + } + case EulerOrder::YZX: { // Euler angles in YZX convention. // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix // @@ -584,7 +584,7 @@ Vector3 Basis::get_euler(EulerOrder p_order) const { } return euler; } break; - case EULER_ORDER_ZXY: { + case EulerOrder::ZXY: { // Euler angles in ZXY convention. // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix // @@ -612,7 +612,7 @@ Vector3 Basis::get_euler(EulerOrder p_order) const { } return euler; } break; - case EULER_ORDER_ZYX: { + case EulerOrder::ZYX: { // Euler angles in ZYX convention. // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix // @@ -639,7 +639,7 @@ Vector3 Basis::get_euler(EulerOrder p_order) const { euler.z = -Math::atan2(rows[0][1], rows[1][1]); } return euler; - } break; + } default: { ERR_FAIL_V_MSG(Vector3(), "Invalid parameter for get_euler(order)"); } @@ -663,22 +663,22 @@ void Basis::set_euler(const Vector3 &p_euler, EulerOrder p_order) { Basis zmat(c, -s, 0, s, c, 0, 0, 0, 1); switch (p_order) { - case EULER_ORDER_XYZ: { + case EulerOrder::XYZ: { *this = xmat * (ymat * zmat); } break; - case EULER_ORDER_XZY: { + case EulerOrder::XZY: { *this = xmat * zmat * ymat; } break; - case EULER_ORDER_YXZ: { + case EulerOrder::YXZ: { *this = ymat * xmat * zmat; } break; - case EULER_ORDER_YZX: { + case EulerOrder::YZX: { *this = ymat * zmat * xmat; } break; - case EULER_ORDER_ZXY: { + case EulerOrder::ZXY: { *this = zmat * xmat * ymat; } break; - case EULER_ORDER_ZYX: { + case EulerOrder::ZYX: { *this = zmat * ymat * xmat; } break; default: { @@ -691,6 +691,10 @@ bool Basis::is_equal_approx(const Basis &p_basis) const { return rows[0].is_equal_approx(p_basis.rows[0]) && rows[1].is_equal_approx(p_basis.rows[1]) && rows[2].is_equal_approx(p_basis.rows[2]); } +bool Basis::is_finite() const { + return rows[0].is_finite() && rows[1].is_finite() && rows[2].is_finite(); +} + bool Basis::operator==(const Basis &p_matrix) const { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { @@ -749,95 +753,33 @@ Quaternion Basis::get_quaternion() const { return Quaternion(temp[0], temp[1], temp[2], temp[3]); } -static const Basis _ortho_bases[24] = { - Basis(1, 0, 0, 0, 1, 0, 0, 0, 1), - Basis(0, -1, 0, 1, 0, 0, 0, 0, 1), - Basis(-1, 0, 0, 0, -1, 0, 0, 0, 1), - Basis(0, 1, 0, -1, 0, 0, 0, 0, 1), - Basis(1, 0, 0, 0, 0, -1, 0, 1, 0), - Basis(0, 0, 1, 1, 0, 0, 0, 1, 0), - Basis(-1, 0, 0, 0, 0, 1, 0, 1, 0), - Basis(0, 0, -1, -1, 0, 0, 0, 1, 0), - Basis(1, 0, 0, 0, -1, 0, 0, 0, -1), - Basis(0, 1, 0, 1, 0, 0, 0, 0, -1), - Basis(-1, 0, 0, 0, 1, 0, 0, 0, -1), - Basis(0, -1, 0, -1, 0, 0, 0, 0, -1), - Basis(1, 0, 0, 0, 0, 1, 0, -1, 0), - Basis(0, 0, -1, 1, 0, 0, 0, -1, 0), - Basis(-1, 0, 0, 0, 0, -1, 0, -1, 0), - Basis(0, 0, 1, -1, 0, 0, 0, -1, 0), - Basis(0, 0, 1, 0, 1, 0, -1, 0, 0), - Basis(0, -1, 0, 0, 0, 1, -1, 0, 0), - Basis(0, 0, -1, 0, -1, 0, -1, 0, 0), - Basis(0, 1, 0, 0, 0, -1, -1, 0, 0), - Basis(0, 0, 1, 0, -1, 0, 1, 0, 0), - Basis(0, 1, 0, 0, 0, 1, 1, 0, 0), - Basis(0, 0, -1, 0, 1, 0, 1, 0, 0), - Basis(0, -1, 0, 0, 0, -1, 1, 0, 0) -}; - -int Basis::get_orthogonal_index() const { - //could be sped up if i come up with a way - Basis orth = *this; - for (int i = 0; i < 3; i++) { - for (int j = 0; j < 3; j++) { - real_t v = orth[i][j]; - if (v > 0.5f) { - v = 1.0f; - } else if (v < -0.5f) { - v = -1.0f; - } else { - v = 0; - } - - orth[i][j] = v; - } - } - - for (int i = 0; i < 24; i++) { - if (_ortho_bases[i] == orth) { - return i; - } - } - - return 0; -} - -void Basis::set_orthogonal_index(int p_index) { - //there only exist 24 orthogonal bases in r3 - ERR_FAIL_INDEX(p_index, 24); - - *this = _ortho_bases[p_index]; -} - void Basis::get_axis_angle(Vector3 &r_axis, real_t &r_angle) const { /* checking this is a bad idea, because obtaining from scaled transform is a valid use case #ifdef MATH_CHECKS ERR_FAIL_COND(!is_rotation()); #endif -*/ - real_t angle, x, y, z; // variables for result - real_t angle_epsilon = 0.1; // margin to distinguish between 0 and 180 degrees - - if ((Math::abs(rows[1][0] - rows[0][1]) < CMP_EPSILON) && (Math::abs(rows[2][0] - rows[0][2]) < CMP_EPSILON) && (Math::abs(rows[2][1] - rows[1][2]) < CMP_EPSILON)) { - // singularity found - // first check for identity matrix which must have +1 for all terms - // in leading diagonal and zero in other terms - if ((Math::abs(rows[1][0] + rows[0][1]) < angle_epsilon) && (Math::abs(rows[2][0] + rows[0][2]) < angle_epsilon) && (Math::abs(rows[2][1] + rows[1][2]) < angle_epsilon) && (Math::abs(rows[0][0] + rows[1][1] + rows[2][2] - 3) < angle_epsilon)) { - // this singularity is identity matrix so angle = 0 + */ + + // https://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToAngle/index.htm + real_t x, y, z; // Variables for result. + if (Math::is_zero_approx(rows[0][1] - rows[1][0]) && Math::is_zero_approx(rows[0][2] - rows[2][0]) && Math::is_zero_approx(rows[1][2] - rows[2][1])) { + // Singularity found. + // First check for identity matrix which must have +1 for all terms in leading diagonal and zero in other terms. + if (is_diagonal() && (Math::abs(rows[0][0] + rows[1][1] + rows[2][2] - 3) < 3 * CMP_EPSILON)) { + // This singularity is identity matrix so angle = 0. r_axis = Vector3(0, 1, 0); r_angle = 0; return; } - // otherwise this singularity is angle = 180 - angle = Math_PI; + // Otherwise this singularity is angle = 180. real_t xx = (rows[0][0] + 1) / 2; real_t yy = (rows[1][1] + 1) / 2; real_t zz = (rows[2][2] + 1) / 2; - real_t xy = (rows[1][0] + rows[0][1]) / 4; - real_t xz = (rows[2][0] + rows[0][2]) / 4; - real_t yz = (rows[2][1] + rows[1][2]) / 4; - if ((xx > yy) && (xx > zz)) { // rows[0][0] is the largest diagonal term + real_t xy = (rows[0][1] + rows[1][0]) / 4; + real_t xz = (rows[0][2] + rows[2][0]) / 4; + real_t yz = (rows[1][2] + rows[2][1]) / 4; + + if ((xx > yy) && (xx > zz)) { // rows[0][0] is the largest diagonal term. if (xx < CMP_EPSILON) { x = 0; y = Math_SQRT12; @@ -847,7 +789,7 @@ void Basis::get_axis_angle(Vector3 &r_axis, real_t &r_angle) const { y = xy / x; z = xz / x; } - } else if (yy > zz) { // rows[1][1] is the largest diagonal term + } else if (yy > zz) { // rows[1][1] is the largest diagonal term. if (yy < CMP_EPSILON) { x = Math_SQRT12; y = 0; @@ -857,7 +799,7 @@ void Basis::get_axis_angle(Vector3 &r_axis, real_t &r_angle) const { x = xy / y; z = yz / y; } - } else { // rows[2][2] is the largest diagonal term so base result on this + } else { // rows[2][2] is the largest diagonal term so base result on this. if (zz < CMP_EPSILON) { x = Math_SQRT12; y = Math_SQRT12; @@ -869,22 +811,24 @@ void Basis::get_axis_angle(Vector3 &r_axis, real_t &r_angle) const { } } r_axis = Vector3(x, y, z); - r_angle = angle; + r_angle = Math_PI; return; } - // as we have reached here there are no singularities so we can handle normally - real_t s = Math::sqrt((rows[1][2] - rows[2][1]) * (rows[1][2] - rows[2][1]) + (rows[2][0] - rows[0][2]) * (rows[2][0] - rows[0][2]) + (rows[0][1] - rows[1][0]) * (rows[0][1] - rows[1][0])); // s=|axis||sin(angle)|, used to normalise + // As we have reached here there are no singularities so we can handle normally. + double s = Math::sqrt((rows[2][1] - rows[1][2]) * (rows[2][1] - rows[1][2]) + (rows[0][2] - rows[2][0]) * (rows[0][2] - rows[2][0]) + (rows[1][0] - rows[0][1]) * (rows[1][0] - rows[0][1])); // Used to normalize. - angle = Math::acos((rows[0][0] + rows[1][1] + rows[2][2] - 1) / 2); - if (angle < 0) { - s = -s; + if (Math::abs(s) < CMP_EPSILON) { + // Prevent divide by zero, should not happen if matrix is orthogonal and should be caught by singularity test above. + s = 1; } + x = (rows[2][1] - rows[1][2]) / s; y = (rows[0][2] - rows[2][0]) / s; z = (rows[1][0] - rows[0][1]) / s; r_axis = Vector3(x, y, z); - r_angle = angle; + // CLAMP to avoid NaN if the value passed to acos is not in [0,1]. + r_angle = Math::acos(CLAMP((rows[0][0] + rows[1][1] + rows[2][2] - 1) / 2, (real_t)0.0, (real_t)1.0)); } void Basis::set_quaternion(const Quaternion &p_quaternion) { @@ -1094,13 +1038,13 @@ void Basis::rotate_sh(real_t *p_values) { Basis Basis::looking_at(const Vector3 &p_target, const Vector3 &p_up) { #ifdef MATH_CHECKS - ERR_FAIL_COND_V_MSG(p_target.is_equal_approx(Vector3()), Basis(), "The target vector can't be zero."); - ERR_FAIL_COND_V_MSG(p_up.is_equal_approx(Vector3()), Basis(), "The up vector can't be zero."); + ERR_FAIL_COND_V_MSG(p_target.is_zero_approx(), Basis(), "The target vector can't be zero."); + ERR_FAIL_COND_V_MSG(p_up.is_zero_approx(), Basis(), "The up vector can't be zero."); #endif Vector3 v_z = -p_target.normalized(); Vector3 v_x = p_up.cross(v_z); #ifdef MATH_CHECKS - ERR_FAIL_COND_V_MSG(v_x.is_equal_approx(Vector3()), Basis(), "The target vector and up vector can't be parallel to each other."); + ERR_FAIL_COND_V_MSG(v_x.is_zero_approx(), Basis(), "The target vector and up vector can't be parallel to each other."); #endif v_x.normalize(); Vector3 v_y = v_z.cross(v_x); diff --git a/core/math/basis.h b/core/math/basis.h index 4be325cdd2..a1d9fccef1 100644 --- a/core/math/basis.h +++ b/core/math/basis.h @@ -56,15 +56,6 @@ struct _NO_DISCARD_ Basis { _FORCE_INLINE_ real_t determinant() const; - enum EulerOrder { - EULER_ORDER_XYZ, - EULER_ORDER_XZY, - EULER_ORDER_YXZ, - EULER_ORDER_YZX, - EULER_ORDER_ZXY, - EULER_ORDER_ZYX - }; - void from_z(const Vector3 &p_z); void rotate(const Vector3 &p_axis, real_t p_angle); @@ -73,13 +64,13 @@ struct _NO_DISCARD_ Basis { void rotate_local(const Vector3 &p_axis, real_t p_angle); Basis rotated_local(const Vector3 &p_axis, real_t p_angle) const; - void rotate(const Vector3 &p_euler, EulerOrder p_order = EULER_ORDER_YXZ); - Basis rotated(const Vector3 &p_euler, EulerOrder p_order = EULER_ORDER_YXZ) const; + void rotate(const Vector3 &p_euler, EulerOrder p_order = EulerOrder::YXZ); + Basis rotated(const Vector3 &p_euler, EulerOrder p_order = EulerOrder::YXZ) const; void rotate(const Quaternion &p_quaternion); Basis rotated(const Quaternion &p_quaternion) const; - Vector3 get_euler_normalized(EulerOrder p_order = EULER_ORDER_YXZ) const; + Vector3 get_euler_normalized(EulerOrder p_order = EulerOrder::YXZ) const; void get_rotation_axis_angle(Vector3 &p_axis, real_t &p_angle) const; void get_rotation_axis_angle_local(Vector3 &p_axis, real_t &p_angle) const; Quaternion get_rotation_quaternion() const; @@ -88,9 +79,9 @@ struct _NO_DISCARD_ Basis { Vector3 rotref_posscale_decomposition(Basis &rotref) const; - Vector3 get_euler(EulerOrder p_order = EULER_ORDER_YXZ) const; - void set_euler(const Vector3 &p_euler, EulerOrder p_order = EULER_ORDER_YXZ); - static Basis from_euler(const Vector3 &p_euler, EulerOrder p_order = EULER_ORDER_YXZ) { + Vector3 get_euler(EulerOrder p_order = EulerOrder::YXZ) const; + void set_euler(const Vector3 &p_euler, EulerOrder p_order = EulerOrder::YXZ); + static Basis from_euler(const Vector3 &p_euler, EulerOrder p_order = EulerOrder::YXZ) { Basis b; b.set_euler(p_euler, p_order); return b; @@ -119,7 +110,7 @@ struct _NO_DISCARD_ Basis { Vector3 get_scale_local() const; void set_axis_angle_scale(const Vector3 &p_axis, real_t p_angle, const Vector3 &p_scale); - void set_euler_scale(const Vector3 &p_euler, const Vector3 &p_scale, EulerOrder p_order = EULER_ORDER_YXZ); + void set_euler_scale(const Vector3 &p_euler, const Vector3 &p_scale, EulerOrder p_order = EulerOrder::YXZ); void set_quaternion_scale(const Quaternion &p_quaternion, const Vector3 &p_scale); // transposed dot products @@ -134,6 +125,7 @@ struct _NO_DISCARD_ Basis { } bool is_equal_approx(const Basis &p_basis) const; + bool is_finite() const; bool operator==(const Basis &p_matrix) const; bool operator!=(const Basis &p_matrix) const; @@ -149,9 +141,6 @@ struct _NO_DISCARD_ Basis { _FORCE_INLINE_ void operator*=(const real_t p_val); _FORCE_INLINE_ Basis operator*(const real_t p_val) const; - int get_orthogonal_index() const; - void set_orthogonal_index(int p_index); - bool is_orthogonal() const; bool is_diagonal() const; bool is_rotation() const; @@ -241,10 +230,8 @@ struct _NO_DISCARD_ Basis { Basis(const Vector3 &p_axis, real_t p_angle, const Vector3 &p_scale) { set_axis_angle_scale(p_axis, p_angle, p_scale); } static Basis from_scale(const Vector3 &p_scale); - _FORCE_INLINE_ Basis(const Vector3 &row0, const Vector3 &row1, const Vector3 &row2) { - rows[0] = row0; - rows[1] = row1; - rows[2] = row2; + _FORCE_INLINE_ Basis(const Vector3 &p_x_axis, const Vector3 &p_y_axis, const Vector3 &p_z_axis) { + set_columns(p_x_axis, p_y_axis, p_z_axis); } _FORCE_INLINE_ Basis() {} diff --git a/core/math/bvh_abb.h b/core/math/bvh_abb.h index 8a44f1c4da..699f7de604 100644 --- a/core/math/bvh_abb.h +++ b/core/math/bvh_abb.h @@ -251,7 +251,9 @@ struct BVH_ABB { void expand(real_t p_change) { POINT change; - change.set_all(p_change); + for (int axis = 0; axis < POINT::AXIS_COUNT; ++axis) { + change[axis] = p_change; + } grow(change); } @@ -262,7 +264,9 @@ struct BVH_ABB { } void set_to_max_opposite_extents() { - neg_max.set_all(FLT_MAX); + for (int axis = 0; axis < POINT::AXIS_COUNT; ++axis) { + neg_max[axis] = FLT_MAX; + } min = neg_max; } diff --git a/core/math/bvh_split.inc b/core/math/bvh_split.inc index ff07166d4a..180bbfb511 100644 --- a/core/math/bvh_split.inc +++ b/core/math/bvh_split.inc @@ -13,7 +13,7 @@ void _split_inform_references(uint32_t p_node_id) { void _split_leaf_sort_groups_simple(int &num_a, int &num_b, uint16_t *group_a, uint16_t *group_b, const BVHABB_CLASS *temp_bounds, const BVHABB_CLASS full_bound) { // special case for low leaf sizes .. should static compile out - if (MAX_ITEMS < 4) { + if constexpr (MAX_ITEMS < 4) { uint32_t ind = group_a[0]; // add to b @@ -34,7 +34,7 @@ void _split_leaf_sort_groups_simple(int &num_a, int &num_b, uint16_t *group_a, u order[POINT::AXIS_COUNT - 1] = size.max_axis_index(); static_assert(POINT::AXIS_COUNT <= 3, "BVH POINT::AXIS_COUNT has unexpected size"); - if (POINT::AXIS_COUNT == 3) { + if constexpr (POINT::AXIS_COUNT == 3) { order[1] = 3 - (order[0] + order[2]); } diff --git a/core/math/bvh_structs.inc b/core/math/bvh_structs.inc index 58c8f0479a..06f6e5d05d 100644 --- a/core/math/bvh_structs.inc +++ b/core/math/bvh_structs.inc @@ -100,7 +100,11 @@ public: num_items++; return id; } +#ifdef DEV_ENABLED return -1; +#else + ERR_FAIL_V_MSG(0, "BVH request_item error."); +#endif } }; diff --git a/core/math/bvh_tree.h b/core/math/bvh_tree.h index cdb2bb4413..3836e92a83 100644 --- a/core/math/bvh_tree.h +++ b/core/math/bvh_tree.h @@ -43,7 +43,6 @@ #include "core/math/bvh_abb.h" #include "core/math/geometry_3d.h" #include "core/math/vector3.h" -#include "core/string/print_string.h" #include "core/templates/local_vector.h" #include "core/templates/pooled_list.h" #include <limits.h> @@ -235,7 +234,7 @@ private: // no need to keep back references for children at the moment - uint32_t sibling_id; // always a node id, as tnode is never a leaf + uint32_t sibling_id = 0; // always a node id, as tnode is never a leaf bool sibling_present = false; // if there are more children, or this is the root node, don't try and delete diff --git a/core/math/color.cpp b/core/math/color.cpp index 4bdeafd2f2..f223853f6b 100644 --- a/core/math/color.cpp +++ b/core/math/color.cpp @@ -32,85 +32,85 @@ #include "color_names.inc" #include "core/math/math_funcs.h" -#include "core/string/print_string.h" +#include "core/string/ustring.h" #include "core/templates/rb_map.h" #include "thirdparty/misc/ok_color.h" uint32_t Color::to_argb32() const { - uint32_t c = (uint8_t)Math::round(a * 255); + uint32_t c = (uint8_t)Math::round(a * 255.0f); c <<= 8; - c |= (uint8_t)Math::round(r * 255); + c |= (uint8_t)Math::round(r * 255.0f); c <<= 8; - c |= (uint8_t)Math::round(g * 255); + c |= (uint8_t)Math::round(g * 255.0f); c <<= 8; - c |= (uint8_t)Math::round(b * 255); + c |= (uint8_t)Math::round(b * 255.0f); return c; } uint32_t Color::to_abgr32() const { - uint32_t c = (uint8_t)Math::round(a * 255); + uint32_t c = (uint8_t)Math::round(a * 255.0f); c <<= 8; - c |= (uint8_t)Math::round(b * 255); + c |= (uint8_t)Math::round(b * 255.0f); c <<= 8; - c |= (uint8_t)Math::round(g * 255); + c |= (uint8_t)Math::round(g * 255.0f); c <<= 8; - c |= (uint8_t)Math::round(r * 255); + c |= (uint8_t)Math::round(r * 255.0f); return c; } uint32_t Color::to_rgba32() const { - uint32_t c = (uint8_t)Math::round(r * 255); + uint32_t c = (uint8_t)Math::round(r * 255.0f); c <<= 8; - c |= (uint8_t)Math::round(g * 255); + c |= (uint8_t)Math::round(g * 255.0f); c <<= 8; - c |= (uint8_t)Math::round(b * 255); + c |= (uint8_t)Math::round(b * 255.0f); c <<= 8; - c |= (uint8_t)Math::round(a * 255); + c |= (uint8_t)Math::round(a * 255.0f); return c; } uint64_t Color::to_abgr64() const { - uint64_t c = (uint16_t)Math::round(a * 65535); + uint64_t c = (uint16_t)Math::round(a * 65535.0f); c <<= 16; - c |= (uint16_t)Math::round(b * 65535); + c |= (uint16_t)Math::round(b * 65535.0f); c <<= 16; - c |= (uint16_t)Math::round(g * 65535); + c |= (uint16_t)Math::round(g * 65535.0f); c <<= 16; - c |= (uint16_t)Math::round(r * 65535); + c |= (uint16_t)Math::round(r * 65535.0f); return c; } uint64_t Color::to_argb64() const { - uint64_t c = (uint16_t)Math::round(a * 65535); + uint64_t c = (uint16_t)Math::round(a * 65535.0f); c <<= 16; - c |= (uint16_t)Math::round(r * 65535); + c |= (uint16_t)Math::round(r * 65535.0f); c <<= 16; - c |= (uint16_t)Math::round(g * 65535); + c |= (uint16_t)Math::round(g * 65535.0f); c <<= 16; - c |= (uint16_t)Math::round(b * 65535); + c |= (uint16_t)Math::round(b * 65535.0f); return c; } uint64_t Color::to_rgba64() const { - uint64_t c = (uint16_t)Math::round(r * 65535); + uint64_t c = (uint16_t)Math::round(r * 65535.0f); c <<= 16; - c |= (uint16_t)Math::round(g * 65535); + c |= (uint16_t)Math::round(g * 65535.0f); c <<= 16; - c |= (uint16_t)Math::round(b * 65535); + c |= (uint16_t)Math::round(b * 65535.0f); c <<= 16; - c |= (uint16_t)Math::round(a * 65535); + c |= (uint16_t)Math::round(a * 65535.0f); return c; } String _to_hex(float p_val) { - int v = Math::round(p_val * 255); + int v = Math::round(p_val * 255.0f); v = CLAMP(v, 0, 255); String ret; @@ -150,8 +150,8 @@ float Color::get_h() const { float delta = max - min; - if (delta == 0) { - return 0; + if (delta == 0.0f) { + return 0.0f; } float h; @@ -164,7 +164,7 @@ float Color::get_h() const { } h /= 6.0f; - if (h < 0) { + if (h < 0.0f) { h += 1.0f; } @@ -179,7 +179,7 @@ float Color::get_s() const { float delta = max - min; - return (max != 0) ? (delta / max) : 0; + return (max != 0.0f) ? (delta / max) : 0.0f; } float Color::get_v() const { @@ -193,7 +193,7 @@ void Color::set_hsv(float p_h, float p_s, float p_v, float p_alpha) { float f, p, q, t; a = p_alpha; - if (p_s == 0) { + if (p_s == 0.0f) { // Achromatic (grey) r = g = b = p_v; return; @@ -204,9 +204,9 @@ void Color::set_hsv(float p_h, float p_s, float p_v, float p_alpha) { i = Math::floor(p_h); f = p_h - i; - p = p_v * (1 - p_s); - q = p_v * (1 - p_s * f); - t = p_v * (1 - p_s * (1 - f)); + p = p_v * (1.0f - p_s); + q = p_v * (1.0f - p_s * f); + t = p_v * (1.0f - p_s * (1.0f - f)); switch (i) { case 0: // Red is the dominant color @@ -347,7 +347,7 @@ Color Color::html(const String &p_rgba) { ERR_FAIL_V_MSG(Color(), "Invalid color code: " + p_rgba + "."); } - float r, g, b, a = 1.0; + float r, g, b, a = 1.0f; if (is_shorthand) { r = _parse_col4(color, 0) / 15.0f; g = _parse_col4(color, 1) / 15.0f; @@ -363,10 +363,10 @@ Color Color::html(const String &p_rgba) { a = _parse_col8(color, 6) / 255.0f; } } - ERR_FAIL_COND_V_MSG(r < 0, Color(), "Invalid color code: " + p_rgba + "."); - ERR_FAIL_COND_V_MSG(g < 0, Color(), "Invalid color code: " + p_rgba + "."); - ERR_FAIL_COND_V_MSG(b < 0, Color(), "Invalid color code: " + p_rgba + "."); - ERR_FAIL_COND_V_MSG(a < 0, Color(), "Invalid color code: " + p_rgba + "."); + ERR_FAIL_COND_V_MSG(r < 0.0f, Color(), "Invalid color code: " + p_rgba + "."); + ERR_FAIL_COND_V_MSG(g < 0.0f, Color(), "Invalid color code: " + p_rgba + "."); + ERR_FAIL_COND_V_MSG(b < 0.0f, Color(), "Invalid color code: " + p_rgba + "."); + ERR_FAIL_COND_V_MSG(a < 0.0f, Color(), "Invalid color code: " + p_rgba + "."); return Color(r, g, b, a); } @@ -474,7 +474,7 @@ Color Color::from_rgbe9995(uint32_t p_rgbe) { float g = (p_rgbe >> 9) & 0x1ff; float b = (p_rgbe >> 18) & 0x1ff; float e = (p_rgbe >> 27); - float m = Math::pow(2, e - 15.0f - 9.0f); + float m = Math::pow(2.0f, e - 15.0f - 9.0f); float rd = r * m; float gd = g * m; diff --git a/core/math/color.h b/core/math/color.h index 65036f74cc..a23a4953ce 100644 --- a/core/math/color.h +++ b/core/math/color.h @@ -32,7 +32,8 @@ #define COLOR_H #include "core/math/math_funcs.h" -#include "core/string/ustring.h" + +class String; struct _NO_DISCARD_ Color { union { @@ -55,11 +56,11 @@ struct _NO_DISCARD_ Color { float get_h() const; float get_s() const; float get_v() const; - void set_hsv(float p_h, float p_s, float p_v, float p_alpha = 1.0); + void set_hsv(float p_h, float p_s, float p_v, float p_alpha = 1.0f); float get_ok_hsl_h() const; float get_ok_hsl_s() const; float get_ok_hsl_l() const; - void set_ok_hsl(float p_h, float p_s, float p_l, float p_alpha = 1.0); + void set_ok_hsl(float p_h, float p_s, float p_l, float p_alpha = 1.0f); _FORCE_INLINE_ float &operator[](int p_idx) { return components[p_idx]; @@ -175,9 +176,9 @@ struct _NO_DISCARD_ Color { _FORCE_INLINE_ Color srgb_to_linear() const { return Color( - r < 0.04045f ? r * (1.0 / 12.92) : Math::pow((r + 0.055f) * (float)(1.0 / (1 + 0.055)), 2.4f), - g < 0.04045f ? g * (1.0 / 12.92) : Math::pow((g + 0.055f) * (float)(1.0 / (1 + 0.055)), 2.4f), - b < 0.04045f ? b * (1.0 / 12.92) : Math::pow((b + 0.055f) * (float)(1.0 / (1 + 0.055)), 2.4f), + r < 0.04045f ? r * (1.0f / 12.92f) : Math::pow((r + 0.055f) * (float)(1.0 / (1.0 + 0.055)), 2.4f), + g < 0.04045f ? g * (1.0f / 12.92f) : Math::pow((g + 0.055f) * (float)(1.0 / (1.0 + 0.055)), 2.4f), + b < 0.04045f ? b * (1.0f / 12.92f) : Math::pow((b + 0.055f) * (float)(1.0 / (1.0 + 0.055)), 2.4f), a); } _FORCE_INLINE_ Color linear_to_srgb() const { @@ -198,11 +199,11 @@ struct _NO_DISCARD_ Color { static String get_named_color_name(int p_idx); static Color get_named_color(int p_idx); static Color from_string(const String &p_string, const Color &p_default); - static Color from_hsv(float p_h, float p_s, float p_v, float p_alpha = 1.0); - static Color from_ok_hsl(float p_h, float p_s, float p_l, float p_alpha = 1.0); + static Color from_hsv(float p_h, float p_s, float p_v, float p_alpha = 1.0f); + static Color from_ok_hsl(float p_h, float p_s, float p_l, float p_alpha = 1.0f); static Color from_rgbe9995(uint32_t p_rgbe); - _FORCE_INLINE_ bool operator<(const Color &p_color) const; //used in set keys + _FORCE_INLINE_ bool operator<(const Color &p_color) const; // Used in set keys. operator String() const; // For the binder. diff --git a/core/math/convex_hull.cpp b/core/math/convex_hull.cpp index 996f4f4d67..561970d2ee 100644 --- a/core/math/convex_hull.cpp +++ b/core/math/convex_hull.cpp @@ -62,6 +62,7 @@ subject to the following restrictions: #include "core/math/aabb.h" #include "core/math/math_defs.h" #include "core/os/memory.h" +#include "core/templates/oa_hash_map.h" #include "core/templates/paged_allocator.h" #include <string.h> @@ -2252,19 +2253,62 @@ Error ConvexHullComputer::convex_hull(const Vector<Vector3> &p_points, Geometry3 r_mesh.vertices = ch.vertices; + // Tag which face each edge belongs to + LocalVector<int32_t> edge_faces; + edge_faces.resize(ch.edges.size()); + + for (uint32_t i = 0; i < ch.edges.size(); i++) { + edge_faces[i] = -1; + } + + for (uint32_t i = 0; i < ch.faces.size(); i++) { + const Edge *e_start = &ch.edges[ch.faces[i]]; + const Edge *e = e_start; + do { + int64_t ofs = e - ch.edges.ptr(); + edge_faces[ofs] = i; + + e = e->get_next_edge_of_face(); + } while (e != e_start); + } + // Copy the edges over. There's two "half-edges" for every edge, so we pick only one of them. r_mesh.edges.resize(ch.edges.size() / 2); + OAHashMap<uint64_t, int32_t> edge_map; + edge_map.reserve(ch.edges.size() * 4); // The higher the capacity, the faster the insert + uint32_t edges_copied = 0; for (uint32_t i = 0; i < ch.edges.size(); i++) { + ERR_CONTINUE(edge_faces[i] == -1); // Sanity check + uint32_t a = (&ch.edges[i])->get_source_vertex(); uint32_t b = (&ch.edges[i])->get_target_vertex(); if (a < b) { // Copy only the "canonical" edge. For the reverse edge, this will be false. ERR_BREAK(edges_copied >= (uint32_t)r_mesh.edges.size()); - r_mesh.edges.write[edges_copied].a = a; - r_mesh.edges.write[edges_copied].b = b; + r_mesh.edges[edges_copied].vertex_a = a; + r_mesh.edges[edges_copied].vertex_b = b; + r_mesh.edges[edges_copied].face_a = edge_faces[i]; + r_mesh.edges[edges_copied].face_b = -1; + + uint64_t key = a; + key <<= 32; + key |= b; + edge_map.insert(key, edges_copied); + edges_copied++; + } else { + uint64_t key = b; + key <<= 32; + key |= a; + int32_t index; + if (!edge_map.lookup(key, index)) { + ERR_PRINT("Invalid edge"); + } else { + r_mesh.edges[index].face_b = edge_faces[i]; + } } } + if (edges_copied != (uint32_t)r_mesh.edges.size()) { ERR_PRINT("Invalid edge count."); } @@ -2273,7 +2317,7 @@ Error ConvexHullComputer::convex_hull(const Vector<Vector3> &p_points, Geometry3 for (uint32_t i = 0; i < ch.faces.size(); i++) { const Edge *e_start = &ch.edges[ch.faces[i]]; const Edge *e = e_start; - Geometry3D::MeshData::Face &face = r_mesh.faces.write[i]; + Geometry3D::MeshData::Face &face = r_mesh.faces[i]; do { face.indices.push_back(e->get_target_vertex()); @@ -2284,8 +2328,8 @@ Error ConvexHullComputer::convex_hull(const Vector<Vector3> &p_points, Geometry3 // reverse indices: Godot wants clockwise, but this is counter-clockwise if (face.indices.size() > 2) { // reverse all but the first index. - int *indices = face.indices.ptrw(); - for (int c = 0; c < (face.indices.size() - 1) / 2; c++) { + int *indices = face.indices.ptr(); + for (uint32_t c = 0; c < (face.indices.size() - 1) / 2; c++) { SWAP(indices[c + 1], indices[face.indices.size() - 1 - c]); } } diff --git a/core/math/convex_hull.h b/core/math/convex_hull.h index bd86fe0eba..ab6671a7d0 100644 --- a/core/math/convex_hull.h +++ b/core/math/convex_hull.h @@ -28,6 +28,9 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifndef CONVEX_HULL_H +#define CONVEX_HULL_H + /* Copyright (c) 2011 Ole Kniemeyer, MAXON, www.maxon.net This software is provided 'as-is', without any express or implied warranty. @@ -40,9 +43,6 @@ subject to the following restrictions: 3. This notice may not be removed or altered from any source distribution. */ -#ifndef CONVEX_HULL_H -#define CONVEX_HULL_H - #include "core/math/geometry_3d.h" #include "core/math/vector3.h" #include "core/templates/local_vector.h" @@ -62,6 +62,10 @@ public: friend class ConvexHullComputer; public: + int32_t get_next_relative() const { + return next; + } + int32_t get_source_vertex() const { return (this + reverse)->target_vertex; } @@ -86,7 +90,7 @@ public: }; // Vertices of the output hull - Vector<Vector3> vertices; + LocalVector<Vector3> vertices; // Edges of the output hull LocalVector<Edge> edges; diff --git a/core/math/delaunay_3d.h b/core/math/delaunay_3d.h index 898c3c2d91..3f8fe09445 100644 --- a/core/math/delaunay_3d.h +++ b/core/math/delaunay_3d.h @@ -35,7 +35,6 @@ #include "core/math/aabb.h" #include "core/math/projection.h" #include "core/math/vector3.h" -#include "core/string/print_string.h" #include "core/templates/local_vector.h" #include "core/templates/oa_hash_map.h" #include "core/templates/vector.h" @@ -186,25 +185,25 @@ class Delaunay3D { Projection cm; - cm.matrix[0][0] = p_points[p_simplex.points[0]].x; - cm.matrix[0][1] = p_points[p_simplex.points[1]].x; - cm.matrix[0][2] = p_points[p_simplex.points[2]].x; - cm.matrix[0][3] = p_points[p_simplex.points[3]].x; - - cm.matrix[1][0] = p_points[p_simplex.points[0]].y; - cm.matrix[1][1] = p_points[p_simplex.points[1]].y; - cm.matrix[1][2] = p_points[p_simplex.points[2]].y; - cm.matrix[1][3] = p_points[p_simplex.points[3]].y; - - cm.matrix[2][0] = p_points[p_simplex.points[0]].z; - cm.matrix[2][1] = p_points[p_simplex.points[1]].z; - cm.matrix[2][2] = p_points[p_simplex.points[2]].z; - cm.matrix[2][3] = p_points[p_simplex.points[3]].z; - - cm.matrix[3][0] = 1.0; - cm.matrix[3][1] = 1.0; - cm.matrix[3][2] = 1.0; - cm.matrix[3][3] = 1.0; + cm.columns[0][0] = p_points[p_simplex.points[0]].x; + cm.columns[0][1] = p_points[p_simplex.points[1]].x; + cm.columns[0][2] = p_points[p_simplex.points[2]].x; + cm.columns[0][3] = p_points[p_simplex.points[3]].x; + + cm.columns[1][0] = p_points[p_simplex.points[0]].y; + cm.columns[1][1] = p_points[p_simplex.points[1]].y; + cm.columns[1][2] = p_points[p_simplex.points[2]].y; + cm.columns[1][3] = p_points[p_simplex.points[3]].y; + + cm.columns[2][0] = p_points[p_simplex.points[0]].z; + cm.columns[2][1] = p_points[p_simplex.points[1]].z; + cm.columns[2][2] = p_points[p_simplex.points[2]].z; + cm.columns[2][3] = p_points[p_simplex.points[3]].z; + + cm.columns[3][0] = 1.0; + cm.columns[3][1] = 1.0; + cm.columns[3][2] = 1.0; + cm.columns[3][3] = 1.0; return ABS(cm.determinant()) <= CMP_EPSILON; } diff --git a/core/math/expression.cpp b/core/math/expression.cpp index e230b69dc9..26b809e7f2 100644 --- a/core/math/expression.cpp +++ b/core/math/expression.cpp @@ -560,7 +560,7 @@ const char *Expression::token_name[TK_MAX] = { }; Expression::ENode *Expression::_parse_expression() { - Vector<ExpressionNode> expression; + Vector<ExpressionNode> expression_nodes; while (true) { //keep appending stuff to expression @@ -838,14 +838,14 @@ Expression::ENode *Expression::_parse_expression() { ExpressionNode e; e.is_op = true; e.op = Variant::OP_NEGATE; - expression.push_back(e); + expression_nodes.push_back(e); continue; } break; case TK_OP_NOT: { ExpressionNode e; e.is_op = true; e.op = Variant::OP_NOT; - expression.push_back(e); + expression_nodes.push_back(e); continue; } break; @@ -960,7 +960,7 @@ Expression::ENode *Expression::_parse_expression() { ExpressionNode e; e.is_op = false; e.node = expr; - expression.push_back(e); + expression_nodes.push_back(e); } //ok finally look for an operator @@ -1054,19 +1054,19 @@ Expression::ENode *Expression::_parse_expression() { ExpressionNode e; e.is_op = true; e.op = op; - expression.push_back(e); + expression_nodes.push_back(e); } } /* Reduce the set of expressions and place them in an operator tree, respecting precedence */ - while (expression.size() > 1) { + while (expression_nodes.size() > 1) { int next_op = -1; int min_priority = 0xFFFFF; bool is_unary = false; - for (int i = 0; i < expression.size(); i++) { - if (!expression[i].is_op) { + for (int i = 0; i < expression_nodes.size(); i++) { + if (!expression_nodes[i].is_op) { continue; } @@ -1074,7 +1074,7 @@ Expression::ENode *Expression::_parse_expression() { bool unary = false; - switch (expression[i].op) { + switch (expression_nodes[i].op) { case Variant::OP_POWER: priority = 0; break; @@ -1130,7 +1130,7 @@ Expression::ENode *Expression::_parse_expression() { priority = 14; break; default: { - _set_error("Parser bug, invalid operator in expression: " + itos(expression[i].op)); + _set_error("Parser bug, invalid operator in expression: " + itos(expression_nodes[i].op)); return nullptr; } } @@ -1153,9 +1153,9 @@ Expression::ENode *Expression::_parse_expression() { // OK! create operator.. if (is_unary) { int expr_pos = next_op; - while (expression[expr_pos].is_op) { + while (expression_nodes[expr_pos].is_op) { expr_pos++; - if (expr_pos == expression.size()) { + if (expr_pos == expression_nodes.size()) { //can happen.. _set_error("Unexpected end of expression..."); return nullptr; @@ -1165,29 +1165,29 @@ Expression::ENode *Expression::_parse_expression() { //consecutively do unary operators for (int i = expr_pos - 1; i >= next_op; i--) { OperatorNode *op = alloc_node<OperatorNode>(); - op->op = expression[i].op; - op->nodes[0] = expression[i + 1].node; + op->op = expression_nodes[i].op; + op->nodes[0] = expression_nodes[i + 1].node; op->nodes[1] = nullptr; - expression.write[i].is_op = false; - expression.write[i].node = op; - expression.remove_at(i + 1); + expression_nodes.write[i].is_op = false; + expression_nodes.write[i].node = op; + expression_nodes.remove_at(i + 1); } } else { - if (next_op < 1 || next_op >= (expression.size() - 1)) { + if (next_op < 1 || next_op >= (expression_nodes.size() - 1)) { _set_error("Parser bug..."); ERR_FAIL_V(nullptr); } OperatorNode *op = alloc_node<OperatorNode>(); - op->op = expression[next_op].op; + op->op = expression_nodes[next_op].op; - if (expression[next_op - 1].is_op) { + if (expression_nodes[next_op - 1].is_op) { _set_error("Parser bug..."); ERR_FAIL_V(nullptr); } - if (expression[next_op + 1].is_op) { + if (expression_nodes[next_op + 1].is_op) { // this is not invalid and can really appear // but it becomes invalid anyway because no binary op // can be followed by a unary op in a valid combination, @@ -1197,17 +1197,17 @@ Expression::ENode *Expression::_parse_expression() { return nullptr; } - op->nodes[0] = expression[next_op - 1].node; //expression goes as left - op->nodes[1] = expression[next_op + 1].node; //next expression goes as right + op->nodes[0] = expression_nodes[next_op - 1].node; //expression goes as left + op->nodes[1] = expression_nodes[next_op + 1].node; //next expression goes as right //replace all 3 nodes by this operator and make it an expression - expression.write[next_op - 1].node = op; - expression.remove_at(next_op); - expression.remove_at(next_op); + expression_nodes.write[next_op - 1].node = op; + expression_nodes.remove_at(next_op); + expression_nodes.remove_at(next_op); } } - return expression[0].node; + return expression_nodes[0].node; } bool Expression::_compile_expression() { @@ -1420,7 +1420,7 @@ bool Expression::_execute(const Array &p_inputs, Object *p_instance, Expression: Callable::CallError ce; Variant::call_utility_function(bifunc->func, &r_ret, (const Variant **)argp.ptr(), argp.size(), ce); if (ce.error != Callable::CallError::CALL_OK) { - r_error_str = "Builtin Call Failed. " + Variant::get_call_error_text(bifunc->func, (const Variant **)argp.ptr(), argp.size(), ce); + r_error_str = "Builtin call failed: " + Variant::get_call_error_text(bifunc->func, (const Variant **)argp.ptr(), argp.size(), ce); return true; } diff --git a/core/math/geometry_3d.cpp b/core/math/geometry_3d.cpp index ec96753c79..548b9e4620 100644 --- a/core/math/geometry_3d.cpp +++ b/core/math/geometry_3d.cpp @@ -30,29 +30,132 @@ #include "geometry_3d.h" -#include "core/string/print_string.h" - #include "thirdparty/misc/clipper.hpp" #include "thirdparty/misc/polypartition.h" +void Geometry3D::get_closest_points_between_segments(const Vector3 &p_p0, const Vector3 &p_p1, const Vector3 &p_q0, const Vector3 &p_q1, Vector3 &r_ps, Vector3 &r_qt) { + // Based on David Eberly's Computation of Distance Between Line Segments algorithm. + + Vector3 p = p_p1 - p_p0; + Vector3 q = p_q1 - p_q0; + Vector3 r = p_p0 - p_q0; + + real_t a = p.dot(p); + real_t b = p.dot(q); + real_t c = q.dot(q); + real_t d = p.dot(r); + real_t e = q.dot(r); + + real_t s = 0.0f; + real_t t = 0.0f; + + real_t det = a * c - b * b; + if (det > CMP_EPSILON) { + // Non-parallel segments + real_t bte = b * e; + real_t ctd = c * d; + + if (bte <= ctd) { + // s <= 0.0f + if (e <= 0.0f) { + // t <= 0.0f + s = (-d >= a ? 1 : (-d > 0.0f ? -d / a : 0.0f)); + t = 0.0f; + } else if (e < c) { + // 0.0f < t < 1 + s = 0.0f; + t = e / c; + } else { + // t >= 1 + s = (b - d >= a ? 1 : (b - d > 0.0f ? (b - d) / a : 0.0f)); + t = 1; + } + } else { + // s > 0.0f + s = bte - ctd; + if (s >= det) { + // s >= 1 + if (b + e <= 0.0f) { + // t <= 0.0f + s = (-d <= 0.0f ? 0.0f : (-d < a ? -d / a : 1)); + t = 0.0f; + } else if (b + e < c) { + // 0.0f < t < 1 + s = 1; + t = (b + e) / c; + } else { + // t >= 1 + s = (b - d <= 0.0f ? 0.0f : (b - d < a ? (b - d) / a : 1)); + t = 1; + } + } else { + // 0.0f < s < 1 + real_t ate = a * e; + real_t btd = b * d; + + if (ate <= btd) { + // t <= 0.0f + s = (-d <= 0.0f ? 0.0f : (-d >= a ? 1 : -d / a)); + t = 0.0f; + } else { + // t > 0.0f + t = ate - btd; + if (t >= det) { + // t >= 1 + s = (b - d <= 0.0f ? 0.0f : (b - d >= a ? 1 : (b - d) / a)); + t = 1; + } else { + // 0.0f < t < 1 + s /= det; + t /= det; + } + } + } + } + } else { + // Parallel segments + if (e <= 0.0f) { + s = (-d <= 0.0f ? 0.0f : (-d >= a ? 1 : -d / a)); + t = 0.0f; + } else if (e >= c) { + s = (b - d <= 0.0f ? 0.0f : (b - d >= a ? 1 : (b - d) / a)); + t = 1; + } else { + s = 0.0f; + t = e / c; + } + } + + r_ps = (1 - s) * p_p0 + s * p_p1; + r_qt = (1 - t) * p_q0 + t * p_q1; +} + +real_t Geometry3D::get_closest_distance_between_segments(const Vector3 &p_p0, const Vector3 &p_p1, const Vector3 &p_q0, const Vector3 &p_q1) { + Vector3 ps; + Vector3 qt; + get_closest_points_between_segments(p_p0, p_p1, p_q0, p_q1, ps, qt); + Vector3 st = qt - ps; + return st.length(); +} + void Geometry3D::MeshData::optimize_vertices() { HashMap<int, int> vtx_remap; - for (int i = 0; i < faces.size(); i++) { - for (int j = 0; j < faces[i].indices.size(); j++) { + for (uint32_t i = 0; i < faces.size(); i++) { + for (uint32_t j = 0; j < faces[i].indices.size(); j++) { int idx = faces[i].indices[j]; if (!vtx_remap.has(idx)) { int ni = vtx_remap.size(); vtx_remap[idx] = ni; } - faces.write[i].indices.write[j] = vtx_remap[idx]; + faces[i].indices[j] = vtx_remap[idx]; } } - for (int i = 0; i < edges.size(); i++) { - int a = edges[i].a; - int b = edges[i].b; + for (uint32_t i = 0; i < edges.size(); i++) { + int a = edges[i].vertex_a; + int b = edges[i].vertex_b; if (!vtx_remap.has(a)) { int ni = vtx_remap.size(); @@ -63,16 +166,16 @@ void Geometry3D::MeshData::optimize_vertices() { vtx_remap[b] = ni; } - edges.write[i].a = vtx_remap[a]; - edges.write[i].b = vtx_remap[b]; + edges[i].vertex_a = vtx_remap[a]; + edges[i].vertex_b = vtx_remap[b]; } - Vector<Vector3> new_vertices; + LocalVector<Vector3> new_vertices; new_vertices.resize(vtx_remap.size()); - for (int i = 0; i < vertices.size(); i++) { + for (uint32_t i = 0; i < vertices.size(); i++) { if (vtx_remap.has(i)) { - new_vertices.write[vtx_remap[i]] = vertices[i]; + new_vertices[vtx_remap[i]] = vertices[i]; } } vertices = new_vertices; @@ -648,7 +751,7 @@ Geometry3D::MeshData Geometry3D::build_convex_mesh(const Vector<Plane> &p_planes Vector3 center = p.center(); // make a quad clockwise - Vector<Vector3> vertices = { + LocalVector<Vector3> vertices = { center - up * subplane_size + right * subplane_size, center - up * subplane_size - right * subplane_size, center + up * subplane_size - right * subplane_size, @@ -660,7 +763,7 @@ Geometry3D::MeshData Geometry3D::build_convex_mesh(const Vector<Plane> &p_planes continue; } - Vector<Vector3> new_vertices; + LocalVector<Vector3> new_vertices; Plane clip = p_planes[j]; if (clip.normal.dot(p.normal) > 0.95f) { @@ -671,7 +774,7 @@ Geometry3D::MeshData Geometry3D::build_convex_mesh(const Vector<Plane> &p_planes break; } - for (int k = 0; k < vertices.size(); k++) { + for (uint32_t k = 0; k < vertices.size(); k++) { int k_n = (k + 1) % vertices.size(); Vector3 edge0_A = vertices[k]; @@ -713,9 +816,9 @@ Geometry3D::MeshData Geometry3D::build_convex_mesh(const Vector<Plane> &p_planes MeshData::Face face; // Add face indices. - for (int j = 0; j < vertices.size(); j++) { + for (uint32_t j = 0; j < vertices.size(); j++) { int idx = -1; - for (int k = 0; k < mesh.vertices.size(); k++) { + for (uint32_t k = 0; k < mesh.vertices.size(); k++) { if (mesh.vertices[k].distance_to(vertices[j]) < 0.001f) { idx = k; break; @@ -734,28 +837,34 @@ Geometry3D::MeshData Geometry3D::build_convex_mesh(const Vector<Plane> &p_planes // Add edge. - for (int j = 0; j < face.indices.size(); j++) { + for (uint32_t j = 0; j < face.indices.size(); j++) { int a = face.indices[j]; int b = face.indices[(j + 1) % face.indices.size()]; bool found = false; - for (int k = 0; k < mesh.edges.size(); k++) { - if (mesh.edges[k].a == a && mesh.edges[k].b == b) { + int found_idx = -1; + for (uint32_t k = 0; k < mesh.edges.size(); k++) { + if (mesh.edges[k].vertex_a == a && mesh.edges[k].vertex_b == b) { found = true; + found_idx = k; break; } - if (mesh.edges[k].b == a && mesh.edges[k].a == b) { + if (mesh.edges[k].vertex_b == a && mesh.edges[k].vertex_a == b) { found = true; + found_idx = k; break; } } if (found) { + mesh.edges[found_idx].face_b = j; continue; } MeshData::Edge edge; - edge.a = a; - edge.b = b; + edge.vertex_a = a; + edge.vertex_b = b; + edge.face_a = j; + edge.face_b = -1; mesh.edges.push_back(edge); } } diff --git a/core/math/geometry_3d.h b/core/math/geometry_3d.h index 59c56906f4..4b4c173a1e 100644 --- a/core/math/geometry_3d.h +++ b/core/math/geometry_3d.h @@ -33,100 +33,13 @@ #include "core/math/face3.h" #include "core/object/object.h" +#include "core/templates/local_vector.h" #include "core/templates/vector.h" class Geometry3D { public: - static void get_closest_points_between_segments(const Vector3 &p1, const Vector3 &p2, const Vector3 &q1, const Vector3 &q2, Vector3 &c1, Vector3 &c2) { -// Do the function 'd' as defined by pb. I think it's a dot product of some sort. -#define d_of(m, n, o, p) ((m.x - n.x) * (o.x - p.x) + (m.y - n.y) * (o.y - p.y) + (m.z - n.z) * (o.z - p.z)) - - // Calculate the parametric position on the 2 curves, mua and mub. - real_t mua = (d_of(p1, q1, q2, q1) * d_of(q2, q1, p2, p1) - d_of(p1, q1, p2, p1) * d_of(q2, q1, q2, q1)) / (d_of(p2, p1, p2, p1) * d_of(q2, q1, q2, q1) - d_of(q2, q1, p2, p1) * d_of(q2, q1, p2, p1)); - real_t mub = (d_of(p1, q1, q2, q1) + mua * d_of(q2, q1, p2, p1)) / d_of(q2, q1, q2, q1); - - // Clip the value between [0..1] constraining the solution to lie on the original curves. - if (mua < 0) { - mua = 0; - } - if (mub < 0) { - mub = 0; - } - if (mua > 1) { - mua = 1; - } - if (mub > 1) { - mub = 1; - } - c1 = p1.lerp(p2, mua); - c2 = q1.lerp(q2, mub); - } - - static real_t get_closest_distance_between_segments(const Vector3 &p_from_a, const Vector3 &p_to_a, const Vector3 &p_from_b, const Vector3 &p_to_b) { - Vector3 u = p_to_a - p_from_a; - Vector3 v = p_to_b - p_from_b; - Vector3 w = p_from_a - p_to_a; - real_t a = u.dot(u); // Always >= 0 - real_t b = u.dot(v); - real_t c = v.dot(v); // Always >= 0 - real_t d = u.dot(w); - real_t e = v.dot(w); - real_t D = a * c - b * b; // Always >= 0 - real_t sc, sN, sD = D; // sc = sN / sD, default sD = D >= 0 - real_t tc, tN, tD = D; // tc = tN / tD, default tD = D >= 0 - - // Compute the line parameters of the two closest points. - if (D < (real_t)CMP_EPSILON) { // The lines are almost parallel. - sN = 0.0f; // Force using point P0 on segment S1 - sD = 1.0f; // to prevent possible division by 0.0 later. - tN = e; - tD = c; - } else { // Get the closest points on the infinite lines - sN = (b * e - c * d); - tN = (a * e - b * d); - if (sN < 0.0f) { // sc < 0 => the s=0 edge is visible. - sN = 0.0f; - tN = e; - tD = c; - } else if (sN > sD) { // sc > 1 => the s=1 edge is visible. - sN = sD; - tN = e + b; - tD = c; - } - } - - if (tN < 0.0f) { // tc < 0 => the t=0 edge is visible. - tN = 0.0f; - // Recompute sc for this edge. - if (-d < 0.0f) { - sN = 0.0f; - } else if (-d > a) { - sN = sD; - } else { - sN = -d; - sD = a; - } - } else if (tN > tD) { // tc > 1 => the t=1 edge is visible. - tN = tD; - // Recompute sc for this edge. - if ((-d + b) < 0.0f) { - sN = 0; - } else if ((-d + b) > a) { - sN = sD; - } else { - sN = (-d + b); - sD = a; - } - } - // Finally do the division to get sc and tc. - sc = (Math::is_zero_approx(sN) ? 0.0f : sN / sD); - tc = (Math::is_zero_approx(tN) ? 0.0f : tN / tD); - - // Get the difference of the two closest points. - Vector3 dP = w + (sc * u) - (tc * v); // = S1(sc) - S2(tc) - - return dP.length(); // Return the closest distance. - } + static void get_closest_points_between_segments(const Vector3 &p_p0, const Vector3 &p_p1, const Vector3 &p_q0, const Vector3 &p_q1, Vector3 &r_ps, Vector3 &r_qt); + static real_t get_closest_distance_between_segments(const Vector3 &p_p0, const Vector3 &p_p1, const Vector3 &p_q0, const Vector3 &p_q1); static inline bool ray_intersects_triangle(const Vector3 &p_from, const Vector3 &p_dir, const Vector3 &p_v0, const Vector3 &p_v1, const Vector3 &p_v2, Vector3 *r_res = nullptr) { Vector3 e1 = p_v1 - p_v0; @@ -627,18 +540,19 @@ public: struct MeshData { struct Face { Plane plane; - Vector<int> indices; + LocalVector<int> indices; }; - Vector<Face> faces; + LocalVector<Face> faces; struct Edge { - int a, b; + int vertex_a, vertex_b; + int face_a, face_b; }; - Vector<Edge> edges; + LocalVector<Edge> edges; - Vector<Vector3> vertices; + LocalVector<Vector3> vertices; void optimize_vertices(); }; diff --git a/core/math/math_defs.h b/core/math/math_defs.h index b8b82f2ff4..759667e2d5 100644 --- a/core/math/math_defs.h +++ b/core/math/math_defs.h @@ -116,6 +116,15 @@ enum Corner { CORNER_BOTTOM_LEFT }; +enum class EulerOrder { + XYZ, + XZY, + YXZ, + YZX, + ZXY, + ZYX +}; + /** * The "Real" type is an abstract type used for real numbers, such as 1.5, * in contrast to integer numbers. Precision can be controlled with the diff --git a/core/math/math_fieldwise.cpp b/core/math/math_fieldwise.cpp index 208f89f449..7b30b9a98c 100644 --- a/core/math/math_fieldwise.cpp +++ b/core/math/math_fieldwise.cpp @@ -56,6 +56,15 @@ Variant fieldwise_assign(const Variant &p_target, const Variant &p_source, const return target; } + case Variant::VECTOR2I: { + SETUP_TYPE(Vector2i) + + /**/ TRY_TRANSFER_FIELD("x", x) + else TRY_TRANSFER_FIELD("y", y) + + return target; + } + case Variant::RECT2: { SETUP_TYPE(Rect2) @@ -67,6 +76,17 @@ Variant fieldwise_assign(const Variant &p_target, const Variant &p_source, const return target; } + case Variant::RECT2I: { + SETUP_TYPE(Rect2i) + + /**/ TRY_TRANSFER_FIELD("x", position.x) + else TRY_TRANSFER_FIELD("y", position.y) + else TRY_TRANSFER_FIELD("w", size.x) + else TRY_TRANSFER_FIELD("h", size.y) + + return target; + } + case Variant::VECTOR3: { SETUP_TYPE(Vector3) @@ -76,6 +96,7 @@ Variant fieldwise_assign(const Variant &p_target, const Variant &p_source, const return target; } + case Variant::VECTOR3I: { SETUP_TYPE(Vector3i) @@ -85,6 +106,7 @@ Variant fieldwise_assign(const Variant &p_target, const Variant &p_source, const return target; } + case Variant::VECTOR4: { SETUP_TYPE(Vector4) @@ -95,6 +117,7 @@ Variant fieldwise_assign(const Variant &p_target, const Variant &p_source, const return target; } + case Variant::VECTOR4I: { SETUP_TYPE(Vector4i) @@ -106,7 +129,6 @@ Variant fieldwise_assign(const Variant &p_target, const Variant &p_source, const return target; } - case Variant::PLANE: { SETUP_TYPE(Plane) @@ -190,6 +212,29 @@ Variant fieldwise_assign(const Variant &p_target, const Variant &p_source, const return target; } + case Variant::PROJECTION: { + SETUP_TYPE(Projection) + + /**/ TRY_TRANSFER_FIELD("xx", columns[0].x) + else TRY_TRANSFER_FIELD("xy", columns[0].y) + else TRY_TRANSFER_FIELD("xz", columns[0].z) + else TRY_TRANSFER_FIELD("xw", columns[0].w) + else TRY_TRANSFER_FIELD("yx", columns[1].x) + else TRY_TRANSFER_FIELD("yy", columns[1].y) + else TRY_TRANSFER_FIELD("yz", columns[1].z) + else TRY_TRANSFER_FIELD("yw", columns[1].w) + else TRY_TRANSFER_FIELD("zx", columns[2].x) + else TRY_TRANSFER_FIELD("zy", columns[2].y) + else TRY_TRANSFER_FIELD("zz", columns[2].z) + else TRY_TRANSFER_FIELD("zw", columns[2].w) + else TRY_TRANSFER_FIELD("xo", columns[3].x) + else TRY_TRANSFER_FIELD("yo", columns[3].y) + else TRY_TRANSFER_FIELD("zo", columns[3].z) + else TRY_TRANSFER_FIELD("wo", columns[3].w) + + return target; + } + default: { ERR_FAIL_V(p_target); } diff --git a/core/math/math_funcs.h b/core/math/math_funcs.h index 53deb9bd42..0af529ad98 100644 --- a/core/math/math_funcs.h +++ b/core/math/math_funcs.h @@ -184,6 +184,9 @@ public: #endif } + static _ALWAYS_INLINE_ bool is_finite(double p_val) { return isfinite(p_val); } + static _ALWAYS_INLINE_ bool is_finite(float p_val) { return isfinite(p_val); } + static _ALWAYS_INLINE_ double abs(double g) { return absd(g); } static _ALWAYS_INLINE_ float abs(float g) { return absf(g); } static _ALWAYS_INLINE_ int abs(int g) { return g > 0 ? g : -g; } @@ -229,11 +232,11 @@ public: return value; } - static _ALWAYS_INLINE_ double deg2rad(double p_y) { return p_y * (Math_PI / 180.0); } - static _ALWAYS_INLINE_ float deg2rad(float p_y) { return p_y * (float)(Math_PI / 180.0); } + static _ALWAYS_INLINE_ double deg_to_rad(double p_y) { return p_y * (Math_PI / 180.0); } + static _ALWAYS_INLINE_ float deg_to_rad(float p_y) { return p_y * (float)(Math_PI / 180.0); } - static _ALWAYS_INLINE_ double rad2deg(double p_y) { return p_y * (180.0 / Math_PI); } - static _ALWAYS_INLINE_ float rad2deg(float p_y) { return p_y * (float)(180.0 / Math_PI); } + static _ALWAYS_INLINE_ double rad_to_deg(double p_y) { return p_y * (180.0 / Math_PI); } + static _ALWAYS_INLINE_ float rad_to_deg(float p_y) { return p_y * (float)(180.0 / Math_PI); } static _ALWAYS_INLINE_ double lerp(double p_from, double p_to, double p_weight) { return p_from + (p_to - p_from) * p_weight; } static _ALWAYS_INLINE_ float lerp(float p_from, float p_to, float p_weight) { return p_from + (p_to - p_from) * p_weight; } @@ -253,6 +256,92 @@ public: (-p_pre + 3.0f * p_from - 3.0f * p_to + p_post) * (p_weight * p_weight * p_weight)); } + static _ALWAYS_INLINE_ double cubic_interpolate_angle(double p_from, double p_to, double p_pre, double p_post, double p_weight) { + double from_rot = fmod(p_from, Math_TAU); + + double pre_diff = fmod(p_pre - from_rot, Math_TAU); + double pre_rot = from_rot + fmod(2.0 * pre_diff, Math_TAU) - pre_diff; + + double to_diff = fmod(p_to - from_rot, Math_TAU); + double to_rot = from_rot + fmod(2.0 * to_diff, Math_TAU) - to_diff; + + double post_diff = fmod(p_post - to_rot, Math_TAU); + double post_rot = to_rot + fmod(2.0 * post_diff, Math_TAU) - post_diff; + + return cubic_interpolate(from_rot, to_rot, pre_rot, post_rot, p_weight); + } + + static _ALWAYS_INLINE_ float cubic_interpolate_angle(float p_from, float p_to, float p_pre, float p_post, float p_weight) { + float from_rot = fmod(p_from, (float)Math_TAU); + + float pre_diff = fmod(p_pre - from_rot, (float)Math_TAU); + float pre_rot = from_rot + fmod(2.0f * pre_diff, (float)Math_TAU) - pre_diff; + + float to_diff = fmod(p_to - from_rot, (float)Math_TAU); + float to_rot = from_rot + fmod(2.0f * to_diff, (float)Math_TAU) - to_diff; + + float post_diff = fmod(p_post - to_rot, (float)Math_TAU); + float post_rot = to_rot + fmod(2.0f * post_diff, (float)Math_TAU) - post_diff; + + return cubic_interpolate(from_rot, to_rot, pre_rot, post_rot, p_weight); + } + + static _ALWAYS_INLINE_ double cubic_interpolate_in_time(double p_from, double p_to, double p_pre, double p_post, double p_weight, + double p_to_t, double p_pre_t, double p_post_t) { + /* Barry-Goldman method */ + double t = Math::lerp(0.0, p_to_t, p_weight); + double a1 = Math::lerp(p_pre, p_from, p_pre_t == 0 ? 0.0 : (t - p_pre_t) / -p_pre_t); + double a2 = Math::lerp(p_from, p_to, p_to_t == 0 ? 0.5 : t / p_to_t); + double a3 = Math::lerp(p_to, p_post, p_post_t - p_to_t == 0 ? 1.0 : (t - p_to_t) / (p_post_t - p_to_t)); + double b1 = Math::lerp(a1, a2, p_to_t - p_pre_t == 0 ? 0.0 : (t - p_pre_t) / (p_to_t - p_pre_t)); + double b2 = Math::lerp(a2, a3, p_post_t == 0 ? 1.0 : t / p_post_t); + return Math::lerp(b1, b2, p_to_t == 0 ? 0.5 : t / p_to_t); + } + + static _ALWAYS_INLINE_ float cubic_interpolate_in_time(float p_from, float p_to, float p_pre, float p_post, float p_weight, + float p_to_t, float p_pre_t, float p_post_t) { + /* Barry-Goldman method */ + float t = Math::lerp(0.0f, p_to_t, p_weight); + float a1 = Math::lerp(p_pre, p_from, p_pre_t == 0 ? 0.0f : (t - p_pre_t) / -p_pre_t); + float a2 = Math::lerp(p_from, p_to, p_to_t == 0 ? 0.5f : t / p_to_t); + float a3 = Math::lerp(p_to, p_post, p_post_t - p_to_t == 0 ? 1.0f : (t - p_to_t) / (p_post_t - p_to_t)); + float b1 = Math::lerp(a1, a2, p_to_t - p_pre_t == 0 ? 0.0f : (t - p_pre_t) / (p_to_t - p_pre_t)); + float b2 = Math::lerp(a2, a3, p_post_t == 0 ? 1.0f : t / p_post_t); + return Math::lerp(b1, b2, p_to_t == 0 ? 0.5f : t / p_to_t); + } + + static _ALWAYS_INLINE_ double cubic_interpolate_angle_in_time(double p_from, double p_to, double p_pre, double p_post, double p_weight, + double p_to_t, double p_pre_t, double p_post_t) { + double from_rot = fmod(p_from, Math_TAU); + + double pre_diff = fmod(p_pre - from_rot, Math_TAU); + double pre_rot = from_rot + fmod(2.0 * pre_diff, Math_TAU) - pre_diff; + + double to_diff = fmod(p_to - from_rot, Math_TAU); + double to_rot = from_rot + fmod(2.0 * to_diff, Math_TAU) - to_diff; + + double post_diff = fmod(p_post - to_rot, Math_TAU); + double post_rot = to_rot + fmod(2.0 * post_diff, Math_TAU) - post_diff; + + return cubic_interpolate_in_time(from_rot, to_rot, pre_rot, post_rot, p_weight, p_to_t, p_pre_t, p_post_t); + } + + static _ALWAYS_INLINE_ float cubic_interpolate_angle_in_time(float p_from, float p_to, float p_pre, float p_post, float p_weight, + float p_to_t, float p_pre_t, float p_post_t) { + float from_rot = fmod(p_from, (float)Math_TAU); + + float pre_diff = fmod(p_pre - from_rot, (float)Math_TAU); + float pre_rot = from_rot + fmod(2.0f * pre_diff, (float)Math_TAU) - pre_diff; + + float to_diff = fmod(p_to - from_rot, (float)Math_TAU); + float to_rot = from_rot + fmod(2.0f * to_diff, (float)Math_TAU) - to_diff; + + float post_diff = fmod(p_post - to_rot, (float)Math_TAU); + float post_rot = to_rot + fmod(2.0f * post_diff, (float)Math_TAU) - post_diff; + + return cubic_interpolate_in_time(from_rot, to_rot, pre_rot, post_rot, p_weight, p_to_t, p_pre_t, p_post_t); + } + static _ALWAYS_INLINE_ double bezier_interpolate(double p_start, double p_control_1, double p_control_2, double p_end, double p_t) { /* Formula from Wikipedia article on Bezier curves. */ double omt = (1.0 - p_t); @@ -263,6 +352,7 @@ public: return p_start * omt3 + p_control_1 * omt2 * p_t * 3.0 + p_control_2 * omt * t2 * 3.0 + p_end * t3; } + static _ALWAYS_INLINE_ float bezier_interpolate(float p_start, float p_control_1, float p_control_2, float p_end, float p_t) { /* Formula from Wikipedia article on Bezier curves. */ float omt = (1.0f - p_t); @@ -285,11 +375,19 @@ public: return p_from + distance * p_weight; } - static _ALWAYS_INLINE_ double inverse_lerp(double p_from, double p_to, double p_value) { return (p_value - p_from) / (p_to - p_from); } - static _ALWAYS_INLINE_ float inverse_lerp(float p_from, float p_to, float p_value) { return (p_value - p_from) / (p_to - p_from); } + static _ALWAYS_INLINE_ double inverse_lerp(double p_from, double p_to, double p_value) { + return (p_value - p_from) / (p_to - p_from); + } + static _ALWAYS_INLINE_ float inverse_lerp(float p_from, float p_to, float p_value) { + return (p_value - p_from) / (p_to - p_from); + } - static _ALWAYS_INLINE_ double range_lerp(double p_value, double p_istart, double p_istop, double p_ostart, double p_ostop) { return Math::lerp(p_ostart, p_ostop, Math::inverse_lerp(p_istart, p_istop, p_value)); } - static _ALWAYS_INLINE_ float range_lerp(float p_value, float p_istart, float p_istop, float p_ostart, float p_ostop) { return Math::lerp(p_ostart, p_ostop, Math::inverse_lerp(p_istart, p_istop, p_value)); } + static _ALWAYS_INLINE_ double remap(double p_value, double p_istart, double p_istop, double p_ostart, double p_ostop) { + return Math::lerp(p_ostart, p_ostop, Math::inverse_lerp(p_istart, p_istop, p_value)); + } + static _ALWAYS_INLINE_ float remap(float p_value, float p_istart, float p_istop, float p_ostart, float p_ostop) { + return Math::lerp(p_ostart, p_ostop, Math::inverse_lerp(p_istart, p_istop, p_value)); + } static _ALWAYS_INLINE_ double smoothstep(double p_from, double p_to, double p_s) { if (is_equal_approx(p_from, p_to)) { @@ -305,14 +403,26 @@ public: float s = CLAMP((p_s - p_from) / (p_to - p_from), 0.0f, 1.0f); return s * s * (3.0f - 2.0f * s); } - static _ALWAYS_INLINE_ double move_toward(double p_from, double p_to, double p_delta) { return abs(p_to - p_from) <= p_delta ? p_to : p_from + SIGN(p_to - p_from) * p_delta; } - static _ALWAYS_INLINE_ float move_toward(float p_from, float p_to, float p_delta) { return abs(p_to - p_from) <= p_delta ? p_to : p_from + SIGN(p_to - p_from) * p_delta; } + static _ALWAYS_INLINE_ double move_toward(double p_from, double p_to, double p_delta) { + return abs(p_to - p_from) <= p_delta ? p_to : p_from + SIGN(p_to - p_from) * p_delta; + } + static _ALWAYS_INLINE_ float move_toward(float p_from, float p_to, float p_delta) { + return abs(p_to - p_from) <= p_delta ? p_to : p_from + SIGN(p_to - p_from) * p_delta; + } - static _ALWAYS_INLINE_ double linear2db(double p_linear) { return Math::log(p_linear) * 8.6858896380650365530225783783321; } - static _ALWAYS_INLINE_ float linear2db(float p_linear) { return Math::log(p_linear) * (float)8.6858896380650365530225783783321; } + static _ALWAYS_INLINE_ double linear_to_db(double p_linear) { + return Math::log(p_linear) * 8.6858896380650365530225783783321; + } + static _ALWAYS_INLINE_ float linear_to_db(float p_linear) { + return Math::log(p_linear) * (float)8.6858896380650365530225783783321; + } - static _ALWAYS_INLINE_ double db2linear(double p_db) { return Math::exp(p_db * 0.11512925464970228420089957273422); } - static _ALWAYS_INLINE_ float db2linear(float p_db) { return Math::exp(p_db * (float)0.11512925464970228420089957273422); } + static _ALWAYS_INLINE_ double db_to_linear(double p_db) { + return Math::exp(p_db * 0.11512925464970228420089957273422); + } + static _ALWAYS_INLINE_ float db_to_linear(float p_db) { + return Math::exp(p_db * (float)0.11512925464970228420089957273422); + } static _ALWAYS_INLINE_ double round(double p_val) { return ::round(p_val); } static _ALWAYS_INLINE_ float round(float p_val) { return ::roundf(p_val); } diff --git a/core/math/plane.cpp b/core/math/plane.cpp index 6881ad4014..a5d2fe5628 100644 --- a/core/math/plane.cpp +++ b/core/math/plane.cpp @@ -147,6 +147,7 @@ Variant Plane::intersect_3_bind(const Plane &p_plane1, const Plane &p_plane2) co return Variant(); } } + Variant Plane::intersects_ray_bind(const Vector3 &p_from, const Vector3 &p_dir) const { Vector3 inters; if (intersects_ray(p_from, p_dir, &inters)) { @@ -155,6 +156,7 @@ Variant Plane::intersects_ray_bind(const Vector3 &p_from, const Vector3 &p_dir) return Variant(); } } + Variant Plane::intersects_segment_bind(const Vector3 &p_begin, const Vector3 &p_end) const { Vector3 inters; if (intersects_segment(p_begin, p_end, &inters)) { @@ -174,6 +176,10 @@ bool Plane::is_equal_approx(const Plane &p_plane) const { return normal.is_equal_approx(p_plane.normal) && Math::is_equal_approx(d, p_plane.d); } +bool Plane::is_finite() const { + return normal.is_finite() && Math::is_finite(d); +} + Plane::operator String() const { return "[N: " + normal.operator String() + ", D: " + String::num_real(d, false) + "]"; } diff --git a/core/math/plane.h b/core/math/plane.h index 73babfa496..77da59fb27 100644 --- a/core/math/plane.h +++ b/core/math/plane.h @@ -74,6 +74,7 @@ struct _NO_DISCARD_ Plane { Plane operator-() const { return Plane(-normal, -d); } bool is_equal_approx(const Plane &p_plane) const; bool is_equal_approx_any_side(const Plane &p_plane) const; + bool is_finite() const; _FORCE_INLINE_ bool operator==(const Plane &p_plane) const; _FORCE_INLINE_ bool operator!=(const Plane &p_plane) const; diff --git a/core/math/projection.cpp b/core/math/projection.cpp index edf8bf36cd..9af388b081 100644 --- a/core/math/projection.cpp +++ b/core/math/projection.cpp @@ -35,27 +35,27 @@ #include "core/math/plane.h" #include "core/math/rect2.h" #include "core/math/transform_3d.h" -#include "core/string/print_string.h" +#include "core/string/ustring.h" float Projection::determinant() const { - return matrix[0][3] * matrix[1][2] * matrix[2][1] * matrix[3][0] - matrix[0][2] * matrix[1][3] * matrix[2][1] * matrix[3][0] - - matrix[0][3] * matrix[1][1] * matrix[2][2] * matrix[3][0] + matrix[0][1] * matrix[1][3] * matrix[2][2] * matrix[3][0] + - matrix[0][2] * matrix[1][1] * matrix[2][3] * matrix[3][0] - matrix[0][1] * matrix[1][2] * matrix[2][3] * matrix[3][0] - - matrix[0][3] * matrix[1][2] * matrix[2][0] * matrix[3][1] + matrix[0][2] * matrix[1][3] * matrix[2][0] * matrix[3][1] + - matrix[0][3] * matrix[1][0] * matrix[2][2] * matrix[3][1] - matrix[0][0] * matrix[1][3] * matrix[2][2] * matrix[3][1] - - matrix[0][2] * matrix[1][0] * matrix[2][3] * matrix[3][1] + matrix[0][0] * matrix[1][2] * matrix[2][3] * matrix[3][1] + - matrix[0][3] * matrix[1][1] * matrix[2][0] * matrix[3][2] - matrix[0][1] * matrix[1][3] * matrix[2][0] * matrix[3][2] - - matrix[0][3] * matrix[1][0] * matrix[2][1] * matrix[3][2] + matrix[0][0] * matrix[1][3] * matrix[2][1] * matrix[3][2] + - matrix[0][1] * matrix[1][0] * matrix[2][3] * matrix[3][2] - matrix[0][0] * matrix[1][1] * matrix[2][3] * matrix[3][2] - - matrix[0][2] * matrix[1][1] * matrix[2][0] * matrix[3][3] + matrix[0][1] * matrix[1][2] * matrix[2][0] * matrix[3][3] + - matrix[0][2] * matrix[1][0] * matrix[2][1] * matrix[3][3] - matrix[0][0] * matrix[1][2] * matrix[2][1] * matrix[3][3] - - matrix[0][1] * matrix[1][0] * matrix[2][2] * matrix[3][3] + matrix[0][0] * matrix[1][1] * matrix[2][2] * matrix[3][3]; + return columns[0][3] * columns[1][2] * columns[2][1] * columns[3][0] - columns[0][2] * columns[1][3] * columns[2][1] * columns[3][0] - + columns[0][3] * columns[1][1] * columns[2][2] * columns[3][0] + columns[0][1] * columns[1][3] * columns[2][2] * columns[3][0] + + columns[0][2] * columns[1][1] * columns[2][3] * columns[3][0] - columns[0][1] * columns[1][2] * columns[2][3] * columns[3][0] - + columns[0][3] * columns[1][2] * columns[2][0] * columns[3][1] + columns[0][2] * columns[1][3] * columns[2][0] * columns[3][1] + + columns[0][3] * columns[1][0] * columns[2][2] * columns[3][1] - columns[0][0] * columns[1][3] * columns[2][2] * columns[3][1] - + columns[0][2] * columns[1][0] * columns[2][3] * columns[3][1] + columns[0][0] * columns[1][2] * columns[2][3] * columns[3][1] + + columns[0][3] * columns[1][1] * columns[2][0] * columns[3][2] - columns[0][1] * columns[1][3] * columns[2][0] * columns[3][2] - + columns[0][3] * columns[1][0] * columns[2][1] * columns[3][2] + columns[0][0] * columns[1][3] * columns[2][1] * columns[3][2] + + columns[0][1] * columns[1][0] * columns[2][3] * columns[3][2] - columns[0][0] * columns[1][1] * columns[2][3] * columns[3][2] - + columns[0][2] * columns[1][1] * columns[2][0] * columns[3][3] + columns[0][1] * columns[1][2] * columns[2][0] * columns[3][3] + + columns[0][2] * columns[1][0] * columns[2][1] * columns[3][3] - columns[0][0] * columns[1][2] * columns[2][1] * columns[3][3] - + columns[0][1] * columns[1][0] * columns[2][2] * columns[3][3] + columns[0][0] * columns[1][1] * columns[2][2] * columns[3][3]; } void Projection::set_identity() { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { - matrix[i][j] = (i == j) ? 1 : 0; + columns[i][j] = (i == j) ? 1 : 0; } } } @@ -63,7 +63,7 @@ void Projection::set_identity() { void Projection::set_zero() { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { - matrix[i][j] = 0; + columns[i][j] = 0; } } } @@ -71,26 +71,26 @@ void Projection::set_zero() { Plane Projection::xform4(const Plane &p_vec4) const { Plane ret; - ret.normal.x = matrix[0][0] * p_vec4.normal.x + matrix[1][0] * p_vec4.normal.y + matrix[2][0] * p_vec4.normal.z + matrix[3][0] * p_vec4.d; - ret.normal.y = matrix[0][1] * p_vec4.normal.x + matrix[1][1] * p_vec4.normal.y + matrix[2][1] * p_vec4.normal.z + matrix[3][1] * p_vec4.d; - ret.normal.z = matrix[0][2] * p_vec4.normal.x + matrix[1][2] * p_vec4.normal.y + matrix[2][2] * p_vec4.normal.z + matrix[3][2] * p_vec4.d; - ret.d = matrix[0][3] * p_vec4.normal.x + matrix[1][3] * p_vec4.normal.y + matrix[2][3] * p_vec4.normal.z + matrix[3][3] * p_vec4.d; + ret.normal.x = columns[0][0] * p_vec4.normal.x + columns[1][0] * p_vec4.normal.y + columns[2][0] * p_vec4.normal.z + columns[3][0] * p_vec4.d; + ret.normal.y = columns[0][1] * p_vec4.normal.x + columns[1][1] * p_vec4.normal.y + columns[2][1] * p_vec4.normal.z + columns[3][1] * p_vec4.d; + ret.normal.z = columns[0][2] * p_vec4.normal.x + columns[1][2] * p_vec4.normal.y + columns[2][2] * p_vec4.normal.z + columns[3][2] * p_vec4.d; + ret.d = columns[0][3] * p_vec4.normal.x + columns[1][3] * p_vec4.normal.y + columns[2][3] * p_vec4.normal.z + columns[3][3] * p_vec4.d; return ret; } Vector4 Projection::xform(const Vector4 &p_vec4) const { return Vector4( - matrix[0][0] * p_vec4.x + matrix[1][0] * p_vec4.y + matrix[2][0] * p_vec4.z + matrix[3][0] * p_vec4.w, - matrix[0][1] * p_vec4.x + matrix[1][1] * p_vec4.y + matrix[2][1] * p_vec4.z + matrix[3][1] * p_vec4.w, - matrix[0][2] * p_vec4.x + matrix[1][2] * p_vec4.y + matrix[2][2] * p_vec4.z + matrix[3][2] * p_vec4.w, - matrix[0][3] * p_vec4.x + matrix[1][3] * p_vec4.y + matrix[2][3] * p_vec4.z + matrix[3][3] * p_vec4.w); + columns[0][0] * p_vec4.x + columns[1][0] * p_vec4.y + columns[2][0] * p_vec4.z + columns[3][0] * p_vec4.w, + columns[0][1] * p_vec4.x + columns[1][1] * p_vec4.y + columns[2][1] * p_vec4.z + columns[3][1] * p_vec4.w, + columns[0][2] * p_vec4.x + columns[1][2] * p_vec4.y + columns[2][2] * p_vec4.z + columns[3][2] * p_vec4.w, + columns[0][3] * p_vec4.x + columns[1][3] * p_vec4.y + columns[2][3] * p_vec4.z + columns[3][3] * p_vec4.w); } Vector4 Projection::xform_inv(const Vector4 &p_vec4) const { return Vector4( - matrix[0][0] * p_vec4.x + matrix[0][1] * p_vec4.y + matrix[0][2] * p_vec4.z + matrix[0][3] * p_vec4.w, - matrix[1][0] * p_vec4.x + matrix[1][1] * p_vec4.y + matrix[1][2] * p_vec4.z + matrix[1][3] * p_vec4.w, - matrix[2][0] * p_vec4.x + matrix[2][1] * p_vec4.y + matrix[2][2] * p_vec4.z + matrix[2][3] * p_vec4.w, - matrix[3][0] * p_vec4.x + matrix[3][1] * p_vec4.y + matrix[3][2] * p_vec4.z + matrix[3][3] * p_vec4.w); + columns[0][0] * p_vec4.x + columns[0][1] * p_vec4.y + columns[0][2] * p_vec4.z + columns[0][3] * p_vec4.w, + columns[1][0] * p_vec4.x + columns[1][1] * p_vec4.y + columns[1][2] * p_vec4.z + columns[1][3] * p_vec4.w, + columns[2][0] * p_vec4.x + columns[2][1] * p_vec4.y + columns[2][2] * p_vec4.z + columns[2][3] * p_vec4.w, + columns[3][0] * p_vec4.x + columns[3][1] * p_vec4.y + columns[3][2] * p_vec4.z + columns[3][3] * p_vec4.w); } void Projection::adjust_perspective_znear(real_t p_new_znear) { @@ -98,8 +98,8 @@ void Projection::adjust_perspective_znear(real_t p_new_znear) { real_t znear = p_new_znear; real_t deltaZ = zfar - znear; - matrix[2][2] = -(zfar + znear) / deltaZ; - matrix[3][2] = -2 * znear * zfar / deltaZ; + columns[2][2] = -(zfar + znear) / deltaZ; + columns[3][2] = -2 * znear * zfar / deltaZ; } Projection Projection::create_depth_correction(bool p_flip_y) { @@ -169,7 +169,7 @@ Projection Projection::perspective_znear_adjusted(real_t p_new_znear) const { } Plane Projection::get_projection_plane(Planes p_plane) const { - const real_t *matrix = (const real_t *)this->matrix; + const real_t *matrix = (const real_t *)columns; switch (p_plane) { case PLANE_NEAR: { @@ -181,7 +181,7 @@ Plane Projection::get_projection_plane(Planes p_plane) const { new_plane.normal = -new_plane.normal; new_plane.normalize(); return new_plane; - } break; + } case PLANE_FAR: { Plane new_plane = Plane(matrix[3] - matrix[2], matrix[7] - matrix[6], @@ -191,7 +191,7 @@ Plane Projection::get_projection_plane(Planes p_plane) const { new_plane.normal = -new_plane.normal; new_plane.normalize(); return new_plane; - } break; + } case PLANE_LEFT: { Plane new_plane = Plane(matrix[3] + matrix[0], matrix[7] + matrix[4], @@ -201,7 +201,7 @@ Plane Projection::get_projection_plane(Planes p_plane) const { new_plane.normal = -new_plane.normal; new_plane.normalize(); return new_plane; - } break; + } case PLANE_TOP: { Plane new_plane = Plane(matrix[3] - matrix[1], matrix[7] - matrix[5], @@ -211,7 +211,7 @@ Plane Projection::get_projection_plane(Planes p_plane) const { new_plane.normal = -new_plane.normal; new_plane.normalize(); return new_plane; - } break; + } case PLANE_RIGHT: { Plane new_plane = Plane(matrix[3] - matrix[0], matrix[7] - matrix[4], @@ -221,7 +221,7 @@ Plane Projection::get_projection_plane(Planes p_plane) const { new_plane.normal = -new_plane.normal; new_plane.normalize(); return new_plane; - } break; + } case PLANE_BOTTOM: { Plane new_plane = Plane(matrix[3] + matrix[1], matrix[7] + matrix[5], @@ -231,7 +231,7 @@ Plane Projection::get_projection_plane(Planes p_plane) const { new_plane.normal = -new_plane.normal; new_plane.normalize(); return new_plane; - } break; + } } return Plane(); @@ -255,7 +255,7 @@ void Projection::set_perspective(real_t p_fovy_degrees, real_t p_aspect, real_t } real_t sine, cotangent, deltaZ; - real_t radians = Math::deg2rad(p_fovy_degrees / 2.0); + real_t radians = Math::deg_to_rad(p_fovy_degrees / 2.0); deltaZ = p_z_far - p_z_near; sine = Math::sin(radians); @@ -267,12 +267,12 @@ void Projection::set_perspective(real_t p_fovy_degrees, real_t p_aspect, real_t set_identity(); - matrix[0][0] = cotangent / p_aspect; - matrix[1][1] = cotangent; - matrix[2][2] = -(p_z_far + p_z_near) / deltaZ; - matrix[2][3] = -1; - matrix[3][2] = -2 * p_z_near * p_z_far / deltaZ; - matrix[3][3] = 0; + columns[0][0] = cotangent / p_aspect; + columns[1][1] = cotangent; + columns[2][2] = -(p_z_far + p_z_near) / deltaZ; + columns[2][3] = -1; + columns[3][2] = -2 * p_z_near * p_z_far / deltaZ; + columns[3][3] = 0; } void Projection::set_perspective(real_t p_fovy_degrees, real_t p_aspect, real_t p_z_near, real_t p_z_far, bool p_flip_fov, int p_eye, real_t p_intraocular_dist, real_t p_convergence_dist) { @@ -282,7 +282,7 @@ void Projection::set_perspective(real_t p_fovy_degrees, real_t p_aspect, real_t real_t left, right, modeltranslation, ymax, xmax, frustumshift; - ymax = p_z_near * tan(Math::deg2rad(p_fovy_degrees / 2.0)); + ymax = p_z_near * tan(Math::deg_to_rad(p_fovy_degrees / 2.0)); xmax = ymax * p_aspect; frustumshift = (p_intraocular_dist / 2.0) * p_z_near / p_convergence_dist; @@ -309,7 +309,7 @@ void Projection::set_perspective(real_t p_fovy_degrees, real_t p_aspect, real_t // translate matrix by (modeltranslation, 0.0, 0.0) Projection cm; cm.set_identity(); - cm.matrix[3][0] = modeltranslation; + cm.columns[3][0] = modeltranslation; *this = *this * cm; } @@ -344,13 +344,13 @@ void Projection::set_for_hmd(int p_eye, real_t p_aspect, real_t p_intraocular_di void Projection::set_orthogonal(real_t p_left, real_t p_right, real_t p_bottom, real_t p_top, real_t p_znear, real_t p_zfar) { set_identity(); - matrix[0][0] = 2.0 / (p_right - p_left); - matrix[3][0] = -((p_right + p_left) / (p_right - p_left)); - matrix[1][1] = 2.0 / (p_top - p_bottom); - matrix[3][1] = -((p_top + p_bottom) / (p_top - p_bottom)); - matrix[2][2] = -2.0 / (p_zfar - p_znear); - matrix[3][2] = -((p_zfar + p_znear) / (p_zfar - p_znear)); - matrix[3][3] = 1.0; + columns[0][0] = 2.0 / (p_right - p_left); + columns[3][0] = -((p_right + p_left) / (p_right - p_left)); + columns[1][1] = 2.0 / (p_top - p_bottom); + columns[3][1] = -((p_top + p_bottom) / (p_top - p_bottom)); + columns[2][2] = -2.0 / (p_zfar - p_znear); + columns[3][2] = -((p_zfar + p_znear) / (p_zfar - p_znear)); + columns[3][3] = 1.0; } void Projection::set_orthogonal(real_t p_size, real_t p_aspect, real_t p_znear, real_t p_zfar, bool p_flip_fov) { @@ -366,7 +366,7 @@ void Projection::set_frustum(real_t p_left, real_t p_right, real_t p_bottom, rea ERR_FAIL_COND(p_top <= p_bottom); ERR_FAIL_COND(p_far <= p_near); - real_t *te = &matrix[0][0]; + real_t *te = &columns[0][0]; real_t x = 2 * p_near / (p_right - p_left); real_t y = 2 * p_near / (p_top - p_bottom); @@ -402,7 +402,7 @@ void Projection::set_frustum(real_t p_size, real_t p_aspect, Vector2 p_offset, r } real_t Projection::get_z_far() const { - const real_t *matrix = (const real_t *)this->matrix; + const real_t *matrix = (const real_t *)columns; Plane new_plane = Plane(matrix[3] - matrix[2], matrix[7] - matrix[6], matrix[11] - matrix[10], @@ -415,7 +415,7 @@ real_t Projection::get_z_far() const { } real_t Projection::get_z_near() const { - const real_t *matrix = (const real_t *)this->matrix; + const real_t *matrix = (const real_t *)columns; Plane new_plane = Plane(matrix[3] + matrix[2], matrix[7] + matrix[6], matrix[11] + matrix[10], @@ -426,7 +426,7 @@ real_t Projection::get_z_near() const { } Vector2 Projection::get_viewport_half_extents() const { - const real_t *matrix = (const real_t *)this->matrix; + const real_t *matrix = (const real_t *)columns; ///////--- Near Plane ---/////// Plane near_plane = Plane(matrix[3] + matrix[2], matrix[7] + matrix[6], @@ -454,7 +454,7 @@ Vector2 Projection::get_viewport_half_extents() const { } Vector2 Projection::get_far_plane_half_extents() const { - const real_t *matrix = (const real_t *)this->matrix; + const real_t *matrix = (const real_t *)columns; ///////--- Far Plane ---/////// Plane far_plane = Plane(matrix[3] - matrix[2], matrix[7] - matrix[6], @@ -496,7 +496,10 @@ bool Projection::get_endpoints(const Transform3D &p_transform, Vector3 *p_8point for (int i = 0; i < 8; i++) { Vector3 point; - bool res = planes[intersections[i][0]].intersect_3(planes[intersections[i][1]], planes[intersections[i][2]], &point); + Plane a = planes[intersections[i][0]]; + Plane b = planes[intersections[i][1]]; + Plane c = planes[intersections[i][2]]; + bool res = a.intersect_3(b, c, &point); ERR_FAIL_COND_V(!res, false); p_8points[i] = p_transform.xform(point); } @@ -514,7 +517,7 @@ Vector<Plane> Projection::get_projection_planes(const Transform3D &p_transform) Vector<Plane> planes; planes.resize(6); - const real_t *matrix = (const real_t *)this->matrix; + const real_t *matrix = (const real_t *)columns; Plane new_plane; @@ -601,15 +604,15 @@ void Projection::invert() { real_t determinant = 1.0f; for (k = 0; k < 4; k++) { /** Locate k'th pivot element **/ - pvt_val = matrix[k][k]; /** Initialize for search **/ + pvt_val = columns[k][k]; /** Initialize for search **/ pvt_i[k] = k; pvt_j[k] = k; for (i = k; i < 4; i++) { for (j = k; j < 4; j++) { - if (Math::abs(matrix[i][j]) > Math::abs(pvt_val)) { + if (Math::abs(columns[i][j]) > Math::abs(pvt_val)) { pvt_i[k] = i; pvt_j[k] = j; - pvt_val = matrix[i][j]; + pvt_val = columns[i][j]; } } } @@ -624,9 +627,9 @@ void Projection::invert() { i = pvt_i[k]; if (i != k) { /** If rows are different **/ for (j = 0; j < 4; j++) { - hold = -matrix[k][j]; - matrix[k][j] = matrix[i][j]; - matrix[i][j] = hold; + hold = -columns[k][j]; + columns[k][j] = columns[i][j]; + columns[i][j] = hold; } } @@ -634,25 +637,25 @@ void Projection::invert() { j = pvt_j[k]; if (j != k) { /** If columns are different **/ for (i = 0; i < 4; i++) { - hold = -matrix[i][k]; - matrix[i][k] = matrix[i][j]; - matrix[i][j] = hold; + hold = -columns[i][k]; + columns[i][k] = columns[i][j]; + columns[i][j] = hold; } } /** Divide column by minus pivot value **/ for (i = 0; i < 4; i++) { if (i != k) { - matrix[i][k] /= (-pvt_val); + columns[i][k] /= (-pvt_val); } } /** Reduce the matrix **/ for (i = 0; i < 4; i++) { - hold = matrix[i][k]; + hold = columns[i][k]; for (j = 0; j < 4; j++) { if (i != k && j != k) { - matrix[i][j] += hold * matrix[k][j]; + columns[i][j] += hold * columns[k][j]; } } } @@ -660,12 +663,12 @@ void Projection::invert() { /** Divide row by pivot **/ for (j = 0; j < 4; j++) { if (j != k) { - matrix[k][j] /= pvt_val; + columns[k][j] /= pvt_val; } } /** Replace pivot by reciprocal (at last we can touch it). **/ - matrix[k][k] = 1.0 / pvt_val; + columns[k][k] = 1.0 / pvt_val; } /* That was most of the work, one final pass of row/column interchange */ @@ -674,18 +677,18 @@ void Projection::invert() { i = pvt_j[k]; /* Rows to swap correspond to pivot COLUMN */ if (i != k) { /* If rows are different */ for (j = 0; j < 4; j++) { - hold = matrix[k][j]; - matrix[k][j] = -matrix[i][j]; - matrix[i][j] = hold; + hold = columns[k][j]; + columns[k][j] = -columns[i][j]; + columns[i][j] = hold; } } j = pvt_i[k]; /* Columns to swap correspond to pivot ROW */ if (j != k) { /* If columns are different */ for (i = 0; i < 4; i++) { - hold = matrix[i][k]; - matrix[i][k] = -matrix[i][j]; - matrix[i][j] = hold; + hold = columns[i][k]; + columns[i][k] = -columns[i][j]; + columns[i][j] = hold; } } } @@ -693,7 +696,7 @@ void Projection::invert() { void Projection::flip_y() { for (int i = 0; i < 4; i++) { - matrix[1][i] = -matrix[1][i]; + columns[1][i] = -columns[1][i]; } } @@ -708,9 +711,9 @@ Projection Projection::operator*(const Projection &p_matrix) const { for (int i = 0; i < 4; i++) { real_t ab = 0; for (int k = 0; k < 4; k++) { - ab += matrix[k][i] * p_matrix.matrix[j][k]; + ab += columns[k][i] * p_matrix.columns[j][k]; } - new_matrix.matrix[j][i] = ab; + new_matrix.columns[j][i] = ab; } } @@ -718,7 +721,7 @@ Projection Projection::operator*(const Projection &p_matrix) const { } void Projection::set_depth_correction(bool p_flip_y) { - real_t *m = &matrix[0][0]; + real_t *m = &columns[0][0]; m[0] = 1; m[1] = 0.0; @@ -739,7 +742,7 @@ void Projection::set_depth_correction(bool p_flip_y) { } void Projection::set_light_bias() { - real_t *m = &matrix[0][0]; + real_t *m = &columns[0][0]; m[0] = 0.5; m[1] = 0.0; @@ -760,7 +763,7 @@ void Projection::set_light_bias() { } void Projection::set_light_atlas_rect(const Rect2 &p_rect) { - real_t *m = &matrix[0][0]; + real_t *m = &columns[0][0]; m[0] = p_rect.size.width; m[1] = 0.0; @@ -784,7 +787,7 @@ Projection::operator String() const { String str; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { - str += String((j > 0) ? ", " : "\n") + rtos(matrix[i][j]); + str += String((j > 0) ? ", " : "\n") + rtos(columns[i][j]); } } @@ -803,11 +806,11 @@ int Projection::get_pixels_per_meter(int p_for_pixel_width) const { } bool Projection::is_orthogonal() const { - return matrix[3][3] == 1.0; + return columns[3][3] == 1.0; } real_t Projection::get_fov() const { - const real_t *matrix = (const real_t *)this->matrix; + const real_t *matrix = (const real_t *)columns; Plane right_plane = Plane(matrix[3] - matrix[0], matrix[7] - matrix[4], @@ -816,7 +819,7 @@ real_t Projection::get_fov() const { right_plane.normalize(); if ((matrix[8] == 0) && (matrix[9] == 0)) { - return Math::rad2deg(Math::acos(Math::abs(right_plane.normal.x))) * 2.0; + return Math::rad_to_deg(Math::acos(Math::abs(right_plane.normal.x))) * 2.0; } else { // our frustum is asymmetrical need to calculate the left planes angle separately.. Plane left_plane = Plane(matrix[3] + matrix[0], @@ -825,7 +828,7 @@ real_t Projection::get_fov() const { matrix[15] + matrix[12]); left_plane.normalize(); - return Math::rad2deg(Math::acos(Math::abs(left_plane.normal.x))) + Math::rad2deg(Math::acos(Math::abs(right_plane.normal.x))); + return Math::rad_to_deg(Math::acos(Math::abs(left_plane.normal.x))) + Math::rad_to_deg(Math::acos(Math::abs(right_plane.normal.x))); } } @@ -838,48 +841,49 @@ float Projection::get_lod_multiplier() const { return 1.0 / (zn / width); } - //usage is lod_size / (lod_distance * multiplier) < threshold + // Usage is lod_size / (lod_distance * multiplier) < threshold } + void Projection::make_scale(const Vector3 &p_scale) { set_identity(); - matrix[0][0] = p_scale.x; - matrix[1][1] = p_scale.y; - matrix[2][2] = p_scale.z; + columns[0][0] = p_scale.x; + columns[1][1] = p_scale.y; + columns[2][2] = p_scale.z; } void Projection::scale_translate_to_fit(const AABB &p_aabb) { Vector3 min = p_aabb.position; Vector3 max = p_aabb.position + p_aabb.size; - matrix[0][0] = 2 / (max.x - min.x); - matrix[1][0] = 0; - matrix[2][0] = 0; - matrix[3][0] = -(max.x + min.x) / (max.x - min.x); + columns[0][0] = 2 / (max.x - min.x); + columns[1][0] = 0; + columns[2][0] = 0; + columns[3][0] = -(max.x + min.x) / (max.x - min.x); - matrix[0][1] = 0; - matrix[1][1] = 2 / (max.y - min.y); - matrix[2][1] = 0; - matrix[3][1] = -(max.y + min.y) / (max.y - min.y); + columns[0][1] = 0; + columns[1][1] = 2 / (max.y - min.y); + columns[2][1] = 0; + columns[3][1] = -(max.y + min.y) / (max.y - min.y); - matrix[0][2] = 0; - matrix[1][2] = 0; - matrix[2][2] = 2 / (max.z - min.z); - matrix[3][2] = -(max.z + min.z) / (max.z - min.z); + columns[0][2] = 0; + columns[1][2] = 0; + columns[2][2] = 2 / (max.z - min.z); + columns[3][2] = -(max.z + min.z) / (max.z - min.z); - matrix[0][3] = 0; - matrix[1][3] = 0; - matrix[2][3] = 0; - matrix[3][3] = 1; + columns[0][3] = 0; + columns[1][3] = 0; + columns[2][3] = 0; + columns[3][3] = 1; } void Projection::add_jitter_offset(const Vector2 &p_offset) { - matrix[3][0] += p_offset.x; - matrix[3][1] += p_offset.y; + columns[3][0] += p_offset.x; + columns[3][1] += p_offset.y; } Projection::operator Transform3D() const { Transform3D tr; - const real_t *m = &matrix[0][0]; + const real_t *m = &columns[0][0]; tr.basis.rows[0][0] = m[0]; tr.basis.rows[1][0] = m[1]; @@ -899,15 +903,17 @@ Projection::operator Transform3D() const { return tr; } + Projection::Projection(const Vector4 &p_x, const Vector4 &p_y, const Vector4 &p_z, const Vector4 &p_w) { - matrix[0] = p_x; - matrix[1] = p_y; - matrix[2] = p_z; - matrix[3] = p_w; + columns[0] = p_x; + columns[1] = p_y; + columns[2] = p_z; + columns[3] = p_w; } + Projection::Projection(const Transform3D &p_transform) { const Transform3D &tr = p_transform; - real_t *m = &matrix[0][0]; + real_t *m = &columns[0][0]; m[0] = tr.basis.rows[0][0]; m[1] = tr.basis.rows[1][0]; diff --git a/core/math/projection.h b/core/math/projection.h index a3d2d7720b..38fb9781ae 100644 --- a/core/math/projection.h +++ b/core/math/projection.h @@ -31,10 +31,11 @@ #ifndef PROJECTION_H #define PROJECTION_H -#include "core/math/math_defs.h" #include "core/math/vector3.h" #include "core/math/vector4.h" -#include "core/templates/vector.h" + +template <class T> +class Vector; struct AABB; struct Plane; @@ -42,7 +43,7 @@ struct Rect2; struct Transform3D; struct Vector2; -struct Projection { +struct _NO_DISCARD_ Projection { enum Planes { PLANE_NEAR, PLANE_FAR, @@ -52,16 +53,16 @@ struct Projection { PLANE_BOTTOM }; - Vector4 matrix[4]; + Vector4 columns[4]; _FORCE_INLINE_ const Vector4 &operator[](const int p_axis) const { DEV_ASSERT((unsigned int)p_axis < 4); - return matrix[p_axis]; + return columns[p_axis]; } _FORCE_INLINE_ Vector4 &operator[](const int p_axis) { DEV_ASSERT((unsigned int)p_axis < 4); - return matrix[p_axis]; + return columns[p_axis]; } float determinant() const; @@ -96,7 +97,7 @@ struct Projection { Projection jitter_offseted(const Vector2 &p_offset) const; static real_t get_fovy(real_t p_fovx, real_t p_aspect) { - return Math::rad2deg(Math::atan(p_aspect * Math::tan(Math::deg2rad(p_fovx) * 0.5)) * 2.0); + return Math::rad_to_deg(Math::atan(p_aspect * Math::tan(Math::deg_to_rad(p_fovx) * 0.5)) * 2.0); } real_t get_z_far() const; @@ -135,7 +136,7 @@ struct Projection { bool operator==(const Projection &p_cam) const { for (uint32_t i = 0; i < 4; i++) { for (uint32_t j = 0; j < 4; j++) { - if (matrix[i][j] != p_cam.matrix[i][j]) { + if (columns[i][j] != p_cam.columns[i][j]) { return false; } } @@ -157,10 +158,10 @@ struct Projection { Vector3 Projection::xform(const Vector3 &p_vec3) const { Vector3 ret; - ret.x = matrix[0][0] * p_vec3.x + matrix[1][0] * p_vec3.y + matrix[2][0] * p_vec3.z + matrix[3][0]; - ret.y = matrix[0][1] * p_vec3.x + matrix[1][1] * p_vec3.y + matrix[2][1] * p_vec3.z + matrix[3][1]; - ret.z = matrix[0][2] * p_vec3.x + matrix[1][2] * p_vec3.y + matrix[2][2] * p_vec3.z + matrix[3][2]; - real_t w = matrix[0][3] * p_vec3.x + matrix[1][3] * p_vec3.y + matrix[2][3] * p_vec3.z + matrix[3][3]; + ret.x = columns[0][0] * p_vec3.x + columns[1][0] * p_vec3.y + columns[2][0] * p_vec3.z + columns[3][0]; + ret.y = columns[0][1] * p_vec3.x + columns[1][1] * p_vec3.y + columns[2][1] * p_vec3.z + columns[3][1]; + ret.z = columns[0][2] * p_vec3.x + columns[1][2] * p_vec3.y + columns[2][2] * p_vec3.z + columns[3][2]; + real_t w = columns[0][3] * p_vec3.x + columns[1][3] * p_vec3.y + columns[2][3] * p_vec3.z + columns[3][3]; return ret / w; } diff --git a/core/math/quaternion.cpp b/core/math/quaternion.cpp index c681c60694..942a0b766e 100644 --- a/core/math/quaternion.cpp +++ b/core/math/quaternion.cpp @@ -31,32 +31,18 @@ #include "quaternion.h" #include "core/math/basis.h" -#include "core/string/print_string.h" +#include "core/string/ustring.h" real_t Quaternion::angle_to(const Quaternion &p_to) const { real_t d = dot(p_to); return Math::acos(CLAMP(d * d * 2 - 1, -1, 1)); } -// get_euler_xyz returns a vector containing the Euler angles in the format -// (ax,ay,az), where ax is the angle of rotation around x axis, -// and similar for other axes. -// This implementation uses XYZ convention (Z is the first rotation). -Vector3 Quaternion::get_euler_xyz() const { - Basis m(*this); - return m.get_euler(Basis::EULER_ORDER_XYZ); -} - -// get_euler_yxz returns a vector containing the Euler angles in the format -// (ax,ay,az), where ax is the angle of rotation around x axis, -// and similar for other axes. -// This implementation uses YXZ convention (Z is the first rotation). -Vector3 Quaternion::get_euler_yxz() 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."); #endif - Basis m(*this); - return m.get_euler(Basis::EULER_ORDER_YXZ); + return Basis(*this).get_euler(p_order); } void Quaternion::operator*=(const Quaternion &p_q) { @@ -79,6 +65,10 @@ bool Quaternion::is_equal_approx(const Quaternion &p_quaternion) const { return Math::is_equal_approx(x, p_quaternion.x) && Math::is_equal_approx(y, p_quaternion.y) && Math::is_equal_approx(z, p_quaternion.z) && Math::is_equal_approx(w, p_quaternion.w); } +bool Quaternion::is_finite() const { + return Math::is_finite(x) && Math::is_finite(y) && Math::is_finite(z) && Math::is_finite(w); +} + real_t Quaternion::length() const { return Math::sqrt(length_squared()); } @@ -112,10 +102,11 @@ Quaternion Quaternion::exp() const { Quaternion src = *this; Vector3 src_v = Vector3(src.x, src.y, src.z); real_t theta = src_v.length(); - if (theta < CMP_EPSILON) { + src_v = src_v.normalized(); + if (theta < CMP_EPSILON || !src_v.is_normalized()) { return Quaternion(0, 0, 0, 1); } - return Quaternion(src_v.normalized(), theta); + return Quaternion(src_v, theta); } Quaternion Quaternion::slerp(const Quaternion &p_to, const real_t &p_weight) const { @@ -233,6 +224,57 @@ Quaternion Quaternion::spherical_cubic_interpolate(const Quaternion &p_b, const return q1.slerp(q2, p_weight); } +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."); +#endif + Quaternion from_q = *this; + Quaternion pre_q = p_pre_a; + Quaternion to_q = p_b; + Quaternion post_q = p_post_b; + + // Align flip phases. + from_q = Basis(from_q).get_rotation_quaternion(); + pre_q = Basis(pre_q).get_rotation_quaternion(); + to_q = Basis(to_q).get_rotation_quaternion(); + post_q = Basis(post_q).get_rotation_quaternion(); + + // Flip quaternions to shortest path if necessary. + bool flip1 = signbit(from_q.dot(pre_q)); + pre_q = flip1 ? -pre_q : pre_q; + bool flip2 = signbit(from_q.dot(to_q)); + to_q = flip2 ? -to_q : to_q; + bool flip3 = flip2 ? to_q.dot(post_q) <= 0 : signbit(to_q.dot(post_q)); + post_q = flip3 ? -post_q : post_q; + + // Calc by Expmap in from_q space. + Quaternion ln_from = Quaternion(0, 0, 0, 0); + Quaternion ln_to = (from_q.inverse() * to_q).log(); + Quaternion ln_pre = (from_q.inverse() * pre_q).log(); + Quaternion ln_post = (from_q.inverse() * post_q).log(); + Quaternion ln = Quaternion(0, 0, 0, 0); + ln.x = Math::cubic_interpolate_in_time(ln_from.x, ln_to.x, ln_pre.x, ln_post.x, p_weight, p_b_t, p_pre_a_t, p_post_b_t); + ln.y = Math::cubic_interpolate_in_time(ln_from.y, ln_to.y, ln_pre.y, ln_post.y, p_weight, p_b_t, p_pre_a_t, p_post_b_t); + ln.z = Math::cubic_interpolate_in_time(ln_from.z, ln_to.z, ln_pre.z, ln_post.z, p_weight, p_b_t, p_pre_a_t, p_post_b_t); + Quaternion q1 = from_q * ln.exp(); + + // Calc by Expmap in to_q space. + ln_from = (to_q.inverse() * from_q).log(); + ln_to = Quaternion(0, 0, 0, 0); + ln_pre = (to_q.inverse() * pre_q).log(); + ln_post = (to_q.inverse() * post_q).log(); + ln = Quaternion(0, 0, 0, 0); + ln.x = Math::cubic_interpolate_in_time(ln_from.x, ln_to.x, ln_pre.x, ln_post.x, p_weight, p_b_t, p_pre_a_t, p_post_b_t); + ln.y = Math::cubic_interpolate_in_time(ln_from.y, ln_to.y, ln_pre.y, ln_post.y, p_weight, p_b_t, p_pre_a_t, p_post_b_t); + ln.z = Math::cubic_interpolate_in_time(ln_from.z, ln_to.z, ln_pre.z, ln_post.z, p_weight, p_b_t, p_pre_a_t, p_post_b_t); + Quaternion q2 = to_q * ln.exp(); + + // To cancel error made by Expmap ambiguity, do blends. + return q1.slerp(q2, p_weight); +} + Quaternion::operator String() const { return "(" + String::num_real(x, false) + ", " + String::num_real(y, false) + ", " + String::num_real(z, false) + ", " + String::num_real(w, false) + ")"; } @@ -274,7 +316,7 @@ Quaternion::Quaternion(const Vector3 &p_axis, real_t p_angle) { // (ax, ay, az), where ax is the angle of rotation around x axis, // and similar for other axes. // This implementation uses YXZ convention (Z is the first rotation). -Quaternion::Quaternion(const Vector3 &p_euler) { +Quaternion Quaternion::from_euler(const Vector3 &p_euler) { real_t half_a1 = p_euler.y * 0.5f; real_t half_a2 = p_euler.x * 0.5f; real_t half_a3 = p_euler.z * 0.5f; @@ -290,8 +332,9 @@ Quaternion::Quaternion(const Vector3 &p_euler) { real_t cos_a3 = Math::cos(half_a3); real_t sin_a3 = Math::sin(half_a3); - x = sin_a1 * cos_a2 * sin_a3 + cos_a1 * sin_a2 * cos_a3; - y = sin_a1 * cos_a2 * cos_a3 - cos_a1 * sin_a2 * sin_a3; - z = -sin_a1 * sin_a2 * cos_a3 + cos_a1 * cos_a2 * sin_a3; - w = sin_a1 * sin_a2 * sin_a3 + cos_a1 * cos_a2 * cos_a3; + return Quaternion( + sin_a1 * cos_a2 * sin_a3 + cos_a1 * sin_a2 * cos_a3, + sin_a1 * cos_a2 * cos_a3 - cos_a1 * sin_a2 * sin_a3, + -sin_a1 * sin_a2 * cos_a3 + cos_a1 * cos_a2 * sin_a3, + sin_a1 * sin_a2 * sin_a3 + cos_a1 * cos_a2 * cos_a3); } diff --git a/core/math/quaternion.h b/core/math/quaternion.h index cb54a6f540..c5af2121d9 100644 --- a/core/math/quaternion.h +++ b/core/math/quaternion.h @@ -31,10 +31,10 @@ #ifndef QUATERNION_H #define QUATERNION_H -#include "core/math/math_defs.h" #include "core/math/math_funcs.h" #include "core/math/vector3.h" -#include "core/string/ustring.h" + +class String; struct _NO_DISCARD_ Quaternion { union { @@ -55,6 +55,7 @@ struct _NO_DISCARD_ Quaternion { } _FORCE_INLINE_ real_t length_squared() const; bool is_equal_approx(const Quaternion &p_quaternion) const; + bool is_finite() const; real_t length() const; void normalize(); Quaternion normalized() const; @@ -65,13 +66,13 @@ struct _NO_DISCARD_ Quaternion { _FORCE_INLINE_ real_t dot(const Quaternion &p_q) const; real_t angle_to(const Quaternion &p_to) const; - Vector3 get_euler_xyz() const; - Vector3 get_euler_yxz() const; - Vector3 get_euler() const { return get_euler_yxz(); }; + Vector3 get_euler(EulerOrder p_order = EulerOrder::YXZ) const; + static Quaternion from_euler(const Vector3 &p_euler); Quaternion slerp(const Quaternion &p_to, const real_t &p_weight) const; Quaternion slerpni(const Quaternion &p_to, const real_t &p_weight) const; Quaternion spherical_cubic_interpolate(const Quaternion &p_b, const Quaternion &p_pre_a, const Quaternion &p_post_b, const real_t &p_weight) const; + 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; Vector3 get_axis() const; real_t get_angle() const; @@ -126,8 +127,6 @@ struct _NO_DISCARD_ Quaternion { Quaternion(const Vector3 &p_axis, real_t p_angle); - Quaternion(const Vector3 &p_euler); - Quaternion(const Quaternion &p_q) : x(p_q.x), y(p_q.y), @@ -142,8 +141,7 @@ struct _NO_DISCARD_ Quaternion { w = p_q.w; } - Quaternion(const Vector3 &v0, const Vector3 &v1) // shortest arc - { + Quaternion(const Vector3 &v0, const Vector3 &v1) { // Shortest arc. Vector3 c = v0.cross(v1); real_t d = v0.dot(v1); diff --git a/core/math/quick_hull.cpp b/core/math/quick_hull.cpp index c7727a44a1..c194e1cc21 100644 --- a/core/math/quick_hull.cpp +++ b/core/math/quick_hull.cpp @@ -369,7 +369,7 @@ Error QuickHull::build(const Vector<Vector3> &p_points, Geometry3D::MeshData &r_ for (List<Geometry3D::MeshData::Face>::Element *E = ret_faces.front(); E; E = E->next()) { Geometry3D::MeshData::Face &f = E->get(); - for (int i = 0; i < f.indices.size(); i++) { + for (uint32_t i = 0; i < f.indices.size(); i++) { int a = E->get().indices[i]; int b = E->get().indices[(i + 1) % f.indices.size()]; Edge e(a, b); @@ -436,17 +436,24 @@ Error QuickHull::build(const Vector<Vector3> &p_points, Geometry3D::MeshData &r_ r_mesh.faces.clear(); r_mesh.faces.resize(ret_faces.size()); + HashMap<List<Geometry3D::MeshData::Face>::Element *, int> face_indices; + int idx = 0; - for (const Geometry3D::MeshData::Face &E : ret_faces) { - r_mesh.faces.write[idx++] = E; + for (List<Geometry3D::MeshData::Face>::Element *E = ret_faces.front(); E; E = E->next()) { + face_indices[E] = idx; + r_mesh.faces[idx++] = E->get(); } r_mesh.edges.resize(ret_edges.size()); idx = 0; for (const KeyValue<Edge, RetFaceConnect> &E : ret_edges) { Geometry3D::MeshData::Edge e; - e.a = E.key.vertices[0]; - e.b = E.key.vertices[1]; - r_mesh.edges.write[idx++] = e; + e.vertex_a = E.key.vertices[0]; + e.vertex_b = E.key.vertices[1]; + ERR_CONTINUE(!face_indices.has(E.value.left)); + ERR_CONTINUE(!face_indices.has(E.value.right)); + e.face_a = face_indices[E.value.left]; + e.face_b = face_indices[E.value.right]; + r_mesh.edges[idx++] = e; } r_mesh.vertices = p_points; diff --git a/core/math/random_number_generator.h b/core/math/random_number_generator.h index 9352bae0a6..bf67154cd6 100644 --- a/core/math/random_number_generator.h +++ b/core/math/random_number_generator.h @@ -57,7 +57,7 @@ public: _FORCE_INLINE_ real_t randfn(real_t p_mean = 0.0, real_t p_deviation = 1.0) { return randbase.randfn(p_mean, p_deviation); } _FORCE_INLINE_ int randi_range(int p_from, int p_to) { return randbase.random(p_from, p_to); } - RandomNumberGenerator() {} + RandomNumberGenerator() { randbase.randomize(); } }; #endif // RANDOM_NUMBER_GENERATOR_H diff --git a/core/math/rect2.cpp b/core/math/rect2.cpp index 9e78ead816..facf4eb3c4 100644 --- a/core/math/rect2.cpp +++ b/core/math/rect2.cpp @@ -38,6 +38,10 @@ bool Rect2::is_equal_approx(const Rect2 &p_rect) const { return position.is_equal_approx(p_rect.position) && size.is_equal_approx(p_rect.size); } +bool Rect2::is_finite() const { + return position.is_finite() && size.is_finite(); +} + bool Rect2::intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2 *r_pos, Point2 *r_normal) const { #ifdef MATH_CHECKS if (unlikely(size.x < 0 || size.y < 0)) { diff --git a/core/math/rect2.h b/core/math/rect2.h index 679af933c2..9863405d8e 100644 --- a/core/math/rect2.h +++ b/core/math/rect2.h @@ -140,8 +140,8 @@ struct _NO_DISCARD_ Rect2 { ((p_rect.position.y + p_rect.size.y) <= (position.y + size.y)); } - _FORCE_INLINE_ bool has_no_area() const { - return (size.x <= 0 || size.y <= 0); + _FORCE_INLINE_ bool has_area() const { + return size.x > 0.0f && size.y > 0.0f; } // Returns the instersection between two Rect2s or an empty Rect2 if there is no intersection @@ -178,7 +178,7 @@ struct _NO_DISCARD_ Rect2 { new_rect.size.x = MAX(p_rect.position.x + p_rect.size.x, position.x + size.x); new_rect.size.y = MAX(p_rect.position.y + p_rect.size.y, position.y + size.y); - new_rect.size = new_rect.size - new_rect.position; //make relative again + new_rect.size = new_rect.size - new_rect.position; // Make relative again. return new_rect; } @@ -207,6 +207,7 @@ struct _NO_DISCARD_ Rect2 { } bool is_equal_approx(const Rect2 &p_rect) const; + bool is_finite() const; bool operator==(const Rect2 &p_rect) const { return position == p_rect.position && size == p_rect.size; } bool operator!=(const Rect2 &p_rect) const { return position != p_rect.position || size != p_rect.size; } @@ -253,7 +254,7 @@ struct _NO_DISCARD_ Rect2 { return r; } - inline void expand_to(const Vector2 &p_vector) { //in place function for speed + inline void expand_to(const Vector2 &p_vector) { // In place function for speed. #ifdef MATH_CHECKS if (unlikely(size.x < 0 || size.y < 0)) { ERR_PRINT("Rect2 size is negative, this is not supported. Use Rect2.abs() to get a Rect2 with a positive size."); @@ -281,7 +282,7 @@ struct _NO_DISCARD_ Rect2 { } _FORCE_INLINE_ Rect2 abs() const { - return Rect2(Point2(position.x + MIN(size.x, 0), position.y + MIN(size.y, 0)), size.abs()); + return Rect2(Point2(position.x + MIN(size.x, (real_t)0), position.y + MIN(size.y, (real_t)0)), size.abs()); } Vector2 get_support(const Vector2 &p_normal) const { @@ -311,7 +312,7 @@ struct _NO_DISCARD_ Rect2 { continue; } - //check inside + // Check inside. Vector2 tg = r.orthogonal(); float s = tg.dot(center) - tg.dot(a); if (s < 0.0f) { @@ -320,7 +321,7 @@ struct _NO_DISCARD_ Rect2 { side_minus++; } - //check ray box + // Check ray box. r /= l; Vector2 ir(1.0f / r.x, 1.0f / r.y); @@ -341,7 +342,7 @@ struct _NO_DISCARD_ Rect2 { } if (side_plus * side_minus == 0) { - return true; //all inside + return true; // All inside. } else { return false; } diff --git a/core/math/rect2i.h b/core/math/rect2i.h index db1459a3e6..c92f2cae02 100644 --- a/core/math/rect2i.h +++ b/core/math/rect2i.h @@ -83,8 +83,8 @@ struct _NO_DISCARD_ Rect2i { ((p_rect.position.y + p_rect.size.y) <= (position.y + size.y)); } - _FORCE_INLINE_ bool has_no_area() const { - return (size.x <= 0 || size.y <= 0); + _FORCE_INLINE_ bool has_area() const { + return size.x > 0 && size.y > 0; } // Returns the instersection between two Rect2is or an empty Rect2i if there is no intersection @@ -121,7 +121,7 @@ struct _NO_DISCARD_ Rect2i { new_rect.size.x = MAX(p_rect.position.x + p_rect.size.x, position.x + size.x); new_rect.size.y = MAX(p_rect.position.y + p_rect.size.y, position.y + size.y); - new_rect.size = new_rect.size - new_rect.position; //make relative again + new_rect.size = new_rect.size - new_rect.position; // Make relative again. return new_rect; } diff --git a/core/math/transform_2d.cpp b/core/math/transform_2d.cpp index 226076029b..548a82d254 100644 --- a/core/math/transform_2d.cpp +++ b/core/math/transform_2d.cpp @@ -168,6 +168,10 @@ bool Transform2D::is_equal_approx(const Transform2D &p_transform) const { return columns[0].is_equal_approx(p_transform.columns[0]) && columns[1].is_equal_approx(p_transform.columns[1]) && columns[2].is_equal_approx(p_transform.columns[2]); } +bool Transform2D::is_finite() const { + return columns[0].is_finite() && columns[1].is_finite() && columns[2].is_finite(); +} + Transform2D Transform2D::looking_at(const Vector2 &p_target) const { Transform2D return_trans = Transform2D(get_rotation(), get_origin()); Vector2 target_position = affine_inverse().xform(p_target); @@ -282,7 +286,7 @@ Transform2D Transform2D::interpolate_with(const Transform2D &p_transform, const real_t dot = v1.dot(v2); - dot = CLAMP(dot, -1.0f, 1.0f); + dot = CLAMP(dot, (real_t)-1.0, (real_t)1.0); Vector2 v; diff --git a/core/math/transform_2d.h b/core/math/transform_2d.h index f23f32867a..2b11f36535 100644 --- a/core/math/transform_2d.h +++ b/core/math/transform_2d.h @@ -98,6 +98,7 @@ struct _NO_DISCARD_ Transform2D { void orthonormalize(); Transform2D orthonormalized() const; bool is_equal_approx(const Transform2D &p_transform) const; + bool is_finite() const; Transform2D looking_at(const Vector2 &p_target) const; diff --git a/core/math/transform_3d.cpp b/core/math/transform_3d.cpp index a634faca9a..3285cbd664 100644 --- a/core/math/transform_3d.cpp +++ b/core/math/transform_3d.cpp @@ -31,7 +31,7 @@ #include "transform_3d.h" #include "core/math/math_funcs.h" -#include "core/string/print_string.h" +#include "core/string/ustring.h" void Transform3D::affine_invert() { basis.invert(); @@ -94,9 +94,7 @@ void Transform3D::set_look_at(const Vector3 &p_eye, const Vector3 &p_target, con origin = p_eye; } -Transform3D Transform3D::spherical_interpolate_with(const Transform3D &p_transform, real_t p_c) const { - /* not sure if very "efficient" but good enough? */ - +Transform3D Transform3D::interpolate_with(const Transform3D &p_transform, real_t p_c) const { Transform3D interp; Vector3 src_scale = basis.get_scale(); @@ -113,15 +111,6 @@ Transform3D Transform3D::spherical_interpolate_with(const Transform3D &p_transfo return interp; } -Transform3D Transform3D::interpolate_with(const Transform3D &p_transform, real_t p_c) const { - Transform3D interp; - - interp.basis = basis.lerp(p_transform.basis, p_c); - interp.origin = origin.lerp(p_transform.origin, p_c); - - return interp; -} - void Transform3D::scale(const Vector3 &p_scale) { basis.scale(p_scale); origin *= p_scale; @@ -185,6 +174,10 @@ bool Transform3D::is_equal_approx(const Transform3D &p_transform) const { return basis.is_equal_approx(p_transform.basis) && origin.is_equal_approx(p_transform.origin); } +bool Transform3D::is_finite() const { + return basis.is_finite() && origin.is_finite(); +} + bool Transform3D::operator==(const Transform3D &p_transform) const { return (basis == p_transform.basis && origin == p_transform.origin); } diff --git a/core/math/transform_3d.h b/core/math/transform_3d.h index b572e90859..cb347aa1c1 100644 --- a/core/math/transform_3d.h +++ b/core/math/transform_3d.h @@ -34,6 +34,7 @@ #include "core/math/aabb.h" #include "core/math/basis.h" #include "core/math/plane.h" +#include "core/templates/vector.h" struct _NO_DISCARD_ Transform3D { Basis basis; @@ -74,6 +75,7 @@ struct _NO_DISCARD_ Transform3D { void orthogonalize(); Transform3D orthogonalized() const; bool is_equal_approx(const Transform3D &p_transform) const; + bool is_finite() const; bool operator==(const Transform3D &p_transform) const; bool operator!=(const Transform3D &p_transform) const; @@ -103,7 +105,6 @@ struct _NO_DISCARD_ Transform3D { void operator*=(const real_t p_val); Transform3D operator*(const real_t p_val) const; - Transform3D spherical_interpolate_with(const Transform3D &p_transform, real_t p_c) const; Transform3D interpolate_with(const Transform3D &p_transform, real_t p_c) const; _FORCE_INLINE_ Transform3D inverse_xform(const Transform3D &t) const { diff --git a/core/math/triangle_mesh.cpp b/core/math/triangle_mesh.cpp index 4433559e6d..6515c55a85 100644 --- a/core/math/triangle_mesh.cpp +++ b/core/math/triangle_mesh.cpp @@ -215,10 +215,8 @@ Vector3 TriangleMesh::get_area_normal(const AABB &p_aabb) const { switch (stack[level] >> VISITED_BIT_SHIFT) { case TEST_AABB_BIT: { - bool valid = b.aabb.intersects(p_aabb); - if (!valid) { + if (!b.aabb.intersects(p_aabb)) { stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node; - } else { if (b.face_index >= 0) { const Triangle &s = triangleptr[b.face_index]; @@ -302,12 +300,8 @@ bool TriangleMesh::intersect_segment(const Vector3 &p_begin, const Vector3 &p_en switch (stack[level] >> VISITED_BIT_SHIFT) { case TEST_AABB_BIT: { - bool valid = b.aabb.intersects_segment(p_begin, p_end); - //bool valid = b.aabb.intersects(ray_aabb); - - if (!valid) { + if (!b.aabb.intersects_segment(p_begin, p_end)) { stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node; - } else { if (b.face_index >= 0) { const Triangle &s = triangleptr[b.face_index]; @@ -407,10 +401,8 @@ bool TriangleMesh::intersect_ray(const Vector3 &p_begin, const Vector3 &p_dir, V switch (stack[level] >> VISITED_BIT_SHIFT) { case TEST_AABB_BIT: { - bool valid = b.aabb.intersects_ray(p_begin, p_dir); - if (!valid) { + if (!b.aabb.intersects_ray(p_begin, p_dir)) { stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node; - } else { if (b.face_index >= 0) { const Triangle &s = triangleptr[b.face_index]; @@ -508,10 +500,8 @@ bool TriangleMesh::intersect_convex_shape(const Plane *p_planes, int p_plane_cou switch (stack[level] >> VISITED_BIT_SHIFT) { case TEST_AABB_BIT: { - bool valid = b.aabb.intersects_convex_shape(p_planes, p_plane_count, p_points, p_point_count); - if (!valid) { + if (!b.aabb.intersects_convex_shape(p_planes, p_plane_count, p_points, p_point_count)) { stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node; - } else { if (b.face_index >= 0) { const Triangle &s = triangleptr[b.face_index]; diff --git a/core/math/vector2.cpp b/core/math/vector2.cpp index d9b5d55454..5366587126 100644 --- a/core/math/vector2.cpp +++ b/core/math/vector2.cpp @@ -182,6 +182,14 @@ bool Vector2::is_equal_approx(const Vector2 &p_v) const { return Math::is_equal_approx(x, p_v.x) && Math::is_equal_approx(y, p_v.y); } +bool Vector2::is_zero_approx() const { + return Math::is_zero_approx(x) && Math::is_zero_approx(y); +} + +bool Vector2::is_finite() const { + return Math::is_finite(x) && Math::is_finite(y); +} + Vector2::operator String() const { return "(" + String::num_real(x, false) + ", " + String::num_real(y, false) + ")"; } diff --git a/core/math/vector2.h b/core/math/vector2.h index 91d3d3a56b..5775d8e735 100644 --- a/core/math/vector2.h +++ b/core/math/vector2.h @@ -69,10 +69,6 @@ struct _NO_DISCARD_ Vector2 { return coord[p_idx]; } - _FORCE_INLINE_ void set_all(const real_t p_value) { - x = y = p_value; - } - _FORCE_INLINE_ Vector2::Axis min_axis_index() const { return x < y ? Vector2::AXIS_X : Vector2::AXIS_Y; } @@ -114,6 +110,7 @@ struct _NO_DISCARD_ Vector2 { _FORCE_INLINE_ Vector2 lerp(const Vector2 &p_to, const real_t p_weight) const; _FORCE_INLINE_ Vector2 slerp(const Vector2 &p_to, const real_t p_weight) const; _FORCE_INLINE_ Vector2 cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, const real_t p_weight) const; + _FORCE_INLINE_ Vector2 cubic_interpolate_in_time(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &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; _FORCE_INLINE_ Vector2 bezier_interpolate(const Vector2 &p_control_1, const Vector2 &p_control_2, const Vector2 &p_end, const real_t p_t) const; Vector2 move_toward(const Vector2 &p_to, const real_t p_delta) const; @@ -123,6 +120,8 @@ struct _NO_DISCARD_ Vector2 { Vector2 reflect(const Vector2 &p_normal) const; bool is_equal_approx(const Vector2 &p_v) const; + bool is_zero_approx() const; + bool is_finite() const; Vector2 operator+(const Vector2 &p_v) const; void operator+=(const Vector2 &p_v); @@ -270,6 +269,13 @@ Vector2 Vector2::cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, c return res; } +Vector2 Vector2::cubic_interpolate_in_time(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &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 { + Vector2 res = *this; + res.x = Math::cubic_interpolate_in_time(res.x, p_b.x, p_pre_a.x, p_post_b.x, p_weight, p_b_t, p_pre_a_t, p_post_b_t); + res.y = Math::cubic_interpolate_in_time(res.y, p_b.y, p_pre_a.y, p_post_b.y, p_weight, p_b_t, p_pre_a_t, p_post_b_t); + return res; +} + Vector2 Vector2::bezier_interpolate(const Vector2 &p_control_1, const Vector2 &p_control_2, const Vector2 &p_end, const real_t p_t) const { Vector2 res = *this; diff --git a/core/math/vector2i.h b/core/math/vector2i.h index 13b70031bd..e131bdea94 100644 --- a/core/math/vector2i.h +++ b/core/math/vector2i.h @@ -38,6 +38,8 @@ class String; struct Vector2; struct _NO_DISCARD_ Vector2i { + static const int AXIS_COUNT = 2; + enum Axis { AXIS_X, AXIS_Y, @@ -115,7 +117,7 @@ struct _NO_DISCARD_ Vector2i { real_t aspect() const { return width / (real_t)height; } Vector2i sign() const { return Vector2i(SIGN(x), SIGN(y)); } - Vector2i abs() const { return Vector2i(ABS(x), ABS(y)); } + Vector2i abs() const { return Vector2i(Math::abs(x), Math::abs(y)); } Vector2i clamp(const Vector2i &p_min, const Vector2i &p_max) const; operator String() const; diff --git a/core/math/vector3.cpp b/core/math/vector3.cpp index d71d365053..b106200c4a 100644 --- a/core/math/vector3.cpp +++ b/core/math/vector3.cpp @@ -45,16 +45,6 @@ Vector3 Vector3::rotated(const Vector3 &p_axis, const real_t p_angle) const { return r; } -void Vector3::set_axis(const int p_axis, const real_t p_value) { - ERR_FAIL_INDEX(p_axis, 3); - coord[p_axis] = p_value; -} - -real_t Vector3::get_axis(const int p_axis) const { - ERR_FAIL_INDEX_V(p_axis, 3, 0); - return operator[](p_axis); -} - Vector3 Vector3::clamp(const Vector3 &p_min, const Vector3 &p_max) const { return Vector3( CLAMP(x, p_min.x, p_max.x), @@ -117,18 +107,42 @@ Vector3 Vector3::octahedron_decode(const Vector2 &p_oct) { return n.normalized(); } -Basis Vector3::outer(const Vector3 &p_with) const { - Vector3 row0(x * p_with.x, x * p_with.y, x * p_with.z); - Vector3 row1(y * p_with.x, y * p_with.y, y * p_with.z); - Vector3 row2(z * p_with.x, z * p_with.y, z * p_with.z); +Vector2 Vector3::octahedron_tangent_encode(const float sign) const { + Vector2 res = this->octahedron_encode(); + res.y = res.y * 0.5f + 0.5f; + res.y = sign >= 0.0f ? res.y : 1 - res.y; + return res; +} - return Basis(row0, row1, row2); +Vector3 Vector3::octahedron_tangent_decode(const Vector2 &p_oct, float *sign) { + Vector2 oct_compressed = p_oct; + oct_compressed.y = oct_compressed.y * 2 - 1; + *sign = oct_compressed.y >= 0.0f ? 1.0f : -1.0f; + oct_compressed.y = Math::abs(oct_compressed.y); + Vector3 res = Vector3::octahedron_decode(oct_compressed); + return res; +} + +Basis Vector3::outer(const Vector3 &p_with) const { + Basis basis; + basis.rows[0] = Vector3(x * p_with.x, x * p_with.y, x * p_with.z); + basis.rows[1] = Vector3(y * p_with.x, y * p_with.y, y * p_with.z); + basis.rows[2] = Vector3(z * p_with.x, z * p_with.y, z * p_with.z); + return basis; } bool Vector3::is_equal_approx(const Vector3 &p_v) const { return Math::is_equal_approx(x, p_v.x) && Math::is_equal_approx(y, p_v.y) && Math::is_equal_approx(z, p_v.z); } +bool Vector3::is_zero_approx() const { + return Math::is_zero_approx(x) && Math::is_zero_approx(y) && Math::is_zero_approx(z); +} + +bool Vector3::is_finite() const { + return Math::is_finite(x) && Math::is_finite(y) && Math::is_finite(z); +} + Vector3::operator String() const { return "(" + String::num_real(x, false) + ", " + String::num_real(y, false) + ", " + String::num_real(z, false) + ")"; } diff --git a/core/math/vector3.h b/core/math/vector3.h index 4ce01da60e..19771eb312 100644 --- a/core/math/vector3.h +++ b/core/math/vector3.h @@ -68,13 +68,6 @@ struct _NO_DISCARD_ Vector3 { return coord[p_axis]; } - void set_axis(const int p_axis, const real_t p_value); - real_t get_axis(const int p_axis) const; - - _FORCE_INLINE_ void set_all(const real_t p_value) { - x = y = z = p_value; - } - _FORCE_INLINE_ Vector3::Axis min_axis_index() const { return x < y ? (x < z ? Vector3::AXIS_X : Vector3::AXIS_Z) : (y < z ? Vector3::AXIS_Y : Vector3::AXIS_Z); } @@ -105,12 +98,15 @@ struct _NO_DISCARD_ Vector3 { _FORCE_INLINE_ Vector3 lerp(const Vector3 &p_to, const real_t p_weight) const; _FORCE_INLINE_ Vector3 slerp(const Vector3 &p_to, const real_t p_weight) const; _FORCE_INLINE_ Vector3 cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, const real_t p_weight) const; + _FORCE_INLINE_ Vector3 cubic_interpolate_in_time(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &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; _FORCE_INLINE_ Vector3 bezier_interpolate(const Vector3 &p_control_1, const Vector3 &p_control_2, const Vector3 &p_end, const real_t p_t) const; Vector3 move_toward(const Vector3 &p_to, const real_t p_delta) const; Vector2 octahedron_encode() const; static Vector3 octahedron_decode(const Vector2 &p_oct); + Vector2 octahedron_tangent_encode(const float sign) const; + static Vector3 octahedron_tangent_decode(const Vector2 &p_oct, float *sign); _FORCE_INLINE_ Vector3 cross(const Vector3 &p_with) const; _FORCE_INLINE_ real_t dot(const Vector3 &p_with) const; @@ -139,6 +135,8 @@ struct _NO_DISCARD_ Vector3 { _FORCE_INLINE_ Vector3 reflect(const Vector3 &p_normal) const; bool is_equal_approx(const Vector3 &p_v) const; + bool is_zero_approx() const; + bool is_finite() const; /* Operators */ @@ -246,6 +244,14 @@ Vector3 Vector3::cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, c return res; } +Vector3 Vector3::cubic_interpolate_in_time(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &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 { + Vector3 res = *this; + res.x = Math::cubic_interpolate_in_time(res.x, p_b.x, p_pre_a.x, p_post_b.x, p_weight, p_b_t, p_pre_a_t, p_post_b_t); + res.y = Math::cubic_interpolate_in_time(res.y, p_b.y, p_pre_a.y, p_post_b.y, p_weight, p_b_t, p_pre_a_t, p_post_b_t); + res.z = Math::cubic_interpolate_in_time(res.z, p_b.z, p_pre_a.z, p_post_b.z, p_weight, p_b_t, p_pre_a_t, p_post_b_t); + return res; +} + Vector3 Vector3::bezier_interpolate(const Vector3 &p_control_1, const Vector3 &p_control_2, const Vector3 &p_end, const real_t p_t) const { Vector3 res = *this; diff --git a/core/math/vector3i.cpp b/core/math/vector3i.cpp index b8e74ea6d2..b248f35035 100644 --- a/core/math/vector3i.cpp +++ b/core/math/vector3i.cpp @@ -33,16 +33,6 @@ #include "core/math/vector3.h" #include "core/string/ustring.h" -void Vector3i::set_axis(const int p_axis, const int32_t p_value) { - ERR_FAIL_INDEX(p_axis, 3); - coord[p_axis] = p_value; -} - -int32_t Vector3i::get_axis(const int p_axis) const { - ERR_FAIL_INDEX_V(p_axis, 3, 0); - return operator[](p_axis); -} - Vector3i::Axis Vector3i::min_axis_index() const { return x < y ? (x < z ? Vector3i::AXIS_X : Vector3i::AXIS_Z) : (y < z ? Vector3i::AXIS_Y : Vector3i::AXIS_Z); } diff --git a/core/math/vector3i.h b/core/math/vector3i.h index b49c1142ed..710fd96376 100644 --- a/core/math/vector3i.h +++ b/core/math/vector3i.h @@ -38,6 +38,8 @@ class String; struct Vector3; struct _NO_DISCARD_ Vector3i { + static const int AXIS_COUNT = 3; + enum Axis { AXIS_X, AXIS_Y, @@ -64,9 +66,6 @@ struct _NO_DISCARD_ Vector3i { return coord[p_axis]; } - void set_axis(const int p_axis, const int32_t p_value); - int32_t get_axis(const int p_axis) const; - Vector3i::Axis min_axis_index() const; Vector3i::Axis max_axis_index() const; @@ -128,7 +127,7 @@ double Vector3i::length() const { } Vector3i Vector3i::abs() const { - return Vector3i(ABS(x), ABS(y), ABS(z)); + return Vector3i(Math::abs(x), Math::abs(y), Math::abs(z)); } Vector3i Vector3i::sign() const { diff --git a/core/math/vector4.cpp b/core/math/vector4.cpp index 1dd5adad2b..3b189f7ed4 100644 --- a/core/math/vector4.cpp +++ b/core/math/vector4.cpp @@ -30,18 +30,7 @@ #include "vector4.h" -#include "core/math/basis.h" -#include "core/string/print_string.h" - -void Vector4::set_axis(const int p_axis, const real_t p_value) { - ERR_FAIL_INDEX(p_axis, 4); - components[p_axis] = p_value; -} - -real_t Vector4::get_axis(const int p_axis) const { - ERR_FAIL_INDEX_V(p_axis, 4, 0); - return operator[](p_axis); -} +#include "core/string/ustring.h" Vector4::Axis Vector4::min_axis_index() const { uint32_t min_index = 0; @@ -71,20 +60,39 @@ bool Vector4::is_equal_approx(const Vector4 &p_vec4) const { return Math::is_equal_approx(x, p_vec4.x) && Math::is_equal_approx(y, p_vec4.y) && Math::is_equal_approx(z, p_vec4.z) && Math::is_equal_approx(w, p_vec4.w); } +bool Vector4::is_zero_approx() const { + return Math::is_zero_approx(x) && Math::is_zero_approx(y) && Math::is_zero_approx(z) && Math::is_zero_approx(w); +} + +bool Vector4::is_finite() const { + return Math::is_finite(x) && Math::is_finite(y) && Math::is_finite(z) && Math::is_finite(w); +} + real_t Vector4::length() const { return Math::sqrt(length_squared()); } void Vector4::normalize() { - *this /= length(); + real_t lengthsq = length_squared(); + if (lengthsq == 0) { + x = y = z = w = 0; + } else { + real_t length = Math::sqrt(lengthsq); + x /= length; + y /= length; + z /= length; + w /= length; + } } Vector4 Vector4::normalized() const { - return *this / length(); + Vector4 v = *this; + v.normalize(); + return v; } bool Vector4::is_normalized() const { - return Math::is_equal_approx(length_squared(), 1, (real_t)UNIT_EPSILON); // Use less epsilon. + return Math::is_equal_approx(length_squared(), (real_t)1, (real_t)UNIT_EPSILON); } real_t Vector4::distance_to(const Vector4 &p_to) const { @@ -138,6 +146,15 @@ Vector4 Vector4::cubic_interpolate(const Vector4 &p_b, const Vector4 &p_pre_a, c return res; } +Vector4 Vector4::cubic_interpolate_in_time(const Vector4 &p_b, const Vector4 &p_pre_a, const Vector4 &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 { + Vector4 res = *this; + res.x = Math::cubic_interpolate_in_time(res.x, p_b.x, p_pre_a.x, p_post_b.x, p_weight, p_b_t, p_pre_a_t, p_post_b_t); + res.y = Math::cubic_interpolate_in_time(res.y, p_b.y, p_pre_a.y, p_post_b.y, p_weight, p_b_t, p_pre_a_t, p_post_b_t); + res.z = Math::cubic_interpolate_in_time(res.z, p_b.z, p_pre_a.z, p_post_b.z, p_weight, p_b_t, p_pre_a_t, p_post_b_t); + res.w = Math::cubic_interpolate_in_time(res.w, p_b.w, p_pre_a.w, p_post_b.w, p_weight, p_b_t, p_pre_a_t, p_post_b_t); + return res; +} + Vector4 Vector4::posmod(const real_t p_mod) const { return Vector4(Math::fposmod(x, p_mod), Math::fposmod(y, p_mod), Math::fposmod(z, p_mod), Math::fposmod(w, p_mod)); } @@ -174,3 +191,5 @@ Vector4 Vector4::clamp(const Vector4 &p_min, const Vector4 &p_max) const { Vector4::operator String() const { return "(" + String::num_real(x, false) + ", " + String::num_real(y, false) + ", " + String::num_real(z, false) + ", " + String::num_real(w, false) + ")"; } + +static_assert(sizeof(Vector4) == 4 * sizeof(real_t)); diff --git a/core/math/vector4.h b/core/math/vector4.h index d26fe15941..7c4bdc1788 100644 --- a/core/math/vector4.h +++ b/core/math/vector4.h @@ -31,12 +31,14 @@ #ifndef VECTOR4_H #define VECTOR4_H -#include "core/math/math_defs.h" +#include "core/error/error_macros.h" #include "core/math/math_funcs.h" -#include "core/math/vector3.h" -#include "core/string/ustring.h" + +class String; struct _NO_DISCARD_ Vector4 { + static const int AXIS_COUNT = 4; + enum Axis { AXIS_X, AXIS_Y, @@ -63,16 +65,13 @@ struct _NO_DISCARD_ Vector4 { return components[p_axis]; } - _FORCE_INLINE_ void set_all(const real_t p_value); - - void set_axis(const int p_axis, const real_t p_value); - real_t get_axis(const int p_axis) const; - Vector4::Axis min_axis_index() const; Vector4::Axis max_axis_index() const; _FORCE_INLINE_ real_t length_squared() const; bool is_equal_approx(const Vector4 &p_vec4) const; + bool is_zero_approx() const; + bool is_finite() const; real_t length() const; void normalize(); Vector4 normalized() const; @@ -89,6 +88,7 @@ struct _NO_DISCARD_ Vector4 { Vector4 round() const; Vector4 lerp(const Vector4 &p_to, const real_t p_weight) const; Vector4 cubic_interpolate(const Vector4 &p_b, const Vector4 &p_pre_a, const Vector4 &p_post_b, const real_t p_weight) const; + Vector4 cubic_interpolate_in_time(const Vector4 &p_b, const Vector4 &p_pre_a, const Vector4 &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; Vector4 posmod(const real_t p_mod) const; Vector4 posmodv(const Vector4 &p_modv) const; @@ -146,10 +146,6 @@ struct _NO_DISCARD_ Vector4 { } }; -void Vector4::set_all(const real_t p_value) { - x = y = z = p_value; -} - real_t Vector4::dot(const Vector4 &p_vec4) const { return x * p_vec4.x + y * p_vec4.y + z * p_vec4.z + w * p_vec4.w; } diff --git a/core/math/vector4i.cpp b/core/math/vector4i.cpp index 2dc5b74202..77f6fbd5b7 100644 --- a/core/math/vector4i.cpp +++ b/core/math/vector4i.cpp @@ -33,16 +33,6 @@ #include "core/math/vector4.h" #include "core/string/ustring.h" -void Vector4i::set_axis(const int p_axis, const int32_t p_value) { - ERR_FAIL_INDEX(p_axis, 4); - coord[p_axis] = p_value; -} - -int32_t Vector4i::get_axis(const int p_axis) const { - ERR_FAIL_INDEX_V(p_axis, 4, 0); - return operator[](p_axis); -} - Vector4i::Axis Vector4i::min_axis_index() const { uint32_t min_index = 0; int32_t min_value = x; @@ -84,8 +74,10 @@ Vector4i::operator Vector4() const { } Vector4i::Vector4i(const Vector4 &p_vec4) { - x = p_vec4.x; - y = p_vec4.y; - z = p_vec4.z; - w = p_vec4.w; + x = (int32_t)p_vec4.x; + y = (int32_t)p_vec4.y; + z = (int32_t)p_vec4.z; + w = (int32_t)p_vec4.w; } + +static_assert(sizeof(Vector4i) == 4 * sizeof(int32_t)); diff --git a/core/math/vector4i.h b/core/math/vector4i.h index 37d905878f..a32414bb18 100644 --- a/core/math/vector4i.h +++ b/core/math/vector4i.h @@ -38,6 +38,8 @@ class String; struct Vector4; struct _NO_DISCARD_ Vector4i { + static const int AXIS_COUNT = 4; + enum Axis { AXIS_X, AXIS_Y, @@ -66,9 +68,6 @@ struct _NO_DISCARD_ Vector4i { return coord[p_axis]; } - void set_axis(const int p_axis, const int32_t p_value); - int32_t get_axis(const int p_axis) const; - Vector4i::Axis min_axis_index() const; Vector4i::Axis max_axis_index() const; @@ -132,7 +131,7 @@ double Vector4i::length() const { } Vector4i Vector4i::abs() const { - return Vector4i(ABS(x), ABS(y), ABS(z), ABS(w)); + return Vector4i(Math::abs(x), Math::abs(y), Math::abs(z), Math::abs(w)); } Vector4i Vector4i::sign() const { diff --git a/core/object/class_db.cpp b/core/object/class_db.cpp index 9790cc44e3..ac6ad0fdd2 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/object/script_language.h" #include "core/os/mutex.h" #include "core/version.h" @@ -376,7 +377,12 @@ bool ClassDB::is_virtual(const StringName &p_class) { OBJTYPE_RLOCK; ClassInfo *ti = classes.getptr(p_class); - ERR_FAIL_COND_V_MSG(!ti, false, "Cannot get class '" + String(p_class) + "'."); + if (!ti) { + if (!ScriptServer::is_global_class(p_class)) { + ERR_FAIL_V_MSG(false, "Cannot get class '" + String(p_class) + "'."); + } + return false; + } #ifdef TOOLS_ENABLED if (ti->api == API_EDITOR && !Engine::get_singleton()->is_editor_hint()) { return false; @@ -981,7 +987,7 @@ void ClassDB::get_property_list(const StringName &p_class, List<PropertyInfo> *p if (p_validator) { // Making a copy as we may modify it. PropertyInfo pi_mut = pi; - p_validator->_validate_property(pi_mut); + p_validator->validate_property(pi_mut); p_list->push_back(pi_mut); } else { p_list->push_back(pi); @@ -1022,7 +1028,7 @@ bool ClassDB::get_property_info(const StringName &p_class, const StringName &p_p if (check->property_map.has(p_property)) { PropertyInfo pinfo = check->property_map[p_property]; if (p_validator) { - p_validator->_validate_property(pinfo); + p_validator->validate_property(pinfo); } if (r_info) { *r_info = pinfo; @@ -1454,7 +1460,7 @@ Variant ClassDB::class_get_default_property_value(const StringName &p_class, con if (Engine::get_singleton()->has_singleton(p_class)) { c = Engine::get_singleton()->get_singleton_object(p_class); cleanup_c = false; - } else if (ClassDB::can_instantiate(p_class) && !ClassDB::is_virtual(p_class)) { + } else if (ClassDB::can_instantiate(p_class) && !ClassDB::is_virtual(p_class)) { // Keep this condition in sync with doc_tools.cpp get_documentation_default_value. c = ClassDB::instantiate(p_class); cleanup_c = true; } @@ -1528,7 +1534,10 @@ void ClassDB::register_extension_class(ObjectNativeExtension *p_extension) { c.api = p_extension->editor_class ? API_EDITOR_EXTENSION : API_EXTENSION; c.native_extension = p_extension; c.name = p_extension->class_name; - c.creation_func = parent->creation_func; + c.is_virtual = p_extension->is_virtual; + if (!p_extension->is_abstract) { + c.creation_func = parent->creation_func; + } c.inherits = parent->name; c.class_ptr = parent->class_ptr; c.inherits_ptr = parent; @@ -1538,7 +1547,11 @@ void ClassDB::register_extension_class(ObjectNativeExtension *p_extension) { } void ClassDB::unregister_extension_class(const StringName &p_class) { - ERR_FAIL_COND(!classes.has(p_class)); + ClassInfo *c = classes.getptr(p_class); + ERR_FAIL_COND_MSG(!c, "Class " + p_class + "does not exist"); + for (KeyValue<StringName, MethodBind *> &F : c->method_map) { + memdelete(F.value); + } classes.erase(p_class); } diff --git a/core/object/make_virtuals.py b/core/object/make_virtuals.py index c18d70d9f6..61bf6d900a 100644 --- a/core/object/make_virtuals.py +++ b/core/object/make_virtuals.py @@ -5,18 +5,19 @@ mutable bool _gdvirtual_##m_name##_initialized = false;\\ mutable GDNativeExtensionClassCallVirtual _gdvirtual_##m_name = nullptr;\\ template<bool required>\\ _FORCE_INLINE_ bool _gdvirtual_##m_name##_call($CALLARGS) $CONST { \\ - ScriptInstance *script_instance = ((Object*)(this))->get_script_instance();\\ - if (script_instance) {\\ + ScriptInstance *_script_instance = ((Object*)(this))->get_script_instance();\\ + if (_script_instance) {\\ Callable::CallError ce; \\ $CALLSIARGS\\ - $CALLSIBEGINscript_instance->callp(_gdvirtual_##m_name##_sn, $CALLSIARGPASS, ce);\\ + $CALLSIBEGIN_script_instance->callp(_gdvirtual_##m_name##_sn, $CALLSIARGPASS, ce);\\ if (ce.error == Callable::CallError::CALL_OK) {\\ $CALLSIRET\\ return true;\\ } \\ }\\ if (unlikely(_get_extension() && !_gdvirtual_##m_name##_initialized)) {\\ - _gdvirtual_##m_name = (_get_extension() && _get_extension()->get_virtual) ? _get_extension()->get_virtual(_get_extension()->class_userdata, #m_name) : (GDNativeExtensionClassCallVirtual) nullptr;\\ + /* TODO: C-style cast because GDNativeStringNamePtr's const qualifier is broken (see https://github.com/godotengine/godot/pull/67751) */\\ + _gdvirtual_##m_name = (_get_extension() && _get_extension()->get_virtual) ? _get_extension()->get_virtual(_get_extension()->class_userdata, (GDNativeStringNamePtr)&_gdvirtual_##m_name##_sn) : (GDNativeExtensionClassCallVirtual) nullptr;\\ _gdvirtual_##m_name##_initialized = true;\\ }\\ if (_gdvirtual_##m_name) {\\ @@ -35,12 +36,13 @@ _FORCE_INLINE_ bool _gdvirtual_##m_name##_call($CALLARGS) $CONST { \\ return false;\\ }\\ _FORCE_INLINE_ bool _gdvirtual_##m_name##_overridden() const { \\ - ScriptInstance *script_instance = ((Object*)(this))->get_script_instance();\\ - if (script_instance) {\\ - return script_instance->has_method(_gdvirtual_##m_name##_sn);\\ + ScriptInstance *_script_instance = ((Object*)(this))->get_script_instance();\\ + if (_script_instance) {\\ + return _script_instance->has_method(_gdvirtual_##m_name##_sn);\\ }\\ if (unlikely(_get_extension() && !_gdvirtual_##m_name##_initialized)) {\\ - _gdvirtual_##m_name = (_get_extension() && _get_extension()->get_virtual) ? _get_extension()->get_virtual(_get_extension()->class_userdata, #m_name) : (GDNativeExtensionClassCallVirtual) nullptr;\\ + /* TODO: C-style cast because GDNativeStringNamePtr's const qualifier is broken (see https://github.com/godotengine/godot/pull/67751) */\\ + _gdvirtual_##m_name = (_get_extension() && _get_extension()->get_virtual) ? _get_extension()->get_virtual(_get_extension()->class_userdata, (GDNativeStringNamePtr)&_gdvirtual_##m_name##_sn) : (GDNativeExtensionClassCallVirtual) nullptr;\\ _gdvirtual_##m_name##_initialized = true;\\ }\\ if (_gdvirtual_##m_name) {\\ diff --git a/core/object/method_bind.cpp b/core/object/method_bind.cpp index 0edc7a90c2..5381c596ce 100644 --- a/core/object/method_bind.cpp +++ b/core/object/method_bind.cpp @@ -63,7 +63,9 @@ PropertyInfo MethodBind::get_argument_info(int p_argument) const { PropertyInfo info = _gen_argument_type_info(p_argument); #ifdef DEBUG_METHODS_ENABLED - info.name = p_argument < arg_names.size() ? String(arg_names[p_argument]) : String("_unnamed_arg" + itos(p_argument)); + if (info.name.is_empty()) { + info.name = p_argument < arg_names.size() ? String(arg_names[p_argument]) : String("_unnamed_arg" + itos(p_argument)); + } #endif return info; } diff --git a/core/object/method_bind.h b/core/object/method_bind.h index d60550c899..598e8a224d 100644 --- a/core/object/method_bind.h +++ b/core/object/method_bind.h @@ -241,9 +241,17 @@ class MethodBindVarArgTR : public MethodBindVarArgBase<MethodBindVarArgTR<T, R>, friend class MethodBindVarArgBase<MethodBindVarArgTR<T, R>, T, R, true>; public: +#if defined(SANITIZERS_ENABLED) && defined(__GNUC__) && !defined(__clang__) + // Workaround GH-66343 raised only with UBSAN, seems to be a false positive. +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wmaybe-uninitialized" +#endif virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) override { return (static_cast<T *>(p_object)->*MethodBindVarArgBase<MethodBindVarArgTR<T, R>, T, R, true>::method)(p_args, p_arg_count, r_error); } +#if defined(SANITIZERS_ENABLED) && defined(__GNUC__) && !defined(__clang__) +#pragma GCC diagnostic pop +#endif MethodBindVarArgTR( R (T::*p_method)(const Variant **, int, Callable::CallError &), @@ -284,11 +292,6 @@ class MethodBindT : public MethodBind { void (MB_T::*method)(P...); protected: -// GCC raises warnings in the case P = {} as the comparison is always false... -#if defined(__GNUC__) && !defined(__clang__) -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wlogical-op" -#endif virtual Variant::Type _gen_argument_type(int p_arg) const override { if (p_arg >= 0 && p_arg < (int)sizeof...(P)) { return call_get_argument_type<P...>(p_arg); @@ -296,9 +299,6 @@ protected: return Variant::NIL; } } -#if defined(__GNUC__) && !defined(__clang__) -#pragma GCC diagnostic pop -#endif virtual PropertyInfo _gen_argument_type_info(int p_arg) const override { PropertyInfo pi; @@ -359,11 +359,6 @@ class MethodBindTC : public MethodBind { void (MB_T::*method)(P...) const; protected: -// GCC raises warnings in the case P = {} as the comparison is always false... -#if defined(__GNUC__) && !defined(__clang__) -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wlogical-op" -#endif virtual Variant::Type _gen_argument_type(int p_arg) const override { if (p_arg >= 0 && p_arg < (int)sizeof...(P)) { return call_get_argument_type<P...>(p_arg); @@ -371,9 +366,6 @@ protected: return Variant::NIL; } } -#if defined(__GNUC__) && !defined(__clang__) -#pragma GCC diagnostic pop -#endif virtual PropertyInfo _gen_argument_type_info(int p_arg) const override { PropertyInfo pi; @@ -436,11 +428,6 @@ class MethodBindTR : public MethodBind { (P...); protected: -// GCC raises warnings in the case P = {} as the comparison is always false... -#if defined(__GNUC__) && !defined(__clang__) -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wlogical-op" -#endif virtual Variant::Type _gen_argument_type(int p_arg) const override { if (p_arg >= 0 && p_arg < (int)sizeof...(P)) { return call_get_argument_type<P...>(p_arg); @@ -458,9 +445,6 @@ protected: return GetTypeInfo<R>::get_class_info(); } } -#if defined(__GNUC__) && !defined(__clang__) -#pragma GCC diagnostic pop -#endif public: #ifdef DEBUG_METHODS_ENABLED @@ -523,11 +507,6 @@ class MethodBindTRC : public MethodBind { (P...) const; protected: -// GCC raises warnings in the case P = {} as the comparison is always false... -#if defined(__GNUC__) && !defined(__clang__) -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wlogical-op" -#endif virtual Variant::Type _gen_argument_type(int p_arg) const override { if (p_arg >= 0 && p_arg < (int)sizeof...(P)) { return call_get_argument_type<P...>(p_arg); @@ -545,9 +524,6 @@ protected: return GetTypeInfo<R>::get_class_info(); } } -#if defined(__GNUC__) && !defined(__clang__) -#pragma GCC diagnostic pop -#endif public: #ifdef DEBUG_METHODS_ENABLED @@ -607,11 +583,6 @@ class MethodBindTS : public MethodBind { void (*function)(P...); protected: -// GCC raises warnings in the case P = {} as the comparison is always false... -#if defined(__GNUC__) && !defined(__clang__) -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wlogical-op" -#endif virtual Variant::Type _gen_argument_type(int p_arg) const override { if (p_arg >= 0 && p_arg < (int)sizeof...(P)) { return call_get_argument_type<P...>(p_arg); @@ -619,9 +590,6 @@ protected: return Variant::NIL; } } -#if defined(__GNUC__) && !defined(__clang__) -#pragma GCC diagnostic pop -#endif virtual PropertyInfo _gen_argument_type_info(int p_arg) const override { PropertyInfo pi; @@ -670,11 +638,6 @@ class MethodBindTRS : public MethodBind { (P...); protected: -// GCC raises warnings in the case P = {} as the comparison is always false... -#if defined(__GNUC__) && !defined(__clang__) -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wlogical-op" -#endif virtual Variant::Type _gen_argument_type(int p_arg) const override { if (p_arg >= 0 && p_arg < (int)sizeof...(P)) { return call_get_argument_type<P...>(p_arg); @@ -682,9 +645,6 @@ protected: return GetTypeInfo<R>::VARIANT_TYPE; } } -#if defined(__GNUC__) && !defined(__clang__) -#pragma GCC diagnostic pop -#endif virtual PropertyInfo _gen_argument_type_info(int p_arg) const override { if (p_arg >= 0 && p_arg < (int)sizeof...(P)) { diff --git a/core/object/object.cpp b/core/object/object.cpp index 0fcd1c0e40..d27e0d7621 100644 --- a/core/object/object.cpp +++ b/core/object/object.cpp @@ -38,6 +38,7 @@ #include "core/os/os.h" #include "core/string/print_string.h" #include "core/string/translation.h" +#include "core/variant/typed_array.h" #ifdef DEBUG_ENABLED @@ -102,8 +103,8 @@ PropertyInfo PropertyInfo::from_dict(const Dictionary &p_dict) { return pi; } -Array convert_property_list(const List<PropertyInfo> *p_list) { - Array va; +TypedArray<Dictionary> convert_property_list(const List<PropertyInfo> *p_list) { + TypedArray<Dictionary> va; for (const List<PropertyInfo>::Element *E = p_list->front(); E; E = E->next()) { va.push_back(Dictionary(E->get())); } @@ -515,7 +516,61 @@ void Object::get_property_list(List<PropertyInfo> *p_list, bool p_reversed) cons } } -void Object::_validate_property(PropertyInfo &property) const { +void Object::validate_property(PropertyInfo &p_property) const { + _validate_propertyv(p_property); +} + +bool Object::property_can_revert(const StringName &p_name) const { + if (script_instance) { + if (script_instance->property_can_revert(p_name)) { + return true; + } + } + +// C style pointer casts should never trigger a compiler warning because the risk is assumed by the user, so GCC should keep quiet about it. +#if defined(__GNUC__) && !defined(__clang__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wignored-qualifiers" +#endif + if (_extension && _extension->property_can_revert) { + if (_extension->property_can_revert(_extension_instance, (const GDNativeStringNamePtr)&p_name)) { + return true; + } + } +#if defined(__GNUC__) && !defined(__clang__) +#pragma GCC diagnostic pop +#endif + + return _property_can_revertv(p_name); +} + +Variant Object::property_get_revert(const StringName &p_name) const { + Variant ret; + + if (script_instance) { + if (script_instance->property_get_revert(p_name, ret)) { + return ret; + } + } + +// C style pointer casts should never trigger a compiler warning because the risk is assumed by the user, so GCC should keep quiet about it. +#if defined(__GNUC__) && !defined(__clang__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wignored-qualifiers" +#endif + if (_extension && _extension->property_get_revert) { + if (_extension->property_get_revert(_extension_instance, (const GDNativeStringNamePtr)&p_name, (GDNativeVariantPtr)&ret)) { + return ret; + } + } +#if defined(__GNUC__) && !defined(__clang__) +#pragma GCC diagnostic pop +#endif + + if (_property_get_revertv(p_name, ret)) { + return ret; + } + return Variant(); } void Object::get_method_list(List<MethodInfo> *p_list) const { @@ -753,10 +808,11 @@ String Object::to_string() { } if (_extension && _extension->to_string) { String ret; - _extension->to_string(_extension_instance, &ret); + GDNativeBool is_valid; + _extension->to_string(_extension_instance, &is_valid, &ret); return ret; } - return "[" + get_class() + ":" + itos(get_instance_id()) + "]"; + return "<" + get_class() + "#" + itos(get_instance_id()) + ">"; } void Object::set_script_and_instance(const Variant &p_script, ScriptInstance *p_instance) { @@ -858,16 +914,16 @@ void Object::remove_meta(const StringName &p_name) { set_meta(p_name, Variant()); } -Array Object::_get_property_list_bind() const { +TypedArray<Dictionary> Object::_get_property_list_bind() const { List<PropertyInfo> lpi; get_property_list(&lpi); return convert_property_list(&lpi); } -Array Object::_get_method_list_bind() const { +TypedArray<Dictionary> Object::_get_method_list_bind() const { List<MethodInfo> ml; get_method_list(&ml); - Array ret; + TypedArray<Dictionary> ret; for (List<MethodInfo>::Element *E = ml.front(); E; E = E->next()) { Dictionary d = E->get(); @@ -1005,7 +1061,7 @@ Error Object::emit_signalp(const StringName &p_name, const Variant **p_args, int } } - bool disconnect = c.flags & CONNECT_ONESHOT; + bool disconnect = c.flags & CONNECT_ONE_SHOT; #ifdef TOOLS_ENABLED if (disconnect && (c.flags & CONNECT_PERSIST) && Engine::get_singleton()->is_editor_hint()) { //this signal was connected from the editor, and is being edited. just don't disconnect for now @@ -1055,11 +1111,11 @@ void Object::_add_user_signal(const String &p_name, const Array &p_args) { add_user_signal(mi); } -Array Object::_get_signal_list() const { +TypedArray<Dictionary> Object::_get_signal_list() const { List<MethodInfo> signal_list; get_signal_list(&signal_list); - Array ret; + TypedArray<Dictionary> ret; for (const MethodInfo &E : signal_list) { ret.push_back(Dictionary(E)); } @@ -1067,11 +1123,11 @@ Array Object::_get_signal_list() const { return ret; } -Array Object::_get_signal_connection_list(const StringName &p_signal) const { +TypedArray<Dictionary> Object::_get_signal_connection_list(const StringName &p_signal) const { List<Connection> conns; get_all_signal_connections(&conns); - Array ret; + TypedArray<Dictionary> ret; for (const Connection &c : conns) { if (c.signal.get_name() == p_signal) { @@ -1082,8 +1138,8 @@ Array Object::_get_signal_connection_list(const StringName &p_signal) const { return ret; } -Array Object::_get_incoming_connections() const { - Array ret; +TypedArray<Dictionary> Object::_get_incoming_connections() const { + TypedArray<Dictionary> ret; int connections_amount = connections.size(); for (int idx_conn = 0; idx_conn < connections_amount; idx_conn++) { ret.push_back(connections[idx_conn]); @@ -1413,8 +1469,8 @@ void Object::_bind_methods() { 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); - ClassDB::bind_method(D_METHOD("get_indexed", "property"), &Object::_get_indexed_bind); + ClassDB::bind_method(D_METHOD("set_indexed", "property_path", "value"), &Object::_set_indexed_bind); + ClassDB::bind_method(D_METHOD("get_indexed", "property_path"), &Object::_get_indexed_bind); ClassDB::bind_method(D_METHOD("get_property_list"), &Object::_get_property_list_bind); ClassDB::bind_method(D_METHOD("get_method_list"), &Object::_get_method_list_bind); ClassDB::bind_method(D_METHOD("notification", "what", "reversed"), &Object::notification, DEFVAL(false)); @@ -1500,10 +1556,17 @@ void Object::_bind_methods() { BIND_OBJ_CORE_METHOD(miget); MethodInfo plget("_get_property_list"); - plget.return_val.type = Variant::ARRAY; + plget.return_val.hint = PROPERTY_HINT_ARRAY_TYPE; + plget.return_val.hint_string = "Dictionary"; BIND_OBJ_CORE_METHOD(plget); + BIND_OBJ_CORE_METHOD(MethodInfo(Variant::BOOL, "_property_can_revert", PropertyInfo(Variant::STRING_NAME, "property"))); + MethodInfo mipgr("_property_get_revert", PropertyInfo(Variant::STRING_NAME, "property")); + mipgr.return_val.name = "Variant"; + mipgr.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT; + BIND_OBJ_CORE_METHOD(mipgr); + #endif BIND_OBJ_CORE_METHOD(MethodInfo("_init")); BIND_OBJ_CORE_METHOD(MethodInfo(Variant::STRING, "_to_string")); @@ -1513,7 +1576,7 @@ void Object::_bind_methods() { BIND_ENUM_CONSTANT(CONNECT_DEFERRED); BIND_ENUM_CONSTANT(CONNECT_PERSIST); - BIND_ENUM_CONSTANT(CONNECT_ONESHOT); + BIND_ENUM_CONSTANT(CONNECT_ONE_SHOT); BIND_ENUM_CONSTANT(CONNECT_REFERENCE_COUNTED); } @@ -1794,6 +1857,46 @@ void ObjectDB::debug_objects(DebugFunc p_func) { } void Object::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const { + if (p_idx == 0) { + if (p_function == "connect" || p_function == "is_connected" || p_function == "disconnect" || p_function == "emit_signal" || p_function == "has_signal") { + List<MethodInfo> signals; + get_signal_list(&signals); + for (const MethodInfo &E : signals) { + r_options->push_back(E.name.quote()); + } + } else if (p_function == "call" || p_function == "call_deferred" || p_function == "callv" || p_function == "has_method") { + List<MethodInfo> methods; + get_method_list(&methods); + for (const MethodInfo &E : methods) { + if (E.name.begins_with("_") && !(E.flags & METHOD_FLAG_VIRTUAL)) { + continue; + } + r_options->push_back(E.name.quote()); + } + } else if (p_function == "set" || p_function == "set_deferred" || p_function == "get") { + List<PropertyInfo> properties; + get_property_list(&properties); + for (const PropertyInfo &E : properties) { + if (E.usage & PROPERTY_USAGE_DEFAULT && !(E.usage & PROPERTY_USAGE_INTERNAL)) { + r_options->push_back(E.name.quote()); + } + } + } else if (p_function == "set_meta" || p_function == "get_meta" || p_function == "has_meta" || p_function == "remove_meta") { + for (const KeyValue<StringName, Variant> &K : metadata) { + r_options->push_back(String(K.key).quote()); + } + } + } else if (p_idx == 2) { + if (p_function == "connect") { + // Ideally, the constants should be inferred by the parameter. + // But a parameter's PropertyInfo does not store the enum they come from, so this will do for now. + List<StringName> constants; + ClassDB::get_enum_constants("Object", "ConnectFlags", &constants); + for (const StringName &E : constants) { + r_options->push_back(String(E)); + } + } + } } SpinLock ObjectDB::spin_lock; diff --git a/core/object/object.h b/core/object/object.h index 35d0aaaa7d..16ad7b8832 100644 --- a/core/object/object.h +++ b/core/object/object.h @@ -45,9 +45,12 @@ #include "core/variant/callable_bind.h" #include "core/variant/variant.h" +template <typename T> +class TypedArray; + enum PropertyHint { PROPERTY_HINT_NONE, ///< no hint provided. - PROPERTY_HINT_RANGE, ///< hint_text = "min,max[,step][,or_greater][,or_lesser][,no_slider][,radians][,degrees][,exp][,suffix:<keyword>] range. + PROPERTY_HINT_RANGE, ///< hint_text = "min,max[,step][,or_greater][,or_less][,hide_slider][,radians][,degrees][,exp][,suffix:<keyword>] range. PROPERTY_HINT_ENUM, ///< hint_text= "val1,val2,val3,etc" PROPERTY_HINT_ENUM_SUGGESTION, ///< hint_text= "val1,val2,val3,etc" PROPERTY_HINT_EXP_EASING, /// exponential easing function (Math::ease) use "attenuation" hint string to revert (flip h), "positive_only" to exclude in-out and out-in. (ie: "attenuation,positive_only") @@ -68,8 +71,6 @@ enum PropertyHint { PROPERTY_HINT_EXPRESSION, ///< used for string properties that can contain multiple lines PROPERTY_HINT_PLACEHOLDER_TEXT, ///< used to set a placeholder text for string properties PROPERTY_HINT_COLOR_NO_ALPHA, ///< used for ignoring alpha component when editing a color - PROPERTY_HINT_IMAGE_COMPRESS_LOSSY, - PROPERTY_HINT_IMAGE_COMPRESS_LOSSLESS, PROPERTY_HINT_OBJECT_ID, PROPERTY_HINT_TYPE_STRING, ///< a type string, the hint is the base type to choose PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE, ///< so something else can provide this (used in scripts) @@ -86,11 +87,13 @@ enum PropertyHint { PROPERTY_HINT_SAVE_FILE, ///< a file path must be passed, hint_text (optionally) is a filter "*.png,*.wav,*.doc,". This opens a save dialog PROPERTY_HINT_GLOBAL_SAVE_FILE, ///< a file path must be passed, hint_text (optionally) is a filter "*.png,*.wav,*.doc,". This opens a save dialog PROPERTY_HINT_INT_IS_OBJECTID, - PROPERTY_HINT_ARRAY_TYPE, PROPERTY_HINT_INT_IS_POINTER, + PROPERTY_HINT_ARRAY_TYPE, PROPERTY_HINT_LOCALE_ID, PROPERTY_HINT_LOCALIZABLE_STRING, PROPERTY_HINT_NODE_TYPE, ///< a node object type + PROPERTY_HINT_HIDE_QUATERNION_EDIT, /// Only Node3D::transform should hide the quaternion editor. + PROPERTY_HINT_PASSWORD, PROPERTY_HINT_MAX, // When updating PropertyHint, also sync the hardcoded list in VisualScriptEditorVariableEdit }; @@ -128,7 +131,7 @@ enum PropertyUsageFlags { PROPERTY_USAGE_ARRAY = 1 << 29, // Used in the inspector to group properties as elements of an array. PROPERTY_USAGE_DEFAULT = PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_EDITOR, - PROPERTY_USAGE_DEFAULT_INTL = PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_INTERNATIONALIZED, + PROPERTY_USAGE_DEFAULT_INTL = PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNATIONALIZED, PROPERTY_USAGE_NO_EDITOR = PROPERTY_USAGE_STORAGE, }; @@ -187,10 +190,10 @@ struct PropertyInfo { explicit PropertyInfo(const GDNativePropertyInfo &pinfo) : type((Variant::Type)pinfo.type), - name(pinfo.name), - class_name(pinfo.class_name), // can be null + name(*reinterpret_cast<StringName *>(pinfo.name)), + class_name(*reinterpret_cast<StringName *>(pinfo.class_name)), hint((PropertyHint)pinfo.hint), - hint_string(pinfo.hint_string), // can be null + hint_string(*reinterpret_cast<String *>(pinfo.hint_string)), usage(pinfo.usage) {} bool operator==(const PropertyInfo &p_info) const { @@ -207,7 +210,7 @@ struct PropertyInfo { } }; -Array convert_property_list(const List<PropertyInfo> *p_list); +TypedArray<Dictionary> convert_property_list(const List<PropertyInfo> *p_list); enum MethodFlags { METHOD_FLAG_NORMAL = 1, @@ -237,6 +240,20 @@ struct MethodInfo { MethodInfo() {} + explicit MethodInfo(const GDNativeMethodInfo &pinfo) : + name(*reinterpret_cast<StringName *>(pinfo.name)), + return_val(PropertyInfo(pinfo.return_value)), + flags(pinfo.flags), + id(pinfo.id) { + for (uint32_t j = 0; j < pinfo.argument_count; j++) { + arguments.push_back(PropertyInfo(pinfo.arguments[j])); + } + const Variant *def_values = (const Variant *)pinfo.default_arguments; + for (uint32_t j = 0; j < pinfo.default_argument_count; j++) { + default_arguments.push_back(def_values[j]); + } + } + void _push_params(const PropertyInfo &p_param) { arguments.push_back(p_param); } @@ -290,10 +307,14 @@ struct ObjectNativeExtension { StringName parent_class_name; StringName class_name; bool editor_class = false; + bool is_virtual = false; + bool is_abstract = false; GDNativeExtensionClassSet set; GDNativeExtensionClassGet get; GDNativeExtensionClassGetPropertyList get_property_list; GDNativeExtensionClassFreePropertyList free_property_list; + GDNativeExtensionClassPropertyCanRevert property_can_revert; + GDNativeExtensionClassPropertyGetRevert property_get_revert; GDNativeExtensionClassNotification notification; GDNativeExtensionClassToString to_string; GDNativeExtensionClassReference reference; @@ -469,6 +490,37 @@ protected: m_inherits::_get_property_listv(p_list, p_reversed); \ } \ } \ + _FORCE_INLINE_ void (Object::*_get_validate_property() const)(PropertyInfo & p_property) const { \ + return (void(Object::*)(PropertyInfo &) const) & m_class::_validate_property; \ + } \ + virtual void _validate_propertyv(PropertyInfo &p_property) const override { \ + m_inherits::_validate_propertyv(p_property); \ + if (m_class::_get_validate_property() != m_inherits::_get_validate_property()) { \ + _validate_property(p_property); \ + } \ + } \ + _FORCE_INLINE_ bool (Object::*_get_property_can_revert() const)(const StringName &p_name) const { \ + return (bool(Object::*)(const StringName &) const) & m_class::_property_can_revert; \ + } \ + virtual bool _property_can_revertv(const StringName &p_name) const override { \ + if (m_class::_get_property_can_revert() != m_inherits::_get_property_can_revert()) { \ + if (_property_can_revert(p_name)) { \ + return true; \ + } \ + } \ + return m_inherits::_property_can_revertv(p_name); \ + } \ + _FORCE_INLINE_ bool (Object::*_get_property_get_revert() const)(const StringName &p_name, Variant &) const { \ + return (bool(Object::*)(const StringName &, Variant &) const) & m_class::_property_get_revert; \ + } \ + virtual bool _property_get_revertv(const StringName &p_name, Variant &r_ret) const override { \ + if (m_class::_get_property_get_revert() != m_inherits::_get_property_get_revert()) { \ + if (_property_get_revert(p_name, r_ret)) { \ + return true; \ + } \ + } \ + return m_inherits::_property_get_revertv(p_name, r_ret); \ + } \ _FORCE_INLINE_ void (Object::*_get_notification() const)(int) { \ return (void(Object::*)(int)) & m_class::_notification; \ } \ @@ -499,7 +551,7 @@ public: enum ConnectFlags { CONNECT_DEFERRED = 1, CONNECT_PERSIST = 2, // hint for scene to save this connection - CONNECT_ONESHOT = 4, + CONNECT_ONE_SHOT = 4, CONNECT_REFERENCE_COUNTED = 8, }; @@ -564,9 +616,9 @@ private: void _add_user_signal(const String &p_name, const Array &p_args = Array()); bool _has_user_signal(const StringName &p_name) const; Error _emit_signal(const Variant **p_args, int p_argcount, Callable::CallError &r_error); - Array _get_signal_list() const; - Array _get_signal_connection_list(const StringName &p_signal) const; - Array _get_incoming_connections() const; + TypedArray<Dictionary> _get_signal_list() const; + TypedArray<Dictionary> _get_signal_connection_list(const StringName &p_signal) const; + TypedArray<Dictionary> _get_incoming_connections() const; void _set_bind(const StringName &p_set, const Variant &p_value); Variant _get_bind(const StringName &p_name) const; void _set_indexed_bind(const NodePath &p_name, const Variant &p_value); @@ -613,12 +665,18 @@ protected: virtual bool _setv(const StringName &p_name, const Variant &p_property) { return false; }; virtual bool _getv(const StringName &p_name, Variant &r_property) const { return false; }; virtual void _get_property_listv(List<PropertyInfo> *p_list, bool p_reversed) const {}; + virtual void _validate_propertyv(PropertyInfo &p_property) const {}; + virtual bool _property_can_revertv(const StringName &p_name) const { return false; }; + virtual bool _property_get_revertv(const StringName &p_name, Variant &r_property) const { return false; }; virtual void _notificationv(int p_notification, bool p_reversed) {} static void _bind_methods(); bool _set(const StringName &p_name, const Variant &p_property) { return false; }; bool _get(const StringName &p_name, Variant &r_property) const { return false; }; void _get_property_list(List<PropertyInfo> *p_list) const {}; + void _validate_property(PropertyInfo &p_property) const {}; + bool _property_can_revert(const StringName &p_name) const { return false; }; + bool _property_get_revert(const StringName &p_name, Variant &r_property) const { return false; }; void _notification(int p_notification) {} _FORCE_INLINE_ static void (*_get_bind_methods())() { @@ -633,6 +691,15 @@ protected: _FORCE_INLINE_ void (Object::*_get_get_property_list() const)(List<PropertyInfo> *p_list) const { return &Object::_get_property_list; } + _FORCE_INLINE_ void (Object::*_get_validate_property() const)(PropertyInfo &p_property) const { + return &Object::_validate_property; + } + _FORCE_INLINE_ bool (Object::*_get_property_can_revert() const)(const StringName &p_name) const { + return &Object::_property_can_revert; + } + _FORCE_INLINE_ bool (Object::*_get_property_get_revert() const)(const StringName &p_name, Variant &) const { + return &Object::_property_get_revert; + } _FORCE_INLINE_ void (Object::*_get_notification() const)(int) { return &Object::_notification; } @@ -650,13 +717,12 @@ protected: } Vector<StringName> _get_meta_list_bind() const; - Array _get_property_list_bind() const; - Array _get_method_list_bind() const; + TypedArray<Dictionary> _get_property_list_bind() const; + TypedArray<Dictionary> _get_method_list_bind() const; void _clear_internal_resource_paths(const Variant &p_var); friend class ClassDB; - virtual void _validate_property(PropertyInfo &property) const; void _disconnect(const StringName &p_signal, const Callable &p_callable, bool p_force = false); @@ -681,34 +747,12 @@ public: template <class T> static T *cast_to(Object *p_object) { -#ifndef NO_SAFE_CAST return dynamic_cast<T *>(p_object); -#else - if (!p_object) { - return nullptr; - } - if (p_object->is_class_ptr(T::get_class_ptr_static())) { - return static_cast<T *>(p_object); - } else { - return nullptr; - } -#endif } template <class T> static const T *cast_to(const Object *p_object) { -#ifndef NO_SAFE_CAST return dynamic_cast<const T *>(p_object); -#else - if (!p_object) { - return nullptr; - } - if (p_object->is_class_ptr(T::get_class_ptr_static())) { - return static_cast<const T *>(p_object); - } else { - return nullptr; - } -#endif } enum { @@ -757,6 +801,9 @@ public: Variant get_indexed(const Vector<StringName> &p_names, bool *r_valid = nullptr) const; void get_property_list(List<PropertyInfo> *p_list, bool p_reversed = false) const; + void validate_property(PropertyInfo &p_property) const; + bool property_can_revert(const StringName &p_name) const; + Variant property_get_revert(const StringName &p_name) const; bool has_method(const StringName &p_method) const; void get_method_list(List<MethodInfo> *p_list) const; diff --git a/core/object/ref_counted.cpp b/core/object/ref_counted.cpp index 726e2c012c..50467d795d 100644 --- a/core/object/ref_counted.cpp +++ b/core/object/ref_counted.cpp @@ -48,9 +48,10 @@ void RefCounted::_bind_methods() { ClassDB::bind_method(D_METHOD("init_ref"), &RefCounted::init_ref); ClassDB::bind_method(D_METHOD("reference"), &RefCounted::reference); ClassDB::bind_method(D_METHOD("unreference"), &RefCounted::unreference); + ClassDB::bind_method(D_METHOD("get_reference_count"), &RefCounted::get_reference_count); } -int RefCounted::reference_get_count() const { +int RefCounted::get_reference_count() const { return refcount.get(); } @@ -85,7 +86,8 @@ bool RefCounted::unreference() { _get_extension()->unreference(_get_extension_instance()); } - die = die && _instance_binding_reference(false); + bool binding_ret = _instance_binding_reference(false); + die = die && binding_ret; } return die; diff --git a/core/object/ref_counted.h b/core/object/ref_counted.h index bd06a84bd8..71790fb825 100644 --- a/core/object/ref_counted.h +++ b/core/object/ref_counted.h @@ -47,7 +47,7 @@ public: bool init_ref(); bool reference(); // returns false if refcount is at zero and didn't get increased bool unreference(); - int reference_get_count() const; + int get_reference_count() const; RefCounted(); ~RefCounted() {} diff --git a/core/object/script_language.cpp b/core/object/script_language.cpp index b06c2e8896..36a0d03aaf 100644 --- a/core/object/script_language.cpp +++ b/core/object/script_language.cpp @@ -34,6 +34,7 @@ #include "core/core_string_names.h" #include "core/debugger/engine_debugger.h" #include "core/debugger/script_debugger.h" +#include "core/variant/typed_array.h" #include <stdint.h> @@ -61,8 +62,8 @@ Variant Script::_get_property_default_value(const StringName &p_property) { return ret; } -Array Script::_get_script_property_list() { - Array ret; +TypedArray<Dictionary> Script::_get_script_property_list() { + TypedArray<Dictionary> ret; List<PropertyInfo> list; get_script_property_list(&list); for (const PropertyInfo &E : list) { @@ -71,8 +72,8 @@ Array Script::_get_script_property_list() { return ret; } -Array Script::_get_script_method_list() { - Array ret; +TypedArray<Dictionary> Script::_get_script_method_list() { + TypedArray<Dictionary> ret; List<MethodInfo> list; get_script_method_list(&list); for (const MethodInfo &E : list) { @@ -81,8 +82,8 @@ Array Script::_get_script_method_list() { return ret; } -Array Script::_get_script_signal_list() { - Array ret; +TypedArray<Dictionary> Script::_get_script_signal_list() { + TypedArray<Dictionary> ret; List<MethodInfo> list; get_script_signal_list(&list); for (const MethodInfo &E : list) { @@ -105,23 +106,23 @@ Dictionary Script::_get_script_constant_map() { PropertyInfo Script::get_class_category() const { String path = get_path(); - String name; + String scr_name; if (is_built_in()) { if (get_name().is_empty()) { - name = TTR("Built-in script"); + scr_name = TTR("Built-in script"); } else { - name = vformat("%s (%s)", get_name(), TTR("Built-in")); + scr_name = vformat("%s (%s)", get_name(), TTR("Built-in")); } } else { if (get_name().is_empty()) { - name = path.get_file(); + scr_name = path.get_file(); } else { - name = get_name(); + scr_name = get_name(); } } - return PropertyInfo(Variant::NIL, name, PROPERTY_HINT_NONE, path, PROPERTY_USAGE_CATEGORY); + return PropertyInfo(Variant::NIL, scr_name, PROPERTY_HINT_NONE, path, PROPERTY_USAGE_CATEGORY); } #endif // TOOLS_ENABLED @@ -165,6 +166,7 @@ ScriptLanguage *ScriptServer::get_language(int p_idx) { } void ScriptServer::register_language(ScriptLanguage *p_language) { + ERR_FAIL_NULL(p_language); ERR_FAIL_COND(_language_count >= MAX_LANGUAGES); _languages[_language_count++] = p_language; } @@ -182,10 +184,10 @@ void ScriptServer::unregister_language(const ScriptLanguage *p_language) { } void ScriptServer::init_languages() { - { //load global classes + { // Load global classes. global_classes_clear(); if (ProjectSettings::get_singleton()->has_setting("_global_script_classes")) { - Array script_classes = ProjectSettings::get_singleton()->get("_global_script_classes"); + Array script_classes = GLOBAL_GET("_global_script_classes"); for (int i = 0; i < script_classes.size(); i++) { Dictionary c = script_classes[i]; @@ -303,7 +305,7 @@ void ScriptServer::save_global_classes() { Array old; if (ProjectSettings::get_singleton()->has_setting("_global_script_classes")) { - old = ProjectSettings::get_singleton()->get("_global_script_classes"); + old = GLOBAL_GET("_global_script_classes"); } if ((!old.is_empty() || gcarr.is_empty()) && gcarr.hash() == old.hash()) { return; @@ -407,7 +409,9 @@ bool PlaceHolderScriptInstance::set(const StringName &p_name, const Variant &p_v if (values.has(p_name)) { Variant defval; if (script->get_property_default_value(p_name, defval)) { - if (defval == p_value) { + // The evaluate function ensures that a NIL variant is equal to e.g. an empty Resource. + // Simply doing defval == p_value does not do this. + if (Variant::evaluate(Variant::OP_EQUAL, defval, p_value)) { values.erase(p_name); return true; } @@ -417,7 +421,7 @@ bool PlaceHolderScriptInstance::set(const StringName &p_name, const Variant &p_v } else { Variant defval; if (script->get_property_default_value(p_name, defval)) { - if (defval != p_value) { + if (Variant::evaluate(Variant::OP_NOT_EQUAL, defval, p_value)) { values[p_name] = p_value; } return true; diff --git a/core/object/script_language.h b/core/object/script_language.h index f5f052b600..12a21150bc 100644 --- a/core/object/script_language.h +++ b/core/object/script_language.h @@ -37,6 +37,8 @@ #include "core/templates/rb_map.h" class ScriptLanguage; +template <typename T> +class TypedArray; typedef void (*ScriptEditRequestFunction)(const String &p_path); @@ -108,9 +110,9 @@ protected: virtual void _placeholder_erased(PlaceHolderScriptInstance *p_placeholder) {} Variant _get_property_default_value(const StringName &p_property); - Array _get_script_property_list(); - Array _get_script_method_list(); - Array _get_script_signal_list(); + TypedArray<Dictionary> _get_script_property_list(); + TypedArray<Dictionary> _get_script_method_list(); + TypedArray<Dictionary> _get_script_signal_list(); Dictionary _get_script_constant_map(); public: @@ -171,6 +173,9 @@ public: virtual void get_property_list(List<PropertyInfo> *p_properties) const = 0; virtual Variant::Type get_property_type(const StringName &p_name, bool *r_is_valid = nullptr) const = 0; + virtual bool property_can_revert(const StringName &p_name) const = 0; + virtual bool property_get_revert(const StringName &p_name, Variant &r_ret) const = 0; + virtual Object *get_owner() { return nullptr; } virtual void get_property_state(List<Pair<StringName, Variant>> &state); @@ -447,6 +452,9 @@ public: virtual void get_property_list(List<PropertyInfo> *p_properties) const override; virtual Variant::Type get_property_type(const StringName &p_name, bool *r_is_valid = nullptr) const override; + virtual bool property_can_revert(const StringName &p_name) const override { return false; }; + virtual bool property_get_revert(const StringName &p_name, Variant &r_ret) const override { return false; }; + virtual void get_method_list(List<MethodInfo> *p_list) const override; virtual bool has_method(const StringName &p_method) const override; diff --git a/core/object/script_language_extension.cpp b/core/object/script_language_extension.cpp index 9de784ea7f..78e9038b66 100644 --- a/core/object/script_language_extension.cpp +++ b/core/object/script_language_extension.cpp @@ -62,6 +62,7 @@ void ScriptExtension::_bind_methods() { GDVIRTUAL_BIND(_has_script_signal, "signal"); GDVIRTUAL_BIND(_get_script_signal_list); + GDVIRTUAL_BIND(_has_property_default_value, "property"); GDVIRTUAL_BIND(_get_property_default_value, "property"); GDVIRTUAL_BIND(_update_exports); diff --git a/core/object/script_language_extension.h b/core/object/script_language_extension.h index 2869f4ad98..9a2a176096 100644 --- a/core/object/script_language_extension.h +++ b/core/object/script_language_extension.h @@ -82,9 +82,10 @@ public: GDVIRTUAL_REQUIRED_CALL(_get_documentation, doc); Vector<DocData::ClassDoc> class_doc; -#ifndef _MSC_VER -#warning missing conversion from documentation to ClassDoc -#endif + for (int i = 0; i < doc.size(); i++) { + class_doc.append(DocData::ClassDoc::from_dict(doc[i])); + } + return class_doc; } #endif // TOOLS_ENABLED @@ -686,12 +687,24 @@ public: if (r_is_valid) { *r_is_valid = is_valid != 0; } - return Variant::Type(type); } return Variant::NIL; } + virtual bool property_can_revert(const StringName &p_name) const override { + if (native_info->property_can_revert_func) { + return native_info->property_can_revert_func(instance, (const GDNativeStringNamePtr)&p_name); + } + return false; + } + virtual bool property_get_revert(const StringName &p_name, Variant &r_ret) const override { + if (native_info->property_get_revert_func) { + return native_info->property_get_revert_func(instance, (const GDNativeStringNamePtr)&p_name, (GDNativeVariantPtr)&r_ret); + } + return false; + } + virtual Object *get_owner() override { if (native_info->get_owner_func) { return (Object *)native_info->get_owner_func(instance); @@ -713,19 +726,7 @@ public: uint32_t mcount; const GDNativeMethodInfo *minfo = native_info->get_method_list_func(instance, &mcount); for (uint32_t i = 0; i < mcount; i++) { - MethodInfo m; - m.name = minfo[i].name; - m.flags = minfo[i].flags; - m.id = minfo[i].id; - m.return_val = PropertyInfo(minfo[i].return_value); - for (uint32_t j = 0; j < minfo[i].argument_count; j++) { - m.arguments.push_back(PropertyInfo(minfo[i].arguments[j])); - } - const Variant *def_values = (const Variant *)minfo[i].default_arguments; - for (uint32_t j = 0; j < minfo[i].default_argument_count; j++) { - m.default_arguments.push_back(def_values[j]); - } - p_list->push_back(m); + p_list->push_back(MethodInfo(minfo[i])); } if (native_info->free_method_list_func) { native_info->free_method_list_func(instance, minfo); @@ -759,7 +760,8 @@ public: virtual String to_string(bool *r_valid) override { if (native_info->to_string_func) { GDNativeBool valid; - String ret = native_info->to_string_func(instance, &valid); + String ret; + native_info->to_string_func(instance, &valid, reinterpret_cast<GDNativeStringPtr>(&ret)); if (r_valid) { *r_valid = valid != 0; } diff --git a/core/object/undo_redo.cpp b/core/object/undo_redo.cpp index d3c48853f1..9f8a1de697 100644 --- a/core/object/undo_redo.cpp +++ b/core/object/undo_redo.cpp @@ -126,27 +126,28 @@ void UndoRedo::create_action(const String &p_name, MergeMode p_mode) { force_keep_in_merge_ends = false; } -void UndoRedo::add_do_methodp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount) { - ERR_FAIL_COND(p_object == nullptr); +void UndoRedo::add_do_method(const Callable &p_callable) { + ERR_FAIL_COND(p_callable.is_null()); ERR_FAIL_COND(action_level <= 0); ERR_FAIL_COND((current_action + 1) >= actions.size()); + + Object *object = p_callable.get_object(); + ERR_FAIL_NULL(object); + Operation do_op; - do_op.object = p_object->get_instance_id(); - if (Object::cast_to<RefCounted>(p_object)) { - do_op.ref = Ref<RefCounted>(Object::cast_to<RefCounted>(p_object)); + do_op.callable = p_callable; + do_op.object = p_callable.get_object_id(); + if (Object::cast_to<RefCounted>(object)) { + do_op.ref = Ref<RefCounted>(Object::cast_to<RefCounted>(object)); } - do_op.type = Operation::TYPE_METHOD; - do_op.name = p_method; + do_op.name = p_callable.get_method(); - for (int i = 0; i < p_argcount; i++) { - do_op.args.push_back(*p_args[i]); - } actions.write[current_action + 1].do_ops.push_back(do_op); } -void UndoRedo::add_undo_methodp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount) { - ERR_FAIL_COND(p_object == nullptr); +void UndoRedo::add_undo_method(const Callable &p_callable) { + ERR_FAIL_COND(p_callable.is_null()); ERR_FAIL_COND(action_level <= 0); ERR_FAIL_COND((current_action + 1) >= actions.size()); @@ -155,19 +156,19 @@ void UndoRedo::add_undo_methodp(Object *p_object, const StringName &p_method, co return; } + Object *object = p_callable.get_object(); + ERR_FAIL_NULL(object); + Operation undo_op; - undo_op.object = p_object->get_instance_id(); - if (Object::cast_to<RefCounted>(p_object)) { - undo_op.ref = Ref<RefCounted>(Object::cast_to<RefCounted>(p_object)); + undo_op.callable = p_callable; + undo_op.object = p_callable.get_object_id(); + if (Object::cast_to<RefCounted>(object)) { + undo_op.ref = Ref<RefCounted>(Object::cast_to<RefCounted>(object)); } - undo_op.type = Operation::TYPE_METHOD; undo_op.force_keep_in_merge_ends = force_keep_in_merge_ends; - undo_op.name = p_method; + undo_op.name = p_callable.get_method(); - for (int i = 0; i < p_argcount; i++) { - undo_op.args.push_back(*p_args[i]); - } actions.write[current_action + 1].undo_ops.push_back(undo_op); } @@ -183,7 +184,7 @@ void UndoRedo::add_do_property(Object *p_object, const StringName &p_property, c do_op.type = Operation::TYPE_PROPERTY; do_op.name = p_property; - do_op.args.push_back(p_value); + do_op.value = p_value; actions.write[current_action + 1].do_ops.push_back(do_op); } @@ -206,7 +207,7 @@ void UndoRedo::add_undo_property(Object *p_object, const StringName &p_property, undo_op.type = Operation::TYPE_PROPERTY; undo_op.force_keep_in_merge_ends = force_keep_in_merge_ends; undo_op.name = p_property; - undo_op.args.push_back(p_value); + undo_op.value = p_value; actions.write[current_action + 1].undo_ops.push_back(undo_op); } @@ -312,33 +313,42 @@ void UndoRedo::_process_operation_list(List<Operation>::Element *E) { switch (op.type) { case Operation::TYPE_METHOD: { - int argc = op.args.size(); - Vector<const Variant *> argptrs; - argptrs.resize(argc); - - for (int i = 0; i < argc; i++) { - argptrs.write[i] = &op.args[i]; - } - Callable::CallError ce; - obj->callp(op.name, (const Variant **)argptrs.ptr(), argc, ce); + Variant ret; + op.callable.callp(nullptr, 0, ret, ce); if (ce.error != Callable::CallError::CALL_OK) { - ERR_PRINT("Error calling UndoRedo method operation '" + String(op.name) + "': " + Variant::get_call_error_text(obj, op.name, (const Variant **)argptrs.ptr(), argc, ce)); + ERR_PRINT("Error calling UndoRedo method operation '" + String(op.name) + "': " + Variant::get_call_error_text(obj, op.name, nullptr, 0, ce)); } #ifdef TOOLS_ENABLED Resource *res = Object::cast_to<Resource>(obj); if (res) { res->set_edited(true); } - #endif if (method_callback) { - method_callback(method_callback_ud, obj, op.name, (const Variant **)argptrs.ptr(), argc); + Vector<Variant> binds; + if (op.callable.is_custom()) { + CallableCustomBind *ccb = dynamic_cast<CallableCustomBind *>(op.callable.get_custom()); + if (ccb) { + binds = ccb->get_binds(); + } + } + + if (binds.is_empty()) { + method_callback(method_callback_ud, obj, op.name, nullptr, 0); + } else { + const Variant **args = (const Variant **)alloca(sizeof(const Variant **) * binds.size()); + for (int i = 0; i < binds.size(); i++) { + args[i] = (const Variant *)&binds[i]; + } + + method_callback(method_callback_ud, obj, op.name, args, binds.size()); + } } } break; case Operation::TYPE_PROPERTY: { - obj->set(op.name, op.args[0]); + obj->set(op.name, op.value); #ifdef TOOLS_ENABLED Resource *res = Object::cast_to<Resource>(obj); if (res) { @@ -346,7 +356,7 @@ void UndoRedo::_process_operation_list(List<Operation>::Element *E) { } #endif if (property_callback) { - property_callback(prop_callback_ud, obj, op.name, op.args[0]); + property_callback(prop_callback_ud, obj, op.name, op.value); } } break; case Operation::TYPE_REFERENCE: { @@ -413,6 +423,10 @@ String UndoRedo::get_current_action_name() const { return actions[current_action].name; } +int UndoRedo::get_action_level() const { + return action_level; +} + bool UndoRedo::has_undo() const { return current_action >= 0; } @@ -444,87 +458,13 @@ UndoRedo::~UndoRedo() { clear_history(); } -void UndoRedo::_add_do_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error) { - if (p_argcount < 2) { - r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; - r_error.argument = 0; - return; - } - - if (p_args[0]->get_type() != Variant::OBJECT) { - r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; - r_error.argument = 0; - r_error.expected = Variant::OBJECT; - return; - } - - if (p_args[1]->get_type() != Variant::STRING_NAME && p_args[1]->get_type() != Variant::STRING) { - r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; - r_error.argument = 1; - r_error.expected = Variant::STRING_NAME; - return; - } - - r_error.error = Callable::CallError::CALL_OK; - - Object *object = *p_args[0]; - StringName method = *p_args[1]; - - add_do_methodp(object, method, p_args + 2, p_argcount - 2); -} - -void UndoRedo::_add_undo_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error) { - if (p_argcount < 2) { - r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; - r_error.argument = 0; - return; - } - - if (p_args[0]->get_type() != Variant::OBJECT) { - r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; - r_error.argument = 0; - r_error.expected = Variant::OBJECT; - return; - } - - if (p_args[1]->get_type() != Variant::STRING_NAME && p_args[1]->get_type() != Variant::STRING) { - r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; - r_error.argument = 1; - r_error.expected = Variant::STRING_NAME; - return; - } - - r_error.error = Callable::CallError::CALL_OK; - - Object *object = *p_args[0]; - StringName method = *p_args[1]; - - add_undo_methodp(object, method, p_args + 2, p_argcount - 2); -} - void UndoRedo::_bind_methods() { ClassDB::bind_method(D_METHOD("create_action", "name", "merge_mode"), &UndoRedo::create_action, DEFVAL(MERGE_DISABLE)); ClassDB::bind_method(D_METHOD("commit_action", "execute"), &UndoRedo::commit_action, DEFVAL(true)); ClassDB::bind_method(D_METHOD("is_committing_action"), &UndoRedo::is_committing_action); - { - MethodInfo mi; - mi.name = "add_do_method"; - mi.arguments.push_back(PropertyInfo(Variant::OBJECT, "object")); - mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method")); - - ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "add_do_method", &UndoRedo::_add_do_method, mi, varray(), false); - } - - { - MethodInfo mi; - mi.name = "add_undo_method"; - mi.arguments.push_back(PropertyInfo(Variant::OBJECT, "object")); - mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method")); - - ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "add_undo_method", &UndoRedo::_add_undo_method, mi, varray(), false); - } - + ClassDB::bind_method(D_METHOD("add_do_method", "callable"), &UndoRedo::add_do_method); + ClassDB::bind_method(D_METHOD("add_undo_method", "callable"), &UndoRedo::add_undo_method); ClassDB::bind_method(D_METHOD("add_do_property", "object", "property", "value"), &UndoRedo::add_do_property); ClassDB::bind_method(D_METHOD("add_undo_property", "object", "property", "value"), &UndoRedo::add_undo_property); ClassDB::bind_method(D_METHOD("add_do_reference", "object"), &UndoRedo::add_do_reference); diff --git a/core/object/undo_redo.h b/core/object/undo_redo.h index 63cf3e5cbe..9c6d2d10ed 100644 --- a/core/object/undo_redo.h +++ b/core/object/undo_redo.h @@ -46,8 +46,6 @@ public: }; typedef void (*CommitNotifyCallback)(void *p_ud, const String &p_name); - void _add_do_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error); - void _add_undo_method(const Variant **p_args, int p_argcount, Callable::CallError &r_error); typedef void (*MethodNotifyCallback)(void *p_ud, Object *p_base, const StringName &p_name, const Variant **p_args, int p_argcount); typedef void (*PropertyNotifyCallback)(void *p_ud, Object *p_base, const StringName &p_property, const Variant &p_value); @@ -58,14 +56,14 @@ private: TYPE_METHOD, TYPE_PROPERTY, TYPE_REFERENCE - }; + } type; - Type type; bool force_keep_in_merge_ends; Ref<RefCounted> ref; ObjectID object; StringName name; - Vector<Variant> args; + Callable callable; + Variant value; void delete_reference(); }; @@ -106,30 +104,8 @@ protected: public: void create_action(const String &p_name = "", MergeMode p_mode = MERGE_DISABLE); - void add_do_methodp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount); - void add_undo_methodp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount); - - template <typename... VarArgs> - void add_do_method(Object *p_object, const StringName &p_method, VarArgs... p_args) { - Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported. - const Variant *argptrs[sizeof...(p_args) + 1]; - for (uint32_t i = 0; i < sizeof...(p_args); i++) { - argptrs[i] = &args[i]; - } - - add_do_methodp(p_object, p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args)); - } - template <typename... VarArgs> - void add_undo_method(Object *p_object, const StringName &p_method, VarArgs... p_args) { - Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported. - const Variant *argptrs[sizeof...(p_args) + 1]; - for (uint32_t i = 0; i < sizeof...(p_args); i++) { - argptrs[i] = &args[i]; - } - - add_undo_methodp(p_object, p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args)); - } - + void add_do_method(const Callable &p_callable); + void add_undo_method(const Callable &p_callable); void add_do_property(Object *p_object, const StringName &p_property, const Variant &p_value); void add_undo_property(Object *p_object, const StringName &p_property, const Variant &p_value); void add_do_reference(Object *p_object); @@ -144,6 +120,7 @@ public: bool redo(); bool undo(); String get_current_action_name() const; + int get_action_level() const; int get_history_count(); int get_current_action(); diff --git a/core/object/worker_thread_pool.cpp b/core/object/worker_thread_pool.cpp index c770515b9e..9b3dc6833e 100644 --- a/core/object/worker_thread_pool.cpp +++ b/core/object/worker_thread_pool.cpp @@ -395,7 +395,7 @@ void WorkerThreadPool::wait_for_group_task_completion(GroupID p_group) { uint32_t finished_users = group->finished.increment(); // fetch happens before inc, so increment later. if (finished_users == max_users) { - // All tasks using this group are gone (finished before the group), so clear the gorup too. + // All tasks using this group are gone (finished before the group), so clear the group too. task_mutex.lock(); group_allocator.free(group); task_mutex.unlock(); diff --git a/core/os/keyboard.cpp b/core/os/keyboard.cpp index a592791d06..f3be495c8e 100644 --- a/core/os/keyboard.cpp +++ b/core/os/keyboard.cpp @@ -61,12 +61,16 @@ static const _KeyCodeText _keycodes[] = { {Key::PAGEDOWN ,"PageDown"}, {Key::SHIFT ,"Shift"}, {Key::CTRL ,"Ctrl"}, -#ifdef MACOS_ENABLED +#if defined(MACOS_ENABLED) {Key::META ,"Command"}, + {Key::ALT ,"Option"}, +#elif defined(WINDOWS_ENABLED) + {Key::META ,"Windows"}, + {Key::ALT ,"Alt"}, #else {Key::META ,"Meta"}, -#endif {Key::ALT ,"Alt"}, +#endif {Key::CAPSLOCK ,"CapsLock"}, {Key::NUMLOCK ,"NumLock"}, {Key::SCROLLLOCK ,"ScrollLock"}, @@ -437,6 +441,14 @@ String keycode_get_string(Key p_code) { codestr += find_keycode_name(Key::ALT); codestr += "+"; } + if ((p_code & KeyModifierMask::CMD_OR_CTRL) != Key::NONE) { +#ifdef MACOS_ENABLED + codestr += find_keycode_name(Key::META); +#else + codestr += find_keycode_name(Key::CTRL); +#endif + codestr += "+"; + } if ((p_code & KeyModifierMask::CTRL) != Key::NONE) { codestr += find_keycode_name(Key::CTRL); codestr += "+"; diff --git a/core/os/keyboard.h b/core/os/keyboard.h index 517a53e505..e5d9b24e85 100644 --- a/core/os/keyboard.h +++ b/core/os/keyboard.h @@ -36,11 +36,11 @@ enum class Key { NONE = 0, // Special key: The strategy here is similar to the one used by toolkits, - // which consists in leaving the 24 bits unicode range for printable - // characters, and use the upper 8 bits for special keys and modifiers. + // which consists in leaving the 21 bits unicode range for printable + // characters, and use the upper 11 bits for special keys and modifiers. // This way everything (char/keycode) can fit nicely in one 32-bit // integer (the enum's underlying type is `int` by default). - SPECIAL = (1 << 24), + SPECIAL = (1 << 22), /* CURSOR/FUNCTION/BROWSER/MULTIMEDIA/MISC KEYS */ ESCAPE = SPECIAL | 0x01, TAB = SPECIAL | 0x02, @@ -312,84 +312,85 @@ enum class Key { }; enum class KeyModifierMask { - CODE_MASK = ((1 << 25) - 1), ///< Apply this mask to any keycode to remove modifiers. - MODIFIER_MASK = (0x7F << 24), ///< Apply this mask to isolate modifiers. + CODE_MASK = ((1 << 23) - 1), ///< Apply this mask to any keycode to remove modifiers. + MODIFIER_MASK = (0x7F << 22), ///< Apply this mask to isolate modifiers. + //RESERVED = (1 << 23), + CMD_OR_CTRL = (1 << 24), SHIFT = (1 << 25), ALT = (1 << 26), META = (1 << 27), CTRL = (1 << 28), -#ifdef APPLE_STYLE_KEYS - CMD = META, -#else - CMD = CTRL, -#endif KPAD = (1 << 29), GROUP_SWITCH = (1 << 30) }; // To avoid having unnecessary operators, only define the ones that are needed. -inline Key operator-(uint32_t a, Key b) { +constexpr Key operator-(uint32_t a, Key b) { return (Key)(a - (uint32_t)b); } -inline Key &operator-=(Key &a, int b) { - return (Key &)((int &)a -= b); +constexpr Key &operator-=(Key &a, int b) { + a = static_cast<Key>(static_cast<int>(a) - static_cast<int>(b)); + return a; } -inline Key operator+(Key a, int b) { +constexpr Key operator+(Key a, int b) { return (Key)((int)a + (int)b); } -inline Key operator+(Key a, Key b) { +constexpr Key operator+(Key a, Key b) { return (Key)((int)a + (int)b); } -inline Key operator-(Key a, Key b) { +constexpr Key operator-(Key a, Key b) { return (Key)((int)a - (int)b); } -inline Key operator&(Key a, Key b) { +constexpr Key operator&(Key a, Key b) { return (Key)((int)a & (int)b); } -inline Key operator|(Key a, Key b) { +constexpr Key operator|(Key a, Key b) { return (Key)((int)a | (int)b); } -inline Key &operator|=(Key &a, Key b) { - return (Key &)((int &)a |= (int)b); +constexpr Key &operator|=(Key &a, Key b) { + a = static_cast<Key>(static_cast<int>(a) | static_cast<int>(b)); + return a; } -inline Key &operator|=(Key &a, KeyModifierMask b) { - return (Key &)((int &)a |= (int)b); +constexpr Key &operator|=(Key &a, KeyModifierMask b) { + a = static_cast<Key>(static_cast<int>(a) | static_cast<int>(b)); + return a; } -inline Key &operator&=(Key &a, KeyModifierMask b) { - return (Key &)((int &)a &= (int)b); +constexpr Key &operator&=(Key &a, KeyModifierMask b) { + a = static_cast<Key>(static_cast<int>(a) & static_cast<int>(b)); + return a; } -inline Key operator|(Key a, KeyModifierMask b) { +constexpr Key operator|(Key a, KeyModifierMask b) { return (Key)((int)a | (int)b); } -inline Key operator&(Key a, KeyModifierMask b) { +constexpr Key operator&(Key a, KeyModifierMask b) { return (Key)((int)a & (int)b); } -inline Key operator+(KeyModifierMask a, Key b) { +constexpr Key operator+(KeyModifierMask a, Key b) { return (Key)((int)a + (int)b); } -inline Key operator|(KeyModifierMask a, Key b) { +constexpr Key operator|(KeyModifierMask a, Key b) { return (Key)((int)a | (int)b); } -inline KeyModifierMask operator+(KeyModifierMask a, KeyModifierMask b) { +constexpr KeyModifierMask operator+(KeyModifierMask a, KeyModifierMask b) { return (KeyModifierMask)((int)a + (int)b); } -inline KeyModifierMask operator|(KeyModifierMask a, KeyModifierMask b) { +constexpr KeyModifierMask operator|(KeyModifierMask a, KeyModifierMask b) { return (KeyModifierMask)((int)a | (int)b); } diff --git a/core/os/main_loop.cpp b/core/os/main_loop.cpp index a96e1989f9..c0504a174c 100644 --- a/core/os/main_loop.cpp +++ b/core/os/main_loop.cpp @@ -65,21 +65,15 @@ void MainLoop::initialize() { } bool MainLoop::physics_process(double p_time) { - bool quit; - if (GDVIRTUAL_CALL(_physics_process, p_time, quit)) { - return quit; - } - - return false; + bool quit = false; + GDVIRTUAL_CALL(_physics_process, p_time, quit); + return quit; } bool MainLoop::process(double p_time) { - bool quit; - if (GDVIRTUAL_CALL(_process, p_time, quit)) { - return quit; - } - - return false; + bool quit = false; + GDVIRTUAL_CALL(_process, p_time, quit); + return quit; } void MainLoop::finalize() { diff --git a/core/os/midi_driver.cpp b/core/os/midi_driver.cpp index 410b62068a..79eef95ef2 100644 --- a/core/os/midi_driver.cpp +++ b/core/os/midi_driver.cpp @@ -86,11 +86,6 @@ void MIDIDriver::receive_input_packet(uint64_t timestamp, uint8_t *data, uint32_ if (length >= 2 + param_position) { event->set_pitch(data[param_position]); event->set_velocity(data[param_position + 1]); - - if (event->get_message() == MIDIMessage::NOTE_ON && event->get_velocity() == 0) { - // https://www.midi.org/forum/228-writing-midi-software-send-note-off,-or-zero-velocity-note-on - event->set_message(MIDIMessage::NOTE_OFF); - } } break; diff --git a/core/os/mutex.cpp b/core/os/mutex.cpp index 1d4400bfc1..512db1737a 100644 --- a/core/os/mutex.cpp +++ b/core/os/mutex.cpp @@ -40,11 +40,7 @@ void _global_unlock() { _global_mutex.unlock(); } -#ifndef NO_THREADS - template class MutexImpl<std::recursive_mutex>; template class MutexImpl<std::mutex>; template class MutexLock<MutexImpl<std::recursive_mutex>>; template class MutexLock<MutexImpl<std::mutex>>; - -#endif diff --git a/core/os/mutex.h b/core/os/mutex.h index a51248807b..eb58418bd6 100644 --- a/core/os/mutex.h +++ b/core/os/mutex.h @@ -34,8 +34,6 @@ #include "core/error/error_list.h" #include "core/typedefs.h" -#if !defined(NO_THREADS) - #include <mutex> template <class StdMutexT> @@ -79,29 +77,4 @@ extern template class MutexImpl<std::mutex>; extern template class MutexLock<MutexImpl<std::recursive_mutex>>; extern template class MutexLock<MutexImpl<std::mutex>>; -#else - -class FakeMutex { - FakeMutex() {} -}; - -template <class MutexT> -class MutexImpl { -public: - _ALWAYS_INLINE_ void lock() const {} - _ALWAYS_INLINE_ void unlock() const {} - _ALWAYS_INLINE_ Error try_lock() const { return OK; } -}; - -template <class MutexT> -class MutexLock { -public: - explicit MutexLock(const MutexT &p_mutex) {} -}; - -using Mutex = MutexImpl<FakeMutex>; -using BinaryMutex = MutexImpl<FakeMutex>; // Non-recursive, handle with care - -#endif // !NO_THREADS - #endif // MUTEX_H diff --git a/core/os/os.cpp b/core/os/os.cpp index 1358c926d1..bbb2a94fe7 100644 --- a/core/os/os.cpp +++ b/core/os/os.cpp @@ -38,6 +38,7 @@ #include "core/version_generated.gen.h" #include <stdarg.h> +#include <thread> OS *OS::singleton = nullptr; uint64_t OS::target_ticks = 0; @@ -54,10 +55,6 @@ double OS::get_unix_time() const { return 0; } -void OS::debug_break() { - // something -} - void OS::_set_logger(CompositeLogger *p_logger) { if (_logger) { memdelete(_logger); @@ -159,17 +156,13 @@ int OS::get_process_id() const { } void OS::vibrate_handheld(int p_duration_ms) { - WARN_PRINT("vibrate_handheld() only works with Android, iOS and HTML5"); + WARN_PRINT("vibrate_handheld() only works with Android, iOS and Web"); } bool OS::is_stdout_verbose() const { return _verbose_stdout; } -bool OS::is_single_window() const { - return _single_window; -} - bool OS::is_stdout_debug_enabled() const { return _debug_stdout; } @@ -190,50 +183,6 @@ void OS::set_stderr_enabled(bool p_enabled) { _stderr_enabled = p_enabled; } -void OS::dump_memory_to_file(const char *p_file) { - //Memory::dump_static_mem_to_file(p_file); -} - -static Ref<FileAccess> _OSPRF; - -static void _OS_printres(Object *p_obj) { - Resource *res = Object::cast_to<Resource>(p_obj); - if (!res) { - return; - } - - String str = vformat("%s - %s - %s", res->to_string(), res->get_name(), res->get_path()); - if (_OSPRF.is_valid()) { - _OSPRF->store_line(str); - } else { - print_line(str); - } -} - -void OS::print_all_resources(String p_to_file) { - ERR_FAIL_COND(!p_to_file.is_empty() && _OSPRF.is_valid()); - if (!p_to_file.is_empty()) { - Error err; - _OSPRF = FileAccess::open(p_to_file, FileAccess::WRITE, &err); - if (err != OK) { - _OSPRF.unref(); - ERR_FAIL_MSG("Can't print all resources to file: " + String(p_to_file) + "."); - } - } - - ObjectDB::debug_objects(_OS_printres); - - _OSPRF.unref(); -} - -void OS::print_resources_in_use(bool p_short) { - ResourceCache::dump(nullptr, p_short); -} - -void OS::dump_resources_to_file(const char *p_file) { - ResourceCache::dump(p_file); -} - int OS::get_exit_code() const { return _exit_code; } @@ -373,21 +322,13 @@ String OS::get_unique_id() const { } int OS::get_processor_count() const { - return 1; + return std::thread::hardware_concurrency(); } String OS::get_processor_name() const { return ""; } -bool OS::can_use_threads() const { -#ifdef NO_THREADS - return false; -#else - return true; -#endif -} - void OS::set_has_server_feature_callback(HasServerFeatureCallback p_callback) { has_server_feature_callback = p_callback; } @@ -571,10 +512,10 @@ void OS::add_frame_delay(bool p_can_draw) { if (is_in_low_processor_usage_mode() || !p_can_draw) { dynamic_delay = get_low_processor_usage_mode_sleep_usec(); } - const int target_fps = Engine::get_singleton()->get_target_fps(); - if (target_fps > 0 && !Engine::get_singleton()->is_editor_hint()) { + const int max_fps = Engine::get_singleton()->get_max_fps(); + if (max_fps > 0 && !Engine::get_singleton()->is_editor_hint()) { // Override the low processor usage mode sleep delay if the target FPS is lower. - dynamic_delay = MAX(dynamic_delay, (uint64_t)(1000000 / target_fps)); + dynamic_delay = MAX(dynamic_delay, (uint64_t)(1000000 / max_fps)); } if (dynamic_delay > 0) { diff --git a/core/os/os.h b/core/os/os.h index 9152b797ef..af7b40f3ec 100644 --- a/core/os/os.h +++ b/core/os/os.h @@ -52,7 +52,6 @@ class OS { int low_processor_usage_mode_sleep_usec = 10000; bool _verbose_stdout = false; bool _debug_stdout = false; - bool _single_window = false; String _local_clipboard; int _exit_code = EXIT_FAILURE; // unexpected exit is marked as failure bool _allow_hidpi = false; @@ -70,6 +69,7 @@ class OS { // so we can retrieve the rendering drivers available int _display_driver_id = -1; String _current_rendering_driver_name; + String _current_rendering_method; protected: void _set_logger(CompositeLogger *p_logger); @@ -99,6 +99,8 @@ protected: virtual void initialize_joypads() = 0; void set_current_rendering_driver_name(String p_driver_name) { _current_rendering_driver_name = p_driver_name; } + void set_current_rendering_method(String p_name) { _current_rendering_method = p_name; } + void set_display_driver_id(int p_display_driver_id) { _display_driver_id = p_display_driver_id; } virtual void set_main_loop(MainLoop *p_main_loop) = 0; @@ -117,8 +119,12 @@ public: static OS *get_singleton(); String get_current_rendering_driver_name() const { return _current_rendering_driver_name; } + String get_current_rendering_method() const { return _current_rendering_method; } + int get_display_driver_id() const { return _display_driver_id; } + virtual Vector<String> get_video_adapter_driver_info() const = 0; + void print_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, bool p_editor_notify = false, Logger::ErrorType p_type = Logger::ERR_ERROR); void print(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3; void print_rich(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3; @@ -162,6 +168,8 @@ public: virtual bool set_environment(const String &p_var, const String &p_value) const = 0; virtual String get_name() const = 0; + virtual String get_distribution_name() const = 0; + virtual String get_version() const = 0; virtual List<String> get_cmdline_args() const { return _cmdline; } virtual List<String> get_cmdline_user_args() const { return _user_args; } virtual List<String> get_cmdline_platform_args() const { return List<String>(); } @@ -203,18 +211,15 @@ public: MONTH_DECEMBER, }; - struct Date { + struct DateTime { int64_t year; Month month; uint8_t day; Weekday weekday; - bool dst; - }; - - struct Time { uint8_t hour; uint8_t minute; uint8_t second; + bool dst; }; struct TimeZoneInfo { @@ -222,8 +227,7 @@ public: String name; }; - virtual Date get_date(bool p_utc = false) const = 0; - virtual Time get_time(bool p_utc = false) const = 0; + virtual DateTime get_datetime(bool utc = false) const = 0; virtual TimeZoneInfo get_time_zone_info() const = 0; virtual double get_unix_time() const; @@ -243,17 +247,10 @@ public: void set_stdout_enabled(bool p_enabled); void set_stderr_enabled(bool p_enabled); - virtual bool is_single_window() const; - virtual void disable_crash_handler() {} virtual bool is_disable_crash_handler() const { return false; } virtual void initialize_debugging() {} - virtual void dump_memory_to_file(const char *p_file); - virtual void dump_resources_to_file(const char *p_file); - virtual void print_resources_in_use(bool p_short = false); - virtual void print_all_resources(String p_to_file = ""); - virtual uint64_t get_static_memory_usage() const; virtual uint64_t get_static_memory_peak_usage() const; virtual uint64_t get_free_static_memory() const; @@ -292,8 +289,6 @@ public: virtual Error move_to_trash(const String &p_path) { return FAILED; } - virtual void debug_break(); - virtual int get_exit_code() const; // `set_exit_code` should only be used from `SceneTree` (or from a similar // level, e.g. from the `Main::start` if leaving without creating a `SceneTree`). @@ -306,8 +301,6 @@ public: virtual String get_unique_id() const; - virtual bool can_use_threads() const; - bool has_feature(const String &p_feature); void set_has_server_feature_callback(HasServerFeatureCallback p_callback); diff --git a/core/os/pool_allocator.cpp b/core/os/pool_allocator.cpp index f622e2c7c5..e7f2cff7c5 100644 --- a/core/os/pool_allocator.cpp +++ b/core/os/pool_allocator.cpp @@ -35,8 +35,6 @@ #include "core/os/os.h" #include "core/string/print_string.h" -#include <assert.h> - #define COMPACT_CHUNK(m_entry, m_to_pos) \ do { \ void *_dst = &((unsigned char *)pool)[m_to_pos]; \ @@ -169,11 +167,6 @@ bool PoolAllocator::find_entry_index(EntryIndicesPos *p_map_pos, const Entry *p_ PoolAllocator::ID PoolAllocator::alloc(int p_size) { ERR_FAIL_COND_V(p_size < 1, POOL_ALLOCATOR_INVALID_ID); -#ifdef DEBUG_ENABLED - if (p_size > free_mem) { - OS::get_singleton()->debug_break(); - } -#endif ERR_FAIL_COND_V(p_size > free_mem, POOL_ALLOCATOR_INVALID_ID); mt_lock(); @@ -482,7 +475,6 @@ void *PoolAllocator::get(ID p_mem) { ERR_FAIL_COND_V(!e, nullptr); } if (e->lock == 0) { - //assert(0); mt_unlock(); ERR_PRINT("e->lock == 0"); return nullptr; diff --git a/core/os/rw_lock.h b/core/os/rw_lock.h index a046f474ea..d3206547c7 100644 --- a/core/os/rw_lock.h +++ b/core/os/rw_lock.h @@ -33,8 +33,6 @@ #include "core/error/error_list.h" -#if !defined(NO_THREADS) - #include <shared_mutex> class RWLock { @@ -72,21 +70,6 @@ public: } }; -#else - -class RWLock { -public: - void read_lock() const {} - void read_unlock() const {} - Error read_try_lock() const { return OK; } - - void write_lock() {} - void write_unlock() {} - Error write_try_lock() { return OK; } -}; - -#endif - class RWLockRead { const RWLock &lock; diff --git a/core/os/semaphore.h b/core/os/semaphore.h index 72df52dd34..1a93d3ee2c 100644 --- a/core/os/semaphore.h +++ b/core/os/semaphore.h @@ -34,8 +34,6 @@ #include "core/error/error_list.h" #include "core/typedefs.h" -#if !defined(NO_THREADS) - #include <condition_variable> #include <mutex> @@ -70,15 +68,4 @@ public: } }; -#else - -class Semaphore { -public: - _ALWAYS_INLINE_ void post() const {} - _ALWAYS_INLINE_ void wait() const {} - _ALWAYS_INLINE_ bool try_wait() const { return true; } -}; - -#endif - #endif // SEMAPHORE_H diff --git a/core/os/thread.cpp b/core/os/thread.cpp index c8072b7280..712f4793eb 100644 --- a/core/os/thread.cpp +++ b/core/os/thread.cpp @@ -34,15 +34,9 @@ #include "thread.h" #include "core/object/script_language.h" - -#if !defined(NO_THREADS) - #include "core/templates/safe_refcount.h" -Error (*Thread::set_name_func)(const String &) = nullptr; -void (*Thread::set_priority_func)(Thread::Priority) = nullptr; -void (*Thread::init_func)() = nullptr; -void (*Thread::term_func)() = nullptr; +Thread::PlatformFunctions Thread::platform_functions; uint64_t Thread::_thread_id_hash(const std::thread::id &p_t) { static std::hash<std::thread::id> hasher; @@ -52,30 +46,27 @@ uint64_t Thread::_thread_id_hash(const std::thread::id &p_t) { Thread::ID Thread::main_thread_id = _thread_id_hash(std::this_thread::get_id()); thread_local Thread::ID Thread::caller_id = 0; -void Thread::_set_platform_funcs( - Error (*p_set_name_func)(const String &), - void (*p_set_priority_func)(Thread::Priority), - void (*p_init_func)(), - void (*p_term_func)()) { - Thread::set_name_func = p_set_name_func; - Thread::set_priority_func = p_set_priority_func; - Thread::init_func = p_init_func; - Thread::term_func = p_term_func; +void Thread::_set_platform_functions(const PlatformFunctions &p_functions) { + platform_functions = p_functions; } void Thread::callback(Thread *p_self, const Settings &p_settings, Callback p_callback, void *p_userdata) { Thread::caller_id = _thread_id_hash(p_self->thread.get_id()); - if (set_priority_func) { - set_priority_func(p_settings.priority); + if (platform_functions.set_priority) { + platform_functions.set_priority(p_settings.priority); } - if (init_func) { - init_func(); + if (platform_functions.init) { + platform_functions.init(); + } + ScriptServer::thread_enter(); // Scripts may need to attach a stack. + if (platform_functions.wrapper) { + platform_functions.wrapper(p_callback, p_userdata); + } else { + p_callback(p_userdata); } - ScriptServer::thread_enter(); //scripts may need to attach a stack - p_callback(p_userdata); ScriptServer::thread_exit(); - if (term_func) { - term_func(); + if (platform_functions.term) { + platform_functions.term(); } } @@ -108,8 +99,8 @@ void Thread::wait_to_finish() { } Error Thread::set_name(const String &p_name) { - if (set_name_func) { - return set_name_func(p_name); + if (platform_functions.set_name) { + return platform_functions.set_name(p_name); } return ERR_UNAVAILABLE; @@ -128,5 +119,4 @@ Thread::~Thread() { } } -#endif #endif // PLATFORM_THREAD_OVERRIDE diff --git a/core/os/thread.h b/core/os/thread.h index 0fb283e224..86442de760 100644 --- a/core/os/thread.h +++ b/core/os/thread.h @@ -35,15 +35,14 @@ #ifdef PLATFORM_THREAD_OVERRIDE #include "platform_thread.h" #else + #ifndef THREAD_H #define THREAD_H +#include "core/templates/safe_refcount.h" #include "core/typedefs.h" -#if !defined(NO_THREADS) -#include "core/templates/safe_refcount.h" #include <thread> -#endif class String; @@ -64,8 +63,15 @@ public: 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: -#if !defined(NO_THREADS) friend class Main; static ID main_thread_id; @@ -78,20 +84,11 @@ private: static void callback(Thread *p_self, const Settings &p_settings, Thread::Callback p_callback, void *p_userdata); - static Error (*set_name_func)(const String &); - static void (*set_priority_func)(Thread::Priority); - static void (*init_func)(); - static void (*term_func)(); -#endif + static PlatformFunctions platform_functions; public: - static void _set_platform_funcs( - Error (*p_set_name_func)(const String &), - void (*p_set_priority_func)(Thread::Priority), - void (*p_init_func)() = nullptr, - void (*p_term_func)() = nullptr); + static void _set_platform_functions(const PlatformFunctions &p_functions); -#if !defined(NO_THREADS) _FORCE_INLINE_ ID get_id() const { return id; } // get the ID of the caller thread _FORCE_INLINE_ static ID get_caller_id() { return caller_id; } @@ -107,19 +104,6 @@ public: Thread(); ~Thread(); -#else - _FORCE_INLINE_ ID get_id() const { return 0; } - // get the ID of the caller thread - _FORCE_INLINE_ static ID get_caller_id() { return 0; } - // get the ID of the main thread - _FORCE_INLINE_ static ID get_main_id() { return 0; } - - 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 }; #endif // THREAD_H diff --git a/core/os/threaded_array_processor.h b/core/os/threaded_array_processor.h index 935fc7a360..95a2253f14 100644 --- a/core/os/threaded_array_processor.h +++ b/core/os/threaded_array_processor.h @@ -49,8 +49,6 @@ struct ThreadArrayProcessData { } }; -#ifndef NO_THREADS - template <class T> void process_array_thread(void *ud) { T &data = *(T *)ud; @@ -86,21 +84,4 @@ void thread_process_array(uint32_t p_elements, C *p_instance, M p_method, U p_us memdelete_arr(threads); } -#else - -template <class C, class M, class U> -void thread_process_array(uint32_t p_elements, C *p_instance, M p_method, U p_userdata) { - ThreadArrayProcessData<C, U> data; - data.method = p_method; - data.instance = p_instance; - data.userdata = p_userdata; - data.index.set(0); - data.elements = p_elements; - for (uint32_t i = 0; i < p_elements; i++) { - data.process(i); - } -} - -#endif - #endif // THREADED_ARRAY_PROCESSOR_H diff --git a/core/os/time.cpp b/core/os/time.cpp index a30e2a906b..a3c2c99b4c 100644 --- a/core/os/time.cpp +++ b/core/os/time.cpp @@ -324,63 +324,60 @@ String Time::get_offset_string_from_offset_minutes(int64_t p_offset_minutes) con } Dictionary Time::get_datetime_dict_from_system(bool p_utc) const { - OS::Date date = OS::get_singleton()->get_date(p_utc); - OS::Time time = OS::get_singleton()->get_time(p_utc); + OS::DateTime dt = OS::get_singleton()->get_datetime(p_utc); Dictionary datetime; - datetime[YEAR_KEY] = date.year; - datetime[MONTH_KEY] = (uint8_t)date.month; - datetime[DAY_KEY] = date.day; - datetime[WEEKDAY_KEY] = (uint8_t)date.weekday; - datetime[DST_KEY] = date.dst; - datetime[HOUR_KEY] = time.hour; - datetime[MINUTE_KEY] = time.minute; - datetime[SECOND_KEY] = time.second; + datetime[YEAR_KEY] = dt.year; + datetime[MONTH_KEY] = (uint8_t)dt.month; + datetime[DAY_KEY] = dt.day; + datetime[WEEKDAY_KEY] = (uint8_t)dt.weekday; + datetime[HOUR_KEY] = dt.hour; + datetime[MINUTE_KEY] = dt.minute; + datetime[SECOND_KEY] = dt.second; + datetime[DST_KEY] = dt.dst; return datetime; } Dictionary Time::get_date_dict_from_system(bool p_utc) const { - OS::Date date = OS::get_singleton()->get_date(p_utc); + OS::DateTime dt = OS::get_singleton()->get_datetime(p_utc); Dictionary date_dictionary; - date_dictionary[YEAR_KEY] = date.year; - date_dictionary[MONTH_KEY] = (uint8_t)date.month; - date_dictionary[DAY_KEY] = date.day; - date_dictionary[WEEKDAY_KEY] = (uint8_t)date.weekday; - date_dictionary[DST_KEY] = date.dst; + date_dictionary[YEAR_KEY] = dt.year; + date_dictionary[MONTH_KEY] = (uint8_t)dt.month; + date_dictionary[DAY_KEY] = dt.day; + date_dictionary[WEEKDAY_KEY] = (uint8_t)dt.weekday; return date_dictionary; } Dictionary Time::get_time_dict_from_system(bool p_utc) const { - OS::Time time = OS::get_singleton()->get_time(p_utc); + OS::DateTime dt = OS::get_singleton()->get_datetime(p_utc); Dictionary time_dictionary; - time_dictionary[HOUR_KEY] = time.hour; - time_dictionary[MINUTE_KEY] = time.minute; - time_dictionary[SECOND_KEY] = time.second; + time_dictionary[HOUR_KEY] = dt.hour; + time_dictionary[MINUTE_KEY] = dt.minute; + time_dictionary[SECOND_KEY] = dt.second; return time_dictionary; } String Time::get_datetime_string_from_system(bool p_utc, bool p_use_space) const { - OS::Date date = OS::get_singleton()->get_date(p_utc); - OS::Time time = OS::get_singleton()->get_time(p_utc); + OS::DateTime dt = OS::get_singleton()->get_datetime(p_utc); // vformat only supports up to 6 arguments, so we need to split this up into 2 parts. - String timestamp = vformat("%04d-%02d-%02d", date.year, (uint8_t)date.month, date.day); + String timestamp = vformat("%04d-%02d-%02d", dt.year, (uint8_t)dt.month, dt.day); if (p_use_space) { - timestamp = vformat("%s %02d:%02d:%02d", timestamp, time.hour, time.minute, time.second); + timestamp = vformat("%s %02d:%02d:%02d", timestamp, dt.hour, dt.minute, dt.second); } else { - timestamp = vformat("%sT%02d:%02d:%02d", timestamp, time.hour, time.minute, time.second); + timestamp = vformat("%sT%02d:%02d:%02d", timestamp, dt.hour, dt.minute, dt.second); } return timestamp; } String Time::get_date_string_from_system(bool p_utc) const { - OS::Date date = OS::get_singleton()->get_date(p_utc); + OS::DateTime dt = OS::get_singleton()->get_datetime(p_utc); // Android is picky about the types passed to make Variant, so we need a cast. - return vformat("%04d-%02d-%02d", date.year, (uint8_t)date.month, date.day); + return vformat("%04d-%02d-%02d", dt.year, (uint8_t)dt.month, dt.day); } String Time::get_time_string_from_system(bool p_utc) const { - OS::Time time = OS::get_singleton()->get_time(p_utc); - return vformat("%02d:%02d:%02d", time.hour, time.minute, time.second); + OS::DateTime dt = OS::get_singleton()->get_datetime(p_utc); + return vformat("%02d:%02d:%02d", dt.hour, dt.minute, dt.second); } Dictionary Time::get_time_zone_from_system() const { diff --git a/core/register_core_types.cpp b/core/register_core_types.cpp index 9ad6b0ca68..2e144a4c9a 100644 --- a/core/register_core_types.cpp +++ b/core/register_core_types.cpp @@ -44,6 +44,7 @@ #include "core/input/input_map.h" #include "core/input/shortcut.h" #include "core/io/config_file.h" +#include "core/io/dir_access.h" #include "core/io/dtls_server.h" #include "core/io/http_client.h" #include "core/io/image_loader.h" @@ -58,12 +59,14 @@ #include "core/io/resource_format_binary.h" #include "core/io/resource_importer.h" #include "core/io/resource_uid.h" -#include "core/io/stream_peer_ssl.h" +#include "core/io/stream_peer_gzip.h" +#include "core/io/stream_peer_tls.h" #include "core/io/tcp_server.h" #include "core/io/translation_loader_po.h" #include "core/io/udp_server.h" #include "core/io/xml_parser.h" #include "core/math/a_star.h" +#include "core/math/a_star_grid_2d.h" #include "core/math/expression.h" #include "core/math/geometry_2d.h" #include "core/math/geometry_3d.h" @@ -86,6 +89,8 @@ static Ref<TranslationLoaderPO> resource_format_po; static Ref<ResourceFormatSaverCrypto> resource_format_saver_crypto; static Ref<ResourceFormatLoaderCrypto> resource_format_loader_crypto; static Ref<NativeExtensionResourceLoader> resource_loader_native_extension; +static Ref<ResourceFormatSaverJSON> resource_saver_json; +static Ref<ResourceFormatLoaderJSON> resource_loader_json; static core_bind::ResourceLoader *_resource_loader = nullptr; static core_bind::ResourceSaver *_resource_saver = nullptr; @@ -181,6 +186,7 @@ void register_core_types() { GDREGISTER_ABSTRACT_CLASS(StreamPeer); GDREGISTER_CLASS(StreamPeerExtension); GDREGISTER_CLASS(StreamPeerBuffer); + GDREGISTER_CLASS(StreamPeerGZIP); GDREGISTER_CLASS(StreamPeerTCP); GDREGISTER_CLASS(TCPServer); @@ -201,7 +207,7 @@ void register_core_types() { ClassDB::register_custom_instance_class<CryptoKey>(); ClassDB::register_custom_instance_class<HMACContext>(); ClassDB::register_custom_instance_class<Crypto>(); - ClassDB::register_custom_instance_class<StreamPeerSSL>(); + ClassDB::register_custom_instance_class<StreamPeerTLS>(); ClassDB::register_custom_instance_class<PacketPeerDTLS>(); ClassDB::register_custom_instance_class<DTLSServer>(); @@ -210,6 +216,12 @@ void register_core_types() { resource_format_loader_crypto.instantiate(); ResourceLoader::add_resource_format_loader(resource_format_loader_crypto); + resource_loader_json.instantiate(); + ResourceLoader::add_resource_format_loader(resource_loader_json); + + resource_saver_json.instantiate(); + ResourceSaver::add_resource_format_saver(resource_saver_json); + GDREGISTER_CLASS(MainLoop); GDREGISTER_CLASS(Translation); GDREGISTER_CLASS(OptimizedTranslation); @@ -219,8 +231,8 @@ void register_core_types() { GDREGISTER_CLASS(ResourceFormatLoader); GDREGISTER_CLASS(ResourceFormatSaver); - GDREGISTER_CLASS(core_bind::File); - GDREGISTER_CLASS(core_bind::Directory); + GDREGISTER_ABSTRACT_CLASS(FileAccess); + GDREGISTER_ABSTRACT_CLASS(DirAccess); GDREGISTER_CLASS(core_bind::Thread); GDREGISTER_CLASS(core_bind::Mutex); GDREGISTER_CLASS(core_bind::Semaphore); @@ -236,9 +248,12 @@ void register_core_types() { GDREGISTER_ABSTRACT_CLASS(PackedDataContainerRef); GDREGISTER_CLASS(AStar3D); GDREGISTER_CLASS(AStar2D); + GDREGISTER_CLASS(AStarGrid2D); GDREGISTER_CLASS(EncodedObjectAsID); GDREGISTER_CLASS(RandomNumberGenerator); + GDREGISTER_ABSTRACT_CLASS(ImageFormatLoader); + GDREGISTER_CLASS(ImageFormatLoaderExtension); GDREGISTER_ABSTRACT_CLASS(ResourceImporter); GDREGISTER_CLASS(NativeExtension); @@ -282,8 +297,8 @@ void register_core_settings() { ProjectSettings::get_singleton()->set_custom_property_info("network/limits/tcp/connect_timeout_seconds", PropertyInfo(Variant::INT, "network/limits/tcp/connect_timeout_seconds", PROPERTY_HINT_RANGE, "1,1800,1")); GLOBAL_DEF_RST("network/limits/packet_peer_stream/max_buffer_po2", (16)); ProjectSettings::get_singleton()->set_custom_property_info("network/limits/packet_peer_stream/max_buffer_po2", PropertyInfo(Variant::INT, "network/limits/packet_peer_stream/max_buffer_po2", PROPERTY_HINT_RANGE, "0,64,1,or_greater")); - GLOBAL_DEF("network/ssl/certificate_bundle_override", ""); - ProjectSettings::get_singleton()->set_custom_property_info("network/ssl/certificate_bundle_override", PropertyInfo(Variant::STRING, "network/ssl/certificate_bundle_override", PROPERTY_HINT_FILE, "*.crt")); + GLOBAL_DEF("network/tls/certificate_bundle_override", ""); + ProjectSettings::get_singleton()->set_custom_property_info("network/tls/certificate_bundle_override", PropertyInfo(Variant::STRING, "network/tls/certificate_bundle_override", PROPERTY_HINT_FILE, "*.crt")); int worker_threads = GLOBAL_DEF("threading/worker_pool/max_threads", -1); bool low_priority_use_system_threads = GLOBAL_DEF("threading/worker_pool/use_system_threads_for_low_priority_tasks", true); @@ -385,6 +400,12 @@ void unregister_core_types() { ResourceLoader::remove_resource_format_loader(resource_format_loader_crypto); resource_format_loader_crypto.unref(); + ResourceSaver::remove_resource_format_saver(resource_saver_json); + resource_saver_json.unref(); + + ResourceLoader::remove_resource_format_loader(resource_loader_json); + resource_loader_json.unref(); + if (ip) { memdelete(ip); } diff --git a/core/string/locales.h b/core/string/locales.h index 32d6608ec2..0ccb17a436 100644 --- a/core/string/locales.h +++ b/core/string/locales.h @@ -1072,6 +1072,7 @@ static const char *script_list[][2] = { { "Jurchen", "Jurc" }, { "Kayah Li", "Kali" }, { "Katakana", "Kana" }, + { "Kawi", "Kawi" }, { "Kharoshthi", "Khar" }, { "Khmer", "Khmr" }, { "Khojki", "Khoj" }, @@ -1110,6 +1111,7 @@ static const char *script_list[][2] = { { "Meitei Mayek", "Mtei" }, { "Multani", "Mult" }, { "Myanmar (Burmese)", "Mymr" }, + { "​Nag Mundari", "Nagm" }, { "Nandinagari", "Nand" }, { "Old North Arabian", "Narb" }, { "Nabataean", "Nbat" }, diff --git a/core/string/optimized_translation.cpp b/core/string/optimized_translation.cpp index 07302cc8c3..2fb3b54e27 100644 --- a/core/string/optimized_translation.cpp +++ b/core/string/optimized_translation.cpp @@ -179,14 +179,14 @@ void OptimizedTranslation::generate(const Ref<Translation> &p_from) { } bool OptimizedTranslation::_set(const StringName &p_name, const Variant &p_value) { - String name = p_name.operator String(); - if (name == "hash_table") { + String prop_name = p_name.operator String(); + if (prop_name == "hash_table") { hash_table = p_value; - } else if (name == "bucket_table") { + } else if (prop_name == "bucket_table") { bucket_table = p_value; - } else if (name == "strings") { + } else if (prop_name == "strings") { strings = p_value; - } else if (name == "load_from") { + } else if (prop_name == "load_from") { generate(p_value); } else { return false; @@ -196,12 +196,12 @@ bool OptimizedTranslation::_set(const StringName &p_name, const Variant &p_value } bool OptimizedTranslation::_get(const StringName &p_name, Variant &r_ret) const { - String name = p_name.operator String(); - if (name == "hash_table") { + String prop_name = p_name.operator String(); + if (prop_name == "hash_table") { r_ret = hash_table; - } else if (name == "bucket_table") { + } else if (prop_name == "bucket_table") { r_ret = bucket_table; - } else if (name == "strings") { + } else if (prop_name == "strings") { r_ret = strings; } else { return false; @@ -267,6 +267,39 @@ StringName OptimizedTranslation::get_message(const StringName &p_src_text, const } } +Vector<String> OptimizedTranslation::get_translated_message_list() const { + Vector<String> msgs; + + const int *htr = hash_table.ptr(); + const uint32_t *htptr = (const uint32_t *)&htr[0]; + const int *btr = bucket_table.ptr(); + const uint32_t *btptr = (const uint32_t *)&btr[0]; + const uint8_t *sr = strings.ptr(); + const char *sptr = (const char *)&sr[0]; + + for (int i = 0; i < hash_table.size(); i++) { + uint32_t p = htptr[i]; + if (p != 0xFFFFFFFF) { + const Bucket &bucket = *(const Bucket *)&btptr[p]; + for (int j = 0; j < bucket.size; j++) { + if (bucket.elem[j].comp_size == bucket.elem[j].uncomp_size) { + String rstr; + rstr.parse_utf8(&sptr[bucket.elem[j].str_offset], bucket.elem[j].uncomp_size); + msgs.push_back(rstr); + } else { + CharString uncomp; + uncomp.resize(bucket.elem[j].uncomp_size + 1); + smaz_decompress(&sptr[bucket.elem[j].str_offset], bucket.elem[j].comp_size, uncomp.ptrw(), bucket.elem[j].uncomp_size); + String rstr; + rstr.parse_utf8(uncomp.get_data()); + msgs.push_back(rstr); + } + } + } + } + return msgs; +} + StringName OptimizedTranslation::get_plural_message(const StringName &p_src_text, const StringName &p_plural_text, int p_n, const StringName &p_context) const { // The use of plurals translation is not yet supported in OptimizedTranslation. return get_message(p_src_text, p_context); diff --git a/core/string/optimized_translation.h b/core/string/optimized_translation.h index f3dbfe8f5c..1cd12782d0 100644 --- a/core/string/optimized_translation.h +++ b/core/string/optimized_translation.h @@ -81,6 +81,7 @@ protected: public: virtual StringName get_message(const StringName &p_src_text, const StringName &p_context = "") const override; //overridable for other implementations virtual StringName get_plural_message(const StringName &p_src_text, const StringName &p_plural_text, int p_n, const StringName &p_context = "") const override; + virtual Vector<String> get_translated_message_list() const override; void generate(const Ref<Translation> &p_from); OptimizedTranslation() {} diff --git a/core/string/translation.cpp b/core/string/translation.cpp index b83b7c786f..d1ac91957a 100644 --- a/core/string/translation.cpp +++ b/core/string/translation.cpp @@ -59,6 +59,18 @@ Vector<String> Translation::_get_message_list() const { return msgs; } +Vector<String> Translation::get_translated_message_list() const { + Vector<String> msgs; + msgs.resize(translation_map.size()); + int idx = 0; + for (const KeyValue<StringName, StringName> &E : translation_map) { + msgs.set(idx, E.value); + idx += 1; + } + + return msgs; +} + void Translation::_set_messages(const Dictionary &p_messages) { List<Variant> keys; p_messages.get_key_list(&keys); @@ -70,7 +82,7 @@ void Translation::_set_messages(const Dictionary &p_messages) { void Translation::set_locale(const String &p_locale) { locale = TranslationServer::get_singleton()->standardize_locale(p_locale); - if (OS::get_singleton()->get_main_loop() && TranslationServer::get_singleton()->get_loaded_locales().has(this)) { + if (OS::get_singleton()->get_main_loop() && TranslationServer::get_singleton()->get_loaded_locales().has(get_locale())) { OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_TRANSLATION_CHANGED); } } @@ -140,6 +152,7 @@ void Translation::_bind_methods() { ClassDB::bind_method(D_METHOD("get_plural_message", "src_message", "src_plural_message", "n", "context"), &Translation::get_plural_message, DEFVAL("")); ClassDB::bind_method(D_METHOD("erase_message", "src_message", "context"), &Translation::erase_message, DEFVAL("")); ClassDB::bind_method(D_METHOD("get_message_list"), &Translation::_get_message_list); + ClassDB::bind_method(D_METHOD("get_translated_message_list"), &Translation::get_translated_message_list); ClassDB::bind_method(D_METHOD("get_message_count"), &Translation::get_message_count); ClassDB::bind_method(D_METHOD("_set_messages", "messages"), &Translation::_set_messages); ClassDB::bind_method(D_METHOD("_get_messages"), &Translation::_get_messages); @@ -293,31 +306,35 @@ void TranslationServer::init_locale_info() { } String TranslationServer::standardize_locale(const String &p_locale) const { + return _standardize_locale(p_locale, false); +} + +String TranslationServer::_standardize_locale(const String &p_locale, bool p_add_defaults) const { // Replaces '-' with '_' for macOS style locales. String univ_locale = p_locale.replace("-", "_"); // Extract locale elements. - String lang, script, country, variant; + String lang_name, script_name, country_name, variant_name; Vector<String> locale_elements = univ_locale.get_slice("@", 0).split("_"); - lang = locale_elements[0]; + lang_name = locale_elements[0]; if (locale_elements.size() >= 2) { if (locale_elements[1].length() == 4 && is_ascii_upper_case(locale_elements[1][0]) && is_ascii_lower_case(locale_elements[1][1]) && is_ascii_lower_case(locale_elements[1][2]) && is_ascii_lower_case(locale_elements[1][3])) { - script = locale_elements[1]; + script_name = locale_elements[1]; } if (locale_elements[1].length() == 2 && is_ascii_upper_case(locale_elements[1][0]) && is_ascii_upper_case(locale_elements[1][1])) { - country = locale_elements[1]; + country_name = locale_elements[1]; } } if (locale_elements.size() >= 3) { if (locale_elements[2].length() == 2 && is_ascii_upper_case(locale_elements[2][0]) && is_ascii_upper_case(locale_elements[2][1])) { - country = locale_elements[2]; - } else if (variant_map.has(locale_elements[2].to_lower()) && variant_map[locale_elements[2].to_lower()] == lang) { - variant = locale_elements[2].to_lower(); + country_name = locale_elements[2]; + } else if (variant_map.has(locale_elements[2].to_lower()) && variant_map[locale_elements[2].to_lower()] == lang_name) { + variant_name = locale_elements[2].to_lower(); } } if (locale_elements.size() >= 4) { - if (variant_map.has(locale_elements[3].to_lower()) && variant_map[locale_elements[3].to_lower()] == lang) { - variant = locale_elements[3].to_lower(); + if (variant_map.has(locale_elements[3].to_lower()) && variant_map[locale_elements[3].to_lower()] == lang_name) { + variant_name = locale_elements[3].to_lower(); } } @@ -325,74 +342,76 @@ String TranslationServer::standardize_locale(const String &p_locale) const { Vector<String> script_extra = univ_locale.get_slice("@", 1).split(";"); for (int i = 0; i < script_extra.size(); i++) { if (script_extra[i].to_lower() == "cyrillic") { - script = "Cyrl"; + script_name = "Cyrl"; break; } else if (script_extra[i].to_lower() == "latin") { - script = "Latn"; + script_name = "Latn"; break; } else if (script_extra[i].to_lower() == "devanagari") { - script = "Deva"; + script_name = "Deva"; break; - } else if (variant_map.has(script_extra[i].to_lower()) && variant_map[script_extra[i].to_lower()] == lang) { - variant = script_extra[i].to_lower(); + } else if (variant_map.has(script_extra[i].to_lower()) && variant_map[script_extra[i].to_lower()] == lang_name) { + variant_name = script_extra[i].to_lower(); } } // Handles known non-ISO language names used e.g. on Windows. - if (locale_rename_map.has(lang)) { - lang = locale_rename_map[lang]; + if (locale_rename_map.has(lang_name)) { + lang_name = locale_rename_map[lang_name]; } // Handle country renames. - if (country_rename_map.has(country)) { - country = country_rename_map[country]; + if (country_rename_map.has(country_name)) { + country_name = country_rename_map[country_name]; } // Remove unsupported script codes. - if (!script_map.has(script)) { - script = ""; + if (!script_map.has(script_name)) { + script_name = ""; } // Add script code base on language and country codes for some ambiguous cases. - if (script.is_empty()) { - for (int i = 0; i < locale_script_info.size(); i++) { - const LocaleScriptInfo &info = locale_script_info[i]; - if (info.name == lang) { - if (country.is_empty() || info.supported_countries.has(country)) { - script = info.script; - break; + if (p_add_defaults) { + if (script_name.is_empty()) { + for (int i = 0; i < locale_script_info.size(); i++) { + const LocaleScriptInfo &info = locale_script_info[i]; + if (info.name == lang_name) { + if (country_name.is_empty() || info.supported_countries.has(country_name)) { + script_name = info.script; + break; + } } } } - } - if (!script.is_empty() && country.is_empty()) { - // Add conntry code based on script for some ambiguous cases. - for (int i = 0; i < locale_script_info.size(); i++) { - const LocaleScriptInfo &info = locale_script_info[i]; - if (info.name == lang && info.script == script) { - country = info.default_country; - break; + if (!script_name.is_empty() && country_name.is_empty()) { + // Add conntry code based on script for some ambiguous cases. + for (int i = 0; i < locale_script_info.size(); i++) { + const LocaleScriptInfo &info = locale_script_info[i]; + if (info.name == lang_name && info.script == script_name) { + country_name = info.default_country; + break; + } } } } // Combine results. - String locale = lang; - if (!script.is_empty()) { - locale = locale + "_" + script; + String out = lang_name; + if (!script_name.is_empty()) { + out = out + "_" + script_name; } - if (!country.is_empty()) { - locale = locale + "_" + country; + if (!country_name.is_empty()) { + out = out + "_" + country_name; } - if (!variant.is_empty()) { - locale = locale + "_" + variant; + if (!variant_name.is_empty()) { + out = out + "_" + variant_name; } - return locale; + return out; } int TranslationServer::compare_locales(const String &p_locale_a, const String &p_locale_b) const { - String locale_a = standardize_locale(p_locale_a); - String locale_b = standardize_locale(p_locale_b); + String locale_a = _standardize_locale(p_locale_a, true); + String locale_b = _standardize_locale(p_locale_b, true); if (locale_a == locale_b) { // Exact match. @@ -420,31 +439,29 @@ int TranslationServer::compare_locales(const String &p_locale_a, const String &p } String TranslationServer::get_locale_name(const String &p_locale) const { - String locale = standardize_locale(p_locale); - - String lang, script, country; - Vector<String> locale_elements = locale.split("_"); - lang = locale_elements[0]; + String lang_name, script_name, country_name; + Vector<String> locale_elements = standardize_locale(p_locale).split("_"); + lang_name = locale_elements[0]; if (locale_elements.size() >= 2) { if (locale_elements[1].length() == 4 && is_ascii_upper_case(locale_elements[1][0]) && is_ascii_lower_case(locale_elements[1][1]) && is_ascii_lower_case(locale_elements[1][2]) && is_ascii_lower_case(locale_elements[1][3])) { - script = locale_elements[1]; + script_name = locale_elements[1]; } if (locale_elements[1].length() == 2 && is_ascii_upper_case(locale_elements[1][0]) && is_ascii_upper_case(locale_elements[1][1])) { - country = locale_elements[1]; + country_name = locale_elements[1]; } } if (locale_elements.size() >= 3) { if (locale_elements[2].length() == 2 && is_ascii_upper_case(locale_elements[2][0]) && is_ascii_upper_case(locale_elements[2][1])) { - country = locale_elements[2]; + country_name = locale_elements[2]; } } - String name = language_map[lang]; - if (!script.is_empty()) { - name = name + " (" + script_map[script] + ")"; + String name = language_map[lang_name]; + if (!script_name.is_empty()) { + name = name + " (" + script_map[script_name] + ")"; } - if (!country.is_empty()) { - name = name + ", " + country_name_map[country]; + if (!country_name.is_empty()) { + name = name + ", " + country_name_map[country_name]; } return name; } @@ -505,11 +522,11 @@ String TranslationServer::get_locale() const { return locale; } -Array TranslationServer::get_loaded_locales() const { - Array locales; +PackedStringArray TranslationServer::get_loaded_locales() const { + PackedStringArray locales; for (const Ref<Translation> &E : translations) { const Ref<Translation> &t = E; - ERR_FAIL_COND_V(t.is_null(), Array()); + ERR_FAIL_COND_V(t.is_null(), PackedStringArray()); String l = t->get_locale(); locales.push_back(l); @@ -630,12 +647,12 @@ TranslationServer *TranslationServer::singleton = nullptr; bool TranslationServer::_load_translations(const String &p_from) { if (ProjectSettings::get_singleton()->has_setting(p_from)) { - Vector<String> translations = ProjectSettings::get_singleton()->get(p_from); + const Vector<String> &translation_names = GLOBAL_GET(p_from); - int tcount = translations.size(); + int tcount = translation_names.size(); if (tcount) { - const String *r = translations.ptr(); + const String *r = translation_names.ptr(); for (int i = 0; i < tcount; i++) { Ref<Translation> tr = ResourceLoader::load(r[i]); @@ -964,7 +981,6 @@ void TranslationServer::_bind_methods() { } void TranslationServer::load_translations() { - String locale = get_locale(); _load_translations("internationalization/locale/translations"); //all _load_translations("internationalization/locale/translations_" + locale.substr(0, 2)); diff --git a/core/string/translation.h b/core/string/translation.h index 20c6ebd5a5..5e8344baac 100644 --- a/core/string/translation.h +++ b/core/string/translation.h @@ -64,6 +64,7 @@ public: virtual void erase_message(const StringName &p_src_text, const StringName &p_context = ""); virtual void get_message_list(List<StringName> *r_messages) const; virtual int get_message_count() const; + virtual Vector<String> get_translated_message_list() const; Translation() {} }; @@ -102,6 +103,7 @@ class TranslationServer : public Object { static TranslationServer *singleton; bool _load_translations(const String &p_from); + String _standardize_locale(const String &p_locale, bool p_add_defaults) const; StringName _get_message_from_translations(const StringName &p_message, const StringName &p_context, const String &p_locale, bool plural, const String &p_message_plural = "", int p_n = 0) const; @@ -145,7 +147,7 @@ public: String get_locale_name(const String &p_locale) const; - Array get_loaded_locales() const; + PackedStringArray get_loaded_locales() const; void add_translation(const Ref<Translation> &p_translation); void remove_translation(const Ref<Translation> &p_translation); diff --git a/core/string/translation_po.cpp b/core/string/translation_po.cpp index fa656b634d..724c1d42bc 100644 --- a/core/string/translation_po.cpp +++ b/core/string/translation_po.cpp @@ -103,6 +103,23 @@ void TranslationPO::_set_messages(const Dictionary &p_messages) { } } +Vector<String> TranslationPO::get_translated_message_list() const { + Vector<String> msgs; + for (const KeyValue<StringName, HashMap<StringName, Vector<StringName>>> &E : translation_map) { + if (E.key != StringName()) { + continue; + } + + for (const KeyValue<StringName, Vector<StringName>> &E2 : E.value) { + for (const StringName &E3 : E2.value) { + msgs.push_back(E3); + } + } + } + + return msgs; +} + Vector<String> TranslationPO::_get_message_list() const { // Return all keys in translation_map. diff --git a/core/string/translation_po.h b/core/string/translation_po.h index 7d63af2246..c50ea85744 100644 --- a/core/string/translation_po.h +++ b/core/string/translation_po.h @@ -70,6 +70,7 @@ protected: static void _bind_methods(); public: + Vector<String> get_translated_message_list() const override; void get_message_list(List<StringName> *r_messages) const override; int get_message_count() const override; void add_message(const StringName &p_src_text, const StringName &p_xlated_text, const StringName &p_context = "") override; diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp index e93375bff7..175c42542b 100644 --- a/core/string/ustring.cpp +++ b/core/string/ustring.cpp @@ -531,10 +531,12 @@ String &String::operator+=(const String &p_str) { resize(lhs_len + rhs_len + 1); - const char32_t *src = p_str.get_data(); + const char32_t *src = p_str.ptr(); char32_t *dst = ptrw() + lhs_len; - memcpy(dst, src, (rhs_len + 1) * sizeof(char32_t)); + // Don't copy the terminating null with `memcpy` to avoid undefined behavior when string is being added to itself (it would overlap the destination). + memcpy(dst, src, rhs_len * sizeof(char32_t)); + *(dst + rhs_len) = _null; return *this; } @@ -968,62 +970,71 @@ const char32_t *String::get_data() const { return size() ? &operator[](0) : &zero; } -String String::capitalize() const { - String aux = this->camelcase_to_underscore(true).replace("_", " ").strip_edges(); - String cap; - for (int i = 0; i < aux.get_slice_count(" "); i++) { - String slice = aux.get_slicec(' ', i); - if (slice.length() > 0) { - slice[0] = _find_upper(slice[0]); - if (i > 0) { - cap += " "; - } - cap += slice; - } - } - - return cap; -} - -String String::camelcase_to_underscore(bool lowercase) const { +String String::_camelcase_to_underscore() const { const char32_t *cstr = get_data(); String new_string; int start_index = 0; for (int i = 1; i < this->size(); i++) { - bool is_upper = is_ascii_upper_case(cstr[i]); - bool is_number = is_digit(cstr[i]); + bool is_prev_upper = is_ascii_upper_case(cstr[i - 1]); + bool is_prev_lower = is_ascii_lower_case(cstr[i - 1]); + bool is_prev_digit = is_digit(cstr[i - 1]); - bool are_next_2_lower = false; - bool is_next_lower = false; - bool is_next_number = false; - bool was_precedent_upper = is_ascii_upper_case(cstr[i - 1]); - bool was_precedent_number = is_digit(cstr[i - 1]); - - if (i + 2 < this->size()) { - are_next_2_lower = is_ascii_lower_case(cstr[i + 1]) && is_ascii_lower_case(cstr[i + 2]); - } + bool is_curr_upper = is_ascii_upper_case(cstr[i]); + bool is_curr_lower = is_ascii_lower_case(cstr[i]); + bool is_curr_digit = is_digit(cstr[i]); + bool is_next_lower = false; if (i + 1 < this->size()) { is_next_lower = is_ascii_lower_case(cstr[i + 1]); - is_next_number = is_digit(cstr[i + 1]); } - const bool cond_a = is_upper && !was_precedent_upper && !was_precedent_number; - const bool cond_b = was_precedent_upper && is_upper && are_next_2_lower; - const bool cond_c = is_number && !was_precedent_number; - const bool can_break_number_letter = is_number && !was_precedent_number && is_next_lower; - const bool can_break_letter_number = !is_number && was_precedent_number && (is_next_lower || is_next_number); + const bool cond_a = is_prev_lower && is_curr_upper; // aA + const bool cond_b = (is_prev_upper || is_prev_digit) && is_curr_upper && is_next_lower; // AAa, 2Aa + const bool cond_c = is_prev_digit && is_curr_lower && is_next_lower; // 2aa + const bool cond_d = (is_prev_upper || is_prev_lower) && is_curr_digit; // A2, a2 - bool should_split = cond_a || cond_b || cond_c || can_break_number_letter || can_break_letter_number; - if (should_split) { + if (cond_a || cond_b || cond_c || cond_d) { new_string += this->substr(start_index, i - start_index) + "_"; start_index = i; } } new_string += this->substr(start_index, this->size() - start_index); - return lowercase ? new_string.to_lower() : new_string; + return new_string.to_lower(); +} + +String String::capitalize() const { + String aux = this->_camelcase_to_underscore().replace("_", " ").strip_edges(); + String cap; + for (int i = 0; i < aux.get_slice_count(" "); i++) { + String slice = aux.get_slicec(' ', i); + if (slice.length() > 0) { + slice[0] = _find_upper(slice[0]); + if (i > 0) { + cap += " "; + } + cap += slice; + } + } + + return cap; +} + +String String::to_camel_case() const { + String s = this->to_pascal_case(); + if (!s.is_empty()) { + s[0] = _find_lower(s[0]); + } + return s; +} + +String String::to_pascal_case() const { + return this->capitalize().replace(" ", ""); +} + +String String::to_snake_case() const { + return this->_camelcase_to_underscore().replace(" ", "_").strip_edges(); } String String::get_with_code_lines() const { @@ -1169,9 +1180,14 @@ Vector<String> String::split(const String &p_splitter, bool p_allow_empty, int p int len = length(); while (true) { - int end = find(p_splitter, from); - if (end < 0) { - end = len; + int end; + if (p_splitter.is_empty()) { + end = from + 1; + } else { + end = find(p_splitter, from); + if (end < 0) { + end = len; + } } if (p_allow_empty || (end > from)) { if (p_maxsplit <= 0) { @@ -1212,7 +1228,15 @@ Vector<String> String::rsplit(const String &p_splitter, bool p_allow_empty, int break; } - int left_edge = rfind(p_splitter, remaining_len - p_splitter.length()); + int left_edge; + if (p_splitter.is_empty()) { + left_edge = remaining_len - 1; + if (left_edge == 0) { + left_edge--; // Skip to the < 0 condition. + } + } else { + left_edge = rfind(p_splitter, remaining_len - p_splitter.length()); + } if (left_edge < 0) { // no more splitters, we're done @@ -1232,8 +1256,8 @@ Vector<String> String::rsplit(const String &p_splitter, bool p_allow_empty, int return ret; } -Vector<float> String::split_floats(const String &p_splitter, bool p_allow_empty) const { - Vector<float> ret; +Vector<double> String::split_floats(const String &p_splitter, bool p_allow_empty) const { + Vector<double> ret; int from = 0; int len = length(); @@ -1436,15 +1460,25 @@ String String::num(double p_num, int p_decimals) { fmt[5] = 'f'; fmt[6] = 0; } - char buf[256]; + // if we want to convert a double with as much decimal places as as + // DBL_MAX or DBL_MIN then we would theoretically need a buffer of at least + // DBL_MAX_10_EXP + 2 for DBL_MAX and DBL_MAX_10_EXP + 4 for DBL_MIN. + // BUT those values where still giving me exceptions, so I tested from + // DBL_MAX_10_EXP + 10 incrementing one by one and DBL_MAX_10_EXP + 17 (325) + // was the first buffer size not to throw an exception + char buf[325]; #if defined(__GNUC__) || defined(_MSC_VER) - snprintf(buf, 256, fmt, p_num); + // PLEASE NOTE that, albeit vcrt online reference states that snprintf + // should safely truncate the output to the given buffer size, we have + // found a case where this is not true, so we should create a buffer + // as big as needed + snprintf(buf, 325, fmt, p_num); #else sprintf(buf, fmt, p_num); #endif - buf[255] = 0; + buf[324] = 0; //destroy trailing zeroes { bool period = false; @@ -1556,10 +1590,11 @@ String String::num_real(double p_num, bool p_trailing) { #else int decimals = 6; #endif - // We want to align the digits to the above sane default, so we only - // need to subtract log10 for numbers with a positive power of ten. - if (p_num > 10) { - decimals -= (int)floor(log10(p_num)); + // We want to align the digits to the above sane default, so we only need + // to subtract log10 for numbers with a positive power of ten magnitude. + double abs_num = Math::abs(p_num); + if (abs_num > 10) { + decimals -= (int)floor(log10(abs_num)); } return num(p_num, decimals); } @@ -2613,10 +2648,11 @@ double String::to_float() const { uint32_t String::hash(const char *p_cstr) { uint32_t hashv = 5381; - uint32_t c; + uint32_t c = *p_cstr++; - while ((c = *p_cstr++)) { + while (c) { hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */ + c = *p_cstr++; } return hashv; @@ -2642,10 +2678,11 @@ uint32_t String::hash(const wchar_t *p_cstr, int p_len) { uint32_t String::hash(const wchar_t *p_cstr) { uint32_t hashv = 5381; - uint32_t c; + uint32_t c = *p_cstr++; - while ((c = *p_cstr++)) { + while (c) { hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */ + c = *p_cstr++; } return hashv; @@ -2662,10 +2699,11 @@ uint32_t String::hash(const char32_t *p_cstr, int p_len) { uint32_t String::hash(const char32_t *p_cstr) { uint32_t hashv = 5381; - uint32_t c; + uint32_t c = *p_cstr++; - while ((c = *p_cstr++)) { + while (c) { hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */ + c = *p_cstr++; } return hashv; @@ -2676,10 +2714,11 @@ uint32_t String::hash() const { const char32_t *chr = get_data(); uint32_t hashv = 5381; - uint32_t c; + uint32_t c = *chr++; - while ((c = *chr++)) { + while (c) { hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */ + c = *chr++; } return hashv; @@ -2690,10 +2729,11 @@ uint64_t String::hash64() const { const char32_t *chr = get_data(); uint64_t hashv = 5381; - uint64_t c; + uint64_t c = *chr++; - while ((c = *chr++)) { + while (c) { hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */ + c = *chr++; } return hashv; @@ -2799,7 +2839,7 @@ String String::substr(int p_from, int p_chars) const { return String(*this); } - String s = String(); + String s; s.copy_from_unchecked(&get_data()[p_from], p_chars); return s; } @@ -3449,18 +3489,19 @@ String String::replacen(const String &p_key, const String &p_with) const { String String::repeat(int p_count) const { ERR_FAIL_COND_V_MSG(p_count < 0, "", "Parameter count should be a positive number."); - String new_string; - const char32_t *src = this->get_data(); - - new_string.resize(length() * p_count + 1); - new_string[length() * p_count] = 0; - - for (int i = 0; i < p_count; i++) { - for (int j = 0; j < length(); j++) { - new_string[i * length() + j] = src[j]; - } - } - + int len = length(); + String new_string = *this; + new_string.resize(p_count * len + 1); + + char32_t *dst = new_string.ptrw(); + int offset = 1; + int stride = 1; + while (offset < p_count) { + memcpy(dst + offset * len, dst, stride * len * sizeof(char32_t)); + offset += stride; + stride = MIN(stride * 2, p_count - offset); + } + dst[p_count * len] = _null; return new_string; } @@ -3650,29 +3691,43 @@ bool String::is_network_share_path() const { String String::simplify_path() const { String s = *this; String drive; - if (s.begins_with("local://")) { - drive = "local://"; - s = s.substr(8, s.length()); - } else if (s.begins_with("res://")) { - drive = "res://"; - s = s.substr(6, s.length()); - } else if (s.begins_with("user://")) { - drive = "user://"; - s = s.substr(7, s.length()); - } else if (is_network_share_path()) { - drive = s.substr(0, 2); - s = s.substr(2, s.length() - 2); - } else if (s.begins_with("/") || s.begins_with("\\")) { - drive = s.substr(0, 1); - s = s.substr(1, s.length() - 1); - } else { - int p = s.find(":/"); - if (p == -1) { - p = s.find(":\\"); + + // Check if we have a special path (like res://) or a protocol identifier. + int p = s.find("://"); + bool found = false; + if (p > 0) { + bool only_chars = true; + for (int i = 0; i < p; i++) { + if (!is_ascii_alphanumeric_char(s[i])) { + only_chars = false; + break; + } } - if (p != -1 && p < s.find("/")) { - drive = s.substr(0, p + 2); - s = s.substr(p + 2, s.length()); + if (only_chars) { + found = true; + drive = s.substr(0, p + 3); + s = s.substr(p + 3); + } + } + if (!found) { + if (is_network_share_path()) { + // Network path, beginning with // or \\. + drive = s.substr(0, 2); + s = s.substr(2); + } else if (s.begins_with("/") || s.begins_with("\\")) { + // Absolute path. + drive = s.substr(0, 1); + s = s.substr(1); + } else { + // Windows-style drive path, like C:/ or C:\. + p = s.find(":/"); + if (p == -1) { + p = s.find(":\\"); + } + if (p != -1 && p < s.find("/")) { + drive = s.substr(0, p + 2); + s = s.substr(p + 2); + } } } @@ -3867,7 +3922,6 @@ String String::c_unescape() const { escaped = escaped.replace("\\v", "\v"); escaped = escaped.replace("\\'", "\'"); escaped = escaped.replace("\\\"", "\""); - escaped = escaped.replace("\\?", "\?"); escaped = escaped.replace("\\\\", "\\"); return escaped; @@ -3884,7 +3938,6 @@ String String::c_escape() const { escaped = escaped.replace("\t", "\\t"); escaped = escaped.replace("\v", "\\v"); escaped = escaped.replace("\'", "\\'"); - escaped = escaped.replace("\?", "\\?"); escaped = escaped.replace("\"", "\\\""); return escaped; @@ -4448,7 +4501,7 @@ String String::get_extension() const { return substr(pos + 1, length()); } -String String::plus_file(const String &p_file) const { +String String::path_join(const String &p_file) const { if (is_empty()) { return p_file; } @@ -4634,15 +4687,18 @@ String String::sprintf(const Array &values, bool *error) const { double value = values[value_index]; bool is_negative = (value < 0); String str = String::num(ABS(value), min_decimals); + const bool is_finite = Math::is_finite(value); // Pad decimals out. - str = str.pad_decimals(min_decimals); + if (is_finite) { + str = str.pad_decimals(min_decimals); + } int initial_len = str.length(); // Padding. Leave room for sign later if required. int pad_chars_count = (is_negative || show_sign) ? min_chars - 1 : min_chars; - String pad_char = pad_with_zeros ? String("0") : String(" "); + String pad_char = (pad_with_zeros && is_finite) ? String("0") : String(" "); // Never pad NaN or inf with zeros if (left_justified) { str = str.rpad(pad_chars_count, pad_char); } else { @@ -4664,6 +4720,76 @@ String String::sprintf(const Array &values, bool *error) const { in_format = false; break; } + case 'v': { // Vector2/3/4/2i/3i/4i + if (value_index >= values.size()) { + return "not enough arguments for format string"; + } + + int count; + switch (values[value_index].get_type()) { + case Variant::VECTOR2: + case Variant::VECTOR2I: { + count = 2; + } break; + case Variant::VECTOR3: + case Variant::VECTOR3I: { + count = 3; + } break; + case Variant::VECTOR4: + case Variant::VECTOR4I: { + count = 4; + } break; + default: { + return "%v requires a vector type (Vector2/3/4/2i/3i/4i)"; + } + } + + Vector4 vec = values[value_index]; + String str = "("; + for (int i = 0; i < count; i++) { + double val = vec[i]; + String number_str = String::num(ABS(val), min_decimals); + const bool is_finite = Math::is_finite(val); + + // Pad decimals out. + if (is_finite) { + number_str = number_str.pad_decimals(min_decimals); + } + + int initial_len = number_str.length(); + + // Padding. Leave room for sign later if required. + int pad_chars_count = val < 0 ? min_chars - 1 : min_chars; + String pad_char = (pad_with_zeros && is_finite) ? String("0") : String(" "); // Never pad NaN or inf with zeros + if (left_justified) { + number_str = number_str.rpad(pad_chars_count, pad_char); + } else { + number_str = number_str.lpad(pad_chars_count, pad_char); + } + + // Add sign if needed. + if (val < 0) { + if (left_justified) { + number_str = number_str.insert(0, "-"); + } else { + number_str = number_str.insert(pad_with_zeros ? 0 : number_str.length() - initial_len, "-"); + } + } + + // Add number to combined string + str += number_str; + + if (i < count - 1) { + str += ", "; + } + } + str += ")"; + + formatted += str; + ++value_index; + in_format = false; + break; + } case 's': { // String if (value_index >= values.size()) { return "not enough arguments for format string"; @@ -4756,7 +4882,7 @@ String String::sprintf(const Array &values, bool *error) const { } break; } - case '.': { // Float separator. + case '.': { // Float/Vector separator. if (in_decimals) { return "too many decimal points in format"; } @@ -4770,8 +4896,12 @@ String String::sprintf(const Array &values, bool *error) const { return "not enough arguments for format string"; } - if (!values[value_index].is_num()) { - return "* wants number"; + Variant::Type value_type = values[value_index].get_type(); + if (!values[value_index].is_num() && + value_type != Variant::VECTOR2 && value_type != Variant::VECTOR2I && + value_type != Variant::VECTOR3 && value_type != Variant::VECTOR3I && + value_type != Variant::VECTOR4 && value_type != Variant::VECTOR4I) { + return "* wants number or vector"; } int size = values[value_index]; diff --git a/core/string/ustring.h b/core/string/ustring.h index 6c3169f136..0c171024f7 100644 --- a/core/string/ustring.h +++ b/core/string/ustring.h @@ -28,11 +28,11 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -// Note: _GODOT suffix added to avoid conflict with ICU header with the same guard. - #ifndef USTRING_GODOT_H #define USTRING_GODOT_H +// Note: _GODOT suffix added to header guard to avoid conflict with ICU header. + #include "core/string/char_utils.h" #include "core/templates/cowdata.h" #include "core/templates/vector.h" @@ -196,6 +196,7 @@ class String { bool _base_is_subsequence_of(const String &p_string, bool case_insensitive) const; int _count(const String &p_string, int p_from, int p_to, bool p_case_insensitive) const; + String _camelcase_to_underscore() const; public: enum { @@ -335,17 +336,19 @@ public: static double to_float(const char32_t *p_str, const char32_t **r_end = nullptr); String capitalize() const; - String camelcase_to_underscore(bool lowercase = true) const; + String to_camel_case() const; + String to_pascal_case() const; + String to_snake_case() const; String get_with_code_lines() const; int get_slice_count(String p_splitter) const; String get_slice(String p_splitter, int p_slice) const; String get_slicec(char32_t p_splitter, int p_slice) const; - Vector<String> split(const String &p_splitter, bool p_allow_empty = true, int p_maxsplit = 0) const; - Vector<String> rsplit(const String &p_splitter, bool p_allow_empty = true, int p_maxsplit = 0) const; + Vector<String> split(const String &p_splitter = "", bool p_allow_empty = true, int p_maxsplit = 0) const; + Vector<String> rsplit(const String &p_splitter = "", bool p_allow_empty = true, int p_maxsplit = 0) const; Vector<String> split_spaces() const; - Vector<float> split_floats(const String &p_splitter, bool p_allow_empty = true) const; + Vector<double> split_floats(const String &p_splitter, bool p_allow_empty = true) const; Vector<float> split_floats_mk(const Vector<String> &p_splitters, bool p_allow_empty = true) const; Vector<int> split_ints(const String &p_splitter, bool p_allow_empty = true) const; Vector<int> split_ints_mk(const Vector<String> &p_splitters, bool p_allow_empty = true) const; @@ -370,11 +373,9 @@ public: String rstrip(const String &p_chars) const; String get_extension() const; String get_basename() const; - String plus_file(const String &p_file) const; + String path_join(const String &p_file) const; char32_t unicode_at(int p_idx) const; - void erase(int p_pos, int p_chars); - CharString ascii(bool p_allow_extended = false) const; CharString utf8() const; Error parse_utf8(const char *p_utf8, int p_len = -1, bool p_skip_cr = false); diff --git a/core/templates/cowdata.h b/core/templates/cowdata.h index f98b2308c9..1482e3eef1 100644 --- a/core/templates/cowdata.h +++ b/core/templates/cowdata.h @@ -46,9 +46,7 @@ class CharString; template <class T, class V> class VMap; -#if !defined(NO_THREADS) SAFE_NUMERIC_TYPE_PUN_GUARANTEES(uint32_t) -#endif // Silence a false positive warning (see GH-52119). #if defined(__GNUC__) && !defined(__clang__) diff --git a/core/templates/hashfuncs.h b/core/templates/hashfuncs.h index d85cdf7adc..456a7b01ed 100644 --- a/core/templates/hashfuncs.h +++ b/core/templates/hashfuncs.h @@ -61,10 +61,11 @@ static _FORCE_INLINE_ uint32_t hash_djb2(const char *p_cstr) { const unsigned char *chr = (const unsigned char *)p_cstr; uint32_t hash = 5381; - uint32_t c; + uint32_t c = *chr++; - while ((c = *chr++)) { + while (c) { hash = ((hash << 5) + hash) ^ c; /* hash * 33 ^ c */ + c = *chr++; } return hash; diff --git a/core/templates/local_vector.h b/core/templates/local_vector.h index 49690f2373..dc93acc8ab 100644 --- a/core/templates/local_vector.h +++ b/core/templates/local_vector.h @@ -68,7 +68,7 @@ public: CRASH_COND_MSG(!data, "Out of memory"); } - if (!std::is_trivially_constructible<T>::value && !force_trivial) { + if constexpr (!std::is_trivially_constructible<T>::value && !force_trivial) { memnew_placement(&data[count++], T(p_elem)); } else { data[count++] = p_elem; @@ -81,7 +81,7 @@ public: for (U i = p_index; i < count; i++) { data[i] = data[i + 1]; } - if (!std::is_trivially_destructible<T>::value && !force_trivial) { + if constexpr (!std::is_trivially_destructible<T>::value && !force_trivial) { data[count].~T(); } } @@ -94,7 +94,7 @@ public: if (count > p_index) { data[p_index] = data[count]; } - if (!std::is_trivially_destructible<T>::value && !force_trivial) { + if constexpr (!std::is_trivially_destructible<T>::value && !force_trivial) { data[count].~T(); } } @@ -135,7 +135,7 @@ public: _FORCE_INLINE_ U size() const { return count; } void resize(U p_size) { if (p_size < count) { - if (!std::is_trivially_destructible<T>::value && !force_trivial) { + if constexpr (!std::is_trivially_destructible<T>::value && !force_trivial) { for (U i = p_size; i < count; i++) { data[i].~T(); } @@ -152,7 +152,7 @@ public: data = (T *)memrealloc(data, capacity * sizeof(T)); CRASH_COND_MSG(!data, "Out of memory"); } - if (!std::is_trivially_constructible<T>::value && !force_trivial) { + if constexpr (!std::is_trivially_constructible<T>::value && !force_trivial) { for (U i = count; i < p_size; i++) { memnew_placement(&data[i], T); } diff --git a/core/templates/paged_array.h b/core/templates/paged_array.h index f1ede556e6..2992dd463e 100644 --- a/core/templates/paged_array.h +++ b/core/templates/paged_array.h @@ -268,7 +268,7 @@ public: uint32_t remainder = count & page_size_mask; T *remainder_page = nullptr; - uint32_t remainder_page_id; + uint32_t remainder_page_id = 0; if (remainder > 0) { uint32_t last_page = _get_pages_in_use() - 1; diff --git a/core/templates/pooled_list.h b/core/templates/pooled_list.h index f13156b292..2b67da87ba 100644 --- a/core/templates/pooled_list.h +++ b/core/templates/pooled_list.h @@ -28,7 +28,8 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#pragma once +#ifndef POOLED_LIST_H +#define POOLED_LIST_H // Simple template to provide a pool with O(1) allocate and free. // The freelist could alternatively be a linked list placed within the unused elements @@ -111,7 +112,7 @@ public: list.resize(r_id + 1); static_assert((!zero_on_first_request) || (__is_pod(T)), "zero_on_first_request requires trivial type"); - if (zero_on_first_request && __is_pod(T)) { + if constexpr (zero_on_first_request && __is_pod(T)) { list[r_id] = {}; } @@ -206,3 +207,5 @@ private: LocalVector<U, U> _active_map; LocalVector<U, U> _active_list; }; + +#endif // POOLED_LIST_H diff --git a/core/templates/rid_owner.h b/core/templates/rid_owner.h index 320faebe98..a83ffa575f 100644 --- a/core/templates/rid_owner.h +++ b/core/templates/rid_owner.h @@ -335,15 +335,8 @@ public: ~RID_Alloc() { if (alloc_count) { - if (description) { - print_error("ERROR: " + itos(alloc_count) + " RID allocations of type '" + description + "' were leaked at exit."); - } else { -#ifdef NO_SAFE_CAST - print_error("ERROR: " + itos(alloc_count) + " RID allocations of type 'unknown' were leaked at exit."); -#else - print_error("ERROR: " + itos(alloc_count) + " RID allocations of type '" + typeid(T).name() + "' were leaked at exit."); -#endif - } + print_error(vformat("ERROR: %d RID allocations of type '%s' were leaked at exit.", + alloc_count, description ? description : typeid(T).name())); for (size_t i = 0; i < max_alloc; i++) { uint64_t validator = validator_chunks[i / elements_in_chunk][i % elements_in_chunk]; diff --git a/core/templates/safe_list.h b/core/templates/safe_list.h index e850f3bd5e..95e8ca1a14 100644 --- a/core/templates/safe_list.h +++ b/core/templates/safe_list.h @@ -33,11 +33,9 @@ #include "core/os/memory.h" #include "core/typedefs.h" -#include <functional> - -#if !defined(NO_THREADS) #include <atomic> +#include <functional> #include <type_traits> // Design goals for these classes: @@ -239,159 +237,4 @@ public: } }; -#else // NO_THREADS - -// Effectively the same structure without the atomics. It's probably possible to simplify it but the semantics shouldn't differ greatly. -template <class T, class A = DefaultAllocator> -class SafeList { - struct SafeListNode { - SafeListNode *next = nullptr; - - // If the node is logically deleted, this pointer will typically point to the previous list item in time that was also logically deleted. - SafeListNode *graveyard_next = nullptr; - - std::function<void(T)> deletion_fn = [](T t) { return; }; - - T val; - }; - - SafeListNode *head = nullptr; - SafeListNode *graveyard_head = nullptr; - - unsigned int active_iterator_count = 0; - -public: - class Iterator { - friend class SafeList; - - SafeListNode *cursor = nullptr; - SafeList *list = nullptr; - - public: - Iterator(SafeListNode *p_cursor, SafeList *p_list) : - cursor(p_cursor), list(p_list) { - list->active_iterator_count++; - } - - ~Iterator() { - list->active_iterator_count--; - } - - T &operator*() { - return cursor->val; - } - - Iterator &operator++() { - cursor = cursor->next; - return *this; - } - - // These two operators are mostly useful for comparisons to nullptr. - bool operator==(const void *p_other) const { - return cursor == p_other; - } - - bool operator!=(const void *p_other) const { - return cursor != p_other; - } - - // These two allow easy range-based for loops. - bool operator==(const Iterator &p_other) const { - return cursor == p_other.cursor; - } - - bool operator!=(const Iterator &p_other) const { - return cursor != p_other.cursor; - } - }; - -public: - // Calling this will cause an allocation. - void insert(T p_value) { - SafeListNode *new_node = memnew_allocator(SafeListNode, A); - new_node->val = p_value; - new_node->next = head; - head = new_node; - } - - Iterator find(T p_value) { - for (Iterator it = begin(); it != end(); ++it) { - if (*it == p_value) { - return it; - } - } - return end(); - } - - void erase(T p_value, std::function<void(T)> p_deletion_fn) { - erase(find(p_value), p_deletion_fn); - } - - void erase(T p_value) { - erase(find(p_value), [](T t) { return; }); - } - - void erase(Iterator p_iterator, std::function<void(T)> p_deletion_fn) { - p_iterator.cursor->deletion_fn = p_deletion_fn; - erase(p_iterator); - } - - void erase(Iterator p_iterator) { - Iterator prev = begin(); - for (; prev != end(); ++prev) { - if (prev.cursor && prev.cursor->next == p_iterator.cursor) { - break; - } - } - if (prev == end()) { - // Not in the list, nothing to do. - return; - } - // First, remove the node from the list. - prev.cursor->next = p_iterator.cursor->next; - - // Then queue it for deletion by putting it in the node graveyard. Don't touch `next` because an iterator might still be pointing at this node. - p_iterator.cursor->graveyard_next = graveyard_head; - graveyard_head = p_iterator.cursor; - } - - Iterator begin() { - return Iterator(head, this); - } - - Iterator end() { - return Iterator(nullptr, this); - } - - // Calling this will cause zero to many deallocations. - bool maybe_cleanup() { - SafeListNode *cursor = graveyard_head; - if (active_iterator_count != 0) { - // It's not safe to clean up with an active iterator, because that iterator could be pointing to an element that we want to delete. - return false; - } - graveyard_head = nullptr; - // Our graveyard list is now unreachable by any active iterators, detached from the main graveyard head and ready for deletion. - while (cursor) { - SafeListNode *tmp = cursor; - cursor = cursor->next; - tmp->deletion_fn(tmp->val); - memdelete_allocator<SafeListNode, A>(tmp); - } - return true; - } - - ~SafeList() { -#ifdef DEBUG_ENABLED - if (!maybe_cleanup()) { - ERR_PRINT("There are still iterators around when destructing a SafeList. Memory will be leaked. This is a bug."); - } -#else - maybe_cleanup(); -#endif - } -}; - -#endif - #endif // SAFE_LIST_H diff --git a/core/templates/safe_refcount.h b/core/templates/safe_refcount.h index 1f6551762e..c4ffe5ca02 100644 --- a/core/templates/safe_refcount.h +++ b/core/templates/safe_refcount.h @@ -33,8 +33,6 @@ #include "core/typedefs.h" -#if !defined(NO_THREADS) - #include <atomic> #include <type_traits> @@ -191,141 +189,4 @@ public: } }; -#else - -template <class T> -class SafeNumeric { -protected: - T value; - -public: - _ALWAYS_INLINE_ void set(T p_value) { - value = p_value; - } - - _ALWAYS_INLINE_ T get() const { - return value; - } - - _ALWAYS_INLINE_ T increment() { - return ++value; - } - - _ALWAYS_INLINE_ T postincrement() { - return value++; - } - - _ALWAYS_INLINE_ T decrement() { - return --value; - } - - _ALWAYS_INLINE_ T postdecrement() { - return value--; - } - - _ALWAYS_INLINE_ T add(T p_value) { - return value += p_value; - } - - _ALWAYS_INLINE_ T postadd(T p_value) { - T old = value; - value += p_value; - return old; - } - - _ALWAYS_INLINE_ T sub(T p_value) { - return value -= p_value; - } - - _ALWAYS_INLINE_ T postsub(T p_value) { - T old = value; - value -= p_value; - return old; - } - - _ALWAYS_INLINE_ T exchange_if_greater(T p_value) { - if (value < p_value) { - value = p_value; - } - return value; - } - - _ALWAYS_INLINE_ T conditional_increment() { - if (value == 0) { - return 0; - } else { - return ++value; - } - } - - _ALWAYS_INLINE_ explicit SafeNumeric<T>(T p_value = static_cast<T>(0)) : - value(p_value) { - } -}; - -class SafeFlag { -protected: - bool flag; - -public: - _ALWAYS_INLINE_ bool is_set() const { - return flag; - } - - _ALWAYS_INLINE_ void set() { - flag = true; - } - - _ALWAYS_INLINE_ void clear() { - flag = false; - } - - _ALWAYS_INLINE_ void set_to(bool p_value) { - flag = p_value; - } - - _ALWAYS_INLINE_ explicit SafeFlag(bool p_value = false) : - flag(p_value) {} -}; - -class SafeRefCount { - uint32_t count = 0; - -public: - _ALWAYS_INLINE_ bool ref() { // true on success - if (count != 0) { - ++count; - return true; - } else { - return false; - } - } - - _ALWAYS_INLINE_ uint32_t refval() { // none-zero on success - if (count != 0) { - return ++count; - } else { - return 0; - } - } - - _ALWAYS_INLINE_ bool unref() { // true if must be disposed of - return --count == 0; - } - - _ALWAYS_INLINE_ uint32_t unrefval() { // 0 if must be disposed of - return --count; - } - - _ALWAYS_INLINE_ uint32_t get() const { - return count; - } - - _ALWAYS_INLINE_ void init(uint32_t p_value = 1) { - count = p_value; - } -}; - -#endif - #endif // SAFE_REFCOUNT_H diff --git a/core/variant/array.cpp b/core/variant/array.cpp index c1bdd6a6bc..c6bbd43dc4 100644 --- a/core/variant/array.cpp +++ b/core/variant/array.cpp @@ -31,6 +31,7 @@ #include "array.h" #include "container_type_validate.h" +#include "core/math/math_funcs.h" #include "core/object/class_db.h" #include "core/object/script_language.h" #include "core/templates/hashfuncs.h" @@ -299,6 +300,11 @@ Variant Array::back() const { return operator[](_p->array.size() - 1); } +Variant Array::pick_random() const { + ERR_FAIL_COND_V_MSG(_p->array.size() == 0, Variant(), "Can't take value from empty array."); + return operator[](Math::rand() % _p->array.size()); +} + int Array::find(const Variant &p_value, int p_from) const { ERR_FAIL_COND_V(!_p->typed.validate(p_value, "find"), -1); return _p->array.find(p_value, p_from); @@ -402,6 +408,7 @@ Array Array::recursive_duplicate(bool p_deep, int recursion_count) const { Array Array::slice(int p_begin, int p_end, int p_step, bool p_deep) const { Array result; + result._p->typed = _p->typed; ERR_FAIL_COND_V_MSG(p_step == 0, result, "Slice step cannot be zero."); @@ -433,6 +440,7 @@ Array Array::slice(int p_begin, int p_end, int p_step, bool p_deep) const { Array Array::filter(const Callable &p_callable) const { Array new_arr; new_arr.resize(size()); + new_arr._p->typed = _p->typed; int accepted_count = 0; const Variant *argptrs[1]; diff --git a/core/variant/array.h b/core/variant/array.h index c007376734..ee265a9ffd 100644 --- a/core/variant/array.h +++ b/core/variant/array.h @@ -47,7 +47,6 @@ class Array { void _unref() const; protected: - Array(const Array &p_base, uint32_t p_type, const StringName &p_class_name, const Variant &p_script); bool _assign(const Array &p_array); public: @@ -80,6 +79,7 @@ public: Variant front() const; Variant back() const; + Variant pick_random() const; void sort(); void sort_custom(const Callable &p_callable); @@ -131,6 +131,7 @@ public: void set_read_only(bool p_enable); bool is_read_only() const; + Array(const Array &p_base, uint32_t p_type, const StringName &p_class_name, const Variant &p_script); Array(const Array &p_from); Array(); ~Array(); diff --git a/core/variant/binder_common.h b/core/variant/binder_common.h index f0c3b1ce38..696b27f9f2 100644 --- a/core/variant/binder_common.h +++ b/core/variant/binder_common.h @@ -138,7 +138,7 @@ VARIANT_ENUM_CAST(Vector3::Axis); VARIANT_ENUM_CAST(Vector3i::Axis); VARIANT_ENUM_CAST(Vector4::Axis); VARIANT_ENUM_CAST(Vector4i::Axis); -VARIANT_ENUM_CAST(Basis::EulerOrder); +VARIANT_ENUM_CAST(EulerOrder); VARIANT_ENUM_CAST(Projection::Planes); VARIANT_ENUM_CAST(Error); diff --git a/core/variant/callable.cpp b/core/variant/callable.cpp index 28efb43fc5..79532d9aca 100644 --- a/core/variant/callable.cpp +++ b/core/variant/callable.cpp @@ -63,6 +63,21 @@ void Callable::callp(const Variant **p_arguments, int p_argcount, Variant &r_ret } } +Variant Callable::callv(const Array &p_arguments) const { + int argcount = p_arguments.size(); + const Variant **argptrs = nullptr; + if (argcount) { + argptrs = (const Variant **)alloca(sizeof(Variant *) * argcount); + for (int i = 0; i < argcount; i++) { + argptrs[i] = &p_arguments[i]; + } + } + CallError ce; + Variant ret; + callp(argptrs, argcount, ret, ce); + return ret; +} + Error Callable::rpcp(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; @@ -387,33 +402,33 @@ Error Signal::emit(const Variant **p_arguments, int p_argcount) const { } Error Signal::connect(const Callable &p_callable, uint32_t p_flags) { - Object *object = get_object(); - ERR_FAIL_COND_V(!object, ERR_UNCONFIGURED); + Object *obj = get_object(); + ERR_FAIL_COND_V(!obj, ERR_UNCONFIGURED); - return object->connect(name, p_callable, p_flags); + return obj->connect(name, p_callable, p_flags); } void Signal::disconnect(const Callable &p_callable) { - Object *object = get_object(); - ERR_FAIL_COND(!object); - object->disconnect(name, p_callable); + Object *obj = get_object(); + ERR_FAIL_COND(!obj); + obj->disconnect(name, p_callable); } bool Signal::is_connected(const Callable &p_callable) const { - Object *object = get_object(); - ERR_FAIL_COND_V(!object, false); + Object *obj = get_object(); + ERR_FAIL_COND_V(!obj, false); - return object->is_connected(name, p_callable); + return obj->is_connected(name, p_callable); } Array Signal::get_connections() const { - Object *object = get_object(); - if (!object) { + Object *obj = get_object(); + if (!obj) { return Array(); } List<Object::Connection> connections; - object->get_signal_connection_list(name, &connections); + obj->get_signal_connection_list(name, &connections); Array arr; for (const Object::Connection &E : connections) { diff --git a/core/variant/callable.h b/core/variant/callable.h index 1f1c983eb3..32770bd663 100644 --- a/core/variant/callable.h +++ b/core/variant/callable.h @@ -71,6 +71,17 @@ public: void callp(const Variant **p_arguments, int p_argcount, Variant &r_return_value, CallError &r_call_error) const; void call_deferredp(const Variant **p_arguments, int p_argcount) const; + Variant callv(const Array &p_arguments) const; + + template <typename... VarArgs> + void call_deferred(VarArgs... p_args) const { + Variant args[sizeof...(p_args) + 1] = { p_args..., 0 }; // +1 makes sure zero sized arrays are also supported. + const Variant *argptrs[sizeof...(p_args) + 1]; + for (uint32_t i = 0; i < sizeof...(p_args); i++) { + argptrs[i] = &args[i]; + } + return call_deferredp(sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args)); + } Error rpcp(int p_id, const Variant **p_arguments, int p_argcount, CallError &r_call_error) const; diff --git a/core/variant/container_type_validate.h b/core/variant/container_type_validate.h index 6171c8c88f..427a337aab 100644 --- a/core/variant/container_type_validate.h +++ b/core/variant/container_type_validate.h @@ -79,9 +79,12 @@ struct ContainerTypeValidate { return true; } - ERR_FAIL_COND_V_MSG(type != p_variant.get_type(), false, "Attempted to " + String(p_operation) + " a variable of type '" + Variant::get_type_name(p_variant.get_type()) + "' into a " + where + " of type '" + Variant::get_type_name(type) + "'."); if (type != p_variant.get_type()) { - return false; + if (p_variant.get_type() == Variant::NIL && type == Variant::OBJECT) { + return true; + } + + ERR_FAIL_V_MSG(false, "Attempted to " + String(p_operation) + " a variable of type '" + Variant::get_type_name(p_variant.get_type()) + "' into a " + where + " of type '" + Variant::get_type_name(type) + "'."); } if (type != Variant::OBJECT) { @@ -90,7 +93,7 @@ struct ContainerTypeValidate { #ifdef DEBUG_ENABLED ObjectID object_id = p_variant; if (object_id == ObjectID()) { - return true; //fine its null; + return true; // This is fine, it's null. } Object *object = ObjectDB::get_instance(object_id); ERR_FAIL_COND_V_MSG(object == nullptr, false, "Attempted to " + String(p_operation) + " an invalid (previously freed?) object instance into a '" + String(where) + "."); @@ -101,7 +104,7 @@ struct ContainerTypeValidate { } #endif if (class_name == StringName()) { - return true; //all good, no class type requested + return true; // All good, no class type requested. } StringName obj_class = object->get_class_name(); @@ -110,12 +113,12 @@ struct ContainerTypeValidate { } if (script.is_null()) { - return true; //all good + return true; // All good, no script requested. } Ref<Script> other_script = object->get_script(); - //check base script.. + // Check base script.. ERR_FAIL_COND_V_MSG(other_script.is_null(), false, "Attempted to " + String(p_operation) + " an object into a " + String(where) + ", that does not inherit from '" + String(script->get_class_name()) + "'."); ERR_FAIL_COND_V_MSG(!other_script->inherits_script(script), false, "Attempted to " + String(p_operation) + " an object into a " + String(where) + ", that does not inherit from '" + String(script->get_class_name()) + "'."); diff --git a/core/variant/dictionary.cpp b/core/variant/dictionary.cpp index d9f4359ee5..c1cb782a57 100644 --- a/core/variant/dictionary.cpp +++ b/core/variant/dictionary.cpp @@ -195,6 +195,15 @@ bool Dictionary::has_all(const Array &p_keys) const { return true; } +Variant Dictionary::find_key(const Variant &p_value) const { + for (const KeyValue<Variant, Variant> &E : _p->variant_map) { + if (E.value == p_value) { + return E.key; + } + } + return Variant(); +} + bool Dictionary::erase(const Variant &p_key) { ERR_FAIL_COND_V_MSG(_p->read_only, false, "Dictionary is in read-only state."); if (p_key.get_type() == Variant::STRING_NAME) { diff --git a/core/variant/dictionary.h b/core/variant/dictionary.h index 2632893e8d..d9c9db56cf 100644 --- a/core/variant/dictionary.h +++ b/core/variant/dictionary.h @@ -66,6 +66,7 @@ public: bool has(const Variant &p_key) const; bool has_all(const Array &p_keys) const; + Variant find_key(const Variant &p_value) const; bool erase(const Variant &p_key); diff --git a/core/variant/type_info.h b/core/variant/type_info.h index 7372c60754..e355053296 100644 --- a/core/variant/type_info.h +++ b/core/variant/type_info.h @@ -276,7 +276,7 @@ inline String enum_qualified_name_to_class_info_name(const String &p_qualified_n template <typename T> inline StringName __constant_get_enum_name(T param, const String &p_constant) { - if (GetTypeInfo<T>::VARIANT_TYPE == Variant::NIL) { + if constexpr (GetTypeInfo<T>::VARIANT_TYPE == Variant::NIL) { ERR_PRINT("Missing VARIANT_ENUM_CAST for constant's enum: " + p_constant); } return GetTypeInfo<T>::get_class_info().class_name; @@ -284,14 +284,14 @@ inline StringName __constant_get_enum_name(T param, const String &p_constant) { template <class T> class BitField { - uint32_t value = 0; + int64_t value = 0; public: _FORCE_INLINE_ void set_flag(T p_flag) { value |= p_flag; } _FORCE_INLINE_ bool has_flag(T p_flag) const { return value & p_flag; } _FORCE_INLINE_ void clear_flag(T p_flag) { return value &= ~p_flag; } - _FORCE_INLINE_ BitField(uint32_t p_value) { value = p_value; } - _FORCE_INLINE_ operator uint32_t() const { return value; } + _FORCE_INLINE_ BitField(int64_t p_value) { value = p_value; } + _FORCE_INLINE_ operator int64_t() const { return value; } _FORCE_INLINE_ operator Variant() const { return value; } }; diff --git a/core/variant/variant.cpp b/core/variant/variant.cpp index a5bc6c229d..c1166a0a14 100644 --- a/core/variant/variant.cpp +++ b/core/variant/variant.cpp @@ -47,146 +47,126 @@ String Variant::get_type_name(Variant::Type p_type) { switch (p_type) { case NIL: { return "Nil"; - } break; + } - // atomic types + // Atomic types. case BOOL: { return "bool"; - } break; + } case INT: { return "int"; - - } break; + } case FLOAT: { return "float"; - - } break; + } case STRING: { return "String"; - } break; + } - // math types + // Math types. case VECTOR2: { return "Vector2"; - } break; + } case VECTOR2I: { return "Vector2i"; - } break; + } case RECT2: { return "Rect2"; - } break; + } case RECT2I: { return "Rect2i"; - } break; + } case TRANSFORM2D: { return "Transform2D"; - } break; + } case VECTOR3: { return "Vector3"; - } break; + } case VECTOR3I: { return "Vector3i"; - } break; + } case VECTOR4: { return "Vector4"; - } break; + } case VECTOR4I: { return "Vector4i"; - } break; + } case PLANE: { return "Plane"; - - } break; + } case AABB: { return "AABB"; - } break; + } case QUATERNION: { return "Quaternion"; - - } break; + } case BASIS: { return "Basis"; - - } break; + } case TRANSFORM3D: { return "Transform3D"; - - } break; + } case PROJECTION: { return "Projection"; + } - } break; - - // misc types + // Miscellaneous types. case COLOR: { return "Color"; - - } break; + } case RID: { return "RID"; - } break; + } case OBJECT: { return "Object"; - } break; + } case CALLABLE: { return "Callable"; - } break; + } case SIGNAL: { return "Signal"; - } break; + } case STRING_NAME: { return "StringName"; - - } break; + } case NODE_PATH: { return "NodePath"; - - } break; + } case DICTIONARY: { return "Dictionary"; - - } break; + } case ARRAY: { return "Array"; + } - } break; - - // arrays + // Arrays. case PACKED_BYTE_ARRAY: { return "PackedByteArray"; - - } break; + } case PACKED_INT32_ARRAY: { return "PackedInt32Array"; - - } break; + } case PACKED_INT64_ARRAY: { return "PackedInt64Array"; - - } break; + } case PACKED_FLOAT32_ARRAY: { return "PackedFloat32Array"; - - } break; + } case PACKED_FLOAT64_ARRAY: { return "PackedFloat64Array"; - - } break; + } case PACKED_STRING_ARRAY: { return "PackedStringArray"; - } break; + } case PACKED_VECTOR2_ARRAY: { return "PackedVector2Array"; - - } break; + } case PACKED_VECTOR3_ARRAY: { return "PackedVector3Array"; - - } break; + } case PACKED_COLOR_ARRAY: { return "PackedColorArray"; - - } break; + } default: { } } @@ -880,157 +860,126 @@ bool Variant::is_zero() const { switch (type) { case NIL: { return true; - } break; + } - // atomic types + // Atomic types. case BOOL: { return !(_data._bool); - } break; + } case INT: { return _data._int == 0; - - } break; + } case FLOAT: { return _data._float == 0; - - } break; + } case STRING: { return *reinterpret_cast<const String *>(_data._mem) == String(); + } - } break; - - // math types + // Math types. case VECTOR2: { return *reinterpret_cast<const Vector2 *>(_data._mem) == Vector2(); - - } break; + } case VECTOR2I: { return *reinterpret_cast<const Vector2i *>(_data._mem) == Vector2i(); - - } break; + } case RECT2: { return *reinterpret_cast<const Rect2 *>(_data._mem) == Rect2(); - - } break; + } case RECT2I: { return *reinterpret_cast<const Rect2i *>(_data._mem) == Rect2i(); - - } break; + } case TRANSFORM2D: { return *_data._transform2d == Transform2D(); - - } break; + } case VECTOR3: { return *reinterpret_cast<const Vector3 *>(_data._mem) == Vector3(); - - } break; + } case VECTOR3I: { return *reinterpret_cast<const Vector3i *>(_data._mem) == Vector3i(); - - } break; + } case VECTOR4: { return *reinterpret_cast<const Vector4 *>(_data._mem) == Vector4(); - - } break; + } case VECTOR4I: { return *reinterpret_cast<const Vector4i *>(_data._mem) == Vector4i(); - - } break; + } case PLANE: { return *reinterpret_cast<const Plane *>(_data._mem) == Plane(); - - } break; + } case AABB: { return *_data._aabb == ::AABB(); - } break; + } case QUATERNION: { return *reinterpret_cast<const Quaternion *>(_data._mem) == Quaternion(); - - } break; + } case BASIS: { return *_data._basis == Basis(); - - } break; + } case TRANSFORM3D: { return *_data._transform3d == Transform3D(); - - } break; + } case PROJECTION: { return *_data._projection == Projection(); + } - } break; - - // misc types + // Miscellaneous types. case COLOR: { return *reinterpret_cast<const Color *>(_data._mem) == Color(); - - } break; + } case RID: { return *reinterpret_cast<const ::RID *>(_data._mem) == ::RID(); - } break; + } case OBJECT: { return _get_obj().obj == nullptr; - } break; + } case CALLABLE: { return reinterpret_cast<const Callable *>(_data._mem)->is_null(); - } break; + } case SIGNAL: { return reinterpret_cast<const Signal *>(_data._mem)->is_null(); - } break; + } case STRING_NAME: { return *reinterpret_cast<const StringName *>(_data._mem) != StringName(); - - } break; + } case NODE_PATH: { return reinterpret_cast<const NodePath *>(_data._mem)->is_empty(); - - } break; + } case DICTIONARY: { return reinterpret_cast<const Dictionary *>(_data._mem)->is_empty(); - - } break; + } case ARRAY: { return reinterpret_cast<const Array *>(_data._mem)->is_empty(); + } - } break; - - // arrays + // Arrays. case PACKED_BYTE_ARRAY: { return PackedArrayRef<uint8_t>::get_array(_data.packed_array).size() == 0; - - } break; + } case PACKED_INT32_ARRAY: { return PackedArrayRef<int32_t>::get_array(_data.packed_array).size() == 0; - - } break; + } case PACKED_INT64_ARRAY: { return PackedArrayRef<int64_t>::get_array(_data.packed_array).size() == 0; - - } break; + } case PACKED_FLOAT32_ARRAY: { return PackedArrayRef<float>::get_array(_data.packed_array).size() == 0; - - } break; + } case PACKED_FLOAT64_ARRAY: { return PackedArrayRef<double>::get_array(_data.packed_array).size() == 0; - - } break; + } case PACKED_STRING_ARRAY: { return PackedArrayRef<String>::get_array(_data.packed_array).size() == 0; - - } break; + } case PACKED_VECTOR2_ARRAY: { return PackedArrayRef<Vector2>::get_array(_data.packed_array).size() == 0; - - } break; + } case PACKED_VECTOR3_ARRAY: { return PackedArrayRef<Vector3>::get_array(_data.packed_array).size() == 0; - - } break; + } case PACKED_COLOR_ARRAY: { return PackedArrayRef<Color>::get_array(_data.packed_array).size() == 0; - - } break; + } default: { } } @@ -1042,67 +991,54 @@ bool Variant::is_one() const { switch (type) { case NIL: { return true; - } break; + } - // atomic types case BOOL: { return _data._bool; - } break; + } case INT: { return _data._int == 1; - - } break; + } case FLOAT: { return _data._float == 1; + } - } break; case VECTOR2: { return *reinterpret_cast<const Vector2 *>(_data._mem) == Vector2(1, 1); - - } break; + } case VECTOR2I: { return *reinterpret_cast<const Vector2i *>(_data._mem) == Vector2i(1, 1); - - } break; + } case RECT2: { return *reinterpret_cast<const Rect2 *>(_data._mem) == Rect2(1, 1, 1, 1); - - } break; + } case RECT2I: { return *reinterpret_cast<const Rect2i *>(_data._mem) == Rect2i(1, 1, 1, 1); - - } break; + } case VECTOR3: { return *reinterpret_cast<const Vector3 *>(_data._mem) == Vector3(1, 1, 1); - - } break; + } case VECTOR3I: { return *reinterpret_cast<const Vector3i *>(_data._mem) == Vector3i(1, 1, 1); - - } break; + } case VECTOR4: { return *reinterpret_cast<const Vector4 *>(_data._mem) == Vector4(1, 1, 1, 1); - - } break; + } case VECTOR4I: { return *reinterpret_cast<const Vector4i *>(_data._mem) == Vector4i(1, 1, 1, 1); - - } break; + } case PLANE: { return *reinterpret_cast<const Plane *>(_data._mem) == Plane(1, 1, 1, 1); + } - } break; case COLOR: { return *reinterpret_cast<const Color *>(_data._mem) == Color(1, 1, 1, 1); - - } break; + } default: { return !is_zero(); } } - - return false; } bool Variant::is_null() const { @@ -1135,10 +1071,10 @@ void Variant::reference(const Variant &p_variant) { switch (p_variant.type) { case NIL: { - // none + // None. } break; - // atomic types + // Atomic types. case BOOL: { _data._bool = p_variant._data._bool; } break; @@ -1152,7 +1088,7 @@ void Variant::reference(const Variant &p_variant) { memnew_placement(_data._mem, String(*reinterpret_cast<const String *>(p_variant._data._mem))); } break; - // math types + // Math types. case VECTOR2: { memnew_placement(_data._mem, Vector2(*reinterpret_cast<const Vector2 *>(p_variant._data._mem))); } break; @@ -1204,10 +1140,9 @@ void Variant::reference(const Variant &p_variant) { memnew_placement(_data._projection, Projection(*p_variant._data._projection)); } break; - // misc types + // Miscellaneous types. case COLOR: { memnew_placement(_data._mem, Color(*reinterpret_cast<const Color *>(p_variant._data._mem))); - } break; case RID: { memnew_placement(_data._mem, ::RID(*reinterpret_cast<const ::RID *>(p_variant._data._mem))); @@ -1226,7 +1161,6 @@ void Variant::reference(const Variant &p_variant) { _get_obj().obj = const_cast<Object *>(p_variant._get_obj().obj); _get_obj().id = p_variant._get_obj().id; - } break; case CALLABLE: { memnew_placement(_data._mem, Callable(*reinterpret_cast<const Callable *>(p_variant._data._mem))); @@ -1236,84 +1170,71 @@ void Variant::reference(const Variant &p_variant) { } break; case STRING_NAME: { memnew_placement(_data._mem, StringName(*reinterpret_cast<const StringName *>(p_variant._data._mem))); - } break; case NODE_PATH: { memnew_placement(_data._mem, NodePath(*reinterpret_cast<const NodePath *>(p_variant._data._mem))); - } break; case DICTIONARY: { memnew_placement(_data._mem, Dictionary(*reinterpret_cast<const Dictionary *>(p_variant._data._mem))); - } break; case ARRAY: { memnew_placement(_data._mem, Array(*reinterpret_cast<const Array *>(p_variant._data._mem))); - } break; - // arrays + // Arrays. case PACKED_BYTE_ARRAY: { _data.packed_array = static_cast<PackedArrayRef<uint8_t> *>(p_variant._data.packed_array)->reference(); if (!_data.packed_array) { _data.packed_array = PackedArrayRef<uint8_t>::create(); } - } break; case PACKED_INT32_ARRAY: { _data.packed_array = static_cast<PackedArrayRef<int32_t> *>(p_variant._data.packed_array)->reference(); if (!_data.packed_array) { _data.packed_array = PackedArrayRef<int32_t>::create(); } - } break; case PACKED_INT64_ARRAY: { _data.packed_array = static_cast<PackedArrayRef<int64_t> *>(p_variant._data.packed_array)->reference(); if (!_data.packed_array) { _data.packed_array = PackedArrayRef<int64_t>::create(); } - } break; case PACKED_FLOAT32_ARRAY: { _data.packed_array = static_cast<PackedArrayRef<float> *>(p_variant._data.packed_array)->reference(); if (!_data.packed_array) { _data.packed_array = PackedArrayRef<float>::create(); } - } break; case PACKED_FLOAT64_ARRAY: { _data.packed_array = static_cast<PackedArrayRef<double> *>(p_variant._data.packed_array)->reference(); if (!_data.packed_array) { _data.packed_array = PackedArrayRef<double>::create(); } - } break; case PACKED_STRING_ARRAY: { _data.packed_array = static_cast<PackedArrayRef<String> *>(p_variant._data.packed_array)->reference(); if (!_data.packed_array) { _data.packed_array = PackedArrayRef<String>::create(); } - } break; case PACKED_VECTOR2_ARRAY: { _data.packed_array = static_cast<PackedArrayRef<Vector2> *>(p_variant._data.packed_array)->reference(); if (!_data.packed_array) { _data.packed_array = PackedArrayRef<Vector2>::create(); } - } break; case PACKED_VECTOR3_ARRAY: { _data.packed_array = static_cast<PackedArrayRef<Vector3> *>(p_variant._data.packed_array)->reference(); if (!_data.packed_array) { _data.packed_array = PackedArrayRef<Vector3>::create(); } - } break; case PACKED_COLOR_ARRAY: { _data.packed_array = static_cast<PackedArrayRef<Color> *>(p_variant._data.packed_array)->reference(); if (!_data.packed_array) { _data.packed_array = PackedArrayRef<Color>::create(); } - } break; default: { } @@ -1333,6 +1254,7 @@ void Variant::zero() { case FLOAT: this->_data._float = 0; break; + case VECTOR2: *reinterpret_cast<Vector2 *>(this->_data._mem) = Vector2(); break; @@ -1363,9 +1285,11 @@ void Variant::zero() { case QUATERNION: *reinterpret_cast<Quaternion *>(this->_data._mem) = Quaternion(); break; + case COLOR: *reinterpret_cast<Color *>(this->_data._mem) = Color(); break; + default: this->clear(); break; @@ -1377,15 +1301,8 @@ void Variant::_clear_internal() { case STRING: { reinterpret_cast<String *>(_data._mem)->~String(); } break; - /* - // no point, they don't allocate memory - VECTOR3, - PLANE, - QUATERNION, - COLOR, - VECTOR2, - RECT2 - */ + + // Math types. case TRANSFORM2D: { if (_data._transform2d) { _data._transform2d->~Transform2D(); @@ -1421,7 +1338,8 @@ void Variant::_clear_internal() { _data._projection = nullptr; } } break; - // misc types + + // Miscellaneous types. case STRING_NAME: { reinterpret_cast<StringName *>(_data._mem)->~StringName(); } break; @@ -1430,7 +1348,7 @@ void Variant::_clear_internal() { } break; case OBJECT: { if (_get_obj().id.is_ref_counted()) { - //we are safe that there is a reference here + // We are safe that there is a reference here. RefCounted *ref_counted = static_cast<RefCounted *>(_get_obj().obj); if (ref_counted->unreference()) { memdelete(ref_counted); @@ -1440,8 +1358,8 @@ void Variant::_clear_internal() { _get_obj().id = ObjectID(); } break; case RID: { - // not much need probably - // Can't seem to use destructor + scoping operator, so hack. + // Not much need probably. + // HACK: Can't seem to use destructor + scoping operator, so hack. typedef ::RID RID_Class; reinterpret_cast<RID_Class *>(_data._mem)->~RID_Class(); } break; @@ -1457,7 +1375,8 @@ void Variant::_clear_internal() { case ARRAY: { reinterpret_cast<Array *>(_data._mem)->~Array(); } break; - // arrays + + // Arrays. case PACKED_BYTE_ARRAY: { PackedArrayRefBase::destroy(_data.packed_array); } break; @@ -1486,7 +1405,9 @@ void Variant::_clear_internal() { PackedArrayRefBase::destroy(_data.packed_array); } break; default: { - } /* not needed */ + // Not needed, there is no point. The following do not allocate memory: + // VECTOR2, VECTOR3, RECT2, PLANE, QUATERNION, COLOR. + } } } @@ -1787,7 +1708,7 @@ String stringify_vector(const T &vec, int recursion_count) { String Variant::stringify(int recursion_count) const { switch (type) { case NIL: - return "null"; + return "<null>"; case BOOL: return _data._bool ? "true" : "false"; case INT: @@ -1835,11 +1756,13 @@ String Variant::stringify(int recursion_count) const { case DICTIONARY: { const Dictionary &d = *reinterpret_cast<const Dictionary *>(_data._mem); if (recursion_count > MAX_RECURSION) { - ERR_PRINT("Max recursion reached"); - return "{...}"; + ERR_PRINT("Maximum dictionary recursion reached!"); + return "{ ... }"; } - String str("{"); + // Add leading and trailing space to Dictionary printing. This distinguishes it + // from array printing on fonts that have similar-looking {} and [] characters. + String str("{ "); List<Variant> keys; d.get_key_list(&keys); @@ -1858,84 +1781,79 @@ String Variant::stringify(int recursion_count) const { if (i > 0) { str += ", "; } - str += pairs[i].key + ":" + pairs[i].value; + str += pairs[i].key + ": " + pairs[i].value; } - str += "}"; + str += " }"; return str; - } break; + } case PACKED_VECTOR2_ARRAY: { return stringify_vector(operator Vector<Vector2>(), recursion_count); - } break; + } case PACKED_VECTOR3_ARRAY: { return stringify_vector(operator Vector<Vector3>(), recursion_count); - } break; + } case PACKED_COLOR_ARRAY: { return stringify_vector(operator Vector<Color>(), recursion_count); - } break; + } case PACKED_STRING_ARRAY: { return stringify_vector(operator Vector<String>(), recursion_count); - } break; + } case PACKED_BYTE_ARRAY: { return stringify_vector(operator Vector<uint8_t>(), recursion_count); - } break; + } case PACKED_INT32_ARRAY: { return stringify_vector(operator Vector<int32_t>(), recursion_count); - } break; + } case PACKED_INT64_ARRAY: { return stringify_vector(operator Vector<int64_t>(), recursion_count); - } break; + } case PACKED_FLOAT32_ARRAY: { return stringify_vector(operator Vector<float>(), recursion_count); - } break; + } case PACKED_FLOAT64_ARRAY: { return stringify_vector(operator Vector<double>(), recursion_count); - } break; + } case ARRAY: { Array arr = operator Array(); if (recursion_count > MAX_RECURSION) { - ERR_PRINT("Max recursion reached"); + ERR_PRINT("Maximum array recursion reached!"); return "[...]"; } return stringify_vector(arr, recursion_count); - - } break; + } case OBJECT: { if (_get_obj().obj) { if (!_get_obj().id.is_ref_counted() && ObjectDB::get_instance(_get_obj().id) == nullptr) { - return "[Freed Object]"; + return "<Freed Object>"; } return _get_obj().obj->to_string(); } else { - return "[Object:null]"; + return "<Object#null>"; } - - } break; + } case CALLABLE: { const Callable &c = *reinterpret_cast<const Callable *>(_data._mem); return c; - } break; + } case SIGNAL: { const Signal &s = *reinterpret_cast<const Signal *>(_data._mem); return s; - } break; + } case RID: { const ::RID &s = *reinterpret_cast<const ::RID *>(_data._mem); return "RID(" + itos(s.get_id()) + ")"; - } break; + } default: { - return "[" + get_type_name(type) + "]"; + return "<" + get_type_name(type) + ">"; } } - - return ""; } String Variant::to_json_string() const { - JSON json; - return json.stringify(*this); + return JSON::stringify(*this); } Variant::operator Vector2() const { @@ -3121,22 +3039,22 @@ uint32_t Variant::recursive_hash(int recursion_count) const { case PROJECTION: { uint32_t h = HASH_MURMUR3_SEED; const Projection &t = *_data._projection; - h = hash_murmur3_one_real(t.matrix[0].x, h); - h = hash_murmur3_one_real(t.matrix[0].y, h); - h = hash_murmur3_one_real(t.matrix[0].z, h); - h = hash_murmur3_one_real(t.matrix[0].w, h); - h = hash_murmur3_one_real(t.matrix[1].x, h); - h = hash_murmur3_one_real(t.matrix[1].y, h); - h = hash_murmur3_one_real(t.matrix[1].z, h); - h = hash_murmur3_one_real(t.matrix[1].w, h); - h = hash_murmur3_one_real(t.matrix[2].x, h); - h = hash_murmur3_one_real(t.matrix[2].y, h); - h = hash_murmur3_one_real(t.matrix[2].z, h); - h = hash_murmur3_one_real(t.matrix[2].w, h); - h = hash_murmur3_one_real(t.matrix[3].x, h); - h = hash_murmur3_one_real(t.matrix[3].y, h); - h = hash_murmur3_one_real(t.matrix[3].z, h); - h = hash_murmur3_one_real(t.matrix[3].w, h); + h = hash_murmur3_one_real(t.columns[0].x, h); + h = hash_murmur3_one_real(t.columns[0].y, h); + h = hash_murmur3_one_real(t.columns[0].z, h); + h = hash_murmur3_one_real(t.columns[0].w, h); + h = hash_murmur3_one_real(t.columns[1].x, h); + h = hash_murmur3_one_real(t.columns[1].y, h); + h = hash_murmur3_one_real(t.columns[1].z, h); + h = hash_murmur3_one_real(t.columns[1].w, h); + h = hash_murmur3_one_real(t.columns[2].x, h); + h = hash_murmur3_one_real(t.columns[2].y, h); + h = hash_murmur3_one_real(t.columns[2].z, h); + h = hash_murmur3_one_real(t.columns[2].w, h); + h = hash_murmur3_one_real(t.columns[3].x, h); + h = hash_murmur3_one_real(t.columns[3].y, h); + h = hash_murmur3_one_real(t.columns[3].z, h); + h = hash_murmur3_one_real(t.columns[3].w, h); return hash_fmix32(h); } break; // misc types @@ -3507,7 +3425,7 @@ bool Variant::hash_compare(const Variant &p_variant, int recursion_count) const const Projection *r = p_variant._data._projection; for (int i = 0; i < 4; i++) { - if (!(hash_compare_vector4(l->matrix[i], r->matrix[i]))) { + if (!(hash_compare_vector4(l->columns[i], r->columns[i]))) { return false; } } @@ -3571,8 +3489,6 @@ bool Variant::hash_compare(const Variant &p_variant, int recursion_count) const evaluate(OP_EQUAL, *this, p_variant, r, v); return r; } - - return false; } bool Variant::is_ref_counted() const { @@ -3693,16 +3609,16 @@ String Variant::get_call_error_text(Object *p_base, const StringName &p_method, if (ce.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) { int errorarg = ce.argument; if (p_argptrs) { - err_text = "Cannot convert argument " + itos(errorarg + 1) + " from " + Variant::get_type_name(p_argptrs[errorarg]->get_type()) + " to " + Variant::get_type_name(Variant::Type(ce.expected)) + "."; + err_text = "Cannot convert argument " + itos(errorarg + 1) + " from " + Variant::get_type_name(p_argptrs[errorarg]->get_type()) + " to " + Variant::get_type_name(Variant::Type(ce.expected)); } else { - err_text = "Cannot convert argument " + itos(errorarg + 1) + " from [missing argptr, type unknown] to " + Variant::get_type_name(Variant::Type(ce.expected)) + "."; + err_text = "Cannot convert argument " + itos(errorarg + 1) + " from [missing argptr, type unknown] to " + Variant::get_type_name(Variant::Type(ce.expected)); } } else if (ce.error == Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) { - err_text = "Method expected " + itos(ce.argument) + " arguments, but called with " + itos(p_argcount) + "."; + err_text = "Method expected " + itos(ce.argument) + " arguments, but called with " + itos(p_argcount); } else if (ce.error == Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) { - err_text = "Method expected " + itos(ce.argument) + " arguments, but called with " + itos(p_argcount) + "."; + err_text = "Method expected " + itos(ce.argument) + " arguments, but called with " + itos(p_argcount); } else if (ce.error == Callable::CallError::CALL_ERROR_INVALID_METHOD) { - err_text = "Method not found."; + err_text = "Method not found"; } else if (ce.error == Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL) { err_text = "Instance is null"; } else if (ce.error == Callable::CallError::CALL_ERROR_METHOD_NOT_CONST) { @@ -3727,36 +3643,6 @@ String Variant::get_callable_error_text(const Callable &p_callable, const Varian return get_call_error_text(p_callable.get_object(), p_callable.get_method(), p_argptrs, p_argcount, ce); } -String vformat(const String &p_text, const Variant &p1, const Variant &p2, const Variant &p3, const Variant &p4, const Variant &p5) { - Array args; - if (p1.get_type() != Variant::NIL) { - args.push_back(p1); - - if (p2.get_type() != Variant::NIL) { - args.push_back(p2); - - if (p3.get_type() != Variant::NIL) { - args.push_back(p3); - - if (p4.get_type() != Variant::NIL) { - args.push_back(p4); - - if (p5.get_type() != Variant::NIL) { - args.push_back(p5); - } - } - } - } - } - - bool error = false; - String fmt = p_text.sprintf(args, &error); - - ERR_FAIL_COND_V_MSG(error, String(), fmt); - - return fmt; -} - void Variant::register_types() { _register_variant_operators(); _register_variant_methods(); diff --git a/core/variant/variant.h b/core/variant/variant.h index 212f94a9a8..c5be609184 100644 --- a/core/variant/variant.h +++ b/core/variant/variant.h @@ -490,6 +490,7 @@ public: } // Only enum classes that need to be bound need this to be defined. + VARIANT_ENUM_CLASS_CONSTRUCTOR(EulerOrder) VARIANT_ENUM_CLASS_CONSTRUCTOR(JoyAxis) VARIANT_ENUM_CLASS_CONSTRUCTOR(JoyButton) VARIANT_ENUM_CLASS_CONSTRUCTOR(Key) @@ -552,9 +553,6 @@ public: void zero(); Variant duplicate(bool p_deep = false) const; Variant recursive_duplicate(bool p_deep, int recursion_count) const; - static void blend(const Variant &a, const Variant &b, float c, Variant &r_dst); - static void interpolate(const Variant &a, const Variant &b, float c, Variant &r_dst); - static void sub(const Variant &a, const Variant &b, Variant &r_dst); /* Built-In Methods */ @@ -807,7 +805,22 @@ const Variant::ObjData &Variant::_get_obj() const { return *reinterpret_cast<const ObjData *>(&_data._mem[0]); } -String vformat(const String &p_text, const Variant &p1 = Variant(), const Variant &p2 = Variant(), const Variant &p3 = Variant(), const Variant &p4 = Variant(), const Variant &p5 = Variant()); +template <typename... VarArgs> +String vformat(const String &p_text, const VarArgs... p_args) { + Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported. + Array args_array; + args_array.resize(sizeof...(p_args)); + for (uint32_t i = 0; i < sizeof...(p_args); i++) { + args_array[i] = args[i]; + } + + bool error = false; + String fmt = p_text.sprintf(args_array, &error); + + ERR_FAIL_COND_V_MSG(error, String(), fmt); + + return fmt; +} template <typename... VarArgs> Callable Callable::bind(VarArgs... p_args) { diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp index eba12b68bb..a231a956bf 100644 --- a/core/variant/variant_call.cpp +++ b/core/variant/variant_call.cpp @@ -1506,8 +1506,11 @@ static void _register_variant_builtin_methods() { bind_method(String, repeat, sarray("count"), varray()); bind_method(String, insert, sarray("position", "what"), varray()); bind_method(String, capitalize, sarray(), varray()); - bind_method(String, split, sarray("delimiter", "allow_empty", "maxsplit"), varray(true, 0)); - bind_method(String, rsplit, sarray("delimiter", "allow_empty", "maxsplit"), varray(true, 0)); + bind_method(String, to_camel_case, sarray(), varray()); + bind_method(String, to_pascal_case, sarray(), varray()); + bind_method(String, to_snake_case, sarray(), varray()); + bind_method(String, split, sarray("delimiter", "allow_empty", "maxsplit"), varray("", true, 0)); + bind_method(String, rsplit, sarray("delimiter", "allow_empty", "maxsplit"), varray("", true, 0)); bind_method(String, split_floats, sarray("delimiter", "allow_empty"), varray(true)); bind_method(String, join, sarray("parts"), varray()); @@ -1523,7 +1526,7 @@ static void _register_variant_builtin_methods() { bind_method(String, rstrip, sarray("chars"), varray()); bind_method(String, get_extension, sarray(), varray()); bind_method(String, get_basename, sarray(), varray()); - bind_method(String, plus_file, sarray("file"), varray()); + bind_method(String, path_join, sarray("file"), varray()); bind_method(String, unicode_at, sarray("at"), varray()); bind_method(String, indent, sarray("prefix"), varray()); bind_method(String, dedent, sarray(), varray()); @@ -1602,12 +1605,15 @@ static void _register_variant_builtin_methods() { bind_method(Vector2, normalized, sarray(), varray()); bind_method(Vector2, is_normalized, sarray(), varray()); bind_method(Vector2, is_equal_approx, sarray("to"), varray()); + bind_method(Vector2, is_zero_approx, sarray(), varray()); + bind_method(Vector2, is_finite, sarray(), varray()); bind_method(Vector2, posmod, sarray("mod"), varray()); bind_method(Vector2, posmodv, sarray("modv"), varray()); bind_method(Vector2, project, sarray("b"), varray()); bind_method(Vector2, lerp, sarray("to", "weight"), varray()); bind_method(Vector2, slerp, sarray("to", "weight"), varray()); bind_method(Vector2, cubic_interpolate, sarray("b", "pre_a", "post_b", "weight"), varray()); + bind_method(Vector2, cubic_interpolate_in_time, sarray("b", "pre_a", "post_b", "weight", "b_t", "pre_a_t", "post_b_t"), varray()); bind_method(Vector2, bezier_interpolate, sarray("control_1", "control_2", "end", "t"), varray()); bind_method(Vector2, max_axis_index, sarray(), varray()); bind_method(Vector2, min_axis_index, sarray(), varray()); @@ -1645,9 +1651,10 @@ static void _register_variant_builtin_methods() { bind_method(Rect2, get_center, sarray(), varray()); bind_method(Rect2, get_area, sarray(), varray()); - bind_method(Rect2, has_no_area, sarray(), varray()); + bind_method(Rect2, has_area, sarray(), varray()); bind_method(Rect2, has_point, sarray("point"), varray()); bind_method(Rect2, is_equal_approx, sarray("rect"), varray()); + bind_method(Rect2, is_finite, sarray(), varray()); bind_method(Rect2, intersects, sarray("b", "include_borders"), varray(false)); bind_method(Rect2, encloses, sarray("b"), varray()); bind_method(Rect2, intersection, sarray("b"), varray()); @@ -1662,7 +1669,7 @@ static void _register_variant_builtin_methods() { bind_method(Rect2i, get_center, sarray(), varray()); bind_method(Rect2i, get_area, sarray(), varray()); - bind_method(Rect2i, has_no_area, sarray(), varray()); + bind_method(Rect2i, has_area, sarray(), varray()); bind_method(Rect2i, has_point, sarray("point"), varray()); bind_method(Rect2i, intersects, sarray("b"), varray()); bind_method(Rect2i, encloses, sarray("b"), varray()); @@ -1689,6 +1696,8 @@ static void _register_variant_builtin_methods() { bind_method(Vector3, normalized, sarray(), varray()); bind_method(Vector3, is_normalized, sarray(), varray()); bind_method(Vector3, is_equal_approx, sarray("to"), varray()); + bind_method(Vector3, is_zero_approx, sarray(), varray()); + bind_method(Vector3, is_finite, sarray(), varray()); bind_method(Vector3, inverse, sarray(), varray()); bind_method(Vector3, clamp, sarray("min", "max"), varray()); bind_method(Vector3, snapped, sarray("step"), varray()); @@ -1696,6 +1705,7 @@ static void _register_variant_builtin_methods() { bind_method(Vector3, lerp, sarray("to", "weight"), varray()); bind_method(Vector3, slerp, sarray("to", "weight"), varray()); bind_method(Vector3, cubic_interpolate, sarray("b", "pre_a", "post_b", "weight"), varray()); + bind_method(Vector3, cubic_interpolate_in_time, sarray("b", "pre_a", "post_b", "weight", "b_t", "pre_a_t", "post_b_t"), varray()); bind_method(Vector3, bezier_interpolate, sarray("control_1", "control_2", "end", "t"), varray()); bind_method(Vector3, move_toward, sarray("to", "delta"), varray()); bind_method(Vector3, dot, sarray("with"), varray()); @@ -1738,6 +1748,7 @@ static void _register_variant_builtin_methods() { bind_method(Vector4, round, sarray(), varray()); bind_method(Vector4, lerp, sarray("to", "weight"), varray()); bind_method(Vector4, cubic_interpolate, sarray("b", "pre_a", "post_b", "weight"), varray()); + bind_method(Vector4, cubic_interpolate_in_time, sarray("b", "pre_a", "post_b", "weight", "b_t", "pre_a_t", "post_b_t"), varray()); bind_method(Vector4, posmod, sarray("mod"), varray()); bind_method(Vector4, posmodv, sarray("modv"), varray()); bind_method(Vector4, snapped, sarray("step"), varray()); @@ -1750,6 +1761,8 @@ static void _register_variant_builtin_methods() { bind_method(Vector4, dot, sarray("with"), varray()); bind_method(Vector4, inverse, sarray(), varray()); bind_method(Vector4, is_equal_approx, sarray("with"), varray()); + bind_method(Vector4, is_zero_approx, sarray(), varray()); + bind_method(Vector4, is_finite, sarray(), varray()); /* Vector4i */ @@ -1766,6 +1779,7 @@ static void _register_variant_builtin_methods() { bind_method(Plane, normalized, sarray(), varray()); bind_method(Plane, center, sarray(), varray()); bind_method(Plane, is_equal_approx, sarray("to_plane"), varray()); + bind_method(Plane, is_finite, sarray(), varray()); bind_method(Plane, is_point_over, sarray("point"), varray()); bind_method(Plane, distance_to, sarray("point"), varray()); bind_method(Plane, has_point, sarray("point", "tolerance"), varray(CMP_EPSILON)); @@ -1781,6 +1795,7 @@ static void _register_variant_builtin_methods() { bind_method(Quaternion, normalized, sarray(), varray()); bind_method(Quaternion, is_normalized, sarray(), varray()); bind_method(Quaternion, is_equal_approx, sarray("to"), varray()); + bind_method(Quaternion, is_finite, sarray(), varray()); bind_method(Quaternion, inverse, sarray(), varray()); bind_method(Quaternion, log, sarray(), varray()); bind_method(Quaternion, exp, sarray(), varray()); @@ -1789,7 +1804,9 @@ static void _register_variant_builtin_methods() { bind_method(Quaternion, slerp, sarray("to", "weight"), varray()); bind_method(Quaternion, slerpni, sarray("to", "weight"), varray()); bind_method(Quaternion, spherical_cubic_interpolate, sarray("b", "pre_a", "post_b", "weight"), varray()); - bind_method(Quaternion, get_euler, sarray(), varray()); + bind_method(Quaternion, spherical_cubic_interpolate_in_time, sarray("b", "pre_a", "post_b", "weight", "b_t", "pre_a_t", "post_b_t"), varray()); + bind_method(Quaternion, get_euler, sarray("order"), varray((int64_t)EulerOrder::YXZ)); + bind_static_method(Quaternion, from_euler, sarray("euler"), varray()); bind_method(Quaternion, get_axis, sarray(), varray()); bind_method(Quaternion, get_angle, sarray(), varray()); @@ -1819,10 +1836,6 @@ static void _register_variant_builtin_methods() { bind_static_method(Color, hex64, sarray("hex"), varray()); bind_static_method(Color, html, sarray("rgba"), varray()); bind_static_method(Color, html_is_valid, sarray("color"), varray()); - bind_static_method(Color, find_named_color, sarray("name"), varray()); - bind_static_method(Color, get_named_color_count, sarray(), varray()); - bind_static_method(Color, get_named_color_name, sarray("idx"), varray()); - bind_static_method(Color, get_named_color, sarray("idx"), varray()); bind_static_method(Color, from_string, sarray("str", "default"), varray()); bind_static_method(Color, from_hsv, sarray("h", "s", "v", "alpha"), varray(1.0)); bind_static_method(Color, from_ok_hsl, sarray("h", "s", "l", "alpha"), varray(1.0)); @@ -1849,6 +1862,7 @@ static void _register_variant_builtin_methods() { /* Callable */ + bind_method(Callable, callv, sarray("arguments"), varray()); bind_method(Callable, is_null, sarray(), varray()); bind_method(Callable, is_custom, sarray(), varray()); bind_method(Callable, is_standard, sarray(), varray()); @@ -1898,6 +1912,7 @@ static void _register_variant_builtin_methods() { bind_method(Transform2D, basis_xform_inv, sarray("v"), varray()); bind_method(Transform2D, interpolate_with, sarray("xform", "weight"), varray()); bind_method(Transform2D, is_equal_approx, sarray("xform"), varray()); + bind_method(Transform2D, is_finite, sarray(), varray()); bind_method(Transform2D, set_rotation, sarray("rotation"), varray()); bind_method(Transform2D, set_scale, sarray("scale"), varray()); bind_method(Transform2D, set_skew, sarray("skew"), varray()); @@ -1912,27 +1927,28 @@ static void _register_variant_builtin_methods() { bind_methodv(Basis, rotated, static_cast<Basis (Basis::*)(const Vector3 &, real_t) const>(&Basis::rotated), sarray("axis", "angle"), varray()); bind_method(Basis, scaled, sarray("scale"), varray()); bind_method(Basis, get_scale, sarray(), varray()); - bind_method(Basis, get_euler, sarray("order"), varray(Basis::EULER_ORDER_YXZ)); + bind_method(Basis, get_euler, sarray("order"), varray((int64_t)EulerOrder::YXZ)); bind_method(Basis, tdotx, sarray("with"), varray()); bind_method(Basis, tdoty, sarray("with"), varray()); bind_method(Basis, tdotz, sarray("with"), varray()); - bind_method(Basis, get_orthogonal_index, sarray(), varray()); bind_method(Basis, slerp, sarray("to", "weight"), varray()); bind_method(Basis, is_equal_approx, sarray("b"), varray()); + bind_method(Basis, is_finite, sarray(), varray()); bind_method(Basis, get_rotation_quaternion, sarray(), varray()); bind_static_method(Basis, looking_at, sarray("target", "up"), varray(Vector3(0, 1, 0))); bind_static_method(Basis, from_scale, sarray("scale"), varray()); - bind_static_method(Basis, from_euler, sarray("euler", "order"), varray(Basis::EULER_ORDER_YXZ)); + bind_static_method(Basis, from_euler, sarray("euler", "order"), varray((int64_t)EulerOrder::YXZ)); /* AABB */ bind_method(AABB, abs, sarray(), varray()); bind_method(AABB, get_center, sarray(), varray()); bind_method(AABB, get_volume, sarray(), varray()); - bind_method(AABB, has_no_volume, sarray(), varray()); - bind_method(AABB, has_no_surface, sarray(), varray()); + bind_method(AABB, has_volume, sarray(), varray()); + bind_method(AABB, has_surface, sarray(), varray()); bind_method(AABB, has_point, sarray("point"), varray()); bind_method(AABB, is_equal_approx, sarray("aabb"), varray()); + bind_method(AABB, is_finite, sarray(), varray()); bind_method(AABB, intersects, sarray("with"), varray()); bind_method(AABB, encloses, sarray("with"), varray()); bind_method(AABB, intersects_plane, sarray("plane"), varray()); @@ -1963,9 +1979,9 @@ static void _register_variant_builtin_methods() { bind_method(Transform3D, translated, sarray("offset"), varray()); bind_method(Transform3D, translated_local, sarray("offset"), varray()); bind_method(Transform3D, looking_at, sarray("target", "up"), varray(Vector3(0, 1, 0))); - bind_method(Transform3D, spherical_interpolate_with, sarray("xform", "weight"), varray()); bind_method(Transform3D, interpolate_with, sarray("xform", "weight"), varray()); bind_method(Transform3D, is_equal_approx, sarray("xform"), varray()); + bind_method(Transform3D, is_finite, sarray(), varray()); /* Projection */ @@ -2009,6 +2025,7 @@ static void _register_variant_builtin_methods() { bind_method(Dictionary, merge, sarray("dictionary", "overwrite"), varray(false)); bind_method(Dictionary, has, sarray("key"), varray()); bind_method(Dictionary, has_all, sarray("keys"), varray()); + bind_method(Dictionary, find_key, sarray("value"), varray()); bind_method(Dictionary, erase, sarray("key"), varray()); bind_method(Dictionary, hash, sarray(), varray()); bind_method(Dictionary, keys, sarray(), varray()); @@ -2033,6 +2050,7 @@ static void _register_variant_builtin_methods() { bind_method(Array, erase, sarray("value"), varray()); bind_method(Array, front, sarray(), varray()); bind_method(Array, back, sarray(), varray()); + bind_method(Array, pick_random, sarray(), varray()); bind_method(Array, find, sarray("what", "from"), varray(0)); bind_method(Array, rfind, sarray("what", "from"), varray(-1)); bind_method(Array, find_last, sarray("value"), varray()); @@ -2056,6 +2074,14 @@ static void _register_variant_builtin_methods() { bind_method(Array, all, sarray("method"), varray()); bind_method(Array, max, sarray(), varray()); bind_method(Array, min, sarray(), varray()); + bind_method(Array, typed_assign, sarray("array"), varray()); + bind_method(Array, set_typed, sarray("type", "class_name", "script"), varray()); + bind_method(Array, is_typed, sarray(), varray()); + bind_method(Array, get_typed_builtin, sarray(), varray()); + bind_method(Array, get_typed_class_name, sarray(), varray()); + bind_method(Array, get_typed_script, sarray(), varray()); + bind_method(Array, set_read_only, sarray("enable"), varray()); + bind_method(Array, is_read_only, sarray(), varray()); /* Byte Array */ bind_method(PackedByteArray, size, sarray(), varray()); @@ -2408,25 +2434,11 @@ static void _register_variant_builtin_methods() { _VariantCall::add_variant_constant(Variant::VECTOR2I, "UP", Vector2i(0, -1)); _VariantCall::add_variant_constant(Variant::VECTOR2I, "DOWN", Vector2i(0, 1)); - _VariantCall::add_constant(Variant::BASIS, "EULER_ORDER_XYZ", Basis::EULER_ORDER_XYZ); - _VariantCall::add_constant(Variant::BASIS, "EULER_ORDER_XZY", Basis::EULER_ORDER_XZY); - _VariantCall::add_constant(Variant::BASIS, "EULER_ORDER_YXZ", Basis::EULER_ORDER_YXZ); - _VariantCall::add_constant(Variant::BASIS, "EULER_ORDER_YZX", Basis::EULER_ORDER_YZX); - _VariantCall::add_constant(Variant::BASIS, "EULER_ORDER_ZXY", Basis::EULER_ORDER_ZXY); - _VariantCall::add_constant(Variant::BASIS, "EULER_ORDER_ZYX", Basis::EULER_ORDER_ZYX); - - _VariantCall::add_enum_constant(Variant::BASIS, "EulerOrder", "EULER_ORDER_XYZ", Basis::EULER_ORDER_XYZ); - _VariantCall::add_enum_constant(Variant::BASIS, "EulerOrder", "EULER_ORDER_XZY", Basis::EULER_ORDER_XZY); - _VariantCall::add_enum_constant(Variant::BASIS, "EulerOrder", "EULER_ORDER_YXZ", Basis::EULER_ORDER_YXZ); - _VariantCall::add_enum_constant(Variant::BASIS, "EulerOrder", "EULER_ORDER_YZX", Basis::EULER_ORDER_YZX); - _VariantCall::add_enum_constant(Variant::BASIS, "EulerOrder", "EULER_ORDER_ZXY", Basis::EULER_ORDER_ZXY); - _VariantCall::add_enum_constant(Variant::BASIS, "EulerOrder", "EULER_ORDER_ZYX", Basis::EULER_ORDER_ZYX); - _VariantCall::add_variant_constant(Variant::TRANSFORM2D, "IDENTITY", Transform2D()); _VariantCall::add_variant_constant(Variant::TRANSFORM2D, "FLIP_X", Transform2D(-1, 0, 0, 1, 0, 0)); _VariantCall::add_variant_constant(Variant::TRANSFORM2D, "FLIP_Y", Transform2D(1, 0, 0, -1, 0, 0)); - Transform3D identity_transform = Transform3D(); + Transform3D identity_transform; Transform3D flip_x_transform = Transform3D(-1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0); Transform3D flip_y_transform = Transform3D(1, 0, 0, 0, -1, 0, 0, 0, 1, 0, 0, 0); Transform3D flip_z_transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, -1, 0, 0, 0); @@ -2435,7 +2447,7 @@ static void _register_variant_builtin_methods() { _VariantCall::add_variant_constant(Variant::TRANSFORM3D, "FLIP_Y", flip_y_transform); _VariantCall::add_variant_constant(Variant::TRANSFORM3D, "FLIP_Z", flip_z_transform); - Basis identity_basis = Basis(); + Basis identity_basis; Basis flip_x_basis = Basis(-1, 0, 0, 0, 1, 0, 0, 0, 1); Basis flip_y_basis = Basis(1, 0, 0, 0, -1, 0, 0, 0, 1); Basis flip_z_basis = Basis(1, 0, 0, 0, 1, 0, 0, 0, -1); diff --git a/core/variant/variant_construct.cpp b/core/variant/variant_construct.cpp index 3a0b6c1bb9..624a6f5e06 100644 --- a/core/variant/variant_construct.cpp +++ b/core/variant/variant_construct.cpp @@ -68,11 +68,13 @@ void Variant::_register_variant_constructors() { add_constructor<VariantConstructor<int64_t, int64_t>>(sarray("from")); add_constructor<VariantConstructor<int64_t, double>>(sarray("from")); add_constructor<VariantConstructor<int64_t, bool>>(sarray("from")); + add_constructor<VariantConstructorFromString<int64_t>>(sarray("from")); add_constructor<VariantConstructNoArgs<double>>(sarray()); add_constructor<VariantConstructor<double, double>>(sarray("from")); add_constructor<VariantConstructor<double, int64_t>>(sarray("from")); add_constructor<VariantConstructor<double, bool>>(sarray("from")); + add_constructor<VariantConstructorFromString<double>>(sarray("from")); add_constructor<VariantConstructNoArgs<String>>(sarray()); add_constructor<VariantConstructor<String, String>>(sarray("from")); @@ -141,7 +143,6 @@ void Variant::_register_variant_constructors() { add_constructor<VariantConstructor<Quaternion, Vector3, double>>(sarray("axis", "angle")); add_constructor<VariantConstructor<Quaternion, Vector3, Vector3>>(sarray("arc_from", "arc_to")); add_constructor<VariantConstructor<Quaternion, double, double, double, double>>(sarray("x", "y", "z", "w")); - add_constructor<VariantConstructor<Quaternion, Vector3>>(sarray("euler_yxz")); add_constructor<VariantConstructNoArgs<::AABB>>(sarray()); add_constructor<VariantConstructor<::AABB, ::AABB>>(sarray("from")); @@ -162,6 +163,7 @@ void Variant::_register_variant_constructors() { add_constructor<VariantConstructNoArgs<Projection>>(sarray()); add_constructor<VariantConstructor<Projection, Projection>>(sarray("from")); add_constructor<VariantConstructor<Projection, Transform3D>>(sarray("from")); + add_constructor<VariantConstructor<Projection, Vector4, Vector4, Vector4, Vector4>>(sarray("x_axis", "y_axis", "z_axis", "w_axis")); add_constructor<VariantConstructNoArgs<Color>>(sarray()); add_constructor<VariantConstructor<Color, Color>>(sarray("from")); @@ -199,6 +201,7 @@ void Variant::_register_variant_constructors() { add_constructor<VariantConstructNoArgs<Array>>(sarray()); add_constructor<VariantConstructor<Array, Array>>(sarray("from")); + add_constructor<VariantConstructorTypedArray>(sarray("base", "type", "class_name", "script")); add_constructor<VariantConstructorToArray<PackedByteArray>>(sarray("from")); add_constructor<VariantConstructorToArray<PackedInt32Array>>(sarray("from")); add_constructor<VariantConstructorToArray<PackedInt64Array>>(sarray("from")); diff --git a/core/variant/variant_construct.h b/core/variant/variant_construct.h index 58a0f34c1e..f52cc43914 100644 --- a/core/variant/variant_construct.h +++ b/core/variant/variant_construct.h @@ -222,6 +222,64 @@ public: } }; +template <class T> +class VariantConstructorFromString { +public: + static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) { + if (p_args[0]->get_type() != Variant::STRING) { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.argument = 0; + r_error.expected = Variant::STRING; + return; + } + + VariantTypeChanger<T>::change(&r_ret); + const String &src_str = *VariantGetInternalPtr<String>::get_ptr(p_args[0]); + + if (r_ret.get_type() == Variant::Type::INT) { + r_ret = src_str.to_int(); + } else if (r_ret.get_type() == Variant::Type::FLOAT) { + r_ret = src_str.to_float(); + } + } + + static inline void validated_construct(Variant *r_ret, const Variant **p_args) { + VariantTypeChanger<T>::change(r_ret); + const String &src_str = *VariantGetInternalPtr<String>::get_ptr(p_args[0]); + T ret = Variant(); + if (r_ret->get_type() == Variant::Type::INT) { + ret = src_str.to_int(); + } else if (r_ret->get_type() == Variant::Type::FLOAT) { + ret = src_str.to_float(); + } + *r_ret = ret; + } + + static void ptr_construct(void *base, const void **p_args) { + String src_str = PtrToArg<String>::convert(p_args[0]); + T dst_var = Variant(); + Variant type_test = Variant(dst_var); + if (type_test.get_type() == Variant::Type::INT) { + dst_var = src_str.to_int(); + } else if (type_test.get_type() == Variant::Type::FLOAT) { + dst_var = src_str.to_float(); + } + PtrConstruct<T>::construct(dst_var, base); + } + + static int get_argument_count() { + return 1; + } + + static Variant::Type get_argument_type(int p_arg) { + return Variant::STRING; + } + + static Variant::Type get_base_type() { + return GetTypeInfo<T>::VARIANT_TYPE; + } +}; + class VariantConstructorCallableArgs { public: static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) { @@ -336,6 +394,82 @@ public: } }; +class VariantConstructorTypedArray { +public: + static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) { + if (p_args[0]->get_type() != Variant::ARRAY) { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.argument = 0; + r_error.expected = Variant::ARRAY; + return; + } + + if (p_args[1]->get_type() != Variant::INT) { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.argument = 1; + r_error.expected = Variant::INT; + return; + } + + if (p_args[2]->get_type() != Variant::STRING_NAME) { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.argument = 2; + r_error.expected = Variant::STRING_NAME; + return; + } + + const Array &base_arr = *VariantGetInternalPtr<Array>::get_ptr(p_args[0]); + const uint32_t type = p_args[1]->operator uint32_t(); + const StringName &class_name = *VariantGetInternalPtr<StringName>::get_ptr(p_args[2]); + r_ret = Array(base_arr, type, class_name, *p_args[3]); + } + + static inline void validated_construct(Variant *r_ret, const Variant **p_args) { + const Array &base_arr = *VariantGetInternalPtr<Array>::get_ptr(p_args[0]); + const uint32_t type = p_args[1]->operator uint32_t(); + const StringName &class_name = *VariantGetInternalPtr<StringName>::get_ptr(p_args[2]); + *r_ret = Array(base_arr, type, class_name, *p_args[3]); + } + + static void ptr_construct(void *base, const void **p_args) { + const Array &base_arr = PtrToArg<Array>::convert(p_args[0]); + const uint32_t type = PtrToArg<uint32_t>::convert(p_args[1]); + const StringName &class_name = PtrToArg<StringName>::convert(p_args[2]); + const Variant &script = PtrToArg<Variant>::convert(p_args[3]); + Array dst_arr = Array(base_arr, type, class_name, script); + + PtrConstruct<Array>::construct(dst_arr, base); + } + + static int get_argument_count() { + return 4; + } + + static Variant::Type get_argument_type(int p_arg) { + switch (p_arg) { + case 0: { + return Variant::ARRAY; + } break; + case 1: { + return Variant::INT; + } break; + case 2: { + return Variant::STRING_NAME; + } break; + case 3: { + return Variant::NIL; + } break; + default: { + return Variant::NIL; + } break; + } + } + + static Variant::Type get_base_type() { + return Variant::ARRAY; + } +}; + template <class T> class VariantConstructorToArray { public: diff --git a/core/variant/variant_destruct.cpp b/core/variant/variant_destruct.cpp index ab8303f3ae..5117c33e2b 100644 --- a/core/variant/variant_destruct.cpp +++ b/core/variant/variant_destruct.cpp @@ -41,13 +41,8 @@ static void add_destructor() { void Variant::_register_variant_destructors() { add_destructor<VariantDestruct<String>>(); - add_destructor<VariantDestruct<Transform2D>>(); - add_destructor<VariantDestruct<::AABB>>(); - add_destructor<VariantDestruct<Basis>>(); - add_destructor<VariantDestruct<Transform3D>>(); add_destructor<VariantDestruct<StringName>>(); add_destructor<VariantDestruct<NodePath>>(); - add_destructor<VariantDestruct<::RID>>(); add_destructor<VariantDestruct<Callable>>(); add_destructor<VariantDestruct<Signal>>(); add_destructor<VariantDestruct<Dictionary>>(); diff --git a/core/variant/variant_destruct.h b/core/variant/variant_destruct.h index 5e3478635d..2730110c0f 100644 --- a/core/variant/variant_destruct.h +++ b/core/variant/variant_destruct.h @@ -50,13 +50,8 @@ struct VariantDestruct {}; } MAKE_PTRDESTRUCT(String); -MAKE_PTRDESTRUCT(Transform2D); -MAKE_PTRDESTRUCT(AABB); -MAKE_PTRDESTRUCT(Basis); -MAKE_PTRDESTRUCT(Transform3D); MAKE_PTRDESTRUCT(StringName); MAKE_PTRDESTRUCT(NodePath); -MAKE_PTRDESTRUCT(RID); MAKE_PTRDESTRUCT(Callable); MAKE_PTRDESTRUCT(Signal); MAKE_PTRDESTRUCT(Dictionary); diff --git a/core/variant/variant_internal.h b/core/variant/variant_internal.h index 874a183d29..94e7296a99 100644 --- a/core/variant/variant_internal.h +++ b/core/variant/variant_internal.h @@ -824,9 +824,9 @@ VARIANT_ACCESSOR_NUMBER(Vector4i::Axis) VARIANT_ACCESSOR_NUMBER(Projection::Planes) template <> -struct VariantInternalAccessor<Basis::EulerOrder> { - static _FORCE_INLINE_ Basis::EulerOrder get(const Variant *v) { return Basis::EulerOrder(*VariantInternal::get_int(v)); } - static _FORCE_INLINE_ void set(Variant *v, Basis::EulerOrder p_value) { *VariantInternal::get_int(v) = p_value; } +struct VariantInternalAccessor<EulerOrder> { + static _FORCE_INLINE_ EulerOrder get(const Variant *v) { return EulerOrder(*VariantInternal::get_int(v)); } + static _FORCE_INLINE_ void set(Variant *v, EulerOrder p_value) { *VariantInternal::get_int(v) = (int64_t)p_value; } }; template <> @@ -1047,7 +1047,7 @@ struct VariantInternalAccessor<PackedColorArray> { template <> struct VariantInternalAccessor<Object *> { static _FORCE_INLINE_ Object *get(const Variant *v) { return const_cast<Object *>(*VariantInternal::get_object(v)); } - static _FORCE_INLINE_ void set(Variant *v, const Object *p_value) { *VariantInternal::get_object(v) = const_cast<Object *>(p_value); } + static _FORCE_INLINE_ void set(Variant *v, const Object *p_value) { VariantInternal::object_assign(v, p_value); } }; template <> @@ -1532,27 +1532,4 @@ struct VariantTypeConstructor { } }; -template <> -struct VariantTypeConstructor<Object *> { - _FORCE_INLINE_ static void variant_from_type(void *p_variant, void *p_value) { - Variant *variant = reinterpret_cast<Variant *>(p_variant); - VariantInitializer<Object *>::init(variant); - Object *object = *(reinterpret_cast<Object **>(p_value)); - if (object) { - if (object->is_ref_counted()) { - if (!VariantInternal::initialize_ref(object)) { - return; - } - } - VariantInternalAccessor<Object *>::set(variant, object); - VariantInternalAccessor<ObjectID>::set(variant, object->get_instance_id()); - } - } - - _FORCE_INLINE_ static void type_from_variant(void *p_value, void *p_variant) { - Object **value = reinterpret_cast<Object **>(p_value); - *value = VariantInternalAccessor<Object *>::get(reinterpret_cast<Variant *>(p_variant)); - } -}; - #endif // VARIANT_INTERNAL_H diff --git a/core/variant/variant_op.cpp b/core/variant/variant_op.cpp index 6cca7955ae..25bc241e9b 100644 --- a/core/variant/variant_op.cpp +++ b/core/variant/variant_op.cpp @@ -240,8 +240,6 @@ void Variant::_register_variant_operators() { register_op<OperatorEvaluatorAdd<double, double, int64_t>>(Variant::OP_ADD, Variant::FLOAT, Variant::INT); register_op<OperatorEvaluatorAdd<double, double, double>>(Variant::OP_ADD, Variant::FLOAT, Variant::FLOAT); register_op<OperatorEvaluatorAdd<String, String, String>>(Variant::OP_ADD, Variant::STRING, Variant::STRING); - register_op<OperatorEvaluatorAdd<String, char32_t, String>>(Variant::OP_ADD, Variant::INT, Variant::STRING); - register_op<OperatorEvaluatorAdd<String, String, char32_t>>(Variant::OP_ADD, Variant::STRING, Variant::INT); register_op<OperatorEvaluatorAdd<Vector2, Vector2, Vector2>>(Variant::OP_ADD, Variant::VECTOR2, Variant::VECTOR2); register_op<OperatorEvaluatorAdd<Vector2i, Vector2i, Vector2i>>(Variant::OP_ADD, Variant::VECTOR2I, Variant::VECTOR2I); register_op<OperatorEvaluatorAdd<Vector3, Vector3, Vector3>>(Variant::OP_ADD, Variant::VECTOR3, Variant::VECTOR3); @@ -384,10 +382,6 @@ void Variant::_register_variant_operators() { register_op<OperatorEvaluatorDivNZ<Vector2, Vector2i, double>>(Variant::OP_DIVIDE, Variant::VECTOR2I, Variant::FLOAT); register_op<OperatorEvaluatorDivNZ<Vector2i, Vector2i, int64_t>>(Variant::OP_DIVIDE, Variant::VECTOR2I, Variant::INT); - register_op<OperatorEvaluatorDiv<Vector2, Vector2, Vector2>>(Variant::OP_DIVIDE, Variant::VECTOR2, Variant::VECTOR2); - register_op<OperatorEvaluatorDiv<Vector2, Vector2, double>>(Variant::OP_DIVIDE, Variant::VECTOR2, Variant::FLOAT); - register_op<OperatorEvaluatorDiv<Vector2, Vector2, int64_t>>(Variant::OP_DIVIDE, Variant::VECTOR2, Variant::INT); - register_op<OperatorEvaluatorDiv<Vector3, Vector3, Vector3>>(Variant::OP_DIVIDE, Variant::VECTOR3, Variant::VECTOR3); register_op<OperatorEvaluatorDiv<Vector3, Vector3, double>>(Variant::OP_DIVIDE, Variant::VECTOR3, Variant::FLOAT); register_op<OperatorEvaluatorDiv<Vector3, Vector3, int64_t>>(Variant::OP_DIVIDE, Variant::VECTOR3, Variant::INT); @@ -498,8 +492,6 @@ void Variant::_register_variant_operators() { register_op<OperatorEvaluatorBitXor<int64_t, int64_t, int64_t>>(Variant::OP_BIT_XOR, Variant::INT, Variant::INT); register_op<OperatorEvaluatorBitNeg<int64_t, int64_t>>(Variant::OP_BIT_NEGATE, Variant::INT, Variant::NIL); - register_op<OperatorEvaluatorBitNeg<int64_t, int64_t>>(Variant::OP_BIT_NEGATE, Variant::INT, Variant::NIL); - register_op<OperatorEvaluatorAlwaysTrue<Variant::OP_EQUAL, Variant::NIL, Variant::NIL>>(Variant::OP_EQUAL, Variant::NIL, Variant::NIL); register_op<OperatorEvaluatorEqual<bool, bool>>(Variant::OP_EQUAL, Variant::BOOL, Variant::BOOL); register_op<OperatorEvaluatorEqual<int64_t, int64_t>>(Variant::OP_EQUAL, Variant::INT, Variant::INT); @@ -567,6 +559,7 @@ void Variant::_register_variant_operators() { register_op<OperatorEvaluatorAlwaysFalse<Variant::OP_EQUAL, Variant::AABB, Variant::NIL>>(Variant::OP_EQUAL, Variant::AABB, Variant::NIL); register_op<OperatorEvaluatorAlwaysFalse<Variant::OP_EQUAL, Variant::BASIS, Variant::NIL>>(Variant::OP_EQUAL, Variant::BASIS, Variant::NIL); register_op<OperatorEvaluatorAlwaysFalse<Variant::OP_EQUAL, Variant::TRANSFORM3D, Variant::NIL>>(Variant::OP_EQUAL, Variant::TRANSFORM3D, Variant::NIL); + register_op<OperatorEvaluatorAlwaysFalse<Variant::OP_EQUAL, Variant::PROJECTION, Variant::NIL>>(Variant::OP_EQUAL, Variant::PROJECTION, Variant::NIL); register_op<OperatorEvaluatorAlwaysFalse<Variant::OP_EQUAL, Variant::COLOR, Variant::NIL>>(Variant::OP_EQUAL, Variant::COLOR, Variant::NIL); register_op<OperatorEvaluatorAlwaysFalse<Variant::OP_EQUAL, Variant::STRING_NAME, Variant::NIL>>(Variant::OP_EQUAL, Variant::STRING_NAME, Variant::NIL); register_op<OperatorEvaluatorAlwaysFalse<Variant::OP_EQUAL, Variant::NODE_PATH, Variant::NIL>>(Variant::OP_EQUAL, Variant::NODE_PATH, Variant::NIL); @@ -603,6 +596,7 @@ void Variant::_register_variant_operators() { register_op<OperatorEvaluatorAlwaysFalse<Variant::OP_EQUAL, Variant::NIL, Variant::AABB>>(Variant::OP_EQUAL, Variant::NIL, Variant::AABB); register_op<OperatorEvaluatorAlwaysFalse<Variant::OP_EQUAL, Variant::NIL, Variant::BASIS>>(Variant::OP_EQUAL, Variant::NIL, Variant::BASIS); register_op<OperatorEvaluatorAlwaysFalse<Variant::OP_EQUAL, Variant::NIL, Variant::TRANSFORM3D>>(Variant::OP_EQUAL, Variant::NIL, Variant::TRANSFORM3D); + register_op<OperatorEvaluatorAlwaysFalse<Variant::OP_EQUAL, Variant::NIL, Variant::PROJECTION>>(Variant::OP_EQUAL, Variant::NIL, Variant::PROJECTION); register_op<OperatorEvaluatorAlwaysFalse<Variant::OP_EQUAL, Variant::NIL, Variant::COLOR>>(Variant::OP_EQUAL, Variant::NIL, Variant::COLOR); register_op<OperatorEvaluatorAlwaysFalse<Variant::OP_EQUAL, Variant::NIL, Variant::STRING_NAME>>(Variant::OP_EQUAL, Variant::NIL, Variant::STRING_NAME); register_op<OperatorEvaluatorAlwaysFalse<Variant::OP_EQUAL, Variant::NIL, Variant::NODE_PATH>>(Variant::OP_EQUAL, Variant::NIL, Variant::NODE_PATH); @@ -688,6 +682,7 @@ void Variant::_register_variant_operators() { register_op<OperatorEvaluatorAlwaysTrue<Variant::OP_NOT_EQUAL, Variant::AABB, Variant::NIL>>(Variant::OP_NOT_EQUAL, Variant::AABB, Variant::NIL); register_op<OperatorEvaluatorAlwaysTrue<Variant::OP_NOT_EQUAL, Variant::BASIS, Variant::NIL>>(Variant::OP_NOT_EQUAL, Variant::BASIS, Variant::NIL); register_op<OperatorEvaluatorAlwaysTrue<Variant::OP_NOT_EQUAL, Variant::TRANSFORM3D, Variant::NIL>>(Variant::OP_NOT_EQUAL, Variant::TRANSFORM3D, Variant::NIL); + register_op<OperatorEvaluatorAlwaysTrue<Variant::OP_NOT_EQUAL, Variant::PROJECTION, Variant::NIL>>(Variant::OP_NOT_EQUAL, Variant::PROJECTION, Variant::NIL); register_op<OperatorEvaluatorAlwaysTrue<Variant::OP_NOT_EQUAL, Variant::COLOR, Variant::NIL>>(Variant::OP_NOT_EQUAL, Variant::COLOR, Variant::NIL); register_op<OperatorEvaluatorAlwaysTrue<Variant::OP_NOT_EQUAL, Variant::STRING_NAME, Variant::NIL>>(Variant::OP_NOT_EQUAL, Variant::STRING_NAME, Variant::NIL); register_op<OperatorEvaluatorAlwaysTrue<Variant::OP_NOT_EQUAL, Variant::NODE_PATH, Variant::NIL>>(Variant::OP_NOT_EQUAL, Variant::NODE_PATH, Variant::NIL); @@ -724,6 +719,7 @@ void Variant::_register_variant_operators() { register_op<OperatorEvaluatorAlwaysTrue<Variant::OP_NOT_EQUAL, Variant::NIL, Variant::AABB>>(Variant::OP_NOT_EQUAL, Variant::NIL, Variant::AABB); register_op<OperatorEvaluatorAlwaysTrue<Variant::OP_NOT_EQUAL, Variant::NIL, Variant::BASIS>>(Variant::OP_NOT_EQUAL, Variant::NIL, Variant::BASIS); register_op<OperatorEvaluatorAlwaysTrue<Variant::OP_NOT_EQUAL, Variant::NIL, Variant::TRANSFORM3D>>(Variant::OP_NOT_EQUAL, Variant::NIL, Variant::TRANSFORM3D); + register_op<OperatorEvaluatorAlwaysTrue<Variant::OP_NOT_EQUAL, Variant::NIL, Variant::PROJECTION>>(Variant::OP_NOT_EQUAL, Variant::NIL, Variant::PROJECTION); register_op<OperatorEvaluatorAlwaysTrue<Variant::OP_NOT_EQUAL, Variant::NIL, Variant::COLOR>>(Variant::OP_NOT_EQUAL, Variant::NIL, Variant::COLOR); register_op<OperatorEvaluatorAlwaysTrue<Variant::OP_NOT_EQUAL, Variant::NIL, Variant::STRING_NAME>>(Variant::OP_NOT_EQUAL, Variant::NIL, Variant::STRING_NAME); register_op<OperatorEvaluatorAlwaysTrue<Variant::OP_NOT_EQUAL, Variant::NIL, Variant::NODE_PATH>>(Variant::OP_NOT_EQUAL, Variant::NIL, Variant::NODE_PATH); diff --git a/core/variant/variant_parser.cpp b/core/variant/variant_parser.cpp index 34653310b1..d2e4d752a4 100644 --- a/core/variant/variant_parser.cpp +++ b/core/variant/variant_parser.cpp @@ -1651,7 +1651,7 @@ Error VariantWriter::write(const Variant &p_variant, StoreStringFunc p_store_str if (i != 0 || j != 0) { s += ", "; } - s += rtos_fix(t.matrix[i][j]); + s += rtos_fix(t.columns[i][j]); } } @@ -1680,7 +1680,7 @@ Error VariantWriter::write(const Variant &p_variant, StoreStringFunc p_store_str } break; case Variant::OBJECT: { - Object *obj = p_variant; + Object *obj = p_variant.get_validated_object(); if (!obj) { p_store_string_func(p_store_string_ud, "null"); diff --git a/core/variant/variant_setget.cpp b/core/variant/variant_setget.cpp index 57b953f7f0..188103ee5e 100644 --- a/core/variant/variant_setget.cpp +++ b/core/variant/variant_setget.cpp @@ -826,7 +826,7 @@ INDEXED_SETGET_STRUCT_BULTIN_NUMERIC(Color, double, float, 4) INDEXED_SETGET_STRUCT_BULTIN_ACCESSOR(Transform2D, Vector2, .columns, 3) INDEXED_SETGET_STRUCT_BULTIN_FUNC(Basis, Vector3, set_column, get_column, 3) -INDEXED_SETGET_STRUCT_BULTIN_ACCESSOR(Projection, Vector4, .matrix, 4) +INDEXED_SETGET_STRUCT_BULTIN_ACCESSOR(Projection, Vector4, .columns, 4) INDEXED_SETGET_STRUCT_TYPED_NUMERIC(PackedByteArray, int64_t, uint8_t) INDEXED_SETGET_STRUCT_TYPED_NUMERIC(PackedInt32Array, int64_t, int32_t) @@ -1911,572 +1911,6 @@ Variant Variant::recursive_duplicate(bool p_deep, int recursion_count) const { } } -void Variant::sub(const Variant &a, const Variant &b, Variant &r_dst) { - if (a.type != b.type) { - return; - } - - switch (a.type) { - case NIL: { - r_dst = Variant(); - } - return; - case INT: { - int64_t va = a._data._int; - int64_t vb = b._data._int; - r_dst = int(va - vb); - } - return; - case FLOAT: { - double ra = a._data._float; - double rb = b._data._float; - r_dst = ra - rb; - } - return; - case VECTOR2: { - r_dst = *reinterpret_cast<const Vector2 *>(a._data._mem) - *reinterpret_cast<const Vector2 *>(b._data._mem); - } - return; - case VECTOR2I: { - int32_t vax = reinterpret_cast<const Vector2i *>(a._data._mem)->x; - int32_t vbx = reinterpret_cast<const Vector2i *>(b._data._mem)->x; - int32_t vay = reinterpret_cast<const Vector2i *>(a._data._mem)->y; - int32_t vby = reinterpret_cast<const Vector2i *>(b._data._mem)->y; - r_dst = Vector2i(int32_t(vax - vbx), int32_t(vay - vby)); - } - return; - case RECT2: { - const Rect2 *ra = reinterpret_cast<const Rect2 *>(a._data._mem); - const Rect2 *rb = reinterpret_cast<const Rect2 *>(b._data._mem); - r_dst = Rect2(ra->position - rb->position, ra->size - rb->size); - } - return; - case RECT2I: { - const Rect2i *ra = reinterpret_cast<const Rect2i *>(a._data._mem); - const Rect2i *rb = reinterpret_cast<const Rect2i *>(b._data._mem); - - int32_t vax = ra->position.x; - int32_t vay = ra->position.y; - int32_t vbx = ra->size.x; - int32_t vby = ra->size.y; - int32_t vcx = rb->position.x; - int32_t vcy = rb->position.y; - int32_t vdx = rb->size.x; - int32_t vdy = rb->size.y; - - r_dst = Rect2i(int32_t(vax - vbx), int32_t(vay - vby), int32_t(vcx - vdx), int32_t(vcy - vdy)); - } - return; - case VECTOR3: { - r_dst = *reinterpret_cast<const Vector3 *>(a._data._mem) - *reinterpret_cast<const Vector3 *>(b._data._mem); - } - return; - case VECTOR3I: { - int32_t vax = reinterpret_cast<const Vector3i *>(a._data._mem)->x; - int32_t vbx = reinterpret_cast<const Vector3i *>(b._data._mem)->x; - int32_t vay = reinterpret_cast<const Vector3i *>(a._data._mem)->y; - int32_t vby = reinterpret_cast<const Vector3i *>(b._data._mem)->y; - int32_t vaz = reinterpret_cast<const Vector3i *>(a._data._mem)->z; - int32_t vbz = reinterpret_cast<const Vector3i *>(b._data._mem)->z; - r_dst = Vector3i(int32_t(vax - vbx), int32_t(vay - vby), int32_t(vaz - vbz)); - } - return; - case AABB: { - const ::AABB *ra = reinterpret_cast<const ::AABB *>(a._data._mem); - const ::AABB *rb = reinterpret_cast<const ::AABB *>(b._data._mem); - r_dst = ::AABB(ra->position - rb->position, ra->size - rb->size); - } - return; - case QUATERNION: { - Quaternion empty_rot; - const Quaternion *qa = reinterpret_cast<const Quaternion *>(a._data._mem); - const Quaternion *qb = reinterpret_cast<const Quaternion *>(b._data._mem); - r_dst = (*qb).inverse() * *qa; - } - return; - case COLOR: { - const Color *ca = reinterpret_cast<const Color *>(a._data._mem); - const Color *cb = reinterpret_cast<const Color *>(b._data._mem); - float new_r = ca->r - cb->r; - float new_g = ca->g - cb->g; - float new_b = ca->b - cb->b; - float new_a = ca->a - cb->a; - new_r = new_r > 1.0 ? 1.0 : new_r; - new_g = new_g > 1.0 ? 1.0 : new_g; - new_b = new_b > 1.0 ? 1.0 : new_b; - new_a = new_a > 1.0 ? 1.0 : new_a; - r_dst = Color(new_r, new_g, new_b, new_a); - } - return; - default: { - r_dst = a; - } - return; - } -} - -void Variant::blend(const Variant &a, const Variant &b, float c, Variant &r_dst) { - if (a.type != b.type) { - if (a.is_num() && b.is_num()) { - real_t va = a; - real_t vb = b; - r_dst = va + vb * c; - } else { - r_dst = a; - } - return; - } - - switch (a.type) { - case NIL: { - r_dst = Variant(); - } - return; - case INT: { - int64_t va = a._data._int; - int64_t vb = b._data._int; - r_dst = int(va + vb * c + 0.5); - } - return; - case FLOAT: { - double ra = a._data._float; - double rb = b._data._float; - r_dst = ra + rb * c; - } - return; - case VECTOR2: { - r_dst = *reinterpret_cast<const Vector2 *>(a._data._mem) + *reinterpret_cast<const Vector2 *>(b._data._mem) * c; - } - return; - case VECTOR2I: { - int32_t vax = reinterpret_cast<const Vector2i *>(a._data._mem)->x; - int32_t vbx = reinterpret_cast<const Vector2i *>(b._data._mem)->x; - int32_t vay = reinterpret_cast<const Vector2i *>(a._data._mem)->y; - int32_t vby = reinterpret_cast<const Vector2i *>(b._data._mem)->y; - r_dst = Vector2i(int32_t(vax + vbx * c + 0.5), int32_t(vay + vby * c + 0.5)); - } - return; - case RECT2: { - const Rect2 *ra = reinterpret_cast<const Rect2 *>(a._data._mem); - const Rect2 *rb = reinterpret_cast<const Rect2 *>(b._data._mem); - r_dst = Rect2(ra->position + rb->position * c, ra->size + rb->size * c); - } - return; - case RECT2I: { - const Rect2i *ra = reinterpret_cast<const Rect2i *>(a._data._mem); - const Rect2i *rb = reinterpret_cast<const Rect2i *>(b._data._mem); - - int32_t vax = ra->position.x; - int32_t vay = ra->position.y; - int32_t vbx = ra->size.x; - int32_t vby = ra->size.y; - int32_t vcx = rb->position.x; - int32_t vcy = rb->position.y; - int32_t vdx = rb->size.x; - int32_t vdy = rb->size.y; - - r_dst = Rect2i(int32_t(vax + vbx * c + 0.5), int32_t(vay + vby * c + 0.5), int32_t(vcx + vdx * c + 0.5), int32_t(vcy + vdy * c + 0.5)); - } - return; - case VECTOR3: { - r_dst = *reinterpret_cast<const Vector3 *>(a._data._mem) + *reinterpret_cast<const Vector3 *>(b._data._mem) * c; - } - return; - case VECTOR3I: { - int32_t vax = reinterpret_cast<const Vector3i *>(a._data._mem)->x; - int32_t vbx = reinterpret_cast<const Vector3i *>(b._data._mem)->x; - int32_t vay = reinterpret_cast<const Vector3i *>(a._data._mem)->y; - int32_t vby = reinterpret_cast<const Vector3i *>(b._data._mem)->y; - int32_t vaz = reinterpret_cast<const Vector3i *>(a._data._mem)->z; - int32_t vbz = reinterpret_cast<const Vector3i *>(b._data._mem)->z; - r_dst = Vector3i(int32_t(vax + vbx * c + 0.5), int32_t(vay + vby * c + 0.5), int32_t(vaz + vbz * c + 0.5)); - } - return; - case AABB: { - const ::AABB *ra = reinterpret_cast<const ::AABB *>(a._data._mem); - const ::AABB *rb = reinterpret_cast<const ::AABB *>(b._data._mem); - r_dst = ::AABB(ra->position + rb->position * c, ra->size + rb->size * c); - } - return; - case QUATERNION: { - Quaternion empty_rot; - const Quaternion *qa = reinterpret_cast<const Quaternion *>(a._data._mem); - const Quaternion *qb = reinterpret_cast<const Quaternion *>(b._data._mem); - r_dst = *qa * empty_rot.slerp(*qb, c); - } - return; - case COLOR: { - const Color *ca = reinterpret_cast<const Color *>(a._data._mem); - const Color *cb = reinterpret_cast<const Color *>(b._data._mem); - float new_r = ca->r + cb->r * c; - float new_g = ca->g + cb->g * c; - float new_b = ca->b + cb->b * c; - float new_a = ca->a + cb->a * c; - new_r = new_r > 1.0 ? 1.0 : new_r; - new_g = new_g > 1.0 ? 1.0 : new_g; - new_b = new_b > 1.0 ? 1.0 : new_b; - new_a = new_a > 1.0 ? 1.0 : new_a; - r_dst = Color(new_r, new_g, new_b, new_a); - } - return; - default: { - r_dst = c < 0.5 ? a : b; - } - return; - } -} - -void Variant::interpolate(const Variant &a, const Variant &b, float c, Variant &r_dst) { - if (a.type != b.type) { - if (a.is_num() && b.is_num()) { - //not as efficient but.. - real_t va = a; - real_t vb = b; - r_dst = va + (vb - va) * c; - - } else { - r_dst = a; - } - return; - } - - switch (a.type) { - case NIL: { - r_dst = Variant(); - } - return; - case BOOL: { - r_dst = a; - } - return; - case INT: { - int64_t va = a._data._int; - int64_t vb = b._data._int; - r_dst = int(va + (vb - va) * c); - } - return; - case FLOAT: { - real_t va = a._data._float; - real_t vb = b._data._float; - r_dst = va + (vb - va) * c; - } - return; - case STRING: { - //this is pretty funny and bizarre, but artists like to use it for typewriter effects - String sa = *reinterpret_cast<const String *>(a._data._mem); - String sb = *reinterpret_cast<const String *>(b._data._mem); - String dst; - int sa_len = sa.length(); - int sb_len = sb.length(); - int csize = sa_len + (sb_len - sa_len) * c; - if (csize == 0) { - r_dst = ""; - return; - } - dst.resize(csize + 1); - dst[csize] = 0; - int split = csize / 2; - - for (int i = 0; i < csize; i++) { - char32_t chr = ' '; - - if (i < split) { - if (i < sa.length()) { - chr = sa[i]; - } else if (i < sb.length()) { - chr = sb[i]; - } - - } else { - if (i < sb.length()) { - chr = sb[i]; - } else if (i < sa.length()) { - chr = sa[i]; - } - } - - dst[i] = chr; - } - - r_dst = dst; - } - return; - case VECTOR2: { - r_dst = reinterpret_cast<const Vector2 *>(a._data._mem)->lerp(*reinterpret_cast<const Vector2 *>(b._data._mem), c); - } - return; - case VECTOR2I: { - int32_t vax = reinterpret_cast<const Vector2i *>(a._data._mem)->x; - int32_t vbx = reinterpret_cast<const Vector2i *>(b._data._mem)->x; - int32_t vay = reinterpret_cast<const Vector2i *>(a._data._mem)->y; - int32_t vby = reinterpret_cast<const Vector2i *>(b._data._mem)->y; - r_dst = Vector2i(int32_t(vax + vbx * c + 0.5), int32_t(vay + vby * c + 0.5)); - } - return; - - case RECT2: { - r_dst = Rect2(reinterpret_cast<const Rect2 *>(a._data._mem)->position.lerp(reinterpret_cast<const Rect2 *>(b._data._mem)->position, c), reinterpret_cast<const Rect2 *>(a._data._mem)->size.lerp(reinterpret_cast<const Rect2 *>(b._data._mem)->size, c)); - } - return; - case RECT2I: { - const Rect2i *ra = reinterpret_cast<const Rect2i *>(a._data._mem); - const Rect2i *rb = reinterpret_cast<const Rect2i *>(b._data._mem); - - int32_t vax = ra->position.x; - int32_t vay = ra->position.y; - int32_t vbx = ra->size.x; - int32_t vby = ra->size.y; - int32_t vcx = rb->position.x; - int32_t vcy = rb->position.y; - int32_t vdx = rb->size.x; - int32_t vdy = rb->size.y; - - r_dst = Rect2i(int32_t(vax + vbx * c + 0.5), int32_t(vay + vby * c + 0.5), int32_t(vcx + vdx * c + 0.5), int32_t(vcy + vdy * c + 0.5)); - } - return; - - case VECTOR3: { - r_dst = reinterpret_cast<const Vector3 *>(a._data._mem)->lerp(*reinterpret_cast<const Vector3 *>(b._data._mem), c); - } - return; - case VECTOR3I: { - int32_t vax = reinterpret_cast<const Vector3i *>(a._data._mem)->x; - int32_t vbx = reinterpret_cast<const Vector3i *>(b._data._mem)->x; - int32_t vay = reinterpret_cast<const Vector3i *>(a._data._mem)->y; - int32_t vby = reinterpret_cast<const Vector3i *>(b._data._mem)->y; - int32_t vaz = reinterpret_cast<const Vector3i *>(a._data._mem)->z; - int32_t vbz = reinterpret_cast<const Vector3i *>(b._data._mem)->z; - r_dst = Vector3i(int32_t(vax + vbx * c + 0.5), int32_t(vay + vby * c + 0.5), int32_t(vaz + vbz * c + 0.5)); - } - return; - - case TRANSFORM2D: { - r_dst = a._data._transform2d->interpolate_with(*b._data._transform2d, c); - } - return; - case PLANE: { - r_dst = a; - } - return; - case QUATERNION: { - r_dst = reinterpret_cast<const Quaternion *>(a._data._mem)->slerp(*reinterpret_cast<const Quaternion *>(b._data._mem), c); - } - return; - case AABB: { - r_dst = ::AABB(a._data._aabb->position.lerp(b._data._aabb->position, c), a._data._aabb->size.lerp(b._data._aabb->size, c)); - } - return; - case BASIS: { - r_dst = a._data._basis->lerp(*b._data._basis, c); - } - return; - case TRANSFORM3D: { - r_dst = a._data._transform3d->interpolate_with(*b._data._transform3d, c); - } - return; - case COLOR: { - r_dst = reinterpret_cast<const Color *>(a._data._mem)->lerp(*reinterpret_cast<const Color *>(b._data._mem), c); - } - return; - case STRING_NAME: { - r_dst = a; - } - return; - case NODE_PATH: { - r_dst = a; - } - return; - case RID: { - r_dst = a; - } - return; - case OBJECT: { - r_dst = a; - } - return; - case DICTIONARY: { - } - return; - case ARRAY: { - r_dst = a; - } - return; - case PACKED_BYTE_ARRAY: { - r_dst = a; - } - return; - case PACKED_INT32_ARRAY: { - const Vector<int32_t> *arr_a = &PackedArrayRef<int32_t>::get_array(a._data.packed_array); - const Vector<int32_t> *arr_b = &PackedArrayRef<int32_t>::get_array(b._data.packed_array); - int32_t sz = arr_a->size(); - if (sz == 0 || arr_b->size() != sz) { - r_dst = a; - } else { - Vector<int32_t> v; - v.resize(sz); - { - int32_t *vw = v.ptrw(); - const int32_t *ar = arr_a->ptr(); - const int32_t *br = arr_b->ptr(); - - Variant va; - for (int32_t i = 0; i < sz; i++) { - Variant::interpolate(ar[i], br[i], c, va); - vw[i] = va; - } - } - r_dst = v; - } - } - return; - case PACKED_INT64_ARRAY: { - const Vector<int64_t> *arr_a = &PackedArrayRef<int64_t>::get_array(a._data.packed_array); - const Vector<int64_t> *arr_b = &PackedArrayRef<int64_t>::get_array(b._data.packed_array); - int64_t sz = arr_a->size(); - if (sz == 0 || arr_b->size() != sz) { - r_dst = a; - } else { - Vector<int64_t> v; - v.resize(sz); - { - int64_t *vw = v.ptrw(); - const int64_t *ar = arr_a->ptr(); - const int64_t *br = arr_b->ptr(); - - Variant va; - for (int64_t i = 0; i < sz; i++) { - Variant::interpolate(ar[i], br[i], c, va); - vw[i] = va; - } - } - r_dst = v; - } - } - return; - case PACKED_FLOAT32_ARRAY: { - const Vector<float> *arr_a = &PackedArrayRef<float>::get_array(a._data.packed_array); - const Vector<float> *arr_b = &PackedArrayRef<float>::get_array(b._data.packed_array); - int sz = arr_a->size(); - if (sz == 0 || arr_b->size() != sz) { - r_dst = a; - } else { - Vector<float> v; - v.resize(sz); - { - float *vw = v.ptrw(); - const float *ar = arr_a->ptr(); - const float *br = arr_b->ptr(); - - Variant va; - for (int i = 0; i < sz; i++) { - Variant::interpolate(ar[i], br[i], c, va); - vw[i] = va; - } - } - r_dst = v; - } - } - return; - case PACKED_FLOAT64_ARRAY: { - const Vector<double> *arr_a = &PackedArrayRef<double>::get_array(a._data.packed_array); - const Vector<double> *arr_b = &PackedArrayRef<double>::get_array(b._data.packed_array); - int sz = arr_a->size(); - if (sz == 0 || arr_b->size() != sz) { - r_dst = a; - } else { - Vector<double> v; - v.resize(sz); - { - double *vw = v.ptrw(); - const double *ar = arr_a->ptr(); - const double *br = arr_b->ptr(); - - Variant va; - for (int i = 0; i < sz; i++) { - Variant::interpolate(ar[i], br[i], c, va); - vw[i] = va; - } - } - r_dst = v; - } - } - return; - case PACKED_STRING_ARRAY: { - r_dst = a; - } - return; - case PACKED_VECTOR2_ARRAY: { - const Vector<Vector2> *arr_a = &PackedArrayRef<Vector2>::get_array(a._data.packed_array); - const Vector<Vector2> *arr_b = &PackedArrayRef<Vector2>::get_array(b._data.packed_array); - int sz = arr_a->size(); - if (sz == 0 || arr_b->size() != sz) { - r_dst = a; - } else { - Vector<Vector2> v; - v.resize(sz); - { - Vector2 *vw = v.ptrw(); - const Vector2 *ar = arr_a->ptr(); - const Vector2 *br = arr_b->ptr(); - - for (int i = 0; i < sz; i++) { - vw[i] = ar[i].lerp(br[i], c); - } - } - r_dst = v; - } - } - return; - case PACKED_VECTOR3_ARRAY: { - const Vector<Vector3> *arr_a = &PackedArrayRef<Vector3>::get_array(a._data.packed_array); - const Vector<Vector3> *arr_b = &PackedArrayRef<Vector3>::get_array(b._data.packed_array); - int sz = arr_a->size(); - if (sz == 0 || arr_b->size() != sz) { - r_dst = a; - } else { - Vector<Vector3> v; - v.resize(sz); - { - Vector3 *vw = v.ptrw(); - const Vector3 *ar = arr_a->ptr(); - const Vector3 *br = arr_b->ptr(); - - for (int i = 0; i < sz; i++) { - vw[i] = ar[i].lerp(br[i], c); - } - } - r_dst = v; - } - } - return; - case PACKED_COLOR_ARRAY: { - const Vector<Color> *arr_a = &PackedArrayRef<Color>::get_array(a._data.packed_array); - const Vector<Color> *arr_b = &PackedArrayRef<Color>::get_array(b._data.packed_array); - int sz = arr_a->size(); - if (sz == 0 || arr_b->size() != sz) { - r_dst = a; - } else { - Vector<Color> v; - v.resize(sz); - { - Color *vw = v.ptrw(); - const Color *ar = arr_a->ptr(); - const Color *br = arr_b->ptr(); - - for (int i = 0; i < sz; i++) { - vw[i] = ar[i].lerp(br[i], c); - } - } - r_dst = v; - } - } - return; - default: { - r_dst = a; - } - } -} - void Variant::_register_variant_setters_getters() { register_named_setters_getters(); register_indexed_setters_getters(); diff --git a/core/variant/variant_setget.h b/core/variant/variant_setget.h index 570277dc7a..d151a85a6e 100644 --- a/core/variant/variant_setget.h +++ b/core/variant/variant_setget.h @@ -325,10 +325,10 @@ SETGET_STRUCT_FUNC_INDEX(Basis, Vector3, z, set_column, get_column, 2) SETGET_STRUCT(Transform3D, Basis, basis) SETGET_STRUCT(Transform3D, Vector3, origin) -SETGET_STRUCT_CUSTOM(Projection, Vector4, x, matrix[0]) -SETGET_STRUCT_CUSTOM(Projection, Vector4, y, matrix[1]) -SETGET_STRUCT_CUSTOM(Projection, Vector4, z, matrix[2]) -SETGET_STRUCT_CUSTOM(Projection, Vector4, w, matrix[3]) +SETGET_STRUCT_CUSTOM(Projection, Vector4, x, columns[0]) +SETGET_STRUCT_CUSTOM(Projection, Vector4, y, columns[1]) +SETGET_STRUCT_CUSTOM(Projection, Vector4, z, columns[2]) +SETGET_STRUCT_CUSTOM(Projection, Vector4, w, columns[3]) SETGET_NUMBER_STRUCT(Color, double, r) SETGET_NUMBER_STRUCT(Color, double, g) diff --git a/core/variant/variant_utility.cpp b/core/variant/variant_utility.cpp index 1f1439ab24..3843c32bcc 100644 --- a/core/variant/variant_utility.cpp +++ b/core/variant/variant_utility.cpp @@ -128,8 +128,8 @@ struct VariantUtilityFunctions { return Math::floor(x); } - static inline int floori(double x) { - return int(x); + static inline int64_t floori(double x) { + return int64_t(Math::floor(x)); } static inline Variant ceil(Variant x, Callable::CallError &r_error) { @@ -161,8 +161,8 @@ struct VariantUtilityFunctions { return Math::ceil(x); } - static inline int ceili(double x) { - return int(Math::ceil(x)); + static inline int64_t ceili(double x) { + return int64_t(Math::ceil(x)); } static inline Variant round(Variant x, Callable::CallError &r_error) { @@ -194,8 +194,8 @@ struct VariantUtilityFunctions { return Math::round(x); } - static inline int roundi(double x) { - return int(Math::round(x)); + static inline int64_t roundi(double x) { + return int64_t(Math::round(x)); } static inline Variant abs(const Variant &x, Callable::CallError &r_error) { @@ -310,6 +310,10 @@ struct VariantUtilityFunctions { return Math::is_zero_approx(x); } + static inline bool is_finite(double x) { + return Math::is_finite(x); + } + static inline double ease(float x, float curve) { return Math::ease(x, curve); } @@ -367,6 +371,20 @@ struct VariantUtilityFunctions { return Math::cubic_interpolate(from, to, pre, post, weight); } + static inline double cubic_interpolate_angle(double from, double to, double pre, double post, double weight) { + return Math::cubic_interpolate_angle(from, to, pre, post, weight); + } + + static inline double cubic_interpolate_in_time(double from, double to, double pre, double post, double weight, + double to_t, double pre_t, double post_t) { + return Math::cubic_interpolate_in_time(from, to, pre, post, weight, to_t, pre_t, post_t); + } + + static inline double cubic_interpolate_angle_in_time(double from, double to, double pre, double post, double weight, + double to_t, double pre_t, double post_t) { + return Math::cubic_interpolate_angle_in_time(from, to, pre, post, weight, to_t, pre_t, post_t); + } + static inline double bezier_interpolate(double p_start, double p_control_1, double p_control_2, double p_end, double p_t) { return Math::bezier_interpolate(p_start, p_control_1, p_control_2, p_end, p_t); } @@ -379,8 +397,8 @@ struct VariantUtilityFunctions { return Math::inverse_lerp(from, to, weight); } - static inline double range_lerp(double value, double istart, double istop, double ostart, double ostop) { - return Math::range_lerp(value, istart, istop, ostart, ostop); + static inline double remap(double value, double istart, double istop, double ostart, double ostop) { + return Math::remap(value, istart, istop, ostart, ostop); } static inline double smoothstep(double from, double to, double val) { @@ -391,20 +409,20 @@ struct VariantUtilityFunctions { return Math::move_toward(from, to, delta); } - static inline double deg2rad(double angle_deg) { - return Math::deg2rad(angle_deg); + static inline double deg_to_rad(double angle_deg) { + return Math::deg_to_rad(angle_deg); } - static inline double rad2deg(double angle_rad) { - return Math::rad2deg(angle_rad); + static inline double rad_to_deg(double angle_rad) { + return Math::rad_to_deg(angle_rad); } - static inline double linear2db(double linear) { - return Math::linear2db(linear); + static inline double linear_to_db(double linear) { + return Math::linear_to_db(linear); } - static inline double db2linear(double db) { - return Math::db2linear(db); + static inline double db_to_linear(double db) { + return Math::db_to_linear(db); } static inline Variant wrap(const Variant &p_x, const Variant &p_min, const Variant &p_max, Callable::CallError &r_error) { @@ -832,13 +850,13 @@ struct VariantUtilityFunctions { r_error.error = Callable::CallError::CALL_OK; } - static inline String var2str(const Variant &p_var) { + static inline String var_to_str(const Variant &p_var) { String vars; VariantWriter::write_to_string(p_var, vars); return vars; } - static inline Variant str2var(const String &p_var) { + static inline Variant str_to_var(const String &p_var) { VariantParser::StreamString ss; ss.s = p_var; @@ -850,7 +868,7 @@ struct VariantUtilityFunctions { return ret; } - static inline PackedByteArray var2bytes(const Variant &p_var) { + static inline PackedByteArray var_to_bytes(const Variant &p_var) { int len; Error err = encode_variant(p_var, nullptr, len, false); if (err != OK) { @@ -870,7 +888,7 @@ struct VariantUtilityFunctions { return barr; } - static inline PackedByteArray var2bytes_with_objects(const Variant &p_var) { + static inline PackedByteArray var_to_bytes_with_objects(const Variant &p_var) { int len; Error err = encode_variant(p_var, nullptr, len, true); if (err != OK) { @@ -890,7 +908,7 @@ struct VariantUtilityFunctions { return barr; } - static inline Variant bytes2var(const PackedByteArray &p_arr) { + static inline Variant bytes_to_var(const PackedByteArray &p_arr) { Variant ret; { const uint8_t *r = p_arr.ptr(); @@ -902,7 +920,7 @@ struct VariantUtilityFunctions { return ret; } - static inline Variant bytes2var_with_objects(const PackedByteArray &p_arr) { + static inline Variant bytes_to_var_with_objects(const PackedByteArray &p_arr) { Variant ret; { const uint8_t *r = p_arr.ptr(); @@ -1406,6 +1424,7 @@ void Variant::_register_variant_utility_functions() { FUNCBINDR(is_equal_approx, sarray("a", "b"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(is_zero_approx, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(is_finite, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(ease, sarray("x", "curve"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(step_decimals, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); @@ -1414,18 +1433,21 @@ void Variant::_register_variant_utility_functions() { FUNCBINDVR3(lerp, sarray("from", "to", "weight"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(lerpf, sarray("from", "to", "weight"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(cubic_interpolate, sarray("from", "to", "pre", "post", "weight"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(cubic_interpolate_angle, sarray("from", "to", "pre", "post", "weight"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(cubic_interpolate_in_time, sarray("from", "to", "pre", "post", "weight", "to_t", "pre_t", "post_t"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(cubic_interpolate_angle_in_time, sarray("from", "to", "pre", "post", "weight", "to_t", "pre_t", "post_t"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(bezier_interpolate, sarray("start", "control_1", "control_2", "end", "t"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(lerp_angle, sarray("from", "to", "weight"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(inverse_lerp, sarray("from", "to", "weight"), Variant::UTILITY_FUNC_TYPE_MATH); - FUNCBINDR(range_lerp, sarray("value", "istart", "istop", "ostart", "ostop"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(remap, sarray("value", "istart", "istop", "ostart", "ostop"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(smoothstep, sarray("from", "to", "x"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(move_toward, sarray("from", "to", "delta"), Variant::UTILITY_FUNC_TYPE_MATH); - FUNCBINDR(deg2rad, sarray("deg"), Variant::UTILITY_FUNC_TYPE_MATH); - FUNCBINDR(rad2deg, sarray("rad"), Variant::UTILITY_FUNC_TYPE_MATH); - FUNCBINDR(linear2db, sarray("lin"), Variant::UTILITY_FUNC_TYPE_MATH); - FUNCBINDR(db2linear, sarray("db"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(deg_to_rad, sarray("deg"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(rad_to_deg, sarray("rad"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(linear_to_db, sarray("lin"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(db_to_linear, sarray("db"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDVR3(wrap, sarray("value", "min", "max"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(wrapi, sarray("value", "min", "max"), Variant::UTILITY_FUNC_TYPE_MATH); @@ -1473,14 +1495,14 @@ void Variant::_register_variant_utility_functions() { FUNCBINDVARARGV(push_error, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL); FUNCBINDVARARGV(push_warning, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL); - FUNCBINDR(var2str, sarray("variable"), Variant::UTILITY_FUNC_TYPE_GENERAL); - FUNCBINDR(str2var, sarray("string"), Variant::UTILITY_FUNC_TYPE_GENERAL); + FUNCBINDR(var_to_str, sarray("variable"), Variant::UTILITY_FUNC_TYPE_GENERAL); + FUNCBINDR(str_to_var, sarray("string"), Variant::UTILITY_FUNC_TYPE_GENERAL); - FUNCBINDR(var2bytes, sarray("variable"), Variant::UTILITY_FUNC_TYPE_GENERAL); - FUNCBINDR(bytes2var, sarray("bytes"), Variant::UTILITY_FUNC_TYPE_GENERAL); + FUNCBINDR(var_to_bytes, sarray("variable"), Variant::UTILITY_FUNC_TYPE_GENERAL); + FUNCBINDR(bytes_to_var, sarray("bytes"), Variant::UTILITY_FUNC_TYPE_GENERAL); - FUNCBINDR(var2bytes_with_objects, sarray("variable"), Variant::UTILITY_FUNC_TYPE_GENERAL); - FUNCBINDR(bytes2var_with_objects, sarray("bytes"), Variant::UTILITY_FUNC_TYPE_GENERAL); + FUNCBINDR(var_to_bytes_with_objects, sarray("variable"), Variant::UTILITY_FUNC_TYPE_GENERAL); + FUNCBINDR(bytes_to_var_with_objects, sarray("bytes"), Variant::UTILITY_FUNC_TYPE_GENERAL); FUNCBINDR(hash, sarray("variable"), Variant::UTILITY_FUNC_TYPE_GENERAL); |