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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22