/[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 600 - (show annotations) (download)
Fri Jun 7 10:27:53 2013 UTC (7 years, 5 months ago) by william
File size: 19492 byte(s)

1 #region Logging Defines
2 // include this any class or method that required logging, and comment-out what is not needed
3
4 #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 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 using RomCheater.PluginFramework.Events;
23 using ManagedWinapi;
24
25 namespace Sojaner.MemoryScanner.MemoryProviers
26 {
27 #region public abstract class BaseMemoryProvider
28 public abstract class BaseMemoryProvider :
29 IPatchMemory,
30 IReadMemory,
31 IAcceptsProcess<Process>,
32 IAcceptsPlugin<IConfigPlugin>,
33 IMemoryReader,
34 IMemoryWriter,
35 IFileWriter,
36 IDisposable,
37 IAcceptsBytesReadEvent
38 {
39 private ProcessMemoryReader provider;
40 public BaseMemoryProvider() { this.AcceptedPlugin = null; this.AcceptedProcess = null; isClosed = true; isOpen = false; }
41 public BaseMemoryProvider(IConfigPlugin config) : this() { this.AcceptedPlugin = config; }
42 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
45
46 public event BaseEventHandler<OnBytesReadEventArgs> OnBytesRead;
47
48 private bool isOpen { get; set; }
49 private bool isClosed { get; set; }
50
51 private void provider_OnBytesRead(OnBytesReadEventArgs e)
52 {
53 e.Sender = this;
54 if (this.OnBytesRead != null)
55 this.OnBytesRead.Invoke(e);
56 }
57 #region Open/Close Provider
58 #region public virtual void OpenProvider()
59 public virtual void OpenProvider()
60 {
61 if (isOpen)
62 {
63 logger.Warn.WriteLine("Provider has already been opened.");
64 return;
65 }
66 try
67 {
68 provider = new ProcessMemoryReader();
69 provider.ReadProcess = this.AcceptedProcess;
70 provider.OnBytesRead += new BaseEventHandler<OnBytesReadEventArgs>(provider_OnBytesRead);
71 if (provider.ReadProcess == null) { logger.Error.WriteLine("{0}.OpenProvider() Could not attach to process: {1}", "", this.GetType().Name, this.AcceptedProcess.ToString()); return; }
72 //provider.OpenProcess();
73 isOpen = true;
74 isClosed = false;
75 }
76 catch (Exception ex)
77 {
78 logger.Error.WriteLine("Failed to open provider: {0}{1}", System.Environment.NewLine, ex.ToString());
79 isOpen = false;
80 isClosed = true;
81 }
82 }
83 #endregion
84 #region public virtual void CloseProvider()
85 public virtual void CloseProvider()
86 {
87 if (isClosed)
88 {
89 logger.Warn.WriteLine("Provider has already been closed.");
90 return;
91 }
92 if (!isOpen)
93 {
94 logger.Warn.WriteLine("Provider cannot be closed, it was never opened...attempting to open provider.");
95 OpenProvider();
96 if (!isOpen)
97 {
98 logger.Warn.WriteLine("Could not open provider");
99 return;
100 }
101 }
102 try
103 {
104 //logger.VerboseDebug.WriteLine("CloseProvider(): System.Environment.StackTrace: {0}{1}", System.Environment.NewLine, System.Environment.StackTrace);
105 if (provider == null) return;
106 //provider.CloseHandle();
107 provider = null; // free any memory associated with the provider
108 isClosed = true;
109 isOpen = false;
110 }
111 catch (Exception ex)
112 {
113 logger.Error.WriteLine("Failed to close provider: {0}{1}", System.Environment.NewLine, ex.ToString());
114 isClosed = false;
115 if (isOpen)
116 {
117 throw new Exception("Provider is failed to close and still open.");
118 }
119 }
120 }
121 #endregion
122 #endregion
123
124 #region IAcceptsProcess<Process> Members
125 public Process AcceptedProcess { get; set; }
126 #endregion
127 #region IAcceptsPlugin<IConfigPlugin> Members
128 public IConfigPlugin AcceptedPlugin { get; set; }
129 #endregion
130 #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 logger.Error.WriteLine(ex.ToString());
143 if (ThrowError)
144 throw ex;
145 return false;
146 }
147 }
148 return true;
149 }
150 #endregion
151
152 #region IPatchMemory members
153 #region public virtual bool PatchMemory(uint address, byte value)
154 public virtual bool PatchMemory(ulong address, byte value)
155 {
156 if (!EnsureProviderIsOpen()) { return false; }
157 return provider.PatchMemory(address, value);
158 }
159 #endregion
160 #region public virtual bool PatchMemory(uint address, sbyte value)
161 public virtual bool PatchMemory(ulong address, sbyte value)
162 {
163 if (!EnsureProviderIsOpen()) { return false; }
164 return provider.PatchMemory(address, value);
165 }
166 #endregion
167 #region public virtual bool PatchMemory(uint address, ushort value)
168 public virtual bool PatchMemory(ulong address, ushort value)
169 {
170 if (!EnsureProviderIsOpen()) { return false; }
171 return provider.PatchMemory(address, value);
172 }
173 #endregion
174 #region public virtual bool PatchMemory(uint address, short value)
175 public virtual bool PatchMemory(ulong address, short value)
176 {
177 if (!EnsureProviderIsOpen()) { return false; }
178 return provider.PatchMemory(address, value);
179 }
180 #endregion
181 #region public virtual bool PatchMemory(uint address, uint value)
182 public virtual bool PatchMemory(ulong address, uint value)
183 {
184 if (!EnsureProviderIsOpen()) { return false; }
185 return provider.PatchMemory(address, value);
186 }
187 #endregion
188 #region public virtual bool PatchMemory(uint address, int value)
189 public virtual bool PatchMemory(ulong address, int value)
190 {
191 if (!EnsureProviderIsOpen()) { return false; }
192 return provider.PatchMemory(address, value);
193 }
194 #endregion
195 #region public virtual bool PatchMemory(uint address, ulong value)
196 public virtual bool PatchMemory(ulong address, ulong value)
197 {
198 if (!EnsureProviderIsOpen()) { return false; }
199 return provider.PatchMemory(address, value);
200 }
201 #endregion
202 #region public virtual bool PatchMemory(uint address, long value)
203 public virtual bool PatchMemory(ulong address, long value)
204 {
205 if (!EnsureProviderIsOpen()) { return false; }
206 return provider.PatchMemory(address, value);
207 }
208 #endregion
209 #endregion
210
211 #region IReadMemory members
212 #region public virtual bool ReadMemory(uint address, out byte value)
213 public virtual bool ReadMemory(ulong address, out byte value)
214 {
215 value = 0;
216 if (!EnsureProviderIsOpen()) { return false; }
217 return provider.ReadMemory(address, out value);
218 }
219 #endregion
220 #region public virtual bool ReadMemory(uint address, out sbyte value)
221 public virtual bool ReadMemory(ulong address, out sbyte value)
222 {
223 value = 0;
224 if (!EnsureProviderIsOpen()) { return false; }
225 return provider.ReadMemory(address, out value);
226 }
227 #endregion
228 #region public virtual bool ReadMemory(uint address, out ushort value)
229 public virtual bool ReadMemory(ulong address, out ushort value)
230 {
231 value = 0;
232 if (!EnsureProviderIsOpen()) { return false; }
233 return provider.ReadMemory(address, out value);
234 }
235 #endregion
236 #region public virtual bool ReadMemory(uint address, out short value)
237 public virtual bool ReadMemory(ulong address, out short value)
238 {
239 value = 0;
240 if (!EnsureProviderIsOpen()) { return false; }
241 return provider.ReadMemory(address, out value);
242 }
243 #endregion
244 #region public virtual bool ReadMemory(uint address, out uint value)
245 public virtual bool ReadMemory(ulong address, out uint value)
246 {
247 value = 0;
248 if (!EnsureProviderIsOpen()) { return false; }
249 return provider.ReadMemory(address, out value);
250 }
251 #endregion
252 #region public virtual bool ReadMemory(uint address, out int value)
253 public virtual bool ReadMemory(ulong address, out int value)
254 {
255 value = 0;
256 if (!EnsureProviderIsOpen()) { return false; }
257 return provider.ReadMemory(address, out value);
258 }
259 #endregion
260 #region public virtual bool ReadMemory(uint address, out ulong value)
261 public virtual bool ReadMemory(ulong address, out ulong value)
262 {
263 value = 0;
264 if (!EnsureProviderIsOpen()) { return false; }
265 return provider.ReadMemory(address, out value);
266 }
267 #endregion
268 #region public virtual bool ReadMemory(uint address, out long value)
269 public virtual bool ReadMemory(ulong address, out long value)
270 {
271 value = 0;
272 if (!EnsureProviderIsOpen()) { return false; }
273 return provider.ReadMemory(address, out value);
274 }
275 #endregion
276 #endregion
277
278 #region IMemoryReader member
279 #region public void QueryMemoryRegion()
280 public List<MEMORY_REGION_INFORMATION> QueryMemoryRegions(ulong start, ulong size)
281 {
282 if (!EnsureProviderIsOpen()) { return new List<MEMORY_REGION_INFORMATION>(); }
283 try { return provider.QueryMemoryRegions(start,size); }
284 catch { return new List<MEMORY_REGION_INFORMATION>(); }
285 }
286 #endregion
287 #region public virtual bool ReadFirstNonZeroByte(int MemoryAddress, uint bytesToRead, out int address)
288 public virtual bool ReadFirstNonZeroByte(ulong MemoryAddress, ulong bytesToRead, out ulong address)
289 {
290 address = 0;
291 if (!EnsureProviderIsOpen()) { return false; }
292 try { provider.ReadFirstNonZeroByte(MemoryAddress, bytesToRead, out address); return true; }
293 catch { address = 0x00; return false; }
294 }
295 #endregion
296 #region public virtual void ReadProcessMemoryAtOnce(int MemoryAddress, uint bytesToRead, out int bytesRead, out byte[] data)
297 public virtual void ReadProcessMemoryAtOnce(ulong MemoryAddress, ulong bytesToRead, object UserState)
298 {
299 if (!EnsureProviderIsOpen()) { return; }
300 try { provider.ReadProcessMemoryAtOnce(MemoryAddress, bytesToRead, UserState); }
301 catch { }
302 }
303 public virtual void ReadProcessMemoryAtOnce(ulong MemoryAddress, ulong bytesToRead, out ulong bytesRead, out byte[] data)
304 {
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
312 public virtual void UpdateAddressArray(ulong[] addresses, ulong size, out byte[][] values)
313 {
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 #endregion
326 #region public virtual void ReadProcessMemory(int MemoryAddress, int bytesToRead, out int bytesRead, out byte[] data)
327 public virtual void ReadProcessMemory(ulong MemoryAddress, ulong bytesToRead, out ulong bytesRead, out byte[] data)
328 {
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 public virtual void ReadProcessMemory(long MemoryAddress, long bytesToRead, out ulong bytesRead, out byte[] data)
336 {
337 bytesRead = 0x00;
338 data = new byte[bytesToRead];
339 if (!EnsureProviderIsOpen()) { return; }
340 try { provider.ReadProcessMemory(MemoryAddress, bytesToRead, out bytesRead, out data); }
341 catch { bytesRead = 0x00; data = new byte[] { }; }
342 }
343 #endregion
344 #region IMemoryWriter members
345 //#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 #region public virtual void WriteProcessMemory(int MemoryAddress, byte[] bytesToWrite, out int bytesWritten)
355 public virtual void WriteProcessMemory(long MemoryAddress, byte[] bytesToWrite, out ulong bytesWritten)
356 {
357 bytesWritten = 0;
358 if (!EnsureProviderIsOpen()) { return; }
359 try { provider.WriteProcessMemory(MemoryAddress, bytesToWrite, out bytesWritten); }
360 catch { bytesWritten = 0x00; }
361 }
362 public virtual void WriteProcessMemory(ulong MemoryAddress, byte[] bytesToWrite, out ulong bytesWritten)
363 {
364 bytesWritten = 0;
365 if (!EnsureProviderIsOpen()) { return; }
366 try { provider.WriteProcessMemory(MemoryAddress, bytesToWrite, out bytesWritten); }
367 catch { bytesWritten = 0x00; }
368 }
369 #endregion
370 #endregion
371 #endregion
372 #region IFileWriter members
373 #region public virtual bool WriteProcessMemoryToFile(string filename, int MemoryAddress, uint bytesToRead, out int bytesRead)
374 public virtual bool WriteProcessMemoryToFile(string filename, ulong MemoryAddress, ulong bytesToRead, out ulong bytesRead)
375 {
376 bytesRead = 0;
377 if (!EnsureProviderIsOpen()) { return false; }
378 try { provider.WriteProcessMemoryToFile(filename, MemoryAddress, bytesToRead, out bytesRead); return true; }
379 catch
380 { bytesRead = 0x00; return false; }
381 }
382 #endregion
383 #endregion
384
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 if (!isClosed)
426 CloseProvider();
427 // 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 }
446 #endregion
447 }

  ViewVC Help
Powered by ViewVC 1.1.22