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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22