/[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 398 - (hide annotations) (download)
Wed Jun 20 12:16:34 2012 UTC (8 years, 10 months ago) by william
File size: 17492 byte(s)
MemmoryProvider: using statements

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

  ViewVC Help
Powered by ViewVC 1.1.22