/[RomCheater]/trunk/Win32/Sojaner.MemoryScanner/MemoryProviers/BaseMemoryProvider.cs
ViewVC logotype

Contents of /trunk/Win32/Sojaner.MemoryScanner/MemoryProviers/BaseMemoryProvider.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 354 - (show annotations) (download)
Sat Jun 9 21:46:37 2012 UTC (9 years, 6 months ago) by william
File size: 18738 byte(s)

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 pconfig) : this() { this.AcceptedPlugin = pconfig.AcceptedPlugin; this.AcceptedProcess = pconfig.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 {
247 int bytesReadSize;
248 byte[] bitData;
249 provider.ReadProcessMemory(address, sizeof(byte), out bytesReadSize, out bitData);
250 value = bitData[0];
251
252 return true;
253 }
254 catch
255 {
256 value = 0x00;
257 return false;
258 }
259 }
260 #endregion
261 #region public virtual bool ReadMemory(int address, out sbyte value)
262 public virtual bool ReadMemory(int address, out sbyte value)
263 {
264 value = 0;
265 if (!EnsureProviderIsOpen()) { return false; }
266 try
267 {
268 int bytesReadSize;
269 byte[] bitData = new byte[sizeof(sbyte)];
270 provider.ReadProcessMemory(address, sizeof(sbyte), out bytesReadSize, out bitData);
271 value = Convert.ToSByte(bitData[0]);
272
273 return true;
274 }
275 catch
276 {
277 value = 0x00;
278 return false;
279 }
280 }
281 #endregion
282 #region public virtual bool ReadMemory(int address, out ushort value)
283 public virtual bool ReadMemory(int address, out ushort value)
284 {
285 value = 0;
286 if (!EnsureProviderIsOpen()) { return false; }
287 try
288 {
289 int bytesReadSize;
290 byte[] bitData = new byte[sizeof(ushort)];
291 provider.ReadProcessMemory(address, sizeof(ushort), out bytesReadSize, out bitData);
292 value = BitConverter.ToUInt16(bitData, 0);
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 = new byte[sizeof(short)];
311 provider.ReadProcessMemory(address, sizeof(short), 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 = new byte[sizeof(uint)];
332 provider.ReadProcessMemory(address, sizeof(uint), 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 = new byte[sizeof(int)];
353 provider.ReadProcessMemory(address, sizeof(int), 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 = new byte[sizeof(ulong)];
374 provider.ReadProcessMemory(address, sizeof(ulong), 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 = new byte[sizeof(long)];
395 provider.ReadProcessMemory(address, sizeof(long), 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 public void ReadProcessMemoryAtOnce(int MemoryAddress, uint bytesToRead, out int bytesRead, out byte[] data)
420 {
421 bytesRead = 0x00;
422 data = new byte[bytesToRead];
423 if (!EnsureProviderIsOpen()) { return; }
424 try { provider.ReadProcessMemoryAtOnce(MemoryAddress, bytesToRead, out bytesRead, out data); }
425 catch { bytesRead = 0x00; data = new byte[] { }; }
426 }
427 #region public virtual void ReadProcessMemory(int MemoryAddress, int bytesToRead, out int bytesRead, out byte[] data)
428 public virtual void ReadProcessMemory(int MemoryAddress, uint bytesToRead, out int bytesRead, out byte[] data)
429 {
430 bytesRead = 0x00;
431 data = new byte[bytesToRead];
432 if (!EnsureProviderIsOpen()) { return; }
433 try { provider.ReadProcessMemory(MemoryAddress, bytesToRead, out bytesRead, out data); }
434 catch { bytesRead = 0x00; data = new byte[] { }; }
435 }
436 #endregion
437 #region IMemoryWriter members
438 //#region public virtual void WriteProcessMemory(int MemoryAddress, byte byteToWrite, out int bytesWritten)
439 //public virtual void WriteProcessMemory(int MemoryAddress, byte byteToWrite, out int bytesWritten)
440 //{
441 // bytesWritten = 0;
442 // if (!EnsureProviderIsOpen()) { return; }
443 // try { provider.WriteProcessMemory(MemoryAddress, new byte[] { byteToWrite }, out bytesWritten); }
444 // catch { bytesWritten = 0x00; }
445 //}
446 //#endregion
447 #region public virtual void WriteProcessMemory(int MemoryAddress, byte[] bytesToWrite, out int bytesWritten)
448 public virtual void WriteProcessMemory(int MemoryAddress, byte[] bytesToWrite, out int bytesWritten)
449 {
450 bytesWritten = 0;
451 if (!EnsureProviderIsOpen()) { return; }
452 try { provider.WriteProcessMemory(MemoryAddress, bytesToWrite, out bytesWritten); }
453 catch { bytesWritten = 0x00; }
454 }
455 #endregion
456 #endregion
457 #endregion
458 #region IFileWriter members
459 #region public virtual bool WriteProcessMemoryToFile(string filename, int MemoryAddress, uint bytesToRead, out int bytesRead)
460 public virtual bool WriteProcessMemoryToFile(string filename, int MemoryAddress, uint bytesToRead, out int bytesRead)
461 {
462 bytesRead = 0;
463 if (!EnsureProviderIsOpen()) { return false; }
464 try { provider.WriteProcessMemoryToFile(filename, MemoryAddress, bytesToRead, out bytesRead); return true; }
465 catch
466 { bytesRead = 0x00; return false; }
467 }
468 #endregion
469 #endregion
470 }
471 #endregion
472 }

  ViewVC Help
Powered by ViewVC 1.1.22