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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 275 - (hide annotations) (download)
Sun Jun 3 22:01:05 2012 UTC (8 years, 4 months ago) by william
File size: 43791 byte(s)
+ add BrowseMemoryRegion event in MemorySearcher
: calls back to main form which calls MemoryViewer.AcceptExternalMemoryAddress(address)

1 william 229 //#define DO_NOT_SUSPEND_RESUME_THREAD_ON_FREEZE // when defined will not freeze/resume thread on freeze
2     using System;
3 william 227 using System.Collections.Generic;
4     using System.ComponentModel;
5     using System.Data;
6     using System.Drawing;
7     using System.Linq;
8     using System.Text;
9     using System.Windows.Forms;
10     using WeifenLuo.WinFormsUI.Docking;
11     using RomCheater.PluginFramework.Interfaces;
12     using System.Diagnostics;
13 william 229 using RomCheater.Docking.MemorySearch;
14     using libWin32.Win32.Threading;
15 william 230 using System.Threading;
16     using RomCheater.Logging;
17     using System.IO;
18 william 242 using Sojaner.MemoryScanner.MemoryProviers;
19 william 275 using RomCheater.PluginFramework.Events;
20 william 227
21     namespace RomCheater.Docking
22     {
23 william 228 public partial class FloatingMemorySearcher : DockContent,
24 william 243 IAcceptsPlugin<IConfigPlugin>,
25     IAcceptsProcess<Process>,
26     IAcceptsProcessAndConfig,
27 william 275 ISearchInProgress,
28     IAcceptsBrowseMemoryRegion
29 william 227 {
30 william 229 private bool DefaultUnsignedState = true; // set unsigned to true
31     public FloatingMemorySearcher() { InitializeComponent(); this.AcceptedPlugin = null; this.AcceptedProcess = null; SearchInProgess = false; Reload(); }
32 william 227 public FloatingMemorySearcher(IConfigPlugin config) : this() { this.AcceptedPlugin = config; }
33     public FloatingMemorySearcher(IConfigPlugin config, Process process) : this() { this.AcceptedPlugin = config; this.AcceptedProcess = process; }
34    
35 william 237 #region IAcceptsProcess<Process> Members
36 william 243 private Process _AcceptedProcess;
37     public Process AcceptedProcess { get { return _AcceptedProcess; } set { _AcceptedProcess = value; UpdateAcceptedProcessAndConfig(AcceptedPlugin, value); } }
38 william 227 #endregion
39     #region IAcceptsPlugin<IConfigPlugin> Members
40 william 243 private IConfigPlugin _AcceptedPlugin;
41     public IConfigPlugin AcceptedPlugin { get { return _AcceptedPlugin; } set { _AcceptedPlugin = value; UpdateAcceptedProcessAndConfig(value, AcceptedProcess); } }
42 william 227 #endregion
43 william 275 #region IAcceptsBrowseMemoryRegion members
44     public BaseEventHandler<BrowseMemoryRegionEvent> OnBrowseMemoryRegion { get; set; }
45     #endregion
46    
47 william 243 private void UpdateAcceptedProcessAndConfig(IConfigPlugin config, Process process)
48     {
49     this.lstResults.AcceptedPlugin = config;
50     this.lstResults.AcceptedProcess = process;
51     this.lstPatchList.AcceptedPlugin = config;
52     this.lstPatchList.AcceptedProcess = process;
53     }
54 william 228 #region ISearchInProgress members
55     public bool SearchInProgess { get; private set; }
56     #endregion
57 william 229
58    
59     public void Reload()
60     {
61     chkUnsigned.Checked = DefaultUnsignedState;
62     radio_8bits.Checked = true;
63     radiocompare_equal.Checked = true;
64     radio_oldvalue.Checked = true;
65 william 230 chkRefreshResults.Checked = true;
66 william 229 }
67 william 230 public enum eListViewResults
68     {
69     SEARCH_RESULTS_LIST = 0x3000,
70     PATCH_RESULTS_LIST = 0x3001,
71     UKNOWN_RESULTS_LIST = 0x3001
72     }
73 william 272 static int col_Found_Address = 1;
74     static int col_Found_Value = 2;
75     static int col_Found_Frozen = 3;
76     static int col_Added_Address = 1;
77     static int col_Added_Value = 2;
78     static int col_Added_Frozen = 3;
79 william 230 List<ListViewItem> ResultItems = new List<ListViewItem>();
80     List<ListViewItem> AddedItems = new List<ListViewItem>();
81     private bool _PatchedValue_NeedsUpdate;
82     bool PatchedValue_NeedsUpdate
83     {
84     get { if (_PatchedValue_NeedsUpdate) this.ThawResultsUpdate(); return _PatchedValue_NeedsUpdate; }
85     set { _PatchedValue_NeedsUpdate = value; if (value) this.ThawResultsUpdate(); }
86     }
87     private delegate ListViewItem ThreadSafe_GetResultItem(int index, int lv_type);
88     private ListViewItem GetResultItem(int index, int lv_type)
89     {
90     try
91     {
92     AddressValuePairList lv = null;
93     switch (lv_type)
94     {
95     case (int)eListViewResults.SEARCH_RESULTS_LIST: lv = lstResults; break;
96     case (int)eListViewResults.PATCH_RESULTS_LIST: lv = lstPatchList; break;
97     default: throw new IndexOutOfRangeException("Detected: " + Enum.GetName(typeof(eListViewResults), eListViewResults.UKNOWN_RESULTS_LIST) + " with value: " + lv_type.ToString("x4"));
98     }
99     ListViewItem item = new ListViewItem();
100     item = (ListViewItem)lv.Items[index].Clone();
101     return item;
102     }
103     catch (Exception)
104     {
105     return null;
106     }
107     }
108 william 229 private void radiocompare_equal_CheckedChanged(object sender, EventArgs e)
109     {
110     //if (!radiocompare_between.Checked && !radiocompare_notbetween.Checked)
111     //{
112     if (radio_oldvalue.Checked)
113     {
114     txtStartAddr.ReadOnly = true;
115     txtEndAddr.ReadOnly = true;
116     }
117     if (radio_specificvalue.Checked)
118     {
119     txtStartAddr.ReadOnly = false;
120     txtEndAddr.ReadOnly = true;
121     }
122     //}
123     }
124    
125     private void radiocompare_between_CheckedChanged(object sender, EventArgs e)
126     {
127     if (!radiocompare_equal.Checked &&
128     !radiocompare_greaterthan.Checked &&
129     !radiocompare_greaterthan.Checked &&
130     !radiocompare_lessthan.Checked &&
131     !radiocompare_greaterthan_orequal.Checked &&
132     !radiocompare_lessthan_orequal.Checked &&
133     !radiocompare_notequal.Checked)
134     if (radiocompare_between.Checked)
135     {
136     txtStartAddr.ReadOnly = false;
137     txtEndAddr.ReadOnly = false;
138     return;
139     }
140     if (!radiocompare_between.Checked && !radiocompare_notbetween.Checked)
141     {
142     if (radio_oldvalue.Checked)
143     {
144     txtStartAddr.ReadOnly = true;
145     txtEndAddr.ReadOnly = true;
146     }
147     if (radio_specificvalue.Checked)
148     {
149     txtStartAddr.ReadOnly = false;
150     txtEndAddr.ReadOnly = true;
151     }
152     }
153     }
154    
155     private void radiocompare_notbetween_CheckedChanged(object sender, EventArgs e)
156     {
157     if (!radiocompare_equal.Checked &&
158     !radiocompare_greaterthan.Checked &&
159     !radiocompare_greaterthan.Checked &&
160     !radiocompare_lessthan.Checked &&
161     !radiocompare_greaterthan_orequal.Checked &&
162     !radiocompare_lessthan_orequal.Checked &&
163     !radiocompare_notequal.Checked)
164     if (radiocompare_notbetween.Checked)
165     {
166     txtStartAddr.ReadOnly = false;
167     txtEndAddr.ReadOnly = false;
168     return;
169     }
170     if (!radiocompare_between.Checked && !radiocompare_notbetween.Checked)
171     {
172     if (radio_oldvalue.Checked)
173     {
174     txtStartAddr.ReadOnly = true;
175     txtEndAddr.ReadOnly = true;
176     }
177     if (radio_specificvalue.Checked)
178     {
179     txtStartAddr.ReadOnly = false;
180     txtEndAddr.ReadOnly = true;
181     }
182     }
183     }
184    
185     private void radio_8bits_CheckedChanged(object sender, EventArgs e)
186     {
187     if (chkUnsigned.Checked)
188     {
189     txtStartAddr.CreateTypeSize<byte>();
190     txtEndAddr.CreateTypeSize<byte>();
191     }
192     else
193     {
194     txtStartAddr.CreateTypeSize<sbyte>();
195     txtEndAddr.CreateTypeSize<sbyte>();
196     }
197     }
198    
199     private void radio_16bits_CheckedChanged(object sender, EventArgs e)
200     {
201     if (chkUnsigned.Checked)
202     {
203     txtStartAddr.CreateTypeSize<ushort>();
204     txtEndAddr.CreateTypeSize<ushort>();
205     }
206     else
207     {
208     txtStartAddr.CreateTypeSize<short>();
209     txtEndAddr.CreateTypeSize<short>();
210     }
211     }
212    
213     private void radio_32bits_CheckedChanged(object sender, EventArgs e)
214     {
215    
216     if (chkUnsigned.Checked)
217     {
218     txtStartAddr.CreateTypeSize<uint>();
219     txtEndAddr.CreateTypeSize<uint>();
220     }
221     else
222     {
223     txtStartAddr.CreateTypeSize<int>();
224     txtEndAddr.CreateTypeSize<int>();
225     }
226     }
227    
228     private void radio_64bits_CheckedChanged(object sender, EventArgs e)
229     {
230    
231     if (chkUnsigned.Checked)
232     {
233     txtStartAddr.CreateTypeSize<ulong>();
234     txtEndAddr.CreateTypeSize<ulong>();
235     }
236     else
237     {
238     txtStartAddr.CreateTypeSize<long>();
239     txtEndAddr.CreateTypeSize<long>();
240     }
241     }
242    
243     private void radio_oldvalue_CheckedChanged(object sender, EventArgs e)
244     {
245     if (!radiocompare_between.Checked && !radiocompare_notbetween.Checked)
246     {
247     txtStartAddr.ReadOnly = true;
248     txtEndAddr.ReadOnly = true;
249     }
250     }
251    
252     private void radio_specificvalue_CheckedChanged(object sender, EventArgs e)
253     {
254     if (!radiocompare_between.Checked && !radiocompare_notbetween.Checked)
255     {
256     txtStartAddr.ReadOnly = false;
257     txtEndAddr.ReadOnly = true;
258     }
259     }
260    
261     private void chkRefreshResults_CheckedChanged(object sender, EventArgs e)
262     {
263     if (chkRefreshResults.Checked)
264     {
265     timer_update_results.Enabled = true;
266     }
267     else
268     {
269     timer_update_results.Enabled = false;
270     ResultsUpdateWorkerThread.CancelAsync();
271     }
272     }
273    
274     private void timer_update_results_Tick(object sender, EventArgs e)
275     {
276     if (chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
277     {
278     ResultsUpdateWorkerThread.RunWorkerAsync();
279     }
280     }
281 william 230 private bool ShouldUpdateResults()
282     {
283     if (this.AcceptedProcess == null) return false;
284     if (SearchWorkerThread.IsBusy) return false;
285     //if (JokerSearchWorker.IsBusy) return false;
286     if (this.IsResultsUpdateFrozen) return false;
287     if (mnuAddedResults.Visible) return false;
288     if (mnuResults.Visible) return false;
289     if (Process.GetProcessById(this.AcceptedProcess.Id) == null) return false;
290     if (lstResults.Items.Count > 0) return true;
291     if (lstPatchList.Items.Count > 0) return true;
292     return false;
293     }
294 william 229 private void ResultsUpdateWorkerThread_DoWork(object sender, DoWorkEventArgs e)
295     {
296 william 230 Thread.Sleep(250); // keep thread from blocking
297     if (!this.ShouldUpdateResults()) return;
298     ////if (SearchArgs == null) return;
299     ////if (this.IsResultsUpdateFrozen) return;
300     ////// put thread to sleep for 500ms
301     ////System.Threading.Thread.Sleep(500);
302     //PCSX2MemoryProvider provider = new PCSX2MemoryProvider(this.SearchPCSX2ProcessPID, resultslog);
303     //byte[] buffered_mem = provider.GetMemory();
304     //MemoryStream ms = new MemoryStream(buffered_mem);
305     //BinaryReader r_ms = new BinaryReader(ms);
306 william 229
307 william 230 #region Update Results List
308     ResultItems = new List<ListViewItem>();
309     //r_ms.BaseStream.Seek(0, SeekOrigin.Begin);
310     for (int i = 0; i < lstResults.Items.Count; i++)
311     {
312     if (this.lstResults.InvokeRequired)
313     {
314     ThreadSafe_GetResultItem _get_item = new ThreadSafe_GetResultItem(GetResultItem);
315     object item = this.lstResults.Invoke(_get_item, new object[] { i, (int)eListViewResults.SEARCH_RESULTS_LIST });
316     if (item != null)
317     ResultItems.Add((ListViewItem)item);
318     }
319     else
320     {
321     ResultItems.Add(lstResults.Items[i]);
322     }
323    
324     }
325     for (int i = 0; i < ResultItems.Count; i++)
326     {
327     if (ResultsUpdateWorkerThread.CancellationPending == true)
328     {
329     e.Cancel = true;
330     return;
331     }
332 william 249 int Address = 0;
333 william 230 ResultDataType _result = (ResultDataType)ResultItems[i].Tag;
334    
335 william 249 Address = Convert.ToInt32(ResultItems[i].SubItems[col_Found_Address].Text, 16);
336 william 242 //r_ms.BaseStream.Seek(Address, SeekOrigin.Begin);
337     GenericMemoryProvider provider = new GenericMemoryProvider((IAcceptsProcessAndConfig)this);
338 william 245 provider.OpenProvider();
339 william 230 int bytesReadSize;
340 william 242 byte[] data;
341 william 230 uint bytesToRead = 0;
342     switch (_result.ValueType)
343     {
344     case SearchDataTypes._8bits:
345     bytesToRead = 1;
346     break;
347     case SearchDataTypes._16bits:
348     bytesToRead = 2;
349     break;
350     case SearchDataTypes._32bits:
351     bytesToRead = 4;
352     break;
353     case SearchDataTypes._64bits:
354     bytesToRead = 8;
355     break;
356     }
357 william 242 provider.ReadProcessMemory(Address, bytesToRead, out bytesReadSize, out data);
358 william 230 MemoryStream ms = new MemoryStream(data);
359     BinaryReader r_ms = new BinaryReader(ms);
360     switch (_result.ValueType)
361     {
362     case SearchDataTypes._8bits:
363     if (_result.IsUnsigned) { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x2}", r_ms.ReadByte()); }
364     else { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x2}", r_ms.ReadSByte()); }
365     break;
366     case SearchDataTypes._16bits:
367     if (_result.IsUnsigned) { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x4}", r_ms.ReadUInt16()); }
368     else { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x4}", r_ms.ReadInt16()); }
369     break;
370     case SearchDataTypes._32bits:
371     if (_result.IsUnsigned) { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x8}", r_ms.ReadUInt32()); }
372     else { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x8}", r_ms.ReadInt32()); }
373     break;
374     case SearchDataTypes._64bits:
375     if (_result.IsUnsigned) { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x16}", r_ms.ReadUInt64()); }
376     else { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x16}", r_ms.ReadInt64()); }
377     break;
378     }
379     r_ms.Close();
380 william 245 provider.CloseProvider();
381 william 230 Application.DoEvents();
382     }
383     #endregion
384    
385     #region Update Added Results List
386     AddedItems = new List<ListViewItem>();
387     //r_ms.BaseStream.Seek(0, SeekOrigin.Begin);
388     for (int i = 0; i < lstPatchList.Items.Count; i++)
389     {
390     if (this.lstResults.InvokeRequired)
391     {
392     ThreadSafe_GetResultItem _get_item = new ThreadSafe_GetResultItem(GetResultItem);
393     object item = this.lstResults.Invoke(_get_item, new object[] { i, (int)eListViewResults.PATCH_RESULTS_LIST });
394     if (item != null)
395     AddedItems.Add((ListViewItem)item);
396     }
397     else
398     {
399     AddedItems.Add(lstPatchList.Items[i]);
400     }
401    
402     }
403     for (int i = 0; i < AddedItems.Count; i++)
404     {
405     if (ResultsUpdateWorkerThread.CancellationPending == true)
406     {
407     e.Cancel = true;
408     return;
409     }
410 william 249 int Address = 0;
411 william 230 ResultDataType _result = (ResultDataType)AddedItems[i].Tag;
412 william 249 Address = Convert.ToInt32(AddedItems[i].SubItems[col_Added_Address].Text, 16);
413 william 242 GenericMemoryProvider provider = new GenericMemoryProvider((IAcceptsProcessAndConfig)this);
414 william 245 provider.OpenProvider();
415 william 230 int bytesReadSize;
416     byte[] data;
417     uint bytesToRead = 0;
418     switch (_result.ValueType)
419     {
420     case SearchDataTypes._8bits:
421     bytesToRead = 1;
422     break;
423     case SearchDataTypes._16bits:
424     bytesToRead = 2;
425     break;
426     case SearchDataTypes._32bits:
427     bytesToRead = 4;
428     break;
429     case SearchDataTypes._64bits:
430     bytesToRead = 8;
431     break;
432     }
433 william 242 provider.ReadProcessMemory(Address, bytesToRead, out bytesReadSize, out data);
434 william 230 MemoryStream ms = new MemoryStream(data);
435     BinaryReader r_ms = new BinaryReader(ms);
436     switch (_result.ValueType)
437     {
438     case SearchDataTypes._8bits:
439     if (_result.IsUnsigned) { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x2}", r_ms.ReadByte()); }
440     else { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x2}", r_ms.ReadSByte()); }
441     break;
442     case SearchDataTypes._16bits:
443     if (_result.IsUnsigned) { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x4}", r_ms.ReadUInt16()); }
444     else { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x4}", r_ms.ReadInt16()); }
445     break;
446     case SearchDataTypes._32bits:
447     if (_result.IsUnsigned) { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x8}", r_ms.ReadUInt32()); }
448     else { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x8}", r_ms.ReadInt32()); }
449     break;
450     case SearchDataTypes._64bits:
451     if (_result.IsUnsigned) { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x16}", r_ms.ReadUInt64()); }
452     else { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x16}", r_ms.ReadInt64()); }
453     break;
454     }
455     r_ms.Close();
456 william 245 provider.CloseProvider();
457 william 230 Application.DoEvents();
458     }
459     #endregion
460    
461    
462 william 229 }
463    
464     private void ResultsUpdateWorkerThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
465     {
466 william 230 try
467     {
468     //if ((lstResults.SelectedItems.Count > 0) && !PatchedValue_NeedsUpdate ) return;
469     //if ((lstPatchList.SelectedItems.Count > 0) && !PatchedValue_NeedsUpdate) return;
470     if (!this.ShouldUpdateResults()) return;
471     if (ResultItems.Count > 0)
472     {
473     //lstResults.Items.Clear();
474     //lstResults.Items.AddRange(ResultItems.ToArray());
475 william 229
476 william 230 for (int i = 0; i < ResultItems.Count; i++)
477     {
478     lstResults.Items[i].SubItems[new AVPColumnText(AVPColumnType.VALUE).ColumnIndex].Text =
479     ResultItems[i].SubItems[new AVPColumnText(AVPColumnType.VALUE).ColumnIndex].Text;
480     }
481    
482     }
483     if (AddedItems.Count > 0)
484     {
485     //lstPatchList.Items.Clear();
486     //lstPatchList.Items.AddRange(AddedItems.ToArray());
487    
488     for (int i = 0; i < AddedItems.Count; i++)
489     {
490     lstPatchList.Items[i].SubItems[new AVPColumnText(AVPColumnType.VALUE).ColumnIndex].Text =
491     AddedItems[i].SubItems[new AVPColumnText(AVPColumnType.VALUE).ColumnIndex].Text;
492     }
493    
494     }
495     PatchedValue_NeedsUpdate = false;
496     }
497     catch { }
498 william 229 }
499    
500     private void btnImportFile_Click(object sender, EventArgs e)
501     {
502     this.FreezeResultsUpdate();
503     if (!lstPatchList.ImportFromFile())
504     {
505     MessageBox.Show("Failed to Import Patch List from File.", "Import Failure", MessageBoxButtons.OK, MessageBoxIcon.Error);
506     this.ThawResultsUpdate();
507     return;
508     }
509     else
510     {
511     MessageBox.Show("Succesfully Imported Patch List from File.", "Import Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
512     this.ThawResultsUpdate();
513     return;
514     }
515     }
516     bool g_isFrozen = false;
517     private bool IsResultsUpdateFrozen
518     {
519     get { return g_isFrozen; }
520     set { g_isFrozen = value; }
521     }
522     private void ThawResultsUpdate()
523     {
524     this.IsResultsUpdateFrozen = false;
525     if (this.AcceptedProcess != null)
526     {
527     #if !DO_NOT_SUSPEND_RESUME_THREAD_ON_FREEZE
528     ThreadControl.ResumeProcess(this.AcceptedProcess.Id);
529     #endif
530     }
531     }
532    
533     private void FreezeResultsUpdate()
534     {
535     this.IsResultsUpdateFrozen = true;
536     this.IsResultsUpdateFrozen = false;
537     if (this.AcceptedProcess != null)
538     {
539     #if !DO_NOT_SUSPEND_RESUME_THREAD_ON_FREEZE
540     ThreadControl.SuspendProcess(this.AcceptedProcess.Id);
541     #endif
542     }
543     }
544    
545     private void btnExportFile_Click(object sender, EventArgs e)
546     {
547     this.FreezeResultsUpdate();
548     if (!lstPatchList.ExportToFile())
549     {
550     MessageBox.Show("Failed to Export Patch List to File.", "Export Failure", MessageBoxButtons.OK, MessageBoxIcon.Error);
551     this.ThawResultsUpdate();
552     return;
553     }
554     else
555     {
556     MessageBox.Show("Succesfully Exported Patch List to File.", "Export Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
557     this.ThawResultsUpdate();
558     return;
559     }
560     }
561    
562     private void btnImportClipboard_Click(object sender, EventArgs e)
563     {
564     this.FreezeResultsUpdate();
565     if (!lstPatchList.ImportFromClipboard())
566     {
567     MessageBox.Show("Failed to Import Patch List from Clipboard.", "Import Failure", MessageBoxButtons.OK, MessageBoxIcon.Error);
568     this.ThawResultsUpdate();
569     return;
570     }
571     else
572     {
573     MessageBox.Show("Succesfully Import Patch List from Clipboard.", "Import Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
574     this.ThawResultsUpdate();
575     }
576     }
577    
578     private void btnExportClipboard_Click(object sender, EventArgs e)
579     {
580     this.FreezeResultsUpdate();
581     if (!lstPatchList.ExportToClipboard())
582     {
583     MessageBox.Show("Failed to Export Patch List to Clipboard.", "Export Failure", MessageBoxButtons.OK, MessageBoxIcon.Error);
584     this.ThawResultsUpdate();
585     return;
586     }
587     else
588     {
589     MessageBox.Show("Succesfully Exported Patch List to Clipboard.", "Export Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
590     this.ThawResultsUpdate();
591     return;
592     }
593     }
594    
595     private void btnAddPatchAddress_Click(object sender, EventArgs e)
596     {
597 william 245 PatchAdder adder = new PatchAdder((IAcceptsProcessAndConfig)this);
598 william 229 adder.ShowDialog();
599     if (adder.WasAPatchAdded) AddToPatchList(adder.AddedPatchValue);
600     }
601    
602     private void btnAddAddressRange_Click(object sender, EventArgs e)
603     {
604 william 245 PatchRangeAdder adder = new PatchRangeAdder((IAcceptsProcessAndConfig)this);
605 william 229 adder.ShowDialog();
606     if (adder.WasAPatchAdded) AddToPatchList(adder.AddedPatchValue);
607     }
608     private void AddToPatchList(List<ResultDataType> item) { foreach (ResultDataType data in item) { AddToPatchList(data); } }
609     private void AddToPatchList(ResultDataType item)
610     {
611     ResultItem item2 = null;
612     switch (item.ValueType)
613     {
614     case SearchDataTypes._8bits:
615     if (item.IsUnsigned) { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToByte(item.Value)); }
616     else { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToSByte(item.Value)); }
617     break;
618     case SearchDataTypes._16bits:
619     if (item.IsUnsigned) { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToUInt16(item.Value)); }
620     else { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToInt16(item.Value)); }
621     break;
622     case SearchDataTypes._32bits:
623     if (item.IsUnsigned) { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToUInt32(item.Value)); }
624     else { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToInt32(item.Value)); }
625     break;
626     case SearchDataTypes._64bits:
627     if (item.IsUnsigned) { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToUInt64(item.Value)); }
628     else { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToInt64(item.Value)); }
629     break;
630     }
631     this.AddToPatchList(item2);
632     }
633     private void AddToPatchList(ListViewItem item)
634     {
635     try
636     {
637     ResultDataType _result = (ResultDataType)item.Tag;
638     this.AddToPatchList(_result);
639     }
640     catch (InvalidCastException ex)
641     {
642     // unable to cast
643     MessageBox.Show(ex.Message, "Invalid Cast Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
644     }
645     catch (Exception ex)
646     {
647     // other exception
648     MessageBox.Show(ex.Message, "Unhandled Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
649     }
650     }
651     private void AddToPatchList(ResultItem item)
652     {
653     if (!lstPatchList.Items.Contains(item)) lstPatchList.Items.Add(item);
654     }
655     private void AddToPatchList(string address, SearchDataTypes bitsize, bool IsUnsigned)
656     {
657 william 245 ResultItemState state = new ResultItemState(address, bitsize, IsUnsigned, (IAcceptsProcessAndConfig)this);
658 william 229 ResultItem item = new ResultItem(state.Address, state.Value, state.Frozen, state.ValueType, state.IsUnsigned);
659     this.AddToPatchList(item);
660     }
661 william 230
662     private void mnuItemAddToPatchList_Click(object sender, EventArgs e)
663     {
664     if (!(lstResults.SelectedItems.Count > 0)) return;
665     //if (SearchArgs == null) return;
666    
667     try
668     {
669     for (int i = 0; i < lstResults.SelectedIndices.Count; i++)
670     {
671     //ResultDataType result = (ResultDataType)lstResults.Items[selected_index].Tag;
672     ListViewItem item = lstResults.Items[lstResults.SelectedIndices[i]];
673     this.AddToPatchList(item);
674     }
675     }
676     catch (Exception ex)
677     {
678     logger.Error.WriteLine(ex.ToString());
679     }
680     }
681    
682     private void mnuItemRemoveResult_Click(object sender, EventArgs e)
683     {
684     if (!(lstPatchList.SelectedItems.Count > 0)) return;
685     //if (SearchArgs == null) return;
686     try
687     {
688     this.FreezeResultsUpdate();
689     for (int i = 0; i < lstPatchList.SelectedIndices.Count; i++)
690     {
691     //lstPatchList.ThawItem(lstPatchList.SelectedIndices[i]);
692     lstPatchList.Items[lstPatchList.SelectedIndices[i]].Remove();
693     }
694     this.ThawResultsUpdate();
695     }
696     catch (Exception ex)
697     {
698     Debug.WriteLine(ex.ToString());
699     }
700     }
701     private void PatchRange(bool SingleEntry)
702     {
703     //if (SearchArgs == null) return;
704     #region Patch Selected Address
705     // stop ResultsUpdate Thread
706     ResultsUpdateWorkerThread.CancelAsync();
707    
708     List<ResultDataType> patch_list = new List<ResultDataType>();
709     List<int> SelectedIndexes = new List<int>();
710    
711     if (SingleEntry)
712     {
713     SelectedIndexes.Add(lstPatchList.SelectedIndices[0]);
714     }
715     else
716     {
717     foreach (int index in lstPatchList.SelectedIndices)
718     {
719     SelectedIndexes.Add(index);
720     }
721     }
722     //PCSX2MemoryProvider provider = new PCSX2MemoryProvider(this.SearchPCSX2ProcessPID, resultslog);
723     foreach (int index in SelectedIndexes)
724     {
725     if (SingleEntry)
726     {
727     SearchPatcher patcher = null;
728     uint Address = 0;
729     ListViewItem item = lstPatchList.Items[index];
730     ResultDataType _result = (ResultDataType)item.Tag;
731     Address = Convert.ToUInt32(item.SubItems[col_Found_Address].Text, 16);
732     switch (_result.ValueType)
733     {
734     case SearchDataTypes._8bits:
735     if (_result.IsUnsigned)
736     {
737     byte value = Convert.ToByte(item.SubItems[col_Found_Value].Text, 16);
738 william 243 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
739 william 230 timer_update_results.Enabled = false;
740     patcher.ShowDialog();
741     timer_update_results.Enabled = true;
742     PatchedValue_NeedsUpdate = true;
743     if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
744     ResultsUpdateWorkerThread.RunWorkerAsync();
745     }
746     else
747     {
748     sbyte value = Convert.ToSByte(item.SubItems[col_Found_Value].Text, 16);
749 william 243 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
750 william 230 timer_update_results.Enabled = false;
751     patcher.ShowDialog();
752     timer_update_results.Enabled = true;
753     PatchedValue_NeedsUpdate = true;
754     if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
755     ResultsUpdateWorkerThread.RunWorkerAsync();
756     }
757     break;
758     case SearchDataTypes._16bits:
759     if (_result.IsUnsigned)
760     {
761     ushort value = Convert.ToUInt16(item.SubItems[col_Found_Value].Text, 16);
762 william 243 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
763 william 230 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     else
771     {
772     short value = Convert.ToInt16(item.SubItems[col_Found_Value].Text, 16);
773 william 243 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
774 william 230 timer_update_results.Enabled = false;
775     patcher.ShowDialog();
776     timer_update_results.Enabled = true;
777     PatchedValue_NeedsUpdate = true;
778     if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
779     ResultsUpdateWorkerThread.RunWorkerAsync();
780     }
781     break;
782     case SearchDataTypes._32bits:
783     if (_result.IsUnsigned)
784     {
785     uint value = Convert.ToUInt32(item.SubItems[col_Found_Value].Text, 16);
786 william 243 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
787 william 230 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     else
795     {
796     int value = Convert.ToInt32(item.SubItems[col_Found_Value].Text, 16);
797 william 243 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
798 william 230 timer_update_results.Enabled = false;
799     patcher.ShowDialog();
800     timer_update_results.Enabled = true;
801     PatchedValue_NeedsUpdate = true;
802     if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
803     ResultsUpdateWorkerThread.RunWorkerAsync();
804     }
805     break;
806     case SearchDataTypes._64bits:
807     if (_result.IsUnsigned)
808     {
809     ulong value = Convert.ToUInt32(item.SubItems[col_Found_Value].Text, 16);
810 william 243 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
811 william 230 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     else
819     {
820     long value = Convert.ToInt32(item.SubItems[col_Found_Value].Text, 16);
821 william 243 patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value);
822 william 230 timer_update_results.Enabled = false;
823     patcher.ShowDialog();
824     timer_update_results.Enabled = true;
825     PatchedValue_NeedsUpdate = true;
826     if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy)
827     ResultsUpdateWorkerThread.RunWorkerAsync();
828     }
829     break;
830     }
831     }
832     else
833     {
834    
835     ListViewItem item = lstPatchList.Items[index];
836     ResultDataType _result = (ResultDataType)item.Tag;
837     patch_list.Add(_result);
838     }
839     }
840    
841     if (patch_list.Count > 0)
842     {
843 william 243 SearchRangePatcher rangePatcher = new SearchRangePatcher((IAcceptsProcessAndConfig)this, patch_list);
844 william 230 rangePatcher.ShowDialog();
845     }
846    
847     #endregion
848     }
849     private void mnuItemPatchSelectedEntry_Click(object sender, EventArgs e)
850     {
851     if (!(lstPatchList.SelectedItems.Count == 1)) return;
852     PatchRange(true);
853     }
854    
855     private void mnuItemPatchSelectedRange_Click(object sender, EventArgs e)
856     {
857     if (!(lstPatchList.SelectedItems.Count >= 1)) return;
858     PatchRange(false);
859     }
860    
861     private void mnuItemFreezeSelectedPatches_Click(object sender, EventArgs e)
862     {
863     if (!(lstPatchList.SelectedItems.Count > 0)) return;
864     //if (SearchArgs == null) return;
865     try
866     {
867     lstPatchList.ProcessID = this.AcceptedProcess.Id;
868     this.FreezeResultsUpdate();
869     for (int i = 0; i < lstPatchList.SelectedIndices.Count; i++)
870     {
871     lstPatchList.FreezeItem(lstPatchList.SelectedIndices[i]);
872     }
873     // force thaw and update
874     this.ThawResultsUpdate();
875     this.Update();
876     }
877     catch (Exception ex)
878     {
879     Debug.WriteLine(ex.ToString());
880     }
881     }
882    
883     private void mnuItemThawSelectedPatches_Click(object sender, EventArgs e)
884     {
885     if (!(lstPatchList.SelectedItems.Count > 0)) return;
886     //if (SearchArgs == null) return;
887     try
888     {
889     lstPatchList.ProcessID = this.AcceptedProcess.Id;
890     this.FreezeResultsUpdate();
891     for (int i = 0; i < lstPatchList.SelectedIndices.Count; i++)
892     {
893     lstPatchList.ThawItem(lstPatchList.SelectedIndices[i]);
894     }
895     // force thaw and update
896     this.ThawResultsUpdate();
897     this.Update();
898     }
899     catch (Exception ex)
900     {
901     Debug.WriteLine(ex.ToString());
902     }
903     }
904    
905     private void SearchWorkerThread_DoWork(object sender, DoWorkEventArgs e)
906     {
907    
908     }
909    
910     private void SearchWorkerThread_ProgressChanged(object sender, ProgressChangedEventArgs e)
911     {
912    
913     }
914    
915     private void SearchWorkerThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
916     {
917    
918     }
919    
920     private void search_progress_updater_Tick(object sender, EventArgs e)
921     {
922     if ((this.AcceptedProcess ==null) || Process.GetProcessById(this.AcceptedProcess.Id) == null)
923     {
924     SearchWorkerThread.CancelAsync();
925     //JokerSearchWorker.CancelAsync();
926     ResultsUpdateWorkerThread.CancelAsync();
927     }
928     }
929    
930     private void btnSearch_Click(object sender, EventArgs e)
931     {
932 william 271 this.SearchInProgess = true;
933     btnCancel.Enabled = true;
934     btnReset.Enabled = false;
935     btnSearch.Enabled = false;
936 william 230 this.FreezeResultsUpdate();
937     }
938 william 271
939     private void btnReset_Click(object sender, EventArgs e)
940     {
941     this.SearchInProgess = false;
942     btnSearch.Enabled = true;
943     btnCancel.Enabled = false;
944     }
945    
946     private void btnCancel_Click(object sender, EventArgs e)
947     {
948     this.SearchInProgess = false;
949     btnCancel.Enabled = false;
950     btnSearch.Enabled = true;
951     btnReset.Enabled = true;
952     }
953 william 275
954     private void mnuItemPatchListViewMemoryRegion_Click(object sender, EventArgs e)
955     {
956     List<ResultDataType> patch_list = new List<ResultDataType>();
957     List<int> SelectedIndexes = new List<int>();
958     foreach (int index in lstPatchList.SelectedIndices) { SelectedIndexes.Add(index); }
959     foreach (int index in SelectedIndexes)
960     {
961     ListViewItem item = lstPatchList.Items[index];
962     ResultDataType rdt = (ResultDataType)item.Tag;
963     ViewMemoryRegion(rdt);
964     break; // only get the fist item
965     }
966     }
967    
968     private void mnuItemResultsListViewMemoryRegion_Click(object sender, EventArgs e)
969     {
970     List<ResultDataType> patch_list = new List<ResultDataType>();
971     List<int> SelectedIndexes = new List<int>();
972     foreach (int index in lstResults.SelectedIndices) { SelectedIndexes.Add(index); }
973     foreach (int index in SelectedIndexes)
974     {
975     ListViewItem item = lstResults.Items[index];
976     ResultDataType rdt = (ResultDataType)item.Tag;
977     ViewMemoryRegion(rdt);
978     break; // only get the fist item
979     }
980     }
981     private void ViewMemoryRegion(ResultDataType rdt)
982     {
983     if (OnBrowseMemoryRegion != null)
984     {
985     OnBrowseMemoryRegion(new BrowseMemoryRegionEvent(rdt.Address));
986     }
987     }
988 william 227 }
989     }

  ViewVC Help
Powered by ViewVC 1.1.22