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:
parent
e7564f11ae
commit
a0d56df06b
|
@ -4,9 +4,11 @@ using System.ComponentModel;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
using LuaInterface;
|
using LuaInterface;
|
||||||
|
using BizHawk.Common;
|
||||||
using BizHawk.Emulation.Common;
|
using BizHawk.Emulation.Common;
|
||||||
using BizHawk.Client.Common;
|
using BizHawk.Client.Common;
|
||||||
|
|
||||||
|
|
||||||
namespace BizHawk.Client.EmuHawk
|
namespace BizHawk.Client.EmuHawk
|
||||||
{
|
{
|
||||||
[Description("A library for manipulating the EmuHawk client UI")]
|
[Description("A library for manipulating the EmuHawk client UI")]
|
||||||
|
@ -418,16 +420,16 @@ namespace BizHawk.Client.EmuHawk
|
||||||
}
|
}
|
||||||
|
|
||||||
[LuaMethodAttributes(
|
[LuaMethodAttributes(
|
||||||
"getopentools",
|
"getavailabletools",
|
||||||
"Returns a list of the tools currently open"
|
"Returns a list of the tools currently open"
|
||||||
)]
|
)]
|
||||||
public LuaTable GetOpenTools()
|
public LuaTable GetAvailableTools()
|
||||||
{
|
{
|
||||||
var t = Lua.NewTable();
|
var t = Lua.NewTable();
|
||||||
var tools = GlobalWin.Tools.AvailableTools.ToList();
|
var tools = GlobalWin.Tools.AvailableTools.ToList();
|
||||||
for (int i = 0; i < tools.Count; i++)
|
for (int i = 0; i < tools.Count; i++)
|
||||||
{
|
{
|
||||||
t[i] = tools[i].GetType().Name.ToLower();
|
t[i] = tools[i].Name.ToLower();
|
||||||
}
|
}
|
||||||
|
|
||||||
return t;
|
return t;
|
||||||
|
@ -435,10 +437,18 @@ namespace BizHawk.Client.EmuHawk
|
||||||
|
|
||||||
[LuaMethodAttributes(
|
[LuaMethodAttributes(
|
||||||
"gettool",
|
"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)
|
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
|
var selectedTool = GlobalWin.Tools.AvailableTools
|
||||||
.FirstOrDefault(tool => tool.GetType().Name.ToLower() == name.ToLower());
|
.FirstOrDefault(tool => tool.GetType().Name.ToLower() == name.ToLower());
|
||||||
|
|
||||||
|
|
|
@ -319,11 +319,17 @@ namespace BizHawk.Client.EmuHawk
|
||||||
return Load<T>(false);
|
return Load<T>(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<IToolForm> AvailableTools
|
public IEnumerable<Type> AvailableTools
|
||||||
{
|
{
|
||||||
get
|
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>()
|
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
|
// Eventually we want a single game genie tool, then this mess goes away
|
||||||
|
|
|
@ -4,6 +4,7 @@ using System.IO;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
using BizHawk.Common.BufferExtensions;
|
using BizHawk.Common.BufferExtensions;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
namespace BizHawk.Common
|
namespace BizHawk.Common
|
||||||
{
|
{
|
||||||
|
@ -449,4 +450,32 @@ namespace BizHawk.Common
|
||||||
return StaticPart + "-" + myctr;
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue