From fec630f360b93fa221acb7d47efd7aa9b1605801 Mon Sep 17 00:00:00 2001 From: Ninni Pipping Date: Thu, 2 Mar 2023 15:24:00 +0100 Subject: [PATCH] Add keycode project conversion --- editor/project_converter_3_to_4.cpp | 62 +++++++++++++++++++++++++++++ editor/project_converter_3_to_4.h | 3 ++ 2 files changed, 65 insertions(+) diff --git a/editor/project_converter_3_to_4.cpp b/editor/project_converter_3_to_4.cpp index 3c6f5c09554..663a2af065e 100644 --- a/editor/project_converter_3_to_4.cpp +++ b/editor/project_converter_3_to_4.cpp @@ -139,6 +139,9 @@ public: LocalVector class_gd_regexes; LocalVector class_shader_regexes; + // Keycode. + RegEx input_map_keycode = RegEx("\\b,\"((physical_)?)scancode\":(\\d+)\\b"); + LocalVector class_regexes; RegEx class_temp_tscn = RegEx("\\bTEMP_RENAMED_CLASS.tscn\\b"); @@ -415,6 +418,7 @@ bool ProjectConverter3To4::convert() { } else if (file_name.ends_with("project.godot")) { rename_common(RenamesMap3To4::project_godot_renames, reg_container.project_godot_regexes, lines); rename_common(RenamesMap3To4::builtin_types_renames, reg_container.builtin_types_regexes, lines); + rename_input_map_scancode(lines, reg_container); rename_common(RenamesMap3To4::input_map_renames, reg_container.input_map_regexes, lines); } else if (file_name.ends_with(".csproj")) { // TODO @@ -587,6 +591,7 @@ bool ProjectConverter3To4::validate_conversion() { } else if (file_name.ends_with("project.godot")) { changed_elements.append_array(check_for_rename_common(RenamesMap3To4::project_godot_renames, reg_container.project_godot_regexes, lines)); changed_elements.append_array(check_for_rename_common(RenamesMap3To4::builtin_types_renames, reg_container.builtin_types_regexes, lines)); + changed_elements.append_array(check_for_rename_input_map_scancode(lines, reg_container)); changed_elements.append_array(check_for_rename_common(RenamesMap3To4::input_map_renames, reg_container.input_map_regexes, lines)); } else if (file_name.ends_with(".csproj")) { // TODO @@ -913,6 +918,10 @@ bool ProjectConverter3To4::test_conversion(RegExContainer ®_container) { valid = valid && test_conversion_with_regex("AAA Color.white AF", "AAA Color.WHITE AF", &ProjectConverter3To4::rename_colors, "custom rename", reg_container); + // Note: Do not change to *scancode*, it is applied before that conversion. + valid = valid && test_conversion_with_regex("\"device\":-1,\"scancode\":16777231,\"physical_scancode\":16777232", "\"device\":-1,\"scancode\":4194319,\"physical_scancode\":4194320", &ProjectConverter3To4::rename_input_map_scancode, "custom rename", reg_container); + valid = valid && test_conversion_with_regex("\"device\":-1,\"scancode\":65,\"physical_scancode\":66", "\"device\":-1,\"scancode\":65,\"physical_scancode\":66", &ProjectConverter3To4::rename_input_map_scancode, "custom rename", reg_container); + // Custom rule conversion { String from = "instance"; @@ -2495,6 +2504,59 @@ Vector ProjectConverter3To4::check_for_rename_gdscript_keywords(Vector &lines, const RegExContainer ®_container) { + // The old Special Key, now colliding with CMD_OR_CTRL. + const int old_spkey = (1 << 24); + + for (String &line : lines) { + if (uint64_t(line.length()) <= maximum_line_length) { + TypedArray reg_match = reg_container.input_map_keycode.search_all(line); + + for (int i = 0; i < reg_match.size(); ++i) { + Ref match = reg_match[i]; + PackedStringArray strings = match->get_strings(); + int key = strings[3].to_int(); + + if (key & old_spkey) { + // Create new key, clearing old Special Key and setting new one. + key = (key & ~old_spkey) | (int)Key::SPECIAL; + + line = line.replace(strings[0], String(",\"") + strings[1] + "scancode\":" + String::num_int64(key)); + } + } + } + } +} + +Vector ProjectConverter3To4::check_for_rename_input_map_scancode(Vector &lines, const RegExContainer ®_container) { + Vector found_renames; + + // The old Special Key, now colliding with CMD_OR_CTRL. + const int old_spkey = (1 << 24); + + int current_line = 1; + for (String &line : lines) { + if (uint64_t(line.length()) <= maximum_line_length) { + TypedArray reg_match = reg_container.input_map_keycode.search_all(line); + + for (int i = 0; i < reg_match.size(); ++i) { + Ref match = reg_match[i]; + PackedStringArray strings = match->get_strings(); + int key = strings[3].to_int(); + + if (key & old_spkey) { + // Create new key, clearing old Special Key and setting new one. + key = (key & ~old_spkey) | (int)Key::SPECIAL; + + found_renames.append(line_formatter(current_line, strings[3], String::num_int64(key), line)); + } + } + } + current_line++; + } + return found_renames; +} + void ProjectConverter3To4::custom_rename(Vector &lines, String from, String to) { RegEx reg = RegEx(String("\\b") + from + "\\b"); CRASH_COND(!reg.is_valid()); diff --git a/editor/project_converter_3_to_4.h b/editor/project_converter_3_to_4.h index 420dd79d727..90c05c22d28 100644 --- a/editor/project_converter_3_to_4.h +++ b/editor/project_converter_3_to_4.h @@ -86,6 +86,9 @@ class ProjectConverter3To4 { void rename_gdscript_keywords(Vector &lines, const RegExContainer ®_container); Vector check_for_rename_gdscript_keywords(Vector &lines, const RegExContainer ®_container); + void rename_input_map_scancode(Vector &lines, const RegExContainer ®_container); + Vector check_for_rename_input_map_scancode(Vector &lines, const RegExContainer ®_container); + void custom_rename(Vector &lines, String from, String to); Vector check_for_custom_rename(Vector &lines, String from, String to);