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 |
int Address = 0; |
327 |
ResultDataType _result = (ResultDataType)ResultItems[i].Tag; |
328 |
|
329 |
Address = Convert.ToInt32(ResultItems[i].SubItems[col_Found_Address].Text, 16); |
330 |
//r_ms.BaseStream.Seek(Address, SeekOrigin.Begin); |
331 |
GenericMemoryProvider provider = new GenericMemoryProvider((IAcceptsProcessAndConfig)this); |
332 |
provider.OpenProvider(); |
333 |
int bytesReadSize; |
334 |
byte[] data; |
335 |
uint bytesToRead = 0; |
336 |
switch (_result.ValueType) |
337 |
{ |
338 |
case SearchDataTypes._8bits: |
339 |
bytesToRead = 1; |
340 |
break; |
341 |
case SearchDataTypes._16bits: |
342 |
bytesToRead = 2; |
343 |
break; |
344 |
case SearchDataTypes._32bits: |
345 |
bytesToRead = 4; |
346 |
break; |
347 |
case SearchDataTypes._64bits: |
348 |
bytesToRead = 8; |
349 |
break; |
350 |
} |
351 |
provider.ReadProcessMemory(Address, bytesToRead, out bytesReadSize, out data); |
352 |
MemoryStream ms = new MemoryStream(data); |
353 |
BinaryReader r_ms = new BinaryReader(ms); |
354 |
switch (_result.ValueType) |
355 |
{ |
356 |
case SearchDataTypes._8bits: |
357 |
if (_result.IsUnsigned) { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x2}", r_ms.ReadByte()); } |
358 |
else { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x2}", r_ms.ReadSByte()); } |
359 |
break; |
360 |
case SearchDataTypes._16bits: |
361 |
if (_result.IsUnsigned) { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x4}", r_ms.ReadUInt16()); } |
362 |
else { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x4}", r_ms.ReadInt16()); } |
363 |
break; |
364 |
case SearchDataTypes._32bits: |
365 |
if (_result.IsUnsigned) { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x8}", r_ms.ReadUInt32()); } |
366 |
else { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x8}", r_ms.ReadInt32()); } |
367 |
break; |
368 |
case SearchDataTypes._64bits: |
369 |
if (_result.IsUnsigned) { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x16}", r_ms.ReadUInt64()); } |
370 |
else { ResultItems[i].SubItems[col_Found_Value].Text = string.Format("0x{0:x16}", r_ms.ReadInt64()); } |
371 |
break; |
372 |
} |
373 |
r_ms.Close(); |
374 |
provider.CloseProvider(); |
375 |
Application.DoEvents(); |
376 |
} |
377 |
#endregion |
378 |
|
379 |
#region Update Added Results List |
380 |
AddedItems = new List<ListViewItem>(); |
381 |
//r_ms.BaseStream.Seek(0, SeekOrigin.Begin); |
382 |
for (int i = 0; i < lstPatchList.Items.Count; i++) |
383 |
{ |
384 |
if (this.lstResults.InvokeRequired) |
385 |
{ |
386 |
ThreadSafe_GetResultItem _get_item = new ThreadSafe_GetResultItem(GetResultItem); |
387 |
object item = this.lstResults.Invoke(_get_item, new object[] { i, (int)eListViewResults.PATCH_RESULTS_LIST }); |
388 |
if (item != null) |
389 |
AddedItems.Add((ListViewItem)item); |
390 |
} |
391 |
else |
392 |
{ |
393 |
AddedItems.Add(lstPatchList.Items[i]); |
394 |
} |
395 |
|
396 |
} |
397 |
for (int i = 0; i < AddedItems.Count; i++) |
398 |
{ |
399 |
if (ResultsUpdateWorkerThread.CancellationPending == true) |
400 |
{ |
401 |
e.Cancel = true; |
402 |
return; |
403 |
} |
404 |
int Address = 0; |
405 |
ResultDataType _result = (ResultDataType)AddedItems[i].Tag; |
406 |
Address = Convert.ToInt32(AddedItems[i].SubItems[col_Added_Address].Text, 16); |
407 |
GenericMemoryProvider provider = new GenericMemoryProvider((IAcceptsProcessAndConfig)this); |
408 |
provider.OpenProvider(); |
409 |
int bytesReadSize; |
410 |
byte[] data; |
411 |
uint bytesToRead = 0; |
412 |
switch (_result.ValueType) |
413 |
{ |
414 |
case SearchDataTypes._8bits: |
415 |
bytesToRead = 1; |
416 |
break; |
417 |
case SearchDataTypes._16bits: |
418 |
bytesToRead = 2; |
419 |
break; |
420 |
case SearchDataTypes._32bits: |
421 |
bytesToRead = 4; |
422 |
break; |
423 |
case SearchDataTypes._64bits: |
424 |
bytesToRead = 8; |
425 |
break; |
426 |
} |
427 |
provider.ReadProcessMemory(Address, bytesToRead, out bytesReadSize, out data); |
428 |
MemoryStream ms = new MemoryStream(data); |
429 |
BinaryReader r_ms = new BinaryReader(ms); |
430 |
switch (_result.ValueType) |
431 |
{ |
432 |
case SearchDataTypes._8bits: |
433 |
if (_result.IsUnsigned) { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x2}", r_ms.ReadByte()); } |
434 |
else { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x2}", r_ms.ReadSByte()); } |
435 |
break; |
436 |
case SearchDataTypes._16bits: |
437 |
if (_result.IsUnsigned) { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x4}", r_ms.ReadUInt16()); } |
438 |
else { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x4}", r_ms.ReadInt16()); } |
439 |
break; |
440 |
case SearchDataTypes._32bits: |
441 |
if (_result.IsUnsigned) { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x8}", r_ms.ReadUInt32()); } |
442 |
else { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x8}", r_ms.ReadInt32()); } |
443 |
break; |
444 |
case SearchDataTypes._64bits: |
445 |
if (_result.IsUnsigned) { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x16}", r_ms.ReadUInt64()); } |
446 |
else { AddedItems[i].SubItems[col_Added_Value].Text = string.Format("0x{0:x16}", r_ms.ReadInt64()); } |
447 |
break; |
448 |
} |
449 |
r_ms.Close(); |
450 |
provider.CloseProvider(); |
451 |
Application.DoEvents(); |
452 |
} |
453 |
#endregion |
454 |
|
455 |
|
456 |
} |
457 |
|
458 |
private void ResultsUpdateWorkerThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) |
459 |
{ |
460 |
try |
461 |
{ |
462 |
//if ((lstResults.SelectedItems.Count > 0) && !PatchedValue_NeedsUpdate ) return; |
463 |
//if ((lstPatchList.SelectedItems.Count > 0) && !PatchedValue_NeedsUpdate) return; |
464 |
if (!this.ShouldUpdateResults()) return; |
465 |
if (ResultItems.Count > 0) |
466 |
{ |
467 |
//lstResults.Items.Clear(); |
468 |
//lstResults.Items.AddRange(ResultItems.ToArray()); |
469 |
|
470 |
for (int i = 0; i < ResultItems.Count; i++) |
471 |
{ |
472 |
lstResults.Items[i].SubItems[new AVPColumnText(AVPColumnType.VALUE).ColumnIndex].Text = |
473 |
ResultItems[i].SubItems[new AVPColumnText(AVPColumnType.VALUE).ColumnIndex].Text; |
474 |
} |
475 |
|
476 |
} |
477 |
if (AddedItems.Count > 0) |
478 |
{ |
479 |
//lstPatchList.Items.Clear(); |
480 |
//lstPatchList.Items.AddRange(AddedItems.ToArray()); |
481 |
|
482 |
for (int i = 0; i < AddedItems.Count; i++) |
483 |
{ |
484 |
lstPatchList.Items[i].SubItems[new AVPColumnText(AVPColumnType.VALUE).ColumnIndex].Text = |
485 |
AddedItems[i].SubItems[new AVPColumnText(AVPColumnType.VALUE).ColumnIndex].Text; |
486 |
} |
487 |
|
488 |
} |
489 |
PatchedValue_NeedsUpdate = false; |
490 |
} |
491 |
catch { } |
492 |
} |
493 |
|
494 |
private void btnImportFile_Click(object sender, EventArgs e) |
495 |
{ |
496 |
this.FreezeResultsUpdate(); |
497 |
if (!lstPatchList.ImportFromFile()) |
498 |
{ |
499 |
MessageBox.Show("Failed to Import Patch List from File.", "Import Failure", MessageBoxButtons.OK, MessageBoxIcon.Error); |
500 |
this.ThawResultsUpdate(); |
501 |
return; |
502 |
} |
503 |
else |
504 |
{ |
505 |
MessageBox.Show("Succesfully Imported Patch List from File.", "Import Success", MessageBoxButtons.OK, MessageBoxIcon.Information); |
506 |
this.ThawResultsUpdate(); |
507 |
return; |
508 |
} |
509 |
} |
510 |
bool g_isFrozen = false; |
511 |
private bool IsResultsUpdateFrozen |
512 |
{ |
513 |
get { return g_isFrozen; } |
514 |
set { g_isFrozen = value; } |
515 |
} |
516 |
private void ThawResultsUpdate() |
517 |
{ |
518 |
this.IsResultsUpdateFrozen = false; |
519 |
if (this.AcceptedProcess != null) |
520 |
{ |
521 |
#if !DO_NOT_SUSPEND_RESUME_THREAD_ON_FREEZE |
522 |
ThreadControl.ResumeProcess(this.AcceptedProcess.Id); |
523 |
#endif |
524 |
} |
525 |
} |
526 |
|
527 |
private void FreezeResultsUpdate() |
528 |
{ |
529 |
this.IsResultsUpdateFrozen = true; |
530 |
this.IsResultsUpdateFrozen = false; |
531 |
if (this.AcceptedProcess != null) |
532 |
{ |
533 |
#if !DO_NOT_SUSPEND_RESUME_THREAD_ON_FREEZE |
534 |
ThreadControl.SuspendProcess(this.AcceptedProcess.Id); |
535 |
#endif |
536 |
} |
537 |
} |
538 |
|
539 |
private void btnExportFile_Click(object sender, EventArgs e) |
540 |
{ |
541 |
this.FreezeResultsUpdate(); |
542 |
if (!lstPatchList.ExportToFile()) |
543 |
{ |
544 |
MessageBox.Show("Failed to Export Patch List to File.", "Export Failure", MessageBoxButtons.OK, MessageBoxIcon.Error); |
545 |
this.ThawResultsUpdate(); |
546 |
return; |
547 |
} |
548 |
else |
549 |
{ |
550 |
MessageBox.Show("Succesfully Exported Patch List to File.", "Export Success", MessageBoxButtons.OK, MessageBoxIcon.Information); |
551 |
this.ThawResultsUpdate(); |
552 |
return; |
553 |
} |
554 |
} |
555 |
|
556 |
private void btnImportClipboard_Click(object sender, EventArgs e) |
557 |
{ |
558 |
this.FreezeResultsUpdate(); |
559 |
if (!lstPatchList.ImportFromClipboard()) |
560 |
{ |
561 |
MessageBox.Show("Failed to Import Patch List from Clipboard.", "Import Failure", MessageBoxButtons.OK, MessageBoxIcon.Error); |
562 |
this.ThawResultsUpdate(); |
563 |
return; |
564 |
} |
565 |
else |
566 |
{ |
567 |
MessageBox.Show("Succesfully Import Patch List from Clipboard.", "Import Success", MessageBoxButtons.OK, MessageBoxIcon.Information); |
568 |
this.ThawResultsUpdate(); |
569 |
} |
570 |
} |
571 |
|
572 |
private void btnExportClipboard_Click(object sender, EventArgs e) |
573 |
{ |
574 |
this.FreezeResultsUpdate(); |
575 |
if (!lstPatchList.ExportToClipboard()) |
576 |
{ |
577 |
MessageBox.Show("Failed to Export Patch List to Clipboard.", "Export Failure", MessageBoxButtons.OK, MessageBoxIcon.Error); |
578 |
this.ThawResultsUpdate(); |
579 |
return; |
580 |
} |
581 |
else |
582 |
{ |
583 |
MessageBox.Show("Succesfully Exported Patch List to Clipboard.", "Export Success", MessageBoxButtons.OK, MessageBoxIcon.Information); |
584 |
this.ThawResultsUpdate(); |
585 |
return; |
586 |
} |
587 |
} |
588 |
|
589 |
private void btnAddPatchAddress_Click(object sender, EventArgs e) |
590 |
{ |
591 |
PatchAdder adder = new PatchAdder((IAcceptsProcessAndConfig)this); |
592 |
adder.ShowDialog(); |
593 |
if (adder.WasAPatchAdded) AddToPatchList(adder.AddedPatchValue); |
594 |
} |
595 |
|
596 |
private void btnAddAddressRange_Click(object sender, EventArgs e) |
597 |
{ |
598 |
PatchRangeAdder adder = new PatchRangeAdder((IAcceptsProcessAndConfig)this); |
599 |
adder.ShowDialog(); |
600 |
if (adder.WasAPatchAdded) AddToPatchList(adder.AddedPatchValue); |
601 |
} |
602 |
private void AddToPatchList(List<ResultDataType> item) { foreach (ResultDataType data in item) { AddToPatchList(data); } } |
603 |
private void AddToPatchList(ResultDataType item) |
604 |
{ |
605 |
ResultItem item2 = null; |
606 |
switch (item.ValueType) |
607 |
{ |
608 |
case SearchDataTypes._8bits: |
609 |
if (item.IsUnsigned) { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToByte(item.Value)); } |
610 |
else { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToSByte(item.Value)); } |
611 |
break; |
612 |
case SearchDataTypes._16bits: |
613 |
if (item.IsUnsigned) { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToUInt16(item.Value)); } |
614 |
else { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToInt16(item.Value)); } |
615 |
break; |
616 |
case SearchDataTypes._32bits: |
617 |
if (item.IsUnsigned) { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToUInt32(item.Value)); } |
618 |
else { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToInt32(item.Value)); } |
619 |
break; |
620 |
case SearchDataTypes._64bits: |
621 |
if (item.IsUnsigned) { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToUInt64(item.Value)); } |
622 |
else { item2 = new ResultItem(item.Address, item.IsFrozen, Convert.ToInt64(item.Value)); } |
623 |
break; |
624 |
} |
625 |
this.AddToPatchList(item2); |
626 |
} |
627 |
private void AddToPatchList(ListViewItem item) |
628 |
{ |
629 |
try |
630 |
{ |
631 |
ResultDataType _result = (ResultDataType)item.Tag; |
632 |
this.AddToPatchList(_result); |
633 |
} |
634 |
catch (InvalidCastException ex) |
635 |
{ |
636 |
// unable to cast |
637 |
MessageBox.Show(ex.Message, "Invalid Cast Exception", MessageBoxButtons.OK, MessageBoxIcon.Error); |
638 |
} |
639 |
catch (Exception ex) |
640 |
{ |
641 |
// other exception |
642 |
MessageBox.Show(ex.Message, "Unhandled Exception", MessageBoxButtons.OK, MessageBoxIcon.Error); |
643 |
} |
644 |
} |
645 |
private void AddToPatchList(ResultItem item) |
646 |
{ |
647 |
if (!lstPatchList.Items.Contains(item)) lstPatchList.Items.Add(item); |
648 |
} |
649 |
private void AddToPatchList(string address, SearchDataTypes bitsize, bool IsUnsigned) |
650 |
{ |
651 |
ResultItemState state = new ResultItemState(address, bitsize, IsUnsigned, (IAcceptsProcessAndConfig)this); |
652 |
ResultItem item = new ResultItem(state.Address, state.Value, state.Frozen, state.ValueType, state.IsUnsigned); |
653 |
this.AddToPatchList(item); |
654 |
} |
655 |
|
656 |
private void mnuItemAddToPatchList_Click(object sender, EventArgs e) |
657 |
{ |
658 |
if (!(lstResults.SelectedItems.Count > 0)) return; |
659 |
//if (SearchArgs == null) return; |
660 |
|
661 |
try |
662 |
{ |
663 |
for (int i = 0; i < lstResults.SelectedIndices.Count; i++) |
664 |
{ |
665 |
//ResultDataType result = (ResultDataType)lstResults.Items[selected_index].Tag; |
666 |
ListViewItem item = lstResults.Items[lstResults.SelectedIndices[i]]; |
667 |
this.AddToPatchList(item); |
668 |
} |
669 |
} |
670 |
catch (Exception ex) |
671 |
{ |
672 |
logger.Error.WriteLine(ex.ToString()); |
673 |
} |
674 |
} |
675 |
|
676 |
private void mnuItemRemoveResult_Click(object sender, EventArgs e) |
677 |
{ |
678 |
if (!(lstPatchList.SelectedItems.Count > 0)) return; |
679 |
//if (SearchArgs == null) return; |
680 |
try |
681 |
{ |
682 |
this.FreezeResultsUpdate(); |
683 |
for (int i = 0; i < lstPatchList.SelectedIndices.Count; i++) |
684 |
{ |
685 |
//lstPatchList.ThawItem(lstPatchList.SelectedIndices[i]); |
686 |
lstPatchList.Items[lstPatchList.SelectedIndices[i]].Remove(); |
687 |
} |
688 |
this.ThawResultsUpdate(); |
689 |
} |
690 |
catch (Exception ex) |
691 |
{ |
692 |
Debug.WriteLine(ex.ToString()); |
693 |
} |
694 |
} |
695 |
private void PatchRange(bool SingleEntry) |
696 |
{ |
697 |
//if (SearchArgs == null) return; |
698 |
#region Patch Selected Address |
699 |
// stop ResultsUpdate Thread |
700 |
ResultsUpdateWorkerThread.CancelAsync(); |
701 |
|
702 |
List<ResultDataType> patch_list = new List<ResultDataType>(); |
703 |
List<int> SelectedIndexes = new List<int>(); |
704 |
|
705 |
if (SingleEntry) |
706 |
{ |
707 |
SelectedIndexes.Add(lstPatchList.SelectedIndices[0]); |
708 |
} |
709 |
else |
710 |
{ |
711 |
foreach (int index in lstPatchList.SelectedIndices) |
712 |
{ |
713 |
SelectedIndexes.Add(index); |
714 |
} |
715 |
} |
716 |
//PCSX2MemoryProvider provider = new PCSX2MemoryProvider(this.SearchPCSX2ProcessPID, resultslog); |
717 |
foreach (int index in SelectedIndexes) |
718 |
{ |
719 |
if (SingleEntry) |
720 |
{ |
721 |
SearchPatcher patcher = null; |
722 |
uint Address = 0; |
723 |
ListViewItem item = lstPatchList.Items[index]; |
724 |
ResultDataType _result = (ResultDataType)item.Tag; |
725 |
Address = Convert.ToUInt32(item.SubItems[col_Found_Address].Text, 16); |
726 |
switch (_result.ValueType) |
727 |
{ |
728 |
case SearchDataTypes._8bits: |
729 |
if (_result.IsUnsigned) |
730 |
{ |
731 |
byte value = Convert.ToByte(item.SubItems[col_Found_Value].Text, 16); |
732 |
patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value); |
733 |
timer_update_results.Enabled = false; |
734 |
patcher.ShowDialog(); |
735 |
timer_update_results.Enabled = true; |
736 |
PatchedValue_NeedsUpdate = true; |
737 |
if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy) |
738 |
ResultsUpdateWorkerThread.RunWorkerAsync(); |
739 |
} |
740 |
else |
741 |
{ |
742 |
sbyte value = Convert.ToSByte(item.SubItems[col_Found_Value].Text, 16); |
743 |
patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value); |
744 |
timer_update_results.Enabled = false; |
745 |
patcher.ShowDialog(); |
746 |
timer_update_results.Enabled = true; |
747 |
PatchedValue_NeedsUpdate = true; |
748 |
if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy) |
749 |
ResultsUpdateWorkerThread.RunWorkerAsync(); |
750 |
} |
751 |
break; |
752 |
case SearchDataTypes._16bits: |
753 |
if (_result.IsUnsigned) |
754 |
{ |
755 |
ushort value = Convert.ToUInt16(item.SubItems[col_Found_Value].Text, 16); |
756 |
patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value); |
757 |
timer_update_results.Enabled = false; |
758 |
patcher.ShowDialog(); |
759 |
timer_update_results.Enabled = true; |
760 |
PatchedValue_NeedsUpdate = true; |
761 |
if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy) |
762 |
ResultsUpdateWorkerThread.RunWorkerAsync(); |
763 |
} |
764 |
else |
765 |
{ |
766 |
short value = Convert.ToInt16(item.SubItems[col_Found_Value].Text, 16); |
767 |
patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value); |
768 |
timer_update_results.Enabled = false; |
769 |
patcher.ShowDialog(); |
770 |
timer_update_results.Enabled = true; |
771 |
PatchedValue_NeedsUpdate = true; |
772 |
if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy) |
773 |
ResultsUpdateWorkerThread.RunWorkerAsync(); |
774 |
} |
775 |
break; |
776 |
case SearchDataTypes._32bits: |
777 |
if (_result.IsUnsigned) |
778 |
{ |
779 |
uint value = Convert.ToUInt32(item.SubItems[col_Found_Value].Text, 16); |
780 |
patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value); |
781 |
timer_update_results.Enabled = false; |
782 |
patcher.ShowDialog(); |
783 |
timer_update_results.Enabled = true; |
784 |
PatchedValue_NeedsUpdate = true; |
785 |
if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy) |
786 |
ResultsUpdateWorkerThread.RunWorkerAsync(); |
787 |
} |
788 |
else |
789 |
{ |
790 |
int value = Convert.ToInt32(item.SubItems[col_Found_Value].Text, 16); |
791 |
patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value); |
792 |
timer_update_results.Enabled = false; |
793 |
patcher.ShowDialog(); |
794 |
timer_update_results.Enabled = true; |
795 |
PatchedValue_NeedsUpdate = true; |
796 |
if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy) |
797 |
ResultsUpdateWorkerThread.RunWorkerAsync(); |
798 |
} |
799 |
break; |
800 |
case SearchDataTypes._64bits: |
801 |
if (_result.IsUnsigned) |
802 |
{ |
803 |
ulong value = Convert.ToUInt32(item.SubItems[col_Found_Value].Text, 16); |
804 |
patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value); |
805 |
timer_update_results.Enabled = false; |
806 |
patcher.ShowDialog(); |
807 |
timer_update_results.Enabled = true; |
808 |
PatchedValue_NeedsUpdate = true; |
809 |
if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy) |
810 |
ResultsUpdateWorkerThread.RunWorkerAsync(); |
811 |
} |
812 |
else |
813 |
{ |
814 |
long value = Convert.ToInt32(item.SubItems[col_Found_Value].Text, 16); |
815 |
patcher = new SearchPatcher((IAcceptsProcessAndConfig)this, Address, value); |
816 |
timer_update_results.Enabled = false; |
817 |
patcher.ShowDialog(); |
818 |
timer_update_results.Enabled = true; |
819 |
PatchedValue_NeedsUpdate = true; |
820 |
if (!chkRefreshResults.Checked && !ResultsUpdateWorkerThread.IsBusy) |
821 |
ResultsUpdateWorkerThread.RunWorkerAsync(); |
822 |
} |
823 |
break; |
824 |
} |
825 |
} |
826 |
else |
827 |
{ |
828 |
|
829 |
ListViewItem item = lstPatchList.Items[index]; |
830 |
ResultDataType _result = (ResultDataType)item.Tag; |
831 |
patch_list.Add(_result); |
832 |
} |
833 |
} |
834 |
|
835 |
if (patch_list.Count > 0) |
836 |
{ |
837 |
SearchRangePatcher rangePatcher = new SearchRangePatcher((IAcceptsProcessAndConfig)this, patch_list); |
838 |
rangePatcher.ShowDialog(); |
839 |
} |
840 |
|
841 |
#endregion |
842 |
} |
843 |
private void mnuItemPatchSelectedEntry_Click(object sender, EventArgs e) |
844 |
{ |
845 |
if (!(lstPatchList.SelectedItems.Count == 1)) return; |
846 |
PatchRange(true); |
847 |
} |
848 |
|
849 |
private void mnuItemPatchSelectedRange_Click(object sender, EventArgs e) |
850 |
{ |
851 |
if (!(lstPatchList.SelectedItems.Count >= 1)) return; |
852 |
PatchRange(false); |
853 |
} |
854 |
|
855 |
private void mnuItemFreezeSelectedPatches_Click(object sender, EventArgs e) |
856 |
{ |
857 |
if (!(lstPatchList.SelectedItems.Count > 0)) return; |
858 |
//if (SearchArgs == null) return; |
859 |
try |
860 |
{ |
861 |
lstPatchList.ProcessID = this.AcceptedProcess.Id; |
862 |
this.FreezeResultsUpdate(); |
863 |
for (int i = 0; i < lstPatchList.SelectedIndices.Count; i++) |
864 |
{ |
865 |
lstPatchList.FreezeItem(lstPatchList.SelectedIndices[i]); |
866 |
} |
867 |
// force thaw and update |
868 |
this.ThawResultsUpdate(); |
869 |
this.Update(); |
870 |
} |
871 |
catch (Exception ex) |
872 |
{ |
873 |
Debug.WriteLine(ex.ToString()); |
874 |
} |
875 |
} |
876 |
|
877 |
private void mnuItemThawSelectedPatches_Click(object sender, EventArgs e) |
878 |
{ |
879 |
if (!(lstPatchList.SelectedItems.Count > 0)) return; |
880 |
//if (SearchArgs == null) return; |
881 |
try |
882 |
{ |
883 |
lstPatchList.ProcessID = this.AcceptedProcess.Id; |
884 |
this.FreezeResultsUpdate(); |
885 |
for (int i = 0; i < lstPatchList.SelectedIndices.Count; i++) |
886 |
{ |
887 |
lstPatchList.ThawItem(lstPatchList.SelectedIndices[i]); |
888 |
} |
889 |
// force thaw and update |
890 |
this.ThawResultsUpdate(); |
891 |
this.Update(); |
892 |
} |
893 |
catch (Exception ex) |
894 |
{ |
895 |
Debug.WriteLine(ex.ToString()); |
896 |
} |
897 |
} |
898 |
|
899 |
private void SearchWorkerThread_DoWork(object sender, DoWorkEventArgs e) |
900 |
{ |
901 |
|
902 |
} |
903 |
|
904 |
private void SearchWorkerThread_ProgressChanged(object sender, ProgressChangedEventArgs e) |
905 |
{ |
906 |
|
907 |
} |
908 |
|
909 |
private void SearchWorkerThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) |
910 |
{ |
911 |
|
912 |
} |
913 |
|
914 |
private void search_progress_updater_Tick(object sender, EventArgs e) |
915 |
{ |
916 |
if ((this.AcceptedProcess ==null) || Process.GetProcessById(this.AcceptedProcess.Id) == null) |
917 |
{ |
918 |
SearchWorkerThread.CancelAsync(); |
919 |
//JokerSearchWorker.CancelAsync(); |
920 |
ResultsUpdateWorkerThread.CancelAsync(); |
921 |
} |
922 |
} |
923 |
|
924 |
private void btnSearch_Click(object sender, EventArgs e) |
925 |
{ |
926 |
this.SearchInProgess = true; |
927 |
btnCancel.Enabled = true; |
928 |
btnReset.Enabled = false; |
929 |
btnSearch.Enabled = false; |
930 |
this.FreezeResultsUpdate(); |
931 |
} |
932 |
|
933 |
private void btnReset_Click(object sender, EventArgs e) |
934 |
{ |
935 |
this.SearchInProgess = false; |
936 |
btnSearch.Enabled = true; |
937 |
btnCancel.Enabled = false; |
938 |
} |
939 |
|
940 |
private void btnCancel_Click(object sender, EventArgs e) |
941 |
{ |
942 |
this.SearchInProgess = false; |
943 |
btnCancel.Enabled = false; |
944 |
btnSearch.Enabled = true; |
945 |
btnReset.Enabled = true; |
946 |
} |
947 |
} |
948 |
} |