/[RomCheater]/trunk/RomCheater/Serialization/SearchResultReader.cs
ViewVC logotype

Contents of /trunk/RomCheater/Serialization/SearchResultReader.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 475 - (show annotations) (download)
Mon Jun 3 17:43:12 2013 UTC (8 years, 4 months ago) by william
File size: 37765 byte(s)

1 #region Logging Defines
2 // include this any class or method that required logging, and comment-out what is not needed
3
4 #region Enabled logging levels
5 #define LOGGING_ENABLE_INFO
6 #define LOGGING_ENABLE_WARN
7 #define LOGGING_ENABLE_DEBUG
8 #define LOGGING_ENABLE_VERBOSEDEBUG
9 #define LOGGING_ENABLE_ERROR
10 #define LOGGING_ENABLE_VERBOSEERROR
11 #define LOGGING_ENABLE_PROFILER
12 #endregion
13 #endregion
14 using System;
15 using System.Collections.Generic;
16 using System.Linq;
17 using System.Text;
18 using RomCheater.Docking.MemorySearch;
19 using RomCheater.Logging;
20 using System.IO;
21 using Utilities.TransparentControls;
22 using Sojaner.MemoryScanner.MemoryProviers;
23 using RomCheater.PluginFramework.Interfaces;
24 using System.Windows.Forms;
25
26 namespace RomCheater.Serialization
27 {
28 public interface ISearchResultReader
29 {
30 ResultType<TValue> GetNextResult<TValue>() where TValue : IConvertible;
31 }
32 public class SearchResultReader : SerializationReader, ISearchResultReader
33 {
34
35 public delegate void update_progress(int progress, string message);
36
37 private Guid _ResultGuid;
38 private Guid ResultGuid { get { return _ResultGuid; } set { _ResultGuid = value; } }
39
40 private long ResultDataOffset = 0;
41 //private long CurrentResultOffset = 0;
42 //public SearchResultReader() : base() { ReadHeader(); }
43 public SearchResultReader(Guid guid) : base(guid) { ResultGuid = guid; ReadHeader(); }
44
45 protected override string TemporaryFolder { get { return SearchResultsConstants.SearchResultsFolder; } }
46 private void ReadHeader()
47 {
48 try
49 {
50 using (FileStream fs = CreateReader())
51 {
52 using (BinaryReader binReader = new BinaryReader(fs))
53 {
54 //int ResultsRead = 0;
55 // SRD (string)
56 string magic = Encoding.UTF8.GetString(binReader.ReadBytes(3));
57 string SRD = "SRD";
58 if (magic != SRD)
59 {
60 throw new InvalidOperationException(string.Format("Encountered unexpected magic: {0} expected: {1}", magic, SRD));
61 }
62 // version (int)
63 int version = binReader.ReadInt32();
64
65 if (version == 1)
66 {
67 // do nothing
68 }
69 else if (version == 2)
70 {
71 int guid_array_length = binReader.ReadInt32();
72 byte[] guid_array = new byte[guid_array_length];
73 binReader.Read(guid_array, 0, guid_array_length);
74 Guid g = new Guid(guid_array);
75 if (g != ResultGuid)
76 {
77 throw new InvalidOperationException(string.Format("Encountered wrong search results guid: read '{1}' excpected '{2}'", g.ToString(), ResultGuid.ToString()));
78 }
79 }
80 else
81 {
82 throw new InvalidOperationException(string.Format("Encountered unexpected version: {0} expected: {1} or {2}", version, 1, 2));
83 }
84 // resultcount
85 int resultcount = binReader.ReadInt32();
86 if (resultcount == 0)
87 {
88 throw new InvalidOperationException(string.Format("Result Count is zero"));
89 }
90 ResultCount = resultcount;
91 ResultDataOffset = binReader.BaseStream.Position;
92 //for (int i = 0; i < ResultCount; i++)
93 //{
94 // try
95 // {
96 // ResultsRead = i;
97 // uint address = 0;
98 // // assume uint for data type
99 // uint value = 0;
100 // address = binReader.ReadUInt32();
101 // value = binReader.ReadUInt32();
102 // //if (i % 100000 == 0)
103 // // logger.VerboseDebug.WriteLine("Result: @0x{0:x8}=0x{1:x8}", address, value);
104 // }
105 // catch (Exception ex)
106 // {
107 // logger.VerboseError.WriteLine("SearchResultReader.ReadHeader():Consistency Check");
108 // //logger.VerboseError.WriteLine(ex.ToString());
109 // logger.VerboseError.WriteLine("Faied entry: {0}", ResultsRead);
110 // break;
111 // }
112 // ResultsRead++; // add 1
113 //}
114 ////throw new NotImplementedException("DEBUG: testing SearchResultReader consistency");
115 //if (ResultCount != ResultsRead)
116 //{
117 // throw new InvalidOperationException(string.Format("ResultCount does not match ResultsRead: 0x{0:x8} != 0x{1:x8}", ResultCount, ResultsRead));
118 //}
119 binReader.Close();
120 }
121 fs.Close();
122 }
123 }
124 catch (System.IO.EndOfStreamException) { }
125 }
126 #region ISearchResultReader members
127 public bool ReadCurrentAddess { get; private set; }
128 public bool ReadCurrentValue { get; private set; }
129
130 [Obsolete("GetNextResult has been replaced by GetResults")]
131 public ResultType<TValue> GetNextResult<TValue>() where TValue : IConvertible
132 {
133 return new ResultType<TValue>();
134 }
135 #endregion
136
137
138 public ResultItem[] GetResultItems(bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress)
139 {
140 var arr = GetResults(unsigned, datatype, update_progress);
141 ResultItem[] items = new ResultItem[arr.Count()];
142 for (int i = 0; i < items.Count(); i++)
143 {
144 var v = arr[i];
145 switch (datatype)
146 {
147 case SearchDataTypes._8bits:
148 if (unsigned) { items[i] = new ResultItem(v.Address, false, Convert.ToByte(v.Value)); }
149 else { items[i] = new ResultItem(v.Address, false, Convert.ToSByte(v.Value)); }
150 break;
151 case SearchDataTypes._16bits:
152 if (unsigned) { items[i] = new ResultItem(v.Address, false, Convert.ToUInt16(v.Value)); }
153 else { items[i] = new ResultItem(v.Address, false, Convert.ToInt16(v.Value)); }
154 break;
155 case SearchDataTypes._32bits:
156 if (unsigned) { items[i] = new ResultItem(v.Address, false, Convert.ToUInt32(v.Value)); }
157 else { items[i] = new ResultItem(v.Address, false, Convert.ToInt32(v.Value)); }
158 break;
159 case SearchDataTypes._64bits:
160 if (unsigned) { items[i] = new ResultItem(v.Address, false, Convert.ToUInt64(v.Value)); }
161 else { items[i] = new ResultItem(v.Address, false, Convert.ToInt64(v.Value)); }
162 break;
163 }
164 arr[i] = null; // free memory used by the data
165 }
166 return items;
167 }
168
169
170 public uint[] GetResultAddresses(bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress)
171 {
172 var arr = GetResults(unsigned, datatype, update_progress);
173 uint[] items = new uint[arr.Count()];
174 for (int i = 0; i < items.Count(); i++)
175 {
176 var v = arr[i];
177 items[i] = v.Address;
178 arr[i] = null; // free memory used by the data
179 }
180 return items;
181 }
182 public ResultType<object>[] GetResults(bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress)
183 {
184 return GetResultsInternal(unsigned, datatype, update_progress);
185 }
186
187 private ResultType<object>[] GetResultsInternal(bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress)
188 {
189 update_progress.Invoke(0, string.Empty);
190 int Last_Whole_Percent_Done = 0;
191
192 ResultType<object>[] results = new ResultType<object>[ResultCount];
193 using (FileStream fs = CreateReader())
194 {
195 using (BinaryReader binReader = new BinaryReader(fs))
196 {
197
198 binReader.BaseStream.Seek(ResultDataOffset, SeekOrigin.Begin); // seek to start of result data
199 for (int i = 0; i < ResultCount; i++)
200 {
201 uint Address = binReader.ReadUInt32();
202 object Value = 0;
203 switch (datatype)
204 {
205 case SearchDataTypes._8bits:
206 if (unsigned) { Value = binReader.ReadByte(); } else { Value = binReader.ReadSByte(); }
207 break;
208 case SearchDataTypes._16bits:
209 if (unsigned) { Value = binReader.ReadUInt16(); } else { Value = binReader.ReadInt16(); }
210 break;
211 case SearchDataTypes._32bits:
212 if (unsigned) { Value = binReader.ReadUInt32(); } else { Value = binReader.ReadInt32(); }
213 break;
214 case SearchDataTypes._64bits:
215 if (unsigned) { Value = binReader.ReadUInt64(); } else { Value = binReader.ReadInt64(); }
216 break;
217 }
218 results[i] = new ResultType<object>(Address, Value);
219
220 double double_percent_done = 100.0 * (double)((double)i / (double)ResultCount);
221 int int_percent_done = (int)double_percent_done;
222 if (int_percent_done != Last_Whole_Percent_Done && i % 100000 == 0)
223 {
224 if (int_percent_done <= 100)
225 {
226 update_progress.Invoke(int_percent_done, string.Format(" -> Loading value for Address: 0x{0:x8}", Address));
227 Last_Whole_Percent_Done = int_percent_done;
228 }
229 }
230 }
231 }
232 }
233 update_progress.Invoke(0, string.Empty);
234 return results;
235 }
236
237 public uint[] GetAddressRangeAtIndex(int index, int count, bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress)
238 {
239 uint[] results = new uint[index + count];
240 for (int i = 0; i < count; i++)
241 {
242 uint result = GetAddressAtIndex(i + index, unsigned, datatype, update_progress);
243 results[i] = result;
244 //double percent = 100.0 * ((double)i / (double)count);
245 //update_progress.Invoke((int)percent, string.Format("-> Reading Address: 0x{1:x8} from index: 0x{1:x8}", results[i].Address, i + index));
246 }
247 return results;
248 }
249
250 public ResultType<object>[] GetResultRangeAtIndex(int index, int count, bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress)
251 {
252 ResultType<object>[] results = new ResultType<object>[index+count];
253 for (int i = 0; i < count; i++)
254 {
255 ResultType<object> result = GetResultAtIndex(i + index, unsigned, datatype, update_progress);
256 results[i] = result;
257 result = null;
258 //double percent = 100.0 * ((double)i / (double)count);
259 //update_progress.Invoke((int)percent, string.Format("-> Reading Address: 0x{1:x8} from index: 0x{1:x8}", results[i].Address, i + index));
260 }
261 return results;
262 }
263 public uint GetAddressAtIndex(int index, bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress)
264 {
265 //update_progress.Invoke(0, string.Empty);
266 int data_size = sizeof(uint); // address size ... should be 4 bytes (could be 8, if we have a 64-bit address)
267 uint result = 0;
268 using (FileStream fs = CreateReader())
269 {
270 using (BinaryReader binReader = new BinaryReader(fs))
271 {
272 binReader.BaseStream.Seek(ResultDataOffset, SeekOrigin.Begin); // seek to start of result data
273 data_size += (int)datatype / 8;
274 long offset = data_size * index;
275 binReader.BaseStream.Seek(offset, SeekOrigin.Current);
276 uint Address = binReader.ReadUInt32();
277 object Value = 0;
278 switch (datatype)
279 {
280 case SearchDataTypes._8bits:
281 if (unsigned) { Value = binReader.ReadByte(); } else { Value = binReader.ReadSByte(); }
282 break;
283 case SearchDataTypes._16bits:
284 if (unsigned) { Value = binReader.ReadUInt16(); } else { Value = binReader.ReadInt16(); }
285 break;
286 case SearchDataTypes._32bits:
287 if (unsigned) { Value = binReader.ReadUInt32(); } else { Value = binReader.ReadInt32(); }
288 break;
289 case SearchDataTypes._64bits:
290 if (unsigned) { Value = binReader.ReadUInt64(); } else { Value = binReader.ReadInt64(); }
291 break;
292 }
293 result = Address;
294 }
295 }
296 return result;
297 }
298 public ResultType<object> GetResultAtIndex(int index, bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress)
299 {
300 //update_progress.Invoke(0, string.Empty);
301 int data_size = sizeof(uint); // address size ... should be 4 bytes (could be 8, if we have a 64-bit address)
302 ResultType<object> result = new ResultType<object>();
303 using (FileStream fs = CreateReader())
304 {
305 using (BinaryReader binReader = new BinaryReader(fs))
306 {
307 binReader.BaseStream.Seek(ResultDataOffset, SeekOrigin.Begin); // seek to start of result data
308 data_size += (int)datatype / 8;
309 long offset = data_size * index;
310 binReader.BaseStream.Seek(offset, SeekOrigin.Current);
311 uint Address = binReader.ReadUInt32();
312 object Value = 0;
313 switch (datatype)
314 {
315 case SearchDataTypes._8bits:
316 if (unsigned) { Value = binReader.ReadByte(); } else { Value = binReader.ReadSByte(); }
317 break;
318 case SearchDataTypes._16bits:
319 if (unsigned) { Value = binReader.ReadUInt16(); } else { Value = binReader.ReadInt16(); }
320 break;
321 case SearchDataTypes._32bits:
322 if (unsigned) { Value = binReader.ReadUInt32(); } else { Value = binReader.ReadInt32(); }
323 break;
324 case SearchDataTypes._64bits:
325 if (unsigned) { Value = binReader.ReadUInt64(); } else { Value = binReader.ReadInt64(); }
326 break;
327 }
328 result = new ResultType<object>(Address, Value);
329 }
330 }
331 return result;
332 }
333
334 public ResultType<object>[] GetSearchAddressValueMatches(IAcceptsProcessAndConfig iapc, SearchType SearchArgs, Action<int, string> update_progress)
335 {
336 int Last_Whole_Percent_Done = 0;
337 bool unsigned = SearchArgs.IsUnsignedDataType;
338 SearchDataTypes sdt = SearchArgs.DataType;
339 var addresses = this.GetAddressRangeAtIndex(0, this.ResultCount, unsigned, sdt, update_progress);
340
341
342 uint STEP_SIZE = (uint)sdt/8;
343
344 List<ResultType<object>> foundAddresses = new List<ResultType<object>>();
345
346 int count = 0;
347 int totalcount = addresses.Length;
348 foreach (var address in addresses)
349 {
350 byte[] data = new byte[0];
351 using (GenericMemoryProvider provider = new GenericMemoryProvider(iapc))
352 {
353 int bytesRead=0;
354 provider.OpenProvider();
355 provider.ReadProcessMemory(address, STEP_SIZE, out bytesRead, out data);
356 provider.CloseProvider();
357 }
358 using (MemoryStream ms = new MemoryStream(data))
359 {
360 using (BinaryReader br = new BinaryReader(ms))
361 {
362 #region comparison
363 switch (sdt)
364 {
365 case SearchDataTypes._8bits:
366 if (unsigned)
367 {
368 #region 8bits - unsigned
369 var Value = br.ReadByte();
370 using (_8bit_unsigned_comparer_ comparer = new _8bit_unsigned_comparer_(SearchArgs, address))
371 {
372 byte value = 0;
373 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
374 {
375 using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
376 {
377 try
378 {
379 gmp.OpenProvider();
380 gmp.ReadMemory(address, out value);
381 gmp.CloseProvider();
382 }
383 catch (Exception ex)
384 {
385 logger.VerboseError.WriteLine(ex.ToString());
386 }
387 }
388 comparer.Value = value;
389 }
390 else
391 {
392 value = Convert.ToByte(SearchArgs.CompareStartValue);
393 comparer.Value = value;
394 }
395 if (comparer.Compare(Convert.ToByte(Value), value))
396 {
397 foundAddresses.Add(new ResultType<object>(comparer.Address, comparer.Value));
398 }
399 }
400 #endregion
401 }
402 else
403 {
404 #region 8bits - signed
405 var Value = br.ReadSByte();
406 using (_8bit_signed_comparer_ comparer = new _8bit_signed_comparer_(SearchArgs, address))
407 {
408 sbyte value = 0;
409 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
410 {
411 using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
412 {
413 try
414 {
415 gmp.OpenProvider();
416 gmp.ReadMemory(address, out value);
417 gmp.CloseProvider();
418 }
419 catch (Exception ex)
420 {
421 logger.VerboseError.WriteLine(ex.ToString());
422 }
423 }
424 comparer.Value = value;
425 }
426 else
427 {
428 value = Convert.ToSByte(SearchArgs.CompareStartValue);
429 comparer.Value = value;
430 }
431 if (comparer.Compare(Convert.ToSByte(Value), value))
432 {
433 foundAddresses.Add(new ResultType<object>(comparer.Address, comparer.Value));
434 }
435 }
436 #endregion
437 }
438 break;
439 case SearchDataTypes._16bits:
440 if (unsigned)
441 {
442 #region 16bits - unsigned
443 var Value = br.ReadUInt16();
444 using (_16bit_unsigned_comparer_ comparer = new _16bit_unsigned_comparer_(SearchArgs, address))
445 {
446 ushort value = 0;
447 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
448 {
449 using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
450 {
451 try
452 {
453 gmp.OpenProvider();
454 gmp.ReadMemory(address, out value);
455 gmp.CloseProvider();
456 }
457 catch (Exception ex)
458 {
459 logger.VerboseError.WriteLine(ex.ToString());
460 }
461 }
462 comparer.Value = value;
463 }
464 else
465 {
466 value = Convert.ToUInt16(SearchArgs.CompareStartValue);
467 comparer.Value = value;
468 }
469 if (comparer.Compare(Convert.ToUInt16(Value), value))
470 {
471 foundAddresses.Add(new ResultType<object>(comparer.Address, comparer.Value));
472 }
473 }
474 #endregion
475 }
476 else
477 {
478 #region 16bits - signed
479 var Value = br.ReadInt16();
480 using (_16bit_signed_comparer_ comparer = new _16bit_signed_comparer_(SearchArgs, address))
481 {
482 short value = 0;
483 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
484 {
485 using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
486 {
487 try
488 {
489 gmp.OpenProvider();
490 gmp.ReadMemory(address, out value);
491 gmp.CloseProvider();
492 }
493 catch (Exception ex)
494 {
495 logger.VerboseError.WriteLine(ex.ToString());
496 }
497 }
498 comparer.Value = value;
499 }
500 else
501 {
502 value = Convert.ToInt16(SearchArgs.CompareStartValue);
503 comparer.Value = value;
504 }
505 if (comparer.Compare(Convert.ToInt16(Value), value))
506 {
507 foundAddresses.Add(new ResultType<object>(comparer.Address, comparer.Value));
508 }
509 }
510 #endregion
511 }
512 break;
513 case SearchDataTypes._32bits:
514 if (unsigned)
515 {
516 #region 32bits - unsigned
517 var Value = br.ReadUInt32();
518 using (_32bit_unsigned_comparer_ comparer = new _32bit_unsigned_comparer_(SearchArgs, address))
519 {
520 uint value = 0;
521 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
522 {
523 using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
524 {
525 try
526 {
527 gmp.OpenProvider();
528 gmp.ReadMemory(address, out value);
529 gmp.CloseProvider();
530 }
531 catch (Exception ex)
532 {
533 logger.VerboseError.WriteLine(ex.ToString());
534 }
535 }
536 comparer.Value = value;
537 }
538 else
539 {
540 value = Convert.ToUInt32(SearchArgs.CompareStartValue);
541 comparer.Value = value;
542 }
543 if (comparer.Compare(Convert.ToUInt32(Value), value))
544 {
545 foundAddresses.Add(new ResultType<object>(comparer.Address, comparer.Value));
546 }
547 }
548 #endregion
549 }
550 else
551 {
552 #region 32bits - signed
553 var Value = br.ReadInt32();
554 using (_32bit_signed_comparer_ comparer = new _32bit_signed_comparer_(SearchArgs, address))
555 {
556 int value = 0;
557 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
558 {
559 using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
560 {
561 try
562 {
563 gmp.OpenProvider();
564 gmp.ReadMemory(address, out value);
565 gmp.CloseProvider();
566 }
567 catch (Exception ex)
568 {
569 logger.VerboseError.WriteLine(ex.ToString());
570 }
571 }
572 comparer.Value = value;
573 }
574 else
575 {
576 value = Convert.ToInt32(SearchArgs.CompareStartValue);
577 comparer.Value = value;
578 }
579 if (comparer.Compare(Convert.ToInt32(Value), value))
580 {
581 foundAddresses.Add(new ResultType<object>(comparer.Address, comparer.Value));
582 }
583 }
584 #endregion
585 }
586 break;
587 case SearchDataTypes._64bits:
588 if (unsigned)
589 {
590 #region 64bits - unsigned
591 var Value = br.ReadUInt64();
592 using (_64bit_unsigned_comparer_ comparer = new _64bit_unsigned_comparer_(SearchArgs, address))
593 {
594 ulong value = 0;
595 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
596 {
597 using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
598 {
599 try
600 {
601 gmp.OpenProvider();
602 gmp.ReadMemory(address, out value);
603 gmp.CloseProvider();
604 }
605 catch (Exception ex)
606 {
607 logger.VerboseError.WriteLine(ex.ToString());
608 }
609 }
610 comparer.Value = value;
611 }
612 else
613 {
614 value = Convert.ToUInt64(SearchArgs.CompareStartValue);
615 comparer.Value = value;
616 }
617 if (comparer.Compare(Convert.ToUInt64(Value), value))
618 {
619 foundAddresses.Add(new ResultType<object>(comparer.Address, comparer.Value));
620 }
621 }
622 #endregion
623 }
624 else
625 {
626 #region 64bits - signed
627 var Value = br.ReadInt64();
628 using (_64bit_signed_comparer_ comparer = new _64bit_signed_comparer_(SearchArgs, address))
629 {
630 long value = 0;
631 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
632 {
633 using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
634 {
635 try
636 {
637 gmp.OpenProvider();
638 gmp.ReadMemory(address, out value);
639 gmp.CloseProvider();
640 }
641 catch (Exception ex)
642 {
643 logger.VerboseError.WriteLine(ex.ToString());
644 }
645 }
646 comparer.Value = value;
647 }
648 else
649 {
650 value = Convert.ToInt64(SearchArgs.CompareStartValue);
651 comparer.Value = value;
652 }
653 if (comparer.Compare(Convert.ToInt64(Value), value))
654 {
655 foundAddresses.Add(new ResultType<object>(comparer.Address, comparer.Value));
656 }
657 }
658 #endregion
659 }
660 break;
661 }
662 #endregion
663 }
664 }
665
666 double double_percent_done = 100.0 * (double)((double)count / (double)totalcount);
667 int int_percent_done = (int)double_percent_done;
668 if (int_percent_done != Last_Whole_Percent_Done && count % 100000 == 0)
669 {
670 if (int_percent_done <= 100)
671 {
672 update_progress.Invoke(int_percent_done, string.Format(" -> Reading Address: 0x{0:x8}", address));
673 Last_Whole_Percent_Done = int_percent_done;
674 }
675 }
676
677 count++;
678 }
679 return foundAddresses.ToArray();
680 }
681
682 }
683 }

  ViewVC Help
Powered by ViewVC 1.1.22