Lua - client library - change GetOpenTools() to GetAvailableTools() which returns a list of tools available to the currently loaded core, change GetTool() to receive any avaialble tool, if the tool isn't open, GetTool() opens it

This commit is contained in:
adelikat 2015-04-12 17:37:06 +00:00
parent e7564f11ae
commit a0d56df06b
3 changed files with 57 additions and 7 deletions

View File

@ -4,9 +4,11 @@ using System.ComponentModel;
using System.Linq;
using LuaInterface;
using BizHawk.Common;
using BizHawk.Emulation.Common;
using BizHawk.Client.Common;
namespace BizHawk.Client.EmuHawk
{
[Description("A library for manipulating the EmuHawk client UI")]
@ -418,16 +420,16 @@ namespace BizHawk.Client.EmuHawk
}
[LuaMethodAttributes(
"getopentools",
"getavailabletools",
"Returns a list of the tools currently open"
)]
public LuaTable GetOpenTools()
public LuaTable GetAvailableTools()
{
var t = Lua.NewTable();
var tools = GlobalWin.Tools.AvailableTools.ToList();
for (int i = 0; i < tools.Count; i++)
{
t[i] = tools[i].GetType().Name.ToLower();
t[i] = tools[i].Name.ToLower();
}
return t;
@ -435,10 +437,18 @@ namespace BizHawk.Client.EmuHawk
[LuaMethodAttributes(
"gettool",
"Returns an object that represents a currently open tool of the given name. Use getopentools to get a list of names"
"Returns an object that represents a tool of the given name (not case sensitive). If the tool is not open, it will be loaded if available. Use gettools to get a list of names"
)]
public LuaTable GetTool(string name)
{
var toolType = ReflectionUtil.GetTypeByName(name)
.FirstOrDefault(x => typeof(IToolForm).IsAssignableFrom(x) && !x.IsInterface);
if (toolType != null)
{
GlobalWin.Tools.Load(toolType);
}
var selectedTool = GlobalWin.Tools.AvailableTools
.FirstOrDefault(tool => tool.GetType().Name.ToLower() == name.ToLower());

View File

@ -319,11 +319,17 @@ namespace BizHawk.Client.EmuHawk
return Load<T>(false);
}
public IEnumerable<IToolForm> AvailableTools
public IEnumerable<Type> AvailableTools
{
get
{
return _tools.Where(t => !t.IsDisposed);
//return _tools.Where(t => !t.IsDisposed);
return Assembly
.GetAssembly(typeof(IToolForm))
.GetTypes()
.Where(t => typeof(IToolForm).IsAssignableFrom(t))
.Where(t => !t.IsInterface)
.Where(t => IsAvailable(t));
}
}
@ -556,7 +562,12 @@ namespace BizHawk.Client.EmuHawk
public bool IsAvailable<T>()
{
return ServiceInjector.IsAvailable(Global.Emulator.ServiceProvider, typeof(T));
return IsAvailable(typeof(T));
}
public bool IsAvailable(Type t)
{
return ServiceInjector.IsAvailable(Global.Emulator.ServiceProvider, t);
}
// Eventually we want a single game genie tool, then this mess goes away

View File

@ -4,6 +4,7 @@ using System.IO;
using System.Text;
using BizHawk.Common.BufferExtensions;
using System.Reflection;
namespace BizHawk.Common
{
@ -449,4 +450,32 @@ namespace BizHawk.Common
return StaticPart + "-" + myctr;
}
}
public static class ReflectionUtil
{
// http://stackoverflow.com/questions/9273629/avoid-giving-namespace-name-in-type-gettype
/// <summary>
/// Gets a all Type instances matching the specified class name with just non-namespace qualified class name.
/// </summary>
/// <param name="className">Name of the class sought.</param>
/// <returns>Types that have the class name specified. They may not be in the same namespace.</returns>
public static Type[] GetTypeByName(string className)
{
var returnVal = new List<Type>();
foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
{
Type[] assemblyTypes = a.GetTypes();
for (int j = 0; j < assemblyTypes.Length; j++)
{
if (assemblyTypes[j].Name.ToLower() == className.ToLower())
{
returnVal.Add(assemblyTypes[j]);
}
}
}
return returnVal.ToArray();
}
}
}