#region Logging Defines // include this any class or method that required logging, and comment-out what is not needed #region Enabled logging levels #define LOGGING_ENABLE_INFO #define LOGGING_ENABLE_WARN #define LOGGING_ENABLE_DEBUG #define LOGGING_ENABLE_VERBOSEDEBUG #define LOGGING_ENABLE_ERROR #define LOGGING_ENABLE_VERBOSEERROR #define LOGGING_ENABLE_PROFILER #endregion #endregion using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using WeifenLuo.WinFormsUI.Docking; using RomCheater.Logging; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using RomCheater.PluginFramework.Core; using RomCheater.Core; namespace RomCheater.RVACalculator { public partial class RVACalculatorDockControl : DockContent { private UserControlPlugin plugin; public RVACalculatorDockControl(UserControlPlugin plugin) { this.plugin = plugin; InitPluginFramework(); InitializeComponent(); } private void InitPluginFramework() { if (this.plugin == null) { return; } this.plugin.OnSelectedProcessChanged += new BaseEventHandler(plugin_OnSelectedProcessChanged); this.plugin.OnSelectedConfigChanged += new BaseEventHandler(plugin_OnSelectedConfigChanged); this.plugin.OnPEDataUpdated += new BaseEventHandler(plugin_OnPEDataUpdated); RaisePluginFrameworkEvents(); } bool EventsRaised = false; private void RaisePluginFrameworkEvents() { if (this.plugin == null) { EventsRaised = true; return; } if (!EventsRaised) { this.plugin.RaisePluginFrameworkEvents(); EventsRaised = true; } } void plugin_OnPEDataUpdated(PEViewerDataUpdatedEventArgs e) { //logger.Warn.WriteLine("plugin_OnPEDataUpdated::has not been implemented!"); } void plugin_OnSelectedConfigChanged(ConfigChangedEventArgs e) { //logger.Warn.WriteLine("plugin_OnSelectedConfigChanged::has not been implemented!"); } void plugin_OnSelectedProcessChanged(ProcessChangedEventArgs e) { //logger.Warn.WriteLine("plugin_OnSelectedProcessChanged::has not been implemented!"); } private void ResizeColumns() { foreach (var col in Enumerable.Range(0, lstCheats.Columns.Count)) { lstCheats.AutoResizeColumn(col, ColumnHeaderAutoResizeStyle.ColumnContent); } } private void btnAdd_Click(object sender, EventArgs e) { CheatInputDialog dlg = new CheatInputDialog(); DialogResult result = dlg.ShowDialog(); if (result == DialogResult.Cancel) { return; } uint physical = dlg.CheatAddress + txtRVA.ToUInt32(); ListViewItem li = new ListViewItem(dlg.CheatName); li.SubItems.Add(new ListViewItem.ListViewSubItem(li, string.Format("0x{0:x8}", dlg.CheatAddress))); li.SubItems.Add(new ListViewItem.ListViewSubItem(li, string.Format("0x{0:x8}", physical))); lstCheats.Items.Add(li); ResizeColumns(); } private void btnRemove_Click(object sender, EventArgs e) { if (lstCheats.SelectedItems.Count == 0 || lstCheats.SelectedItems.Count > 1) { return; } int index = lstCheats.SelectedIndices[0]; lstCheats.Items.RemoveAt(index); ResizeColumns(); } private void btnUpdate_Click(object sender, EventArgs e) { if (lstCheats.SelectedItems.Count == 0 || lstCheats.SelectedItems.Count > 1) { return; } int index = lstCheats.SelectedIndices[0]; var li = lstCheats.SelectedItems[0]; string name = li.Text; string address = li.SubItems[1].Text; CheatInputDialog dlg = new CheatInputDialog(name, Convert.ToUInt32(address, 16)); DialogResult result = dlg.ShowDialog(); if (result == DialogResult.Cancel) { return; } uint physical = dlg.CheatAddress + txtRVA.ToUInt32(); li.Text = dlg.CheatName; li.SubItems[1].Text = string.Format("0x{0:x8}", dlg.CheatAddress); li.SubItems[2].Text = string.Format("0x{0:x8}", physical); lstCheats.Items[index] = li; ResizeColumns(); } private void btnCopy_Click(object sender, EventArgs e) { if (lstCheats.SelectedItems.Count == 0 || lstCheats.SelectedItems.Count > 1) { return; } var li = lstCheats.SelectedItems[0]; string physical = li.SubItems[2].Text; Clipboard.SetText(physical); } private void btnSave_Click(object sender, EventArgs e) { DialogResult result = CheatSaver.ShowDialog(); if (result != DialogResult.OK) { return; } ICheatList2 list = new ICheatList2(); list.RVA = txtRVA.ToUInt32(); List cheats = new List(); foreach (ListViewItem li in lstCheats.Items) { cheats.Add(new ICheatEntry2(li.Text, Convert.ToUInt32(li.SubItems[1].Text, 16), Convert.ToUInt32(li.SubItems[2].Text, 16))); } list.Cheats = cheats; try { using (FileStream fs = new FileStream(CheatSaver.FileName, FileMode.Create, FileAccess.ReadWrite, FileShare.Read)) { try { fs.Seek(0, SeekOrigin.Begin); 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); return; } } 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 { ICheatList2 list = new ICheatList2(); BinaryFormatter bin = new BinaryFormatter(); try { fs.Seek(0, SeekOrigin.Begin); var t_list = (ICheatList2)bin.Deserialize(fs); list = t_list; } catch (Exception) { fs.Seek(0, SeekOrigin.Begin); var t_list = (ICheatList)bin.Deserialize(fs); list = new ICheatList2(t_list); } txtRVA.Value = list.RVA; if (lstCheats.Items.Count > 0) { 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); return; } } 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); } ResizeColumns(); } private void btnRefresh_Click(object sender, EventArgs e) { int index = 0; foreach (ListViewItem li in lstCheats.Items) { string name = li.Text; string address = li.SubItems[1].Text; uint physical = Convert.ToUInt32(address, 16) + txtRVA.ToUInt32(); li.SubItems[2].Text = string.Format("0x{0:x8}", physical); lstCheats.Items[index] = li; index++; } ResizeColumns(); } private void btnCopyAll_Click(object sender, EventArgs e) { ICheatList2 list = new ICheatList2(); list.RVA = txtRVA.ToUInt32(); List cheats = new List(); foreach (ListViewItem li in lstCheats.Items) { cheats.Add(new ICheatEntry2(li.Text, Convert.ToUInt32(li.SubItems[1].Text, 16), Convert.ToUInt32(li.SubItems[2].Text, 16))); } list.Cheats = cheats; StringBuilder builder = new StringBuilder(); builder.AppendFormat("RVA: 0x{0:x8}", list.RVA); builder.AppendLine(); foreach (ColumnHeader t in lstCheats.Columns) { builder.AppendFormat("{0}:\t\t", t.Text); } builder.AppendLine(); foreach (var cheat in list.Cheats) { builder.AppendFormat("{0}\t\t0x{1:x8}\t\t0x{2:x8}", cheat.CheatName, cheat.CheatAddress, cheat.PhysicalAddress); builder.AppendLine(); } Clipboard.SetText(builder.ToString()); } private void RVACalculatorDockControl_Shown(object sender, EventArgs e) { //const int t = 100; ////txtRVA.SuspendLayout(); //logger.Debug.WriteLine("txtRva.Width={0}", txtRVA.Width); //logger.Debug.WriteLine("increasing txtRva.Width to {0}", txtRVA.Width + t); //txtRVA.Width = txtRVA.Width + t; //logger.Debug.WriteLine("txtRva.Width={0}", txtRVA.Width); ////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 ICheatEntry2 { public ICheatEntry2(string name, uint address, uint physical) { CheatName = name; CheatAddress = address; PhysicalAddress = physical; } public string CheatName; public uint CheatAddress; public uint PhysicalAddress; } [Serializable] private struct ICheatList { public ICheatList(ICheatList2 t) { RVA = t.RVA; List cheats = new List(); t.Cheats.ForEach(c => cheats.Add(new ICheatEntry(c.CheatName,c.CheatAddress))); Cheats = cheats; } public ICheatList(uint rva, List cheats) { RVA = rva; Cheats = cheats; } public uint RVA; public List Cheats; } [Serializable] private struct ICheatList2 { public ICheatList2(ICheatList t) { RVA = t.RVA; List cheats = new List(); t.Cheats.ForEach(c => cheats.Add(new ICheatEntry2(c.CheatName, c.CheatAddress, c.CheatAddress))); Cheats = cheats; } public ICheatList2(uint rva, List cheats) { RVA = rva; Cheats = cheats; } public uint RVA; public List Cheats; } private void txtRVA_ValueChanged(object sender, ValueChangedEventArgs e) { btnRefresh.PerformClick(); } } }