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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 538 - (show annotations) (download)
Wed Jun 5 10:33:38 2013 UTC (7 years, 4 months ago) by william
File size: 191120 byte(s)

1 #region Logging Defines
2 // include this any class or method that required logging, and comment-out what is not needed
3
4 #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 #if !USE_AUTOMATIC_MEMORY_SEARCH_RANGE
16 #define FORCE_USE_OF_MEMORYSIZECONSTANTS // when defined wil force the use of the constants defined in MemorySizeConstants for memory search range
17 #endif
18 #define DONOT_HAVE_RANGED_SEARCH_SUPPORT // when defined, indicates that ranged searches have not been implemented
19 #define INCREASE_NUMBER_OF_RESULTS_BEFORE_DISPLAY // when defined will set MIN RESULTS to 0x2701 otherwise 0x03e8
20 //#define DO_NOT_SUSPEND_RESUME_THREAD_ON_FREEZE // when defined will not freeze/resume thread on freeze
21 using System;
22 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 using RomCheater.Docking.MemorySearch;
33 using libWin32.Win32.Threading;
34 using System.Threading;
35 using RomCheater.Logging;
36 using System.IO;
37 using Sojaner.MemoryScanner.MemoryProviers;
38 using RomCheater.PluginFramework.Events;
39 using System.Reflection;
40 using Sojaner.MemoryScanner;
41 using System.Collections;
42 using RomCheater.Serialization;
43 using RomCheater.Core;
44
45 namespace RomCheater.Docking
46 {
47 public partial class FloatingMemorySearcher : DockContent,
48 IAcceptsPlugin<IConfigPlugin>,
49 IAcceptsProcess<Process>,
50 IAcceptsProcessAndConfig,
51 ISearchInProgress,
52 IAcceptsBrowseMemoryRegion,
53 IAcceptsMemoryRange
54 {
55 #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 const bool USE_OLD_SEARCH_RESULTS_COMPRATOR_CODE = false;
62 const bool USE_NONRANGE_SEARCH_RESULT_READER = false;
63 private bool DefaultUnsignedState = true; // set unsigned to true
64 public FloatingMemorySearcher() { InitializeComponent(); this.AcceptedPlugin = null; OnBrowseMemoryRegion = null; this.AcceptedProcess = null; SearchInProgess = false; Reload(); }
65 public FloatingMemorySearcher(IConfigPlugin config) : this() { this.AcceptedPlugin = config; }
66 public FloatingMemorySearcher(IConfigPlugin config, Process process) : this() { this.AcceptedPlugin = config; this.AcceptedProcess = process; }
67
68 //new Action<int, string>(UpdateProgress) to use this as a delegate
69
70 private void thread_UpdateProgress(object o)
71 {
72 IProgressMessage pm = (o as IProgressMessage);
73 if (pm == null) { return; }
74 resultsprogress.Value = pm.Progress;
75 resultsprogress.Message = pm.Message;
76 //Application.DoEvents();
77 }
78
79 private bool ShouldCancelAnySearchOperation() { return SearchWorkerThread.CancellationPending; }
80
81 private void UpdateProgress(int progress, string message)
82 {
83 // detach the progress update from execution within the current thread (so it does not block a cpu-intensive or long running sequence)
84 Thread t = new Thread(new ParameterizedThreadStart(thread_UpdateProgress));
85 t.SetApartmentState(ApartmentState.STA);
86 t.Start(new ProgressMessage(progress, message));
87 //resultsprogress.Value = progress;
88 //resultsprogress.Message = message;
89 }
90
91 #region IAcceptsProcess<Process> Members
92 private Process _AcceptedProcess;
93 public Process AcceptedProcess { get { return _AcceptedProcess; } set { _AcceptedProcess = value; UpdateAcceptedProcess(value); } }
94 #endregion
95 #region IAcceptsPlugin<IConfigPlugin> Members
96 private IConfigPlugin _AcceptedPlugin;
97 public IConfigPlugin AcceptedPlugin { get { return _AcceptedPlugin; } set { _AcceptedPlugin = value; UpdateAcceptedPlugin(value); } }
98 #endregion
99 #region IAcceptsBrowseMemoryRegion members
100 public event BaseEventHandler<BrowseMemoryRegionEvent> OnBrowseMemoryRegion;
101 #endregion
102
103 private void UpdateAcceptedPlugin(IConfigPlugin config)
104 {
105 this.lstResults.AcceptedPlugin = config;
106 this.lstPatchList.AcceptedPlugin = config;
107 if (config != null)
108 {
109 MemoryRangeStart = AcceptedPlugin.MemoryRangeStart;
110 MemoryRangeSize = AcceptedPlugin.MemoryRangeStart + AcceptedPlugin.MemoryRangeSize;
111 }
112 }
113 private void UpdateAcceptedProcess(Process process)
114 {
115 this.lstResults.AcceptedProcess = process;
116 this.lstPatchList.AcceptedProcess = process;
117 #if USE_AUTOMATIC_MEMORY_SEARCH_RANGE && FORCE_USE_OF_MEMORYSIZECONSTANTS
118 logger.Warn.WriteLine("FloatingMemorySearcher.UpdateAcceptedProcessAndConfig(IConfigPlugin config, Process process):");
119 logger.Warn.WriteLine("Both USE_AUTOMATIC_MEMORY_SEARCH_RANGE and FORCE_USE_OF_MEMORYSIZECONSTANTS are defined");
120 logger.Warn.WriteLine("FORCE_USE_OF_MEMORYSIZECONSTANTS will take precedence and will ignore the values supplied in the memeory search range");
121 #endif
122 #if FORCE_USE_OF_MEMORYSIZECONSTANTS
123 // force use of MemorySizeConstants
124 txtMemoryRangeStart.Value = MemorySizeConstants.MinimumSearchAddress;
125 txtMemoryRangeSize.Value = MemorySizeConstants.MinimumSearchAddress + MemorySizeConstants.MaximumSearchSize;
126 #endif
127 #if USE_AUTOMATIC_MEMORY_SEARCH_RANGE && !FORCE_USE_OF_MEMORYSIZECONSTANTS
128 ////// code to automatically choose the best starting memory address and size
129 //if (process != null)
130 //{
131 // string filename = process.MainModule.FileName;
132 // //string filename = @"c:\Windows\notepad.exe";
133 // PEReader peReader = new PEReader(filename);
134 //}
135 //else
136 //{
137 //txtMemoryRangeStart.Value = MemorySizeConstants.MinimumSearchAddress;
138 //txtMemoryRangeSize.Value = MemorySizeConstants.MinimumSearchAddress + MemorySizeConstants.MaximumSearchSize;
139 //}
140 if (AcceptedPlugin != null)
141 {
142 MemoryRangeStart = AcceptedPlugin.MemoryRangeStart;
143 MemoryRangeSize = AcceptedPlugin.MemoryRangeStart + AcceptedPlugin.MemoryRangeSize;
144 }
145
146 #endif
147
148 }
149 #region ISearchInProgress members
150 private bool _SearchInProgess;
151 public bool SearchInProgess
152 {
153 get { return _SearchInProgess; }
154 private set
155 {
156 _SearchInProgess = value;
157 if (this.AcceptedPlugin != null)
158 this.AcceptedPlugin.SetMemorySearchReference(this);
159 }
160 }
161 private Guid _SearchGuid;
162 public Guid SearchGuid
163 {
164 get { return _SearchGuid; }
165 private set { _SearchGuid = value; }
166 }
167 #endregion
168
169 #region IAcceptsMemoryRange
170 #if !FORCE_USE_OF_MEMORYSIZECONSTANTS
171 private uint _MemoryRangeStart;
172 private uint _MemoryRangeSize;
173 #endif
174 public uint MemoryRangeStart
175 {
176 get
177 {
178 #if FORCE_USE_OF_MEMORYSIZECONSTANTS
179 return MemorySizeConstants.MinimumSearchAddress;
180 #else
181 return _MemoryRangeStart;
182 #endif
183 }
184 set
185 {
186 #if !FORCE_USE_OF_MEMORYSIZECONSTANTS
187 _MemoryRangeStart = value;
188 txtMemoryRangeStart.Value = value;
189 #endif
190 }
191 }
192 public uint MemoryRangeSize
193 {
194 get
195 {
196 #if FORCE_USE_OF_MEMORYSIZECONSTANTS
197 return MemorySizeConstants.MinimumSearchAddress + MemorySizeConstants.MaximumSearchSize;
198 #else
199 return _MemoryRangeSize;
200 #endif
201 }
202 set
203 {
204 #if !FORCE_USE_OF_MEMORYSIZECONSTANTS
205 _MemoryRangeSize = value;
206 txtMemoryRangeSize.Value = value;
207 #endif
208 }
209 }
210 #endregion
211
212 public void Reload()
213 {
214 chkUnsigned.Checked = DefaultUnsignedState;
215 radio_8bits.Checked = true;
216 radiocompare_equal.Checked = true;
217 radio_oldvalue.Checked = true;
218 chkRefreshResults.Checked = true;
219 }
220 public enum eListViewResults
221 {
222 SEARCH_RESULTS_LIST = 0x3000,
223 PATCH_RESULTS_LIST = 0x3001,
224 UKNOWN_RESULTS_LIST = 0x3001
225 }
226 bool IsFirstSearch = true;
227 SearchType SearchArgs;
228 static int col_Found_Address = 1;
229 static int col_Found_Value = 2;
230 //static int col_Found_Frozen = 3; /* unused */
231 static int col_Added_Address = 1;
232 static int col_Added_Value = 2;
233 //static int col_Added_Frozen = 3; /* unused */
234 List<ListViewItem> ResultItems = new List<ListViewItem>();
235 List<ListViewItem> AddedItems = new List<ListViewItem>();
236 private bool _PatchedValue_NeedsUpdate;
237 bool PatchedValue_NeedsUpdate
238 {
239 get { if (_PatchedValue_NeedsUpdate) this.ThawResultsUpdate(); return _PatchedValue_NeedsUpdate; }
240 set { _PatchedValue_NeedsUpdate = value; if (value) this.ThawResultsUpdate(); }
241 }
242 private delegate ListViewItem ThreadSafe_GetResultItem(int index, int lv_type);
243 private ListViewItem GetResultItem(int index, int lv_type)
244 {
245 try
246 {
247 AddressValuePairList lv = null;
248 switch (lv_type)
249 {
250 case (int)eListViewResults.SEARCH_RESULTS_LIST: lv = lstResults; break;
251 case (int)eListViewResults.PATCH_RESULTS_LIST: lv = lstPatchList; break;
252 default: throw new IndexOutOfRangeException("Detected: " + Enum.GetName(typeof(eListViewResults), eListViewResults.UKNOWN_RESULTS_LIST) + " with value: " + lv_type.ToString("x4"));
253 }
254 ListViewItem item = new ListViewItem();
255 item = (ListViewItem)lv.Items[index].Clone();
256 return item;
257 }
258 catch (Exception)
259 {
260 return null;
261 }
262 }
263 private void radiocompare_equal_CheckedChanged(object sender, EventArgs e)
264 {
265 //if (!radiocompare_between.Checked && !radiocompare_notbetween.Checked)
266 //{
267 if (radio_oldvalue.Checked)
268 {
269 txtStartAddr.ReadOnly = true;
270 txtEndAddr.ReadOnly = true;
271 }
272 if (radio_specificvalue.Checked)
273 {
274 txtStartAddr.ReadOnly = false;
275 txtEndAddr.ReadOnly = true;
276 }
277 //}
278 }
279
280 private void radiocompare_between_CheckedChanged(object sender, EventArgs e)
281 {
282 if (!radiocompare_equal.Checked &&
283 !radiocompare_greaterthan.Checked &&
284 !radiocompare_greaterthan.Checked &&
285 !radiocompare_lessthan.Checked &&
286 !radiocompare_greaterthan_orequal.Checked &&
287 !radiocompare_lessthan_orequal.Checked &&
288 !radiocompare_notequal.Checked)
289 if (radiocompare_between.Checked)
290 {
291 txtStartAddr.ReadOnly = false;
292 txtEndAddr.ReadOnly = false;
293 return;
294 }
295 if (!radiocompare_between.Checked && !radiocompare_notbetween.Checked)
296 {
297 if (radio_oldvalue.Checked)
298 {
299 txtStartAddr.ReadOnly = true;
300 txtEndAddr.ReadOnly = true;
301 }
302 if (radio_specificvalue.Checked)
303 {
304 txtStartAddr.ReadOnly = false;
305 txtEndAddr.ReadOnly = true;
306 }
307 }
308 }
309
310 private void radiocompare_notbetween_CheckedChanged(object sender, EventArgs e)
311 {
312 if (!radiocompare_equal.Checked &&
313 !radiocompare_greaterthan.Checked &&
314 !radiocompare_greaterthan.Checked &&
315 !radiocompare_lessthan.Checked &&
316 !radiocompare_greaterthan_orequal.Checked &&
317 !radiocompare_lessthan_orequal.Checked &&
318 !radiocompare_notequal.Checked)
319 if (radiocompare_notbetween.Checked)
320 {
321 txtStartAddr.ReadOnly = false;
322 txtEndAddr.ReadOnly = false;
323 return;
324 }
325 if (!radiocompare_between.Checked && !radiocompare_notbetween.Checked)
326 {
327 if (radio_oldvalue.Checked)
328 {
329 txtStartAddr.ReadOnly = true;
330 txtEndAddr.ReadOnly = true;
331 }
332 if (radio_specificvalue.Checked)
333 {
334 txtStartAddr.ReadOnly = false;
335 txtEndAddr.ReadOnly = true;
336 }
337 }
338 }
339
340 private void radio_8bits_CheckedChanged(object sender, EventArgs e)
341 {
342 if (chkUnsigned.Checked)
343 {
344 txtStartAddr.CreateTypeSize<byte>();
345 txtEndAddr.CreateTypeSize<byte>();
346 }
347 else
348 {
349 txtStartAddr.CreateTypeSize<sbyte>();
350 txtEndAddr.CreateTypeSize<sbyte>();
351 }
352 }
353
354 private void radio_16bits_CheckedChanged(object sender, EventArgs e)
355 {
356 if (chkUnsigned.Checked)
357 {
358 txtStartAddr.CreateTypeSize<ushort>();
359 txtEndAddr.CreateTypeSize<ushort>();
360 }
361 else
362 {
363 txtStartAddr.CreateTypeSize<short>();
364 txtEndAddr.CreateTypeSize<short>();
365 }
366 }
367
368 private void radio_32bits_CheckedChanged(object sender, EventArgs e)
369 {
370
371 if (chkUnsigned.Checked)
372 {
373 txtStartAddr.CreateTypeSize<uint>();
374 txtEndAddr.CreateTypeSize<uint>();
375 }
376 else
377 {
378 txtStartAddr.CreateTypeSize<int>();
379 txtEndAddr.CreateTypeSize<int>();
380 }
381 }
382
383 private void radio_64bits_CheckedChanged(object sender, EventArgs e)
384 {
385
386 if (chkUnsigned.Checked)
387 {
388 txtStartAddr.CreateTypeSize<ulong>();
389 txtEndAddr.CreateTypeSize<ulong>();
390 }
391 else
392 {
393 txtStartAddr.CreateTypeSize<long>();
394 txtEndAddr.CreateTypeSize<long>();
395 }
396 }
397
398 private void radio_oldvalue_CheckedChanged(object sender, EventArgs e)
399 {
400 if (!radiocompare_between.Checked && !radiocompare_notbetween.Checked)
401 {
402 txtStartAddr.ReadOnly = true;
403 txtEndAddr.ReadOnly = true;
404 }
405 }
406
407 private void radio_specificvalue_CheckedChanged(object sender, EventArgs e)
408 {
409 if (!radiocompare_between.Checked && !radiocompare_notbetween.Checked)
410 {
411 txtStartAddr.ReadOnly = false;
412 txtEndAddr.ReadOnly = true;
413 }
414 }
415
416 private void chkRefreshResults_CheckedChanged(object sender, EventArgs e)
417 {
418 if (chkRefreshResults.Checked)
419 {
420 timer_update_results.Enabled = true;
421 }
422 else
423 {
424 timer_update_results.Enabled = false;
425 ResultsUpdateWorkerThread.CancelAsync();
426 }
427 }
428
429 private void timer_update_results_Tick(object sender, EventArgs e)
430 {
431 if (chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
432 {
433 ResultsUpdateWorkerThread.RunWorkerAsync();
434 }
435 }
436 private bool ShouldUpdateResults()
437 {
438 if (this.AcceptedProcess == null) return false;
439 if (SearchWorkerThread.IsBusy) return false;
440 //if (JokerSearchWorker.IsBusy) return false;
441 if (this.IsResultsUpdateFrozen) return false;
442 if (mnuAddedResults.Visible) return false;
443 if (mnuResults.Visible) return false;
444 if (Process.GetProcessById(this.AcceptedProcess.Id) == null) return false;
445 if (lstResults.Items.Count > 0) return true;
446 if (lstPatchList.Items.Count > 0) return true;
447 return false;
448 }
449 private void ResultsUpdateWorkerThread_DoWork(object sender, DoWorkEventArgs e)
450 {
451 Thread.Sleep(250); // keep thread from blocking
452 if (!this.ShouldUpdateResults()) return;
453 ////if (SearchArgs == null) return;
454 ////if (this.IsResultsUpdateFrozen) return;
455 ////// put thread to sleep for 500ms
456 ////System.Threading.Thread.Sleep(500);
457 //PCSX2MemoryProvider provider = new PCSX2MemoryProvider(this.SearchPCSX2ProcessPID, resultslog);
458 //byte[] buffered_mem = provider.GetMemory();
459 //MemoryStream ms = new MemoryStream(buffered_mem);
460 //BinaryReader r_ms = new BinaryReader(ms);
461
462 #region Update Results List
463 ResultItems = new List<ListViewItem>();
464 //r_ms.BaseStream.Seek(0, SeekOrigin.Begin);
465 for (int i = 0; i < lstResults.Items.Count; i++)
466 {
467 if (this.lstResults.InvokeRequired)
468 {
469 ThreadSafe_GetResultItem _get_item = new ThreadSafe_GetResultItem(GetResultItem);
470 object item = this.lstResults.Invoke(_get_item, new object[] { i, (int)eListViewResults.SEARCH_RESULTS_LIST });
471 if (item != null)
472 ResultItems.Add((ListViewItem)item);
473 }
474 else
475 {
476 ResultItems.Add(lstResults.Items[i]);
477 }
478
479 }
480 for (int i = 0; i < ResultItems.Count; i++)
481 {
482 if (ResultsUpdateWorkerThread.CancellationPending == true)
483 {
484 e.Cancel = true;
485 return;
486 }
487 int Address = 0;
488 ResultDataType _result = (ResultDataType)ResultItems[i].Tag;
489
490 Address = Convert.ToInt32(ResultItems[i].SubItems[col_Found_Address].Text, 16);
491 //r_ms.BaseStream.Seek(Address, SeekOrigin.Begin);
492 using (GenericMemoryProvider provider = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
493 {
494 provider.OpenProvider();
495 int bytesReadSize;
496 byte[] data;
497 uint bytesToRead = 0;
498 switch (_result.ValueType)
499 {
500 case SearchDataTypes._8bits:
501 bytesToRead = 1;
502 break;
503 case SearchDataTypes._16bits:
504 bytesToRead = 2;
505 break;
506 case SearchDataTypes._32bits:
507 bytesToRead = 4;
508 break;
509 case SearchDataTypes._64bits:
510 bytesToRead = 8;
511 break;
512 }
513 provider.ReadProcessMemory(Address, bytesToRead, out bytesReadSize, out data);
514 using (MemoryStream ms = new MemoryStream(data))
515 {
516 using (BinaryReader r_ms = new BinaryReader(ms))
517 {
518 switch (_result.ValueType)
519 {
520 case SearchDataTypes._8bits:
521 if (_result.IsUnsigned) { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x2}", r_ms.ReadByte()); }
522 else { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x2}", r_ms.ReadSByte()); }
523 break;
524 case SearchDataTypes._16bits:
525 if (_result.IsUnsigned) { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x4}", r_ms.ReadUInt16()); }
526 else { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x4}", r_ms.ReadInt16()); }
527 break;
528 case SearchDataTypes._32bits:
529 if (_result.IsUnsigned) { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x8}", r_ms.ReadUInt32()); }
530 else { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x8}", r_ms.ReadInt32()); }
531 break;
532 case SearchDataTypes._64bits:
533 if (_result.IsUnsigned) { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x16}", r_ms.ReadUInt64()); }
534 else { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x16}", r_ms.ReadInt64()); }
535 break;
536 }
537 r_ms.Close();
538 }
539 }
540 provider.CloseProvider();
541 }
542 //Application.DoEvents();
543 }
544 #endregion
545
546 #region Update Added Results List
547 AddedItems = new List<ListViewItem>();
548 //r_ms.BaseStream.Seek(0, SeekOrigin.Begin);
549 for (int i = 0; i < lstPatchList.Items.Count; i++)
550 {
551 if (this.lstResults.InvokeRequired)
552 {
553 ThreadSafe_GetResultItem _get_item = new ThreadSafe_GetResultItem(GetResultItem);
554 object item = this.lstResults.Invoke(_get_item, new object[] { i, (int)eListViewResults.PATCH_RESULTS_LIST });
555 if (item != null)
556 AddedItems.Add((ListViewItem)item);
557 }
558 else
559 {
560 AddedItems.Add(lstPatchList.Items[i]);
561 }
562
563 }
564 for (int i = 0; i < AddedItems.Count; i++)
565 {
566 if (ResultsUpdateWorkerThread.CancellationPending == true)
567 {
568 e.Cancel = true;
569 return;
570 }
571 int Address = 0;
572 ResultDataType _result = (ResultDataType)AddedItems[i].Tag;
573 Address = Convert.ToInt32(AddedItems[i].SubItems[col_Added_Address].Text, 16);
574 using (GenericMemoryProvider provider = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
575 {
576 provider.OpenProvider();
577 int bytesReadSize;
578 byte[] data;
579 uint bytesToRead = 0;
580 switch (_result.ValueType)
581 {
582 case SearchDataTypes._8bits:
583 bytesToRead = 1;
584 break;
585 case SearchDataTypes._16bits:
586 bytesToRead = 2;
587 break;
588 case SearchDataTypes._32bits:
589 bytesToRead = 4;
590 break;
591 case SearchDataTypes._64bits:
592 bytesToRead = 8;
593 break;
594 }
595 provider.ReadProcessMemory(Address, bytesToRead, out bytesReadSize, out data);
596 provider.CloseProvider();
597 using (MemoryStream ms = new MemoryStream(data))
598 {
599 using (BinaryReader r_ms = new BinaryReader(ms))
600 {
601 switch (_result.ValueType)
602 {
603 case SearchDataTypes._8bits:
604 if (_result.IsUnsigned) { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x2}", r_ms.ReadByte()); }
605 else { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x2}", r_ms.ReadSByte()); }
606 break;
607 case SearchDataTypes._16bits:
608 if (_result.IsUnsigned) { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x4}", r_ms.ReadUInt16()); }
609 else { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x4}", r_ms.ReadInt16()); }
610 break;
611 case SearchDataTypes._32bits:
612 if (_result.IsUnsigned) { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x8}", r_ms.ReadUInt32()); }
613 else { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x8}", r_ms.ReadInt32()); }
614 break;
615 case SearchDataTypes._64bits:
616 if (_result.IsUnsigned) { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x16}", r_ms.ReadUInt64()); }
617 else { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x16}", r_ms.ReadInt64()); }
618 break;
619 }
620 r_ms.Close();
621 }
622 }
623 }
624 //Application.DoEvents();
625 }
626 #endregion
627
628
629 }
630
631 private void ResultsUpdateWorkerThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
632 {
633 try
634 {
635 //if ((lstResults.SelectedItems.Count > 0) && !PatchedValue_NeedsUpdate ) return;
636 //if ((lstPatchList.SelectedItems.Count > 0) && !PatchedValue_NeedsUpdate) return;
637 if (!this.ShouldUpdateResults()) return;
638 if (ResultItems.Count > 0)
639 {
640 //lstResults.Items.Clear();
641 //lstResults.Items.AddRange(ResultItems.ToArray());
642
643 for (int i = 0; i < ResultItems.Count; i++)
644 {
645 lstResults.Items[i].SubItems[new AVPColumnText(AVPColumnType.VALUE).ColumnIndex].Text =
646 ResultItems[i].SubItems[new AVPColumnText(AVPColumnType.VALUE).ColumnIndex].Text;
647 }
648
649 }
650 if (AddedItems.Count > 0)
651 {
652 //lstPatchList.Items.Clear();
653 //lstPatchList.Items.AddRange(AddedItems.ToArray());
654
655 for (int i = 0; i < AddedItems.Count; i++)
656 {
657 lstPatchList.Items[i].SubItems[new AVPColumnText(AVPColumnType.VALUE).ColumnIndex].Text =
658 AddedItems[i].SubItems[new AVPColumnText(AVPColumnType.VALUE).ColumnIndex].Text;
659 }
660
661 }
662 PatchedValue_NeedsUpdate = false;
663 }
664 catch { }
665 }
666
667 private void btnImportFile_Click(object sender, EventArgs e)
668 {
669 this.FreezeResultsUpdate();
670 if (!lstPatchList.ImportFromFile())
671 {
672 MessageBox.Show("Failed to Import Patch List from File.", "Import Failure", MessageBoxButtons.OK, MessageBoxIcon.Error);
673 this.ThawResultsUpdate();
674 return;
675 }
676 else
677 {
678 MessageBox.Show("Succesfully Imported Patch List from File.", "Import Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
679 this.ThawResultsUpdate();
680 return;
681 }
682 }
683 bool g_isFrozen = false;
684 private bool IsResultsUpdateFrozen
685 {
686 get { return g_isFrozen; }
687 set { g_isFrozen = value; }
688 }
689 private void ThawResultsUpdate()
690 {
691 this.IsResultsUpdateFrozen = false;
692 if (this.AcceptedProcess != null)
693 {
694 #if !DO_NOT_SUSPEND_RESUME_THREAD_ON_FREEZE
695 ThreadControl.ResumeProcess(this.AcceptedProcess.Id);
696 #endif
697 }
698 }
699
700 private void FreezeResultsUpdate()
701 {
702 this.IsResultsUpdateFrozen = true;
703 //this.IsResultsUpdateFrozen = false;
704 if (this.AcceptedProcess != null)
705 {
706 #if !DO_NOT_SUSPEND_RESUME_THREAD_ON_FREEZE
707 ThreadControl.SuspendProcess(this.AcceptedProcess.Id);
708 #endif
709 }
710 }
711
712 private void btnExportFile_Click(object sender, EventArgs e)
713 {
714 this.FreezeResultsUpdate();
715 if (!lstPatchList.ExportToFile())
716 {
717 MessageBox.Show("Failed to Export Patch List to File.", "Export Failure", MessageBoxButtons.OK, MessageBoxIcon.Error);
718 this.ThawResultsUpdate();
719 return;
720 }
721 else
722 {
723 MessageBox.Show("Succesfully Exported Patch List to File.", "Export Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
724 this.ThawResultsUpdate();
725 return;
726 }
727 }
728
729 private void btnImportClipboard_Click(object sender, EventArgs e)
730 {
731 this.FreezeResultsUpdate();
732 if (!lstPatchList.ImportFromClipboard())
733 {
734 MessageBox.Show("Failed to Import Patch List from Clipboard.", "Import Failure", MessageBoxButtons.OK, MessageBoxIcon.Error);
735 this.ThawResultsUpdate();
736 return;
737 }
738 else
739 {
740 MessageBox.Show("Succesfully Import Patch List from Clipboard.", "Import Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
741 this.ThawResultsUpdate();
742 }
743 }
744
745 private void btnExportClipboard_Click(object sender, EventArgs e)
746 {
747 this.FreezeResultsUpdate();
748 if (!lstPatchList.ExportToClipboard())
749 {
750 MessageBox.Show("Failed to Export Patch List to Clipboard.", "Export Failure", MessageBoxButtons.OK, MessageBoxIcon.Error);
751 this.ThawResultsUpdate();
752 return;
753 }
754 else
755 {
756 MessageBox.Show("Succesfully Exported Patch List to Clipboard.", "Export Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
757 this.ThawResultsUpdate();
758 return;
759 }
760 }
761
762 private void btnAddPatchAddress_Click(object sender, EventArgs e)
763 {
764 PatchAdder adder = new PatchAdder((IAcceptsProcessAndConfig)this);
765 adder.ShowDialog();
766 if (adder.WasAPatchAdded) AddToPatchList(adder.AddedPatchValue);
767 }
768
769 private void btnAddAddressRange_Click(object sender, EventArgs e)
770 {
771 PatchRangeAdder adder = new PatchRangeAdder((IAcceptsProcessAndConfig)this);
772 adder.ShowDialog();
773 if (adder.WasAPatchAdded) AddToPatchList(adder.AddedPatchValue);
774 }
775 private void AddToPatchList(List<ResultDataType> item) { foreach (ResultDataType data in item) { AddToPatchList(data); } }
776 private void AddToPatchList(ResultDataType item)
777 {
778 ResultItem item2 = null;
779 switch (item.ValueType)
780 {
781 case SearchDataTypes._8bits:
782 if (item.IsUnsigned) { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToByte(item.Value)); }
783 else { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToSByte(item.Value)); }
784 break;
785 case SearchDataTypes._16bits:
786 if (item.IsUnsigned) { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToUInt16(item.Value)); }
787 else { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToInt16(item.Value)); }
788 break;
789 case SearchDataTypes._32bits:
790 if (item.IsUnsigned) { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToUInt32(item.Value)); }
791 else { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToInt32(item.Value)); }
792 break;
793 case SearchDataTypes._64bits:
794 if (item.IsUnsigned) { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToUInt64(item.Value)); }
795 else { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToInt64(item.Value)); }
796 break;
797 }
798 this.AddToPatchList(item2);
799 }
800 private void AddToPatchList(ListViewItem item)
801 {
802 try
803 {
804 ResultDataType _result = (ResultDataType)item.Tag;
805 this.AddToPatchList(_result);
806 }
807 catch (InvalidCastException ex)
808 {
809 // unable to cast
810 MessageBox.Show(ex.Message, "Invalid Cast Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
811 }
812 catch (Exception ex)
813 {
814 // other exception
815 MessageBox.Show(ex.Message, "Unhandled Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
816 }
817 }
818 private void AddToPatchList(ResultItem item)
819 {
820 if (!lstPatchList.Items.Contains(item)) lstPatchList.Items.Add(item);
821 }
822 private void AddToPatchList(string address, SearchDataTypes bitsize, bool IsUnsigned)
823 {
824 ResultItemState state = new ResultItemState(address, bitsize, IsUnsigned, (IAcceptsProcessAndConfig)this);
825 ResultItem item = new ResultItem(state.Address, state.Value, state.Frozen, state.ValueType, state.IsUnsigned);
826 this.AddToPatchList(item);
827 }
828
829 private void mnuItemAddToPatchList_Click(object sender, EventArgs e)
830 {
831 if (!(lstResults.SelectedItems.Count > 0)) return;
832 //if (SearchArgs == null) return;
833
834 try
835 {
836 for (int i = 0; i < lstResults.SelectedIndices.Count; i++)
837 {
838 //ResultDataType result = (ResultDataType)lstResults.Items[selected_index].Tag;
839 ListViewItem item = lstResults.Items[lstResults.SelectedIndices[i]];
840 this.AddToPatchList(item);
841 }
842 }
843 catch (Exception ex)
844 {
845 logger.Error.WriteLine(ex.ToString());
846 }
847 }
848
849 private void mnuItemRemoveResult_Click(object sender, EventArgs e)
850 {
851 if (!(lstPatchList.SelectedItems.Count > 0)) return;
852 //if (SearchArgs == null) return;
853 try
854 {
855 this.FreezeResultsUpdate();
856 for (int i = 0; i < lstPatchList.SelectedIndices.Count; i++)
857 {
858 //lstPatchList.ThawItem(lstPatchList.SelectedIndices[i]);
859 lstPatchList.Items[lstPatchList.SelectedIndices[i]].Remove();
860 }
861 this.ThawResultsUpdate();
862 }
863 catch (Exception ex)
864 {
865 Debug.WriteLine(ex.ToString());
866 }
867 }
868 private void PatchRange(bool SingleEntry)
869 {
870 //if (SearchArgs == null) return;
871 #region Patch Selected Address
872 // stop ResultsUpdate Thread
873 ResultsUpdateWorkerThread.CancelAsync();
874
875 List<ResultDataType> patch_list = new List<ResultDataType>();
876 List<int> SelectedIndexes = new List<int>();
877
878 if (SingleEntry)
879 {
880 SelectedIndexes.Add(lstPatchList.SelectedIndices[0]);
881 }
882 else
883 {
884 foreach (int index in lstPatchList.SelectedIndices)
885 {
886 SelectedIndexes.Add(index);
887 }
888 }
889 //PCSX2MemoryProvider provider = new PCSX2MemoryProvider(this.SearchPCSX2ProcessPID, resultslog);
890 foreach (int index in SelectedIndexes)
891 {
892 if (SingleEntry)
893 {
894 SearchPatcher patcher = null;
895 uint Address = 0;
896 ListViewItem item = lstPatchList.Items[index];
897 ResultDataType _result = (ResultDataType)item.Tag;
898 Address = Convert.ToUInt32(item.SubItems[col_Found_Address].Text, 16);
899 switch (_result.ValueType)
900 {
901 case SearchDataTypes._8bits:
902 if (_result.IsUnsigned)
903 {
904 byte value = Convert.ToByte(item.SubItems[col_Found_Value].Text, 16);
905 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
906 timer_update_results.Enabled = false;
907 patcher.ShowDialog();
908 timer_update_results.Enabled = true;
909 PatchedValue_NeedsUpdate = true;
910 if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
911 ResultsUpdateWorkerThread.RunWorkerAsync();
912 }
913 else
914 {
915 sbyte value = Convert.ToSByte(item.SubItems[col_Found_Value].Text, 16);
916 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
917 timer_update_results.Enabled = false;
918 patcher.ShowDialog();
919 timer_update_results.Enabled = true;
920 PatchedValue_NeedsUpdate = true;
921 if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
922 ResultsUpdateWorkerThread.RunWorkerAsync();
923 }
924 break;
925 case SearchDataTypes._16bits:
926 if (_result.IsUnsigned)
927 {
928 ushort value = Convert.ToUInt16(item.SubItems[col_Found_Value].Text, 16);
929 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
930 timer_update_results.Enabled = false;
931 patcher.ShowDialog();
932 timer_update_results.Enabled = true;
933 PatchedValue_NeedsUpdate = true;
934 if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
935 ResultsUpdateWorkerThread.RunWorkerAsync();
936 }
937 else
938 {
939 short value = Convert.ToInt16(item.SubItems[col_Found_Value].Text, 16);
940 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
941 timer_update_results.Enabled = false;
942 patcher.ShowDialog();
943 timer_update_results.Enabled = true;
944 PatchedValue_NeedsUpdate = true;
945 if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
946 ResultsUpdateWorkerThread.RunWorkerAsync();
947 }
948 break;
949 case SearchDataTypes._32bits:
950 if (_result.IsUnsigned)
951 {
952 uint value = Convert.ToUInt32(item.SubItems[col_Found_Value].Text, 16);
953 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
954 timer_update_results.Enabled = false;
955 patcher.ShowDialog();
956 timer_update_results.Enabled = true;
957 PatchedValue_NeedsUpdate = true;
958 if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
959 ResultsUpdateWorkerThread.RunWorkerAsync();
960 }
961 else
962 {
963 int value = Convert.ToInt32(item.SubItems[col_Found_Value].Text, 16);
964 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
965 timer_update_results.Enabled = false;
966 patcher.ShowDialog();
967 timer_update_results.Enabled = true;
968 PatchedValue_NeedsUpdate = true;
969 if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
970 ResultsUpdateWorkerThread.RunWorkerAsync();
971 }
972 break;
973 case SearchDataTypes._64bits:
974 if (_result.IsUnsigned)
975 {
976 ulong value = Convert.ToUInt32(item.SubItems[col_Found_Value].Text, 16);
977 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
978 timer_update_results.Enabled = false;
979 patcher.ShowDialog();
980 timer_update_results.Enabled = true;
981 PatchedValue_NeedsUpdate = true;
982 if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
983 ResultsUpdateWorkerThread.RunWorkerAsync();
984 }
985 else
986 {
987 long value = Convert.ToInt32(item.SubItems[col_Found_Value].Text, 16);
988 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
989 timer_update_results.Enabled = false;
990 patcher.ShowDialog();
991 timer_update_results.Enabled = true;
992 PatchedValue_NeedsUpdate = true;
993 if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
994 ResultsUpdateWorkerThread.RunWorkerAsync();
995 }
996 break;
997 }
998 }
999 else
1000 {
1001
1002 ListViewItem item = lstPatchList.Items[index];
1003 ResultDataType _result = (ResultDataType)item.Tag;
1004 patch_list.Add(_result);
1005 }
1006 }
1007
1008 if (patch_list.Count > 0)
1009 {
1010 SearchRangePatcher rangePatcher = new SearchRangePatcher((IAcceptsProcessAndConfig)this, patch_list);
1011 rangePatcher.ShowDialog();
1012 }
1013
1014 #endregion
1015 }
1016 private void mnuItemPatchSelectedEntry_Click(object sender, EventArgs e)
1017 {
1018 if (!(lstPatchList.SelectedItems.Count == 1)) return;
1019 PatchRange(true);
1020 }
1021
1022 private void mnuItemPatchSelectedRange_Click(object sender, EventArgs e)
1023 {
1024 if (!(lstPatchList.SelectedItems.Count >= 1)) return;
1025 PatchRange(false);
1026 }
1027
1028 private void mnuItemFreezeSelectedPatches_Click(object sender, EventArgs e)
1029 {
1030 if (!(lstPatchList.SelectedItems.Count > 0)) return;
1031 //if (SearchArgs == null) return;
1032 try
1033 {
1034 lstPatchList.ProcessID = this.AcceptedProcess.Id;
1035 this.FreezeResultsUpdate();
1036 for (int i = 0; i < lstPatchList.SelectedIndices.Count; i++)
1037 {
1038 lstPatchList.FreezeItem(lstPatchList.SelectedIndices[i]);
1039 }
1040 // force thaw and update
1041 this.ThawResultsUpdate();
1042 this.Update();
1043 }
1044 catch (Exception ex)
1045 {
1046 Debug.WriteLine(ex.ToString());
1047 }
1048 }
1049
1050 private void mnuItemThawSelectedPatches_Click(object sender, EventArgs e)
1051 {
1052 if (!(lstPatchList.SelectedItems.Count > 0)) return;
1053 //if (SearchArgs == null) return;
1054 try
1055 {
1056 lstPatchList.ProcessID = this.AcceptedProcess.Id;
1057 this.FreezeResultsUpdate();
1058 for (int i = 0; i < lstPatchList.SelectedIndices.Count; i++)
1059 {
1060 lstPatchList.ThawItem(lstPatchList.SelectedIndices[i]);
1061 }
1062 // force thaw and update
1063 this.ThawResultsUpdate();
1064 this.Update();
1065 }
1066 catch (Exception ex)
1067 {
1068 Debug.WriteLine(ex.ToString());
1069 }
1070 }
1071
1072 #region old-code: private void search_provider_OnBytesRead_compare_only(OnBytesReadEventArgs e)
1073 //private void search_provider_OnBytesRead_compare_only(OnBytesReadEventArgs e)
1074 //{
1075 // if (SearchWorkerThread.CancellationPending) { e.Canceled = true; return; }
1076 // SearchDataTypes sdt = SearchArgs.DataType;
1077 // bool unsigned = SearchArgs.IsUnsignedDataType;
1078 // //int Last_Whole_Percent_Done = 0;
1079 // uint CurrentIndex = e.CurrentIndex;
1080 // if (e.UserState != null)
1081 // {
1082 // SearchResultWriter writer = (e.UserState as SearchResultWriter);
1083 // if (writer == null)
1084 // throw new InvalidOperationException("writer cannot be null");
1085 // using (MemoryStream ms = new MemoryStream(e.Data))
1086 // {
1087 // using (BinaryReader br = new BinaryReader(ms))
1088 // {
1089 // while (br.BaseStream.Position < br.BaseStream.Length)
1090 // {
1091 // try
1092 // {
1093 // switch (sdt)
1094 // {
1095 // case SearchDataTypes._8bits:
1096 // if (unsigned)
1097 // {
1098 // #region 8bits - unsigned
1099 // var Value = br.ReadByte();
1100 // using (_8bit_unsigned_comparer_ comparer = new _8bit_unsigned_comparer_(SearchArgs, CurrentIndex))
1101 // {
1102 // byte value = 0;
1103 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1104 // {
1105 // using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1106 // {
1107 // try
1108 // {
1109 // gmp.OpenProvider();
1110 // gmp.ReadMemory(CurrentIndex, out value);
1111 // gmp.CloseProvider();
1112 // }
1113 // catch (Exception ex)
1114 // {
1115 // logger.VerboseError.WriteLine(ex.ToString());
1116 // }
1117 // }
1118 // comparer.Value = value;
1119 // }
1120 // else
1121 // {
1122 // value = Convert.ToByte(SearchArgs.CompareStartValue);
1123 // comparer.Value = value;
1124 // }
1125 // if (comparer.Compare(Convert.ToByte(Value), value))
1126 // {
1127 // writer.WriteResult<byte>(comparer.Address, comparer.Value);
1128 // }
1129 // }
1130 // #endregion
1131 // }
1132 // else
1133 // {
1134 // #region 8bits - signed
1135 // var Value = br.ReadSByte();
1136 // using (_8bit_signed_comparer_ comparer = new _8bit_signed_comparer_(SearchArgs, CurrentIndex))
1137 // {
1138 // sbyte value = 0;
1139 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1140 // {
1141 // using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1142 // {
1143 // try
1144 // {
1145 // gmp.OpenProvider();
1146 // gmp.ReadMemory(CurrentIndex, out value);
1147 // gmp.CloseProvider();
1148 // }
1149 // catch (Exception ex)
1150 // {
1151 // logger.VerboseError.WriteLine(ex.ToString());
1152 // }
1153 // }
1154 // comparer.Value = value;
1155 // }
1156 // else
1157 // {
1158 // value = Convert.ToSByte(SearchArgs.CompareStartValue);
1159 // comparer.Value = value;
1160 // }
1161 // if (comparer.Compare(Convert.ToSByte(Value), value))
1162 // {
1163 // writer.WriteResult<sbyte>(comparer.Address, comparer.Value);
1164 // }
1165 // }
1166 // #endregion
1167 // } break;
1168 // case SearchDataTypes._16bits:
1169 // if (unsigned)
1170 // {
1171 // #region 16bits - unsigned
1172 // var Value = br.ReadUInt16();
1173 // using (_16bit_unsigned_comparer_ comparer = new _16bit_unsigned_comparer_(SearchArgs, CurrentIndex))
1174 // {
1175 // ushort value = 0;
1176 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1177 // {
1178 // using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1179 // {
1180 // try
1181 // {
1182 // gmp.OpenProvider();
1183 // gmp.ReadMemory(CurrentIndex, out value);
1184 // gmp.CloseProvider();
1185 // }
1186 // catch (Exception ex)
1187 // {
1188 // logger.VerboseError.WriteLine(ex.ToString());
1189 // }
1190 // }
1191 // comparer.Value = value;
1192 // }
1193 // else
1194 // {
1195 // value = Convert.ToUInt16(SearchArgs.CompareStartValue);
1196 // comparer.Value = value;
1197 // }
1198 // if (comparer.Compare(Convert.ToUInt16(Value), value))
1199 // {
1200 // writer.WriteResult<ushort>(comparer.Address, comparer.Value);
1201 // }
1202 // }
1203 // #endregion
1204 // }
1205 // else
1206 // {
1207 // #region 16bits - signed
1208 // var Value = br.ReadInt16();
1209 // using (_16bit_signed_comparer_ comparer = new _16bit_signed_comparer_(SearchArgs, CurrentIndex))
1210 // {
1211 // short value = 0;
1212 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1213 // {
1214 // using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1215 // {
1216 // try
1217 // {
1218 // gmp.OpenProvider();
1219 // gmp.ReadMemory(CurrentIndex, out value);
1220 // gmp.CloseProvider();
1221 // }
1222 // catch (Exception ex)
1223 // {
1224 // logger.VerboseError.WriteLine(ex.ToString());
1225 // }
1226 // }
1227 // comparer.Value = value;
1228 // }
1229 // else
1230 // {
1231 // value = Convert.ToInt16(SearchArgs.CompareStartValue);
1232 // comparer.Value = value;
1233 // }
1234 // if (comparer.Compare(Convert.ToSByte(Value), value))
1235 // {
1236 // writer.WriteResult<short>(comparer.Address, comparer.Value);
1237 // }
1238 // }
1239 // #endregion
1240 // } break;
1241 // case SearchDataTypes._32bits:
1242 // if (unsigned)
1243 // {
1244 // #region 32bits - unsigned
1245 // var Value = br.ReadUInt32();
1246 // using (_32bit_unsigned_comparer_ comparer = new _32bit_unsigned_comparer_(SearchArgs, CurrentIndex))
1247 // {
1248 // uint value = 0;
1249 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1250 // {
1251 // using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1252 // {
1253 // try
1254 // {
1255 // gmp.OpenProvider();
1256 // gmp.ReadMemory(CurrentIndex, out value);
1257 // gmp.CloseProvider();
1258 // }
1259 // catch (Exception ex)
1260 // {
1261 // logger.VerboseError.WriteLine(ex.ToString());
1262 // }
1263 // }
1264 // comparer.Value = value;
1265 // }
1266 // else
1267 // {
1268 // value = Convert.ToUInt32(SearchArgs.CompareStartValue);
1269 // comparer.Value = value;
1270 // }
1271 // if (comparer.Compare(Convert.ToUInt32(Value), value))
1272 // {
1273 // writer.WriteResult<uint>(comparer.Address, comparer.Value);
1274 // }
1275 // }
1276 // #endregion
1277 // }
1278 // else
1279 // {
1280 // #region 32bits - signed
1281 // var Value = br.ReadInt32();
1282 // using (_32bit_signed_comparer_ comparer = new _32bit_signed_comparer_(SearchArgs, CurrentIndex))
1283 // {
1284 // int value = 0;
1285 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1286 // {
1287 // using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1288 // {
1289 // try
1290 // {
1291 // gmp.OpenProvider();
1292 // gmp.ReadMemory(CurrentIndex, out value);
1293 // gmp.CloseProvider();
1294 // }
1295 // catch (Exception ex)
1296 // {
1297 // logger.VerboseError.WriteLine(ex.ToString());
1298 // }
1299 // }
1300 // comparer.Value = value;
1301 // }
1302 // else
1303 // {
1304 // value = Convert.ToInt32(SearchArgs.CompareStartValue);
1305 // comparer.Value = value;
1306 // }
1307 // if (comparer.Compare(Convert.ToInt32(Value), value))
1308 // {
1309 // writer.WriteResult<int>(comparer.Address, comparer.Value);
1310 // }
1311 // }
1312 // #endregion
1313 // } break;
1314 // case SearchDataTypes._64bits:
1315 // if (unsigned)
1316 // {
1317 // #region 64bits - unsigned
1318 // var Value = br.ReadUInt64();
1319 // using (_64bit_unsigned_comparer_ comparer = new _64bit_unsigned_comparer_(SearchArgs, CurrentIndex))
1320 // {
1321 // ulong value = 0;
1322 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1323 // {
1324 // using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1325 // {
1326 // try
1327 // {
1328 // gmp.OpenProvider();
1329 // gmp.ReadMemory(CurrentIndex, out value);
1330 // gmp.CloseProvider();
1331 // }
1332 // catch (Exception ex)
1333 // {
1334 // logger.VerboseError.WriteLine(ex.ToString());
1335 // }
1336 // }
1337 // comparer.Value = value;
1338 // }
1339 // else
1340 // {
1341 // value = Convert.ToUInt64(SearchArgs.CompareStartValue);
1342 // comparer.Value = value;
1343 // }
1344 // if (comparer.Compare(Convert.ToUInt64(Value), value))
1345 // {
1346 // writer.WriteResult<ulong>(comparer.Address, comparer.Value);
1347 // }
1348 // }
1349 // #endregion
1350 // }
1351 // else
1352 // {
1353 // #region 64bits - signed
1354 // var Value = br.ReadInt64();
1355 // using (_64bit_signed_comparer_ comparer = new _64bit_signed_comparer_(SearchArgs, CurrentIndex))
1356 // {
1357 // long value = 0;
1358 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1359 // {
1360 // using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1361 // {
1362 // try
1363 // {
1364 // gmp.OpenProvider();
1365 // gmp.ReadMemory(CurrentIndex, out value);
1366 // gmp.CloseProvider();
1367 // }
1368 // catch (Exception ex)
1369 // {
1370 // logger.VerboseError.WriteLine(ex.ToString());
1371 // }
1372 // }
1373 // comparer.Value = value;
1374 // }
1375 // else
1376 // {
1377 // value = Convert.ToInt64(SearchArgs.CompareStartValue);
1378 // comparer.Value = value;
1379 // }
1380 // if (comparer.Compare(Convert.ToInt64(Value), value))
1381 // {
1382 // writer.WriteResult<long>(comparer.Address, comparer.Value);
1383 // }
1384 // }
1385 // #endregion
1386 // } break;
1387 // }
1388 // if (SearchWorkerThread.CancellationPending) { e.Canceled = true; return; }
1389 // }
1390 // catch (EndOfStreamException) { }
1391 // if (e.ReportProgress)
1392 // {
1393 // double double_percent_done = 100.0 * (double)((double)CurrentIndex / (double)e.TotalCount);
1394 // int int_percent_done = (int)double_percent_done;
1395 // //if (CurrentIndex % 10 == 0)
1396 // //{
1397 // // if (int_percent_done <= 100)
1398 // // {
1399 // new Action<int, string>(UpdateProgress).Invoke(int_percent_done, string.Format(" -> Reading Index: 0x{0:x8} of 0x{1:x8}", CurrentIndex, e.TotalCount));
1400 // // }
1401 // // if (SearchWorkerThread.CancellationPending) { e.Canceled = true; return; }
1402 // //}
1403 // }
1404 // //switch (sdt)
1405 // //{
1406 // // case SearchDataTypes._8bits:
1407 // // CurrentIndex += sizeof(byte);
1408 // // break;
1409 // // case SearchDataTypes._16bits:
1410 // // CurrentIndex += sizeof(ushort);
1411 // // break;
1412 // // case SearchDataTypes._32bits:
1413 // // CurrentIndex += sizeof(uint);
1414 // // break;
1415 // // case SearchDataTypes._64bits:
1416 // // CurrentIndex += sizeof(ulong);
1417 // // break;
1418 // //}
1419 // }
1420 // }
1421 // }
1422 // }
1423 // if (SearchWorkerThread.CancellationPending) { e.Canceled = true; return; }
1424 //}
1425 #endregion
1426
1427 #region old-code: private void search_provider_OnBytesRead(OnBytesReadEventArgs e)
1428 //private void search_provider_OnBytesRead(OnBytesReadEventArgs e)
1429 //{
1430 // if (SearchWorkerThread.CancellationPending) { e.Canceled = true; return; }
1431 // SearchDataTypes sdt = SearchArgs.DataType;
1432 // bool unsigned = SearchArgs.IsUnsignedDataType;
1433 // int Last_Whole_Percent_Done = 0;
1434 // uint CurrentIndex = e.CurrentIndex;
1435 // if (e.UserState != null)
1436 // {
1437 // SearchResultWriter writer = (e.UserState as SearchResultWriter);
1438 // if (writer == null)
1439 // throw new InvalidOperationException("writer cannot be null");
1440 // using (MemoryStream ms = new MemoryStream(e.Data))
1441 // {
1442 // using (BinaryReader br = new BinaryReader(ms))
1443 // {
1444 // while (br.BaseStream.Position < br.BaseStream.Length)
1445 // {
1446 // try
1447 // {
1448 // //if (e.ReportProgress)
1449 // //{
1450 // // double double_percent_done = 100.0 * (double)((double)CurrentIndex / (double)e.TotalCount);
1451 // // int int_percent_done = (int)double_percent_done;
1452 // // int align_base = 100;
1453 // // int align_power = 1;
1454 // // int align = align_base * align_power;
1455 // // if (int_percent_done != Last_Whole_Percent_Done && (CurrentIndex % align) == 0)
1456 // // {
1457 // // string message = string.Format(" -> Reading Address: 0x{0:x8}", (CurrentIndex + MemoryRangeStart));
1458 // // UpdateProgress((int)double_percent_done, message);
1459 // // //new Action<int, string>(UpdateProgress).Invoke(int_percent_done, message);
1460 // // Last_Whole_Percent_Done = int_percent_done;
1461 // // if (SearchWorkerThread.CancellationPending) { e.Canceled = true; return; }
1462 // // }
1463 // //}
1464
1465 // switch (sdt)
1466 // {
1467 // case SearchDataTypes._8bits:
1468 // if (unsigned)
1469 // {
1470 // #region 8bits - unsigned
1471 // var Value = br.ReadByte();
1472 // using (_8bit_unsigned_comparer_ comparer = new _8bit_unsigned_comparer_(SearchArgs, CurrentIndex))
1473 // {
1474 // byte value = 0;
1475 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1476 // {
1477 // using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1478 // {
1479 // try
1480 // {
1481 // gmp.OpenProvider();
1482 // gmp.ReadMemory(CurrentIndex, out value);
1483 // gmp.CloseProvider();
1484 // }
1485 // catch (Exception ex)
1486 // {
1487 // logger.VerboseError.WriteLine(ex.ToString());
1488 // }
1489 // }
1490 // comparer.Value = value;
1491 // }
1492 // else
1493 // {
1494 // value = Convert.ToByte(SearchArgs.CompareStartValue);
1495 // comparer.Value = value;
1496 // }
1497 // if (comparer.Compare(Convert.ToByte(Value), value))
1498 // {
1499 // writer.WriteResult<byte>(comparer.Address, comparer.Value);
1500 // }
1501 // }
1502 // #endregion
1503 // }
1504 // else
1505 // {
1506 // #region 8bits - signed
1507 // var Value = br.ReadSByte();
1508 // using (_8bit_signed_comparer_ comparer = new _8bit_signed_comparer_(SearchArgs, CurrentIndex))
1509 // {
1510 // sbyte value = 0;
1511 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1512 // {
1513 // using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1514 // {
1515 // try
1516 // {
1517 // gmp.OpenProvider();
1518 // gmp.ReadMemory(CurrentIndex, out value);
1519 // gmp.CloseProvider();
1520 // }
1521 // catch (Exception ex)
1522 // {
1523 // logger.VerboseError.WriteLine(ex.ToString());
1524 // }
1525 // }
1526 // comparer.Value = value;
1527 // }
1528 // else
1529 // {
1530 // value = Convert.ToSByte(SearchArgs.CompareStartValue);
1531 // comparer.Value = value;
1532 // }
1533 // if (comparer.Compare(Convert.ToSByte(Value), value))
1534 // {
1535 // writer.WriteResult<sbyte>(comparer.Address, comparer.Value);
1536 // }
1537 // }
1538 // #endregion
1539 // } break;
1540 // case SearchDataTypes._16bits:
1541 // if (unsigned)
1542 // {
1543 // #region 16bits - unsigned
1544 // var Value = br.ReadUInt16();
1545 // using (_16bit_unsigned_comparer_ comparer = new _16bit_unsigned_comparer_(SearchArgs, CurrentIndex))
1546 // {
1547 // ushort value = 0;
1548 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1549 // {
1550 // using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1551 // {
1552 // try
1553 // {
1554 // gmp.OpenProvider();
1555 // gmp.ReadMemory(CurrentIndex, out value);
1556 // gmp.CloseProvider();
1557 // }
1558 // catch (Exception ex)
1559 // {
1560 // logger.VerboseError.WriteLine(ex.ToString());
1561 // }
1562 // }
1563 // comparer.Value = value;
1564 // }
1565 // else
1566 // {
1567 // value = Convert.ToUInt16(SearchArgs.CompareStartValue);
1568 // comparer.Value = value;
1569 // }
1570 // if (comparer.Compare(Convert.ToUInt16(Value), value))
1571 // {
1572 // writer.WriteResult<ushort>(comparer.Address, comparer.Value);
1573 // }
1574 // }
1575 // #endregion
1576 // }
1577 // else
1578 // {
1579 // #region 16bits - signed
1580 // var Value = br.ReadInt16();
1581 // using (_16bit_signed_comparer_ comparer = new _16bit_signed_comparer_(SearchArgs, CurrentIndex))
1582 // {
1583 // short value = 0;
1584 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1585 // {
1586 // using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1587 // {
1588 // try
1589 // {
1590 // gmp.OpenProvider();
1591 // gmp.ReadMemory(CurrentIndex, out value);
1592 // gmp.CloseProvider();
1593 // }
1594 // catch (Exception ex)
1595 // {
1596 // logger.VerboseError.WriteLine(ex.ToString());
1597 // }
1598 // }
1599 // comparer.Value = value;
1600 // }
1601 // else
1602 // {
1603 // value = Convert.ToInt16(SearchArgs.CompareStartValue);
1604 // comparer.Value = value;
1605 // }
1606 // if (comparer.Compare(Convert.ToSByte(Value), value))
1607 // {
1608 // writer.WriteResult<short>(comparer.Address, comparer.Value);
1609 // }
1610 // }
1611 // #endregion
1612 // } break;
1613 // case SearchDataTypes._32bits:
1614 // if (unsigned)
1615 // {
1616 // #region 32bits - unsigned
1617 // var Value = br.ReadUInt32();
1618 // using (_32bit_unsigned_comparer_ comparer = new _32bit_unsigned_comparer_(SearchArgs, CurrentIndex))
1619 // {
1620 // uint value = 0;
1621 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1622 // {
1623 // using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1624 // {
1625 // try
1626 // {
1627 // gmp.OpenProvider();
1628 // gmp.ReadMemory(CurrentIndex, out value);
1629 // gmp.CloseProvider();
1630 // }
1631 // catch (Exception ex)
1632 // {
1633 // logger.VerboseError.WriteLine(ex.ToString());
1634 // }
1635 // }
1636 // comparer.Value = value;
1637 // }
1638 // else
1639 // {
1640 // value = Convert.ToUInt32(SearchArgs.CompareStartValue);
1641 // comparer.Value = value;
1642 // }
1643 // if (comparer.Compare(Convert.ToUInt32(Value), value))
1644 // {
1645 // writer.WriteResult<uint>(comparer.Address, comparer.Value);
1646 // }
1647 // }
1648 // #endregion
1649 // }
1650 // else
1651 // {
1652 // #region 32bits - signed
1653 // var Value = br.ReadInt32();
1654 // using (_32bit_signed_comparer_ comparer = new _32bit_signed_comparer_(SearchArgs, CurrentIndex))
1655 // {
1656 // int value = 0;
1657 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1658 // {
1659 // using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1660 // {
1661 // try
1662 // {
1663 // gmp.OpenProvider();
1664 // gmp.ReadMemory(CurrentIndex, out value);
1665 // gmp.CloseProvider();
1666 // }
1667 // catch (Exception ex)
1668 // {
1669 // logger.VerboseError.WriteLine(ex.ToString());
1670 // }
1671 // }
1672 // comparer.Value = value;
1673 // }
1674 // else
1675 // {
1676 // value = Convert.ToInt32(SearchArgs.CompareStartValue);
1677 // comparer.Value = value;
1678 // }
1679 // if (comparer.Compare(Convert.ToInt32(Value), value))
1680 // {
1681 // writer.WriteResult<int>(comparer.Address, comparer.Value);
1682 // }
1683 // }
1684 // #endregion
1685 // } break;
1686 // case SearchDataTypes._64bits:
1687 // if (unsigned)
1688 // {
1689 // #region 64bits - unsigned
1690 // var Value = br.ReadUInt64();
1691 // using (_64bit_unsigned_comparer_ comparer = new _64bit_unsigned_comparer_(SearchArgs, CurrentIndex))
1692 // {
1693 // ulong value = 0;
1694 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1695 // {
1696 // using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1697 // {
1698 // try
1699 // {
1700 // gmp.OpenProvider();
1701 // gmp.ReadMemory(CurrentIndex, out value);
1702 // gmp.CloseProvider();
1703 // }
1704 // catch (Exception ex)
1705 // {
1706 // logger.VerboseError.WriteLine(ex.ToString());
1707 // }
1708 // }
1709 // comparer.Value = value;
1710 // }
1711 // else
1712 // {
1713 // value = Convert.ToUInt64(SearchArgs.CompareStartValue);
1714 // comparer.Value = value;
1715 // }
1716 // if (comparer.Compare(Convert.ToUInt64(Value), value))
1717 // {
1718 // writer.WriteResult<ulong>(comparer.Address, comparer.Value);
1719 // }
1720 // }
1721 // #endregion
1722 // }
1723 // else
1724 // {
1725 // #region 64bits - signed
1726 // var Value = br.ReadInt64();
1727 // using (_64bit_signed_comparer_ comparer = new _64bit_signed_comparer_(SearchArgs, CurrentIndex))
1728 // {
1729 // long value = 0;
1730 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1731 // {
1732 // using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1733 // {
1734 // try
1735 // {
1736 // gmp.OpenProvider();
1737 // gmp.ReadMemory(CurrentIndex, out value);
1738 // gmp.CloseProvider();
1739 // }
1740 // catch (Exception ex)
1741 // {
1742 // logger.VerboseError.WriteLine(ex.ToString());
1743 // }
1744 // }
1745 // comparer.Value = value;
1746 // }
1747 // else
1748 // {
1749 // value = Convert.ToInt64(SearchArgs.CompareStartValue);
1750 // comparer.Value = value;
1751 // }
1752 // if (comparer.Compare(Convert.ToInt64(Value), value))
1753 // {
1754 // writer.WriteResult<long>(comparer.Address, comparer.Value);
1755 // }
1756 // }
1757 // #endregion
1758 // } break;
1759 // }
1760 // if (SearchWorkerThread.CancellationPending) { e.Canceled = true; return; }
1761 // }
1762 // catch (EndOfStreamException) { }
1763
1764 // uint size = 0;
1765 // BitTools.SizeOf(sdt, out size);
1766 // CurrentIndex += size;
1767 // }
1768 // }
1769 // }
1770 // }
1771 // if (SearchWorkerThread.CancellationPending) { e.Canceled = true; return; }
1772 //}
1773 #endregion
1774
1775 private void SearchWorkerThread_DoWork(object sender, DoWorkEventArgs e)
1776 {
1777 try
1778 {
1779 Stopwatch st = new Stopwatch();
1780 st.Start();
1781
1782 Stopwatch st_first_search = new Stopwatch();
1783 Stopwatch st_nonrange_search = new Stopwatch();
1784 Stopwatch st_ranged_search = new Stopwatch();
1785
1786 e.Result = st;
1787 //List<ResultType<object>> tmp_Results = new List<ResultType<object>>();
1788 List<ResultType<ulong>> second_tmp_Results = new List<ResultType<ulong>>();
1789 //const int ElementsBeforeDisplay = 100;
1790 SearchArgs.LogSearchOptions();
1791 uint STEP_SIZE = (uint)SearchArgs.DataType / 8;
1792
1793 bool unsigned = SearchArgs.IsUnsignedDataType;
1794 SearchDataTypes sdt = SearchArgs.DataType;
1795 //byte[] buffered_mem = new byte[(MemoryRangeSize - MemoryRangeStart)]; // throws OutOfMemoryException if size is over 2G
1796 logger.Debug.WriteLine(string.Format("Buffered Memory Size -> 0x{0:x8}", MemoryRangeSize - MemoryRangeStart));
1797 UpdateProgress(0, string.Format("Search is Warming Up...Please Wait..."));
1798 //new Action<int, string>(UpdateProgress).Invoke(0, string.Format("Search is Warming Up...Please Wait..."));
1799 Stopwatch provider_st = new Stopwatch();
1800 provider_st.Start();
1801 using (GenericMemoryProvider provider = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1802 {
1803
1804 if (SearchArgs.IsFirstSearch)
1805 {
1806 provider.OpenProvider();
1807 int count = (int)((MemoryRangeSize - MemoryRangeStart) / STEP_SIZE);
1808 SearchResultWriter writer = new SearchResultWriter((int)(count), SearchGuid);
1809 int Last_Whole_Percent_Done = 0;
1810 uint size = STEP_SIZE * 4096;
1811 for (uint i = MemoryRangeStart; i < MemoryRangeSize; i+=size)
1812 {
1813
1814 int bytesRead=0;
1815 byte[] data = new byte[size];
1816 provider.ReadProcessMemoryAtOnce(i, size, out bytesRead, out data);
1817 string message = string.Format(" -> Reading Address: 0x{0:x8}", (i + MemoryRangeStart));
1818 double double_percent_done = 100.0 * (double)((double)i / (double)MemoryRangeSize);
1819 int int_percent_done = (int)double_percent_done;
1820 if ( (int_percent_done != Last_Whole_Percent_Done) && (i % 10000) == 0)
1821 {
1822 UpdateProgress((int)double_percent_done, message);
1823 Last_Whole_Percent_Done = int_percent_done;
1824 }
1825
1826 uint address_index = i;
1827 #region comparison and serialization
1828 switch (sdt)
1829 {
1830 case SearchDataTypes._8bits:
1831 if (unsigned)
1832 {
1833 #region 8bits - unsigned
1834 var t_data = data;
1835 foreach (var Value in t_data)
1836 {
1837 if (SearchWorkerThread.CancellationPending) { break; }
1838 using (_8bit_unsigned_comparer_ comparer = new _8bit_unsigned_comparer_(SearchArgs, address_index))
1839 {
1840 byte value = 0;
1841 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1842 {
1843 using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1844 {
1845 try
1846 {
1847 gmp.OpenProvider();
1848 gmp.ReadMemory(address_index, out value);
1849 gmp.CloseProvider();
1850 }
1851 catch (Exception ex)
1852 {
1853 logger.VerboseError.WriteLine(ex.ToString());
1854 }
1855 }
1856 comparer.Value = value;
1857 }
1858 else
1859 {
1860 value = Convert.ToByte(SearchArgs.CompareStartValue);
1861 comparer.Value = value;
1862 }
1863 if (comparer.Compare(Convert.ToByte(Value), value))
1864 {
1865 writer.WriteResult<byte>(comparer.Address, comparer.Value);
1866 }
1867 }
1868 address_index += STEP_SIZE;
1869 }
1870 #endregion
1871 }
1872 else
1873 {
1874 #region 8bits - signed
1875 var t_data = data;
1876 foreach (var Value in t_data)
1877 {
1878 if (SearchWorkerThread.CancellationPending) { break; }
1879 using (_8bit_signed_comparer_ comparer = new _8bit_signed_comparer_(SearchArgs, address_index))
1880 {
1881 sbyte value = 0;
1882 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1883 {
1884 using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1885 {
1886 try
1887 {
1888 gmp.OpenProvider();
1889 gmp.ReadMemory(address_index, out value);
1890 gmp.CloseProvider();
1891 }
1892 catch (Exception ex)
1893 {
1894 logger.VerboseError.WriteLine(ex.ToString());
1895 }
1896 }
1897 comparer.Value = value;
1898 }
1899 else
1900 {
1901 value = Convert.ToSByte(SearchArgs.CompareStartValue);
1902 comparer.Value = value;
1903 }
1904 if (comparer.Compare(Convert.ToSByte(Value), value))
1905 {
1906 writer.WriteResult<sbyte>(comparer.Address, comparer.Value);
1907 }
1908 }
1909 address_index += STEP_SIZE;
1910 }
1911 #endregion
1912 } break;
1913 case SearchDataTypes._16bits:
1914 if (unsigned)
1915 {
1916 #region 16bits - unsigned
1917 var t_data = BitTools.ConvertByteArray<ushort>(data, new Func<bool>(ShouldCancelAnySearchOperation));
1918 foreach (var Value in t_data)
1919 {
1920 if (SearchWorkerThread.CancellationPending) { break; }
1921 using (_16bit_unsigned_comparer_ comparer = new _16bit_unsigned_comparer_(SearchArgs, address_index))
1922 {
1923 ushort value = 0;
1924 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1925 {
1926 using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1927 {
1928 try
1929 {
1930 gmp.OpenProvider();
1931 gmp.ReadMemory(address_index, out value);
1932 gmp.CloseProvider();
1933 }
1934 catch (Exception ex)
1935 {
1936 logger.VerboseError.WriteLine(ex.ToString());
1937 }
1938 }
1939 comparer.Value = value;
1940 }
1941 else
1942 {
1943 value = Convert.ToUInt16(SearchArgs.CompareStartValue);
1944 comparer.Value = value;
1945 }
1946 if (comparer.Compare(Convert.ToUInt16(Value), value))
1947 {
1948 writer.WriteResult<ushort>(comparer.Address, comparer.Value);
1949 }
1950 }
1951 address_index += STEP_SIZE;
1952 }
1953 #endregion
1954 }
1955 else
1956 {
1957 #region 16bits - signed
1958 var t_data = BitTools.ConvertByteArray<short>(data, new Func<bool>(ShouldCancelAnySearchOperation));
1959 foreach (var Value in t_data)
1960 {
1961 if (SearchWorkerThread.CancellationPending) { break; }
1962 using (_16bit_signed_comparer_ comparer = new _16bit_signed_comparer_(SearchArgs, address_index))
1963 {
1964 short value = 0;
1965 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1966 {
1967 using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1968 {
1969 try
1970 {
1971 gmp.OpenProvider();
1972 gmp.ReadMemory(address_index, out value);
1973 gmp.CloseProvider();
1974 }
1975 catch (Exception ex)
1976 {
1977 logger.VerboseError.WriteLine(ex.ToString());
1978 }
1979 }
1980 comparer.Value = value;
1981 }
1982 else
1983 {
1984 value = Convert.ToInt16(SearchArgs.CompareStartValue);
1985 comparer.Value = value;
1986 }
1987 if (comparer.Compare(Convert.ToSByte(Value), value))
1988 {
1989 writer.WriteResult<short>(comparer.Address, comparer.Value);
1990 }
1991 }
1992 address_index += STEP_SIZE;
1993 }
1994 #endregion
1995 } break;
1996 case SearchDataTypes._32bits:
1997 if (unsigned)
1998 {
1999 #region 32bits - unsigned
2000 var t_data = BitTools.ConvertByteArray<uint>(data, new Func<bool>(ShouldCancelAnySearchOperation));
2001 foreach (var Value in t_data)
2002 {
2003 if (SearchWorkerThread.CancellationPending) { break; }
2004 using (_32bit_unsigned_comparer_ comparer = new _32bit_unsigned_comparer_(SearchArgs, address_index))
2005 {
2006 uint value = 0;
2007 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
2008 {
2009 using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
2010 {
2011 try
2012 {
2013 gmp.OpenProvider();
2014 gmp.ReadMemory(address_index, out value);
2015 gmp.CloseProvider();
2016 }
2017 catch (Exception ex)
2018 {
2019 logger.VerboseError.WriteLine(ex.ToString());
2020 }
2021 }
2022 comparer.Value = value;
2023 }
2024 else
2025 {
2026 value = Convert.ToUInt32(SearchArgs.CompareStartValue);
2027 comparer.Value = value;
2028 }
2029 if (comparer.Compare(Convert.ToUInt32(Value), value))
2030 {
2031 writer.WriteResult<uint>(comparer.Address, comparer.Value);
2032 }
2033 }
2034 address_index += STEP_SIZE;
2035 }
2036 #endregion
2037 }
2038 else
2039 {
2040 #region 32bits - signed
2041 var t_data = BitTools.ConvertByteArray<int>(data, new Func<bool>(ShouldCancelAnySearchOperation));
2042 foreach (var Value in t_data)
2043 {
2044 if (SearchWorkerThread.CancellationPending) { break; }
2045 using (_32bit_signed_comparer_ comparer = new _32bit_signed_comparer_(SearchArgs, address_index))
2046 {
2047 int value = 0;
2048 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
2049 {
2050 using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
2051 {
2052 try
2053 {
2054 gmp.OpenProvider();
2055 gmp.ReadMemory(address_index, out value);
2056 gmp.CloseProvider();
2057 }
2058 catch (Exception ex)
2059 {
2060 logger.VerboseError.WriteLine(ex.ToString());
2061 }
2062 }
2063 comparer.Value = value;
2064 }
2065 else
2066 {
2067 value = Convert.ToInt32(SearchArgs.CompareStartValue);
2068 comparer.Value = value;
2069 }
2070 if (comparer.Compare(Convert.ToInt32(Value), value))
2071 {
2072 writer.WriteResult<int>(comparer.Address, comparer.Value);
2073 }
2074 }
2075 address_index += STEP_SIZE;
2076 }
2077 #endregion
2078 } break;
2079 case SearchDataTypes._64bits:
2080 if (unsigned)
2081 {
2082 #region 64bits - unsigned
2083 var t_data = BitTools.ConvertByteArray<ulong>(data, new Func<bool>(ShouldCancelAnySearchOperation));
2084 foreach (var Value in t_data)
2085 {
2086 if (SearchWorkerThread.CancellationPending) { break; }
2087 using (_64bit_unsigned_comparer_ comparer = new _64bit_unsigned_comparer_(SearchArgs, address_index))
2088 {
2089 ulong value = 0;
2090 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
2091 {
2092 using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
2093 {
2094 try
2095 {
2096 gmp.OpenProvider();
2097 gmp.ReadMemory(address_index, out value);
2098 gmp.CloseProvider();
2099 }
2100 catch (Exception ex)
2101 {
2102 logger.VerboseError.WriteLine(ex.ToString());
2103 }
2104 }
2105 comparer.Value = value;
2106 }
2107 else
2108 {
2109 value = Convert.ToUInt64(SearchArgs.CompareStartValue);
2110 comparer.Value = value;
2111 }
2112 if (comparer.Compare(Convert.ToUInt64(Value), value))
2113 {
2114 writer.WriteResult<ulong>(comparer.Address, comparer.Value);
2115 }
2116 }
2117 address_index += STEP_SIZE;
2118 }
2119 #endregion
2120 }
2121 else
2122 {
2123 #region 64bits - signed
2124 var t_data = BitTools.ConvertByteArray<long>(data, new Func<bool>(ShouldCancelAnySearchOperation));
2125 foreach (var Value in t_data)
2126 {
2127 if (SearchWorkerThread.CancellationPending) { break; }
2128 using (_64bit_signed_comparer_ comparer = new _64bit_signed_comparer_(SearchArgs, address_index))
2129 {
2130 long value = 0;
2131 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
2132 {
2133 using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
2134 {
2135 try
2136 {
2137 gmp.OpenProvider();
2138 gmp.ReadMemory(address_index, out value);
2139 gmp.CloseProvider();
2140 }
2141 catch (Exception ex)
2142 {
2143 logger.VerboseError.WriteLine(ex.ToString());
2144 }
2145 }
2146 comparer.Value = value;
2147 }
2148 else
2149 {
2150 value = Convert.ToInt64(SearchArgs.CompareStartValue);
2151 comparer.Value = value;
2152 }
2153 if (comparer.Compare(Convert.ToInt64(Value), value))
2154 {
2155 writer.WriteResult<long>(comparer.Address, comparer.Value);
2156 }
2157 }
2158 address_index += STEP_SIZE;
2159 }
2160 #endregion
2161 } break;
2162
2163 }
2164 #endregion
2165
2166
2167 if (SearchWorkerThread.CancellationPending)
2168 {
2169 provider_st.Stop();
2170 st.Stop();
2171 writer.CancelRequest();
2172 writer.Dispose();
2173 writer = null;
2174 e.Result = true;
2175 provider.CloseProvider();
2176 return;
2177 }
2178 }
2179
2180 if (SearchWorkerThread.CancellationPending)
2181 {
2182 provider_st.Stop();
2183 st.Stop();
2184 writer.CancelRequest();
2185 writer.Dispose();
2186 writer = null;
2187 e.Result = true;
2188 provider.CloseProvider();
2189 return;
2190 }
2191 writer.Dispose();
2192 writer = null;
2193 provider.CloseProvider();
2194 }
2195 else
2196 {
2197 Guid new_SearchGuid = SearchGuid;
2198 // need to get the address list
2199 using (SearchResultReader reader = new SearchResultReader(SearchGuid, false)) // delete the file on dispose
2200 {
2201 #region old-code
2202 //provider.OpenProvider();
2203 ////int count = (int)((MemoryRangeSize - MemoryRangeStart) / STEP_SIZE);
2204 ////SearchResultWriter writer = new SearchResultWriter((int)(count), SearchGuid);
2205 //provider.OnBytesRead += new BaseEventHandler<OnBytesReadEventArgs>(action_onbytesread);
2206 //provider.ReadProcessMemoryAtOnce(MemoryRangeStart, (MemoryRangeSize - MemoryRangeStart), reader);
2207 //if (SearchWorkerThread.CancellationPending)
2208 //{
2209 // provider_st.Stop();
2210 // st.Stop();
2211 // e.Result = true;
2212 // provider.CloseProvider();
2213 // return;
2214 //}
2215 //provider.CloseProvider();
2216
2217 //byte[] guid = SearchGuid.ToByteArray();
2218 //guid[guid.Length - 1]++; // increment the search guid by 1
2219 //Guid new_SearchGuid = new Guid(guid);
2220 ////const int item_count = 0x100;
2221 //using (SearchResultWriter writer = new SearchResultWriter((int)(reader.ResultCount), new_SearchGuid))
2222 //{
2223 // var items = reader.GetSearchAddressValueMatches((IAcceptsProcessAndConfig)this, SearchArgs, new Action<int, string>(UpdateProgress));
2224 // reader.Dispose(); // delete the result file, if allowed
2225 // foreach (var item in items)
2226 // {
2227 // switch (SearchArgs.DataType)
2228 // {
2229 // case SearchDataTypes._8bits:
2230 // if (SearchArgs.IsUnsignedDataType)
2231 // {
2232 // writer.WriteResult<byte>(item.Address, Convert.ToByte(item.Value));
2233 // }
2234 // else
2235 // {
2236 // writer.WriteResult<sbyte>(item.Address, Convert.ToSByte(item.Value));
2237 // }
2238 // break;
2239 // case SearchDataTypes._16bits:
2240 // if (SearchArgs.IsUnsignedDataType)
2241 // {
2242 // writer.WriteResult<ushort>(item.Address, Convert.ToUInt16(item.Value));
2243 // }
2244 // else
2245 // {
2246 // writer.WriteResult<short>(item.Address, Convert.ToInt16(item.Value));
2247 // }
2248 // break;
2249 // case SearchDataTypes._32bits:
2250 // if (SearchArgs.IsUnsignedDataType)
2251 // {
2252 // writer.WriteResult<uint>(item.Address, Convert.ToUInt32(item.Value));
2253 // }
2254 // else
2255 // {
2256 // writer.WriteResult<int>(item.Address, Convert.ToInt32(item.Value));
2257 // }
2258 // break;
2259 // case SearchDataTypes._64bits:
2260 // if (SearchArgs.IsUnsignedDataType)
2261 // {
2262 // writer.WriteResult<ulong>(item.Address, Convert.ToUInt64(item.Value));
2263 // }
2264 // else
2265 // {
2266 // writer.WriteResult<long>(item.Address, Convert.ToInt64(item.Value));
2267 // }
2268 // break;
2269 // }
2270 // }
2271 //}
2272 //SearchGuid = new_SearchGuid;
2273 #endregion
2274
2275 new_SearchGuid = GuidTools.IncrementGuid(SearchGuid);
2276 using (SearchResultWriter writer = new SearchResultWriter(0, new_SearchGuid))
2277 {
2278 int index = 0;
2279 foreach (var k in reader.GetResults(SearchArgs.IsUnsignedDataType, SearchArgs.DataType, new Action<int, string>(UpdateProgress), new Func<bool>(ShouldCancelAnySearchOperation)))
2280 {
2281 if (SearchWorkerThread.CancellationPending)
2282 {
2283 provider_st.Stop();
2284 st.Stop();
2285 writer.CancelRequest();
2286 writer.Dispose();
2287 e.Result = true;
2288 return;
2289 }
2290 int Last_Whole_Percent_Done = 0;
2291 provider.OpenProvider();
2292 int bytesRead = 0;
2293 byte[] data = new byte[STEP_SIZE];
2294 provider.ReadProcessMemory(k.Address, STEP_SIZE, out bytesRead, out data);
2295 provider.CloseProvider();
2296 if (SearchWorkerThread.CancellationPending)
2297 {
2298 provider_st.Stop();
2299 st.Stop();
2300 writer.CancelRequest();
2301 writer.Dispose();
2302 e.Result = true;
2303 return;
2304 }
2305 uint address_index = k.Address;
2306
2307 string message = string.Format(" -> Reading Address 0x{0:x8} from index 0x{1}", k.Address, index.ToString("X"));
2308 double double_percent_done = 100.0 * (double)((double)index / (double)reader.ResultCount);
2309 int int_percent_done = (int)double_percent_done;
2310 if ((int_percent_done != Last_Whole_Percent_Done) && (index % 1000) == 0)
2311 {
2312 UpdateProgress((int)double_percent_done, message);
2313 Last_Whole_Percent_Done = int_percent_done;
2314 }
2315
2316 #region comparison and serialization
2317 switch (sdt)
2318 {
2319 case SearchDataTypes._8bits:
2320 if (unsigned)
2321 {
2322 #region 8bits - unsigned
2323 var t_data = data;
2324 foreach (var Value in t_data)
2325 {
2326 if (SearchWorkerThread.CancellationPending) { break; }
2327 using (_8bit_unsigned_comparer_ comparer = new _8bit_unsigned_comparer_(SearchArgs, address_index))
2328 {
2329 byte value = 0;
2330 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
2331 {
2332 using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
2333 {
2334 try
2335 {
2336 gmp.OpenProvider();
2337 gmp.ReadMemory(address_index, out value);
2338 gmp.CloseProvider();
2339 }
2340 catch (Exception ex)
2341 {
2342 logger.VerboseError.WriteLine(ex.ToString());
2343 }
2344 }
2345 comparer.Value = value;
2346 }
2347 else
2348 {
2349 value = Convert.ToByte(SearchArgs.CompareStartValue);
2350 comparer.Value = value;
2351 }
2352 if (comparer.Compare(Convert.ToByte(Value), value))
2353 {
2354 writer.WriteResult<byte>(comparer.Address, comparer.Value);
2355 }
2356 }
2357 address_index += STEP_SIZE;
2358 }
2359 #endregion
2360 }
2361 else
2362 {
2363 #region 8bits - signed
2364 var t_data = data;
2365 foreach (var Value in t_data)
2366 {
2367 if (SearchWorkerThread.CancellationPending) { break; }
2368 using (_8bit_signed_comparer_ comparer = new _8bit_signed_comparer_(SearchArgs, address_index))
2369 {
2370 sbyte value = 0;
2371 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
2372 {
2373 using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
2374 {
2375 try
2376 {
2377 gmp.OpenProvider();
2378 gmp.ReadMemory(address_index, out value);
2379 gmp.CloseProvider();
2380 }
2381 catch (Exception ex)
2382 {
2383 logger.VerboseError.WriteLine(ex.ToString());
2384 }
2385 }
2386 comparer.Value = value;
2387 }
2388 else
2389 {
2390 value = Convert.ToSByte(SearchArgs.CompareStartValue);
2391 comparer.Value = value;
2392 }
2393 if (comparer.Compare(Convert.ToSByte(Value), value))
2394 {
2395 writer.WriteResult<sbyte>(comparer.Address, comparer.Value);
2396 }
2397 }
2398 address_index += STEP_SIZE;
2399 }
2400 #endregion
2401 } break;
2402 case SearchDataTypes._16bits:
2403 if (unsigned)
2404 {
2405 #region 16bits - unsigned
2406 var t_data = BitTools.ConvertByteArray<ushort>(data, new Func<bool>(ShouldCancelAnySearchOperation));
2407 foreach (var Value in t_data)
2408 {
2409 if (SearchWorkerThread.CancellationPending) { break; }
2410 using (_16bit_unsigned_comparer_ comparer = new _16bit_unsigned_comparer_(SearchArgs, address_index))
2411 {
2412 ushort value = 0;
2413 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
2414 {
2415 using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
2416 {
2417 try
2418 {
2419 gmp.OpenProvider();
2420 gmp.ReadMemory(address_index, out value);
2421 gmp.CloseProvider();
2422 }
2423 catch (Exception ex)
2424 {
2425 logger.VerboseError.WriteLine(ex.ToString());
2426 }
2427 }
2428 comparer.Value = value;
2429 }
2430 else
2431 {
2432 value = Convert.ToUInt16(SearchArgs.CompareStartValue);
2433 comparer.Value = value;
2434 }
2435 if (comparer.Compare(Convert.ToUInt16(Value), value))
2436 {
2437 writer.WriteResult<ushort>(comparer.Address, comparer.Value);
2438 }
2439 }
2440 address_index += STEP_SIZE;
2441 }
2442 #endregion
2443 }
2444 else
2445 {
2446 #region 16bits - signed
2447 var t_data = BitTools.ConvertByteArray<short>(data, new Func<bool>(ShouldCancelAnySearchOperation));
2448 foreach (var Value in t_data)
2449 {
2450 if (SearchWorkerThread.CancellationPending) { break; }
2451 using (_16bit_signed_comparer_ comparer = new _16bit_signed_comparer_(SearchArgs, address_index))
2452 {
2453 short value = 0;
2454 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
2455 {
2456 using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
2457 {
2458 try
2459 {
2460 gmp.OpenProvider();
2461 gmp.ReadMemory(address_index, out value);
2462 gmp.CloseProvider();
2463 }
2464 catch (Exception ex)
2465 {
2466 logger.VerboseError.WriteLine(ex.ToString());
2467 }
2468 }
2469 comparer.Value = value;
2470 }
2471 else
2472 {
2473 value = Convert.ToInt16(SearchArgs.CompareStartValue);
2474 comparer.Value = value;
2475 }
2476 if (comparer.Compare(Convert.ToSByte(Value), value))
2477 {
2478 writer.WriteResult<short>(comparer.Address, comparer.Value);
2479 }
2480 }
2481 address_index += STEP_SIZE;
2482 }
2483 #endregion
2484 } break;
2485 case SearchDataTypes._32bits:
2486 if (unsigned)
2487 {
2488 #region 32bits - unsigned
2489 var t_data = BitTools.ConvertByteArray<uint>(data, new Func<bool>(ShouldCancelAnySearchOperation));
2490 foreach (var Value in t_data)
2491 {
2492 if (SearchWorkerThread.CancellationPending) { break; }
2493 using (_32bit_unsigned_comparer_ comparer = new _32bit_unsigned_comparer_(SearchArgs, address_index))
2494 {
2495 uint value = 0;
2496 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
2497 {
2498 using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
2499 {
2500 try
2501 {
2502 gmp.OpenProvider();
2503 gmp.ReadMemory(address_index, out value);
2504 gmp.CloseProvider();
2505 }
2506 catch (Exception ex)
2507 {
2508 logger.VerboseError.WriteLine(ex.ToString());
2509 }
2510 }
2511 comparer.Value = value;
2512 }
2513 else
2514 {
2515 value = Convert.ToUInt32(SearchArgs.CompareStartValue);
2516 comparer.Value = value;
2517 }
2518 if (comparer.Compare(Convert.ToUInt32(Value), value))
2519 {
2520 writer.WriteResult<uint>(comparer.Address, comparer.Value);
2521 }
2522 }
2523 address_index += STEP_SIZE;
2524 }
2525 #endregion
2526 }
2527 else
2528 {
2529 #region 32bits - signed
2530 var t_data = BitTools.ConvertByteArray<int>(data, new Func<bool>(ShouldCancelAnySearchOperation));
2531 foreach (var Value in t_data)
2532 {
2533 if (SearchWorkerThread.CancellationPending) { break; }
2534 using (_32bit_signed_comparer_ comparer = new _32bit_signed_comparer_(SearchArgs, address_index))
2535 {
2536 int value = 0;
2537 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
2538 {
2539 using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
2540 {
2541 try
2542 {
2543 gmp.OpenProvider();
2544 gmp.ReadMemory(address_index, out value);
2545 gmp.CloseProvider();
2546 }
2547 catch (Exception ex)
2548 {
2549 logger.VerboseError.WriteLine(ex.ToString());
2550 }
2551 }
2552 comparer.Value = value;
2553 }
2554 else
2555 {
2556 value = Convert.ToInt32(SearchArgs.CompareStartValue);
2557 comparer.Value = value;
2558 }
2559 if (comparer.Compare(Convert.ToInt32(Value), value))
2560 {
2561 writer.WriteResult<int>(comparer.Address, comparer.Value);
2562 }
2563 }
2564 address_index += STEP_SIZE;
2565 }
2566 #endregion
2567 } break;
2568 case SearchDataTypes._64bits:
2569 if (unsigned)
2570 {
2571 #region 64bits - unsigned
2572 var t_data = BitTools.ConvertByteArray<ulong>(data, new Func<bool>(ShouldCancelAnySearchOperation));
2573 foreach (var Value in t_data)
2574 {
2575 if (SearchWorkerThread.CancellationPending) { break; }
2576 using (_64bit_unsigned_comparer_ comparer = new _64bit_unsigned_comparer_(SearchArgs, address_index))
2577 {
2578 ulong value = 0;
2579 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
2580 {
2581 using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
2582 {
2583 try
2584 {
2585 gmp.OpenProvider();
2586 gmp.ReadMemory(address_index, out value);
2587 gmp.CloseProvider();
2588 }
2589 catch (Exception ex)
2590 {
2591 logger.VerboseError.WriteLine(ex.ToString());
2592 }
2593 }
2594 comparer.Value = value;
2595 }
2596 else
2597 {
2598 value = Convert.ToUInt64(SearchArgs.CompareStartValue);
2599 comparer.Value = value;
2600 }
2601 if (comparer.Compare(Convert.ToUInt64(Value), value))
2602 {
2603 writer.WriteResult<ulong>(comparer.Address, comparer.Value);
2604 }
2605 }
2606 address_index += STEP_SIZE;
2607 }
2608 #endregion
2609 }
2610 else
2611 {
2612 #region 64bits - signed
2613 var t_data = BitTools.ConvertByteArray<long>(data, new Func<bool>(ShouldCancelAnySearchOperation));
2614 foreach (var Value in t_data)
2615 {
2616 if (SearchWorkerThread.CancellationPending) { break; }
2617 using (_64bit_signed_comparer_ comparer = new _64bit_signed_comparer_(SearchArgs, address_index))
2618 {
2619 long value = 0;
2620 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
2621 {
2622 using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
2623 {
2624 try
2625 {
2626 gmp.OpenProvider();
2627 gmp.ReadMemory(address_index, out value);
2628 gmp.CloseProvider();
2629 }
2630 catch (Exception ex)
2631 {
2632 logger.VerboseError.WriteLine(ex.ToString());
2633 }
2634 }
2635 comparer.Value = value;
2636 }
2637 else
2638 {
2639 value = Convert.ToInt64(SearchArgs.CompareStartValue);
2640 comparer.Value = value;
2641 }
2642 if (comparer.Compare(Convert.ToInt64(Value), value))
2643 {
2644 writer.WriteResult<long>(comparer.Address, comparer.Value);
2645 }
2646 }
2647 address_index += STEP_SIZE;
2648 }
2649 #endregion
2650 } break;
2651
2652 }
2653 #endregion
2654
2655 index++;
2656 }
2657
2658
2659 //ISerializedResult sr = (reader as ISerializedResult);
2660 //if (sr == null)
2661 //{
2662 // throw new ArgumentNullException("sr", string.Format("Unable to cast: '{0}' to '{1}'", reader.GetType().Name, typeof(ISerializedResult).Name));
2663 //}
2664 //int sr_index = 0;
2665 //try
2666 //{
2667 // if (sr.ContainsAddress(obre_CurrentIndex, obre_unsigned, obre_sdt, new Action<int, string>(this.UpdateProgress), out sr_index))
2668 // {
2669 // StructResultType<ulong> result = StructResultType<ulong>.Empty;
2670 // sr.GetResultAtIndex(sr_index, obre_unsigned, obre_sdt, new Action<int, string>(this.UpdateProgress), out result);
2671 // if (Debugger.IsAttached)
2672 // {
2673 // Debugger.Break();
2674 // }
2675 // }
2676 //}
2677
2678 }
2679
2680 }
2681 SearchGuid = new_SearchGuid; // update with out new guid
2682 }
2683 }
2684 provider_st.Stop();
2685 logger.Profiler.WriteLine("It took a total of {0} seconds for the memory provider to complete it's operation(s).", provider_st.Elapsed.TotalSeconds);
2686 //if (buffered_mem.Length == 0) { logger.Warn.WriteLine("Buffered Memory is Zero Length."); return; }
2687 //int Last_Whole_Percent_Done = 0;
2688
2689
2690 #region Subsequent Searches
2691 //r_ms.BaseStream.Seek(0, SeekOrigin.Begin);
2692
2693
2694 // hack to help with OutOfMemory Exceptions (OldValue and Equal compare will always add all found search results)
2695 bool NeedToCompare = true;
2696 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue &&
2697 SearchArgs.CompareType == SearchCompareTypes.Equal &&
2698 SearchArgs.IsFirstSearch)
2699 {
2700 NeedToCompare = false;
2701 //second_tmp_Results = null; // Free Memory
2702 }
2703
2704 if (NeedToCompare)
2705 {
2706 #region Ranged Searches
2707 #if !DONOT_HAVE_RANGED_SEARCH_SUPPORT
2708 if (SearchArgs.CompareType == SearchCompareTypes.Between || SearchArgs.CompareType == SearchCompareTypes.NotBetween)
2709 {
2710 st_ranged_search.Start();
2711 object start, end;
2712
2713 start = SearchArgs.CompareStartValue;
2714 end = SearchArgs.CompareEndValue;
2715 using (SearchResultReader reader = new SearchResultReader(SearchGuid))
2716 {
2717 for (int i = 0; i < reader.ResultCount; i += 1)
2718 {
2719 uint address = 0;
2720 #region switch (SearchArgs.DataType)
2721 switch (SearchArgs.DataType)
2722 {
2723 case SearchDataTypes._8bits: if (unsigned) { using (ResultType<byte> result = reader.GetNextResult<byte>()) { address = result.Address; } }
2724 else { using (ResultType<sbyte> result = reader.GetNextResult<sbyte>()) { address = result.Address; } } break;
2725 case SearchDataTypes._16bits: if (unsigned) { using (ResultType<ushort> result = reader.GetNextResult<ushort>()) { address = result.Address; } }
2726 else { using (ResultType<short> result = reader.GetNextResult<short>()) { address = result.Address; } } break;
2727 case SearchDataTypes._32bits: if (unsigned) { using (ResultType<uint> result = reader.GetNextResult<uint>()) { address = result.Address; } }
2728 else { using (ResultType<int> result = reader.GetNextResult<int>()) { address = result.Address; } } break;
2729 case SearchDataTypes._64bits: if (unsigned) { using (ResultType<ulong> result = reader.GetNextResult<ulong>()) { address = result.Address; } }
2730 else { using (ResultType<long> result = reader.GetNextResult<long>()) { address = result.Address; } } break;
2731 }
2732 #endregion
2733
2734 if (MemoryRangeStart > 0 && !SearchArgs.IsFirstSearch) { address = address - MemoryRangeStart; }
2735 //r_ms.BaseStream.Seek(address, SeekOrigin.Begin);
2736 if (SearchArgs.CompareType == SearchCompareTypes.Between)
2737 {
2738 using (InRangeComparer comparer = new InRangeComparer(address, 0))
2739 {
2740 if (comparer.Compare(start, end, SearchArgs.DataType, SearchArgs.IsUnsignedDataType, (IAcceptsProcessAndConfig)this))
2741 {
2742 using (ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value))
2743 {
2744 second_tmp_Results.Add(_tmp_result);
2745 }
2746 }
2747 }
2748 }
2749 else if (SearchArgs.CompareType == SearchCompareTypes.NotBetween)
2750 {
2751 using (NotInRangeComparer comparer = new NotInRangeComparer(address, 0))
2752 {
2753 if (comparer.Compare(start, end, SearchArgs.DataType, SearchArgs.IsUnsignedDataType, (IAcceptsProcessAndConfig)this))
2754 {
2755 using (ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value))
2756 {
2757 second_tmp_Results.Add(_tmp_result);
2758 }
2759 }
2760 }
2761 }
2762 else
2763 {
2764 throw new InvalidOperationException("Encounted unkown range search type: " + SearchArgs.CompareType);
2765 }
2766 double double_percent_done = 100.0 * (double)((double)i / (double)reader.ResultCount);
2767 int int_percent_done = (int)double_percent_done;
2768 if (int_percent_done != Last_Whole_Percent_Done)
2769 {
2770 if (int_percent_done <= 100)
2771 {
2772 resultsprogress.Value = int_percent_done;
2773 resultsprogress.Message = string.Format(" -> Reading Address: 0x{0:x8}", i + MemoryRangeStart);
2774 Last_Whole_Percent_Done = int_percent_done;
2775 }
2776 }
2777 }
2778 }
2779 st_ranged_search.Stop();
2780 logger.Profiler.WriteLine("Ranged search took a total of {0} seconds to complete.", st_ranged_search.Elapsed.TotalSeconds);
2781 }
2782 #endif
2783 #endregion
2784 }
2785 #endregion
2786 //// leave SearchArgs.Results alone, if false
2787 //if (NeedToCompare)
2788 //{
2789 // // fix addresses when memory start is not zero
2790 // if (MemoryRangeStart > 0 && SearchArgs.IsFirstSearch) { for (int i = 0; i < second_tmp_Results.Count; i++) { second_tmp_Results[i].Address = second_tmp_Results[i].Address + MemoryRangeStart; } }
2791 // using (SearchResultWriter writer = new SearchResultWriter(second_tmp_Results.Count, SearchGuid))
2792 // {
2793 // for (int i = 0; i < second_tmp_Results.Count; i++)
2794 // {
2795 // switch (sdt)
2796 // {
2797 // case SearchDataTypes._8bits:
2798 // if (unsigned) { writer.WriteResult<Byte>(second_tmp_Results[i].Address, Convert.ToByte(second_tmp_Results[i].Value)); }
2799 // else { writer.WriteResult<SByte>(second_tmp_Results[i].Address, Convert.ToSByte(second_tmp_Results[i].Value)); } break;
2800 // case SearchDataTypes._16bits:
2801 // if (unsigned) { writer.WriteResult<UInt16>(second_tmp_Results[i].Address, Convert.ToUInt16(second_tmp_Results[i].Value)); }
2802 // else { writer.WriteResult<Int16>(second_tmp_Results[i].Address, Convert.ToInt16(second_tmp_Results[i].Value)); } break;
2803 // case SearchDataTypes._32bits:
2804 // if (unsigned) { writer.WriteResult<UInt32>(second_tmp_Results[i].Address, Convert.ToUInt32(second_tmp_Results[i].Value)); }
2805 // else { writer.WriteResult<Int32>(second_tmp_Results[i].Address, Convert.ToInt32(second_tmp_Results[i].Value)); } break;
2806 // case SearchDataTypes._64bits:
2807 // if (unsigned) { writer.WriteResult<UInt64>(second_tmp_Results[i].Address, Convert.ToUInt64(second_tmp_Results[i].Value)); }
2808 // else { writer.WriteResult<Int64>(second_tmp_Results[i].Address, Convert.ToInt64(second_tmp_Results[i].Value)); } break;
2809 // }
2810 // }
2811 // }
2812 // second_tmp_Results = null; // free memory
2813 //}
2814 }
2815 catch (OutOfMemoryException ex)
2816 {
2817 e.Result = true;
2818 logger.Error.WriteLine("Encounted {0} for search: {0}", ex.GetType().Name, SearchGuid.ToString());
2819 logger.VerboseError.WriteLine(ex.ToString());
2820 MessageBox.Show(string.Format("Encounted {0} for search: {0} ... Please try again.", ex.GetType().Name, SearchGuid.ToString()), string.Format("{0} was thrown", ex.GetType().Name), MessageBoxButtons.OK, MessageBoxIcon.Error);
2821 return;
2822 }
2823 catch (Exception ex)
2824 {
2825 throw ex;
2826 }
2827 }
2828
2829
2830
2831 private void SearchWorkerThread_ProgressChanged(object sender, ProgressChangedEventArgs e)
2832 {
2833 //if (SearchArgs.ProgressLogger != null)
2834 //{
2835 // resultsprogress.Value = e.ProgressPercentage;
2836 // //Application.DoEvents();
2837 //}
2838 }
2839
2840 private void SearchWorkerThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
2841 {
2842 try { if ((bool)e.Result) { logger.Warn.WriteLine("Search operation was cancelled.");
2843 //new Action<int, string>(UpdateProgress).Invoke(0, string.Empty);
2844 UpdateProgress(0, string.Empty);
2845 return; }
2846 }
2847 catch (InvalidCastException) { }
2848 try
2849 {
2850 Stopwatch st = (Stopwatch)e.Result;
2851 st.Stop();
2852 logger.Profiler.WriteLine("Search took {0} seconds, overall, to complete.", st.Elapsed.TotalSeconds);
2853 }
2854 catch (InvalidCastException) { }
2855 catch (Exception ex) { throw ex; }
2856
2857 UpdateProgress(100, String.Empty);
2858 //new Action<int, string>(UpdateProgress).Invoke(100, string.Empty);
2859 bool unsigned = SearchArgs.IsUnsignedDataType;
2860 using (SearchResultReader reader = new SearchResultReader(SearchGuid))
2861 {
2862 logger.Info.WriteLine(string.Format("Found 0x{0:x8} results", reader.ResultCount));
2863
2864 if (reader.ResultCount <= MIN_NUMBER_OF_RESULTS_BEFORE_DISPLAY)
2865 {
2866 lstResults.Items.Clear();
2867 List<ResultItem> items = new List<ResultItem>();
2868 foreach (var k in reader.GetResults(SearchArgs.IsUnsignedDataType, SearchArgs.DataType, new Action<int, string>(UpdateProgress), new Func<bool>(ShouldCancelAnySearchOperation)))
2869 {
2870 switch (SearchArgs.DataType)
2871 {
2872 case SearchDataTypes._8bits:
2873 if (SearchArgs.IsUnsignedDataType) { items.Add(new ResultItem(k.Address, false, Convert.ToByte(k.Value))); }
2874 else { items.Add(new ResultItem(k.Address, false, Convert.ToSByte(k.Value))); }
2875 break;
2876 case SearchDataTypes._16bits:
2877 if (SearchArgs.IsUnsignedDataType) { items.Add(new ResultItem(k.Address, false, Convert.ToUInt16(k.Value))); }
2878 else { items.Add(new ResultItem(k.Address, false, Convert.ToInt16(k.Value))); }
2879 break;
2880 case SearchDataTypes._32bits:
2881 if (SearchArgs.IsUnsignedDataType) { items.Add(new ResultItem(k.Address, false, Convert.ToUInt32(k.Value))); }
2882 else { items.Add(new ResultItem(k.Address, false, Convert.ToInt32(k.Value))); }
2883 break;
2884 case SearchDataTypes._64bits:
2885 if (SearchArgs.IsUnsignedDataType) { items.Add(new ResultItem(k.Address, false, Convert.ToUInt64(k.Value))); }
2886 else { items.Add(new ResultItem(k.Address, false, Convert.ToInt64(k.Value))); }
2887 break;
2888 }
2889 }
2890 lstResults.Items.AddRange(items.ToArray());
2891 }
2892 }
2893
2894 this.DoSearchDoneSpecific();
2895 //System.Threading.Thread.Sleep(100);
2896 //if (_SEARCH_MODE != SearchMode.SEARCH_MODE_NORMAL_WITH_JOKER)
2897 this.ThawResultsUpdate();
2898 Application.DoEvents();
2899 }
2900 private void DoSearchDoneSpecific()
2901 {
2902 SearchWorkerThread.CancelAsync();
2903 if (lstResults.Items.Count > 0) { timer_update_results.Enabled = true; }
2904 else { timer_update_results.Enabled = false; }
2905
2906 search_progress_updater.Enabled = false;
2907
2908 btnCancel.Enabled = false;
2909 btnReset.Enabled = true;
2910 btnSearch.Enabled = true;
2911 grpCompareType.Enabled = true;
2912 grpCompareValue.Enabled = true;
2913 UpdateProgress(0, string.Empty);
2914 //new Action<int, string>(UpdateProgress).Invoke(0, string.Empty);
2915 grpDataType.Enabled = false;
2916 // resume process on reset, incase it was suspended
2917 ThreadControl.ResumeProcess(this.AcceptedProcess.Id);
2918 //Application.DoEvents();
2919 this.Refresh();
2920 }
2921
2922 private void DoCancelSpecific()
2923 {
2924 this.DoSearchDoneSpecific();
2925 }
2926 private void DoResetSpecific()
2927 {
2928 this.DoCancelSpecific();
2929 IsFirstSearch = true;
2930 grpDataType.Enabled = true;
2931 btnReset.Enabled = false;
2932
2933 // delete any temporary search result files
2934 SearchResultWriter.CleanupTemporarySearchResultFiles();
2935
2936 }
2937 private void search_progress_updater_Tick(object sender, EventArgs e)
2938 {
2939 if ((this.AcceptedProcess ==null) || Process.GetProcessById(this.AcceptedProcess.Id) == null)
2940 {
2941 SearchWorkerThread.CancelAsync();
2942 //JokerSearchWorker.CancelAsync();
2943 ResultsUpdateWorkerThread.CancelAsync();
2944 }
2945 }
2946
2947 #region Search Button
2948 private void btnSearch_Click(object sender, EventArgs e)
2949 {
2950 if (this.SearchGuid == Guid.Empty)
2951 this.SearchGuid = Guid.NewGuid();
2952 chkMemoryRangeExpertMode.Enabled = false;
2953 this.SearchInProgess = true;
2954 btnCancel.Enabled = true;
2955 btnReset.Enabled = false; // disable during search
2956 btnSearch.Enabled = false;
2957 this.FreezeResultsUpdate();
2958 this.handle_btnSearch_Click();
2959 }
2960 private void handle_btnSearch_Click()
2961 {
2962 //this.FreezeResultsUpdate();
2963 lstResults.Items.Clear();
2964
2965 if (lstResults.Items.Count > 0) { timer_update_results.Enabled = true; }
2966 else { timer_update_results.Enabled = false; }
2967 UpdateProgress(0, string.Empty);
2968 //new Action<int, string>(UpdateProgress).Invoke(0, string.Empty);
2969 bool _is_unsigned = chkUnsigned.Checked;
2970 SearchType search_type = new SearchType();
2971 SearchDataTypes _data_type = new SearchDataTypes();
2972 SearchCompareTypes _compare_type = new SearchCompareTypes();
2973 CompareValueTypes _compare_value_type = new CompareValueTypes();
2974 object start_value = 0;
2975 object end_value = 0;
2976 // get datatype
2977 if (radio_8bits.Checked) { _data_type = SearchDataTypes._8bits; }
2978 else if (radio_16bits.Checked) { _data_type = SearchDataTypes._16bits; }
2979 else if (radio_32bits.Checked) { _data_type = SearchDataTypes._32bits; }
2980 else if (radio_64bits.Checked) { _data_type = SearchDataTypes._64bits; }
2981 else { logger.Error.WriteLine("Could not determine search data type bit size. (was not 8/16/32/or 64bits)"); }
2982 // get compare type
2983 if (radiocompare_equal.Checked) { _compare_type = SearchCompareTypes.Equal; }
2984 else if (radiocompare_greaterthan.Checked) { _compare_type = SearchCompareTypes.GreaterThan; }
2985 else if (radiocompare_lessthan.Checked) { _compare_type = SearchCompareTypes.LessThan; }
2986 else if (radiocompare_greaterthan_orequal.Checked) { _compare_type = SearchCompareTypes.GreaterThanOrEqual; }
2987 else if (radiocompare_lessthan_orequal.Checked) { _compare_type = SearchCompareTypes.LessThanOrEqual; }
2988 else if (radiocompare_notequal.Checked) { _compare_type = SearchCompareTypes.NotEqual; }
2989 else if (radiocompare_between.Checked) { _compare_type = SearchCompareTypes.Between; }
2990 else if (radiocompare_notbetween.Checked) { _compare_type = SearchCompareTypes.NotBetween; }
2991 else { logger.Error.WriteLine("Could not determine search comparison type. (was not == > < >= <= != <> or !<>)"); }
2992 // get compare valure type
2993 if (radio_oldvalue.Checked) { _compare_value_type = CompareValueTypes.OldValue; }
2994 else if (radio_specificvalue.Checked) { _compare_value_type = CompareValueTypes.SpecificValue; }
2995 else { logger.Error.WriteLine("Could not determine search comparison type. (was not old or specific value"); }
2996
2997 if (_compare_value_type == CompareValueTypes.SpecificValue || (_compare_type == SearchCompareTypes.Between || _compare_type == SearchCompareTypes.NotBetween))
2998 {
2999
3000 switch (_data_type)
3001 {
3002 case SearchDataTypes._8bits:
3003 if (_is_unsigned) { start_value = txtStartAddr.ToByte(); }
3004 else { start_value = txtStartAddr.ToSByte(); }
3005 break;
3006 case SearchDataTypes._16bits:
3007 if (_is_unsigned) { start_value = txtStartAddr.ToUInt16(); }
3008 else { start_value = txtStartAddr.ToInt16(); }
3009 break;
3010 case SearchDataTypes._32bits:
3011 if (_is_unsigned) { start_value = txtStartAddr.ToUInt32(); }
3012 else { start_value = txtStartAddr.ToInt32(); }
3013 break;
3014 case SearchDataTypes._64bits:
3015 if (_is_unsigned) { start_value = txtStartAddr.ToUInt64(); }
3016 else { start_value = txtStartAddr.ToInt64(); }
3017 break;
3018 default: throw new InvalidOperationException("In SearchType(): Encounterd an Unkown Search Data Type.");
3019 }
3020 }
3021 if (_compare_type == SearchCompareTypes.Between || _compare_type == SearchCompareTypes.NotBetween)
3022 {
3023 switch (_data_type)
3024 {
3025 case SearchDataTypes._8bits:
3026 if (_is_unsigned) { end_value = txtEndAddr.ToByte(); }
3027 else { end_value = txtEndAddr.ToSByte(); }
3028 break;
3029 case SearchDataTypes._16bits:
3030 if (_is_unsigned) { end_value = txtEndAddr.ToUInt16(); }
3031 else { end_value = txtEndAddr.ToInt16(); }
3032 break;
3033 case SearchDataTypes._32bits:
3034 if (_is_unsigned) { end_value = txtEndAddr.ToUInt32(); }
3035 else { end_value = txtEndAddr.ToInt32(); }
3036 break;
3037 case SearchDataTypes._64bits:
3038 if (_is_unsigned) { end_value = txtEndAddr.ToUInt64(); }
3039 else { end_value = txtEndAddr.ToInt64(); }
3040 break;
3041 default: throw new InvalidOperationException("In SearchType(): Encounterd an Unkown Search Data Type.");
3042 }
3043 }
3044
3045 search_type = new SearchType(_data_type, _is_unsigned, _compare_type, _compare_value_type, start_value, end_value, resultsprogress);
3046
3047 //search_type.LogSearchOptions();
3048
3049 search_type.IsFirstSearch = IsFirstSearch;
3050
3051
3052
3053 DoSearch(search_type);
3054 IsFirstSearch = false;
3055 }
3056 private void DoSearch(SearchType args)
3057 {
3058 if (!args.IsFirstSearch && SearchArgs != null)
3059 {
3060 //args.Results.AddRange(SearchArgs.Results.ToArray());
3061 //args.Results = SearchArgs.Results;
3062 }
3063 SearchArgs = args;
3064 #if DONOT_HAVE_RANGED_SEARCH_SUPPORT
3065 if (SearchArgs.CompareType == SearchCompareTypes.Between || SearchArgs.CompareType == SearchCompareTypes.NotBetween)
3066 {
3067 throw new NotImplementedException("Between and Not Between Range searches have not been implemented.");
3068 }
3069 #endif
3070 search_progress_updater.Enabled = true;
3071 //padPluginSelector.Enabled = false;
3072 //gsPluginSelector.Enabled = false;
3073 btnReset.Enabled = false; // this will be disabled until the earch is finished
3074 btnSearch.Enabled = false;
3075 btnCancel.Enabled = true;
3076 grpDataType.Enabled = false;
3077 grpCompareType.Enabled = false;
3078 grpCompareValue.Enabled = false;
3079 this.Refresh();
3080 Application.DoEvents();
3081 SearchWorkerThread.RunWorkerAsync();
3082 }
3083 #endregion
3084 private void btnReset_Click(object sender, EventArgs e)
3085 {
3086 this.SearchGuid = Guid.Empty;
3087 this.SearchInProgess = false;
3088 chkMemoryRangeExpertMode.Enabled = true;
3089 btnSearch.Enabled = true;
3090 btnReset.Enabled = false;
3091 btnCancel.Enabled = false;
3092 this.DoResetSpecific();
3093 lstResults.Items.Clear();
3094 //try { SearchArgs.Results = new List<ResultType<object>>(); }
3095 //catch { }
3096 }
3097
3098 private void btnCancel_Click(object sender, EventArgs e)
3099 {
3100 this.SearchInProgess = false;
3101 btnCancel.Enabled = false;
3102 btnSearch.Enabled = true;
3103 btnReset.Enabled = true;
3104 this.DoCancelSpecific();
3105 }
3106
3107 private void mnuItemPatchListViewMemoryRegion_Click(object sender, EventArgs e)
3108 {
3109 List<ResultDataType> patch_list = new List<ResultDataType>();
3110 List<int> SelectedIndexes = new List<int>();
3111 foreach (int index in lstPatchList.SelectedIndices) { SelectedIndexes.Add(index); }
3112 foreach (int index in SelectedIndexes)
3113 {
3114 ListViewItem item = lstPatchList.Items[index];
3115 ResultDataType rdt = (ResultDataType)item.Tag;
3116 ViewMemoryRegion(rdt);
3117 break; // only get the fist item
3118 }
3119 }
3120
3121 private void mnuItemResultsListViewMemoryRegion_Click(object sender, EventArgs e)
3122 {
3123 List<ResultDataType> patch_list = new List<ResultDataType>();
3124 List<int> SelectedIndexes = new List<int>();
3125 foreach (int index in lstResults.SelectedIndices) { SelectedIndexes.Add(index); }
3126 foreach (int index in SelectedIndexes)
3127 {
3128 ListViewItem item = lstResults.Items[index];
3129 ResultDataType rdt = (ResultDataType)item.Tag;
3130 ViewMemoryRegion(rdt);
3131 break; // only get the fist item
3132 }
3133 }
3134 private void ViewMemoryRegion(ResultDataType rdt)
3135 {
3136 if (OnBrowseMemoryRegion != null)
3137 OnBrowseMemoryRegion(new BrowseMemoryRegionEvent(this, rdt.Address));
3138 }
3139
3140 private void mnuAddedResults_Opening(object sender, CancelEventArgs e)
3141 {
3142 if (!(lstPatchList.Items.Count > 0)) { mnuItemRemoveResult.Visible = false; e.Cancel = true; }
3143 if (!(lstPatchList.Items.Count > 0)) { mnuItemPatchSelectedEntry.Visible = false; e.Cancel = true; }
3144 if (e.Cancel) return;
3145 if (lstPatchList.Items.Count > 0) mnuItemRemoveResult.Visible = true;
3146 if (lstPatchList.Items.Count > 0) mnuItemPatchSelectedEntry.Visible = true;
3147
3148 if (!(lstPatchList.Items.Count > 0)) { mnuItemFreezeSelectedPatches.Visible = false; e.Cancel = true; }
3149 if (!(lstPatchList.Items.Count > 0)) { mnuItemThawSelectedPatches.Visible = false; e.Cancel = true; }
3150 if (e.Cancel) return;
3151
3152 if (lstPatchList.Items.Count > 0) mnuItemFreezeSelectedPatches.Visible = true;
3153 if (lstPatchList.Items.Count > 0) mnuItemThawSelectedPatches.Visible = true;
3154
3155 if (lstPatchList.SelectedItems.Count == 0) e.Cancel = true;
3156 if (e.Cancel) return;
3157
3158 }
3159
3160 private void mnuResults_Opening(object sender, CancelEventArgs e)
3161 {
3162 if (!(lstResults.Items.Count > 0)) e.Cancel = true;
3163 if (lstResults.SelectedItems.Count == 0) e.Cancel = true;
3164 if (SearchArgs == null) e.Cancel = true;
3165 if (e.Cancel) return;
3166 }
3167
3168 private void chkMemoryRangeExpertMode_CheckedChanged(object sender, EventArgs e)
3169 {
3170 txtMemoryRangeStart.ReadOnly = !chkMemoryRangeExpertMode.Checked;
3171 txtMemoryRangeSize.ReadOnly = !chkMemoryRangeExpertMode.Checked;
3172 }
3173
3174 private void txtMemoryRangeStart_ValueChanged(object sender, ValueChangedEventArgs e) { this.MemoryRangeStart = Convert.ToUInt32(e.NewValue); }
3175 private void txtMemoryRangeSize_ValueChanged(object sender, ValueChangedEventArgs e) { this.MemoryRangeSize = Convert.ToUInt32(e.NewValue); }
3176<