From 3f8aa0e018bb98603613f05fbafd582fdafdab7f Mon Sep 17 00:00:00 2001 From: adelikat Date: Sat, 5 Aug 2017 11:04:00 -0500 Subject: [PATCH] Lua - implement tastudio.getbranches() and tastudio.getbranchinput(), fixes #843 --- .../Lua/Libraries/EmuLuaLibrary.Tastudio.cs | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Tastudio.cs b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Tastudio.cs index a1c7ad4194..831cc599e8 100644 --- a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Tastudio.cs +++ b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Tastudio.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Linq; @@ -289,5 +290,74 @@ namespace BizHawk.Client.EmuHawk } } } + + public class TastudioBranchInfo + { + public string Id { get; set; } + public int Frame { get; set; } + public string Text { get; set; } + } + + [LuaMethod("getbranches", "Returns a list of the current tastudio branches. Each entry will have the Id, Frame, and Text properties of the branch")] + public LuaTable GetBranches() + { + var table = Lua.NewTable(); + + if (Engaged()) + { + var branches = Tastudio.CurrentTasMovie.Branches.Select(b => new + { + Id = b.UniqueIdentifier.ToString(), + Frame = b.Frame, + Text = b.UserText + }) + .ToList(); + + for (int i = 0; i < branches.Count; i++) + { + table[i] = branches[i]; + } + } + + return table; + } + + + [LuaMethod("getbranchinput", "Gets the controller state of the given frame with the given branch identifier")] + public LuaTable GetBranchInput(string branchId, int frame) + { + var table = Lua.NewTable(); + + if (Engaged()) + { + if (Tastudio.CurrentTasMovie.Branches.Any(b => b.UniqueIdentifier.ToString() == branchId)) + { + var branch = Tastudio.CurrentTasMovie.Branches.First(b => b.UniqueIdentifier.ToString() == branchId); + if (frame < branch.InputLog.Count) + { + var input = branch.InputLog[frame]; + + var adapter = new Bk2ControllerAdapter + { + Definition = Global.MovieSession.MovieControllerAdapter.Definition + }; + + adapter.SetControllersAsMnemonic(input); + + foreach (var button in adapter.Definition.BoolButtons) + { + table[button] = adapter.IsPressed(button); + } + + foreach (var button in adapter.Definition.FloatControls) + { + table[button] = adapter.GetFloat(button); + } + } + } + } + + return table; + } } }