From 56b7ffc4bd1f7e97697b430258b392a9fc5aee5e Mon Sep 17 00:00:00 2001 From: Mikhail Date: Wed, 9 Apr 2025 16:28:28 -0400 Subject: [PATCH] significant improvements to server selector --- ServerSelector/GameSettings.cs | 51 ++++++++++ ServerSelector/SearchReplace.cs | 128 +++++++++++++++++++++++++ ServerSelector/ServerSwitcher.cs | 120 +++++++---------------- ServerSelector/Views/MainView.axaml | 2 +- ServerSelector/Views/MainView.axaml.cs | 28 +++--- 5 files changed, 232 insertions(+), 97 deletions(-) create mode 100644 ServerSelector/GameSettings.cs create mode 100644 ServerSelector/SearchReplace.cs diff --git a/ServerSelector/GameSettings.cs b/ServerSelector/GameSettings.cs new file mode 100644 index 0000000..c3ca1b0 --- /dev/null +++ b/ServerSelector/GameSettings.cs @@ -0,0 +1,51 @@ +using System; +using System.IO; +using System.Text.Json; + +namespace ServerSelector +{ + public class GameSettings + { + private static GameSettings? _settings; + public string GameRoot { get; set; } = "C:\\Nikke"; + public string LastIp { get; set; } = "127.0.0.1"; + public int LastOffset { get; set; } + + public static GameSettings Settings + { + get + { + if (_settings != null) + return _settings; + + string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "serverselectorsettings.json"); + + try + { + if (File.Exists(path)) + { + string json = File.ReadAllText(path); + _settings = JsonSerializer.Deserialize(json); + } + } + catch + { + + } + + if (_settings == null) + { + _settings = new(); + } + + return _settings; + } + } + + public static void Save() + { + string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "serverselectorsettings.json"); + File.WriteAllText(path, JsonSerializer.Serialize(_settings)); + } + } +} \ No newline at end of file diff --git a/ServerSelector/SearchReplace.cs b/ServerSelector/SearchReplace.cs new file mode 100644 index 0000000..8652c1e --- /dev/null +++ b/ServerSelector/SearchReplace.cs @@ -0,0 +1,128 @@ +// From: https://github.com/Ninka-Rex/CSharp-Search-and-Replace/blob/main/SearchReplace.cs + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; + +public static class PatchUtility +{ + // This method searches and replaces binary patterns in a given file + // It takes three parameters: + // - filePath: the path of the file to be patched + // - searchPatterns: an array of hexadecimal strings representing the patterns to be searched for + // - replacePatterns: an array of hexadecimal strings representing the patterns to be replaced with + // It returns true if the patching was successful, or false if there was an error or a pattern was not found + public static bool SearchAndReplace(string filePath, string[] searchPatterns, string[] replacePatterns) + { + try + { + // Check if the file exists + if (!File.Exists(filePath)) { Console.WriteLine("[ERROR] File not found: " + filePath); return false; } + + // Read the binary data from the file as an array of bytes + byte[] fileData = File.ReadAllBytes(filePath); + + // Backup the original file by copying it to a new file with a .bak extension + string backupFilePath = filePath + ".bak"; + if (!File.Exists(backupFilePath)) + { + File.Copy(filePath, backupFilePath); + } + + // Loop through each pair of search and replace patterns and apply them to the file data + for (int k = 0; k < searchPatterns.Length; k++) + { + // Convert the hexadecimal strings to byte arrays using a helper method + byte[] searchBytes = HexStringToBytes(searchPatterns[k]); + byte[] replaceBytes = HexStringToBytes(replacePatterns[k]); + + // Find the index of the first occurrence of the search pattern in the file data using another helper method + int index = FindPatternIndex(fileData, searchBytes)[1]; + + Console.WriteLine("offset: " + index.ToString("X")); + + // If the index is -1, it means the pattern was not found, so we return false and log an error message + if (index == -1) + { + Console.WriteLine("[ERROR] Search pattern not found: " + searchPatterns[k]); + return false; + } + + // Replace the pattern at the found index with the replace pattern, preserving original values when wildcards are encountered + // A wildcard is represented by either 00 or FF in the replace pattern, meaning that we keep the original value at that position + for (int i = 0; i < replaceBytes.Length; i++) + { + if (replaceBytes[i] != 0x00 && replaceBytes[i] != 0xFF) + { + fileData[index + i] = replaceBytes[i]; + } + else if (replaceBytes[i] == 0x00) + { + fileData[index + i] = 0x00; + } + } + + // Log a success message with the offset and file name where the patch was applied + string exeName = Path.GetFileName(filePath); + Console.WriteLine($"[Patch] Apply patch success at 0x{index:X} in {exeName}"); + } + + // Write the modified data back to the file, overwriting the original content + File.WriteAllBytes(filePath, fileData); + + return true; + } + catch (Exception ex) + { + // If any exception occurs during the patching process, we return false and log an error message with the exception details + Console.WriteLine("[ERROR] An error occurred while writing the file: " + ex.Message); + return false; + } + } + + // This helper method converts a hexadecimal string to a byte array + // It takes one parameter: + // - hex: a string of hexadecimal digits, optionally separated by spaces or question marks + // It returns a byte array corresponding to the hexadecimal values in the string + private static byte[] HexStringToBytes(string hex) + { + hex = hex.Replace(" ", "").Replace("??", "FF"); // Replace ?? with FF for wildcards + return Enumerable.Range(0, hex.Length) + .Where(x => x % 2 == 0) // Take every second character in the string + .Select(x => Convert.ToByte(hex.Substring(x, 2), 16)) // Convert each pair of characters to a byte value in base 16 + .ToArray(); // Convert the result to an array of bytes + } + + // This helper method finds the index of the first occurrence of a pattern in a data array + // It takes two parameters: + // - data: an array of bytes representing the data to be searched in + // - pattern: an array of bytes representing the pattern to be searched for + // It returns an integer representing the index where the pattern was found, or -1 if it was not found + private static List FindPatternIndex(byte[] data, byte[] pattern) + { + List points = []; + // Loop through each possible position in the data array where the pattern could start + for (int i = 0; i < data.Length - pattern.Length + 1; i++) + { + bool found = true; // Assume that the pattern is found until proven otherwise + // Loop through each byte in the pattern and compare it with the corresponding byte in the data array + for (int j = 0; j < pattern.Length; j++) + { + // If the pattern byte is not FF (wildcard) and it does not match the data byte, then the pattern is not found at this position + if (pattern[j] != 0xFF && data[i + j] != pattern[j]) + { + found = false; + break; + } + } + // If the pattern was found at this position, return the index + if (found) + { + points.Add(i); + } + } + // If the pattern was not found in the entire data array, return -1 + return points; + } +} \ No newline at end of file diff --git a/ServerSelector/ServerSwitcher.cs b/ServerSelector/ServerSwitcher.cs index e821059..ca64294 100644 --- a/ServerSelector/ServerSwitcher.cs +++ b/ServerSelector/ServerSwitcher.cs @@ -7,11 +7,9 @@ namespace ServerSelector { public class ServerSwitcher { - private static int GameAssemblySodiumIntegrityFuncHint = 0x6014080; - public static string PatchGameVersion = "131.10.2"; - - private static byte[] GameAssemblySodiumIntegrityFuncOrg = [0x40, 0x53, 0x56, 0x57, 0x41]; - private static byte[] GameAssemblySodiumIntegrityFuncPatch = [0xb0, 0x01, 0xc3, 0x90, 0x90]; + private static string[] GameAssemblySodiumIntegrityFuncHint = ["40 53 56 57 41 54 41 55 41 56 41 57 48 81 EC C0 00 00 00 80 3d ?? ?? ?? ?? 00 0f 85 ?? 00 00 00 48"]; + public static bool GameAssemblyNeedsPatch = true; // Set to false if running on versions before v124 + private static string[] GameAssemblySodiumIntegrityFuncPatch = ["b0 01 c3 90 90"]; private const string HostsStartMarker = "# begin ServerSelector entries"; private const string HostsEndMarker = "# end ServerSelector entries"; @@ -67,8 +65,7 @@ namespace ServerSelector { var certList1 = await File.ReadAllTextAsync(launcherCertList); - int goodSslIndex1 = certList1.IndexOf("Good SSL Ca"); - if (goodSslIndex1 == -1) + if (!certList1.Contains("Good SSL Ca")) return "Patch missing"; } @@ -76,8 +73,7 @@ namespace ServerSelector { var certList2 = await File.ReadAllTextAsync(gameCertList); - int goodSslIndex2 = certList2.IndexOf("Good SSL Ca"); - if (goodSslIndex2 == -1) + if (!certList2.Contains("Good SSL Ca")) return "Patch missing"; } @@ -127,7 +123,7 @@ namespace ServerSelector endIdx = txt.IndexOf(endIndexStr) + endIndexStr.Length; } - txt = txt.Substring(0, startIdx) + txt.Substring(endIdx); + txt = string.Concat(txt.AsSpan(0, startIdx), txt.AsSpan(endIdx)); await File.WriteAllTextAsync(hostsFilePath, txt); @@ -138,6 +134,24 @@ namespace ServerSelector } } + public static bool PatchGameAssembly(string dll, bool install) + { + if (!GameAssemblyNeedsPatch) return true; + + bool backupExists = File.Exists(dll + ".bak"); + + if (install && !backupExists) + { + return PatchUtility.SearchAndReplace(dll, GameAssemblySodiumIntegrityFuncHint, GameAssemblySodiumIntegrityFuncPatch); + } + else if (backupExists) + { + File.Move(dll + ".bak", dll, true); + } + + return true; + } + public static async Task SaveCfg(bool useOffical, string gamePath, string launcherPath, string ip, bool offlineMode) { string sodiumLib = AppDomain.CurrentDomain.BaseDirectory + "sodium.dll"; @@ -172,7 +186,7 @@ namespace ServerSelector // remove cert if (OperatingSystem.IsWindows()) { - X509Store store = new X509Store(StoreName.Root, StoreLocation.LocalMachine); + X509Store store = new(StoreName.Root, StoreLocation.LocalMachine); store.Open(OpenFlags.ReadWrite); store.Remove(new X509Certificate2(X509Certificate.CreateFromCertFile(AppDomain.CurrentDomain.BaseDirectory + "myCA.pfx"))); store.Close(); @@ -186,39 +200,13 @@ namespace ServerSelector // restore sodium if (!File.Exists(sodiumBackup)) { - throw new Exception("sodium backup does not exist"); + throw new Exception("sodium backup does not exist. Repair the game in the launcher and switch to local server and back to official."); } File.Copy(sodiumBackup, gameSodium, true); - // revert gameassembly changes - var gameAssemblyBytes = await File.ReadAllBytesAsync(gameAssembly); - var i = GameAssemblySodiumIntegrityFuncHint; - if (gameAssemblyBytes[i] == GameAssemblySodiumIntegrityFuncOrg[0] && - gameAssemblyBytes[i + 1] == GameAssemblySodiumIntegrityFuncOrg[1] && - gameAssemblyBytes[i + 2] == GameAssemblySodiumIntegrityFuncOrg[2] && - gameAssemblyBytes[i + 3] == GameAssemblySodiumIntegrityFuncOrg[3] && - gameAssemblyBytes[i + 4] == GameAssemblySodiumIntegrityFuncOrg[4]) + if (!PatchGameAssembly(gameAssembly, false)) { - - } - else if (gameAssemblyBytes[i] == GameAssemblySodiumIntegrityFuncPatch[0] && - gameAssemblyBytes[i + 1] == GameAssemblySodiumIntegrityFuncPatch[1] && - gameAssemblyBytes[i + 2] == GameAssemblySodiumIntegrityFuncPatch[2] && - gameAssemblyBytes[i + 3] == GameAssemblySodiumIntegrityFuncPatch[3] && - gameAssemblyBytes[i + 4] == GameAssemblySodiumIntegrityFuncPatch[4]) - { - gameAssemblyBytes[i] = GameAssemblySodiumIntegrityFuncOrg[0]; - gameAssemblyBytes[i + 1] = GameAssemblySodiumIntegrityFuncOrg[1]; - gameAssemblyBytes[i + 2] = GameAssemblySodiumIntegrityFuncOrg[2]; - gameAssemblyBytes[i + 3] = GameAssemblySodiumIntegrityFuncOrg[3]; - gameAssemblyBytes[i + 4] = GameAssemblySodiumIntegrityFuncOrg[4]; - - File.WriteAllBytes(gameAssembly, gameAssemblyBytes); - } - else - { - // TODO: unsupported version - supported = false; + throw new Exception("Failed to restore GameAssembly.dll. Please repair the game in the launcher."); } if (File.Exists(launcherCertList)) @@ -227,7 +215,7 @@ namespace ServerSelector int goodSslIndex1 = certList1.IndexOf("Good SSL Ca"); if (goodSslIndex1 != -1) - await File.WriteAllTextAsync(launcherCertList, certList1.Substring(0, goodSslIndex1)); + await File.WriteAllTextAsync(launcherCertList, certList1[..goodSslIndex1]); } if (File.Exists(gameCertList)) @@ -236,7 +224,7 @@ namespace ServerSelector int goodSslIndex2 = certList2.IndexOf("Good SSL Ca"); if (goodSslIndex2 != -1) - await File.WriteAllTextAsync(gameCertList, certList2.Substring(0, goodSslIndex2)); + await File.WriteAllTextAsync(gameCertList, certList2[..goodSslIndex2]); } } else @@ -323,38 +311,7 @@ namespace ServerSelector await File.WriteAllBytesAsync(gameSodium, await File.ReadAllBytesAsync(sodiumLib)); // patch gameassembly to remove sodium IntegrityUtility Check introduced in v124.6.10 - var gameAssemblyBytes = await File.ReadAllBytesAsync(gameAssembly); - - var i = GameAssemblySodiumIntegrityFuncHint; - if (gameAssemblyBytes[i] == GameAssemblySodiumIntegrityFuncOrg[0] && - gameAssemblyBytes[i + 1] == GameAssemblySodiumIntegrityFuncOrg[1] && - gameAssemblyBytes[i + 2] == GameAssemblySodiumIntegrityFuncOrg[2] && - gameAssemblyBytes[i + 3] == GameAssemblySodiumIntegrityFuncOrg[3] && - gameAssemblyBytes[i + 4] == GameAssemblySodiumIntegrityFuncOrg[4]) - { - gameAssemblyBytes[i] = GameAssemblySodiumIntegrityFuncPatch[0]; // MOV ax, 1 - gameAssemblyBytes[i + 1] = GameAssemblySodiumIntegrityFuncPatch[1]; - gameAssemblyBytes[i + 2] = GameAssemblySodiumIntegrityFuncPatch[2]; // NOP - gameAssemblyBytes[i + 3] = GameAssemblySodiumIntegrityFuncPatch[3]; // NOP - gameAssemblyBytes[i + 4] = GameAssemblySodiumIntegrityFuncPatch[4]; // NOP - - await File.WriteAllBytesAsync(gameAssembly, gameAssemblyBytes); - } - else if (gameAssemblyBytes[i] == GameAssemblySodiumIntegrityFuncPatch[0] && - gameAssemblyBytes[i + 1] == GameAssemblySodiumIntegrityFuncPatch[1] && - gameAssemblyBytes[i + 2] == GameAssemblySodiumIntegrityFuncPatch[2] && - gameAssemblyBytes[i + 3] == GameAssemblySodiumIntegrityFuncPatch[3] && - gameAssemblyBytes[i + 4] == GameAssemblySodiumIntegrityFuncPatch[4]) - { - // was already patched - } - else - { - // TODO: unsupported version - supported = false; - } - - + supported = PatchGameAssembly(gameAssembly, true); // update launcher/game ca cert list @@ -373,17 +330,10 @@ namespace ServerSelector } } - public class ServerSwitchResult + public class ServerSwitchResult(bool ok, Exception? exception, bool isSupported) { - public bool Ok { get; set; } - public Exception? Exception { get; set; } - public bool IsSupported { get; set; } - - public ServerSwitchResult(bool ok, Exception? exception, bool isSupported) - { - this.Ok = ok; - this.Exception = exception; - this.IsSupported = isSupported; - } + public bool Ok { get; set; } = ok; + public Exception? Exception { get; set; } = exception; + public bool IsSupported { get; set; } = isSupported; } } diff --git a/ServerSelector/Views/MainView.axaml b/ServerSelector/Views/MainView.axaml index 2def89e..0e791cd 100644 --- a/ServerSelector/Views/MainView.axaml +++ b/ServerSelector/Views/MainView.axaml @@ -114,7 +114,7 @@ - Game path: + Game root path: C:\NIKKE\ diff --git a/ServerSelector/Views/MainView.axaml.cs b/ServerSelector/Views/MainView.axaml.cs index 15d9ede..ffd6cdf 100644 --- a/ServerSelector/Views/MainView.axaml.cs +++ b/ServerSelector/Views/MainView.axaml.cs @@ -34,11 +34,14 @@ public partial class MainView : UserControl { VerticalAlignment = Avalonia.Layout.VerticalAlignment.Center, HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Center, - Text = "Root is required to change servers." + Text = "Root is required to change servers in order to modify /etc/hosts." }; } UpdateIntegrityLabel(); + + txtGamePath.Text = GameSettings.Settings.GameRoot; + TxtIpAddress.Text = GameSettings.Settings.LastIp; } private void SetGamePathValid(bool isValid) @@ -83,7 +86,7 @@ public partial class MainView : UserControl { SetGamePathValid(false); if (showMessage) - ShowWarningMsg("Game path does not exist", "Error"); + ShowWarningMsg("game folder does not exist in the game root folder", "Error"); return false; } @@ -91,7 +94,7 @@ public partial class MainView : UserControl { SetGamePathValid(false); if (showMessage) - ShowWarningMsg("Launcher path is invalid. Make sure that nikke_launcher.exe exists in the launcher folder", "Error"); + ShowWarningMsg("Game path is invalid. Make sure that nikke_launcher.exe exists in the /launcher folder", "Error"); return false; } @@ -130,15 +133,18 @@ public partial class MainView : UserControl if (!ValidatePaths(true) || txtGamePath.Text == null || GamePath == null || LauncherPath == null) return; - if (CmbServerSelection.SelectedIndex == 1) + if (CmbServerSelection.SelectedIndex == 1 && !IPAddress.TryParse(TxtIpAddress.Text, out _)) { - if (!IPAddress.TryParse(TxtIpAddress.Text, out _)) - { - ShowWarningMsg("Invalid IP address. The entered IP address should be IPv4 or IPv6.", "Error"); - return; - } + ShowWarningMsg("Invalid IP address. The entered IP address should be IPv4 or IPv6.", "Error"); + return; } - if (TxtIpAddress.Text == null) TxtIpAddress.Text = ""; + + GameSettings.Settings.GameRoot = txtGamePath.Text; + GameSettings.Settings.LastIp = TxtIpAddress.Text ?? "127.0.0.1"; + + TxtIpAddress.Text ??= ""; + + GameSettings.Save(); SetLoadingScreenVisible(true); try @@ -170,7 +176,7 @@ public partial class MainView : UserControl public static void ShowWarningMsg(string text, string title) { - ContentDialog dlg = new ContentDialog() { Title = title, Content = text, PrimaryButtonText = "OK" }; + ContentDialog dlg = new() { Title = title, Content = text, PrimaryButtonText = "OK" }; dlg.ShowAsync(); }