From 19949270fc9d771ae4b943d051318b63c7d5cf58 Mon Sep 17 00:00:00 2001 From: kagikn Date: Thu, 29 Mar 2018 12:53:46 +0900 Subject: [PATCH] Get direct pickup address instead of its handle --- GlowingPickups/GlowingPickups.cs | 46 +++++++++++++++++++-- GlowingPickups/MemoryAccess.cs | 68 +++++++++++++++++++++++--------- 2 files changed, 92 insertions(+), 22 deletions(-) diff --git a/GlowingPickups/GlowingPickups.cs b/GlowingPickups/GlowingPickups.cs index 5bc3125..8d15a1c 100644 --- a/GlowingPickups/GlowingPickups.cs +++ b/GlowingPickups/GlowingPickups.cs @@ -38,7 +38,48 @@ private void OnTick(object o, EventArgs e) var offset = (int)Game.Version >= (int)GameVersion.VER_1_0_944_2_STEAM ? 0x480 : 0x470; - var pickupProps = PickupObjectPoolTask.GetPickupObjects(); + + var pickupAddresses = PickupObjectPoolTask.GetPickupObjectAddresses(); + foreach (var pickupAddr in pickupAddresses) + { + unsafe + { + var isVisible = (Marshal.ReadByte(pickupAddr, 0x2C) & 0x01) == 1; + + if (!isVisible) + { + continue; + } + + var pos = *(Vector3*)(pickupAddr + 0x90); + var dataAddress = Marshal.ReadIntPtr(pickupAddr, offset); + + if (dataAddress != IntPtr.Zero) + { + var red = (int)(BitConverter.ToSingle( + BitConverter.GetBytes(Marshal.ReadInt32(dataAddress, 0x5C)), 0) * 255); + var green = (int)(BitConverter.ToSingle( + BitConverter.GetBytes(Marshal.ReadInt32(dataAddress, 0x60)), 0) * 255); + var blue = (int)(BitConverter.ToSingle( + BitConverter.GetBytes(Marshal.ReadInt32(dataAddress, 0x64)), 0) * 255); + var range = BitConverter.ToSingle( + BitConverter.GetBytes(Marshal.ReadInt32(dataAddress, 0x10)), 0) * settings.RangeMultiplier; + var intensity = BitConverter.ToSingle( + BitConverter.GetBytes(Marshal.ReadInt32(dataAddress, 0x68)), 0) * settings.LightIntensityMultiplier; + var darkIntensity = BitConverter.ToSingle( + BitConverter.GetBytes(Marshal.ReadInt32(dataAddress, 0x6C)), 0) * settings.ShadowMultiplier; + Function.Call(Hash._DRAW_LIGHT_WITH_RANGE_WITH_SHADOW, pos.X, pos.Y, pos.Z, red, + green, blue, range, intensity, darkIntensity); + } + else + { + Function.Call(Hash._DRAW_LIGHT_WITH_RANGE_WITH_SHADOW, pos.X, pos.Y, pos.Z, 255, 57, 0, 5.0f, 30.0f, 10.0f); + } + } + } + + //This is unstable + /*var pickupProps = PickupObjectPoolTask.GetPickupObjects(); foreach (var pickup in pickupProps) { unsafe @@ -46,7 +87,6 @@ private void OnTick(object o, EventArgs e) var dataAddress = Marshal.ReadIntPtr(new IntPtr(pickup.MemoryAddress), offset); if (dataAddress != IntPtr.Zero) { - //Color glowingColor; var pos = pickup.Position; var red = (int)(BitConverter.ToSingle( BitConverter.GetBytes(Marshal.ReadInt32(dataAddress, 0x5C)), 0) * 255); @@ -69,7 +109,7 @@ private void OnTick(object o, EventArgs e) Function.Call(Hash._DRAW_LIGHT_WITH_RANGE_WITH_SHADOW, pos.X, pos.Y, pos.Z, 255, 57, 0, 5.0f, 30.0f, 10.0f); } } - } + }*/ } } } diff --git a/GlowingPickups/MemoryAccess.cs b/GlowingPickups/MemoryAccess.cs index 0e2d549..ab668ff 100644 --- a/GlowingPickups/MemoryAccess.cs +++ b/GlowingPickups/MemoryAccess.cs @@ -36,19 +36,21 @@ static internal class MemoryAccess } [StructLayout(LayoutKind.Explicit)] - public struct EntityPool + internal struct EntityPool { - [FieldOffset(0x10)] UInt32 num1; - [FieldOffset(0x20)] UInt32 num2; + [FieldOffset(0x10)] + internal uint num1; + [FieldOffset(0x20)] + internal uint num2; - public bool IsFull() + internal bool IsFull() { return num1 - (num2 & 0x3FFFFFFF) <= 256; } } [StructLayout(LayoutKind.Explicit)] - public unsafe struct GenericPool + internal unsafe struct GenericPool { [FieldOffset(0x00)] public ulong poolStartAddress; @@ -83,8 +85,8 @@ private ulong Mask(uint index) unsafe public static class PickupObjectPoolTask { //static public IntPtr _AddEntityToPoolFuncAddress; - static public IntPtr _EntityPoolAddress; - static public IntPtr _PickupObjectPoolAddress; + static public ulong* _EntityPoolAddress; + static public ulong* _PickupObjectPoolAddress; internal delegate int AddEntityToPoolFunc(ulong address); //returns an entity handle static internal AddEntityToPoolFunc _addEntToPoolFunc; @@ -97,19 +99,16 @@ static public void Init() FindAddEntityToPoolFuncAddress(); } - static public List GetPickupObjects() + //This is unstable and can easily crash the game + /*static public List GetPickupObjects() { - //FindEntityPoolAddress(); - //FindPickupPoolAddress(); - //FindAddEntityToPoolFuncAddress(); - - if (**(ulong**)_EntityPoolAddress.ToPointer() == 0 || *(ulong*)_PickupObjectPoolAddress.ToPointer() == 0) + if (*_EntityPoolAddress == 0 || *_PickupObjectPoolAddress == 0) { return new List(); } - GenericPool* pickupPool = (GenericPool*)(*(ulong*)_PickupObjectPoolAddress.ToPointer()); - EntityPool* entitiesPool = (EntityPool*)(*(ulong*)_EntityPoolAddress.ToPointer()); + GenericPool* pickupPool = (GenericPool*)(*_PickupObjectPoolAddress); + EntityPool* entitiesPool = (EntityPool*)(*_EntityPoolAddress); List pickupHandles = new List(); @@ -127,24 +126,55 @@ static public List GetPickupObjects() if (address != 0) { - int handle; - handle = _addEntToPoolFunc(address); + int handle = _addEntToPoolFunc(address); pickupHandles.Add(new Prop(handle)); } } } return pickupHandles; + }*/ + + static public List GetPickupObjectAddresses() + { + if (*_EntityPoolAddress == 0 || *_PickupObjectPoolAddress == 0) + { + return new List(); + } + + GenericPool* pickupPool = (GenericPool*)(*_PickupObjectPoolAddress); + EntityPool* entitiesPool = (EntityPool*)(*_EntityPoolAddress); + + List pickupsAddresses = new List(); + + for (uint i = 0; i < pickupPool->size; i++) + { + if (entitiesPool->IsFull()) + { + break; + } + + if (pickupPool->IsValid(i)) + { + ulong address = pickupPool->GetAddress(i); + + if (address != 0) + { + pickupsAddresses.Add(new IntPtr((long)address)); + } + } + } + return pickupsAddresses; } static public void FindEntityPoolAddress() { var address = MemoryAccess.FindPattern("\x4C\x8B\x0D\x00\x00\x00\x00\x44\x8B\xC1\x49\x8B\x41\x08", "xxx????xxxxxxx"); - _EntityPoolAddress = new IntPtr(*(int*)(address + 3) + address + 7); + _EntityPoolAddress = (ulong*)(*(int*)(address + 3) + address + 7); } static public void FindPickupPoolAddress() { var address = MemoryAccess.FindPattern("\x4C\x8B\x05\x00\x00\x00\x00\x40\x8A\xF2\x8B\xE9", "xxx????xxxxx"); - _PickupObjectPoolAddress = new IntPtr((*(int*)(address + 3) + address + 7)); + _PickupObjectPoolAddress = (ulong*)(*(int*)(address + 3) + address + 7); } static public void FindAddEntityToPoolFuncAddress() {