#region Logging Defines // include this any class or method that required logging, and comment-out what is not needed #define LOGGING_ENABLED // this is only valid within the logger class #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 System.IO; using RomCheater.Logging; using RomCheater.Core; using RomCheater.PluginFramework.Core; namespace RomCheater.ScratchPad { public partial class ScratchPad : DockContent { private UserControlPlugin plugin; private bool PostInitDone = false; public ScratchPad(UserControlPlugin plugin) { this.plugin = plugin; InitPluginFramework(); InitializeComponent(); this.PerformPreInit(); } 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 PerformPreInit() { txtScratchPad.Font = new System.Drawing.Font(txtScratchPad.Font, FontStyle.Italic); txtScratchPad.ForeColor = SystemColors.GrayText; StringBuilder builder = new StringBuilder(); txtScratchPad.Clear(); builder.AppendFormat(System.Environment.NewLine); builder.AppendFormat("\tThis is a scratchpad"); builder.AppendFormat(System.Environment.NewLine); builder.AppendFormat("\tYou can type anything in here, and save it for later or load an existing file."); txtScratchPad.AppendText(builder.ToString()); } private void PerformSaveOperation() { } private void mnuItemSave_Click(object sender, EventArgs e) { PerformPostInit(); DialogResult result = ScracthPadSaver.ShowDialog(); if (result != DialogResult.OK) { return; } try { using (FileStream fs = new FileStream(ScracthPadSaver.FileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read)) { try { using (StreamWriter sw = new StreamWriter(fs)) { string line = string.Empty; using (StringReader sr = new StringReader(txtScratchPad.Text)) { while ((line = sr.ReadLine()) != null) { sw.WriteLine(line); } } sw.Flush(); sw.Close(); } } catch (Exception ex) { logger.Error.WriteLine("Failed to save file: {0}", ScracthPadSaver.FileName); logger.VerboseError.WriteLine(ex.ToString()); MessageBox.Show(string.Format("Failed to save: '{0}'", new FileInfo(ScracthPadSaver.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } MessageBox.Show(string.Format("Successfully saved file: '{0}'", new FileInfo(ScracthPadSaver.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { logger.Error.WriteLine("Failed to save file: {0}", ScracthPadSaver.FileName); logger.VerboseError.WriteLine(ex.ToString()); MessageBox.Show(string.Format("Failed to save: '{0}'", new FileInfo(ScracthPadSaver.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void mnuItemOpen_Click(object sender, EventArgs e) { PerformPostInit(); DialogResult result = ScracthPadLoader.ShowDialog(); if (result != DialogResult.OK) { return; } txtScratchPad.Clear(); try { using (FileStream fs = new FileStream(ScracthPadLoader.FileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { try { using (StreamReader sr = new StreamReader(fs)) { txtScratchPad.AppendText(sr.ReadToEnd()); } } catch (Exception ex) { logger.Error.WriteLine("Failed to open file: {0}", ScracthPadLoader.FileName); logger.VerboseError.WriteLine(ex.ToString()); MessageBox.Show(string.Format("Failed to open: '{0}'", new FileInfo(ScracthPadLoader.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //MessageBox.Show(string.Format("Successfully opened file: '{0}'", new FileInfo(ScracthPadLoader.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ex) { logger.Error.WriteLine("Failed to open file: {0}", ScracthPadLoader.FileName); logger.VerboseError.WriteLine(ex.ToString()); MessageBox.Show(string.Format("Failed to open: '{0}'", new FileInfo(ScracthPadLoader.FileName).Name), "", MessageBoxButtons.OK, MessageBoxIcon.Error); } txtScratchPad.SelectionStart = 0; txtScratchPad.ScrollToCaret(); } private void PerformPostInit() { if (!PostInitDone) { txtScratchPad.Clear(); txtScratchPad.Font = new System.Drawing.Font(txtScratchPad.Font, FontStyle.Regular); txtScratchPad.ForeColor = Color.Black; PostInitDone = true; } } private void txtScratchPad_MouseDown(object sender, MouseEventArgs e) { PerformPostInit(); } private void txtScratchPad_KeyDown(object sender, KeyEventArgs e) { PerformPostInit(); } private void mnuItemUndo_Click(object sender, EventArgs e) { PerformPostInit(); txtScratchPad.Focus(); SendKeys.Send("^Z"); } private void mnuItemRedo_Click(object sender, EventArgs e) { PerformPostInit(); txtScratchPad.Focus(); SendKeys.Send("^Y"); } private void mnuItemClear_Click(object sender, EventArgs e) { txtScratchPad.Clear(); } private void mnuItemCopy_Click(object sender, EventArgs e) { PerformPostInit(); string text = txtScratchPad.Text; text = text.Replace("\n", System.Environment.NewLine); Clipboard.SetText(text); } private void mnuItemPaste_Click(object sender, EventArgs e) { PerformPostInit(); string text = Clipboard.GetText(); txtScratchPad.Text = text; } private void ScratchPad_FormClosing(object sender, FormClosingEventArgs e) { if (txtScratchPad.Text != string.Empty && PostInitDone) { DialogResult result = MessageBox.Show("Would you like to save the ScratchPad data before Closing?", "", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); if (result == DialogResult.Cancel) { e.Cancel = true; return; } if (result == DialogResult.Yes) { mnuItemSave.PerformClick(); } } } private void ScratchPad_Deactivate(object sender, T e) where T: EventArgs { try { FormClosingEventArgs args = (e as FormClosingEventArgs); ScratchPad_FormClosing(sender, args); } catch { } } private void txtScratchPad_LinkClicked(object sender, LinkClickedEventArgs e) { //System.Diagnostics.Process.Start(e.LinkText); if (this.plugin != null) { var p = this.plugin.WebBrowserInterface; if (p != null) { p.Navigate(e.LinkText); } else { logger.Debug.WriteLine("Could not navigate to url: '{0}'", e.LinkText); logger.Debug.WriteLine("Could not obtain a handle to the WebBrowser Provider's Interface."); } } else { logger.Debug.WriteLine("Could not navigate to url: '{0}'", e.LinkText); logger.Debug.WriteLine("Loaded plugin is null"); } } } }