/[RomCheater]/trunk/RomCheater.RVACalculator/RVACalculatorDockControl.cs
ViewVC logotype

Contents of /trunk/RomCheater.RVACalculator/RVACalculatorDockControl.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 701 - (show annotations) (download)
Mon Jun 17 13:14:15 2013 UTC (8 years, 4 months ago) by william
File size: 15593 byte(s)
+ auto-size columns for RVA Calculator: Fit the column content(s) and then fit header 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 lstCheats.AutoResizeColumn(col, ColumnHeaderAutoResizeStyle.HeaderSize);
80 }
81 }
82
83 private void btnAdd_Click(object sender, EventArgs e)
84 {
85 CheatInputDialog dlg = new CheatInputDialog();
86 DialogResult result = dlg.ShowDialog();
87 if (result == DialogResult.Cancel) { return; }
88 uint physical = dlg.CheatAddress + txtRVA.ToUInt32();
89 ListViewItem li = new ListViewItem(dlg.CheatName);
90 li.SubItems.Add(new ListViewItem.ListViewSubItem(li, string.Format("0x{0:x8}", dlg.CheatAddress)));
91 li.SubItems.Add(new ListViewItem.ListViewSubItem(li, string.Format("0x{0:x8}", physical)));
92 lstCheats.Items.Add(li);
93 ResizeColumns();
94 }
95
96 private void btnRemove_Click(object sender, EventArgs e)
97 {
98 if (lstCheats.SelectedItems.Count == 0 || lstCheats.SelectedItems.Count > 1) { return; }
99 int index = lstCheats.SelectedIndices[0];
100 lstCheats.Items.RemoveAt(index);
101 ResizeColumns();
102 }
103
104 private void btnUpdate_Click(object sender, EventArgs e)
105 {
106 if (lstCheats.SelectedItems.Count == 0 || lstCheats.SelectedItems.Count > 1) { return; }
107 int index = lstCheats.SelectedIndices[0];
108 var li = lstCheats.SelectedItems[0];
109 string name = li.Text;
110 string address = li.SubItems[1].Text;
111
112 CheatInputDialog dlg = new CheatInputDialog(name, Convert.ToUInt32(address, 16));
113 DialogResult result = dlg.ShowDialog();
114 if (result == DialogResult.Cancel) { return; }
115 uint physical = dlg.CheatAddress + txtRVA.ToUInt32();
116 li.Text = dlg.CheatName;
117 li.SubItems[1].Text = string.Format("0x{0:x8}", dlg.CheatAddress);
118 li.SubItems[2].Text = string.Format("0x{0:x8}", physical);
119 lstCheats.Items[index] = li;
120 ResizeColumns();
121 }
122
123 private void btnCopy_Click(object sender, EventArgs e)
124 {
125 if (lstCheats.SelectedItems.Count == 0 || lstCheats.SelectedItems.Count > 1) { return; }
126 var li = lstCheats.SelectedItems[0];
127 string physical = li.SubItems[2].Text;
128 Clipboard.SetText(physical);
129 }
130
131 private void btnSave_Click(object sender, EventArgs e)
132 {
133 DialogResult result = CheatSaver.ShowDialog();
134 if (result != DialogResult.OK) { return; }
135
136
137 ICheatList2 list = new ICheatList2();
138 list.RVA = txtRVA.ToUInt32();
139 List<ICheatEntry2> cheats = new List<ICheatEntry2>();
140 foreach (ListViewItem li in lstCheats.Items)
141 {
142 cheats.Add(new ICheatEntry2(li.Text, Convert.ToUInt32(li.SubItems[1].Text, 16), Convert.ToUInt32(li.SubItems[2].Text, 16)));
143 }
144
145 list.Cheats = cheats;
146
147 try
148 {
149 using (FileStream fs = new FileStream(CheatSaver.FileName, FileMode.Create, FileAccess.ReadWrite, FileShare.Read))
150 {
151 try
152 {
153 fs.Seek(0, SeekOrigin.Begin);
154 BinaryFormatter bin = new BinaryFormatter();
155 bin.Serialize(fs, list);
156 }
157 catch (Exception ex)
158 {
159 logger.Error.WriteLine("Failed to save file: {0}", CheatSaver.FileName);
160 logger.VerboseError.WriteLine(ex.ToString());
161 MessageBox.Show(string.Format("Failed to save: '{0}'", new FileInfo(CheatSaver.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
162 return;
163 }
164 }
165 MessageBox.Show(string.Format("Successfully saved file: '{0}'", new FileInfo(CheatSaver.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Information);
166 }
167 catch (Exception ex)
168 {
169 logger.Error.WriteLine("Failed to save file: {0}", CheatSaver.FileName);
170 logger.VerboseError.WriteLine(ex.ToString());
171 MessageBox.Show(string.Format("Failed to save: '{0}'", new FileInfo(CheatSaver.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
172 }
173 }
174
175 private void btnLoad_Click(object sender, EventArgs e)
176 {
177 DialogResult result = CheatLoader.ShowDialog();
178 if (result != DialogResult.OK) { return; }
179 try
180 {
181 using (FileStream fs = new FileStream(CheatLoader.FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
182 {
183 try
184 {
185 ICheatList2 list = new ICheatList2();
186 BinaryFormatter bin = new BinaryFormatter();
187 try
188 {
189 fs.Seek(0, SeekOrigin.Begin);
190 var t_list = (ICheatList2)bin.Deserialize(fs);
191 list = t_list;
192 }
193 catch (Exception)
194 {
195 fs.Seek(0, SeekOrigin.Begin);
196 var t_list = (ICheatList)bin.Deserialize(fs);
197 list = new ICheatList2(t_list);
198 }
199
200 txtRVA.Value = list.RVA;
201 if (lstCheats.Items.Count > 0)
202 {
203 result = MessageBox.Show("Clear existing Cheats?", "", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3);
204 if (result == System.Windows.Forms.DialogResult.Cancel)
205 {
206 // assume abort of load
207 logger.Warn.WriteLine("Abored processing of file (by user request): {0}", CheatLoader.FileName);
208 fs.Close();
209 return;
210 }
211 if (result == DialogResult.Yes)
212 {
213 lstCheats.Items.Clear();
214 }
215 }
216 foreach (var cheat in list.Cheats)
217 {
218 ListViewItem li = new ListViewItem(cheat.CheatName);
219 li.SubItems.Add(new ListViewItem.ListViewSubItem(li, string.Format("0x{0:x8}", cheat.CheatAddress)));
220 uint physical = cheat.CheatAddress + list.RVA;
221 li.SubItems.Add(new ListViewItem.ListViewSubItem(li, string.Format("0x{0:x8}", physical)));
222 lstCheats.Items.Add(li);
223 }
224 btnRefresh.PerformClick(); // do any needed refreshing
225 }
226 catch (Exception ex)
227 {
228 logger.Error.WriteLine("Failed to load file: {0}", CheatLoader.FileName);
229 logger.VerboseError.WriteLine(ex.ToString());
230 MessageBox.Show(string.Format("Failed to open: '{0}'", new FileInfo(CheatLoader.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
231 return;
232 }
233 }
234 MessageBox.Show(string.Format("Successfully opened file: '{0}'", new FileInfo(CheatLoader.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Information);
235 }
236 catch (Exception ex)
237 {
238 logger.Error.WriteLine("Failed to load file: {0}", CheatLoader.FileName);
239 logger.VerboseError.WriteLine(ex.ToString());
240 MessageBox.Show(string.Format("Failed to open: '{0}'", new FileInfo(CheatLoader.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
241 }
242 ResizeColumns();
243 }
244
245 private void btnRefresh_Click(object sender, EventArgs e)
246 {
247 int index = 0;
248 foreach (ListViewItem li in lstCheats.Items)
249 {
250 string name = li.Text;
251 string address = li.SubItems[1].Text;
252 uint physical = Convert.ToUInt32(address, 16) + txtRVA.ToUInt32();
253 li.SubItems[2].Text = string.Format("0x{0:x8}", physical);
254 lstCheats.Items[index] = li;
255 index++;
256 }
257 ResizeColumns();
258 }
259 private void btnCopyAll_Click(object sender, EventArgs e)
260 {
261 ICheatList2 list = new ICheatList2();
262 list.RVA = txtRVA.ToUInt32();
263 List<ICheatEntry2> cheats = new List<ICheatEntry2>();
264 foreach (ListViewItem li in lstCheats.Items)
265 {
266 cheats.Add(new ICheatEntry2(li.Text, Convert.ToUInt32(li.SubItems[1].Text, 16), Convert.ToUInt32(li.SubItems[2].Text, 16)));
267 }
268 list.Cheats = cheats;
269 StringBuilder builder = new StringBuilder();
270 builder.AppendFormat("RVA: 0x{0:x8}", list.RVA);
271 builder.AppendLine();
272 foreach (ColumnHeader t in lstCheats.Columns)
273 {
274 builder.AppendFormat("{0}:\t\t", t.Text);
275 }
276 builder.AppendLine();
277 foreach (var cheat in list.Cheats)
278 {
279 builder.AppendFormat("{0}\t\t0x{1:x8}\t\t0x{2:x8}", cheat.CheatName, cheat.CheatAddress, cheat.PhysicalAddress);
280 builder.AppendLine();
281 }
282 Clipboard.SetText(builder.ToString());
283
284 }
285 private void RVACalculatorDockControl_Shown(object sender, EventArgs e)
286 {
287 //const int t = 100;
288 ////txtRVA.SuspendLayout();
289 //logger.Debug.WriteLine("txtRva.Width={0}", txtRVA.Width);
290 //logger.Debug.WriteLine("increasing txtRva.Width to {0}", txtRVA.Width + t);
291 //txtRVA.Width = txtRVA.Width + t;
292 //logger.Debug.WriteLine("txtRva.Width={0}", txtRVA.Width);
293 ////txtRVA.ResumeLayout();
294 }
295
296 [Serializable]
297 private struct ICheatEntry
298 {
299 public ICheatEntry(string name, uint address)
300 {
301 CheatName = name;
302 CheatAddress = address;
303 }
304 public string CheatName;
305 public uint CheatAddress;
306 }
307 [Serializable]
308 private struct ICheatEntry2
309 {
310 public ICheatEntry2(string name, uint address, uint physical)
311 {
312 CheatName = name;
313 CheatAddress = address;
314 PhysicalAddress = physical;
315 }
316 public string CheatName;
317 public uint CheatAddress;
318 public uint PhysicalAddress;
319 }
320 [Serializable]
321 private struct ICheatList
322 {
323 public ICheatList(ICheatList2 t)
324 {
325 RVA = t.RVA;
326 List<ICheatEntry> cheats = new List<ICheatEntry>();
327 t.Cheats.ForEach(c => cheats.Add(new ICheatEntry(c.CheatName,c.CheatAddress)));
328 Cheats = cheats;
329 }
330 public ICheatList(uint rva, List<ICheatEntry> cheats)
331 {
332 RVA = rva;
333 Cheats = cheats;
334 }
335 public uint RVA;
336 public List<ICheatEntry> Cheats;
337 }
338 [Serializable]
339 private struct ICheatList2
340 {
341 public ICheatList2(ICheatList t)
342 {
343 RVA = t.RVA;
344 List<ICheatEntry2> cheats = new List<ICheatEntry2>();
345 t.Cheats.ForEach(c => cheats.Add(new ICheatEntry2(c.CheatName, c.CheatAddress, c.CheatAddress)));
346 Cheats = cheats;
347 }
348 public ICheatList2(uint rva, List<ICheatEntry2> cheats)
349 {
350 RVA = rva;
351 Cheats = cheats;
352 }
353 public uint RVA;
354 public List<ICheatEntry2> Cheats;
355 }
356
357 private void txtRVA_ValueChanged(object sender, ValueChangedEventArgs e)
358 {
359 btnRefresh.PerformClick();
360 }
361
362
363 }
364 }

  ViewVC Help
Powered by ViewVC 1.1.22