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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22