ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/RomCheater/trunk/RomCheater/Docking/FloatingMemorySearcher.cs
Revision: 285
Committed: Tue Jun 5 01:54:33 2012 UTC (11 years, 6 months ago) by william
File size: 81519 byte(s)
Log Message:
+ search wip

File Contents

# Content
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 provider.OpenProvider();
933 int bytes_read = 0;
934
935 byte[] buffered_mem = new byte[MemorySizeConstants.MaximumAddress]; // throws OutOfMemoryException if size is over 2G
936 provider.ReadProcessMemory(MemorySizeConstants.MinimumAddress, MemorySizeConstants.MaximumAddress, out bytes_read, out buffered_mem);
937 provider.CloseProvider();
938
939 if (buffered_mem.Length == 0) { logger.Warn.WriteLine("Buffered Memory is Zero Length."); return; }
940 MemoryStream ms = new MemoryStream(buffered_mem);
941 BinaryReader r_ms = new BinaryReader(ms);
942 logger.Debug.WriteLine(string.Format("Buffered Memory Size -> 0x{0:x8}", r_ms.BaseStream.Length));
943 int Last_Whole_Percent_Done = 0;
944
945 #region First Search
946 if (SearchArgs.IsFirstSearch)
947 {
948 SearchArgs.Results.Clear();
949 r_ms.BaseStream.Seek(0, SeekOrigin.Begin);
950 for (int i = 0; i < r_ms.BaseStream.Length; i += STEP_SIZE)
951 {
952 ResultType<object> _tmp_result = new ResultType<object>();
953
954
955 switch (SearchArgs.DataType)
956 {
957 case SearchDataTypes._8bits:
958 if (SearchArgs.IsUnsignedDataType) { _tmp_result = new ResultType<object>((int)i, r_ms.ReadByte()); }
959 else { _tmp_result = new ResultType<object>((int)i, r_ms.ReadSByte()); } break;
960 case SearchDataTypes._16bits:
961 if (SearchArgs.IsUnsignedDataType) { _tmp_result = new ResultType<object>((int)i, r_ms.ReadUInt16()); }
962 else { _tmp_result = new ResultType<object>((int)i, r_ms.ReadInt16()); } break;
963 case SearchDataTypes._32bits:
964 if (SearchArgs.IsUnsignedDataType) { _tmp_result = new ResultType<object>((int)i, r_ms.ReadUInt32()); }
965 else { _tmp_result = new ResultType<object>((int)i, r_ms.ReadInt32()); } break;
966 case SearchDataTypes._64bits:
967 if (SearchArgs.IsUnsignedDataType) { _tmp_result = new ResultType<object>((int)i, r_ms.ReadUInt64()); }
968 else { _tmp_result = new ResultType<object>((int)i, r_ms.ReadInt64()); } break;
969 }
970 SearchArgs.Results.Add(_tmp_result);
971 double double_percent_done = 100.0 * (double)((double)i / (double)r_ms.BaseStream.Length);
972 int int_percent_done = (int)double_percent_done;
973 if ((i / UPDATE_DELAY) == (int)(i / UPDATE_DELAY) && int_percent_done != Last_Whole_Percent_Done)
974 {
975 resultsprogress.Value = int_percent_done;
976 resultsprogress.Message = string.Format(" -> Reading Address: 0x{0:x8}", i);
977 Last_Whole_Percent_Done = int_percent_done;
978 Application.DoEvents();
979 }
980
981 if (SearchWorkerThread.CancellationPending == true)
982 {
983 e.Cancel = true;
984 return;
985 }
986
987 }
988 resultsprogress.Value = 100;
989 resultsprogress.Message = "";
990 Application.DoEvents();
991
992 }
993 #endregion
994
995 #region Subsequent Searches
996 r_ms.BaseStream.Seek(0, SeekOrigin.Begin);
997
998
999 // hack to help with OutOfMemory Exceptions (OldValue and Equal compare will always add all found search results)
1000 bool NeedToCompare = true;
1001 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue &&
1002 SearchArgs.CompareType == SearchCompareTypes.Equal &&
1003 SearchArgs.IsFirstSearch)
1004 {
1005 NeedToCompare = false;
1006 second_tmp_Results = null; // Free Memory
1007 }
1008
1009 if (NeedToCompare)
1010 {
1011 if (SearchArgs.CompareType != SearchCompareTypes.Between && SearchArgs.CompareType != SearchCompareTypes.NotBetween)
1012 {
1013 #region Non-Range Searches
1014 //second_tmp_Results = new List<ResultType<object>>(SearchArgs.Results.Count * 1024);
1015 ////second_tmp_Results.c
1016 for (int i = 0; i < SearchArgs.Results.Count; i += 1)
1017 {
1018 r_ms.BaseStream.Seek(SearchArgs.Results[i].Address, SeekOrigin.Begin);
1019
1020 switch (SearchArgs.DataType)
1021 {
1022 #region Comparer Support
1023 case SearchDataTypes._8bits:
1024 if (SearchArgs.IsUnsignedDataType)
1025 {
1026 byte lookup_value = r_ms.ReadByte();
1027 _8bit_unsigned_comparer_ comparer = new _8bit_unsigned_comparer_(SearchArgs, SearchArgs.Results[i].Address);
1028 byte value = 0;
1029 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1030 {
1031 value = Convert.ToByte(SearchArgs.Results[i].Value);
1032 comparer.Value = value;
1033 }
1034 else
1035 {
1036 value = Convert.ToByte(SearchArgs.CompareStartValue);
1037 comparer.Value = value;
1038 }
1039 if (comparer.Compare(lookup_value, value))
1040 {
1041 //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1042 //second_tmp_Results.Add(_tmp_result);
1043 //_tmp_result = null; // free memory
1044 //SearchArgs.Results.RemoveAt(i);
1045 SearchArgs.Results[i].Value = comparer.Value;
1046 second_tmp_Results.Add(SearchArgs.Results[i]);
1047 }
1048 comparer = null; // free memory
1049 }
1050 else
1051 {
1052 sbyte lookup_value = r_ms.ReadSByte();
1053 _8bit_signed_comparer_ comparer = new _8bit_signed_comparer_(SearchArgs, SearchArgs.Results[i].Address);
1054 sbyte value = 0;
1055 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1056 {
1057 value = Convert.ToSByte(SearchArgs.Results[i].Value);
1058 }
1059 else
1060 {
1061 value = Convert.ToSByte(SearchArgs.CompareStartValue);
1062 }
1063 if (comparer.Compare(lookup_value, value))
1064 {
1065 //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1066 //second_tmp_Results.Add(_tmp_result);
1067 //_tmp_result = null; // free memory
1068 //SearchArgs.Results.RemoveAt(i);
1069 SearchArgs.Results[i].Value = comparer.Value;
1070 second_tmp_Results.Add(SearchArgs.Results[i]);
1071 }
1072 comparer = null; // free memory
1073 }
1074 break;
1075 case SearchDataTypes._16bits:
1076 if (SearchArgs.IsUnsignedDataType)
1077 {
1078 ushort lookup_value = r_ms.ReadUInt16();
1079 _16bit_unsigned_comparer_ comparer = new _16bit_unsigned_comparer_(SearchArgs, SearchArgs.Results[i].Address);
1080 ushort value = 0;
1081 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1082 {
1083 value = Convert.ToUInt16(SearchArgs.Results[i].Value);
1084 comparer.Value = value;
1085 }
1086 else
1087 {
1088 value = Convert.ToUInt16(SearchArgs.CompareStartValue);
1089 comparer.Value = value;
1090 }
1091 if (comparer.Compare(lookup_value, value))
1092 {
1093 //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1094 //second_tmp_Results.Add(_tmp_result);
1095 //_tmp_result = null; // free memory
1096 //SearchArgs.Results.RemoveAt(i);
1097 SearchArgs.Results[i].Value = comparer.Value;
1098 second_tmp_Results.Add(SearchArgs.Results[i]);
1099 }
1100 comparer = null; // free memory
1101 }
1102 else
1103 {
1104 short lookup_value = r_ms.ReadInt16();
1105 _16bit_signed_comparer_ comparer = new _16bit_signed_comparer_(SearchArgs, SearchArgs.Results[i].Address);
1106 short value = 0;
1107 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1108 {
1109 value = Convert.ToInt16(SearchArgs.Results[i].Value);
1110 }
1111 else
1112 {
1113 value = Convert.ToInt16(SearchArgs.CompareStartValue);
1114 }
1115 if (comparer.Compare(lookup_value, value))
1116 {
1117 //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1118 //second_tmp_Results.Add(_tmp_result);
1119 //_tmp_result = null; // free memory
1120 //SearchArgs.Results.RemoveAt(i);
1121 SearchArgs.Results[i].Value = comparer.Value;
1122 second_tmp_Results.Add(SearchArgs.Results[i]);
1123 }
1124 comparer = null; // free memory
1125 }
1126 break;
1127 case SearchDataTypes._32bits:
1128 if (SearchArgs.IsUnsignedDataType)
1129 {
1130 uint lookup_value = r_ms.ReadUInt32();
1131 _32bit_unsigned_comparer_ comparer = new _32bit_unsigned_comparer_(SearchArgs, SearchArgs.Results[i].Address);
1132 uint value = 0;
1133 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1134 {
1135 value = Convert.ToUInt32(SearchArgs.Results[i].Value);
1136 comparer.Value = value;
1137 }
1138 else
1139 {
1140 value = Convert.ToUInt32(SearchArgs.CompareStartValue);
1141 comparer.Value = value;
1142 }
1143 if (comparer.Compare(lookup_value, value))
1144 {
1145 //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1146 //second_tmp_Results.Add(_tmp_result);
1147 //_tmp_result = null; // free memory
1148 //SearchArgs.Results.RemoveAt(i);
1149 SearchArgs.Results[i].Value = comparer.Value;
1150 second_tmp_Results.Add(SearchArgs.Results[i]);
1151 }
1152 comparer = null; // free memory
1153 }
1154 else
1155 {
1156 int lookup_value = r_ms.ReadInt32();
1157 _32bit_signed_comparer_ comparer = new _32bit_signed_comparer_(SearchArgs, SearchArgs.Results[i].Address);
1158 int value = 0;
1159 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1160 {
1161 value = Convert.ToInt32(SearchArgs.Results[i].Value);
1162 }
1163 else
1164 {
1165 value = Convert.ToInt32(SearchArgs.CompareStartValue);
1166 }
1167 if (comparer.Compare(lookup_value, value))
1168 {
1169 //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1170 //second_tmp_Results.Add(_tmp_result);
1171 //_tmp_result = null; // free memory
1172 //SearchArgs.Results.RemoveAt(i);
1173 SearchArgs.Results[i].Value = comparer.Value;
1174 second_tmp_Results.Add(SearchArgs.Results[i]);
1175 }
1176 comparer = null; // free memory
1177 }
1178 break;
1179 case SearchDataTypes._64bits:
1180 if (SearchArgs.IsUnsignedDataType)
1181 {
1182 ulong lookup_value = r_ms.ReadUInt64();
1183 _64bit_unsigned_comparer_ comparer = new _64bit_unsigned_comparer_(SearchArgs, SearchArgs.Results[i].Address);
1184 ulong value = 0;
1185 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1186 {
1187 value = Convert.ToUInt64(SearchArgs.Results[i].Value);
1188 comparer.Value = value;
1189 }
1190 else
1191 {
1192 value = Convert.ToUInt64(SearchArgs.CompareStartValue);
1193 comparer.Value = value;
1194 }
1195 if (comparer.Compare(lookup_value, value))
1196 {
1197 //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1198 //second_tmp_Results.Add(_tmp_result);
1199 //_tmp_result = null; // free memory
1200 //SearchArgs.Results.RemoveAt(i);
1201 SearchArgs.Results[i].Value = comparer.Value;
1202 second_tmp_Results.Add(SearchArgs.Results[i]);
1203 }
1204 comparer = null; // free memory
1205 }
1206 else
1207 {
1208 long lookup_value = r_ms.ReadInt64();
1209 _64bit_signed_comparer_ comparer = new _64bit_signed_comparer_(SearchArgs, SearchArgs.Results[i].Address);
1210 long value = 0;
1211 if (SearchArgs.CompareValueType == CompareValueTypes.OldValue)
1212 {
1213 value = Convert.ToInt64(SearchArgs.Results[i].Value);
1214 }
1215 else
1216 {
1217 value = Convert.ToInt64(SearchArgs.CompareStartValue);
1218 }
1219 if (comparer.Compare(lookup_value, value))
1220 {
1221 //ResultType<object> _tmp_result = new ResultType<object>(comparer.Address, comparer.Value);
1222 //second_tmp_Results.Add(_tmp_result);
1223 //_tmp_result = null; // free memory
1224 //SearchArgs.Results.RemoveAt(i);
1225 SearchArgs.Results[i].Value = comparer.Value;
1226 second_tmp_Results.Add(SearchArgs.Results[i]);
1227 }
1228 comparer = null; // free memory
1229 }
1230 break;
1231 #endregion
1232 }
1233
1234 double double_percent_done = 100.0 * (double)((double)i / (double)SearchArgs.Results.Count);
1235 int int_percent_done = (int)double_percent_done;
1236 if ((i / UPDATE_DELAY) == (int)(i / UPDATE_DELAY) && int_percent_done != Last_Whole_Percent_Done)
1237 {
1238 resultsprogress.Value = int_percent_done;
1239 resultsprogress.Message = string.Format(" -> Reading Address: 0x{0:x8}", i);
1240 Last_Whole_Percent_Done = int_percent_done;
1241 Application.DoEvents();
1242 }
1243
1244 }
1245 #endregion
1246 }
1247 #region Ranged Searches
1248 #if !DONOT_HAVE_RANGED_SEARCH_SUPPORT
1249 if (SearchArgs.CompareType == SearchCompareTypes.Between || SearchArgs.CompareType == SearchCompareTypes.NotBetween)
1250 {
1251 object start, end;
1252
1253 start = SearchArgs.CompareStartValue;
1254 end = SearchArgs.CompareEndValue;
1255 for (int i = 0; i < SearchArgs.Results.Count; i += 1)
1256 {
1257 //r_ms.BaseStream.Seek(SearchArgs.Results[i].Address, SeekOrigin.Begin);
1258 if (SearchArgs.CompareType == SearchCompareTypes.Between)
1259 {
1260 InRangeComparer comparer = new InRangeComparer(SearchArgs.Results[i].Address, 0);
1261 if (comparer.Compare(start, end, SearchArgs.DataType, SearchArgs.IsUnsignedDataType, r_ms))
1262 {
1263 SearchArgs.Results[i].Value = comparer.Value;
1264 second_tmp_Results.Add(SearchArgs.Results[i]);
1265 }
1266 comparer = null;
1267 }
1268 else if (SearchArgs.CompareType == SearchCompareTypes.NotBetween)
1269 {
1270 NotInRangeComparer comparer = new NotInRangeComparer(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
1279 {
1280 throw new InvalidOperationException("Encounted unkown range search type: " + SearchArgs.CompareType);
1281 }
1282 double double_percent_done = 100.0 * (double)((double)i / (double)SearchArgs.Results.Count);
1283 int int_percent_done = (int)double_percent_done;
1284 if ((i / UPDATE_DELAY) == (int)(i / UPDATE_DELAY) && int_percent_done != Last_Whole_Percent_Done)
1285 {
1286 resultsprogress.Value = int_percent_done;
1287 resultsprogress.Message = string.Format(" -> Reading Address: 0x{0:x8}", i);
1288 Last_Whole_Percent_Done = int_percent_done;
1289 Application.DoEvents();
1290 }
1291 }
1292 }
1293 #endif
1294 #endregion
1295
1296 }
1297 #endregion
1298 // leave SearchArgs.Results alone, if false
1299 if (NeedToCompare)
1300 {
1301 SearchArgs.Results = second_tmp_Results.GetRange(0, second_tmp_Results.Count);
1302 second_tmp_Results = null; // free memory
1303 }
1304
1305 r_ms.Close();
1306 }
1307
1308 private void SearchWorkerThread_ProgressChanged(object sender, ProgressChangedEventArgs e)
1309 {
1310 //if (SearchArgs.ProgressLogger != null)
1311 //{
1312 // resultsprogress.Value = e.ProgressPercentage;
1313 // //Application.DoEvents();
1314 //}
1315 }
1316
1317 private void SearchWorkerThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
1318 {
1319 resultsprogress.Value = 100;
1320 logger.Debug.WriteLine(string.Format("\nResults Count -> 0x{0:x8}", SearchArgs.Results.Count));
1321
1322 if (SearchArgs.Results.Count == 1) // debug message for 1 result using 32bit unsigned
1323 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));
1324
1325 logger.Info.WriteLine(string.Format("\nFound 0x{0:x8} results", SearchArgs.Results.Count));
1326
1327 if (SearchArgs.Results.Count <= MIN_NUMBER_OF_RESULTS_BEFORE_DISPLAY)
1328 {
1329 lstResults.Items.Clear();
1330 List<ResultItem> items = new List<ResultItem>();
1331 for (int i = 0; i < SearchArgs.Results.Count; i++)
1332 {
1333 ResultItem item = new ResultItem(0, false);
1334 //item.Text = string.Format("0x{0:x8}", SearchArgs.Results[i].Address);
1335 //item.SubItems.Add(string.Format("0x{0:x8}", SearchArgs.Results[i].Address));
1336 switch (SearchArgs.DataType)
1337 {
1338
1339 case SearchDataTypes._8bits:
1340 if (SearchArgs.IsUnsignedDataType) { item = new ResultItem(SearchArgs.Results[i].Address, false, Convert.ToByte(SearchArgs.Results[i].Value)); }
1341 else { item = new ResultItem(SearchArgs.Results[i].Address, false, Convert.ToSByte(SearchArgs.Results[i].Value)); }
1342 break;
1343 case SearchDataTypes._16bits:
1344 if (SearchArgs.IsUnsignedDataType) { item = new ResultItem(SearchArgs.Results[i].Address, false, Convert.ToUInt16(SearchArgs.Results[i].Value)); }
1345 else { item = new ResultItem(SearchArgs.Results[i].Address, false, Convert.ToInt16(SearchArgs.Results[i].Value)); }
1346 break;
1347 case SearchDataTypes._32bits:
1348 if (SearchArgs.IsUnsignedDataType) { item = new ResultItem(SearchArgs.Results[i].Address, false, Convert.ToUInt32(SearchArgs.Results[i].Value)); }
1349 else { item = new ResultItem(SearchArgs.Results[i].Address, false, Convert.ToInt32(SearchArgs.Results[i].Value)); }
1350 break;
1351 case SearchDataTypes._64bits:
1352 if (SearchArgs.IsUnsignedDataType) { item = new ResultItem(SearchArgs.Results[i].Address, false, Convert.ToUInt64(SearchArgs.Results[i].Value)); }
1353 else { item = new ResultItem(SearchArgs.Results[i].Address, false, Convert.ToInt64(SearchArgs.Results[i].Value)); }
1354 break;
1355 }
1356
1357 if (!items.Contains(item))
1358 items.Add(item);
1359 }
1360 lstResults.Items.AddRange(items.ToArray());
1361 }
1362
1363 this.DoSearchDoneSpecific();
1364 //System.Threading.Thread.Sleep(100);
1365 //if (_SEARCH_MODE != SearchMode.SEARCH_MODE_NORMAL_WITH_JOKER)
1366 this.ThawResultsUpdate();
1367 Application.DoEvents();
1368 }
1369 private void DoSearchDoneSpecific()
1370 {
1371 SearchWorkerThread.CancelAsync();
1372 if (lstResults.Items.Count > 0) { timer_update_results.Enabled = true; }
1373 else { timer_update_results.Enabled = false; }
1374
1375 search_progress_updater.Enabled = false;
1376
1377 btnCancel.Enabled = false;
1378 btnReset.Enabled = true;
1379 btnSearch.Enabled = true;
1380 grpCompareType.Enabled = true;
1381 grpCompareValue.Enabled = true;
1382 resultsprogress.Value = 0;
1383 resultsprogress.Message = "";
1384 grpDataType.Enabled = false;
1385 // resume process on reset, incase it was suspended
1386 ThreadControl.ResumeProcess(this.AcceptedProcess.Id);
1387 //Application.DoEvents();
1388 this.Refresh();
1389 }
1390
1391 private void DoCancelSpecific()
1392 {
1393 this.DoSearchDoneSpecific();
1394 }
1395 private void DoResetSpecific()
1396 {
1397 this.DoCancelSpecific();
1398 IsFirstSearch = true;
1399 grpDataType.Enabled = true;
1400 }
1401 private void search_progress_updater_Tick(object sender, EventArgs e)
1402 {
1403 if ((this.AcceptedProcess ==null) || Process.GetProcessById(this.AcceptedProcess.Id) == null)
1404 {
1405 SearchWorkerThread.CancelAsync();
1406 //JokerSearchWorker.CancelAsync();
1407 ResultsUpdateWorkerThread.CancelAsync();
1408 }
1409 }
1410
1411 #region Search Button
1412 private void btnSearch_Click(object sender, EventArgs e)
1413 {
1414 this.SearchInProgess = true;
1415 //btnCancel.Enabled = true;
1416 //btnReset.Enabled = false;
1417 //btnSearch.Enabled = false;
1418 this.FreezeResultsUpdate();
1419 this.handle_btnSearch_Click();
1420 }
1421 private void handle_btnSearch_Click()
1422 {
1423 //this.FreezeResultsUpdate();
1424 lstResults.Items.Clear();
1425
1426 if (lstResults.Items.Count > 0) { timer_update_results.Enabled = true; }
1427 else { timer_update_results.Enabled = false; }
1428
1429
1430 resultsprogress.Value = 0;
1431 bool _is_unsigned = chkUnsigned.Checked;
1432 SearchType search_type = new SearchType();
1433 SearchDataTypes _data_type = new SearchDataTypes();
1434 SearchCompareTypes _compare_type = new SearchCompareTypes();
1435 CompareValueTypes _compare_value_type = new CompareValueTypes();
1436 object start_value = 0;
1437 object end_value = 0;
1438 // get datatype
1439 if (radio_8bits.Checked) { _data_type = SearchDataTypes._8bits; }
1440 else if (radio_16bits.Checked) { _data_type = SearchDataTypes._16bits; }
1441 else if (radio_32bits.Checked) { _data_type = SearchDataTypes._32bits; }
1442 else if (radio_64bits.Checked) { _data_type = SearchDataTypes._64bits; }
1443 else { logger.Error.WriteLine("Could not determine search data type bit size. (was not 8/16/32/or 64bits)"); }
1444 // get compare type
1445 if (radiocompare_equal.Checked) { _compare_type = SearchCompareTypes.Equal; }
1446 else if (radiocompare_greaterthan.Checked) { _compare_type = SearchCompareTypes.GreaterThan; }
1447 else if (radiocompare_lessthan.Checked) { _compare_type = SearchCompareTypes.LessThan; }
1448 else if (radiocompare_greaterthan_orequal.Checked) { _compare_type = SearchCompareTypes.GreaterThanOrEqual; }
1449 else if (radiocompare_lessthan_orequal.Checked) { _compare_type = SearchCompareTypes.LessThanOrEqual; }
1450 else if (radiocompare_notequal.Checked) { _compare_type = SearchCompareTypes.NotEqual; }
1451 else if (radiocompare_between.Checked) { _compare_type = SearchCompareTypes.Between; }
1452 else if (radiocompare_notbetween.Checked) { _compare_type = SearchCompareTypes.NotBetween; }
1453 else { logger.Error.WriteLine("Could not determine search comparison type. (was not == > < >= <= != <> or !<>)"); }
1454 // get compare valure type
1455 if (radio_oldvalue.Checked) { _compare_value_type = CompareValueTypes.OldValue; }
1456 else if (radio_specificvalue.Checked) { _compare_value_type = CompareValueTypes.SpecificValue; }
1457 else { logger.Error.WriteLine("Could not determine search comparison type. (was not old or specific value"); }
1458
1459 if (_compare_value_type == CompareValueTypes.SpecificValue || (_compare_type == SearchCompareTypes.Between || _compare_type == SearchCompareTypes.NotBetween))
1460 {
1461
1462 switch (_data_type)
1463 {
1464 case SearchDataTypes._8bits:
1465 if (_is_unsigned) { start_value = txtStartAddr.ToByte(); }
1466 else { start_value = txtStartAddr.ToSByte(); }
1467 break;
1468 case SearchDataTypes._16bits:
1469 if (_is_unsigned) { start_value = txtStartAddr.ToUInt16(); }
1470 else { start_value = txtStartAddr.ToInt16(); }
1471 break;
1472 case SearchDataTypes._32bits:
1473 if (_is_unsigned) { start_value = txtStartAddr.ToUInt32(); }
1474 else { start_value = txtStartAddr.ToInt32(); }
1475 break;
1476 case SearchDataTypes._64bits:
1477 if (_is_unsigned) { start_value = txtStartAddr.ToUInt64(); }
1478 else { start_value = txtStartAddr.ToInt64(); }
1479 break;
1480 default: throw new InvalidOperationException("In SearchType(): Encounterd an Unkown Search Data Type.");
1481 }
1482 }
1483 if (_compare_type == SearchCompareTypes.Between || _compare_type == SearchCompareTypes.NotBetween)
1484 {
1485 switch (_data_type)
1486 {
1487 case SearchDataTypes._8bits:
1488 if (_is_unsigned) { end_value = txtEndAddr.ToByte(); }
1489 else { end_value = txtEndAddr.ToSByte(); }
1490 break;
1491 case SearchDataTypes._16bits:
1492 if (_is_unsigned) { end_value = txtEndAddr.ToUInt16(); }
1493 else { end_value = txtEndAddr.ToInt16(); }
1494 break;
1495 case SearchDataTypes._32bits:
1496 if (_is_unsigned) { end_value = txtEndAddr.ToUInt32(); }
1497 else { end_value = txtEndAddr.ToInt32(); }
1498 break;
1499 case SearchDataTypes._64bits:
1500 if (_is_unsigned) { end_value = txtEndAddr.ToUInt64(); }
1501 else { end_value = txtEndAddr.ToInt64(); }
1502 break;
1503 default: throw new InvalidOperationException("In SearchType(): Encounterd an Unkown Search Data Type.");
1504 }
1505 }
1506
1507 search_type = new SearchType(_data_type, _is_unsigned, _compare_type, _compare_value_type, start_value, end_value, resultsprogress);
1508
1509 //search_type.LogSearchOptions();
1510
1511 search_type.IsFirstSearch = IsFirstSearch;
1512
1513
1514
1515 DoSearch(search_type);
1516 IsFirstSearch = false;
1517 }
1518 private void DoSearch(SearchType args)
1519 {
1520 if (!args.IsFirstSearch && SearchArgs != null)
1521 {
1522 //args.Results.AddRange(SearchArgs.Results.ToArray());
1523 args.Results = SearchArgs.Results;
1524 }
1525 SearchArgs = args;
1526 #if DONOT_HAVE_RANGED_SEARCH_SUPPORT
1527 if (SearchArgs.CompareType == SearchCompareTypes.Between || SearchArgs.CompareType == SearchCompareTypes.NotBetween)
1528 {
1529 throw new NotImplementedException("Between and Not Between Range searches have not been implemented.");
1530 }
1531 #endif
1532 search_progress_updater.Enabled = true;
1533 //padPluginSelector.Enabled = false;
1534 //gsPluginSelector.Enabled = false;
1535 btnReset.Enabled = false;
1536 btnSearch.Enabled = false;
1537 btnCancel.Enabled = true;
1538 grpDataType.Enabled = false;
1539 grpCompareType.Enabled = false;
1540 grpCompareValue.Enabled = false;
1541 this.Refresh();
1542 Application.DoEvents();
1543 SearchWorkerThread.RunWorkerAsync();
1544 }
1545 #endregion
1546 private void btnReset_Click(object sender, EventArgs e)
1547 {
1548 this.SearchInProgess = false;
1549 //btnSearch.Enabled = true;
1550 //btnCancel.Enabled = false;
1551 this.DoResetSpecific();
1552 lstResults.Items.Clear();
1553 try { SearchArgs.Results = new List<ResultType<object>>(); }
1554 catch { }
1555 }
1556
1557 private void btnCancel_Click(object sender, EventArgs e)
1558 {
1559 this.SearchInProgess = false;
1560 //btnCancel.Enabled = false;
1561 //btnSearch.Enabled = true;
1562 //btnReset.Enabled = true;
1563 this.DoCancelSpecific();
1564 }
1565
1566 private void mnuItemPatchListViewMemoryRegion_Click(object sender, EventArgs e)
1567 {
1568 List<ResultDataType> patch_list = new List<ResultDataType>();
1569 List<int> SelectedIndexes = new List<int>();
1570 foreach (int index in lstPatchList.SelectedIndices) { SelectedIndexes.Add(index); }
1571 foreach (int index in SelectedIndexes)
1572 {
1573 ListViewItem item = lstPatchList.Items[index];
1574 ResultDataType rdt = (ResultDataType)item.Tag;
1575 ViewMemoryRegion(rdt);
1576 break; // only get the fist item
1577 }
1578 }
1579
1580 private void mnuItemResultsListViewMemoryRegion_Click(object sender, EventArgs e)
1581 {
1582 List<ResultDataType> patch_list = new List<ResultDataType>();
1583 List<int> SelectedIndexes = new List<int>();
1584 foreach (int index in lstResults.SelectedIndices) { SelectedIndexes.Add(index); }
1585 foreach (int index in SelectedIndexes)
1586 {
1587 ListViewItem item = lstResults.Items[index];
1588 ResultDataType rdt = (ResultDataType)item.Tag;
1589 ViewMemoryRegion(rdt);
1590 break; // only get the fist item
1591 }
1592 }
1593 private void ViewMemoryRegion(ResultDataType rdt)
1594 {
1595 if (OnBrowseMemoryRegion != null)
1596 {
1597 OnBrowseMemoryRegion(new BrowseMemoryRegionEvent(rdt.Address));
1598 }
1599 }
1600
1601 private void mnuAddedResults_Opening(object sender, CancelEventArgs e)
1602 {
1603 if (!(lstPatchList.Items.Count > 0)) { mnuItemRemoveResult.Visible = false; e.Cancel = true; }
1604 if (!(lstPatchList.Items.Count > 0)) { mnuItemPatchSelectedEntry.Visible = false; e.Cancel = true; }
1605 if (e.Cancel) return;
1606 if (lstPatchList.Items.Count > 0) mnuItemRemoveResult.Visible = true;
1607 if (lstPatchList.Items.Count > 0) mnuItemPatchSelectedEntry.Visible = true;
1608
1609 if (!(lstPatchList.Items.Count > 0)) { mnuItemFreezeSelectedPatches.Visible = false; e.Cancel = true; }
1610 if (!(lstPatchList.Items.Count > 0)) { mnuItemThawSelectedPatches.Visible = false; e.Cancel = true; }
1611 if (e.Cancel) return;
1612
1613 if (lstPatchList.Items.Count > 0) mnuItemFreezeSelectedPatches.Visible = true;
1614 if (lstPatchList.Items.Count > 0) mnuItemThawSelectedPatches.Visible = true;
1615
1616 if (lstPatchList.SelectedItems.Count == 0) e.Cancel = true;
1617 if (e.Cancel) return;
1618
1619 }
1620
1621 private void mnuResults_Opening(object sender, CancelEventArgs e)
1622 {
1623 if (!(lstResults.Items.Count > 0)) e.Cancel = true;
1624 if (lstResults.SelectedItems.Count == 0) e.Cancel = true;
1625 if (SearchArgs == null) e.Cancel = true;
1626 if (e.Cancel) return;
1627 }
1628 }
1629 }