/[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 578 - (show annotations) (download)
Thu Jun 6 08:18:44 2013 UTC (6 years, 8 months ago) by william
File size: 19057 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
24 namespace Sojaner.MemoryScanner.MemoryProviers
25 {
26 #region public abstract class BaseMemoryProvider
27 public abstract class BaseMemoryProvider :
28 IPatchMemory,
29 IReadMemory,
30 IAcceptsProcess<Process>,
31 IAcceptsPlugin<IConfigPlugin>,
32 IMemoryReader,
33 IMemoryWriter,
34 IFileWriter,
35 IDisposable,
36 IAcceptsBytesReadEvent
37 {
38 private ProcessMemoryReader provider;
39 public BaseMemoryProvider() { this.AcceptedPlugin = null; this.AcceptedProcess = null; isClosed = true; isOpen = false; }
40 public BaseMemoryProvider(IConfigPlugin config) : this() { this.AcceptedPlugin = config; }
41 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
44
45 public event BaseEventHandler<OnBytesReadEventArgs> OnBytesRead;
46
47 private bool isOpen { get; set; }
48 private bool isClosed { get; set; }
49
50 private void provider_OnBytesRead(OnBytesReadEventArgs e)
51 {
52 e.Sender = this;
53 if (this.OnBytesRead != null)
54 this.OnBytesRead.Invoke(e);
55 }
56 #region Open/Close Provider
57 #region public virtual void OpenProvider()
58 public virtual void OpenProvider()
59 {
60 if (isOpen)
61 {
62 logger.Warn.WriteLine("Provider has already been opened.");
63 return;
64 }
65 try
66 {
67 provider = new ProcessMemoryReader();
68 provider.ReadProcess = this.AcceptedProcess;
69 provider.OnBytesRead += new BaseEventHandler<OnBytesReadEventArgs>(provider_OnBytesRead);
70 if (provider.ReadProcess == null) { logger.Error.WriteLine("{0}.OpenProvider() Could not attach to process: {1}", "", this.GetType().Name, this.AcceptedProcess.ToString()); return; }
71 //provider.OpenProcess();
72 isOpen = true;
73 isClosed = false;
74 }
75 catch (Exception ex)
76 {
77 logger.Error.WriteLine("Failed to open provider: {0}{1}", System.Environment.NewLine, ex.ToString());
78 isOpen = false;
79 isClosed = true;
80 }
81 }
82 #endregion
83 #region public virtual void CloseProvider()
84 public virtual void CloseProvider()
85 {
86 if (isClosed)
87 {
88 logger.Warn.WriteLine("Provider has already been closed.");
89 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 //logger.VerboseDebug.WriteLine("CloseProvider(): System.Environment.StackTrace: {0}{1}", System.Environment.NewLine, System.Environment.StackTrace);
104 if (provider == null) return;
105 //provider.CloseHandle();
106 provider = null; // free any memory associated with the provider
107 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 }
120 #endregion
121 #endregion
122
123 #region IAcceptsProcess<Process> Members
124 public Process AcceptedProcess { get; set; }
125 #endregion
126 #region IAcceptsPlugin<IConfigPlugin> Members
127 public IConfigPlugin AcceptedPlugin { get; set; }
128 #endregion
129 #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 #region IPatchMemory members
152 #region public virtual bool PatchMemory(uint address, byte value)
153 public virtual bool PatchMemory(ulong address, byte value)
154 {
155 if (!EnsureProviderIsOpen()) { return false; }
156 return provider.PatchMemory(address, value);
157 }
158 #endregion
159 #region public virtual bool PatchMemory(uint address, sbyte value)
160 public virtual bool PatchMemory(ulong address, sbyte value)
161 {
162 if (!EnsureProviderIsOpen()) { return false; }
163 return provider.PatchMemory(address, value);
164 }
165 #endregion
166 #region public virtual bool PatchMemory(uint address, ushort value)
167 public virtual bool PatchMemory(ulong address, ushort value)
168 {
169 if (!EnsureProviderIsOpen()) { return false; }
170 return provider.PatchMemory(address, value);
171 }
172 #endregion
173 #region public virtual bool PatchMemory(uint address, short value)
174 public virtual bool PatchMemory(ulong address, short value)
175 {
176 if (!EnsureProviderIsOpen()) { return false; }
177 return provider.PatchMemory(address, value);
178 }
179 #endregion
180 #region public virtual bool PatchMemory(uint address, uint value)
181 public virtual bool PatchMemory(ulong address, uint value)
182 {
183 if (!EnsureProviderIsOpen()) { return false; }
184 return provider.PatchMemory(address, value);
185 }
186 #endregion
187 #region public virtual bool PatchMemory(uint address, int value)
188 public virtual bool PatchMemory(ulong address, int value)
189 {
190 if (!EnsureProviderIsOpen()) { return false; }
191 return provider.PatchMemory(address, value);
192 }
193 #endregion
194 #region public virtual bool PatchMemory(uint address, ulong value)
195 public virtual bool PatchMemory(ulong address, ulong value)
196 {
197 if (!EnsureProviderIsOpen()) { return false; }
198 return provider.PatchMemory(address, value);
199 }
200 #endregion
201 #region public virtual bool PatchMemory(uint address, long value)
202 public virtual bool PatchMemory(ulong address, long value)
203 {
204 if (!EnsureProviderIsOpen()) { return false; }
205 return provider.PatchMemory(address, value);
206 }
207 #endregion
208 #endregion
209
210 #region IReadMemory members
211 #region public virtual bool ReadMemory(uint address, out byte value)
212 public virtual bool ReadMemory(ulong address, out byte value)
213 {
214 value = 0;
215 if (!EnsureProviderIsOpen()) { return false; }
216 return provider.ReadMemory(address, out value);
217 }
218 #endregion
219 #region public virtual bool ReadMemory(uint address, out sbyte value)
220 public virtual bool ReadMemory(ulong address, out sbyte value)
221 {
222 value = 0;
223 if (!EnsureProviderIsOpen()) { return false; }
224 return provider.ReadMemory(address, out value);
225 }
226 #endregion
227 #region public virtual bool ReadMemory(uint address, out ushort value)
228 public virtual bool ReadMemory(ulong address, out ushort value)
229 {
230 value = 0;
231 if (!EnsureProviderIsOpen()) { return false; }
232 return provider.ReadMemory(address, out value);
233 }
234 #endregion
235 #region public virtual bool ReadMemory(uint address, out short value)
236 public virtual bool ReadMemory(ulong address, out short value)
237 {
238 value = 0;
239 if (!EnsureProviderIsOpen()) { return false; }
240 return provider.ReadMemory(address, out value);
241 }
242 #endregion
243 #region public virtual bool ReadMemory(uint address, out uint value)
244 public virtual bool ReadMemory(ulong address, out uint value)
245 {
246 value = 0;
247 if (!EnsureProviderIsOpen()) { return false; }
248 return provider.ReadMemory(address, out value);
249 }
250 #endregion
251 #region public virtual bool ReadMemory(uint address, out int value)
252 public virtual bool ReadMemory(ulong address, out int value)
253 {
254 value = 0;
255 if (!EnsureProviderIsOpen()) { return false; }
256 return provider.ReadMemory(address, out value);
257 }
258 #endregion
259 #region public virtual bool ReadMemory(uint address, out ulong value)
260 public virtual bool ReadMemory(ulong address, out ulong value)
261 {
262 value = 0;
263 if (!EnsureProviderIsOpen()) { return false; }
264 return provider.ReadMemory(address, out value);
265 }
266 #endregion
267 #region public virtual bool ReadMemory(uint address, out long value)
268 public virtual bool ReadMemory(ulong address, out long value)
269 {
270 value = 0;
271 if (!EnsureProviderIsOpen()) { return false; }
272 return provider.ReadMemory(address, out value);
273 }
274 #endregion
275 #endregion
276
277 #region IMemoryReader member
278 #region public virtual bool ReadFirstNonZeroByte(int MemoryAddress, uint bytesToRead, out int address)
279 public virtual bool ReadFirstNonZeroByte(ulong MemoryAddress, ulong bytesToRead, out ulong address)
280 {
281 address = 0;
282 if (!EnsureProviderIsOpen()) { return false; }
283 try { provider.ReadFirstNonZeroByte(MemoryAddress, bytesToRead, out address); return true; }
284 catch { address = 0x00; return false; }
285 }
286 #endregion
287 #region public virtual void ReadProcessMemoryAtOnce(int MemoryAddress, uint bytesToRead, out int bytesRead, out byte[] data)
288 public virtual void ReadProcessMemoryAtOnce(ulong MemoryAddress, ulong bytesToRead, object UserState)
289 {
290 if (!EnsureProviderIsOpen()) { return; }
291 try { provider.ReadProcessMemoryAtOnce(MemoryAddress, bytesToRead, UserState); }
292 catch { }
293 }
294 public virtual void ReadProcessMemoryAtOnce(ulong MemoryAddress, ulong bytesToRead, out ulong bytesRead, out byte[] data)
295 {
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
303 public virtual void UpdateAddressArray(ulong[] addresses, ulong size, out byte[][] values)
304 {
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 #endregion
317 #region public virtual void ReadProcessMemory(int MemoryAddress, int bytesToRead, out int bytesRead, out byte[] data)
318 public virtual void ReadProcessMemory(ulong MemoryAddress, ulong bytesToRead, out ulong bytesRead, out byte[] data)
319 {
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 public virtual void ReadProcessMemory(long MemoryAddress, long bytesToRead, out ulong bytesRead, out byte[] data)
327 {
328 bytesRead = 0x00;
329 data = new byte[bytesToRead];
330 if (!EnsureProviderIsOpen()) { return; }
331 try { provider.ReadProcessMemory(MemoryAddress, bytesToRead, out bytesRead, out data); }
332 catch { bytesRead = 0x00; data = new byte[] { }; }
333 }
334 #endregion
335 #region IMemoryWriter members
336 //#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 #region public virtual void WriteProcessMemory(int MemoryAddress, byte[] bytesToWrite, out int bytesWritten)
346 public virtual void WriteProcessMemory(long MemoryAddress, byte[] bytesToWrite, out ulong bytesWritten)
347 {
348 bytesWritten = 0;
349 if (!EnsureProviderIsOpen()) { return; }
350 try { provider.WriteProcessMemory(MemoryAddress, bytesToWrite, out bytesWritten); }
351 catch { bytesWritten = 0x00; }
352 }
353 public virtual void WriteProcessMemory(ulong MemoryAddress, byte[] bytesToWrite, out ulong bytesWritten)
354 {
355 bytesWritten = 0;
356 if (!EnsureProviderIsOpen()) { return; }
357 try { provider.WriteProcessMemory(MemoryAddress, bytesToWrite, out bytesWritten); }
358 catch { bytesWritten = 0x00; }
359 }
360 #endregion
361 #endregion
362 #endregion
363 #region IFileWriter members
364 #region public virtual bool WriteProcessMemoryToFile(string filename, int MemoryAddress, uint bytesToRead, out int bytesRead)
365 public virtual bool WriteProcessMemoryToFile(string filename, ulong MemoryAddress, ulong bytesToRead, out ulong bytesRead)
366 {
367 bytesRead = 0;
368 if (!EnsureProviderIsOpen()) { return false; }
369 try { provider.WriteProcessMemoryToFile(filename, MemoryAddress, bytesToRead, out bytesRead); return true; }
370 catch
371 { bytesRead = 0x00; return false; }
372 }
373 #endregion
374 #endregion
375
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 if (!isClosed)
417 CloseProvider();
418 // 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 }
437 #endregion
438 }

  ViewVC Help
Powered by ViewVC 1.1.22