From 3bc614bb1cbcee067d8334f527ad79f5c247c6f3 Mon Sep 17 00:00:00 2001 From: Eric Liu Date: Fri, 29 Sep 2023 14:51:37 +0200 Subject: [PATCH] Fix conversion of hex color strings in project converter (cherry picked from commit d94ad09903a9716a20dc261ace633040a9e7bc61) --- editor/project_converter_3_to_4.cpp | 30 ++++++++++++++++++++++++++++- editor/project_converter_3_to_4.h | 1 + 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/editor/project_converter_3_to_4.cpp b/editor/project_converter_3_to_4.cpp index b2994f3065c..d6ed5eb995f 100644 --- a/editor/project_converter_3_to_4.cpp +++ b/editor/project_converter_3_to_4.cpp @@ -139,6 +139,9 @@ public: LocalVector color_regexes; LocalVector color_renamed; + RegEx color_hexadecimal_short_constructor = RegEx("Color\\(\"#?([a-fA-F0-9]{1})([a-fA-F0-9]{3})\\b"); + RegEx color_hexadecimal_full_constructor = RegEx("Color\\(\"#?([a-fA-F0-9]{2})([a-fA-F0-9]{6})\\b"); + // Classes. LocalVector class_tscn_regexes; LocalVector class_gd_regexes; @@ -409,6 +412,8 @@ bool ProjectConverter3To4::convert() { rename_common(RenamesMap3To4::theme_override_renames, reg_container.theme_override_regexes, source_lines); custom_rename(source_lines, "\\.shader", ".gdshader"); + + convert_hexadecimal_colors(source_lines, reg_container); } else if (file_name.ends_with(".tscn")) { fix_pause_mode(source_lines, reg_container); @@ -429,6 +434,8 @@ bool ProjectConverter3To4::convert() { rename_common(RenamesMap3To4::theme_override_renames, reg_container.theme_override_regexes, source_lines); custom_rename(source_lines, "\\.shader", ".gdshader"); + + convert_hexadecimal_colors(source_lines, reg_container); } else if (file_name.ends_with(".cs")) { // TODO, C# should use different methods. rename_classes(source_lines, reg_container); // Using only specialized function. rename_common(RenamesMap3To4::csharp_function_renames, reg_container.csharp_function_regexes, source_lines); @@ -438,6 +445,7 @@ bool ProjectConverter3To4::convert() { rename_csharp_functions(source_lines, reg_container); rename_csharp_attributes(source_lines, reg_container); custom_rename(source_lines, "public class ", "public partial class "); + convert_hexadecimal_colors(source_lines, reg_container); } else if (file_name.ends_with(".gdshader") || file_name.ends_with(".shader")) { rename_common(RenamesMap3To4::shaders_renames, reg_container.shaders_regexes, source_lines); } else if (file_name.ends_with("tres")) { @@ -1004,7 +1012,10 @@ bool ProjectConverter3To4::test_conversion(RegExContainer ®_container) { valid = valid && test_conversion_gdscript_builtin("button.pressed=1", "button.button_pressed=1", &ProjectConverter3To4::rename_gdscript_functions, "custom rename", reg_container, false); valid = valid && test_conversion_gdscript_builtin("button.pressed SF", "button.pressed SF", &ProjectConverter3To4::rename_gdscript_functions, "custom rename", reg_container, false); - valid = valid && test_conversion_with_regex("AAA Color.white AF", "AAA Color.WHITE AF", &ProjectConverter3To4::rename_colors, "custom rename", reg_container); + valid = valid && test_conversion_with_regex("Color(\"#f47d\")", "Color(\"#47df\")", &ProjectConverter3To4::convert_hexadecimal_colors, "color literals", reg_container); + valid = valid && test_conversion_with_regex("Color(\"#ff478cbf\")", "Color(\"#478cbfff\")", &ProjectConverter3To4::convert_hexadecimal_colors, "color literals", reg_container); + valid = valid && test_conversion_with_regex("Color(\"#de32bf\")", "Color(\"#de32bf\")", &ProjectConverter3To4::convert_hexadecimal_colors, "color literals", reg_container); + valid = valid && test_conversion_with_regex("AAA Color.white AF", "AAA Color.WHITE AF", &ProjectConverter3To4::rename_colors, "color constants", 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); @@ -1458,6 +1469,23 @@ void ProjectConverter3To4::rename_colors(Vector &source_lines, const } }; +// Convert hexadecimal colors from ARGB to RGBA +void ProjectConverter3To4::convert_hexadecimal_colors(Vector &source_lines, const RegExContainer ®_container) { + for (SourceLine &source_line : source_lines) { + if (source_line.is_comment) { + continue; + } + + String &line = source_line.line; + if (uint64_t(line.length()) <= maximum_line_length) { + if (line.contains("Color(\"")) { + line = reg_container.color_hexadecimal_short_constructor.sub(line, "Color(\"#$2$1", true); + line = reg_container.color_hexadecimal_full_constructor.sub(line, "Color(\"#$2$1", true); + } + } + } +} + Vector ProjectConverter3To4::check_for_rename_colors(Vector &lines, const RegExContainer ®_container) { Vector found_renames; diff --git a/editor/project_converter_3_to_4.h b/editor/project_converter_3_to_4.h index e26f4ee49a9..2afd0a24e8c 100644 --- a/editor/project_converter_3_to_4.h +++ b/editor/project_converter_3_to_4.h @@ -75,6 +75,7 @@ class ProjectConverter3To4 { void fix_pause_mode(Vector &source_lines, const RegExContainer ®_container); void rename_colors(Vector &source_lines, const RegExContainer ®_container); + void convert_hexadecimal_colors(Vector &source_lines, const RegExContainer ®_container); Vector check_for_rename_colors(Vector &lines, const RegExContainer ®_container); void rename_classes(Vector &source_lines, const RegExContainer ®_container);