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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

1 william 231 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 william 251 #region public abstract class BaseMemoryProvider
13 william 231 public abstract class BaseMemoryProvider :
14 william 255 IPatchMemory,
15     IReadMemory,
16 william 231 IAcceptsProcess<Process>,
17     IAcceptsPlugin<IConfigPlugin>,
18     IMemoryReader,
19     IMemoryWriter,
20     IFileWriter
21     {
22     private ProcessMemoryReader provider;
23 william 245 public BaseMemoryProvider() { this.AcceptedPlugin = null; this.AcceptedProcess = null; isClosed = true; isOpen = false; }
24 william 231 public BaseMemoryProvider(IConfigPlugin config) : this() { this.AcceptedPlugin = config; }
25     public BaseMemoryProvider(IConfigPlugin config, Process process) : this() { this.AcceptedPlugin = config; this.AcceptedProcess = process; }
26 william 258 public BaseMemoryProvider(IAcceptsProcessAndConfig pconfig) : this() { this.AcceptedPlugin = pconfig.AcceptedPlugin; this.AcceptedProcess = pconfig.AcceptedProcess; }
27 william 231
28 william 245
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 william 231 {
36 william 245 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 william 255 logger.Error.WriteLine("Failed to open provider: {0}{1}", System.Environment.NewLine, ex.ToString());
53 william 245 isOpen = false;
54     isClosed = true;
55     }
56 william 231 }
57 william 245 #endregion
58     #region public virtual void CloseProvider()
59     public virtual void CloseProvider()
60 william 231 {
61 william 255 if (isClosed)
62 william 245 {
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 william 246 //logger.VerboseDebug.WriteLine("CloseProvider(): System.Environment.StackTrace: {0}{1}", System.Environment.NewLine, System.Environment.StackTrace);
79 william 245 if (provider == null) return;
80     provider.CloseHandle();
81 william 283 provider = null; // free any memory associated with the provider
82 william 245 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 william 231 }
95 william 245 #endregion
96     #endregion
97    
98 william 231 #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 william 245 #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 william 231 #region IPatchMemory members
127 william 249 #region public virtual bool PatchMemory(int address, byte value)
128     public virtual bool PatchMemory(int address, byte value)
129 william 231 {
130 william 245 if (!EnsureProviderIsOpen()) { return false; }
131 william 231 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 william 249 #region public virtual bool PatchMemory(int address, sbyte value)
141     public virtual bool PatchMemory(int address, sbyte value)
142 william 231 {
143 william 245 if (!EnsureProviderIsOpen()) { return false; }
144 william 231 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 william 249 #region public virtual bool PatchMemory(int address, ushort value)
154     public virtual bool PatchMemory(int address, ushort value)
155 william 231 {
156 william 245 if (!EnsureProviderIsOpen()) { return false; }
157 william 231 int bytesWritten;
158     byte[] bitData = BitConverter.GetBytes(value);
159     provider.WriteProcessMemory(address, bitData, out bytesWritten);
160 william 255
161 william 231 ushort check = 0;
162     ReadMemory(address, out check);
163     if (check == value) return true;
164     return false;
165     }
166     #endregion
167 william 249 #region public virtual bool PatchMemory(int address, short value)
168     public virtual bool PatchMemory(int address, short value)
169 william 231 {
170 william 245 if (!EnsureProviderIsOpen()) { return false; }
171 william 231 int bytesWritten;
172     byte[] bitData = BitConverter.GetBytes(value);
173     provider.WriteProcessMemory(address, bitData, out bytesWritten);
174 william 255
175 william 231 short check = 0;
176     ReadMemory(address, out check);
177     if (check == value) return true;
178     return false;
179     }
180     #endregion
181 william 249 #region public virtual bool PatchMemory(int address, uint value)
182     public virtual bool PatchMemory(int address, uint value)
183 william 231 {
184 william 245 if (!EnsureProviderIsOpen()) { return false; }
185 william 231 int bytesWritten;
186     byte[] bitData = BitConverter.GetBytes(value);
187     provider.WriteProcessMemory(address, bitData, out bytesWritten);
188 william 255
189 william 231 uint check = 0;
190     ReadMemory(address, out check);
191     if (check == value) return true;
192     return false;
193     }
194     #endregion
195 william 249 #region public virtual bool PatchMemory(int address, int value)
196     public virtual bool PatchMemory(int address, int value)
197 william 231 {
198 william 245 if (!EnsureProviderIsOpen()) { return false; }
199 william 231 int bytesWritten;
200     byte[] bitData = BitConverter.GetBytes(value);
201     provider.WriteProcessMemory(address, bitData, out bytesWritten);
202 william 255
203 william 231 int check = 0;
204     ReadMemory(address, out check);
205     if (check == value) return true;
206     return false;
207     }
208     #endregion
209 william 249 #region public virtual bool PatchMemory(int address, ulong value)
210     public virtual bool PatchMemory(int address, ulong value)
211 william 231 {
212 william 245 if (!EnsureProviderIsOpen()) { return false; }
213 william 231 int bytesWritten;
214     byte[] bitData = BitConverter.GetBytes(value);
215     provider.WriteProcessMemory(address, bitData, out bytesWritten);
216 william 255
217 william 231 ulong check = 0;
218     ReadMemory(address, out check);
219     if (check == value) return true;
220     return false;
221     }
222     #endregion
223 william 249 #region public virtual bool PatchMemory(int address, long value)
224     public virtual bool PatchMemory(int address, long value)
225 william 231 {
226 william 245 if (!EnsureProviderIsOpen()) { return false; }
227 william 231 int bytesWritten;
228     byte[] bitData = BitConverter.GetBytes(value);
229     provider.WriteProcessMemory(address, bitData, out bytesWritten);
230 william 255
231 william 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 william 249 #region public virtual bool ReadMemory(int address, out byte value)
241     public virtual bool ReadMemory(int address, out byte value)
242 william 231 {
243 william 245 value = 0;
244 william 255 if (!EnsureProviderIsOpen()) { return false; }
245 william 231 try
246 william 255 {
247     int bytesReadSize;
248 william 231 byte[] bitData;
249     provider.ReadProcessMemory(address, sizeof(byte), out bytesReadSize, out bitData);
250     value = bitData[0];
251 william 255
252 william 231 return true;
253     }
254     catch
255     {
256     value = 0x00;
257     return false;
258     }
259     }
260     #endregion
261 william 249 #region public virtual bool ReadMemory(int address, out sbyte value)
262     public virtual bool ReadMemory(int address, out sbyte value)
263 william 231 {
264 william 245 value = 0;
265     if (!EnsureProviderIsOpen()) { return false; }
266 william 231 try
267 william 255 {
268 william 231 int bytesReadSize;
269 william 346 byte[] bitData = new byte[sizeof(sbyte)];
270     provider.ReadProcessMemory(address, sizeof(sbyte), out bytesReadSize, out bitData);
271 william 231 value = Convert.ToSByte(bitData[0]);
272 william 255
273 william 231 return true;
274     }
275     catch
276     {
277     value = 0x00;
278     return false;
279     }
280     }
281     #endregion
282 william 249 #region public virtual bool ReadMemory(int address, out ushort value)
283     public virtual bool ReadMemory(int address, out ushort value)
284 william 231 {
285 william 245 value = 0;
286     if (!EnsureProviderIsOpen()) { return false; }
287 william 231 try
288     {
289     int bytesReadSize;
290 william 346 byte[] bitData = new byte[sizeof(ushort)];
291     provider.ReadProcessMemory(address, sizeof(ushort), out bytesReadSize, out bitData);
292 william 231 value = BitConverter.ToUInt16(bitData, 0);
293     return true;
294     }
295     catch
296     {
297     value = 0x00;
298     return false;
299     }
300     }
301     #endregion
302 william 249 #region public virtual bool ReadMemory(int address, out short value)
303     public virtual bool ReadMemory(int address, out short value)
304 william 231 {
305 william 245 value = 0;
306     if (!EnsureProviderIsOpen()) { return false; }
307 william 231 try
308     {
309     int bytesReadSize;
310 william 346 byte[] bitData = new byte[sizeof(short)];
311     provider.ReadProcessMemory(address, sizeof(short), out bytesReadSize, out bitData);
312 william 231 value = BitConverter.ToInt16(bitData, 0);
313 william 255
314 william 231 return true;
315     }
316     catch
317     {
318     value = 0x00;
319     return false;
320     }
321     }
322     #endregion
323 william 249 #region public virtual bool ReadMemory(int address, out uint value)
324     public virtual bool ReadMemory(int address, out uint value)
325 william 231 {
326 william 245 value = 0;
327     if (!EnsureProviderIsOpen()) { return false; }
328 william 231 try
329     {
330     int bytesReadSize;
331 william 346 byte[] bitData = new byte[sizeof(uint)];
332     provider.ReadProcessMemory(address, sizeof(uint), out bytesReadSize, out bitData);
333 william 231 value = BitConverter.ToUInt32(bitData, 0);
334 william 255
335 william 231 return true;
336     }
337     catch
338     {
339     value = 0x00;
340     return false;
341     }
342     }
343     #endregion
344 william 249 #region public virtual bool ReadMemory(int address, out int value)
345     public virtual bool ReadMemory(int address, out int value)
346 william 231 {
347 william 245 value = 0;
348     if (!EnsureProviderIsOpen()) { return false; }
349 william 231 try
350     {
351     int bytesReadSize;
352 william 346 byte[] bitData = new byte[sizeof(int)];
353     provider.ReadProcessMemory(address, sizeof(int), out bytesReadSize, out bitData);
354 william 231 value = BitConverter.ToInt32(bitData, 0);
355 william 255
356 william 231 return true;
357     }
358     catch
359     {
360     value = 0x00;
361     return false;
362     }
363     }
364     #endregion
365 william 249 #region public virtual bool ReadMemory(int address, out ulong value)
366     public virtual bool ReadMemory(int address, out ulong value)
367 william 231 {
368 william 245 value = 0;
369     if (!EnsureProviderIsOpen()) { return false; }
370 william 231 try
371     {
372     int bytesReadSize;
373 william 346 byte[] bitData = new byte[sizeof(ulong)];
374     provider.ReadProcessMemory(address, sizeof(ulong), out bytesReadSize, out bitData);
375 william 231 value = BitConverter.ToUInt64(bitData, 0);
376 william 255
377 william 231 return true;
378     }
379     catch
380     {
381     value = 0x00;
382     return false;
383     }
384     }
385     #endregion
386 william 249 #region public virtual bool ReadMemory(int address, out long value)
387     public virtual bool ReadMemory(int address, out long value)
388 william 231 {
389 william 245 value = 0;
390     if (!EnsureProviderIsOpen()) { return false; }
391 william 231 try
392     {
393     int bytesReadSize;
394 william 346 byte[] bitData = new byte[sizeof(long)];
395     provider.ReadProcessMemory(address, sizeof(long), out bytesReadSize, out bitData);
396 william 231 value = BitConverter.ToInt64(bitData, 0);
397 william 255
398 william 231 return true;
399     }
400     catch
401     {
402     value = 0x00;
403     return false;
404     }
405     }
406     #endregion
407     #endregion
408    
409     #region IMemoryReader members
410 william 249 #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 william 231 {
413 william 245 address = 0;
414     if (!EnsureProviderIsOpen()) { return false; }
415     try { provider.ReadFirstNonZeroByte(MemoryAddress, bytesToRead, out address); return true; }
416     catch { address = 0x00; return false; }
417 william 231 }
418 william 255 #endregion
419 william 354 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 william 249 #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 william 231 {
430 william 245 bytesRead = 0x00;
431 william 273 data = new byte[bytesToRead];
432 william 245 if (!EnsureProviderIsOpen()) { return; }
433     try { provider.ReadProcessMemory(MemoryAddress, bytesToRead, out bytesRead, out data); }
434     catch { bytesRead = 0x00; data = new byte[] { }; }
435 william 231 }
436     #endregion
437     #region IMemoryWriter members
438 william 352 //#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 william 249 #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 william 231 {
450 william 245 bytesWritten = 0;
451     if (!EnsureProviderIsOpen()) { return; }
452     try { provider.WriteProcessMemory(MemoryAddress, bytesToWrite, out bytesWritten); }
453     catch { bytesWritten = 0x00; }
454 william 231 }
455     #endregion
456     #endregion
457     #endregion
458     #region IFileWriter members
459 william 249 #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 william 231 {
462 william 245 bytesRead = 0;
463     if (!EnsureProviderIsOpen()) { return false; }
464     try { provider.WriteProcessMemoryToFile(filename, MemoryAddress, bytesToRead, out bytesRead); return true; }
465 william 231 catch
466 william 245 { bytesRead = 0x00; return false; }
467 william 231 }
468     #endregion
469     #endregion
470     }
471 william 251 #endregion
472 william 255 }

  ViewVC Help
Powered by ViewVC 1.1.22