/[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 245 - (show annotations) (download)
Sun Jun 3 14:50:09 2012 UTC (8 years, 5 months ago) by william
File size: 18261 byte(s)
+ add exception logic to ensure provider is open

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
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; isClosed = true; isOpen = false; }
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
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 {
35 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 }
56 #endregion
57 #region public virtual void CloseProvider()
58 public virtual void CloseProvider()
59 {
60 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 logger.VerboseDebug.WriteLine("CloseProvider(): System.Environment.StackTrace: {0}{1}", System.Environment.NewLine, System.Environment.StackTrace);
78 if (provider == null) return;
79 provider.CloseHandle();
80 provider = null; // free any memory associated with the provider
81 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 }
94 #endregion
95 #endregion
96
97 #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 #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 #region IPatchMemory members
126 #region public virtual bool PatchMemory(uint address, byte value)
127 public virtual bool PatchMemory(uint address, byte value)
128 {
129 if (!EnsureProviderIsOpen()) { return false; }
130 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 #region public virtual bool PatchMemory(uint address, sbyte value)
140 public virtual bool PatchMemory(uint address, sbyte value)
141 {
142 if (!EnsureProviderIsOpen()) { return false; }
143 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 #region public virtual bool PatchMemory(uint address, ushort value)
153 public virtual bool PatchMemory(uint address, ushort value)
154 {
155 if (!EnsureProviderIsOpen()) { return false; }
156 int bytesWritten;
157 byte[] bitData = BitConverter.GetBytes(value);
158 provider.WriteProcessMemory(address, bitData, out bytesWritten);
159
160 ushort check = 0;
161 ReadMemory(address, out check);
162 if (check == value) return true;
163 return false;
164 }
165 #endregion
166 #region public virtual bool PatchMemory(uint address, short value)
167 public virtual bool PatchMemory(uint address, short value)
168 {
169 if (!EnsureProviderIsOpen()) { return false; }
170 int bytesWritten;
171 byte[] bitData = BitConverter.GetBytes(value);
172 provider.WriteProcessMemory(address, bitData, out bytesWritten);
173
174 short check = 0;
175 ReadMemory(address, out check);
176 if (check == value) return true;
177 return false;
178 }
179 #endregion
180 #region public virtual bool PatchMemory(uint address, uint value)
181 public virtual bool PatchMemory(uint address, uint value)
182 {
183 if (!EnsureProviderIsOpen()) { return false; }
184 int bytesWritten;
185 byte[] bitData = BitConverter.GetBytes(value);
186 provider.WriteProcessMemory(address, bitData, out bytesWritten);
187
188 uint check = 0;
189 ReadMemory(address, out check);
190 if (check == value) return true;
191 return false;
192 }
193 #endregion
194 #region public virtual bool PatchMemory(uint address, int value)
195 public virtual bool PatchMemory(uint address, int value)
196 {
197 if (!EnsureProviderIsOpen()) { return false; }
198 int bytesWritten;
199 byte[] bitData = BitConverter.GetBytes(value);
200 provider.WriteProcessMemory(address, bitData, out bytesWritten);
201
202 int check = 0;
203 ReadMemory(address, out check);
204 if (check == value) return true;
205 return false;
206 }
207 #endregion
208 #region public virtual bool PatchMemory(uint address, ulong value)
209 public virtual bool PatchMemory(uint address, ulong value)
210 {
211 if (!EnsureProviderIsOpen()) { return false; }
212 int bytesWritten;
213 byte[] bitData = BitConverter.GetBytes(value);
214 provider.WriteProcessMemory(address, bitData, out bytesWritten);
215
216 ulong check = 0;
217 ReadMemory(address, out check);
218 if (check == value) return true;
219 return false;
220 }
221 #endregion
222 #region public virtual bool PatchMemory(uint address, long value)
223 public virtual bool PatchMemory(uint address, long value)
224 {
225 if (!EnsureProviderIsOpen()) { return false; }
226 int bytesWritten;
227 byte[] bitData = BitConverter.GetBytes(value);
228 provider.WriteProcessMemory(address, bitData, out bytesWritten);
229
230 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 #region public virtual bool ReadMemory(uint address, out byte value)
240 public virtual bool ReadMemory(uint address, out byte value)
241 {
242 value = 0;
243 if (!EnsureProviderIsOpen()) { return false; }
244 try
245 { int bytesReadSize;
246 byte[] bitData;
247 provider.ReadProcessMemory(address, sizeof(byte), out bytesReadSize, out bitData);
248 value = bitData[0];
249
250 return true;
251 }
252 catch
253 {
254 value = 0x00;
255 return false;
256 }
257 }
258 #endregion
259 #region public virtual bool ReadMemory(uint address, out sbyte value)
260 public virtual bool ReadMemory(uint address, out sbyte value)
261 {
262 value = 0;
263 if (!EnsureProviderIsOpen()) { return false; }
264 try
265 {
266 int bytesReadSize;
267 byte[] bitData;
268 provider.ReadProcessMemory(address, sizeof(byte), out bytesReadSize, out bitData);
269 value = Convert.ToSByte(bitData[0]);
270
271 return true;
272 }
273 catch
274 {
275 value = 0x00;
276 return false;
277 }
278 }
279 #endregion
280 #region public virtual bool ReadMemory(uint address, out ushort value)
281 public virtual bool ReadMemory(uint address, out ushort value)
282 {
283 value = 0;
284 if (!EnsureProviderIsOpen()) { return false; }
285 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
292 return true;
293 }
294 catch
295 {
296 value = 0x00;
297 return false;
298 }
299 }
300 #endregion
301 #region public virtual bool ReadMemory(uint address, out short value)
302 public virtual bool ReadMemory(uint address, out short value)
303 {
304 value = 0;
305 if (!EnsureProviderIsOpen()) { return false; }
306 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
313 return true;
314 }
315 catch
316 {
317 value = 0x00;
318 return false;
319 }
320 }
321 #endregion
322 #region public virtual bool ReadMemory(uint address, out uint value)
323 public virtual bool ReadMemory(uint address, out uint value)
324 {
325 value = 0;
326 if (!EnsureProviderIsOpen()) { return false; }
327 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
334 return true;
335 }
336 catch
337 {
338 value = 0x00;
339 return false;
340 }
341 }
342 #endregion
343 #region public virtual bool ReadMemory(uint address, out int value)
344 public virtual bool ReadMemory(uint address, out int value)
345 {
346 value = 0;
347 if (!EnsureProviderIsOpen()) { return false; }
348 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
355 return true;
356 }
357 catch
358 {
359 value = 0x00;
360 return false;
361 }
362 }
363 #endregion
364 #region public virtual bool ReadMemory(uint address, out ulong value)
365 public virtual bool ReadMemory(uint address, out ulong value)
366 {
367 value = 0;
368 if (!EnsureProviderIsOpen()) { return false; }
369 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
376 return true;
377 }
378 catch
379 {
380 value = 0x00;
381 return false;
382 }
383 }
384 #endregion
385 #region public virtual bool ReadMemory(uint address, out long value)
386 public virtual bool ReadMemory(uint address, out long value)
387 {
388 value = 0;
389 if (!EnsureProviderIsOpen()) { return false; }
390 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
397 return true;
398 }
399 catch
400 {
401 value = 0x00;
402 return false;
403 }
404 }
405 #endregion
406 #endregion
407
408 #region IMemoryReader members
409 #region public virtual bool ReadFirstNonZeroByte(uint MemoryAddress, uint bytesToRead, out uint address)
410 public virtual bool ReadFirstNonZeroByte(uint MemoryAddress, uint bytesToRead, out uint address)
411 {
412 address = 0;
413 if (!EnsureProviderIsOpen()) { return false; }
414 try { provider.ReadFirstNonZeroByte(MemoryAddress, bytesToRead, out address); return true; }
415 catch { address = 0x00; return false; }
416 }
417 #endregion
418 #region public virtual void ReadProcessMemory(uint MemoryAddress, int bytesToRead, out int bytesRead, out byte[] data)
419 public virtual void ReadProcessMemory(uint MemoryAddress, uint bytesToRead, out int bytesRead, out byte[] data)
420 {
421 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 }
427 #endregion
428 #region IMemoryWriter members
429 #region public virtual void WriteProcessMemory(uint MemoryAddress, byte byteToWrite, out int bytesWritten)
430 public virtual void WriteProcessMemory(uint MemoryAddress, byte byteToWrite, out int bytesWritten)
431 {
432 bytesWritten = 0;
433 if (!EnsureProviderIsOpen()) { return; }
434 try { provider.WriteProcessMemory(MemoryAddress, new byte[] { byteToWrite }, out bytesWritten); }
435 catch { bytesWritten = 0x00; }
436 }
437 #endregion
438 #region public virtual void WriteProcessMemory(uint MemoryAddress, byte[] bytesToWrite, out int bytesWritten)
439 public virtual void WriteProcessMemory(uint MemoryAddress, byte[] bytesToWrite, out int bytesWritten)
440 {
441 bytesWritten = 0;
442 if (!EnsureProviderIsOpen()) { return; }
443 try { provider.WriteProcessMemory(MemoryAddress, bytesToWrite, out bytesWritten); }
444 catch { bytesWritten = 0x00; }
445 }
446 #endregion
447 #endregion
448 #endregion
449 #region IFileWriter members
450 #region public virtual bool WriteProcessMemoryToFile(string filename, uint MemoryAddress, uint bytesToRead, out int bytesRead)
451 public virtual bool WriteProcessMemoryToFile(string filename, uint MemoryAddress, uint bytesToRead, out int bytesRead)
452 {
453 bytesRead = 0;
454 if (!EnsureProviderIsOpen()) { return false; }
455 try { provider.WriteProcessMemoryToFile(filename, MemoryAddress, bytesToRead, out bytesRead); return true; }
456 catch
457 { bytesRead = 0x00; return false; }
458 }
459 #endregion
460 #endregion
461 }
462 }

  ViewVC Help
Powered by ViewVC 1.1.22