/[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 231 - (hide annotations) (download)
Sun Jun 3 12:36:47 2012 UTC (9 years, 5 months ago) by william
File size: 15418 byte(s)
+ overhaul memory scanner

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

  ViewVC Help
Powered by ViewVC 1.1.22