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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 446 - (hide annotations) (download)
Sun Jun 2 19:52:03 2013 UTC (7 years, 4 months ago) by william
File size: 114213 byte(s)
+ fix search cancel/reset

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

  ViewVC Help
Powered by ViewVC 1.1.22