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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 518 - (hide annotations) (download)
Wed Jun 5 04:31:26 2013 UTC (7 years, 4 months ago) by william
File size: 178171 byte(s)
+ fix update speed of progress logging to amount of memory read

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