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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22