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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22