//delayReducer
//Proof-of-concept hosting latency editor
//Copyright (c) 2008, E.Sandberg (Risc)
//Released under the GPL (http://www.gnu.org/licenses/gpl.txt)
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace DelayReducerConsole
{
class Program
{
private const int PROCESS_ALL_ACCESS = 0x1F0FFF;
#region Win32Imports
[DllImport("kernel32.dll")]
private static extern int ReadProcessMemory(int hProcess, int lpBaseAddress, out int lpBuffer, int nSize,
int lpNumberOfBytesWritten);
[DllImport("kernel32.dll")]
private static extern int WriteProcessMemory(int hProcess, int lpBaseAddress, byte[] lpBuffer, int nSize,
ref int lpNumberOfBytesWritten);
[DllImport("kernel32.dll")]
private static extern int OpenProcess(int dwDesiredAccess, int bInheritHandle, int dwProcessId);
[StructLayout(LayoutKind.Sequential)]
private struct LUID
{
internal int LowPart;
internal int HighPart;
}
private const int ANYSIZE_ARRAY = 1;
[StructLayout(LayoutKind.Sequential)]
private struct LUID_AND_ATTRIBUTES
{
internal LUID pLuid;
internal int Attributes;
}
struct TOKEN_PRIVILEGES
{
public int PrivilegeCount;
public LUID TheLuid;
public int Attributes;
}
[DllImport("advapi32.dll")]
private static extern int OpenProcessToken(int ProcessHandle, int DesiredAccess, ref int TokenHandle);
[DllImport("advapi32.dll", EntryPoint = "LookupPrivilegeValueA")]
private static extern int LookupPrivilegeValue(string lpSystemName, string lpName, ref LUID lpLuid);
[DllImport("advapi32.dll")]
private static extern int AdjustTokenPrivileges(int TokenHandle, int DisableAllPrivileges, ref
TOKEN_PRIVILEGES NewState, int BufferLength, int PreviousState, int ReturnLength);
[DllImport("kernel32.dll")]
private static extern int CloseHandle(int hObject);
private const int TOKEN_ADJUST_PRIVILEGES = 0x20;
private const int TOKEN_QUERY = 0x8;
private const int SE_PRIVILEGE_ENABLED = 0x2;
private const string SE_DEBUG_Name = "SeDebugPrivilege";
#endregion
static void Main(string[] args)
{
Console.WriteLine("DelayReducer 'proof-of-concept' by Risc (2008)");
Console.WriteLine("Searching for Wc3 process...");
Process[] pids = Process.GetProcessesByName("war3");
int proc;
int hToken = 0;
LUID sedebugNameValue = new LUID();
TOKEN_PRIVILEGES tkp = new TOKEN_PRIVILEGES();
OpenProcessToken(Process.GetCurrentProcess().Handle.ToInt32(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
ref hToken);
LookupPrivilegeValue(null, SE_DEBUG_Name, ref sedebugNameValue);
tkp.PrivilegeCount = 1;
tkp.TheLuid = sedebugNameValue;
tkp.Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, 0, ref tkp, Marshal.SizeOf(tkp), 0, 0);
CloseHandle(hToken);
Int32 delay;
int newdelay = 0;
int byteswritten = 0;
if (pids.Length > 0)
{
Console.WriteLine("Found process.");
proc = OpenProcess(PROCESS_ALL_ACCESS, 0, pids[0].Id);
ReadProcessMemory(proc, 0x6F6E5271, out delay, 4, 0);
Console.WriteLine("Current hosting latency is " + delay.ToString() + " ms");
Console.Write("Set latency to: ");
try
{
newdelay = int.Parse(Console.ReadLine());
WriteProcessMemory(proc, 0x6F6E5271, BitConverter.GetBytes(newdelay), 4, ref byteswritten);
ReadProcessMemory(proc, 0x6F6E5271, out delay, 4, 0);
if (delay == newdelay)
{
Console.WriteLine("Successfully set latency to " + delay.ToString() + " ms.");
Console.WriteLine("Press any key to quit.");
Console.ReadKey();
}
else
{
Console.WriteLine("FAILED to set latency to " + delay.ToString() + " ms.");
Console.WriteLine("Press any key to quit.");
Console.ReadKey();
}
}
catch
{
Console.WriteLine("God... OF COURSE you can only enter numbers without decimals!");
Console.WriteLine("Quitting...\n\nTry again, press any key to quit.");
Console.ReadKey();
}
}
else
{
Console.WriteLine("Can't find Wc3 process.. Make sure it's running first!\n\nTry again, press any
key to quit.");
Console.ReadKey();
}
}
}
}