/[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 249 - (hide annotations) (download)
Sun Jun 3 16:09:05 2012 UTC (9 years, 6 months ago) by william
File size: 18221 byte(s)
change address from uint to int

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     public abstract class BaseMemoryProvider :
13     IPatchMemory,
14     IReadMemory,
15     IAcceptsProcess<Process>,
16     IAcceptsPlugin<IConfigPlugin>,
17     IMemoryReader,
18     IMemoryWriter,
19     IFileWriter
20     {
21     private ProcessMemoryReader provider;
22 william 245 public BaseMemoryProvider() { this.AcceptedPlugin = null; this.AcceptedProcess = null; isClosed = true; isOpen = false; }
23 william 231 public BaseMemoryProvider(IConfigPlugin config) : this() { this.AcceptedPlugin = config; }
24     public BaseMemoryProvider(IConfigPlugin config, Process process) : this() { this.AcceptedPlugin = config; this.AcceptedProcess = process; }
25     public BaseMemoryProvider(IAcceptsProcessAndConfig config) : this() { this.AcceptedPlugin = config.AcceptedPlugin; this.AcceptedProcess = config.AcceptedProcess; }
26    
27 william 245
28     private bool isOpen { get; set; }
29     private bool isClosed { get; set; }
30    
31     #region Open/Close Provider
32     #region public virtual void OpenProvider()
33     public virtual void OpenProvider()
34 william 231 {
35 william 245 if (isOpen)
36     {
37     logger.Warn.WriteLine("Provider has already been opened.");
38     return;
39     }
40     try
41     {
42     provider = new ProcessMemoryReader();
43     provider.ReadProcess = this.AcceptedProcess;
44     if (provider.ReadProcess == null) { logger.Error.WriteLine("{0}.OpenProvider() Could not attach to process: {1}", "", this.GetType().Name, this.AcceptedProcess.ToString()); return; }
45     provider.OpenProcess();
46     isOpen = true;
47     isClosed = false;
48     }
49     catch (Exception ex)
50     {
51     logger.Error.WriteLine("Failed to open provider: {0}{1}",System.Environment.NewLine, ex.ToString());
52     isOpen = false;
53     isClosed = true;
54     }
55 william 231 }
56 william 245 #endregion
57     #region public virtual void CloseProvider()
58     public virtual void CloseProvider()
59 william 231 {
60 william 245 if (isClosed)
61     {
62     logger.Warn.WriteLine("Provider has already been closed.");
63     return;
64     }
65     if (!isOpen)
66     {
67     logger.Warn.WriteLine("Provider cannot be closed, it was never opened...attempting to open provider.");
68     OpenProvider();
69     if (!isOpen)
70     {
71     logger.Warn.WriteLine("Could not open provider");
72     return;
73     }
74     }
75     try
76     {
77 william 246 //logger.VerboseDebug.WriteLine("CloseProvider(): System.Environment.StackTrace: {0}{1}", System.Environment.NewLine, System.Environment.StackTrace);
78 william 245 if (provider == null) return;
79     provider.CloseHandle();
80 william 249 //provider = null; // free any memory associated with the provider
81 william 245 isClosed = true;
82     isOpen = false;
83     }
84     catch (Exception ex)
85     {
86     logger.Error.WriteLine("Failed to close provider: {0}{1}", System.Environment.NewLine, ex.ToString());
87     isClosed = false;
88     if (isOpen)
89     {
90     throw new Exception("Provider is failed to close and still open.");
91     }
92     }
93 william 231 }
94 william 245 #endregion
95     #endregion
96    
97 william 231 #region IAcceptsProcess<Process> Members
98     public Process AcceptedProcess { get; set; }
99     #endregion
100     #region IAcceptsPlugin<IConfigPlugin> Members
101     public IConfigPlugin AcceptedPlugin { get; set; }
102     #endregion
103 william 245 #region EnsureProviderIsOpen methods : log and/or throw errors
104     private bool EnsureProviderIsOpen() { return EnsureProviderIsOpenOrThrowError(); }
105     private bool EnsureProviderIsOpenOrLogError() { return EnsureProviderIsOpenOrThrowOrLogError(false, true); }
106     private bool EnsureProviderIsOpenOrThrowError() { return EnsureProviderIsOpenOrThrowOrLogError(true, true); }
107     private bool EnsureProviderIsOpenOrThrowOrLogError(bool ThrowError, bool LogError)
108     {
109     if (!isOpen)
110     {
111     try { throw new Exception("Memory operation could not be completed because the provider is not open"); }
112     catch (Exception ex)
113     {
114     if (LogError)
115     logger.Error.WriteLine(ex.ToString());
116     if (ThrowError)
117     throw ex;
118     return false;
119     }
120     }
121     return true;
122     }
123     #endregion
124    
125 william 231 #region IPatchMemory members
126 william 249 #region public virtual bool PatchMemory(int address, byte value)
127     public virtual bool PatchMemory(int address, byte value)
128 william 231 {
129 william 245 if (!EnsureProviderIsOpen()) { return false; }
130 william 231 int bytesWritten;
131     byte[] bitData = BitConverter.GetBytes(value);
132     provider.WriteProcessMemory(address, bitData, out bytesWritten);
133     byte check = 0;
134     ReadMemory(address, out check);
135     if (check == value) return true;
136     return false;
137     }
138     #endregion
139 william 249 #region public virtual bool PatchMemory(int address, sbyte value)
140     public virtual bool PatchMemory(int address, sbyte value)
141 william 231 {
142 william 245 if (!EnsureProviderIsOpen()) { return false; }
143 william 231 int bytesWritten;
144     byte[] bitData = BitConverter.GetBytes(value);
145     provider.WriteProcessMemory(address, bitData, out bytesWritten);
146     sbyte check = 0;
147     ReadMemory(address, out check);
148     if (check == value) return true;
149     return false;
150     }
151     #endregion
152 william 249 #region public virtual bool PatchMemory(int address, ushort value)
153     public virtual bool PatchMemory(int address, ushort value)
154 william 231 {
155 william 245 if (!EnsureProviderIsOpen()) { return false; }
156 william 231 int bytesWritten;
157     byte[] bitData = BitConverter.GetBytes(value);
158     provider.WriteProcessMemory(address, bitData, out bytesWritten);
159 william 245
160 william 231 ushort check = 0;
161     ReadMemory(address, out check);
162     if (check == value) return true;
163     return false;
164     }
165     #endregion
166 william 249 #region public virtual bool PatchMemory(int address, short value)
167     public virtual bool PatchMemory(int address, short value)
168 william 231 {
169 william 245 if (!EnsureProviderIsOpen()) { return false; }
170 william 231 int bytesWritten;
171     byte[] bitData = BitConverter.GetBytes(value);
172     provider.WriteProcessMemory(address, bitData, out bytesWritten);
173 william 245
174 william 231 short check = 0;
175     ReadMemory(address, out check);
176     if (check == value) return true;
177     return false;
178     }
179     #endregion
180 william 249 #region public virtual bool PatchMemory(int address, uint value)
181     public virtual bool PatchMemory(int address, uint value)
182 william 231 {
183 william 245 if (!EnsureProviderIsOpen()) { return false; }
184 william 231 int bytesWritten;
185     byte[] bitData = BitConverter.GetBytes(value);
186     provider.WriteProcessMemory(address, bitData, out bytesWritten);
187 william 245
188 william 231 uint check = 0;
189     ReadMemory(address, out check);
190     if (check == value) return true;
191     return false;
192     }
193     #endregion
194 william 249 #region public virtual bool PatchMemory(int address, int value)
195     public virtual bool PatchMemory(int address, int value)
196 william 231 {
197 william 245 if (!EnsureProviderIsOpen()) { return false; }
198 william 231 int bytesWritten;
199     byte[] bitData = BitConverter.GetBytes(value);
200     provider.WriteProcessMemory(address, bitData, out bytesWritten);
201 william 245
202 william 231 int check = 0;
203     ReadMemory(address, out check);
204     if (check == value) return true;
205     return false;
206     }
207     #endregion
208 william 249 #region public virtual bool PatchMemory(int address, ulong value)
209     public virtual bool PatchMemory(int address, ulong value)
210 william 231 {
211 william 245 if (!EnsureProviderIsOpen()) { return false; }
212 william 231 int bytesWritten;
213     byte[] bitData = BitConverter.GetBytes(value);
214     provider.WriteProcessMemory(address, bitData, out bytesWritten);
215 william 245
216 william 231 ulong check = 0;
217     ReadMemory(address, out check);
218     if (check == value) return true;
219     return false;
220     }
221     #endregion
222 william 249 #region public virtual bool PatchMemory(int address, long value)
223     public virtual bool PatchMemory(int address, long value)
224 william 231 {
225 william 245 if (!EnsureProviderIsOpen()) { return false; }
226 william 231 int bytesWritten;
227     byte[] bitData = BitConverter.GetBytes(value);
228     provider.WriteProcessMemory(address, bitData, out bytesWritten);
229 william 245
230 william 231 long check = 0;
231     ReadMemory(address, out check);
232     if (check == value) return true;
233     return false;
234     }
235     #endregion
236     #endregion
237    
238     #region IReadMemory members
239 william 249 #region public virtual bool ReadMemory(int address, out byte value)
240     public virtual bool ReadMemory(int address, out byte value)
241 william 231 {
242 william 245 value = 0;
243     if (!EnsureProviderIsOpen()) { return false; }
244 william 231 try
245 william 245 { int bytesReadSize;
246 william 231 byte[] bitData;
247     provider.ReadProcessMemory(address, sizeof(byte), out bytesReadSize, out bitData);
248     value = bitData[0];
249 william 245
250 william 231 return true;
251     }
252     catch
253     {
254     value = 0x00;
255     return false;
256     }
257     }
258     #endregion
259 william 249 #region public virtual bool ReadMemory(int address, out sbyte value)
260     public virtual bool ReadMemory(int address, out sbyte value)
261 william 231 {
262 william 245 value = 0;
263     if (!EnsureProviderIsOpen()) { return false; }
264 william 231 try
265 william 245 {
266 william 231 int bytesReadSize;
267     byte[] bitData;
268     provider.ReadProcessMemory(address, sizeof(byte), out bytesReadSize, out bitData);
269     value = Convert.ToSByte(bitData[0]);
270 william 245
271 william 231 return true;
272     }
273     catch
274     {
275     value = 0x00;
276     return false;
277     }
278     }
279     #endregion
280 william 249 #region public virtual bool ReadMemory(int address, out ushort value)
281     public virtual bool ReadMemory(int address, out ushort value)
282 william 231 {
283 william 245 value = 0;
284     if (!EnsureProviderIsOpen()) { return false; }
285 william 231 try
286     {
287     int bytesReadSize;
288     byte[] bitData;
289     provider.ReadProcessMemory(address, sizeof(byte), out bytesReadSize, out bitData);
290     value = BitConverter.ToUInt16(bitData, 0);
291 william 245
292 william 231 return true;
293     }
294     catch
295     {
296     value = 0x00;
297     return false;
298     }
299     }
300     #endregion
301 william 249 #region public virtual bool ReadMemory(int address, out short value)
302     public virtual bool ReadMemory(int address, out short value)
303 william 231 {
304 william 245 value = 0;
305     if (!EnsureProviderIsOpen()) { return false; }
306 william 231 try
307     {
308     int bytesReadSize;
309     byte[] bitData;
310     provider.ReadProcessMemory(address, sizeof(byte), out bytesReadSize, out bitData);
311     value = BitConverter.ToInt16(bitData, 0);
312 william 245
313 william 231 return true;
314     }
315     catch
316     {
317     value = 0x00;
318     return false;
319     }
320     }
321     #endregion
322 william 249 #region public virtual bool ReadMemory(int address, out uint value)
323     public virtual bool ReadMemory(int address, out uint value)
324 william 231 {
325 william 245 value = 0;
326     if (!EnsureProviderIsOpen()) { return false; }
327 william 231 try
328     {
329     int bytesReadSize;
330     byte[] bitData;
331     provider.ReadProcessMemory(address, sizeof(byte), out bytesReadSize, out bitData);
332     value = BitConverter.ToUInt32(bitData, 0);
333 william 245
334 william 231 return true;
335     }
336     catch
337     {
338     value = 0x00;
339     return false;
340     }
341     }
342     #endregion
343 william 249 #region public virtual bool ReadMemory(int address, out int value)
344     public virtual bool ReadMemory(int address, out int value)
345 william 231 {
346 william 245 value = 0;
347     if (!EnsureProviderIsOpen()) { return false; }
348 william 231 try
349     {
350     int bytesReadSize;
351     byte[] bitData;
352     provider.ReadProcessMemory(address, sizeof(byte), out bytesReadSize, out bitData);
353     value = BitConverter.ToInt32(bitData, 0);
354 william 245
355 william 231 return true;
356     }
357     catch
358     {
359     value = 0x00;
360     return false;
361     }
362     }
363     #endregion
364 william 249 #region public virtual bool ReadMemory(int address, out ulong value)
365     public virtual bool ReadMemory(int address, out ulong value)
366 william 231 {
367 william 245 value = 0;
368     if (!EnsureProviderIsOpen()) { return false; }
369 william 231 try
370     {
371     int bytesReadSize;
372     byte[] bitData;
373     provider.ReadProcessMemory(address, sizeof(byte), out bytesReadSize, out bitData);
374     value = BitConverter.ToUInt64(bitData, 0);
375 william 245
376 william 231 return true;
377     }
378     catch
379     {
380     value = 0x00;
381     return false;
382     }
383     }
384     #endregion
385 william 249 #region public virtual bool ReadMemory(int address, out long value)
386     public virtual bool ReadMemory(int address, out long value)
387 william 231 {
388 william 245 value = 0;
389     if (!EnsureProviderIsOpen()) { return false; }
390 william 231 try
391     {
392     int bytesReadSize;
393     byte[] bitData;
394     provider.ReadProcessMemory(address, sizeof(byte), out bytesReadSize, out bitData);
395     value = BitConverter.ToInt64(bitData, 0);
396 william 245
397 william 231 return true;
398     }
399     catch
400     {
401     value = 0x00;
402     return false;
403     }
404     }
405     #endregion
406     #endregion
407    
408     #region IMemoryReader members
409 william 249 #region public virtual bool ReadFirstNonZeroByte(int MemoryAddress, uint bytesToRead, out int address)
410     public virtual bool ReadFirstNonZeroByte(int MemoryAddress, uint bytesToRead, out int address)
411 william 231 {
412 william 245 address = 0;
413     if (!EnsureProviderIsOpen()) { return false; }
414     try { provider.ReadFirstNonZeroByte(MemoryAddress, bytesToRead, out address); return true; }
415     catch { address = 0x00; return false; }
416 william 231 }
417     #endregion
418 william 249 #region public virtual void ReadProcessMemory(int MemoryAddress, int bytesToRead, out int bytesRead, out byte[] data)
419     public virtual void ReadProcessMemory(int MemoryAddress, uint bytesToRead, out int bytesRead, out byte[] data)
420 william 231 {
421 william 245 bytesRead = 0x00;
422     data = new byte[] { };
423     if (!EnsureProviderIsOpen()) { return; }
424     try { provider.ReadProcessMemory(MemoryAddress, bytesToRead, out bytesRead, out data); }
425     catch { bytesRead = 0x00; data = new byte[] { }; }
426 william 231 }
427     #endregion
428     #region IMemoryWriter members
429 william 249 #region public virtual void WriteProcessMemory(int MemoryAddress, byte byteToWrite, out int bytesWritten)
430     public virtual void WriteProcessMemory(int MemoryAddress, byte byteToWrite, out int bytesWritten)
431 william 231 {
432 william 245 bytesWritten = 0;
433     if (!EnsureProviderIsOpen()) { return; }
434     try { provider.WriteProcessMemory(MemoryAddress, new byte[] { byteToWrite }, out bytesWritten); }
435     catch { bytesWritten = 0x00; }
436 william 231 }
437     #endregion
438 william 249 #region public virtual void WriteProcessMemory(int MemoryAddress, byte[] bytesToWrite, out int bytesWritten)
439     public virtual void WriteProcessMemory(int MemoryAddress, byte[] bytesToWrite, out int bytesWritten)
440 william 231 {
441 william 245 bytesWritten = 0;
442     if (!EnsureProviderIsOpen()) { return; }
443     try { provider.WriteProcessMemory(MemoryAddress, bytesToWrite, out bytesWritten); }
444     catch { bytesWritten = 0x00; }
445 william 231 }
446     #endregion
447     #endregion
448     #endregion
449     #region IFileWriter members
450 william 249 #region public virtual bool WriteProcessMemoryToFile(string filename, int MemoryAddress, uint bytesToRead, out int bytesRead)
451     public virtual bool WriteProcessMemoryToFile(string filename, int MemoryAddress, uint bytesToRead, out int bytesRead)
452 william 231 {
453 william 245 bytesRead = 0;
454     if (!EnsureProviderIsOpen()) { return false; }
455     try { provider.WriteProcessMemoryToFile(filename, MemoryAddress, bytesToRead, out bytesRead); return true; }
456 william 231 catch
457 william 245 { bytesRead = 0x00; return false; }
458 william 231 }
459     #endregion
460     #endregion
461     }
462     }

  ViewVC Help
Powered by ViewVC 1.1.22