From 9b8f490a807b61f8d4852da1fab5e2dc6b0d307b Mon Sep 17 00:00:00 2001 From: pGlase Date: Mon, 19 Apr 2021 21:24:04 +0200 Subject: [PATCH 1/3] Added IP address string checks to MP Menu --- .../Patches/Dynamic/UIMainMenu_Patch.cs | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/NebulaPatcher/Patches/Dynamic/UIMainMenu_Patch.cs b/NebulaPatcher/Patches/Dynamic/UIMainMenu_Patch.cs index 9ad2d5f9b..47b97e37f 100644 --- a/NebulaPatcher/Patches/Dynamic/UIMainMenu_Patch.cs +++ b/NebulaPatcher/Patches/Dynamic/UIMainMenu_Patch.cs @@ -171,6 +171,18 @@ private static void OnJoinGameButtonClick() string ip = parts[0]; int port; + //remove copy and paste mistakes and update the textbox to prevent user confusion in case of invalid ip address + ip = SanitizeIpAddressString(ip); + hostIPAdressInput.text = parts.Length == 1 ? ip : ip + ":" + parts[1]; + + if (!System.Net.IPAddress.TryParse(ip, out _)) + { + //log, notify user and early return to prevent crash on invalid strings + InGamePopup.ShowWarning("Invalid connection string", "Note: This mod currently only accepts IPv4 Addresses.", "OK"); + Log.Info($"IP String {ip} is not a valid IPv4 String"); + return; + } + if (parts.Length == 1) { port = Config.DefaultPort; @@ -181,6 +193,7 @@ private static void OnJoinGameButtonClick() return; } + // TODO: Should display a loader during the connection and only open the game once the player is connected to the server. multiplayerMenu.gameObject.SetActive(false); @@ -195,5 +208,17 @@ private static void OnJoinGameBackButtonClick() multiplayerMenu.gameObject.SetActive(false); UIRoot.instance.OpenMainMenuUI(); } + + private static string SanitizeIpAddressString(string ipAddr) + { + //remove simple copy&paste errors + string cleanedIpAddr = new System.Text.StringBuilder(ipAddr) + .Replace("\n", string.Empty) + .Replace(" ", string.Empty) + .Replace("\t", string.Empty) + .ToString(); + + return cleanedIpAddr; + } } } From e8ae46c7d7f5cadff7269aff78877df050c66c2c Mon Sep 17 00:00:00 2001 From: pGlase Date: Mon, 19 Apr 2021 21:32:58 +0200 Subject: [PATCH 2/3] added /r check for windows --- NebulaPatcher/Patches/Dynamic/UIMainMenu_Patch.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/NebulaPatcher/Patches/Dynamic/UIMainMenu_Patch.cs b/NebulaPatcher/Patches/Dynamic/UIMainMenu_Patch.cs index 47b97e37f..5ca736b64 100644 --- a/NebulaPatcher/Patches/Dynamic/UIMainMenu_Patch.cs +++ b/NebulaPatcher/Patches/Dynamic/UIMainMenu_Patch.cs @@ -213,6 +213,7 @@ private static string SanitizeIpAddressString(string ipAddr) { //remove simple copy&paste errors string cleanedIpAddr = new System.Text.StringBuilder(ipAddr) + .Replace("\r", string.Empty) .Replace("\n", string.Empty) .Replace(" ", string.Empty) .Replace("\t", string.Empty) From 4af168afc916afd4e9d9d1aef8470c4524fff4ee Mon Sep 17 00:00:00 2001 From: pGlase Date: Mon, 19 Apr 2021 22:04:48 +0200 Subject: [PATCH 3/3] removed sanitize function and switched to String.Trim --- .../Patches/Dynamic/UIMainMenu_Patch.cs | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/NebulaPatcher/Patches/Dynamic/UIMainMenu_Patch.cs b/NebulaPatcher/Patches/Dynamic/UIMainMenu_Patch.cs index 5ca736b64..914126606 100644 --- a/NebulaPatcher/Patches/Dynamic/UIMainMenu_Patch.cs +++ b/NebulaPatcher/Patches/Dynamic/UIMainMenu_Patch.cs @@ -172,7 +172,7 @@ private static void OnJoinGameButtonClick() int port; //remove copy and paste mistakes and update the textbox to prevent user confusion in case of invalid ip address - ip = SanitizeIpAddressString(ip); + ip = ip.Trim('\r', '\n', '\t', ' '); hostIPAdressInput.text = parts.Length == 1 ? ip : ip + ":" + parts[1]; if (!System.Net.IPAddress.TryParse(ip, out _)) @@ -193,7 +193,6 @@ private static void OnJoinGameButtonClick() return; } - // TODO: Should display a loader during the connection and only open the game once the player is connected to the server. multiplayerMenu.gameObject.SetActive(false); @@ -208,18 +207,5 @@ private static void OnJoinGameBackButtonClick() multiplayerMenu.gameObject.SetActive(false); UIRoot.instance.OpenMainMenuUI(); } - - private static string SanitizeIpAddressString(string ipAddr) - { - //remove simple copy&paste errors - string cleanedIpAddr = new System.Text.StringBuilder(ipAddr) - .Replace("\r", string.Empty) - .Replace("\n", string.Empty) - .Replace(" ", string.Empty) - .Replace("\t", string.Empty) - .ToString(); - - return cleanedIpAddr; - } } }