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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 237 - (hide annotations) (download)
Sun Jun 3 12:48:09 2012 UTC (8 years, 4 months ago) by william
File size: 41036 byte(s)
+ change inteface implementation

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

  ViewVC Help
Powered by ViewVC 1.1.22