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

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

Parent Directory Parent Directory | Revision Log Revision Log


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