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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 669 - (show annotations) (download)
Mon Jun 17 05:11:25 2013 UTC (7 years, 11 months ago) by william
File size: 13570 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 result = MessageBox.Show("Clear existing Cheats?", "", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3);
159 if (result == System.Windows.Forms.DialogResult.Cancel)
160 {
161 // assume abort of load
162 logger.Warn.WriteLine("Abored processing of file (by user request): {0}", CheatLoader.FileName);
163 fs.Close();
164 return;
165 }
166 if (result == DialogResult.Yes)
167 {
168 lstCheats.Items.Clear();
169 }
170 foreach (var cheat in list.Cheats)
171 {
172 ListViewItem li = new ListViewItem(cheat.CheatName);
173 li.SubItems.Add(new ListViewItem.ListViewSubItem(li, string.Format("0x{0:x8}", cheat.CheatAddress)));
174 uint physical = cheat.CheatAddress + list.RVA;
175 li.SubItems.Add(new ListViewItem.ListViewSubItem(li, string.Format("0x{0:x8}", physical)));
176 lstCheats.Items.Add(li);
177 }
178 btnRefresh.PerformClick(); // do any needed refreshing
179 }
180 catch (Exception ex)
181 {
182 logger.Error.WriteLine("Failed to load file: {0}", CheatLoader.FileName);
183 logger.VerboseError.WriteLine(ex.ToString());
184 MessageBox.Show(string.Format("Failed to open: '{0}'", new FileInfo(CheatLoader.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
185 return;
186 }
187 }
188 MessageBox.Show(string.Format("Successfully opened file: '{0}'", new FileInfo(CheatLoader.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Information);
189 }
190 catch (Exception ex)
191 {
192 logger.Error.WriteLine("Failed to load file: {0}", CheatLoader.FileName);
193 logger.VerboseError.WriteLine(ex.ToString());
194 MessageBox.Show(string.Format("Failed to open: '{0}'", new FileInfo(CheatLoader.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
195 }
196 }
197
198 private void btnRefresh_Click(object sender, EventArgs e)
199 {
200 int index = 0;
201 foreach (ListViewItem li in lstCheats.Items)
202 {
203 string name = li.Text;
204 string address = li.SubItems[1].Text;
205 uint physical = Convert.ToUInt32(address, 16) + txtRVA.ToUInt32();
206 li.SubItems[2].Text = string.Format("0x{0:x8}", physical);
207 lstCheats.Items[index] = li;
208 index++;
209 }
210 }
211 private void btnCopyAll_Click(object sender, EventArgs e)
212 {
213 ICheatList2 list = new ICheatList2();
214 list.RVA = txtRVA.ToUInt32();
215 List<ICheatEntry2> cheats = new List<ICheatEntry2>();
216 foreach (ListViewItem li in lstCheats.Items)
217 {
218 cheats.Add(new ICheatEntry2(li.Text, Convert.ToUInt32(li.SubItems[1].Text, 16), Convert.ToUInt32(li.SubItems[2].Text, 16)));
219 }
220 StringBuilder builder = new StringBuilder();
221 builder.AppendFormat("RVA: 0x{0:x8}", list.RVA);
222 builder.AppendLine();
223 foreach (ColumnHeader t in lstCheats.Columns)
224 {
225 builder.AppendFormat("{0}:\t", t.Name);
226 }
227 builder.AppendLine();
228 foreach (var cheat in list.Cheats)
229 {
230 builder.AppendFormat("{0}\t{1}\t{2}", cheat.CheatName, cheat.CheatAddress, cheat.PhysicalAddress);
231 builder.AppendLine();
232 }
233 Clipboard.SetText(builder.ToString());
234
235 }
236 private void RVACalculatorDockControl_Shown(object sender, EventArgs e)
237 {
238 //const int t = 100;
239 ////txtRVA.SuspendLayout();
240 //logger.Debug.WriteLine("txtRva.Width={0}", txtRVA.Width);
241 //logger.Debug.WriteLine("increasing txtRva.Width to {0}", txtRVA.Width + t);
242 //txtRVA.Width = txtRVA.Width + t;
243 //logger.Debug.WriteLine("txtRva.Width={0}", txtRVA.Width);
244 ////txtRVA.ResumeLayout();
245 }
246
247 [Serializable]
248 private struct ICheatEntry
249 {
250 public ICheatEntry(string name, uint address)
251 {
252 CheatName = name;
253 CheatAddress = address;
254 }
255 public string CheatName;
256 public uint CheatAddress;
257 }
258 [Serializable]
259 private struct ICheatEntry2
260 {
261 public ICheatEntry2(string name, uint address, uint physical)
262 {
263 CheatName = name;
264 CheatAddress = address;
265 PhysicalAddress = physical;
266 }
267 public string CheatName;
268 public uint CheatAddress;
269 public uint PhysicalAddress;
270 }
271 [Serializable]
272 private struct ICheatList
273 {
274 public ICheatList(ICheatList2 t)
275 {
276 RVA = t.RVA;
277 List<ICheatEntry> cheats = new List<ICheatEntry>();
278 t.Cheats.ForEach(c => cheats.Add(new ICheatEntry(c.CheatName,c.CheatAddress)));
279 Cheats = cheats;
280 }
281 public ICheatList(uint rva, List<ICheatEntry> cheats)
282 {
283 RVA = rva;
284 Cheats = cheats;
285 }
286 public uint RVA;
287 public List<ICheatEntry> Cheats;
288 }
289 [Serializable]
290 private struct ICheatList2
291 {
292 public ICheatList2(ICheatList t)
293 {
294 RVA = t.RVA;
295 List<ICheatEntry2> cheats = new List<ICheatEntry2>();
296 t.Cheats.ForEach(c => cheats.Add(new ICheatEntry2(c.CheatName, c.CheatAddress, c.CheatAddress)));
297 Cheats = cheats;
298 }
299 public ICheatList2(uint rva, List<ICheatEntry2> cheats)
300 {
301 RVA = rva;
302 Cheats = cheats;
303 }
304 public uint RVA;
305 public List<ICheatEntry2> Cheats;
306 }
307
308
309 }
310 }

  ViewVC Help
Powered by ViewVC 1.1.22