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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 531 - (show annotations) (download)
Wed Jun 5 08:45:32 2013 UTC (8 years, 2 months ago) by william
File size: 66993 byte(s)
+ fix index increment for SearchResultReader enumerator

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 using RomCheater.Core;
26
27 namespace RomCheater.Serialization
28 {
29 public interface ISearchResultReader
30 {
31 ResultType<TValue> GetNextResult<TValue>() where TValue : IConvertible;
32 }
33
34 public interface ISerializedResult
35 {
36 SearchResultReader.ResultReaderCollection GetResults(bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress);
37 bool ContainsAddress(uint address, bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress);
38 bool ContainsAddress(uint address, bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress, out int index);
39 //void GetResultAtAddress(uint address, bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress, out StructResultType<ulong> result);
40 void GetResultAtIndex(int index, bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress, out StructResultType<ulong> result);
41 }
42
43
44
45 public class SearchResultReader : SerializationReader, ISearchResultReader, ISerializedResult
46 {
47 #region ISerializedResult members
48 public ResultReaderCollection GetResults(bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress)
49 {
50 ResultReaderCollection collection = new ResultReaderCollection(this, unsigned, datatype, update_progress);
51 return collection;
52 }
53 public bool ContainsAddress(uint address, bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress)
54 {
55 int index = 0;
56 return ContainsAddress(address, unsigned, datatype, update_progress, out index);
57 }
58 public bool ContainsAddress(uint address, bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress, out int index)
59 {
60 bool contains = false;
61 index = 0;
62 foreach (var result in GetResults(unsigned, datatype, update_progress))
63 {
64 if (result.Address == address)
65 {
66 if (contains)
67 {
68 throw new ArgumentOutOfRangeException("address", string.Format("Found more than one result that matched address: 0x{0} - index={1}", address.ToString("X"), index));
69 }
70 contains = true;
71 break; // should never throw the exception, if we break here (only returning/checking the first found address
72 }
73 index++;
74 }
75 return contains;
76 }
77 public void GetResultAtIndex(int index, bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress, out StructResultType<ulong> result)
78 {
79 result = StructResultType<ulong>.Empty;
80 int count = 0;
81 foreach (var entry in GetResults(unsigned, datatype, update_progress))
82 {
83 if (index == count)
84 {
85 result = entry;
86 break;
87 }
88 count++;
89 }
90 }
91 //public void GetResultAtAddress(uint address, bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress, out StructResultType<ulong> result)
92 //{
93 // result = StructResultType<ulong>.Empty;
94 // int index = 0;
95 // if (ContainsAddress(address, unsigned, datatype, update_progress, out index))
96 // {
97 // result = GetResultAtIndex(this,index, unsigned, datatype, update_progress);
98 // }
99 // else
100 // {
101 // throw new KeyNotFoundException(string.Format("Could not find result for address: 0x{0}", address.ToString("X")));
102 // }
103 //}
104 #endregion
105
106
107 #region sub-clases
108 #region public class ResultReaderEnumerator : IEnumerator<StructResultType<ulong>>
109 public class ResultReaderEnumerator : IEnumerator<StructResultType<ulong>>
110 {
111 //private IEnumerator<StructResultType<ulong>> _enumerator;
112
113 private int _index = 0;
114 private int index { get { return _index; } set { _index = value; } }
115 private SearchResultReader _reader;
116 private SearchResultReader reader { get { CheckReaderNull(); return _reader; } set { _reader = value; CheckReaderNull(); } }
117 private bool _unsigned;
118 private bool unsigned { get { return _unsigned; } set { _unsigned = value; } }
119 private SearchDataTypes _datatype;
120 private SearchDataTypes datatype { get { return _datatype; } set { _datatype = value; } }
121 private Action<int, string> _update_progress;
122 private Action<int, string> update_progress { get { return _update_progress; } set { _update_progress = value; } }
123
124 private int MinimumIndex { get { return this.reader.MinimumIndex; } }
125 private int MaximumIndex { get { return this.reader.MaximumIndex; } }
126
127
128 #region Exceptions
129 private void CheckReaderNull()
130 {
131 if (_reader == null)
132 {
133 throw new ArgumentNullException("reader", "Reader cannot be null");
134 }
135 }
136 private void CheckIndexLessThanMin()
137 {
138 if (_index < MinimumIndex)
139 {
140 throw new ArgumentOutOfRangeException("index", string.Format("index cannot be less than {0}", MinimumIndex));
141 }
142 }
143 private void CheckIndexGreaterThanMax()
144 {
145 if (_index > MaximumIndex)
146 {
147 throw new ArgumentOutOfRangeException("index", string.Format("index cannot be greater than {0}", MaximumIndex));
148 }
149 }
150 #endregion
151
152 public ResultReaderEnumerator(SearchResultReader reader, bool unsigned, SearchDataTypes datatype, Action<int, string> update_progress)
153 {
154 this.reader = reader;
155 this.unsigned = unsigned;
156 this.datatype = datatype;
157 this.update_progress = update_progress;
158 this.index = -1;
159 }
160
161 public StructResultType<ulong> Current
162 {
163 get
164 {
165 // result the current entry (at position)
166 //throw new NotImplementedException();
167 CheckIndexLessThanMin();
168 CheckIndexGreaterThanMax();
169 return GetResultAtIndex(this.reader, this.index, this.unsigned, this.datatype, this.update_progress);
170 }
171 }
172
173 public void Dispose()
174 {
175 //throw new NotImplementedException();
176 }
177
178 object System.Collections.IEnumerator.Current
179 {
180 get { return this.Current; }
181 }
182
183 public bool MoveNext()
184 {
185 this.index++; // increment after processing index at MinimumIndex
186 if(this.index >= this.MinimumIndex && this.index <= this.MaximumIndex)
187 {
188 return true;
189 }
190 Reset();
191 return false;
192
193 }
194
195 public void Reset()
196 {
197 this.index = -1;
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 double percent_done = 100.0 * ((double)index / (double)reader.ResultCount);
562 string message = string.Format("-> Reading Result at index: 0x{0}", index.ToString("X"));
563 update_progress((int)percent_done, message);
564 return result;
565 }
566 #endregion
567
568 #region old-code: private StructResultType<ulong>[] GetSearchAddressValueMatches(IAcceptsProcessAndConfig iapc, SearchType SearchArgs, Action<int, string> update_progress)
569 //private StructResultType<ulong>[] GetSearchAddressValueMatches(IAcceptsProcessAndConfig iapc, SearchType SearchArgs, Action<int, string> update_progress)
570 //{
571 // //int Last_Whole_Percent_Done = 0;
572 // bool unsigned = SearchArgs.IsUnsignedDataType;
573 // SearchDataTypes sdt = SearchArgs.DataType;
574 // List<StructResultType<ulong>> foundAddresses = new List<StructResultType<ulong>>();
575 // //var addresses = this.GetResultAddresses(unsigned, sdt, update_progress);
576 // const int item_count = 0x1000;
577 // int totalcount = this.ResultCount;
578 // 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));
579 // //var results = this.GetResults(unsigned, sdt, update_progress);
580 // #region old-code
581 // for (int i = 0; i < this.ResultCount; i += item_count)
582 // {
583 // //int count = 0;
584 // var addresses = this.GetAddressRangeAtIndex(i, item_count, unsigned, sdt, update_progress);
585 // uint STEP_SIZE = (uint)sdt / 8;
586 // for (int j = 0; j < addresses.Length; j++)
587 // {
588 // var address = addresses[j];
589 // //foreach (var address in addresses)
590 // //{
591 // #region t
592 // byte[] data = new byte[0];
593 // using (GenericMemoryProvider provider = new GenericMemoryProvider(iapc))
594 // {
595 // int bytesRead = 0;
596 // provider.OpenProvider();
597 // provider.ReadProcessMemory(address, STEP_SIZE, out bytesRead, out data);
598 // provider.CloseProvider();
599 // }
600 // using (MemoryStream ms = new MemoryStream(data))
601 // {
602 // using (BinaryReader br = new BinaryReader(ms))
603 // {
604 // #region comparison
605 // switch (sdt)
606 // {
607 // case SearchDataTypes._8bits:
608 // if (unsigned)
609 // {
610 // #region 8bits - unsigned
611 // var Value = br.ReadByte();
612 // using (_8bit_unsigned_comparer_ comparer = new _8bit_unsigned_comparer_(SearchArgs, address))
613 // {
614 // byte value = 0;
615 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
616 // {
617 // using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
618 // {
619 // try
620 // {
621 // gmp.OpenProvider();
622 // gmp.ReadMemory(address, out value);
623 // gmp.CloseProvider();
624 // }
625 // catch (Exception ex)
626 // {
627 // logger.VerboseError.WriteLine(ex.ToString());
628 // }
629 // }
630 // comparer.Value = value;
631 // }
632 // else
633 // {
634 // value = Convert.ToByte(SearchArgs.CompareStartValue);
635 // comparer.Value = value;
636 // }
637 // if (comparer.Compare(Convert.ToByte(Value), value))
638 // {
639 // foundAddresses.Add(new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value));
640 // }
641 // }
642 // #endregion
643 // }
644 // else
645 // {
646 // #region 8bits - signed
647 // var Value = br.ReadSByte();
648 // using (_8bit_signed_comparer_ comparer = new _8bit_signed_comparer_(SearchArgs, address))
649 // {
650 // sbyte value = 0;
651 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
652 // {
653 // using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
654 // {
655 // try
656 // {
657 // gmp.OpenProvider();
658 // gmp.ReadMemory(address, out value);
659 // gmp.CloseProvider();
660 // }
661 // catch (Exception ex)
662 // {
663 // logger.VerboseError.WriteLine(ex.ToString());
664 // }
665 // }
666 // comparer.Value = value;
667 // }
668 // else
669 // {
670 // value = Convert.ToSByte(SearchArgs.CompareStartValue);
671 // comparer.Value = value;
672 // }
673 // if (comparer.Compare(Convert.ToSByte(Value), value))
674 // {
675 // foundAddresses.Add(new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value));
676 // }
677 // }
678 // #endregion
679 // }
680 // break;
681 // case SearchDataTypes._16bits:
682 // if (unsigned)
683 // {
684 // #region 16bits - unsigned
685 // var Value = br.ReadUInt16();
686 // using (_16bit_unsigned_comparer_ comparer = new _16bit_unsigned_comparer_(SearchArgs, address))
687 // {
688 // ushort value = 0;
689 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
690 // {
691 // using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
692 // {
693 // try
694 // {
695 // gmp.OpenProvider();
696 // gmp.ReadMemory(address, out value);
697 // gmp.CloseProvider();
698 // }
699 // catch (Exception ex)
700 // {
701 // logger.VerboseError.WriteLine(ex.ToString());
702 // }
703 // }
704 // comparer.Value = value;
705 // }
706 // else
707 // {
708 // value = Convert.ToUInt16(SearchArgs.CompareStartValue);
709 // comparer.Value = value;
710 // }
711 // if (comparer.Compare(Convert.ToUInt16(Value), value))
712 // {
713 // foundAddresses.Add(new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value));
714 // }
715 // }
716 // #endregion
717 // }
718 // else
719 // {
720 // #region 16bits - signed
721 // var Value = br.ReadInt16();
722 // using (_16bit_signed_comparer_ comparer = new _16bit_signed_comparer_(SearchArgs, address))
723 // {
724 // short value = 0;
725 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
726 // {
727 // using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
728 // {
729 // try
730 // {
731 // gmp.OpenProvider();
732 // gmp.ReadMemory(address, out value);
733 // gmp.CloseProvider();
734 // }
735 // catch (Exception ex)
736 // {
737 // logger.VerboseError.WriteLine(ex.ToString());
738 // }
739 // }
740 // comparer.Value = value;
741 // }
742 // else
743 // {
744 // value = Convert.ToInt16(SearchArgs.CompareStartValue);
745 // comparer.Value = value;
746 // }
747 // if (comparer.Compare(Convert.ToInt16(Value), value))
748 // {
749 // foundAddresses.Add(new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value));
750 // }
751 // }
752 // #endregion
753 // }
754 // break;
755 // case SearchDataTypes._32bits:
756 // if (unsigned)
757 // {
758 // #region 32bits - unsigned
759 // var Value = br.ReadUInt32();
760 // using (_32bit_unsigned_comparer_ comparer = new _32bit_unsigned_comparer_(SearchArgs, address))
761 // {
762 // uint value = 0;
763 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
764 // {
765 // using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
766 // {
767 // try
768 // {
769 // gmp.OpenProvider();
770 // gmp.ReadMemory(address, out value);
771 // gmp.CloseProvider();
772 // }
773 // catch (Exception ex)
774 // {
775 // logger.VerboseError.WriteLine(ex.ToString());
776 // }
777 // }
778 // comparer.Value = value;
779 // }
780 // else
781 // {
782 // value = Convert.ToUInt32(SearchArgs.CompareStartValue);
783 // comparer.Value = value;
784 // }
785 // if (comparer.Compare(Convert.ToUInt32(Value), value))
786 // {
787 // foundAddresses.Add(new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value));
788 // }
789 // }
790 // #endregion
791 // }
792 // else
793 // {
794 // #region 32bits - signed
795 // var Value = br.ReadInt32();
796 // using (_32bit_signed_comparer_ comparer = new _32bit_signed_comparer_(SearchArgs, address))
797 // {
798 // int value = 0;
799 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
800 // {
801 // using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
802 // {
803 // try
804 // {
805 // gmp.OpenProvider();
806 // gmp.ReadMemory(address, out value);
807 // gmp.CloseProvider();
808 // }
809 // catch (Exception ex)
810 // {
811 // logger.VerboseError.WriteLine(ex.ToString());
812 // }
813 // }
814 // comparer.Value = value;
815 // }
816 // else
817 // {
818 // value = Convert.ToInt32(SearchArgs.CompareStartValue);
819 // comparer.Value = value;
820 // }
821 // if (comparer.Compare(Convert.ToInt32(Value), value))
822 // {
823 // foundAddresses.Add(new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value));
824 // }
825 // }
826 // #endregion
827 // }
828 // break;
829 // case SearchDataTypes._64bits:
830 // if (unsigned)
831 // {
832 // #region 64bits - unsigned
833 // var Value = br.ReadUInt64();
834 // using (_64bit_unsigned_comparer_ comparer = new _64bit_unsigned_comparer_(SearchArgs, address))
835 // {
836 // ulong value = 0;
837 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
838 // {
839 // using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
840 // {
841 // try
842 // {
843 // gmp.OpenProvider();
844 // gmp.ReadMemory(address, out value);
845 // gmp.CloseProvider();
846 // }
847 // catch (Exception ex)
848 // {
849 // logger.VerboseError.WriteLine(ex.ToString());
850 // }
851 // }
852 // comparer.Value = value;
853 // }
854 // else
855 // {
856 // value = Convert.ToUInt64(SearchArgs.CompareStartValue);
857 // comparer.Value = value;
858 // }
859 // if (comparer.Compare(Convert.ToUInt64(Value), value))
860 // {
861 // foundAddresses.Add(new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value));
862 // }
863 // }
864 // #endregion
865 // }
866 // else
867 // {
868 // #region 64bits - signed
869 // var Value = br.ReadInt64();
870 // using (_64bit_signed_comparer_ comparer = new _64bit_signed_comparer_(SearchArgs, address))
871 // {
872 // long value = 0;
873 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
874 // {
875 // using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
876 // {
877 // try
878 // {
879 // gmp.OpenProvider();
880 // gmp.ReadMemory(address, out value);
881 // gmp.CloseProvider();
882 // }
883 // catch (Exception ex)
884 // {
885 // logger.VerboseError.WriteLine(ex.ToString());
886 // }
887 // }
888 // comparer.Value = value;
889 // }
890 // else
891 // {
892 // value = Convert.ToInt64(SearchArgs.CompareStartValue);
893 // comparer.Value = value;
894 // }
895 // if (comparer.Compare(Convert.ToInt64(Value), value))
896 // {
897 // foundAddresses.Add(new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value));
898 // }
899 // }
900 // #endregion
901 // }
902 // break;
903 // }
904 // #endregion
905 // }
906 // }
907 // //double double_percent_done1 = 100.0 * (double)((double)j / (double)addresses.Length);
908 // //int int_percent_done1 = (int)double_percent_done1;
909 // //if (j % 10 == 0)
910 // //{
911 // // if (int_percent_done1 <= 100)
912 // // {
913 // // update_progress.Invoke(int_percent_done1, string.Format(" -> Reading Index: 0x{0:x8} of 0x{1:x8}", j, item_count));
914 // // //Last_Whole_Percent_Done = int_percent_done1;
915 // // }
916 // //}
917 // //count++;
918 // #endregion
919 // //}
920 // }
921 // double double_percent_done2 = 100.0 * (double)((double)i / (double)totalcount);
922 // int int_percent_done2 = (int)double_percent_done2;
923 // if (i % (item_count * 2) == 0)
924 // {
925 // if (int_percent_done2 <= 100)
926 // {
927 // 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));
928 // //Last_Whole_Percent_Done = int_percent_done;
929 // }
930 // }
931 // }
932 // #endregion
933
934 // return foundAddresses.ToArray();
935 //}
936 #endregion
937
938 #region private StructResultType<ulong>[] GetSearchAddressValueMatches(IAcceptsProcessAndConfig iapc, SearchType SearchArgs, Action<int, string> update_progress)
939 //private StructResultType<ulong>[] GetSearchAddressValueMatches(IAcceptsProcessAndConfig iapc, SearchType SearchArgs, Action<int, string> update_progress)
940 //{
941 // List<StructResultType<ulong>> results = new List<StructResultType<ulong>>();
942 // using (GenericMemoryProvider gmp = new GenericMemoryProvider(iapc))
943 // {
944 // try
945 // {
946 // gmp.OpenProvider();
947 // bool unsigned = SearchArgs.IsUnsignedDataType;
948 // SearchDataTypes sdt = SearchArgs.DataType;
949 // uint STEP_SIZE = (uint)sdt / 8;
950 // ulong compare_value = Convert.ToUInt64(SearchArgs.CompareStartValue);
951 // update_progress.Invoke(0, string.Format("-> Checking value(s) for 0x{0:x8} addresses ... Please Wait ...", this.ResultCount));
952 // using (FileStream fs = CreateReader())
953 // {
954 // using (BinaryReader br = new BinaryReader(fs))
955 // {
956 // br.BaseStream.Seek(ResultDataOffset, SeekOrigin.Begin); // seek to start of result data
957 // StructResultType<ulong> result = StructResultType<ulong>.Empty;
958 // for (int i = 0; i < this.ResultCount; i++)
959 // {
960 // uint address = br.ReadUInt32();
961 // ulong read_value = 0;
962 // br.BaseStream.Seek(STEP_SIZE, SeekOrigin.Current); // seek STEP_SIZE bytes from address, to next entry
963 // switch (sdt)
964 // {
965 // case SearchDataTypes._8bits:
966 // if (unsigned)
967 // {
968 // byte v = 0;
969 // gmp.ReadMemory(address, out v);
970 // read_value = (ulong)v;
971 // }
972 // else
973 // {
974 // sbyte v = 0;
975 // gmp.ReadMemory(address, out v);
976 // read_value = (ulong)v;
977 // }
978 // break;
979 // case SearchDataTypes._16bits:
980 // if (unsigned)
981 // {
982 // ushort v = 0;
983 // gmp.ReadMemory(address, out v);
984 // read_value = (ulong)v;
985 // }
986 // else
987 // {
988 // short v = 0;
989 // gmp.ReadMemory(address, out v);
990 // read_value = (ulong)v;
991 // }
992 // break;
993 // case SearchDataTypes._32bits:
994 // if (unsigned)
995 // {
996 // uint v = 0;
997 // gmp.ReadMemory(address, out v);
998 // read_value = (ulong)v;
999 // }
1000 // else
1001 // {
1002 // int v = 0;
1003 // gmp.ReadMemory(address, out v);
1004 // read_value = (ulong)v;
1005 // }
1006 // break;
1007 // case SearchDataTypes._64bits:
1008 // if (unsigned)
1009 // {
1010 // ulong v = 0;
1011 // gmp.ReadMemory(address, out v);
1012 // read_value = (ulong)v;
1013 // }
1014 // else
1015 // {
1016 // long v = 0;
1017 // gmp.ReadMemory(address, out v);
1018 // read_value = (ulong)v;
1019 // }
1020 // break;
1021 // }
1022 // double percent = 100.0 * ((double)i / (double)this.ResultCount);
1023 // update_progress.Invoke((int)percent, string.Format("-> Checking value for Address: 0x{0:x8} from index: 0x{1:x8}", address, i));
1024 // if (CheckResult(iapc, SearchArgs, update_progress, address, compare_value, read_value, out result))
1025 // {
1026 // results.Add(result);
1027 // update_progress.Invoke((int)percent, string.Format("-> Address: 0x{0:x8} ... Found Value: {1} ", result.Address, result.Value.ToString("x16")));
1028 // }
1029 // }
1030 // }
1031 // }
1032 // gmp.CloseProvider();
1033 // }
1034 // catch (Exception ex)
1035 // {
1036 // logger.VerboseError.WriteLine(ex.ToString());
1037 // }
1038 // }
1039 // return results.ToArray();
1040 //}
1041 #endregion
1042
1043 #region private TValue ConvertValue<TValue>(ulong value)
1044 //private TValue ConvertValue<TValue>(ulong value)
1045 // where TValue : IConvertible
1046 //{
1047 // TValue converted_value = (TValue)Convert.ChangeType(value, typeof(TValue));
1048 // return converted_value;
1049 //}
1050 #endregion
1051
1052 #region private bool CheckResult<T>(IAcceptsProcessAndConfig iapc, SearchType SearchArgs, Action<int, string> update_progress, uint address, T compare_value, out StructResultType<ulong> result)
1053 //private bool CheckResult(IAcceptsProcessAndConfig iapc, SearchType SearchArgs, Action<int, string> update_progress, uint address, ulong compare_value, ulong read_value, out StructResultType<ulong> result)
1054 //{
1055 // result = StructResultType<ulong>.Empty;
1056 // bool valid_result = false;
1057 // bool unsigned = SearchArgs.IsUnsignedDataType;
1058 // SearchDataTypes sdt = SearchArgs.DataType;
1059 // switch (sdt)
1060 // {
1061 // case SearchDataTypes._8bits:
1062 // if (unsigned)
1063 // {
1064 // #region 8bits - unsigned
1065 // using (_8bit_unsigned_comparer_ comparer = new _8bit_unsigned_comparer_(SearchArgs, address))
1066 // {
1067 // comparer.Value = ConvertValue<byte>(read_value);
1068 // if (comparer.Compare(Convert.ToByte(compare_value), comparer.Value))
1069 // {
1070 // result = new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value);
1071 // valid_result = true;
1072 // }
1073 // }
1074 // #endregion
1075 // }
1076 // else
1077 // {
1078 // #region 8bits - signed
1079 // using (_8bit_signed_comparer_ comparer = new _8bit_signed_comparer_(SearchArgs, address))
1080 // {
1081 // comparer.Value = ConvertValue<sbyte>(read_value);
1082 // if (comparer.Compare(Convert.ToSByte(compare_value), comparer.Value))
1083 // {
1084 // result = new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value);
1085 // valid_result = true;
1086 // }
1087 // }
1088 // #endregion
1089 // } break;
1090 // case SearchDataTypes._16bits:
1091 // if (unsigned)
1092 // {
1093 // #region 16bits - unsigned
1094 // using (_16bit_unsigned_comparer_ comparer = new _16bit_unsigned_comparer_(SearchArgs, address))
1095 // {
1096 // comparer.Value = ConvertValue<ushort>(read_value);
1097 // if (comparer.Compare(Convert.ToUInt16(compare_value), comparer.Value))
1098 // {
1099 // result = new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value);
1100 // valid_result = true;
1101 // }
1102 // }
1103 // #endregion
1104 // }
1105 // else
1106 // {
1107 // #region 16bits - signed
1108 // using (_16bit_signed_comparer_ comparer = new _16bit_signed_comparer_(SearchArgs, address))
1109 // {
1110 // comparer.Value = ConvertValue<short>(read_value);
1111 // if (comparer.Compare(Convert.ToSByte(compare_value), comparer.Value))
1112 // {
1113 // result = new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value);
1114 // valid_result = true;
1115 // }
1116 // }
1117 // #endregion
1118 // } break;
1119 // case SearchDataTypes._32bits:
1120 // if (unsigned)
1121 // {
1122 // #region 32bits - unsigned
1123 // using (_32bit_unsigned_comparer_ comparer = new _32bit_unsigned_comparer_(SearchArgs, address))
1124 // {
1125 // comparer.Value = ConvertValue<uint>(read_value);
1126 // if (comparer.Compare(Convert.ToUInt32(compare_value), comparer.Value))
1127 // {
1128 // result = new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value);
1129 // valid_result = true;
1130 // }
1131 // }
1132 // #endregion
1133 // }
1134 // else
1135 // {
1136 // #region 32bits - signed
1137 // using (_32bit_signed_comparer_ comparer = new _32bit_signed_comparer_(SearchArgs, address))
1138 // {
1139 // comparer.Value = ConvertValue<int>(read_value);
1140 // if (comparer.Compare(Convert.ToInt32(compare_value), comparer.Value))
1141 // {
1142 // result = new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value);
1143 // valid_result = true;
1144 // }
1145 // }
1146 // #endregion
1147 // } break;
1148 // case SearchDataTypes._64bits:
1149 // if (unsigned)
1150 // {
1151 // #region 64bits - unsigned
1152 // using (_64bit_unsigned_comparer_ comparer = new _64bit_unsigned_comparer_(SearchArgs, address))
1153 // {
1154 // comparer.Value = ConvertValue<ulong>(read_value);
1155 // if (comparer.Compare(Convert.ToUInt64(compare_value), comparer.Value))
1156 // {
1157 // result = new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value);
1158 // valid_result = true;
1159 // }
1160 // }
1161 // #endregion
1162 // }
1163 // else
1164 // {
1165 // #region 64bits - signed
1166 // using (_64bit_signed_comparer_ comparer = new _64bit_signed_comparer_(SearchArgs, address))
1167 // {
1168 // comparer.Value = ConvertValue<long>(read_value);
1169 // if (comparer.Compare(Convert.ToInt64(compare_value), comparer.Value))
1170 // {
1171 // result = new StructResultType<ulong>(comparer.Address, (ulong)comparer.Value);
1172 // valid_result = true;
1173 // }
1174 // }
1175 // #endregion
1176 // } break;
1177 // }
1178 // return valid_result;
1179 //}
1180 #endregion
1181
1182 #region private void WriteResult<T>(IAcceptsProcessAndConfig iapc, SearchType SearchArgs, Action<int, string> update_progress, SearchResultWriter writer, uint address, T value)
1183 //private void WriteResult<T>(IAcceptsProcessAndConfig iapc, SearchType SearchArgs, Action<int, string> update_progress, SearchResultWriter writer, uint address, T value)
1184 // where T : IConvertible
1185 //{
1186 //}
1187 #endregion
1188
1189 }
1190 }

  ViewVC Help
Powered by ViewVC 1.1.22