Skip to content

Commit

Permalink
Performance improvement for memory reading, fixed sound issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ConnorDaytonaHolmes authored and xMcacutt committed Apr 24, 2023
1 parent fa43695 commit affd9a2
Showing 1 changed file with 48 additions and 29 deletions.
77 changes: 48 additions & 29 deletions MulTyPlayerClient/Classes/Utility/ProcessHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@
using System.Linq;
using System.Reflection.Metadata;
using System.CodeDom;
using System.Collections;

namespace MulTyPlayerClient
{
internal unsafe class ProcessHandler
internal class ProcessHandler
{
public static IntPtr HProcess;
public static Process TyProcess;
private static nint TyProcessBaseAddress;
private static bool HasProcess;

public static bool MemoryWriteDebugLogging = false;
public static bool MemoryReadDebugLogging = false;
public static int i = 0;
Expand All @@ -25,7 +29,7 @@ internal unsafe class ProcessHandler
public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);

[DllImport("kernel32.dll")]
internal static extern bool ReadProcessMemory(
internal static unsafe extern bool ReadProcessMemory(
nint hProcess,
void* lpBaseAddress,
void* lpBuffer,
Expand All @@ -35,46 +39,61 @@ internal unsafe class ProcessHandler
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool WriteProcessMemory(IntPtr hProcess, int lpBaseAddress, byte[] lpBuffer, int dwSize, out IntPtr lpNumberOfBytesWritten);

public static void OpenTyHandle()
//Attempts to find the Ty process, returns true if successfully found
//Automatically opens handle if found
public static bool FindTyProcess(bool overrideOld = true)
{
HProcess = OpenProcess(0x1F0FFF, false, TyProcess.Id);
}
if (!overrideOld && HasProcess)
return true;

public static Process FindTyProcess()
{
TyProcess = Process.GetProcessesByName("Mul-Ty-Player").FirstOrDefault();
return TyProcess;
Process[] processes = Process.GetProcessesByName("Mul-Ty-Player");

if (processes.Length == 0)
{
//No process found
return false;
}
else if (processes.Length > 1)
{
//Multiple found
BasicIoC.LoggerInstance.Write($"WARNING: Multiple ({processes.Length}) instances of Mul-Ty-Player are open, can and will cause fuckery.");
}
TyProcess = processes.First();
TyProcess.Refresh();
TyProcess.EnableRaisingEvents = true;

TyProcess.Exited += (o, e) =>
{
TyProcess.Close();
TyProcess.Dispose();
HasProcess = false;
};

HProcess = OpenProcess(0x1F0FFF, false, TyProcess.Id);
TyProcessBaseAddress = TyProcess.MainModule.BaseAddress;
HasProcess = true;
return true;
}

public static void WriteData(int address, byte[] bytes, string writeIndicator)
{
try
{
bool success = WriteProcessMemory(HProcess, address, bytes, bytes.Length, out nint bytesWritten);
if (!success)
if (MemoryWriteDebugLogging)
{
if (MemoryWriteDebugLogging)
{
string errorMsg = "Failed to write " + BitConverter.ToString(bytes) + " to 0x" + address.ToString("X") + " For: " + writeIndicator;
BasicIoC.LoggerInstance.Write(errorMsg);
}
if (FindTyProcess() == null)
{
throw new TyClosedException();
}
string message = BitConverter.ToString(bytes) + " to 0x" + address.ToString("X") + " For: " + writeIndicator;
string logMsg = (success ? "Successfully wrote " : "Failed to write") + message;
BasicIoC.LoggerInstance.Write(logMsg);
}
else
if (!success && !HasProcess)
{
if (MemoryWriteDebugLogging)
{
string errorMsg = "Written " + BitConverter.ToString(bytes) + " to 0x" + address.ToString("X") + " For: " + writeIndicator;
BasicIoC.LoggerInstance.Write(errorMsg);
}
throw new TyClosedException();
}
}
catch(TyClosedException ex)
{
throw new TyClosedException();
throw ex;
}
}

Expand Down Expand Up @@ -120,7 +139,7 @@ public static unsafe bool TryRead<T>(nint address, out T result, bool addBase)
fixed (T* pResult = &result)
{
//string s = GetCallStackAsString();
if(addBase) address = TyProcess.MainModule.BaseAddress + address;
if(addBase) address = TyProcessBaseAddress + address;
nuint nSize = (nuint)sizeof(T), nRead;
//BasicIoC.LoggerInstance.Write(address.ToString() + " " + s);
return ReadProcessMemory(HProcess, (void*)address, pResult, nSize, &nRead)
Expand All @@ -129,7 +148,7 @@ public static unsafe bool TryRead<T>(nint address, out T result, bool addBase)
}
catch(TyClosedException ex)
{
throw new TyClosedException();
throw ex;
}
catch(Win32Exception ex)
{
Expand All @@ -138,7 +157,7 @@ public static unsafe bool TryRead<T>(nint address, out T result, bool addBase)
.Select(frame => frame.GetMethod())
.Any(method => method.Name == "ClientLoop"))
{
throw new TyClosedException();
BasicIoC.LoggerInstance.Write(ex.ToString());
}
result = default;
return false;
Expand Down

0 comments on commit affd9a2

Please sign in to comment.