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 pconfig) : this() { this.AcceptedPlugin = pconfig.AcceptedPlugin; this.AcceptedProcess = pconfig.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 |
return provider.PatchMemory(address, value); |
132 |
} |
133 |
#endregion |
134 |
#region public virtual bool PatchMemory(int address, sbyte value) |
135 |
public virtual bool PatchMemory(int address, sbyte value) |
136 |
{ |
137 |
if (!EnsureProviderIsOpen()) { return false; } |
138 |
return provider.PatchMemory(address, value); |
139 |
} |
140 |
#endregion |
141 |
#region public virtual bool PatchMemory(int address, ushort value) |
142 |
public virtual bool PatchMemory(int address, ushort value) |
143 |
{ |
144 |
if (!EnsureProviderIsOpen()) { return false; } |
145 |
return provider.PatchMemory(address, value); |
146 |
} |
147 |
#endregion |
148 |
#region public virtual bool PatchMemory(int address, short value) |
149 |
public virtual bool PatchMemory(int address, short value) |
150 |
{ |
151 |
if (!EnsureProviderIsOpen()) { return false; } |
152 |
return provider.PatchMemory(address, value); |
153 |
} |
154 |
#endregion |
155 |
#region public virtual bool PatchMemory(int address, uint value) |
156 |
public virtual bool PatchMemory(int address, uint value) |
157 |
{ |
158 |
if (!EnsureProviderIsOpen()) { return false; } |
159 |
return provider.PatchMemory(address, value); |
160 |
} |
161 |
#endregion |
162 |
#region public virtual bool PatchMemory(int address, int value) |
163 |
public virtual bool PatchMemory(int address, int value) |
164 |
{ |
165 |
if (!EnsureProviderIsOpen()) { return false; } |
166 |
return provider.PatchMemory(address, value); |
167 |
} |
168 |
#endregion |
169 |
#region public virtual bool PatchMemory(int address, ulong value) |
170 |
public virtual bool PatchMemory(int address, ulong value) |
171 |
{ |
172 |
if (!EnsureProviderIsOpen()) { return false; } |
173 |
return provider.PatchMemory(address, value); |
174 |
} |
175 |
#endregion |
176 |
#region public virtual bool PatchMemory(int address, long value) |
177 |
public virtual bool PatchMemory(int address, long value) |
178 |
{ |
179 |
if (!EnsureProviderIsOpen()) { return false; } |
180 |
return provider.PatchMemory(address, value); |
181 |
} |
182 |
#endregion |
183 |
#endregion |
184 |
|
185 |
#region IReadMemory members |
186 |
#region public virtual bool ReadMemory(int address, out byte value) |
187 |
public virtual bool ReadMemory(int address, out byte value) |
188 |
{ |
189 |
value = 0; |
190 |
if (!EnsureProviderIsOpen()) { return false; } |
191 |
return provider.ReadMemory(address, out value); |
192 |
} |
193 |
#endregion |
194 |
#region public virtual bool ReadMemory(int address, out sbyte value) |
195 |
public virtual bool ReadMemory(int address, out sbyte value) |
196 |
{ |
197 |
value = 0; |
198 |
if (!EnsureProviderIsOpen()) { return false; } |
199 |
return provider.ReadMemory(address, out value); |
200 |
} |
201 |
#endregion |
202 |
#region public virtual bool ReadMemory(int address, out ushort value) |
203 |
public virtual bool ReadMemory(int address, out ushort value) |
204 |
{ |
205 |
value = 0; |
206 |
if (!EnsureProviderIsOpen()) { return false; } |
207 |
return provider.ReadMemory(address, out value); |
208 |
} |
209 |
#endregion |
210 |
#region public virtual bool ReadMemory(int address, out short value) |
211 |
public virtual bool ReadMemory(int address, out short value) |
212 |
{ |
213 |
value = 0; |
214 |
if (!EnsureProviderIsOpen()) { return false; } |
215 |
return provider.ReadMemory(address, out value); |
216 |
} |
217 |
#endregion |
218 |
#region public virtual bool ReadMemory(int address, out uint value) |
219 |
public virtual bool ReadMemory(int address, out uint value) |
220 |
{ |
221 |
value = 0; |
222 |
if (!EnsureProviderIsOpen()) { return false; } |
223 |
return provider.ReadMemory(address, out value); |
224 |
} |
225 |
#endregion |
226 |
#region public virtual bool ReadMemory(int address, out int value) |
227 |
public virtual bool ReadMemory(int address, out int value) |
228 |
{ |
229 |
value = 0; |
230 |
if (!EnsureProviderIsOpen()) { return false; } |
231 |
return provider.ReadMemory(address, out value); |
232 |
} |
233 |
#endregion |
234 |
#region public virtual bool ReadMemory(int address, out ulong value) |
235 |
public virtual bool ReadMemory(int address, out ulong value) |
236 |
{ |
237 |
value = 0; |
238 |
if (!EnsureProviderIsOpen()) { return false; } |
239 |
return provider.ReadMemory(address, out value); |
240 |
} |
241 |
#endregion |
242 |
#region public virtual bool ReadMemory(int address, out long value) |
243 |
public virtual bool ReadMemory(int address, out long value) |
244 |
{ |
245 |
value = 0; |
246 |
if (!EnsureProviderIsOpen()) { return false; } |
247 |
return provider.ReadMemory(address, out value); |
248 |
} |
249 |
#endregion |
250 |
#endregion |
251 |
|
252 |
#region IMemoryReader members |
253 |
#region public virtual bool ReadFirstNonZeroByte(int MemoryAddress, uint bytesToRead, out int address) |
254 |
public virtual bool ReadFirstNonZeroByte(int MemoryAddress, uint bytesToRead, out int address) |
255 |
{ |
256 |
address = 0; |
257 |
if (!EnsureProviderIsOpen()) { return false; } |
258 |
try { provider.ReadFirstNonZeroByte(MemoryAddress, bytesToRead, out address); return true; } |
259 |
catch { address = 0x00; return false; } |
260 |
} |
261 |
#endregion |
262 |
public void ReadProcessMemoryAtOnce(int MemoryAddress, uint bytesToRead, out int bytesRead, out byte[] data) |
263 |
{ |
264 |
bytesRead = 0x00; |
265 |
data = new byte[bytesToRead]; |
266 |
if (!EnsureProviderIsOpen()) { return; } |
267 |
try { provider.ReadProcessMemoryAtOnce(MemoryAddress, bytesToRead, out bytesRead, out data); } |
268 |
catch { bytesRead = 0x00; data = new byte[] { }; } |
269 |
} |
270 |
#region public virtual void ReadProcessMemory(int MemoryAddress, int bytesToRead, out int bytesRead, out byte[] data) |
271 |
public virtual void ReadProcessMemory(int MemoryAddress, uint bytesToRead, out int bytesRead, out byte[] data) |
272 |
{ |
273 |
bytesRead = 0x00; |
274 |
data = new byte[bytesToRead]; |
275 |
if (!EnsureProviderIsOpen()) { return; } |
276 |
try { provider.ReadProcessMemory(MemoryAddress, bytesToRead, out bytesRead, out data); } |
277 |
catch { bytesRead = 0x00; data = new byte[] { }; } |
278 |
} |
279 |
#endregion |
280 |
#region IMemoryWriter members |
281 |
//#region public virtual void WriteProcessMemory(int MemoryAddress, byte byteToWrite, out int bytesWritten) |
282 |
//public virtual void WriteProcessMemory(int MemoryAddress, byte byteToWrite, out int bytesWritten) |
283 |
//{ |
284 |
// bytesWritten = 0; |
285 |
// if (!EnsureProviderIsOpen()) { return; } |
286 |
// try { provider.WriteProcessMemory(MemoryAddress, new byte[] { byteToWrite }, out bytesWritten); } |
287 |
// catch { bytesWritten = 0x00; } |
288 |
//} |
289 |
//#endregion |
290 |
#region public virtual void WriteProcessMemory(int MemoryAddress, byte[] bytesToWrite, out int bytesWritten) |
291 |
public virtual void WriteProcessMemory(int MemoryAddress, byte[] bytesToWrite, out int bytesWritten) |
292 |
{ |
293 |
bytesWritten = 0; |
294 |
if (!EnsureProviderIsOpen()) { return; } |
295 |
try { provider.WriteProcessMemory(MemoryAddress, bytesToWrite, out bytesWritten); } |
296 |
catch { bytesWritten = 0x00; } |
297 |
} |
298 |
#endregion |
299 |
#endregion |
300 |
#endregion |
301 |
#region IFileWriter members |
302 |
#region public virtual bool WriteProcessMemoryToFile(string filename, int MemoryAddress, uint bytesToRead, out int bytesRead) |
303 |
public virtual bool WriteProcessMemoryToFile(string filename, int MemoryAddress, uint bytesToRead, out int bytesRead) |
304 |
{ |
305 |
bytesRead = 0; |
306 |
if (!EnsureProviderIsOpen()) { return false; } |
307 |
try { provider.WriteProcessMemoryToFile(filename, MemoryAddress, bytesToRead, out bytesRead); return true; } |
308 |
catch |
309 |
{ bytesRead = 0x00; return false; } |
310 |
} |
311 |
#endregion |
312 |
#endregion |
313 |
} |
314 |
#endregion |
315 |
} |