From 7de9309be13dec79886c9dba63799ab43ba78f46 Mon Sep 17 00:00:00 2001 From: adelikat Date: Tue, 31 Dec 2019 16:46:05 -0600 Subject: [PATCH] Lua - pass in MainForm to the EmuHawkLuaLibrary class --- .../Lua/Libraries/EmuLuaLibrary.Client.cs | 108 +++++++++--------- .../tools/Lua/Libraries/EmuLuaLibrary.cs | 12 +- .../tools/Lua/LuaConsole.cs | 2 +- 3 files changed, 67 insertions(+), 55 deletions(-) diff --git a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Client.cs b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Client.cs index 48894b3672..1d16807f51 100644 --- a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Client.cs +++ b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Client.cs @@ -21,6 +21,8 @@ namespace BizHawk.Client.EmuHawk [RequiredService] private IVideoProvider VideoProvider { get; set; } + public MainForm MainForm { get; set; } + public EmuHawkLuaLibrary(Lua lua) : base(lua) { } @@ -33,14 +35,14 @@ namespace BizHawk.Client.EmuHawk [LuaMethod("exit", "Closes the emulator")] public void CloseEmulator() { - GlobalWin.MainForm.CloseEmulator(); + MainForm.CloseEmulator(); } [LuaMethodExample("client.exitCode( 0 );")] [LuaMethod("exitCode", "Closes the emulator and returns the provided code")] public void CloseEmulatorWithCode(int exitCode) { - GlobalWin.MainForm.CloseEmulator(exitCode); + MainForm.CloseEmulator(exitCode); } [LuaMethodExample("local inclibor = client.borderheight( );")] @@ -77,21 +79,21 @@ namespace BizHawk.Client.EmuHawk [LuaMethod("clearautohold", "Clears all autohold keys")] public void ClearAutohold() { - GlobalWin.MainForm.ClearHolds(); + MainForm.ClearHolds(); } [LuaMethodExample("client.closerom( );")] [LuaMethod("closerom", "Closes the loaded Rom")] - public static void CloseRom() + public void CloseRom() { - GlobalWin.MainForm.CloseRom(); + MainForm.CloseRom(); } [LuaMethodExample("client.enablerewind( true );")] [LuaMethod("enablerewind", "Sets whether or not the rewind feature is enabled")] public void EnableRewind(bool enabled) { - GlobalWin.MainForm.EnableRewind(enabled); + MainForm.EnableRewind(enabled); } [LuaMethodExample("client.frameskip( 8 );")] @@ -101,7 +103,7 @@ namespace BizHawk.Client.EmuHawk if (numFrames >= 0) { Global.Config.FrameSkip = numFrames; - GlobalWin.MainForm.FrameSkipMessage(); + MainForm.FrameSkipMessage(); } else { @@ -117,7 +119,7 @@ namespace BizHawk.Client.EmuHawk [LuaMethod("invisibleemulation", "Disables and enables emulator updates")] public void InvisibleEmulation(bool invisible) { - GlobalWin.MainForm.InvisibleEmulation = invisible; + MainForm.InvisibleEmulation = invisible; } /// @@ -128,20 +130,20 @@ namespace BizHawk.Client.EmuHawk [LuaMethod("seekframe", "Makes the emulator seek to the frame specified")] public void SeekFrame(int frame) { - bool wasPaused = GlobalWin.MainForm.EmulatorPaused; + bool wasPaused = MainForm.EmulatorPaused; // can't re-enter lua while doing this - GlobalWin.MainForm.SuppressLua = true; + MainForm.SuppressLua = true; while (Emulator.Frame != frame) { - GlobalWin.MainForm.SeekFrameAdvance(); + MainForm.SeekFrameAdvance(); } - GlobalWin.MainForm.SuppressLua = false; + MainForm.SuppressLua = false; if (!wasPaused) { - GlobalWin.MainForm.UnpauseEmulator(); + MainForm.UnpauseEmulator(); } } @@ -161,10 +163,10 @@ namespace BizHawk.Client.EmuHawk [LuaMethodExample("client.SetGameExtraPadding( 5, 10, 15, 20 );")] [LuaMethod("SetGameExtraPadding", "Sets the extra padding added to the 'emu' surface so that you can draw HUD elements in predictable placements")] - public static void SetGameExtraPadding(int left, int top, int right, int bottom) + public void SetGameExtraPadding(int left, int top, int right, int bottom) { GlobalWin.DisplayManager.GameExtraPadding = new System.Windows.Forms.Padding(left, top, right, bottom); - GlobalWin.MainForm.FrameBufferResized(); + MainForm.FrameBufferResized(); } [LuaMethodExample("client.SetSoundOn( true );")] @@ -185,31 +187,31 @@ namespace BizHawk.Client.EmuHawk [LuaMethodExample("client.SetClientExtraPadding( 5, 10, 15, 20 );")] [LuaMethod("SetClientExtraPadding", "Sets the extra padding added to the 'native' surface so that you can draw HUD elements in predictable placements")] - public static void SetClientExtraPadding(int left, int top, int right, int bottom) + public void SetClientExtraPadding(int left, int top, int right, int bottom) { GlobalWin.DisplayManager.ClientExtraPadding = new System.Windows.Forms.Padding(left, top, right, bottom); - GlobalWin.MainForm.FrameBufferResized(); + MainForm.FrameBufferResized(); } [LuaMethodExample("if ( client.ispaused( ) ) then\r\n\tconsole.log( \"Returns true if emulator is paused, otherwise, false\" );\r\nend;")] [LuaMethod("ispaused", "Returns true if emulator is paused, otherwise, false")] - public static bool IsPaused() + public bool IsPaused() { - return GlobalWin.MainForm.EmulatorPaused; + return MainForm.EmulatorPaused; } [LuaMethodExample("if ( client.client.isturbo( ) ) then\r\n\tconsole.log( \"Returns true if emulator is in turbo mode, otherwise, false\" );\r\nend;")] [LuaMethod("isturbo", "Returns true if emulator is in turbo mode, otherwise, false")] - public static bool IsTurbo() + public bool IsTurbo() { - return GlobalWin.MainForm.IsTurboing; + return MainForm.IsTurboing; } [LuaMethodExample("if ( client.isseeking( ) ) then\r\n\tconsole.log( \"Returns true if emulator is seeking, otherwise, false\" );\r\nend;")] [LuaMethod("isseeking", "Returns true if emulator is seeking, otherwise, false")] - public static bool IsSeeking() + public bool IsSeeking() { - return GlobalWin.MainForm.IsSeeking; + return MainForm.IsSeeking; } [LuaMethodExample("client.opencheats( );")] @@ -230,10 +232,10 @@ namespace BizHawk.Client.EmuHawk [LuaMethodExample("client.openrom( \"C:\\\" );")] [LuaMethod("openrom", "opens the Open ROM dialog")] - public static void OpenRom(string path) + public void OpenRom(string path) { var ioa = OpenAdvancedSerializer.ParseWithLegacy(path); - GlobalWin.MainForm.LoadRom(path, new MainForm.LoadRomArgs { OpenAdvanced = ioa }); + MainForm.LoadRom(path, new MainForm.LoadRomArgs { OpenAdvanced = ioa }); } [LuaMethodExample("client.opentasstudio( );")] @@ -250,53 +252,53 @@ namespace BizHawk.Client.EmuHawk [LuaMethodExample("client.pause( );")] [LuaMethod("pause", "Pauses the emulator")] - public static void Pause() + public void Pause() { - GlobalWin.MainForm.PauseEmulator(); + MainForm.PauseEmulator(); } [LuaMethodExample("client.pause_av( );")] [LuaMethod("pause_av", "If currently capturing Audio/Video, this will suspend the record. Frames will not be captured into the AV until client.unpause_av() is called")] - public static void PauseAv() + public void PauseAv() { - GlobalWin.MainForm.PauseAvi = true; + MainForm.PauseAvi = true; } [LuaMethodExample("client.reboot_core( );")] [LuaMethod("reboot_core", "Reboots the currently loaded core")] - public static void RebootCore() + public void RebootCore() { ((LuaConsole)GlobalWin.Tools.Get()).LuaImp.IsRebootingCore = true; - GlobalWin.MainForm.RebootCore(); + MainForm.RebootCore(); ((LuaConsole)GlobalWin.Tools.Get()).LuaImp.IsRebootingCore = false; } [LuaMethodExample("local incliscr = client.screenheight( );")] [LuaMethod("screenheight", "Gets the current height in pixels of the emulator's drawing area")] - public static int ScreenHeight() + public int ScreenHeight() { - return GlobalWin.MainForm.PresentationPanel.NativeSize.Height; + return MainForm.PresentationPanel.NativeSize.Height; } [LuaMethodExample("client.screenshot( \"C:\\\" );")] [LuaMethod("screenshot", "if a parameter is passed it will function as the Screenshot As menu item of EmuHawk, else it will function as the Screenshot menu item")] - public static void Screenshot(string path = null) + public void Screenshot(string path = null) { if (path == null) { - GlobalWin.MainForm.TakeScreenshot(); + MainForm.TakeScreenshot(); } else { - GlobalWin.MainForm.TakeScreenshot(path); + MainForm.TakeScreenshot(path); } } [LuaMethodExample("client.screenshottoclipboard( );")] [LuaMethod("screenshottoclipboard", "Performs the same function as EmuHawk's Screenshot To Clipboard menu item")] - public static void ScreenshotToClipboard() + public void ScreenshotToClipboard() { - GlobalWin.MainForm.TakeScreenshotToClipboard(); + MainForm.TakeScreenshotToClipboard(); } [LuaMethodExample("client.settargetscanlineintensity( -1000 );")] @@ -315,9 +317,9 @@ namespace BizHawk.Client.EmuHawk [LuaMethodExample("local incliscr = client.screenwidth( );")] [LuaMethod("screenwidth", "Gets the current width in pixels of the emulator's drawing area")] - public static int ScreenWidth() + public int ScreenWidth() { - return GlobalWin.MainForm.PresentationPanel.NativeSize.Width; + return MainForm.PresentationPanel.NativeSize.Width; } [LuaMethodExample("client.setwindowsize( 100 );")] @@ -327,7 +329,7 @@ namespace BizHawk.Client.EmuHawk if (size == 1 || size == 2 || size == 3 || size == 4 || size == 5 || size == 10) { Global.Config.TargetZoomFactors[Emulator.SystemId] = size; - GlobalWin.MainForm.FrameBufferResized(); + MainForm.FrameBufferResized(); GlobalWin.OSD.AddMessage($"Window size set to {size}x"); } else @@ -342,7 +344,7 @@ namespace BizHawk.Client.EmuHawk { if (percent > 0 && percent < 6400) { - GlobalWin.MainForm.ClickSpeedItem(percent); + MainForm.ClickSpeedItem(percent); } else { @@ -359,9 +361,9 @@ namespace BizHawk.Client.EmuHawk [LuaMethodExample("client.togglepause( );")] [LuaMethod("togglepause", "Toggles the current pause state")] - public static void TogglePause() + public void TogglePause() { - GlobalWin.MainForm.TogglePause(); + MainForm.TogglePause(); } [LuaMethodExample("local inclitra = client.transformPointX( 16 );")] @@ -382,30 +384,30 @@ namespace BizHawk.Client.EmuHawk [LuaMethodExample("client.unpause( );")] [LuaMethod("unpause", "Unpauses the emulator")] - public static void Unpause() + public void Unpause() { - GlobalWin.MainForm.UnpauseEmulator(); + MainForm.UnpauseEmulator(); } [LuaMethodExample("client.unpause_av( );")] [LuaMethod("unpause_av", "If currently capturing Audio/Video this resumes capturing")] - public static void UnpauseAv() + public void UnpauseAv() { - GlobalWin.MainForm.PauseAvi = false; + MainForm.PauseAvi = false; } [LuaMethodExample("local inclixpo = client.xpos( );")] [LuaMethod("xpos", "Returns the x value of the screen position where the client currently sits")] - public static int Xpos() + public int Xpos() { - return GlobalWin.MainForm.DesktopLocation.X; + return MainForm.DesktopLocation.X; } [LuaMethodExample("local incliypo = client.ypos( );")] [LuaMethod("ypos", "Returns the y value of the screen position where the client currently sits")] - public static int Ypos() + public int Ypos() { - return GlobalWin.MainForm.DesktopLocation.Y; + return MainForm.DesktopLocation.Y; } [LuaMethodExample("local incbhver = client.getversion( );")] @@ -446,7 +448,7 @@ namespace BizHawk.Client.EmuHawk [LuaMethod("saveram", "flushes save ram to disk")] public void SaveRam() { - GlobalWin.MainForm.FlushSaveRAM(); + MainForm.FlushSaveRAM(); } [LuaMethodExample("client.sleep( 50 );")] diff --git a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.cs b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.cs index 10943833e5..cb25e3a481 100644 --- a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.cs +++ b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.cs @@ -15,15 +15,18 @@ namespace BizHawk.Client.EmuHawk { public class EmuLuaLibrary : PlatformEmuLuaLibrary { + private readonly MainForm _mainForm; public EmuLuaLibrary() { // if (NLua.Lua.WhichLua == "NLua") _lua["keepalives"] = _lua.NewTable(); } - public EmuLuaLibrary(IEmulatorServiceProvider serviceProvider) + public EmuLuaLibrary(IEmulatorServiceProvider serviceProvider, MainForm mainForm) : this() { + _mainForm = mainForm; + static ApiContainer InitApiHawkContainerInstance(IEmulatorServiceProvider sp, Action logCallback) { var ctorParamTypes = new[] { typeof(Action) }; @@ -63,6 +66,13 @@ namespace BizHawk.Client.EmuHawk instance.LogOutputCallback = ConsoleLuaLibrary.LogOutput; ServiceInjector.UpdateServices(serviceProvider, instance); + // TODO: make EmuHawk libraries have a base class with common properties such as this + // and inject them here + if (instance is EmuHawkLuaLibrary emuHawkLibrary) + { + emuHawkLibrary.MainForm = _mainForm; + } + ApiHawkContainerInstance ??= InitApiHawkContainerInstance(serviceProvider, ConsoleLuaLibrary.LogOutput); if (instance is DelegatingLuaLibraryEmu dlgInstanceEmu) dlgInstanceEmu.APIs = ApiHawkContainerInstance; // this is necessary as the property has the `new` modifier else if (instance is DelegatingLuaLibrary dlgInstance) dlgInstance.APIs = ApiHawkContainerInstance; diff --git a/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs b/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs index 7f461e199b..e5f9d07dca 100644 --- a/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs +++ b/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs @@ -190,7 +190,7 @@ namespace BizHawk.Client.EmuHawk } var currentScripts = LuaImp?.ScriptList; // Temp fix for now - LuaImp = OSTailoredCode.IsUnixHost ? (PlatformEmuLuaLibrary) new NotReallyLuaLibrary() : new EmuLuaLibrary(Emulator.ServiceProvider); + LuaImp = OSTailoredCode.IsUnixHost ? (PlatformEmuLuaLibrary) new NotReallyLuaLibrary() : new EmuLuaLibrary(Emulator.ServiceProvider, MainForm); LuaImp.ScriptList.AddRange(currentScripts ?? Enumerable.Empty()); InputBox.AutoCompleteCustomSource.AddRange(LuaImp.Docs.Select(a => $"{a.Library}.{a.Name}").ToArray());