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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 399 - (hide annotations) (download)
Wed Jun 20 13:24:49 2012 UTC (8 years, 4 months ago) by william
File size: 92776 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22