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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 398 - (hide annotations) (download)
Wed Jun 20 12:16:34 2012 UTC (8 years, 4 months ago) by william
File size: 92702 byte(s)
MemmoryProvider: using statements

1 william 395 //#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 william 398 using (GenericMemoryProvider provider = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
436 william 230 {
437 william 398 provider.OpenProvider();
438     int bytesReadSize;
439     byte[] data;
440     uint bytesToRead = 0;
441     switch (_result.ValueType)
442     {
443     case SearchDataTypes._8bits:
444     bytesToRead = 1;
445     break;
446     case SearchDataTypes._16bits:
447     bytesToRead = 2;
448     break;
449     case SearchDataTypes._32bits:
450     bytesToRead = 4;
451     break;
452     case SearchDataTypes._64bits:
453     bytesToRead = 8;
454     break;
455     }
456     provider.ReadProcessMemory(Address, bytesToRead, out bytesReadSize, out data);
457     using (MemoryStream ms = new MemoryStream(data))
458     {
459     using (BinaryReader r_ms = new BinaryReader(ms))
460     {
461     switch (_result.ValueType)
462     {
463     case SearchDataTypes._8bits:
464     if (_result.IsUnsigned) { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x2}", r_ms.ReadByte()); }
465     else { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x2}", r_ms.ReadSByte()); }
466     break;
467     case SearchDataTypes._16bits:
468     if (_result.IsUnsigned) { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x4}", r_ms.ReadUInt16()); }
469     else { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x4}", r_ms.ReadInt16()); }
470     break;
471     case SearchDataTypes._32bits:
472     if (_result.IsUnsigned) { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x8}", r_ms.ReadUInt32()); }
473     else { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x8}", r_ms.ReadInt32()); }
474     break;
475     case SearchDataTypes._64bits:
476     if (_result.IsUnsigned) { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x16}", r_ms.ReadUInt64()); }
477     else { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x16}", r_ms.ReadInt64()); }
478     break;
479     }
480     r_ms.Close();
481     }
482     }
483     provider.CloseProvider();
484 william 230 }
485 william 398 //Application.DoEvents();
486 william 230 }
487     #endregion
488    
489     #region Update Added Results List
490     AddedItems = new List<ListViewItem>();
491     //r_ms.BaseStream.Seek(0, SeekOrigin.Begin);
492     for (int i = 0; i < lstPatchList.Items.Count; i++)
493     {
494     if (this.lstResults.InvokeRequired)
495     {
496     ThreadSafe_GetResultItem _get_item = new ThreadSafe_GetResultItem(GetResultItem);
497     object item = this.lstResults.Invoke(_get_item, new object[] { i, (int)eListViewResults.PATCH_RESULTS_LIST });
498     if (item != null)
499     AddedItems.Add((ListViewItem)item);
500     }
501     else
502     {
503     AddedItems.Add(lstPatchList.Items[i]);
504     }
505    
506     }
507     for (int i = 0; i < AddedItems.Count; i++)
508     {
509     if (ResultsUpdateWorkerThread.CancellationPending == true)
510     {
511     e.Cancel = true;
512     return;
513     }
514 william 249 int Address = 0;
515 william 230 ResultDataType _result = (ResultDataType)AddedItems[i].Tag;
516 william 249 Address = Convert.ToInt32(AddedItems[i].SubItems[col_Added_Address].Text, 16);
517 william 398 using (GenericMemoryProvider provider = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
518 william 230 {
519 william 398 provider.OpenProvider();
520     int bytesReadSize;
521     byte[] data;
522     uint bytesToRead = 0;
523     switch (_result.ValueType)
524     {
525     case SearchDataTypes._8bits:
526     bytesToRead = 1;
527     break;
528     case SearchDataTypes._16bits:
529     bytesToRead = 2;
530     break;
531     case SearchDataTypes._32bits:
532     bytesToRead = 4;
533     break;
534     case SearchDataTypes._64bits:
535     bytesToRead = 8;
536     break;
537     }
538     provider.ReadProcessMemory(Address, bytesToRead, out bytesReadSize, out data);
539     provider.CloseProvider();
540     using (MemoryStream ms = new MemoryStream(data))
541     {
542     using (BinaryReader r_ms = new BinaryReader(ms))
543     {
544     switch (_result.ValueType)
545     {
546     case SearchDataTypes._8bits:
547     if (_result.IsUnsigned) { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x2}", r_ms.ReadByte()); }
548     else { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x2}", r_ms.ReadSByte()); }
549     break;
550     case SearchDataTypes._16bits:
551     if (_result.IsUnsigned) { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x4}", r_ms.ReadUInt16()); }
552     else { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x4}", r_ms.ReadInt16()); }
553     break;
554     case SearchDataTypes._32bits:
555     if (_result.IsUnsigned) { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x8}", r_ms.ReadUInt32()); }
556     else { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x8}", r_ms.ReadInt32()); }
557     break;
558     case SearchDataTypes._64bits:
559     if (_result.IsUnsigned) { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x16}", r_ms.ReadUInt64()); }
560     else { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x16}", r_ms.ReadInt64()); }
561     break;
562     }
563     r_ms.Close();
564     }
565     }
566 william 230 }
567 william 398 //Application.DoEvents();
568 william 230 }
569     #endregion
570    
571    
572 william 229 }
573    
574     private void ResultsUpdateWorkerThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
575     {
576 william 230 try
577     {
578     //if ((lstResults.SelectedItems.Count > 0) && !PatchedValue_NeedsUpdate ) return;
579     //if ((lstPatchList.SelectedItems.Count > 0) && !PatchedValue_NeedsUpdate) return;
580     if (!this.ShouldUpdateResults()) return;
581     if (ResultItems.Count > 0)
582     {
583     //lstResults.Items.Clear();
584     //lstResults.Items.AddRange(ResultItems.ToArray());
585 william 229
586 william 230 for (int i = 0; i < ResultItems.Count; i++)
587     {
588     lstResults.Items[i].SubItems[new AVPColumnText(AVPColumnType.VALUE).ColumnIndex].Text =
589     ResultItems[i].SubItems[new AVPColumnText(AVPColumnType.VALUE).ColumnIndex].Text;
590     }
591    
592     }
593     if (AddedItems.Count > 0)
594     {
595     //lstPatchList.Items.Clear();
596     //lstPatchList.Items.AddRange(AddedItems.ToArray());
597    
598     for (int i = 0; i < AddedItems.Count; i++)
599     {
600     lstPatchList.Items[i].SubItems[new AVPColumnText(AVPColumnType.VALUE).ColumnIndex].Text =
601     AddedItems[i].SubItems[new AVPColumnText(AVPColumnType.VALUE).ColumnIndex].Text;
602     }
603    
604     }
605     PatchedValue_NeedsUpdate = false;
606     }
607     catch { }
608 william 229 }
609    
610     private void btnImportFile_Click(object sender, EventArgs e)
611     {
612     this.FreezeResultsUpdate();
613     if (!lstPatchList.ImportFromFile())
614     {
615     MessageBox.Show("Failed to Import Patch List from File.", "Import Failure", MessageBoxButtons.OK, MessageBoxIcon.Error);
616     this.ThawResultsUpdate();
617     return;
618     }
619     else
620     {
621     MessageBox.Show("Succesfully Imported Patch List from File.", "Import Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
622     this.ThawResultsUpdate();
623     return;
624     }
625     }
626     bool g_isFrozen = false;
627     private bool IsResultsUpdateFrozen
628     {
629     get { return g_isFrozen; }
630     set { g_isFrozen = value; }
631     }
632     private void ThawResultsUpdate()
633     {
634     this.IsResultsUpdateFrozen = false;
635     if (this.AcceptedProcess != null)
636     {
637     #if !DO_NOT_SUSPEND_RESUME_THREAD_ON_FREEZE
638     ThreadControl.ResumeProcess(this.AcceptedProcess.Id);
639     #endif
640     }
641     }
642    
643     private void FreezeResultsUpdate()
644     {
645     this.IsResultsUpdateFrozen = true;
646 william 285 //this.IsResultsUpdateFrozen = false;
647 william 229 if (this.AcceptedProcess != null)
648     {
649     #if !DO_NOT_SUSPEND_RESUME_THREAD_ON_FREEZE
650     ThreadControl.SuspendProcess(this.AcceptedProcess.Id);
651     #endif
652     }
653     }
654    
655     private void btnExportFile_Click(object sender, EventArgs e)
656     {
657     this.FreezeResultsUpdate();
658     if (!lstPatchList.ExportToFile())
659     {
660     MessageBox.Show("Failed to Export Patch List to File.", "Export Failure", MessageBoxButtons.OK, MessageBoxIcon.Error);
661     this.ThawResultsUpdate();
662     return;
663     }
664     else
665     {
666     MessageBox.Show("Succesfully Exported Patch List to File.", "Export Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
667     this.ThawResultsUpdate();
668     return;
669     }
670     }
671    
672     private void btnImportClipboard_Click(object sender, EventArgs e)
673     {
674     this.FreezeResultsUpdate();
675     if (!lstPatchList.ImportFromClipboard())
676     {
677     MessageBox.Show("Failed to Import Patch List from Clipboard.", "Import Failure", MessageBoxButtons.OK, MessageBoxIcon.Error);
678     this.ThawResultsUpdate();
679     return;
680     }
681     else
682     {
683     MessageBox.Show("Succesfully Import Patch List from Clipboard.", "Import Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
684     this.ThawResultsUpdate();
685     }
686     }
687    
688     private void btnExportClipboard_Click(object sender, EventArgs e)
689     {
690     this.FreezeResultsUpdate();
691     if (!lstPatchList.ExportToClipboard())
692     {
693     MessageBox.Show("Failed to Export Patch List to Clipboard.", "Export Failure", MessageBoxButtons.OK, MessageBoxIcon.Error);
694     this.ThawResultsUpdate();
695     return;
696     }
697     else
698     {
699     MessageBox.Show("Succesfully Exported Patch List to Clipboard.", "Export Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
700     this.ThawResultsUpdate();
701     return;
702     }
703     }
704    
705     private void btnAddPatchAddress_Click(object sender, EventArgs e)
706     {
707 william 245 PatchAdder adder = new PatchAdder((IAcceptsProcessAndConfig)this);
708 william 229 adder.ShowDialog();
709     if (adder.WasAPatchAdded) AddToPatchList(adder.AddedPatchValue);
710     }
711    
712     private void btnAddAddressRange_Click(object sender, EventArgs e)
713     {
714 william 245 PatchRangeAdder adder = new PatchRangeAdder((IAcceptsProcessAndConfig)this);
715 william 229 adder.ShowDialog();
716     if (adder.WasAPatchAdded) AddToPatchList(adder.AddedPatchValue);
717     }
718     private void AddToPatchList(List<ResultDataType> item) { foreach (ResultDataType data in item) { AddToPatchList(data); } }
719     private void AddToPatchList(ResultDataType item)
720     {
721     ResultItem item2 = null;
722     switch (item.ValueType)
723     {
724     case SearchDataTypes._8bits:
725     if (item.IsUnsigned) { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToByte(item.Value)); }
726     else { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToSByte(item.Value)); }
727     break;
728     case SearchDataTypes._16bits:
729     if (item.IsUnsigned) { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToUInt16(item.Value)); }
730     else { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToInt16(item.Value)); }
731     break;
732     case SearchDataTypes._32bits:
733     if (item.IsUnsigned) { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToUInt32(item.Value)); }
734     else { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToInt32(item.Value)); }
735     break;
736     case SearchDataTypes._64bits:
737     if (item.IsUnsigned) { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToUInt64(item.Value)); }
738     else { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToInt64(item.Value)); }
739     break;
740     }
741     this.AddToPatchList(item2);
742     }
743     private void AddToPatchList(ListViewItem item)
744     {
745     try
746     {
747     ResultDataType _result = (ResultDataType)item.Tag;
748     this.AddToPatchList(_result);
749     }
750     catch (InvalidCastException ex)
751     {
752     // unable to cast
753     MessageBox.Show(ex.Message, "Invalid Cast Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
754     }
755     catch (Exception ex)
756     {
757     // other exception
758     MessageBox.Show(ex.Message, "Unhandled Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
759     }
760     }
761     private void AddToPatchList(ResultItem item)
762     {
763     if (!lstPatchList.Items.Contains(item)) lstPatchList.Items.Add(item);
764     }
765     private void AddToPatchList(string address, SearchDataTypes bitsize, bool IsUnsigned)
766     {
767 william 245 ResultItemState state = new ResultItemState(address, bitsize, IsUnsigned, (IAcceptsProcessAndConfig)this);
768 william 229 ResultItem item = new ResultItem(state.Address, state.Value, state.Frozen, state.ValueType, state.IsUnsigned);
769     this.AddToPatchList(item);
770     }
771 william 230
772     private void mnuItemAddToPatchList_Click(object sender, EventArgs e)
773     {
774     if (!(lstResults.SelectedItems.Count > 0)) return;
775     //if (SearchArgs == null) return;
776    
777     try
778     {
779     for (int i = 0; i < lstResults.SelectedIndices.Count; i++)
780     {
781     //ResultDataType result = (ResultDataType)lstResults.Items[selected_index].Tag;
782     ListViewItem item = lstResults.Items[lstResults.SelectedIndices[i]];
783     this.AddToPatchList(item);
784     }
785     }
786     catch (Exception ex)
787     {
788     logger.Error.WriteLine(ex.ToString());
789     }
790     }
791    
792     private void mnuItemRemoveResult_Click(object sender, EventArgs e)
793     {
794     if (!(lstPatchList.SelectedItems.Count > 0)) return;
795     //if (SearchArgs == null) return;
796     try
797     {
798     this.FreezeResultsUpdate();
799     for (int i = 0; i < lstPatchList.SelectedIndices.Count; i++)
800     {
801     //lstPatchList.ThawItem(lstPatchList.SelectedIndices[i]);
802     lstPatchList.Items[lstPatchList.SelectedIndices[i]].Remove();
803     }
804     this.ThawResultsUpdate();
805     }
806     catch (Exception ex)
807     {
808     Debug.WriteLine(ex.ToString());
809     }
810     }
811     private void PatchRange(bool SingleEntry)
812     {
813     //if (SearchArgs == null) return;
814     #region Patch Selected Address
815     // stop ResultsUpdate Thread
816     ResultsUpdateWorkerThread.CancelAsync();
817    
818     List<ResultDataType> patch_list = new List<ResultDataType>();
819     List<int> SelectedIndexes = new List<int>();
820    
821     if (SingleEntry)
822     {
823     SelectedIndexes.Add(lstPatchList.SelectedIndices[0]);
824     }
825     else
826     {
827     foreach (int index in lstPatchList.SelectedIndices)
828     {
829     SelectedIndexes.Add(index);
830     }
831     }
832     //PCSX2MemoryProvider provider = new PCSX2MemoryProvider(this.SearchPCSX2ProcessPID, resultslog);
833     foreach (int index in SelectedIndexes)
834     {
835     if (SingleEntry)
836     {
837     SearchPatcher patcher = null;
838     uint Address = 0;
839     ListViewItem item = lstPatchList.Items[index];
840     ResultDataType _result = (ResultDataType)item.Tag;
841     Address = Convert.ToUInt32(item.SubItems[col_Found_Address].Text, 16);
842     switch (_result.ValueType)
843     {
844     case SearchDataTypes._8bits:
845     if (_result.IsUnsigned)
846     {
847     byte value = Convert.ToByte(item.SubItems[col_Found_Value].Text, 16);
848 william 243 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
849 william 230 timer_update_results.Enabled = false;
850     patcher.ShowDialog();
851     timer_update_results.Enabled = true;
852     PatchedValue_NeedsUpdate = true;
853     if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
854     ResultsUpdateWorkerThread.RunWorkerAsync();
855     }
856     else
857     {
858     sbyte value = Convert.ToSByte(item.SubItems[col_Found_Value].Text, 16);
859 william 243 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
860 william 230 timer_update_results.Enabled = false;
861     patcher.ShowDialog();
862     timer_update_results.Enabled = true;
863     PatchedValue_NeedsUpdate = true;
864     if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
865     ResultsUpdateWorkerThread.RunWorkerAsync();
866     }
867     break;
868     case SearchDataTypes._16bits:
869     if (_result.IsUnsigned)
870     {
871     ushort value = Convert.ToUInt16(item.SubItems[col_Found_Value].Text, 16);
872 william 243 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
873 william 230 timer_update_results.Enabled = false;
874     patcher.ShowDialog();
875     timer_update_results.Enabled = true;
876     PatchedValue_NeedsUpdate = true;
877     if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
878     ResultsUpdateWorkerThread.RunWorkerAsync();
879     }
880     else
881     {
882     short value = Convert.ToInt16(item.SubItems[col_Found_Value].Text, 16);
883 william 243 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
884 william 230 timer_update_results.Enabled = false;
885     patcher.ShowDialog();
886     timer_update_results.Enabled = true;
887     PatchedValue_NeedsUpdate = true;
888     if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
889     ResultsUpdateWorkerThread.RunWorkerAsync();
890     }
891     break;
892     case SearchDataTypes._32bits:
893     if (_result.IsUnsigned)
894     {
895     uint value = Convert.ToUInt32(item.SubItems[col_Found_Value].Text, 16);
896 william 243 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
897 william 230 timer_update_results.Enabled = false;
898     patcher.ShowDialog();
899     timer_update_results.Enabled = true;
900     PatchedValue_NeedsUpdate = true;
901     if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
902     ResultsUpdateWorkerThread.RunWorkerAsync();
903     }
904     else
905     {
906     int value = Convert.ToInt32(item.SubItems[col_Found_Value].Text, 16);
907 william 243 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
908 william 230 timer_update_results.Enabled = false;
909     patcher.ShowDialog();
910     timer_update_results.Enabled = true;
911     PatchedValue_NeedsUpdate = true;
912     if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
913     ResultsUpdateWorkerThread.RunWorkerAsync();
914     }
915     break;
916     case SearchDataTypes._64bits:
917     if (_result.IsUnsigned)
918     {
919     ulong value = Convert.ToUInt32(item.SubItems[col_Found_Value].Text, 16);
920 william 243 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
921 william 230 timer_update_results.Enabled = false;
922     patcher.ShowDialog();
923     timer_update_results.Enabled = true;
924     PatchedValue_NeedsUpdate = true;
925     if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
926     ResultsUpdateWorkerThread.RunWorkerAsync();
927     }
928     else
929     {
930     long value = Convert.ToInt32(item.SubItems[col_Found_Value].Text, 16);
931 william 243 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
932 william 230 timer_update_results.Enabled = false;
933     patcher.ShowDialog();
934     timer_update_results.Enabled = true;
935     PatchedValue_NeedsUpdate = true;
936     if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
937     ResultsUpdateWorkerThread.RunWorkerAsync();
938     }
939     break;
940     }
941     }
942     else
943     {
944    
945     ListViewItem item = lstPatchList.Items[index];
946     ResultDataType _result = (ResultDataType)item.Tag;
947     patch_list.Add(_result);
948     }
949     }
950    
951     if (patch_list.Count > 0)
952     {
953 william 243 SearchRangePatcher rangePatcher = new SearchRangePatcher((IAcceptsProcessAndConfig)this, patch_list);
954 william 230 rangePatcher.ShowDialog();
955     }
956    
957     #endregion
958     }
959     private void mnuItemPatchSelectedEntry_Click(object sender, EventArgs e)
960     {
961     if (!(lstPatchList.SelectedItems.Count == 1)) return;
962     PatchRange(true);
963     }
964    
965     private void mnuItemPatchSelectedRange_Click(object sender, EventArgs e)
966     {
967     if (!(lstPatchList.SelectedItems.Count >= 1)) return;
968     PatchRange(false);
969     }
970    
971     private void mnuItemFreezeSelectedPatches_Click(object sender, EventArgs e)
972     {
973     if (!(lstPatchList.SelectedItems.Count > 0)) return;
974     //if (SearchArgs == null) return;
975     try
976     {
977     lstPatchList.ProcessID = this.AcceptedProcess.Id;
978     this.FreezeResultsUpdate();
979     for (int i = 0; i < lstPatchList.SelectedIndices.Count; i++)
980     {
981     lstPatchList.FreezeItem(lstPatchList.SelectedIndices[i]);
982     }
983     // force thaw and update
984     this.ThawResultsUpdate();
985     this.Update();
986     }
987     catch (Exception ex)
988     {
989     Debug.WriteLine(ex.ToString());
990     }
991     }
992    
993     private void mnuItemThawSelectedPatches_Click(object sender, EventArgs e)
994     {
995     if (!(lstPatchList.SelectedItems.Count > 0)) return;
996     //if (SearchArgs == null) return;
997     try
998     {
999     lstPatchList.ProcessID = this.AcceptedProcess.Id;
1000     this.FreezeResultsUpdate();
1001     for (int i = 0; i < lstPatchList.SelectedIndices.Count; i++)
1002     {
1003     lstPatchList.ThawItem(lstPatchList.SelectedIndices[i]);
1004     }
1005     // force thaw and update
1006     this.ThawResultsUpdate();
1007     this.Update();
1008     }
1009     catch (Exception ex)
1010     {
1011     Debug.WriteLine(ex.ToString());
1012     }
1013     }
1014    
1015     private void SearchWorkerThread_DoWork(object sender, DoWorkEventArgs e)
1016     {
1017 william 286 Stopwatch st = new Stopwatch();
1018     st.Start();
1019 william 392
1020     Stopwatch st_first_search = new Stopwatch();
1021     Stopwatch st_nonrange_search = new Stopwatch();
1022     Stopwatch st_ranged_search = new Stopwatch();
1023    
1024 william 286 e.Result = st;
1025 william 280 //List<ResultType<object>> tmp_Results = new List<ResultType<object>>();
1026     List<ResultType<object>> second_tmp_Results = new List<ResultType<object>>();
1027 william 384 const int ElementsBeforeDisplay = 100;
1028     //const double _UPDATE_DELAY = 1024.0;
1029     //double UPDATE_DELAY = 1;
1030 william 280 //tmp_Results = SearchArgs.Results.GetRange(0,SearchArgs.Results.Count);
1031     //SearchArgs.Results = null;
1032     //SearchArgs.Results.Clear();
1033     // log options
1034     SearchArgs.LogSearchOptions();
1035 william 398 uint STEP_SIZE = (uint)SearchArgs.DataType / 8;
1036 william 384 byte[] buffered_mem = new byte[(MemoryRangeSize - MemoryRangeStart)]; // throws OutOfMemoryException if size is over 2G
1037 william 398 using (GenericMemoryProvider provider = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1038     {
1039     provider.OpenProvider();
1040     int bytes_read = 0;
1041     provider.ReadProcessMemoryAtOnce(MemoryRangeStart, (MemoryRangeSize - MemoryRangeStart), out bytes_read, out buffered_mem);
1042     provider.CloseProvider();
1043     }
1044 william 280 if (buffered_mem.Length == 0) { logger.Warn.WriteLine("Buffered Memory is Zero Length."); return; }
1045 william 398 using (MemoryStream ms = new MemoryStream(buffered_mem))
1046 william 280 {
1047 william 398 using (BinaryReader r_ms = new BinaryReader(ms))
1048 william 280 {
1049 william 398 logger.Debug.WriteLine(string.Format("Buffered Memory Size -> 0x{0:x8}", buffered_mem.Length));
1050     int Last_Whole_Percent_Done = 0;
1051    
1052     #region First Search
1053     if (SearchArgs.IsFirstSearch)
1054 william 280 {
1055 william 398 st_first_search.Start();
1056     SearchArgs.Results.Clear();
1057     r_ms.BaseStream.Seek(0, SeekOrigin.Begin);
1058     //List<ResultType<object>> results_list = new List<ResultType<object>>();
1059     //for (uint i = 0; i < buffered_mem.Length; i += STEP_SIZE)
1060     //{
1061     while (r_ms.BaseStream.Position < r_ms.BaseStream.Length)
1062     {
1063     using (ResultType<object> _tmp_result = new ResultType<object>())
1064     {
1065     switch (SearchArgs.DataType)
1066     {
1067     case SearchDataTypes._8bits:
1068     if (SearchArgs.IsUnsignedDataType) { _tmp_result.Address = (uint)r_ms.BaseStream.Position; _tmp_result.Value = r_ms.ReadByte(); }
1069     else { _tmp_result.Address = (uint)r_ms.BaseStream.Position; _tmp_result.Value = r_ms.ReadSByte(); } break;
1070     case SearchDataTypes._16bits:
1071     if (SearchArgs.IsUnsignedDataType) { _tmp_result.Address = (uint)r_ms.BaseStream.Position; _tmp_result.Value = r_ms.ReadUInt16(); }
1072     else { _tmp_result.Address = (uint)r_ms.BaseStream.Position; _tmp_result.Value = r_ms.ReadInt16(); } break;
1073     case SearchDataTypes._32bits:
1074     if (SearchArgs.IsUnsignedDataType) { _tmp_result.Address = (uint)r_ms.BaseStream.Position; _tmp_result.Value = r_ms.ReadUInt32(); }
1075     else { _tmp_result.Address = (uint)r_ms.BaseStream.Position; _tmp_result.Value = r_ms.ReadInt32(); } break;
1076     case SearchDataTypes._64bits:
1077     if (SearchArgs.IsUnsignedDataType) { _tmp_result.Address = (uint)r_ms.BaseStream.Position; _tmp_result.Value = r_ms.ReadUInt64(); }
1078     else { _tmp_result.Address = (uint)r_ms.BaseStream.Position; _tmp_result.Value = r_ms.ReadInt64(); } break;
1079     }
1080     //results_list.Add(_tmp_result);
1081     SearchArgs.Results.Add(_tmp_result);
1082     }
1083     double double_percent_done = 100.0 * (double)((double)r_ms.BaseStream.Position / (double)buffered_mem.Length);
1084     int int_percent_done = (int)double_percent_done;
1085     if (int_percent_done != Last_Whole_Percent_Done && r_ms.BaseStream.Position % ElementsBeforeDisplay == 0)
1086     {
1087     resultsprogress.Value = int_percent_done;
1088     resultsprogress.Message = string.Format(" -> Reading Address: 0x{0:x8}", r_ms.BaseStream.Position);
1089     Last_Whole_Percent_Done = int_percent_done;
1090     //Application.DoEvents();
1091     }
1092    
1093     if (SearchWorkerThread.CancellationPending == true)
1094     {
1095     e.Cancel = true;
1096     return;
1097     }
1098     //Application.DoEvents();
1099     }
1100     //}
1101     //SearchArgs.Results.AddRange(results_list);
1102     //results_list = null;
1103     resultsprogress.Value = 100;
1104     resultsprogress.Message = "";
1105 william 388 //Application.DoEvents();
1106 william 398 st_first_search.Stop();
1107     logger.Profiler.WriteLine("First search took a total of {0} seconds to complete.", st_first_search.Elapsed.TotalSeconds);
1108 william 356 }
1109 william 398 #endregion
1110 william 280
1111 william 398 #region Subsequent Searches
1112     r_ms.BaseStream.Seek(0, SeekOrigin.Begin);
1113    
1114    
1115     // hack to help with OutOfMemory Exceptions (OldValue and Equal compare will always add all found search results)
1116     bool NeedToCompare = true;
1117     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue &&
1118     SearchArgs.CompareType == SearchCompareTypes.Equal &&
1119     SearchArgs.IsFirstSearch)
1120 william 280 {
1121 william 398 NeedToCompare = false;
1122     second_tmp_Results = null; // Free Memory
1123 william 280 }
1124    
1125 william 398 if (NeedToCompare)
1126     {
1127     if (SearchArgs.CompareType != SearchCompareTypes.Between && SearchArgs.CompareType != SearchCompareTypes.NotBetween)
1128 william 280 {
1129 william 398 #region Non-Range Searches
1130     st_nonrange_search.Start();
1131     //second_tmp_Results = new List<ResultType<object>>(SearchArgs.Results.Count * 1024);
1132     ////second_tmp_Results.c
1133     for (int i = 0; i < SearchArgs.Results.Count; i += 1)
1134     {
1135     uint address = SearchArgs.Results[i].Address;
1136     if (MemoryRangeStart > 0 && !SearchArgs.IsFirstSearch) { address = address - MemoryRangeStart; }
1137     r_ms.BaseStream.Seek(address, SeekOrigin.Begin);
1138     switch (SearchArgs.DataType)
1139 william 280 {
1140 william 398 #region Comparer Support
1141     #region case SearchDataTypes._8bits:
1142     case SearchDataTypes._8bits:
1143     if (SearchArgs.IsUnsignedDataType)
1144     {
1145     byte lookup_value = r_ms.ReadByte();
1146     _8bit_unsigned_comparer_ comparer = new _8bit_unsigned_comparer_(SearchArgs, address);
1147     byte value = 0;
1148     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1149     {
1150     value = Convert.ToByte(SearchArgs.Results[i].Value);
1151     comparer.Value = value;
1152     }
1153     else
1154     {
1155     value = Convert.ToByte(SearchArgs.CompareStartValue);
1156     comparer.Value = value;
1157     }
1158     if (comparer.Compare(lookup_value, value))
1159     {
1160     //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1161     //second_tmp_Results.Add(_tmp_result);
1162     //_tmp_result = null; // free memory
1163     //SearchArgs.Results.RemoveAt(i);
1164     SearchArgs.Results[i].Value = comparer.Value;
1165     second_tmp_Results.Add(SearchArgs.Results[i]);
1166     }
1167     comparer = null; // free memory
1168     }
1169     else
1170     {
1171     sbyte lookup_value = r_ms.ReadSByte();
1172     _8bit_signed_comparer_ comparer = new _8bit_signed_comparer_(SearchArgs, address);
1173     sbyte value = 0;
1174     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1175     {
1176     value = Convert.ToSByte(SearchArgs.Results[i].Value);
1177     }
1178     else
1179     {
1180     value = Convert.ToSByte(SearchArgs.CompareStartValue);
1181     }
1182     if (comparer.Compare(lookup_value, value))
1183     {
1184     //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1185     //second_tmp_Results.Add(_tmp_result);
1186     //_tmp_result = null; // free memory
1187     //SearchArgs.Results.RemoveAt(i);
1188     SearchArgs.Results[i].Value = comparer.Value;
1189     second_tmp_Results.Add(SearchArgs.Results[i]);
1190     }
1191     comparer = null; // free memory
1192     }
1193     break;
1194     #endregion
1195     #region case SearchDataTypes._16bits:
1196     case SearchDataTypes._16bits:
1197     if (SearchArgs.IsUnsignedDataType)
1198     {
1199     ushort lookup_value = r_ms.ReadUInt16();
1200     _16bit_unsigned_comparer_ comparer = new _16bit_unsigned_comparer_(SearchArgs, address);
1201     ushort value = 0;
1202     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1203     {
1204     value = Convert.ToUInt16(SearchArgs.Results[i].Value);
1205     comparer.Value = value;
1206     }
1207     else
1208     {
1209     value = Convert.ToUInt16(SearchArgs.CompareStartValue);
1210     comparer.Value = value;
1211     }
1212     if (comparer.Compare(lookup_value, value))
1213     {
1214     //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1215     //second_tmp_Results.Add(_tmp_result);
1216     //_tmp_result = null; // free memory
1217     //SearchArgs.Results.RemoveAt(i);
1218     SearchArgs.Results[i].Value = comparer.Value;
1219     second_tmp_Results.Add(SearchArgs.Results[i]);
1220     }
1221     comparer = null; // free memory
1222     }
1223     else
1224     {
1225     short lookup_value = r_ms.ReadInt16();
1226     _16bit_signed_comparer_ comparer = new _16bit_signed_comparer_(SearchArgs, address);
1227     short value = 0;
1228     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1229     {
1230     value = Convert.ToInt16(SearchArgs.Results[i].Value);
1231     }
1232     else
1233     {
1234     value = Convert.ToInt16(SearchArgs.CompareStartValue);
1235     }
1236     if (comparer.Compare(lookup_value, value))
1237     {
1238     //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1239     //second_tmp_Results.Add(_tmp_result);
1240     //_tmp_result = null; // free memory
1241     //SearchArgs.Results.RemoveAt(i);
1242     SearchArgs.Results[i].Value = comparer.Value;
1243     second_tmp_Results.Add(SearchArgs.Results[i]);
1244     }
1245     comparer = null; // free memory
1246     }
1247     break;
1248     #endregion
1249     #region case SearchDataTypes._32bits:
1250     case SearchDataTypes._32bits:
1251     if (SearchArgs.IsUnsignedDataType)
1252     {
1253     uint lookup_value = r_ms.ReadUInt32();
1254     _32bit_unsigned_comparer_ comparer = new _32bit_unsigned_comparer_(SearchArgs, address);
1255     uint value = 0;
1256     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1257     {
1258     value = Convert.ToUInt32(SearchArgs.Results[i].Value);
1259     comparer.Value = value;
1260     }
1261     else
1262     {
1263     value = Convert.ToUInt32(SearchArgs.CompareStartValue);
1264     comparer.Value = value;
1265     }
1266     if (comparer.Compare(lookup_value, value))
1267     {
1268     //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1269     //second_tmp_Results.Add(_tmp_result);
1270     //_tmp_result = null; // free memory
1271     //SearchArgs.Results.RemoveAt(i);
1272     SearchArgs.Results[i].Value = comparer.Value;
1273     second_tmp_Results.Add(SearchArgs.Results[i]);
1274     }
1275     comparer = null; // free memory
1276     }
1277     else
1278     {
1279     int lookup_value = r_ms.ReadInt32();
1280     _32bit_signed_comparer_ comparer = new _32bit_signed_comparer_(SearchArgs, address);
1281     int value = 0;
1282     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1283     {
1284     value = Convert.ToInt32(SearchArgs.Results[i].Value);
1285     }
1286     else
1287     {
1288     value = Convert.ToInt32(SearchArgs.CompareStartValue);
1289     }
1290     if (comparer.Compare(lookup_value, value))
1291     {
1292     //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1293     //second_tmp_Results.Add(_tmp_result);
1294     //_tmp_result = null; // free memory
1295     //SearchArgs.Results.RemoveAt(i);
1296     SearchArgs.Results[i].Value = comparer.Value;
1297     second_tmp_Results.Add(SearchArgs.Results[i]);
1298     }
1299     comparer = null; // free memory
1300     }
1301     break;
1302     #endregion
1303     #region case SearchDataTypes._64bits:
1304     case SearchDataTypes._64bits:
1305     if (SearchArgs.IsUnsignedDataType)
1306     {
1307     ulong lookup_value = r_ms.ReadUInt64();
1308     _64bit_unsigned_comparer_ comparer = new _64bit_unsigned_comparer_(SearchArgs, address);
1309     ulong value = 0;
1310     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1311     {
1312     value = Convert.ToUInt64(SearchArgs.Results[i].Value);
1313     comparer.Value = value;
1314     }
1315     else
1316     {
1317     value = Convert.ToUInt64(SearchArgs.CompareStartValue);
1318     comparer.Value = value;
1319     }
1320     if (comparer.Compare(lookup_value, value))
1321     {
1322     //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1323     //second_tmp_Results.Add(_tmp_result);
1324     //_tmp_result = null; // free memory
1325     //SearchArgs.Results.RemoveAt(i);
1326     SearchArgs.Results[i].Value = comparer.Value;
1327     second_tmp_Results.Add(SearchArgs.Results[i]);
1328     }
1329     comparer = null; // free memory
1330     }
1331     else
1332     {
1333     long lookup_value = r_ms.ReadInt64();
1334     _64bit_signed_comparer_ comparer = new _64bit_signed_comparer_(SearchArgs, address);
1335     long value = 0;
1336     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1337     {
1338     value = Convert.ToInt64(SearchArgs.Results[i].Value);
1339     }
1340     else
1341     {
1342     value = Convert.ToInt64(SearchArgs.CompareStartValue);
1343     }
1344     if (comparer.Compare(lookup_value, value))
1345     {
1346     //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1347     //second_tmp_Results.Add(_tmp_result);
1348     //_tmp_result = null; // free memory
1349     //SearchArgs.Results.RemoveAt(i);
1350     SearchArgs.Results[i].Value = comparer.Value;
1351     second_tmp_Results.Add(SearchArgs.Results[i]);
1352     }
1353     comparer = null; // free memory
1354     }
1355     break;
1356     #endregion
1357     #endregion
1358 william 280 }
1359 william 398
1360     double double_percent_done = 100.0 * (double)((double)i / (double)SearchArgs.Results.Count);
1361     int int_percent_done = (int)double_percent_done;
1362     if (int_percent_done != Last_Whole_Percent_Done && i % ElementsBeforeDisplay == 0)
1363 william 280 {
1364 william 398 resultsprogress.Value = int_percent_done;
1365     resultsprogress.Message = string.Format(" -> Reading Address: 0x{0:x8}", i);
1366     Last_Whole_Percent_Done = int_percent_done;
1367     //Application.DoEvents();
1368 william 280 }
1369 william 398
1370     }
1371     st_nonrange_search.Stop();
1372     logger.Profiler.WriteLine("Non-Ranged search took a total of {0} seconds to complete.", st_nonrange_search.Elapsed.TotalSeconds);
1373 william 385 #endregion
1374 william 398 }
1375     #region Ranged Searches
1376     #if !DONOT_HAVE_RANGED_SEARCH_SUPPORT
1377     if (SearchArgs.CompareType == SearchCompareTypes.Between || SearchArgs.CompareType == SearchCompareTypes.NotBetween)
1378     {
1379     st_ranged_search.Start();
1380     object start, end;
1381    
1382     start = SearchArgs.CompareStartValue;
1383     end = SearchArgs.CompareEndValue;
1384     for (int i = 0; i < SearchArgs.Results.Count; i += 1)
1385     {
1386     uint address = SearchArgs.Results[i].Address;
1387     if (MemoryRangeStart > 0 && !SearchArgs.IsFirstSearch) { address = address - MemoryRangeStart; }
1388     r_ms.BaseStream.Seek(address, SeekOrigin.Begin);
1389     if (SearchArgs.CompareType == SearchCompareTypes.Between)
1390 william 280 {
1391 william 398 InRangeComparer comparer = new InRangeComparer(address, 0);
1392     if (comparer.Compare(start, end, SearchArgs.DataType, SearchArgs.IsUnsignedDataType, r_ms))
1393 william 280 {
1394     SearchArgs.Results[i].Value = comparer.Value;
1395     second_tmp_Results.Add(SearchArgs.Results[i]);
1396     }
1397 william 398 comparer = null;
1398 william 280 }
1399 william 398 else if (SearchArgs.CompareType == SearchCompareTypes.NotBetween)
1400 william 280 {
1401 william 398 NotInRangeComparer comparer = new NotInRangeComparer(address, 0);
1402     if (comparer.Compare(start, end, SearchArgs.DataType, SearchArgs.IsUnsignedDataType, r_ms))
1403 william 280 {
1404     SearchArgs.Results[i].Value = comparer.Value;
1405     second_tmp_Results.Add(SearchArgs.Results[i]);
1406     }
1407 william 398 comparer = null;
1408 william 280 }
1409     else
1410     {
1411 william 398 throw new InvalidOperationException("Encounted unkown range search type: " + SearchArgs.CompareType);
1412 william 280 }
1413 william 398 double double_percent_done = 100.0 * (double)((double)i / (double)SearchArgs.Results.Count);
1414     int int_percent_done = (int)double_percent_done;
1415     if (int_percent_done != Last_Whole_Percent_Done && i % ElementsBeforeDisplay == 0)
1416 william 280 {
1417 william 398 resultsprogress.Value = int_percent_done;
1418     resultsprogress.Message = string.Format(" -> Reading Address: 0x{0:x8}", i);
1419     Last_Whole_Percent_Done = int_percent_done;
1420     //Application.DoEvents();
1421 william 280 }
1422 william 398 }
1423     st_ranged_search.Stop();
1424     logger.Profiler.WriteLine("Ranged search took a total of {0} seconds to complete.", st_ranged_search.Elapsed.TotalSeconds);
1425 william 280 }
1426 william 398 #endif
1427     #endregion
1428 william 280
1429     }
1430     #endregion
1431 william 398 // leave SearchArgs.Results alone, if false
1432     if (NeedToCompare)
1433 william 280 {
1434 william 398 SearchArgs.Results = second_tmp_Results.GetRange(0, second_tmp_Results.Count);
1435     // fix addresses when memory start is not zero
1436     if (MemoryRangeStart > 0 && SearchArgs.IsFirstSearch) { for (int i = 0; i < SearchArgs.Results.Count; i++) { SearchArgs.Results[i].Address = SearchArgs.Results[i].Address + MemoryRangeStart; } }
1437     second_tmp_Results = null; // free memory
1438 william 280 }
1439 william 398
1440     r_ms.Close();
1441 william 280 }
1442     }
1443 william 230 }
1444    
1445     private void SearchWorkerThread_ProgressChanged(object sender, ProgressChangedEventArgs e)
1446     {
1447 william 280 //if (SearchArgs.ProgressLogger != null)
1448     //{
1449     // resultsprogress.Value = e.ProgressPercentage;
1450     // //Application.DoEvents();
1451     //}
1452 william 230 }
1453    
1454     private void SearchWorkerThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
1455     {
1456 william 286 if (!e.Cancelled)
1457     {
1458     Stopwatch st = (e.Result as Stopwatch);
1459     st.Stop();
1460 william 392 logger.Profiler.WriteLine("Search took {0} seconds, overall, to complete.", st.Elapsed.TotalSeconds);
1461 william 286 }
1462    
1463 william 280 resultsprogress.Value = 100;
1464 william 286 logger.Debug.WriteLine(string.Format("Results Count -> 0x{0:x8}", SearchArgs.Results.Count));
1465 william 230
1466 william 280 if (SearchArgs.Results.Count == 1) // debug message for 1 result using 32bit unsigned
1467 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));
1468 william 280
1469 william 286 logger.Info.WriteLine(string.Format("Found 0x{0:x8} results", SearchArgs.Results.Count));
1470 william 280
1471     if (SearchArgs.Results.Count <= MIN_NUMBER_OF_RESULTS_BEFORE_DISPLAY)
1472     {
1473     lstResults.Items.Clear();
1474     List<ResultItem> items = new List<ResultItem>();
1475     for (int i = 0; i < SearchArgs.Results.Count; i++)
1476     {
1477     ResultItem item = new ResultItem(0, false);
1478     //item.Text = string.Format("0x{0:x8}", SearchArgs.Results[i].Address);
1479     //item.SubItems.Add(string.Format("0x{0:x8}", SearchArgs.Results[i].Address));
1480     switch (SearchArgs.DataType)
1481     {
1482    
1483     case SearchDataTypes._8bits:
1484     if (SearchArgs.IsUnsignedDataType) { item = new ResultItem(SearchArgs.Results[i].Address, false, Convert.ToByte(SearchArgs.Results[i].Value)); }
1485     else { item = new ResultItem(SearchArgs.Results[i].Address, false, Convert.ToSByte(SearchArgs.Results[i].Value)); }
1486     break;
1487     case SearchDataTypes._16bits:
1488     if (SearchArgs.IsUnsignedDataType) { item = new ResultItem(SearchArgs.Results[i].Address, false, Convert.ToUInt16(SearchArgs.Results[i].Value)); }
1489     else { item = new ResultItem(SearchArgs.Results[i].Address, false, Convert.ToInt16(SearchArgs.Results[i].Value)); }
1490     break;
1491     case SearchDataTypes._32bits:
1492     if (SearchArgs.IsUnsignedDataType) { item = new ResultItem(SearchArgs.Results[i].Address, false, Convert.ToUInt32(SearchArgs.Results[i].Value)); }
1493     else { item = new ResultItem(SearchArgs.Results[i].Address, false, Convert.ToInt32(SearchArgs.Results[i].Value)); }
1494     break;
1495     case SearchDataTypes._64bits:
1496     if (SearchArgs.IsUnsignedDataType) { item = new ResultItem(SearchArgs.Results[i].Address, false, Convert.ToUInt64(SearchArgs.Results[i].Value)); }
1497     else { item = new ResultItem(SearchArgs.Results[i].Address, false, Convert.ToInt64(SearchArgs.Results[i].Value)); }
1498     break;
1499     }
1500    
1501     if (!items.Contains(item))
1502     items.Add(item);
1503     }
1504     lstResults.Items.AddRange(items.ToArray());
1505     }
1506    
1507     this.DoSearchDoneSpecific();
1508     //System.Threading.Thread.Sleep(100);
1509     //if (_SEARCH_MODE != SearchMode.SEARCH_MODE_NORMAL_WITH_JOKER)
1510     this.ThawResultsUpdate();
1511     Application.DoEvents();
1512 william 230 }
1513 william 280 private void DoSearchDoneSpecific()
1514     {
1515     SearchWorkerThread.CancelAsync();
1516     if (lstResults.Items.Count > 0) { timer_update_results.Enabled = true; }
1517     else { timer_update_results.Enabled = false; }
1518 william 230
1519 william 280 search_progress_updater.Enabled = false;
1520    
1521     btnCancel.Enabled = false;
1522     btnReset.Enabled = true;
1523     btnSearch.Enabled = true;
1524     grpCompareType.Enabled = true;
1525     grpCompareValue.Enabled = true;
1526     resultsprogress.Value = 0;
1527     resultsprogress.Message = "";
1528     grpDataType.Enabled = false;
1529     // resume process on reset, incase it was suspended
1530     ThreadControl.ResumeProcess(this.AcceptedProcess.Id);
1531     //Application.DoEvents();
1532     this.Refresh();
1533     }
1534    
1535     private void DoCancelSpecific()
1536     {
1537     this.DoSearchDoneSpecific();
1538     }
1539     private void DoResetSpecific()
1540     {
1541     this.DoCancelSpecific();
1542     IsFirstSearch = true;
1543     grpDataType.Enabled = true;
1544     }
1545 william 230 private void search_progress_updater_Tick(object sender, EventArgs e)
1546     {
1547     if ((this.AcceptedProcess ==null) || Process.GetProcessById(this.AcceptedProcess.Id) == null)
1548     {
1549     SearchWorkerThread.CancelAsync();
1550     //JokerSearchWorker.CancelAsync();
1551     ResultsUpdateWorkerThread.CancelAsync();
1552     }
1553     }
1554    
1555 william 280 #region Search Button
1556 william 230 private void btnSearch_Click(object sender, EventArgs e)
1557     {
1558 william 271 this.SearchInProgess = true;
1559 william 280 //btnCancel.Enabled = true;
1560     //btnReset.Enabled = false;
1561     //btnSearch.Enabled = false;
1562     this.FreezeResultsUpdate();
1563     this.handle_btnSearch_Click();
1564     }
1565     private void handle_btnSearch_Click()
1566     {
1567     //this.FreezeResultsUpdate();
1568     lstResults.Items.Clear();
1569    
1570     if (lstResults.Items.Count > 0) { timer_update_results.Enabled = true; }
1571     else { timer_update_results.Enabled = false; }
1572    
1573    
1574     resultsprogress.Value = 0;
1575     bool _is_unsigned = chkUnsigned.Checked;
1576     SearchType search_type = new SearchType();
1577     SearchDataTypes _data_type = new SearchDataTypes();
1578     SearchCompareTypes _compare_type = new SearchCompareTypes();
1579     CompareValueTypes _compare_value_type = new CompareValueTypes();
1580     object start_value = 0;
1581     object end_value = 0;
1582     // get datatype
1583     if (radio_8bits.Checked) { _data_type = SearchDataTypes._8bits; }
1584     else if (radio_16bits.Checked) { _data_type = SearchDataTypes._16bits; }
1585     else if (radio_32bits.Checked) { _data_type = SearchDataTypes._32bits; }
1586     else if (radio_64bits.Checked) { _data_type = SearchDataTypes._64bits; }
1587     else { logger.Error.WriteLine("Could not determine search data type bit size. (was not 8/16/32/or 64bits)"); }
1588     // get compare type
1589     if (radiocompare_equal.Checked) { _compare_type = SearchCompareTypes.Equal; }
1590     else if (radiocompare_greaterthan.Checked) { _compare_type = SearchCompareTypes.GreaterThan; }
1591     else if (radiocompare_lessthan.Checked) { _compare_type = SearchCompareTypes.LessThan; }
1592     else if (radiocompare_greaterthan_orequal.Checked) { _compare_type = SearchCompareTypes.GreaterThanOrEqual; }
1593     else if (radiocompare_lessthan_orequal.Checked) { _compare_type = SearchCompareTypes.LessThanOrEqual; }
1594     else if (radiocompare_notequal.Checked) { _compare_type = SearchCompareTypes.NotEqual; }
1595     else if (radiocompare_between.Checked) { _compare_type = SearchCompareTypes.Between; }
1596     else if (radiocompare_notbetween.Checked) { _compare_type = SearchCompareTypes.NotBetween; }
1597     else { logger.Error.WriteLine("Could not determine search comparison type. (was not == > < >= <= != <> or !<>)"); }
1598     // get compare valure type
1599     if (radio_oldvalue.Checked) { _compare_value_type = CompareValueTypes.OldValue; }
1600     else if (radio_specificvalue.Checked) { _compare_value_type = CompareValueTypes.SpecificValue; }
1601     else { logger.Error.WriteLine("Could not determine search comparison type. (was not old or specific value"); }
1602    
1603     if (_compare_value_type == CompareValueTypes.SpecificValue || (_compare_type == SearchCompareTypes.Between || _compare_type == SearchCompareTypes.NotBetween))
1604     {
1605    
1606     switch (_data_type)
1607     {
1608     case SearchDataTypes._8bits:
1609     if (_is_unsigned) { start_value = txtStartAddr.ToByte(); }
1610     else { start_value = txtStartAddr.ToSByte(); }
1611     break;
1612     case SearchDataTypes._16bits:
1613     if (_is_unsigned) { start_value = txtStartAddr.ToUInt16(); }
1614     else { start_value = txtStartAddr.ToInt16(); }
1615     break;
1616     case SearchDataTypes._32bits:
1617     if (_is_unsigned) { start_value = txtStartAddr.ToUInt32(); }
1618     else { start_value = txtStartAddr.ToInt32(); }
1619     break;
1620     case SearchDataTypes._64bits:
1621     if (_is_unsigned) { start_value = txtStartAddr.ToUInt64(); }
1622     else { start_value = txtStartAddr.ToInt64(); }
1623     break;
1624     default: throw new InvalidOperationException("In SearchType(): Encounterd an Unkown Search Data Type.");
1625     }
1626     }
1627     if (_compare_type == SearchCompareTypes.Between || _compare_type == SearchCompareTypes.NotBetween)
1628     {
1629     switch (_data_type)
1630     {
1631     case SearchDataTypes._8bits:
1632     if (_is_unsigned) { end_value = txtEndAddr.ToByte(); }
1633     else { end_value = txtEndAddr.ToSByte(); }
1634     break;
1635     case SearchDataTypes._16bits:
1636     if (_is_unsigned) { end_value = txtEndAddr.ToUInt16(); }
1637     else { end_value = txtEndAddr.ToInt16(); }
1638     break;
1639     case SearchDataTypes._32bits:
1640     if (_is_unsigned) { end_value = txtEndAddr.ToUInt32(); }
1641     else { end_value = txtEndAddr.ToInt32(); }
1642     break;
1643     case SearchDataTypes._64bits:
1644     if (_is_unsigned) { end_value = txtEndAddr.ToUInt64(); }
1645     else { end_value = txtEndAddr.ToInt64(); }
1646     break;
1647     default: throw new InvalidOperationException("In SearchType(): Encounterd an Unkown Search Data Type.");
1648     }
1649     }
1650    
1651     search_type = new SearchType(_data_type, _is_unsigned, _compare_type, _compare_value_type, start_value, end_value, resultsprogress);
1652    
1653     //search_type.LogSearchOptions();
1654    
1655     search_type.IsFirstSearch = IsFirstSearch;
1656    
1657    
1658    
1659     DoSearch(search_type);
1660     IsFirstSearch = false;
1661     }
1662     private void DoSearch(SearchType args)
1663     {
1664     if (!args.IsFirstSearch && SearchArgs != null)
1665     {
1666     //args.Results.AddRange(SearchArgs.Results.ToArray());
1667     args.Results = SearchArgs.Results;
1668     }
1669     SearchArgs = args;
1670     #if DONOT_HAVE_RANGED_SEARCH_SUPPORT
1671     if (SearchArgs.CompareType == SearchCompareTypes.Between || SearchArgs.CompareType == SearchCompareTypes.NotBetween)
1672     {
1673     throw new NotImplementedException("Between and Not Between Range searches have not been implemented.");
1674     }
1675     #endif
1676     search_progress_updater.Enabled = true;
1677     //padPluginSelector.Enabled = false;
1678     //gsPluginSelector.Enabled = false;
1679 william 271 btnReset.Enabled = false;
1680     btnSearch.Enabled = false;
1681 william 280 btnCancel.Enabled = true;
1682     grpDataType.Enabled = false;
1683     grpCompareType.Enabled = false;
1684     grpCompareValue.Enabled = false;
1685     this.Refresh();
1686     Application.DoEvents();
1687     SearchWorkerThread.RunWorkerAsync();
1688 william 230 }
1689 william 280 #endregion
1690 william 271 private void btnReset_Click(object sender, EventArgs e)
1691     {
1692     this.SearchInProgess = false;
1693 william 280 //btnSearch.Enabled = true;
1694     //btnCancel.Enabled = false;
1695     this.DoResetSpecific();
1696     lstResults.Items.Clear();
1697     try { SearchArgs.Results = new List<ResultType<object>>(); }
1698     catch { }
1699 william 271 }
1700    
1701     private void btnCancel_Click(object sender, EventArgs e)
1702     {
1703     this.SearchInProgess = false;
1704 william 280 //btnCancel.Enabled = false;
1705     //btnSearch.Enabled = true;
1706     //btnReset.Enabled = true;
1707     this.DoCancelSpecific();
1708 william 271 }
1709 william 275
1710     private void mnuItemPatchListViewMemoryRegion_Click(object sender, EventArgs e)
1711     {
1712     List<ResultDataType> patch_list = new List<ResultDataType>();
1713     List<int> SelectedIndexes = new List<int>();
1714     foreach (int index in lstPatchList.SelectedIndices) { SelectedIndexes.Add(index); }
1715     foreach (int index in SelectedIndexes)
1716     {
1717     ListViewItem item = lstPatchList.Items[index];
1718     ResultDataType rdt = (ResultDataType)item.Tag;
1719     ViewMemoryRegion(rdt);
1720     break; // only get the fist item
1721     }
1722     }
1723    
1724     private void mnuItemResultsListViewMemoryRegion_Click(object sender, EventArgs e)
1725     {
1726     List<ResultDataType> patch_list = new List<ResultDataType>();
1727     List<int> SelectedIndexes = new List<int>();
1728     foreach (int index in lstResults.SelectedIndices) { SelectedIndexes.Add(index); }
1729     foreach (int index in SelectedIndexes)
1730     {
1731     ListViewItem item = lstResults.Items[index];
1732     ResultDataType rdt = (ResultDataType)item.Tag;
1733     ViewMemoryRegion(rdt);
1734     break; // only get the fist item
1735     }
1736     }
1737     private void ViewMemoryRegion(ResultDataType rdt)
1738     {
1739     if (OnBrowseMemoryRegion != null)
1740 william 323 OnBrowseMemoryRegion(new BrowseMemoryRegionEvent(this, rdt.Address));
1741 william 275 }
1742 william 277
1743     private void mnuAddedResults_Opening(object sender, CancelEventArgs e)
1744     {
1745     if (!(lstPatchList.Items.Count > 0)) { mnuItemRemoveResult.Visible = false; e.Cancel = true; }
1746     if (!(lstPatchList.Items.Count > 0)) { mnuItemPatchSelectedEntry.Visible = false; e.Cancel = true; }
1747     if (e.Cancel) return;
1748     if (lstPatchList.Items.Count > 0) mnuItemRemoveResult.Visible = true;
1749     if (lstPatchList.Items.Count > 0) mnuItemPatchSelectedEntry.Visible = true;
1750    
1751     if (!(lstPatchList.Items.Count > 0)) { mnuItemFreezeSelectedPatches.Visible = false; e.Cancel = true; }
1752     if (!(lstPatchList.Items.Count > 0)) { mnuItemThawSelectedPatches.Visible = false; e.Cancel = true; }
1753     if (e.Cancel) return;
1754    
1755     if (lstPatchList.Items.Count > 0) mnuItemFreezeSelectedPatches.Visible = true;
1756     if (lstPatchList.Items.Count > 0) mnuItemThawSelectedPatches.Visible = true;
1757    
1758     if (lstPatchList.SelectedItems.Count == 0) e.Cancel = true;
1759     if (e.Cancel) return;
1760    
1761     }
1762    
1763     private void mnuResults_Opening(object sender, CancelEventArgs e)
1764     {
1765     if (!(lstResults.Items.Count > 0)) e.Cancel = true;
1766     if (lstResults.SelectedItems.Count == 0) e.Cancel = true;
1767     if (SearchArgs == null) e.Cancel = true;
1768     if (e.Cancel) return;
1769     }
1770 william 287
1771     private void chkMemoryRangeExpertMode_CheckedChanged(object sender, EventArgs e)
1772     {
1773 william 288 txtMemoryRangeStart.ReadOnly = !chkMemoryRangeExpertMode.Checked;
1774     txtMemoryRangeSize.ReadOnly = !chkMemoryRangeExpertMode.Checked;
1775 william 287 }
1776    
1777 william 378 private void txtMemoryRangeStart_ValueChanged(object sender, ValueChangedEventArgs e) { this.MemoryRangeStart = Convert.ToUInt32(e.NewValue); }
1778 william 288 private void txtMemoryRangeSize_ValueChanged(object sender, ValueChangedEventArgs e) { this.MemoryRangeSize = Convert.ToUInt32(e.NewValue); }
1779 william 287
1780 william 227 }
1781     }

  ViewVC Help
Powered by ViewVC 1.1.22