Skip to content

Commit

Permalink
Changed: DLL Injector to use https://github.com/Sewer56/dll-syringe […
Browse files Browse the repository at this point in the history
…WIP]
  • Loading branch information
Sewer56 committed Feb 25, 2024
1 parent 0a7dd9b commit 572a66e
Show file tree
Hide file tree
Showing 14 changed files with 32 additions and 214 deletions.
Binary file added source/Packages/dll_syringe.Net.Sys.0.16.0.nupkg
Binary file not shown.
18 changes: 0 additions & 18 deletions source/Reloaded-II.sln
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestAppA", "Testing\Apps\Te
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestInterfaces", "Testing\Other\TestInterfaces\TestInterfaces.csproj", "{D0363CCD-17A6-4700-8C94-D4E3327C6A07}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Reloaded.Mod.Launcher.Kernel32AddressDumper", "Reloaded.Mod.Launcher.Kernel32AddressDumper\Reloaded.Mod.Launcher.Kernel32AddressDumper.csproj", "{B0BFEE3E-4A56-4F9F-95A8-9229FC74D46E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestModD", "Testing\Mods\TestModD\TestModD.csproj", "{E036F18E-312A-4C78-A8E9-5E37E5EBA9B9}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestModE", "Testing\Mods\TestModE\TestModE.csproj", "{0C911851-C2F3-4E84-BF0D-413DAD7887A7}"
Expand Down Expand Up @@ -277,22 +275,6 @@ Global
{D0363CCD-17A6-4700-8C94-D4E3327C6A07}.Release-ModLoaderOnly|Any CPU.ActiveCfg = Release|Any CPU
{D0363CCD-17A6-4700-8C94-D4E3327C6A07}.Release-ModLoaderOnly|Any CPU.Build.0 = Release|Any CPU
{D0363CCD-17A6-4700-8C94-D4E3327C6A07}.Release-ModLoaderOnly|X64+X86.ActiveCfg = Release|Any CPU
{B0BFEE3E-4A56-4F9F-95A8-9229FC74D46E}.Debug|Any CPU.ActiveCfg = Debug|x86
{B0BFEE3E-4A56-4F9F-95A8-9229FC74D46E}.Debug|Any CPU.Build.0 = Debug|x86
{B0BFEE3E-4A56-4F9F-95A8-9229FC74D46E}.Debug|X64+X86.ActiveCfg = Debug|x86
{B0BFEE3E-4A56-4F9F-95A8-9229FC74D46E}.Debug|X64+X86.Build.0 = Debug|x86
{B0BFEE3E-4A56-4F9F-95A8-9229FC74D46E}.Debug-ModLoaderOnly|Any CPU.ActiveCfg = Debug|x86
{B0BFEE3E-4A56-4F9F-95A8-9229FC74D46E}.Debug-ModLoaderOnly|Any CPU.Build.0 = Debug|x86
{B0BFEE3E-4A56-4F9F-95A8-9229FC74D46E}.Debug-ModLoaderOnly|X64+X86.ActiveCfg = Debug|x86
{B0BFEE3E-4A56-4F9F-95A8-9229FC74D46E}.Debug-ModLoaderOnly|X64+X86.Build.0 = Debug|x86
{B0BFEE3E-4A56-4F9F-95A8-9229FC74D46E}.Release|Any CPU.ActiveCfg = Release|x86
{B0BFEE3E-4A56-4F9F-95A8-9229FC74D46E}.Release|Any CPU.Build.0 = Release|x86
{B0BFEE3E-4A56-4F9F-95A8-9229FC74D46E}.Release|X64+X86.ActiveCfg = Release|x86
{B0BFEE3E-4A56-4F9F-95A8-9229FC74D46E}.Release|X64+X86.Build.0 = Release|x86
{B0BFEE3E-4A56-4F9F-95A8-9229FC74D46E}.Release-ModLoaderOnly|Any CPU.ActiveCfg = Release|x86
{B0BFEE3E-4A56-4F9F-95A8-9229FC74D46E}.Release-ModLoaderOnly|Any CPU.Build.0 = Release|x86
{B0BFEE3E-4A56-4F9F-95A8-9229FC74D46E}.Release-ModLoaderOnly|X64+X86.ActiveCfg = Release|x86
{B0BFEE3E-4A56-4F9F-95A8-9229FC74D46E}.Release-ModLoaderOnly|X64+X86.Build.0 = Release|x86
{E036F18E-312A-4C78-A8E9-5E37E5EBA9B9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E036F18E-312A-4C78-A8E9-5E37E5EBA9B9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E036F18E-312A-4C78-A8E9-5E37E5EBA9B9}.Debug|X64+X86.ActiveCfg = Debug|Any CPU
Expand Down
28 changes: 0 additions & 28 deletions source/Reloaded.Mod.Launcher.Kernel32AddressDumper/Program.cs

This file was deleted.

This file was deleted.

3 changes: 0 additions & 3 deletions source/Reloaded.Mod.Launcher.Kernel32AddressDumper/Usings.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="dll_syringe.Net.Sys" Version="0.16.0" />
<PackageReference Include="DotNet.ReproducibleBuilds" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="IoC.Container" Version="1.3.7" />
<PackageReference Include="McMaster.NETCore.Plugins" Version="2.0.0-beta.0" />
Expand Down
33 changes: 28 additions & 5 deletions source/Reloaded.Mod.Launcher.Lib/Utility/ApplicationInjector.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,58 @@
using dll_syringe.Net.Sys;

namespace Reloaded.Mod.Launcher.Lib.Utility;

/// <summary>
/// Class that can be used to inject Reloaded into an active process.
/// </summary>
public class ApplicationInjector
public unsafe class ApplicationInjector : IDisposable
{
private readonly int _modLoaderSetupTimeout;
private readonly int _modLoaderSetupSleepTime;

private Process _process;
private BasicDllInjector _injector;
private CSyringe* _syringe;

/// <summary/>
public ApplicationInjector(Process process)
{
_process = process;
_injector = new BasicDllInjector(process);
_syringe = NativeMethods.syringe_for_suspended_process((uint)_process.Id);

var loaderConfig = IoC.Get<LoaderConfig>();
_modLoaderSetupTimeout = loaderConfig.LoaderSetupTimeout;
_modLoaderSetupSleepTime = loaderConfig.LoaderSetupSleeptime;
}

~ApplicationInjector() => Dispose();

Check warning on line 27 in source/Reloaded.Mod.Launcher.Lib/Utility/ApplicationInjector.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'ApplicationInjector.~ApplicationInjector()'

/// <inheritdoc />
public void Dispose()
{
NativeMethods.syringe_free(_syringe);
GC.SuppressFinalize(this);
}

/// <summary>
/// Injects the Reloaded bootstrapper into an active process.
/// </summary>
/// <exception cref="ArgumentException">DLL Injection failed, likely due to bad DLL or application.</exception>
public void Inject()
{
long handle = _injector.Inject(GetBootstrapperPath(_process));
if (handle == 0)
// TODO: This is slow and wasteful, change this when we change encoding in injector.
var bootstrapperPath = GetBootstrapperPath(_process);
var bootstrapperPathBytes = Encoding.UTF8.GetBytes(bootstrapperPath);
var bootstrapperPathWithNull = new byte[bootstrapperPathBytes.Length + 1];
Array.Copy(bootstrapperPathBytes, bootstrapperPathWithNull, bootstrapperPathBytes.Length);
bootstrapperPathWithNull[bootstrapperPathBytes.Length] = 0;

bool success;
fixed(byte* bootstrapperPathPtr = bootstrapperPathWithNull)
{
success = NativeMethods.syringe_inject(_syringe, bootstrapperPathPtr);
}

if (!success)
throw new ArgumentException(Resources.ErrorDllInjectionFailed.Get());

try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void Start()

// DLL Injection
var process = Process.GetProcessById((int) processInformation.dwProcessId);
var injector = new ApplicationInjector(process);
using var injector = new ApplicationInjector(process);

try
{
Expand Down
2 changes: 1 addition & 1 deletion source/Reloaded.Mod.Launcher.Lib/Utility/AutoInjector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private void ProcessWatcherOnOnNewProcess(Process newProcess)
var config = _configService.Items.FirstOrDefault(x => string.Equals(ApplicationConfig.GetAbsoluteAppLocation(x), fullPath, StringComparison.OrdinalIgnoreCase));
if (config != null && config.Config.AutoInject)
{
var appInjector = new ApplicationInjector(newProcess);
using var appInjector = new ApplicationInjector(newProcess);
appInjector.Inject();
}
}
Expand Down
120 changes: 0 additions & 120 deletions source/Reloaded.Mod.Launcher.Lib/Utility/BasicDllInjector.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
var process = ViewModel.ApplicationViewModel.SelectedProcess;
if (!process!.HasExited)
{
var injector = new ApplicationInjector(ViewModel.ApplicationViewModel.SelectedProcess!);
using var injector = new ApplicationInjector(ViewModel.ApplicationViewModel.SelectedProcess!);
injector.Inject();

// Exit page.
Expand Down
7 changes: 0 additions & 7 deletions source/Reloaded.Mod.Loader.IO/Paths.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace Reloaded.Mod.Loader.IO;
public class Paths
{
private const string BootstrapperDllName = "Reloaded.Mod.Loader.Bootstrapper.dll";
private const string Kernel32AddressDumperRelativePath = "Loader/Kernel32AddressDumper.exe";

/// <summary>
/// Set to true if the currently executing application is the Reloaded launcher.
Expand Down Expand Up @@ -75,12 +74,6 @@ public class Paths

// UTILITY METHODS //

/// <summary>
/// Gets the name of the Kernel32 Address Dumper; a tool which extracts the address of Kernel32 and shares it using mapped files.
/// </summary>
/// <param name="launcherPath">Path to the launcher folder.</param>
public static string GetKernel32AddressDumperPath(string launcherPath) => Path.Combine(launcherPath, Kernel32AddressDumperRelativePath);

/// <summary>
/// Gets the path of the AnyCPU Reloaded Loader Folder.
/// </summary>
Expand Down
1 change: 0 additions & 1 deletion source/Reloaded.Mod.Shared/Reloaded.Mod.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)EntryPointParameters.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Environment.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SharedConstants.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Usings.cs" />
</ItemGroup>
</Project>
12 changes: 0 additions & 12 deletions source/Reloaded.Mod.Shared/SharedConstants.cs

This file was deleted.

0 comments on commit 572a66e

Please sign in to comment.