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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 492 - (show annotations) (download)
Mon Jun 3 22:07:07 2013 UTC (8 years, 4 months ago) by william
File size: 57409 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) : this(guid, false) { }
44 public SearchResultReader(Guid guid, bool delete) : base(guid, delete) { ResultGuid = guid; ReadHeader(); }
45
46 protected override string TemporaryFolder { get { return SearchResultsConstants.SearchResultsFolder; } }
47 private void ReadHeader()
48 {
49 try
50 {
51 using (FileStream fs = CreateReader())
52 {
53 using (BinaryReader binReader = new BinaryReader(fs))
54 {
55 //int ResultsRead = 0;
56 // SRD (string)
57 string magic = Encoding.UTF8.GetString(binReader.ReadBytes(3));
58 string SRD = "SRD";
59 if (magic != SRD)
60 {
61 throw new InvalidOperationException(string.Format("Encountered unexpected magic: {0} expected: {1}", magic, SRD));
62 }
63 // version (int)
64 int version = binReader.ReadInt32();
65
66 if (version == 1)
67 {
68 // do nothing
69 }
70 else if (version == 2)
71 {
72 int guid_array_length = binReader.ReadInt32();
73 byte[] guid_array = new byte[guid_array_length];
74 binReader.Read(guid_array, 0, guid_array_length);
75 Guid g = new Guid(guid_array);
76 if (g != ResultGuid)
77 {
78 throw new InvalidOperationException(string.Format("Encountered wrong search results guid: read '{1}' excpected '{2}'", g.ToString(), ResultGuid.ToString()));
79 }
80 }
81 else
82 {
83 throw new InvalidOperationException(string.Format("Encountered unexpected version: {0} expected: {1} or {2}", version, 1, 2));
84 }
85 // resultcount
86 int resultcount = binReader.ReadInt32();
87 if (resultcount == 0)
88 {
89 throw new InvalidOperationException(string.Format("Result Count is zero"));
90 }
91 ResultCount = resultcount;
92 ResultDataOffset = binReader.BaseStream.Position;
93 //for (int i = 0; i < ResultCount; i++)
94 //{
95 // try
96 // {
97 // ResultsRead = i;
98 // uint address = 0;
99 // // assume uint for data type
100 // uint value = 0;
101 // address = binReader.ReadUInt32();
102 // value = binReader.ReadUInt32();
103 // //if (i % 100000 == 0)
104 // // logger.VerboseDebug.WriteLine("Result: @0x{0:x8}=0x{1:x8}", address, value);
105 // }
106 // catch (Exception ex)
107 // {
108 // logger.VerboseError.WriteLine("SearchResultReader.ReadHeader():Consistency Check");
109 // //logger.VerboseError.WriteLine(ex.ToString());
110 // logger.VerboseError.WriteLine("Faied entry: {0}", ResultsRead);
111 // break;
112 // }
113 // ResultsRead++; // add 1
114 //}
115 ////throw new NotImplementedException("DEBUG: testing SearchResultReader consistency");
116 //if (ResultCount != ResultsRead)
117 //{
118 // throw new InvalidOperationException(string.Format("ResultCount does not match ResultsRead: 0x{0:x8} != 0x{1:x8}", ResultCount, ResultsRead));
119 //}
120 binReader.Close();
121 }
122 fs.Close();
123 }
124 }
125 catch (System.IO.EndOfStreamException) { }
126 }
127 #region ISearchResultReader members
128 public bool ReadCurrentAddess { get; private set; }
129 public bool ReadCurrentValue { get; private set; }
130
131 [Obsolete("GetNextResult has been replaced by GetResults")]
132 public ResultType<TValue> GetNextResult<TValue>() where TValue : IConvertible
133 {
134 return new ResultType<TValue>();
135 }
136 #endregion
137
138
139 public ResultItem[] GetResultItems(bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress)
140 {
141 //var arr = GetResults(unsigned, datatype, update_progress);
142 //ResultItem[] items = new ResultItem[arr.Count()];
143 //for (int i = 0; i < items.Count(); i++)
144 //{
145 // var v = arr[i];
146 // switch (datatype)
147 // {
148 // case SearchDataTypes._8bits:
149 // if (unsigned) { items[i] = new ResultItem(v.Address, false, Convert.ToByte(v.Value)); }
150 // else { items[i] = new ResultItem(v.Address, false, Convert.ToSByte(v.Value)); }
151 // break;
152 // case SearchDataTypes._16bits:
153 // if (unsigned) { items[i] = new ResultItem(v.Address, false, Convert.ToUInt16(v.Value)); }
154 // else { items[i] = new ResultItem(v.Address, false, Convert.ToInt16(v.Value)); }
155 // break;
156 // case SearchDataTypes._32bits:
157 // if (unsigned) { items[i] = new ResultItem(v.Address, false, Convert.ToUInt32(v.Value)); }
158 // else { items[i] = new ResultItem(v.Address, false, Convert.ToInt32(v.Value)); }
159 // break;
160 // case SearchDataTypes._64bits:
161 // if (unsigned) { items[i] = new ResultItem(v.Address, false, Convert.ToUInt64(v.Value)); }
162 // else { items[i] = new ResultItem(v.Address, false, Convert.ToInt64(v.Value)); }
163 // break;
164 // }
165 // //arr[i] = null; // free memory used by the data
166 //}
167 //return items;
168 throw new NotImplementedException();
169 }
170
171
172 //public uint[] GetResultAddresses(bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress)
173 //{
174 // var arr = GetResults(unsigned, datatype, update_progress);
175 // uint[] items = new uint[arr.Count()];
176 // for (int i = 0; i < items.Count(); i++)
177 // {
178 // var v = arr[i];
179 // items[i] = v.Address;
180 // //arr[i] = null; // free memory used by the data
181 // }
182 // return items;
183 //}
184 //public StructResultType<ulong>[] GetResults(bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress)
185 //{
186 // return GetResultsInternal(unsigned, datatype, update_progress);
187 //}
188
189 //private StructResultType<ulong>[] GetResultsInternal(bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress)
190 //{
191 // //update_progress.Invoke(0, string.Empty);
192 // //int Last_Whole_Percent_Done = 0;
193
194 // StructResultType<ulong>[] results = new StructResultType<ulong>[ResultCount];
195 // using (FileStream fs = CreateReader())
196 // {
197 // using (BinaryReader binReader = new BinaryReader(fs))
198 // {
199
200 // binReader.BaseStream.Seek(ResultDataOffset, SeekOrigin.Begin); // seek to start of result data
201 // for (int i = 0; i < ResultCount; i++)
202 // {
203 // uint Address = binReader.ReadUInt32();
204 // ulong Value = 0;
205 // switch (datatype)
206 // {
207 // case SearchDataTypes._8bits:
208 // if (unsigned) { Value = binReader.ReadByte(); } else { Value = (ulong)binReader.ReadSByte(); }
209 // break;
210 // case SearchDataTypes._16bits:
211 // if (unsigned) { Value = binReader.ReadUInt16(); } else { Value = (ulong)binReader.ReadInt16(); }
212 // break;
213 // case SearchDataTypes._32bits:
214 // if (unsigned) { Value = binReader.ReadUInt32(); } else { Value = (ulong)binReader.ReadInt32(); }
215 // break;
216 // case SearchDataTypes._64bits:
217 // if (unsigned) { Value = binReader.ReadUInt64(); } else { Value = (ulong)binReader.ReadInt64(); }
218 // break;
219 // }
220 // results[i] = new StructResultType<ulong>(Address, Value);
221
222 // //double double_percent_done = 100.0 * (double)((double)i / (double)ResultCount);
223 // //int int_percent_done = (int)double_percent_done;
224 // //if (int_percent_done != Last_Whole_Percent_Done && i % 100000 == 0)
225 // //{
226 // // if (int_percent_done <= 100)
227 // // {
228 // // update_progress.Invoke(int_percent_done, string.Format(" -> Loading value for Address: 0x{0:x8}", Address));
229 // // Last_Whole_Percent_Done = int_percent_done;
230 // // }
231 // //}
232 // }
233 // }
234 // }
235 // update_progress.Invoke(0, string.Empty);
236 // return results;
237 //}
238
239 //public uint[] GetAddressRangeAtIndex(int index, int count, bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress)
240 //{
241 // uint[] results = new uint[index + count];
242 // for (int i = 0; i < count; i++)
243 // {
244 // uint result = GetAddressAtIndex(i + index, unsigned, datatype, update_progress);
245 // results[i] = result;
246 // //double percent = 100.0 * ((double)i / (double)count);
247 // //update_progress.Invoke((int)percent, string.Format("-> Reading Address: 0x{1:x8} from index: 0x{1:x8}", results[i].Address, i + index));
248 // }
249 // return results;
250 //}
251
252 //public StructResultType<ulong>[] GetResultRangeAtIndex(int index, int count, bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress)
253 //{
254 // StructResultType<ulong>[] results = new StructResultType<ulong>[index + count];
255 // for (int i = 0; i < count; i++)
256 // {
257 // StructResultType<ulong> result = GetResultAtIndex(i + index, unsigned, datatype, update_progress);
258 // results[i] = result;
259 // //double percent = 100.0 * ((double)i / (double)count);
260 // //update_progress.Invoke((int)percent, string.Format("-> Reading Address: 0x{1:x8} from index: 0x{1:x8}", results[i].Address, i + index));
261 // }
262 // return results;
263 //}
264 //public uint GetAddressAtIndex(int index, bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress)
265 //{
266 // //update_progress.Invoke(0, string.Empty);
267 // int data_size = sizeof(uint); // address size ... should be 4 bytes (could be 8, if we have a 64-bit address)
268 // uint result = 0;
269 // using (FileStream fs = CreateReader())
270 // {
271 // using (BinaryReader binReader = new BinaryReader(fs))
272 // {
273 // binReader.BaseStream.Seek(ResultDataOffset, SeekOrigin.Begin); // seek to start of result data
274 // data_size += (int)datatype / 8;
275 // long offset = data_size * index;
276 // binReader.BaseStream.Seek(offset, SeekOrigin.Current);
277 // uint Address = binReader.ReadUInt32();
278 // object Value = 0;
279 // switch (datatype)
280 // {
281 // case SearchDataTypes._8bits:
282 // if (unsigned) { Value = binReader.ReadByte(); } else { Value = binReader.ReadSByte(); }
283 // break;
284 // case SearchDataTypes._16bits:
285 // if (unsigned) { Value = binReader.ReadUInt16(); } else { Value = binReader.ReadInt16(); }
286 // break;
287 // case SearchDataTypes._32bits:
288 // if (unsigned) { Value = binReader.ReadUInt32(); } else { Value = binReader.ReadInt32(); }
289 // break;
290 // case SearchDataTypes._64bits:
291 // if (unsigned) { Value = binReader.ReadUInt64(); } else { Value = binReader.ReadInt64(); }
292 // break;
293 // }
294 // result = Address;
295 // }
296 // }
297 // return result;
298 //}
299 //public StructResultType<ulong> GetResultAtIndex(int index, bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress)
300 //{
301 // //update_progress.Invoke(0, string.Empty);
302 // int data_size = sizeof(uint); // address size ... should be 4 bytes (could be 8, if we have a 64-bit address)
303 // StructResultType<ulong> result = StructResultType<ulong>.Empty;
304 // using (FileStream fs = CreateReader())
305 // {
306 // using (BinaryReader binReader = new BinaryReader(fs))
307 // {
308 // binReader.BaseStream.Seek(ResultDataOffset, SeekOrigin.Begin); // seek to start of result data
309 // data_size += (int)datatype / 8;
310 // long offset = data_size * index;
311 // binReader.BaseStream.Seek(offset, SeekOrigin.Current);
312 // uint Address = binReader.ReadUInt32();
313 // ulong Value = 0;
314 // switch (datatype)
315 // {
316 // case SearchDataTypes._8bits:
317 // if (unsigned) { Value = binReader.ReadByte(); } else { Value = (ulong)binReader.ReadSByte(); }
318 // break;
319 // case SearchDataTypes._16bits:
320 // if (unsigned) { Value = binReader.ReadUInt16(); } else { Value = (ulong)binReader.ReadInt16(); }
321 // break;
322 // case SearchDataTypes._32bits:
323 // if (unsigned) { Value = binReader.ReadUInt32(); } else { Value = (ulong)binReader.ReadInt32(); }
324 // break;
325 // case SearchDataTypes._64bits:
326 // if (unsigned) { Value = binReader.ReadUInt64(); } else { Value = (ulong)binReader.ReadInt64(); }
327 // break;
328 // }
329 // result = new StructResultType<ulong>(Address, Value);
330 // }
331 // }
332 // return result;
333 //}
334
335 #region old-code: public StructResultType<ulong>[] GetSearchAddressValueMatches(IAcceptsProcessAndConfig iapc, SearchType SearchArgs, Action<int, string> update_progress)
336 //public StructResultType<ulong>[] GetSearchAddressValueMatches(IAcceptsProcessAndConfig iapc, SearchType SearchArgs, Action<int, string> update_progress)
337 //{
338
339 // //int Last_Whole_Percent_Done = 0;
340 // bool unsigned = SearchArgs.IsUnsignedDataType;
341 // SearchDataTypes sdt = SearchArgs.DataType;
342 // List<StructResultType<ulong>> foundAddresses = new List<StructResultType<ulong>>();
343 // //var addresses = this.GetResultAddresses(unsigned, sdt, update_progress);
344 // const int item_count = 0x1000;
345 // int totalcount = this.ResultCount;
346 // update_progress.Invoke(0, string.Format(" -> Reading 0x{1:x8} total Address Indices (0x{1:x8} indicies at a time) ... Please wait ...", this.ResultCount, item_count));
347
348 // //var results = this.GetResults(unsigned, sdt, update_progress);
349
350 // #region old-code
351 // for (int i = 0; i < this.ResultCount; i += item_count)
352 // {
353 // //int count = 0;
354 // var addresses = this.GetAddressRangeAtIndex(i, item_count, unsigned, sdt, update_progress);
355 // uint STEP_SIZE = (uint)sdt / 8;
356 // for (int j = 0; j < addresses.Length; j++)
357 // {
358 // var address = addresses[j];
359 // //foreach (var address in addresses)
360 // //{
361 // #region t
362 // byte[] data = new byte[0];
363 // using (GenericMemoryProvider provider = new GenericMemoryProvider(iapc))
364 // {
365 // int bytesRead = 0;
366 // provider.OpenProvider();
367 // provider.ReadProcessMemory(address, STEP_SIZE, out bytesRead, out data);
368 // provider.CloseProvider();
369 // }
370 // using (MemoryStream ms = new MemoryStream(data))
371 // {
372 // using (BinaryReader br = new BinaryReader(ms))
373 // {
374 // #region comparison
375 // switch (sdt)
376 // {
377 // case SearchDataTypes._8bits:
378 // if (unsigned)
379 // {
380 // #region 8bits - unsigned
381 // var Value = br.ReadByte();
382 // using (_8bit_unsigned_comparer_ comparer = new _8bit_unsigned_comparer_(SearchArgs, address))
383 // {
384 // byte value = 0;
385 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
386 // {
387 // using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
388 // {
389 // try
390 // {
391 // gmp.OpenProvider();
392 // gmp.ReadMemory(address, out value);
393 // gmp.CloseProvider();
394 // }
395 // catch (Exception ex)
396 // {
397 // logger.VerboseError.WriteLine(ex.ToString());
398 // }
399 // }
400 // comparer.Value = value;
401 // }
402 // else
403 // {
404 // value = Convert.ToByte(SearchArgs.CompareStartValue);
405 // comparer.Value = value;
406 // }
407 // if (comparer.Compare(Convert.ToByte(Value), value))
408 // {
409 // foundAddresses.Add(new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value));
410 // }
411 // }
412 // #endregion
413 // }
414 // else
415 // {
416 // #region 8bits - signed
417 // var Value = br.ReadSByte();
418 // using (_8bit_signed_comparer_ comparer = new _8bit_signed_comparer_(SearchArgs, address))
419 // {
420 // sbyte value = 0;
421 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
422 // {
423 // using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
424 // {
425 // try
426 // {
427 // gmp.OpenProvider();
428 // gmp.ReadMemory(address, out value);
429 // gmp.CloseProvider();
430 // }
431 // catch (Exception ex)
432 // {
433 // logger.VerboseError.WriteLine(ex.ToString());
434 // }
435 // }
436 // comparer.Value = value;
437 // }
438 // else
439 // {
440 // value = Convert.ToSByte(SearchArgs.CompareStartValue);
441 // comparer.Value = value;
442 // }
443 // if (comparer.Compare(Convert.ToSByte(Value), value))
444 // {
445 // foundAddresses.Add(new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value));
446 // }
447 // }
448 // #endregion
449 // }
450 // break;
451 // case SearchDataTypes._16bits:
452 // if (unsigned)
453 // {
454 // #region 16bits - unsigned
455 // var Value = br.ReadUInt16();
456 // using (_16bit_unsigned_comparer_ comparer = new _16bit_unsigned_comparer_(SearchArgs, address))
457 // {
458 // ushort value = 0;
459 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
460 // {
461 // using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
462 // {
463 // try
464 // {
465 // gmp.OpenProvider();
466 // gmp.ReadMemory(address, out value);
467 // gmp.CloseProvider();
468 // }
469 // catch (Exception ex)
470 // {
471 // logger.VerboseError.WriteLine(ex.ToString());
472 // }
473 // }
474 // comparer.Value = value;
475 // }
476 // else
477 // {
478 // value = Convert.ToUInt16(SearchArgs.CompareStartValue);
479 // comparer.Value = value;
480 // }
481 // if (comparer.Compare(Convert.ToUInt16(Value), value))
482 // {
483 // foundAddresses.Add(new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value));
484 // }
485 // }
486 // #endregion
487 // }
488 // else
489 // {
490 // #region 16bits - signed
491 // var Value = br.ReadInt16();
492 // using (_16bit_signed_comparer_ comparer = new _16bit_signed_comparer_(SearchArgs, address))
493 // {
494 // short value = 0;
495 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
496 // {
497 // using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
498 // {
499 // try
500 // {
501 // gmp.OpenProvider();
502 // gmp.ReadMemory(address, out value);
503 // gmp.CloseProvider();
504 // }
505 // catch (Exception ex)
506 // {
507 // logger.VerboseError.WriteLine(ex.ToString());
508 // }
509 // }
510 // comparer.Value = value;
511 // }
512 // else
513 // {
514 // value = Convert.ToInt16(SearchArgs.CompareStartValue);
515 // comparer.Value = value;
516 // }
517 // if (comparer.Compare(Convert.ToInt16(Value), value))
518 // {
519 // foundAddresses.Add(new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value));
520 // }
521 // }
522 // #endregion
523 // }
524 // break;
525 // case SearchDataTypes._32bits:
526 // if (unsigned)
527 // {
528 // #region 32bits - unsigned
529 // var Value = br.ReadUInt32();
530 // using (_32bit_unsigned_comparer_ comparer = new _32bit_unsigned_comparer_(SearchArgs, address))
531 // {
532 // uint value = 0;
533 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
534 // {
535 // using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
536 // {
537 // try
538 // {
539 // gmp.OpenProvider();
540 // gmp.ReadMemory(address, out value);
541 // gmp.CloseProvider();
542 // }
543 // catch (Exception ex)
544 // {
545 // logger.VerboseError.WriteLine(ex.ToString());
546 // }
547 // }
548 // comparer.Value = value;
549 // }
550 // else
551 // {
552 // value = Convert.ToUInt32(SearchArgs.CompareStartValue);
553 // comparer.Value = value;
554 // }
555 // if (comparer.Compare(Convert.ToUInt32(Value), value))
556 // {
557 // foundAddresses.Add(new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value));
558 // }
559 // }
560 // #endregion
561 // }
562 // else
563 // {
564 // #region 32bits - signed
565 // var Value = br.ReadInt32();
566 // using (_32bit_signed_comparer_ comparer = new _32bit_signed_comparer_(SearchArgs, address))
567 // {
568 // int value = 0;
569 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
570 // {
571 // using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
572 // {
573 // try
574 // {
575 // gmp.OpenProvider();
576 // gmp.ReadMemory(address, out value);
577 // gmp.CloseProvider();
578 // }
579 // catch (Exception ex)
580 // {
581 // logger.VerboseError.WriteLine(ex.ToString());
582 // }
583 // }
584 // comparer.Value = value;
585 // }
586 // else
587 // {
588 // value = Convert.ToInt32(SearchArgs.CompareStartValue);
589 // comparer.Value = value;
590 // }
591 // if (comparer.Compare(Convert.ToInt32(Value), value))
592 // {
593 // foundAddresses.Add(new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value));
594 // }
595 // }
596 // #endregion
597 // }
598 // break;
599 // case SearchDataTypes._64bits:
600 // if (unsigned)
601 // {
602 // #region 64bits - unsigned
603 // var Value = br.ReadUInt64();
604 // using (_64bit_unsigned_comparer_ comparer = new _64bit_unsigned_comparer_(SearchArgs, address))
605 // {
606 // ulong value = 0;
607 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
608 // {
609 // using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
610 // {
611 // try
612 // {
613 // gmp.OpenProvider();
614 // gmp.ReadMemory(address, out value);
615 // gmp.CloseProvider();
616 // }
617 // catch (Exception ex)
618 // {
619 // logger.VerboseError.WriteLine(ex.ToString());
620 // }
621 // }
622 // comparer.Value = value;
623 // }
624 // else
625 // {
626 // value = Convert.ToUInt64(SearchArgs.CompareStartValue);
627 // comparer.Value = value;
628 // }
629 // if (comparer.Compare(Convert.ToUInt64(Value), value))
630 // {
631 // foundAddresses.Add(new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value));
632 // }
633 // }
634 // #endregion
635 // }
636 // else
637 // {
638 // #region 64bits - signed
639 // var Value = br.ReadInt64();
640 // using (_64bit_signed_comparer_ comparer = new _64bit_signed_comparer_(SearchArgs, address))
641 // {
642 // long value = 0;
643 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
644 // {
645 // using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
646 // {
647 // try
648 // {
649 // gmp.OpenProvider();
650 // gmp.ReadMemory(address, out value);
651 // gmp.CloseProvider();
652 // }
653 // catch (Exception ex)
654 // {
655 // logger.VerboseError.WriteLine(ex.ToString());
656 // }
657 // }
658 // comparer.Value = value;
659 // }
660 // else
661 // {
662 // value = Convert.ToInt64(SearchArgs.CompareStartValue);
663 // comparer.Value = value;
664 // }
665 // if (comparer.Compare(Convert.ToInt64(Value), value))
666 // {
667 // foundAddresses.Add(new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value));
668 // }
669 // }
670 // #endregion
671 // }
672 // break;
673 // }
674 // #endregion
675 // }
676 // }
677 // //double double_percent_done1 = 100.0 * (double)((double)j / (double)addresses.Length);
678 // //int int_percent_done1 = (int)double_percent_done1;
679 // //if (j % 10 == 0)
680 // //{
681 // // if (int_percent_done1 <= 100)
682 // // {
683 // // update_progress.Invoke(int_percent_done1, string.Format(" -> Reading Index: 0x{0:x8} of 0x{1:x8}", j, item_count));
684 // // //Last_Whole_Percent_Done = int_percent_done1;
685 // // }
686 // //}
687 // //count++;
688 // #endregion
689 // //}
690 // }
691 // double double_percent_done2 = 100.0 * (double)((double)i / (double)totalcount);
692 // int int_percent_done2 = (int)double_percent_done2;
693 // if (i % (item_count * 2) == 0)
694 // {
695 // if (int_percent_done2 <= 100)
696 // {
697 // update_progress.Invoke(int_percent_done2, string.Format(" -> Read 0x{0:x8} indicies for Address Indice: 0x{1:x8} of 0x{2:x8}", item_count, i, totalcount));
698 // //Last_Whole_Percent_Done = int_percent_done;
699 // }
700 // }
701 // }
702 // #endregion
703
704 // return foundAddresses.ToArray();
705 //}
706 #endregion
707
708
709
710 public StructResultType<ulong>[] GetSearchAddressValueMatches(IAcceptsProcessAndConfig iapc, SearchType SearchArgs, Action<int, string> update_progress)
711 {
712
713 bool unsigned = SearchArgs.IsUnsignedDataType;
714 SearchDataTypes sdt = SearchArgs.DataType;
715 uint STEP_SIZE = (uint)sdt / 8;
716 List<StructResultType<ulong>> results = new List<StructResultType<ulong>>();
717
718 ulong compare_value = Convert.ToUInt64(SearchArgs.CompareStartValue);
719 update_progress.Invoke(0, string.Format("-> Checking value(s) for 0x{0:x8} addresses ... Please Wait ...", this.ResultCount));
720 using (FileStream fs = CreateReader())
721 {
722 using (BinaryReader br = new BinaryReader(fs))
723 {
724 br.BaseStream.Seek(ResultDataOffset, SeekOrigin.Begin); // seek to start of result data
725 for (int i = 0; i < this.ResultCount; i++)
726 {
727 uint address = br.ReadUInt32();
728 ulong value = 0;
729 switch (sdt)
730 {
731 case SearchDataTypes._8bits:
732 if (unsigned) { value = br.ReadByte(); } else { value = (ulong)br.ReadSByte(); }
733 break;
734 case SearchDataTypes._16bits:
735 if (unsigned) { value = br.ReadUInt16(); } else { value = (ulong)br.ReadInt16(); }
736 break;
737 case SearchDataTypes._32bits:
738 if (unsigned) { value = br.ReadUInt32(); } else { value = (ulong)br.ReadInt32(); }
739 break;
740 case SearchDataTypes._64bits:
741 if (unsigned) { value = br.ReadUInt64(); } else { value = (ulong)br.ReadInt64(); }
742 break;
743 }
744
745 StructResultType<ulong> result = StructResultType<ulong>.Empty;
746 if (CheckResult<ulong>(iapc, SearchArgs, update_progress, address, compare_value, out result))
747 {
748 results.Add(result);
749 }
750 //double percent = 100.0 * ((double)i / (double)this.ResultCount);
751 //update_progress.Invoke((int)percent, string.Format("-> Checking value for Address: 0x{0:x8} from index: 0x{1:x8}", address, i));
752 }
753 }
754 }
755 return results.ToArray();
756 }
757
758
759 #region private bool CheckResult<T>(IAcceptsProcessAndConfig iapc, SearchType SearchArgs, Action<int, string> update_progress, uint address, T compare_value, out StructResultType<ulong> result)
760 private bool CheckResult<T>(IAcceptsProcessAndConfig iapc, SearchType SearchArgs, Action<int, string> update_progress, uint address, T compare_value, out StructResultType<ulong> result)
761 {
762 result = StructResultType<ulong>.Empty;
763 bool valid_result = false;
764 bool unsigned = SearchArgs.IsUnsignedDataType;
765 SearchDataTypes sdt = SearchArgs.DataType;
766 switch (sdt)
767 {
768 case SearchDataTypes._8bits:
769 if (unsigned)
770 {
771 #region 8bits - unsigned
772 using (_8bit_unsigned_comparer_ comparer = new _8bit_unsigned_comparer_(SearchArgs, address))
773 {
774 byte value = 0;
775 using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
776 {
777 try
778 {
779 gmp.OpenProvider();
780 gmp.ReadMemory(address, out value);
781 gmp.CloseProvider();
782 }
783 catch (Exception ex)
784 {
785 logger.VerboseError.WriteLine(ex.ToString());
786 }
787 }
788 comparer.Value = value;
789 if (comparer.Compare(Convert.ToByte(compare_value), value))
790 {
791 result = new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value);
792 valid_result = true;
793 }
794 }
795 #endregion
796 }
797 else
798 {
799 #region 8bits - signed
800 using (_8bit_signed_comparer_ comparer = new _8bit_signed_comparer_(SearchArgs, address))
801 {
802 sbyte value = 0;
803 using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
804 {
805 try
806 {
807 gmp.OpenProvider();
808 gmp.ReadMemory(address, out value);
809 gmp.CloseProvider();
810 }
811 catch (Exception ex)
812 {
813 logger.VerboseError.WriteLine(ex.ToString());
814 }
815 }
816 comparer.Value = value;
817 if (comparer.Compare(Convert.ToSByte(compare_value), value))
818 {
819 result = new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value);
820 valid_result = true;
821 }
822 }
823 #endregion
824 } break;
825 case SearchDataTypes._16bits:
826 if (unsigned)
827 {
828 #region 16bits - unsigned
829 using (_16bit_unsigned_comparer_ comparer = new _16bit_unsigned_comparer_(SearchArgs, address))
830 {
831 ushort value = 0;
832 using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
833 {
834 try
835 {
836 gmp.OpenProvider();
837 gmp.ReadMemory(address, out value);
838 gmp.CloseProvider();
839 }
840 catch (Exception ex)
841 {
842 logger.VerboseError.WriteLine(ex.ToString());
843 }
844 }
845 comparer.Value = value;
846 if (comparer.Compare(Convert.ToUInt16(compare_value), value))
847 {
848 result = new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value);
849 valid_result = true;
850 }
851 }
852 #endregion
853 }
854 else
855 {
856 #region 16bits - signed
857 using (_16bit_signed_comparer_ comparer = new _16bit_signed_comparer_(SearchArgs, address))
858 {
859 short value = 0;
860 using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
861 {
862 try
863 {
864 gmp.OpenProvider();
865 gmp.ReadMemory(address, out value);
866 gmp.CloseProvider();
867 }
868 catch (Exception ex)
869 {
870 logger.VerboseError.WriteLine(ex.ToString());
871 }
872 }
873 comparer.Value = value;
874 if (comparer.Compare(Convert.ToSByte(compare_value), value))
875 {
876 result = new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value);
877 valid_result = true;
878 }
879 }
880 #endregion
881 } break;
882 case SearchDataTypes._32bits:
883 if (unsigned)
884 {
885 #region 32bits - unsigned
886 using (_32bit_unsigned_comparer_ comparer = new _32bit_unsigned_comparer_(SearchArgs, address))
887 {
888 uint value = 0;
889 using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
890 {
891 try
892 {
893 gmp.OpenProvider();
894 gmp.ReadMemory(address, out value);
895 gmp.CloseProvider();
896 }
897 catch (Exception ex)
898 {
899 logger.VerboseError.WriteLine(ex.ToString());
900 }
901 }
902 comparer.Value = value;
903 if (comparer.Compare(Convert.ToUInt32(compare_value), value))
904 {
905 result = new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value);
906 valid_result = true;
907 }
908 }
909 #endregion
910 }
911 else
912 {
913 #region 32bits - signed
914 using (_32bit_signed_comparer_ comparer = new _32bit_signed_comparer_(SearchArgs, address))
915 {
916 int value = 0;
917 using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
918 {
919 try
920 {
921 gmp.OpenProvider();
922 gmp.ReadMemory(address, out value);
923 gmp.CloseProvider();
924 }
925 catch (Exception ex)
926 {
927 logger.VerboseError.WriteLine(ex.ToString());
928 }
929 }
930 comparer.Value = value;
931 if (comparer.Compare(Convert.ToInt32(compare_value), value))
932 {
933 result = new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value);
934 valid_result = true;
935 }
936 }
937 #endregion
938 } break;
939 case SearchDataTypes._64bits:
940 if (unsigned)
941 {
942 #region 64bits - unsigned
943 using (_64bit_unsigned_comparer_ comparer = new _64bit_unsigned_comparer_(SearchArgs, address))
944 {
945 ulong value = 0;
946 using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
947 {
948 try
949 {
950 gmp.OpenProvider();
951 gmp.ReadMemory(address, out value);
952 gmp.CloseProvider();
953 }
954 catch (Exception ex)
955 {
956 logger.VerboseError.WriteLine(ex.ToString());
957 }
958 }
959 comparer.Value = value;
960 if (comparer.Compare(Convert.ToUInt64(compare_value), value))
961 {
962 result = new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value);
963 valid_result = true;
964 }
965 }
966 #endregion
967 }
968 else
969 {
970 #region 64bits - signed
971 using (_64bit_signed_comparer_ comparer = new _64bit_signed_comparer_(SearchArgs, address))
972 {
973 long value = 0;
974 using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
975 {
976 try
977 {
978 gmp.OpenProvider();
979 gmp.ReadMemory(address, out value);
980 gmp.CloseProvider();
981 }
982 catch (Exception ex)
983 {
984 logger.VerboseError.WriteLine(ex.ToString());
985 }
986 }
987 comparer.Value = value;
988 if (comparer.Compare(Convert.ToInt64(compare_value), value))
989 {
990 result = new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value);
991 valid_result = true;
992 }
993 }
994 #endregion
995 } break;
996 }
997 return valid_result;
998 }
999 #endregion
1000
1001 //private void WriteResult<T>(IAcceptsProcessAndConfig iapc, SearchType SearchArgs, Action<int, string> update_progress, SearchResultWriter writer, uint address, T value)
1002 // where T: IConvertible
1003 //{
1004
1005 //}
1006
1007 }
1008 }

  ViewVC Help
Powered by ViewVC 1.1.22