diff --git a/.gitignore b/.gitignore index e43e824099..a804025af1 100644 --- a/.gitignore +++ b/.gitignore @@ -246,7 +246,7 @@ /References/*.xml /output/ELFSharp.dll /output/dll/ELFSharp.dll -/output/ExternalTools/*.dll +/output/ExternalTools/*.* *.opensdf *.user *.suo diff --git a/BizHawk.Client.EmuHawk/MainForm.Designer.cs b/BizHawk.Client.EmuHawk/MainForm.Designer.cs index a18e41fc28..68603d3b1e 100644 --- a/BizHawk.Client.EmuHawk/MainForm.Designer.cs +++ b/BizHawk.Client.EmuHawk/MainForm.Designer.cs @@ -209,6 +209,7 @@ this.CheatsMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.LuaConsoleMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.externalToolToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.dummyExternalTool = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripSeparator29 = new System.Windows.Forms.ToolStripSeparator(); this.MultiDiskBundlerFileMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.gameSharkConverterToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); @@ -409,7 +410,6 @@ this.ShowMenuContextMenuSeparator = new System.Windows.Forms.ToolStripSeparator(); this.ShowMenuContextMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.timerMouseIdle = new System.Windows.Forms.Timer(this.components); - this.dummyExternalTool = new System.Windows.Forms.ToolStripMenuItem(); this.MainformMenu.SuspendLayout(); this.MainStatusBar.SuspendLayout(); this.MainFormContextMenu.SuspendLayout(); @@ -1985,6 +1985,12 @@ this.externalToolToolStripMenuItem.Text = "External Tool"; this.externalToolToolStripMenuItem.DropDownOpening += new System.EventHandler(this.ExternalToolToolStripMenuItem_DropDownOpening); // + // dummyExternalTool + // + this.dummyExternalTool.Name = "dummyExternalTool"; + this.dummyExternalTool.Size = new System.Drawing.Size(103, 22); + this.dummyExternalTool.Text = "None"; + // // toolStripSeparator29 // this.toolStripSeparator29.Name = "toolStripSeparator29"; @@ -3622,12 +3628,6 @@ this.timerMouseIdle.Interval = 2000; this.timerMouseIdle.Tick += new System.EventHandler(this.timerMouseIdle_Tick); // - // dummyExternalTool - // - this.dummyExternalTool.Name = "dummyExternalTool"; - this.dummyExternalTool.Size = new System.Drawing.Size(172, 22); - this.dummyExternalTool.Text = "DummyItemMenu"; - // // MainForm // this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None; diff --git a/BizHawk.Client.EmuHawk/MainForm.Events.cs b/BizHawk.Client.EmuHawk/MainForm.Events.cs index 33813d0ac3..197b6bb96b 100644 --- a/BizHawk.Client.EmuHawk/MainForm.Events.cs +++ b/BizHawk.Client.EmuHawk/MainForm.Events.cs @@ -1250,7 +1250,7 @@ namespace BizHawk.Client.EmuHawk AutoHawkMenuItem.Visible = VersionInfo.DeveloperBuild; BasicBotMenuItem.Enabled = GlobalWin.Tools.IsAvailable(); - + gameSharkConverterToolStripMenuItem.Enabled = GlobalWin.Tools.IsAvailable(); } @@ -1261,11 +1261,25 @@ namespace BizHawk.Client.EmuHawk if (Directory.Exists(path)) { DirectoryInfo dInfo = new DirectoryInfo(path); - foreach (FileInfo fi in dInfo.GetFiles("*.dll")) + Type[] assemblyTypes; + Assembly externalToolFile; + foreach (FileInfo fi in dInfo.GetFiles("*.dll")) { - Assembly externalToolFile = Assembly.ReflectionOnlyLoadFrom(fi.FullName); + try + { + externalToolFile = Assembly.ReflectionOnlyLoadFrom(fi.FullName); + } + catch (BadImageFormatException) + { + ToolStripMenuItem item = new ToolStripMenuItem(fi.Name, Properties.Resources.ExclamationRed); + item.ToolTipText = "This is not an assembly"; + item.ForeColor = Color.Gray; + externalToolToolStripMenuItem.DropDownItems.Add(item); + continue; + } + ToolStripMenuItem externalToolMenu = new ToolStripMenuItem(externalToolFile.GetName().Name); - Type[] assemblyTypes; + /* The reason of using this ugly try catch is due to the use of ReflectionOnlyLoadFrom methods When the assembly is loaded this way, referenced assemblies are not loaded and so, as soon as a type @@ -1275,28 +1289,34 @@ namespace BizHawk.Client.EmuHawk */ try { - assemblyTypes = externalToolFile.GetTypes(); - } - catch(ReflectionTypeLoadException ex) + assemblyTypes = externalToolFile.GetTypes(); + } + catch (ReflectionTypeLoadException ex) { assemblyTypes = ex.Types.Where(t => t != null && t.FullName.Contains("BizHawk.Client.EmuHawk.CustomMainForm")).ToArray(); } if (assemblyTypes.Count() == 1) { - externalToolMenu.Image = Properties.Resources.Debugger; + externalToolMenu.Image = Properties.Resources.Debugger; + externalToolMenu.Tag = fi.FullName; + externalToolMenu.Click += delegate (object sender2, EventArgs e2) + { + GlobalWin.Tools.Load(fi.FullName); + }; } else { - externalToolMenu.Image = Properties.Resources.ExclamationRed; + externalToolMenu.Image = Properties.Resources.ExclamationRed; + externalToolMenu.ForeColor = Color.Gray; } externalToolToolStripMenuItem.DropDownItems.Add(externalToolMenu); - } - } - if(externalToolToolStripMenuItem.DropDownItems.Count == 0) - { - externalToolToolStripMenuItem.DropDownItems.Add("Dummy External Tool"); + } } + if (externalToolToolStripMenuItem.DropDownItems.Count == 0) + { + externalToolToolStripMenuItem.DropDownItems.Add("None"); + } } private void AutoHawkMenuItem_Click(object sender, EventArgs e) @@ -1362,7 +1382,7 @@ namespace BizHawk.Client.EmuHawk private void LuaConsoleMenuItem_Click(object sender, EventArgs e) { OpenLuaConsole(); - } + } private void batchRunnerToolStripMenuItem_Click(object sender, EventArgs e) { diff --git a/BizHawk.Client.EmuHawk/tools/IExternalToolForm.cs b/BizHawk.Client.EmuHawk/tools/IExternalToolForm.cs index c758942ba7..f8116f705c 100644 --- a/BizHawk.Client.EmuHawk/tools/IExternalToolForm.cs +++ b/BizHawk.Client.EmuHawk/tools/IExternalToolForm.cs @@ -1,14 +1,16 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using System.Windows.Forms; namespace BizHawk.Client.EmuHawk { /// - /// Interface to implements in order to make a custom tool for a specific game + /// Interface to implements in order to make a custom tool /// - public interface IExternalToolForm :IToolForm + public interface IExternalToolForm : IToolForm { + /// + /// + /// + event FormClosedEventHandler FormClosed; } } diff --git a/BizHawk.Client.EmuHawk/tools/ToolBox.cs b/BizHawk.Client.EmuHawk/tools/ToolBox.cs index e25645e2d4..3105a711ff 100644 --- a/BizHawk.Client.EmuHawk/tools/ToolBox.cs +++ b/BizHawk.Client.EmuHawk/tools/ToolBox.cs @@ -53,6 +53,8 @@ namespace BizHawk.Client.EmuHawk private void SetTools() { + ToolBoxStrip.Items.Clear(); + var availableTools = Assembly .GetAssembly(typeof(IToolForm)) .GetTypes() @@ -73,8 +75,8 @@ namespace BizHawk.Client.EmuHawk ShowIcon = (instance as Form).ShowIcon }) .ToList(); - - foreach (var tool in availableTools) + + foreach (var tool in availableTools) { var t = new ToolStripButton { diff --git a/BizHawk.Client.EmuHawk/tools/ToolManager.cs b/BizHawk.Client.EmuHawk/tools/ToolManager.cs index bb7dbfa939..7e377033c4 100644 --- a/BizHawk.Client.EmuHawk/tools/ToolManager.cs +++ b/BizHawk.Client.EmuHawk/tools/ToolManager.cs @@ -16,10 +16,7 @@ namespace BizHawk.Client.EmuHawk { public class ToolManager { - public ToolManager(Form owner) - { - _owner = owner; - } + #region Fields private readonly Form _owner; @@ -28,31 +25,70 @@ namespace BizHawk.Client.EmuHawk // Also a UsesRam, and similar method private readonly List _tools = new List(); - /// - /// Loads the tool dialog T, if it does not exist it will be created, if it is already open, it will be focused - /// - public T Load(bool focus = true) where T : IToolForm - { - return (T)Load(typeof(T), focus); - } + #endregion + + #region cTor(s) /// - /// Loads a tool dialog of type toolType if it does not exist it will be - /// created, if it is already open, it will be focused. + /// Initialize an new ToolManager instance /// - public IToolForm Load(Type toolType, bool focus = true) + /// Form that handle the ToolManager + public ToolManager(Form owner) + { + _owner = owner; + } + + #endregion + + /// + /// Loads the tool dialog T (T must implemants ) , if it does not exist it will be created, if it is already open, it will be focused + /// This method should be used only if you can't use the generic one + /// + /// Type of tool you want to load + /// Define if the tool form has to get the focus or not (Default is true) + /// An instanciated + /// Raised if can't be casted into IToolForm + internal IToolForm Load(Type toolType, bool focus = true) { if (!typeof(IToolForm).IsAssignableFrom(toolType)) { throw new ArgumentException(string.Format("Type {0} does not implement IToolForm.", toolType.Name)); } + else + { + MethodInfo method = GetType().GetMethod("Load").MakeGenericMethod(toolType); + return (IToolForm)method.Invoke(this, new object[] { focus }); + } + } - if (!IsAvailable(toolType)) + /// + /// Loads the tool dialog T (T must implemants ) , if it does not exist it will be created, if it is already open, it will be focused + /// + /// Type of tool you want to load + /// Define if the tool form has to get the focus or not (Default is true) + /// An instanciated + public T Load(bool focus = true) + where T : class, IToolForm + { + return Load(string.Empty, focus); + } + + /// + /// Loads the tool dialog T (T must implemants ) , if it does not exist it will be created, if it is already open, it will be focused + /// + /// Type of tool you want to load + /// Define if the tool form has to get the focus or not (Default is true) + /// Path to the dll of the external tool + /// An instanciated + public T Load(string toolPath, bool focus = true) + where T : class, IToolForm + { + if (!IsAvailable()) { return null; } - var existingTool = _tools.FirstOrDefault(x => toolType.IsAssignableFrom(x.GetType())); + T existingTool = (T)_tools.FirstOrDefault(x => x is T); if (existingTool != null) { @@ -72,28 +108,29 @@ namespace BizHawk.Client.EmuHawk } } - var newTool = CreateInstance(toolType); + IToolForm newTool = CreateInstance(toolPath); - if (newTool == null) - { - return null; - } + if (newTool == null) + { + return null; + } - if (newTool is Form) + if (newTool is Form) { (newTool as Form).Owner = GlobalWin.MainForm; } ServiceInjector.UpdateServices(Global.Emulator.ServiceProvider, newTool); + string toolType = typeof(T).ToString(); // auto settings if (newTool is IToolFormAutoConfig) { ToolDialogSettings settings; - if (!Global.Config.CommonToolSettings.TryGetValue(toolType.ToString(), out settings)) + if (!Global.Config.CommonToolSettings.TryGetValue(toolType, out settings)) { settings = new ToolDialogSettings(); - Global.Config.CommonToolSettings[toolType.ToString()] = settings; + Global.Config.CommonToolSettings[toolType] = settings; } AttachSettingHooks(newTool as IToolFormAutoConfig, settings); } @@ -102,17 +139,17 @@ namespace BizHawk.Client.EmuHawk if (HasCustomConfig(newTool)) { Dictionary settings; - if (!Global.Config.CustomToolSettings.TryGetValue(toolType.ToString(), out settings)) + if (!Global.Config.CustomToolSettings.TryGetValue(toolType, out settings)) { settings = new Dictionary(); - Global.Config.CustomToolSettings[toolType.ToString()] = settings; + Global.Config.CustomToolSettings[toolType] = settings; } InstallCustomConfig(newTool, settings); } newTool.Restart(); newTool.Show(); - return newTool; + return (T)newTool; } public void AutoLoad() @@ -301,7 +338,7 @@ namespace BizHawk.Client.EmuHawk } } - + /// /// Determines whether a given IToolForm is already loaded /// @@ -327,7 +364,7 @@ namespace BizHawk.Client.EmuHawk /// /// Gets the instance of T, or creates and returns a new instance /// - public IToolForm Get() where T : IToolForm + public IToolForm Get() where T : class, IToolForm { return Load(false); } @@ -499,63 +536,73 @@ namespace BizHawk.Client.EmuHawk { _tools.ForEach(x => x.Close()); _tools.Clear(); - } + } - private IToolForm CreateInstance() - where T: IToolForm + /// + /// Create a new instance of an IToolForm and return it + /// + /// Type of tool you want to create + /// Path dll for an external tool + /// New instance of an IToolForm + private IToolForm CreateInstance(string dllPath) + where T : IToolForm { - return CreateInstance(typeof(T)); + return CreateInstance(typeof(T), dllPath); + } + + /// + /// Create a new instance of an IToolForm and return it + /// + /// Type of tool you want to create + /// Path dll for an external tool + /// New instance of an IToolForm + private IToolForm CreateInstance(Type toolType, string dllPath) + { + IToolForm tool; + + //Specific case for custom tools + //TODO: Use AppDomain in order to be able to unload the assembly + if (toolType == typeof(IExternalToolForm)) + { + if (MessageBox.Show(@"Are you sure want to load this external tool?\r\nAccept ONLY if you trust the source and if you know what you're doing. In any other case, choose no." + , "Confirmm loading", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + try + { + tool = Activator.CreateInstanceFrom(dllPath, "BizHawk.Client.EmuHawk.CustomMainForm").Unwrap() as IExternalToolForm; + if (tool == null) + { + MessageBox.Show("It seems that the object CustomMainForm does not implement IExternalToolForm. Please review the code.", "No, no, no. Wrong Way !", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); + return null; + } + } + catch (MissingMethodException) + { + MessageBox.Show("It seems that the object CustomMainForm does not have a public default constructor. Please review the code.", "No, no, no. Wrong Way !", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); + return null; + } + catch (TypeLoadException ex) + { + MessageBox.Show("It seems that the object CustomMainForm does not exists. Please review the code.", "No, no, no. Wrong Way !", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); + return null; + } + } + else + { + return null; + } + } + else + { + tool = (IToolForm)Activator.CreateInstance(toolType); + } + + // Add to our list of tools + _tools.Add(tool); + return tool; } - private IToolForm CreateInstance(Type toolType) - { - IToolForm tool; - - //Specific case for custom tools - if (toolType == typeof(IExternalToolForm)) - { - string path = Path.Combine(Global.Config.PathEntries["Global", "External Tools"].Path, string.Format("{0}.dll", Global.Game.Name)); - if (File.Exists(path) - && MessageBox.Show("A custom plugin has been found for the ROM you're loading. Do you want to load it?\r\nAccept ONLY if you trust the source and if you know what you're doing. In any other case, choose no." - , "Answer to life, universe and everything else?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) - { - try - { - // As the object is "remote"(external to the project), the CreateInstanceFrom returns a handle.We need to Unwrap in order to make the casting - tool = Activator.CreateInstanceFrom(path, "BizHawk.Client.EmuHawk.CustomMainForm").Unwrap() as IToolForm; - if (tool == null) - { - MessageBox.Show("It seems that the object CustomMainForm does not implement IToolForm. Please review the code.", "Boom!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); - return null; - } - } - catch (MissingMethodException) - { - MessageBox.Show("It seems that the object CustomMainForm does not have a public default constructor. Please review the code.", "Boom!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); - return null; - } - catch (TypeLoadException) - { - MessageBox.Show("It seems that the object CustomMainForm does not exists. Please review the code.", "Boom!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); - return null; - } - } - else - { - return null; - } - } - else - { - tool = (IToolForm)Activator.CreateInstance(toolType); - } - - // Add to our list of tools - _tools.Add(tool); - return tool; - } - - public void UpdateToolsBefore(bool fromLua = false) + public void UpdateToolsBefore(bool fromLua = false) { if (Has()) { @@ -694,7 +741,7 @@ namespace BizHawk.Client.EmuHawk return tool as RamWatch; } } - + var newTool = new RamWatch(); _tools.Add(newTool); return newTool; @@ -889,7 +936,7 @@ namespace BizHawk.Client.EmuHawk public static string GenerateDefaultCheatFilename() { var pathEntry = Global.Config.PathEntries[Global.Game.System, "Cheats"] - ?? Global.Config.PathEntries[Global.Game.System, "Base"]; + ?? Global.Config.PathEntries[Global.Game.System, "Base"]; var path = PathManager.MakeAbsolutePath(pathEntry.Path, Global.Game.System);