ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/RomCheater/trunk/Win32/Sojaner.MemoryScanner/MemoryProviers/BaseMemoryProvider.cs
Revision: 575
Committed: Thu Jun 6 07:46:38 2013 UTC (10 years, 5 months ago) by william
File size: 19037 byte(s)
Log Message:

File Contents

# User Rev Content
1 william 414 #region Logging Defines
2     // include this any class or method that required logging, and comment-out what is not needed
3 william 415
4 william 414 #region Enabled logging levels
5     #define LOGGING_ENABLE_INFO
6     #define LOGGING_ENABLE_WARN
7     #define LOGGING_ENABLE_DEBUG
8     #define LOGGING_ENABLE_VERBOSEDEBUG
9     #define LOGGING_ENABLE_ERROR
10     #define LOGGING_ENABLE_VERBOSEERROR
11     #define LOGGING_ENABLE_PROFILER
12     #endregion
13     #endregion
14     using System;
15 william 231 using System.Collections.Generic;
16     using System.Linq;
17     using System.Text;
18     using RomCheater.Logging;
19     using System.Diagnostics;
20     using RomCheater.PluginFramework.Interfaces;
21     using System.IO;
22 william 408 using RomCheater.PluginFramework.Events;
23 william 231
24     namespace Sojaner.MemoryScanner.MemoryProviers
25     {
26 william 251 #region public abstract class BaseMemoryProvider
27 william 231 public abstract class BaseMemoryProvider :
28 william 255 IPatchMemory,
29     IReadMemory,
30 william 477 IAcceptsProcess<Process>,
31 william 231 IAcceptsPlugin<IConfigPlugin>,
32     IMemoryReader,
33     IMemoryWriter,
34 william 398 IFileWriter,
35 william 408 IDisposable,
36     IAcceptsBytesReadEvent
37 william 231 {
38     private ProcessMemoryReader provider;
39 william 477 public BaseMemoryProvider() { this.AcceptedPlugin = null; this.AcceptedProcess = null; isClosed = true; isOpen = false; }
40 william 231 public BaseMemoryProvider(IConfigPlugin config) : this() { this.AcceptedPlugin = config; }
41 william 477 public BaseMemoryProvider(IConfigPlugin config, Process process) : this() { this.AcceptedPlugin = config; this.AcceptedProcess = process; }
42     public BaseMemoryProvider(IAcceptsProcessAndConfig pconfig) : this() { this.AcceptedPlugin = pconfig.AcceptedPlugin; this.AcceptedProcess = pconfig.AcceptedProcess; }
43 william 231
44 william 245
45 william 408 public event BaseEventHandler<OnBytesReadEventArgs> OnBytesRead;
46    
47 william 245 private bool isOpen { get; set; }
48     private bool isClosed { get; set; }
49    
50 william 408 private void provider_OnBytesRead(OnBytesReadEventArgs e)
51     {
52     e.Sender = this;
53     if (this.OnBytesRead != null)
54     this.OnBytesRead.Invoke(e);
55     }
56 william 245 #region Open/Close Provider
57     #region public virtual void OpenProvider()
58     public virtual void OpenProvider()
59 william 231 {
60 william 245 if (isOpen)
61     {
62 william 424 logger.Warn.WriteLine("Provider has already been opened.");
63 william 245 return;
64     }
65     try
66     {
67     provider = new ProcessMemoryReader();
68 william 477 provider.ReadProcess = this.AcceptedProcess;
69 william 429 provider.OnBytesRead += new BaseEventHandler<OnBytesReadEventArgs>(provider_OnBytesRead);
70 william 477 if (provider.ReadProcess == null) { logger.Error.WriteLine("{0}.OpenProvider() Could not attach to process: {1}", "", this.GetType().Name, this.AcceptedProcess.ToString()); return; }
71 william 428 //provider.OpenProcess();
72 william 245 isOpen = true;
73     isClosed = false;
74     }
75     catch (Exception ex)
76     {
77 william 255 logger.Error.WriteLine("Failed to open provider: {0}{1}", System.Environment.NewLine, ex.ToString());
78 william 245 isOpen = false;
79     isClosed = true;
80     }
81 william 231 }
82 william 245 #endregion
83     #region public virtual void CloseProvider()
84     public virtual void CloseProvider()
85 william 231 {
86 william 255 if (isClosed)
87 william 245 {
88 william 424 logger.Warn.WriteLine("Provider has already been closed.");
89 william 245 return;
90     }
91     if (!isOpen)
92     {
93     logger.Warn.WriteLine("Provider cannot be closed, it was never opened...attempting to open provider.");
94     OpenProvider();
95     if (!isOpen)
96     {
97     logger.Warn.WriteLine("Could not open provider");
98     return;
99     }
100     }
101     try
102     {
103 william 246 //logger.VerboseDebug.WriteLine("CloseProvider(): System.Environment.StackTrace: {0}{1}", System.Environment.NewLine, System.Environment.StackTrace);
104 william 245 if (provider == null) return;
105 william 428 //provider.CloseHandle();
106 william 283 provider = null; // free any memory associated with the provider
107 william 245 isClosed = true;
108     isOpen = false;
109     }
110     catch (Exception ex)
111     {
112     logger.Error.WriteLine("Failed to close provider: {0}{1}", System.Environment.NewLine, ex.ToString());
113     isClosed = false;
114     if (isOpen)
115     {
116     throw new Exception("Provider is failed to close and still open.");
117     }
118     }
119 william 231 }
120 william 245 #endregion
121     #endregion
122    
123 william 231 #region IAcceptsProcess<Process> Members
124 william 477 public Process AcceptedProcess { get; set; }
125 william 231 #endregion
126     #region IAcceptsPlugin<IConfigPlugin> Members
127     public IConfigPlugin AcceptedPlugin { get; set; }
128     #endregion
129 william 245 #region EnsureProviderIsOpen methods : log and/or throw errors
130     private bool EnsureProviderIsOpen() { return EnsureProviderIsOpenOrThrowError(); }
131     private bool EnsureProviderIsOpenOrLogError() { return EnsureProviderIsOpenOrThrowOrLogError(false, true); }
132     private bool EnsureProviderIsOpenOrThrowError() { return EnsureProviderIsOpenOrThrowOrLogError(true, true); }
133     private bool EnsureProviderIsOpenOrThrowOrLogError(bool ThrowError, bool LogError)
134     {
135     if (!isOpen)
136     {
137     try { throw new Exception("Memory operation could not be completed because the provider is not open"); }
138     catch (Exception ex)
139     {
140     if (LogError)
141     logger.Error.WriteLine(ex.ToString());
142     if (ThrowError)
143     throw ex;
144     return false;
145     }
146     }
147     return true;
148     }
149     #endregion
150    
151 william 231 #region IPatchMemory members
152 william 378 #region public virtual bool PatchMemory(uint address, byte value)
153 william 575 public virtual bool PatchMemory(ulong address, byte value)
154 william 231 {
155 william 245 if (!EnsureProviderIsOpen()) { return false; }
156 william 370 return provider.PatchMemory(address, value);
157 william 231 }
158     #endregion
159 william 378 #region public virtual bool PatchMemory(uint address, sbyte value)
160 william 575 public virtual bool PatchMemory(ulong address, sbyte value)
161 william 231 {
162 william 245 if (!EnsureProviderIsOpen()) { return false; }
163 william 370 return provider.PatchMemory(address, value);
164 william 231 }
165     #endregion
166 william 378 #region public virtual bool PatchMemory(uint address, ushort value)
167 william 575 public virtual bool PatchMemory(ulong address, ushort value)
168 william 231 {
169 william 245 if (!EnsureProviderIsOpen()) { return false; }
170 william 370 return provider.PatchMemory(address, value);
171 william 231 }
172     #endregion
173 william 378 #region public virtual bool PatchMemory(uint address, short value)
174 william 575 public virtual bool PatchMemory(ulong address, short value)
175 william 231 {
176 william 245 if (!EnsureProviderIsOpen()) { return false; }
177 william 370 return provider.PatchMemory(address, value);
178 william 231 }
179     #endregion
180 william 378 #region public virtual bool PatchMemory(uint address, uint value)
181 william 575 public virtual bool PatchMemory(ulong address, uint value)
182 william 231 {
183 william 245 if (!EnsureProviderIsOpen()) { return false; }
184 william 370 return provider.PatchMemory(address, value);
185 william 231 }
186     #endregion
187 william 378 #region public virtual bool PatchMemory(uint address, int value)
188 william 575 public virtual bool PatchMemory(ulong address, int value)
189 william 231 {
190 william 245 if (!EnsureProviderIsOpen()) { return false; }
191 william 370 return provider.PatchMemory(address, value);
192 william 231 }
193     #endregion
194 william 378 #region public virtual bool PatchMemory(uint address, ulong value)
195 william 575 public virtual bool PatchMemory(ulong address, ulong value)
196 william 231 {
197 william 245 if (!EnsureProviderIsOpen()) { return false; }
198 william 370 return provider.PatchMemory(address, value);
199 william 231 }
200     #endregion
201 william 378 #region public virtual bool PatchMemory(uint address, long value)
202 william 575 public virtual bool PatchMemory(ulong address, long value)
203 william 231 {
204 william 245 if (!EnsureProviderIsOpen()) { return false; }
205 william 370 return provider.PatchMemory(address, value);
206 william 231 }
207     #endregion
208     #endregion
209    
210     #region IReadMemory members
211 william 378 #region public virtual bool ReadMemory(uint address, out byte value)
212 william 575 public virtual bool ReadMemory(ulong address, out byte value)
213 william 231 {
214 william 245 value = 0;
215 william 255 if (!EnsureProviderIsOpen()) { return false; }
216 william 370 return provider.ReadMemory(address, out value);
217 william 231 }
218     #endregion
219 william 378 #region public virtual bool ReadMemory(uint address, out sbyte value)
220 william 575 public virtual bool ReadMemory(ulong address, out sbyte value)
221 william 231 {
222 william 245 value = 0;
223     if (!EnsureProviderIsOpen()) { return false; }
224 william 370 return provider.ReadMemory(address, out value);
225 william 231 }
226     #endregion
227 william 378 #region public virtual bool ReadMemory(uint address, out ushort value)
228 william 575 public virtual bool ReadMemory(ulong address, out ushort value)
229 william 231 {
230 william 245 value = 0;
231     if (!EnsureProviderIsOpen()) { return false; }
232 william 370 return provider.ReadMemory(address, out value);
233 william 231 }
234     #endregion
235 william 378 #region public virtual bool ReadMemory(uint address, out short value)
236 william 575 public virtual bool ReadMemory(ulong address, out short value)
237 william 231 {
238 william 245 value = 0;
239     if (!EnsureProviderIsOpen()) { return false; }
240 william 370 return provider.ReadMemory(address, out value);
241 william 231 }
242     #endregion
243 william 378 #region public virtual bool ReadMemory(uint address, out uint value)
244 william 575 public virtual bool ReadMemory(ulong address, out uint value)
245 william 231 {
246 william 245 value = 0;
247     if (!EnsureProviderIsOpen()) { return false; }
248 william 370 return provider.ReadMemory(address, out value);
249 william 231 }
250     #endregion
251 william 378 #region public virtual bool ReadMemory(uint address, out int value)
252 william 575 public virtual bool ReadMemory(ulong address, out int value)
253 william 231 {
254 william 245 value = 0;
255     if (!EnsureProviderIsOpen()) { return false; }
256 william 370 return provider.ReadMemory(address, out value);
257 william 231 }
258     #endregion
259 william 378 #region public virtual bool ReadMemory(uint address, out ulong value)
260 william 575 public virtual bool ReadMemory(ulong address, out ulong value)
261 william 231 {
262 william 245 value = 0;
263     if (!EnsureProviderIsOpen()) { return false; }
264 william 370 return provider.ReadMemory(address, out value);
265 william 231 }
266     #endregion
267 william 378 #region public virtual bool ReadMemory(uint address, out long value)
268 william 575 public virtual bool ReadMemory(ulong address, out long value)
269 william 231 {
270 william 245 value = 0;
271     if (!EnsureProviderIsOpen()) { return false; }
272 william 370 return provider.ReadMemory(address, out value);
273 william 231 }
274     #endregion
275     #endregion
276    
277 william 543 #region IMemoryReader member
278 william 249 #region public virtual bool ReadFirstNonZeroByte(int MemoryAddress, uint bytesToRead, out int address)
279 william 575 public virtual bool ReadFirstNonZeroByte(long MemoryAddress, uint bytesToRead, out int address)
280 william 231 {
281 william 245 address = 0;
282     if (!EnsureProviderIsOpen()) { return false; }
283     try { provider.ReadFirstNonZeroByte(MemoryAddress, bytesToRead, out address); return true; }
284     catch { address = 0x00; return false; }
285 william 231 }
286 william 255 #endregion
287 william 404 #region public virtual void ReadProcessMemoryAtOnce(int MemoryAddress, uint bytesToRead, out int bytesRead, out byte[] data)
288 william 575 public virtual void ReadProcessMemoryAtOnce(ulong MemoryAddress, uint bytesToRead, object UserState)
289 william 408 {
290     if (!EnsureProviderIsOpen()) { return; }
291 william 425 try { provider.ReadProcessMemoryAtOnce(MemoryAddress, bytesToRead, UserState); }
292     catch { }
293 william 408 }
294 william 575 public virtual void ReadProcessMemoryAtOnce(ulong MemoryAddress, uint bytesToRead, out int bytesRead, out byte[] data)
295 william 378 {
296     bytesRead = 0x00;
297     data = new byte[bytesToRead];
298     if (!EnsureProviderIsOpen()) { return; }
299     try { provider.ReadProcessMemoryAtOnce(MemoryAddress, bytesToRead, out bytesRead, out data); }
300     catch { bytesRead = 0x00; data = new byte[] { }; }
301     }
302 william 543
303 william 575 public virtual void UpdateAddressArray(ulong[] addresses, uint size, out byte[][] values)
304 william 543 {
305     values = new byte[addresses.Length][];
306     for (int x = 0; x < values.Length; x++) { values[x] = new byte[size]; }
307     if (!EnsureProviderIsOpen()) { return; }
308     try { provider.UpdateAddressArray(addresses, size, out values); }
309     catch
310     {
311     values = new byte[addresses.Length][];
312     for (int x = 0; x < values.Length; x++) { values[x] = new byte[size]; }
313     }
314     }
315    
316 william 404 #endregion
317 william 249 #region public virtual void ReadProcessMemory(int MemoryAddress, int bytesToRead, out int bytesRead, out byte[] data)
318 william 575 public virtual void ReadProcessMemory(ulong MemoryAddress, uint bytesToRead, out int bytesRead, out byte[] data)
319 william 378 {
320     bytesRead = 0x00;
321     data = new byte[bytesToRead];
322     if (!EnsureProviderIsOpen()) { return; }
323     try { provider.ReadProcessMemory(MemoryAddress, bytesToRead, out bytesRead, out data); }
324     catch { bytesRead = 0x00; data = new byte[] { }; }
325     }
326 william 575 public virtual void ReadProcessMemory(long MemoryAddress, uint bytesToRead, out int bytesRead, out byte[] data)
327 william 231 {
328 william 245 bytesRead = 0x00;
329 william 273 data = new byte[bytesToRead];
330 william 245 if (!EnsureProviderIsOpen()) { return; }
331     try { provider.ReadProcessMemory(MemoryAddress, bytesToRead, out bytesRead, out data); }
332     catch { bytesRead = 0x00; data = new byte[] { }; }
333 william 231 }
334     #endregion
335     #region IMemoryWriter members
336 william 352 //#region public virtual void WriteProcessMemory(int MemoryAddress, byte byteToWrite, out int bytesWritten)
337     //public virtual void WriteProcessMemory(int MemoryAddress, byte byteToWrite, out int bytesWritten)
338     //{
339     // bytesWritten = 0;
340     // if (!EnsureProviderIsOpen()) { return; }
341     // try { provider.WriteProcessMemory(MemoryAddress, new byte[] { byteToWrite }, out bytesWritten); }
342     // catch { bytesWritten = 0x00; }
343     //}
344     //#endregion
345 william 249 #region public virtual void WriteProcessMemory(int MemoryAddress, byte[] bytesToWrite, out int bytesWritten)
346 william 575 public virtual void WriteProcessMemory(long MemoryAddress, byte[] bytesToWrite, out int bytesWritten)
347 william 231 {
348 william 245 bytesWritten = 0;
349     if (!EnsureProviderIsOpen()) { return; }
350     try { provider.WriteProcessMemory(MemoryAddress, bytesToWrite, out bytesWritten); }
351     catch { bytesWritten = 0x00; }
352 william 231 }
353 william 575 public virtual void WriteProcessMemory(ulong MemoryAddress, byte[] bytesToWrite, out uint bytesWritten)
354 william 378 {
355     bytesWritten = 0;
356     if (!EnsureProviderIsOpen()) { return; }
357     try { provider.WriteProcessMemory(MemoryAddress, bytesToWrite, out bytesWritten); }
358     catch { bytesWritten = 0x00; }
359     }
360 william 231 #endregion
361     #endregion
362     #endregion
363     #region IFileWriter members
364 william 249 #region public virtual bool WriteProcessMemoryToFile(string filename, int MemoryAddress, uint bytesToRead, out int bytesRead)
365 william 575 public virtual bool WriteProcessMemoryToFile(string filename, ulong MemoryAddress, uint bytesToRead, out int bytesRead)
366 william 231 {
367 william 245 bytesRead = 0;
368     if (!EnsureProviderIsOpen()) { return false; }
369     try { provider.WriteProcessMemoryToFile(filename, MemoryAddress, bytesToRead, out bytesRead); return true; }
370 william 231 catch
371 william 245 { bytesRead = 0x00; return false; }
372 william 231 }
373     #endregion
374     #endregion
375 william 398
376     #region IDisposable Support
377     // Track whether Dispose has been called.
378     private bool disposed = false;
379     // Implement IDisposable.
380     // Do not make this method virtual.
381     // A derived class should not be able to override this method.
382     public void Dispose()
383     {
384     Dispose(true);
385     // This object will be cleaned up by the Dispose method.
386     // Therefore, you should call GC.SupressFinalize to
387     // take this object off the finalization queue
388     // and prevent finalization code for this object
389     // from executing a second time.
390     GC.SuppressFinalize(this);
391     }
392     // Dispose(bool disposing) executes in two distinct scenarios.
393     // If disposing equals true, the method has been called directly
394     // or indirectly by a user's code. Managed and unmanaged resources
395     // can be disposed.
396     // If disposing equals false, the method has been called by the
397     // runtime from inside the finalizer and you should not reference
398     // other objects. Only unmanaged resources can be disposed.
399     protected virtual void Dispose(bool disposing)
400     {
401     // Check to see if Dispose has already been called.
402     if (!this.disposed)
403     {
404     // If disposing equals true, dispose all managed
405     // and unmanaged resources.
406     if (disposing)
407     {
408     // Dispose managed resources.
409     //component.Dispose();
410     }
411    
412     // Call the appropriate methods to clean up
413     // unmanaged resources here.
414     // If disposing is false,
415     // only the following code is executed.
416 william 399 if (!isClosed)
417     CloseProvider();
418 william 398 // Note disposing has been done.
419     disposed = true;
420    
421     }
422     }
423     // Use C# destructor syntax for finalization code.
424     // This destructor will run only if the Dispose method
425     // does not get called.
426     // It gives your base class the opportunity to finalize.
427     // Do not provide destructors in types derived from this class.
428     ~BaseMemoryProvider()
429     {
430     // Do not re-create Dispose clean-up code here.
431     // Calling Dispose(false) is optimal in terms of
432     // readability and maintainability.
433     Dispose(false);
434     }
435     #endregion
436 william 231 }
437 william 251 #endregion
438 william 255 }