diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index e1ef87a..f14b3d5 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -40,6 +40,10 @@ public partial class MainWindow : Window, IComponentConnector private TextBlock[] player_name_tbs = new TextBlock[4]; private TextBlock[] player_dmg_tbs = new TextBlock[4]; private double last_activated = MainWindow.time(); + private static byte?[] pattern_1; + private static byte?[] pattern_2; + private static byte?[] pattern_3; + private static byte?[] pattern_4; private Process game; private bool init_finished; private bool in_quest = false; @@ -52,6 +56,27 @@ public MainWindow() this.WindowStyle = WindowStyle.None; this.Background = (Brush)Brushes.Transparent; this.find_game_proc(); + ulong[] patterns = memory.find_patterns(this.game, (IntPtr)5368725504L, (IntPtr)5452595200L, new List() + { + MainWindow.pattern_1, + MainWindow.pattern_2, + MainWindow.pattern_3, + MainWindow.pattern_4 + }); + MainWindow.assert(patterns[0] > 5369757695UL && patterns[1] > 5369757695UL && patterns[1] > 5369757695UL && patterns[3] > 5369757695UL, "failed to locate offsets (step 1).", true); + ulong num1 = patterns[0] + (ulong)mhw.read_uint(this.game.Handle, (IntPtr)((long)patterns[0] + 2L)) + 6UL; + ulong num2 = patterns[1] + 51UL + (ulong)mhw.read_uint(this.game.Handle, (IntPtr)((long)patterns[1] + 54L)) + 7UL; + ulong num3 = patterns[2] + 15UL + (ulong)mhw.read_uint(this.game.Handle, (IntPtr)((long)patterns[2] + 15L + 2L)) + 6UL; + ulong num4 = patterns[3] + (ulong)mhw.read_uint(this.game.Handle, (IntPtr)((long)patterns[3] + 3L)) + 7UL; + Console.WriteLine(num1.ToString("X")); + Console.WriteLine(num2.ToString("X")); + Console.WriteLine(num3.ToString("X")); + Console.WriteLine(num4.ToString("X")); + MainWindow.assert(num1 > 5368725504UL && num1 < 5637144576UL && (num2 > 5368725504UL && num2 < 5637144576UL) && (num3 > 5368725504UL && num3 < 5637144576UL && num4 > 5368725504UL) && num4 < 5637144576UL, "failed to locate offsets (step 2).", true); + mhw.loc1 = (long)num1; + mhw.loc2 = (long)num2; + mhw.loc3 = (long)num3; + mhw.loc4 = (long)num4; this.InitializeComponent(); } @@ -318,5 +343,124 @@ private void Window_MouseWheel(object sender, MouseWheelEventArgs e) } } + static MainWindow() + { + byte?[] nullableArray1 = new byte?[26]; + nullableArray1[0] = new byte?((byte)139); + nullableArray1[1] = new byte?((byte)13); + nullableArray1[6] = new byte?((byte)35); + nullableArray1[7] = new byte?((byte)202); + nullableArray1[8] = new byte?((byte)129); + nullableArray1[9] = new byte?((byte)249); + nullableArray1[10] = new byte?((byte)0); + nullableArray1[11] = new byte?((byte)1); + nullableArray1[12] = new byte?((byte)0); + nullableArray1[13] = new byte?((byte)0); + nullableArray1[14] = new byte?((byte)115); + nullableArray1[15] = new byte?((byte)47); + nullableArray1[16] = new byte?((byte)15); + nullableArray1[17] = new byte?((byte)183); + nullableArray1[23] = new byte?((byte)193); + nullableArray1[24] = new byte?((byte)234); + nullableArray1[25] = new byte?((byte)16); + MainWindow.pattern_1 = nullableArray1; + byte?[] nullableArray2 = new byte?[58]; + nullableArray2[0] = new byte?((byte)72); + nullableArray2[1] = new byte?((byte)137); + nullableArray2[2] = new byte?((byte)116); + nullableArray2[3] = new byte?((byte)36); + nullableArray2[4] = new byte?((byte)56); + nullableArray2[5] = new byte?((byte)139); + nullableArray2[6] = new byte?((byte)112); + nullableArray2[7] = new byte?((byte)24); + nullableArray2[8] = new byte?((byte)72); + nullableArray2[9] = new byte?((byte)139); + nullableArray2[15] = new byte?((byte)137); + nullableArray2[16] = new byte?((byte)136); + nullableArray2[17] = new byte?((byte)12); + nullableArray2[18] = new byte?((byte)5); + nullableArray2[19] = new byte?((byte)0); + nullableArray2[20] = new byte?((byte)0); + nullableArray2[21] = new byte?((byte)72); + nullableArray2[22] = new byte?((byte)139); + nullableArray2[28] = new byte?((byte)137); + nullableArray2[29] = new byte?((byte)144); + nullableArray2[30] = new byte?((byte)16); + nullableArray2[31] = new byte?((byte)5); + nullableArray2[32] = new byte?((byte)0); + nullableArray2[33] = new byte?((byte)0); + nullableArray2[34] = new byte?((byte)72); + nullableArray2[35] = new byte?((byte)139); + nullableArray2[41] = new byte?((byte)137); + nullableArray2[42] = new byte?((byte)152); + nullableArray2[43] = new byte?((byte)20); + nullableArray2[44] = new byte?((byte)5); + nullableArray2[45] = new byte?((byte)0); + nullableArray2[46] = new byte?((byte)0); + nullableArray2[47] = new byte?((byte)133); + nullableArray2[48] = new byte?((byte)219); + nullableArray2[49] = new byte?((byte)126); + nullableArray2[51] = new byte?((byte)72); + nullableArray2[52] = new byte?((byte)139); + MainWindow.pattern_2 = nullableArray2; + byte?[] nullableArray3 = new byte?[21]; + nullableArray3[0] = new byte?((byte)178); + nullableArray3[1] = new byte?((byte)172); + nullableArray3[2] = new byte?((byte)11); + nullableArray3[3] = new byte?((byte)0); + nullableArray3[4] = new byte?((byte)0); + nullableArray3[5] = new byte?((byte)73); + nullableArray3[6] = new byte?((byte)139); + nullableArray3[7] = new byte?((byte)217); + nullableArray3[8] = new byte?((byte)139); + nullableArray3[9] = new byte?((byte)81); + nullableArray3[10] = new byte?((byte)84); + nullableArray3[11] = new byte?((byte)73); + nullableArray3[12] = new byte?((byte)139); + nullableArray3[13] = new byte?((byte)248); + nullableArray3[14] = new byte?((byte)72); + nullableArray3[15] = new byte?((byte)139); + nullableArray3[16] = new byte?((byte)13); + MainWindow.pattern_3 = nullableArray3; + byte?[] nullableArray4 = new byte?[37]; + nullableArray4[0] = new byte?((byte)72); + nullableArray4[1] = new byte?((byte)139); + nullableArray4[2] = new byte?((byte)13); + nullableArray4[7] = new byte?((byte)72); + nullableArray4[8] = new byte?((byte)141); + nullableArray4[9] = new byte?((byte)84); + nullableArray4[10] = new byte?((byte)36); + nullableArray4[11] = new byte?((byte)56); + nullableArray4[12] = new byte?((byte)198); + nullableArray4[13] = new byte?((byte)68); + nullableArray4[14] = new byte?((byte)36); + nullableArray4[15] = new byte?((byte)32); + nullableArray4[16] = new byte?((byte)0); + nullableArray4[17] = new byte?((byte)77); + nullableArray4[18] = new byte?((byte)139); + nullableArray4[19] = new byte?((byte)64); + nullableArray4[20] = new byte?((byte)8); + nullableArray4[21] = new byte?((byte)232); + nullableArray4[26] = new byte?((byte)72); + nullableArray4[27] = new byte?((byte)139); + nullableArray4[28] = new byte?((byte)92); + nullableArray4[29] = new byte?((byte)36); + nullableArray4[30] = new byte?((byte)96); + nullableArray4[31] = new byte?((byte)72); + nullableArray4[32] = new byte?((byte)131); + nullableArray4[33] = new byte?((byte)196); + nullableArray4[34] = new byte?((byte)80); + nullableArray4[35] = new byte?((byte)95); + nullableArray4[36] = new byte?((byte)195); + MainWindow.pattern_4 = nullableArray4; + // MainWindow.player_colors = new Color[4] + // { + //Color.FromRgb((byte) 225, (byte) 65, (byte) 55), + //Color.FromRgb((byte) 53, (byte) 136, (byte) 227), + //Color.FromRgb((byte) 196, (byte) 172, (byte) 44), + //Color.FromRgb((byte) 42, (byte) 208, (byte) 55) + // }; + } + } } diff --git a/memory.cs b/memory.cs new file mode 100644 index 0000000..263f596 --- /dev/null +++ b/memory.cs @@ -0,0 +1,108 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Runtime.InteropServices; + +public static class memory +{ + [DllImport("kernel32.dll")] + private static extern int VirtualQueryEx(IntPtr hProcess, IntPtr lpAddress, out memory.MEMORY_BASIC_INFORMATION64 lpBuffer, uint dwLength); + + [DllImport("kernel32.dll")] + public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, int dwSize, ref int lpNumberOfBytesRead); + + public static bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer) + { + int lpNumberOfBytesRead = 0; + return memory.ReadProcessMemory(hProcess, lpBaseAddress, lpBuffer, lpBuffer.Length, ref lpNumberOfBytesRead); + } + + private static List byte_find(byte[] src, byte[] pattern) + { + List intList = new List(); + if (src.Length < pattern.Length) + return intList; + for (int index1 = 0; index1 < src.Length - pattern.Length + 1; ++index1) + { + bool flag = true; + for (int index2 = 0; index2 < pattern.Length; ++index2) + { + if ((int)src[index1 + index2] != (int)pattern[index2]) + flag = false; + } + if (flag) + intList.Add(index1); + } + return intList; + } + + private static int byte_find_first(byte[] src, byte?[] pattern) + { + List intList = new List(); + if (src.Length < pattern.Length) + return -1; + for (int index1 = 0; index1 < src.Length - pattern.Length + 1; ++index1) + { + bool flag = true; + for (int index2 = 0; index2 < pattern.Length; ++index2) + { + if (pattern[index2].HasValue) + { + int num = (int)src[index1 + index2]; + byte? nullable1 = pattern[index2]; + int? nullable2 = nullable1.HasValue ? new int?((int)nullable1.GetValueOrDefault()) : new int?(); + int valueOrDefault = nullable2.GetValueOrDefault(); + if ((num == valueOrDefault ? (!nullable2.HasValue ? 1 : 0) : 1) != 0) + flag = false; + } + } + if (flag) + return index1; + } + return -1; + } + + public static ulong[] find_patterns(Process proc, IntPtr start_from, IntPtr end_at, List patterns) + { + IntPtr lpAddress = start_from; + ulong[] numArray1 = new ulong[patterns.Count]; + int count = patterns.Count; + do + { + memory.MEMORY_BASIC_INFORMATION64 lpBuffer; + if (memory.VirtualQueryEx(proc.Handle, lpAddress, out lpBuffer, (uint)Marshal.SizeOf(typeof(memory.MEMORY_BASIC_INFORMATION64))) > 0 && lpBuffer.RegionSize > 0UL) + { + byte[] numArray2 = new byte[(int)lpBuffer.RegionSize]; + memory.ReadProcessMemory(proc.Handle, (IntPtr)((long)lpBuffer.BaseAddress), numArray2); + for (int index = 0; index < patterns.Count; ++index) + { + if (numArray1[index] <= 0UL) + { + int first = memory.byte_find_first(numArray2, patterns[index]); + if (first > 0) + { + numArray1[index] = lpBuffer.BaseAddress + (ulong)(uint)first; + --count; + } + } + } + } + lpAddress = (IntPtr)((long)lpBuffer.BaseAddress + (long)lpBuffer.RegionSize); + } + while ((ulong)(long)lpAddress < (ulong)(long)end_at && count > 0); + return numArray1; + } + + public struct MEMORY_BASIC_INFORMATION64 + { + public ulong BaseAddress; + public ulong AllocationBase; + public int AllocationProtect; + public int __alignment1; + public ulong RegionSize; + public int State; + public int Protect; + public int Type; + public int __alignment2; + } +} diff --git a/mhw.cs b/mhw.cs index 3b284e4..804ce18 100644 --- a/mhw.cs +++ b/mhw.cs @@ -11,109 +11,114 @@ public static class mhw { - [DllImport("kernel32.dll")] - private static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, int dwSize, ref int lpNumberOfBytesRead); + public static long loc1 = -1; + public static long loc2 = -1; + public static long loc3 = -1; + public static long loc4 = -1; - private static bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer) - { - int lpNumberOfBytesRead = 0; - return mhw.ReadProcessMemory(hProcess, lpBaseAddress, lpBuffer, lpBuffer.Length, ref lpNumberOfBytesRead); - } + [DllImport("kernel32.dll")] + private static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, int dwSize, ref int lpNumberOfBytesRead); - private static ulong read_ulong(IntPtr hProcess, IntPtr lpBaseAddress) - { - byte[] lpBuffer = new byte[8]; - mhw.ReadProcessMemory(hProcess, lpBaseAddress, lpBuffer); - return BitConverter.ToUInt64(lpBuffer, 0); - } + private static bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer) + { + int lpNumberOfBytesRead = 0; + return mhw.ReadProcessMemory(hProcess, lpBaseAddress, lpBuffer, lpBuffer.Length, ref lpNumberOfBytesRead); + } - private static uint read_uint(IntPtr hProcess, IntPtr lpBaseAddress) - { - byte[] lpBuffer = new byte[4]; - mhw.ReadProcessMemory(hProcess, lpBaseAddress, lpBuffer); - return BitConverter.ToUInt32(lpBuffer, 0); - } + public static ulong read_ulong(IntPtr hProcess, IntPtr lpBaseAddress) + { + byte[] lpBuffer = new byte[8]; + mhw.ReadProcessMemory(hProcess, lpBaseAddress, lpBuffer); + return BitConverter.ToUInt64(lpBuffer, 0); + } - private static int dword_to_int(ref byte[] array) - { - return (int) array[0] + ((int) array[1] << 8) + ((int) array[2] << 16) + ((int) array[3] << 24); - } + public static uint read_uint(IntPtr hProcess, IntPtr lpBaseAddress) + { + byte[] lpBuffer = new byte[4]; + mhw.ReadProcessMemory(hProcess, lpBaseAddress, lpBuffer); + return BitConverter.ToUInt32(lpBuffer, 0); + } - private static ulong asm_func1(Process proc, ulong rcx, uint edx) - { - int num1 = (int) mhw.read_uint(proc.Handle, (IntPtr) 5428922716L); - ulong num2 = rcx; - int num3 = (int) edx; - rcx = (ulong) (uint) (num1 & num3) * 88UL; - return num2 + 72UL + rcx; - } + public static int dword_to_int(ref byte[] array) + { + return (int)array[0] + ((int)array[1] << 8) + ((int)array[2] << 16) + ((int)array[3] << 24); + } - public static int[] get_team_dmg(Process proc) - { - int[] numArray = new int[4]; - byte[] array = new byte[4]; - byte[] lpBuffer = new byte[8]; - mhw.ReadProcessMemory(proc.Handle, (IntPtr) 5430696680L, lpBuffer); - ulong num1 = BitConverter.ToUInt64(lpBuffer, 0) + 26288UL; - mhw.ReadProcessMemory(proc.Handle, (IntPtr) 5430707384L, lpBuffer); - ulong uint64_1 = BitConverter.ToUInt64(lpBuffer, 0); - for (int index = 0; index < 4; ++index) + private static ulong asm_func1(Process proc, ulong rcx, uint edx) { - mhw.ReadProcessMemory(proc.Handle, (IntPtr) ((long) num1 + 4L * (long) index), array); - uint edx = mhw.read_uint(proc.Handle, (IntPtr) ((long) num1 + 4L * (long) index)); - ulong num2 = mhw.asm_func1(proc, uint64_1, edx); - if (num2 > 0UL) - { - mhw.ReadProcessMemory(proc.Handle, (IntPtr) ((long) num2 + 72L), lpBuffer); - ulong uint64_2 = BitConverter.ToUInt64(lpBuffer, 0); - if (uint64_2 > 0UL) + int num1 = (int)mhw.read_uint(proc.Handle, (IntPtr)mhw.loc1); + ulong num2 = rcx; + int num3 = (int)edx; + rcx = (ulong)(uint)(num1 & num3) * 88UL; + return num2 + 72UL + rcx; + } + + public static int[] get_team_dmg(Process proc) + { + int[] numArray = new int[4]; + byte[] array = new byte[4]; + byte[] lpBuffer = new byte[8]; + mhw.ReadProcessMemory(proc.Handle, (IntPtr)mhw.loc2, lpBuffer); + ulong num1 = BitConverter.ToUInt64(lpBuffer, 0) + 26288UL; + mhw.ReadProcessMemory(proc.Handle, (IntPtr)mhw.loc3, lpBuffer); + ulong uint64_1 = BitConverter.ToUInt64(lpBuffer, 0); + for (int index = 0; index < 4; ++index) { - int num3 = mhw.ReadProcessMemory(proc.Handle, (IntPtr) ((long) uint64_2 + 72L), array) ? 1 : 0; - int num4 = mhw.dword_to_int(ref array); - if (num3 != 0 && num4 >= 0 && num4 <= 1048575) - numArray[index] = num4; + mhw.ReadProcessMemory(proc.Handle, (IntPtr)((long)num1 + 4L * (long)index), array); + uint edx = mhw.read_uint(proc.Handle, (IntPtr)((long)num1 + 4L * (long)index)); + ulong num2 = mhw.asm_func1(proc, uint64_1, edx); + if (num2 > 0UL) + { + mhw.ReadProcessMemory(proc.Handle, (IntPtr)((long)num2 + 72L), lpBuffer); + ulong uint64_2 = BitConverter.ToUInt64(lpBuffer, 0); + if (uint64_2 > 0UL) + { + int num3 = mhw.ReadProcessMemory(proc.Handle, (IntPtr)((long)uint64_2 + 72L), array) ? 1 : 0; + int num4 = mhw.dword_to_int(ref array); + if (num3 != 0 && num4 >= 0 && num4 <= 1048575) + numArray[index] = num4; + } + } } - } + return numArray; } - return numArray; - } - public static int get_player_seat_id(Process proc) - { - uint num1 = mhw.read_uint(proc.Handle, (IntPtr) 5444288144L); - uint num2 = mhw.read_uint(proc.Handle, (IntPtr) ((long) (num1 + 600U))); - int num3 = -1; - if (num2 > 4096U) + public static int get_player_seat_id(Process proc) { - uint num4 = mhw.read_uint(proc.Handle, (IntPtr) ((long) (num2 + 16U))); - if (num4 != 0U) - num3 = (int) mhw.read_uint(proc.Handle, (IntPtr) ((long) (num4 + 49132U))); + uint num1 = mhw.read_uint(proc.Handle, (IntPtr)mhw.loc4); + uint num2 = mhw.read_uint(proc.Handle, (IntPtr)((long)(num1 + 600U))); + int num3 = -1; + if (num2 > 4096U) + { + uint num4 = mhw.read_uint(proc.Handle, (IntPtr)((long)(num2 + 16U))); + if (num4 != 0U) + num3 = (int)mhw.read_uint(proc.Handle, (IntPtr)((long)(num4 + 49132U))); + } + return num3; } - return num3; - } - public static string[] get_team_player_names(Process proc) - { - string[] strArray = new string[4]; - byte[] array = new byte[40]; - int num = (int) mhw.read_uint(proc.Handle, (IntPtr) 5444288144L) + 346693; - for (int index1 = 0; index1 < 4; ++index1) + public static string[] get_team_player_names(Process proc) { - Array.Resize(ref array, 40); - mhw.ReadProcessMemory(proc.Handle, (IntPtr) (num + 33 * index1), array); - int index2 = Array.FindIndex(array, (Predicate) (x => x == (byte) 0)); - switch (index2) - { - case -1: - case 0: - strArray[index1] = ""; - break; - default: - Array.Resize(ref array, index2); - strArray[index1] = Encoding.UTF8.GetString(array); - break; - } + string[] strArray = new string[4]; + byte[] array = new byte[40]; + int num = (int)mhw.read_uint(proc.Handle, (IntPtr)mhw.loc4) + 346693; + for (int index1 = 0; index1 < 4; ++index1) + { + Array.Resize(ref array, 40); + mhw.ReadProcessMemory(proc.Handle, (IntPtr)(num + 33 * index1), array); + int index2 = Array.FindIndex(array, (Predicate)(x => x == (byte)0)); + switch (index2) + { + case -1: + case 0: + strArray[index1] = ""; + break; + default: + Array.Resize(ref array, index2); + strArray[index1] = Encoding.UTF8.GetString(array); + break; + } + } + return strArray; } - return strArray; - } -} +} \ No newline at end of file diff --git a/mhw_damage_meter_1_0.csproj b/mhw_damage_meter_1_0.csproj index c4585a1..9adbdf9 100644 --- a/mhw_damage_meter_1_0.csproj +++ b/mhw_damage_meter_1_0.csproj @@ -60,6 +60,7 @@ +