/[RomCheater]/trunk/RomCheater/Docking/FloatingMemorySearcher.cs
ViewVC logotype

Annotation of /trunk/RomCheater/Docking/FloatingMemorySearcher.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 569 - (hide annotations) (download)
Thu Jun 6 06:44:39 2013 UTC (7 years, 4 months ago) by william
File size: 150208 byte(s)

1 william 414 #region Logging Defines
2     // include this any class or method that required logging, and comment-out what is not needed
3 william 415
4 william 414 #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     //#define USE_AUTOMATIC_MEMORY_SEARCH_RANGE // when defined will automatically choose the best starting address and size for memory search otherwise it will use the constants defined in MemorySizeConstants
15 william 289 #if !USE_AUTOMATIC_MEMORY_SEARCH_RANGE
16 william 287 #define FORCE_USE_OF_MEMORYSIZECONSTANTS // when defined wil force the use of the constants defined in MemorySizeConstants for memory search range
17 william 289 #endif
18 william 452 #define DONOT_HAVE_RANGED_SEARCH_SUPPORT // when defined, indicates that ranged searches have not been implemented
19 william 280 #define INCREASE_NUMBER_OF_RESULTS_BEFORE_DISPLAY // when defined will set MIN RESULTS to 0x2701 otherwise 0x03e8
20     //#define DO_NOT_SUSPEND_RESUME_THREAD_ON_FREEZE // when defined will not freeze/resume thread on freeze
21 william 229 using System;
22 william 227 using System.Collections.Generic;
23     using System.ComponentModel;
24     using System.Data;
25     using System.Drawing;
26     using System.Linq;
27     using System.Text;
28     using System.Windows.Forms;
29     using WeifenLuo.WinFormsUI.Docking;
30     using RomCheater.PluginFramework.Interfaces;
31     using System.Diagnostics;
32 william 229 using RomCheater.Docking.MemorySearch;
33     using libWin32.Win32.Threading;
34 william 230 using System.Threading;
35     using RomCheater.Logging;
36     using System.IO;
37 william 242 using Sojaner.MemoryScanner.MemoryProviers;
38 william 275 using RomCheater.PluginFramework.Events;
39 william 287 using System.Reflection;
40 william 292 using Sojaner.MemoryScanner;
41 william 399 using System.Collections;
42 william 401 using RomCheater.Serialization;
43 william 511 using RomCheater.Core;
44 william 227
45     namespace RomCheater.Docking
46     {
47 william 228 public partial class FloatingMemorySearcher : DockContent,
48 william 243 IAcceptsPlugin<IConfigPlugin>,
49 william 477 IAcceptsProcess<Process>,
50 william 243 IAcceptsProcessAndConfig,
51 william 275 ISearchInProgress,
52 william 287 IAcceptsBrowseMemoryRegion,
53     IAcceptsMemoryRange
54 william 227 {
55 william 280 #if INCREASE_NUMBER_OF_RESULTS_BEFORE_DISPLAY
56     const int MIN_NUMBER_OF_RESULTS_BEFORE_DISPLAY = 0x2701; // 10,000 results
57     #else
58     const int MIN_NUMBER_OF_RESULTS_BEFORE_DISPLAY = 0x03e8; // 1,000 results
59     #endif
60    
61 william 566 const uint MEM_1B = 0x1;
62 william 565 const uint MEM_1KB = 0x400;
63     const uint MEM_1MB = MEM_1KB * MEM_1KB; // 1024 * 1024 = 1MB
64 william 566 const uint MEM_CHUNK_SIZE_MB = MEM_1MB * 64; /* MEM_1MB * N = N in megabytes (in bytes) */
65 william 565 const uint MEM_CHUNK_SIZE_KB = MEM_1KB * 64; /* MEM_1KB * N = N in kilobytes (in bytes) */
66 william 566 const uint MEM_CHUNK_SIZE_B = MEM_1B * 64; /* MEM_1B * N = N in bytes (in bytes) - duh! a little redundant, don't you think */
67 william 546
68 william 566 const uint MEM_CHUNK_SIZE = MEM_CHUNK_SIZE_MB;
69 william 565
70    
71 william 452 const bool USE_OLD_SEARCH_RESULTS_COMPRATOR_CODE = false;
72 william 457 const bool USE_NONRANGE_SEARCH_RESULT_READER = false;
73 william 229 private bool DefaultUnsignedState = true; // set unsigned to true
74 william 477 public FloatingMemorySearcher() { InitializeComponent(); this.AcceptedPlugin = null; OnBrowseMemoryRegion = null; this.AcceptedProcess = null; SearchInProgess = false; Reload(); }
75 william 227 public FloatingMemorySearcher(IConfigPlugin config) : this() { this.AcceptedPlugin = config; }
76 william 477 public FloatingMemorySearcher(IConfigPlugin config, Process process) : this() { this.AcceptedPlugin = config; this.AcceptedProcess = process; }
77 william 227
78 william 462 //new Action<int, string>(UpdateProgress) to use this as a delegate
79 william 511
80     private void thread_UpdateProgress(object o)
81 william 461 {
82 william 511 IProgressMessage pm = (o as IProgressMessage);
83     if (pm == null) { return; }
84     resultsprogress.Value = pm.Progress;
85 william 517 resultsprogress.Message = pm.Message;
86 william 534 //Application.DoEvents();
87 william 461 }
88    
89 william 532 private bool ShouldCancelAnySearchOperation() { return SearchWorkerThread.CancellationPending; }
90    
91 william 511 private void UpdateProgress(int progress, string message)
92 william 532 {
93 william 511 // detach the progress update from execution within the current thread (so it does not block a cpu-intensive or long running sequence)
94     Thread t = new Thread(new ParameterizedThreadStart(thread_UpdateProgress));
95 william 517 t.SetApartmentState(ApartmentState.STA);
96 william 511 t.Start(new ProgressMessage(progress, message));
97 william 517 //resultsprogress.Value = progress;
98     //resultsprogress.Message = message;
99 william 511 }
100    
101 william 237 #region IAcceptsProcess<Process> Members
102 william 477 private Process _AcceptedProcess;
103     public Process AcceptedProcess { get { return _AcceptedProcess; } set { _AcceptedProcess = value; UpdateAcceptedProcess(value); } }
104 william 227 #endregion
105     #region IAcceptsPlugin<IConfigPlugin> Members
106 william 243 private IConfigPlugin _AcceptedPlugin;
107 william 305 public IConfigPlugin AcceptedPlugin { get { return _AcceptedPlugin; } set { _AcceptedPlugin = value; UpdateAcceptedPlugin(value); } }
108 william 227 #endregion
109 william 275 #region IAcceptsBrowseMemoryRegion members
110 william 287 public event BaseEventHandler<BrowseMemoryRegionEvent> OnBrowseMemoryRegion;
111 william 275 #endregion
112    
113 william 305 private void UpdateAcceptedPlugin(IConfigPlugin config)
114 william 243 {
115     this.lstResults.AcceptedPlugin = config;
116     this.lstPatchList.AcceptedPlugin = config;
117 william 389 if (config != null)
118     {
119     MemoryRangeStart = AcceptedPlugin.MemoryRangeStart;
120     MemoryRangeSize = AcceptedPlugin.MemoryRangeStart + AcceptedPlugin.MemoryRangeSize;
121     }
122 william 305 }
123 william 477 private void UpdateAcceptedProcess(Process process)
124     {
125     this.lstResults.AcceptedProcess = process;
126     this.lstPatchList.AcceptedProcess = process;
127 william 287 #if USE_AUTOMATIC_MEMORY_SEARCH_RANGE && FORCE_USE_OF_MEMORYSIZECONSTANTS
128     logger.Warn.WriteLine("FloatingMemorySearcher.UpdateAcceptedProcessAndConfig(IConfigPlugin config, Process process):");
129     logger.Warn.WriteLine("Both USE_AUTOMATIC_MEMORY_SEARCH_RANGE and FORCE_USE_OF_MEMORYSIZECONSTANTS are defined");
130     logger.Warn.WriteLine("FORCE_USE_OF_MEMORYSIZECONSTANTS will take precedence and will ignore the values supplied in the memeory search range");
131 william 289 #endif
132     #if FORCE_USE_OF_MEMORYSIZECONSTANTS
133     // force use of MemorySizeConstants
134 william 349 txtMemoryRangeStart.Value = MemorySizeConstants.MinimumSearchAddress;
135 william 401 txtMemoryRangeSize.Value = MemorySizeConstants.MinimumSearchAddress + MemorySizeConstants.MaximumSearchSize;
136 william 287 #endif
137     #if USE_AUTOMATIC_MEMORY_SEARCH_RANGE && !FORCE_USE_OF_MEMORYSIZECONSTANTS
138 william 318 ////// code to automatically choose the best starting memory address and size
139     //if (process != null)
140     //{
141     // string filename = process.MainModule.FileName;
142     // //string filename = @"c:\Windows\notepad.exe";
143     // PEReader peReader = new PEReader(filename);
144     //}
145     //else
146     //{
147 william 389 //txtMemoryRangeStart.Value = MemorySizeConstants.MinimumSearchAddress;
148     //txtMemoryRangeSize.Value = MemorySizeConstants.MinimumSearchAddress + MemorySizeConstants.MaximumSearchSize;
149 william 318 //}
150 william 389 if (AcceptedPlugin != null)
151     {
152     MemoryRangeStart = AcceptedPlugin.MemoryRangeStart;
153     MemoryRangeSize = AcceptedPlugin.MemoryRangeStart + AcceptedPlugin.MemoryRangeSize;
154     }
155    
156 william 287 #endif
157    
158 william 243 }
159 william 228 #region ISearchInProgress members
160 william 399 private bool _SearchInProgess;
161     public bool SearchInProgess
162     {
163     get { return _SearchInProgess; }
164     private set
165     {
166     _SearchInProgess = value;
167     if (this.AcceptedPlugin != null)
168     this.AcceptedPlugin.SetMemorySearchReference(this);
169     }
170     }
171 william 444 private Guid _SearchGuid;
172     public Guid SearchGuid
173     {
174     get { return _SearchGuid; }
175     private set { _SearchGuid = value; }
176     }
177 william 228 #endregion
178 william 229
179 william 287 #region IAcceptsMemoryRange
180     #if !FORCE_USE_OF_MEMORYSIZECONSTANTS
181 william 389 private uint _MemoryRangeStart;
182 william 287 private uint _MemoryRangeSize;
183     #endif
184 william 378 public uint MemoryRangeStart
185 william 287 {
186     get
187     {
188     #if FORCE_USE_OF_MEMORYSIZECONSTANTS
189 william 349 return MemorySizeConstants.MinimumSearchAddress;
190 william 287 #else
191     return _MemoryRangeStart;
192     #endif
193     }
194     set
195     {
196     #if !FORCE_USE_OF_MEMORYSIZECONSTANTS
197     _MemoryRangeStart = value;
198 william 389 txtMemoryRangeStart.Value = value;
199 william 287 #endif
200     }
201     }
202     public uint MemoryRangeSize
203     {
204     get
205     {
206     #if FORCE_USE_OF_MEMORYSIZECONSTANTS
207 william 401 return MemorySizeConstants.MinimumSearchAddress + MemorySizeConstants.MaximumSearchSize;
208 william 287 #else
209     return _MemoryRangeSize;
210     #endif
211     }
212     set
213     {
214     #if !FORCE_USE_OF_MEMORYSIZECONSTANTS
215     _MemoryRangeSize = value;
216 william 389 txtMemoryRangeSize.Value = value;
217 william 287 #endif
218     }
219     }
220     #endregion
221 william 229
222     public void Reload()
223     {
224     chkUnsigned.Checked = DefaultUnsignedState;
225     radio_8bits.Checked = true;
226     radiocompare_equal.Checked = true;
227     radio_oldvalue.Checked = true;
228 william 230 chkRefreshResults.Checked = true;
229 william 229 }
230 william 230 public enum eListViewResults
231     {
232     SEARCH_RESULTS_LIST = 0x3000,
233     PATCH_RESULTS_LIST = 0x3001,
234     UKNOWN_RESULTS_LIST = 0x3001
235     }
236 william 280 bool IsFirstSearch = true;
237 william 277 SearchType SearchArgs;
238 william 272 static int col_Found_Address = 1;
239     static int col_Found_Value = 2;
240 william 419 //static int col_Found_Frozen = 3; /* unused */
241 william 272 static int col_Added_Address = 1;
242     static int col_Added_Value = 2;
243 william 419 //static int col_Added_Frozen = 3; /* unused */
244 william 230 List<ListViewItem> ResultItems = new List<ListViewItem>();
245     List<ListViewItem> AddedItems = new List<ListViewItem>();
246     private bool _PatchedValue_NeedsUpdate;
247     bool PatchedValue_NeedsUpdate
248     {
249     get { if (_PatchedValue_NeedsUpdate) this.ThawResultsUpdate(); return _PatchedValue_NeedsUpdate; }
250     set { _PatchedValue_NeedsUpdate = value; if (value) this.ThawResultsUpdate(); }
251     }
252     private delegate ListViewItem ThreadSafe_GetResultItem(int index, int lv_type);
253     private ListViewItem GetResultItem(int index, int lv_type)
254     {
255     try
256     {
257     AddressValuePairList lv = null;
258     switch (lv_type)
259     {
260     case (int)eListViewResults.SEARCH_RESULTS_LIST: lv = lstResults; break;
261     case (int)eListViewResults.PATCH_RESULTS_LIST: lv = lstPatchList; break;
262     default: throw new IndexOutOfRangeException("Detected: " + Enum.GetName(typeof(eListViewResults), eListViewResults.UKNOWN_RESULTS_LIST) + " with value: " + lv_type.ToString("x4"));
263     }
264     ListViewItem item = new ListViewItem();
265     item = (ListViewItem)lv.Items[index].Clone();
266     return item;
267     }
268     catch (Exception)
269     {
270     return null;
271     }
272     }
273 william 229 private void radiocompare_equal_CheckedChanged(object sender, EventArgs e)
274     {
275     //if (!radiocompare_between.Checked && !radiocompare_notbetween.Checked)
276     //{
277     if (radio_oldvalue.Checked)
278     {
279     txtStartAddr.ReadOnly = true;
280     txtEndAddr.ReadOnly = true;
281     }
282     if (radio_specificvalue.Checked)
283     {
284     txtStartAddr.ReadOnly = false;
285     txtEndAddr.ReadOnly = true;
286     }
287     //}
288     }
289    
290     private void radiocompare_between_CheckedChanged(object sender, EventArgs e)
291     {
292     if (!radiocompare_equal.Checked &&
293     !radiocompare_greaterthan.Checked &&
294     !radiocompare_greaterthan.Checked &&
295     !radiocompare_lessthan.Checked &&
296     !radiocompare_greaterthan_orequal.Checked &&
297     !radiocompare_lessthan_orequal.Checked &&
298     !radiocompare_notequal.Checked)
299     if (radiocompare_between.Checked)
300     {
301     txtStartAddr.ReadOnly = false;
302     txtEndAddr.ReadOnly = false;
303     return;
304     }
305     if (!radiocompare_between.Checked && !radiocompare_notbetween.Checked)
306     {
307     if (radio_oldvalue.Checked)
308     {
309     txtStartAddr.ReadOnly = true;
310     txtEndAddr.ReadOnly = true;
311     }
312     if (radio_specificvalue.Checked)
313     {
314     txtStartAddr.ReadOnly = false;
315     txtEndAddr.ReadOnly = true;
316     }
317     }
318     }
319    
320     private void radiocompare_notbetween_CheckedChanged(object sender, EventArgs e)
321     {
322     if (!radiocompare_equal.Checked &&
323     !radiocompare_greaterthan.Checked &&
324     !radiocompare_greaterthan.Checked &&
325     !radiocompare_lessthan.Checked &&
326     !radiocompare_greaterthan_orequal.Checked &&
327     !radiocompare_lessthan_orequal.Checked &&
328     !radiocompare_notequal.Checked)
329     if (radiocompare_notbetween.Checked)
330     {
331     txtStartAddr.ReadOnly = false;
332     txtEndAddr.ReadOnly = false;
333     return;
334     }
335     if (!radiocompare_between.Checked && !radiocompare_notbetween.Checked)
336     {
337     if (radio_oldvalue.Checked)
338     {
339     txtStartAddr.ReadOnly = true;
340     txtEndAddr.ReadOnly = true;
341     }
342     if (radio_specificvalue.Checked)
343     {
344     txtStartAddr.ReadOnly = false;
345     txtEndAddr.ReadOnly = true;
346     }
347     }
348     }
349    
350     private void radio_8bits_CheckedChanged(object sender, EventArgs e)
351     {
352     if (chkUnsigned.Checked)
353     {
354     txtStartAddr.CreateTypeSize<byte>();
355     txtEndAddr.CreateTypeSize<byte>();
356     }
357     else
358     {
359     txtStartAddr.CreateTypeSize<sbyte>();
360     txtEndAddr.CreateTypeSize<sbyte>();
361     }
362     }
363    
364     private void radio_16bits_CheckedChanged(object sender, EventArgs e)
365     {
366     if (chkUnsigned.Checked)
367     {
368     txtStartAddr.CreateTypeSize<ushort>();
369     txtEndAddr.CreateTypeSize<ushort>();
370     }
371     else
372     {
373     txtStartAddr.CreateTypeSize<short>();
374     txtEndAddr.CreateTypeSize<short>();
375     }
376     }
377    
378     private void radio_32bits_CheckedChanged(object sender, EventArgs e)
379     {
380    
381     if (chkUnsigned.Checked)
382     {
383     txtStartAddr.CreateTypeSize<uint>();
384     txtEndAddr.CreateTypeSize<uint>();
385     }
386     else
387     {
388     txtStartAddr.CreateTypeSize<int>();
389     txtEndAddr.CreateTypeSize<int>();
390     }
391     }
392    
393     private void radio_64bits_CheckedChanged(object sender, EventArgs e)
394     {
395    
396     if (chkUnsigned.Checked)
397     {
398     txtStartAddr.CreateTypeSize<ulong>();
399     txtEndAddr.CreateTypeSize<ulong>();
400     }
401     else
402     {
403     txtStartAddr.CreateTypeSize<long>();
404     txtEndAddr.CreateTypeSize<long>();
405     }
406     }
407    
408     private void radio_oldvalue_CheckedChanged(object sender, EventArgs e)
409     {
410     if (!radiocompare_between.Checked && !radiocompare_notbetween.Checked)
411     {
412     txtStartAddr.ReadOnly = true;
413     txtEndAddr.ReadOnly = true;
414     }
415     }
416    
417     private void radio_specificvalue_CheckedChanged(object sender, EventArgs e)
418     {
419     if (!radiocompare_between.Checked && !radiocompare_notbetween.Checked)
420     {
421     txtStartAddr.ReadOnly = false;
422     txtEndAddr.ReadOnly = true;
423     }
424     }
425    
426     private void chkRefreshResults_CheckedChanged(object sender, EventArgs e)
427     {
428     if (chkRefreshResults.Checked)
429     {
430     timer_update_results.Enabled = true;
431     }
432     else
433     {
434     timer_update_results.Enabled = false;
435     ResultsUpdateWorkerThread.CancelAsync();
436     }
437     }
438    
439     private void timer_update_results_Tick(object sender, EventArgs e)
440     {
441     if (chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
442     {
443     ResultsUpdateWorkerThread.RunWorkerAsync();
444     }
445     }
446 william 230 private bool ShouldUpdateResults()
447     {
448 william 477 if (this.AcceptedProcess == null) return false;
449 william 230 if (SearchWorkerThread.IsBusy) return false;
450     //if (JokerSearchWorker.IsBusy) return false;
451     if (this.IsResultsUpdateFrozen) return false;
452     if (mnuAddedResults.Visible) return false;
453     if (mnuResults.Visible) return false;
454 william 477 if (Process.GetProcessById(this.AcceptedProcess.Id) == null) return false;
455 william 230 if (lstResults.Items.Count > 0) return true;
456     if (lstPatchList.Items.Count > 0) return true;
457     return false;
458     }
459 william 229 private void ResultsUpdateWorkerThread_DoWork(object sender, DoWorkEventArgs e)
460     {
461 william 230 Thread.Sleep(250); // keep thread from blocking
462     if (!this.ShouldUpdateResults()) return;
463     ////if (SearchArgs == null) return;
464     ////if (this.IsResultsUpdateFrozen) return;
465     ////// put thread to sleep for 500ms
466     ////System.Threading.Thread.Sleep(500);
467     //PCSX2MemoryProvider provider = new PCSX2MemoryProvider(this.SearchPCSX2ProcessPID, resultslog);
468     //byte[] buffered_mem = provider.GetMemory();
469     //MemoryStream ms = new MemoryStream(buffered_mem);
470     //BinaryReader r_ms = new BinaryReader(ms);
471 william 229
472 william 230 #region Update Results List
473     ResultItems = new List<ListViewItem>();
474     //r_ms.BaseStream.Seek(0, SeekOrigin.Begin);
475     for (int i = 0; i < lstResults.Items.Count; i++)
476     {
477     if (this.lstResults.InvokeRequired)
478     {
479     ThreadSafe_GetResultItem _get_item = new ThreadSafe_GetResultItem(GetResultItem);
480     object item = this.lstResults.Invoke(_get_item, new object[] { i, (int)eListViewResults.SEARCH_RESULTS_LIST });
481     if (item != null)
482     ResultItems.Add((ListViewItem)item);
483     }
484     else
485     {
486     ResultItems.Add(lstResults.Items[i]);
487     }
488    
489     }
490     for (int i = 0; i < ResultItems.Count; i++)
491     {
492     if (ResultsUpdateWorkerThread.CancellationPending == true)
493     {
494     e.Cancel = true;
495     return;
496     }
497 william 249 int Address = 0;
498 william 230 ResultDataType _result = (ResultDataType)ResultItems[i].Tag;
499    
500 william 249 Address = Convert.ToInt32(ResultItems[i].SubItems[col_Found_Address].Text, 16);
501 william 242 //r_ms.BaseStream.Seek(Address, SeekOrigin.Begin);
502 william 398 using (GenericMemoryProvider provider = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
503 william 230 {
504 william 398 provider.OpenProvider();
505     int bytesReadSize;
506     byte[] data;
507     uint bytesToRead = 0;
508     switch (_result.ValueType)
509     {
510     case SearchDataTypes._8bits:
511     bytesToRead = 1;
512     break;
513     case SearchDataTypes._16bits:
514     bytesToRead = 2;
515     break;
516     case SearchDataTypes._32bits:
517     bytesToRead = 4;
518     break;
519     case SearchDataTypes._64bits:
520     bytesToRead = 8;
521     break;
522     }
523     provider.ReadProcessMemory(Address, bytesToRead, out bytesReadSize, out data);
524     using (MemoryStream ms = new MemoryStream(data))
525     {
526     using (BinaryReader r_ms = new BinaryReader(ms))
527     {
528     switch (_result.ValueType)
529     {
530     case SearchDataTypes._8bits:
531     if (_result.IsUnsigned) { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x2}", r_ms.ReadByte()); }
532     else { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x2}", r_ms.ReadSByte()); }
533     break;
534     case SearchDataTypes._16bits:
535     if (_result.IsUnsigned) { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x4}", r_ms.ReadUInt16()); }
536     else { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x4}", r_ms.ReadInt16()); }
537     break;
538     case SearchDataTypes._32bits:
539     if (_result.IsUnsigned) { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x8}", r_ms.ReadUInt32()); }
540     else { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x8}", r_ms.ReadInt32()); }
541     break;
542     case SearchDataTypes._64bits:
543     if (_result.IsUnsigned) { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x16}", r_ms.ReadUInt64()); }
544     else { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x16}", r_ms.ReadInt64()); }
545     break;
546     }
547     r_ms.Close();
548     }
549     }
550 william 424 provider.CloseProvider();
551 william 230 }
552 william 398 //Application.DoEvents();
553 william 230 }
554     #endregion
555    
556     #region Update Added Results List
557     AddedItems = new List<ListViewItem>();
558     //r_ms.BaseStream.Seek(0, SeekOrigin.Begin);
559     for (int i = 0; i < lstPatchList.Items.Count; i++)
560     {
561     if (this.lstResults.InvokeRequired)
562     {
563     ThreadSafe_GetResultItem _get_item = new ThreadSafe_GetResultItem(GetResultItem);
564     object item = this.lstResults.Invoke(_get_item, new object[] { i, (int)eListViewResults.PATCH_RESULTS_LIST });
565     if (item != null)
566     AddedItems.Add((ListViewItem)item);
567     }
568     else
569     {
570     AddedItems.Add(lstPatchList.Items[i]);
571     }
572    
573     }
574     for (int i = 0; i < AddedItems.Count; i++)
575     {
576     if (ResultsUpdateWorkerThread.CancellationPending == true)
577     {
578     e.Cancel = true;
579     return;
580     }
581 william 249 int Address = 0;
582 william 230 ResultDataType _result = (ResultDataType)AddedItems[i].Tag;
583 william 249 Address = Convert.ToInt32(AddedItems[i].SubItems[col_Added_Address].Text, 16);
584 william 398 using (GenericMemoryProvider provider = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
585 william 230 {
586 william 398 provider.OpenProvider();
587     int bytesReadSize;
588     byte[] data;
589     uint bytesToRead = 0;
590     switch (_result.ValueType)
591     {
592     case SearchDataTypes._8bits:
593     bytesToRead = 1;
594     break;
595     case SearchDataTypes._16bits:
596     bytesToRead = 2;
597     break;
598     case SearchDataTypes._32bits:
599     bytesToRead = 4;
600     break;
601     case SearchDataTypes._64bits:
602     bytesToRead = 8;
603     break;
604     }
605     provider.ReadProcessMemory(Address, bytesToRead, out bytesReadSize, out data);
606 william 424 provider.CloseProvider();
607 william 398 using (MemoryStream ms = new MemoryStream(data))
608     {
609     using (BinaryReader r_ms = new BinaryReader(ms))
610     {
611     switch (_result.ValueType)
612     {
613     case SearchDataTypes._8bits:
614     if (_result.IsUnsigned) { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x2}", r_ms.ReadByte()); }
615     else { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x2}", r_ms.ReadSByte()); }
616     break;
617     case SearchDataTypes._16bits:
618     if (_result.IsUnsigned) { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x4}", r_ms.ReadUInt16()); }
619     else { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x4}", r_ms.ReadInt16()); }
620     break;
621     case SearchDataTypes._32bits:
622     if (_result.IsUnsigned) { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x8}", r_ms.ReadUInt32()); }
623     else { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x8}", r_ms.ReadInt32()); }
624     break;
625     case SearchDataTypes._64bits:
626     if (_result.IsUnsigned) { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x16}", r_ms.ReadUInt64()); }
627     else { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x16}", r_ms.ReadInt64()); }
628     break;
629     }
630     r_ms.Close();
631     }
632     }
633 william 230 }
634 william 398 //Application.DoEvents();
635 william 230 }
636     #endregion
637    
638    
639 william 229 }
640    
641     private void ResultsUpdateWorkerThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
642     {
643 william 230 try
644     {
645     //if ((lstResults.SelectedItems.Count > 0) && !PatchedValue_NeedsUpdate ) return;
646     //if ((lstPatchList.SelectedItems.Count > 0) && !PatchedValue_NeedsUpdate) return;
647     if (!this.ShouldUpdateResults()) return;
648     if (ResultItems.Count > 0)
649     {
650     //lstResults.Items.Clear();
651     //lstResults.Items.AddRange(ResultItems.ToArray());
652 william 229
653 william 230 for (int i = 0; i < ResultItems.Count; i++)
654     {
655     lstResults.Items[i].SubItems[new AVPColumnText(AVPColumnType.VALUE).ColumnIndex].Text =
656     ResultItems[i].SubItems[new AVPColumnText(AVPColumnType.VALUE).ColumnIndex].Text;
657     }
658    
659     }
660     if (AddedItems.Count > 0)
661     {
662     //lstPatchList.Items.Clear();
663     //lstPatchList.Items.AddRange(AddedItems.ToArray());
664    
665     for (int i = 0; i < AddedItems.Count; i++)
666     {
667     lstPatchList.Items[i].SubItems[new AVPColumnText(AVPColumnType.VALUE).ColumnIndex].Text =
668     AddedItems[i].SubItems[new AVPColumnText(AVPColumnType.VALUE).ColumnIndex].Text;
669     }
670    
671     }
672     PatchedValue_NeedsUpdate = false;
673     }
674     catch { }
675 william 229 }
676    
677     private void btnImportFile_Click(object sender, EventArgs e)
678     {
679     this.FreezeResultsUpdate();
680     if (!lstPatchList.ImportFromFile())
681     {
682     MessageBox.Show("Failed to Import Patch List from File.", "Import Failure", MessageBoxButtons.OK, MessageBoxIcon.Error);
683     this.ThawResultsUpdate();
684     return;
685     }
686     else
687     {
688     MessageBox.Show("Succesfully Imported Patch List from File.", "Import Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
689     this.ThawResultsUpdate();
690     return;
691     }
692     }
693     bool g_isFrozen = false;
694     private bool IsResultsUpdateFrozen
695     {
696     get { return g_isFrozen; }
697     set { g_isFrozen = value; }
698     }
699     private void ThawResultsUpdate()
700     {
701     this.IsResultsUpdateFrozen = false;
702 william 477 if (this.AcceptedProcess != null)
703 william 229 {
704     #if !DO_NOT_SUSPEND_RESUME_THREAD_ON_FREEZE
705 william 477 ThreadControl.ResumeProcess(this.AcceptedProcess.Id);
706 william 229 #endif
707     }
708     }
709    
710     private void FreezeResultsUpdate()
711     {
712     this.IsResultsUpdateFrozen = true;
713 william 285 //this.IsResultsUpdateFrozen = false;
714 william 477 if (this.AcceptedProcess != null)
715 william 229 {
716     #if !DO_NOT_SUSPEND_RESUME_THREAD_ON_FREEZE
717 william 477 ThreadControl.SuspendProcess(this.AcceptedProcess.Id);
718 william 229 #endif
719     }
720     }
721    
722     private void btnExportFile_Click(object sender, EventArgs e)
723     {
724     this.FreezeResultsUpdate();
725     if (!lstPatchList.ExportToFile())
726     {
727     MessageBox.Show("Failed to Export Patch List to File.", "Export Failure", MessageBoxButtons.OK, MessageBoxIcon.Error);
728     this.ThawResultsUpdate();
729     return;
730     }
731     else
732     {
733     MessageBox.Show("Succesfully Exported Patch List to File.", "Export Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
734     this.ThawResultsUpdate();
735     return;
736     }
737     }
738    
739     private void btnImportClipboard_Click(object sender, EventArgs e)
740     {
741     this.FreezeResultsUpdate();
742     if (!lstPatchList.ImportFromClipboard())
743     {
744     MessageBox.Show("Failed to Import Patch List from Clipboard.", "Import Failure", MessageBoxButtons.OK, MessageBoxIcon.Error);
745     this.ThawResultsUpdate();
746     return;
747     }
748     else
749     {
750     MessageBox.Show("Succesfully Import Patch List from Clipboard.", "Import Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
751     this.ThawResultsUpdate();
752     }
753     }
754    
755     private void btnExportClipboard_Click(object sender, EventArgs e)
756     {
757     this.FreezeResultsUpdate();
758     if (!lstPatchList.ExportToClipboard())
759     {
760     MessageBox.Show("Failed to Export Patch List to Clipboard.", "Export Failure", MessageBoxButtons.OK, MessageBoxIcon.Error);
761     this.ThawResultsUpdate();
762     return;
763     }
764     else
765     {
766     MessageBox.Show("Succesfully Exported Patch List to Clipboard.", "Export Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
767     this.ThawResultsUpdate();
768     return;
769     }
770     }
771    
772     private void btnAddPatchAddress_Click(object sender, EventArgs e)
773     {
774 william 245 PatchAdder adder = new PatchAdder((IAcceptsProcessAndConfig)this);
775 william 229 adder.ShowDialog();
776     if (adder.WasAPatchAdded) AddToPatchList(adder.AddedPatchValue);
777     }
778    
779     private void btnAddAddressRange_Click(object sender, EventArgs e)
780     {
781 william 245 PatchRangeAdder adder = new PatchRangeAdder((IAcceptsProcessAndConfig)this);
782 william 229 adder.ShowDialog();
783     if (adder.WasAPatchAdded) AddToPatchList(adder.AddedPatchValue);
784     }
785     private void AddToPatchList(List<ResultDataType> item) { foreach (ResultDataType data in item) { AddToPatchList(data); } }
786     private void AddToPatchList(ResultDataType item)
787     {
788     ResultItem item2 = null;
789     switch (item.ValueType)
790     {
791     case SearchDataTypes._8bits:
792     if (item.IsUnsigned) { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToByte(item.Value)); }
793     else { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToSByte(item.Value)); }
794     break;
795     case SearchDataTypes._16bits:
796     if (item.IsUnsigned) { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToUInt16(item.Value)); }
797     else { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToInt16(item.Value)); }
798     break;
799     case SearchDataTypes._32bits:
800     if (item.IsUnsigned) { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToUInt32(item.Value)); }
801     else { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToInt32(item.Value)); }
802     break;
803     case SearchDataTypes._64bits:
804     if (item.IsUnsigned) { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToUInt64(item.Value)); }
805     else { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToInt64(item.Value)); }
806     break;
807     }
808     this.AddToPatchList(item2);
809     }
810     private void AddToPatchList(ListViewItem item)
811     {
812     try
813     {
814     ResultDataType _result = (ResultDataType)item.Tag;
815     this.AddToPatchList(_result);
816     }
817     catch (InvalidCastException ex)
818     {
819     // unable to cast
820     MessageBox.Show(ex.Message, "Invalid Cast Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
821     }
822     catch (Exception ex)
823     {
824     // other exception
825     MessageBox.Show(ex.Message, "Unhandled Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
826     }
827     }
828     private void AddToPatchList(ResultItem item)
829     {
830     if (!lstPatchList.Items.Contains(item)) lstPatchList.Items.Add(item);
831     }
832     private void AddToPatchList(string address, SearchDataTypes bitsize, bool IsUnsigned)
833     {
834 william 245 ResultItemState state = new ResultItemState(address, bitsize, IsUnsigned, (IAcceptsProcessAndConfig)this);
835 william 229 ResultItem item = new ResultItem(state.Address, state.Value, state.Frozen, state.ValueType, state.IsUnsigned);
836     this.AddToPatchList(item);
837     }
838 william 230
839     private void mnuItemAddToPatchList_Click(object sender, EventArgs e)
840     {
841     if (!(lstResults.SelectedItems.Count > 0)) return;
842     //if (SearchArgs == null) return;
843    
844     try
845     {
846     for (int i = 0; i < lstResults.SelectedIndices.Count; i++)
847     {
848     //ResultDataType result = (ResultDataType)lstResults.Items[selected_index].Tag;
849     ListViewItem item = lstResults.Items[lstResults.SelectedIndices[i]];
850     this.AddToPatchList(item);
851     }
852     }
853     catch (Exception ex)
854     {
855     logger.Error.WriteLine(ex.ToString());
856     }
857     }
858    
859     private void mnuItemRemoveResult_Click(object sender, EventArgs e)
860     {
861     if (!(lstPatchList.SelectedItems.Count > 0)) return;
862     //if (SearchArgs == null) return;
863     try
864     {
865     this.FreezeResultsUpdate();
866     for (int i = 0; i < lstPatchList.SelectedIndices.Count; i++)
867     {
868     //lstPatchList.ThawItem(lstPatchList.SelectedIndices[i]);
869     lstPatchList.Items[lstPatchList.SelectedIndices[i]].Remove();
870     }
871     this.ThawResultsUpdate();
872     }
873     catch (Exception ex)
874     {
875     Debug.WriteLine(ex.ToString());
876     }
877     }
878     private void PatchRange(bool SingleEntry)
879     {
880     //if (SearchArgs == null) return;
881     #region Patch Selected Address
882     // stop ResultsUpdate Thread
883     ResultsUpdateWorkerThread.CancelAsync();
884    
885     List<ResultDataType> patch_list = new List<ResultDataType>();
886     List<int> SelectedIndexes = new List<int>();
887    
888     if (SingleEntry)
889     {
890     SelectedIndexes.Add(lstPatchList.SelectedIndices[0]);
891     }
892     else
893     {
894     foreach (int index in lstPatchList.SelectedIndices)
895     {
896     SelectedIndexes.Add(index);
897     }
898     }
899     //PCSX2MemoryProvider provider = new PCSX2MemoryProvider(this.SearchPCSX2ProcessPID, resultslog);
900     foreach (int index in SelectedIndexes)
901     {
902     if (SingleEntry)
903     {
904     SearchPatcher patcher = null;
905     uint Address = 0;
906     ListViewItem item = lstPatchList.Items[index];
907     ResultDataType _result = (ResultDataType)item.Tag;
908     Address = Convert.ToUInt32(item.SubItems[col_Found_Address].Text, 16);
909     switch (_result.ValueType)
910     {
911     case SearchDataTypes._8bits:
912     if (_result.IsUnsigned)
913     {
914     byte value = Convert.ToByte(item.SubItems[col_Found_Value].Text, 16);
915 william 243 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
916 william 230 timer_update_results.Enabled = false;
917     patcher.ShowDialog();
918     timer_update_results.Enabled = true;
919     PatchedValue_NeedsUpdate = true;
920     if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
921     ResultsUpdateWorkerThread.RunWorkerAsync();
922     }
923     else
924     {
925     sbyte value = Convert.ToSByte(item.SubItems[col_Found_Value].Text, 16);
926 william 243 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
927 william 230 timer_update_results.Enabled = false;
928     patcher.ShowDialog();
929     timer_update_results.Enabled = true;
930     PatchedValue_NeedsUpdate = true;
931     if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
932     ResultsUpdateWorkerThread.RunWorkerAsync();
933     }
934     break;
935     case SearchDataTypes._16bits:
936     if (_result.IsUnsigned)
937     {
938     ushort value = Convert.ToUInt16(item.SubItems[col_Found_Value].Text, 16);
939 william 243 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
940 william 230 timer_update_results.Enabled = false;
941     patcher.ShowDialog();
942     timer_update_results.Enabled = true;
943     PatchedValue_NeedsUpdate = true;
944     if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
945     ResultsUpdateWorkerThread.RunWorkerAsync();
946     }
947     else
948     {
949     short value = Convert.ToInt16(item.SubItems[col_Found_Value].Text, 16);
950 william 243 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
951 william 230 timer_update_results.Enabled = false;
952     patcher.ShowDialog();
953     timer_update_results.Enabled = true;
954     PatchedValue_NeedsUpdate = true;
955     if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
956     ResultsUpdateWorkerThread.RunWorkerAsync();
957     }
958     break;
959     case SearchDataTypes._32bits:
960     if (_result.IsUnsigned)
961     {
962     uint value = Convert.ToUInt32(item.SubItems[col_Found_Value].Text, 16);
963 william 243 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
964 william 230 timer_update_results.Enabled = false;
965     patcher.ShowDialog();
966     timer_update_results.Enabled = true;
967     PatchedValue_NeedsUpdate = true;
968     if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
969     ResultsUpdateWorkerThread.RunWorkerAsync();
970     }
971     else
972     {
973     int value = Convert.ToInt32(item.SubItems[col_Found_Value].Text, 16);
974 william 243 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
975 william 230 timer_update_results.Enabled = false;
976     patcher.ShowDialog();
977     timer_update_results.Enabled = true;
978     PatchedValue_NeedsUpdate = true;
979     if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
980     ResultsUpdateWorkerThread.RunWorkerAsync();
981     }
982     break;
983     case SearchDataTypes._64bits:
984     if (_result.IsUnsigned)
985     {
986     ulong value = Convert.ToUInt32(item.SubItems[col_Found_Value].Text, 16);
987 william 243 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
988 william 230 timer_update_results.Enabled = false;
989     patcher.ShowDialog();
990     timer_update_results.Enabled = true;
991     PatchedValue_NeedsUpdate = true;
992     if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
993     ResultsUpdateWorkerThread.RunWorkerAsync();
994     }
995     else
996     {
997     long value = Convert.ToInt32(item.SubItems[col_Found_Value].Text, 16);
998 william 243 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
999 william 230 timer_update_results.Enabled = false;
1000     patcher.ShowDialog();
1001     timer_update_results.Enabled = true;
1002     PatchedValue_NeedsUpdate = true;
1003     if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
1004     ResultsUpdateWorkerThread.RunWorkerAsync();
1005     }
1006     break;
1007     }
1008     }
1009     else
1010     {
1011    
1012     ListViewItem item = lstPatchList.Items[index];
1013     ResultDataType _result = (ResultDataType)item.Tag;
1014     patch_list.Add(_result);
1015     }
1016     }
1017    
1018     if (patch_list.Count > 0)
1019     {
1020 william 243 SearchRangePatcher rangePatcher = new SearchRangePatcher((IAcceptsProcessAndConfig)this, patch_list);
1021 william 230 rangePatcher.ShowDialog();
1022     }
1023    
1024     #endregion
1025     }
1026     private void mnuItemPatchSelectedEntry_Click(object sender, EventArgs e)
1027     {
1028     if (!(lstPatchList.SelectedItems.Count == 1)) return;
1029     PatchRange(true);
1030     }
1031    
1032     private void mnuItemPatchSelectedRange_Click(object sender, EventArgs e)
1033     {
1034     if (!(lstPatchList.SelectedItems.Count >= 1)) return;
1035     PatchRange(false);
1036     }
1037    
1038     private void mnuItemFreezeSelectedPatches_Click(object sender, EventArgs e)
1039     {
1040     if (!(lstPatchList.SelectedItems.Count > 0)) return;
1041     //if (SearchArgs == null) return;
1042     try
1043     {
1044 william 477 lstPatchList.ProcessID = this.AcceptedProcess.Id;
1045 william 230 this.FreezeResultsUpdate();
1046     for (int i = 0; i < lstPatchList.SelectedIndices.Count; i++)
1047     {
1048     lstPatchList.FreezeItem(lstPatchList.SelectedIndices[i]);
1049     }
1050     // force thaw and update
1051     this.ThawResultsUpdate();
1052     this.Update();
1053     }
1054     catch (Exception ex)
1055     {
1056     Debug.WriteLine(ex.ToString());
1057     }
1058     }
1059    
1060     private void mnuItemThawSelectedPatches_Click(object sender, EventArgs e)
1061     {
1062     if (!(lstPatchList.SelectedItems.Count > 0)) return;
1063     //if (SearchArgs == null) return;
1064     try
1065     {
1066 william 477 lstPatchList.ProcessID = this.AcceptedProcess.Id;
1067 william 230 this.FreezeResultsUpdate();
1068     for (int i = 0; i < lstPatchList.SelectedIndices.Count; i++)
1069     {
1070     lstPatchList.ThawItem(lstPatchList.SelectedIndices[i]);
1071     }
1072     // force thaw and update
1073     this.ThawResultsUpdate();
1074     this.Update();
1075     }
1076     catch (Exception ex)
1077     {
1078     Debug.WriteLine(ex.ToString());
1079     }
1080     }
1081 william 540
1082 william 230 private void SearchWorkerThread_DoWork(object sender, DoWorkEventArgs e)
1083     {
1084 william 410 try
1085 william 483 {
1086 william 410 Stopwatch st = new Stopwatch();
1087     st.Start();
1088 william 392
1089 william 410 Stopwatch st_first_search = new Stopwatch();
1090     Stopwatch st_nonrange_search = new Stopwatch();
1091     Stopwatch st_ranged_search = new Stopwatch();
1092 william 392
1093 william 410 e.Result = st;
1094     //List<ResultType<object>> tmp_Results = new List<ResultType<object>>();
1095 william 476 List<ResultType<ulong>> second_tmp_Results = new List<ResultType<ulong>>();
1096 william 410 //const int ElementsBeforeDisplay = 100;
1097     SearchArgs.LogSearchOptions();
1098     uint STEP_SIZE = (uint)SearchArgs.DataType / 8;
1099 william 399
1100 william 410 bool unsigned = SearchArgs.IsUnsignedDataType;
1101     SearchDataTypes sdt = SearchArgs.DataType;
1102     //byte[] buffered_mem = new byte[(MemoryRangeSize - MemoryRangeStart)]; // throws OutOfMemoryException if size is over 2G
1103     logger.Debug.WriteLine(string.Format("Buffered Memory Size -> 0x{0:x8}", MemoryRangeSize - MemoryRangeStart));
1104 william 517 UpdateProgress(0, string.Format("Search is Warming Up...Please Wait..."));
1105     //new Action<int, string>(UpdateProgress).Invoke(0, string.Format("Search is Warming Up...Please Wait..."));
1106 william 410 Stopwatch provider_st = new Stopwatch();
1107     provider_st.Start();
1108     using (GenericMemoryProvider provider = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1109     {
1110 william 483
1111 william 459 if (SearchArgs.IsFirstSearch)
1112     {
1113 william 560 #region FirstSearch
1114 william 460 provider.OpenProvider();
1115     int count = (int)((MemoryRangeSize - MemoryRangeStart) / STEP_SIZE);
1116 william 560 SearchResultWriter writer = new SearchResultWriter((int)(count), SearchGuid);
1117 william 525 int Last_Whole_Percent_Done = 0;
1118 william 547 uint size = 0;
1119     switch (sdt)
1120     {
1121 william 565 case SearchDataTypes._8bits: size = (MEM_CHUNK_SIZE / BitTools.SizeOf<uint>(sdt)); break;
1122     case SearchDataTypes._16bits: size = (MEM_CHUNK_SIZE / BitTools.SizeOf<uint>(sdt)); break;
1123     case SearchDataTypes._32bits: size = (MEM_CHUNK_SIZE / BitTools.SizeOf<uint>(sdt)); break;
1124     case SearchDataTypes._64bits: size = (MEM_CHUNK_SIZE / BitTools.SizeOf<uint>(sdt)); break;
1125 william 547 }
1126     //uint size = STEP_SIZE * 4096;
1127 william 560 for (uint i = MemoryRangeStart; i < MemoryRangeSize; i += size)
1128 william 521 {
1129 william 560
1130     int bytesRead = 0;
1131 william 521 byte[] data = new byte[size];
1132     provider.ReadProcessMemoryAtOnce(i, size, out bytesRead, out data);
1133     string message = string.Format(" -> Reading Address: 0x{0:x8}", (i + MemoryRangeStart));
1134     double double_percent_done = 100.0 * (double)((double)i / (double)MemoryRangeSize);
1135 william 525 int int_percent_done = (int)double_percent_done;
1136 william 560 if ((int_percent_done != Last_Whole_Percent_Done))// && (i % 10000) == 0)
1137 william 521 {
1138     UpdateProgress((int)double_percent_done, message);
1139 william 525 Last_Whole_Percent_Done = int_percent_done;
1140 william 521 }
1141    
1142 william 526 uint address_index = i;
1143 william 521 #region comparison and serialization
1144     switch (sdt)
1145     {
1146     case SearchDataTypes._8bits:
1147     if (unsigned)
1148     {
1149     #region 8bits - unsigned
1150 william 534 var t_data = data;
1151 william 521 foreach (var Value in t_data)
1152     {
1153     if (SearchWorkerThread.CancellationPending) { break; }
1154 william 526 using (_8bit_unsigned_comparer_ comparer = new _8bit_unsigned_comparer_(SearchArgs, address_index))
1155 william 521 {
1156     byte value = 0;
1157     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1158     {
1159     using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1160     {
1161     try
1162     {
1163     gmp.OpenProvider();
1164 william 526 gmp.ReadMemory(address_index, out value);
1165 william 521 gmp.CloseProvider();
1166     }
1167     catch (Exception ex)
1168     {
1169     logger.VerboseError.WriteLine(ex.ToString());
1170     }
1171     }
1172     comparer.Value = value;
1173     }
1174     else
1175     {
1176     value = Convert.ToByte(SearchArgs.CompareStartValue);
1177     comparer.Value = value;
1178     }
1179     if (comparer.Compare(Convert.ToByte(Value), value))
1180     {
1181     writer.WriteResult<byte>(comparer.Address, comparer.Value);
1182     }
1183     }
1184 william 526 address_index += STEP_SIZE;
1185 william 521 }
1186     #endregion
1187     }
1188     else
1189     {
1190     #region 8bits - signed
1191 william 534 var t_data = data;
1192 william 521 foreach (var Value in t_data)
1193     {
1194     if (SearchWorkerThread.CancellationPending) { break; }
1195 william 526 using (_8bit_signed_comparer_ comparer = new _8bit_signed_comparer_(SearchArgs, address_index))
1196 william 521 {
1197     sbyte value = 0;
1198     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1199     {
1200     using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1201     {
1202     try
1203     {
1204     gmp.OpenProvider();
1205 william 526 gmp.ReadMemory(address_index, out value);
1206 william 521 gmp.CloseProvider();
1207     }
1208     catch (Exception ex)
1209     {
1210     logger.VerboseError.WriteLine(ex.ToString());
1211     }
1212     }
1213     comparer.Value = value;
1214     }
1215     else
1216     {
1217     value = Convert.ToSByte(SearchArgs.CompareStartValue);
1218     comparer.Value = value;
1219     }
1220     if (comparer.Compare(Convert.ToSByte(Value), value))
1221     {
1222     writer.WriteResult<sbyte>(comparer.Address, comparer.Value);
1223     }
1224     }
1225 william 526 address_index += STEP_SIZE;
1226 william 521 }
1227     #endregion
1228     } break;
1229     case SearchDataTypes._16bits:
1230     if (unsigned)
1231     {
1232     #region 16bits - unsigned
1233 william 532 var t_data = BitTools.ConvertByteArray<ushort>(data, new Func<bool>(ShouldCancelAnySearchOperation));
1234 william 560 foreach (var Value in t_data)
1235     {
1236     if (SearchWorkerThread.CancellationPending) { break; }
1237     using (_16bit_unsigned_comparer_ comparer = new _16bit_unsigned_comparer_(SearchArgs, address_index))
1238     {
1239     ushort value = 0;
1240     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1241     {
1242     using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1243     {
1244     try
1245     {
1246     gmp.OpenProvider();
1247     gmp.ReadMemory(address_index, out value);
1248     gmp.CloseProvider();
1249     }
1250     catch (Exception ex)
1251     {
1252     logger.VerboseError.WriteLine(ex.ToString());
1253     }
1254     }
1255     comparer.Value = value;
1256     }
1257     else
1258     {
1259     value = Convert.ToUInt16(SearchArgs.CompareStartValue);
1260     comparer.Value = value;
1261     }
1262     if (comparer.Compare(Convert.ToUInt16(Value), value))
1263     {
1264     writer.WriteResult<ushort>(comparer.Address, comparer.Value);
1265     }
1266     }
1267     address_index += STEP_SIZE;
1268     }
1269 william 521 #endregion
1270     }
1271     else
1272     {
1273     #region 16bits - signed
1274 william 532 var t_data = BitTools.ConvertByteArray<short>(data, new Func<bool>(ShouldCancelAnySearchOperation));
1275 william 560 foreach (var Value in t_data)
1276     {
1277     if (SearchWorkerThread.CancellationPending) { break; }
1278     using (_16bit_signed_comparer_ comparer = new _16bit_signed_comparer_(SearchArgs, address_index))
1279     {
1280     short value = 0;
1281     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1282     {
1283     using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1284     {
1285     try
1286     {
1287     gmp.OpenProvider();
1288     gmp.ReadMemory(address_index, out value);
1289     gmp.CloseProvider();
1290     }
1291     catch (Exception ex)
1292     {
1293     logger.VerboseError.WriteLine(ex.ToString());
1294     }
1295     }
1296     comparer.Value = value;
1297     }
1298     else
1299     {
1300     value = Convert.ToInt16(SearchArgs.CompareStartValue);
1301     comparer.Value = value;
1302     }
1303     if (comparer.Compare(Convert.ToSByte(Value), value))
1304     {
1305     writer.WriteResult<short>(comparer.Address, comparer.Value);
1306     }
1307     }
1308     address_index += STEP_SIZE;
1309     }
1310 william 521 #endregion
1311     } break;
1312     case SearchDataTypes._32bits:
1313     if (unsigned)
1314     {
1315     #region 32bits - unsigned
1316 william 532 var t_data = BitTools.ConvertByteArray<uint>(data, new Func<bool>(ShouldCancelAnySearchOperation));
1317 william 560 foreach (var Value in t_data)
1318     {
1319     if (SearchWorkerThread.CancellationPending) { break; }
1320     using (_32bit_unsigned_comparer_ comparer = new _32bit_unsigned_comparer_(SearchArgs, address_index))
1321     {
1322     uint value = 0;
1323     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1324     {
1325     using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1326     {
1327     try
1328     {
1329     gmp.OpenProvider();
1330     gmp.ReadMemory(address_index, out value);
1331     gmp.CloseProvider();
1332     }
1333     catch (Exception ex)
1334     {
1335     logger.VerboseError.WriteLine(ex.ToString());
1336     }
1337     }
1338     comparer.Value = value;
1339     }
1340     else
1341     {
1342     value = Convert.ToUInt32(SearchArgs.CompareStartValue);
1343     comparer.Value = value;
1344     }
1345     if (comparer.Compare(Convert.ToUInt32(Value), value))
1346     {
1347     writer.WriteResult<uint>(comparer.Address, comparer.Value);
1348     }
1349     }
1350     address_index += STEP_SIZE;
1351     }
1352 william 521 #endregion
1353     }
1354     else
1355     {
1356     #region 32bits - signed
1357 william 532 var t_data = BitTools.ConvertByteArray<int>(data, new Func<bool>(ShouldCancelAnySearchOperation));
1358 william 560 foreach (var Value in t_data)
1359     {
1360     if (SearchWorkerThread.CancellationPending) { break; }
1361     using (_32bit_signed_comparer_ comparer = new _32bit_signed_comparer_(SearchArgs, address_index))
1362     {
1363     int value = 0;
1364     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1365     {
1366     using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1367     {
1368     try
1369     {
1370     gmp.OpenProvider();
1371     gmp.ReadMemory(address_index, out value);
1372     gmp.CloseProvider();
1373     }
1374     catch (Exception ex)
1375     {
1376     logger.VerboseError.WriteLine(ex.ToString());
1377     }
1378     }
1379     comparer.Value = value;
1380     }
1381     else
1382     {
1383     value = Convert.ToInt32(SearchArgs.CompareStartValue);
1384     comparer.Value = value;
1385     }
1386     if (comparer.Compare(Convert.ToInt32(Value), value))
1387     {
1388     writer.WriteResult<int>(comparer.Address, comparer.Value);
1389     }
1390     }
1391     address_index += STEP_SIZE;
1392     }
1393 william 521 #endregion
1394     } break;
1395     case SearchDataTypes._64bits:
1396     if (unsigned)
1397     {
1398     #region 64bits - unsigned
1399 william 532 var t_data = BitTools.ConvertByteArray<ulong>(data, new Func<bool>(ShouldCancelAnySearchOperation));
1400 william 560 foreach (var Value in t_data)
1401     {
1402     if (SearchWorkerThread.CancellationPending) { break; }
1403     using (_64bit_unsigned_comparer_ comparer = new _64bit_unsigned_comparer_(SearchArgs, address_index))
1404     {
1405     ulong value = 0;
1406     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1407     {
1408     using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1409     {
1410     try
1411     {
1412     gmp.OpenProvider();
1413     gmp.ReadMemory(address_index, out value);
1414     gmp.CloseProvider();
1415     }
1416     catch (Exception ex)
1417     {
1418     logger.VerboseError.WriteLine(ex.ToString());
1419     }
1420     }
1421     comparer.Value = value;
1422     }
1423     else
1424     {
1425     value = Convert.ToUInt64(SearchArgs.CompareStartValue);
1426     comparer.Value = value;
1427     }
1428     if (comparer.Compare(Convert.ToUInt64(Value), value))
1429     {
1430     writer.WriteResult<ulong>(comparer.Address, comparer.Value);
1431     }
1432     }
1433     address_index += STEP_SIZE;
1434     }
1435 william 521 #endregion
1436     }
1437     else
1438     {
1439     #region 64bits - signed
1440 william 532 var t_data = BitTools.ConvertByteArray<long>(data, new Func<bool>(ShouldCancelAnySearchOperation));
1441 william 560 foreach (var Value in t_data)
1442     {
1443     if (SearchWorkerThread.CancellationPending) { break; }
1444     using (_64bit_signed_comparer_ comparer = new _64bit_signed_comparer_(SearchArgs, address_index))
1445     {
1446     long value = 0;
1447     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1448     {
1449     using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1450     {
1451     try
1452     {
1453     gmp.OpenProvider();
1454     gmp.ReadMemory(address_index, out value);
1455     gmp.CloseProvider();
1456     }
1457     catch (Exception ex)
1458     {
1459     logger.VerboseError.WriteLine(ex.ToString());
1460     }
1461     }
1462     comparer.Value = value;
1463     }
1464     else
1465     {
1466     value = Convert.ToInt64(SearchArgs.CompareStartValue);
1467     comparer.Value = value;
1468     }
1469     if (comparer.Compare(Convert.ToInt64(Value), value))
1470     {
1471     writer.WriteResult<long>(comparer.Address, comparer.Value);
1472     }
1473     }
1474     address_index += STEP_SIZE;
1475     }
1476 william 521 #endregion
1477     } break;
1478 william 560
1479 william 521 }
1480     #endregion
1481    
1482     if (SearchWorkerThread.CancellationPending)
1483     {
1484     provider_st.Stop();
1485     st.Stop();
1486     writer.CancelRequest();
1487     writer.Dispose();
1488     writer = null;
1489     e.Result = true;
1490     provider.CloseProvider();
1491     return;
1492     }
1493     }
1494    
1495 william 460 if (SearchWorkerThread.CancellationPending)
1496     {
1497     provider_st.Stop();
1498     st.Stop();
1499     writer.CancelRequest();
1500     writer.Dispose();
1501     writer = null;
1502     e.Result = true;
1503     provider.CloseProvider();
1504     return;
1505     }
1506     writer.Dispose();
1507     writer = null;
1508     provider.CloseProvider();
1509 william 560 #endregion
1510 william 561
1511     GC.Collect(); // force gc collection
1512 william 459 }
1513     else
1514     {
1515 william 560 #region SubsequentSearch
1516     Guid new_SearchGuid = GuidTools.IncrementGuid(SearchGuid);
1517 william 549 int Last_Whole_Percent_Done = 0;
1518 william 526 // need to get the address list
1519 william 560 uint[] reader_addresses = new uint[0];
1520     #region Address pre-population
1521     using (SearchResultReader reader = new SearchResultReader(SearchGuid, true, SearchArgs.IsUnsignedDataType, SearchArgs.DataType, new Action<int, string>(UpdateProgress), new Func<bool>(ShouldCancelAnySearchOperation))) // delete the file on dispose
1522 william 526 {
1523 william 560 reader_addresses = new uint[reader.ResultCount];
1524 william 549 // pre-populate the addresses array
1525     Stopwatch st_profiler_reader_addresses = new Stopwatch();
1526     st_profiler_reader_addresses.Start();
1527     int reader_address_index = 0;
1528 william 554 foreach (var k in reader)
1529 william 549 {
1530 william 560
1531 william 549 if (SearchWorkerThread.CancellationPending) { st_profiler_reader_addresses.Stop(); break; }
1532     reader_addresses[reader_address_index] = k.Address;
1533    
1534     string message = string.Format(" -> Creating Address Array (0x{0} of 0x{1})", reader_address_index.ToString("X"), reader_addresses.Length.ToString("X"));
1535     double double_percent_done = 100.0 * (double)((double)reader_address_index / (double)reader_addresses.Length);
1536     int int_percent_done = (int)double_percent_done;
1537     if ((int_percent_done != Last_Whole_Percent_Done))// && (reader_address_index % 10000) == 0)
1538     {
1539     UpdateProgress((int)double_percent_done, message);
1540     Last_Whole_Percent_Done = int_percent_done;
1541     logger.Profiler.WriteLine("It has taken {0} seconds to pre-populate the address array.", st_profiler_reader_addresses.Elapsed.TotalSeconds);
1542     }
1543    
1544     reader_address_index++;
1545     }
1546     st_profiler_reader_addresses.Stop();
1547     logger.Profiler.WriteLine("It took a total of {0} seconds to pre-populate the address array.", st_profiler_reader_addresses.Elapsed.TotalSeconds);
1548 william 560 }
1549     GC.Collect(); // force gc collection
1550     #endregion
1551    
1552     #region result writing
1553     using (SearchResultWriter writer = new SearchResultWriter(0, new_SearchGuid))
1554     {
1555    
1556 william 569 if (SearchWorkerThread.CancellationPending)
1557 william 527 {
1558 william 569 provider_st.Stop();
1559     st.Stop();
1560     writer.CancelRequest();
1561     writer.Dispose();
1562     e.Result = true;
1563     provider.CloseProvider();
1564     return;
1565 william 560 }
1566 william 569
1567    
1568     for (int i = 0; i < reader_addresses.Length; i++)
1569 william 560 {
1570 william 569 if (SearchWorkerThread.CancellationPending)
1571 william 546 {
1572 william 569 provider_st.Stop();
1573     st.Stop();
1574     writer.CancelRequest();
1575     writer.Dispose();
1576     e.Result = true;
1577     provider.CloseProvider();
1578     return;
1579 william 546 }
1580 william 569 var address = reader_addresses[i];
1581     provider.OpenProvider();
1582 william 560
1583 william 569 int bytesRead =0;
1584     byte[] data = new byte[STEP_SIZE];
1585     provider.ReadProcessMemory(address, 0, out bytesRead, out data);
1586    
1587 william 560 switch (sdt)
1588 william 527 {
1589 william 560 case SearchDataTypes._8bits:
1590     if (unsigned)
1591     {
1592     #region 8bits - unsigned
1593 william 569 byte Value = data[0];
1594     using (_8bit_unsigned_comparer_ comparer = new _8bit_unsigned_comparer_(SearchArgs, address))
1595 william 545 {
1596 william 569 byte value = 0;
1597     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1598 william 563 {
1599 william 569 using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1600 william 548 {
1601 william 569 try
1602 william 545 {
1603 william 569 gmp.OpenProvider();
1604     gmp.ReadMemory(address, out value);
1605     gmp.CloseProvider();
1606 william 545 }
1607 william 569 catch (Exception ex)
1608 william 545 {
1609 william 569 logger.VerboseError.WriteLine(ex.ToString());
1610 william 545 }
1611     }
1612 william 569 comparer.Value = value;
1613 william 545 }
1614 william 569 else
1615 william 563 {
1616 william 569 value = Convert.ToByte(SearchArgs.CompareStartValue);
1617     comparer.Value = value;
1618 william 563 }
1619 william 569 if (comparer.Compare(Convert.ToByte(Value), value))
1620 william 545 {
1621 william 569 writer.WriteResult<ushort>(comparer.Address, comparer.Value);
1622 william 560 }
1623 william 545 }
1624 william 560 #endregion
1625     }
1626     else
1627     {
1628 william 569 sbyte value = Convert.ToSByte(data[0]);
1629     }
1630     break;
1631     case SearchDataTypes._16bits:
1632     break;
1633 william 560 case SearchDataTypes._32bits:
1634 william 569 break;
1635 william 560 case SearchDataTypes._64bits:
1636 william 569 break;
1637 william 460 }
1638 william 496
1639 william 569 provider.CloseProvider();
1640 william 527 }
1641 william 569
1642     #region old-code:
1643     //Last_Whole_Percent_Done = 0;
1644     ////int index = 0;
1645    
1646     //provider.OpenProvider();
1647     ////int count = (int)((MemoryRangeSize - MemoryRangeStart) / STEP_SIZE);
1648     ////SearchResultWriter writer = new SearchResultWriter((int)(count), SearchGuid);
1649     //uint size = 0;
1650     //switch (sdt)
1651     //{
1652     // case SearchDataTypes._8bits: size = (MEM_CHUNK_SIZE / BitTools.SizeOf<uint>(sdt)); break;
1653     // case SearchDataTypes._16bits: size = (MEM_CHUNK_SIZE / BitTools.SizeOf<uint>(sdt)); break;
1654     // case SearchDataTypes._32bits: size = (MEM_CHUNK_SIZE / BitTools.SizeOf<uint>(sdt)); break;
1655     // case SearchDataTypes._64bits: size = (MEM_CHUNK_SIZE / BitTools.SizeOf<uint>(sdt)); break;
1656     //}
1657     ////size = STEP_SIZE * 4096;
1658     //for (uint i = MemoryRangeStart; i < MemoryRangeSize; i += size)
1659     //{
1660     // int bytesRead = 0;
1661     // byte[] data = new byte[size];
1662     // provider.ReadProcessMemoryAtOnce(i, size, out bytesRead, out data);
1663     // string message = string.Format(" -> Reading Address: 0x{0:x8}", (i + MemoryRangeStart));
1664     // double double_percent_done = 100.0 * (double)((double)i / (double)MemoryRangeSize);
1665     // int int_percent_done = (int)double_percent_done;
1666     // if ((int_percent_done != Last_Whole_Percent_Done))// && (i % 10000) == 0)
1667     // {
1668     // UpdateProgress(int_percent_done, message);
1669     // Last_Whole_Percent_Done = int_percent_done;
1670     // }
1671     // uint address_index = i;
1672     // int inner_Last_Whole_Percent_Done = 0;
1673     // #region comparison and serialization
1674     // switch (sdt)
1675     // {
1676     // case SearchDataTypes._8bits:
1677     // if (unsigned)
1678     // {
1679     // #region 8bits - unsigned
1680     // var t_data = data;
1681     // foreach (var Value in t_data)
1682     // {
1683     // string inner_message = string.Format(" -> Parsing value @ Address: 0x{0:x8}", address_index);
1684     // double inner_double_percent_done = 100.0 * (double)((double)address_index / (double)MemoryRangeSize);
1685     // int inner_int_percent_done = (int)inner_double_percent_done;
1686     // if ((address_index % 10000) == 0)
1687     // {
1688     // UpdateProgress(inner_int_percent_done, inner_message);
1689     // inner_Last_Whole_Percent_Done = inner_int_percent_done;
1690     // }
1691     // if (SearchWorkerThread.CancellationPending) { break; }
1692     // if (!reader_addresses.Contains(address_index))
1693     // {
1694     // address_index += STEP_SIZE;
1695     // continue;
1696     // }
1697     // else
1698     // {
1699     // writer.WriteResult<byte>(address_index, Value);
1700     // }
1701     // address_index += STEP_SIZE;
1702     // }
1703     // #endregion
1704     // }
1705     // else
1706     // {
1707     // #region 8bits - signed
1708     // var t_data = data;
1709     // foreach (var Value in t_data)
1710     // {
1711     // string inner_message = string.Format(" -> Parsing value @ Address: 0x{0:x8} of 0x{1:x8}", address_index, MemoryRangeSize);
1712     // double inner_double_percent_done = 100.0 * (double)((double)address_index / (double)MemoryRangeSize);
1713     // int inner_int_percent_done = (int)inner_double_percent_done;
1714     // if ((address_index % 10000) == 0)
1715     // {
1716     // UpdateProgress(inner_int_percent_done, inner_message);
1717     // inner_Last_Whole_Percent_Done = inner_int_percent_done;
1718     // }
1719     // if (SearchWorkerThread.CancellationPending) { break; }
1720     // if (!reader_addresses.Contains(address_index))
1721     // {
1722     // address_index += STEP_SIZE;
1723     // continue;
1724     // }
1725     // using (_8bit_signed_comparer_ comparer = new _8bit_signed_comparer_(SearchArgs, address_index))
1726     // {
1727     // sbyte value = 0;
1728     // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1729     // {
1730     // using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1731     // {
1732     // try
1733     // {
1734     // gmp.OpenProvider();
1735     // gmp.ReadMemory(address_index, out value);
1736     // gmp.CloseProvider();
1737     // }
1738     // catch (Exception ex)
1739     // {
1740     // logger.VerboseError.WriteLine(ex.ToString());
1741     // }
1742     // }
1743     // comparer.Value = value;
1744     // }
1745     // else
1746     // {
1747     // value = Convert.ToSByte(SearchArgs.CompareStartValue);
1748     // comparer.Value = value;
1749     // }
1750     // if (comparer.Compare(Convert.ToSByte(Value), value))
1751     // {
1752     // writer.WriteResult<sbyte>(comparer.Address, comparer.Value);
1753     // }
1754     // }
1755     // address_index += STEP_SIZE;
1756     // }
1757     // #endregion
1758     // } break;
1759     // case SearchDataTypes._16bits:
1760     // if (unsigned)
1761     // {
1762     // #region 16bits - unsigned
1763     // var t_data = BitTools.ConvertByteArray<ushort>(data, new Func<bool>(ShouldCancelAnySearchOperation));
1764     // foreach (var Value in t_data)
1765     // {
1766     // string inner_message = string.Format(" -> Parsing value @ Address: 0x{0:x8}", address_index);
1767     // double inner_double_percent_done = 100.0 * (double)((double)address_index / (double)MemoryRangeSize);
1768     // int inner_int_percent_done = (int)inner_double_percent_done;
1769     // if ((address_index % 10000) == 0)
1770     // {
1771     // UpdateProgress(inner_int_percent_done, inner_message);
1772     // inner_Last_Whole_Percent_Done = inner_int_percent_done;
1773     // }
1774     // if (SearchWorkerThread.CancellationPending) { break; }
1775     // if (!reader_addresses.Contains(address_index))
1776     // {
1777     // address_index += STEP_SIZE;
1778     // continue;
1779     // }
1780     // using (_16bit_unsigned_comparer_ comparer = new _16bit_unsigned_comparer_(SearchArgs, address_index))
1781     // {
1782     // ushort value = 0;
1783     // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1784     // {
1785     // using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1786     // {
1787     // try
1788     // {
1789     // gmp.OpenProvider();
1790     // gmp.ReadMemory(address_index, out value);
1791     // gmp.CloseProvider();
1792     // }
1793     // catch (Exception ex)
1794     // {
1795     // logger.VerboseError.WriteLine(ex.ToString());
1796     // }
1797     // }
1798     // comparer.Value = value;
1799     // }
1800     // else
1801     // {
1802     // value = Convert.ToUInt16(SearchArgs.CompareStartValue);
1803     // comparer.Value = value;
1804     // }
1805     // if (comparer.Compare(Convert.ToUInt16(Value), value))
1806     // {
1807     // writer.WriteResult<ushort>(comparer.Address, comparer.Value);
1808     // }
1809     // }
1810     // address_index += STEP_SIZE;
1811     // }
1812     // #endregion
1813     // }
1814     // else
1815     // {
1816     // #region 16bits - signed
1817     // var t_data = BitTools.ConvertByteArray<short>(data, new Func<bool>(ShouldCancelAnySearchOperation));
1818     // foreach (var Value in t_data)
1819     // {
1820     // string inner_message = string.Format(" -> Parsing value @ Address: 0x{0:x8}", address_index);
1821     // double inner_double_percent_done = 100.0 * (double)((double)address_index / (double)MemoryRangeSize);
1822     // int inner_int_percent_done = (int)inner_double_percent_done;
1823     // if ((address_index % 10000) == 0)
1824     // {
1825     // UpdateProgress(inner_int_percent_done, inner_message);
1826     // inner_Last_Whole_Percent_Done = inner_int_percent_done;
1827     // }
1828     // if (SearchWorkerThread.CancellationPending) { break; }
1829     // if (!reader_addresses.Contains(address_index))
1830     // {
1831     // address_index += STEP_SIZE;
1832     // continue;
1833     // }
1834     // using (_16bit_signed_comparer_ comparer = new _16bit_signed_comparer_(SearchArgs, address_index))
1835     // {
1836     // short value = 0;
1837     // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1838     // {
1839     // using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1840     // {
1841     // try
1842     // {
1843     // gmp.OpenProvider();
1844     // gmp.ReadMemory(address_index, out value);
1845     // gmp.CloseProvider();
1846     // }
1847     // catch (Exception ex)
1848     // {
1849     // logger.VerboseError.WriteLine(ex.ToString());
1850     // }
1851     // }
1852     // comparer.Value = value;
1853     // }
1854     // else
1855     // {
1856     // value = Convert.ToInt16(SearchArgs.CompareStartValue);
1857     // comparer.Value = value;
1858     // }
1859     // if (comparer.Compare(Convert.ToSByte(Value), value))
1860     // {
1861     // writer.WriteResult<short>(comparer.Address, comparer.Value);
1862     // }
1863     // }
1864     // address_index += STEP_SIZE;
1865     // }
1866     // #endregion
1867     // } break;
1868     // case SearchDataTypes._32bits:
1869     // if (unsigned)
1870     // {
1871     // #region 32bits - unsigned
1872     // var t_data = BitTools.ConvertByteArray<uint>(data, new Func<bool>(ShouldCancelAnySearchOperation));
1873     // foreach (var Value in t_data)
1874     // {
1875     // string inner_message = string.Format(" -> Parsing value @ Address: 0x{0:x8}", address_index);
1876     // double inner_double_percent_done = 100.0 * (double)((double)address_index / (double)MemoryRangeSize);
1877     // int inner_int_percent_done = (int)inner_double_percent_done;
1878     // if ((address_index % 10000) == 0)
1879     // {
1880     // UpdateProgress(inner_int_percent_done, inner_message);
1881     // inner_Last_Whole_Percent_Done = inner_int_percent_done;
1882     // }
1883     // if (SearchWorkerThread.CancellationPending) { break; }
1884     // if (!reader_addresses.Contains(address_index))
1885     // {
1886     // address_index += STEP_SIZE;
1887     // continue;
1888     // }
1889     // using (_32bit_unsigned_comparer_ comparer = new _32bit_unsigned_comparer_(SearchArgs, address_index))
1890     // {
1891     // uint value = 0;
1892     // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1893     // {
1894     // using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1895     // {
1896     // try
1897     // {
1898     // gmp.OpenProvider();
1899     // gmp.ReadMemory(address_index, out value);
1900     // gmp.CloseProvider();
1901     // }
1902     // catch (Exception ex)
1903     // {
1904     // logger.VerboseError.WriteLine(ex.ToString());
1905     // }
1906     // }
1907     // comparer.Value = value;
1908     // }
1909     // else
1910     // {
1911     // value = Convert.ToUInt32(SearchArgs.CompareStartValue);
1912     // comparer.Value = value;
1913     // }
1914     // if (comparer.Compare(Convert.ToUInt32(Value), value))
1915     // {
1916     // writer.WriteResult<uint>(comparer.Address, comparer.Value);
1917     // }
1918     // }
1919     // address_index += STEP_SIZE;
1920     // }
1921     // #endregion
1922     // }
1923     // else
1924     // {
1925     // #region 32bits - signed
1926     // var t_data = BitTools.ConvertByteArray<int>(data, new Func<bool>(ShouldCancelAnySearchOperation));
1927     // foreach (var Value in t_data)
1928     // {
1929     // string inner_message = string.Format(" -> Parsing value @ Address: 0x{0:x8}", address_index);
1930     // double inner_double_percent_done = 100.0 * (double)((double)address_index / (double)MemoryRangeSize);
1931     // int inner_int_percent_done = (int)inner_double_percent_done;
1932     // if ((address_index % 10000) == 0)
1933     // {
1934     // UpdateProgress(inner_int_percent_done, inner_message);
1935     // inner_Last_Whole_Percent_Done = inner_int_percent_done;
1936     // }
1937     // if (SearchWorkerThread.CancellationPending) { break; }
1938     // if (!reader_addresses.Contains(address_index))
1939     // {
1940     // address_index += STEP_SIZE;
1941     // continue;
1942     // }
1943     // using (_32bit_signed_comparer_ comparer = new _32bit_signed_comparer_(SearchArgs, address_index))
1944     // {
1945     // int value = 0;
1946     // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1947     // {
1948     // using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1949     // {
1950     // try
1951     // {
1952     // gmp.OpenProvider();
1953     // gmp.ReadMemory(address_index, out value);
1954     // gmp.CloseProvider();
1955     // }
1956     // catch (Exception ex)
1957     // {
1958     // logger.VerboseError.WriteLine(ex.ToString());
1959     // }
1960     // }
1961     // comparer.Value = value;
1962     // }
1963     // else
1964     // {
1965     // value = Convert.ToInt32(SearchArgs.CompareStartValue);
1966     // comparer.Value = value;
1967     // }
1968     // if (comparer.Compare(Convert.ToInt32(Value), value))
1969     // {
1970     // writer.WriteResult<int>(comparer.Address, comparer.Value);
1971     // }
1972     // }
1973     // address_index += STEP_SIZE;
1974     // }
1975     // #endregion
1976     // } break;
1977     // case SearchDataTypes._64bits:
1978     // if (unsigned)
1979     // {
1980     // #region 64bits - unsigned
1981     // var t_data = BitTools.ConvertByteArray<ulong>(data, new Func<bool>(ShouldCancelAnySearchOperation));
1982     // foreach (var Value in t_data)
1983     // {
1984     // string inner_message = string.Format(" -> Parsing value @ Address: 0x{0:x8}", address_index);
1985     // double inner_double_percent_done = 100.0 * (double)((double)address_index / (double)MemoryRangeSize);
1986     // int inner_int_percent_done = (int)inner_double_percent_done;
1987     // if ((address_index % 10000) == 0)
1988     // {
1989     // UpdateProgress(inner_int_percent_done, inner_message);
1990     // inner_Last_Whole_Percent_Done = inner_int_percent_done;
1991     // }
1992     // if (SearchWorkerThread.CancellationPending) { break; }
1993     // if (!reader_addresses.Contains(address_index))
1994     // {
1995     // address_index += STEP_SIZE;
1996     // continue;
1997     // }
1998     // using (_64bit_unsigned_comparer_ comparer = new _64bit_unsigned_comparer_(SearchArgs, address_index))
1999     // {
2000     // ulong value = 0;
2001     // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
2002     // {
2003     // using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
2004     // {
2005     // try
2006     // {
2007     // gmp.OpenProvider();
2008     // gmp.ReadMemory(address_index, out value);
2009     // gmp.CloseProvider();
2010     // }
2011     // catch (Exception ex)
2012     // {
2013     // logger.VerboseError.WriteLine(ex.ToString());
2014     // }
2015     // }
2016     // comparer.Value = value;
2017     // }
2018     // else
2019     // {
2020     // value = Convert.ToUInt64(SearchArgs.CompareStartValue);
2021     // comparer.Value = value;
2022     // }
2023     // if (comparer.Compare(Convert.ToUInt64(Value), value))
2024     // {
2025     // writer.WriteResult<ulong>(comparer.Address, comparer.Value);
2026     // }
2027     // }
2028     // address_index += STEP_SIZE;
2029     // }
2030     // #endregion
2031     // }
2032     // else
2033     // {
2034     // #region 64bits - signed
2035     // var t_data = BitTools.ConvertByteArray<long>(data, new Func<bool>(ShouldCancelAnySearchOperation));
2036     // foreach (var Value in t_data)
2037     // {
2038     // string inner_message = string.Format(" -> Parsing value @ Address: 0x{0:x8}", address_index);
2039     // double inner_double_percent_done = 100.0 * (double)((double)address_index / (double)MemoryRangeSize);
2040     // int inner_int_percent_done = (int)inner_double_percent_done;
2041     // if ((address_index % 10000) == 0)
2042     // {
2043     // UpdateProgress(inner_int_percent_done, inner_message);
2044     // inner_Last_Whole_Percent_Done = inner_int_percent_done;
2045     // }
2046     // if (SearchWorkerThread.CancellationPending) { break; }
2047     // if (!reader_addresses.Contains(address_index))
2048     // {
2049     // address_index += STEP_SIZE;
2050     // continue;
2051     // }
2052     // using (_64bit_signed_comparer_ comparer = new _64bit_signed_comparer_(SearchArgs, address_index))
2053     // {
2054     // long value = 0;
2055     // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
2056     // {
2057     // using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
2058     // {
2059     // try
2060     // {
2061     // gmp.OpenProvider();
2062     // gmp.ReadMemory(address_index, out value);
2063     // gmp.CloseProvider();
2064     // }
2065     // catch (Exception ex)
2066     // {
2067     // logger.VerboseError.WriteLine(ex.ToString());
2068     // }
2069     // }
2070     // comparer.Value = value;
2071     // }
2072     // else
2073     // {
2074     // value = Convert.ToInt64(SearchArgs.CompareStartValue);
2075     // comparer.Value = value;
2076     // }
2077     // if (comparer.Compare(Convert.ToInt64(Value), value))
2078     // {
2079     // writer.WriteResult<long>(comparer.Address, comparer.Value);
2080     // }
2081     // }
2082     // address_index += STEP_SIZE;
2083     // }
2084     // #endregion
2085     // } break;
2086     // }
2087     // #endregion
2088     // if (SearchWorkerThread.CancellationPending)
2089     // {
2090     // provider_st.Stop();
2091     // st.Stop();
2092     // writer.CancelRequest();
2093     // writer.Dispose();
2094     // e.Result = true;
2095     // provider.CloseProvider();
2096     // return;
2097     // }
2098     //}
2099     #endregion
2100 william 560 }
2101     #endregion
2102 william 526
2103 william 527 SearchGuid = new_SearchGuid; // update with out new guid
2104 william 560 reader_addresses = null; // free memory used by address array
2105     GC.Collect(); // force gc collection
2106     #endregion
2107 william 459 }
2108 william 560
2109 william 410 }
2110     provider_st.Stop();
2111     logger.Profiler.WriteLine("It took a total of {0} seconds for the memory provider to complete it's operation(s).", provider_st.Elapsed.TotalSeconds);
2112     //if (buffered_mem.Length == 0) { logger.Warn.WriteLine("Buffered Memory is Zero Length."); return; }
2113 william 456 //int Last_Whole_Percent_Done = 0;
2114 william 408
2115    
2116 william 410 #region Subsequent Searches
2117     //r_ms.BaseStream.Seek(0, SeekOrigin.Begin);
2118 william 408
2119    
2120 william 410 // hack to help with OutOfMemory Exceptions (OldValue and Equal compare will always add all found search results)
2121     bool NeedToCompare = true;
2122     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue &&
2123     SearchArgs.CompareType == SearchCompareTypes.Equal &&
2124     SearchArgs.IsFirstSearch)
2125     {
2126     NeedToCompare = false;
2127     //second_tmp_Results = null; // Free Memory
2128     }
2129 william 408
2130 william 410 if (NeedToCompare)
2131 william 535 {
2132 william 410 #region Ranged Searches
2133     #if !DONOT_HAVE_RANGED_SEARCH_SUPPORT
2134     if (SearchArgs.CompareType == SearchCompareTypes.Between || SearchArgs.CompareType == SearchCompareTypes.NotBetween)
2135 william 409 {
2136 william 410 st_ranged_search.Start();
2137     object start, end;
2138 william 408
2139 william 410 start = SearchArgs.CompareStartValue;
2140     end = SearchArgs.CompareEndValue;
2141 william 444 using (SearchResultReader reader = new SearchResultReader(SearchGuid))
2142 william 402 {
2143 william 410 for (int i = 0; i < reader.ResultCount; i += 1)
2144 william 409 {
2145 william 410 uint address = 0;
2146 william 483 #region switch (SearchArgs.DataType)
2147 william 410 switch (SearchArgs.DataType)
2148 william 402 {
2149 william 410 case SearchDataTypes._8bits: if (unsigned) { using (ResultType<byte> result = reader.GetNextResult<byte>()) { address = result.Address; } }
2150     else { using (ResultType<sbyte> result = reader.GetNextResult<sbyte>()) { address = result.Address; } } break;
2151     case SearchDataTypes._16bits: if (unsigned) { using (ResultType<ushort> result = reader.GetNextResult<ushort>()) { address = result.Address; } }
2152     else { using (ResultType<short> result = reader.GetNextResult<short>()) { address = result.Address; } } break;
2153     case SearchDataTypes._32bits: if (unsigned) { using (ResultType<uint> result = reader.GetNextResult<uint>()) { address = result.Address; } }
2154     else { using (ResultType<int> result = reader.GetNextResult<int>()) { address = result.Address; } } break;
2155     case SearchDataTypes._64bits: if (unsigned) { using (ResultType<ulong> result = reader.GetNextResult<ulong>()) { address = result.Address; } }
2156     else { using (ResultType<long> result = reader.GetNextResult<long>()) { address = result.Address; } } break;
2157     }
2158     #endregion
2159    
2160     if (MemoryRangeStart > 0 && !SearchArgs.IsFirstSearch) { address = address - MemoryRangeStart; }
2161     //r_ms.BaseStream.Seek(address, SeekOrigin.Begin);
2162     if (SearchArgs.CompareType == SearchCompareTypes.Between)
2163     {
2164     using (InRangeComparer comparer = new InRangeComparer(address, 0))
2165 william 409 {
2166 william 410 if (comparer.Compare(start, end, SearchArgs.DataType, SearchArgs.IsUnsignedDataType, (IAcceptsProcessAndConfig)this))
2167     {
2168     using (ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value))
2169     {
2170     second_tmp_Results.Add(_tmp_result);
2171     }
2172     }
2173 william 409 }
2174 william 402 }
2175 william 410 else if (SearchArgs.CompareType == SearchCompareTypes.NotBetween)
2176 william 408 {
2177 william 410 using (NotInRangeComparer comparer = new NotInRangeComparer(address, 0))
2178 william 409 {
2179 william 410 if (comparer.Compare(start, end, SearchArgs.DataType, SearchArgs.IsUnsignedDataType, (IAcceptsProcessAndConfig)this))
2180     {
2181     using (ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value))
2182     {
2183     second_tmp_Results.Add(_tmp_result);
2184     }
2185     }
2186 william 409 }
2187 william 408 }
2188 william 410 else
2189 william 408 {
2190 william 410 throw new InvalidOperationException("Encounted unkown range search type: " + SearchArgs.CompareType);
2191 william 408 }
2192 william 410 double double_percent_done = 100.0 * (double)((double)i / (double)reader.ResultCount);
2193     int int_percent_done = (int)double_percent_done;
2194     if (int_percent_done != Last_Whole_Percent_Done)
2195 william 409 {
2196 william 410 if (int_percent_done <= 100)
2197     {
2198     resultsprogress.Value = int_percent_done;
2199     resultsprogress.Message = string.Format(" -> Reading Address: 0x{0:x8}", i + MemoryRangeStart);
2200     Last_Whole_Percent_Done = int_percent_done;
2201     }
2202 william 409 }
2203 william 408 }
2204     }
2205 william 410 st_ranged_search.Stop();
2206     logger.Profiler.WriteLine("Ranged search took a total of {0} seconds to complete.", st_ranged_search.Elapsed.TotalSeconds);
2207 william 280 }
2208 william 410 #endif
2209     #endregion
2210 william 408 }
2211     #endregion
2212 william 457 //// leave SearchArgs.Results alone, if false
2213     //if (NeedToCompare)
2214     //{
2215     // // fix addresses when memory start is not zero
2216     // if (MemoryRangeStart > 0 && SearchArgs.IsFirstSearch) { for (int i = 0; i < second_tmp_Results.Count; i++) { second_tmp_Results[i].Address = second_tmp_Results[i].Address + MemoryRangeStart; } }
2217     // using (SearchResultWriter writer = new SearchResultWriter(second_tmp_Results.Count, SearchGuid))
2218     // {
2219     // for (int i = 0; i < second_tmp_Results.Count; i++)
2220     // {
2221     // switch (sdt)
2222     // {
2223     // case SearchDataTypes._8bits:
2224     // if (unsigned) { writer.WriteResult<Byte>(second_tmp_Results[i].Address, Convert.ToByte(second_tmp_Results[i].Value)); }
2225     // else { writer.WriteResult<SByte>(second_tmp_Results[i].Address, Convert.ToSByte(second_tmp_Results[i].Value)); } break;
2226     // case SearchDataTypes._16bits:
2227     // if (unsigned) { writer.WriteResult<UInt16>(second_tmp_Results[i].Address, Convert.ToUInt16(second_tmp_Results[i].Value)); }
2228     // else { writer.WriteResult<Int16>(second_tmp_Results[i].Address, Convert.ToInt16(second_tmp_Results[i].Value)); } break;
2229     // case SearchDataTypes._32bits:
2230     // if (unsigned) { writer.WriteResult<UInt32>(second_tmp_Results[i].Address, Convert.ToUInt32(second_tmp_Results[i].Value)); }
2231     // else { writer.WriteResult<Int32>(second_tmp_Results[i].Address, Convert.ToInt32(second_tmp_Results[i].Value)); } break;
2232     // case SearchDataTypes._64bits:
2233     // if (unsigned) { writer.WriteResult<UInt64>(second_tmp_Results[i].Address, Convert.ToUInt64(second_tmp_Results[i].Value)); }
2234     // else { writer.WriteResult<Int64>(second_tmp_Results[i].Address, Convert.ToInt64(second_tmp_Results[i].Value)); } break;
2235     // }
2236     // }
2237     // }
2238     // second_tmp_Results = null; // free memory
2239     //}
2240 william 280 }
2241 william 483 catch (OutOfMemoryException ex)
2242     {
2243     e.Result = true;
2244     logger.Error.WriteLine("Encounted {0} for search: {0}", ex.GetType().Name, SearchGuid.ToString());
2245     logger.VerboseError.WriteLine(ex.ToString());
2246     MessageBox.Show(string.Format("Encounted {0} for search: {0} ... Please try again.", ex.GetType().Name, SearchGuid.ToString()), string.Format("{0} was thrown", ex.GetType().Name), MessageBoxButtons.OK, MessageBoxIcon.Error);
2247     return;
2248     }
2249 william 410 catch (Exception ex)
2250     {
2251 william 419 throw ex;
2252 william 410 }
2253 william 230 }
2254    
2255 william 408
2256    
2257 william 230 private void SearchWorkerThread_ProgressChanged(object sender, ProgressChangedEventArgs e)
2258     {
2259 william 280 //if (SearchArgs.ProgressLogger != null)
2260     //{
2261     // resultsprogress.Value = e.ProgressPercentage;
2262     // //Application.DoEvents();
2263     //}
2264 william 230 }
2265    
2266     private void SearchWorkerThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
2267     {
2268 william 463 try { if ((bool)e.Result) { logger.Warn.WriteLine("Search operation was cancelled.");
2269 william 517 //new Action<int, string>(UpdateProgress).Invoke(0, string.Empty);
2270     UpdateProgress(0, string.Empty);
2271 william 463 return; }
2272     }
2273 william 446 catch (InvalidCastException) { }
2274     try
2275 william 286 {
2276 william 446 Stopwatch st = (Stopwatch)e.Result;
2277 william 286 st.Stop();
2278 william 392 logger.Profiler.WriteLine("Search took {0} seconds, overall, to complete.", st.Elapsed.TotalSeconds);
2279 william 286 }
2280 william 446 catch (InvalidCastException) { }
2281     catch (Exception ex) { throw ex; }
2282    
2283 william 517 UpdateProgress(100, String.Empty);
2284     //new Action<int, string>(UpdateProgress).Invoke(100, string.Empty);
2285 william 409 bool unsigned = SearchArgs.IsUnsignedDataType;
2286 william 549 using (SearchResultReader reader = new SearchResultReader(SearchGuid, SearchArgs.IsUnsignedDataType, SearchArgs.DataType, new Action<int, string>(UpdateProgress), new Func<bool>(ShouldCancelAnySearchOperation)))
2287 william 402 {
2288     logger.Info.WriteLine(string.Format("Found 0x{0:x8} results", reader.ResultCount));
2289    
2290     if (reader.ResultCount <= MIN_NUMBER_OF_RESULTS_BEFORE_DISPLAY)
2291 william 280 {
2292 william 402 lstResults.Items.Clear();
2293 william 538 List<ResultItem> items = new List<ResultItem>();
2294 william 554 foreach (var k in reader)
2295 william 538 {
2296     switch (SearchArgs.DataType)
2297     {
2298     case SearchDataTypes._8bits:
2299     if (SearchArgs.IsUnsignedDataType) { items.Add(new ResultItem(k.Address, false, Convert.ToByte(k.Value))); }
2300     else { items.Add(new ResultItem(k.Address, false, Convert.ToSByte(k.Value))); }
2301     break;
2302     case SearchDataTypes._16bits:
2303     if (SearchArgs.IsUnsignedDataType) { items.Add(new ResultItem(k.Address, false, Convert.ToUInt16(k.Value))); }
2304     else { items.Add(new ResultItem(k.Address, false, Convert.ToInt16(k.Value))); }
2305     break;
2306     case SearchDataTypes._32bits:
2307     if (SearchArgs.IsUnsignedDataType) { items.Add(new ResultItem(k.Address, false, Convert.ToUInt32(k.Value))); }
2308     else { items.Add(new ResultItem(k.Address, false, Convert.ToInt32(k.Value))); }
2309     break;
2310     case SearchDataTypes._64bits:
2311     if (SearchArgs.IsUnsignedDataType) { items.Add(new ResultItem(k.Address, false, Convert.ToUInt64(k.Value))); }
2312     else { items.Add(new ResultItem(k.Address, false, Convert.ToInt64(k.Value))); }
2313     break;
2314     }
2315     }
2316     lstResults.Items.AddRange(items.ToArray());
2317 william 280 }
2318     }
2319 william 560 GC.Collect(); // force gc collection
2320 william 280 this.DoSearchDoneSpecific();
2321     //System.Threading.Thread.Sleep(100);
2322     //if (_SEARCH_MODE != SearchMode.SEARCH_MODE_NORMAL_WITH_JOKER)
2323     this.ThawResultsUpdate();
2324     Application.DoEvents();
2325 william 230 }
2326 william 280 private void DoSearchDoneSpecific()
2327     {
2328     SearchWorkerThread.CancelAsync();
2329     if (lstResults.Items.Count > 0) { timer_update_results.Enabled = true; }
2330     else { timer_update_results.Enabled = false; }
2331 william 230
2332 william 280 search_progress_updater.Enabled = false;
2333    
2334     btnCancel.Enabled = false;
2335     btnReset.Enabled = true;
2336     btnSearch.Enabled = true;
2337     grpCompareType.Enabled = true;
2338     grpCompareValue.Enabled = true;
2339 william 517 UpdateProgress(0, string.Empty);
2340     //new Action<int, string>(UpdateProgress).Invoke(0, string.Empty);
2341 william 280 grpDataType.Enabled = false;
2342     // resume process on reset, incase it was suspended
2343 william 477 ThreadControl.ResumeProcess(this.AcceptedProcess.Id);
2344 william 280 //Application.DoEvents();
2345     this.Refresh();
2346     }
2347    
2348     private void DoCancelSpecific()
2349     {
2350     this.DoSearchDoneSpecific();
2351     }
2352     private void DoResetSpecific()
2353     {
2354     this.DoCancelSpecific();
2355     IsFirstSearch = true;
2356     grpDataType.Enabled = true;
2357 william