/[RomCheater]/trunk/RomCheater.RVACheatList/RVACheatListDockControl.cs
ViewVC logotype

Contents of /trunk/RomCheater.RVACheatList/RVACheatListDockControl.cs

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22