Initial commit
A modern .NET 9 NativeAOT framework for Asheron's Call client modding. Self-contained injection system that hooks D3D9 EndScene to render an ImGui overlay inside the AC client process.
This commit is contained in:
commit
80b71d295d
179 changed files with 52858 additions and 0 deletions
663
src/RynthCore.Injector/EngineInjectionService.cs
Normal file
663
src/RynthCore.Injector/EngineInjectionService.cs
Normal file
|
|
@ -0,0 +1,663 @@
|
|||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RynthCore.Injector;
|
||||
|
||||
public sealed class EngineInjectionService
|
||||
{
|
||||
public const string DefaultAcProcessName = "acclient";
|
||||
public const string EngineDllName = "RynthCore.Engine.dll";
|
||||
private const string InitExport = "RynthCoreInit";
|
||||
private const string RuntimeDirectoryName = "Runtime";
|
||||
|
||||
private const uint ProcessAllAccess = 0x001F0FFF;
|
||||
private const uint CreateSuspended = 0x00000004;
|
||||
private const uint MemCommit = 0x1000;
|
||||
private const uint MemReserve = 0x2000;
|
||||
private const uint MemRelease = 0x8000;
|
||||
private const uint PageReadWrite = 0x04;
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
private static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle, int dwProcessId);
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
private static extern IntPtr VirtualAllocEx(
|
||||
IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
private static extern bool VirtualFreeEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint dwFreeType);
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
private static extern bool WriteProcessMemory(
|
||||
IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, uint nSize, out uint lpNumberOfBytesWritten);
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
private static extern IntPtr CreateRemoteThread(
|
||||
IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize,
|
||||
IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, out uint lpThreadId);
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
private static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
private static extern bool GetExitCodeThread(IntPtr hThread, out uint lpExitCode);
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
private static extern bool CloseHandle(IntPtr hObject);
|
||||
|
||||
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
private static extern bool CreateProcessW(
|
||||
string? lpApplicationName,
|
||||
System.Text.StringBuilder lpCommandLine,
|
||||
IntPtr lpProcessAttributes,
|
||||
IntPtr lpThreadAttributes,
|
||||
bool bInheritHandles,
|
||||
uint dwCreationFlags,
|
||||
IntPtr lpEnvironment,
|
||||
string? lpCurrentDirectory,
|
||||
ref STARTUPINFO lpStartupInfo,
|
||||
out PROCESS_INFORMATION lpProcessInformation);
|
||||
|
||||
[DllImport("kernel32.dll", CharSet = CharSet.Ansi)]
|
||||
private static extern IntPtr GetModuleHandleA(string lpModuleName);
|
||||
|
||||
[DllImport("kernel32.dll", CharSet = CharSet.Ansi)]
|
||||
private static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName);
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
private static extern uint ResumeThread(IntPtr hThread);
|
||||
|
||||
public string? TryResolveEnginePath(string? explicitEnginePath)
|
||||
{
|
||||
foreach (string path in GetEngineCandidatePaths(explicitEnginePath))
|
||||
{
|
||||
if (File.Exists(path))
|
||||
return path;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public Process[] FindTargetProcesses(string processName = DefaultAcProcessName)
|
||||
{
|
||||
return Process.GetProcessesByName(processName);
|
||||
}
|
||||
|
||||
public InjectionResult InjectFirstRunning(string enginePath, Action<string>? log = null, string processName = DefaultAcProcessName)
|
||||
{
|
||||
Process[] processes = FindTargetProcesses(processName);
|
||||
if (processes.Length == 0)
|
||||
return InjectionResult.Failure(1, $"{processName}.exe is not running.", enginePath);
|
||||
|
||||
return InjectIntoProcess(processes[0], enginePath, log);
|
||||
}
|
||||
|
||||
public async Task<bool> WaitForGraphicsReadyAsync(
|
||||
Process targetProcess,
|
||||
Action<string>? log = null,
|
||||
int timeoutMs = 120000,
|
||||
int stableReadyMs = 5000,
|
||||
int pollIntervalMs = 1000,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
log ??= _ => { };
|
||||
|
||||
DateTime deadline = DateTime.UtcNow.AddMilliseconds(timeoutMs);
|
||||
DateTime? readySince = null;
|
||||
string? lastStatus = null;
|
||||
|
||||
while (DateTime.UtcNow < deadline)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
bool isReady = TryDescribeGraphicsReadiness(targetProcess, out string status);
|
||||
if (!string.Equals(lastStatus, status, StringComparison.Ordinal))
|
||||
{
|
||||
log(status);
|
||||
lastStatus = status;
|
||||
}
|
||||
|
||||
if (isReady)
|
||||
{
|
||||
readySince ??= DateTime.UtcNow;
|
||||
if ((DateTime.UtcNow - readySince.Value).TotalMilliseconds >= stableReadyMs)
|
||||
{
|
||||
log("Auto-inject wait: graphics-ready state is stable.");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
readySince = null;
|
||||
}
|
||||
|
||||
await Task.Delay(pollIntervalMs, cancellationToken);
|
||||
}
|
||||
|
||||
log("Auto-inject wait: timed out before the AC client reached a graphics-ready state.");
|
||||
return false;
|
||||
}
|
||||
|
||||
public InjectionResult InjectIntoProcess(Process targetProcess, string enginePath, Action<string>? log = null)
|
||||
{
|
||||
log ??= _ => { };
|
||||
|
||||
if (string.IsNullOrWhiteSpace(enginePath))
|
||||
return InjectionResult.Failure(1, $"Engine DLL not found: {enginePath}", enginePath, targetProcess.Id);
|
||||
|
||||
if (!TryResolveInjectableEnginePath(enginePath, log, out string resolvedEnginePath, out uint initRva, out string resolveError))
|
||||
return InjectionResult.Failure(1, resolveError, enginePath, targetProcess.Id);
|
||||
|
||||
log($"Engine DLL: {resolvedEnginePath}");
|
||||
|
||||
string minhookPath = Path.Combine(Path.GetDirectoryName(resolvedEnginePath)!, "minhook.x86.dll");
|
||||
if (!File.Exists(minhookPath))
|
||||
log("WARNING: minhook.x86.dll not found next to engine DLL.");
|
||||
else
|
||||
log("MinHook found beside engine DLL.");
|
||||
|
||||
log($"{InitExport} RVA: 0x{initRva:X8}");
|
||||
|
||||
log($"Target PID: {targetProcess.Id} ({targetProcess.ProcessName})");
|
||||
|
||||
IntPtr hProcess = OpenProcess(ProcessAllAccess, false, targetProcess.Id);
|
||||
if (hProcess == IntPtr.Zero)
|
||||
{
|
||||
return InjectionResult.Failure(
|
||||
1,
|
||||
$"OpenProcess failed (error {Marshal.GetLastWin32Error()}). Try running as Administrator.",
|
||||
resolvedEnginePath,
|
||||
targetProcess.Id);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
IntPtr hKernel32 = GetModuleHandleA("kernel32.dll");
|
||||
IntPtr loadLibAddr = GetProcAddress(hKernel32, "LoadLibraryA");
|
||||
if (loadLibAddr == IntPtr.Zero)
|
||||
return InjectionResult.Failure(1, "Could not locate LoadLibraryA.", resolvedEnginePath, targetProcess.Id);
|
||||
|
||||
byte[] dllPathBytes = Encoding.ASCII.GetBytes(resolvedEnginePath + "\0");
|
||||
IntPtr remoteStr = VirtualAllocEx(hProcess, IntPtr.Zero, (uint)dllPathBytes.Length, MemCommit | MemReserve, PageReadWrite);
|
||||
if (remoteStr == IntPtr.Zero)
|
||||
return InjectionResult.Failure(1, "VirtualAllocEx failed for remote DLL path.", resolvedEnginePath, targetProcess.Id);
|
||||
|
||||
try
|
||||
{
|
||||
if (!WriteProcessMemory(hProcess, remoteStr, dllPathBytes, (uint)dllPathBytes.Length, out _))
|
||||
{
|
||||
return InjectionResult.Failure(
|
||||
1,
|
||||
$"WriteProcessMemory failed (error {Marshal.GetLastWin32Error()}).",
|
||||
resolvedEnginePath,
|
||||
targetProcess.Id);
|
||||
}
|
||||
|
||||
log("[1/2] Injecting engine DLL...");
|
||||
IntPtr loadThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, loadLibAddr, remoteStr, 0, out _);
|
||||
if (loadThread == IntPtr.Zero)
|
||||
{
|
||||
return InjectionResult.Failure(
|
||||
1,
|
||||
$"CreateRemoteThread for LoadLibraryA failed (error {Marshal.GetLastWin32Error()}).",
|
||||
resolvedEnginePath,
|
||||
targetProcess.Id);
|
||||
}
|
||||
|
||||
uint loadLibResult;
|
||||
try
|
||||
{
|
||||
WaitForSingleObject(loadThread, 10000);
|
||||
GetExitCodeThread(loadThread, out loadLibResult);
|
||||
}
|
||||
finally
|
||||
{
|
||||
CloseHandle(loadThread);
|
||||
}
|
||||
|
||||
if (loadLibResult == 0)
|
||||
{
|
||||
return InjectionResult.Failure(
|
||||
1,
|
||||
"LoadLibrary returned NULL. Check engine dependencies beside the DLL.",
|
||||
resolvedEnginePath,
|
||||
targetProcess.Id);
|
||||
}
|
||||
|
||||
IntPtr remoteBase = (IntPtr)loadLibResult;
|
||||
log($"[1/2] Engine mapped at 0x{remoteBase:X8}");
|
||||
|
||||
log("[2/2] Calling RynthCoreInit...");
|
||||
IntPtr remoteInitAddr = IntPtr.Add(remoteBase, (int)initRva);
|
||||
IntPtr initThread = CreateRemoteThread(hProcess, IntPtr.Zero, 0, remoteInitAddr, IntPtr.Zero, 0, out _);
|
||||
if (initThread == IntPtr.Zero)
|
||||
{
|
||||
return InjectionResult.Failure(
|
||||
1,
|
||||
$"CreateRemoteThread for {InitExport} failed (error {Marshal.GetLastWin32Error()}).",
|
||||
resolvedEnginePath,
|
||||
targetProcess.Id);
|
||||
}
|
||||
|
||||
uint initResult;
|
||||
try
|
||||
{
|
||||
WaitForSingleObject(initThread, 10000);
|
||||
GetExitCodeThread(initThread, out initResult);
|
||||
}
|
||||
finally
|
||||
{
|
||||
CloseHandle(initThread);
|
||||
}
|
||||
|
||||
log(initResult == 0 ? "RynthCoreInit returned success." : $"RynthCoreInit returned {initResult}.");
|
||||
return InjectionResult.SuccessResult("RynthCore injected successfully.", resolvedEnginePath, targetProcess.Id, initResult);
|
||||
}
|
||||
finally
|
||||
{
|
||||
VirtualFreeEx(hProcess, remoteStr, 0, MemRelease);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
CloseHandle(hProcess);
|
||||
}
|
||||
}
|
||||
|
||||
public InjectionResult LaunchSuspendedAndInject(
|
||||
string clientPath,
|
||||
string arguments,
|
||||
string enginePath,
|
||||
Action<string>? log = null,
|
||||
Action<int>? onProcessCreated = null)
|
||||
{
|
||||
log ??= _ => { };
|
||||
|
||||
if (string.IsNullOrWhiteSpace(clientPath) || !File.Exists(clientPath))
|
||||
return InjectionResult.Failure(1, $"AC client not found: {clientPath}", enginePath);
|
||||
|
||||
string workingDirectory = Path.GetDirectoryName(clientPath) ?? Environment.CurrentDirectory;
|
||||
string commandLine = string.IsNullOrWhiteSpace(arguments)
|
||||
? $"\"{clientPath}\""
|
||||
: $"\"{clientPath}\" {arguments}";
|
||||
|
||||
var startupInfo = new STARTUPINFO
|
||||
{
|
||||
cb = (uint)Marshal.SizeOf<STARTUPINFO>()
|
||||
};
|
||||
|
||||
if (!CreateProcessW(
|
||||
clientPath,
|
||||
new System.Text.StringBuilder(commandLine),
|
||||
IntPtr.Zero,
|
||||
IntPtr.Zero,
|
||||
false,
|
||||
CreateSuspended,
|
||||
IntPtr.Zero,
|
||||
workingDirectory,
|
||||
ref startupInfo,
|
||||
out PROCESS_INFORMATION processInfo))
|
||||
{
|
||||
return InjectionResult.Failure(
|
||||
1,
|
||||
$"CreateProcessW failed (error {Marshal.GetLastWin32Error()}).",
|
||||
enginePath);
|
||||
}
|
||||
|
||||
bool resumed = false;
|
||||
|
||||
void ResumeMainThread()
|
||||
{
|
||||
if (resumed)
|
||||
return;
|
||||
|
||||
uint resumeResult = ResumeThread(processInfo.hThread);
|
||||
if (resumeResult == 0xFFFFFFFF)
|
||||
throw new InvalidOperationException($"ResumeThread failed (error {Marshal.GetLastWin32Error()}).");
|
||||
|
||||
resumed = true;
|
||||
log($"Resumed AC main thread (PID {processInfo.dwProcessId}).");
|
||||
}
|
||||
|
||||
log($"Launched AC suspended (PID {processInfo.dwProcessId}).");
|
||||
log("Launching suspended so RynthCore can patch early startup gates before AC runs its single-instance checks.");
|
||||
onProcessCreated?.Invoke((int)processInfo.dwProcessId);
|
||||
|
||||
try
|
||||
{
|
||||
using var launchedProcess = Process.GetProcessById((int)processInfo.dwProcessId);
|
||||
InjectionResult injectResult = InjectIntoProcess(launchedProcess, enginePath, log);
|
||||
ResumeMainThread();
|
||||
|
||||
if (!injectResult.Success)
|
||||
return InjectionResult.Failure(injectResult.ExitCode, injectResult.Summary, injectResult.EnginePath, (int)processInfo.dwProcessId);
|
||||
|
||||
return InjectionResult.SuccessResult(
|
||||
"Launched AC and injected RynthCore successfully.",
|
||||
injectResult.EnginePath,
|
||||
(int)processInfo.dwProcessId,
|
||||
injectResult.InitResult ?? 0);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
try
|
||||
{
|
||||
ResumeMainThread();
|
||||
}
|
||||
catch (Exception resumeEx)
|
||||
{
|
||||
return InjectionResult.Failure(
|
||||
1,
|
||||
$"Launch + inject failed: {ex.Message} ResumeThread also failed: {resumeEx.Message}",
|
||||
enginePath,
|
||||
(int)processInfo.dwProcessId);
|
||||
}
|
||||
|
||||
return InjectionResult.Failure(
|
||||
1,
|
||||
$"Launch + inject failed: {ex.Message}",
|
||||
enginePath,
|
||||
(int)processInfo.dwProcessId);
|
||||
}
|
||||
finally
|
||||
{
|
||||
CloseHandle(processInfo.hThread);
|
||||
CloseHandle(processInfo.hProcess);
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerable<string> GetEngineCandidatePaths(string? explicitEnginePath)
|
||||
{
|
||||
var seen = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
void AddCandidate(List<string> candidates, string? path)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(path))
|
||||
return;
|
||||
|
||||
string fullPath = Path.GetFullPath(path);
|
||||
if (seen.Add(fullPath))
|
||||
candidates.Add(fullPath);
|
||||
}
|
||||
|
||||
void AddEngineDirectoryCandidates(List<string> candidates, string? directory)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(directory))
|
||||
return;
|
||||
|
||||
AddCandidate(candidates, Path.Combine(directory, EngineDllName));
|
||||
AddCandidate(candidates, Path.Combine(directory, "publish", EngineDllName));
|
||||
AddCandidate(candidates, Path.Combine(directory, "native", EngineDllName));
|
||||
AddCandidate(candidates, Path.Combine(directory, "Native", EngineDllName));
|
||||
}
|
||||
|
||||
void AddLauncherLayoutCandidates(List<string> candidates, string? directory)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(directory))
|
||||
return;
|
||||
|
||||
AddEngineDirectoryCandidates(candidates, directory);
|
||||
AddEngineDirectoryCandidates(candidates, Path.Combine(directory, RuntimeDirectoryName));
|
||||
AddEngineDirectoryCandidates(candidates, Path.Combine(directory, RuntimeDirectoryName, "Engine"));
|
||||
}
|
||||
|
||||
var candidates = new List<string>();
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(explicitEnginePath))
|
||||
{
|
||||
string explicitFullPath = Path.GetFullPath(explicitEnginePath);
|
||||
string? runtimeSibling = TryGetRuntimeSiblingEnginePath(explicitFullPath);
|
||||
AddCandidate(candidates, runtimeSibling);
|
||||
AddCandidate(candidates, explicitFullPath);
|
||||
|
||||
string? explicitDir = Path.GetDirectoryName(explicitFullPath);
|
||||
if (!string.IsNullOrWhiteSpace(explicitDir))
|
||||
{
|
||||
AddLauncherLayoutCandidates(candidates, explicitDir);
|
||||
|
||||
string? parentDir = Directory.GetParent(explicitDir)?.FullName;
|
||||
if (!string.IsNullOrWhiteSpace(parentDir))
|
||||
AddLauncherLayoutCandidates(candidates, parentDir);
|
||||
}
|
||||
}
|
||||
|
||||
string cwd = Environment.CurrentDirectory;
|
||||
string baseDir = AppDomain.CurrentDomain.BaseDirectory;
|
||||
string exeDir = Path.GetDirectoryName(Environment.ProcessPath ?? string.Empty) ?? string.Empty;
|
||||
|
||||
AddLauncherLayoutCandidates(candidates, baseDir);
|
||||
AddLauncherLayoutCandidates(candidates, exeDir);
|
||||
AddLauncherLayoutCandidates(candidates, cwd);
|
||||
AddEngineDirectoryCandidates(candidates, Path.Combine(cwd, "src", "RynthCore.Engine", "bin", "x86", "Release", "net9.0-windows", "win-x86"));
|
||||
AddEngineDirectoryCandidates(candidates, Path.Combine(cwd, "src", "RynthCore.Engine", "bin", "Release", "net9.0-windows", "win-x86"));
|
||||
AddEngineDirectoryCandidates(candidates, Path.Combine(baseDir, "..", "..", "..", "..", "RynthCore.Engine", "bin", "x86", "Release", "net9.0-windows", "win-x86"));
|
||||
AddEngineDirectoryCandidates(candidates, Path.Combine(baseDir, "..", "..", "..", "..", "RynthCore.Engine", "bin", "Release", "net9.0-windows", "win-x86"));
|
||||
AddEngineDirectoryCandidates(candidates, Path.Combine(baseDir, "..", "RynthCore.Engine", "bin", "Release", "net9.0-windows", "win-x86"));
|
||||
|
||||
return candidates;
|
||||
}
|
||||
|
||||
private static string? TryGetRuntimeSiblingEnginePath(string enginePath)
|
||||
{
|
||||
if (!string.Equals(Path.GetFileName(enginePath), EngineDllName, StringComparison.OrdinalIgnoreCase))
|
||||
return null;
|
||||
|
||||
string? engineDir = Path.GetDirectoryName(enginePath);
|
||||
if (string.IsNullOrWhiteSpace(engineDir))
|
||||
return null;
|
||||
|
||||
if (string.Equals(Path.GetFileName(Path.TrimEndingDirectorySeparator(engineDir)), RuntimeDirectoryName, StringComparison.OrdinalIgnoreCase))
|
||||
return null;
|
||||
|
||||
string runtimeSibling = Path.Combine(engineDir, RuntimeDirectoryName, EngineDllName);
|
||||
return File.Exists(runtimeSibling) ? runtimeSibling : null;
|
||||
}
|
||||
|
||||
private bool TryResolveInjectableEnginePath(
|
||||
string requestedEnginePath,
|
||||
Action<string> log,
|
||||
out string resolvedEnginePath,
|
||||
out uint initRva,
|
||||
out string error)
|
||||
{
|
||||
string? lastError = null;
|
||||
|
||||
foreach (string candidate in GetEngineCandidatePaths(requestedEnginePath))
|
||||
{
|
||||
if (!File.Exists(candidate))
|
||||
continue;
|
||||
|
||||
try
|
||||
{
|
||||
uint candidateRva = GetExportRva(candidate, InitExport);
|
||||
resolvedEnginePath = candidate;
|
||||
initRva = candidateRva;
|
||||
error = string.Empty;
|
||||
|
||||
if (!string.Equals(Path.GetFullPath(requestedEnginePath), candidate, StringComparison.OrdinalIgnoreCase))
|
||||
log($"Using native engine candidate: {candidate}");
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
lastError = ex.Message;
|
||||
}
|
||||
}
|
||||
|
||||
resolvedEnginePath = Path.GetFullPath(requestedEnginePath);
|
||||
initRva = 0;
|
||||
error = lastError ?? $"Engine DLL not found: {requestedEnginePath}";
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool TryDescribeGraphicsReadiness(Process targetProcess, out string status)
|
||||
{
|
||||
try
|
||||
{
|
||||
targetProcess.Refresh();
|
||||
|
||||
if (targetProcess.HasExited)
|
||||
{
|
||||
status = "Auto-inject wait: AC process exited before injection.";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (targetProcess.MainWindowHandle == IntPtr.Zero)
|
||||
{
|
||||
status = "Auto-inject wait: waiting for the AC game window.";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!targetProcess.Responding)
|
||||
{
|
||||
status = "Auto-inject wait: waiting for the AC window to respond.";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!IsModuleLoaded(targetProcess, "d3d9.dll"))
|
||||
{
|
||||
status = "Auto-inject wait: waiting for Direct3D 9 to initialize.";
|
||||
return false;
|
||||
}
|
||||
|
||||
status = "Auto-inject wait: AC window and Direct3D 9 are ready.";
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
status = $"Auto-inject wait: readiness probe failed ({ex.Message}).";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static uint GetExportRva(string dllPath, string exportName)
|
||||
{
|
||||
byte[] pe = File.ReadAllBytes(dllPath);
|
||||
int peOffset = BitConverter.ToInt32(pe, 0x3C);
|
||||
|
||||
if (pe[peOffset] != 'P' || pe[peOffset + 1] != 'E')
|
||||
throw new InvalidOperationException("Invalid PE signature.");
|
||||
|
||||
int coffHeader = peOffset + 4;
|
||||
ushort numSections = BitConverter.ToUInt16(pe, coffHeader + 2);
|
||||
ushort optionalHeaderSize = BitConverter.ToUInt16(pe, coffHeader + 16);
|
||||
int optionalHeader = coffHeader + 20;
|
||||
|
||||
ushort magic = BitConverter.ToUInt16(pe, optionalHeader);
|
||||
if (magic != 0x10B)
|
||||
throw new InvalidOperationException($"Expected x86 PE32 image, got 0x{magic:X4}.");
|
||||
|
||||
int dataDirectoryOffset = optionalHeader + 96;
|
||||
uint exportDirRva = BitConverter.ToUInt32(pe, dataDirectoryOffset);
|
||||
if (exportDirRva == 0)
|
||||
throw new InvalidOperationException("No export directory found.");
|
||||
|
||||
int sectionHeaders = optionalHeader + optionalHeaderSize;
|
||||
uint exportDirFileOffset = RvaToFileOffset(pe, sectionHeaders, numSections, exportDirRva);
|
||||
|
||||
uint numberOfNames = BitConverter.ToUInt32(pe, (int)exportDirFileOffset + 24);
|
||||
uint addressOfFunctions = BitConverter.ToUInt32(pe, (int)exportDirFileOffset + 28);
|
||||
uint addressOfNames = BitConverter.ToUInt32(pe, (int)exportDirFileOffset + 32);
|
||||
uint addressOfNameOrdinals = BitConverter.ToUInt32(pe, (int)exportDirFileOffset + 36);
|
||||
|
||||
uint namesFileOffset = RvaToFileOffset(pe, sectionHeaders, numSections, addressOfNames);
|
||||
uint ordinalsFileOffset = RvaToFileOffset(pe, sectionHeaders, numSections, addressOfNameOrdinals);
|
||||
uint functionsFileOffset = RvaToFileOffset(pe, sectionHeaders, numSections, addressOfFunctions);
|
||||
|
||||
for (uint i = 0; i < numberOfNames; i++)
|
||||
{
|
||||
uint nameRva = BitConverter.ToUInt32(pe, (int)namesFileOffset + (int)(i * 4));
|
||||
uint nameOffset = RvaToFileOffset(pe, sectionHeaders, numSections, nameRva);
|
||||
|
||||
int end = (int)nameOffset;
|
||||
while (end < pe.Length && pe[end] != 0)
|
||||
end++;
|
||||
|
||||
string name = Encoding.ASCII.GetString(pe, (int)nameOffset, end - (int)nameOffset);
|
||||
if (!string.Equals(name, exportName, StringComparison.Ordinal))
|
||||
continue;
|
||||
|
||||
ushort ordinalIndex = BitConverter.ToUInt16(pe, (int)ordinalsFileOffset + (int)(i * 2));
|
||||
return BitConverter.ToUInt32(pe, (int)functionsFileOffset + ordinalIndex * 4);
|
||||
}
|
||||
|
||||
throw new InvalidOperationException($"Export '{exportName}' not found in {Path.GetFileName(dllPath)}.");
|
||||
}
|
||||
|
||||
private static uint RvaToFileOffset(byte[] pe, int sectionHeaders, ushort numSections, uint rva)
|
||||
{
|
||||
for (int i = 0; i < numSections; i++)
|
||||
{
|
||||
int sectionHeader = sectionHeaders + (i * 40);
|
||||
uint virtualAddress = BitConverter.ToUInt32(pe, sectionHeader + 12);
|
||||
uint virtualSize = BitConverter.ToUInt32(pe, sectionHeader + 8);
|
||||
uint rawDataPointer = BitConverter.ToUInt32(pe, sectionHeader + 20);
|
||||
|
||||
if (rva >= virtualAddress && rva < virtualAddress + virtualSize)
|
||||
return rva - virtualAddress + rawDataPointer;
|
||||
}
|
||||
|
||||
throw new InvalidOperationException($"Cannot map RVA 0x{rva:X8} to file offset.");
|
||||
}
|
||||
|
||||
private static bool IsModuleLoaded(Process targetProcess, string moduleName)
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (ProcessModule module in targetProcess.Modules)
|
||||
{
|
||||
if (string.Equals(module.ModuleName, moduleName, StringComparison.OrdinalIgnoreCase))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
|
||||
private struct STARTUPINFO
|
||||
{
|
||||
public uint cb;
|
||||
public string? lpReserved;
|
||||
public string? lpDesktop;
|
||||
public string? lpTitle;
|
||||
public uint dwX;
|
||||
public uint dwY;
|
||||
public uint dwXSize;
|
||||
public uint dwYSize;
|
||||
public uint dwXCountChars;
|
||||
public uint dwYCountChars;
|
||||
public uint dwFillAttribute;
|
||||
public uint dwFlags;
|
||||
public ushort wShowWindow;
|
||||
public ushort cbReserved2;
|
||||
public IntPtr lpReserved2;
|
||||
public IntPtr hStdInput;
|
||||
public IntPtr hStdOutput;
|
||||
public IntPtr hStdError;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
private struct PROCESS_INFORMATION
|
||||
{
|
||||
public IntPtr hProcess;
|
||||
public IntPtr hThread;
|
||||
public uint dwProcessId;
|
||||
public uint dwThreadId;
|
||||
}
|
||||
}
|
||||
38
src/RynthCore.Injector/InjectionResult.cs
Normal file
38
src/RynthCore.Injector/InjectionResult.cs
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
using System;
|
||||
|
||||
namespace RynthCore.Injector;
|
||||
|
||||
public sealed class InjectionResult
|
||||
{
|
||||
public bool Success { get; init; }
|
||||
public int ExitCode { get; init; }
|
||||
public string Summary { get; init; } = string.Empty;
|
||||
public string EnginePath { get; init; } = string.Empty;
|
||||
public int? ProcessId { get; init; }
|
||||
public uint? InitResult { get; init; }
|
||||
|
||||
public static InjectionResult Failure(int exitCode, string summary, string enginePath = "", int? processId = null)
|
||||
{
|
||||
return new InjectionResult
|
||||
{
|
||||
Success = false,
|
||||
ExitCode = exitCode,
|
||||
Summary = summary,
|
||||
EnginePath = enginePath,
|
||||
ProcessId = processId
|
||||
};
|
||||
}
|
||||
|
||||
public static InjectionResult SuccessResult(string summary, string enginePath, int processId, uint initResult)
|
||||
{
|
||||
return new InjectionResult
|
||||
{
|
||||
Success = true,
|
||||
ExitCode = 0,
|
||||
Summary = summary,
|
||||
EnginePath = enginePath,
|
||||
ProcessId = processId,
|
||||
InitResult = initResult
|
||||
};
|
||||
}
|
||||
}
|
||||
65
src/RynthCore.Injector/Program.cs
Normal file
65
src/RynthCore.Injector/Program.cs
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
using System;
|
||||
|
||||
namespace RynthCore.Injector;
|
||||
|
||||
internal static class Program
|
||||
{
|
||||
private static int Main(string[] args)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Run(args);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine();
|
||||
Console.WriteLine($"FATAL EXCEPTION: {ex}");
|
||||
Console.ResetColor();
|
||||
return 99;
|
||||
}
|
||||
finally
|
||||
{
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("Press any key to exit...");
|
||||
Console.ReadKey(true);
|
||||
}
|
||||
}
|
||||
|
||||
private static int Run(string[] args)
|
||||
{
|
||||
var service = new EngineInjectionService();
|
||||
|
||||
Console.WriteLine("========================================");
|
||||
Console.WriteLine(" RynthCore Injector Console ");
|
||||
Console.WriteLine("========================================");
|
||||
Console.WriteLine();
|
||||
|
||||
string? enginePath = service.TryResolveEnginePath(args.Length > 0 ? args[0] : null);
|
||||
if (enginePath == null)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine($"Could not locate {EngineInjectionService.EngineDllName}.");
|
||||
Console.WriteLine("Copy it next to the injector or pass the full path as the first argument.");
|
||||
Console.ResetColor();
|
||||
return 1;
|
||||
}
|
||||
|
||||
InjectionResult result = service.InjectFirstRunning(enginePath, Console.WriteLine);
|
||||
Console.WriteLine();
|
||||
|
||||
if (result.Success)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
Console.WriteLine(result.Summary);
|
||||
Console.WriteLine("Check Desktop\\RynthCore.log for in-process status.");
|
||||
Console.ResetColor();
|
||||
return 0;
|
||||
}
|
||||
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine(result.Summary);
|
||||
Console.ResetColor();
|
||||
return result.ExitCode;
|
||||
}
|
||||
}
|
||||
13
src/RynthCore.Injector/RynthCore.Injector.csproj
Normal file
13
src/RynthCore.Injector/RynthCore.Injector.csproj
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net9.0-windows</TargetFramework>
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
<RootNamespace>RynthCore.Injector</RootNamespace>
|
||||
<AssemblyName>RynthCore.Injector</AssemblyName>
|
||||
<Platforms>x86</Platforms>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
Loading…
Reference in a new issue