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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 516 - (show annotations) (download)
Wed Jun 5 03:23:27 2013 UTC (7 years, 4 months ago) by william
File size: 177605 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22