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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 684 - (show annotations) (download)
Mon Jun 17 08:52:54 2013 UTC (8 years, 4 months ago) by william
File size: 15370 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22