From 49d9953c407c8425ac2e6f90e121661fb143b1cf Mon Sep 17 00:00:00 2001 From: adelikat Date: Sun, 3 Nov 2013 01:02:17 +0000 Subject: [PATCH] move lua console to tools, tried not to break portable branch code but I did remove nearly every ifdef windows reference, and instead put one only on console load, from then on it does the proper ToolManager checks before calling luaconsole so in theory it should never crash if lua isn't support. Of course, really we should be figuring out how to support lua! --- BizHawk.MultiClient/MainForm.Events.cs | 14 +++- BizHawk.MultiClient/MainForm.cs | 80 +++++++------------ .../Lua/Libraries/EmuLuaLibrary.Console.cs | 10 +-- BizHawk.MultiClient/tools/Lua/LuaConsole.cs | 15 ++-- .../tools/Lua/LuaFunctionsForm.cs | 20 ++--- .../tools/Lua/LuaRegisteredFunctionsList.cs | 8 +- BizHawk.MultiClient/tools/Lua/LuaWinform.cs | 2 +- BizHawk.MultiClient/tools/Lua/LuaWriter.cs | 6 +- BizHawk.MultiClient/tools/ToolManager.cs | 23 ++++++ 9 files changed, 94 insertions(+), 84 deletions(-) diff --git a/BizHawk.MultiClient/MainForm.Events.cs b/BizHawk.MultiClient/MainForm.Events.cs index 938db28b05..c442207928 100644 --- a/BizHawk.MultiClient/MainForm.Events.cs +++ b/BizHawk.MultiClient/MainForm.Events.cs @@ -2147,18 +2147,26 @@ namespace BizHawk.MultiClient if (extension != null && extension.ToUpper() == ".LUA") { OpenLuaConsole(); - LuaConsole1.LoadLuaFile(path); + if (GlobalWinF.Tools.Has()) + { + GlobalWinF.Tools.LuaConsole.LoadLuaFile(path); + } isLua = true; } } if (isLua) + { return; + } - var ext = Path.GetExtension(filePaths[0]) ?? ""; + var ext = Path.GetExtension(filePaths[0]) ?? String.Empty; if (ext.ToUpper() == ".LUASES") { OpenLuaConsole(); - LuaConsole1.LoadLuaSession(filePaths[0]); + if (GlobalWinF.Tools.Has()) + { + GlobalWinF.Tools.LuaConsole.LoadLuaSession(filePaths[0]); + } } else if (IsValidMovieExtension(ext)) { diff --git a/BizHawk.MultiClient/MainForm.cs b/BizHawk.MultiClient/MainForm.cs index 2951f30dcf..beff3a6ef8 100644 --- a/BizHawk.MultiClient/MainForm.cs +++ b/BizHawk.MultiClient/MainForm.cs @@ -116,11 +116,6 @@ namespace BizHawk.MultiClient return Path.Combine(path, PathManager.FilesystemSafeName(Global.Game) + ".cht"); } -#if WINDOWS - private LuaConsole _luaconsole = null; - public LuaConsole LuaConsole1 { get { if (_luaconsole == null) _luaconsole = new LuaConsole(); return _luaconsole; } set { _luaconsole = value; } } -#endif - /// /// number of frames to autodump /// @@ -187,9 +182,6 @@ namespace BizHawk.MultiClient CloseGame(); Global.MovieSession.Movie.Stop(); GlobalWinF.Tools.Close(); -#if WINDOWS - CloseForm(LuaConsole1); -#endif SaveConfig(); }; @@ -635,12 +627,11 @@ namespace BizHawk.MultiClient GlobalWinF.AutofireStickyXORAdapter.MassToggleStickyState(Global.ActiveController.PressedButtons); } - //if (!EmulatorPaused) - //Global.ClickyVirtualPadController.FrameTick(); + if (GlobalWinF.Tools.Has()) + { + GlobalWinF.Tools.LuaConsole.ResumeScripts(false); + } -#if WINDOWS - LuaConsole1.ResumeScripts(false); -#endif StepRunLoop_Core(); //if(!IsNullEmulator()) @@ -1555,9 +1546,7 @@ namespace BizHawk.MultiClient //im not really a fan of how this is done.. if (Global.Config.RecentRoms.Empty || Global.Config.RecentRoms[0] != file.CanonicalFullPath) { -#if WINDOWS - LuaConsole1.Restart(); -#endif + GlobalWinF.Tools.Restart(); } Global.Config.RecentRoms.Add(file.CanonicalFullPath); @@ -2282,18 +2271,16 @@ namespace BizHawk.MultiClient GlobalWinF.Sound.UpdateSilence(); } - /// - /// Update all tools that are frame dependent like Ram Search before processing - /// public void UpdateToolsBefore(bool fromLua = false) { -#if WINDOWS - if (_luaconsole != null) + if (GlobalWinF.Tools.Has()) { - if (!fromLua) LuaConsole1.StartLuaDrawing(); - LuaConsole1.LuaImp.CallFrameBeforeEvent(); + if (!fromLua) + { + GlobalWinF.Tools.LuaConsole.StartLuaDrawing(); + } + GlobalWinF.Tools.LuaConsole.LuaImp.CallFrameBeforeEvent(); } -#endif GlobalWinF.Tools.UpdateBefore(); } @@ -2307,26 +2294,23 @@ namespace BizHawk.MultiClient public void UpdateToolsAfter(bool fromLua = false) { -#if WINDOWS - if (_luaconsole != null && !fromLua) + if (!fromLua && GlobalWinF.Tools.Has()) { - LuaConsole1.ResumeScripts(true); + GlobalWinF.Tools.LuaConsole.ResumeScripts(true); } -#endif GlobalWinF.Tools.UpdateAfter(); HandleToggleLight(); -#if WINDOWS - if (_luaconsole != null) + + if (GlobalWinF.Tools.Has()) { - LuaConsole1.LuaImp.CallFrameAfterEvent(); + GlobalWinF.Tools.LuaConsole.LuaImp.CallFrameAfterEvent(); if (!fromLua) { GlobalWinF.DisplayManager.PreFrameUpdateLuaSource(); - LuaConsole1.EndLuaDrawing(); + GlobalWinF.Tools.LuaConsole.EndLuaDrawing(); } } -#endif } private unsafe Image MakeScreenshotImage() @@ -2419,7 +2403,11 @@ namespace BizHawk.MultiClient } SaveStateFile(path, name, false); - LuaConsole1.LuaImp.CallSaveStateEvent(name); + + if (GlobalWinF.Tools.Has()) + { + GlobalWinF.Tools.LuaConsole.LuaImp.CallSaveStateEvent(name); + } } public void SaveStateFile(string filename, string name, bool fromLua) @@ -2467,7 +2455,11 @@ namespace BizHawk.MultiClient UpdateToolsAfter(fromLua); UpdateToolsLoadstate(); GlobalWinF.OSD.AddMessage("Loaded state: " + name); - LuaConsole1.LuaImp.CallLoadStateEvent(name); + + if (GlobalWinF.Tools.Has()) + { + GlobalWinF.Tools.LuaConsole.LuaImp.CallLoadStateEvent(name); + } } else { @@ -2884,17 +2876,12 @@ namespace BizHawk.MultiClient RewireSound(); ResetRewindBuffer(); Cheats_Restart(); -#if WINDOWS - LuaConsole1.Restart(); -#endif Text = "BizHawk" + (INTERIM ? " (interim) " : ""); HandlePlatformMenus(); StateSlots.Clear(); UpdateDumpIcon(); } - //------------------------------------------------------- - private void SaveConfig() { if (Global.Config.SaveWindowPosition) @@ -2912,11 +2899,6 @@ namespace BizHawk.MultiClient ConfigService.Save(PathManager.DefaultIniPath, Global.Config); } - private void CloseForm(Form form) - { - if (form != null && form.IsHandleCreated) form.Close(); - } - private void PreviousSlot() { if (Global.Config.SaveSlot == 0) @@ -3446,13 +3428,7 @@ namespace BizHawk.MultiClient public void OpenLuaConsole() { #if WINDOWS - if (!LuaConsole1.IsHandleCreated || LuaConsole1.IsDisposed) - { - LuaConsole1 = new LuaConsole(); - LuaConsole1.Show(); - } - else - LuaConsole1.Focus(); + GlobalWinF.Tools.Load(); #else MessageBox.Show("Sorry, Lua is not supported on this platform.", "Lua not supported", MessageBoxButtons.OK, MessageBoxIcon.Error); #endif diff --git a/BizHawk.MultiClient/tools/Lua/Libraries/EmuLuaLibrary.Console.cs b/BizHawk.MultiClient/tools/Lua/Libraries/EmuLuaLibrary.Console.cs index a446187e60..877251d872 100644 --- a/BizHawk.MultiClient/tools/Lua/Libraries/EmuLuaLibrary.Console.cs +++ b/BizHawk.MultiClient/tools/Lua/Libraries/EmuLuaLibrary.Console.cs @@ -26,13 +26,13 @@ namespace BizHawk.MultiClient public static void console_clear() { - GlobalWinF.MainForm.LuaConsole1.ClearOutputWindow(); + GlobalWinF.Tools.LuaConsole.ClearOutputWindow(); } public static string console_getluafunctionslist() { StringBuilder list = new StringBuilder(); - foreach (var function in GlobalWinF.MainForm.LuaConsole1.LuaImp.Docs.FunctionList) + foreach (var function in GlobalWinF.Tools.LuaConsole.LuaImp.Docs.FunctionList) { list.AppendLine(function.Name); } @@ -48,7 +48,7 @@ namespace BizHawk.MultiClient { if (lua_input == null) { - GlobalWinF.MainForm.LuaConsole1.WriteToOutputWindow("NULL"); + GlobalWinF.Tools.LuaConsole.WriteToOutputWindow("NULL"); } else { @@ -81,11 +81,11 @@ namespace BizHawk.MultiClient .AppendLine(); } - GlobalWinF.MainForm.LuaConsole1.WriteToOutputWindow(sb.ToString()); + GlobalWinF.Tools.LuaConsole.WriteToOutputWindow(sb.ToString()); } else { - GlobalWinF.MainForm.LuaConsole1.WriteToOutputWindow(lua_input.ToString()); + GlobalWinF.Tools.LuaConsole.WriteToOutputWindow(lua_input.ToString()); } } } diff --git a/BizHawk.MultiClient/tools/Lua/LuaConsole.cs b/BizHawk.MultiClient/tools/Lua/LuaConsole.cs index 8e1708f2a9..8c0dcc49d6 100644 --- a/BizHawk.MultiClient/tools/Lua/LuaConsole.cs +++ b/BizHawk.MultiClient/tools/Lua/LuaConsole.cs @@ -10,17 +10,17 @@ using BizHawk.Client.Common; namespace BizHawk.MultiClient { - public partial class LuaConsole : Form + public partial class LuaConsole : Form, IToolForm { //TODO: remember column widths //TODO: restore column width on restore default settings int defaultWidth; //For saving the default size of the dialog, so the user can restore if desired int defaultHeight; - string currentSessionFile = ""; + string currentSessionFile = String.Empty; List luaList = new List(); public EmuLuaLibrary LuaImp; - string lastLuaFile = ""; //TODO: this isn't getting used! + string lastLuaFile = String.Empty; //TODO: this isn't getting used! bool changes = false; private List GetLuaFileList() @@ -32,6 +32,9 @@ namespace BizHawk.MultiClient return l; } + public bool UpdateBefore { get { return true; } } + public void UpdateValues() { } + public LuaConsole get() { return this; @@ -1098,7 +1101,7 @@ namespace BizHawk.MultiClient turnOffAllScriptsToolStripMenuItem.Enabled = luaRunning; - showRegisteredFunctionsToolStripMenuItem.Enabled = GlobalWinF.MainForm.LuaConsole1.LuaImp.RegisteredFunctions.Any(); + showRegisteredFunctionsToolStripMenuItem.Enabled = GlobalWinF.Tools.LuaConsole.LuaImp.RegisteredFunctions.Any(); } private void contextMenuStrip1_Opening(object sender, CancelEventArgs e) @@ -1244,7 +1247,7 @@ namespace BizHawk.MultiClient private void showRegisteredFunctionsToolStripMenuItem_Click(object sender, EventArgs e) { - if (GlobalWinF.MainForm.LuaConsole1.LuaImp.RegisteredFunctions.Any()) + if (LuaImp.RegisteredFunctions.Any()) { LuaRegisteredFunctionsList dialog = new LuaRegisteredFunctionsList(); dialog.ShowDialog(); @@ -1253,7 +1256,7 @@ namespace BizHawk.MultiClient private void contextMenuStrip2_Opening(object sender, CancelEventArgs e) { - registeredFunctionsToolStripMenuItem.Enabled = GlobalWinF.MainForm.LuaConsole1.LuaImp.RegisteredFunctions.Any(); + registeredFunctionsToolStripMenuItem.Enabled = LuaImp.RegisteredFunctions.Any(); } } } diff --git a/BizHawk.MultiClient/tools/Lua/LuaFunctionsForm.cs b/BizHawk.MultiClient/tools/Lua/LuaFunctionsForm.cs index 5495b82e8e..dd763b1d6e 100644 --- a/BizHawk.MultiClient/tools/Lua/LuaFunctionsForm.cs +++ b/BizHawk.MultiClient/tools/Lua/LuaFunctionsForm.cs @@ -24,7 +24,7 @@ namespace BizHawk.MultiClient private void PopulateListView() { FunctionView.Items.Clear(); - foreach (LuaDocumentation.LibraryFunction l in GlobalWinF.MainForm.LuaConsole1.LuaImp.Docs.FunctionList) + foreach (LuaDocumentation.LibraryFunction l in GlobalWinF.Tools.LuaConsole.LuaImp.Docs.FunctionList) { ListViewItem item = new ListViewItem {Text = l.ReturnType}; item.SubItems.Add(l.Library + "."); @@ -42,16 +42,16 @@ namespace BizHawk.MultiClient switch (column) { case 0: //Return - GlobalWinF.MainForm.LuaConsole1.LuaImp.Docs.FunctionList = GlobalWinF.MainForm.LuaConsole1.LuaImp.Docs.FunctionList.OrderByDescending(x => x.ReturnType).ToList(); + GlobalWinF.Tools.LuaConsole.LuaImp.Docs.FunctionList = GlobalWinF.Tools.LuaConsole.LuaImp.Docs.FunctionList.OrderByDescending(x => x.ReturnType).ToList(); break; case 1: //Library - GlobalWinF.MainForm.LuaConsole1.LuaImp.Docs.FunctionList = GlobalWinF.MainForm.LuaConsole1.LuaImp.Docs.FunctionList.OrderByDescending(x => x.Library).ToList(); + GlobalWinF.Tools.LuaConsole.LuaImp.Docs.FunctionList = GlobalWinF.Tools.LuaConsole.LuaImp.Docs.FunctionList.OrderByDescending(x => x.Library).ToList(); break; case 2: //Name - GlobalWinF.MainForm.LuaConsole1.LuaImp.Docs.FunctionList = GlobalWinF.MainForm.LuaConsole1.LuaImp.Docs.FunctionList.OrderByDescending(x => x.Name).ToList(); + GlobalWinF.Tools.LuaConsole.LuaImp.Docs.FunctionList = GlobalWinF.Tools.LuaConsole.LuaImp.Docs.FunctionList.OrderByDescending(x => x.Name).ToList(); break; case 3: //Parameters - GlobalWinF.MainForm.LuaConsole1.LuaImp.Docs.FunctionList = GlobalWinF.MainForm.LuaConsole1.LuaImp.Docs.FunctionList.OrderByDescending(x => x.ParameterList).ToList(); + GlobalWinF.Tools.LuaConsole.LuaImp.Docs.FunctionList = GlobalWinF.Tools.LuaConsole.LuaImp.Docs.FunctionList.OrderByDescending(x => x.ParameterList).ToList(); break; } } @@ -60,16 +60,16 @@ namespace BizHawk.MultiClient switch (column) { case 0: //Return - GlobalWinF.MainForm.LuaConsole1.LuaImp.Docs.FunctionList = GlobalWinF.MainForm.LuaConsole1.LuaImp.Docs.FunctionList.OrderBy(x => x.ReturnType).ToList(); + GlobalWinF.Tools.LuaConsole.LuaImp.Docs.FunctionList = GlobalWinF.Tools.LuaConsole.LuaImp.Docs.FunctionList.OrderBy(x => x.ReturnType).ToList(); break; case 1: //Library - GlobalWinF.MainForm.LuaConsole1.LuaImp.Docs.FunctionList = GlobalWinF.MainForm.LuaConsole1.LuaImp.Docs.FunctionList.OrderBy(x => x.Library).ToList(); + GlobalWinF.Tools.LuaConsole.LuaImp.Docs.FunctionList = GlobalWinF.Tools.LuaConsole.LuaImp.Docs.FunctionList.OrderBy(x => x.Library).ToList(); break; case 2: //Name - GlobalWinF.MainForm.LuaConsole1.LuaImp.Docs.FunctionList = GlobalWinF.MainForm.LuaConsole1.LuaImp.Docs.FunctionList.OrderBy(x => x.Name).ToList(); + GlobalWinF.Tools.LuaConsole.LuaImp.Docs.FunctionList = GlobalWinF.Tools.LuaConsole.LuaImp.Docs.FunctionList.OrderBy(x => x.Name).ToList(); break; case 3: //Parameters - GlobalWinF.MainForm.LuaConsole1.LuaImp.Docs.FunctionList = GlobalWinF.MainForm.LuaConsole1.LuaImp.Docs.FunctionList.OrderBy(x => x.ParameterList).ToList(); + GlobalWinF.Tools.LuaConsole.LuaImp.Docs.FunctionList = GlobalWinF.Tools.LuaConsole.LuaImp.Docs.FunctionList.OrderBy(x => x.ParameterList).ToList(); break; } } @@ -133,7 +133,7 @@ namespace BizHawk.MultiClient foreach (int index in indexes) { - var library_function = GlobalWinF.MainForm.LuaConsole1.LuaImp.Docs.FunctionList[index]; + var library_function = GlobalWinF.Tools.LuaConsole.LuaImp.Docs.FunctionList[index]; sb.Append(library_function.Library).Append('.').Append(library_function.Name).Append("()\n"); } diff --git a/BizHawk.MultiClient/tools/Lua/LuaRegisteredFunctionsList.cs b/BizHawk.MultiClient/tools/Lua/LuaRegisteredFunctionsList.cs index a5e7ba0a94..1b0b1a7f61 100644 --- a/BizHawk.MultiClient/tools/Lua/LuaRegisteredFunctionsList.cs +++ b/BizHawk.MultiClient/tools/Lua/LuaRegisteredFunctionsList.cs @@ -32,7 +32,7 @@ namespace BizHawk.MultiClient { FunctionView.Items.Clear(); - List nlfs = GlobalWinF.MainForm.LuaConsole1.LuaImp.RegisteredFunctions.OrderBy(x => x.Event).ThenBy(x => x.Name).ToList(); + List nlfs = GlobalWinF.Tools.LuaConsole.LuaImp.RegisteredFunctions.OrderBy(x => x.Event).ThenBy(x => x.Name).ToList(); foreach (NamedLuaFunction nlf in nlfs) { ListViewItem item = new ListViewItem { Text = nlf.Event }; @@ -57,7 +57,7 @@ namespace BizHawk.MultiClient ListView.SelectedIndexCollection indexes = FunctionView.SelectedIndices; if (indexes.Count > 0) { - GlobalWinF.MainForm.LuaConsole1.LuaImp.RegisteredFunctions[indexes[0]].Call(); + GlobalWinF.Tools.LuaConsole.LuaImp.RegisteredFunctions[indexes[0]].Call(); } } @@ -66,8 +66,8 @@ namespace BizHawk.MultiClient ListView.SelectedIndexCollection indexes = FunctionView.SelectedIndices; if (indexes.Count > 0) { - NamedLuaFunction nlf = GlobalWinF.MainForm.LuaConsole1.LuaImp.RegisteredFunctions[indexes[0]]; - GlobalWinF.MainForm.LuaConsole1.LuaImp.RegisteredFunctions.Remove(nlf); + NamedLuaFunction nlf = GlobalWinF.Tools.LuaConsole.LuaImp.RegisteredFunctions[indexes[0]]; + GlobalWinF.Tools.LuaConsole.LuaImp.RegisteredFunctions.Remove(nlf); PopulateListView(); } } diff --git a/BizHawk.MultiClient/tools/Lua/LuaWinform.cs b/BizHawk.MultiClient/tools/Lua/LuaWinform.cs index 31ef3c08db..6a00821f3e 100644 --- a/BizHawk.MultiClient/tools/Lua/LuaWinform.cs +++ b/BizHawk.MultiClient/tools/Lua/LuaWinform.cs @@ -22,7 +22,7 @@ namespace BizHawk.MultiClient public void CloseThis() { - GlobalWinF.MainForm.LuaConsole1.LuaImp.WindowClosed(Handle); + GlobalWinF.Tools.LuaConsole.LuaImp.WindowClosed(Handle); } public void DoLuaEvent(IntPtr handle) diff --git a/BizHawk.MultiClient/tools/Lua/LuaWriter.cs b/BizHawk.MultiClient/tools/Lua/LuaWriter.cs index 52a415ab01..6eedfcaee6 100644 --- a/BizHawk.MultiClient/tools/Lua/LuaWriter.cs +++ b/BizHawk.MultiClient/tools/Lua/LuaWriter.cs @@ -434,7 +434,7 @@ namespace BizHawk.MultiClient private void GenerateLibraryRegex() { StringBuilder list = new StringBuilder(); - List Libs = GlobalWinF.MainForm.LuaConsole1.LuaImp.Docs.GetLibraryList(); + List Libs = GlobalWinF.Tools.LuaConsole.LuaImp.Docs.GetLibraryList(); for (int i = 0; i < Libs.Count; i++) { list.Append(Libs[i]); @@ -659,7 +659,7 @@ namespace BizHawk.MultiClient string currentword = CurrentWord(); if (IsLibraryWord(currentword)) { - List libfunctions = GlobalWinF.MainForm.LuaConsole1.LuaImp.Docs.GetFunctionsByLibrary(currentword); + List libfunctions = GlobalWinF.Tools.LuaConsole.LuaImp.Docs.GetFunctionsByLibrary(currentword); // Position autocomplete box near the cursor's current position int x = LuaText.GetPositionFromCharIndex(LuaText.SelectionStart).X + LuaText.Location.X + 5; @@ -783,7 +783,7 @@ namespace BizHawk.MultiClient private bool IsLibraryWord(string word) { - List Libs = GlobalWinF.MainForm.LuaConsole1.LuaImp.Docs.GetLibraryList(); + List Libs = GlobalWinF.Tools.LuaConsole.LuaImp.Docs.GetLibraryList(); if (Libs.Contains(word)) { return true; diff --git a/BizHawk.MultiClient/tools/ToolManager.cs b/BizHawk.MultiClient/tools/ToolManager.cs index 52239c8208..3d4be09519 100644 --- a/BizHawk.MultiClient/tools/ToolManager.cs +++ b/BizHawk.MultiClient/tools/ToolManager.cs @@ -296,6 +296,29 @@ namespace BizHawk.MultiClient } } + public LuaConsole LuaConsole + { + get + { + var tool = _tools.FirstOrDefault(x => x is LuaConsole); + if (tool != null) + { + if (tool.IsDisposed) + { + _tools.Remove(tool); + } + else + { + return tool as LuaConsole; + } + } + + var newTool = new LuaConsole(); + _tools.Add(newTool); + return newTool; + } + } + #endregion } }