From 8d86ee012ecf8a0ea84990a930bbac27910a8855 Mon Sep 17 00:00:00 2001 From: adelikat Date: Fri, 10 Apr 2015 21:56:03 +0000 Subject: [PATCH] Lua - implement client.GetOpenTools() to return a list of currently open tools. And client.GetTool(string name) which return an object to lua representing a currently loaded tool, lua then has access to any public methods of that object --- .../Lua/Libraries/EmuLuaLibrary.Client.cs | 34 +++++++++++++++++++ BizHawk.Client.EmuHawk/tools/ToolManager.cs | 8 +++++ 2 files changed, 42 insertions(+) diff --git a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Client.cs b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Client.cs index 4a83c02dde..4bac828991 100644 --- a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Client.cs +++ b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Client.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.ComponentModel; +using System.Linq; using LuaInterface; using BizHawk.Emulation.Common; @@ -415,5 +416,38 @@ namespace BizHawk.Client.EmuHawk { return GlobalWin.MainForm.DesktopLocation.Y; } + + [LuaMethodAttributes( + "getopentools", + "Returns a list of the tools currently open" + )] + public LuaTable GetOpenTools() + { + 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(); + } + + return t; + } + + [LuaMethodAttributes( + "gettool", + "Returns an object that represents a currently open tool of the given name. Use getopentools to get a list of names" + )] + public LuaTable GetTool(string name) + { + var selectedTool = GlobalWin.Tools.AvailableTools + .FirstOrDefault(tool => tool.GetType().Name.ToLower() == name.ToLower()); + + if (selectedTool != null) + { + return LuaHelper.ToLuaTable(Lua, selectedTool); + } + + return null; + } } } diff --git a/BizHawk.Client.EmuHawk/tools/ToolManager.cs b/BizHawk.Client.EmuHawk/tools/ToolManager.cs index 7834fd95ee..0800d3c133 100644 --- a/BizHawk.Client.EmuHawk/tools/ToolManager.cs +++ b/BizHawk.Client.EmuHawk/tools/ToolManager.cs @@ -319,6 +319,14 @@ namespace BizHawk.Client.EmuHawk return Load(false); } + public IEnumerable AvailableTools + { + get + { + return _tools.Where(t => !t.IsDisposed); + } + } + public void UpdateBefore() { var beforeList = _tools.Where(x => x.UpdateBefore);