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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 473 - (hide annotations) (download)
Mon Jun 3 09:21:03 2013 UTC (7 years, 4 months ago) by william
File size: 144035 byte(s)

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