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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 444 - (hide annotations) (download)
Sun Jun 2 18:56:41 2013 UTC (7 years, 4 months ago) by william
File size: 113381 byte(s)
+ update serialization reader/writer to include a search results guid

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     SearchDataTypes sdt = SearchArgs.DataType;
1048     bool unsigned = SearchArgs.IsUnsignedDataType;
1049     int Last_Whole_Percent_Done = 0;
1050     uint CurrentIndex = e.CurrentIndex;
1051     if (e.UserState != null)
1052     {
1053     SearchResultWriter writer = (e.UserState as SearchResultWriter);
1054     if (writer == null)
1055     throw new InvalidOperationException("writer cannot be null");
1056     using (MemoryStream ms = new MemoryStream(e.Data))
1057     {
1058     using (BinaryReader br = new BinaryReader(ms))
1059     {
1060     while (br.BaseStream.Position < br.BaseStream.Length)
1061     {
1062     try
1063     {
1064     switch (sdt)
1065     {
1066     case SearchDataTypes._8bits:
1067 william 413 if (unsigned) { writer.WriteResult<Byte>((uint)CurrentIndex, br.ReadByte()); }
1068     else { writer.WriteResult<SByte>((uint)CurrentIndex, br.ReadSByte()); } break;
1069 william 408 case SearchDataTypes._16bits:
1070 william 413 if (unsigned) { writer.WriteResult<UInt16>((uint)CurrentIndex, br.ReadUInt16()); }
1071     else { writer.WriteResult<Int16>((uint)CurrentIndex, br.ReadInt16()); } break;
1072 william 408 case SearchDataTypes._32bits:
1073 william 413 if (unsigned) { writer.WriteResult<UInt32>((uint)CurrentIndex, br.ReadUInt32()); }
1074     else { writer.WriteResult<Int32>((uint)CurrentIndex, br.ReadInt32()); } break;
1075 william 408 case SearchDataTypes._64bits:
1076 william 413 if (unsigned) { writer.WriteResult<UInt64>((uint)CurrentIndex, br.ReadUInt64()); }
1077     else { writer.WriteResult<Int64>((uint)CurrentIndex, br.ReadInt64()); } break;
1078 william 408 }
1079     }
1080     catch (EndOfStreamException) { }
1081     double double_percent_done = 100.0 * (double)((double)CurrentIndex / (double)e.TotalCount);
1082     int int_percent_done = (int)double_percent_done;
1083     if (int_percent_done != Last_Whole_Percent_Done && (((double)Last_Whole_Percent_Done / 2.0) == (int)((double)Last_Whole_Percent_Done / 2.0)))
1084     {
1085     if (int_percent_done <= 100)
1086     {
1087     resultsprogress.Value = int_percent_done;
1088     resultsprogress.Message = string.Format(" -> Reading Address: 0x{0:x8}", (CurrentIndex + MemoryRangeStart));
1089     Last_Whole_Percent_Done = int_percent_done;
1090     }
1091     }
1092 william 413 switch (sdt)
1093     {
1094     case SearchDataTypes._8bits:
1095     CurrentIndex += sizeof(byte);
1096     break;
1097     case SearchDataTypes._16bits:
1098     CurrentIndex += sizeof(ushort);
1099     break;
1100     case SearchDataTypes._32bits:
1101     CurrentIndex += sizeof(uint);
1102     break;
1103     case SearchDataTypes._64bits:
1104     CurrentIndex += sizeof(ulong);
1105     break;
1106     }
1107 william 408 }
1108    
1109     }
1110     }
1111     }
1112     }
1113 william 230 private void SearchWorkerThread_DoWork(object sender, DoWorkEventArgs e)
1114     {
1115 william 410 try
1116     {
1117     Stopwatch st = new Stopwatch();
1118     st.Start();
1119 william 392
1120 william 410 Stopwatch st_first_search = new Stopwatch();
1121     Stopwatch st_nonrange_search = new Stopwatch();
1122     Stopwatch st_ranged_search = new Stopwatch();
1123 william 392
1124 william 410 e.Result = st;
1125     //List<ResultType<object>> tmp_Results = new List<ResultType<object>>();
1126     List<ResultType<object>> second_tmp_Results = new List<ResultType<object>>();
1127     //const int ElementsBeforeDisplay = 100;
1128     SearchArgs.LogSearchOptions();
1129     uint STEP_SIZE = (uint)SearchArgs.DataType / 8;
1130 william 399
1131 william 410 bool unsigned = SearchArgs.IsUnsignedDataType;
1132     SearchDataTypes sdt = SearchArgs.DataType;
1133     //byte[] buffered_mem = new byte[(MemoryRangeSize - MemoryRangeStart)]; // throws OutOfMemoryException if size is over 2G
1134     logger.Debug.WriteLine(string.Format("Buffered Memory Size -> 0x{0:x8}", MemoryRangeSize - MemoryRangeStart));
1135     resultsprogress.Value = 0;
1136     resultsprogress.Message = string.Format("Search is Warming Up...Please Wait...");
1137 william 408
1138 william 410 Stopwatch provider_st = new Stopwatch();
1139     provider_st.Start();
1140     using (GenericMemoryProvider provider = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1141     {
1142     provider.OpenProvider();
1143 william 429 int count = (int)((MemoryRangeSize - MemoryRangeStart) / STEP_SIZE);
1144 william 444 SearchResultWriter writer = new SearchResultWriter((int)(count), SearchGuid);
1145 william 410 provider.OnBytesRead += new BaseEventHandler<OnBytesReadEventArgs>(search_provider_OnBytesRead);
1146     provider.ReadProcessMemoryAtOnce(MemoryRangeStart, (MemoryRangeSize - MemoryRangeStart), writer);
1147 william 424 provider.CloseProvider();
1148 william 410 writer.Dispose();
1149     }
1150     provider_st.Stop();
1151     logger.Profiler.WriteLine("It took a total of {0} seconds for the memory provider to complete it's operation(s).", provider_st.Elapsed.TotalSeconds);
1152     //if (buffered_mem.Length == 0) { logger.Warn.WriteLine("Buffered Memory is Zero Length."); return; }
1153     int Last_Whole_Percent_Done = 0;
1154 william 408
1155    
1156 william 410 #region Subsequent Searches
1157     //r_ms.BaseStream.Seek(0, SeekOrigin.Begin);
1158 william 408
1159    
1160 william 410 // hack to help with OutOfMemory Exceptions (OldValue and Equal compare will always add all found search results)
1161     bool NeedToCompare = true;
1162     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue &&
1163     SearchArgs.CompareType == SearchCompareTypes.Equal &&
1164     SearchArgs.IsFirstSearch)
1165     {
1166     NeedToCompare = false;
1167     //second_tmp_Results = null; // Free Memory
1168     }
1169 william 408
1170 william 410 if (NeedToCompare)
1171 william 280 {
1172 william 410 if (SearchArgs.CompareType != SearchCompareTypes.Between && SearchArgs.CompareType != SearchCompareTypes.NotBetween)
1173 william 280 {
1174 william 410 #region Non-Range Searches
1175     st_nonrange_search.Start();
1176     //second_tmp_Results = new List<ResultType<object>>(SearchArgs.Results.Count * 1024);
1177     ////second_tmp_Results.c
1178     try
1179 william 398 {
1180 william 444 using (SearchResultReader reader = new SearchResultReader(SearchGuid))
1181 william 398 {
1182 william 410 try
1183 william 398 {
1184 william 410 for (int i = 0; i < reader.ResultCount; i += 1)
1185     {
1186     object result_value = 0;
1187     uint address = 0;
1188     #region switch (SearchArgs.DataType)
1189     switch (SearchArgs.DataType)
1190 william 403 {
1191 william 410 case SearchDataTypes._8bits: if (unsigned)
1192     {
1193     using (ResultType<byte> result = reader.GetNextResult<byte>())
1194     {
1195     address = result.Address;
1196     result_value = result.Value;
1197     }
1198     }
1199     else
1200     {
1201     using (ResultType<sbyte> result = reader.GetNextResult<sbyte>())
1202     {
1203     address = result.Address;
1204     result_value = result.Value;
1205     }
1206     } break;
1207     case SearchDataTypes._16bits: if (unsigned)
1208     {
1209     using (ResultType<ushort> result = reader.GetNextResult<ushort>())
1210     {
1211     address = result.Address;
1212     result_value = result.Value;
1213     }
1214     }
1215     else
1216     {
1217     using (ResultType<short> result = reader.GetNextResult<short>())
1218     {
1219     address = result.Address;
1220     result_value = result.Value;
1221     }
1222     } break;
1223     case SearchDataTypes._32bits: if (unsigned)
1224     {
1225     using (ResultType<uint> result = reader.GetNextResult<uint>())
1226     {
1227     address = result.Address;
1228     result_value = result.Value;
1229     }
1230     }
1231     else
1232     {
1233     using (ResultType<int> result = reader.GetNextResult<int>())
1234     {
1235     address = result.Address;
1236     result_value = result.Value;
1237     }
1238     } break;
1239     case SearchDataTypes._64bits: if (unsigned)
1240     {
1241     using (ResultType<ulong> result = reader.GetNextResult<ulong>())
1242     {
1243     address = result.Address;
1244     result_value = result.Value;
1245     }
1246     }
1247     else
1248     {
1249     using (ResultType<long> result = reader.GetNextResult<long>())
1250     {
1251     address = result.Address;
1252     result_value = result.Value;
1253     }
1254     } break;
1255 william 403 }
1256 william 410 #endregion
1257     if (MemoryRangeStart > 0 && !SearchArgs.IsFirstSearch) { address = address - MemoryRangeStart; }
1258     try
1259 william 408 {
1260 william 410 //r_ms.BaseStream.Seek(address, SeekOrigin.Begin);
1261 william 408 }
1262 william 410 catch (Exception)
1263 william 408 {
1264 william 410 throw;
1265 william 408 }
1266 william 410 switch (SearchArgs.DataType)
1267 william 408 {
1268 william 410 #region Comparer Support
1269     #region case SearchDataTypes._8bits:
1270     case SearchDataTypes._8bits:
1271     if (SearchArgs.IsUnsignedDataType)
1272 william 409 {
1273 william 410 byte lookup_value = 0;
1274     //using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this)) { gmp.OpenProvider(); gmp.ReadMemory(address, out lookup_value); gmp.CloseProvider(); }
1275     lookup_value = Convert.ToByte(result_value);
1276     using (_8bit_unsigned_comparer_ comparer = new _8bit_unsigned_comparer_(SearchArgs, address))
1277     {
1278     byte value = 0;
1279     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1280     {
1281     using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1282     {
1283     try
1284     {
1285     gmp.OpenProvider();
1286     gmp.ReadMemory(address, out value);
1287 william 424 gmp.CloseProvider();
1288 william 410 }
1289     catch (Exception ex)
1290     {
1291     logger.VerboseError.WriteLine(ex.ToString());
1292     }
1293     }
1294     comparer.Value = value;
1295     }
1296     else
1297     {
1298     value = Convert.ToByte(SearchArgs.CompareStartValue);
1299     comparer.Value = value;
1300     }
1301     if (comparer.Compare(lookup_value, value))
1302     {
1303     second_tmp_Results.Add(new ResultType<object>(comparer.Address, comparer.Value));
1304     }
1305     }
1306 william 409 }
1307 william 410 else
1308 william 409 {
1309 william 410 sbyte lookup_value = 0;
1310     //using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this)) { gmp.OpenProvider(); gmp.ReadMemory(address, out lookup_value); gmp.CloseProvider(); }
1311     lookup_value = Convert.ToSByte(result_value);
1312     using (_8bit_signed_comparer_ comparer = new _8bit_signed_comparer_(SearchArgs, address))
1313     {
1314     sbyte value = 0;
1315     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1316     {
1317     using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1318     {
1319     try
1320     {
1321     gmp.OpenProvider();
1322     gmp.ReadMemory(address, out value);
1323 william 424 gmp.CloseProvider();
1324 william 410 }
1325     catch (Exception ex)
1326     {
1327     logger.VerboseError.WriteLine(ex.ToString());
1328     }
1329     }
1330     comparer.Value = value;
1331     }
1332     else
1333     {
1334     value = Convert.ToSByte(SearchArgs.CompareStartValue);
1335     }
1336     if (comparer.Compare(lookup_value, value))
1337     {
1338     second_tmp_Results.Add(new ResultType<object>(comparer.Address, comparer.Value));
1339     }
1340     }
1341 william 409 }
1342 william 410 break;
1343     #endregion
1344     #region case SearchDataTypes._16bits:
1345     case SearchDataTypes._16bits:
1346     if (SearchArgs.IsUnsignedDataType)
1347 william 409 {
1348 william 410 ushort lookup_value = 0;
1349     //using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this)) { gmp.OpenProvider(); gmp.ReadMemory(address, out lookup_value); gmp.CloseProvider(); }
1350     lookup_value = Convert.ToUInt16(result_value);
1351     using (_16bit_unsigned_comparer_ comparer = new _16bit_unsigned_comparer_(SearchArgs, address))
1352     {
1353     ushort value = 0;
1354     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1355     {
1356     using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1357     {
1358     try
1359     {
1360     gmp.OpenProvider();
1361     gmp.ReadMemory(address, out value);
1362 william 424 gmp.CloseProvider();
1363 william 410 }
1364     catch (Exception ex)
1365     {
1366     logger.VerboseError.WriteLine(ex.ToString());
1367     }
1368     }
1369     comparer.Value = value;
1370     }
1371     else
1372     {
1373     value = Convert.ToUInt16(SearchArgs.CompareStartValue);
1374     comparer.Value = value;
1375     }
1376     if (comparer.Compare(lookup_value, value))
1377     {
1378     second_tmp_Results.Add(new ResultType<object>(comparer.Address, comparer.Value));
1379     }
1380     }
1381 william 409 }
1382 william 410 else
1383 william 409 {
1384 william 410 short lookup_value = 0;
1385     //using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this)) { gmp.OpenProvider(); gmp.ReadMemory(address, out lookup_value); gmp.CloseProvider(); }
1386     lookup_value = Convert.ToInt16(result_value);
1387     using (_16bit_signed_comparer_ comparer = new _16bit_signed_comparer_(SearchArgs, address))
1388     {
1389     short value = 0;
1390     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1391     {
1392     using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1393     {
1394     try
1395     {
1396     gmp.OpenProvider();
1397     gmp.ReadMemory(address, out value);
1398 william 424 gmp.CloseProvider();
1399 william 410 }
1400     catch (Exception ex)
1401     {
1402     logger.VerboseError.WriteLine(ex.ToString());
1403     }
1404     }
1405     comparer.Value = value;
1406     }
1407     else
1408     {
1409     value = Convert.ToInt16(SearchArgs.CompareStartValue);
1410     }
1411     if (comparer.Compare(lookup_value, value))
1412     {
1413     second_tmp_Results.Add(new ResultType<object>(comparer.Address, comparer.Value));
1414     }
1415     }
1416 william 409 }
1417 william 410 break;
1418     #endregion
1419     #region case SearchDataTypes._32bits:
1420     case SearchDataTypes._32bits:
1421     if (SearchArgs.IsUnsignedDataType)
1422 william 409 {
1423 william 410 uint lookup_value = 0;
1424     //using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this)) { gmp.OpenProvider(); gmp.ReadMemory(address, out lookup_value); gmp.CloseProvider(); }
1425 william 412 lookup_value = Convert.ToUInt32(result_value);
1426 william 410 using (_32bit_unsigned_comparer_ comparer = new _32bit_unsigned_comparer_(SearchArgs, address))
1427     {
1428     uint value = 0;
1429     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1430     {
1431     using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1432     {
1433     try
1434     {
1435     gmp.OpenProvider();
1436     gmp.ReadMemory(address, out value);
1437 william 424 gmp.CloseProvider();
1438 william 410 }
1439     catch (Exception ex)
1440     {
1441     logger.VerboseError.WriteLine(ex.ToString());
1442     }
1443     }
1444     comparer.Value = value;
1445     }
1446     else
1447     {
1448     value = Convert.ToUInt32(SearchArgs.CompareStartValue);
1449     comparer.Value = value;
1450     }
1451     if (comparer.Compare(lookup_value, value))
1452     {
1453     second_tmp_Results.Add(new ResultType<object>(comparer.Address, comparer.Value));
1454     }
1455     }
1456 william 409 }
1457 william 410 else
1458 william 409 {
1459 william 410 int lookup_value = 0;
1460     //using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this)) { gmp.OpenProvider(); gmp.ReadMemory(address, out lookup_value); gmp.CloseProvider(); }
1461     lookup_value = Convert.ToInt32(result_value);
1462     using (_32bit_signed_comparer_ comparer = new _32bit_signed_comparer_(SearchArgs, address))
1463     {
1464     int value = 0;
1465     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1466     {
1467     using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1468     {
1469     try
1470     {
1471     gmp.OpenProvider();
1472     gmp.ReadMemory(address, out value);
1473 william 424 gmp.CloseProvider();
1474 william 410 }
1475     catch (Exception ex)
1476     {
1477     logger.VerboseError.WriteLine(ex.ToString());
1478     }
1479     }
1480     comparer.Value = value;
1481     }
1482     else
1483     {
1484     value = Convert.ToInt32(SearchArgs.CompareStartValue);
1485     }
1486     if (comparer.Compare(lookup_value, value))
1487     {
1488     second_tmp_Results.Add(new ResultType<object>(comparer.Address, comparer.Value));
1489     }
1490     }
1491 william 409 }
1492 william 410 break;
1493     #endregion
1494     #region case SearchDataTypes._64bits:
1495     case SearchDataTypes._64bits:
1496     if (SearchArgs.IsUnsignedDataType)
1497 william 409 {
1498 william 410 ulong lookup_value = 0;
1499     //using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this)) { gmp.OpenProvider(); gmp.ReadMemory(address, out lookup_value); gmp.CloseProvider(); }
1500     lookup_value = Convert.ToUInt64(result_value);
1501     using (_64bit_unsigned_comparer_ comparer = new _64bit_unsigned_comparer_(SearchArgs, address))
1502     {
1503     ulong value = 0;
1504     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1505     {
1506     using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1507     {
1508     try
1509     {
1510     gmp.OpenProvider();
1511     gmp.ReadMemory(address, out value);
1512 william 424 gmp.CloseProvider();
1513 william 410 }
1514     catch (Exception ex)
1515     {
1516     logger.VerboseError.WriteLine(ex.ToString());
1517     }
1518     }
1519     comparer.Value = value;
1520     }
1521     else
1522     {
1523     value = Convert.ToUInt64(SearchArgs.CompareStartValue);
1524     comparer.Value = value;
1525     }
1526     if (comparer.Compare(lookup_value, value))
1527     {
1528     second_tmp_Results.Add(new ResultType<object>(comparer.Address, comparer.Value));
1529     }
1530     }
1531 william 409 }
1532 william 410 else
1533 william 409 {
1534 william 410 long lookup_value = 0;
1535     //using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this)) { gmp.OpenProvider(); gmp.ReadMemory(address, out lookup_value); gmp.CloseProvider(); }
1536     lookup_value = Convert.ToInt64(result_value);
1537     using (_64bit_signed_comparer_ comparer = new _64bit_signed_comparer_(SearchArgs, address))
1538     {
1539     long value = 0;
1540     if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1541     {
1542     using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1543     {
1544     try
1545     {
1546     gmp.OpenProvider();
1547     gmp.ReadMemory(address, out value);
1548 william 424 gmp.CloseProvider();
1549 william 410 }
1550     catch (Exception ex)
1551     {
1552     logger.VerboseError.WriteLine(ex.ToString());
1553     }
1554     }
1555     comparer.Value = value;
1556     }
1557     else
1558     {
1559     value = Convert.ToInt64(SearchArgs.CompareStartValue);
1560     }
1561     if (comparer.Compare(lookup_value, value))
1562     {
1563     second_tmp_Results.Add(new ResultType<object>(comparer.Address, comparer.Value));
1564     }
1565     }
1566 william 409 }
1567 william 410 break;
1568     #endregion
1569     #endregion
1570 william 408 }
1571 william 410 double double_percent_done = 100.0 * (double)((double)i / (double)reader.ResultCount);
1572     int int_percent_done = (int)double_percent_done;
1573     if (int_percent_done != Last_Whole_Percent_Done && i % 100000 == 0)
1574 william 408 {
1575 william 410 if (int_percent_done <= 100)
1576 william 409 {
1577 william 410 resultsprogress.Value = int_percent_done;
1578     resultsprogress.Message = string.Format(" -> Reading Address: 0x{0:x8}", i + MemoryRangeStart);
1579     Last_Whole_Percent_Done = int_percent_done;
1580 william 409 }
1581 william 402 }
1582 william 410 }
1583 william 409 }
1584 william 410 catch (Exception ex)
1585 william 409 {
1586 william 419 throw ex;
1587 william 280 }
1588 william 398 }
1589 william 280 }
1590 william 410 catch (Exception ex)
1591     {
1592 william 419 throw ex;
1593 william 410 }
1594     st_nonrange_search.Stop();
1595     logger.Profiler.WriteLine("Non-Ranged search took a total of {0} seconds to complete.", st_nonrange_search.Elapsed.TotalSeconds);
1596     Last_Whole_Percent_Done = 0;
1597     #endregion
1598 william 280 }
1599 william 410 #region Ranged Searches
1600     #if !DONOT_HAVE_RANGED_SEARCH_SUPPORT
1601     if (SearchArgs.CompareType == SearchCompareTypes.Between || SearchArgs.CompareType == SearchCompareTypes.NotBetween)
1602 william 409 {
1603 william 410 st_ranged_search.Start();
1604     object start, end;
1605 william 408
1606 william 410 start = SearchArgs.CompareStartValue;
1607     end = SearchArgs.CompareEndValue;
1608 william 444 using (SearchResultReader reader = new SearchResultReader(SearchGuid))
1609 william 402 {
1610 william 410 for (int i = 0; i < reader.ResultCount; i += 1)
1611 william 409 {
1612 william 410 uint address = 0;
1613     #region switch (SearchArgs.DataType)
1614     switch (SearchArgs.DataType)
1615 william 402 {
1616 william 410 case SearchDataTypes._8bits: if (unsigned) { using (ResultType<byte> result = reader.GetNextResult<byte>()) { address = result.Address; } }
1617     else { using (ResultType<sbyte> result = reader.GetNextResult<sbyte>()) { address = result.Address; } } break;
1618     case SearchDataTypes._16bits: if (unsigned) { using (ResultType<ushort> result = reader.GetNextResult<ushort>()) { address = result.Address; } }
1619     else { using (ResultType<short> result = reader.GetNextResult<short>()) { address = result.Address; } } break;
1620     case SearchDataTypes._32bits: if (unsigned) { using (ResultType<uint> result = reader.GetNextResult<uint>()) { address = result.Address; } }
1621     else { using (ResultType<int> result = reader.GetNextResult<int>()) { address = result.Address; } } break;
1622     case SearchDataTypes._64bits: if (unsigned) { using (ResultType<ulong> result = reader.GetNextResult<ulong>()) { address = result.Address; } }
1623     else { using (ResultType<long> result = reader.GetNextResult<long>()) { address = result.Address; } } break;
1624     }
1625     #endregion
1626    
1627     if (MemoryRangeStart > 0 && !SearchArgs.IsFirstSearch) { address = address - MemoryRangeStart; }
1628     //r_ms.BaseStream.Seek(address, SeekOrigin.Begin);
1629     if (SearchArgs.CompareType == SearchCompareTypes.Between)
1630     {
1631     using (InRangeComparer comparer = new InRangeComparer(address, 0))
1632 william 409 {
1633 william 410 if (comparer.Compare(start, end, SearchArgs.DataType, SearchArgs.IsUnsignedDataType, (IAcceptsProcessAndConfig)this))
1634     {
1635     using (ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value))
1636     {
1637     second_tmp_Results.Add(_tmp_result);
1638     }
1639     }
1640 william 409 }
1641 william 402 }
1642 william 410 else if (SearchArgs.CompareType == SearchCompareTypes.NotBetween)
1643 william 408 {
1644 william 410 using (NotInRangeComparer comparer = new NotInRangeComparer(address, 0))
1645 william 409 {
1646 william 410 if (comparer.Compare(start, end, SearchArgs.DataType, SearchArgs.IsUnsignedDataType, (IAcceptsProcessAndConfig)this))
1647     {
1648     using (ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value))
1649     {
1650     second_tmp_Results.Add(_tmp_result);
1651     }
1652     }
1653 william 409 }
1654 william 408 }
1655 william 410 else
1656 william 408 {
1657 william 410 throw new InvalidOperationException("Encounted unkown range search type: " + SearchArgs.CompareType);
1658 william 408 }
1659 william 410 double double_percent_done = 100.0 * (double)((double)i / (double)reader.ResultCount);
1660     int int_percent_done = (int)double_percent_done;
1661     if (int_percent_done != Last_Whole_Percent_Done)
1662 william 409 {
1663 william 410 if (int_percent_done <= 100)
1664     {
1665     resultsprogress.Value = int_percent_done;
1666     resultsprogress.Message = string.Format(" -> Reading Address: 0x{0:x8}", i + MemoryRangeStart);
1667     Last_Whole_Percent_Done = int_percent_done;
1668     }
1669 william 409 }
1670 william 408 }
1671     }
1672 william 410 st_ranged_search.Stop();
1673     logger.Profiler.WriteLine("Ranged search took a total of {0} seconds to complete.", st_ranged_search.Elapsed.TotalSeconds);
1674 william 280 }
1675 william 410 #endif
1676     #endregion
1677    
1678 william 408 }
1679     #endregion
1680 william 410 // leave SearchArgs.Results alone, if false
1681     if (NeedToCompare)
1682 william 408 {
1683 william 410 // fix addresses when memory start is not zero
1684     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; } }
1685 william 444 using (SearchResultWriter writer = new SearchResultWriter(second_tmp_Results.Count, SearchGuid))
1686 william 408 {
1687 william 410 for (int i = 0; i < second_tmp_Results.Count; i++)
1688 william 408 {
1689 william 410 switch (sdt)
1690     {
1691     case SearchDataTypes._8bits:
1692     if (unsigned) { writer.WriteResult<Byte>(second_tmp_Results[i].Address, Convert.ToByte(second_tmp_Results[i].Value)); }
1693     else { writer.WriteResult<SByte>(second_tmp_Results[i].Address, Convert.ToSByte(second_tmp_Results[i].Value)); } break;
1694     case SearchDataTypes._16bits:
1695     if (unsigned) { writer.WriteResult<UInt16>(second_tmp_Results[i].Address, Convert.ToUInt16(second_tmp_Results[i].Value)); }
1696     else { writer.WriteResult<Int16>(second_tmp_Results[i].Address, Convert.ToInt16(second_tmp_Results[i].Value)); } break;
1697     case SearchDataTypes._32bits:
1698     if (unsigned) { writer.WriteResult<UInt32>(second_tmp_Results[i].Address, Convert.ToUInt32(second_tmp_Results[i].Value)); }
1699     else { writer.WriteResult<Int32>(second_tmp_Results[i].Address, Convert.ToInt32(second_tmp_Results[i].Value)); } break;
1700     case SearchDataTypes._64bits:
1701     if (unsigned) { writer.WriteResult<UInt64>(second_tmp_Results[i].Address, Convert.ToUInt64(second_tmp_Results[i].Value)); }
1702     else { writer.WriteResult<Int64>(second_tmp_Results[i].Address, Convert.ToInt64(second_tmp_Results[i].Value)); } break;
1703     }
1704 william 408 }
1705     }
1706 william 410 second_tmp_Results = null; // free memory
1707 william 280 }
1708     }
1709 william 410 catch (Exception ex)
1710     {
1711 william 419 throw ex;
1712 william 410 }
1713 william 230 }
1714    
1715 william 408
1716    
1717 william 230 private void SearchWorkerThread_ProgressChanged(object sender, ProgressChangedEventArgs e)
1718     {
1719 william 280 //if (SearchArgs.ProgressLogger != null)
1720     //{
1721     // resultsprogress.Value = e.ProgressPercentage;
1722     // //Application.DoEvents();
1723     //}
1724 william 230 }
1725    
1726     private void SearchWorkerThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
1727     {
1728 william 286 if (!e.Cancelled)
1729     {
1730     Stopwatch st = (e.Result as Stopwatch);
1731     st.Stop();
1732 william 392 logger.Profiler.WriteLine("Search took {0} seconds, overall, to complete.", st.Elapsed.TotalSeconds);
1733 william 286 }
1734    
1735 william 280 resultsprogress.Value = 100;
1736 william 409 bool unsigned = SearchArgs.IsUnsignedDataType;
1737 william 444 using (SearchResultReader reader = new SearchResultReader(SearchGuid))
1738 william 402 {
1739     logger.Info.WriteLine(string.Format("Found 0x{0:x8} results", reader.ResultCount));
1740    
1741     if (reader.ResultCount <= MIN_NUMBER_OF_RESULTS_BEFORE_DISPLAY)
1742 william 280 {
1743 william 402 lstResults.Items.Clear();
1744     List<ResultItem> items = new List<ResultItem>();
1745     for (int i = 0; i < reader.ResultCount; i++)
1746 william 280 {
1747 william 402 ResultItem item = new ResultItem(0, false);
1748     //item.Text = string.Format("0x{0:x8}", SearchArgs.Results[i].Address);
1749     //item.SubItems.Add(string.Format("0x{0:x8}", SearchArgs.Results[i].Address));
1750     switch (SearchArgs.DataType)
1751     {
1752 william 280
1753 william 402 case SearchDataTypes._8bits:
1754 william 413 if (SearchArgs.IsUnsignedDataType) { ResultType<byte> result = new ResultType<byte>(); item = new ResultItem(result.Address, false, result.Value); }
1755 william 409 else { ResultType<sbyte> result = reader.GetNextResult<sbyte>(); item = new ResultItem(result.Address, false, result.Value); }
1756 william 402 break;
1757     case SearchDataTypes._16bits:
1758 william 409 if (SearchArgs.IsUnsignedDataType) { ResultType<ushort> result = reader.GetNextResult<ushort>(); item = new ResultItem(result.Address, false, result.Value); }
1759     else { ResultType<short> result = reader.GetNextResult<short>(); item = new ResultItem(result.Address, false, result.Value); }
1760 william 402 break;
1761     case SearchDataTypes._32bits:
1762 william 409 if (SearchArgs.IsUnsignedDataType) { ResultType<uint> result = reader.GetNextResult<uint>(); item = new ResultItem(result.Address, false, result.Value); }
1763     else { ResultType<int> result = reader.GetNextResult<int>(); item = new ResultItem(result.Address, false, result.Value); }
1764 william 402 break;
1765     case SearchDataTypes._64bits:
1766 william 409 if (SearchArgs.IsUnsignedDataType) { ResultType<ulong> result = reader.GetNextResult<ulong>(); item = new ResultItem(result.Address, false, result.Value); }
1767     else { ResultType<long> result = reader.GetNextResult<long>(); item = new ResultItem(result.Address, false, result.Value); }
1768 william 402 break;
1769     }
1770    
1771     if (!items.Contains(item))
1772     items.Add(item);
1773 william 280 }
1774 william 402 lstResults.Items.AddRange(items.ToArray());
1775 william 280 }
1776     }
1777    
1778     this.DoSearchDoneSpecific();
1779     //System.Threading.Thread.Sleep(100);
1780     //if (_SEARCH_MODE != SearchMode.SEARCH_MODE_NORMAL_WITH_JOKER)
1781     this.ThawResultsUpdate();
1782     Application.DoEvents();
1783 william 230 }
1784 william 280 private void DoSearchDoneSpecific()
1785     {
1786     SearchWorkerThread.CancelAsync();
1787     if (lstResults.Items.Count > 0) { timer_update_results.Enabled = true; }
1788     else { timer_update_results.Enabled = false; }
1789 william 230
1790 william 280 search_progress_updater.Enabled = false;
1791    
1792     btnCancel.Enabled = false;
1793     btnReset.Enabled = true;
1794     btnSearch.Enabled = true;
1795     grpCompareType.Enabled = true;
1796     grpCompareValue.Enabled = true;
1797     resultsprogress.Value = 0;
1798     resultsprogress.Message = "";
1799     grpDataType.Enabled = false;
1800     // resume process on reset, incase it was suspended
1801     ThreadControl.ResumeProcess(this.AcceptedProcess.Id);
1802     //Application.DoEvents();
1803     this.Refresh();
1804     }
1805    
1806     private void DoCancelSpecific()
1807     {
1808     this.DoSearchDoneSpecific();
1809     }
1810     private void DoResetSpecific()
1811     {
1812     this.DoCancelSpecific();
1813     IsFirstSearch = true;
1814     grpDataType.Enabled = true;
1815     }
1816 william 230 private void search_progress_updater_Tick(object sender, EventArgs e)
1817     {
1818     if ((this.AcceptedProcess ==null) || Process.GetProcessById(this.AcceptedProcess.Id) == null)
1819     {
1820     SearchWorkerThread.CancelAsync();
1821     //JokerSearchWorker.CancelAsync();
1822     ResultsUpdateWorkerThread.CancelAsync();
1823     }
1824     }
1825    
1826 william 280 #region Search Button
1827 william 230 private void btnSearch_Click(object sender, EventArgs e)
1828     {
1829 william 444 if (this.SearchGuid == Guid.Empty)
1830     this.SearchGuid = Guid.NewGuid();
1831 william 271 this.SearchInProgess = true;
1832 william 280 //btnCancel.Enabled = true;
1833     //btnReset.Enabled = false;
1834     //btnSearch.Enabled = false;
1835     this.FreezeResultsUpdate();
1836     this.handle_btnSearch_Click();
1837     }
1838     private void handle_btnSearch_Click()
1839     {
1840     //this.FreezeResultsUpdate();
1841     lstResults.Items.Clear();
1842    
1843     if (lstResults.Items.Count > 0) { timer_update_results.Enabled = true; }
1844     else { timer_update_results.Enabled = false; }
1845    
1846    
1847     resultsprogress.Value = 0;
1848     bool _is_unsigned = chkUnsigned.Checked;
1849     SearchType search_type = new SearchType();
1850     SearchDataTypes _data_type = new SearchDataTypes();
1851     SearchCompareTypes _compare_type = new SearchCompareTypes();
1852     CompareValueTypes _compare_value_type = new CompareValueTypes();
1853     object start_value = 0;
1854     object end_value = 0;
1855     // get datatype
1856     if (radio_8bits.Checked) { _data_type = SearchDataTypes._8bits; }
1857     else if (radio_16bits.Checked) { _data_type = SearchDataTypes._16bits; }
1858     else if (radio_32bits.Checked) { _data_type = SearchDataTypes._32bits; }
1859     else if (radio_64bits.Checked) { _data_type = SearchDataTypes._64bits; }
1860     else { logger.Error.WriteLine("Could not determine search data type bit size. (was not 8/16/32/or 64bits)"); }
1861     // get compare type
1862     if (radiocompare_equal.Checked) { _compare_type = SearchCompareTypes.Equal; }
1863     else if (radiocompare_greaterthan.Checked) { _compare_type = SearchCompareTypes.GreaterThan; }
1864     else if (radiocompare_lessthan.Checked) { _compare_type = SearchCompareTypes.LessThan; }
1865     else if (radiocompare_greaterthan_orequal.Checked) { _compare_type = SearchCompareTypes.GreaterThanOrEqual; }
1866     else if (radiocompare_lessthan_orequal.Checked) { _compare_type = SearchCompareTypes.LessThanOrEqual; }
1867     else if (radiocompare_notequal.Checked) { _compare_type = SearchCompareTypes.NotEqual; }
1868     else if (radiocompare_between.Checked) { _compare_type = SearchCompareTypes.Between; }
1869     else if (radiocompare_notbetween.Checked) { _compare_type = SearchCompareTypes.NotBetween; }
1870     else { logger.Error.WriteLine("Could not determine search comparison type. (was not == > < >= <= != <> or !<>)"); }
1871     // get compare valure type
1872     if (radio_oldvalue.Checked) { _compare_value_type = CompareValueTypes.OldValue; }
1873     else if (radio_specificvalue.Checked) { _compare_value_type = CompareValueTypes.SpecificValue; }
1874     else { logger.Error.WriteLine("Could not determine search comparison type. (was not old or specific value"); }
1875    
1876     if (_compare_value_type == CompareValueTypes.SpecificValue || (_compare_type == SearchCompareTypes.Between || _compare_type == SearchCompareTypes.NotBetween))
1877     {
1878    
1879     switch (_data_type)
1880     {
1881     case SearchDataTypes._8bits:
1882     if (_is_unsigned) { start_value = txtStartAddr.ToByte(); }
1883     else { start_value = txtStartAddr.ToSByte(); }
1884     break;
1885     case SearchDataTypes._16bits:
1886     if (_is_unsigned) { start_value = txtStartAddr.ToUInt16(); }
1887     else { start_value = txtStartAddr.ToInt16(); }
1888     break;
1889     case SearchDataTypes._32bits:
1890     if (_is_unsigned) { start_value = txtStartAddr.ToUInt32(); }
1891     else { start_value = txtStartAddr.ToInt32(); }
1892     break;
1893     case SearchDataTypes._64bits:
1894     if (_is_unsigned) { start_value = txtStartAddr.ToUInt64(); }
1895     else { start_value = txtStartAddr.ToInt64(); }
1896     break;
1897     default: throw new InvalidOperationException("In SearchType(): Encounterd an Unkown Search Data Type.");
1898     }
1899     }
1900     if (_compare_type == SearchCompareTypes.Between || _compare_type == SearchCompareTypes.NotBetween)
1901     {
1902     switch (_data_type)
1903     {
1904     case SearchDataTypes._8bits:
1905     if (_is_unsigned) { end_value = txtEndAddr.ToByte(); }
1906     else { end_value = txtEndAddr.ToSByte(); }
1907     break;
1908     case SearchDataTypes._16bits:
1909     if (_is_unsigned) { end_value = txtEndAddr.ToUInt16(); }
1910     else { end_value = txtEndAddr.ToInt16(); }
1911     break;
1912     case SearchDataTypes._32bits:
1913     if (_is_unsigned) { end_value = txtEndAddr.ToUInt32(); }
1914     else { end_value = txtEndAddr.ToInt32(); }
1915     break;
1916     case SearchDataTypes._64bits:
1917     if (_is_unsigned) { end_value = txtEndAddr.ToUInt64(); }
1918     else { end_value = txtEndAddr.ToInt64(); }
1919     break;
1920     default: throw new InvalidOperationException("In SearchType(): Encounterd an Unkown Search Data Type.");
1921     }
1922     }
1923    
1924     search_type = new SearchType(_data_type, _is_unsigned, _compare_type, _compare_value_type, start_value, end_value, resultsprogress);
1925    
1926     //search_type.LogSearchOptions();
1927    
1928     search_type.IsFirstSearch = IsFirstSearch;
1929    
1930    
1931    
1932     DoSearch(search_type);
1933     IsFirstSearch = false;
1934     }
1935     private void DoSearch(SearchType args)
1936     {
1937     if (!args.IsFirstSearch && SearchArgs != null)
1938     {
1939     //args.Results.AddRange(SearchArgs.Results.ToArray());
1940 william 402 //args.Results = SearchArgs.Results;
1941 william 280 }
1942     SearchArgs = args;
1943     #if DONOT_HAVE_RANGED_SEARCH_SUPPORT
1944     if (SearchArgs.CompareType == SearchCompareTypes.Between || SearchArgs.CompareType == SearchCompareTypes.NotBetween)
1945     {
1946     throw new NotImplementedException("Between and Not Between Range searches have not been implemented.");
1947     }
1948     #endif
1949     search_progress_updater.Enabled = true;
1950     //padPluginSelector.Enabled = false;
1951     //gsPluginSelector.Enabled = false;
1952 william 271 btnReset.Enabled = false;
1953     btnSearch.Enabled = false;
1954 william 280 btnCancel.Enabled = true;
1955     grpDataType.Enabled = false;
1956     grpCompareType.Enabled = false;
1957     grpCompareValue.Enabled = false;
1958     this.Refresh();
1959     Application.DoEvents();
1960     SearchWorkerThread.RunWorkerAsync();
1961 william 230 }
1962 william 280 #endregion
1963 william 271 private void btnReset_Click(object sender, EventArgs e)
1964     {
1965 william 444 this.SearchGuid = Guid.Empty;
1966 william 271 this.SearchInProgess = false;
1967 william 280 //btnSearch.Enabled = true;
1968     //btnCancel.Enabled = false;
1969     this.DoResetSpecific();
1970     lstResults.Items.Clear();
1971 william 402 //try { SearchArgs.Results = new List<ResultType<object>>(); }
1972     //catch { }
1973 william 271 }
1974    
1975     private void btnCancel_Click(object sender, EventArgs e)
1976     {
1977     this.SearchInProgess = false;
1978 william 280 //btnCancel.Enabled = false;
1979     //btnSearch.Enabled = true;
1980     //btnReset.Enabled = true;
1981     this.DoCancelSpecific();
1982 william 271 }
1983 william 275
1984     private void mnuItemPatchListViewMemoryRegion_Click(object sender, EventArgs e)
1985     {
1986     List<ResultDataType> patch_list = new List<ResultDataType>();
1987     List<int> SelectedIndexes = new List<int>();
1988     foreach (int index in lstPatchList.SelectedIndices) { SelectedIndexes.Add(index); }
1989     foreach (int index in SelectedIndexes)
1990     {
1991     ListViewItem item = lstPatchList.Items[index];
1992     ResultDataType rdt = (ResultDataType)item.Tag;
1993     ViewMemoryRegion(rdt);
1994     break; // only get the fist item
1995     }
1996     }
1997    
1998     private void mnuItemResultsListViewMemoryRegion_Click(object sender, EventArgs e)
1999     {
2000     List<ResultDataType> patch_list = new List<ResultDataType>();
2001     List<int> SelectedIndexes = new List<int>();
2002     foreach (int index in lstResults.SelectedIndices) { SelectedIndexes.Add(index); }
2003     foreach (int index in SelectedIndexes)
2004     {
2005     ListViewItem item = lstResults.Items[index];
2006     ResultDataType rdt = (ResultDataType)item.Tag;
2007     ViewMemoryRegion(rdt);
2008     break; // only get the fist item
2009     }
2010     }
2011     private void ViewMemoryRegion(ResultDataType rdt)
2012     {
2013     if (OnBrowseMemoryRegion != null)
2014 william 323 OnBrowseMemoryRegion(new BrowseMemoryRegionEvent(this, rdt.Address));
2015 william 275 }
2016 william 277
2017     private void mnuAddedResults_Opening(object sender, CancelEventArgs e)
2018     {
2019     if (!(lstPatchList.Items.Count > 0)) { mnuItemRemoveResult.Visible = false; e.Cancel = true; }
2020     if (!(lstPatchList.Items.Count > 0)) { mnuItemPatchSelectedEntry.Visible = false; e.Cancel = true; }
2021     if (e.Cancel) return;
2022     if (lstPatchList.Items.Count > 0) mnuItemRemoveResult.Visible = true;
2023     if (lstPatchList.Items.Count > 0) mnuItemPatchSelectedEntry.Visible = true;
2024    
2025     if (!(lstPatchList.Items.Count > 0)) { mnuItemFreezeSelectedPatches.Visible = false; e.Cancel = true; }
2026     if (!(lstPatchList.Items.Count > 0)) { mnuItemThawSelectedPatches.Visible = false; e.Cancel = true; }
2027     if (e.Cancel) return;
2028    
2029     if (lstPatchList.Items.Count > 0) mnuItemFreezeSelectedPatches.Visible = true;
2030     if (lstPatchList.Items.Count > 0) mnuItemThawSelectedPatches.Visible = true;
2031    
2032     if (lstPatchList.SelectedItems.Count == 0) e.Cancel = true;
2033     if (e.Cancel) return;
2034    
2035     }
2036    
2037     private void mnuResults_Opening(object sender, CancelEventArgs e)
2038     {
2039     if (!(lstResults.Items.Count > 0)) e.Cancel = true;
2040     if (lstResults.SelectedItems.Count == 0) e.Cancel = true;
2041     if (SearchArgs == null) e.Cancel = true;
2042     if (e.Cancel) return;
2043     }
2044 william 287
2045     private void chkMemoryRangeExpertMode_CheckedChanged(object sender, EventArgs e)
2046     {
2047 william 288 txtMemoryRangeStart.ReadOnly = !chkMemoryRangeExpertMode.Checked;
2048     txtMemoryRangeSize.ReadOnly = !chkMemoryRangeExpertMode.Checked;
2049 william 287 }
2050    
2051 william 378 private void txtMemoryRangeStart_ValueChanged(object sender, ValueChangedEventArgs e) { this.MemoryRangeStart = Convert.ToUInt32(e.NewValue); }
2052 william 288 private void txtMemoryRangeSize_ValueChanged(object sender, ValueChangedEventArgs e) { this.MemoryRangeSize = Convert.ToUInt32(e.NewValue); }
2053 william 287
2054 william 227 }
2055     }

  ViewVC Help
Powered by ViewVC 1.1.22