Debugging and POC

Change some logic into ToolManager for better practice
Adjust the look of toolstrip items
Update comments
This commit is contained in:
Hathor86 2015-11-22 17:37:00 +01:00
parent d92eed5845
commit 7d2e02b94b
6 changed files with 185 additions and 114 deletions

2
.gitignore vendored
View File

@ -246,7 +246,7 @@
/References/*.xml
/output/ELFSharp.dll
/output/dll/ELFSharp.dll
/output/ExternalTools/*.dll
/output/ExternalTools/*.*
*.opensdf
*.user
*.suo

View File

@ -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;

View File

@ -1250,7 +1250,7 @@ namespace BizHawk.Client.EmuHawk
AutoHawkMenuItem.Visible = VersionInfo.DeveloperBuild;
BasicBotMenuItem.Enabled = GlobalWin.Tools.IsAvailable<BasicBot>();
gameSharkConverterToolStripMenuItem.Enabled = GlobalWin.Tools.IsAvailable<GameShark>();
}
@ -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<Type>(t => t != null && t.FullName.Contains("BizHawk.Client.EmuHawk.CustomMainForm")).ToArray<Type>();
}
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<IExternalToolForm>(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)
{

View File

@ -1,14 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace BizHawk.Client.EmuHawk
{
/// <summary>
/// Interface to implements in order to make a custom tool for a specific game
/// Interface to implements in order to make a custom tool
/// </summary>
public interface IExternalToolForm :IToolForm
public interface IExternalToolForm : IToolForm
{
/// <summary>
/// <see cref="FormClosedEventHandler"/>
/// </summary>
event FormClosedEventHandler FormClosed;
}
}

View File

@ -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
{

View File

@ -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<IToolForm> _tools = new List<IToolForm>();
/// <summary>
/// Loads the tool dialog T, if it does not exist it will be created, if it is already open, it will be focused
/// </summary>
public T Load<T>(bool focus = true) where T : IToolForm
{
return (T)Load(typeof(T), focus);
}
#endregion
#region cTor(s)
/// <summary>
/// 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
/// </summary>
public IToolForm Load(Type toolType, bool focus = true)
/// <param name="owner">Form that handle the ToolManager</param>
public ToolManager(Form owner)
{
_owner = owner;
}
#endregion
/// <summary>
/// Loads the tool dialog T (T must implemants <see cref="IToolForm"/>) , 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
/// </summary>
/// <param name="toolType">Type of tool you want to load</param>
/// <param name="focus">Define if the tool form has to get the focus or not (Default is true)</param>
/// <returns>An instanciated <see cref="IToolForm"/></returns>
/// <exception cref="ArgumentException">Raised if <paramref name="toolType"/> can't be casted into IToolForm </exception>
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))
/// <summary>
/// Loads the tool dialog T (T must implemants <see cref="IToolForm"/>) , if it does not exist it will be created, if it is already open, it will be focused
/// </summary>
/// <typeparam name="T">Type of tool you want to load</typeparam>
/// <param name="focus">Define if the tool form has to get the focus or not (Default is true)</param>
/// <returns>An instanciated <see cref="IToolForm"/></returns>
public T Load<T>(bool focus = true)
where T : class, IToolForm
{
return Load<T>(string.Empty, focus);
}
/// <summary>
/// Loads the tool dialog T (T must implemants <see cref="IToolForm"/>) , if it does not exist it will be created, if it is already open, it will be focused
/// </summary>
/// <typeparam name="T">Type of tool you want to load</typeparam>
/// <param name="focus">Define if the tool form has to get the focus or not (Default is true)</param>
/// <param name="toolPath">Path to the dll of the external tool</param>
/// <returns>An instanciated <see cref="IToolForm"/></returns>
public T Load<T>(string toolPath, bool focus = true)
where T : class, IToolForm
{
if (!IsAvailable<T>())
{
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<T>(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<string, object> settings;
if (!Global.Config.CustomToolSettings.TryGetValue(toolType.ToString(), out settings))
if (!Global.Config.CustomToolSettings.TryGetValue(toolType, out settings))
{
settings = new Dictionary<string, object>();
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
}
}
/// <summary>
/// Determines whether a given IToolForm is already loaded
/// </summary>
@ -327,7 +364,7 @@ namespace BizHawk.Client.EmuHawk
/// <summary>
/// Gets the instance of T, or creates and returns a new instance
/// </summary>
public IToolForm Get<T>() where T : IToolForm
public IToolForm Get<T>() where T : class, IToolForm
{
return Load<T>(false);
}
@ -499,63 +536,73 @@ namespace BizHawk.Client.EmuHawk
{
_tools.ForEach(x => x.Close());
_tools.Clear();
}
}
private IToolForm CreateInstance<T>()
where T: IToolForm
/// <summary>
/// Create a new instance of an IToolForm and return it
/// </summary>
/// <typeparam name="T">Type of tool you want to create</typeparam>
/// <param name="dllPath">Path dll for an external tool</param>
/// <returns>New instance of an IToolForm</returns>
private IToolForm CreateInstance<T>(string dllPath)
where T : IToolForm
{
return CreateInstance(typeof(T));
return CreateInstance(typeof(T), dllPath);
}
/// <summary>
/// Create a new instance of an IToolForm and return it
/// </summary>
/// <param name="toolType">Type of tool you want to create</param>
/// <param name="dllPath">Path dll for an external tool</param>
/// <returns>New instance of an IToolForm</returns>
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<LuaConsole>())
{
@ -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);