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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22