1 |
//#region Logging Defines |
2 |
//// include this any class or method that required logging, and comment-out what is not needed |
3 |
|
4 |
//#region Enabled logging levels |
5 |
//#define LOGGING_ENABLE_INFO |
6 |
//#define LOGGING_ENABLE_WARN |
7 |
//#define LOGGING_ENABLE_DEBUG |
8 |
//#define LOGGING_ENABLE_VERBOSEDEBUG |
9 |
//#define LOGGING_ENABLE_ERROR |
10 |
//#define LOGGING_ENABLE_VERBOSEERROR |
11 |
//#define LOGGING_ENABLE_PROFILER |
12 |
//#endregion |
13 |
//#endregion |
14 |
using System; |
15 |
using System.Collections.Generic; |
16 |
using System.ComponentModel; |
17 |
using System.Data; |
18 |
using System.Drawing; |
19 |
using System.Linq; |
20 |
using System.Text; |
21 |
using System.Windows.Forms; |
22 |
using WeifenLuo.WinFormsUI.Docking; |
23 |
using RomCheater.Logging; |
24 |
using System.IO; |
25 |
using System.Runtime.Serialization.Formatters.Binary; |
26 |
using RomCheater.PluginFramework.Core; |
27 |
using RomCheater.Core; |
28 |
using Enterprise.Logging; |
29 |
|
30 |
namespace RomCheater.RVACalculator |
31 |
{ |
32 |
public partial class RVACalculatorDockControl : DockContent |
33 |
{ |
34 |
private UserControlPlugin plugin; |
35 |
public RVACalculatorDockControl(UserControlPlugin plugin) |
36 |
{ |
37 |
this.plugin = plugin; |
38 |
InitPluginFramework(); |
39 |
InitializeComponent(); |
40 |
} |
41 |
private void InitPluginFramework() |
42 |
{ |
43 |
if (this.plugin == null) { return; } |
44 |
this.plugin.OnSelectedProcessChanged += new BaseEventHandler<ProcessChangedEventArgs>(plugin_OnSelectedProcessChanged); |
45 |
this.plugin.OnSelectedConfigChanged += new BaseEventHandler<ConfigChangedEventArgs>(plugin_OnSelectedConfigChanged); |
46 |
this.plugin.OnPEDataUpdated += new BaseEventHandler<PEViewerDataUpdatedEventArgs>(plugin_OnPEDataUpdated); |
47 |
RaisePluginFrameworkEvents(); |
48 |
} |
49 |
|
50 |
bool EventsRaised = false; |
51 |
private void RaisePluginFrameworkEvents() |
52 |
{ |
53 |
|
54 |
if (this.plugin == null) { EventsRaised = true; return; } |
55 |
if (!EventsRaised) |
56 |
{ |
57 |
this.plugin.RaisePluginFrameworkEvents(); |
58 |
EventsRaised = true; |
59 |
} |
60 |
} |
61 |
void plugin_OnPEDataUpdated(PEViewerDataUpdatedEventArgs e) |
62 |
{ |
63 |
//logger.Warn.WriteLine("plugin_OnPEDataUpdated::has not been implemented!"); |
64 |
} |
65 |
void plugin_OnSelectedConfigChanged(ConfigChangedEventArgs e) |
66 |
{ |
67 |
//logger.Warn.WriteLine("plugin_OnSelectedConfigChanged::has not been implemented!"); |
68 |
} |
69 |
void plugin_OnSelectedProcessChanged(ProcessChangedEventArgs e) |
70 |
{ |
71 |
//logger.Warn.WriteLine("plugin_OnSelectedProcessChanged::has not been implemented!"); |
72 |
} |
73 |
|
74 |
|
75 |
private void ResizeColumns() |
76 |
{ |
77 |
foreach (var col in Enumerable.Range(0, lstCheats.Columns.Count)) |
78 |
{ |
79 |
lstCheats.AutoResizeColumn(col, ColumnHeaderAutoResizeStyle.ColumnContent); |
80 |
lstCheats.AutoResizeColumn(col, ColumnHeaderAutoResizeStyle.HeaderSize); |
81 |
} |
82 |
} |
83 |
|
84 |
private void btnAdd_Click(object sender, EventArgs e) |
85 |
{ |
86 |
CheatInputDialog dlg = new CheatInputDialog(); |
87 |
DialogResult result = dlg.ShowDialog(); |
88 |
if (result == DialogResult.Cancel) { return; } |
89 |
uint physical = dlg.CheatAddress + txtRVA.ToUInt32(); |
90 |
ListViewItem li = new ListViewItem(dlg.CheatName); |
91 |
li.SubItems.Add(new ListViewItem.ListViewSubItem(li, string.Format("0x{0:x8}", dlg.CheatAddress))); |
92 |
li.SubItems.Add(new ListViewItem.ListViewSubItem(li, string.Format("0x{0:x8}", physical))); |
93 |
lstCheats.Items.Add(li); |
94 |
ResizeColumns(); |
95 |
} |
96 |
|
97 |
private void btnRemove_Click(object sender, EventArgs e) |
98 |
{ |
99 |
if (lstCheats.SelectedItems.Count == 0 || lstCheats.SelectedItems.Count > 1) { return; } |
100 |
int index = lstCheats.SelectedIndices[0]; |
101 |
lstCheats.Items.RemoveAt(index); |
102 |
ResizeColumns(); |
103 |
} |
104 |
|
105 |
private void btnUpdate_Click(object sender, EventArgs e) |
106 |
{ |
107 |
if (lstCheats.SelectedItems.Count == 0 || lstCheats.SelectedItems.Count > 1) { return; } |
108 |
int index = lstCheats.SelectedIndices[0]; |
109 |
var li = lstCheats.SelectedItems[0]; |
110 |
string name = li.Text; |
111 |
string address = li.SubItems[1].Text; |
112 |
|
113 |
CheatInputDialog dlg = new CheatInputDialog(name, Convert.ToUInt32(address, 16)); |
114 |
DialogResult result = dlg.ShowDialog(); |
115 |
if (result == DialogResult.Cancel) { return; } |
116 |
uint physical = dlg.CheatAddress + txtRVA.ToUInt32(); |
117 |
li.Text = dlg.CheatName; |
118 |
li.SubItems[1].Text = string.Format("0x{0:x8}", dlg.CheatAddress); |
119 |
li.SubItems[2].Text = string.Format("0x{0:x8}", physical); |
120 |
lstCheats.Items[index] = li; |
121 |
ResizeColumns(); |
122 |
} |
123 |
|
124 |
private void btnCopy_Click(object sender, EventArgs e) |
125 |
{ |
126 |
if (lstCheats.SelectedItems.Count == 0 || lstCheats.SelectedItems.Count > 1) { return; } |
127 |
var li = lstCheats.SelectedItems[0]; |
128 |
string physical = li.SubItems[2].Text; |
129 |
Clipboard.SetText(physical); |
130 |
} |
131 |
|
132 |
private void btnSave_Click(object sender, EventArgs e) |
133 |
{ |
134 |
DialogResult result = CheatSaver.ShowDialog(); |
135 |
if (result != DialogResult.OK) { return; } |
136 |
|
137 |
|
138 |
ICheatList2 list = new ICheatList2(); |
139 |
list.RVA = txtRVA.ToUInt32(); |
140 |
List<ICheatEntry2> cheats = new List<ICheatEntry2>(); |
141 |
foreach (ListViewItem li in lstCheats.Items) |
142 |
{ |
143 |
cheats.Add(new ICheatEntry2(li.Text, Convert.ToUInt32(li.SubItems[1].Text, 16), Convert.ToUInt32(li.SubItems[2].Text, 16))); |
144 |
} |
145 |
|
146 |
list.Cheats = cheats; |
147 |
|
148 |
try |
149 |
{ |
150 |
using (FileStream fs = new FileStream(CheatSaver.FileName, FileMode.Create, FileAccess.ReadWrite, FileShare.Read)) |
151 |
{ |
152 |
try |
153 |
{ |
154 |
fs.Seek(0, SeekOrigin.Begin); |
155 |
BinaryFormatter bin = new BinaryFormatter(); |
156 |
bin.Serialize(fs, list); |
157 |
} |
158 |
catch (Exception ex) |
159 |
{ |
160 |
gLog.Error.WriteLine("Failed to save file: {0}", CheatSaver.FileName); |
161 |
gLog.Verbose.Error.WriteLine(ex.ToString()); |
162 |
MessageBox.Show(string.Format("Failed to save: '{0}'", new FileInfo(CheatSaver.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error); |
163 |
return; |
164 |
} |
165 |
} |
166 |
MessageBox.Show(string.Format("Successfully saved file: '{0}'", new FileInfo(CheatSaver.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Information); |
167 |
} |
168 |
catch (Exception ex) |
169 |
{ |
170 |
gLog.Error.WriteLine("Failed to save file: {0}", CheatSaver.FileName); |
171 |
gLog.Verbose.Error.WriteLine(ex.ToString()); |
172 |
MessageBox.Show(string.Format("Failed to save: '{0}'", new FileInfo(CheatSaver.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error); |
173 |
} |
174 |
} |
175 |
|
176 |
private void btnLoad_Click(object sender, EventArgs e) |
177 |
{ |
178 |
DialogResult result = CheatLoader.ShowDialog(); |
179 |
if (result != DialogResult.OK) { return; } |
180 |
try |
181 |
{ |
182 |
using (FileStream fs = new FileStream(CheatLoader.FileName, FileMode.Open, FileAccess.Read, FileShare.Read)) |
183 |
{ |
184 |
try |
185 |
{ |
186 |
ICheatList2 list = new ICheatList2(); |
187 |
BinaryFormatter bin = new BinaryFormatter(); |
188 |
try |
189 |
{ |
190 |
fs.Seek(0, SeekOrigin.Begin); |
191 |
var t_list = (ICheatList2)bin.Deserialize(fs); |
192 |
list = t_list; |
193 |
} |
194 |
catch (Exception) |
195 |
{ |
196 |
fs.Seek(0, SeekOrigin.Begin); |
197 |
var t_list = (ICheatList)bin.Deserialize(fs); |
198 |
list = new ICheatList2(t_list); |
199 |
} |
200 |
|
201 |
txtRVA.Value = list.RVA; |
202 |
if (lstCheats.Items.Count > 0) |
203 |
{ |
204 |
result = MessageBox.Show("Clear existing Cheats?", "", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3); |
205 |
if (result == System.Windows.Forms.DialogResult.Cancel) |
206 |
{ |
207 |
// assume abort of load |
208 |
gLog.Warn.WriteLine("Abored processing of file (by user request): {0}", CheatLoader.FileName); |
209 |
fs.Close(); |
210 |
return; |
211 |
} |
212 |
if (result == DialogResult.Yes) |
213 |
{ |
214 |
lstCheats.Items.Clear(); |
215 |
} |
216 |
} |
217 |
foreach (var cheat in list.Cheats) |
218 |
{ |
219 |
ListViewItem li = new ListViewItem(cheat.CheatName); |
220 |
li.SubItems.Add(new ListViewItem.ListViewSubItem(li, string.Format("0x{0:x8}", cheat.CheatAddress))); |
221 |
uint physical = cheat.CheatAddress + list.RVA; |
222 |
li.SubItems.Add(new ListViewItem.ListViewSubItem(li, string.Format("0x{0:x8}", physical))); |
223 |
lstCheats.Items.Add(li); |
224 |
} |
225 |
btnRefresh.PerformClick(); // do any needed refreshing |
226 |
} |
227 |
catch (Exception ex) |
228 |
{ |
229 |
gLog.Error.WriteLine("Failed to load file: {0}", CheatLoader.FileName); |
230 |
gLog.Verbose.Error.WriteLine(ex.ToString()); |
231 |
MessageBox.Show(string.Format("Failed to open: '{0}'", new FileInfo(CheatLoader.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error); |
232 |
return; |
233 |
} |
234 |
} |
235 |
//MessageBox.Show(string.Format("Successfully opened file: '{0}'", new FileInfo(CheatLoader.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Information); |
236 |
} |
237 |
catch (Exception ex) |
238 |
{ |
239 |
gLog.Error.WriteLine("Failed to load file: {0}", CheatLoader.FileName); |
240 |
gLog.Verbose.Error.WriteLine(ex.ToString()); |
241 |
MessageBox.Show(string.Format("Failed to open: '{0}'", new FileInfo(CheatLoader.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error); |
242 |
} |
243 |
ResizeColumns(); |
244 |
} |
245 |
|
246 |
private void btnRefresh_Click(object sender, EventArgs e) |
247 |
{ |
248 |
int index = 0; |
249 |
foreach (ListViewItem li in lstCheats.Items) |
250 |
{ |
251 |
string name = li.Text; |
252 |
string address = li.SubItems[1].Text; |
253 |
uint physical = Convert.ToUInt32(address, 16) + txtRVA.ToUInt32(); |
254 |
li.SubItems[2].Text = string.Format("0x{0:x8}", physical); |
255 |
lstCheats.Items[index] = li; |
256 |
index++; |
257 |
} |
258 |
ResizeColumns(); |
259 |
} |
260 |
private void btnCopyAll_Click(object sender, EventArgs e) |
261 |
{ |
262 |
ICheatList2 list = new ICheatList2(); |
263 |
list.RVA = txtRVA.ToUInt32(); |
264 |
List<ICheatEntry2> cheats = new List<ICheatEntry2>(); |
265 |
foreach (ListViewItem li in lstCheats.Items) |
266 |
{ |
267 |
cheats.Add(new ICheatEntry2(li.Text, Convert.ToUInt32(li.SubItems[1].Text, 16), Convert.ToUInt32(li.SubItems[2].Text, 16))); |
268 |
} |
269 |
list.Cheats = cheats; |
270 |
StringBuilder builder = new StringBuilder(); |
271 |
builder.AppendFormat("RVA: 0x{0:x8}", list.RVA); |
272 |
builder.AppendLine(); |
273 |
foreach (ColumnHeader t in lstCheats.Columns) |
274 |
{ |
275 |
builder.AppendFormat("{0}:\t\t", t.Text); |
276 |
} |
277 |
builder.AppendLine(); |
278 |
foreach (var cheat in list.Cheats) |
279 |
{ |
280 |
builder.AppendFormat("{0}\t\t0x{1:x8}\t\t0x{2:x8}", cheat.CheatName, cheat.CheatAddress, cheat.PhysicalAddress); |
281 |
builder.AppendLine(); |
282 |
} |
283 |
Clipboard.SetText(builder.ToString()); |
284 |
|
285 |
} |
286 |
private void RVACalculatorDockControl_Shown(object sender, EventArgs e) |
287 |
{ |
288 |
//const int t = 100; |
289 |
////txtRVA.SuspendLayout(); |
290 |
//logger.Debug.WriteLine("txtRva.Width={0}", txtRVA.Width); |
291 |
//logger.Debug.WriteLine("increasing txtRva.Width to {0}", txtRVA.Width + t); |
292 |
//txtRVA.Width = txtRVA.Width + t; |
293 |
//logger.Debug.WriteLine("txtRva.Width={0}", txtRVA.Width); |
294 |
////txtRVA.ResumeLayout(); |
295 |
} |
296 |
|
297 |
[Serializable] |
298 |
private struct ICheatEntry |
299 |
{ |
300 |
public ICheatEntry(string name, uint address) |
301 |
{ |
302 |
CheatName = name; |
303 |
CheatAddress = address; |
304 |
} |
305 |
public string CheatName; |
306 |
public uint CheatAddress; |
307 |
} |
308 |
[Serializable] |
309 |
private struct ICheatEntry2 |
310 |
{ |
311 |
public ICheatEntry2(string name, uint address, uint physical) |
312 |
{ |
313 |
CheatName = name; |
314 |
CheatAddress = address; |
315 |
PhysicalAddress = physical; |
316 |
} |
317 |
public string CheatName; |
318 |
public uint CheatAddress; |
319 |
public uint PhysicalAddress; |
320 |
} |
321 |
[Serializable] |
322 |
private struct ICheatList |
323 |
{ |
324 |
public ICheatList(ICheatList2 t) |
325 |
{ |
326 |
RVA = t.RVA; |
327 |
List<ICheatEntry> cheats = new List<ICheatEntry>(); |
328 |
t.Cheats.ForEach(c => cheats.Add(new ICheatEntry(c.CheatName,c.CheatAddress))); |
329 |
Cheats = cheats; |
330 |
} |
331 |
public ICheatList(uint rva, List<ICheatEntry> cheats) |
332 |
{ |
333 |
RVA = rva; |
334 |
Cheats = cheats; |
335 |
} |
336 |
public uint RVA; |
337 |
public List<ICheatEntry> Cheats; |
338 |
} |
339 |
[Serializable] |
340 |
private struct ICheatList2 |
341 |
{ |
342 |
public ICheatList2(ICheatList t) |
343 |
{ |
344 |
RVA = t.RVA; |
345 |
List<ICheatEntry2> cheats = new List<ICheatEntry2>(); |
346 |
t.Cheats.ForEach(c => cheats.Add(new ICheatEntry2(c.CheatName, c.CheatAddress, c.CheatAddress))); |
347 |
Cheats = cheats; |
348 |
} |
349 |
public ICheatList2(uint rva, List<ICheatEntry2> cheats) |
350 |
{ |
351 |
RVA = rva; |
352 |
Cheats = cheats; |
353 |
} |
354 |
public uint RVA; |
355 |
public List<ICheatEntry2> Cheats; |
356 |
} |
357 |
|
358 |
private void txtRVA_ValueChanged(object sender, ValueChangedEventArgs e) |
359 |
{ |
360 |
btnRefresh.PerformClick(); |
361 |
} |
362 |
|
363 |
|
364 |
} |
365 |
} |