/[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 889 - (hide annotations) (download)
Wed Sep 17 04:55:52 2014 UTC (6 years, 7 months ago) by william
File size: 19481 byte(s)


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

  ViewVC Help
Powered by ViewVC 1.1.22