/[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 255 - (show annotations) (download)
Sun Jun 3 16:45:47 2012 UTC (8 years, 5 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 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 #region public abstract class BaseMemoryProvider
13 public abstract class BaseMemoryProvider :
14 IPatchMemory,
15 IReadMemory,
16 IAcceptsProcess<Process>,
17 IAcceptsPlugin<IConfigPlugin>,
18 IMemoryReader,
19 IMemoryWriter,
20 IFileWriter
21 {
22 private ProcessMemoryReader provider;
23 public BaseMemoryProvider() { this.AcceptedPlugin = null; this.AcceptedProcess = null; isClosed = true; isOpen = false; }
24 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
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 {
36 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 logger.Error.WriteLine("Failed to open provider: {0}{1}", System.Environment.NewLine, ex.ToString());
53 isOpen = false;
54 isClosed = true;
55 }
56 }
57 #endregion
58 #region public virtual void CloseProvider()
59 public virtual void CloseProvider()
60 {
61 if (isClosed)
62 {
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 //logger.VerboseDebug.WriteLine("CloseProvider(): System.Environment.StackTrace: {0}{1}", System.Environment.NewLine, System.Environment.StackTrace);
79 if (provider == null) return;
80 provider.CloseHandle();
81 //provider = null; // free any memory associated with the provider
82 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 }
95 #endregion
96 #endregion
97
98 #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 #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 #region IPatchMemory members
127 #region public virtual bool PatchMemory(int address, byte value)
128 public virtual bool PatchMemory(int address, byte value)
129 {
130 if (!EnsureProviderIsOpen()) { return false; }
131 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 #region public virtual bool PatchMemory(int address, sbyte value)
141 public virtual bool PatchMemory(int address, sbyte value)
142 {
143 if (!EnsureProviderIsOpen()) { return false; }
144 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 #region public virtual bool PatchMemory(int address, ushort value)
154 public virtual bool PatchMemory(int address, ushort value)
155 {
156 if (!EnsureProviderIsOpen()) { return false; }
157 int bytesWritten;
158 byte[] bitData = BitConverter.GetBytes(value);
159 provider.WriteProcessMemory(address, bitData, out bytesWritten);
160
161 ushort check = 0;
162 ReadMemory(address, out check);
163 if (check == value) return true;
164 return false;
165 }
166 #endregion
167 #region public virtual bool PatchMemory(int address, short value)
168 public virtual bool PatchMemory(int address, short value)
169 {
170 if (!EnsureProviderIsOpen()) { return false; }
171 int bytesWritten;
172 byte[] bitData = BitConverter.GetBytes(value);
173 provider.WriteProcessMemory(address, bitData, out bytesWritten);
174
175 short check = 0;
176 ReadMemory(address, out check);
177 if (check == value) return true;
178 return false;
179 }
180 #endregion
181 #region public virtual bool PatchMemory(int address, uint value)
182 public virtual bool PatchMemory(int address, uint value)
183 {
184 if (!EnsureProviderIsOpen()) { return false; }
185 int bytesWritten;
186 byte[] bitData = BitConverter.GetBytes(value);
187 provider.WriteProcessMemory(address, bitData, out bytesWritten);
188
189 uint check = 0;
190 ReadMemory(address, out check);
191 if (check == value) return true;
192 return false;
193 }
194 #endregion
195 #region public virtual bool PatchMemory(int address, int value)
196 public virtual bool PatchMemory(int address, int value)
197 {
198 if (!EnsureProviderIsOpen()) { return false; }
199 int bytesWritten;
200 byte[] bitData = BitConverter.GetBytes(value);
201 provider.WriteProcessMemory(address, bitData, out bytesWritten);
202
203 int check = 0;
204 ReadMemory(address, out check);
205 if (check == value) return true;
206 return false;
207 }
208 #endregion
209 #region public virtual bool PatchMemory(int address, ulong value)
210 public virtual bool PatchMemory(int address, ulong value)
211 {
212 if (!EnsureProviderIsOpen()) { return false; }
213 int bytesWritten;
214 byte[] bitData = BitConverter.GetBytes(value);
215 provider.WriteProcessMemory(address, bitData, out bytesWritten);
216
217 ulong check = 0;
218 ReadMemory(address, out check);
219 if (check == value) return true;
220 return false;
221 }
222 #endregion
223 #region public virtual bool PatchMemory(int address, long value)
224 public virtual bool PatchMemory(int address, long value)
225 {
226 if (!EnsureProviderIsOpen()) { return false; }
227 int bytesWritten;
228 byte[] bitData = BitConverter.GetBytes(value);
229 provider.WriteProcessMemory(address, bitData, out bytesWritten);
230
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 #region public virtual bool ReadMemory(int address, out byte value)
241 public virtual bool ReadMemory(int address, out byte value)
242 {
243 value = 0;
244 if (!EnsureProviderIsOpen()) { return false; }
245 try
246 {
247 int bytesReadSize;
248 byte[] bitData;
249 provider.ReadProcessMemory(address, sizeof(byte), out bytesReadSize, out bitData);
250 value = bitData[0];
251
252 return true;
253 }
254 catch
255 {
256 value = 0x00;
257 return false;
258 }
259 }
260 #endregion
261 #region public virtual bool ReadMemory(int address, out sbyte value)
262 public virtual bool ReadMemory(int address, out sbyte value)
263 {
264 value = 0;
265 if (!EnsureProviderIsOpen()) { return false; }
266 try
267 {
268 int bytesReadSize;
269 byte[] bitData;
270 provider.ReadProcessMemory(address, sizeof(byte), out bytesReadSize, out bitData);
271 value = Convert.ToSByte(bitData[0]);
272
273 return true;
274 }
275 catch
276 {
277 value = 0x00;
278 return false;
279 }
280 }
281 #endregion
282 #region public virtual bool ReadMemory(int address, out ushort value)
283 public virtual bool ReadMemory(int address, out ushort value)
284 {
285 value = 0;
286 if (!EnsureProviderIsOpen()) { return false; }
287 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
294 return true;
295 }
296 catch
297 {
298 value = 0x00;
299 return false;
300 }
301 }
302 #endregion
303 #region public virtual bool ReadMemory(int address, out short value)
304 public virtual bool ReadMemory(int address, out short value)
305 {
306 value = 0;
307 if (!EnsureProviderIsOpen()) { return false; }
308 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
315 return true;
316 }
317 catch
318 {
319 value = 0x00;
320 return false;
321 }
322 }
323 #endregion
324 #region public virtual bool ReadMemory(int address, out uint value)
325 public virtual bool ReadMemory(int address, out uint value)
326 {
327 value = 0;
328 if (!EnsureProviderIsOpen()) { return false; }
329 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
336 return true;
337 }
338 catch
339 {
340 value = 0x00;
341 return false;
342 }
343 }
344 #endregion
345 #region public virtual bool ReadMemory(int address, out int value)
346 public virtual bool ReadMemory(int address, out int value)
347 {
348 value = 0;
349 if (!EnsureProviderIsOpen()) { return false; }
350 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
357 return true;
358 }
359 catch
360 {
361 value = 0x00;
362 return false;
363 }
364 }
365 #endregion
366 #region public virtual bool ReadMemory(int address, out ulong value)
367 public virtual bool ReadMemory(int address, out ulong value)
368 {
369 value = 0;
370 if (!EnsureProviderIsOpen()) { return false; }
371 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
378 return true;
379 }
380 catch
381 {
382 value = 0x00;
383 return false;
384 }
385 }
386 #endregion
387 #region public virtual bool ReadMemory(int address, out long value)
388 public virtual bool ReadMemory(int address, out long value)
389 {
390 value = 0;
391 if (!EnsureProviderIsOpen()) { return false; }
392 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
399 return true;
400 }
401 catch
402 {
403 value = 0x00;
404 return false;
405 }
406 }
407 #endregion
408 #endregion
409
410 #region IMemoryReader members
411 #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 {
414 address = 0;
415 if (!EnsureProviderIsOpen()) { return false; }
416 try { provider.ReadFirstNonZeroByte(MemoryAddress, bytesToRead, out address); return true; }
417 catch { address = 0x00; return false; }
418 }
419 #endregion
420 #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 {
423 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 }
429 #endregion
430 #region IMemoryWriter members
431 #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 {
434 bytesWritten = 0;
435 if (!EnsureProviderIsOpen()) { return; }
436 try { provider.WriteProcessMemory(MemoryAddress, new byte[] { byteToWrite }, out bytesWritten); }
437 catch { bytesWritten = 0x00; }
438 }
439 #endregion
440 #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 {
443 bytesWritten = 0;
444 if (!EnsureProviderIsOpen()) { return; }
445 try { provider.WriteProcessMemory(MemoryAddress, bytesToWrite, out bytesWritten); }
446 catch { bytesWritten = 0x00; }
447 }
448 #endregion
449 #endregion
450 #endregion
451 #region IFileWriter members
452 #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 {
455 bytesRead = 0;
456 if (!EnsureProviderIsOpen()) { return false; }
457 try { provider.WriteProcessMemoryToFile(filename, MemoryAddress, bytesToRead, out bytesRead); return true; }
458 catch
459 { bytesRead = 0x00; return false; }
460 }
461 #endregion
462 #endregion
463 }
464 #endregion
465 }

  ViewVC Help
Powered by ViewVC 1.1.22