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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 286 - (show annotations) (download)
Tue Jun 5 02:19:13 2012 UTC (8 years, 4 months ago) by william
File size: 81875 byte(s)
+ more wip

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 Stopwatch st = new Stopwatch();
921 st.Start();
922 e.Result = st;
923 //List<ResultType<object>> tmp_Results = new List<ResultType<object>>();
924 List<ResultType<object>> second_tmp_Results = new List<ResultType<object>>();
925 const double _UPDATE_DELAY = 1024.0;
926 int UPDATE_DELAY = (int)(_UPDATE_DELAY / 1000);
927 //tmp_Results = SearchArgs.Results.GetRange(0,SearchArgs.Results.Count);
928 //SearchArgs.Results = null;
929 //SearchArgs.Results.Clear();
930 // log options
931 SearchArgs.LogSearchOptions();
932 int STEP_SIZE = (int)SearchArgs.DataType / 8;
933
934 GenericMemoryProvider provider = new GenericMemoryProvider((IAcceptsProcessAndConfig)this);
935 provider.OpenProvider();
936 int bytes_read = 0;
937
938 byte[] buffered_mem = new byte[MemorySizeConstants.MaximumAddress]; // throws OutOfMemoryException if size is over 2G
939 provider.ReadProcessMemory(MemorySizeConstants.MinimumAddress, MemorySizeConstants.MaximumAddress, out bytes_read, out buffered_mem);
940 provider.CloseProvider();
941
942 if (buffered_mem.Length == 0) { logger.Warn.WriteLine("Buffered Memory is Zero Length."); return; }
943 MemoryStream ms = new MemoryStream(buffered_mem);
944 BinaryReader r_ms = new BinaryReader(ms);
945 logger.Debug.WriteLine(string.Format("Buffered Memory Size -> 0x{0:x8}", r_ms.BaseStream.Length));
946 int Last_Whole_Percent_Done = 0;
947
948 #region First Search
949 if (SearchArgs.IsFirstSearch)
950 {
951 SearchArgs.Results.Clear();
952 r_ms.BaseStream.Seek(0, SeekOrigin.Begin);
953 for (int i = 0; i < r_ms.BaseStream.Length; i += STEP_SIZE)
954 {
955 ResultType<object> _tmp_result = new ResultType<object>();
956
957
958 switch (SearchArgs.DataType)
959 {
960 case SearchDataTypes._8bits:
961 if (SearchArgs.IsUnsignedDataType) { _tmp_result = new ResultType<object>((int)i, r_ms.ReadByte()); }
962 else { _tmp_result = new ResultType<object>((int)i, r_ms.ReadSByte()); } break;
963 case SearchDataTypes._16bits:
964 if (SearchArgs.IsUnsignedDataType) { _tmp_result = new ResultType<object>((int)i, r_ms.ReadUInt16()); }
965 else { _tmp_result = new ResultType<object>((int)i, r_ms.ReadInt16()); } break;
966 case SearchDataTypes._32bits:
967 if (SearchArgs.IsUnsignedDataType) { _tmp_result = new ResultType<object>((int)i, r_ms.ReadUInt32()); }
968 else { _tmp_result = new ResultType<object>((int)i, r_ms.ReadInt32()); } break;
969 case SearchDataTypes._64bits:
970 if (SearchArgs.IsUnsignedDataType) { _tmp_result = new ResultType<object>((int)i, r_ms.ReadUInt64()); }
971 else { _tmp_result = new ResultType<object>((int)i, r_ms.ReadInt64()); } break;
972 }
973 SearchArgs.Results.Add(_tmp_result);
974 double double_percent_done = 100.0 * (double)((double)i / (double)r_ms.BaseStream.Length);
975 int int_percent_done = (int)double_percent_done;
976 if ((i / UPDATE_DELAY) == (int)(i / UPDATE_DELAY) && int_percent_done != Last_Whole_Percent_Done)
977 {
978 resultsprogress.Value = int_percent_done;
979 resultsprogress.Message = string.Format(" -> Reading Address: 0x{0:x8}", i);
980 Last_Whole_Percent_Done = int_percent_done;
981 Application.DoEvents();
982 }
983
984 if (SearchWorkerThread.CancellationPending == true)
985 {
986 e.Cancel = true;
987 return;
988 }
989
990 }
991 resultsprogress.Value = 100;
992 resultsprogress.Message = "";
993 Application.DoEvents();
994
995 }
996 #endregion
997
998 #region Subsequent Searches
999 r_ms.BaseStream.Seek(0, SeekOrigin.Begin);
1000
1001
1002 // hack to help with OutOfMemory Exceptions (OldValue and Equal compare will always add all found search results)
1003 bool NeedToCompare = true;
1004 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue &&
1005 SearchArgs.CompareType == SearchCompareTypes.Equal &&
1006 SearchArgs.IsFirstSearch)
1007 {
1008 NeedToCompare = false;
1009 second_tmp_Results = null; // Free Memory
1010 }
1011
1012 if (NeedToCompare)
1013 {
1014 if (SearchArgs.CompareType != SearchCompareTypes.Between && SearchArgs.CompareType != SearchCompareTypes.NotBetween)
1015 {
1016 #region Non-Range Searches
1017 //second_tmp_Results = new List<ResultType<object>>(SearchArgs.Results.Count * 1024);
1018 ////second_tmp_Results.c
1019 for (int i = 0; i < SearchArgs.Results.Count; i += 1)
1020 {
1021 r_ms.BaseStream.Seek(SearchArgs.Results[i].Address, SeekOrigin.Begin);
1022
1023 switch (SearchArgs.DataType)
1024 {
1025 #region Comparer Support
1026 case SearchDataTypes._8bits:
1027 if (SearchArgs.IsUnsignedDataType)
1028 {
1029 byte lookup_value = r_ms.ReadByte();
1030 _8bit_unsigned_comparer_ comparer = new _8bit_unsigned_comparer_(SearchArgs, SearchArgs.Results[i].Address);
1031 byte value = 0;
1032 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1033 {
1034 value = Convert.ToByte(SearchArgs.Results[i].Value);
1035 comparer.Value = value;
1036 }
1037 else
1038 {
1039 value = Convert.ToByte(SearchArgs.CompareStartValue);
1040 comparer.Value = value;
1041 }
1042 if (comparer.Compare(lookup_value, value))
1043 {
1044 //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1045 //second_tmp_Results.Add(_tmp_result);
1046 //_tmp_result = null; // free memory
1047 //SearchArgs.Results.RemoveAt(i);
1048 SearchArgs.Results[i].Value = comparer.Value;
1049 second_tmp_Results.Add(SearchArgs.Results[i]);
1050 }
1051 comparer = null; // free memory
1052 }
1053 else
1054 {
1055 sbyte lookup_value = r_ms.ReadSByte();
1056 _8bit_signed_comparer_ comparer = new _8bit_signed_comparer_(SearchArgs, SearchArgs.Results[i].Address);
1057 sbyte value = 0;
1058 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1059 {
1060 value = Convert.ToSByte(SearchArgs.Results[i].Value);
1061 }
1062 else
1063 {
1064 value = Convert.ToSByte(SearchArgs.CompareStartValue);
1065 }
1066 if (comparer.Compare(lookup_value, value))
1067 {
1068 //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1069 //second_tmp_Results.Add(_tmp_result);
1070 //_tmp_result = null; // free memory
1071 //SearchArgs.Results.RemoveAt(i);
1072 SearchArgs.Results[i].Value = comparer.Value;
1073 second_tmp_Results.Add(SearchArgs.Results[i]);
1074 }
1075 comparer = null; // free memory
1076 }
1077 break;
1078 case SearchDataTypes._16bits:
1079 if (SearchArgs.IsUnsignedDataType)
1080 {
1081 ushort lookup_value = r_ms.ReadUInt16();
1082 _16bit_unsigned_comparer_ comparer = new _16bit_unsigned_comparer_(SearchArgs, SearchArgs.Results[i].Address);
1083 ushort value = 0;
1084 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1085 {
1086 value = Convert.ToUInt16(SearchArgs.Results[i].Value);
1087 comparer.Value = value;
1088 }
1089 else
1090 {
1091 value = Convert.ToUInt16(SearchArgs.CompareStartValue);
1092 comparer.Value = value;
1093 }
1094 if (comparer.Compare(lookup_value, value))
1095 {
1096 //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1097 //second_tmp_Results.Add(_tmp_result);
1098 //_tmp_result = null; // free memory
1099 //SearchArgs.Results.RemoveAt(i);
1100 SearchArgs.Results[i].Value = comparer.Value;
1101 second_tmp_Results.Add(SearchArgs.Results[i]);
1102 }
1103 comparer = null; // free memory
1104 }
1105 else
1106 {
1107 short lookup_value = r_ms.ReadInt16();
1108 _16bit_signed_comparer_ comparer = new _16bit_signed_comparer_(SearchArgs, SearchArgs.Results[i].Address);
1109 short value = 0;
1110 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1111 {
1112 value = Convert.ToInt16(SearchArgs.Results[i].Value);
1113 }
1114 else
1115 {
1116 value = Convert.ToInt16(SearchArgs.CompareStartValue);
1117 }
1118 if (comparer.Compare(lookup_value, value))
1119 {
1120 //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1121 //second_tmp_Results.Add(_tmp_result);
1122 //_tmp_result = null; // free memory
1123 //SearchArgs.Results.RemoveAt(i);
1124 SearchArgs.Results[i].Value = comparer.Value;
1125 second_tmp_Results.Add(SearchArgs.Results[i]);
1126 }
1127 comparer = null; // free memory
1128 }
1129 break;
1130 case SearchDataTypes._32bits:
1131 if (SearchArgs.IsUnsignedDataType)
1132 {
1133 uint lookup_value = r_ms.ReadUInt32();
1134 _32bit_unsigned_comparer_ comparer = new _32bit_unsigned_comparer_(SearchArgs, SearchArgs.Results[i].Address);
1135 uint value = 0;
1136 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1137 {
1138 value = Convert.ToUInt32(SearchArgs.Results[i].Value);
1139 comparer.Value = value;
1140 }
1141 else
1142 {
1143 value = Convert.ToUInt32(SearchArgs.CompareStartValue);
1144 comparer.Value = value;
1145 }
1146 if (comparer.Compare(lookup_value, value))
1147 {
1148 //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1149 //second_tmp_Results.Add(_tmp_result);
1150 //_tmp_result = null; // free memory
1151 //SearchArgs.Results.RemoveAt(i);
1152 SearchArgs.Results[i].Value = comparer.Value;
1153 second_tmp_Results.Add(SearchArgs.Results[i]);
1154 }
1155 comparer = null; // free memory
1156 }
1157 else
1158 {
1159 int lookup_value = r_ms.ReadInt32();
1160 _32bit_signed_comparer_ comparer = new _32bit_signed_comparer_(SearchArgs, SearchArgs.Results[i].Address);
1161 int value = 0;
1162 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1163 {
1164 value = Convert.ToInt32(SearchArgs.Results[i].Value);
1165 }
1166 else
1167 {
1168 value = Convert.ToInt32(SearchArgs.CompareStartValue);
1169 }
1170 if (comparer.Compare(lookup_value, value))
1171 {
1172 //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1173 //second_tmp_Results.Add(_tmp_result);
1174 //_tmp_result = null; // free memory
1175 //SearchArgs.Results.RemoveAt(i);
1176 SearchArgs.Results[i].Value = comparer.Value;
1177 second_tmp_Results.Add(SearchArgs.Results[i]);
1178 }
1179 comparer = null; // free memory
1180 }
1181 break;
1182 case SearchDataTypes._64bits:
1183 if (SearchArgs.IsUnsignedDataType)
1184 {
1185 ulong lookup_value = r_ms.ReadUInt64();
1186 _64bit_unsigned_comparer_ comparer = new _64bit_unsigned_comparer_(SearchArgs, SearchArgs.Results[i].Address);
1187 ulong value = 0;
1188 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1189 {
1190 value = Convert.ToUInt64(SearchArgs.Results[i].Value);
1191 comparer.Value = value;
1192 }
1193 else
1194 {
1195 value = Convert.ToUInt64(SearchArgs.CompareStartValue);
1196 comparer.Value = value;
1197 }
1198 if (comparer.Compare(lookup_value, value))
1199 {
1200 //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1201 //second_tmp_Results.Add(_tmp_result);
1202 //_tmp_result = null; // free memory
1203 //SearchArgs.Results.RemoveAt(i);
1204 SearchArgs.Results[i].Value = comparer.Value;
1205 second_tmp_Results.Add(SearchArgs.Results[i]);
1206 }
1207 comparer = null; // free memory
1208 }
1209 else
1210 {
1211 long lookup_value = r_ms.ReadInt64();
1212 _64bit_signed_comparer_ comparer = new _64bit_signed_comparer_(SearchArgs, SearchArgs.Results[i].Address);
1213 long value = 0;
1214 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1215 {
1216 value = Convert.ToInt64(SearchArgs.Results[i].Value);
1217 }
1218 else
1219 {
1220 value = Convert.ToInt64(SearchArgs.CompareStartValue);
1221 }
1222 if (comparer.Compare(lookup_value, value))
1223 {
1224 //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1225 //second_tmp_Results.Add(_tmp_result);
1226 //_tmp_result = null; // free memory
1227 //SearchArgs.Results.RemoveAt(i);
1228 SearchArgs.Results[i].Value = comparer.Value;
1229 second_tmp_Results.Add(SearchArgs.Results[i]);
1230 }
1231 comparer = null; // free memory
1232 }
1233 break;
1234 #endregion
1235 }
1236
1237 double double_percent_done = 100.0 * (double)((double)i / (double)SearchArgs.Results.Count);
1238 int int_percent_done = (int)double_percent_done;
1239 if ((i / UPDATE_DELAY) == (int)(i / UPDATE_DELAY) && int_percent_done != Last_Whole_Percent_Done)
1240 {
1241 resultsprogress.Value = int_percent_done;
1242 resultsprogress.Message = string.Format(" -> Reading Address: 0x{0:x8}", i);
1243 Last_Whole_Percent_Done = int_percent_done;
1244 Application.DoEvents();
1245 }
1246
1247 }
1248 #endregion
1249 }
1250 #region Ranged Searches
1251 #if !DONOT_HAVE_RANGED_SEARCH_SUPPORT
1252 if (SearchArgs.CompareType == SearchCompareTypes.Between || SearchArgs.CompareType == SearchCompareTypes.NotBetween)
1253 {
1254 object start, end;
1255
1256 start = SearchArgs.CompareStartValue;
1257 end = SearchArgs.CompareEndValue;
1258 for (int i = 0; i < SearchArgs.Results.Count; i += 1)
1259 {
1260 //r_ms.BaseStream.Seek(SearchArgs.Results[i].Address, SeekOrigin.Begin);
1261 if (SearchArgs.CompareType == SearchCompareTypes.Between)
1262 {
1263 InRangeComparer comparer = new InRangeComparer(SearchArgs.Results[i].Address, 0);
1264 if (comparer.Compare(start, end, SearchArgs.DataType, SearchArgs.IsUnsignedDataType, r_ms))
1265 {
1266 SearchArgs.Results[i].Value = comparer.Value;
1267 second_tmp_Results.Add(SearchArgs.Results[i]);
1268 }
1269 comparer = null;
1270 }
1271 else if (SearchArgs.CompareType == SearchCompareTypes.NotBetween)
1272 {
1273 NotInRangeComparer comparer = new NotInRangeComparer(SearchArgs.Results[i].Address, 0);
1274 if (comparer.Compare(start, end, SearchArgs.DataType, SearchArgs.IsUnsignedDataType, r_ms))
1275 {
1276 SearchArgs.Results[i].Value = comparer.Value;
1277 second_tmp_Results.Add(SearchArgs.Results[i]);
1278 }
1279 comparer = null;
1280 }
1281 else
1282 {
1283 throw new InvalidOperationException("Encounted unkown range search type: " + SearchArgs.CompareType);
1284 }
1285 double double_percent_done = 100.0 * (double)((double)i / (double)SearchArgs.Results.Count);
1286 int int_percent_done = (int)double_percent_done;
1287 if ((i / UPDATE_DELAY) == (int)(i / UPDATE_DELAY) && int_percent_done != Last_Whole_Percent_Done)
1288 {
1289 resultsprogress.Value = int_percent_done;
1290 resultsprogress.Message = string.Format(" -> Reading Address: 0x{0:x8}", i);
1291 Last_Whole_Percent_Done = int_percent_done;
1292 Application.DoEvents();
1293 }
1294 }
1295 }
1296 #endif
1297 #endregion
1298
1299 }
1300 #endregion
1301 // leave SearchArgs.Results alone, if false
1302 if (NeedToCompare)
1303 {
1304 SearchArgs.Results = second_tmp_Results.GetRange(0, second_tmp_Results.Count);
1305 second_tmp_Results = null; // free memory
1306 }
1307
1308 r_ms.Close();
1309 }
1310
1311 private void SearchWorkerThread_ProgressChanged(object sender, ProgressChangedEventArgs e)
1312 {
1313 //if (SearchArgs.ProgressLogger != null)
1314 //{
1315 // resultsprogress.Value = e.ProgressPercentage;
1316 // //Application.DoEvents();
1317 //}
1318 }
1319
1320 private void SearchWorkerThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
1321 {
1322 if (!e.Cancelled)
1323 {
1324 Stopwatch st = (e.Result as Stopwatch);
1325 st.Stop();
1326 logger.Debug.WriteLine("Search took a total of {0} seconds", st.Elapsed.TotalSeconds);
1327 }
1328
1329 resultsprogress.Value = 100;
1330 logger.Debug.WriteLine(string.Format("Results 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("Debug: 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("Found 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