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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22