/[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 408 - (show annotations) (download)
Thu Jun 21 18:10:21 2012 UTC (8 years, 5 months ago) by william
File size: 18017 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22