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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/RomCheater.RVACalculator/RVACalculatorDockControl.cs	2013/06/17 03:32:25	667
+++ trunk/RomCheater.RVACalculator/RVACalculatorDockControl.cs	2013/06/17 04:49:09	668
@@ -21,6 +21,8 @@
 using System.Windows.Forms;
 using WeifenLuo.WinFormsUI.Docking;
 using RomCheater.Logging;
+using System.IO;
+using System.Runtime.Serialization.Formatters.Binary;
 
 namespace RomCheater.RVACalculator
 {
@@ -85,12 +87,97 @@
 
         private void btnSave_Click(object sender, EventArgs e)
         {
+            DialogResult result = CheatSaver.ShowDialog();
+            if (result != DialogResult.OK) { return; }
 
+            
+            ICheatList list = new ICheatList();
+            list.RVA = txtRVA.ToUInt32();
+            List<ICheatEntry> cheats = new List<ICheatEntry>();
+            foreach (ListViewItem li in lstCheats.Items)
+            {
+                cheats.Add(new ICheatEntry(li.Text, Convert.ToUInt32(li.SubItems[1].Text, 16)));
+            }
+
+            list.Cheats = cheats;
+
+            try
+            {
+                using (FileStream fs = new FileStream(CheatSaver.FileName, FileMode.Create, FileAccess.ReadWrite, FileShare.Read))
+                {
+                    try
+                    {
+                        BinaryFormatter bin = new BinaryFormatter();
+                        bin.Serialize(fs, list);
+                    }
+                    catch (Exception ex)
+                    {
+                        logger.Error.WriteLine("Failed to save file: {0}", CheatSaver.FileName);
+                        logger.VerboseError.WriteLine(ex.ToString());
+                        MessageBox.Show(string.Format("Failed to save: '{0}'", new FileInfo(CheatSaver.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
+                    }
+                }
+                MessageBox.Show(string.Format("Successfully saved file: '{0}'", new FileInfo(CheatSaver.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Information);
+            }
+            catch (Exception ex)
+            {
+                logger.Error.WriteLine("Failed to save file: {0}", CheatSaver.FileName);
+                logger.VerboseError.WriteLine(ex.ToString());
+                MessageBox.Show(string.Format("Failed to save: '{0}'", new FileInfo(CheatSaver.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
+            }
         }
 
         private void btnLoad_Click(object sender, EventArgs e)
         {
-
+            DialogResult result = CheatLoader.ShowDialog();
+            if (result != DialogResult.OK) { return; }
+            try
+            {
+                using (FileStream fs = new FileStream(CheatLoader.FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
+                {
+                    try
+                    {
+                        BinaryFormatter bin = new BinaryFormatter();
+                        var list = (ICheatList)bin.Deserialize(fs);
+
+                        txtRVA.Value = list.RVA;
+                        result = MessageBox.Show("Clear existing Cheats?", "", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3);
+                        if (result == System.Windows.Forms.DialogResult.Cancel)
+                        {
+                            // assume abort of load
+                            logger.Warn.WriteLine("Abored processing of file (by user request): {0}", CheatLoader.FileName);
+                            fs.Close();
+                            return;
+                        }
+                        if (result == DialogResult.Yes)
+                        {
+                            lstCheats.Items.Clear();
+                        }
+                        foreach (var cheat in list.Cheats)
+                        {
+                            ListViewItem li = new ListViewItem(cheat.CheatName);
+                            li.SubItems.Add(new ListViewItem.ListViewSubItem(li, string.Format("0x{0:x8}", cheat.CheatAddress)));
+                            uint physical = cheat.CheatAddress + list.RVA;
+                            li.SubItems.Add(new ListViewItem.ListViewSubItem(li, string.Format("0x{0:x8}", physical)));
+                            lstCheats.Items.Add(li);
+                        }
+                        btnRefresh.PerformClick(); // do any needed refreshing
+                    }
+                    catch (Exception ex)
+                    {
+                        logger.Error.WriteLine("Failed to load file: {0}", CheatLoader.FileName);
+                        logger.VerboseError.WriteLine(ex.ToString());
+                        MessageBox.Show(string.Format("Failed to open: '{0}'", new FileInfo(CheatLoader.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
+                    }
+                }
+                MessageBox.Show(string.Format("Successfully opened file: '{0}'", new FileInfo(CheatLoader.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Information);
+            }
+            catch (Exception ex)
+            {
+                logger.Error.WriteLine("Failed to load file: {0}", CheatLoader.FileName);
+                logger.VerboseError.WriteLine(ex.ToString());
+                MessageBox.Show(string.Format("Failed to open: '{0}'", new FileInfo(CheatLoader.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
+            }
         }
 
         private void btnRefresh_Click(object sender, EventArgs e)
@@ -118,6 +205,23 @@
             ////txtRVA.ResumeLayout();
         }
 
+        [Serializable]
+        private struct ICheatEntry
+        {
+            public ICheatEntry(string name, uint address)
+            {
+                CheatName = name;
+                CheatAddress = address;
+            }
+            public string CheatName;
+            public uint CheatAddress;
+        }
+        [Serializable]
+        private struct ICheatList
+        {
+            public uint RVA;
+            public List<ICheatEntry> Cheats;
+        }
 
     }
 }

 

  ViewVC Help
Powered by ViewVC 1.1.22