/[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 255 - (hide annotations) (download)
Sun Jun 3 16:45:47 2012 UTC (8 years, 7 months ago) by william
File size: 18091 byte(s)
remove code to use MemoryScanner from r204 (I think we fixed the error in the last commit)

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

  ViewVC Help
Powered by ViewVC 1.1.22