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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22