Files
RAGECOOP-V/Client/Scripts/Util/Memory.cs

132 lines
3.7 KiB
C#
Raw Normal View History

2022-10-23 19:02:39 +08:00
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using GTA;
2022-09-06 21:46:35 +08:00
using GTA.Math;
using RageCoop.Core;
2022-09-06 21:46:35 +08:00
using SHVDN;
namespace RageCoop.Client
{
2022-09-06 21:46:35 +08:00
internal unsafe class MemPatch
{
2022-10-23 19:02:39 +08:00
private readonly IntPtr _address;
2022-09-06 21:46:35 +08:00
private readonly byte[] _data;
private readonly byte[] _orginal;
2022-10-23 19:02:39 +08:00
public MemPatch(byte* address, byte[] data)
{
_data = data;
_orginal = new byte[data.Length];
_address = (IntPtr)address;
Marshal.Copy((IntPtr)address, _orginal, 0, data.Length);
}
2022-10-23 19:02:39 +08:00
public void Install()
{
Marshal.Copy(_data, 0, _address, _data.Length);
}
2022-10-23 19:02:39 +08:00
public void Uninstall()
{
Marshal.Copy(_orginal, 0, _address, _orginal.Length);
}
}
2022-09-06 21:46:35 +08:00
internal static unsafe class Memory
{
public static MemPatch VignettingPatch;
public static MemPatch VignettingCallPatch;
public static MemPatch TimeScalePatch;
2022-10-23 19:02:39 +08:00
static Memory()
{
// Weapon/radio wheel slow-mo patch
// Thanks @CamxxCore, https://github.com/CamxxCore/GTAVWeaponWheelMod
var result = NativeMemory.FindPattern("\x38\x51\x64\x74\x19", "xxxxx");
2022-10-23 19:02:39 +08:00
if (result == null)
throw new NotSupportedException("Can't find memory pattern to patch weapon/radio slow-mo");
2022-09-06 21:46:35 +08:00
var address = result + 26;
address = address + *(int*)address + 4u;
2022-09-06 21:46:35 +08:00
VignettingPatch = new MemPatch(address, new byte[] { RET, 0x90, 0x90, 0x90, 0x90 });
VignettingCallPatch = new MemPatch(result + 8, new byte[] { 0x90, 0x90, 0x90, 0x90, 0x90 });
TimeScalePatch = new MemPatch(result + 34, new byte[] { XOR_32_64, 0xD2 });
}
2022-10-23 19:02:39 +08:00
public static void ApplyPatches()
{
VignettingPatch.Install();
VignettingCallPatch.Install();
TimeScalePatch.Install();
}
2022-10-23 19:02:39 +08:00
public static void RestorePatches()
{
VignettingPatch.Uninstall();
VignettingCallPatch.Uninstall();
TimeScalePatch.Uninstall();
}
2022-10-23 19:02:39 +08:00
public static Vector3 ReadPosition(this Entity e)
{
return ReadVector3(e.MemoryAddress + PositionOffset);
}
public static Quaternion ReadQuaternion(this Entity e)
{
return Quaternion.RotationMatrix(e.Matrix);
}
public static Vector3 ReadRotation(this Entity e)
{
return e.ReadQuaternion().ToEulerDegrees();
}
public static Vector3 ReadVelocity(this Ped e)
{
return ReadVector3(e.MemoryAddress + VelocityOffset);
}
public static Vector3 ReadVector3(IntPtr address)
{
2022-10-23 19:02:39 +08:00
var ptr = (float*)address.ToPointer();
return new Vector3
{
2022-09-06 21:46:35 +08:00
X = *ptr,
Y = ptr[1],
Z = ptr[2]
};
}
2022-10-23 19:02:39 +08:00
2022-09-06 21:46:35 +08:00
public static List<int> FindOffset(float toSearch, IntPtr start, int range = 1000, float tolerance = 0.01f)
2022-07-30 15:45:27 +08:00
{
var foundOffsets = new List<int>(100);
2022-10-23 19:02:39 +08:00
for (var i = 0; i <= range; i++)
2022-07-30 15:45:27 +08:00
{
2022-09-06 21:46:35 +08:00
var val = NativeMemory.ReadFloat(start + i);
2022-10-23 19:02:39 +08:00
if (Math.Abs(val - toSearch) < tolerance) foundOffsets.Add(i);
2022-07-30 15:45:27 +08:00
}
2022-10-23 19:02:39 +08:00
2022-07-30 15:45:27 +08:00
return foundOffsets;
}
2022-10-23 19:02:39 +08:00
#region PATCHES
#endregion
#region OFFSET-CONST
public const int PositionOffset = 144;
public const int VelocityOffset = 800;
public const int MatrixOffset = 96;
#endregion
#region OPCODE
private const byte XOR_32_64 = 0x31;
private const byte RET = 0xC3;
#endregion
}
2022-10-23 19:02:39 +08:00
}