Advertisement
bobmarley12345

GTA5 Deluxo CWeaponInfo 'VEHICLE_WEAPON_DELUXO_MISSILE'

Oct 18th, 2023
1,608
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.00 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using VWeaponUtil.Utils;
  7.  
  8. namespace VWeaponUtil.ConsoleTesting {
  9.     internal class Program {
  10.         public class MEM64 {
  11.             [DllImport("kernel32.dll")]
  12.             public static extern int WriteProcessMemory(IntPtr Handle, long Address, byte[] buffer, int Size, int BytesWritten = 0);
  13.  
  14.             [DllImport("kernel32.dll")]
  15.             public static extern int ReadProcessMemory(IntPtr Handle, long Address, byte[] buffer, int Size, int BytesRead = 0);
  16.  
  17.             [DllImport("kernel32.dll", EntryPoint = "ReadProcessMemory")]
  18.             public static extern unsafe int ReadProcessMemory_Unsafe(IntPtr hProcess, long address, byte* buffer, int bufferSize, int bytesRead = 0);
  19.  
  20.             public static unsafe long ToInt64(byte[] value, int startIndex) {
  21.                 fixed (byte* numPtr = &value[startIndex]) {
  22.                     return *(long*) numPtr;
  23.                 }
  24.             }
  25.  
  26.             public static unsafe long GetPointerAddress(IntPtr hProcess, long pointer, int[] offsets = null) {
  27.                 // pointer should contain the BaseAddress of the module being targeted
  28.                 if (offsets == null || offsets.Length < 1) {
  29.                     return pointer;
  30.                 }
  31.  
  32.                 byte* buf = stackalloc byte[8];
  33.                 ReadProcessMemory_Unsafe(hProcess, pointer, buf, 8);
  34.                 for (int i = 0, j = offsets.Length - 1; i < j; i++) {
  35.                     pointer = *(long*) buf + offsets[i];
  36.                     ReadProcessMemory_Unsafe(hProcess, pointer, buf, 8);
  37.                 }
  38.  
  39.                 pointer = *(long*) buf + offsets[offsets.Length - 1];
  40.                 return pointer;
  41.             }
  42.  
  43.             public static void WriteBytes(IntPtr hProcess, long Address, byte[] Bytes) {
  44.                 WriteProcessMemory(hProcess, Address, Bytes, Bytes.Length);
  45.             }
  46.  
  47.             public static void WriteFloat(IntPtr hProcess, long Address, float Value) {
  48.                 WriteProcessMemory(hProcess, Address, BitConverter.GetBytes(Value), 4);
  49.             }
  50.  
  51.             public static void WriteDouble(IntPtr hProcess, long Address, double Value) {
  52.                 WriteProcessMemory(hProcess, Address, BitConverter.GetBytes(Value), 8);
  53.             }
  54.  
  55.             public static void WriteInteger(IntPtr hProcess, long Address, int Value, int size) {
  56.                 WriteProcessMemory(hProcess, Address, BitConverter.GetBytes(Value), size);
  57.             }
  58.  
  59.             public static void WriteString(IntPtr hProcess, long Address, string String) {
  60.                 byte[] Buffer = new ASCIIEncoding().GetBytes(String);
  61.                 WriteProcessMemory(hProcess, Address, Buffer, Buffer.Length);
  62.             }
  63.  
  64.             public static byte[] ReadBytes(IntPtr hProcess, long Address, int Length) {
  65.                 byte[] Buffer = new byte[Length];
  66.                 ReadProcessMemory(hProcess, Address, Buffer, Length);
  67.                 return Buffer;
  68.             }
  69.  
  70.             public static float ReadFloat(IntPtr hProcess, long Address) {
  71.                 byte[] Buffer = new byte[4];
  72.                 ReadProcessMemory(hProcess, Address, Buffer, 4);
  73.                 return BitConverter.ToSingle(Buffer, 0);
  74.             }
  75.  
  76.             public static double ReadDouble(IntPtr hProcess, long Address) {
  77.                 byte[] Buffer = new byte[8];
  78.                 ReadProcessMemory(hProcess, Address, Buffer, 8);
  79.                 return BitConverter.ToDouble(Buffer, 0);
  80.             }
  81.  
  82.             public static int ReadInteger(IntPtr hProcess, long Address, int Length) {
  83.                 byte[] Buffer = new byte[Length];
  84.                 ReadProcessMemory(hProcess, Address, Buffer, Length);
  85.                 return BitConverter.ToInt32(Buffer, 0);
  86.             }
  87.  
  88.             public static string ReadString(IntPtr hProcess, long Address, int size) {
  89.                 byte[] Buffer = new byte[size];
  90.                 ReadProcessMemory(hProcess, Address, Buffer, size);
  91.                 return new ASCIIEncoding().GetString(Buffer);
  92.             }
  93.  
  94.             public static long ReadPointer(IntPtr hProcess, long Address) {
  95.                 byte[] Buffer = new byte[8];
  96.                 ReadProcessMemory(hProcess, Address, Buffer, Buffer.Length);
  97.                 return BitConverter.ToInt64(Buffer, 0);
  98.             }
  99.         }
  100.  
  101.         public readonly struct PointerOffset {
  102.             public readonly long baseAddress;
  103.             public readonly int[] offsets;
  104.             public readonly int trailOffset;
  105.  
  106.             public PointerOffset(long baseAddress, int[] offsets, int trailingOffset = 0) {
  107.                 this.baseAddress = baseAddress;
  108.                 this.offsets = offsets;
  109.                 this.trailOffset = trailingOffset;
  110.             }
  111.  
  112.             public PointerOffset(PointerOffset offset, int trailingOffset = 0) : this(offset.baseAddress, offset.offsets, trailingOffset) {
  113.             }
  114.  
  115.             /// <summary>
  116.             /// Gets the address of the underlying pointer. A value may be readable from the returned address
  117.             /// </summary>
  118.             /// <param name="hProcess">The target process</param>
  119.             /// <param name="hProcessBaseAddress">The process' base address</param>
  120.             /// <returns>An address in which this offset points to, relative to the given process and base address</returns>
  121.             public long GetPointerAddress(IntPtr hProcess, long hProcessBaseAddress) {
  122.                 return MEM64.GetPointerAddress(hProcess, hProcessBaseAddress + this.baseAddress, this.offsets) - this.trailOffset;
  123.             }
  124.         }
  125.  
  126.         public static readonly PointerOffset AWT_POINTER_1               = new PointerOffset(0x2949870, new int[] {0xA40, 0x150});
  127.         public static readonly PointerOffset DELUXO_CWEAPON_INFO_MISSILE = new PointerOffset(AWT_POINTER_1, 0x150);
  128.         public static readonly PointerOffset AWT_POINTER_2               = new PointerOffset(0x2949850, new int[] {0x2D0, 0x978, 0x18, 0x150});
  129.  
  130.         static Program() {
  131.         }
  132.  
  133.         static void Main(string[] args) {
  134.             Console.WriteLine("Opening GTA5 process...");
  135.             Process process = null;
  136.             try {
  137.                 process = Process.GetProcessesByName("GTA5").FirstOrDefault();
  138.                 if (process == null) {
  139.                     Console.WriteLine("Could not find GTA5 proces");
  140.                     return;
  141.                 }
  142.  
  143.                 IntPtr hProcess = process.Handle;
  144.                 ProcessModule module = process.Modules[0];
  145.                 long hProcessBaseAddress = module.BaseAddress.ToInt64();
  146.                 {
  147.                     long ptr = AWT_POINTER_1.GetPointerAddress(hProcess, hProcessBaseAddress);
  148.                     float value = MEM64.ReadFloat(hProcess, ptr);
  149.                     Console.WriteLine("Deluxo Alternate Wait Time value: " + value);
  150.                 }
  151.  
  152.                 {
  153.                     long cWeaponInfoAddress = DELUXO_CWEAPON_INFO_MISSILE.GetPointerAddress(hProcess, hProcessBaseAddress);
  154.  
  155.                     unsafe {
  156.                         byte[] x = MEM64.ReadBytes(hProcess, cWeaponInfoAddress, sizeof(CWeaponInfo));
  157.                         fixed (byte* ptr = x) {
  158.                             CWeaponInfo weaponInfo = *(CWeaponInfo*) ptr;
  159.                             Console.WriteLine(weaponInfo.ToString());
  160.                         }
  161.                     }
  162.                 }
  163.  
  164.                 module.Dispose();
  165.             }
  166.             catch (Exception e) {
  167.                 Console.WriteLine("Exception doing shit: " + e.GetToString());
  168.             }
  169.             finally {
  170.                 try {
  171.                     Console.WriteLine("Disposing process...");
  172.                     process?.Dispose();
  173.                 }
  174.                 catch (Exception e) {
  175.                     Console.WriteLine("Failed to close process: " + e.GetToString());
  176.                 }
  177.             }
  178.         }
  179.     }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement