diff --git a/MulTyPlayerClient/Classes/Utility/ProcessHandler.cs b/MulTyPlayerClient/Classes/Utility/ProcessHandler.cs index a8308ab0..7be9c3e8 100644 --- a/MulTyPlayerClient/Classes/Utility/ProcessHandler.cs +++ b/MulTyPlayerClient/Classes/Utility/ProcessHandler.cs @@ -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; @@ -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, @@ -35,15 +39,40 @@ 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) @@ -51,30 +80,20 @@ 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; } } @@ -120,7 +139,7 @@ public static unsafe bool TryRead(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) @@ -129,7 +148,7 @@ public static unsafe bool TryRead(nint address, out T result, bool addBase) } catch(TyClosedException ex) { - throw new TyClosedException(); + throw ex; } catch(Win32Exception ex) { @@ -138,7 +157,7 @@ public static unsafe bool TryRead(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;