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 |
IDisposable |
22 |
{ |
23 |
private ProcessMemoryReader provider; |
24 |
public BaseMemoryProvider() { this.AcceptedPlugin = null; this.AcceptedProcess = null; isClosed = true; isOpen = false; } |
25 |
public BaseMemoryProvider(IConfigPlugin config) : this() { this.AcceptedPlugin = config; } |
26 |
public BaseMemoryProvider(IConfigPlugin config, Process process) : this() { this.AcceptedPlugin = config; this.AcceptedProcess = process; } |
27 |
public BaseMemoryProvider(IAcceptsProcessAndConfig pconfig) : this() { this.AcceptedPlugin = pconfig.AcceptedPlugin; this.AcceptedProcess = pconfig.AcceptedProcess; } |
28 |
|
29 |
|
30 |
private bool isOpen { get; set; } |
31 |
private bool isClosed { get; set; } |
32 |
|
33 |
#region Open/Close Provider |
34 |
#region public virtual void OpenProvider() |
35 |
public virtual void OpenProvider() |
36 |
{ |
37 |
if (isOpen) |
38 |
{ |
39 |
logger.Warn.WriteLine("Provider has already been opened."); |
40 |
return; |
41 |
} |
42 |
try |
43 |
{ |
44 |
provider = new ProcessMemoryReader(); |
45 |
provider.ReadProcess = this.AcceptedProcess; |
46 |
if (provider.ReadProcess == null) { logger.Error.WriteLine("{0}.OpenProvider() Could not attach to process: {1}", "", this.GetType().Name, this.AcceptedProcess.ToString()); return; } |
47 |
provider.OpenProcess(); |
48 |
isOpen = true; |
49 |
isClosed = false; |
50 |
} |
51 |
catch (Exception ex) |
52 |
{ |
53 |
logger.Error.WriteLine("Failed to open provider: {0}{1}", System.Environment.NewLine, ex.ToString()); |
54 |
isOpen = false; |
55 |
isClosed = true; |
56 |
} |
57 |
} |
58 |
#endregion |
59 |
#region public virtual void CloseProvider() |
60 |
public virtual void CloseProvider() |
61 |
{ |
62 |
if (isClosed) |
63 |
{ |
64 |
logger.Warn.WriteLine("Provider has already been closed."); |
65 |
return; |
66 |
} |
67 |
if (!isOpen) |
68 |
{ |
69 |
logger.Warn.WriteLine("Provider cannot be closed, it was never opened...attempting to open provider."); |
70 |
OpenProvider(); |
71 |
if (!isOpen) |
72 |
{ |
73 |
logger.Warn.WriteLine("Could not open provider"); |
74 |
return; |
75 |
} |
76 |
} |
77 |
try |
78 |
{ |
79 |
//logger.VerboseDebug.WriteLine("CloseProvider(): System.Environment.StackTrace: {0}{1}", System.Environment.NewLine, System.Environment.StackTrace); |
80 |
if (provider == null) return; |
81 |
provider.CloseHandle(); |
82 |
provider = null; // free any memory associated with the provider |
83 |
isClosed = true; |
84 |
isOpen = false; |
85 |
} |
86 |
catch (Exception ex) |
87 |
{ |
88 |
logger.Error.WriteLine("Failed to close provider: {0}{1}", System.Environment.NewLine, ex.ToString()); |
89 |
isClosed = false; |
90 |
if (isOpen) |
91 |
{ |
92 |
throw new Exception("Provider is failed to close and still open."); |
93 |
} |
94 |
} |
95 |
} |
96 |
#endregion |
97 |
#endregion |
98 |
|
99 |
#region IAcceptsProcess<Process> Members |
100 |
public Process AcceptedProcess { get; set; } |
101 |
#endregion |
102 |
#region IAcceptsPlugin<IConfigPlugin> Members |
103 |
public IConfigPlugin AcceptedPlugin { get; set; } |
104 |
#endregion |
105 |
#region EnsureProviderIsOpen methods : log and/or throw errors |
106 |
private bool EnsureProviderIsOpen() { return EnsureProviderIsOpenOrThrowError(); } |
107 |
private bool EnsureProviderIsOpenOrLogError() { return EnsureProviderIsOpenOrThrowOrLogError(false, true); } |
108 |
private bool EnsureProviderIsOpenOrThrowError() { return EnsureProviderIsOpenOrThrowOrLogError(true, true); } |
109 |
private bool EnsureProviderIsOpenOrThrowOrLogError(bool ThrowError, bool LogError) |
110 |
{ |
111 |
if (!isOpen) |
112 |
{ |
113 |
try { throw new Exception("Memory operation could not be completed because the provider is not open"); } |
114 |
catch (Exception ex) |
115 |
{ |
116 |
if (LogError) |
117 |
logger.Error.WriteLine(ex.ToString()); |
118 |
if (ThrowError) |
119 |
throw ex; |
120 |
return false; |
121 |
} |
122 |
} |
123 |
return true; |
124 |
} |
125 |
#endregion |
126 |
|
127 |
#region IPatchMemory members |
128 |
#region public virtual bool PatchMemory(uint address, byte value) |
129 |
public virtual bool PatchMemory(uint address, byte value) |
130 |
{ |
131 |
if (!EnsureProviderIsOpen()) { return false; } |
132 |
return provider.PatchMemory(address, value); |
133 |
} |
134 |
#endregion |
135 |
#region public virtual bool PatchMemory(uint address, sbyte value) |
136 |
public virtual bool PatchMemory(uint address, sbyte value) |
137 |
{ |
138 |
if (!EnsureProviderIsOpen()) { return false; } |
139 |
return provider.PatchMemory(address, value); |
140 |
} |
141 |
#endregion |
142 |
#region public virtual bool PatchMemory(uint address, ushort value) |
143 |
public virtual bool PatchMemory(uint address, ushort value) |
144 |
{ |
145 |
if (!EnsureProviderIsOpen()) { return false; } |
146 |
return provider.PatchMemory(address, value); |
147 |
} |
148 |
#endregion |
149 |
#region public virtual bool PatchMemory(uint address, short value) |
150 |
public virtual bool PatchMemory(uint address, short value) |
151 |
{ |
152 |
if (!EnsureProviderIsOpen()) { return false; } |
153 |
return provider.PatchMemory(address, value); |
154 |
} |
155 |
#endregion |
156 |
#region public virtual bool PatchMemory(uint address, uint value) |
157 |
public virtual bool PatchMemory(uint address, uint value) |
158 |
{ |
159 |
if (!EnsureProviderIsOpen()) { return false; } |
160 |
return provider.PatchMemory(address, value); |
161 |
} |
162 |
#endregion |
163 |
#region public virtual bool PatchMemory(uint address, int value) |
164 |
public virtual bool PatchMemory(uint address, int value) |
165 |
{ |
166 |
if (!EnsureProviderIsOpen()) { return false; } |
167 |
return provider.PatchMemory(address, value); |
168 |
} |
169 |
#endregion |
170 |
#region public virtual bool PatchMemory(uint address, ulong value) |
171 |
public virtual bool PatchMemory(uint address, ulong value) |
172 |
{ |
173 |
if (!EnsureProviderIsOpen()) { return false; } |
174 |
return provider.PatchMemory(address, value); |
175 |
} |
176 |
#endregion |
177 |
#region public virtual bool PatchMemory(uint address, long value) |
178 |
public virtual bool PatchMemory(uint address, long value) |
179 |
{ |
180 |
if (!EnsureProviderIsOpen()) { return false; } |
181 |
return provider.PatchMemory(address, value); |
182 |
} |
183 |
#endregion |
184 |
#endregion |
185 |
|
186 |
#region IReadMemory members |
187 |
#region public virtual bool ReadMemory(uint address, out byte value) |
188 |
public virtual bool ReadMemory(uint address, out byte value) |
189 |
{ |
190 |
value = 0; |
191 |
if (!EnsureProviderIsOpen()) { return false; } |
192 |
return provider.ReadMemory(address, out value); |
193 |
} |
194 |
#endregion |
195 |
#region public virtual bool ReadMemory(uint address, out sbyte value) |
196 |
public virtual bool ReadMemory(uint address, out sbyte value) |
197 |
{ |
198 |
value = 0; |
199 |
if (!EnsureProviderIsOpen()) { return false; } |
200 |
return provider.ReadMemory(address, out value); |
201 |
} |
202 |
#endregion |
203 |
#region public virtual bool ReadMemory(uint address, out ushort value) |
204 |
public virtual bool ReadMemory(uint address, out ushort value) |
205 |
{ |
206 |
value = 0; |
207 |
if (!EnsureProviderIsOpen()) { return false; } |
208 |
return provider.ReadMemory(address, out value); |
209 |
} |
210 |
#endregion |
211 |
#region public virtual bool ReadMemory(uint address, out short value) |
212 |
public virtual bool ReadMemory(uint address, out short value) |
213 |
{ |
214 |
value = 0; |
215 |
if (!EnsureProviderIsOpen()) { return false; } |
216 |
return provider.ReadMemory(address, out value); |
217 |
} |
218 |
#endregion |
219 |
#region public virtual bool ReadMemory(uint address, out uint value) |
220 |
public virtual bool ReadMemory(uint address, out uint value) |
221 |
{ |
222 |
value = 0; |
223 |
if (!EnsureProviderIsOpen()) { return false; } |
224 |
return provider.ReadMemory(address, out value); |
225 |
} |
226 |
#endregion |
227 |
#region public virtual bool ReadMemory(uint address, out int value) |
228 |
public virtual bool ReadMemory(uint address, out int value) |
229 |
{ |
230 |
value = 0; |
231 |
if (!EnsureProviderIsOpen()) { return false; } |
232 |
return provider.ReadMemory(address, out value); |
233 |
} |
234 |
#endregion |
235 |
#region public virtual bool ReadMemory(uint address, out ulong value) |
236 |
public virtual bool ReadMemory(uint address, out ulong value) |
237 |
{ |
238 |
value = 0; |
239 |
if (!EnsureProviderIsOpen()) { return false; } |
240 |
return provider.ReadMemory(address, out value); |
241 |
} |
242 |
#endregion |
243 |
#region public virtual bool ReadMemory(uint address, out long value) |
244 |
public virtual bool ReadMemory(uint address, out long value) |
245 |
{ |
246 |
value = 0; |
247 |
if (!EnsureProviderIsOpen()) { return false; } |
248 |
return provider.ReadMemory(address, out value); |
249 |
} |
250 |
#endregion |
251 |
#endregion |
252 |
|
253 |
#region IMemoryReader members |
254 |
#region public virtual bool ReadFirstNonZeroByte(int MemoryAddress, uint bytesToRead, out int address) |
255 |
public virtual bool ReadFirstNonZeroByte(int MemoryAddress, uint bytesToRead, out int address) |
256 |
{ |
257 |
address = 0; |
258 |
if (!EnsureProviderIsOpen()) { return false; } |
259 |
try { provider.ReadFirstNonZeroByte(MemoryAddress, bytesToRead, out address); return true; } |
260 |
catch { address = 0x00; return false; } |
261 |
} |
262 |
#endregion |
263 |
#region public virtual void ReadProcessMemoryAtOnce(int MemoryAddress, uint bytesToRead, out int bytesRead, out byte[] data) |
264 |
public virtual void ReadProcessMemoryAtOnce(uint MemoryAddress, uint bytesToRead, out int bytesRead, out byte[] data) |
265 |
{ |
266 |
bytesRead = 0x00; |
267 |
data = new byte[bytesToRead]; |
268 |
if (!EnsureProviderIsOpen()) { return; } |
269 |
try { provider.ReadProcessMemoryAtOnce(MemoryAddress, bytesToRead, out bytesRead, out data); } |
270 |
catch { bytesRead = 0x00; data = new byte[] { }; } |
271 |
} |
272 |
#endregion |
273 |
#region public virtual void ReadProcessMemory(int MemoryAddress, int bytesToRead, out int bytesRead, out byte[] data) |
274 |
public virtual void ReadProcessMemory(uint MemoryAddress, uint bytesToRead, out int bytesRead, out byte[] data) |
275 |
{ |
276 |
bytesRead = 0x00; |
277 |
data = new byte[bytesToRead]; |
278 |
if (!EnsureProviderIsOpen()) { return; } |
279 |
try { provider.ReadProcessMemory(MemoryAddress, bytesToRead, out bytesRead, out data); } |
280 |
catch { bytesRead = 0x00; data = new byte[] { }; } |
281 |
} |
282 |
public virtual void ReadProcessMemory(int MemoryAddress, uint bytesToRead, out int bytesRead, out byte[] data) |
283 |
{ |
284 |
bytesRead = 0x00; |
285 |
data = new byte[bytesToRead]; |
286 |
if (!EnsureProviderIsOpen()) { return; } |
287 |
try { provider.ReadProcessMemory(MemoryAddress, bytesToRead, out bytesRead, out data); } |
288 |
catch { bytesRead = 0x00; data = new byte[] { }; } |
289 |
} |
290 |
#endregion |
291 |
#region IMemoryWriter members |
292 |
//#region public virtual void WriteProcessMemory(int MemoryAddress, byte byteToWrite, out int bytesWritten) |
293 |
//public virtual void WriteProcessMemory(int MemoryAddress, byte byteToWrite, out int bytesWritten) |
294 |
//{ |
295 |
// bytesWritten = 0; |
296 |
// if (!EnsureProviderIsOpen()) { return; } |
297 |
// try { provider.WriteProcessMemory(MemoryAddress, new byte[] { byteToWrite }, out bytesWritten); } |
298 |
// catch { bytesWritten = 0x00; } |
299 |
//} |
300 |
//#endregion |
301 |
#region public virtual void WriteProcessMemory(int MemoryAddress, byte[] bytesToWrite, out int bytesWritten) |
302 |
public virtual void WriteProcessMemory(int MemoryAddress, byte[] bytesToWrite, out int bytesWritten) |
303 |
{ |
304 |
bytesWritten = 0; |
305 |
if (!EnsureProviderIsOpen()) { return; } |
306 |
try { provider.WriteProcessMemory(MemoryAddress, bytesToWrite, out bytesWritten); } |
307 |
catch { bytesWritten = 0x00; } |
308 |
} |
309 |
public virtual void WriteProcessMemory(uint MemoryAddress, byte[] bytesToWrite, out uint bytesWritten) |
310 |
{ |
311 |
bytesWritten = 0; |
312 |
if (!EnsureProviderIsOpen()) { return; } |
313 |
try { provider.WriteProcessMemory(MemoryAddress, bytesToWrite, out bytesWritten); } |
314 |
catch { bytesWritten = 0x00; } |
315 |
} |
316 |
#endregion |
317 |
#endregion |
318 |
#endregion |
319 |
#region IFileWriter members |
320 |
#region public virtual bool WriteProcessMemoryToFile(string filename, int MemoryAddress, uint bytesToRead, out int bytesRead) |
321 |
public virtual bool WriteProcessMemoryToFile(string filename, int MemoryAddress, uint bytesToRead, out int bytesRead) |
322 |
{ |
323 |
bytesRead = 0; |
324 |
if (!EnsureProviderIsOpen()) { return false; } |
325 |
try { provider.WriteProcessMemoryToFile(filename, MemoryAddress, bytesToRead, out bytesRead); return true; } |
326 |
catch |
327 |
{ bytesRead = 0x00; return false; } |
328 |
} |
329 |
#endregion |
330 |
#endregion |
331 |
|
332 |
#region IDisposable Support |
333 |
// Track whether Dispose has been called. |
334 |
private bool disposed = false; |
335 |
// Implement IDisposable. |
336 |
// Do not make this method virtual. |
337 |
// A derived class should not be able to override this method. |
338 |
public void Dispose() |
339 |
{ |
340 |
Dispose(true); |
341 |
// This object will be cleaned up by the Dispose method. |
342 |
// Therefore, you should call GC.SupressFinalize to |
343 |
// take this object off the finalization queue |
344 |
// and prevent finalization code for this object |
345 |
// from executing a second time. |
346 |
GC.SuppressFinalize(this); |
347 |
} |
348 |
// Dispose(bool disposing) executes in two distinct scenarios. |
349 |
// If disposing equals true, the method has been called directly |
350 |
// or indirectly by a user's code. Managed and unmanaged resources |
351 |
// can be disposed. |
352 |
// If disposing equals false, the method has been called by the |
353 |
// runtime from inside the finalizer and you should not reference |
354 |
// other objects. Only unmanaged resources can be disposed. |
355 |
protected virtual void Dispose(bool disposing) |
356 |
{ |
357 |
// Check to see if Dispose has already been called. |
358 |
if (!this.disposed) |
359 |
{ |
360 |
// If disposing equals true, dispose all managed |
361 |
// and unmanaged resources. |
362 |
if (disposing) |
363 |
{ |
364 |
// Dispose managed resources. |
365 |
//component.Dispose(); |
366 |
} |
367 |
|
368 |
// Call the appropriate methods to clean up |
369 |
// unmanaged resources here. |
370 |
// If disposing is false, |
371 |
// only the following code is executed. |
372 |
if (!isClosed) |
373 |
CloseProvider(); |
374 |
// Note disposing has been done. |
375 |
disposed = true; |
376 |
|
377 |
} |
378 |
} |
379 |
// Use C# destructor syntax for finalization code. |
380 |
// This destructor will run only if the Dispose method |
381 |
// does not get called. |
382 |
// It gives your base class the opportunity to finalize. |
383 |
// Do not provide destructors in types derived from this class. |
384 |
~BaseMemoryProvider() |
385 |
{ |
386 |
// Do not re-create Dispose clean-up code here. |
387 |
// Calling Dispose(false) is optimal in terms of |
388 |
// readability and maintainability. |
389 |
Dispose(false); |
390 |
} |
391 |
#endregion |
392 |
} |
393 |
#endregion |
394 |
} |