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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22