From 3feaeb05d27c6c0541bbbe9eb7743ba736d0d432 Mon Sep 17 00:00:00 2001 From: SDClowen Date: Fri, 22 Jul 2022 01:56:42 +0300 Subject: [PATCH] Optimized client starting --- .../RSBot/Views/Controls/Cos/CosController.cs | 1 + Library/RSBot.Core/ClientManager.cs | 12 +++++++----- Library/RSBot.Core/Extensions/NativeExtensions.cs | 7 +++++-- Library/RSBot.Loader.Library/Library.cpp | 2 +- .../RSBot.Loader.Library.vcxproj | 2 +- 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/Application/RSBot/Views/Controls/Cos/CosController.cs b/Application/RSBot/Views/Controls/Cos/CosController.cs index 91bcac15..03d7c985 100644 --- a/Application/RSBot/Views/Controls/Cos/CosController.cs +++ b/Application/RSBot/Views/Controls/Cos/CosController.cs @@ -45,6 +45,7 @@ private void OnAgentServerDisconnected() panelTopCenter.Controls.Clear(); _cachedControls.Clear(); _selectedIndex = 0; + Visible = false; } private void OnSummonCos(CosBase obj) diff --git a/Library/RSBot.Core/ClientManager.cs b/Library/RSBot.Core/ClientManager.cs index b5f069a5..00170d4d 100644 --- a/Library/RSBot.Core/ClientManager.cs +++ b/Library/RSBot.Core/ClientManager.cs @@ -34,25 +34,27 @@ public static async Task Start() GlobalConfig.Get("RSBot.SilkroadExecutable") ); - var buffer = Encoding.UTF8.GetBytes(Path.Combine(Environment.CurrentDirectory, "Loader.dll")); + var buffer = Encoding.UTF8.GetBytes(Path.Combine(Environment.CurrentDirectory, "Client.Library.dll")); var pathLen = (uint)buffer.Length; var gatewayIndex = GlobalConfig.Get("RSBot.GatewayIndex"); var divisionIndex = GlobalConfig.Get("RSBot.DivisionIndex"); byte contentId = Game.ReferenceManager.DivisionInfo.Locale; - var args = $"/{contentId} {divisionIndex} {gatewayIndex}"; + var args = $"/{contentId} {divisionIndex} {gatewayIndex} 0"; var si = new STARTUPINFO(); var full = $"\"{path}\" {args}"; - bool result = CreateProcessA(null, full, IntPtr.Zero, IntPtr.Zero, false, CREATE_SUSPENDED, IntPtr.Zero, silkroadDirectory, ref si, out var pi); + bool result = CreateProcess(null, full, IntPtr.Zero, IntPtr.Zero, false, CREATE_SUSPENDED, IntPtr.Zero, silkroadDirectory, ref si, out var pi); if (!result) return false; PrepareTempConfigFile(pi.dwProcessId, divisionIndex); - var semaphore = new Semaphore(0, 1, pi.dwProcessId.ToString()); + //CreateSemaphore(IntPtr.Zero, 0, 1, + //System.Runtime.InteropServices.Marshal.StringToHGlobalAuto(pi.dwProcessId.ToString())); + //var semaphore = new Semaphore(0, 1, pi.dwProcessId.ToString()); var handle = OpenProcess(PROCESS_ALL_ACCESS, false, pi.dwProcessId); if (handle == IntPtr.Zero) @@ -157,7 +159,7 @@ private static void ClientProcess_Exited(object sender, EventArgs e) /// private static void PrepareTempConfigFile(uint processId, int divisionIndex) { - var tmpConfigFile = $"RSBot_{processId}.dat"; + var tmpConfigFile = $"RSBot_{processId}.tmp"; var division = Game.ReferenceManager.DivisionInfo.Divisions[divisionIndex]; var gatewayPort = Game.ReferenceManager.GatewayInfo.Port; diff --git a/Library/RSBot.Core/Extensions/NativeExtensions.cs b/Library/RSBot.Core/Extensions/NativeExtensions.cs index fceda0b5..c39a15b3 100644 --- a/Library/RSBot.Core/Extensions/NativeExtensions.cs +++ b/Library/RSBot.Core/Extensions/NativeExtensions.cs @@ -21,7 +21,7 @@ public static class NativeExtensions public static extern int SetWindowText(IntPtr hWnd, string text); [DllImport("kernel32.dll")] - public static extern bool CreateProcessA( + public static extern bool CreateProcess/*A*/( string lpApplicationName, string lpCommandLine, IntPtr lpProcessAttributes, @@ -84,7 +84,10 @@ public static extern IntPtr CreateRemoteThread( public static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds); [DllImport("kernel32.dll")] - internal static extern IntPtr LoadLibrary(string lpFileName); + public static extern IntPtr LoadLibrary(string lpFileName); + + [DllImport("kernel32.dll")] + public static extern IntPtr CreateSemaphore([In] IntPtr lpSemaphoreAttributes, [In] int lInitialCount, [In] int lMaximumCount, [In] IntPtr lpName); public struct PROCESS_INFORMATION { diff --git a/Library/RSBot.Loader.Library/Library.cpp b/Library/RSBot.Loader.Library/Library.cpp index 393ebaf0..19f8fe81 100644 --- a/Library/RSBot.Loader.Library/Library.cpp +++ b/Library/RSBot.Loader.Library/Library.cpp @@ -175,7 +175,7 @@ void LoadConfig() _dupenv_s(&tempFolder, NULL, "TMP"); stringstream payloadPath; - payloadPath << tempFolder << "\\RSBot_" << GetCurrentProcessId() << ".dat"; + payloadPath << tempFolder << "\\RSBot_" << GetCurrentProcessId() << ".tmp"; ifstream stream(payloadPath.str(), ifstream::binary); diff --git a/Library/RSBot.Loader.Library/RSBot.Loader.Library.vcxproj b/Library/RSBot.Loader.Library/RSBot.Loader.Library.vcxproj index bdc79f80..767bdf12 100644 --- a/Library/RSBot.Loader.Library/RSBot.Loader.Library.vcxproj +++ b/Library/RSBot.Loader.Library/RSBot.Loader.Library.vcxproj @@ -36,7 +36,7 @@ false $(SolutionDir)Build\ - Loader + Client.Library .\Detours;$(LibraryPath)