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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 281 - (show annotations) (download)
Tue Jun 5 00:19:36 2012 UTC (8 years, 4 months ago) by william
File size: 81730 byte(s)
+ trap OutOfMemoryException during search

1 #define USE_FORCED_LOGGING_CONTROL // when defined, will override and force an internal logging control
2 //#define DONOT_HAVE_RANGED_SEARCH_SUPPORT // when defined, indicates that ranged searches have not been implemented
3
4 #define INCREASE_NUMBER_OF_RESULTS_BEFORE_DISPLAY // when defined will set MIN RESULTS to 0x2701 otherwise 0x03e8
5
6 //#define DO_NOT_SUSPEND_RESUME_THREAD_ON_FREEZE // when defined will not freeze/resume thread on freeze
7 using System;
8 using System.Collections.Generic;
9 using System.ComponentModel;
10 using System.Data;
11 using System.Drawing;
12 using System.Linq;
13 using System.Text;
14 using System.Windows.Forms;
15 using WeifenLuo.WinFormsUI.Docking;
16 using RomCheater.PluginFramework.Interfaces;
17 using System.Diagnostics;
18 using RomCheater.Docking.MemorySearch;
19 using libWin32.Win32.Threading;
20 using System.Threading;
21 using RomCheater.Logging;
22 using System.IO;
23 using Sojaner.MemoryScanner.MemoryProviers;
24 using RomCheater.PluginFramework.Events;
25
26 namespace RomCheater.Docking
27 {
28 public partial class FloatingMemorySearcher : DockContent,
29 IAcceptsPlugin<IConfigPlugin>,
30 IAcceptsProcess<Process>,
31 IAcceptsProcessAndConfig,
32 ISearchInProgress,
33 IAcceptsBrowseMemoryRegion
34 {
35 #if INCREASE_NUMBER_OF_RESULTS_BEFORE_DISPLAY
36 const int MIN_NUMBER_OF_RESULTS_BEFORE_DISPLAY = 0x2701; // 10,000 results
37 #else
38 const int MIN_NUMBER_OF_RESULTS_BEFORE_DISPLAY = 0x03e8; // 1,000 results
39 #endif
40
41 private bool DefaultUnsignedState = true; // set unsigned to true
42 public FloatingMemorySearcher() { InitializeComponent(); this.AcceptedPlugin = null; this.AcceptedProcess = null; SearchInProgess = false; Reload(); }
43 public FloatingMemorySearcher(IConfigPlugin config) : this() { this.AcceptedPlugin = config; }
44 public FloatingMemorySearcher(IConfigPlugin config, Process process) : this() { this.AcceptedPlugin = config; this.AcceptedProcess = process; }
45
46 #region IAcceptsProcess<Process> Members
47 private Process _AcceptedProcess;
48 public Process AcceptedProcess { get { return _AcceptedProcess; } set { _AcceptedProcess = value; UpdateAcceptedProcessAndConfig(AcceptedPlugin, value); } }
49 #endregion
50 #region IAcceptsPlugin<IConfigPlugin> Members
51 private IConfigPlugin _AcceptedPlugin;
52 public IConfigPlugin AcceptedPlugin { get { return _AcceptedPlugin; } set { _AcceptedPlugin = value; UpdateAcceptedProcessAndConfig(value, AcceptedProcess); } }
53 #endregion
54 #region IAcceptsBrowseMemoryRegion members
55 public BaseEventHandler<BrowseMemoryRegionEvent> OnBrowseMemoryRegion { get; set; }
56 #endregion
57
58 private void UpdateAcceptedProcessAndConfig(IConfigPlugin config, Process process)
59 {
60 this.lstResults.AcceptedPlugin = config;
61 this.lstResults.AcceptedProcess = process;
62 this.lstPatchList.AcceptedPlugin = config;
63 this.lstPatchList.AcceptedProcess = process;
64 }
65 #region ISearchInProgress members
66 public bool SearchInProgess { get; private set; }
67 #endregion
68
69
70 public void Reload()
71 {
72 chkUnsigned.Checked = DefaultUnsignedState;
73 radio_8bits.Checked = true;
74 radiocompare_equal.Checked = true;
75 radio_oldvalue.Checked = true;
76 chkRefreshResults.Checked = true;
77 }
78 public enum eListViewResults
79 {
80 SEARCH_RESULTS_LIST = 0x3000,
81 PATCH_RESULTS_LIST = 0x3001,
82 UKNOWN_RESULTS_LIST = 0x3001
83 }
84 bool IsFirstSearch = true;
85 SearchType SearchArgs;
86 static int col_Found_Address = 1;
87 static int col_Found_Value = 2;
88 static int col_Found_Frozen = 3;
89 static int col_Added_Address = 1;
90 static int col_Added_Value = 2;
91 static int col_Added_Frozen = 3;
92 List<ListViewItem> ResultItems = new List<ListViewItem>();
93 List<ListViewItem> AddedItems = new List<ListViewItem>();
94 private bool _PatchedValue_NeedsUpdate;
95 bool PatchedValue_NeedsUpdate
96 {
97 get { if (_PatchedValue_NeedsUpdate) this.ThawResultsUpdate(); return _PatchedValue_NeedsUpdate; }
98 set { _PatchedValue_NeedsUpdate = value; if (value) this.ThawResultsUpdate(); }
99 }
100 private delegate ListViewItem ThreadSafe_GetResultItem(int index, int lv_type);
101 private ListViewItem GetResultItem(int index, int lv_type)
102 {
103 try
104 {
105 AddressValuePairList lv = null;
106 switch (lv_type)
107 {
108 case (int)eListViewResults.SEARCH_RESULTS_LIST: lv = lstResults; break;
109 case (int)eListViewResults.PATCH_RESULTS_LIST: lv = lstPatchList; break;
110 default: throw new IndexOutOfRangeException("Detected: " + Enum.GetName(typeof(eListViewResults), eListViewResults.UKNOWN_RESULTS_LIST) + " with value: " + lv_type.ToString("x4"));
111 }
112 ListViewItem item = new ListViewItem();
113 item = (ListViewItem)lv.Items[index].Clone();
114 return item;
115 }
116 catch (Exception)
117 {
118 return null;
119 }
120 }
121 private void radiocompare_equal_CheckedChanged(object sender, EventArgs e)
122 {
123 //if (!radiocompare_between.Checked && !radiocompare_notbetween.Checked)
124 //{
125 if (radio_oldvalue.Checked)
126 {
127 txtStartAddr.ReadOnly = true;
128 txtEndAddr.ReadOnly = true;
129 }
130 if (radio_specificvalue.Checked)
131 {
132 txtStartAddr.ReadOnly = false;
133 txtEndAddr.ReadOnly = true;
134 }
135 //}
136 }
137
138 private void radiocompare_between_CheckedChanged(object sender, EventArgs e)
139 {
140 if (!radiocompare_equal.Checked &&
141 !radiocompare_greaterthan.Checked &&
142 !radiocompare_greaterthan.Checked &&
143 !radiocompare_lessthan.Checked &&
144 !radiocompare_greaterthan_orequal.Checked &&
145 !radiocompare_lessthan_orequal.Checked &&
146 !radiocompare_notequal.Checked)
147 if (radiocompare_between.Checked)
148 {
149 txtStartAddr.ReadOnly = false;
150 txtEndAddr.ReadOnly = false;
151 return;
152 }
153 if (!radiocompare_between.Checked && !radiocompare_notbetween.Checked)
154 {
155 if (radio_oldvalue.Checked)
156 {
157 txtStartAddr.ReadOnly = true;
158 txtEndAddr.ReadOnly = true;
159 }
160 if (radio_specificvalue.Checked)
161 {
162 txtStartAddr.ReadOnly = false;
163 txtEndAddr.ReadOnly = true;
164 }
165 }
166 }
167
168 private void radiocompare_notbetween_CheckedChanged(object sender, EventArgs e)
169 {
170 if (!radiocompare_equal.Checked &&
171 !radiocompare_greaterthan.Checked &&
172 !radiocompare_greaterthan.Checked &&
173 !radiocompare_lessthan.Checked &&
174 !radiocompare_greaterthan_orequal.Checked &&
175 !radiocompare_lessthan_orequal.Checked &&
176 !radiocompare_notequal.Checked)
177 if (radiocompare_notbetween.Checked)
178 {
179 txtStartAddr.ReadOnly = false;
180 txtEndAddr.ReadOnly = false;
181 return;
182 }
183 if (!radiocompare_between.Checked && !radiocompare_notbetween.Checked)
184 {
185 if (radio_oldvalue.Checked)
186 {
187 txtStartAddr.ReadOnly = true;
188 txtEndAddr.ReadOnly = true;
189 }
190 if (radio_specificvalue.Checked)
191 {
192 txtStartAddr.ReadOnly = false;
193 txtEndAddr.ReadOnly = true;
194 }
195 }
196 }
197
198 private void radio_8bits_CheckedChanged(object sender, EventArgs e)
199 {
200 if (chkUnsigned.Checked)
201 {
202 txtStartAddr.CreateTypeSize<byte>();
203 txtEndAddr.CreateTypeSize<byte>();
204 }
205 else
206 {
207 txtStartAddr.CreateTypeSize<sbyte>();
208 txtEndAddr.CreateTypeSize<sbyte>();
209 }
210 }
211
212 private void radio_16bits_CheckedChanged(object sender, EventArgs e)
213 {
214 if (chkUnsigned.Checked)
215 {
216 txtStartAddr.CreateTypeSize<ushort>();
217 txtEndAddr.CreateTypeSize<ushort>();
218 }
219 else
220 {
221 txtStartAddr.CreateTypeSize<short>();
222 txtEndAddr.CreateTypeSize<short>();
223 }
224 }
225
226 private void radio_32bits_CheckedChanged(object sender, EventArgs e)
227 {
228
229 if (chkUnsigned.Checked)
230 {
231 txtStartAddr.CreateTypeSize<uint>();
232 txtEndAddr.CreateTypeSize<uint>();
233 }
234 else
235 {
236 txtStartAddr.CreateTypeSize<int>();
237 txtEndAddr.CreateTypeSize<int>();
238 }
239 }
240
241 private void radio_64bits_CheckedChanged(object sender, EventArgs e)
242 {
243
244 if (chkUnsigned.Checked)
245 {
246 txtStartAddr.CreateTypeSize<ulong>();
247 txtEndAddr.CreateTypeSize<ulong>();
248 }
249 else
250 {
251 txtStartAddr.CreateTypeSize<long>();
252 txtEndAddr.CreateTypeSize<long>();
253 }
254 }
255
256 private void radio_oldvalue_CheckedChanged(object sender, EventArgs e)
257 {
258 if (!radiocompare_between.Checked && !radiocompare_notbetween.Checked)
259 {
260 txtStartAddr.ReadOnly = true;
261 txtEndAddr.ReadOnly = true;
262 }
263 }
264
265 private void radio_specificvalue_CheckedChanged(object sender, EventArgs e)
266 {
267 if (!radiocompare_between.Checked && !radiocompare_notbetween.Checked)
268 {
269 txtStartAddr.ReadOnly = false;
270 txtEndAddr.ReadOnly = true;
271 }
272 }
273
274 private void chkRefreshResults_CheckedChanged(object sender, EventArgs e)
275 {
276 if (chkRefreshResults.Checked)
277 {
278 timer_update_results.Enabled = true;
279 }
280 else
281 {
282 timer_update_results.Enabled = false;
283 ResultsUpdateWorkerThread.CancelAsync();
284 }
285 }
286
287 private void timer_update_results_Tick(object sender, EventArgs e)
288 {
289 if (chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
290 {
291 ResultsUpdateWorkerThread.RunWorkerAsync();
292 }
293 }
294 private bool ShouldUpdateResults()
295 {
296 if (this.AcceptedProcess == null) return false;
297 if (SearchWorkerThread.IsBusy) return false;
298 //if (JokerSearchWorker.IsBusy) return false;
299 if (this.IsResultsUpdateFrozen) return false;
300 if (mnuAddedResults.Visible) return false;
301 if (mnuResults.Visible) return false;
302 if (Process.GetProcessById(this.AcceptedProcess.Id) == null) return false;
303 if (lstResults.Items.Count > 0) return true;
304 if (lstPatchList.Items.Count > 0) return true;
305 return false;
306 }
307 private void ResultsUpdateWorkerThread_DoWork(object sender, DoWorkEventArgs e)
308 {
309 Thread.Sleep(250); // keep thread from blocking
310 if (!this.ShouldUpdateResults()) return;
311 ////if (SearchArgs == null) return;
312 ////if (this.IsResultsUpdateFrozen) return;
313 ////// put thread to sleep for 500ms
314 ////System.Threading.Thread.Sleep(500);
315 //PCSX2MemoryProvider provider = new PCSX2MemoryProvider(this.SearchPCSX2ProcessPID, resultslog);
316 //byte[] buffered_mem = provider.GetMemory();
317 //MemoryStream ms = new MemoryStream(buffered_mem);
318 //BinaryReader r_ms = new BinaryReader(ms);
319
320 #region Update Results List
321 ResultItems = new List<ListViewItem>();
322 //r_ms.BaseStream.Seek(0, SeekOrigin.Begin);
323 for (int i = 0; i < lstResults.Items.Count; i++)
324 {
325 if (this.lstResults.InvokeRequired)
326 {
327 ThreadSafe_GetResultItem _get_item = new ThreadSafe_GetResultItem(GetResultItem);
328 object item = this.lstResults.Invoke(_get_item, new object[] { i, (int)eListViewResults.SEARCH_RESULTS_LIST });
329 if (item != null)
330 ResultItems.Add((ListViewItem)item);
331 }
332 else
333 {
334 ResultItems.Add(lstResults.Items[i]);
335 }
336
337 }
338 for (int i = 0; i < ResultItems.Count; i++)
339 {
340 if (ResultsUpdateWorkerThread.CancellationPending == true)
341 {
342 e.Cancel = true;
343 return;
344 }
345 int Address = 0;
346 ResultDataType _result = (ResultDataType)ResultItems[i].Tag;
347
348 Address = Convert.ToInt32(ResultItems[i].SubItems[col_Found_Address].Text, 16);
349 //r_ms.BaseStream.Seek(Address, SeekOrigin.Begin);
350 GenericMemoryProvider provider = new GenericMemoryProvider((IAcceptsProcessAndConfig)this);
351 provider.OpenProvider();
352 int bytesReadSize;
353 byte[] data;
354 uint bytesToRead = 0;
355 switch (_result.ValueType)
356 {
357 case SearchDataTypes._8bits:
358 bytesToRead = 1;
359 break;
360 case SearchDataTypes._16bits:
361 bytesToRead = 2;
362 break;
363 case SearchDataTypes._32bits:
364 bytesToRead = 4;
365 break;
366 case SearchDataTypes._64bits:
367 bytesToRead = 8;
368 break;
369 }
370 provider.ReadProcessMemory(Address, bytesToRead, out bytesReadSize, out data);
371 MemoryStream ms = new MemoryStream(data);
372 BinaryReader r_ms = new BinaryReader(ms);
373 switch (_result.ValueType)
374 {
375 case SearchDataTypes._8bits:
376 if (_result.IsUnsigned) { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x2}", r_ms.ReadByte()); }
377 else { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x2}", r_ms.ReadSByte()); }
378 break;
379 case SearchDataTypes._16bits:
380 if (_result.IsUnsigned) { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x4}", r_ms.ReadUInt16()); }
381 else { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x4}", r_ms.ReadInt16()); }
382 break;
383 case SearchDataTypes._32bits:
384 if (_result.IsUnsigned) { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x8}", r_ms.ReadUInt32()); }
385 else { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x8}", r_ms.ReadInt32()); }
386 break;
387 case SearchDataTypes._64bits:
388 if (_result.IsUnsigned) { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x16}", r_ms.ReadUInt64()); }
389 else { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x16}", r_ms.ReadInt64()); }
390 break;
391 }
392 r_ms.Close();
393 provider.CloseProvider();
394 Application.DoEvents();
395 }
396 #endregion
397
398 #region Update Added Results List
399 AddedItems = new List<ListViewItem>();
400 //r_ms.BaseStream.Seek(0, SeekOrigin.Begin);
401 for (int i = 0; i < lstPatchList.Items.Count; i++)
402 {
403 if (this.lstResults.InvokeRequired)
404 {
405 ThreadSafe_GetResultItem _get_item = new ThreadSafe_GetResultItem(GetResultItem);
406 object item = this.lstResults.Invoke(_get_item, new object[] { i, (int)eListViewResults.PATCH_RESULTS_LIST });
407 if (item != null)
408 AddedItems.Add((ListViewItem)item);
409 }
410 else
411 {
412 AddedItems.Add(lstPatchList.Items[i]);
413 }
414
415 }
416 for (int i = 0; i < AddedItems.Count; i++)
417 {
418 if (ResultsUpdateWorkerThread.CancellationPending == true)
419 {
420 e.Cancel = true;
421 return;
422 }
423 int Address = 0;
424 ResultDataType _result = (ResultDataType)AddedItems[i].Tag;
425 Address = Convert.ToInt32(AddedItems[i].SubItems[col_Added_Address].Text, 16);
426 GenericMemoryProvider provider = new GenericMemoryProvider((IAcceptsProcessAndConfig)this);
427 provider.OpenProvider();
428 int bytesReadSize;
429 byte[] data;
430 uint bytesToRead = 0;
431 switch (_result.ValueType)
432 {
433 case SearchDataTypes._8bits:
434 bytesToRead = 1;
435 break;
436 case SearchDataTypes._16bits:
437 bytesToRead = 2;
438 break;
439 case SearchDataTypes._32bits:
440 bytesToRead = 4;
441 break;
442 case SearchDataTypes._64bits:
443 bytesToRead = 8;
444 break;
445 }
446 provider.ReadProcessMemory(Address, bytesToRead, out bytesReadSize, out data);
447 MemoryStream ms = new MemoryStream(data);
448 BinaryReader r_ms = new BinaryReader(ms);
449 switch (_result.ValueType)
450 {
451 case SearchDataTypes._8bits:
452 if (_result.IsUnsigned) { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x2}", r_ms.ReadByte()); }
453 else { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x2}", r_ms.ReadSByte()); }
454 break;
455 case SearchDataTypes._16bits:
456 if (_result.IsUnsigned) { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x4}", r_ms.ReadUInt16()); }
457 else { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x4}", r_ms.ReadInt16()); }
458 break;
459 case SearchDataTypes._32bits:
460 if (_result.IsUnsigned) { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x8}", r_ms.ReadUInt32()); }
461 else { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x8}", r_ms.ReadInt32()); }
462 break;
463 case SearchDataTypes._64bits:
464 if (_result.IsUnsigned) { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x16}", r_ms.ReadUInt64()); }
465 else { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x16}", r_ms.ReadInt64()); }
466 break;
467 }
468 r_ms.Close();
469 provider.CloseProvider();
470 Application.DoEvents();
471 }
472 #endregion
473
474
475 }
476
477 private void ResultsUpdateWorkerThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
478 {
479 try
480 {
481 //if ((lstResults.SelectedItems.Count > 0) && !PatchedValue_NeedsUpdate ) return;
482 //if ((lstPatchList.SelectedItems.Count > 0) && !PatchedValue_NeedsUpdate) return;
483 if (!this.ShouldUpdateResults()) return;
484 if (ResultItems.Count > 0)
485 {
486 //lstResults.Items.Clear();
487 //lstResults.Items.AddRange(ResultItems.ToArray());
488
489 for (int i = 0; i < ResultItems.Count; i++)
490 {
491 lstResults.Items[i].SubItems[new AVPColumnText(AVPColumnType.VALUE).ColumnIndex].Text =
492 ResultItems[i].SubItems[new AVPColumnText(AVPColumnType.VALUE).ColumnIndex].Text;
493 }
494
495 }
496 if (AddedItems.Count > 0)
497 {
498 //lstPatchList.Items.Clear();
499 //lstPatchList.Items.AddRange(AddedItems.ToArray());
500
501 for (int i = 0; i < AddedItems.Count; i++)
502 {
503 lstPatchList.Items[i].SubItems[new AVPColumnText(AVPColumnType.VALUE).ColumnIndex].Text =
504 AddedItems[i].SubItems[new AVPColumnText(AVPColumnType.VALUE).ColumnIndex].Text;
505 }
506
507 }
508 PatchedValue_NeedsUpdate = false;
509 }
510 catch { }
511 }
512
513 private void btnImportFile_Click(object sender, EventArgs e)
514 {
515 this.FreezeResultsUpdate();
516 if (!lstPatchList.ImportFromFile())
517 {
518 MessageBox.Show("Failed to Import Patch List from File.", "Import Failure", MessageBoxButtons.OK, MessageBoxIcon.Error);
519 this.ThawResultsUpdate();
520 return;
521 }
522 else
523 {
524 MessageBox.Show("Succesfully Imported Patch List from File.", "Import Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
525 this.ThawResultsUpdate();
526 return;
527 }
528 }
529 bool g_isFrozen = false;
530 private bool IsResultsUpdateFrozen
531 {
532 get { return g_isFrozen; }
533 set { g_isFrozen = value; }
534 }
535 private void ThawResultsUpdate()
536 {
537 this.IsResultsUpdateFrozen = false;
538 if (this.AcceptedProcess != null)
539 {
540 #if !DO_NOT_SUSPEND_RESUME_THREAD_ON_FREEZE
541 ThreadControl.ResumeProcess(this.AcceptedProcess.Id);
542 #endif
543 }
544 }
545
546 private void FreezeResultsUpdate()
547 {
548 this.IsResultsUpdateFrozen = true;
549 this.IsResultsUpdateFrozen = false;
550 if (this.AcceptedProcess != null)
551 {
552 #if !DO_NOT_SUSPEND_RESUME_THREAD_ON_FREEZE
553 ThreadControl.SuspendProcess(this.AcceptedProcess.Id);
554 #endif
555 }
556 }
557
558 private void btnExportFile_Click(object sender, EventArgs e)
559 {
560 this.FreezeResultsUpdate();
561 if (!lstPatchList.ExportToFile())
562 {
563 MessageBox.Show("Failed to Export Patch List to File.", "Export Failure", MessageBoxButtons.OK, MessageBoxIcon.Error);
564 this.ThawResultsUpdate();
565 return;
566 }
567 else
568 {
569 MessageBox.Show("Succesfully Exported Patch List to File.", "Export Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
570 this.ThawResultsUpdate();
571 return;
572 }
573 }
574
575 private void btnImportClipboard_Click(object sender, EventArgs e)
576 {
577 this.FreezeResultsUpdate();
578 if (!lstPatchList.ImportFromClipboard())
579 {
580 MessageBox.Show("Failed to Import Patch List from Clipboard.", "Import Failure", MessageBoxButtons.OK, MessageBoxIcon.Error);
581 this.ThawResultsUpdate();
582 return;
583 }
584 else
585 {
586 MessageBox.Show("Succesfully Import Patch List from Clipboard.", "Import Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
587 this.ThawResultsUpdate();
588 }
589 }
590
591 private void btnExportClipboard_Click(object sender, EventArgs e)
592 {
593 this.FreezeResultsUpdate();
594 if (!lstPatchList.ExportToClipboard())
595 {
596 MessageBox.Show("Failed to Export Patch List to Clipboard.", "Export Failure", MessageBoxButtons.OK, MessageBoxIcon.Error);
597 this.ThawResultsUpdate();
598 return;
599 }
600 else
601 {
602 MessageBox.Show("Succesfully Exported Patch List to Clipboard.", "Export Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
603 this.ThawResultsUpdate();
604 return;
605 }
606 }
607
608 private void btnAddPatchAddress_Click(object sender, EventArgs e)
609 {
610 PatchAdder adder = new PatchAdder((IAcceptsProcessAndConfig)this);
611 adder.ShowDialog();
612 if (adder.WasAPatchAdded) AddToPatchList(adder.AddedPatchValue);
613 }
614
615 private void btnAddAddressRange_Click(object sender, EventArgs e)
616 {
617 PatchRangeAdder adder = new PatchRangeAdder((IAcceptsProcessAndConfig)this);
618 adder.ShowDialog();
619 if (adder.WasAPatchAdded) AddToPatchList(adder.AddedPatchValue);
620 }
621 private void AddToPatchList(List<ResultDataType> item) { foreach (ResultDataType data in item) { AddToPatchList(data); } }
622 private void AddToPatchList(ResultDataType item)
623 {
624 ResultItem item2 = null;
625 switch (item.ValueType)
626 {
627 case SearchDataTypes._8bits:
628 if (item.IsUnsigned) { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToByte(item.Value)); }
629 else { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToSByte(item.Value)); }
630 break;
631 case SearchDataTypes._16bits:
632 if (item.IsUnsigned) { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToUInt16(item.Value)); }
633 else { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToInt16(item.Value)); }
634 break;
635 case SearchDataTypes._32bits:
636 if (item.IsUnsigned) { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToUInt32(item.Value)); }
637 else { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToInt32(item.Value)); }
638 break;
639 case SearchDataTypes._64bits:
640 if (item.IsUnsigned) { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToUInt64(item.Value)); }
641 else { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToInt64(item.Value)); }
642 break;
643 }
644 this.AddToPatchList(item2);
645 }
646 private void AddToPatchList(ListViewItem item)
647 {
648 try
649 {
650 ResultDataType _result = (ResultDataType)item.Tag;
651 this.AddToPatchList(_result);
652 }
653 catch (InvalidCastException ex)
654 {
655 // unable to cast
656 MessageBox.Show(ex.Message, "Invalid Cast Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
657 }
658 catch (Exception ex)
659 {
660 // other exception
661 MessageBox.Show(ex.Message, "Unhandled Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
662 }
663 }
664 private void AddToPatchList(ResultItem item)
665 {
666 if (!lstPatchList.Items.Contains(item)) lstPatchList.Items.Add(item);
667 }
668 private void AddToPatchList(string address, SearchDataTypes bitsize, bool IsUnsigned)
669 {
670 ResultItemState state = new ResultItemState(address, bitsize, IsUnsigned, (IAcceptsProcessAndConfig)this);
671 ResultItem item = new ResultItem(state.Address, state.Value, state.Frozen, state.ValueType, state.IsUnsigned);
672 this.AddToPatchList(item);
673 }
674
675 private void mnuItemAddToPatchList_Click(object sender, EventArgs e)
676 {
677 if (!(lstResults.SelectedItems.Count > 0)) return;
678 //if (SearchArgs == null) return;
679
680 try
681 {
682 for (int i = 0; i < lstResults.SelectedIndices.Count; i++)
683 {
684 //ResultDataType result = (ResultDataType)lstResults.Items[selected_index].Tag;
685 ListViewItem item = lstResults.Items[lstResults.SelectedIndices[i]];
686 this.AddToPatchList(item);
687 }
688 }
689 catch (Exception ex)
690 {
691 logger.Error.WriteLine(ex.ToString());
692 }
693 }
694
695 private void mnuItemRemoveResult_Click(object sender, EventArgs e)
696 {
697 if (!(lstPatchList.SelectedItems.Count > 0)) return;
698 //if (SearchArgs == null) return;
699 try
700 {
701 this.FreezeResultsUpdate();
702 for (int i = 0; i < lstPatchList.SelectedIndices.Count; i++)
703 {
704 //lstPatchList.ThawItem(lstPatchList.SelectedIndices[i]);
705 lstPatchList.Items[lstPatchList.SelectedIndices[i]].Remove();
706 }
707 this.ThawResultsUpdate();
708 }
709 catch (Exception ex)
710 {
711 Debug.WriteLine(ex.ToString());
712 }
713 }
714 private void PatchRange(bool SingleEntry)
715 {
716 //if (SearchArgs == null) return;
717 #region Patch Selected Address
718 // stop ResultsUpdate Thread
719 ResultsUpdateWorkerThread.CancelAsync();
720
721 List<ResultDataType> patch_list = new List<ResultDataType>();
722 List<int> SelectedIndexes = new List<int>();
723
724 if (SingleEntry)
725 {
726 SelectedIndexes.Add(lstPatchList.SelectedIndices[0]);
727 }
728 else
729 {
730 foreach (int index in lstPatchList.SelectedIndices)
731 {
732 SelectedIndexes.Add(index);
733 }
734 }
735 //PCSX2MemoryProvider provider = new PCSX2MemoryProvider(this.SearchPCSX2ProcessPID, resultslog);
736 foreach (int index in SelectedIndexes)
737 {
738 if (SingleEntry)
739 {
740 SearchPatcher patcher = null;
741 uint Address = 0;
742 ListViewItem item = lstPatchList.Items[index];
743 ResultDataType _result = (ResultDataType)item.Tag;
744 Address = Convert.ToUInt32(item.SubItems[col_Found_Address].Text, 16);
745 switch (_result.ValueType)
746 {
747 case SearchDataTypes._8bits:
748 if (_result.IsUnsigned)
749 {
750 byte value = Convert.ToByte(item.SubItems[col_Found_Value].Text, 16);
751 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
752 timer_update_results.Enabled = false;
753 patcher.ShowDialog();
754 timer_update_results.Enabled = true;
755 PatchedValue_NeedsUpdate = true;
756 if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
757 ResultsUpdateWorkerThread.RunWorkerAsync();
758 }
759 else
760 {
761 sbyte value = Convert.ToSByte(item.SubItems[col_Found_Value].Text, 16);
762 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
763 timer_update_results.Enabled = false;
764 patcher.ShowDialog();
765 timer_update_results.Enabled = true;
766 PatchedValue_NeedsUpdate = true;
767 if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
768 ResultsUpdateWorkerThread.RunWorkerAsync();
769 }
770 break;
771 case SearchDataTypes._16bits:
772 if (_result.IsUnsigned)
773 {
774 ushort value = Convert.ToUInt16(item.SubItems[col_Found_Value].Text, 16);
775 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
776 timer_update_results.Enabled = false;
777 patcher.ShowDialog();
778 timer_update_results.Enabled = true;
779 PatchedValue_NeedsUpdate = true;
780 if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
781 ResultsUpdateWorkerThread.RunWorkerAsync();
782 }
783 else
784 {
785 short value = Convert.ToInt16(item.SubItems[col_Found_Value].Text, 16);
786 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
787 timer_update_results.Enabled = false;
788 patcher.ShowDialog();
789 timer_update_results.Enabled = true;
790 PatchedValue_NeedsUpdate = true;
791 if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
792 ResultsUpdateWorkerThread.RunWorkerAsync();
793 }
794 break;
795 case SearchDataTypes._32bits:
796 if (_result.IsUnsigned)
797 {
798 uint value = Convert.ToUInt32(item.SubItems[col_Found_Value].Text, 16);
799 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
800 timer_update_results.Enabled = false;
801 patcher.ShowDialog();
802 timer_update_results.Enabled = true;
803 PatchedValue_NeedsUpdate = true;
804 if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
805 ResultsUpdateWorkerThread.RunWorkerAsync();
806 }
807 else
808 {
809 int value = Convert.ToInt32(item.SubItems[col_Found_Value].Text, 16);
810 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
811 timer_update_results.Enabled = false;
812 patcher.ShowDialog();
813 timer_update_results.Enabled = true;
814 PatchedValue_NeedsUpdate = true;
815 if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
816 ResultsUpdateWorkerThread.RunWorkerAsync();
817 }
818 break;
819 case SearchDataTypes._64bits:
820 if (_result.IsUnsigned)
821 {
822 ulong value = Convert.ToUInt32(item.SubItems[col_Found_Value].Text, 16);
823 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
824 timer_update_results.Enabled = false;
825 patcher.ShowDialog();
826 timer_update_results.Enabled = true;
827 PatchedValue_NeedsUpdate = true;
828 if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
829 ResultsUpdateWorkerThread.RunWorkerAsync();
830 }
831 else
832 {
833 long value = Convert.ToInt32(item.SubItems[col_Found_Value].Text, 16);
834 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
835 timer_update_results.Enabled = false;
836 patcher.ShowDialog();
837 timer_update_results.Enabled = true;
838 PatchedValue_NeedsUpdate = true;
839 if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
840 ResultsUpdateWorkerThread.RunWorkerAsync();
841 }
842 break;
843 }
844 }
845 else
846 {
847
848 ListViewItem item = lstPatchList.Items[index];
849 ResultDataType _result = (ResultDataType)item.Tag;
850 patch_list.Add(_result);
851 }
852 }
853
854 if (patch_list.Count > 0)
855 {
856 SearchRangePatcher rangePatcher = new SearchRangePatcher((IAcceptsProcessAndConfig)this, patch_list);
857 rangePatcher.ShowDialog();
858 }
859
860 #endregion
861 }
862 private void mnuItemPatchSelectedEntry_Click(object sender, EventArgs e)
863 {
864 if (!(lstPatchList.SelectedItems.Count == 1)) return;
865 PatchRange(true);
866 }
867
868 private void mnuItemPatchSelectedRange_Click(object sender, EventArgs e)
869 {
870 if (!(lstPatchList.SelectedItems.Count >= 1)) return;
871 PatchRange(false);
872 }
873
874 private void mnuItemFreezeSelectedPatches_Click(object sender, EventArgs e)
875 {
876 if (!(lstPatchList.SelectedItems.Count > 0)) return;
877 //if (SearchArgs == null) return;
878 try
879 {
880 lstPatchList.ProcessID = this.AcceptedProcess.Id;
881 this.FreezeResultsUpdate();
882 for (int i = 0; i < lstPatchList.SelectedIndices.Count; i++)
883 {
884 lstPatchList.FreezeItem(lstPatchList.SelectedIndices[i]);
885 }
886 // force thaw and update
887 this.ThawResultsUpdate();
888 this.Update();
889 }
890 catch (Exception ex)
891 {
892 Debug.WriteLine(ex.ToString());
893 }
894 }
895
896 private void mnuItemThawSelectedPatches_Click(object sender, EventArgs e)
897 {
898 if (!(lstPatchList.SelectedItems.Count > 0)) return;
899 //if (SearchArgs == null) return;
900 try
901 {
902 lstPatchList.ProcessID = this.AcceptedProcess.Id;
903 this.FreezeResultsUpdate();
904 for (int i = 0; i < lstPatchList.SelectedIndices.Count; i++)
905 {
906 lstPatchList.ThawItem(lstPatchList.SelectedIndices[i]);
907 }
908 // force thaw and update
909 this.ThawResultsUpdate();
910 this.Update();
911 }
912 catch (Exception ex)
913 {
914 Debug.WriteLine(ex.ToString());
915 }
916 }
917
918 private void SearchWorkerThread_DoWork(object sender, DoWorkEventArgs e)
919 {
920 //List<ResultType<object>> tmp_Results = new List<ResultType<object>>();
921 List<ResultType<object>> second_tmp_Results = new List<ResultType<object>>();
922 const double _UPDATE_DELAY = 1024.0;
923 int UPDATE_DELAY = (int)(_UPDATE_DELAY / 1000);
924 //tmp_Results = SearchArgs.Results.GetRange(0,SearchArgs.Results.Count);
925 //SearchArgs.Results = null;
926 //SearchArgs.Results.Clear();
927 // log options
928 SearchArgs.LogSearchOptions();
929 int STEP_SIZE = (int)SearchArgs.DataType / 8;
930
931 GenericMemoryProvider provider = new GenericMemoryProvider((IAcceptsProcessAndConfig)this);
932 byte[] buffered_mem = new byte[0];
933 try
934 {
935 provider.OpenProvider();
936 int bytes_read = 0;
937 buffered_mem = new byte[int.MaxValue]; // throws OutOfMemoryException if size is over 2G
938 provider.ReadProcessMemory(0, int.MaxValue, out bytes_read, out buffered_mem);
939 provider.CloseProvider();
940 }
941 catch (OutOfMemoryException ex)
942 {
943 throw ex;
944 }
945 catch (Exception ex)
946 {
947 throw ex;
948 }
949 if (buffered_mem.Length == 0) { logger.Warn.WriteLine("Buffered Memory is Zero Length."); return; }
950 MemoryStream ms = new MemoryStream(buffered_mem);
951 BinaryReader r_ms = new BinaryReader(ms);
952 logger.Debug.WriteLine(string.Format("Buffered Memory Size -> 0x{0:x8}", r_ms.BaseStream.Length));
953 int Last_Whole_Percent_Done = 0;
954
955 #region First Search
956 if (SearchArgs.IsFirstSearch)
957 {
958 SearchArgs.Results.Clear();
959 r_ms.BaseStream.Seek(0, SeekOrigin.Begin);
960 for (int i = 0; i < r_ms.BaseStream.Length; i += STEP_SIZE)
961 {
962 ResultType<object> _tmp_result = new ResultType<object>();
963
964
965 switch (SearchArgs.DataType)
966 {
967 case SearchDataTypes._8bits:
968 if (SearchArgs.IsUnsignedDataType) { _tmp_result = new ResultType<object>((int)i, r_ms.ReadByte()); }
969 else { _tmp_result = new ResultType<object>((int)i, r_ms.ReadSByte()); } break;
970 case SearchDataTypes._16bits:
971 if (SearchArgs.IsUnsignedDataType) { _tmp_result = new ResultType<object>((int)i, r_ms.ReadUInt16()); }
972 else { _tmp_result = new ResultType<object>((int)i, r_ms.ReadInt16()); } break;
973 case SearchDataTypes._32bits:
974 if (SearchArgs.IsUnsignedDataType) { _tmp_result = new ResultType<object>((int)i, r_ms.ReadUInt32()); }
975 else { _tmp_result = new ResultType<object>((int)i, r_ms.ReadInt32()); } break;
976 case SearchDataTypes._64bits:
977 if (SearchArgs.IsUnsignedDataType) { _tmp_result = new ResultType<object>((int)i, r_ms.ReadUInt64()); }
978 else { _tmp_result = new ResultType<object>((int)i, r_ms.ReadInt64()); } break;
979 }
980 SearchArgs.Results.Add(_tmp_result);
981 double double_percent_done = 100.0 * (double)((double)i / (double)r_ms.BaseStream.Length);
982 int int_percent_done = (int)double_percent_done;
983 if ((i / UPDATE_DELAY) == (int)(i / UPDATE_DELAY) && int_percent_done != Last_Whole_Percent_Done)
984 {
985 resultsprogress.Value = int_percent_done;
986 resultsprogress.Message = string.Format(" -> Reading Address: 0x{0:x8}", i);
987 Last_Whole_Percent_Done = int_percent_done;
988 Application.DoEvents();
989 }
990
991 if (SearchWorkerThread.CancellationPending == true)
992 {
993 e.Cancel = true;
994 return;
995 }
996
997 }
998 resultsprogress.Value = 100;
999 resultsprogress.Message = "";
1000 Application.DoEvents();
1001
1002 }
1003 #endregion
1004
1005 #region Subsequent Searches
1006 r_ms.BaseStream.Seek(0, SeekOrigin.Begin);
1007
1008
1009 // hack to help with OutOfMemory Exceptions (OldValue and Equal compare will always add all found search results)
1010 bool NeedToCompare = true;
1011 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue &&
1012 SearchArgs.CompareType == SearchCompareTypes.Equal &&
1013 SearchArgs.IsFirstSearch)
1014 {
1015 NeedToCompare = false;
1016 second_tmp_Results = null; // Free Memory
1017 }
1018
1019 if (NeedToCompare)
1020 {
1021 if (SearchArgs.CompareType != SearchCompareTypes.Between && SearchArgs.CompareType != SearchCompareTypes.NotBetween)
1022 {
1023 #region Non-Range Searches
1024 //second_tmp_Results = new List<ResultType<object>>(SearchArgs.Results.Count * 1024);
1025 ////second_tmp_Results.c
1026 for (int i = 0; i < SearchArgs.Results.Count; i += 1)
1027 {
1028 r_ms.BaseStream.Seek(SearchArgs.Results[i].Address, SeekOrigin.Begin);
1029
1030 switch (SearchArgs.DataType)
1031 {
1032 #region Comparer Support
1033 case SearchDataTypes._8bits:
1034 if (SearchArgs.IsUnsignedDataType)
1035 {
1036 byte lookup_value = r_ms.ReadByte();
1037 _8bit_unsigned_comparer_ comparer = new _8bit_unsigned_comparer_(SearchArgs, SearchArgs.Results[i].Address);
1038 byte value = 0;
1039 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1040 {
1041 value = Convert.ToByte(SearchArgs.Results[i].Value);
1042 comparer.Value = value;
1043 }
1044 else
1045 {
1046 value = Convert.ToByte(SearchArgs.CompareStartValue);
1047 comparer.Value = value;
1048 }
1049 if (comparer.Compare(lookup_value, value))
1050 {
1051 //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1052 //second_tmp_Results.Add(_tmp_result);
1053 //_tmp_result = null; // free memory
1054 //SearchArgs.Results.RemoveAt(i);
1055 SearchArgs.Results[i].Value = comparer.Value;
1056 second_tmp_Results.Add(SearchArgs.Results[i]);
1057 }
1058 comparer = null; // free memory
1059 }
1060 else
1061 {
1062 sbyte lookup_value = r_ms.ReadSByte();
1063 _8bit_signed_comparer_ comparer = new _8bit_signed_comparer_(SearchArgs, SearchArgs.Results[i].Address);
1064 sbyte value = 0;
1065 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1066 {
1067 value = Convert.ToSByte(SearchArgs.Results[i].Value);
1068 }
1069 else
1070 {
1071 value = Convert.ToSByte(SearchArgs.CompareStartValue);
1072 }
1073 if (comparer.Compare(lookup_value, value))
1074 {
1075 //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1076 //second_tmp_Results.Add(_tmp_result);
1077 //_tmp_result = null; // free memory
1078 //SearchArgs.Results.RemoveAt(i);
1079 SearchArgs.Results[i].Value = comparer.Value;
1080 second_tmp_Results.Add(SearchArgs.Results[i]);
1081 }
1082 comparer = null; // free memory
1083 }
1084 break;
1085 case SearchDataTypes._16bits:
1086 if (SearchArgs.IsUnsignedDataType)
1087 {
1088 ushort lookup_value = r_ms.ReadUInt16();
1089 _16bit_unsigned_comparer_ comparer = new _16bit_unsigned_comparer_(SearchArgs, SearchArgs.Results[i].Address);
1090 ushort value = 0;
1091 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1092 {
1093 value = Convert.ToUInt16(SearchArgs.Results[i].Value);
1094 comparer.Value = value;
1095 }
1096 else
1097 {
1098 value = Convert.ToUInt16(SearchArgs.CompareStartValue);
1099 comparer.Value = value;
1100 }
1101 if (comparer.Compare(lookup_value, value))
1102 {
1103 //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1104 //second_tmp_Results.Add(_tmp_result);
1105 //_tmp_result = null; // free memory
1106 //SearchArgs.Results.RemoveAt(i);
1107 SearchArgs.Results[i].Value = comparer.Value;
1108 second_tmp_Results.Add(SearchArgs.Results[i]);
1109 }
1110 comparer = null; // free memory
1111 }
1112 else
1113 {
1114 short lookup_value = r_ms.ReadInt16();
1115 _16bit_signed_comparer_ comparer = new _16bit_signed_comparer_(SearchArgs, SearchArgs.Results[i].Address);
1116 short value = 0;
1117 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1118 {
1119 value = Convert.ToInt16(SearchArgs.Results[i].Value);
1120 }
1121 else
1122 {
1123 value = Convert.ToInt16(SearchArgs.CompareStartValue);
1124 }
1125 if (comparer.Compare(lookup_value, value))
1126 {
1127 //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1128 //second_tmp_Results.Add(_tmp_result);
1129 //_tmp_result = null; // free memory
1130 //SearchArgs.Results.RemoveAt(i);
1131 SearchArgs.Results[i].Value = comparer.Value;
1132 second_tmp_Results.Add(SearchArgs.Results[i]);
1133 }
1134 comparer = null; // free memory
1135 }
1136 break;
1137 case SearchDataTypes._32bits:
1138 if (SearchArgs.IsUnsignedDataType)
1139 {
1140 uint lookup_value = r_ms.ReadUInt32();
1141 _32bit_unsigned_comparer_ comparer = new _32bit_unsigned_comparer_(SearchArgs, SearchArgs.Results[i].Address);
1142 uint value = 0;
1143 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1144 {
1145 value = Convert.ToUInt32(SearchArgs.Results[i].Value);
1146 comparer.Value = value;
1147 }
1148 else
1149 {
1150 value = Convert.ToUInt32(SearchArgs.CompareStartValue);
1151 comparer.Value = value;
1152 }
1153 if (comparer.Compare(lookup_value, value))
1154 {
1155 //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1156 //second_tmp_Results.Add(_tmp_result);
1157 //_tmp_result = null; // free memory
1158 //SearchArgs.Results.RemoveAt(i);
1159 SearchArgs.Results[i].Value = comparer.Value;
1160 second_tmp_Results.Add(SearchArgs.Results[i]);
1161 }
1162 comparer = null; // free memory
1163 }
1164 else
1165 {
1166 int lookup_value = r_ms.ReadInt32();
1167 _32bit_signed_comparer_ comparer = new _32bit_signed_comparer_(SearchArgs, SearchArgs.Results[i].Address);
1168 int value = 0;
1169 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1170 {
1171 value = Convert.ToInt32(SearchArgs.Results[i].Value);
1172 }
1173 else
1174 {
1175 value = Convert.ToInt32(SearchArgs.CompareStartValue);
1176 }
1177 if (comparer.Compare(lookup_value, value))
1178 {
1179 //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1180 //second_tmp_Results.Add(_tmp_result);
1181 //_tmp_result = null; // free memory
1182 //SearchArgs.Results.RemoveAt(i);
1183 SearchArgs.Results[i].Value = comparer.Value;
1184 second_tmp_Results.Add(SearchArgs.Results[i]);
1185 }
1186 comparer = null; // free memory
1187 }
1188 break;
1189 case SearchDataTypes._64bits:
1190 if (SearchArgs.IsUnsignedDataType)
1191 {
1192 ulong lookup_value = r_ms.ReadUInt64();
1193 _64bit_unsigned_comparer_ comparer = new _64bit_unsigned_comparer_(SearchArgs, SearchArgs.Results[i].Address);
1194 ulong value = 0;
1195 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1196 {
1197 value = Convert.ToUInt64(SearchArgs.Results[i].Value);
1198 comparer.Value = value;
1199 }
1200 else
1201 {
1202 value = Convert.ToUInt64(SearchArgs.CompareStartValue);
1203 comparer.Value = value;
1204 }
1205 if (comparer.Compare(lookup_value, value))
1206 {
1207 //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1208 //second_tmp_Results.Add(_tmp_result);
1209 //_tmp_result = null; // free memory
1210 //SearchArgs.Results.RemoveAt(i);
1211 SearchArgs.Results[i].Value = comparer.Value;
1212 second_tmp_Results.Add(SearchArgs.Results[i]);
1213 }
1214 comparer = null; // free memory
1215 }
1216 else
1217 {
1218 long lookup_value = r_ms.ReadInt64();
1219 _64bit_signed_comparer_ comparer = new _64bit_signed_comparer_(SearchArgs, SearchArgs.Results[i].Address);
1220 long value = 0;
1221 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1222 {
1223 value = Convert.ToInt64(SearchArgs.Results[i].Value);
1224 }
1225 else
1226 {
1227 value = Convert.ToInt64(SearchArgs.CompareStartValue);
1228 }
1229 if (comparer.Compare(lookup_value, value))
1230 {
1231 //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1232 //second_tmp_Results.Add(_tmp_result);
1233 //_tmp_result = null; // free memory
1234 //SearchArgs.Results.RemoveAt(i);
1235 SearchArgs.Results[i].Value = comparer.Value;
1236 second_tmp_Results.Add(SearchArgs.Results[i]);
1237 }
1238 comparer = null; // free memory
1239 }
1240 break;
1241 #endregion
1242 }
1243
1244 double double_percent_done = 100.0 * (double)((double)i / (double)SearchArgs.Results.Count);
1245 int int_percent_done = (int)double_percent_done;
1246 if ((i / UPDATE_DELAY) == (int)(i / UPDATE_DELAY) && int_percent_done != Last_Whole_Percent_Done)
1247 {
1248 resultsprogress.Value = int_percent_done;
1249 resultsprogress.Message = string.Format(" -> Reading Address: 0x{0:x8}", i);
1250 Last_Whole_Percent_Done = int_percent_done;
1251 Application.DoEvents();
1252 }
1253
1254 }
1255 #endregion
1256 }
1257 #region Ranged Searches
1258 #if !DONOT_HAVE_RANGED_SEARCH_SUPPORT
1259 if (SearchArgs.CompareType == SearchCompareTypes.Between || SearchArgs.CompareType == SearchCompareTypes.NotBetween)
1260 {
1261 object start, end;
1262
1263 start = SearchArgs.CompareStartValue;
1264 end = SearchArgs.CompareEndValue;
1265 for (int i = 0; i < SearchArgs.Results.Count; i += 1)
1266 {
1267 //r_ms.BaseStream.Seek(SearchArgs.Results[i].Address, SeekOrigin.Begin);
1268 if (SearchArgs.CompareType == SearchCompareTypes.Between)
1269 {
1270 InRangeComparer comparer = new InRangeComparer(SearchArgs.Results[i].Address, 0);
1271 if (comparer.Compare(start, end, SearchArgs.DataType, SearchArgs.IsUnsignedDataType, r_ms))
1272 {
1273 SearchArgs.Results[i].Value = comparer.Value;
1274 second_tmp_Results.Add(SearchArgs.Results[i]);
1275 }
1276 comparer = null;
1277 }
1278 else if (SearchArgs.CompareType == SearchCompareTypes.NotBetween)
1279 {
1280 NotInRangeComparer comparer = new NotInRangeComparer(SearchArgs.Results[i].Address, 0);
1281 if (comparer.Compare(start, end, SearchArgs.DataType, SearchArgs.IsUnsignedDataType, r_ms))
1282 {
1283 SearchArgs.Results[i].Value = comparer.Value;
1284 second_tmp_Results.Add(SearchArgs.Results[i]);
1285 }
1286 comparer = null;
1287 }
1288 else
1289 {
1290 throw new InvalidOperationException("Encounted unkown range search type: " + SearchArgs.CompareType);
1291 }
1292 double double_percent_done = 100.0 * (double)((double)i / (double)SearchArgs.Results.Count);
1293 int int_percent_done = (int)double_percent_done;
1294 if ((i / UPDATE_DELAY) == (int)(i / UPDATE_DELAY) && int_percent_done != Last_Whole_Percent_Done)
1295 {
1296 resultsprogress.Value = int_percent_done;
1297 resultsprogress.Message = string.Format(" -> Reading Address: 0x{0:x8}", i);
1298 Last_Whole_Percent_Done = int_percent_done;
1299 Application.DoEvents();
1300 }
1301 }
1302 }
1303 #endif
1304 #endregion
1305
1306 }
1307 #endregion
1308 // leave SearchArgs.Results alone, if false
1309 if (NeedToCompare)
1310 {
1311 SearchArgs.Results = second_tmp_Results.GetRange(0, second_tmp_Results.Count);
1312 second_tmp_Results = null; // free memory
1313 }
1314
1315 r_ms.Close();
1316 }
1317
1318 private void SearchWorkerThread_ProgressChanged(object sender, ProgressChangedEventArgs e)
1319 {
1320 //if (SearchArgs.ProgressLogger != null)
1321 //{
1322 // resultsprogress.Value = e.ProgressPercentage;
1323 // //Application.DoEvents();
1324 //}
1325 }
1326
1327 private void SearchWorkerThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
1328 {
1329 resultsprogress.Value = 100;
1330 logger.Debug.WriteLine(string.Format("\nResults Count -> 0x{0:x8}", SearchArgs.Results.Count));
1331
1332 if (SearchArgs.Results.Count == 1) // debug message for 1 result using 32bit unsigned
1333 logger.Debug.WriteLine(string.Format("\nDebug: Found 1 result -> Address: 0x{0:x8} Value: 0x{1:x8}", SearchArgs.Results[0].Address, SearchArgs.Results[0].Value));
1334
1335 logger.Info.WriteLine(string.Format("\nFound 0x{0:x8} results", SearchArgs.Results.Count));
1336
1337 if (SearchArgs.Results.Count <= MIN_NUMBER_OF_RESULTS_BEFORE_DISPLAY)
1338 {
1339 lstResults.Items.Clear();
1340 List<ResultItem> items = new List<ResultItem>();
1341 for (int i = 0; i < SearchArgs.Results.Count; i++)
1342 {
1343 ResultItem item = new ResultItem(0, false);
1344 //item.Text = string.Format("0x{0:x8}", SearchArgs.Results[i].Address);
1345 //item.SubItems.Add(string.Format("0x{0:x8}", SearchArgs.Results[i].Address));
1346 switch (SearchArgs.DataType)
1347 {
1348
1349 case SearchDataTypes._8bits:
1350 if (SearchArgs.IsUnsignedDataType) { item = new ResultItem(SearchArgs.Results[i].Address, false, Convert.ToByte(SearchArgs.Results[i].Value)); }
1351 else { item = new ResultItem(SearchArgs.Results[i].Address, false, Convert.ToSByte(SearchArgs.Results[i].Value)); }
1352 break;
1353 case SearchDataTypes._16bits:
1354 if (SearchArgs.IsUnsignedDataType) { item = new ResultItem(SearchArgs.Results[i].Address, false, Convert.ToUInt16(SearchArgs.Results[i].Value)); }
1355 else { item = new ResultItem(SearchArgs.Results[i].Address, false, Convert.ToInt16(SearchArgs.Results[i].Value)); }
1356 break;
1357 case SearchDataTypes._32bits:
1358 if (SearchArgs.IsUnsignedDataType) { item = new ResultItem(SearchArgs.Results[i].Address, false, Convert.ToUInt32(SearchArgs.Results[i].Value)); }
1359 else { item = new ResultItem(SearchArgs.Results[i].Address, false, Convert.ToInt32(SearchArgs.Results[i].Value)); }
1360 break;
1361 case SearchDataTypes._64bits:
1362 if (SearchArgs.IsUnsignedDataType) { item = new ResultItem(SearchArgs.Results[i].Address, false, Convert.ToUInt64(SearchArgs.Results[i].Value)); }
1363 else { item = new ResultItem(SearchArgs.Results[i].Address, false, Convert.ToInt64(SearchArgs.Results[i].Value)); }
1364 break;
1365 }
1366
1367 if (!items.Contains(item))
1368 items.Add(item);
1369 }
1370 lstResults.Items.AddRange(items.ToArray());
1371 }
1372
1373 this.DoSearchDoneSpecific();
1374 //System.Threading.Thread.Sleep(100);
1375 //if (_SEARCH_MODE != SearchMode.SEARCH_MODE_NORMAL_WITH_JOKER)
1376 this.ThawResultsUpdate();
1377 Application.DoEvents();
1378 }
1379 private void DoSearchDoneSpecific()
1380 {
1381 SearchWorkerThread.CancelAsync();
1382 if (lstResults.Items.Count > 0) { timer_update_results.Enabled = true; }
1383 else { timer_update_results.Enabled = false; }
1384
1385 search_progress_updater.Enabled = false;
1386
1387 btnCancel.Enabled = false;
1388 btnReset.Enabled = true;
1389 btnSearch.Enabled = true;
1390 grpCompareType.Enabled = true;
1391 grpCompareValue.Enabled = true;
1392 resultsprogress.Value = 0;
1393 resultsprogress.Message = "";
1394 grpDataType.Enabled = false;
1395 // resume process on reset, incase it was suspended
1396 ThreadControl.ResumeProcess(this.AcceptedProcess.Id);
1397 //Application.DoEvents();
1398 this.Refresh();
1399 }
1400
1401 private void DoCancelSpecific()
1402 {
1403 this.DoSearchDoneSpecific();
1404 }
1405 private void DoResetSpecific()
1406 {
1407 this.DoCancelSpecific();
1408 IsFirstSearch = true;
1409 grpDataType.Enabled = true;
1410 }
1411 private void search_progress_updater_Tick(object sender, EventArgs e)
1412 {
1413 if ((this.AcceptedProcess ==null) || Process.GetProcessById(this.AcceptedProcess.Id) == null)
1414 {
1415 SearchWorkerThread.CancelAsync();
1416 //JokerSearchWorker.CancelAsync();
1417 ResultsUpdateWorkerThread.CancelAsync();
1418 }
1419 }
1420
1421 #region Search Button
1422 private void btnSearch_Click(object sender, EventArgs e)
1423 {
1424 this.SearchInProgess = true;
1425 //btnCancel.Enabled = true;
1426 //btnReset.Enabled = false;
1427 //btnSearch.Enabled = false;
1428 this.FreezeResultsUpdate();
1429 this.handle_btnSearch_Click();
1430 }
1431 private void handle_btnSearch_Click()
1432 {
1433 //this.FreezeResultsUpdate();
1434 lstResults.Items.Clear();
1435
1436 if (lstResults.Items.Count > 0) { timer_update_results.Enabled = true; }
1437 else { timer_update_results.Enabled = false; }
1438
1439
1440 resultsprogress.Value = 0;
1441 bool _is_unsigned = chkUnsigned.Checked;
1442 SearchType search_type = new SearchType();
1443 SearchDataTypes _data_type = new SearchDataTypes();
1444 SearchCompareTypes _compare_type = new SearchCompareTypes();
1445 CompareValueTypes _compare_value_type = new CompareValueTypes();
1446 object start_value = 0;
1447 object end_value = 0;
1448 // get datatype
1449 if (radio_8bits.Checked) { _data_type = SearchDataTypes._8bits; }
1450 else if (radio_16bits.Checked) { _data_type = SearchDataTypes._16bits; }
1451 else if (radio_32bits.Checked) { _data_type = SearchDataTypes._32bits; }
1452 else if (radio_64bits.Checked) { _data_type = SearchDataTypes._64bits; }
1453 else { logger.Error.WriteLine("Could not determine search data type bit size. (was not 8/16/32/or 64bits)"); }
1454 // get compare type
1455 if (radiocompare_equal.Checked) { _compare_type = SearchCompareTypes.Equal; }
1456 else if (radiocompare_greaterthan.Checked) { _compare_type = SearchCompareTypes.GreaterThan; }
1457 else if (radiocompare_lessthan.Checked) { _compare_type = SearchCompareTypes.LessThan; }
1458 else if (radiocompare_greaterthan_orequal.Checked) { _compare_type = SearchCompareTypes.GreaterThanOrEqual; }
1459 else if (radiocompare_lessthan_orequal.Checked) { _compare_type = SearchCompareTypes.LessThanOrEqual; }
1460 else if (radiocompare_notequal.Checked) { _compare_type = SearchCompareTypes.NotEqual; }
1461 else if (radiocompare_between.Checked) { _compare_type = SearchCompareTypes.Between; }
1462 else if (radiocompare_notbetween.Checked) { _compare_type = SearchCompareTypes.NotBetween; }
1463 else { logger.Error.WriteLine("Could not determine search comparison type. (was not == > < >= <= != <> or !<>)"); }
1464 // get compare valure type
1465 if (radio_oldvalue.Checked) { _compare_value_type = CompareValueTypes.OldValue; }
1466 else if (radio_specificvalue.Checked) { _compare_value_type = CompareValueTypes.SpecificValue; }
1467 else { logger.Error.WriteLine("Could not determine search comparison type. (was not old or specific value"); }
1468
1469 if (_compare_value_type == CompareValueTypes.SpecificValue || (_compare_type == SearchCompareTypes.Between || _compare_type == SearchCompareTypes.NotBetween))
1470 {
1471
1472 switch (_data_type)
1473 {
1474 case SearchDataTypes._8bits:
1475 if (_is_unsigned) { start_value = txtStartAddr.ToByte(); }
1476 else { start_value = txtStartAddr.ToSByte(); }
1477 break;
1478 case SearchDataTypes._16bits:
1479 if (_is_unsigned) { start_value = txtStartAddr.ToUInt16(); }
1480 else { start_value = txtStartAddr.ToInt16(); }
1481 break;
1482 case SearchDataTypes._32bits:
1483 if (_is_unsigned) { start_value = txtStartAddr.ToUInt32(); }
1484 else { start_value = txtStartAddr.ToInt32(); }
1485 break;
1486 case SearchDataTypes._64bits:
1487 if (_is_unsigned) { start_value = txtStartAddr.ToUInt64(); }
1488 else { start_value = txtStartAddr.ToInt64(); }
1489 break;
1490 default: throw new InvalidOperationException("In SearchType(): Encounterd an Unkown Search Data Type.");
1491 }
1492 }
1493 if (_compare_type == SearchCompareTypes.Between || _compare_type == SearchCompareTypes.NotBetween)
1494 {
1495 switch (_data_type)
1496 {
1497 case SearchDataTypes._8bits:
1498 if (_is_unsigned) { end_value = txtEndAddr.ToByte(); }
1499 else { end_value = txtEndAddr.ToSByte(); }
1500 break;
1501 case SearchDataTypes._16bits:
1502 if (_is_unsigned) { end_value = txtEndAddr.ToUInt16(); }
1503 else { end_value = txtEndAddr.ToInt16(); }
1504 break;
1505 case SearchDataTypes._32bits:
1506 if (_is_unsigned) { end_value = txtEndAddr.ToUInt32(); }
1507 else { end_value = txtEndAddr.ToInt32(); }
1508 break;
1509 case SearchDataTypes._64bits:
1510 if (_is_unsigned) { end_value = txtEndAddr.ToUInt64(); }
1511 else { end_value = txtEndAddr.ToInt64(); }
1512 break;
1513 default: throw new InvalidOperationException("In SearchType(): Encounterd an Unkown Search Data Type.");
1514 }
1515 }
1516
1517 search_type = new SearchType(_data_type, _is_unsigned, _compare_type, _compare_value_type, start_value, end_value, resultsprogress);
1518
1519 //search_type.LogSearchOptions();
1520
1521 search_type.IsFirstSearch = IsFirstSearch;
1522
1523
1524
1525 DoSearch(search_type);
1526 IsFirstSearch = false;
1527 }
1528 private void DoSearch(SearchType args)
1529 {
1530 if (!args.IsFirstSearch && SearchArgs != null)
1531 {
1532 //args.Results.AddRange(SearchArgs.Results.ToArray());
1533 args.Results = SearchArgs.Results;
1534 }
1535 SearchArgs = args;
1536 #if DONOT_HAVE_RANGED_SEARCH_SUPPORT
1537 if (SearchArgs.CompareType == SearchCompareTypes.Between || SearchArgs.CompareType == SearchCompareTypes.NotBetween)
1538 {
1539 throw new NotImplementedException("Between and Not Between Range searches have not been implemented.");
1540 }
1541 #endif
1542 search_progress_updater.Enabled = true;
1543 //padPluginSelector.Enabled = false;
1544 //gsPluginSelector.Enabled = false;
1545 btnReset.Enabled = false;
1546 btnSearch.Enabled = false;
1547 btnCancel.Enabled = true;
1548 grpDataType.Enabled = false;
1549 grpCompareType.Enabled = false;
1550 grpCompareValue.Enabled = false;
1551 this.Refresh();
1552 Application.DoEvents();
1553 SearchWorkerThread.RunWorkerAsync();
1554 }
1555 #endregion
1556 private void btnReset_Click(object sender, EventArgs e)
1557 {
1558 this.SearchInProgess = false;
1559 //btnSearch.Enabled = true;
1560 //btnCancel.Enabled = false;
1561 this.DoResetSpecific();
1562 lstResults.Items.Clear();
1563 try { SearchArgs.Results = new List<ResultType<object>>(); }
1564 catch { }
1565 }
1566
1567 private void btnCancel_Click(object sender, EventArgs e)
1568 {
1569 this.SearchInProgess = false;
1570 //btnCancel.Enabled = false;
1571 //btnSearch.Enabled = true;
1572 //btnReset.Enabled = true;
1573 this.DoCancelSpecific();
1574 }
1575
1576 private void mnuItemPatchListViewMemoryRegion_Click(object sender, EventArgs e)
1577 {
1578 List<ResultDataType> patch_list = new List<ResultDataType>();
1579 List<int> SelectedIndexes = new List<int>();
1580 foreach (int index in lstPatchList.SelectedIndices) { SelectedIndexes.Add(index); }
1581 foreach (int index in SelectedIndexes)
1582 {
1583 ListViewItem item = lstPatchList.Items[index];
1584 ResultDataType rdt = (ResultDataType)item.Tag;
1585 ViewMemoryRegion(rdt);
1586 break; // only get the fist item
1587 }
1588 }
1589
1590 private void mnuItemResultsListViewMemoryRegion_Click(object sender, EventArgs e)
1591 {
1592 List<ResultDataType> patch_list = new List<ResultDataType>();
1593 List<int> SelectedIndexes = new List<int>();
1594 foreach (int index in lstResults.SelectedIndices) { SelectedIndexes.Add(index); }
1595 foreach (int index in SelectedIndexes)
1596 {
1597 ListViewItem item = lstResults.Items[index];
1598 ResultDataType rdt = (ResultDataType)item.Tag;
1599 ViewMemoryRegion(rdt);
1600 break; // only get the fist item
1601 }
1602 }
1603 private void ViewMemoryRegion(ResultDataType rdt)
1604 {
1605 if (OnBrowseMemoryRegion != null)
1606 {
1607 OnBrowseMemoryRegion(new BrowseMemoryRegionEvent(rdt.Address));
1608 }
1609 }
1610
1611 private void mnuAddedResults_Opening(object sender, CancelEventArgs e)
1612 {
1613 if (!(lstPatchList.Items.Count > 0)) { mnuItemRemoveResult.Visible = false; e.Cancel = true; }
1614 if (!(lstPatchList.Items.Count > 0)) { mnuItemPatchSelectedEntry.Visible = false; e.Cancel = true; }
1615 if (e.Cancel) return;
1616 if (lstPatchList.Items.Count > 0) mnuItemRemoveResult.Visible = true;
1617 if (lstPatchList.Items.Count > 0) mnuItemPatchSelectedEntry.Visible = true;
1618
1619 if (!(lstPatchList.Items.Count > 0)) { mnuItemFreezeSelectedPatches.Visible = false; e.Cancel = true; }
1620 if (!(lstPatchList.Items.Count > 0)) { mnuItemThawSelectedPatches.Visible = false; e.Cancel = true; }
1621 if (e.Cancel) return;
1622
1623 if (lstPatchList.Items.Count > 0) mnuItemFreezeSelectedPatches.Visible = true;
1624 if (lstPatchList.Items.Count > 0) mnuItemThawSelectedPatches.Visible = true;
1625
1626 if (lstPatchList.SelectedItems.Count == 0) e.Cancel = true;
1627 if (e.Cancel) return;
1628
1629 }
1630
1631 private void mnuResults_Opening(object sender, CancelEventArgs e)
1632 {
1633 if (!(lstResults.Items.Count > 0)) e.Cancel = true;
1634 if (lstResults.SelectedItems.Count == 0) e.Cancel = true;
1635 if (SearchArgs == null) e.Cancel = true;
1636 if (e.Cancel) return;
1637 }
1638 }
1639 }

  ViewVC Help
Powered by ViewVC 1.1.22