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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 280 - (hide annotations) (download)
Tue Jun 5 00:13:29 2012 UTC (8 years, 5 months ago) by william
File size: 81379 byte(s)
+ import search code from PCSX2 Ram Dump (no need to re-invent the wheel...plus it cuts down on code refactoring time)

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

  ViewVC Help
Powered by ViewVC 1.1.22