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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22