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

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

Parent Directory Parent Directory | Revision Log Revision Log


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