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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 546 - (show annotations) (download)
Wed Jun 5 23:12:01 2013 UTC (7 years, 4 months ago) by william
File size: 175739 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22