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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 814 - (hide annotations) (download)
Tue Apr 15 15:58:50 2014 UTC (7 years ago) by william
File size: 15632 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22