From 775b9ee8893a176b6616a047e297d090399fb685 Mon Sep 17 00:00:00 2001 From: Raul Santos Date: Sat, 6 Aug 2022 04:05:15 +0200 Subject: [PATCH] Add conversion for the RPC attributes in C# --- editor/project_converter_3_to_4.cpp | 119 ++++++++++++++++++++++++++++ editor/project_converter_3_to_4.h | 3 + 2 files changed, 122 insertions(+) diff --git a/editor/project_converter_3_to_4.cpp b/editor/project_converter_3_to_4.cpp index fe9c4dfc16e..421e56f9a13 100644 --- a/editor/project_converter_3_to_4.cpp +++ b/editor/project_converter_3_to_4.cpp @@ -1714,6 +1714,7 @@ int ProjectConverter3To4::convert() { rename_common(csharp_properties_renames, file_content); rename_common(csharp_signals_renames, file_content); rename_csharp_functions(file_content); + rename_csharp_attributes(file_content); custom_rename(file_content, "public class ", "public partial class "); } else if (file_name.ends_with(".gdshader") || file_name.ends_with(".shader")) { rename_common(shaders_renames, file_content); @@ -1859,6 +1860,7 @@ int ProjectConverter3To4::validate_conversion() { changed_elements.append_array(check_for_rename_common(csharp_properties_renames, file_content)); changed_elements.append_array(check_for_rename_common(csharp_signals_renames, file_content)); changed_elements.append_array(check_for_rename_csharp_functions(file_content)); + changed_elements.append_array(check_for_rename_csharp_attributes(file_content)); changed_elements.append_array(check_for_custom_rename(file_content, "public class ", "public partial class ")); } else if (file_name.ends_with(".gdshader") || file_name.ends_with(".shader")) { changed_elements.append_array(check_for_rename_common(shaders_renames, file_content)); @@ -2009,6 +2011,15 @@ bool ProjectConverter3To4::test_conversion(const RegExContainer ®_container) valid = valid & test_conversion_single_additional("(Disconnect(A,B,C) != OK):", "(Disconnect(A,new Callable(B,C)) != OK):", &ProjectConverter3To4::rename_csharp_functions, "custom rename csharp"); valid = valid & test_conversion_single_additional("(IsConnected(A,B,C) != OK):", "(IsConnected(A,new Callable(B,C)) != OK):", &ProjectConverter3To4::rename_csharp_functions, "custom rename"); + valid = valid & test_conversion_single_additional("[Remote]", "[RPC(MultiplayerAPI.RPCMode.AnyPeer)]", &ProjectConverter3To4::rename_csharp_attributes, "custom rename csharp"); + valid = valid & test_conversion_single_additional("[RemoteSync]", "[RPC(MultiplayerAPI.RPCMode.AnyPeer, CallLocal = true)]", &ProjectConverter3To4::rename_csharp_attributes, "custom rename csharp"); + valid = valid & test_conversion_single_additional("[Sync]", "[RPC(MultiplayerAPI.RPCMode.AnyPeer, CallLocal = true)]", &ProjectConverter3To4::rename_csharp_attributes, "custom rename csharp"); + valid = valid & test_conversion_single_additional("[Slave]", "[RPC]", &ProjectConverter3To4::rename_csharp_attributes, "custom rename csharp"); + valid = valid & test_conversion_single_additional("[Puppet]", "[RPC]", &ProjectConverter3To4::rename_csharp_attributes, "custom rename csharp"); + valid = valid & test_conversion_single_additional("[PuppetSync]", "[RPC(CallLocal = true)]", &ProjectConverter3To4::rename_csharp_attributes, "custom rename csharp"); + valid = valid & test_conversion_single_additional("[Master]", "The master and mastersync rpc behavior is not officially supported anymore. Try using another keyword or making custom logic using Multiplayer.GetRemoteSenderId()\n[RPC]", &ProjectConverter3To4::rename_csharp_attributes, "custom rename csharp"); + valid = valid & test_conversion_single_additional("[MasterSync]", "The master and mastersync rpc behavior is not officially supported anymore. Try using another keyword or making custom logic using Multiplayer.GetRemoteSenderId()\n[RPC(CallLocal = true)]", &ProjectConverter3To4::rename_csharp_attributes, "custom rename csharp"); + valid = valid & test_conversion_single_additional_builtin("OS.window_fullscreen = Settings.fullscreen", "ProjectSettings.set(\"display/window/size/fullscreen\", Settings.fullscreen)", &ProjectConverter3To4::rename_gdscript_functions, "custom rename", reg_container, false); valid = valid & test_conversion_single_additional_builtin("OS.window_fullscreen = Settings.fullscreen", "ProjectSettings.set(\\\"display/window/size/fullscreen\\\", Settings.fullscreen)", &ProjectConverter3To4::rename_gdscript_functions, "custom rename", reg_container, true); valid = valid & test_conversion_single_additional_builtin("OS.get_window_safe_area()", "DisplayServer.get_display_safe_area()", &ProjectConverter3To4::rename_gdscript_functions, "custom rename", reg_container, false); @@ -3197,6 +3208,114 @@ Vector ProjectConverter3To4::check_for_rename_csharp_functions(Vector [RPC(MultiplayerAPI.RPCMode.AnyPeer)] + { + RegEx reg_remote = RegEx("\\[Remote(Attribute)?(\\(\\))?\\]"); + CRASH_COND(!reg_remote.is_valid()); + file_content = reg_remote.sub(file_content, "[RPC(MultiplayerAPI.RPCMode.AnyPeer)]", true); + } + // -- [RemoteSync] -> [RPC(MultiplayerAPI.RPCMode.AnyPeer, CallLocal = true)] + { + RegEx reg_remotesync = RegEx("\\[(Remote)?Sync(Attribute)?(\\(\\))?\\]"); + CRASH_COND(!reg_remotesync.is_valid()); + file_content = reg_remotesync.sub(file_content, "[RPC(MultiplayerAPI.RPCMode.AnyPeer, CallLocal = true)]", true); + } + // -- [Puppet] -> [RPC] + { + RegEx reg_puppet = RegEx("\\[(Puppet|Slave)(Attribute)?(\\(\\))?\\]"); + CRASH_COND(!reg_puppet.is_valid()); + file_content = reg_puppet.sub(file_content, "[RPC]", true); + } + // -- [PuppetSync] -> [RPC(CallLocal = true)] + { + RegEx reg_puppetsync = RegEx("\\[PuppetSync(Attribute)?(\\(\\))?\\]"); + CRASH_COND(!reg_puppetsync.is_valid()); + file_content = reg_puppetsync.sub(file_content, "[RPC(CallLocal = true)]", true); + } + String error_message = "The master and mastersync rpc behavior is not officially supported anymore. Try using another keyword or making custom logic using Multiplayer.GetRemoteSenderId()\n"; + // -- [Master] -> [RPC] + { + RegEx reg_remote = RegEx("\\[Master(Attribute)?(\\(\\))?\\]"); + CRASH_COND(!reg_remote.is_valid()); + file_content = reg_remote.sub(file_content, error_message + "[RPC]", true); + } + // -- [MasterSync] -> [RPC(CallLocal = true)] + { + RegEx reg_remote = RegEx("\\[MasterSync(Attribute)?(\\(\\))?\\]"); + CRASH_COND(!reg_remote.is_valid()); + file_content = reg_remote.sub(file_content, error_message + "[RPC(CallLocal = true)]", true); + } +} + +Vector ProjectConverter3To4::check_for_rename_csharp_attributes(Vector &file_content) { + int current_line = 1; + + Vector found_things; + + for (String &line : file_content) { + String old; + old = line; + { + RegEx regex = RegEx("\\[Remote(Attribute)?(\\(\\))?\\]"); + CRASH_COND(!regex.is_valid()); + line = regex.sub(line, "[RPC(MultiplayerAPI.RPCMode.AnyPeer)]", true); + } + if (old != line) { + found_things.append(line_formatter(current_line, "[Remote]", "[RPC(MultiplayerAPI.RPCMode.AnyPeer)]", line)); + } + old = line; + { + RegEx regex = RegEx("\\[(Remote)?Sync(Attribute)?(\\(\\))?\\]"); + CRASH_COND(!regex.is_valid()); + line = regex.sub(line, "[RPC(MultiplayerAPI.RPCMode.AnyPeer, CallLocal = true)]", true); + } + if (old != line) { + found_things.append(line_formatter(current_line, "[RemoteSync]", "[RPC(MultiplayerAPI.RPCMode.AnyPeer, CallLocal = true)]", line)); + } + old = line; + { + RegEx regex = RegEx("\\[Puppet(Attribute)?(\\(\\))?\\]"); + CRASH_COND(!regex.is_valid()); + line = regex.sub(line, "[RPC]", true); + } + if (old != line) { + found_things.append(line_formatter(current_line, "[Puppet]", "[RPC]", line)); + } + old = line; + { + RegEx regex = RegEx("\\[(Puppet|Slave)Sync(Attribute)?(\\(\\))?\\]"); + CRASH_COND(!regex.is_valid()); + line = regex.sub(line, "[RPC(CallLocal = true)]", true); + } + if (old != line) { + found_things.append(line_formatter(current_line, "[PuppetSync]", "[RPC(CallLocal = true)]", line)); + } + old = line; + { + RegEx regex = RegEx("\\[Master(Attribute)?(\\(\\))?\\]"); + CRASH_COND(!regex.is_valid()); + line = regex.sub(line, "[RPC]", true); + } + if (old != line) { + found_things.append(line_formatter(current_line, "[Master]", "[RPC]", line)); + } + old = line; + { + RegEx regex = RegEx("\\[MasterSync(Attribute)?(\\(\\))?\\]"); + CRASH_COND(!regex.is_valid()); + line = regex.sub(line, "[RPC(CallLocal = true)]", true); + } + if (old != line) { + found_things.append(line_formatter(current_line, "[MasterSync]", "[RPC(CallLocal = true)]", line)); + } + + current_line++; + } + + return found_things; +} + void ProjectConverter3To4::rename_gdscript_keywords(String &file_content) { { RegEx reg_first = RegEx("([\n]+)tool"); diff --git a/editor/project_converter_3_to_4.h b/editor/project_converter_3_to_4.h index a58bb2767a7..8526e2ceb9e 100644 --- a/editor/project_converter_3_to_4.h +++ b/editor/project_converter_3_to_4.h @@ -55,6 +55,9 @@ private: Vector check_for_rename_csharp_functions(Vector &file_content); void process_csharp_line(String &line); + void rename_csharp_attributes(String &file_content); + Vector check_for_rename_csharp_attributes(Vector &file_content); + void rename_gdscript_keywords(String &file_content); Vector check_for_rename_gdscript_keywords(Vector &file_content);