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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22