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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 242 - (hide annotations) (download)
Sun Jun 3 13:04:02 2012 UTC (8 years, 4 months ago) by william
File size: 40467 byte(s)
+ implement GenericMemoryProvider

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

  ViewVC Help
Powered by ViewVC 1.1.22