ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/RomCheater/trunk/RomCheater.RVACalculator/RVACalculatorDockControl.cs
Revision: 700
Committed: Mon Jun 17 13:09:29 2013 UTC (10 years, 3 months ago) by william
File size: 15503 byte(s)
Log Message:

File Contents

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