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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 545 - (show annotations) (download)
Wed Jun 5 22:08:38 2013 UTC (7 years, 4 months ago) by william
File size: 174834 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 private void SearchWorkerThread_DoWork(object sender, DoWorkEventArgs e)
1073 {
1074 try
1075 {
1076 Stopwatch st = new Stopwatch();
1077 st.Start();
1078
1079 Stopwatch st_first_search = new Stopwatch();
1080 Stopwatch st_nonrange_search = new Stopwatch();
1081 Stopwatch st_ranged_search = new Stopwatch();
1082
1083 e.Result = st;
1084 //List<ResultType<object>> tmp_Results = new List<ResultType<object>>();
1085 List<ResultType<ulong>> second_tmp_Results = new List<ResultType<ulong>>();
1086 //const int ElementsBeforeDisplay = 100;
1087 SearchArgs.LogSearchOptions();
1088 uint STEP_SIZE = (uint)SearchArgs.DataType / 8;
1089
1090 bool unsigned = SearchArgs.IsUnsignedDataType;
1091 SearchDataTypes sdt = SearchArgs.DataType;
1092 //byte[] buffered_mem = new byte[(MemoryRangeSize - MemoryRangeStart)]; // throws OutOfMemoryException if size is over 2G
1093 logger.Debug.WriteLine(string.Format("Buffered Memory Size -> 0x{0:x8}", MemoryRangeSize - MemoryRangeStart));
1094 UpdateProgress(0, string.Format("Search is Warming Up...Please Wait..."));
1095 //new Action<int, string>(UpdateProgress).Invoke(0, string.Format("Search is Warming Up...Please Wait..."));
1096 Stopwatch provider_st = new Stopwatch();
1097 provider_st.Start();
1098 using (GenericMemoryProvider provider = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1099 {
1100
1101 if (SearchArgs.IsFirstSearch)
1102 {
1103 provider.OpenProvider();
1104 int count = (int)((MemoryRangeSize - MemoryRangeStart) / STEP_SIZE);
1105 SearchResultWriter writer = new SearchResultWriter((int)(count), SearchGuid);
1106 int Last_Whole_Percent_Done = 0;
1107 uint size = STEP_SIZE * 4096;
1108 for (uint i = MemoryRangeStart; i < MemoryRangeSize; i+=size)
1109 {
1110
1111 int bytesRead=0;
1112 byte[] data = new byte[size];
1113 provider.ReadProcessMemoryAtOnce(i, size, out bytesRead, out data);
1114 string message = string.Format(" -> Reading Address: 0x{0:x8}", (i + MemoryRangeStart));
1115 double double_percent_done = 100.0 * (double)((double)i / (double)MemoryRangeSize);
1116 int int_percent_done = (int)double_percent_done;
1117 if ( (int_percent_done != Last_Whole_Percent_Done) && (i % 10000) == 0)
1118 {
1119 UpdateProgress((int)double_percent_done, message);
1120 Last_Whole_Percent_Done = int_percent_done;
1121 }
1122
1123 uint address_index = i;
1124 #region comparison and serialization
1125 switch (sdt)
1126 {
1127 case SearchDataTypes._8bits:
1128 if (unsigned)
1129 {
1130 #region 8bits - unsigned
1131 var t_data = data;
1132 foreach (var Value in t_data)
1133 {
1134 if (SearchWorkerThread.CancellationPending) { break; }
1135 using (_8bit_unsigned_comparer_ comparer = new _8bit_unsigned_comparer_(SearchArgs, address_index))
1136 {
1137 byte value = 0;
1138 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1139 {
1140 using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1141 {
1142 try
1143 {
1144 gmp.OpenProvider();
1145 gmp.ReadMemory(address_index, out value);
1146 gmp.CloseProvider();
1147 }
1148 catch (Exception ex)
1149 {
1150 logger.VerboseError.WriteLine(ex.ToString());
1151 }
1152 }
1153 comparer.Value = value;
1154 }
1155 else
1156 {
1157 value = Convert.ToByte(SearchArgs.CompareStartValue);
1158 comparer.Value = value;
1159 }
1160 if (comparer.Compare(Convert.ToByte(Value), value))
1161 {
1162 writer.WriteResult<byte>(comparer.Address, comparer.Value);
1163 }
1164 }
1165 address_index += STEP_SIZE;
1166 }
1167 #endregion
1168 }
1169 else
1170 {
1171 #region 8bits - signed
1172 var t_data = data;
1173 foreach (var Value in t_data)
1174 {
1175 if (SearchWorkerThread.CancellationPending) { break; }
1176 using (_8bit_signed_comparer_ comparer = new _8bit_signed_comparer_(SearchArgs, address_index))
1177 {
1178 sbyte value = 0;
1179 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1180 {
1181 using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1182 {
1183 try
1184 {
1185 gmp.OpenProvider();
1186 gmp.ReadMemory(address_index, out value);
1187 gmp.CloseProvider();
1188 }
1189 catch (Exception ex)
1190 {
1191 logger.VerboseError.WriteLine(ex.ToString());
1192 }
1193 }
1194 comparer.Value = value;
1195 }
1196 else
1197 {
1198 value = Convert.ToSByte(SearchArgs.CompareStartValue);
1199 comparer.Value = value;
1200 }
1201 if (comparer.Compare(Convert.ToSByte(Value), value))
1202 {
1203 writer.WriteResult<sbyte>(comparer.Address, comparer.Value);
1204 }
1205 }
1206 address_index += STEP_SIZE;
1207 }
1208 #endregion
1209 } break;
1210 case SearchDataTypes._16bits:
1211 if (unsigned)
1212 {
1213 #region 16bits - unsigned
1214 var t_data = BitTools.ConvertByteArray<ushort>(data, new Func<bool>(ShouldCancelAnySearchOperation));
1215 foreach (var Value in t_data)
1216 {
1217 if (SearchWorkerThread.CancellationPending) { break; }
1218 using (_16bit_unsigned_comparer_ comparer = new _16bit_unsigned_comparer_(SearchArgs, address_index))
1219 {
1220 ushort value = 0;
1221 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1222 {
1223 using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1224 {
1225 try
1226 {
1227 gmp.OpenProvider();
1228 gmp.ReadMemory(address_index, out value);
1229 gmp.CloseProvider();
1230 }
1231 catch (Exception ex)
1232 {
1233 logger.VerboseError.WriteLine(ex.ToString());
1234 }
1235 }
1236 comparer.Value = value;
1237 }
1238 else
1239 {
1240 value = Convert.ToUInt16(SearchArgs.CompareStartValue);
1241 comparer.Value = value;
1242 }
1243 if (comparer.Compare(Convert.ToUInt16(Value), value))
1244 {
1245 writer.WriteResult<ushort>(comparer.Address, comparer.Value);
1246 }
1247 }
1248 address_index += STEP_SIZE;
1249 }
1250 #endregion
1251 }
1252 else
1253 {
1254 #region 16bits - signed
1255 var t_data = BitTools.ConvertByteArray<short>(data, new Func<bool>(ShouldCancelAnySearchOperation));
1256 foreach (var Value in t_data)
1257 {
1258 if (SearchWorkerThread.CancellationPending) { break; }
1259 using (_16bit_signed_comparer_ comparer = new _16bit_signed_comparer_(SearchArgs, address_index))
1260 {
1261 short value = 0;
1262 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1263 {
1264 using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1265 {
1266 try
1267 {
1268 gmp.OpenProvider();
1269 gmp.ReadMemory(address_index, out value);
1270 gmp.CloseProvider();
1271 }
1272 catch (Exception ex)
1273 {
1274 logger.VerboseError.WriteLine(ex.ToString());
1275 }
1276 }
1277 comparer.Value = value;
1278 }
1279 else
1280 {
1281 value = Convert.ToInt16(SearchArgs.CompareStartValue);
1282 comparer.Value = value;
1283 }
1284 if (comparer.Compare(Convert.ToSByte(Value), value))
1285 {
1286 writer.WriteResult<short>(comparer.Address, comparer.Value);
1287 }
1288 }
1289 address_index += STEP_SIZE;
1290 }
1291 #endregion
1292 } break;
1293 case SearchDataTypes._32bits:
1294 if (unsigned)
1295 {
1296 #region 32bits - unsigned
1297 var t_data = BitTools.ConvertByteArray<uint>(data, new Func<bool>(ShouldCancelAnySearchOperation));
1298 foreach (var Value in t_data)
1299 {
1300 if (SearchWorkerThread.CancellationPending) { break; }
1301 using (_32bit_unsigned_comparer_ comparer = new _32bit_unsigned_comparer_(SearchArgs, address_index))
1302 {
1303 uint value = 0;
1304 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1305 {
1306 using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1307 {
1308 try
1309 {
1310 gmp.OpenProvider();
1311 gmp.ReadMemory(address_index, out value);
1312 gmp.CloseProvider();
1313 }
1314 catch (Exception ex)
1315 {
1316 logger.VerboseError.WriteLine(ex.ToString());
1317 }
1318 }
1319 comparer.Value = value;
1320 }
1321 else
1322 {
1323 value = Convert.ToUInt32(SearchArgs.CompareStartValue);
1324 comparer.Value = value;
1325 }
1326 if (comparer.Compare(Convert.ToUInt32(Value), value))
1327 {
1328 writer.WriteResult<uint>(comparer.Address, comparer.Value);
1329 }
1330 }
1331 address_index += STEP_SIZE;
1332 }
1333 #endregion
1334 }
1335 else
1336 {
1337 #region 32bits - signed
1338 var t_data = BitTools.ConvertByteArray<int>(data, new Func<bool>(ShouldCancelAnySearchOperation));
1339 foreach (var Value in t_data)
1340 {
1341 if (SearchWorkerThread.CancellationPending) { break; }
1342 using (_32bit_signed_comparer_ comparer = new _32bit_signed_comparer_(SearchArgs, address_index))
1343 {
1344 int value = 0;
1345 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1346 {
1347 using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1348 {
1349 try
1350 {
1351 gmp.OpenProvider();
1352 gmp.ReadMemory(address_index, out value);
1353 gmp.CloseProvider();
1354 }
1355 catch (Exception ex)
1356 {
1357 logger.VerboseError.WriteLine(ex.ToString());
1358 }
1359 }
1360 comparer.Value = value;
1361 }
1362 else
1363 {
1364 value = Convert.ToInt32(SearchArgs.CompareStartValue);
1365 comparer.Value = value;
1366 }
1367 if (comparer.Compare(Convert.ToInt32(Value), value))
1368 {
1369 writer.WriteResult<int>(comparer.Address, comparer.Value);
1370 }
1371 }
1372 address_index += STEP_SIZE;
1373 }
1374 #endregion
1375 } break;
1376 case SearchDataTypes._64bits:
1377 if (unsigned)
1378 {
1379 #region 64bits - unsigned
1380 var t_data = BitTools.ConvertByteArray<ulong>(data, new Func<bool>(ShouldCancelAnySearchOperation));
1381 foreach (var Value in t_data)
1382 {
1383 if (SearchWorkerThread.CancellationPending) { break; }
1384 using (_64bit_unsigned_comparer_ comparer = new _64bit_unsigned_comparer_(SearchArgs, address_index))
1385 {
1386 ulong value = 0;
1387 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1388 {
1389 using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1390 {
1391 try
1392 {
1393 gmp.OpenProvider();
1394 gmp.ReadMemory(address_index, out value);
1395 gmp.CloseProvider();
1396 }
1397 catch (Exception ex)
1398 {
1399 logger.VerboseError.WriteLine(ex.ToString());
1400 }
1401 }
1402 comparer.Value = value;
1403 }
1404 else
1405 {
1406 value = Convert.ToUInt64(SearchArgs.CompareStartValue);
1407 comparer.Value = value;
1408 }
1409 if (comparer.Compare(Convert.ToUInt64(Value), value))
1410 {
1411 writer.WriteResult<ulong>(comparer.Address, comparer.Value);
1412 }
1413 }
1414 address_index += STEP_SIZE;
1415 }
1416 #endregion
1417 }
1418 else
1419 {
1420 #region 64bits - signed
1421 var t_data = BitTools.ConvertByteArray<long>(data, new Func<bool>(ShouldCancelAnySearchOperation));
1422 foreach (var Value in t_data)
1423 {
1424 if (SearchWorkerThread.CancellationPending) { break; }
1425 using (_64bit_signed_comparer_ comparer = new _64bit_signed_comparer_(SearchArgs, address_index))
1426 {
1427 long value = 0;
1428 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1429 {
1430 using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1431 {
1432 try
1433 {
1434 gmp.OpenProvider();
1435 gmp.ReadMemory(address_index, out value);
1436 gmp.CloseProvider();
1437 }
1438 catch (Exception ex)
1439 {
1440 logger.VerboseError.WriteLine(ex.ToString());
1441 }
1442 }
1443 comparer.Value = value;
1444 }
1445 else
1446 {
1447 value = Convert.ToInt64(SearchArgs.CompareStartValue);
1448 comparer.Value = value;
1449 }
1450 if (comparer.Compare(Convert.ToInt64(Value), value))
1451 {
1452 writer.WriteResult<long>(comparer.Address, comparer.Value);
1453 }
1454 }
1455 address_index += STEP_SIZE;
1456 }
1457 #endregion
1458 } break;
1459
1460 }
1461 #endregion
1462
1463 if (SearchWorkerThread.CancellationPending)
1464 {
1465 provider_st.Stop();
1466 st.Stop();
1467 writer.CancelRequest();
1468 writer.Dispose();
1469 writer = null;
1470 e.Result = true;
1471 provider.CloseProvider();
1472 return;
1473 }
1474 }
1475
1476 if (SearchWorkerThread.CancellationPending)
1477 {
1478 provider_st.Stop();
1479 st.Stop();
1480 writer.CancelRequest();
1481 writer.Dispose();
1482 writer = null;
1483 e.Result = true;
1484 provider.CloseProvider();
1485 return;
1486 }
1487 writer.Dispose();
1488 writer = null;
1489 provider.CloseProvider();
1490 }
1491 else
1492 {
1493 Guid new_SearchGuid = SearchGuid;
1494 //int Last_Whole_Percent_Done = 0;
1495 // need to get the address list
1496 using (SearchResultReader reader = new SearchResultReader(SearchGuid, true)) // delete the file on dispose
1497 {
1498 new_SearchGuid = GuidTools.IncrementGuid(SearchGuid);
1499 using (SearchResultWriter writer = new SearchResultWriter(0, new_SearchGuid))
1500 {
1501 //int index = 0;
1502
1503 provider.OpenProvider();
1504 //int count = (int)((MemoryRangeSize - MemoryRangeStart) / STEP_SIZE);
1505 //SearchResultWriter writer = new SearchResultWriter((int)(count), SearchGuid);
1506 int Last_Whole_Percent_Done = 0;
1507 uint size = STEP_SIZE * 4096;
1508 for (uint i = MemoryRangeStart; i < MemoryRangeSize; i += size)
1509 {
1510
1511 int bytesRead = 0;
1512 byte[] data = new byte[size];
1513 provider.ReadProcessMemoryAtOnce(i, size, out bytesRead, out data);
1514 string message = string.Format(" -> Reading Address: 0x{0:x8}", (i + MemoryRangeStart));
1515 double double_percent_done = 100.0 * (double)((double)i / (double)MemoryRangeSize);
1516 int int_percent_done = (int)double_percent_done;
1517 if ((int_percent_done != Last_Whole_Percent_Done) && (i % 10000) == 0)
1518 {
1519 UpdateProgress((int)double_percent_done, message);
1520 Last_Whole_Percent_Done = int_percent_done;
1521 }
1522
1523 uint address_index = i;
1524 #region comparison and serialization
1525 switch (sdt)
1526 {
1527 case SearchDataTypes._8bits:
1528 if (unsigned)
1529 {
1530 #region 8bits - unsigned
1531 var t_data = data;
1532 foreach (var Value in t_data)
1533 {
1534 if (SearchWorkerThread.CancellationPending) { break; }
1535 using (_8bit_unsigned_comparer_ comparer = new _8bit_unsigned_comparer_(SearchArgs, address_index))
1536 {
1537 byte value = 0;
1538 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1539 {
1540 using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1541 {
1542 try
1543 {
1544 gmp.OpenProvider();
1545 gmp.ReadMemory(address_index, out value);
1546 gmp.CloseProvider();
1547 }
1548 catch (Exception ex)
1549 {
1550 logger.VerboseError.WriteLine(ex.ToString());
1551 }
1552 }
1553 comparer.Value = value;
1554 }
1555 else
1556 {
1557 value = Convert.ToByte(SearchArgs.CompareStartValue);
1558 comparer.Value = value;
1559 }
1560 if (comparer.Compare(Convert.ToByte(Value), value))
1561 {
1562 if (reader.ContainsAddress(address_index, SearchArgs.IsUnsignedDataType, SearchArgs.DataType, new Action<int, string>(UpdateProgress), new Func<bool>(ShouldCancelAnySearchOperation)))
1563 {
1564 writer.WriteResult<byte>(comparer.Address, comparer.Value);
1565 }
1566 }
1567 }
1568 address_index += STEP_SIZE;
1569 }
1570 #endregion
1571 }
1572 else
1573 {
1574 #region 8bits - signed
1575 var t_data = data;
1576 foreach (var Value in t_data)
1577 {
1578 if (SearchWorkerThread.CancellationPending) { break; }
1579 using (_8bit_signed_comparer_ comparer = new _8bit_signed_comparer_(SearchArgs, address_index))
1580 {
1581 sbyte value = 0;
1582 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1583 {
1584 using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1585 {
1586 try
1587 {
1588 gmp.OpenProvider();
1589 gmp.ReadMemory(address_index, out value);
1590 gmp.CloseProvider();
1591 }
1592 catch (Exception ex)
1593 {
1594 logger.VerboseError.WriteLine(ex.ToString());
1595 }
1596 }
1597 comparer.Value = value;
1598 }
1599 else
1600 {
1601 value = Convert.ToSByte(SearchArgs.CompareStartValue);
1602 comparer.Value = value;
1603 }
1604 if (comparer.Compare(Convert.ToSByte(Value), value))
1605 {
1606 if (reader.ContainsAddress(address_index, SearchArgs.IsUnsignedDataType, SearchArgs.DataType, new Action<int, string>(UpdateProgress), new Func<bool>(ShouldCancelAnySearchOperation)))
1607 {
1608 writer.WriteResult<sbyte>(comparer.Address, comparer.Value);
1609 }
1610 }
1611 }
1612 address_index += STEP_SIZE;
1613 }
1614 #endregion
1615 } break;
1616 case SearchDataTypes._16bits:
1617 if (unsigned)
1618 {
1619 #region 16bits - unsigned
1620 var t_data = BitTools.ConvertByteArray<ushort>(data, new Func<bool>(ShouldCancelAnySearchOperation));
1621 foreach (var Value in t_data)
1622 {
1623 if (SearchWorkerThread.CancellationPending) { break; }
1624 using (_16bit_unsigned_comparer_ comparer = new _16bit_unsigned_comparer_(SearchArgs, address_index))
1625 {
1626 ushort value = 0;
1627 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1628 {
1629 using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1630 {
1631 try
1632 {
1633 gmp.OpenProvider();
1634 gmp.ReadMemory(address_index, out value);
1635 gmp.CloseProvider();
1636 }
1637 catch (Exception ex)
1638 {
1639 logger.VerboseError.WriteLine(ex.ToString());
1640 }
1641 }
1642 comparer.Value = value;
1643 }
1644 else
1645 {
1646 value = Convert.ToUInt16(SearchArgs.CompareStartValue);
1647 comparer.Value = value;
1648 }
1649 if (comparer.Compare(Convert.ToUInt16(Value), value))
1650 {
1651 if (reader.ContainsAddress(address_index, SearchArgs.IsUnsignedDataType, SearchArgs.DataType, new Action<int, string>(UpdateProgress), new Func<bool>(ShouldCancelAnySearchOperation)))
1652 {
1653 writer.WriteResult<ushort>(comparer.Address, comparer.Value);
1654 }
1655 }
1656 }
1657 address_index += STEP_SIZE;
1658 }
1659 #endregion
1660 }
1661 else
1662 {
1663 #region 16bits - signed
1664 var t_data = BitTools.ConvertByteArray<short>(data, new Func<bool>(ShouldCancelAnySearchOperation));
1665 foreach (var Value in t_data)
1666 {
1667 if (SearchWorkerThread.CancellationPending) { break; }
1668 using (_16bit_signed_comparer_ comparer = new _16bit_signed_comparer_(SearchArgs, address_index))
1669 {
1670 short value = 0;
1671 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1672 {
1673 using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1674 {
1675 try
1676 {
1677 gmp.OpenProvider();
1678 gmp.ReadMemory(address_index, out value);
1679 gmp.CloseProvider();
1680 }
1681 catch (Exception ex)
1682 {
1683 logger.VerboseError.WriteLine(ex.ToString());
1684 }
1685 }
1686 comparer.Value = value;
1687 }
1688 else
1689 {
1690 value = Convert.ToInt16(SearchArgs.CompareStartValue);
1691 comparer.Value = value;
1692 }
1693 if (comparer.Compare(Convert.ToSByte(Value), value))
1694 {
1695 if (reader.ContainsAddress(address_index, SearchArgs.IsUnsignedDataType, SearchArgs.DataType, new Action<int, string>(UpdateProgress), new Func<bool>(ShouldCancelAnySearchOperation)))
1696 {
1697 writer.WriteResult<short>(comparer.Address, comparer.Value);
1698 }
1699 }
1700 }
1701 address_index += STEP_SIZE;
1702 }
1703 #endregion
1704 } break;
1705 case SearchDataTypes._32bits:
1706 if (unsigned)
1707 {
1708 #region 32bits - unsigned
1709 var t_data = BitTools.ConvertByteArray<uint>(data, new Func<bool>(ShouldCancelAnySearchOperation));
1710 foreach (var Value in t_data)
1711 {
1712 if (SearchWorkerThread.CancellationPending) { break; }
1713 using (_32bit_unsigned_comparer_ comparer = new _32bit_unsigned_comparer_(SearchArgs, address_index))
1714 {
1715 uint value = 0;
1716 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1717 {
1718 using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1719 {
1720 try
1721 {
1722 gmp.OpenProvider();
1723 gmp.ReadMemory(address_index, out value);
1724 gmp.CloseProvider();
1725 }
1726 catch (Exception ex)
1727 {
1728 logger.VerboseError.WriteLine(ex.ToString());
1729 }
1730 }
1731 comparer.Value = value;
1732 }
1733 else
1734 {
1735 value = Convert.ToUInt32(SearchArgs.CompareStartValue);
1736 comparer.Value = value;
1737 }
1738 if (comparer.Compare(Convert.ToUInt32(Value), value))
1739 {
1740 if (reader.ContainsAddress(address_index, SearchArgs.IsUnsignedDataType, SearchArgs.DataType, new Action<int, string>(UpdateProgress), new Func<bool>(ShouldCancelAnySearchOperation)))
1741 {
1742 writer.WriteResult<uint>(comparer.Address, comparer.Value);
1743 }
1744 }
1745 }
1746 address_index += STEP_SIZE;
1747 }
1748 #endregion
1749 }
1750 else
1751 {
1752 #region 32bits - signed
1753 var t_data = BitTools.ConvertByteArray<int>(data, new Func<bool>(ShouldCancelAnySearchOperation));
1754 foreach (var Value in t_data)
1755 {
1756 if (SearchWorkerThread.CancellationPending) { break; }
1757 using (_32bit_signed_comparer_ comparer = new _32bit_signed_comparer_(SearchArgs, address_index))
1758 {
1759 int value = 0;
1760 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1761 {
1762 using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1763 {
1764 try
1765 {
1766 gmp.OpenProvider();
1767 gmp.ReadMemory(address_index, out value);
1768 gmp.CloseProvider();
1769 }
1770 catch (Exception ex)
1771 {
1772 logger.VerboseError.WriteLine(ex.ToString());
1773 }
1774 }
1775 comparer.Value = value;
1776 }
1777 else
1778 {
1779 value = Convert.ToInt32(SearchArgs.CompareStartValue);
1780 comparer.Value = value;
1781 }
1782 if (comparer.Compare(Convert.ToInt32(Value), value))
1783 {
1784 if (reader.ContainsAddress(address_index, SearchArgs.IsUnsignedDataType, SearchArgs.DataType, new Action<int, string>(UpdateProgress), new Func<bool>(ShouldCancelAnySearchOperation)))
1785 {
1786 writer.WriteResult<int>(comparer.Address, comparer.Value);
1787 }
1788 }
1789 }
1790 address_index += STEP_SIZE;
1791 }
1792 #endregion
1793 } break;
1794 case SearchDataTypes._64bits:
1795 if (unsigned)
1796 {
1797 #region 64bits - unsigned
1798 var t_data = BitTools.ConvertByteArray<ulong>(data, new Func<bool>(ShouldCancelAnySearchOperation));
1799 foreach (var Value in t_data)
1800 {
1801 if (SearchWorkerThread.CancellationPending) { break; }
1802 using (_64bit_unsigned_comparer_ comparer = new _64bit_unsigned_comparer_(SearchArgs, address_index))
1803 {
1804 ulong value = 0;
1805 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1806 {
1807 using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1808 {
1809 try
1810 {
1811 gmp.OpenProvider();
1812 gmp.ReadMemory(address_index, out value);
1813 gmp.CloseProvider();
1814 }
1815 catch (Exception ex)
1816 {
1817 logger.VerboseError.WriteLine(ex.ToString());
1818 }
1819 }
1820 comparer.Value = value;
1821 }
1822 else
1823 {
1824 value = Convert.ToUInt64(SearchArgs.CompareStartValue);
1825 comparer.Value = value;
1826 }
1827 if (comparer.Compare(Convert.ToUInt64(Value), value))
1828 {
1829 if (reader.ContainsAddress(address_index, SearchArgs.IsUnsignedDataType, SearchArgs.DataType, new Action<int, string>(UpdateProgress), new Func<bool>(ShouldCancelAnySearchOperation)))
1830 {
1831 writer.WriteResult<ulong>(comparer.Address, comparer.Value);
1832 }
1833 }
1834 }
1835 address_index += STEP_SIZE;
1836 }
1837 #endregion
1838 }
1839 else
1840 {
1841 #region 64bits - signed
1842 var t_data = BitTools.ConvertByteArray<long>(data, new Func<bool>(ShouldCancelAnySearchOperation));
1843 foreach (var Value in t_data)
1844 {
1845 if (SearchWorkerThread.CancellationPending) { break; }
1846 using (_64bit_signed_comparer_ comparer = new _64bit_signed_comparer_(SearchArgs, address_index))
1847 {
1848 long value = 0;
1849 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1850 {
1851 using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
1852 {
1853 try
1854 {
1855 gmp.OpenProvider();
1856 gmp.ReadMemory(address_index, out value);
1857 gmp.CloseProvider();
1858 }
1859 catch (Exception ex)
1860 {
1861 logger.VerboseError.WriteLine(ex.ToString());
1862 }
1863 }
1864 comparer.Value = value;
1865 }
1866 else
1867 {
1868 value = Convert.ToInt64(SearchArgs.CompareStartValue);
1869 comparer.Value = value;
1870 }
1871 if (comparer.Compare(Convert.ToInt64(Value), value))
1872 {
1873 if (reader.ContainsAddress(address_index, SearchArgs.IsUnsignedDataType, SearchArgs.DataType, new Action<int, string>(UpdateProgress), new Func<bool>(ShouldCancelAnySearchOperation)))
1874 {
1875 writer.WriteResult<long>(comparer.Address, comparer.Value);
1876 }
1877 }
1878 }
1879 address_index += STEP_SIZE;
1880 }
1881 #endregion
1882 } break;
1883
1884 }
1885 #endregion
1886
1887 if (SearchWorkerThread.CancellationPending)
1888 {
1889 provider_st.Stop();
1890 st.Stop();
1891 writer.CancelRequest();
1892 writer.Dispose();
1893 e.Result = true;
1894 provider.CloseProvider();
1895 return;
1896 }
1897 }
1898 #region old-code2
1899 //const int ChunkCount = 0x100;
1900 //for (int ResultIndex = 0; ResultIndex < reader.ResultCount; ResultIndex += ChunkCount)
1901 //{
1902 // StructResultType<ulong>[] ChunkResults = new StructResultType<ulong>[ChunkCount];
1903 // for (int ChunkIndex = 0; ChunkIndex < ChunkCount; ChunkIndex++)
1904 // {
1905 // int ResultChunkIndex = (ResultIndex + ChunkIndex);
1906 // StructResultType<ulong> result = StructResultType<ulong>.Empty;
1907 // reader.GetResultAtIndex(ResultChunkIndex, SearchArgs.IsUnsignedDataType, SearchArgs.DataType, new Action<int, string>(UpdateProgress), out result, new Func<bool>(ShouldCancelAnySearchOperation));
1908 // ChunkResults[ChunkIndex] = result;
1909 // if (SearchWorkerThread.CancellationPending)
1910 // {
1911 // provider_st.Stop();
1912 // st.Stop();
1913 // writer.CancelRequest();
1914 // writer.Dispose();
1915 // e.Result = true;
1916 // break;
1917 // }
1918 // }
1919 // // Get update value(s) for each chunk result
1920 // ISerializedResult sr = (reader as ISerializedResult);
1921 // if(sr == null)
1922 // {
1923 // throw new ArgumentNullException("sr",string.Format("Unable to cast: '{0}' to '{1}'", reader.GetType().Name,typeof(ISerializedResult).Name));
1924 // }
1925 // Stopwatch st_memory_results_updater = new Stopwatch();
1926 // st_memory_results_updater.Start();
1927 // sr.UpdateResultValuesFromMemory(ref ChunkResults, SearchArgs.IsUnsignedDataType, SearchArgs.DataType, new Action<int, string>(UpdateProgress), new Func<bool>(ShouldCancelAnySearchOperation), (IAcceptsProcessAndConfig)this);
1928 // if (SearchWorkerThread.CancellationPending) // catch a cancelled operation from UpdateResultValuesFromMemory
1929 // {
1930 // provider_st.Stop();
1931 // st.Stop();
1932 // writer.CancelRequest();
1933 // writer.Dispose();
1934 // e.Result = true;
1935 // break;
1936 // }
1937 // st_memory_results_updater.Stop();
1938 // logger.Profiler.WriteLine("sr.UpdateResultValuesFromMemory [ResultIndex=0x{0} ChunkCount=0x{1}] took a total of {2} seconds to complete", ResultIndex.ToString("X"), ChunkCount.ToString("X"), st_memory_results_updater.Elapsed.TotalSeconds.ToString());
1939 // // ChunkResults should have had thier values updated from memory
1940 // string message = string.Format(" -> Updateing Values [ResultIndex=0x{0} of Total=0x{1}]", ResultIndex.ToString("X"), reader.ResultCount.ToString("X"));
1941 // double double_percent_done = 100.0 * (double)((double)ResultIndex / (double)reader.ResultCount);
1942 // int int_percent_done = (int)double_percent_done;
1943 // //if ((int_percent_done != Last_Whole_Percent_Done) && (index % 1000) == 0)
1944 // //{
1945 // // UpdateProgress((int)double_percent_done, message);
1946 // // Last_Whole_Percent_Done = int_percent_done;
1947 // //}
1948 // if (((double)ResultIndex % ((double)reader.ResultCount) * 0.25) == 0)
1949 // //if ((ResultIndex % 10000) == 0)
1950 // {
1951 // UpdateProgress((int)double_percent_done, message);
1952 // Last_Whole_Percent_Done = int_percent_done;
1953 // }
1954 // if (SearchWorkerThread.CancellationPending)
1955 // {
1956 // provider_st.Stop();
1957 // st.Stop();
1958 // writer.CancelRequest();
1959 // writer.Dispose();
1960 // e.Result = true;
1961 // break;
1962 // }
1963 //}
1964 #endregion
1965
1966 #region old-code1
1967 //foreach (var k in reader.GetResults(SearchArgs.IsUnsignedDataType, SearchArgs.DataType, new Action<int, string>(UpdateProgress), new Func<bool>(ShouldCancelAnySearchOperation)))
1968 //{
1969 // if (SearchWorkerThread.CancellationPending)
1970 // {
1971 // provider_st.Stop();
1972 // st.Stop();
1973 // writer.CancelRequest();
1974 // writer.Dispose();
1975 // e.Result = true;
1976 // return;
1977 // }
1978 // //int Last_Whole_Percent_Done = 0;
1979 // provider.OpenProvider();
1980 // int bytesRead = 0;
1981 // byte[] data = new byte[STEP_SIZE];
1982 // provider.ReadProcessMemory(k.Address, STEP_SIZE, out bytesRead, out data);
1983 // provider.CloseProvider();
1984 // if (SearchWorkerThread.CancellationPending)
1985 // {
1986 // provider_st.Stop();
1987 // st.Stop();
1988 // writer.CancelRequest();
1989 // writer.Dispose();
1990 // e.Result = true;
1991 // return;
1992 // }
1993 // uint address_index = k.Address;
1994 // string message = string.Format(" -> Reading Address 0x{0:x8} from index 0x{1}", k.Address, index.ToString("X"));
1995 // double double_percent_done = 100.0 * (double)((double)index / (double)reader.ResultCount);
1996 // int int_percent_done = (int)double_percent_done;
1997 // //if ((int_percent_done != Last_Whole_Percent_Done) && (index % 1000) == 0)
1998 // //{
1999 // // UpdateProgress((int)double_percent_done, message);
2000 // // Last_Whole_Percent_Done = int_percent_done;
2001 // //}
2002 // if ((index % 10000) == 0)
2003 // {
2004 // UpdateProgress((int)double_percent_done, message);
2005 // //Last_Whole_Percent_Done = int_percent_done;
2006 // }
2007 // #region comparison and serialization
2008 // switch (sdt)
2009 // {
2010 // case SearchDataTypes._8bits:
2011 // if (unsigned)
2012 // {
2013 // #region 8bits - unsigned
2014 // var t_data = data;
2015 // foreach (var Value in t_data)
2016 // {
2017 // if (SearchWorkerThread.CancellationPending) { break; }
2018 // using (_8bit_unsigned_comparer_ comparer = new _8bit_unsigned_comparer_(SearchArgs, address_index))
2019 // {
2020 // byte value = 0;
2021 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
2022 // {
2023 // using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
2024 // {
2025 // try
2026 // {
2027 // gmp.OpenProvider();
2028 // gmp.ReadMemory(address_index, out value);
2029 // gmp.CloseProvider();
2030 // }
2031 // catch (Exception ex)
2032 // {
2033 // logger.VerboseError.WriteLine(ex.ToString());
2034 // }
2035 // }
2036 // comparer.Value = value;
2037 // }
2038 // else
2039 // {
2040 // value = Convert.ToByte(SearchArgs.CompareStartValue);
2041 // comparer.Value = value;
2042 // }
2043 // if (comparer.Compare(Convert.ToByte(Value), value))
2044 // {
2045 // writer.WriteResult<byte>(comparer.Address, comparer.Value);
2046 // }
2047 // }
2048 // address_index += STEP_SIZE;
2049 // }
2050 // #endregion
2051 // }
2052 // else
2053 // {
2054 // #region 8bits - signed
2055 // var t_data = data;
2056 // foreach (var Value in t_data)
2057 // {
2058 // if (SearchWorkerThread.CancellationPending) { break; }
2059 // using (_8bit_signed_comparer_ comparer = new _8bit_signed_comparer_(SearchArgs, address_index))
2060 // {
2061 // sbyte value = 0;
2062 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
2063 // {
2064 // using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
2065 // {
2066 // try
2067 // {
2068 // gmp.OpenProvider();
2069 // gmp.ReadMemory(address_index, out value);
2070 // gmp.CloseProvider();
2071 // }
2072 // catch (Exception ex)
2073 // {
2074 // logger.VerboseError.WriteLine(ex.ToString());
2075 // }
2076 // }
2077 // comparer.Value = value;
2078 // }
2079 // else
2080 // {
2081 // value = Convert.ToSByte(SearchArgs.CompareStartValue);
2082 // comparer.Value = value;
2083 // }
2084 // if (comparer.Compare(Convert.ToSByte(Value), value))
2085 // {
2086 // writer.WriteResult<sbyte>(comparer.Address, comparer.Value);
2087 // }
2088 // }
2089 // address_index += STEP_SIZE;
2090 // }
2091 // #endregion
2092 // } break;
2093 // case SearchDataTypes._16bits:
2094 // if (unsigned)
2095 // {
2096 // #region 16bits - unsigned
2097 // var t_data = BitTools.ConvertByteArray<ushort>(data, new Func<bool>(ShouldCancelAnySearchOperation));
2098 // foreach (var Value in t_data)
2099 // {
2100 // if (SearchWorkerThread.CancellationPending) { break; }
2101 // using (_16bit_unsigned_comparer_ comparer = new _16bit_unsigned_comparer_(SearchArgs, address_index))
2102 // {
2103 // ushort value = 0;
2104 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
2105 // {
2106 // using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
2107 // {
2108 // try
2109 // {
2110 // gmp.OpenProvider();
2111 // gmp.ReadMemory(address_index, out value);
2112 // gmp.CloseProvider();
2113 // }
2114 // catch (Exception ex)
2115 // {
2116 // logger.VerboseError.WriteLine(ex.ToString());
2117 // }
2118 // }
2119 // comparer.Value = value;
2120 // }
2121 // else
2122 // {
2123 // value = Convert.ToUInt16(SearchArgs.CompareStartValue);
2124 // comparer.Value = value;
2125 // }
2126 // if (comparer.Compare(Convert.ToUInt16(Value), value))
2127 // {
2128 // writer.WriteResult<ushort>(comparer.Address, comparer.Value);
2129 // }
2130 // }
2131 // address_index += STEP_SIZE;
2132 // }
2133 // #endregion
2134 // }
2135 // else
2136 // {
2137 // #region 16bits - signed
2138 // var t_data = BitTools.ConvertByteArray<short>(data, new Func<bool>(ShouldCancelAnySearchOperation));
2139 // foreach (var Value in t_data)
2140 // {
2141 // if (SearchWorkerThread.CancellationPending) { break; }
2142 // using (_16bit_signed_comparer_ comparer = new _16bit_signed_comparer_(SearchArgs, address_index))
2143 // {
2144 // short value = 0;
2145 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
2146 // {
2147 // using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
2148 // {
2149 // try
2150 // {
2151 // gmp.OpenProvider();
2152 // gmp.ReadMemory(address_index, out value);
2153 // gmp.CloseProvider();
2154 // }
2155 // catch (Exception ex)
2156 // {
2157 // logger.VerboseError.WriteLine(ex.ToString());
2158 // }
2159 // }
2160 // comparer.Value = value;
2161 // }
2162 // else
2163 // {
2164 // value = Convert.ToInt16(SearchArgs.CompareStartValue);
2165 // comparer.Value = value;
2166 // }
2167 // if (comparer.Compare(Convert.ToSByte(Value), value))
2168 // {
2169 // writer.WriteResult<short>(comparer.Address, comparer.Value);
2170 // }
2171 // }
2172 // address_index += STEP_SIZE;
2173 // }
2174 // #endregion
2175 // } break;
2176 // case SearchDataTypes._32bits:
2177 // if (unsigned)
2178 // {
2179 // #region 32bits - unsigned
2180 // var t_data = BitTools.ConvertByteArray<uint>(data, new Func<bool>(ShouldCancelAnySearchOperation));
2181 // foreach (var Value in t_data)
2182 // {
2183 // if (SearchWorkerThread.CancellationPending) { break; }
2184 // using (_32bit_unsigned_comparer_ comparer = new _32bit_unsigned_comparer_(SearchArgs, address_index))
2185 // {
2186 // uint value = 0;
2187 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
2188 // {
2189 // using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
2190 // {
2191 // try
2192 // {
2193 // gmp.OpenProvider();
2194 // gmp.ReadMemory(address_index, out value);
2195 // gmp.CloseProvider();
2196 // }
2197 // catch (Exception ex)
2198 // {
2199 // logger.VerboseError.WriteLine(ex.ToString());
2200 // }
2201 // }
2202 // comparer.Value = value;
2203 // }
2204 // else
2205 // {
2206 // value = Convert.ToUInt32(SearchArgs.CompareStartValue);
2207 // comparer.Value = value;
2208 // }
2209 // if (comparer.Compare(Convert.ToUInt32(Value), value))
2210 // {
2211 // writer.WriteResult<uint>(comparer.Address, comparer.Value);
2212 // }
2213 // }
2214 // address_index += STEP_SIZE;
2215 // }
2216 // #endregion
2217 // }
2218 // else
2219 // {
2220 // #region 32bits - signed
2221 // var t_data = BitTools.ConvertByteArray<int>(data, new Func<bool>(ShouldCancelAnySearchOperation));
2222 // foreach (var Value in t_data)
2223 // {
2224 // if (SearchWorkerThread.CancellationPending) { break; }
2225 // using (_32bit_signed_comparer_ comparer = new _32bit_signed_comparer_(SearchArgs, address_index))
2226 // {
2227 // int value = 0;
2228 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
2229 // {
2230 // using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
2231 // {
2232 // try
2233 // {
2234 // gmp.OpenProvider();
2235 // gmp.ReadMemory(address_index, out value);
2236 // gmp.CloseProvider();
2237 // }
2238 // catch (Exception ex)
2239 // {
2240 // logger.VerboseError.WriteLine(ex.ToString());
2241 // }
2242 // }
2243 // comparer.Value = value;
2244 // }
2245 // else
2246 // {
2247 // value = Convert.ToInt32(SearchArgs.CompareStartValue);
2248 // comparer.Value = value;
2249 // }
2250 // if (comparer.Compare(Convert.ToInt32(Value), value))
2251 // {
2252 // writer.WriteResult<int>(comparer.Address, comparer.Value);
2253 // }
2254 // }
2255 // address_index += STEP_SIZE;
2256 // }
2257 // #endregion
2258 // } break;
2259 // case SearchDataTypes._64bits:
2260 // if (unsigned)
2261 // {
2262 // #region 64bits - unsigned
2263 // var t_data = BitTools.ConvertByteArray<ulong>(data, new Func<bool>(ShouldCancelAnySearchOperation));
2264 // foreach (var Value in t_data)
2265 // {
2266 // if (SearchWorkerThread.CancellationPending) { break; }
2267 // using (_64bit_unsigned_comparer_ comparer = new _64bit_unsigned_comparer_(SearchArgs, address_index))
2268 // {
2269 // ulong value = 0;
2270 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
2271 // {
2272 // using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
2273 // {
2274 // try
2275 // {
2276 // gmp.OpenProvider();
2277 // gmp.ReadMemory(address_index, out value);
2278 // gmp.CloseProvider();
2279 // }
2280 // catch (Exception ex)
2281 // {
2282 // logger.VerboseError.WriteLine(ex.ToString());
2283 // }
2284 // }
2285 // comparer.Value = value;
2286 // }
2287 // else
2288 // {
2289 // value = Convert.ToUInt64(SearchArgs.CompareStartValue);
2290 // comparer.Value = value;
2291 // }
2292 // if (comparer.Compare(Convert.ToUInt64(Value), value))
2293 // {
2294 // writer.WriteResult<ulong>(comparer.Address, comparer.Value);
2295 // }
2296 // }
2297 // address_index += STEP_SIZE;
2298 // }
2299 // #endregion
2300 // }
2301 // else
2302 // {
2303 // #region 64bits - signed
2304 // var t_data = BitTools.ConvertByteArray<long>(data, new Func<bool>(ShouldCancelAnySearchOperation));
2305 // foreach (var Value in t_data)
2306 // {
2307 // if (SearchWorkerThread.CancellationPending) { break; }
2308 // using (_64bit_signed_comparer_ comparer = new _64bit_signed_comparer_(SearchArgs, address_index))
2309 // {
2310 // long value = 0;
2311 // if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
2312 // {
2313 // using (GenericMemoryProvider gmp = new GenericMemoryProvider((IAcceptsProcessAndConfig)this))
2314 // {
2315 // try
2316 // {
2317 // gmp.OpenProvider();
2318 // gmp.ReadMemory(address_index, out value);
2319 // gmp.CloseProvider();
2320 // }
2321 // catch (Exception ex)
2322 // {
2323 // logger.VerboseError.WriteLine(ex.ToString());
2324 // }
2325 // }
2326 // comparer.Value = value;
2327 // }
2328 // else
2329 // {
2330 // value = Convert.ToInt64(SearchArgs.CompareStartValue);
2331 // comparer.Value = value;
2332 // }
2333 // if (comparer.Compare(Convert.ToInt64(Value), value))
2334 // {
2335 // writer.WriteResult<long>(comparer.Address, comparer.Value);
2336 // }
2337 // }
2338 // address_index += STEP_SIZE;
2339 // }
2340 // #endregion
2341 // } break;
2342 // }
2343 // #endregion
2344 // index++;
2345 //}
2346 #endregion
2347
2348 #region reference-code (can be deleted - later)
2349 //ISerializedResult sr = (reader as ISerializedResult);
2350 //if (sr == null)
2351 //{
2352 // throw new ArgumentNullException("sr", string.Format("Unable to cast: '{0}' to '{1}'", reader.GetType().Name, typeof(ISerializedResult).Name));
2353 //}
2354 //int sr_index = 0;
2355 //try
2356 //{
2357 // if (sr.ContainsAddress(obre_CurrentIndex, obre_unsigned, obre_sdt, new Action<int, string>(this.UpdateProgress), out sr_index))
2358 // {
2359 // StructResultType<ulong> result = StructResultType<ulong>.Empty;
2360 // sr.GetResultAtIndex(sr_index, obre_unsigned, obre_sdt, new Action<int, string>(this.UpdateProgress), out result);
2361 // if (Debugger.IsAttached)
2362 // {
2363 // Debugger.Break();
2364 // }
2365 // }
2366 //}
2367 #endregion
2368 }
2369
2370 }
2371 SearchGuid = new_SearchGuid; // update with out new guid
2372 }
2373 }
2374 provider_st.Stop();
2375 logger.Profiler.WriteLine("It took a total of {0} seconds for the memory provider to complete it's operation(s).", provider_st.Elapsed.TotalSeconds);
2376 //if (buffered_mem.Length == 0) { logger.Warn.WriteLine("Buffered Memory is Zero Length."); return; }
2377 //int Last_Whole_Percent_Done = 0;
2378
2379
2380 #region Subsequent Searches
2381 //r_ms.BaseStream.Seek(0, SeekOrigin.Begin);
2382
2383
2384 // hack to help with OutOfMemory Exceptions (OldValue and Equal compare will always add all found search results)
2385 bool NeedToCompare = true;
2386 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue &&
2387 SearchArgs.CompareType == SearchCompareTypes.Equal &&
2388 SearchArgs.IsFirstSearch)
2389 {
2390 NeedToCompare = false;
2391 //second_tmp_Results = null; // Free Memory
2392 }
2393
2394 if (NeedToCompare)
2395 {
2396 #region Ranged Searches
2397 #if !DONOT_HAVE_RANGED_SEARCH_SUPPORT
2398 if (SearchArgs.CompareType == SearchCompareTypes.Between || SearchArgs.CompareType == SearchCompareTypes.NotBetween)
2399 {
2400 st_ranged_search.Start();
2401 object start, end;
2402
2403 start = SearchArgs.CompareStartValue;
2404 end = SearchArgs.CompareEndValue;
2405 using (SearchResultReader reader = new SearchResultReader(SearchGuid))
2406 {
2407 for (int i = 0; i < reader.ResultCount; i += 1)
2408 {
2409 uint address = 0;
2410 #region switch (SearchArgs.DataType)
2411 switch (SearchArgs.DataType)
2412 {
2413 case SearchDataTypes._8bits: if (unsigned) { using (ResultType<byte> result = reader.GetNextResult<byte>()) { address = result.Address; } }
2414 else { using (ResultType<sbyte> result = reader.GetNextResult<sbyte>()) { address = result.Address; } } break;
2415 case SearchDataTypes._16bits: if (unsigned) { using (ResultType<ushort> result = reader.GetNextResult<ushort>()) { address = result.Address; } }
2416 else { using (ResultType<short> result = reader.GetNextResult<short>()) { address = result.Address; } } break;
2417 case SearchDataTypes._32bits: if (unsigned) { using (ResultType<uint> result = reader.GetNextResult<uint>()) { address = result.Address; } }
2418 else { using (ResultType<int> result = reader.GetNextResult<int>()) { address = result.Address; } } break;
2419 case SearchDataTypes._64bits: if (unsigned) { using (ResultType<ulong> result = reader.GetNextResult<ulong>()) { address = result.Address; } }
2420 else { using (ResultType<long> result = reader.GetNextResult<long>()) { address = result.Address; } } break;
2421 }
2422 #endregion
2423
2424 if (MemoryRangeStart > 0 && !SearchArgs.IsFirstSearch) { address = address - MemoryRangeStart; }
2425 //r_ms.BaseStream.Seek(address, SeekOrigin.Begin);
2426 if (SearchArgs.CompareType == SearchCompareTypes.Between)
2427 {
2428 using (InRangeComparer comparer = new InRangeComparer(address, 0))
2429 {
2430 if (comparer.Compare(start, end, SearchArgs.DataType, SearchArgs.IsUnsignedDataType, (IAcceptsProcessAndConfig)this))
2431 {
2432 using (ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value))
2433 {
2434 second_tmp_Results.Add(_tmp_result);
2435 }
2436 }
2437 }
2438 }
2439 else if (SearchArgs.CompareType == SearchCompareTypes.NotBetween)
2440 {
2441 using (NotInRangeComparer comparer = new NotInRangeComparer(address, 0))
2442 {
2443 if (comparer.Compare(start, end, SearchArgs.DataType, SearchArgs.IsUnsignedDataType, (IAcceptsProcessAndConfig)this))
2444 {
2445 using (ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value))
2446 {
2447 second_tmp_Results.Add(_tmp_result);
2448 }
2449 }
2450 }
2451 }
2452 else
2453 {
2454 throw new InvalidOperationException("Encounted unkown range search type: " + SearchArgs.CompareType);
2455 }
2456 double double_percent_done = 100.0 * (double)((double)i / (double)reader.ResultCount);
2457 int int_percent_done = (int)double_percent_done;
2458 if (int_percent_done != Last_Whole_Percent_Done)
2459 {
2460 if (int_percent_done <= 100)
2461 {
2462 resultsprogress.Value = int_percent_done;
2463 resultsprogress.Message = string.Format(" -> Reading Address: 0x{0:x8}", i + MemoryRangeStart);
2464 Last_Whole_Percent_Done = int_percent_done;
2465 }
2466 }
2467 }
2468 }
2469 st_ranged_search.Stop();
2470 logger.Profiler.WriteLine("Ranged search took a total of {0} seconds to complete.", st_ranged_search.Elapsed.TotalSeconds);
2471 }
2472 #endif
2473 #endregion
2474 }
2475 #endregion
2476 //// leave SearchArgs.Results alone, if false
2477 //if (NeedToCompare)
2478 //{
2479 // // fix addresses when memory start is not zero
2480 // 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; } }
2481 // using (SearchResultWriter writer = new SearchResultWriter(second_tmp_Results.Count, SearchGuid))
2482 // {
2483 // for (int i = 0; i < second_tmp_Results.Count; i++)
2484 // {
2485 // switch (sdt)
2486 // {
2487 // case SearchDataTypes._8bits:
2488 // if (unsigned) { writer.WriteResult<Byte>(second_tmp_Results[i].Address, Convert.ToByte(second_tmp_Results[i].Value)); }
2489 // else { writer.WriteResult<SByte>(second_tmp_Results[i].Address, Convert.ToSByte(second_tmp_Results[i].Value)); } break;
2490 // case SearchDataTypes._16bits:
2491 // if (unsigned) { writer.WriteResult<UInt16>(second_tmp_Results[i].Address, Convert.ToUInt16(second_tmp_Results[i].Value)); }
2492 // else { writer.WriteResult<Int16>(second_tmp_Results[i].Address, Convert.ToInt16(second_tmp_Results[i].Value)); } break;
2493 // case SearchDataTypes._32bits:
2494 // if (unsigned) { writer.WriteResult<UInt32>(second_tmp_Results[i].Address, Convert.ToUInt32(second_tmp_Results[i].Value)); }
2495 // else { writer.WriteResult<Int32>(second_tmp_Results[i].Address, Convert.ToInt32(second_tmp_Results[i].Value)); } break;
2496 // case SearchDataTypes._64bits:
2497 // if (unsigned) { writer.WriteResult<UInt64>(second_tmp_Results[i].Address, Convert.ToUInt64(second_tmp_Results[i].Value)); }
2498 // else { writer.WriteResult<Int64>(second_tmp_Results[i].Address, Convert.ToInt64(second_tmp_Results[i].Value)); } break;
2499 // }
2500 // }
2501 // }
2502 // second_tmp_Results = null; // free memory
2503 //}
2504 }
2505 catch (OutOfMemoryException ex)
2506 {
2507 e.Result = true;
2508 logger.Error.WriteLine("Encounted {0} for search: {0}", ex.GetType().Name, SearchGuid.ToString());
2509 logger.VerboseError.WriteLine(ex.ToString());
2510 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);
2511 return;
2512 }
2513 catch (Exception ex)
2514 {
2515 throw ex;
2516 }
2517 }
2518
2519
2520
2521 private void SearchWorkerThread_ProgressChanged(object sender, ProgressChangedEventArgs e)
2522 {
2523 //if (SearchArgs.ProgressLogger != null)
2524 //{
2525 // resultsprogress.Value = e.ProgressPercentage;
2526 // //Application.DoEvents();
2527 //}
2528 }
2529
2530 private void SearchWorkerThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
2531 {
2532 try { if ((bool)e.Result) { logger.Warn.WriteLine("Search operation was cancelled.");
2533 //new Action<int, string>(UpdateProgress).Invoke(0, string.Empty);
2534 UpdateProgress(0, string.Empty);
2535 return; }
2536 }
2537 catch (InvalidCastException) { }
2538 try
2539 {
2540 Stopwatch st = (Stopwatch)e.Result;
2541 st.Stop();
2542 logger.Profiler.WriteLine("Search took {0} seconds, overall, to complete.", st.Elapsed.TotalSeconds);
2543 }
2544 catch (InvalidCastException) { }
2545 catch (Exception ex) { throw ex; }
2546
2547 UpdateProgress(100, String.Empty);
2548 //new Action<int, string>(UpdateProgress).Invoke(100, string.Empty);
2549 bool unsigned = SearchArgs.IsUnsignedDataType;
2550 using (SearchResultReader reader = new SearchResultReader(SearchGuid))
2551 {
2552 logger.Info.WriteLine(string.Format("Found 0x{0:x8} results", reader.ResultCount));
2553
2554 if (reader.ResultCount <= MIN_NUMBER_OF_RESULTS_BEFORE_DISPLAY)
2555 {
2556 lstResults.Items.Clear();
2557 List<ResultItem> items = new List<ResultItem>();
2558 foreach (var k in reader.GetResults(SearchArgs.IsUnsignedDataType, SearchArgs.DataType, new Action<int, string>(UpdateProgress), new Func<bool>(ShouldCancelAnySearchOperation)))
2559 {
2560 switch (SearchArgs.DataType)
2561 {
2562 case SearchDataTypes._8bits:
2563 if (SearchArgs.IsUnsignedDataType) { items.Add(new ResultItem(k.Address, false, Convert.ToByte(k.Value))); }
2564 else { items.Add(new ResultItem(k.Address, false, Convert.ToSByte(k.Value))); }
2565 break;
2566 case SearchDataTypes._16bits:
2567 if (SearchArgs.IsUnsignedDataType) { items.Add(new ResultItem(k.Address, false, Convert.ToUInt16(k.Value))); }
2568 else { items.Add(new ResultItem(k.Address, false, Convert.ToInt16(k.Value))); }
2569 break;
2570 case SearchDataTypes._32bits:
2571 if (SearchArgs.IsUnsignedDataType) { items.Add(new ResultItem(k.Address, false, Convert.ToUInt32(k.Value))); }
2572 else { items.Add(new ResultItem(k.Address, false, Convert.ToInt32(k.Value))); }
2573 break;
2574 case SearchDataTypes._64bits:
2575 if (SearchArgs.IsUnsignedDataType) { items.Add(new ResultItem(k.Address, false, Convert.ToUInt64(k.Value))); }
2576 else { items.Add(new ResultItem(k.Address, false, Convert.ToInt64(k.Value))); }
2577 break;
2578 }
2579 }
2580 lstResults.Items.AddRange(items.ToArray());
2581 }
2582 }
2583
2584 this.DoSearchDoneSpecific();
2585 //System.Threading.Thread.Sleep(100);
2586 //if (_SEARCH_MODE != SearchMode.SEARCH_MODE_NORMAL_WITH_JOKER)
2587 this.ThawResultsUpdate();
2588 Application.DoEvents();
2589 }
2590 private void DoSearchDoneSpecific()
2591 {
2592 SearchWorkerThread.CancelAsync();
2593 if (lstResults.Items.Count > 0) { timer_update_results.Enabled = true; }
2594 else { timer_update_results.Enabled = false; }
2595
2596 search_progress_updater.Enabled = false;
2597
2598 btnCancel.Enabled = false;
2599 btnReset.Enabled = true;
2600 btnSearch.Enabled = true;
2601 grpCompareType.Enabled = true;
2602 grpCompareValue.Enabled = true;
2603 UpdateProgress(0, string.Empty);
2604 //new Action<int, string>(UpdateProgress).Invoke(0, string.Empty);
2605 grpDataType.Enabled = false;
2606 // resume process on reset, incase it was suspended
2607 ThreadControl.ResumeProcess(this.AcceptedProcess.Id);
2608 //Application.DoEvents();
2609 this.Refresh();
2610 }
2611
2612 private void DoCancelSpecific()
2613 {
2614 this.DoSearchDoneSpecific();
2615 }
2616 private void DoResetSpecific()
2617 {
2618 this.DoCancelSpecific();
2619 IsFirstSearch = true;
2620 grpDataType.Enabled = true;
2621 btnReset.Enabled = false;
2622
2623 // delete any temporary search result files
2624 SearchResultWriter.CleanupTemporarySearchResultFiles();
2625
2626 }
2627 private void search_progress_updater_Tick(object sender, EventArgs e)
2628 {
2629 if ((this.AcceptedProcess ==null) || Process.GetProcessById(this.AcceptedProcess.Id) == null)
2630 {
2631 SearchWorkerThread.CancelAsync();
2632 //JokerSearchWorker.CancelAsync();
2633 ResultsUpdateWorkerThread.CancelAsync();
2634 }
2635 }
2636
2637 #region Search Button
2638 private void btnSearch_Click(object sender, EventArgs e)
2639 {
2640 if (this.SearchGuid == Guid.Empty)
2641 this.SearchGuid = Guid.NewGuid();
2642 chkMemoryRangeExpertMode.Enabled = false;
2643 this.SearchInProgess = true;
2644 btnCancel.Enabled = true;
2645 btnReset.Enabled = false; // disable during search
2646 btnSearch.Enabled = false;
2647 this.FreezeResultsUpdate();
2648 this.handle_btnSearch_Click();
2649 }
2650 private void handle_btnSearch_Click()
2651 {
2652 //this.FreezeResultsUpdate();
2653 lstResults.Items.Clear();
2654
2655 if (lstResults.Items.Count > 0) { timer_update_results.Enabled = true; }
2656 else { timer_update_results.Enabled = false; }
2657 UpdateProgress(0, string.Empty);
2658 //new Action<int, string>(UpdateProgress).Invoke(0, string.Empty);
2659 bool _is_unsigned = chkUnsigned.Checked;
2660 SearchType search_type = new SearchType();
2661 SearchDataTypes _data_type = new SearchDataTypes();
2662 SearchCompareTypes _compare_type = new SearchCompareTypes();
2663 CompareValueTypes _compare_value_type = new CompareValueTypes();
2664 object start_value = 0;
2665 object end_value = 0;
2666 // get datatype
2667 if (radio_8bits.Checked) { _data_type = SearchDataTypes._8bits; }
2668 else if (radio_16bits.Checked) { _data_type = SearchDataTypes._16bits; }
2669 else if (radio_32bits.Checked) { _data_type = SearchDataTypes._32bits; }
2670 else if (radio_64bits.Checked) { _data_type = SearchDataTypes._64bits; }
2671 else { logger.Error.WriteLine("Could not determine search data type bit size. (was not 8/16/32/or 64bits)"); }
2672 // get compare type
2673 if (radiocompare_equal.Checked) { _compare_type = SearchCompareTypes.Equal; }
2674 else if (radiocompare_greaterthan.Checked) { _compare_type = SearchCompareTypes.GreaterThan; }
2675 else if (radiocompare_lessthan.Checked) { _compare_type = SearchCompareTypes.LessThan; }
2676 else if (radiocompare_greaterthan_orequal.Checked) { _compare_type = SearchCompareTypes.GreaterThanOrEqual; }
2677 else if (radiocompare_lessthan_orequal.Checked) { _compare_type = SearchCompareTypes.LessThanOrEqual; }
2678 else if (radiocompare_notequal.Checked) { _compare_type = SearchCompareTypes.NotEqual; }
2679 else if (radiocompare_between.Checked) { _compare_type = SearchCompareTypes.Between; }
2680 else if (radiocompare_notbetween.Checked) { _compare_type = SearchCompareTypes.NotBetween; }
2681 else { logger.Error.WriteLine("Could not determine search comparison type. (was not == > < >= <= != <> or !<>)"); }
2682 // get compare valure type
2683 if (radio_oldvalue.Checked) { _compare_value_type = CompareValueTypes.OldValue; }
2684 else if (radio_specificvalue.Checked) { _compare_value_type = CompareValueTypes.SpecificValue; }
2685 else { logger.Error.WriteLine("Could not determine search comparison type. (was not old or specific value"); }
2686
2687 if (_compare_value_type == CompareValueTypes.SpecificValue || (_compare_type == SearchCompareTypes.Between || _compare_type == SearchCompareTypes.NotBetween))
2688 {
2689
2690 switch (_data_type)
2691 {
2692 case SearchDataTypes._8bits:
2693 if (_is_unsigned) { start_value = txtStartAddr.ToByte(); }
2694 else { start_value = txtStartAddr.ToSByte(); }
2695 break;
2696 case SearchDataTypes._16bits:
2697 if (_is_unsigned) { start_value = txtStartAddr.ToUInt16(); }
2698 else { start_value = txtStartAddr.ToInt16(); }
2699 break;
2700 case SearchDataTypes._32bits:
2701 if (_is_unsigned) { start_value = txtStartAddr.ToUInt32(); }
2702 else { start_value = txtStartAddr.ToInt32(); }
2703 break;
2704 case SearchDataTypes._64bits:
2705 if (_is_unsigned) { start_value = txtStartAddr.ToUInt64(); }
2706 else { start_value = txtStartAddr.ToInt64(); }
2707 break;
2708 default: throw new InvalidOperationException("In SearchType(): Encounterd an Unkown Search Data Type.");
2709 }
2710 }
2711 if (_compare_type == SearchCompareTypes.Between || _compare_type == SearchCompareTypes.NotBetween)
2712 {
2713 switch (_data_type)
2714 {
2715 case SearchDataTypes._8bits:
2716 if (_is_unsigned) { end_value = txtEndAddr.ToByte(); }
2717 else { end_value = txtEndAddr.ToSByte(); }
2718 break;
2719 case SearchDataTypes._16bits:
2720 if (_is_unsigned) { end_value = txtEndAddr.ToUInt16(); }
2721 else { end_value = txtEndAddr.ToInt16(); }
2722 break;
2723 case SearchDataTypes._32bits:
2724 if (_is_unsigned) { end_value = txtEndAddr.ToUInt32(); }
2725 else { end_value = txtEndAddr.ToInt32(); }
2726 break;
2727 case SearchDataTypes._64bits:
2728 if (_is_unsigned) { end_value = txtEndAddr.ToUInt64(); }
2729 else { end_value = txtEndAddr.ToInt64(); }
2730 break;
2731 default: throw new InvalidOperationException("In SearchType(): Encounterd an Unkown Search Data Type.");
2732 }
2733 }
2734
2735 search_type = new SearchType(_data_type, _is_unsigned, _compare_type, _compare_value_type, start_value, end_value, resultsprogress);
2736
2737 //search_type.LogSearchOptions();
2738
2739 search_type.IsFirstSearch = IsFirstSearch;
2740
2741
2742
2743 DoSearch(search_type);
2744 IsFirstSearch = false;
2745 }
2746 private void DoSearch(SearchType args)
2747 {
2748 if (!args.IsFirstSearch && SearchArgs != null)
2749 {
2750 //args.Results.AddRange(SearchArgs.Results.ToArray());
2751 //args.Results = SearchArgs.Results;
2752 }
2753 SearchArgs = args;
2754 #if DONOT_HAVE_RANGED_SEARCH_SUPPORT
2755 if (SearchArgs.CompareType == SearchCompareTypes.Between || SearchArgs.CompareType == SearchCompareTypes.NotBetween)
2756 {
2757 throw new NotImplementedException("Between and Not Between Range searches have not been implemented.");
2758 }
2759 #endif
2760 search_progress_updater.Enabled = true;
2761 //padPluginSelector.Enabled = false;
2762 //gsPluginSelector.Enabled = false;
2763 btnReset.Enabled = false; // this will be disabled until the earch is finished
2764 btnSearch.Enabled = false;
2765 btnCancel.Enabled = true;
2766 grpDataType.Enabled = false;
2767 grpCompareType.Enabled = false;
2768 grpCompareValue.Enabled = false;
2769 this.Refresh();
2770 Application.DoEvents();
2771 SearchWorkerThread.RunWorkerAsync();
2772 }
2773 #endregion
2774 private void btnReset_Click(object sender, EventArgs e)
2775 {
2776 this.SearchGuid = Guid.Empty;
2777 this.SearchInProgess = false;
2778 chkMemoryRangeExpertMode.Enabled = true;
2779 btnSearch.Enabled = true;
2780 btnReset.Enabled = false;
2781 btnCancel.Enabled = false;
2782 this.DoResetSpecific();
2783 lstResults.Items.Clear();
2784 //try { SearchArgs.Results = new List<ResultType<object>>(); }
2785 //catch { }
2786 }
2787
2788 private void btnCancel_Click(object sender, EventArgs e)
2789 {
2790 this.SearchInProgess = false;
2791 btnCancel.Enabled = false;
2792 btnSearch.Enabled = true;
2793 btnReset.Enabled = true;
2794 this.DoCancelSpecific();
2795 }
2796
2797 private void mnuItemPatchListViewMemoryRegion_Click(object sender, EventArgs e)
2798 {
2799 List<ResultDataType> patch_list = new List<ResultDataType>();
2800 List<int> SelectedIndexes = new List<int>();
2801 foreach (int index in lstPatchList.SelectedIndices) { SelectedIndexes.Add(index); }
2802 foreach (int index in SelectedIndexes)
2803 {
2804 ListViewItem item = lstPatchList.Items[index];
2805 ResultDataType rdt = (ResultDataType)item.Tag;
2806 ViewMemoryRegion(rdt);
2807 break; // only get the fist item
2808 }
2809 }
2810
2811 private void mnuItemResultsListViewMemoryRegion_Click(object sender, EventArgs e)
2812 {
2813 List<ResultDataType> patch_list = new List<ResultDataType>();
2814 List<int> SelectedIndexes = new List<int>();
2815 foreach (int index in lstResults.SelectedIndices) { SelectedIndexes.Add(index); }
2816 foreach (int index in SelectedIndexes)
2817 {
2818 ListViewItem item = lstResults.Items[index];
2819 ResultDataType rdt = (ResultDataType)item.Tag;
2820 ViewMemoryRegion(rdt);
2821 break; // only get the fist item
2822 }
2823 }
2824 private void ViewMemoryRegion(ResultDataType rdt)
2825 {
2826 if (OnBrowseMemoryRegion != null)
2827 OnBrowseMemoryRegion(new BrowseMemoryRegionEvent(this, rdt.Address));
2828 }
2829
2830 private void mnuAddedResults_Opening(object sender, CancelEventArgs e)
2831 {
2832 if (!(lstPatchList.Items.Count > 0)) { mnuItemRemoveResult.Visible = false; e.Cancel = true; }
2833 if (!(lstPatchList.Items.Count > 0)) { mnuItemPatchSelectedEntry.Visible = false; e.Cancel = true; }
2834 if (e.Cancel) return;
2835 if (lstPatchList.Items.Count > 0) mnuItemRemoveResult.Visible = true;
2836 if (lstPatchList.Items.Count > 0) mnuItemPatchSelectedEntry.Visible = true;
2837
2838 if (!(lstPatchList.Items.Count > 0)) { mnuItemFreezeSelectedPatches.Visible = false; e.Cancel = true; }
2839 if (!(lstPatchList.Items.Count > 0)) { mnuItemThawSelectedPatches.Visible = false; e.Cancel = true; }
2840 if (e.Cancel) return;
2841
2842 if (lstPatchList.Items.Count > 0) mnuItemFreezeSelectedPatches.Visible = true;
2843 if (lstPatchList.Items.Count > 0) mnuItemThawSelectedPatches.Visible = true;
2844
2845 if (lstPatchList.SelectedItems.Count == 0) e.Cancel = true;
2846 if (e.Cancel) return;
2847
2848 }
2849
2850 private void mnuResults_Opening(object sender, CancelEventArgs e)
2851 {
2852 if (!(lstResults.Items.Count > 0)) e.Cancel = true;
2853 if (lstResults.SelectedItems.Count == 0) e.Cancel = true;
2854 if (SearchArgs == null) e.Cancel = true;
2855 if (e.Cancel) return;
2856 }
2857
2858 private void chkMemoryRangeExpertMode_CheckedChanged(object sender, EventArgs e)
2859 {
2860 txtMemoryRangeStart.ReadOnly = !chkMemoryRangeExpertMode.Checked;
2861 txtMemoryRangeSize.ReadOnly = !chkMemoryRangeExpertMode.Checked;
2862 }
2863
2864 private void txtMemoryRangeStart_ValueChanged(object sender, ValueChangedEventArgs e) { this.MemoryRangeStart = Convert.ToUInt32(e.NewValue); }
2865 private void txtMemoryRangeSize_ValueChanged(object sender, ValueChangedEventArgs e) { this.MemoryRangeSize = Convert.ToUInt32(e.NewValue); }
2866
2867 private void FloatingMemorySearcher_Load(object sender, EventArgs e)
2868 {
2869 #if DONOT_HAVE_RANGED_SEARCH_SUPPORT
2870 radiocompare_between.Enabled = false;
2871 radiocompare_notbetween.Enabled = false;
2872
2873 #endif
2874 }
2875
2876 }
2877 }

  ViewVC Help
Powered by ViewVC 1.1.22