1 |
using System; |
2 |
using System.Collections.Generic; |
3 |
using System.Linq; |
4 |
using System.Text; |
5 |
using RomCheater.Logging; |
6 |
using System.Diagnostics; |
7 |
using RomCheater.PluginFramework.Interfaces; |
8 |
using System.IO; |
9 |
|
10 |
namespace Sojaner.MemoryScanner.MemoryProviers |
11 |
{ |
12 |
#region public abstract class BaseMemoryProvider |
13 |
public abstract class BaseMemoryProvider : |
14 |
IPatchMemory, |
15 |
IReadMemory, |
16 |
IAcceptsProcess<Process>, |
17 |
IAcceptsPlugin<IConfigPlugin>, |
18 |
IMemoryReader, |
19 |
IMemoryWriter, |
20 |
IFileWriter |
21 |
{ |
22 |
private ProcessMemoryReader provider; |
23 |
public BaseMemoryProvider() { this.AcceptedPlugin = null; this.AcceptedProcess = null; isClosed = true; isOpen = false; } |
24 |
public BaseMemoryProvider(IConfigPlugin config) : this() { this.AcceptedPlugin = config; } |
25 |
public BaseMemoryProvider(IConfigPlugin config, Process process) : this() { this.AcceptedPlugin = config; this.AcceptedProcess = process; } |
26 |
public BaseMemoryProvider(IAcceptsProcessAndConfig config) : this() { this.AcceptedPlugin = config.AcceptedPlugin; this.AcceptedProcess = config.AcceptedProcess; } |
27 |
|
28 |
|
29 |
private bool isOpen { get; set; } |
30 |
private bool isClosed { get; set; } |
31 |
|
32 |
#region Open/Close Provider |
33 |
#region public virtual void OpenProvider() |
34 |
public virtual void OpenProvider() |
35 |
{ |
36 |
if (isOpen) |
37 |
{ |
38 |
logger.Warn.WriteLine("Provider has already been opened."); |
39 |
return; |
40 |
} |
41 |
try |
42 |
{ |
43 |
provider = new ProcessMemoryReader(); |
44 |
provider.ReadProcess = this.AcceptedProcess; |
45 |
if (provider.ReadProcess == null) { logger.Error.WriteLine("{0}.OpenProvider() Could not attach to process: {1}", "", this.GetType().Name, this.AcceptedProcess.ToString()); return; } |
46 |
provider.OpenProcess(); |
47 |
isOpen = true; |
48 |
isClosed = false; |
49 |
} |
50 |
catch (Exception ex) |
51 |
{ |
52 |
logger.Error.WriteLine("Failed to open provider: {0}{1}",System.Environment.NewLine, ex.ToString()); |
53 |
isOpen = false; |
54 |
isClosed = true; |
55 |
} |
56 |
} |
57 |
#endregion |
58 |
#region public virtual void CloseProvider() |
59 |
public virtual void CloseProvider() |
60 |
{ |
61 |
if (isClosed) |
62 |
{ |
63 |
logger.Warn.WriteLine("Provider has already been closed."); |
64 |
return; |
65 |
} |
66 |
if (!isOpen) |
67 |
{ |
68 |
logger.Warn.WriteLine("Provider cannot be closed, it was never opened...attempting to open provider."); |
69 |
OpenProvider(); |
70 |
if (!isOpen) |
71 |
{ |
72 |
logger.Warn.WriteLine("Could not open provider"); |
73 |
return; |
74 |
} |
75 |
} |
76 |
try |
77 |
{ |
78 |
//logger.VerboseDebug.WriteLine("CloseProvider(): System.Environment.StackTrace: {0}{1}", System.Environment.NewLine, System.Environment.StackTrace); |
79 |
if (provider == null) return; |
80 |
provider.CloseHandle(); |
81 |
//provider = null; // free any memory associated with the provider |
82 |
isClosed = true; |
83 |
isOpen = false; |
84 |
} |
85 |
catch (Exception ex) |
86 |
{ |
87 |
logger.Error.WriteLine("Failed to close provider: {0}{1}", System.Environment.NewLine, ex.ToString()); |
88 |
isClosed = false; |
89 |
if (isOpen) |
90 |
{ |
91 |
throw new Exception("Provider is failed to close and still open."); |
92 |
} |
93 |
} |
94 |
} |
95 |
#endregion |
96 |
#endregion |
97 |
|
98 |
#region IAcceptsProcess<Process> Members |
99 |
public Process AcceptedProcess { get; set; } |
100 |
#endregion |
101 |
#region IAcceptsPlugin<IConfigPlugin> Members |
102 |
public IConfigPlugin AcceptedPlugin { get; set; } |
103 |
#endregion |
104 |
#region EnsureProviderIsOpen methods : log and/or throw errors |
105 |
private bool EnsureProviderIsOpen() { return EnsureProviderIsOpenOrThrowError(); } |
106 |
private bool EnsureProviderIsOpenOrLogError() { return EnsureProviderIsOpenOrThrowOrLogError(false, true); } |
107 |
private bool EnsureProviderIsOpenOrThrowError() { return EnsureProviderIsOpenOrThrowOrLogError(true, true); } |
108 |
private bool EnsureProviderIsOpenOrThrowOrLogError(bool ThrowError, bool LogError) |
109 |
{ |
110 |
if (!isOpen) |
111 |
{ |
112 |
try { throw new Exception("Memory operation could not be completed because the provider is not open"); } |
113 |
catch (Exception ex) |
114 |
{ |
115 |
if (LogError) |
116 |
logger.Error.WriteLine(ex.ToString()); |
117 |
if (ThrowError) |
118 |
throw ex; |
119 |
return false; |
120 |
} |
121 |
} |
122 |
return true; |
123 |
} |
124 |
#endregion |
125 |
|
126 |
#region IPatchMemory members |
127 |
#region public virtual bool PatchMemory(int address, byte value) |
128 |
public virtual bool PatchMemory(int address, byte value) |
129 |
{ |
130 |
if (!EnsureProviderIsOpen()) { return false; } |
131 |
int bytesWritten; |
132 |
byte[] bitData = BitConverter.GetBytes(value); |
133 |
provider.WriteProcessMemory(address, bitData, out bytesWritten); |
134 |
byte check = 0; |
135 |
ReadMemory(address, out check); |
136 |
if (check == value) return true; |
137 |
return false; |
138 |
} |
139 |
#endregion |
140 |
#region public virtual bool PatchMemory(int address, sbyte value) |
141 |
public virtual bool PatchMemory(int address, sbyte value) |
142 |
{ |
143 |
if (!EnsureProviderIsOpen()) { return false; } |
144 |
int bytesWritten; |
145 |
byte[] bitData = BitConverter.GetBytes(value); |
146 |
provider.WriteProcessMemory(address, bitData, out bytesWritten); |
147 |
sbyte check = 0; |
148 |
ReadMemory(address, out check); |
149 |
if (check == value) return true; |
150 |
return false; |
151 |
} |
152 |
#endregion |
153 |
#region public virtual bool PatchMemory(int address, ushort value) |
154 |
public virtual bool PatchMemory(int address, ushort value) |
155 |
{ |
156 |
if (!EnsureProviderIsOpen()) { return false; } |
157 |
int bytesWritten; |
158 |
byte[] bitData = BitConverter.GetBytes(value); |
159 |
provider.WriteProcessMemory(address, bitData, out bytesWritten); |
160 |
|
161 |
ushort check = 0; |
162 |
ReadMemory(address, out check); |
163 |
if (check == value) return true; |
164 |
return false; |
165 |
} |
166 |
#endregion |
167 |
#region public virtual bool PatchMemory(int address, short value) |
168 |
public virtual bool PatchMemory(int address, short value) |
169 |
{ |
170 |
if (!EnsureProviderIsOpen()) { return false; } |
171 |
int bytesWritten; |
172 |
byte[] bitData = BitConverter.GetBytes(value); |
173 |
provider.WriteProcessMemory(address, bitData, out bytesWritten); |
174 |
|
175 |
short check = 0; |
176 |
ReadMemory(address, out check); |
177 |
if (check == value) return true; |
178 |
return false; |
179 |
} |
180 |
#endregion |
181 |
#region public virtual bool PatchMemory(int address, uint value) |
182 |
public virtual bool PatchMemory(int address, uint value) |
183 |
{ |
184 |
if (!EnsureProviderIsOpen()) { return false; } |
185 |
int bytesWritten; |
186 |
byte[] bitData = BitConverter.GetBytes(value); |
187 |
provider.WriteProcessMemory(address, bitData, out bytesWritten); |
188 |
|
189 |
uint check = 0; |
190 |
ReadMemory(address, out check); |
191 |
if (check == value) return true; |
192 |
return false; |
193 |
} |
194 |
#endregion |
195 |
#region public virtual bool PatchMemory(int address, int value) |
196 |
public virtual bool PatchMemory(int address, int value) |
197 |
{ |
198 |
if (!EnsureProviderIsOpen()) { return false; } |
199 |
int bytesWritten; |
200 |
byte[] bitData = BitConverter.GetBytes(value); |
201 |
provider.WriteProcessMemory(address, bitData, out bytesWritten); |
202 |
|
203 |
int check = 0; |
204 |
ReadMemory(address, out check); |
205 |
if (check == value) return true; |
206 |
return false; |
207 |
} |
208 |
#endregion |
209 |
#region public virtual bool PatchMemory(int address, ulong value) |
210 |
public virtual bool PatchMemory(int address, ulong value) |
211 |
{ |
212 |
if (!EnsureProviderIsOpen()) { return false; } |
213 |
int bytesWritten; |
214 |
byte[] bitData = BitConverter.GetBytes(value); |
215 |
provider.WriteProcessMemory(address, bitData, out bytesWritten); |
216 |
|
217 |
ulong check = 0; |
218 |
ReadMemory(address, out check); |
219 |
if (check == value) return true; |
220 |
return false; |
221 |
} |
222 |
#endregion |
223 |
#region public virtual bool PatchMemory(int address, long value) |
224 |
public virtual bool PatchMemory(int address, long value) |
225 |
{ |
226 |
if (!EnsureProviderIsOpen()) { return false; } |
227 |
int bytesWritten; |
228 |
byte[] bitData = BitConverter.GetBytes(value); |
229 |
provider.WriteProcessMemory(address, bitData, out bytesWritten); |
230 |
|
231 |
long check = 0; |
232 |
ReadMemory(address, out check); |
233 |
if (check == value) return true; |
234 |
return false; |
235 |
} |
236 |
#endregion |
237 |
#endregion |
238 |
|
239 |
#region IReadMemory members |
240 |
#region public virtual bool ReadMemory(int address, out byte value) |
241 |
public virtual bool ReadMemory(int address, out byte value) |
242 |
{ |
243 |
value = 0; |
244 |
if (!EnsureProviderIsOpen()) { return false; } |
245 |
try |
246 |
{ int bytesReadSize; |
247 |
byte[] bitData; |
248 |
provider.ReadProcessMemory(address, sizeof(byte), out bytesReadSize, out bitData); |
249 |
value = bitData[0]; |
250 |
|
251 |
return true; |
252 |
} |
253 |
catch |
254 |
{ |
255 |
value = 0x00; |
256 |
return false; |
257 |
} |
258 |
} |
259 |
#endregion |
260 |
#region public virtual bool ReadMemory(int address, out sbyte value) |
261 |
public virtual bool ReadMemory(int address, out sbyte value) |
262 |
{ |
263 |
value = 0; |
264 |
if (!EnsureProviderIsOpen()) { return false; } |
265 |
try |
266 |
{ |
267 |
int bytesReadSize; |
268 |
byte[] bitData; |
269 |
provider.ReadProcessMemory(address, sizeof(byte), out bytesReadSize, out bitData); |
270 |
value = Convert.ToSByte(bitData[0]); |
271 |
|
272 |
return true; |
273 |
} |
274 |
catch |
275 |
{ |
276 |
value = 0x00; |
277 |
return false; |
278 |
} |
279 |
} |
280 |
#endregion |
281 |
#region public virtual bool ReadMemory(int address, out ushort value) |
282 |
public virtual bool ReadMemory(int address, out ushort value) |
283 |
{ |
284 |
value = 0; |
285 |
if (!EnsureProviderIsOpen()) { return false; } |
286 |
try |
287 |
{ |
288 |
int bytesReadSize; |
289 |
byte[] bitData; |
290 |
provider.ReadProcessMemory(address, sizeof(byte), out bytesReadSize, out bitData); |
291 |
value = BitConverter.ToUInt16(bitData, 0); |
292 |
|
293 |
return true; |
294 |
} |
295 |
catch |
296 |
{ |
297 |
value = 0x00; |
298 |
return false; |
299 |
} |
300 |
} |
301 |
#endregion |
302 |
#region public virtual bool ReadMemory(int address, out short value) |
303 |
public virtual bool ReadMemory(int address, out short value) |
304 |
{ |
305 |
value = 0; |
306 |
if (!EnsureProviderIsOpen()) { return false; } |
307 |
try |
308 |
{ |
309 |
int bytesReadSize; |
310 |
byte[] bitData; |
311 |
provider.ReadProcessMemory(address, sizeof(byte), out bytesReadSize, out bitData); |
312 |
value = BitConverter.ToInt16(bitData, 0); |
313 |
|
314 |
return true; |
315 |
} |
316 |
catch |
317 |
{ |
318 |
value = 0x00; |
319 |
return false; |
320 |
} |
321 |
} |
322 |
#endregion |
323 |
#region public virtual bool ReadMemory(int address, out uint value) |
324 |
public virtual bool ReadMemory(int address, out uint value) |
325 |
{ |
326 |
value = 0; |
327 |
if (!EnsureProviderIsOpen()) { return false; } |
328 |
try |
329 |
{ |
330 |
int bytesReadSize; |
331 |
byte[] bitData; |
332 |
provider.ReadProcessMemory(address, sizeof(byte), out bytesReadSize, out bitData); |
333 |
value = BitConverter.ToUInt32(bitData, 0); |
334 |
|
335 |
return true; |
336 |
} |
337 |
catch |
338 |
{ |
339 |
value = 0x00; |
340 |
return false; |
341 |
} |
342 |
} |
343 |
#endregion |
344 |
#region public virtual bool ReadMemory(int address, out int value) |
345 |
public virtual bool ReadMemory(int address, out int value) |
346 |
{ |
347 |
value = 0; |
348 |
if (!EnsureProviderIsOpen()) { return false; } |
349 |
try |
350 |
{ |
351 |
int bytesReadSize; |
352 |
byte[] bitData; |
353 |
provider.ReadProcessMemory(address, sizeof(byte), out bytesReadSize, out bitData); |
354 |
value = BitConverter.ToInt32(bitData, 0); |
355 |
|
356 |
return true; |
357 |
} |
358 |
catch |
359 |
{ |
360 |
value = 0x00; |
361 |
return false; |
362 |
} |
363 |
} |
364 |
#endregion |
365 |
#region public virtual bool ReadMemory(int address, out ulong value) |
366 |
public virtual bool ReadMemory(int address, out ulong value) |
367 |
{ |
368 |
value = 0; |
369 |
if (!EnsureProviderIsOpen()) { return false; } |
370 |
try |
371 |
{ |
372 |
int bytesReadSize; |
373 |
byte[] bitData; |
374 |
provider.ReadProcessMemory(address, sizeof(byte), out bytesReadSize, out bitData); |
375 |
value = BitConverter.ToUInt64(bitData, 0); |
376 |
|
377 |
return true; |
378 |
} |
379 |
catch |
380 |
{ |
381 |
value = 0x00; |
382 |
return false; |
383 |
} |
384 |
} |
385 |
#endregion |
386 |
#region public virtual bool ReadMemory(int address, out long value) |
387 |
public virtual bool ReadMemory(int address, out long value) |
388 |
{ |
389 |
value = 0; |
390 |
if (!EnsureProviderIsOpen()) { return false; } |
391 |
try |
392 |
{ |
393 |
int bytesReadSize; |
394 |
byte[] bitData; |
395 |
provider.ReadProcessMemory(address, sizeof(byte), out bytesReadSize, out bitData); |
396 |
value = BitConverter.ToInt64(bitData, 0); |
397 |
|
398 |
return true; |
399 |
} |
400 |
catch |
401 |
{ |
402 |
value = 0x00; |
403 |
return false; |
404 |
} |
405 |
} |
406 |
#endregion |
407 |
#endregion |
408 |
|
409 |
#region IMemoryReader members |
410 |
#region public virtual bool ReadFirstNonZeroByte(int MemoryAddress, uint bytesToRead, out int address) |
411 |
public virtual bool ReadFirstNonZeroByte(int MemoryAddress, uint bytesToRead, out int address) |
412 |
{ |
413 |
address = 0; |
414 |
if (!EnsureProviderIsOpen()) { return false; } |
415 |
try { provider.ReadFirstNonZeroByte(MemoryAddress, bytesToRead, out address); return true; } |
416 |
catch { address = 0x00; return false; } |
417 |
} |
418 |
#endregion |
419 |
#region public virtual void ReadProcessMemory(int MemoryAddress, int bytesToRead, out int bytesRead, out byte[] data) |
420 |
public virtual void ReadProcessMemory(int MemoryAddress, uint bytesToRead, out int bytesRead, out byte[] data) |
421 |
{ |
422 |
bytesRead = 0x00; |
423 |
data = new byte[] { }; |
424 |
if (!EnsureProviderIsOpen()) { return; } |
425 |
try { provider.ReadProcessMemory(MemoryAddress, bytesToRead, out bytesRead, out data); } |
426 |
catch { bytesRead = 0x00; data = new byte[] { }; } |
427 |
} |
428 |
#endregion |
429 |
#region IMemoryWriter members |
430 |
#region public virtual void WriteProcessMemory(int MemoryAddress, byte byteToWrite, out int bytesWritten) |
431 |
public virtual void WriteProcessMemory(int MemoryAddress, byte byteToWrite, out int bytesWritten) |
432 |
{ |
433 |
bytesWritten = 0; |
434 |
if (!EnsureProviderIsOpen()) { return; } |
435 |
try { provider.WriteProcessMemory(MemoryAddress, new byte[] { byteToWrite }, out bytesWritten); } |
436 |
catch { bytesWritten = 0x00; } |
437 |
} |
438 |
#endregion |
439 |
#region public virtual void WriteProcessMemory(int MemoryAddress, byte[] bytesToWrite, out int bytesWritten) |
440 |
public virtual void WriteProcessMemory(int MemoryAddress, byte[] bytesToWrite, out int bytesWritten) |
441 |
{ |
442 |
bytesWritten = 0; |
443 |
if (!EnsureProviderIsOpen()) { return; } |
444 |
try { provider.WriteProcessMemory(MemoryAddress, bytesToWrite, out bytesWritten); } |
445 |
catch { bytesWritten = 0x00; } |
446 |
} |
447 |
#endregion |
448 |
#endregion |
449 |
#endregion |
450 |
#region IFileWriter members |
451 |
#region public virtual bool WriteProcessMemoryToFile(string filename, int MemoryAddress, uint bytesToRead, out int bytesRead) |
452 |
public virtual bool WriteProcessMemoryToFile(string filename, int MemoryAddress, uint bytesToRead, out int bytesRead) |
453 |
{ |
454 |
bytesRead = 0; |
455 |
if (!EnsureProviderIsOpen()) { return false; } |
456 |
try { provider.WriteProcessMemoryToFile(filename, MemoryAddress, bytesToRead, out bytesRead); return true; } |
457 |
catch |
458 |
{ bytesRead = 0x00; return false; } |
459 |
} |
460 |
#endregion |
461 |
#endregion |
462 |
} |
463 |
#endregion |
464 |
|
465 |
#region MemoryScanner-r204 |
466 |
namespace r204 |
467 |
{ |
468 |
#region public abstract class BaseMemoryProvider |
469 |
public abstract class BaseMemoryProvider : |
470 |
IPatchMemory, |
471 |
IReadMemory, |
472 |
IAcceptsProcess<Process>, |
473 |
IAcceptsPlugin<IConfigPlugin>, |
474 |
IMemoryReader, |
475 |
IMemoryWriter, |
476 |
IFileWriter |
477 |
{ |
478 |
private Sojaner.MemoryScanner.r204.ProcessMemoryReader provider; |
479 |
public BaseMemoryProvider() { this.AcceptedPlugin = null; this.AcceptedProcess = null; isClosed = true; isOpen = false; } |
480 |
public BaseMemoryProvider(IConfigPlugin config) : this() { this.AcceptedPlugin = config; } |
481 |
public BaseMemoryProvider(IConfigPlugin config, Process process) : this() { this.AcceptedPlugin = config; this.AcceptedProcess = process; } |
482 |
public BaseMemoryProvider(IAcceptsProcessAndConfig config) : this() { this.AcceptedPlugin = config.AcceptedPlugin; this.AcceptedProcess = config.AcceptedProcess; } |
483 |
|
484 |
|
485 |
private bool isOpen { get; set; } |
486 |
private bool isClosed { get; set; } |
487 |
|
488 |
#region Open/Close Provider |
489 |
#region public virtual void OpenProvider() |
490 |
public virtual void OpenProvider() |
491 |
{ |
492 |
if (isOpen) |
493 |
{ |
494 |
logger.Warn.WriteLine("Provider has already been opened."); |
495 |
return; |
496 |
} |
497 |
try |
498 |
{ |
499 |
provider = new Sojaner.MemoryScanner.r204.ProcessMemoryReader(); |
500 |
provider.ReadProcess = this.AcceptedProcess; |
501 |
if (provider.ReadProcess == null) { logger.Error.WriteLine("{0}.OpenProvider() Could not attach to process: {1}", "", this.GetType().Name, this.AcceptedProcess.ToString()); return; } |
502 |
provider.OpenProcess(); |
503 |
isOpen = true; |
504 |
isClosed = false; |
505 |
} |
506 |
catch (Exception ex) |
507 |
{ |
508 |
logger.Error.WriteLine("Failed to open provider: {0}{1}", System.Environment.NewLine, ex.ToString()); |
509 |
isOpen = false; |
510 |
isClosed = true; |
511 |
} |
512 |
} |
513 |
#endregion |
514 |
#region public virtual void CloseProvider() |
515 |
public virtual void CloseProvider() |
516 |
{ |
517 |
if (isClosed) |
518 |
{ |
519 |
logger.Warn.WriteLine("Provider has already been closed."); |
520 |
return; |
521 |
} |
522 |
if (!isOpen) |
523 |
{ |
524 |
logger.Warn.WriteLine("Provider cannot be closed, it was never opened...attempting to open provider."); |
525 |
OpenProvider(); |
526 |
if (!isOpen) |
527 |
{ |
528 |
logger.Warn.WriteLine("Could not open provider"); |
529 |
return; |
530 |
} |
531 |
} |
532 |
try |
533 |
{ |
534 |
//logger.VerboseDebug.WriteLine("CloseProvider(): System.Environment.StackTrace: {0}{1}", System.Environment.NewLine, System.Environment.StackTrace); |
535 |
if (provider == null) return; |
536 |
provider.CloseHandle(); |
537 |
//provider = null; // free any memory associated with the provider |
538 |
isClosed = true; |
539 |
isOpen = false; |
540 |
} |
541 |
catch (Exception ex) |
542 |
{ |
543 |
logger.Error.WriteLine("Failed to close provider: {0}{1}", System.Environment.NewLine, ex.ToString()); |
544 |
isClosed = false; |
545 |
if (isOpen) |
546 |
{ |
547 |
throw new Exception("Provider is failed to close and still open."); |
548 |
} |
549 |
} |
550 |
} |
551 |
#endregion |
552 |
#endregion |
553 |
|
554 |
#region IAcceptsProcess<Process> Members |
555 |
public Process AcceptedProcess { get; set; } |
556 |
#endregion |
557 |
#region IAcceptsPlugin<IConfigPlugin> Members |
558 |
public IConfigPlugin AcceptedPlugin { get; set; } |
559 |
#endregion |
560 |
#region EnsureProviderIsOpen methods : log and/or throw errors |
561 |
private bool EnsureProviderIsOpen() { return EnsureProviderIsOpenOrThrowError(); } |
562 |
private bool EnsureProviderIsOpenOrLogError() { return EnsureProviderIsOpenOrThrowOrLogError(false, true); } |
563 |
private bool EnsureProviderIsOpenOrThrowError() { return EnsureProviderIsOpenOrThrowOrLogError(true, true); } |
564 |
private bool EnsureProviderIsOpenOrThrowOrLogError(bool ThrowError, bool LogError) |
565 |
{ |
566 |
if (!isOpen) |
567 |
{ |
568 |
try { throw new Exception("Memory operation could not be completed because the provider is not open"); } |
569 |
catch (Exception ex) |
570 |
{ |
571 |
if (LogError) |
572 |
logger.Error.WriteLine(ex.ToString()); |
573 |
if (ThrowError) |
574 |
throw ex; |
575 |
return false; |
576 |
} |
577 |
} |
578 |
return true; |
579 |
} |
580 |
#endregion |
581 |
|
582 |
#region IPatchMemory members |
583 |
#region public virtual bool PatchMemory(int address, byte value) |
584 |
public virtual bool PatchMemory(int address, byte value) |
585 |
{ |
586 |
if (!EnsureProviderIsOpen()) { return false; } |
587 |
int bytesWritten; |
588 |
byte[] bitData = BitConverter.GetBytes(value); |
589 |
provider.WriteProcessMemory(address, bitData, out bytesWritten); |
590 |
byte check = 0; |
591 |
ReadMemory(address, out check); |
592 |
if (check == value) return true; |
593 |
return false; |
594 |
} |
595 |
#endregion |
596 |
#region public virtual bool PatchMemory(int address, sbyte value) |
597 |
public virtual bool PatchMemory(int address, sbyte value) |
598 |
{ |
599 |
if (!EnsureProviderIsOpen()) { return false; } |
600 |
int bytesWritten; |
601 |
byte[] bitData = BitConverter.GetBytes(value); |
602 |
provider.WriteProcessMemory(address, bitData, out bytesWritten); |
603 |
sbyte check = 0; |
604 |
ReadMemory(address, out check); |
605 |
if (check == value) return true; |
606 |
return false; |
607 |
} |
608 |
#endregion |
609 |
#region public virtual bool PatchMemory(int address, ushort value) |
610 |
public virtual bool PatchMemory(int address, ushort value) |
611 |
{ |
612 |
if (!EnsureProviderIsOpen()) { return false; } |
613 |
int bytesWritten; |
614 |
byte[] bitData = BitConverter.GetBytes(value); |
615 |
provider.WriteProcessMemory(address, bitData, out bytesWritten); |
616 |
|
617 |
ushort check = 0; |
618 |
ReadMemory(address, out check); |
619 |
if (check == value) return true; |
620 |
return false; |
621 |
} |
622 |
#endregion |
623 |
#region public virtual bool PatchMemory(int address, short value) |
624 |
public virtual bool PatchMemory(int address, short value) |
625 |
{ |
626 |
if (!EnsureProviderIsOpen()) { return false; } |
627 |
int bytesWritten; |
628 |
byte[] bitData = BitConverter.GetBytes(value); |
629 |
provider.WriteProcessMemory(address, bitData, out bytesWritten); |
630 |
|
631 |
short check = 0; |
632 |
ReadMemory(address, out check); |
633 |
if (check == value) return true; |
634 |
return false; |
635 |
} |
636 |
#endregion |
637 |
#region public virtual bool PatchMemory(int address, uint value) |
638 |
public virtual bool PatchMemory(int address, uint value) |
639 |
{ |
640 |
if (!EnsureProviderIsOpen()) { return false; } |
641 |
int bytesWritten; |
642 |
byte[] bitData = BitConverter.GetBytes(value); |
643 |
provider.WriteProcessMemory(address, bitData, out bytesWritten); |
644 |
|
645 |
uint check = 0; |
646 |
ReadMemory(address, out check); |
647 |
if (check == value) return true; |
648 |
return false; |
649 |
} |
650 |
#endregion |
651 |
#region public virtual bool PatchMemory(int address, int value) |
652 |
public virtual bool PatchMemory(int address, int value) |
653 |
{ |
654 |
if (!EnsureProviderIsOpen()) { return false; } |
655 |
int bytesWritten; |
656 |
byte[] bitData = BitConverter.GetBytes(value); |
657 |
provider.WriteProcessMemory(address, bitData, out bytesWritten); |
658 |
|
659 |
int check = 0; |
660 |
ReadMemory(address, out check); |
661 |
if (check == value) return true; |
662 |
return false; |
663 |
} |
664 |
#endregion |
665 |
#region public virtual bool PatchMemory(int address, ulong value) |
666 |
public virtual bool PatchMemory(int address, ulong value) |
667 |
{ |
668 |
if (!EnsureProviderIsOpen()) { return false; } |
669 |
int bytesWritten; |
670 |
byte[] bitData = BitConverter.GetBytes(value); |
671 |
provider.WriteProcessMemory(address, bitData, out bytesWritten); |
672 |
|
673 |
ulong check = 0; |
674 |
ReadMemory(address, out check); |
675 |
if (check == value) return true; |
676 |
return false; |
677 |
} |
678 |
#endregion |
679 |
#region public virtual bool PatchMemory(int address, long value) |
680 |
public virtual bool PatchMemory(int address, long value) |
681 |
{ |
682 |
if (!EnsureProviderIsOpen()) { return false; } |
683 |
int bytesWritten; |
684 |
byte[] bitData = BitConverter.GetBytes(value); |
685 |
provider.WriteProcessMemory(address, bitData, out bytesWritten); |
686 |
|
687 |
long check = 0; |
688 |
ReadMemory(address, out check); |
689 |
if (check == value) return true; |
690 |
return false; |
691 |
} |
692 |
#endregion |
693 |
#endregion |
694 |
|
695 |
#region IReadMemory members |
696 |
#region public virtual bool ReadMemory(int address, out byte value) |
697 |
public virtual bool ReadMemory(int address, out byte value) |
698 |
{ |
699 |
value = 0; |
700 |
if (!EnsureProviderIsOpen()) { return false; } |
701 |
try |
702 |
{ |
703 |
int bytesReadSize; |
704 |
byte[] bitData; |
705 |
provider.ReadProcessMemory(address, sizeof(byte), out bytesReadSize, out bitData); |
706 |
value = bitData[0]; |
707 |
|
708 |
return true; |
709 |
} |
710 |
catch |
711 |
{ |
712 |
value = 0x00; |
713 |
return false; |
714 |
} |
715 |
} |
716 |
#endregion |
717 |
#region public virtual bool ReadMemory(int address, out sbyte value) |
718 |
public virtual bool ReadMemory(int address, out sbyte value) |
719 |
{ |
720 |
value = 0; |
721 |
if (!EnsureProviderIsOpen()) { return false; } |
722 |
try |
723 |
{ |
724 |
int bytesReadSize; |
725 |
byte[] bitData; |
726 |
provider.ReadProcessMemory(address, sizeof(byte), out bytesReadSize, out bitData); |
727 |
value = Convert.ToSByte(bitData[0]); |
728 |
|
729 |
return true; |
730 |
} |
731 |
catch |
732 |
{ |
733 |
value = 0x00; |
734 |
return false; |
735 |
} |
736 |
} |
737 |
#endregion |
738 |
#region public virtual bool ReadMemory(int address, out ushort value) |
739 |
public virtual bool ReadMemory(int address, out ushort value) |
740 |
{ |
741 |
value = 0; |
742 |
if (!EnsureProviderIsOpen()) { return false; } |
743 |
try |
744 |
{ |
745 |
int bytesReadSize; |
746 |
byte[] bitData; |
747 |
provider.ReadProcessMemory(address, sizeof(byte), out bytesReadSize, out bitData); |
748 |
value = BitConverter.ToUInt16(bitData, 0); |
749 |
|
750 |
return true; |
751 |
} |
752 |
catch |
753 |
{ |
754 |
value = 0x00; |
755 |
return false; |
756 |
} |
757 |
} |
758 |
#endregion |
759 |
#region public virtual bool ReadMemory(int address, out short value) |
760 |
public virtual bool ReadMemory(int address, out short value) |
761 |
{ |
762 |
value = 0; |
763 |
if (!EnsureProviderIsOpen()) { return false; } |
764 |
try |
765 |
{ |
766 |
int bytesReadSize; |
767 |
byte[] bitData; |
768 |
provider.ReadProcessMemory(address, sizeof(byte), out bytesReadSize, out bitData); |
769 |
value = BitConverter.ToInt16(bitData, 0); |
770 |
|
771 |
return true; |
772 |
} |
773 |
catch |
774 |
{ |
775 |
value = 0x00; |
776 |
return false; |
777 |
} |
778 |
} |
779 |
#endregion |
780 |
#region public virtual bool ReadMemory(int address, out uint value) |
781 |
public virtual bool ReadMemory(int address, out uint value) |
782 |
{ |
783 |
value = 0; |
784 |
if (!EnsureProviderIsOpen()) { return false; } |
785 |
try |
786 |
{ |
787 |
int bytesReadSize; |
788 |
byte[] bitData; |
789 |
provider.ReadProcessMemory(address, sizeof(byte), out bytesReadSize, out bitData); |
790 |
value = BitConverter.ToUInt32(bitData, 0); |
791 |
|
792 |
return true; |
793 |
} |
794 |
catch |
795 |
{ |
796 |
value = 0x00; |
797 |
return false; |
798 |
} |
799 |
} |
800 |
#endregion |
801 |
#region public virtual bool ReadMemory(int address, out int value) |
802 |
public virtual bool ReadMemory(int address, out int value) |
803 |
{ |
804 |
value = 0; |
805 |
if (!EnsureProviderIsOpen()) { return false; } |
806 |
try |
807 |
{ |
808 |
int bytesReadSize; |
809 |
byte[] bitData; |
810 |
provider.ReadProcessMemory(address, sizeof(byte), out bytesReadSize, out bitData); |
811 |
value = BitConverter.ToInt32(bitData, 0); |
812 |
|
813 |
return true; |
814 |
} |
815 |
catch |
816 |
{ |
817 |
value = 0x00; |
818 |
return false; |
819 |
} |
820 |
} |
821 |
#endregion |
822 |
#region public virtual bool ReadMemory(int address, out ulong value) |
823 |
public virtual bool ReadMemory(int address, out ulong value) |
824 |
{ |
825 |
value = 0; |
826 |
if (!EnsureProviderIsOpen()) { return false; } |
827 |
try |
828 |
{ |
829 |
int bytesReadSize; |
830 |
byte[] bitData; |
831 |
provider.ReadProcessMemory(address, sizeof(byte), out bytesReadSize, out bitData); |
832 |
value = BitConverter.ToUInt64(bitData, 0); |
833 |
|
834 |
return true; |
835 |
} |
836 |
catch |
837 |
{ |
838 |
value = 0x00; |
839 |
return false; |
840 |
} |
841 |
} |
842 |
#endregion |
843 |
#region public virtual bool ReadMemory(int address, out long value) |
844 |
public virtual bool ReadMemory(int address, out long value) |
845 |
{ |
846 |
value = 0; |
847 |
if (!EnsureProviderIsOpen()) { return false; } |
848 |
try |
849 |
{ |
850 |
int bytesReadSize; |
851 |
byte[] bitData; |
852 |
provider.ReadProcessMemory(address, sizeof(byte), out bytesReadSize, out bitData); |
853 |
value = BitConverter.ToInt64(bitData, 0); |
854 |
|
855 |
return true; |
856 |
} |
857 |
catch |
858 |
{ |
859 |
value = 0x00; |
860 |
return false; |
861 |
} |
862 |
} |
863 |
#endregion |
864 |
#endregion |
865 |
|
866 |
#region IMemoryReader members |
867 |
#region public virtual bool ReadFirstNonZeroByte(int MemoryAddress, uint bytesToRead, out int address) |
868 |
public virtual bool ReadFirstNonZeroByte(int MemoryAddress, uint bytesToRead, out int address) |
869 |
{ |
870 |
address = 0; |
871 |
if (!EnsureProviderIsOpen()) { return false; } |
872 |
try { provider.ReadFirstNonZeroByte(MemoryAddress, bytesToRead, out address); return true; } |
873 |
catch { address = 0x00; return false; } |
874 |
} |
875 |
#endregion |
876 |
#region public virtual void ReadProcessMemory(int MemoryAddress, int bytesToRead, out int bytesRead, out byte[] data) |
877 |
public virtual void ReadProcessMemory(int MemoryAddress, uint bytesToRead, out int bytesRead, out byte[] data) |
878 |
{ |
879 |
bytesRead = 0x00; |
880 |
data = new byte[] { }; |
881 |
if (!EnsureProviderIsOpen()) { return; } |
882 |
try { provider.ReadProcessMemory(MemoryAddress, bytesToRead, out bytesRead, out data); } |
883 |
catch { bytesRead = 0x00; data = new byte[] { }; } |
884 |
} |
885 |
#endregion |
886 |
#region IMemoryWriter members |
887 |
#region public virtual void WriteProcessMemory(int MemoryAddress, byte byteToWrite, out int bytesWritten) |
888 |
public virtual void WriteProcessMemory(int MemoryAddress, byte byteToWrite, out int bytesWritten) |
889 |
{ |
890 |
bytesWritten = 0; |
891 |
if (!EnsureProviderIsOpen()) { return; } |
892 |
try { provider.WriteProcessMemory(MemoryAddress, new byte[] { byteToWrite }, out bytesWritten); } |
893 |
catch { bytesWritten = 0x00; } |
894 |
} |
895 |
#endregion |
896 |
#region public virtual void WriteProcessMemory(int MemoryAddress, byte[] bytesToWrite, out int bytesWritten) |
897 |
public virtual void WriteProcessMemory(int MemoryAddress, byte[] bytesToWrite, out int bytesWritten) |
898 |
{ |
899 |
bytesWritten = 0; |
900 |
if (!EnsureProviderIsOpen()) { return; } |
901 |
try { provider.WriteProcessMemory(MemoryAddress, bytesToWrite, out bytesWritten); } |
902 |
catch { bytesWritten = 0x00; } |
903 |
} |
904 |
#endregion |
905 |
#endregion |
906 |
#endregion |
907 |
#region IFileWriter members |
908 |
#region public virtual bool WriteProcessMemoryToFile(string filename, int MemoryAddress, uint bytesToRead, out int bytesRead) |
909 |
public virtual bool WriteProcessMemoryToFile(string filename, int MemoryAddress, uint bytesToRead, out int bytesRead) |
910 |
{ |
911 |
bytesRead = 0; |
912 |
if (!EnsureProviderIsOpen()) { return false; } |
913 |
try { provider.WriteProcessMemoryToFile(filename, MemoryAddress, bytesToRead, out bytesRead); return true; } |
914 |
catch |
915 |
{ bytesRead = 0x00; return false; } |
916 |
} |
917 |
#endregion |
918 |
#endregion |
919 |
} |
920 |
#endregion |
921 |
} |
922 |
#endregion |
923 |
} |