From 6401e6d7192ea62f14c1a5837910c806b3c56ec8 Mon Sep 17 00:00:00 2001 From: adelikat Date: Tue, 20 May 2014 20:34:51 +0000 Subject: [PATCH] Refactor LuaLibraryBase to have a Lua context rather than misc libraries being in charge of that when needed. Still todo: clean up the inconsistent constructor logic, vs setting these as public properties --- BizHawk.Client.Common/lua/EmuLuaLibrary.Emu.cs | 5 ++--- BizHawk.Client.Common/lua/EmuLuaLibrary.GameInfo.cs | 6 ++---- BizHawk.Client.Common/lua/EmuLuaLibrary.Joypad.cs | 8 +++----- BizHawk.Client.Common/lua/EmuLuaLibrary.MainMemory.cs | 6 ++---- BizHawk.Client.Common/lua/EmuLuaLibrary.Memory.cs | 7 +++---- BizHawk.Client.Common/lua/EmuLuaLibrary.Movie.cs | 6 ++---- BizHawk.Client.Common/lua/EmuLuaLibrary.String.cs | 6 ++---- BizHawk.Client.Common/lua/LuaLibraryBase.cs | 1 + .../tools/Lua/Libraries/EmuLuaLibrary.Input.cs | 8 +++----- 9 files changed, 20 insertions(+), 33 deletions(-) diff --git a/BizHawk.Client.Common/lua/EmuLuaLibrary.Emu.cs b/BizHawk.Client.Common/lua/EmuLuaLibrary.Emu.cs index 33bb4aafc3..9b8bf80cb8 100644 --- a/BizHawk.Client.Common/lua/EmuLuaLibrary.Emu.cs +++ b/BizHawk.Client.Common/lua/EmuLuaLibrary.Emu.cs @@ -13,13 +13,12 @@ namespace BizHawk.Client.Common { public class EmulatorLuaLibrary : LuaLibraryBase { - private readonly Lua _lua; private readonly Action _frameAdvanceCallback; private readonly Action _yieldCallback; public EmulatorLuaLibrary(Lua lua, Action frameAdvanceCallback, Action yieldCallback) { - _lua = lua; + Lua = lua; _frameAdvanceCallback = frameAdvanceCallback; _yieldCallback = yieldCallback; } @@ -68,7 +67,7 @@ namespace BizHawk.Client.Common )] public LuaTable GetRegisters() { - var table = _lua.NewTable(); + var table = Lua.NewTable(); foreach (var kvp in Global.Emulator.GetCpuFlagsAndRegisters()) { table[kvp.Key] = kvp.Value; diff --git a/BizHawk.Client.Common/lua/EmuLuaLibrary.GameInfo.cs b/BizHawk.Client.Common/lua/EmuLuaLibrary.GameInfo.cs index 8253e92249..c394660733 100644 --- a/BizHawk.Client.Common/lua/EmuLuaLibrary.GameInfo.cs +++ b/BizHawk.Client.Common/lua/EmuLuaLibrary.GameInfo.cs @@ -7,13 +7,11 @@ namespace BizHawk.Client.Common { public GameInfoLuaLibrary(Lua lua) { - _lua = lua; + Lua = lua; } public override string Name { get { return "gameinfo"; } } - private readonly Lua _lua; - [LuaMethodAttributes( "getromname", "returns the path of the currently loaded rom, if a rom is loaded" @@ -99,7 +97,7 @@ namespace BizHawk.Client.Common )] public LuaTable GetOptions() { - var options = _lua.NewTable(); + var options = Lua.NewTable(); if (Global.Game != null) { diff --git a/BizHawk.Client.Common/lua/EmuLuaLibrary.Joypad.cs b/BizHawk.Client.Common/lua/EmuLuaLibrary.Joypad.cs index 6249b486a5..5174f0c131 100644 --- a/BizHawk.Client.Common/lua/EmuLuaLibrary.Joypad.cs +++ b/BizHawk.Client.Common/lua/EmuLuaLibrary.Joypad.cs @@ -7,20 +7,18 @@ namespace BizHawk.Client.Common { public JoypadLuaLibrary(Lua lua) { - _lua = lua; + Lua = lua; } public override string Name { get { return "joypad"; } } - private readonly Lua _lua; - [LuaMethodAttributes( "get", "returns a lua table of the controller buttons pressed. If supplied, it will only return a table of buttons for the given controller" )] public LuaTable Get(int? controller = null) { - var buttons = _lua.NewTable(); + var buttons = Lua.NewTable(); foreach (var button in Global.ControllerOutput.Source.Type.BoolButtons) { if (!controller.HasValue) @@ -58,7 +56,7 @@ namespace BizHawk.Client.Common )] public LuaTable GetImmediate() { - var buttons = _lua.NewTable(); + var buttons = Lua.NewTable(); foreach (var button in Global.ActiveController.Type.BoolButtons) { buttons[button] = Global.ActiveController[button]; diff --git a/BizHawk.Client.Common/lua/EmuLuaLibrary.MainMemory.cs b/BizHawk.Client.Common/lua/EmuLuaLibrary.MainMemory.cs index e92fdac4bd..bd1b122202 100644 --- a/BizHawk.Client.Common/lua/EmuLuaLibrary.MainMemory.cs +++ b/BizHawk.Client.Common/lua/EmuLuaLibrary.MainMemory.cs @@ -7,11 +7,9 @@ namespace BizHawk.Client.Common // TODO: this needs a major refactor, as well as MemoryLuaLibrary, and this shoudl inherit memorylua library and extend it public class MainMemoryLuaLibrary : LuaLibraryBase { - private readonly Lua _lua; - public MainMemoryLuaLibrary(Lua lua) { - _lua = lua; + Lua = lua; } public override string Name { get { return "mainmemory"; } } @@ -120,7 +118,7 @@ namespace BizHawk.Client.Common public LuaTable ReadByteRange(int addr, int length) { var lastAddr = length + addr; - var table = _lua.NewTable(); + var table = Lua.NewTable(); if (lastAddr < Global.Emulator.MemoryDomains.MainMemory.Size) { for (var i = addr; i <= lastAddr; i++) diff --git a/BizHawk.Client.Common/lua/EmuLuaLibrary.Memory.cs b/BizHawk.Client.Common/lua/EmuLuaLibrary.Memory.cs index dd86928980..db60cf484a 100644 --- a/BizHawk.Client.Common/lua/EmuLuaLibrary.Memory.cs +++ b/BizHawk.Client.Common/lua/EmuLuaLibrary.Memory.cs @@ -7,12 +7,11 @@ namespace BizHawk.Client.Common { public class MemoryLuaLibrary : LuaLibraryBase { - private readonly Lua _lua; private int _currentMemoryDomain; // Main memory by default public MemoryLuaLibrary(Lua lua) { - _lua = lua; + Lua = lua; } public override string Name { get { return "memory"; } } @@ -103,7 +102,7 @@ namespace BizHawk.Client.Common )] public LuaTable GetMemoryDomainList() { - var table = _lua.NewTable(); + var table = Lua.NewTable(); for (int i = 0; i < Global.Emulator.MemoryDomains.Count; i++) { table[i] = Global.Emulator.MemoryDomains[i].Name; @@ -119,7 +118,7 @@ namespace BizHawk.Client.Common public LuaTable ReadByteRange(int addr, int length) { var lastAddr = length + addr; - var table = _lua.NewTable(); + var table = Lua.NewTable(); if (lastAddr < Global.Emulator.MemoryDomains[_currentMemoryDomain].Size) { diff --git a/BizHawk.Client.Common/lua/EmuLuaLibrary.Movie.cs b/BizHawk.Client.Common/lua/EmuLuaLibrary.Movie.cs index cf40f00d8b..8bd8cc1f5a 100644 --- a/BizHawk.Client.Common/lua/EmuLuaLibrary.Movie.cs +++ b/BizHawk.Client.Common/lua/EmuLuaLibrary.Movie.cs @@ -4,11 +4,9 @@ namespace BizHawk.Client.Common { public class MovieLuaLibrary : LuaLibraryBase { - private readonly Lua _lua; - public MovieLuaLibrary(Lua lua) { - _lua = lua; + Lua = lua; } public override string Name { get { return "movie"; } } @@ -28,7 +26,7 @@ namespace BizHawk.Client.Common )] public LuaTable GetInput(int frame) { - var input = _lua.NewTable(); + var input = Lua.NewTable(); var m = new MovieControllerAdapter { Type = Global.MovieSession.MovieControllerAdapter.Type }; m.SetControllersAsMnemonic( diff --git a/BizHawk.Client.Common/lua/EmuLuaLibrary.String.cs b/BizHawk.Client.Common/lua/EmuLuaLibrary.String.cs index 15f5cf31c5..a81a050fa2 100644 --- a/BizHawk.Client.Common/lua/EmuLuaLibrary.String.cs +++ b/BizHawk.Client.Common/lua/EmuLuaLibrary.String.cs @@ -7,13 +7,11 @@ namespace BizHawk.Client.Common { public class StringLuaLibrary : LuaLibraryBase { - private readonly Lua _lua; - public override string Name { get { return "bizstring"; } } public StringLuaLibrary(Lua lua) { - _lua = lua; + Lua = lua; } [LuaMethodAttributes( @@ -144,7 +142,7 @@ namespace BizHawk.Client.Common )] public LuaTable Split(string str, string separator) { - var table = _lua.NewTable(); + var table = Lua.NewTable(); var splitStr = str.Split( new char[] { separator.FirstOrDefault() }, StringSplitOptions.RemoveEmptyEntries); diff --git a/BizHawk.Client.Common/lua/LuaLibraryBase.cs b/BizHawk.Client.Common/lua/LuaLibraryBase.cs index fb5d5b09a6..d32d4b4dbc 100644 --- a/BizHawk.Client.Common/lua/LuaLibraryBase.cs +++ b/BizHawk.Client.Common/lua/LuaLibraryBase.cs @@ -9,6 +9,7 @@ namespace BizHawk.Client.Common { public abstract string Name { get; } public Action LogOutputCallback { get; set; } + public Lua Lua { get; set; } protected void Log(object message) { diff --git a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Input.cs b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Input.cs index ab9080580c..2bc024daf1 100644 --- a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Input.cs +++ b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Input.cs @@ -10,20 +10,18 @@ namespace BizHawk.Client.EmuHawk { public InputLuaLibrary(Lua lua) { - _lua = lua; + Lua = lua; } public override string Name { get { return "input"; } } - private readonly Lua _lua; - [LuaMethodAttributes( "get", "Returns a lua table of all the buttons the user is currently pressing on their keyboard and gamepads" )] public LuaTable Get() { - var buttons = _lua.NewTable(); + var buttons = Lua.NewTable(); foreach (var kvp in Global.ControllerInputCoalescer.BoolButtons().Where(kvp => kvp.Value)) { buttons[kvp.Key] = true; @@ -38,7 +36,7 @@ namespace BizHawk.Client.EmuHawk )] public LuaTable GetMouse() { - var buttons = _lua.NewTable(); + var buttons = Lua.NewTable(); //TODO - need to specify whether in "emu" or "native" coordinate space. var p = GlobalWin.DisplayManager.UntransformPoint(Control.MousePosition); buttons["X"] = p.X;