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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22