/[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 812 - (show annotations) (download)
Tue Apr 15 14:52:10 2014 UTC (5 years, 9 months ago) by william
File size: 19489 byte(s)
+ update logging to use Enterpise.Logging -- more work is still needed

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

  ViewVC Help
Powered by ViewVC 1.1.22