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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 392 - (hide annotations) (download)
Wed Jun 20 10:03:25 2012 UTC (8 years, 4 months ago) by william
File size: 88341 byte(s)
+ time search operations and use Profiler logging

1 william 389 #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
2 william 289 #if !USE_AUTOMATIC_MEMORY_SEARCH_RANGE
3 william 287 #define FORCE_USE_OF_MEMORYSIZECONSTANTS // when defined wil force the use of the constants defined in MemorySizeConstants for memory search range
4 william 289 #endif
5 william 280 //#define DONOT_HAVE_RANGED_SEARCH_SUPPORT // when defined, indicates that ranged searches have not been implemented
6     #define INCREASE_NUMBER_OF_RESULTS_BEFORE_DISPLAY // when defined will set MIN RESULTS to 0x2701 otherwise 0x03e8
7     //#define DO_NOT_SUSPEND_RESUME_THREAD_ON_FREEZE // when defined will not freeze/resume thread on freeze
8 william 229 using System;
9 william 227 using System.Collections.Generic;
10     using System.ComponentModel;
11     using System.Data;
12     using System.Drawing;
13     using System.Linq;
14     using System.Text;
15     using System.Windows.Forms;
16     using WeifenLuo.WinFormsUI.Docking;
17     using RomCheater.PluginFramework.Interfaces;
18     using System.Diagnostics;
19 william 229 using RomCheater.Docking.MemorySearch;
20     using libWin32.Win32.Threading;
21 william 230 using System.Threading;
22     using RomCheater.Logging;
23     using System.IO;
24 william 242 using Sojaner.MemoryScanner.MemoryProviers;
25 william 275 using RomCheater.PluginFramework.Events;
26 william 287 using System.Reflection;
27 william 292 using Sojaner.MemoryScanner;
28 william 227
29     namespace RomCheater.Docking
30     {
31 william 228 public partial class FloatingMemorySearcher : DockContent,
32 william 243 IAcceptsPlugin<IConfigPlugin>,
33     IAcceptsProcess<Process>,
34     IAcceptsProcessAndConfig,
35 william 275 ISearchInProgress,
36 william 287 IAcceptsBrowseMemoryRegion,
37     IAcceptsMemoryRange
38 william 227 {
39 william 280 #if INCREASE_NUMBER_OF_RESULTS_BEFORE_DISPLAY
40     const int MIN_NUMBER_OF_RESULTS_BEFORE_DISPLAY = 0x2701; // 10,000 results
41     #else
42     const int MIN_NUMBER_OF_RESULTS_BEFORE_DISPLAY = 0x03e8; // 1,000 results
43     #endif
44    
45 william 229 private bool DefaultUnsignedState = true; // set unsigned to true
46 william 287 public FloatingMemorySearcher() { InitializeComponent(); this.AcceptedPlugin = null; OnBrowseMemoryRegion = null; this.AcceptedProcess = null; SearchInProgess = false; Reload(); }
47 william 227 public FloatingMemorySearcher(IConfigPlugin config) : this() { this.AcceptedPlugin = config; }
48     public FloatingMemorySearcher(IConfigPlugin config, Process process) : this() { this.AcceptedPlugin = config; this.AcceptedProcess = process; }
49    
50 william 237 #region IAcceptsProcess<Process> Members
51 william 243 private Process _AcceptedProcess;
52 william 305 public Process AcceptedProcess { get { return _AcceptedProcess; } set { _AcceptedProcess = value; UpdateAcceptedProcess(value); } }
53 william 227 #endregion
54     #region IAcceptsPlugin<IConfigPlugin> Members
55 william 243 private IConfigPlugin _AcceptedPlugin;
56 william 305 public IConfigPlugin AcceptedPlugin { get { return _AcceptedPlugin; } set { _AcceptedPlugin = value; UpdateAcceptedPlugin(value); } }
57 william 227 #endregion
58 william 275 #region IAcceptsBrowseMemoryRegion members
59 william 287 public event BaseEventHandler<BrowseMemoryRegionEvent> OnBrowseMemoryRegion;
60 william 275 #endregion
61    
62 william 305 private void UpdateAcceptedPlugin(IConfigPlugin config)
63 william 243 {
64     this.lstResults.AcceptedPlugin = config;
65     this.lstPatchList.AcceptedPlugin = config;
66 william 389 if (config != null)
67     {
68     MemoryRangeStart = AcceptedPlugin.MemoryRangeStart;
69     MemoryRangeSize = AcceptedPlugin.MemoryRangeStart + AcceptedPlugin.MemoryRangeSize;
70     }
71 william 305 }
72     private void UpdateAcceptedProcess(Process process)
73     {
74     this.lstResults.AcceptedProcess = process;
75 william 243 this.lstPatchList.AcceptedProcess = process;
76 william 287 #if USE_AUTOMATIC_MEMORY_SEARCH_RANGE && FORCE_USE_OF_MEMORYSIZECONSTANTS
77     logger.Warn.WriteLine("FloatingMemorySearcher.UpdateAcceptedProcessAndConfig(IConfigPlugin config, Process process):");
78     logger.Warn.WriteLine("Both USE_AUTOMATIC_MEMORY_SEARCH_RANGE and FORCE_USE_OF_MEMORYSIZECONSTANTS are defined");
79     logger.Warn.WriteLine("FORCE_USE_OF_MEMORYSIZECONSTANTS will take precedence and will ignore the values supplied in the memeory search range");
80 william 289 #endif
81     #if FORCE_USE_OF_MEMORYSIZECONSTANTS
82     // force use of MemorySizeConstants
83 william 349 txtMemoryRangeStart.Value = MemorySizeConstants.MinimumSearchAddress;
84 william 382 txtMemoryRangeSize.Value = MemorySizeConstants.MinimumSearchAddress + MemorySizeConstants.MaximumSearchSize;
85 william 287 #endif
86     #if USE_AUTOMATIC_MEMORY_SEARCH_RANGE && !FORCE_USE_OF_MEMORYSIZECONSTANTS
87 william 318 ////// code to automatically choose the best starting memory address and size
88     //if (process != null)
89     //{
90     // string filename = process.MainModule.FileName;
91     // //string filename = @"c:\Windows\notepad.exe";
92     // PEReader peReader = new PEReader(filename);
93     //}
94     //else
95     //{
96 william 389 //txtMemoryRangeStart.Value = MemorySizeConstants.MinimumSearchAddress;
97     //txtMemoryRangeSize.Value = MemorySizeConstants.MinimumSearchAddress + MemorySizeConstants.MaximumSearchSize;
98 william 318 //}
99 william 389 if (AcceptedPlugin != null)
100     {
101     MemoryRangeStart = AcceptedPlugin.MemoryRangeStart;
102     MemoryRangeSize = AcceptedPlugin.MemoryRangeStart + AcceptedPlugin.MemoryRangeSize;
103     }
104    
105 william 287 #endif
106    
107 william 243 }
108 william 228 #region ISearchInProgress members
109 william 287 public bool SearchInProgess { get; private set; }
110 william 228 #endregion
111 william 229
112 william 287 #region IAcceptsMemoryRange
113     #if !FORCE_USE_OF_MEMORYSIZECONSTANTS
114 william 389 private uint _MemoryRangeStart;
115 william 287 private uint _MemoryRangeSize;
116     #endif
117 william 378 public uint MemoryRangeStart
118 william 287 {
119     get
120     {
121     #if FORCE_USE_OF_MEMORYSIZECONSTANTS
122 william 349 return MemorySizeConstants.MinimumSearchAddress;
123 william 287 #else
124     return _MemoryRangeStart;
125     #endif
126     }
127     set
128     {
129     #if !FORCE_USE_OF_MEMORYSIZECONSTANTS
130     _MemoryRangeStart = value;
131 william 389 txtMemoryRangeStart.Value = value;
132 william 287 #endif
133     }
134     }
135     public uint MemoryRangeSize
136     {
137     get
138     {
139     #if FORCE_USE_OF_MEMORYSIZECONSTANTS
140 william 382 return MemorySizeConstants.MinimumSearchAddress + MemorySizeConstants.MaximumSearchSize;
141 william 287 #else
142     return _MemoryRangeSize;
143     #endif
144     }
145     set
146     {
147     #if !FORCE_USE_OF_MEMORYSIZECONSTANTS
148     _MemoryRangeSize = value;
149 william 389 txtMemoryRangeSize.Value = value;
150 william 287 #endif
151     }
152     }
153     #endregion
154 william 229
155     public void Reload()
156     {
157     chkUnsigned.Checked = DefaultUnsignedState;
158     radio_8bits.Checked = true;
159     radiocompare_equal.Checked = true;
160     radio_oldvalue.Checked = true;
161 william 230 chkRefreshResults.Checked = true;
162 william 229 }
163 william 230 public enum eListViewResults
164     {
165     SEARCH_RESULTS_LIST = 0x3000,
166     PATCH_RESULTS_LIST = 0x3001,
167     UKNOWN_RESULTS_LIST = 0x3001
168     }
169 william 280 bool IsFirstSearch = true;
170 william 277 SearchType SearchArgs;
171 william 272 static int col_Found_Address = 1;
172     static int col_Found_Value = 2;
173     static int col_Found_Frozen = 3;
174     static int col_Added_Address = 1;
175     static int col_Added_Value = 2;
176     static int col_Added_Frozen = 3;
177 william 230 List<ListViewItem> ResultItems = new List<ListViewItem>();
178     List<ListViewItem> AddedItems = new List<ListViewItem>();
179     private bool _PatchedValue_NeedsUpdate;
180     bool PatchedValue_NeedsUpdate
181     {
182     get { if (_PatchedValue_NeedsUpdate) this.ThawResultsUpdate(); return _PatchedValue_NeedsUpdate; }
183     set { _PatchedValue_NeedsUpdate = value; if (value) this.ThawResultsUpdate(); }
184     }
185     private delegate ListViewItem ThreadSafe_GetResultItem(int index, int lv_type);
186     private ListViewItem GetResultItem(int index, int lv_type)
187     {
188     try
189     {
190     AddressValuePairList lv = null;
191     switch (lv_type)
192     {
193     case (int)eListViewResults.SEARCH_RESULTS_LIST: lv = lstResults; break;
194     case (int)eListViewResults.PATCH_RESULTS_LIST: lv = lstPatchList; break;
195     default: throw new IndexOutOfRangeException("Detected: " + Enum.GetName(typeof(eListViewResults), eListViewResults.UKNOWN_RESULTS_LIST) + " with value: " + lv_type.ToString("x4"));
196     }
197     ListViewItem item = new ListViewItem();
198     item = (ListViewItem)lv.Items[index].Clone();
199     return item;
200     }
201     catch (Exception)
202     {
203     return null;
204     }
205     }
206 william 229 private void radiocompare_equal_CheckedChanged(object sender, EventArgs e)
207     {
208     //if (!radiocompare_between.Checked && !radiocompare_notbetween.Checked)
209     //{
210     if (radio_oldvalue.Checked)
211     {
212     txtStartAddr.ReadOnly = true;
213     txtEndAddr.ReadOnly = true;
214     }
215     if (radio_specificvalue.Checked)
216     {
217     txtStartAddr.ReadOnly = false;
218     txtEndAddr.ReadOnly = true;
219     }
220     //}
221     }
222    
223     private void radiocompare_between_CheckedChanged(object sender, EventArgs e)
224     {
225     if (!radiocompare_equal.Checked &&
226     !radiocompare_greaterthan.Checked &&
227     !radiocompare_greaterthan.Checked &&
228     !radiocompare_lessthan.Checked &&
229     !radiocompare_greaterthan_orequal.Checked &&
230     !radiocompare_lessthan_orequal.Checked &&
231     !radiocompare_notequal.Checked)
232     if (radiocompare_between.Checked)
233     {
234     txtStartAddr.ReadOnly = false;
235     txtEndAddr.ReadOnly = false;
236     return;
237     }
238     if (!radiocompare_between.Checked && !radiocompare_notbetween.Checked)
239     {
240     if (radio_oldvalue.Checked)
241     {
242     txtStartAddr.ReadOnly = true;
243     txtEndAddr.ReadOnly = true;
244     }
245     if (radio_specificvalue.Checked)
246     {
247     txtStartAddr.ReadOnly = false;
248     txtEndAddr.ReadOnly = true;
249     }
250     }
251     }
252    
253     private void radiocompare_notbetween_CheckedChanged(object sender, EventArgs e)
254     {
255     if (!radiocompare_equal.Checked &&
256     !radiocompare_greaterthan.Checked &&
257     !radiocompare_greaterthan.Checked &&
258     !radiocompare_lessthan.Checked &&
259     !radiocompare_greaterthan_orequal.Checked &&
260     !radiocompare_lessthan_orequal.Checked &&
261     !radiocompare_notequal.Checked)
262     if (radiocompare_notbetween.Checked)
263     {
264     txtStartAddr.ReadOnly = false;
265     txtEndAddr.ReadOnly = false;
266     return;
267     }
268     if (!radiocompare_between.Checked && !radiocompare_notbetween.Checked)
269     {
270     if (radio_oldvalue.Checked)
271     {
272     txtStartAddr.ReadOnly = true;
273     txtEndAddr.ReadOnly = true;
274     }
275     if (radio_specificvalue.Checked)
276     {
277     txtStartAddr.ReadOnly = false;
278     txtEndAddr.ReadOnly = true;
279     }
280     }
281     }
282    
283     private void radio_8bits_CheckedChanged(object sender, EventArgs e)
284     {
285     if (chkUnsigned.Checked)
286     {
287     txtStartAddr.CreateTypeSize<byte>();
288     txtEndAddr.CreateTypeSize<byte>();
289     }
290     else
291     {
292     txtStartAddr.CreateTypeSize<sbyte>();
293     txtEndAddr.CreateTypeSize<sbyte>();
294     }
295     }
296    
297     private void radio_16bits_CheckedChanged(object sender, EventArgs e)
298     {
299     if (chkUnsigned.Checked)
300     {
301     txtStartAddr.CreateTypeSize<ushort>();
302     txtEndAddr.CreateTypeSize<ushort>();
303     }
304     else
305     {
306     txtStartAddr.CreateTypeSize<short>();
307     txtEndAddr.CreateTypeSize<short>();
308     }
309     }
310    
311     private void radio_32bits_CheckedChanged(object sender, EventArgs e)
312     {
313    
314     if (chkUnsigned.Checked)
315     {
316     txtStartAddr.CreateTypeSize<uint>();
317     txtEndAddr.CreateTypeSize<uint>();
318     }
319     else
320     {
321     txtStartAddr.CreateTypeSize<int>();
322     txtEndAddr.CreateTypeSize<int>();
323     }
324     }
325    
326     private void radio_64bits_CheckedChanged(object sender, EventArgs e)
327     {
328    
329     if (chkUnsigned.Checked)
330     {
331     txtStartAddr.CreateTypeSize<ulong>();
332     txtEndAddr.CreateTypeSize<ulong>();
333     }
334     else
335     {
336     txtStartAddr.CreateTypeSize<long>();
337     txtEndAddr.CreateTypeSize<long>();
338     }
339     }
340    
341     private void radio_oldvalue_CheckedChanged(object sender, EventArgs e)
342     {
343     if (!radiocompare_between.Checked && !radiocompare_notbetween.Checked)
344     {
345     txtStartAddr.ReadOnly = true;
346     txtEndAddr.ReadOnly = true;
347     }
348     }
349    
350     private void radio_specificvalue_CheckedChanged(object sender, EventArgs e)
351     {
352     if (!radiocompare_between.Checked && !radiocompare_notbetween.Checked)
353     {
354     txtStartAddr.ReadOnly = false;
355     txtEndAddr.ReadOnly = true;
356     }
357     }
358    
359     private void chkRefreshResults_CheckedChanged(object sender, EventArgs e)
360     {
361     if (chkRefreshResults.Checked)
362     {
363     timer_update_results.Enabled = true;
364     }
365     else
366     {
367     timer_update_results.Enabled = false;
368     ResultsUpdateWorkerThread.CancelAsync();
369     }
370     }
371    
372     private void timer_update_results_Tick(object sender, EventArgs e)
373     {
374     if (chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
375     {
376     ResultsUpdateWorkerThread.RunWorkerAsync();
377     }
378     }
379 william 230 private bool ShouldUpdateResults()
380     {
381     if (this.AcceptedProcess == null) return false;
382     if (SearchWorkerThread.IsBusy) return false;
383     //if (JokerSearchWorker.IsBusy) return false;
384     if (this.IsResultsUpdateFrozen) return false;
385     if (mnuAddedResults.Visible) return false;
386     if (mnuResults.Visible) return false;
387     if (Process.GetProcessById(this.AcceptedProcess.Id) == null) return false;
388     if (lstResults.Items.Count > 0) return true;
389     if (lstPatchList.Items.Count > 0) return true;
390     return false;
391     }
392 william 229 private void ResultsUpdateWorkerThread_DoWork(object sender, DoWorkEventArgs e)
393     {
394 william 230 Thread.Sleep(250); // keep thread from blocking
395     if (!this.ShouldUpdateResults()) return;
396     ////if (SearchArgs == null) return;
397     ////if (this.IsResultsUpdateFrozen) return;
398     ////// put thread to sleep for 500ms
399     ////System.Threading.Thread.Sleep(500);
400     //PCSX2MemoryProvider provider = new PCSX2MemoryProvider(this.SearchPCSX2ProcessPID, resultslog);
401     //byte[] buffered_mem = provider.GetMemory();
402     //MemoryStream ms = new MemoryStream(buffered_mem);
403     //BinaryReader r_ms = new BinaryReader(ms);
404 william 229
405 william 230 #region Update Results List
406     ResultItems = new List<ListViewItem>();
407     //r_ms.BaseStream.Seek(0, SeekOrigin.Begin);
408     for (int i = 0; i < lstResults.Items.Count; i++)
409     {
410     if (this.lstResults.InvokeRequired)
411     {
412     ThreadSafe_GetResultItem _get_item = new ThreadSafe_GetResultItem(GetResultItem);
413     object item = this.lstResults.Invoke(_get_item, new object[] { i, (int)eListViewResults.SEARCH_RESULTS_LIST });
414     if (item != null)
415     ResultItems.Add((ListViewItem)item);
416     }
417     else
418     {
419     ResultItems.Add(lstResults.Items[i]);
420     }
421    
422     }
423     for (int i = 0; i < ResultItems.Count; i++)
424     {
425     if (ResultsUpdateWorkerThread.CancellationPending == true)
426     {
427     e.Cancel = true;
428     return;
429     }
430 william 249 int Address = 0;
431 william 230 ResultDataType _result = (ResultDataType)ResultItems[i].Tag;
432    
433 william 249 Address = Convert.ToInt32(ResultItems[i].SubItems[col_Found_Address].Text, 16);
434 william 242 //r_ms.BaseStream.Seek(Address, SeekOrigin.Begin);
435     GenericMemoryProvider provider = new GenericMemoryProvider((IAcceptsProcessAndConfig)this);
436 william 245 provider.OpenProvider();
437 william 230 int bytesReadSize;
438 william 242 byte[] data;
439 william 230 uint bytesToRead = 0;
440     switch (_result.ValueType)
441     {
442     case SearchDataTypes._8bits:
443     bytesToRead = 1;
444     break;
445     case SearchDataTypes._16bits:
446     bytesToRead = 2;
447     break;
448     case SearchDataTypes._32bits:
449     bytesToRead = 4;
450     break;
451     case SearchDataTypes._64bits:
452     bytesToRead = 8;
453     break;
454     }
455 william 242 provider.ReadProcessMemory(Address, bytesToRead, out bytesReadSize, out data);
456 william 230 MemoryStream ms = new MemoryStream(data);
457     BinaryReader r_ms = new BinaryReader(ms);
458     switch (_result.ValueType)
459     {
460     case SearchDataTypes._8bits:
461     if (_result.IsUnsigned) { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x2}", r_ms.ReadByte()); }
462     else { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x2}", r_ms.ReadSByte()); }
463     break;
464     case SearchDataTypes._16bits:
465     if (_result.IsUnsigned) { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x4}", r_ms.ReadUInt16()); }
466     else { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x4}", r_ms.ReadInt16()); }
467     break;
468     case SearchDataTypes._32bits:
469     if (_result.IsUnsigned) { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x8}", r_ms.ReadUInt32()); }
470     else { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x8}", r_ms.ReadInt32()); }
471     break;
472     case SearchDataTypes._64bits:
473     if (_result.IsUnsigned) { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x16}", r_ms.ReadUInt64()); }
474     else { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x16}", r_ms.ReadInt64()); }
475     break;
476     }
477     r_ms.Close();
478 william 245 provider.CloseProvider();
479 william 230 Application.DoEvents();
480     }
481     #endregion
482    
483     #region Update Added Results List
484     AddedItems = new List<ListViewItem>();
485     //r_ms.BaseStream.Seek(0, SeekOrigin.Begin);
486     for (int i = 0; i < lstPatchList.Items.Count; i++)
487     {
488     if (this.lstResults.InvokeRequired)
489     {
490     ThreadSafe_GetResultItem _get_item = new ThreadSafe_GetResultItem(GetResultItem);
491     object item = this.lstResults.Invoke(_get_item, new object[] { i, (int)eListViewResults.PATCH_RESULTS_LIST });
492     if (item != null)
493     AddedItems.Add((ListViewItem)item);
494     }
495     else
496     {
497     AddedItems.Add(lstPatchList.Items[i]);
498     }
499    
500     }
501     for (int i = 0; i < AddedItems.Count; i++)
502     {
503     if (ResultsUpdateWorkerThread.CancellationPending == true)
504     {
505     e.Cancel = true;
506     return;
507     }
508 william 249 int Address = 0;
509 william 230 ResultDataType _result = (ResultDataType)AddedItems[i].Tag;
510 william 249 Address = Convert.ToInt32(AddedItems[i].SubItems[col_Added_Address].Text, 16);
511 william 242 GenericMemoryProvider provider = new GenericMemoryProvider((IAcceptsProcessAndConfig)this);
512 william 245 provider.OpenProvider();
513 william 230 int bytesReadSize;
514     byte[] data;
515     uint bytesToRead = 0;
516     switch (_result.ValueType)
517     {
518     case SearchDataTypes._8bits:
519     bytesToRead = 1;
520     break;
521     case SearchDataTypes._16bits:
522     bytesToRead = 2;
523     break;
524     case SearchDataTypes._32bits:
525     bytesToRead = 4;
526     break;
527     case SearchDataTypes._64bits:
528     bytesToRead = 8;
529     break;
530     }
531 william 242 provider.ReadProcessMemory(Address, bytesToRead, out bytesReadSize, out data);
532 william 230 MemoryStream ms = new MemoryStream(data);
533     BinaryReader r_ms = new BinaryReader(ms);
534     switch (_result.ValueType)
535     {
536     case SearchDataTypes._8bits:
537     if (_result.IsUnsigned) { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x2}", r_ms.ReadByte()); }
538     else { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x2}", r_ms.ReadSByte()); }
539     break;
540     case SearchDataTypes._16bits:
541     if (_result.IsUnsigned) { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x4}", r_ms.ReadUInt16()); }
542     else { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x4}", r_ms.ReadInt16()); }
543     break;
544     case SearchDataTypes._32bits:
545     if (_result.IsUnsigned) { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x8}", r_ms.ReadUInt32()); }
546     else { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x8}", r_ms.ReadInt32()); }
547     break;
548     case SearchDataTypes._64bits:
549     if (_result.IsUnsigned) { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x16}", r_ms.ReadUInt64()); }
550     else { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x16}", r_ms.ReadInt64()); }
551     break;
552     }
553     r_ms.Close();
554 william 245 provider.CloseProvider();
555 william 230 Application.DoEvents();
556     }
557     #endregion
558    
559    
560 william 229 }
561    
562     private void ResultsUpdateWorkerThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
563     {
564 william 230 try
565     {
566     //if ((lstResults.SelectedItems.Count > 0) && !PatchedValue_NeedsUpdate ) return;
567     //if ((lstPatchList.SelectedItems.Count > 0) && !PatchedValue_NeedsUpdate) return;
568     if (!this.ShouldUpdateResults()) return;
569     if (ResultItems.Count > 0)
570     {
571     //lstResults.Items.Clear();
572     //lstResults.Items.AddRange(ResultItems.ToArray());
573 william 229
574 william 230 for (int i = 0; i < ResultItems.Count; i++)
575     {
576     lstResults.Items[i].SubItems[new AVPColumnText(AVPColumnType.VALUE).ColumnIndex].Text =
577     ResultItems[i].SubItems[new AVPColumnText(AVPColumnType.VALUE).ColumnIndex].Text;
578     }
579    
580     }
581     if (AddedItems.Count > 0)
582     {
583     //lstPatchList.Items.Clear();
584     //lstPatchList.Items.AddRange(AddedItems.ToArray());
585    
586     for (int i = 0; i < AddedItems.Count; i++)
587     {
588     lstPatchList.Items[i].SubItems[new AVPColumnText(AVPColumnType.VALUE).ColumnIndex].Text =
589     AddedItems[i].SubItems[new AVPColumnText(AVPColumnType.VALUE).ColumnIndex].Text;
590     }
591    
592     }
593     PatchedValue_NeedsUpdate = false;
594     }
595     catch { }
596 william 229 }
597    
598     private void btnImportFile_Click(object sender, EventArgs e)
599     {
600     this.FreezeResultsUpdate();
601     if (!lstPatchList.ImportFromFile())
602     {
603     MessageBox.Show("Failed to Import Patch List from File.", "Import Failure", MessageBoxButtons.OK, MessageBoxIcon.Error);
604     this.ThawResultsUpdate();
605     return;
606     }
607     else
608     {
609     MessageBox.Show("Succesfully Imported Patch List from File.", "Import Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
610     this.ThawResultsUpdate();
611     return;
612     }
613     }
614     bool g_isFrozen = false;
615     private bool IsResultsUpdateFrozen
616     {
617     get { return g_isFrozen; }
618     set { g_isFrozen = value; }
619     }
620     private void ThawResultsUpdate()
621     {
622     this.IsResultsUpdateFrozen = false;
623     if (this.AcceptedProcess != null)
624     {
625     #if !DO_NOT_SUSPEND_RESUME_THREAD_ON_FREEZE
626     ThreadControl.ResumeProcess(this.AcceptedProcess.Id);
627     #endif
628     }
629     }
630    
631     private void FreezeResultsUpdate()
632     {
633     this.IsResultsUpdateFrozen = true;
634 william 285 //this.IsResultsUpdateFrozen = false;
635 william 229 if (this.AcceptedProcess != null)
636     {
637     #if !DO_NOT_SUSPEND_RESUME_THREAD_ON_FREEZE
638     ThreadControl.SuspendProcess(this.AcceptedProcess.Id);
639     #endif
640     }
641     }
642    
643     private void btnExportFile_Click(object sender, EventArgs e)
644     {
645     this.FreezeResultsUpdate();
646     if (!lstPatchList.ExportToFile())
647     {
648     MessageBox.Show("Failed to Export Patch List to File.", "Export Failure", MessageBoxButtons.OK, MessageBoxIcon.Error);
649     this.ThawResultsUpdate();
650     return;
651     }
652     else
653     {
654     MessageBox.Show("Succesfully Exported Patch List to File.", "Export Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
655     this.ThawResultsUpdate();
656     return;
657     }
658     }
659    
660     private void btnImportClipboard_Click(object sender, EventArgs e)
661     {
662     this.FreezeResultsUpdate();
663     if (!lstPatchList.ImportFromClipboard())
664     {
665     MessageBox.Show("Failed to Import Patch List from Clipboard.", "Import Failure", MessageBoxButtons.OK, MessageBoxIcon.Error);
666     this.ThawResultsUpdate();
667     return;
668     }
669     else
670     {
671     MessageBox.Show("Succesfully Import Patch List from Clipboard.", "Import Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
672     this.ThawResultsUpdate();
673     }
674     }
675    
676     private void btnExportClipboard_Click(object sender, EventArgs e)
677     {
678     this.FreezeResultsUpdate();
679     if (!lstPatchList.ExportToClipboard())
680     {
681     MessageBox.Show("Failed to Export Patch List to Clipboard.", "Export Failure", MessageBoxButtons.OK, MessageBoxIcon.Error);
682     this.ThawResultsUpdate();
683     return;
684     }
685     else
686     {
687     MessageBox.Show("Succesfully Exported Patch List to Clipboard.", "Export Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
688     this.ThawResultsUpdate();
689     return;
690     }
691     }
692    
693     private void btnAddPatchAddress_Click(object sender, EventArgs e)
694     {
695 william 245 PatchAdder adder = new PatchAdder((IAcceptsProcessAndConfig)this);
696 william 229 adder.ShowDialog();
697     if (adder.WasAPatchAdded) AddToPatchList(adder.AddedPatchValue);
698     }
699    
700     private void btnAddAddressRange_Click(object sender, EventArgs e)
701     {
702 william 245 PatchRangeAdder adder = new PatchRangeAdder((IAcceptsProcessAndConfig)this);
703 william 229 adder.ShowDialog();
704     if (adder.WasAPatchAdded) AddToPatchList(adder.AddedPatchValue);
705     }
706     private void AddToPatchList(List<ResultDataType> item) { foreach (ResultDataType data in item) { AddToPatchList(data); } }
707     private void AddToPatchList(ResultDataType item)
708     {
709     ResultItem item2 = null;
710     switch (item.ValueType)
711     {
712     case SearchDataTypes._8bits:
713     if (item.IsUnsigned) { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToByte(item.Value)); }
714     else { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToSByte(item.Value)); }
715     break;
716     case SearchDataTypes._16bits:
717     if (item.IsUnsigned) { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToUInt16(item.Value)); }
718     else { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToInt16(item.Value)); }
719     break;
720     case SearchDataTypes._32bits:
721     if (item.IsUnsigned) { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToUInt32(item.Value)); }
722     else { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToInt32(item.Value)); }
723     break;
724     case SearchDataTypes._64bits:
725     if (item.IsUnsigned) { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToUInt64(item.Value)); }
726     else { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToInt64(item.Value)); }
727     break;
728     }
729     this.AddToPatchList(item2);
730     }
731     private void AddToPatchList(ListViewItem item)
732     {
733     try
734     {
735     ResultDataType _result = (ResultDataType)item.Tag;
736     this.AddToPatchList(_result);
737     }
738     catch (InvalidCastException ex)
739     {
740     // unable to cast
741     MessageBox.Show(ex.Message, "Invalid Cast Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
742     }
743     catch (Exception ex)
744     {
745     // other exception
746     MessageBox.Show(ex.Message, "Unhandled Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
747     }
748     }
749     private void AddToPatchList(ResultItem item)
750     {
751     if (!lstPatchList.Items.Contains(item)) lstPatchList.Items.Add(item);
752     }
753     private void AddToPatchList(string address, SearchDataTypes bitsize, bool IsUnsigned)
754     {
755 william 245 ResultItemState state = new ResultItemState(address, bitsize, IsUnsigned, (IAcceptsProcessAndConfig)this);
756 william 229 ResultItem item = new ResultItem(state.Address, state.Value, state.Frozen, state.ValueType, state.IsUnsigned);
757     this.AddToPatchList(item);
758     }
759 william 230
760     private void mnuItemAddToPatchList_Click(object sender, EventArgs e)
761     {
762     if (!(lstResults.SelectedItems.Count > 0)) return;
763     //if (SearchArgs == null) return;
764    
765     try
766     {
767     for (int i = 0; i < lstResults.SelectedIndices.Count; i++)
768     {
769     //ResultDataType result = (ResultDataType)lstResults.Items[selected_index].Tag;
770     ListViewItem item = lstResults.Items[lstResults.SelectedIndices[i]];
771     this.AddToPatchList(item);
772     }
773     }
774     catch (Exception ex)
775     {
776     logger.Error.WriteLine(ex.ToString());
777     }
778     }
779    
780     private void mnuItemRemoveResult_Click(object sender, EventArgs e)
781     {
782     if (!(lstPatchList.SelectedItems.Count > 0)) return;
783     //if (SearchArgs == null) return;
784     try
785     {
786     this.FreezeResultsUpdate();
787     for (int i = 0; i < lstPatchList.SelectedIndices.Count; i++)
788     {
789     //lstPatchList.ThawItem(lstPatchList.SelectedIndices[i]);
790     lstPatchList.Items[lstPatchList.SelectedIndices[i]].Remove();
791     }
792     this.ThawResultsUpdate();
793     }
794     catch (Exception ex)
795     {
796     Debug.WriteLine(ex.ToString());
797     }
798     }
799     private void PatchRange(bool SingleEntry)
800     {
801     //if (SearchArgs == null) return;
802     #region Patch Selected Address
803     // stop ResultsUpdate Thread
804     ResultsUpdateWorkerThread.CancelAsync();
805    
806     List<ResultDataType> patch_list = new List<ResultDataType>();
807     List<int> SelectedIndexes = new List<int>();
808    
809     if (SingleEntry)
810     {
811     SelectedIndexes.Add(lstPatchList.SelectedIndices[0]);
812     }
813     else
814     {
815     foreach (int index in lstPatchList.SelectedIndices)
816     {
817     SelectedIndexes.Add(index);
818     }
819     }
820     //PCSX2MemoryProvider provider = new PCSX2MemoryProvider(this.SearchPCSX2ProcessPID, resultslog);
821     foreach (int index in SelectedIndexes)
822     {
823     if (SingleEntry)
824     {
825     SearchPatcher patcher = null;
826     uint Address = 0;
827     ListViewItem item = lstPatchList.Items[index];
828     ResultDataType _result = (ResultDataType)item.Tag;
829     Address = Convert.ToUInt32(item.SubItems[col_Found_Address].Text, 16);
830     switch (_result.ValueType)
831     {
832     case SearchDataTypes._8bits:
833     if (_result.IsUnsigned)
834     {
835     byte value = Convert.ToByte(item.SubItems[col_Found_Value].Text, 16);
836 william 243 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
837 william 230 timer_update_results.Enabled = false;
838     patcher.ShowDialog();
839     timer_update_results.Enabled = true;
840     PatchedValue_NeedsUpdate = true;
841     if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
842     ResultsUpdateWorkerThread.RunWorkerAsync();
843     }
844     else
845     {
846     sbyte value = Convert.ToSByte(item.SubItems[col_Found_Value].Text, 16);
847 william 243 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
848 william 230 timer_update_results.Enabled = false;
849     patcher.ShowDialog();
850     timer_update_results.Enabled = true;
851     PatchedValue_NeedsUpdate = true;
852     if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
853     ResultsUpdateWorkerThread.RunWorkerAsync();
854     }
855     break;
856     case SearchDataTypes._16bits:
857     if (_result.IsUnsigned)
858     {
859     ushort value = Convert.ToUInt16(item.SubItems[col_Found_Value].Text, 16);
860 william 243 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
861 william 230 timer_update_results.Enabled = false;
862     patcher.ShowDialog();
863     timer_update_results.Enabled = true;
864     PatchedValue_NeedsUpdate = true;
865     if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
866     ResultsUpdateWorkerThread.RunWorkerAsync();
867     }
868     else
869     {
870     short value = Convert.ToInt16(item.SubItems[col_Found_Value].Text, 16);
871 william 243 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
872 william 230 timer_update_results.Enabled = false;
873     patcher.ShowDialog();
874     timer_update_results.Enabled = true;
875     PatchedValue_NeedsUpdate = true;
876     if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
877     ResultsUpdateWorkerThread.RunWorkerAsync();
878     }
879     break;
880     case SearchDataTypes._32bits:
881     if (_result.IsUnsigned)
882     {
883     uint value = Convert.ToUInt32(item.SubItems[col_Found_Value].Text, 16);
884 william 243 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
885 william 230 timer_update_results.Enabled = false;
886     patcher.ShowDialog();
887     timer_update_results.Enabled = true;
888     PatchedValue_NeedsUpdate = true;
889     if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
890     ResultsUpdateWorkerThread.RunWorkerAsync();
891     }
892     else
893     {
894     int value = Convert.ToInt32(item.SubItems[col_Found_Value].Text, 16);
895 william 243 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
896 william 230 timer_update_results.Enabled = false;
897     patcher.ShowDialog();
898     timer_update_results.Enabled = true;
899     PatchedValue_NeedsUpdate = true;
900     if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
901     ResultsUpdateWorkerThread.RunWorkerAsync();
902     }
903     break;
904     case SearchDataTypes._64bits:
905     if (_result.IsUnsigned)
906     {
907     ulong value = Convert.ToUInt32(item.SubItems[col_Found_Value].Text, 16);
908 william 243 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
909 william 230 timer_update_results.Enabled = false;
910     patcher.ShowDialog();
911     timer_update_results.Enabled = true;
912     PatchedValue_NeedsUpdate = true;
913     if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
914     ResultsUpdateWorkerThread.RunWorkerAsync();
915     }
916     else
917     {
918     long value = Convert.ToInt32(item.SubItems[col_Found_Value].Text, 16);
919 william 243 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
920 william 230 timer_update_results.Enabled = false;
921     patcher.ShowDialog();
922     timer_update_results.Enabled = true;
923     PatchedValue_NeedsUpdate = true;
924     if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
925     ResultsUpdateWorkerThread.RunWorkerAsync();
926     }
927     break;
928     }
929     }
930     else
931     {
932    
933     ListViewItem item = lstPatchList.Items[index];
934     ResultDataType _result = (ResultDataType)item.Tag;
935     patch_list.Add(_result);
936     }
937     }
938    
939     if (patch_list.Count > 0)
940     {
941 william 243 SearchRangePatcher rangePatcher = new SearchRangePatcher((IAcceptsProcessAndConfig)this, patch_list);
942 william 230 rangePatcher.ShowDialog();
943     }
944    
945     #endregion
946     }
947     private void mnuItemPatchSelectedEntry_Click(object sender, EventArgs e)
948     {
949     if (!(lstPatchList.SelectedItems.Count == 1)) return;
950     PatchRange(true);
951     }
952    
953     private void mnuItemPatchSelectedRange_Click(object sender, EventArgs e)
954     {
955     if (!(lstPatchList.SelectedItems.Count >= 1)) return;
956     PatchRange(false);
957     }
958    
959     private void mnuItemFreezeSelectedPatches_Click(object sender, EventArgs e)
960     {
961     if (!(lstPatchList.SelectedItems.Count > 0)) return;
962     //if (SearchArgs == null) return;
963     try
964     {
965     lstPatchList.ProcessID = this.AcceptedProcess.Id;
966     this.FreezeResultsUpdate();
967     for (int i = 0; i < lstPatchList.SelectedIndices.Count; i++)
968     {
969     lstPatchList.FreezeItem(lstPatchList.SelectedIndices[i]);
970     }
971     // force thaw and update
972     this.ThawResultsUpdate();
973     this.Update();
974     }
975     catch (Exception ex)
976     {
977     Debug.WriteLine(ex.ToString());
978     }
979     }
980    
981     private void mnuItemThawSelectedPatches_Click(object sender, EventArgs e)
982     {
983     if (!(lstPatchList.SelectedItems.Count > 0)) return;
984     //if (SearchArgs == null) return;
985     try
986     {
987     lstPatchList.ProcessID = this.AcceptedProcess.Id;
988     this.FreezeResultsUpdate();
989     for (int i = 0; i < lstPatchList.SelectedIndices.Count; i++)
990     {
991     lstPatchList.ThawItem(lstPatchList.SelectedIndices[i]);
992     }
993     // force thaw and update
994     this.ThawResultsUpdate();
995     this.Update();
996     }
997     catch (Exception ex)
998     {
999     Debug.WriteLine(ex.ToString());
1000     }
1001     }
1002    
1003     private void SearchWorkerThread_DoWork(object sender, DoWorkEventArgs e)
1004     {
1005 william 286 Stopwatch st = new Stopwatch();
1006     st.Start();
1007 william 392
1008     Stopwatch st_first_search = new Stopwatch();
1009     Stopwatch st_nonrange_search = new Stopwatch();
1010     Stopwatch st_ranged_search = new Stopwatch();
1011    
1012 william 286 e.Result = st;
1013 william 280 //List<ResultType<object>> tmp_Results = new List<ResultType<object>>();
1014     List<ResultType<object>> second_tmp_Results = new List<ResultType<object>>();
1015 william 384 const int ElementsBeforeDisplay = 100;
1016     //const double _UPDATE_DELAY = 1024.0;
1017     //double UPDATE_DELAY = 1;
1018 william 280 //tmp_Results = SearchArgs.Results.GetRange(0,SearchArgs.Results.Count);
1019     //SearchArgs.Results = null;
1020     //SearchArgs.Results.Clear();
1021     // log options
1022     SearchArgs.LogSearchOptions();
1023 william 384 uint STEP_SIZE = (uint)SearchArgs.DataType / 8;
1024 william 280 GenericMemoryProvider provider = new GenericMemoryProvider((IAcceptsProcessAndConfig)this);
1025 william 282 provider.OpenProvider();
1026     int bytes_read = 0;
1027    
1028 william 384 byte[] buffered_mem = new byte[(MemoryRangeSize - MemoryRangeStart)]; // throws OutOfMemoryException if size is over 2G
1029     provider.ReadProcessMemoryAtOnce(MemoryRangeStart, (MemoryRangeSize - MemoryRangeStart), out bytes_read, out buffered_mem);
1030 william 282 provider.CloseProvider();
1031    
1032 william 280 if (buffered_mem.Length == 0) { logger.Warn.WriteLine("Buffered Memory is Zero Length."); return; }
1033 william 384 //MemoryStream ms = new MemoryStream(buffered_mem);
1034     //BinaryReader r_ms = new BinaryReader(ms);
1035     logger.Debug.WriteLine(string.Format("Buffered Memory Size -> 0x{0:x8}", buffered_mem.Length));
1036 william 280 int Last_Whole_Percent_Done = 0;
1037    
1038     #region First Search
1039     if (SearchArgs.IsFirstSearch)
1040     {
1041 william 392 st_first_search.Start();
1042 william 280 SearchArgs.Results.Clear();
1043 william 384 //r_ms.BaseStream.Seek(0, SeekOrigin.Begin);
1044     for (uint i = 0; i < buffered_mem.Length; i += STEP_SIZE)
1045 william 280 {
1046     ResultType<object> _tmp_result = new ResultType<object>();
1047     switch (SearchArgs.DataType)
1048     {
1049     case SearchDataTypes._8bits:
1050 william 384 if (SearchArgs.IsUnsignedDataType) { _tmp_result = new ResultType<object>(i, buffered_mem[i]); }
1051     else { _tmp_result = new ResultType<object>(i, buffered_mem[i]); } break;
1052 william 280 case SearchDataTypes._16bits:
1053 william 384 if (SearchArgs.IsUnsignedDataType) { _tmp_result = new ResultType<object>(i, BitConverter.ToUInt16(buffered_mem,(int)i)); }
1054     else { _tmp_result = new ResultType<object>(i,BitConverter.ToInt16(buffered_mem,(int)i)); } break;
1055 william 280 case SearchDataTypes._32bits:
1056 william 384 if (SearchArgs.IsUnsignedDataType) { _tmp_result = new ResultType<object>(i, BitConverter.ToUInt32(buffered_mem,(int)i)); }
1057     else { _tmp_result = new ResultType<object>(i, BitConverter.ToInt32(buffered_mem,(int)i)); } break;
1058 william 280 case SearchDataTypes._64bits:
1059 william 384 if (SearchArgs.IsUnsignedDataType) { _tmp_result = new ResultType<object>(i, BitConverter.ToUInt64(buffered_mem,(int)i)); }
1060     else { _tmp_result = new ResultType<object>(i, BitConverter.ToInt64(buffered_mem,(int)i)); } break;
1061 william 280 }
1062     SearchArgs.Results.Add(_tmp_result);
1063 william 384 double double_percent_done = 100.0 * (double)((double)i / (double)buffered_mem.Length);
1064 william 280 int int_percent_done = (int)double_percent_done;
1065 william 384 if (int_percent_done != Last_Whole_Percent_Done && i % ElementsBeforeDisplay == 0)
1066 william 356 {
1067     resultsprogress.Value = int_percent_done;
1068 william 388 //resultsprogress.Message = string.Format(" -> Reading Address: 0x{0:x8}", i);
1069 william 356 Last_Whole_Percent_Done = int_percent_done;
1070 william 388 //Application.DoEvents();
1071 william 356 }
1072 william 280
1073     if (SearchWorkerThread.CancellationPending == true)
1074     {
1075     e.Cancel = true;
1076     return;
1077     }
1078 william 384 Application.DoEvents();
1079 william 280 }
1080     resultsprogress.Value = 100;
1081     resultsprogress.Message = "";
1082 william 388 //Application.DoEvents();
1083 william 392 st_first_search.Stop();
1084     logger.Profiler.WriteLine("First search took a total of {0} seconds to complete.", st_first_search.Elapsed.TotalSeconds);
1085 william 280 }
1086     #endregion
1087    
1088     #region Subsequent Searches
1089 william 384 //r_ms.BaseStream.Seek(0, SeekOrigin.Begin);
1090 william 280
1091    
1092     // hack to help with OutOfMemory Exceptions (OldValue and Equal compare will always add all found search results)
1093     bool NeedToCompare = true;
1094     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue &&
1095     SearchArgs.CompareType == SearchCompareTypes.Equal &&
1096     SearchArgs.IsFirstSearch)
1097     {
1098     NeedToCompare = false;
1099     second_tmp_Results = null; // Free Memory
1100     }
1101    
1102     if (NeedToCompare)
1103     {
1104     if (SearchArgs.CompareType != SearchCompareTypes.Between && SearchArgs.CompareType != SearchCompareTypes.NotBetween)
1105     {
1106     #region Non-Range Searches
1107 william 392 st_nonrange_search.Start();
1108 william 280 //second_tmp_Results = new List<ResultType<object>>(SearchArgs.Results.Count * 1024);
1109     ////second_tmp_Results.c
1110     for (int i = 0; i < SearchArgs.Results.Count; i += 1)
1111     {
1112 william 384 //r_ms.BaseStream.Seek(SearchArgs.Results[i].Address, SeekOrigin.Begin);
1113     uint address = SearchArgs.Results[i].Address;
1114 william 385 if (MemoryRangeStart > 0 && !SearchArgs.IsFirstSearch) { address = address - MemoryRangeStart; }
1115 william 280 switch (SearchArgs.DataType)
1116     {
1117     #region Comparer Support
1118 william 385 #region case SearchDataTypes._8bits:
1119 william 280 case SearchDataTypes._8bits:
1120     if (SearchArgs.IsUnsignedDataType)
1121     {
1122 william 384 byte lookup_value = buffered_mem[address];
1123 william 280 _8bit_unsigned_comparer_ comparer = new _8bit_unsigned_comparer_(SearchArgs, SearchArgs.Results[i].Address);
1124     byte value = 0;
1125     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1126     {
1127     value = Convert.ToByte(SearchArgs.Results[i].Value);
1128     comparer.Value = value;
1129     }
1130     else
1131     {
1132     value = Convert.ToByte(SearchArgs.CompareStartValue);
1133     comparer.Value = value;
1134     }
1135     if (comparer.Compare(lookup_value, value))
1136     {
1137     //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1138     //second_tmp_Results.Add(_tmp_result);
1139     //_tmp_result = null; // free memory
1140     //SearchArgs.Results.RemoveAt(i);
1141     SearchArgs.Results[i].Value = comparer.Value;
1142     second_tmp_Results.Add(SearchArgs.Results[i]);
1143     }
1144     comparer = null; // free memory
1145     }
1146     else
1147     {
1148 william 384 sbyte lookup_value = (sbyte)buffered_mem[address];
1149 william 280 _8bit_signed_comparer_ comparer = new _8bit_signed_comparer_(SearchArgs, SearchArgs.Results[i].Address);
1150     sbyte value = 0;
1151     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1152     {
1153     value = Convert.ToSByte(SearchArgs.Results[i].Value);
1154     }
1155     else
1156     {
1157     value = Convert.ToSByte(SearchArgs.CompareStartValue);
1158     }
1159     if (comparer.Compare(lookup_value, value))
1160     {
1161     //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1162     //second_tmp_Results.Add(_tmp_result);
1163     //_tmp_result = null; // free memory
1164     //SearchArgs.Results.RemoveAt(i);
1165     SearchArgs.Results[i].Value = comparer.Value;
1166     second_tmp_Results.Add(SearchArgs.Results[i]);
1167     }
1168     comparer = null; // free memory
1169     }
1170     break;
1171 william 385 #endregion
1172     #region case SearchDataTypes._16bits:
1173 william 280 case SearchDataTypes._16bits:
1174     if (SearchArgs.IsUnsignedDataType)
1175     {
1176 william 384 ushort lookup_value = BitConverter.ToUInt16(buffered_mem,(int)address);
1177 william 280 _16bit_unsigned_comparer_ comparer = new _16bit_unsigned_comparer_(SearchArgs, SearchArgs.Results[i].Address);
1178     ushort value = 0;
1179     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1180     {
1181     value = Convert.ToUInt16(SearchArgs.Results[i].Value);
1182     comparer.Value = value;
1183     }
1184     else
1185     {
1186     value = Convert.ToUInt16(SearchArgs.CompareStartValue);
1187     comparer.Value = value;
1188     }
1189     if (comparer.Compare(lookup_value, value))
1190     {
1191     //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1192     //second_tmp_Results.Add(_tmp_result);
1193     //_tmp_result = null; // free memory
1194     //SearchArgs.Results.RemoveAt(i);
1195     SearchArgs.Results[i].Value = comparer.Value;
1196     second_tmp_Results.Add(SearchArgs.Results[i]);
1197     }
1198     comparer = null; // free memory
1199     }
1200     else
1201     {
1202 william 384 short lookup_value = BitConverter.ToInt16(buffered_mem,(int)address);
1203 william 280 _16bit_signed_comparer_ comparer = new _16bit_signed_comparer_(SearchArgs, SearchArgs.Results[i].Address);
1204     short value = 0;
1205     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1206     {
1207     value = Convert.ToInt16(SearchArgs.Results[i].Value);
1208     }
1209     else
1210     {
1211     value = Convert.ToInt16(SearchArgs.CompareStartValue);
1212     }
1213     if (comparer.Compare(lookup_value, value))
1214     {
1215     //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1216     //second_tmp_Results.Add(_tmp_result);
1217     //_tmp_result = null; // free memory
1218     //SearchArgs.Results.RemoveAt(i);
1219     SearchArgs.Results[i].Value = comparer.Value;
1220     second_tmp_Results.Add(SearchArgs.Results[i]);
1221     }
1222     comparer = null; // free memory
1223     }
1224     break;
1225 william 385 #endregion
1226     #region case SearchDataTypes._32bits:
1227 william 280 case SearchDataTypes._32bits:
1228     if (SearchArgs.IsUnsignedDataType)
1229     {
1230 william 384 uint lookup_value = BitConverter.ToUInt32(buffered_mem,(int)address);
1231 william 280 _32bit_unsigned_comparer_ comparer = new _32bit_unsigned_comparer_(SearchArgs, SearchArgs.Results[i].Address);
1232     uint value = 0;
1233     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1234     {
1235     value = Convert.ToUInt32(SearchArgs.Results[i].Value);
1236     comparer.Value = value;
1237     }
1238     else
1239     {
1240     value = Convert.ToUInt32(SearchArgs.CompareStartValue);
1241     comparer.Value = value;
1242     }
1243     if (comparer.Compare(lookup_value, value))
1244     {
1245     //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1246     //second_tmp_Results.Add(_tmp_result);
1247     //_tmp_result = null; // free memory
1248     //SearchArgs.Results.RemoveAt(i);
1249     SearchArgs.Results[i].Value = comparer.Value;
1250     second_tmp_Results.Add(SearchArgs.Results[i]);
1251     }
1252     comparer = null; // free memory
1253     }
1254     else
1255     {
1256 william 384 int lookup_value =BitConverter.ToInt32(buffered_mem,(int)address);
1257 william 280 _32bit_signed_comparer_ comparer = new _32bit_signed_comparer_(SearchArgs, SearchArgs.Results[i].Address);
1258     int value = 0;
1259     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1260     {
1261     value = Convert.ToInt32(SearchArgs.Results[i].Value);
1262     }
1263     else
1264     {
1265     value = Convert.ToInt32(SearchArgs.CompareStartValue);
1266     }
1267     if (comparer.Compare(lookup_value, value))
1268     {
1269     //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1270     //second_tmp_Results.Add(_tmp_result);
1271     //_tmp_result = null; // free memory
1272     //SearchArgs.Results.RemoveAt(i);
1273     SearchArgs.Results[i].Value = comparer.Value;
1274     second_tmp_Results.Add(SearchArgs.Results[i]);
1275     }
1276     comparer = null; // free memory
1277     }
1278     break;
1279 william 385 #endregion
1280     #region case SearchDataTypes._64bits:
1281 william 280 case SearchDataTypes._64bits:
1282     if (SearchArgs.IsUnsignedDataType)
1283     {
1284 william 384 ulong lookup_value = BitConverter.ToUInt64(buffered_mem,(int)address);
1285 william 280 _64bit_unsigned_comparer_ comparer = new _64bit_unsigned_comparer_(SearchArgs, SearchArgs.Results[i].Address);
1286     ulong value = 0;
1287     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1288     {
1289     value = Convert.ToUInt64(SearchArgs.Results[i].Value);
1290     comparer.Value = value;
1291     }
1292     else
1293     {
1294     value = Convert.ToUInt64(SearchArgs.CompareStartValue);
1295     comparer.Value = value;
1296     }
1297     if (comparer.Compare(lookup_value, value))
1298     {
1299     //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1300     //second_tmp_Results.Add(_tmp_result);
1301     //_tmp_result = null; // free memory
1302     //SearchArgs.Results.RemoveAt(i);
1303     SearchArgs.Results[i].Value = comparer.Value;
1304     second_tmp_Results.Add(SearchArgs.Results[i]);
1305     }
1306     comparer = null; // free memory
1307     }
1308     else
1309     {
1310 william 384 long lookup_value = BitConverter.ToInt64(buffered_mem,(int)address);
1311 william 280 _64bit_signed_comparer_ comparer = new _64bit_signed_comparer_(SearchArgs, SearchArgs.Results[i].Address);
1312     long value = 0;
1313     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1314     {
1315     value = Convert.ToInt64(SearchArgs.Results[i].Value);
1316     }
1317     else
1318     {
1319     value = Convert.ToInt64(SearchArgs.CompareStartValue);
1320     }
1321     if (comparer.Compare(lookup_value, value))
1322     {
1323     //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1324     //second_tmp_Results.Add(_tmp_result);
1325     //_tmp_result = null; // free memory
1326     //SearchArgs.Results.RemoveAt(i);
1327     SearchArgs.Results[i].Value = comparer.Value;
1328     second_tmp_Results.Add(SearchArgs.Results[i]);
1329     }
1330     comparer = null; // free memory
1331     }
1332     break;
1333     #endregion
1334 william 385 #endregion
1335 william 280 }
1336    
1337     double double_percent_done = 100.0 * (double)((double)i / (double)SearchArgs.Results.Count);
1338     int int_percent_done = (int)double_percent_done;
1339 william 384 if (int_percent_done != Last_Whole_Percent_Done && i % ElementsBeforeDisplay == 0)
1340 william 356 {
1341     resultsprogress.Value = int_percent_done;
1342 william 388 //resultsprogress.Message = string.Format(" -> Reading Address: 0x{0:x8}", i);
1343 william 356 Last_Whole_Percent_Done = int_percent_done;
1344 william 388 //Application.DoEvents();
1345 william 356 }
1346 william 280
1347     }
1348 william 392 st_nonrange_search.Stop();
1349     logger.Profiler.WriteLine("Non-Ranged search took a total of {0} seconds to complete.", st_nonrange_search.Elapsed.TotalSeconds);
1350 william 280 #endregion
1351     }
1352     #region Ranged Searches
1353     #if !DONOT_HAVE_RANGED_SEARCH_SUPPORT
1354     if (SearchArgs.CompareType == SearchCompareTypes.Between || SearchArgs.CompareType == SearchCompareTypes.NotBetween)
1355     {
1356 william 392 st_ranged_search.Start();
1357 william 280 object start, end;
1358    
1359     start = SearchArgs.CompareStartValue;
1360     end = SearchArgs.CompareEndValue;
1361     for (int i = 0; i < SearchArgs.Results.Count; i += 1)
1362     {
1363 william 384 uint address = SearchArgs.Results[i].Address;
1364 william 385 if (MemoryRangeStart > 0 && !SearchArgs.IsFirstSearch) { address = address - MemoryRangeStart; }
1365 william 384 //r_ms.BaseStream.Seek(SearchArgs.Results[i].Address, SeekOrigin.Begin);
1366 william 280 if (SearchArgs.CompareType == SearchCompareTypes.Between)
1367     {
1368 william 385 InRangeComparer comparer = new InRangeComparer(address, 0);
1369 william 384 if (comparer.Compare(start, end, SearchArgs.DataType, SearchArgs.IsUnsignedDataType, buffered_mem))
1370 william 280 {
1371     SearchArgs.Results[i].Value = comparer.Value;
1372     second_tmp_Results.Add(SearchArgs.Results[i]);
1373     }
1374     comparer = null;
1375     }
1376     else if (SearchArgs.CompareType == SearchCompareTypes.NotBetween)
1377     {
1378 william 385 NotInRangeComparer comparer = new NotInRangeComparer(address, 0);
1379 william 384 if (comparer.Compare(start, end, SearchArgs.DataType, SearchArgs.IsUnsignedDataType, buffered_mem))
1380 william 280 {
1381     SearchArgs.Results[i].Value = comparer.Value;
1382     second_tmp_Results.Add(SearchArgs.Results[i]);
1383     }
1384     comparer = null;
1385     }
1386     else
1387     {
1388     throw new InvalidOperationException("Encounted unkown range search type: " + SearchArgs.CompareType);
1389     }
1390     double double_percent_done = 100.0 * (double)((double)i / (double)SearchArgs.Results.Count);
1391     int int_percent_done = (int)double_percent_done;
1392 william 384 if (int_percent_done != Last_Whole_Percent_Done && i % ElementsBeforeDisplay == 0)
1393 william 356 {
1394     resultsprogress.Value = int_percent_done;
1395 william 388 //resultsprogress.Message = string.Format(" -> Reading Address: 0x{0:x8}", i);
1396 william 356 Last_Whole_Percent_Done = int_percent_done;
1397 william 388 //Application.DoEvents();
1398 william 356 }
1399 william 280 }
1400 william 392 st_ranged_search.Stop();
1401     logger.Profiler.WriteLine("Ranged search took a total of {0} seconds to complete.", st_ranged_search.Elapsed.TotalSeconds);
1402 william 280 }
1403     #endif
1404     #endregion
1405    
1406     }
1407     #endregion
1408     // leave SearchArgs.Results alone, if false
1409     if (NeedToCompare)
1410     {
1411     SearchArgs.Results = second_tmp_Results.GetRange(0, second_tmp_Results.Count);
1412 william 385 // fix addresses when memory start is not zero
1413     if (MemoryRangeStart > 0 && SearchArgs.IsFirstSearch) { for (int i = 0; i < SearchArgs.Results.Count; i++) { SearchArgs.Results[i].Address = SearchArgs.Results[i].Address + MemoryRangeStart; } }
1414 william 280 second_tmp_Results = null; // free memory
1415     }
1416    
1417 william 384 //r_ms.Close();
1418 william 230 }
1419    
1420     private void SearchWorkerThread_ProgressChanged(object sender, ProgressChangedEventArgs e)
1421     {
1422 william 280 //if (SearchArgs.ProgressLogger != null)
1423     //{
1424     // resultsprogress.Value = e.ProgressPercentage;
1425     // //Application.DoEvents();
1426     //}
1427 william 230 }
1428    
1429     private void SearchWorkerThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
1430     {
1431 william 286 if (!e.Cancelled)
1432     {
1433     Stopwatch st = (e.Result as Stopwatch);
1434     st.Stop();
1435 william 392 logger.Profiler.WriteLine("Search took {0} seconds, overall, to complete.", st.Elapsed.TotalSeconds);
1436 william 286 }
1437    
1438 william 280 resultsprogress.Value = 100;
1439 william 286 logger.Debug.WriteLine(string.Format("Results Count -> 0x{0:x8}", SearchArgs.Results.Count));
1440 william 230
1441 william 280 if (SearchArgs.Results.Count == 1) // debug message for 1 result using 32bit unsigned
1442 william 286 logger.Debug.WriteLine(string.Format("Debug: Found 1 result -> Address: 0x{0:x8} Value: 0x{1:x8}", SearchArgs.Results[0].Address, SearchArgs.Results[0].Value));
1443 william 280
1444 william 286 logger.Info.WriteLine(string.Format("Found 0x{0:x8} results", SearchArgs.Results.Count));
1445 william 280
1446     if (SearchArgs.Results.Count <= MIN_NUMBER_OF_RESULTS_BEFORE_DISPLAY)
1447     {
1448     lstResults.Items.Clear();
1449     List<ResultItem> items = new List<ResultItem>();
1450     for (int i = 0; i < SearchArgs.Results.Count; i++)
1451     {
1452     ResultItem item = new ResultItem(0, false);
1453     //item.Text = string.Format("0x{0:x8}", SearchArgs.Results[i].Address);
1454     //item.SubItems.Add(string.Format("0x{0:x8}", SearchArgs.Results[i].Address));
1455     switch (SearchArgs.DataType)
1456     {
1457    
1458     case SearchDataTypes._8bits:
1459     if (SearchArgs.IsUnsignedDataType) { item = new ResultItem(SearchArgs.Results[i].Address, false, Convert.ToByte(SearchArgs.Results[i].Value)); }
1460     else { item = new ResultItem(SearchArgs.Results[i].Address, false, Convert.ToSByte(SearchArgs.Results[i].Value)); }
1461     break;
1462     case SearchDataTypes._16bits:
1463     if (SearchArgs.IsUnsignedDataType) { item = new ResultItem(SearchArgs.Results[i].Address, false, Convert.ToUInt16(SearchArgs.Results[i].Value)); }
1464     else { item = new ResultItem(SearchArgs.Results[i].Address, false, Convert.ToInt16(SearchArgs.Results[i].Value)); }
1465     break;
1466     case SearchDataTypes._32bits:
1467     if (SearchArgs.IsUnsignedDataType) { item = new ResultItem(SearchArgs.Results[i].Address, false, Convert.ToUInt32(SearchArgs.Results[i].Value)); }
1468     else { item = new ResultItem(SearchArgs.Results[i].Address, false, Convert.ToInt32(SearchArgs.Results[i].Value)); }
1469     break;
1470     case SearchDataTypes._64bits:
1471     if (SearchArgs.IsUnsignedDataType) { item = new ResultItem(SearchArgs.Results[i].Address, false, Convert.ToUInt64(SearchArgs.Results[i].Value)); }
1472     else { item = new ResultItem(SearchArgs.Results[i].Address, false, Convert.ToInt64(SearchArgs.Results[i].Value)); }
1473     break;
1474     }
1475    
1476     if (!items.Contains(item))
1477     items.Add(item);
1478     }
1479     lstResults.Items.AddRange(items.ToArray());
1480     }
1481    
1482     this.DoSearchDoneSpecific();
1483     //System.Threading.Thread.Sleep(100);
1484     //if (_SEARCH_MODE != SearchMode.SEARCH_MODE_NORMAL_WITH_JOKER)
1485     this.ThawResultsUpdate();
1486     Application.DoEvents();
1487 william 230 }
1488 william 280 private void DoSearchDoneSpecific()
1489     {
1490     SearchWorkerThread.CancelAsync();
1491     if (lstResults.Items.Count > 0) { timer_update_results.Enabled = true; }
1492     else { timer_update_results.Enabled = false; }
1493 william 230
1494 william 280 search_progress_updater.Enabled = false;
1495    
1496     btnCancel.Enabled = false;
1497     btnReset.Enabled = true;
1498     btnSearch.Enabled = true;
1499     grpCompareType.Enabled = true;
1500     grpCompareValue.Enabled = true;
1501     resultsprogress.Value = 0;
1502     resultsprogress.Message = "";
1503     grpDataType.Enabled = false;
1504     // resume process on reset, incase it was suspended
1505     ThreadControl.ResumeProcess(this.AcceptedProcess.Id);
1506     //Application.DoEvents();
1507     this.Refresh();
1508     }
1509    
1510     private void DoCancelSpecific()
1511     {
1512     this.DoSearchDoneSpecific();
1513     }
1514     private void DoResetSpecific()
1515     {
1516     this.DoCancelSpecific();
1517     IsFirstSearch = true;
1518     grpDataType.Enabled = true;
1519     }
1520 william 230 private void search_progress_updater_Tick(object sender, EventArgs e)
1521     {
1522     if ((this.AcceptedProcess ==null) || Process.GetProcessById(this.AcceptedProcess.Id) == null)
1523     {
1524     SearchWorkerThread.CancelAsync();
1525     //JokerSearchWorker.CancelAsync();
1526     ResultsUpdateWorkerThread.CancelAsync();
1527     }
1528     }
1529    
1530 william 280 #region Search Button
1531 william 230 private void btnSearch_Click(object sender, EventArgs e)
1532     {
1533 william 271 this.SearchInProgess = true;
1534 william 280 //btnCancel.Enabled = true;
1535     //btnReset.Enabled = false;
1536     //btnSearch.Enabled = false;
1537     this.FreezeResultsUpdate();
1538     this.handle_btnSearch_Click();
1539     }
1540     private void handle_btnSearch_Click()
1541     {
1542     //this.FreezeResultsUpdate();
1543     lstResults.Items.Clear();
1544    
1545     if (lstResults.Items.Count > 0) { timer_update_results.Enabled = true; }
1546     else { timer_update_results.Enabled = false; }
1547    
1548    
1549     resultsprogress.Value = 0;
1550     bool _is_unsigned = chkUnsigned.Checked;
1551     SearchType search_type = new SearchType();
1552     SearchDataTypes _data_type = new SearchDataTypes();
1553     SearchCompareTypes _compare_type = new SearchCompareTypes();
1554     CompareValueTypes _compare_value_type = new CompareValueTypes();
1555     object start_value = 0;
1556     object end_value = 0;
1557     // get datatype
1558     if (radio_8bits.Checked) { _data_type = SearchDataTypes._8bits; }
1559     else if (radio_16bits.Checked) { _data_type = SearchDataTypes._16bits; }
1560     else if (radio_32bits.Checked) { _data_type = SearchDataTypes._32bits; }
1561     else if (radio_64bits.Checked) { _data_type = SearchDataTypes._64bits; }
1562     else { logger.Error.WriteLine("Could not determine search data type bit size. (was not 8/16/32/or 64bits)"); }
1563     // get compare type
1564     if (radiocompare_equal.Checked) { _compare_type = SearchCompareTypes.Equal; }
1565     else if (radiocompare_greaterthan.Checked) { _compare_type = SearchCompareTypes.GreaterThan; }
1566     else if (radiocompare_lessthan.Checked) { _compare_type = SearchCompareTypes.LessThan; }
1567     else if (radiocompare_greaterthan_orequal.Checked) { _compare_type = SearchCompareTypes.GreaterThanOrEqual; }
1568     else if (radiocompare_lessthan_orequal.Checked) { _compare_type = SearchCompareTypes.LessThanOrEqual; }
1569     else if (radiocompare_notequal.Checked) { _compare_type = SearchCompareTypes.NotEqual; }
1570     else if (radiocompare_between.Checked) { _compare_type = SearchCompareTypes.Between; }
1571     else if (radiocompare_notbetween.Checked) { _compare_type = SearchCompareTypes.NotBetween; }
1572     else { logger.Error.WriteLine("Could not determine search comparison type. (was not == > < >= <= != <> or !<>)"); }
1573     // get compare valure type
1574     if (radio_oldvalue.Checked) { _compare_value_type = CompareValueTypes.OldValue; }
1575     else if (radio_specificvalue.Checked) { _compare_value_type = CompareValueTypes.SpecificValue; }
1576     else { logger.Error.WriteLine("Could not determine search comparison type. (was not old or specific value"); }
1577    
1578     if (_compare_value_type == CompareValueTypes.SpecificValue || (_compare_type == SearchCompareTypes.Between || _compare_type == SearchCompareTypes.NotBetween))
1579     {
1580    
1581     switch (_data_type)
1582     {
1583     case SearchDataTypes._8bits:
1584     if (_is_unsigned) { start_value = txtStartAddr.ToByte(); }
1585     else { start_value = txtStartAddr.ToSByte(); }
1586     break;
1587     case SearchDataTypes._16bits:
1588     if (_is_unsigned) { start_value = txtStartAddr.ToUInt16(); }
1589     else { start_value = txtStartAddr.ToInt16(); }
1590     break;
1591     case SearchDataTypes._32bits:
1592     if (_is_unsigned) { start_value = txtStartAddr.ToUInt32(); }
1593     else { start_value = txtStartAddr.ToInt32(); }
1594     break;
1595     case SearchDataTypes._64bits:
1596     if (_is_unsigned) { start_value = txtStartAddr.ToUInt64(); }
1597     else { start_value = txtStartAddr.ToInt64(); }
1598     break;
1599     default: throw new InvalidOperationException("In SearchType(): Encounterd an Unkown Search Data Type.");
1600     }
1601     }
1602     if (_compare_type == SearchCompareTypes.Between || _compare_type == SearchCompareTypes.NotBetween)
1603     {
1604     switch (_data_type)
1605     {
1606     case SearchDataTypes._8bits:
1607     if (_is_unsigned) { end_value = txtEndAddr.ToByte(); }
1608     else { end_value = txtEndAddr.ToSByte(); }
1609     break;
1610     case SearchDataTypes._16bits:
1611     if (_is_unsigned) { end_value = txtEndAddr.ToUInt16(); }
1612     else { end_value = txtEndAddr.ToInt16(); }
1613     break;
1614     case SearchDataTypes._32bits:
1615     if (_is_unsigned) { end_value = txtEndAddr.ToUInt32(); }
1616     else { end_value = txtEndAddr.ToInt32(); }
1617     break;
1618     case SearchDataTypes._64bits:
1619     if (_is_unsigned) { end_value = txtEndAddr.ToUInt64(); }
1620     else { end_value = txtEndAddr.ToInt64(); }
1621     break;
1622     default: throw new InvalidOperationException("In SearchType(): Encounterd an Unkown Search Data Type.");
1623     }
1624     }
1625    
1626     search_type = new SearchType(_data_type, _is_unsigned, _compare_type, _compare_value_type, start_value, end_value, resultsprogress);
1627    
1628     //search_type.LogSearchOptions();
1629    
1630     search_type.IsFirstSearch = IsFirstSearch;
1631    
1632    
1633    
1634     DoSearch(search_type);
1635     IsFirstSearch = false;
1636     }
1637     private void DoSearch(SearchType args)
1638     {
1639     if (!args.IsFirstSearch && SearchArgs != null)
1640     {
1641     //args.Results.AddRange(SearchArgs.Results.ToArray());
1642     args.Results = SearchArgs.Results;
1643     }
1644     SearchArgs = args;
1645     #if DONOT_HAVE_RANGED_SEARCH_SUPPORT
1646     if (SearchArgs.CompareType == SearchCompareTypes.Between || SearchArgs.CompareType == SearchCompareTypes.NotBetween)
1647     {
1648     throw new NotImplementedException("Between and Not Between Range searches have not been implemented.");
1649     }
1650     #endif
1651     search_progress_updater.Enabled = true;
1652     //padPluginSelector.Enabled = false;
1653     //gsPluginSelector.Enabled = false;
1654 william 271 btnReset.Enabled = false;
1655     btnSearch.Enabled = false;
1656 william 280 btnCancel.Enabled = true;
1657     grpDataType.Enabled = false;
1658     grpCompareType.Enabled = false;
1659     grpCompareValue.Enabled = false;
1660     this.Refresh();
1661     Application.DoEvents();
1662     SearchWorkerThread.RunWorkerAsync();
1663 william 230 }
1664 william 280 #endregion
1665 william 271 private void btnReset_Click(object sender, EventArgs e)
1666     {
1667     this.SearchInProgess = false;
1668 william 280 //btnSearch.Enabled = true;
1669     //btnCancel.Enabled = false;
1670     this.DoResetSpecific();
1671     lstResults.Items.Clear();
1672     try { SearchArgs.Results = new List<ResultType<object>>(); }
1673     catch { }
1674 william 271 }
1675    
1676     private void btnCancel_Click(object sender, EventArgs e)
1677     {
1678     this.SearchInProgess = false;
1679 william 280 //btnCancel.Enabled = false;
1680     //btnSearch.Enabled = true;
1681     //btnReset.Enabled = true;
1682     this.DoCancelSpecific();
1683 william 271 }
1684 william 275
1685     private void mnuItemPatchListViewMemoryRegion_Click(object sender, EventArgs e)
1686     {
1687     List<ResultDataType> patch_list = new List<ResultDataType>();
1688     List<int> SelectedIndexes = new List<int>();
1689     foreach (int index in lstPatchList.SelectedIndices) { SelectedIndexes.Add(index); }
1690     foreach (int index in SelectedIndexes)
1691     {
1692     ListViewItem item = lstPatchList.Items[index];
1693     ResultDataType rdt = (ResultDataType)item.Tag;
1694     ViewMemoryRegion(rdt);
1695     break; // only get the fist item
1696     }
1697     }
1698    
1699     private void mnuItemResultsListViewMemoryRegion_Click(object sender, EventArgs e)
1700     {
1701     List<ResultDataType> patch_list = new List<ResultDataType>();
1702     List<int> SelectedIndexes = new List<int>();
1703     foreach (int index in lstResults.SelectedIndices) { SelectedIndexes.Add(index); }
1704     foreach (int index in SelectedIndexes)
1705     {
1706     ListViewItem item = lstResults.Items[index];
1707     ResultDataType rdt = (ResultDataType)item.Tag;
1708     ViewMemoryRegion(rdt);
1709     break; // only get the fist item
1710     }
1711     }
1712     private void ViewMemoryRegion(ResultDataType rdt)
1713     {
1714     if (OnBrowseMemoryRegion != null)
1715 william 323 OnBrowseMemoryRegion(new BrowseMemoryRegionEvent(this, rdt.Address));
1716 william 275 }
1717 william 277
1718     private void mnuAddedResults_Opening(object sender, CancelEventArgs e)
1719     {
1720     if (!(lstPatchList.Items.Count > 0)) { mnuItemRemoveResult.Visible = false; e.Cancel = true; }
1721     if (!(lstPatchList.Items.Count > 0)) { mnuItemPatchSelectedEntry.Visible = false; e.Cancel = true; }
1722     if (e.Cancel) return;
1723     if (lstPatchList.Items.Count > 0) mnuItemRemoveResult.Visible = true;
1724     if (lstPatchList.Items.Count > 0) mnuItemPatchSelectedEntry.Visible = true;
1725    
1726     if (!(lstPatchList.Items.Count > 0)) { mnuItemFreezeSelectedPatches.Visible = false; e.Cancel = true; }
1727     if (!(lstPatchList.Items.Count > 0)) { mnuItemThawSelectedPatches.Visible = false; e.Cancel = true; }
1728     if (e.Cancel) return;
1729    
1730     if (lstPatchList.Items.Count > 0) mnuItemFreezeSelectedPatches.Visible = true;
1731     if (lstPatchList.Items.Count > 0) mnuItemThawSelectedPatches.Visible = true;
1732    
1733     if (lstPatchList.SelectedItems.Count == 0) e.Cancel = true;
1734     if (e.Cancel) return;
1735    
1736     }
1737    
1738     private void mnuResults_Opening(object sender, CancelEventArgs e)
1739     {
1740     if (!(lstResults.Items.Count > 0)) e.Cancel = true;
1741     if (lstResults.SelectedItems.Count == 0) e.Cancel = true;
1742     if (SearchArgs == null) e.Cancel = true;
1743     if (e.Cancel) return;
1744     }
1745 william 287
1746     private void chkMemoryRangeExpertMode_CheckedChanged(object sender, EventArgs e)
1747     {
1748 william 288 txtMemoryRangeStart.ReadOnly = !chkMemoryRangeExpertMode.Checked;
1749     txtMemoryRangeSize.ReadOnly = !chkMemoryRangeExpertMode.Checked;
1750 william 287 }
1751    
1752 william 378 private void txtMemoryRangeStart_ValueChanged(object sender, ValueChangedEventArgs e) { this.MemoryRangeStart = Convert.ToUInt32(e.NewValue); }
1753 william 288 private void txtMemoryRangeSize_ValueChanged(object sender, ValueChangedEventArgs e) { this.MemoryRangeSize = Convert.ToUInt32(e.NewValue); }
1754 william 287
1755 william 227 }
1756     }

  ViewVC Help
Powered by ViewVC 1.1.22