Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimized client starting #315

Merged
merged 1 commit into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Application/RSBot/Views/Controls/Cos/CosController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ private void OnAgentServerDisconnected()
panelTopCenter.Controls.Clear();
_cachedControls.Clear();
_selectedIndex = 0;
Visible = false;
}

private void OnSummonCos(CosBase obj)
Expand Down
12 changes: 7 additions & 5 deletions Library/RSBot.Core/ClientManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,27 @@ public static async Task<bool> Start()
GlobalConfig.Get<string>("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<byte>("RSBot.GatewayIndex");
var divisionIndex = GlobalConfig.Get<byte>("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)
Expand Down Expand Up @@ -157,7 +159,7 @@ private static void ClientProcess_Exited(object sender, EventArgs e)
/// <param name="divisionIndex"></param>
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;
Expand Down
7 changes: 5 additions & 2 deletions Library/RSBot.Core/Extensions/NativeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion Library/RSBot.Loader.Library/Library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion Library/RSBot.Loader.Library/RSBot.Loader.Library.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
<OutDir>$(SolutionDir)Build\</OutDir>
<TargetName>Loader</TargetName>
<TargetName>Client.Library</TargetName>
<LibraryPath>.\Detours;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
Expand Down