/
Write.cs
284 lines (261 loc) · 11.1 KB
/
Write.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static Memory.Imps;
namespace Memory
{
public partial class Mem
{
Dictionary<string, CancellationTokenSource> FreezeTokenSrcs = new Dictionary<string, CancellationTokenSource>();
/// <summary>
/// Freeze a value to an address.
/// </summary>
/// <param name="address">Your address</param>
/// <param name="type">byte, 2bytes, bytes, float, int, string, double or long.</param>
/// <param name="value">Value to freeze</param>
/// <param name="file">ini file to read address from (OPTIONAL)</param>
public void FreezeValue(string address, string type, string value, string file = "")
{
CancellationTokenSource cts = new CancellationTokenSource();
lock (FreezeTokenSrcs)
{
if (FreezeTokenSrcs.ContainsKey(address))
{
Debug.WriteLine("Changing Freezing Address " + address + " Value " + value);
try
{
FreezeTokenSrcs[address].Cancel();
FreezeTokenSrcs.Remove(address);
}
catch
{
Debug.WriteLine("ERROR: Avoided a crash. Address " + address + " was not frozen.");
}
}
else
Debug.WriteLine("Adding Freezing Address " + address + " Value " + value);
FreezeTokenSrcs.Add(address, cts);
}
Task.Factory.StartNew(() =>
{
while (!cts.Token.IsCancellationRequested)
{
WriteMemory(address, type, value, file);
Thread.Sleep(25);
}
},
cts.Token);
}
/// <summary>
/// Unfreeze a frozen value at an address
/// </summary>
/// <param name="address">address where frozen value is stored</param>
public void UnfreezeValue(string address)
{
Debug.WriteLine("Un-Freezing Address " + address);
try
{
lock (FreezeTokenSrcs)
{
FreezeTokenSrcs[address].Cancel();
FreezeTokenSrcs.Remove(address);
}
}
catch
{
Debug.WriteLine("ERROR: Address " + address + " was not frozen.");
}
}
///<summary>
///Write to memory address. See https://github.com/erfg12/memory.dll/wiki/writeMemory() for more information.
///</summary>
///<param name="code">address, module + pointer + offset, module + offset OR label in .ini file.</param>
///<param name="type">byte, 2bytes, bytes, float, int, string, double or long.</param>
///<param name="write">value to write to address.</param>
///<param name="file">path and name of .ini file (OPTIONAL)</param>
///<param name="stringEncoding">System.Text.Encoding.UTF8 (DEFAULT). Other options: ascii, unicode, utf32, utf7</param>
///<param name="RemoveWriteProtection">If building a trainer on an emulator (Ex: RPCS3) you'll want to set this to false</param>
public bool WriteMemory(string code, string type, string write, string file = "", System.Text.Encoding stringEncoding = null, bool RemoveWriteProtection = true)
{
byte[] memory = new byte[4];
int size = 4;
UIntPtr theCode;
theCode = GetCode(code, file);
if (type.ToLower() == "float")
{
write = Convert.ToString(float.Parse(write, CultureInfo.InvariantCulture));
memory = BitConverter.GetBytes(Convert.ToSingle(write));
size = 4;
}
else if (type.ToLower() == "int")
{
memory = BitConverter.GetBytes(Convert.ToInt32(write));
size = 4;
}
else if (type.ToLower() == "byte")
{
memory = new byte[1];
memory[0] = Convert.ToByte(write, 16);
size = 1;
}
else if (type.ToLower() == "2bytes")
{
memory = new byte[2];
memory[0] = (byte)(Convert.ToInt32(write) % 256);
memory[1] = (byte)(Convert.ToInt32(write) / 256);
size = 2;
}
else if (type.ToLower() == "bytes")
{
if (write.Contains(",") || write.Contains(" ")) //check if it's a proper array
{
string[] stringBytes;
if (write.Contains(","))
stringBytes = write.Split(',');
else
stringBytes = write.Split(' ');
//Debug.WriteLine("write:" + write + " stringBytes:" + stringBytes);
int c = stringBytes.Count();
memory = new byte[c];
for (int i = 0; i < c; i++)
{
memory[i] = Convert.ToByte(stringBytes[i], 16);
}
size = stringBytes.Count();
}
else //wasnt array, only 1 byte
{
memory = new byte[1];
memory[0] = Convert.ToByte(write, 16);
size = 1;
}
}
else if (type.ToLower() == "double")
{
memory = BitConverter.GetBytes(Convert.ToDouble(write));
size = 8;
}
else if (type.ToLower() == "long")
{
memory = BitConverter.GetBytes(Convert.ToInt64(write));
size = 8;
}
else if (type.ToLower() == "string")
{
if (stringEncoding == null)
memory = System.Text.Encoding.UTF8.GetBytes(write);
else
memory = stringEncoding.GetBytes(write);
size = memory.Length;
}
//Debug.Write("DEBUG: Writing bytes [TYPE:" + type + " ADDR:" + theCode + "] " + String.Join(",", memory) + Environment.NewLine);
MemoryProtection OldMemProt = 0x00;
bool WriteProcMem = false;
if (RemoveWriteProtection)
ChangeProtection(code, MemoryProtection.ExecuteReadWrite, out OldMemProt); // change protection
WriteProcMem = WriteProcessMemory(mProc.Handle, theCode, memory, (UIntPtr)size, IntPtr.Zero);
if (RemoveWriteProtection)
ChangeProtection(code, OldMemProt, out _); // restore
return WriteProcMem;
}
/// <summary>
/// Write to address and move by moveQty. Good for byte arrays. See https://github.com/erfg12/memory.dll/wiki/Writing-a-Byte-Array for more information.
/// </summary>
///<param name="code">address, module + pointer + offset, module + offset OR label in .ini file.</param>
///<param name="type">byte, bytes, float, int, string or long.</param>
/// <param name="write">byte to write</param>
/// <param name="MoveQty">quantity to move</param>
/// <param name="file">path and name of .ini file (OPTIONAL)</param>
/// <param name="SlowDown">milliseconds to sleep between each byte</param>
/// <returns></returns>
public bool WriteMove(string code, string type, string write, int MoveQty, string file = "", int SlowDown = 0)
{
byte[] memory = new byte[4];
int size = 4;
UIntPtr theCode;
theCode = GetCode(code, file);
if (type == "float")
{
memory = new byte[write.Length];
memory = BitConverter.GetBytes(Convert.ToSingle(write));
size = write.Length;
}
else if (type == "int")
{
memory = BitConverter.GetBytes(Convert.ToInt32(write));
size = 4;
}
else if (type == "double")
{
memory = BitConverter.GetBytes(Convert.ToDouble(write));
size = 8;
}
else if (type == "long")
{
memory = BitConverter.GetBytes(Convert.ToInt64(write));
size = 8;
}
else if (type == "byte")
{
memory = new byte[1];
memory[0] = Convert.ToByte(write, 16);
size = 1;
}
else if (type == "string")
{
memory = new byte[write.Length];
memory = System.Text.Encoding.UTF8.GetBytes(write);
size = write.Length;
}
UIntPtr newCode = UIntPtr.Add(theCode, MoveQty);
//Debug.Write("DEBUG: Writing bytes [TYPE:" + type + " ADDR:[O]" + theCode + " [N]" + newCode + " MQTY:" + MoveQty + "] " + String.Join(",", memory) + Environment.NewLine);
Thread.Sleep(SlowDown);
return WriteProcessMemory(mProc.Handle, newCode, memory, (UIntPtr)size, IntPtr.Zero);
}
/// <summary>
/// Write byte array to addresses.
/// </summary>
/// <param name="code">address to write to</param>
/// <param name="write">byte array to write</param>
/// <param name="file">path and name of ini file. (OPTIONAL)</param>
public void WriteBytes(string code, byte[] write, string file = "")
{
UIntPtr theCode;
theCode = GetCode(code, file);
WriteProcessMemory(mProc.Handle, theCode, write, (UIntPtr)write.Length, IntPtr.Zero);
}
/// <summary>
/// Takes an array of 8 booleans and writes to a single byte
/// </summary>
/// <param name="code">address to write to</param>
/// <param name="bits">Array of 8 booleans to write</param>
/// <param name="file">path and name of ini file. (OPTIONAL)</param>
public void WriteBits(string code, bool[] bits, string file = "")
{
if (bits.Length != 8)
throw new ArgumentException("Not enough bits for a whole byte", nameof(bits));
byte[] buf = new byte[1];
UIntPtr theCode = GetCode(code, file);
for (var i = 0; i < 8; i++)
{
if (bits[i])
buf[0] |= (byte)(1 << i);
}
WriteProcessMemory(mProc.Handle, theCode, buf, (UIntPtr)1, IntPtr.Zero);
}
/// <summary>
/// Write byte array to address
/// </summary>
/// <param name="address">Address to write to</param>
/// <param name="write">Byte array to write to</param>
public void WriteBytes(UIntPtr address, byte[] write)
{
WriteProcessMemory(mProc.Handle, address, write, (UIntPtr)write.Length, out IntPtr bytesRead);
}
}
}