ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/RomCheater/trunk/RomCheater.RVACalculator/RVACalculatorDockControl.cs
Revision: 700
Committed: Mon Jun 17 13:09:29 2013 UTC (9 years, 1 month ago) by william
File size: 15503 byte(s)
Log Message:

File Contents

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