ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/RomCheater/trunk/RomCheater.RVACalculator/RVACalculatorDockControl.cs
Revision: 691
Committed: Mon Jun 17 10:07:40 2013 UTC (9 years, 11 months ago) by william
File size: 15579 byte(s)
Log Message:
+ comment-out the not implemeted messages ...

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