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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 277 - (show annotations) (download)
Sun Jun 3 22:30:23 2012 UTC (8 years, 6 months ago) by william
File size: 45245 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22