Skip to content

Commit 1e681ff

Browse files
committed
Got rid of the stupid memory dll and not doing the stuff while the game is paused
1 parent 4d56988 commit 1e681ff

File tree

3 files changed

+329
-80
lines changed

3 files changed

+329
-80
lines changed

SADXOpenStates/ProcessMemory.cs

Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
// Credits to MainMemory for this
2+
3+
using System;
4+
using System.Diagnostics;
5+
using System.Runtime.InteropServices;
6+
7+
namespace SADXOpenStates
8+
{
9+
public static class ProcessMemory
10+
{
11+
[DllImport("kernel32.dll", SetLastError = true)]
12+
private static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [Out] byte[] lpBuffer, int dwSize, out int lpNumberOfBytesRead);
13+
14+
[DllImport("kernel32.dll", SetLastError = true)]
15+
static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, uint nSize, out UIntPtr lpNumberOfBytesWritten);
16+
17+
public static IntPtr GetFinalAddress(this Process process, int baseAddress, params int[] offsets)
18+
{
19+
int tempValue = ReadInt32(process, baseAddress);
20+
for (int i = 0; i < offsets.Length -1; i++)
21+
{
22+
int currAddress = tempValue + offsets[i];
23+
tempValue = ReadInt32(process, currAddress);
24+
}
25+
26+
return new IntPtr(tempValue + offsets[offsets.Length - 1]);
27+
}
28+
29+
public static byte[] ReadBytes(this Process process, IntPtr address, int size)
30+
{
31+
byte[] buf = new byte[size];
32+
int bytesread;
33+
ReadProcessMemory(process.Handle, address, buf, size, out bytesread);
34+
if (bytesread != size) throw new InvalidOperationException();
35+
return buf;
36+
}
37+
38+
public static byte[] ReadBytes(this Process process, int address, int size) { return ReadBytes(process, new IntPtr(address), size); }
39+
40+
public static byte[] ReadBytes(this Process process, long address, int size) { return ReadBytes(process, new IntPtr(address), size); }
41+
42+
public static byte ReadByte(this Process process, IntPtr address)
43+
{
44+
return ReadBytes(process, address, sizeof(byte))[0];
45+
}
46+
47+
public static byte ReadByte(this Process process, int address) { return ReadByte(process, new IntPtr(address)); }
48+
49+
public static byte ReadByte(this Process process, long address) { return ReadByte(process, new IntPtr(address)); }
50+
51+
public static sbyte ReadSByte(this Process process, IntPtr address)
52+
{
53+
return unchecked((sbyte)ReadBytes(process, address, sizeof(sbyte))[0]);
54+
}
55+
56+
public static sbyte ReadSByte(this Process process, int address) { return ReadSByte(process, new IntPtr(address)); }
57+
58+
public static sbyte ReadSByte(this Process process, long address) { return ReadSByte(process, new IntPtr(address)); }
59+
60+
public static short ReadInt16(this Process process, IntPtr address)
61+
{
62+
return BitConverter.ToInt16(ReadBytes(process, address, sizeof(short)), 0);
63+
}
64+
65+
public static short ReadInt16(this Process process, int address) { return ReadInt16(process, new IntPtr(address)); }
66+
67+
public static short ReadInt16(this Process process, long address) { return ReadInt16(process, new IntPtr(address)); }
68+
69+
public static ushort ReadUInt16(this Process process, IntPtr address)
70+
{
71+
return BitConverter.ToUInt16(ReadBytes(process, address, sizeof(ushort)), 0);
72+
}
73+
74+
public static ushort ReadUInt16(this Process process, int address) { return ReadUInt16(process, new IntPtr(address)); }
75+
76+
public static ushort ReadUInt16(this Process process, long address) { return ReadUInt16(process, new IntPtr(address)); }
77+
78+
public static int ReadInt32(this Process process, IntPtr address)
79+
{
80+
return BitConverter.ToInt32(ReadBytes(process, address, sizeof(int)), 0);
81+
}
82+
83+
public static int ReadInt32(this Process process, int address) { return ReadInt32(process, new IntPtr(address)); }
84+
85+
public static int ReadInt32(this Process process, long address) { return ReadInt32(process, new IntPtr(address)); }
86+
87+
public static uint ReadUInt32(this Process process, IntPtr address)
88+
{
89+
return BitConverter.ToUInt32(ReadBytes(process, address, sizeof(uint)), 0);
90+
}
91+
92+
public static uint ReadUInt32(this Process process, int address) { return ReadUInt32(process, new IntPtr(address)); }
93+
94+
public static uint ReadUInt32(this Process process, long address) { return ReadUInt32(process, new IntPtr(address)); }
95+
96+
public static long ReadInt64(this Process process, IntPtr address)
97+
{
98+
return BitConverter.ToInt64(ReadBytes(process, address, sizeof(long)), 0);
99+
}
100+
101+
public static long ReadInt64(this Process process, int address) { return ReadInt64(process, new IntPtr(address)); }
102+
103+
public static long ReadInt64(this Process process, long address) { return ReadInt64(process, new IntPtr(address)); }
104+
105+
public static ulong ReadUInt64(this Process process, IntPtr address)
106+
{
107+
return BitConverter.ToUInt64(ReadBytes(process, address, sizeof(ulong)), 0);
108+
}
109+
110+
public static ulong ReadUInt64(this Process process, int address) { return ReadUInt64(process, new IntPtr(address)); }
111+
112+
public static ulong ReadUInt64(this Process process, long address) { return ReadUInt64(process, new IntPtr(address)); }
113+
114+
public static float ReadSingle(this Process process, IntPtr address)
115+
{
116+
return BitConverter.ToSingle(ReadBytes(process, address, sizeof(float)), 0);
117+
}
118+
119+
public static float ReadSingle(this Process process, int address) { return ReadSingle(process, new IntPtr(address)); }
120+
121+
public static float ReadSingle(this Process process, long address) { return ReadSingle(process, new IntPtr(address)); }
122+
123+
public static double ReadDouble(this Process process, IntPtr address)
124+
{
125+
return BitConverter.ToDouble(ReadBytes(process, address, sizeof(double)), 0);
126+
}
127+
128+
public static double ReadDouble(this Process process, int address) { return ReadDouble(process, new IntPtr(address)); }
129+
130+
public static double ReadDouble(this Process process, long address) { return ReadDouble(process, new IntPtr(address)); }
131+
132+
public static bool ReadBoolean(this Process process, IntPtr address)
133+
{
134+
return BitConverter.ToBoolean(ReadBytes(process, address, sizeof(bool)), 0);
135+
}
136+
137+
public static bool ReadBoolean(this Process process, int address) { return ReadBoolean(process, new IntPtr(address)); }
138+
139+
public static bool ReadBoolean(this Process process, long address) { return ReadBoolean(process, new IntPtr(address)); }
140+
141+
public static void Write(this Process process, IntPtr address, byte[] value)
142+
{
143+
UIntPtr byteswritten;
144+
WriteProcessMemory(process.Handle, address, value, (uint)value.Length, out byteswritten);
145+
if (byteswritten.ToUInt32() != value.Length) throw new InvalidOperationException();
146+
}
147+
148+
public static void Write(this Process process, int address, byte[] value) { Write(process, new IntPtr(address), value); }
149+
150+
public static void Write(this Process process, long address, byte[] value) { Write(process, new IntPtr(address), value); }
151+
152+
public static void Write(this Process process, IntPtr address, byte value)
153+
{
154+
Write(process, address, new byte[] { value });
155+
}
156+
157+
public static void Write(this Process process, int address, byte value) { Write(process, new IntPtr(address), value); }
158+
159+
public static void Write(this Process process, long address, byte value) { Write(process, new IntPtr(address), value); }
160+
161+
public static void Write(this Process process, IntPtr address, sbyte value)
162+
{
163+
Write(process, address, new byte[] { unchecked((byte)value) });
164+
}
165+
166+
public static void Write(this Process process, int address, sbyte value) { Write(process, new IntPtr(address), value); }
167+
168+
public static void Write(this Process process, long address, sbyte value) { Write(process, new IntPtr(address), value); }
169+
170+
public static void Write(this Process process, IntPtr address, short value)
171+
{
172+
Write(process, address, BitConverter.GetBytes(value));
173+
}
174+
175+
public static void Write(this Process process, int address, short value) { Write(process, new IntPtr(address), value); }
176+
177+
public static void Write(this Process process, long address, short value) { Write(process, new IntPtr(address), value); }
178+
179+
public static void Write(this Process process, IntPtr address, ushort value)
180+
{
181+
Write(process, address, BitConverter.GetBytes(value));
182+
}
183+
184+
public static void Write(this Process process, int address, ushort value) { Write(process, new IntPtr(address), value); }
185+
186+
public static void Write(this Process process, long address, ushort value) { Write(process, new IntPtr(address), value); }
187+
188+
public static void Write(this Process process, IntPtr address, int value)
189+
{
190+
Write(process, address, BitConverter.GetBytes(value));
191+
}
192+
193+
public static void Write(this Process process, int address, int value) { Write(process, new IntPtr(address), value); }
194+
195+
public static void Write(this Process process, long address, int value) { Write(process, new IntPtr(address), value); }
196+
197+
public static void Write(this Process process, IntPtr address, uint value)
198+
{
199+
Write(process, address, BitConverter.GetBytes(value));
200+
}
201+
202+
public static void Write(this Process process, int address, uint value) { Write(process, new IntPtr(address), value); }
203+
204+
public static void Write(this Process process, long address, uint value) { Write(process, new IntPtr(address), value); }
205+
206+
public static void Write(this Process process, IntPtr address, long value)
207+
{
208+
Write(process, address, BitConverter.GetBytes(value));
209+
}
210+
211+
public static void Write(this Process process, int address, long value) { Write(process, new IntPtr(address), value); }
212+
213+
public static void Write(this Process process, long address, long value) { Write(process, new IntPtr(address), value); }
214+
215+
public static void Write(this Process process, IntPtr address, ulong value)
216+
{
217+
Write(process, address, BitConverter.GetBytes(value));
218+
}
219+
220+
public static void Write(this Process process, int address, ulong value) { Write(process, new IntPtr(address), value); }
221+
222+
public static void Write(this Process process, long address, ulong value) { Write(process, new IntPtr(address), value); }
223+
224+
public static void Write(this Process process, IntPtr address, float value)
225+
{
226+
Write(process, address, BitConverter.GetBytes(value));
227+
}
228+
229+
public static void Write(this Process process, int address, float value) { Write(process, new IntPtr(address), value); }
230+
231+
public static void Write(this Process process, long address, float value) { Write(process, new IntPtr(address), value); }
232+
233+
public static void Write(this Process process, IntPtr address, double value)
234+
{
235+
Write(process, address, BitConverter.GetBytes(value));
236+
}
237+
238+
public static void Write(this Process process, int address, double value) { Write(process, new IntPtr(address), value); }
239+
240+
public static void Write(this Process process, long address, double value) { Write(process, new IntPtr(address), value); }
241+
242+
public static void Write(this Process process, IntPtr address, bool value)
243+
{
244+
Write(process, address, BitConverter.GetBytes(value));
245+
}
246+
247+
public static void Write(this Process process, int address, bool value) { Write(process, new IntPtr(address), value); }
248+
249+
public static void Write(this Process process, long address, bool value) { Write(process, new IntPtr(address), value); }
250+
}
251+
}

0 commit comments

Comments
 (0)