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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 691 - (hide annotations) (download)
Mon Jun 17 10:07:40 2013 UTC (8 years, 5 months ago) by william
File size: 15579 byte(s)
+ comment-out the not implemeted messages ...

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

  ViewVC Help
Powered by ViewVC 1.1.22