From 6196d81b5dda0124eb125e901aa046405a39447f Mon Sep 17 00:00:00 2001 From: adelikat Date: Mon, 22 May 2017 16:47:27 -0500 Subject: [PATCH] Move _luaList from LuaConsole to LuaImp --- .../tools/Lua/Libraries/EmuLuaLibrary.cs | 26 +-- .../tools/Lua/LuaConsole.cs | 163 +++++++++--------- 2 files changed, 93 insertions(+), 96 deletions(-) diff --git a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.cs b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.cs index abbd60bbd4..402ebfd7f7 100644 --- a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.cs +++ b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.cs @@ -15,25 +15,12 @@ namespace BizHawk.Client.EmuHawk { public class EmuLuaLibrary { - private readonly Dictionary Libraries = new Dictionary(); - - private Lua _lua = new Lua(); - private Lua _currThread; - public EmuLuaLibrary() { Docs = new LuaDocumentation(); _lua["keepalives"] = _lua.NewTable(); } - private FormsLuaLibrary FormsLibrary => (FormsLuaLibrary)Libraries[typeof(FormsLuaLibrary)]; - - private EventLuaLibrary EventsLibrary => (EventLuaLibrary)Libraries[typeof(EventLuaLibrary)]; - - private EmulatorLuaLibrary EmulatorLuaLibrary => (EmulatorLuaLibrary)Libraries[typeof(EmulatorLuaLibrary)]; - - public GuiLuaLibrary GuiLibrary => (GuiLuaLibrary)Libraries[typeof(GuiLuaLibrary)]; - public EmuLuaLibrary(IEmulatorServiceProvider serviceProvider) : this() { @@ -94,6 +81,19 @@ namespace BizHawk.Client.EmuHawk } } + private readonly Dictionary Libraries = new Dictionary(); + public LuaFileList ScriptList { get; } = new LuaFileList(); + private Lua _lua = new Lua(); + private Lua _currThread; + + private FormsLuaLibrary FormsLibrary => (FormsLuaLibrary)Libraries[typeof(FormsLuaLibrary)]; + + private EventLuaLibrary EventsLibrary => (EventLuaLibrary)Libraries[typeof(EventLuaLibrary)]; + + private EmulatorLuaLibrary EmulatorLuaLibrary => (EmulatorLuaLibrary)Libraries[typeof(EmulatorLuaLibrary)]; + + public GuiLuaLibrary GuiLibrary => (GuiLuaLibrary)Libraries[typeof(GuiLuaLibrary)]; + public void Restart(IEmulatorServiceProvider newServiceProvider) { foreach (var lib in Libraries) diff --git a/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs b/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs index 829d8e8062..26edb437bc 100644 --- a/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs +++ b/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs @@ -19,7 +19,6 @@ namespace BizHawk.Client.EmuHawk [RequiredService] private IEmulator Emulator { get; set; } - private readonly LuaFileList _luaList; private bool _sortReverse; private string _lastColumnSorted; @@ -52,11 +51,6 @@ namespace BizHawk.Client.EmuHawk Settings = new LuaConsoleSettings(); _sortReverse = false; _lastColumnSorted = ""; - _luaList = new LuaFileList - { - ChangedCallback = SessionChangedCallback, - LoadCallback = ClearOutputWindow - }; InitializeComponent(); @@ -92,7 +86,7 @@ namespace BizHawk.Client.EmuHawk private IEnumerable SelectedItems { - get { return LuaListView.SelectedIndices().Select(index => _luaList[index]); } + get { return LuaListView.SelectedIndices().Select(index => LuaImp.ScriptList[index]); } } private IEnumerable SelectedFiles @@ -122,6 +116,9 @@ namespace BizHawk.Client.EmuHawk private void LuaConsole_Load(object sender, EventArgs e) { + LuaImp.ScriptList.ChangedCallback = SessionChangedCallback; + LuaImp.ScriptList.LoadCallback = ClearOutputWindow; + if (Global.Config.RecentLuaSession.AutoLoad && !Global.Config.RecentLuaSession.Empty) { LoadSessionFromRecent(Global.Config.RecentLuaSession.MostRecent); @@ -151,7 +148,7 @@ namespace BizHawk.Client.EmuHawk LuaImp.GuiLibrary.DrawFinish(); } - var runningScripts = _luaList.Where(f => f.Enabled).ToList(); + var runningScripts = LuaImp?.ScriptList.Where(f => f.Enabled).ToList() ?? new List(); foreach (var file in runningScripts) { @@ -202,7 +199,7 @@ namespace BizHawk.Client.EmuHawk private void AddFileWatches() { _watches.Clear(); - foreach (var item in _luaList) + foreach (var item in LuaImp.ScriptList) { var processedPath = PathManager.TryMakeRelative(item.Path); string pathToLoad = ProcessPath(processedPath); @@ -246,8 +243,8 @@ namespace BizHawk.Client.EmuHawk { var luaFile = new LuaFile("", processedPath); - _luaList.Add(luaFile); - LuaListView.ItemCount = _luaList.Count; + LuaImp.ScriptList.Add(luaFile); + LuaListView.ItemCount = LuaImp.ScriptList.Count; Global.Config.RecentLua.Add(processedPath); if (!Global.Config.DisableLuaScriptsOnLoad) @@ -281,7 +278,7 @@ namespace BizHawk.Client.EmuHawk } else { - foreach (var file in _luaList.Where(file => processedPath == file.Path && file.Enabled == false && !Global.Config.DisableLuaScriptsOnLoad)) + foreach (var file in LuaImp.ScriptList.Where(file => processedPath == file.Path && file.Enabled == false && !Global.Config.DisableLuaScriptsOnLoad)) { file.Toggle(); break; @@ -295,7 +292,7 @@ namespace BizHawk.Client.EmuHawk private void UpdateDialog() { - LuaListView.ItemCount = _luaList.Count; + LuaListView.ItemCount = LuaImp.ScriptList.Count; LuaListView.Refresh(); UpdateNumberOfScripts(); UpdateRegisteredFunctionsDialog(); @@ -303,7 +300,7 @@ namespace BizHawk.Client.EmuHawk private void RunLuaScripts() { - foreach (var file in _luaList) + foreach (var file in LuaImp.ScriptList) { if (!file.Enabled && file.Thread == null) { @@ -334,8 +331,8 @@ namespace BizHawk.Client.EmuHawk private void SessionChangedCallback() { OutputMessages.Text = - (_luaList.Changes ? "* " : "") + - Path.GetFileName(_luaList.Filename); + (LuaImp.ScriptList.Changes ? "* " : "") + + Path.GetFileName(LuaImp.ScriptList.Filename); } private void LuaListView_QueryItemImage(int item, int subItem, out int imageIndex) @@ -346,11 +343,11 @@ namespace BizHawk.Client.EmuHawk return; } - if (_luaList[item].Paused) + if (LuaImp.ScriptList[item].Paused) { imageIndex = 2; } - else if (_luaList[item].Enabled) + else if (LuaImp.ScriptList[item].Enabled) { imageIndex = 1; } @@ -369,15 +366,15 @@ namespace BizHawk.Client.EmuHawk { if (column == 0) { - if (_luaList[index].IsSeparator) + if (LuaImp.ScriptList[index].IsSeparator) { color = BackColor; } - else if (_luaList[index].Enabled && !_luaList[index].Paused) + else if (LuaImp.ScriptList[index].Enabled && !LuaImp.ScriptList[index].Paused) { color = Color.LightCyan; } - else if (_luaList[index].Enabled && _luaList[index].Paused) + else if (LuaImp.ScriptList[index].Enabled && LuaImp.ScriptList[index].Paused) { color = Color.LightPink; } @@ -391,11 +388,11 @@ namespace BizHawk.Client.EmuHawk text = ""; if (column == 0) { - text = Path.GetFileNameWithoutExtension(_luaList[index].Path); // TODO: how about allow the user to name scripts? + text = Path.GetFileNameWithoutExtension(LuaImp.ScriptList[index].Path); // TODO: how about allow the user to name scripts? } else if (column == 1) { - text = DressUpRelative(_luaList[index].Path); + text = DressUpRelative(LuaImp.ScriptList[index].Path); } } @@ -436,8 +433,8 @@ namespace BizHawk.Client.EmuHawk { var message = ""; var total = SelectedFiles.Count(); - var active = _luaList.Count(file => file.Enabled); - var paused = _luaList.Count(file => file.Enabled && file.Paused); + var active = LuaImp.ScriptList.Count(file => file.Enabled); + var paused = LuaImp.ScriptList.Count(file => file.Enabled && file.Paused); if (total == 1) { @@ -462,7 +459,7 @@ namespace BizHawk.Client.EmuHawk private bool LuaAlreadyInSession(string path) { - return _luaList.Any(t => path == t.Path); + return LuaImp.ScriptList.Any(t => path == t.Path); } public void WriteToOutputWindow(string message) @@ -496,11 +493,11 @@ namespace BizHawk.Client.EmuHawk public bool LoadLuaSession(string path) { - var result = _luaList.LoadLuaSession(path); + var result = LuaImp.ScriptList.LoadLuaSession(path); RunLuaScripts(); UpdateDialog(); - _luaList.Changes = false; + LuaImp.ScriptList.Changes = false; return result; } @@ -511,7 +508,7 @@ namespace BizHawk.Client.EmuHawk /// should frame waiters be waken up? only use this immediately before a frame of emulation public void ResumeScripts(bool includeFrameWaiters) { - if (!_luaList.Any()) + if (!LuaImp.ScriptList.Any()) { return; } @@ -521,7 +518,7 @@ namespace BizHawk.Client.EmuHawk LuaImp.GuiLibrary.DrawNew("emu"); } - foreach (var lf in _luaList.Where(l => l.Enabled && l.Thread != null && !l.Paused)) + foreach (var lf in LuaImp.ScriptList.Where(l => l.Enabled && l.Thread != null && !l.Paused)) { try { @@ -554,7 +551,7 @@ namespace BizHawk.Client.EmuHawk public void StartLuaDrawing() { - if (_luaList.Any() && LuaImp.GuiLibrary.SurfaceIsNull) + if (LuaImp.ScriptList.Any() && LuaImp.GuiLibrary.SurfaceIsNull) { LuaImp.GuiLibrary.DrawNew("emu"); } @@ -562,7 +559,7 @@ namespace BizHawk.Client.EmuHawk public void EndLuaDrawing() { - if (_luaList.Any()) + if (LuaImp.ScriptList.Any()) { LuaImp.GuiLibrary.DrawFinish(); } @@ -581,10 +578,10 @@ namespace BizHawk.Client.EmuHawk private FileInfo GetSaveFileFromUser() { var sfd = new SaveFileDialog(); - if (!string.IsNullOrWhiteSpace(_luaList.Filename)) + if (!string.IsNullOrWhiteSpace(LuaImp.ScriptList.Filename)) { - sfd.FileName = Path.GetFileNameWithoutExtension(_luaList.Filename); - sfd.InitialDirectory = Path.GetDirectoryName(_luaList.Filename); + sfd.FileName = Path.GetFileNameWithoutExtension(LuaImp.ScriptList.Filename); + sfd.InitialDirectory = Path.GetDirectoryName(LuaImp.ScriptList.Filename); } else if (Global.Game != null) { @@ -613,22 +610,22 @@ namespace BizHawk.Client.EmuHawk var file = GetSaveFileFromUser(); if (file != null) { - _luaList.SaveSession(file.FullName); - OutputMessages.Text = Path.GetFileName(_luaList.Filename) + " saved."; + LuaImp.ScriptList.SaveSession(file.FullName); + OutputMessages.Text = Path.GetFileName(LuaImp.ScriptList.Filename) + " saved."; } } private void LoadSessionFromRecent(string path) { var doload = true; - if (_luaList.Changes) + if (LuaImp.ScriptList.Changes) { doload = AskSaveChanges(); } if (doload) { - if (!_luaList.LoadLuaSession(path)) + if (!LuaImp.ScriptList.LoadLuaSession(path)) { Global.Config.RecentLuaSession.HandleLoadError(path); } @@ -636,7 +633,7 @@ namespace BizHawk.Client.EmuHawk { RunLuaScripts(); UpdateDialog(); - _luaList.Changes = false; + LuaImp.ScriptList.Changes = false; } } @@ -645,16 +642,16 @@ namespace BizHawk.Client.EmuHawk public bool AskSaveChanges() { - if (_luaList.Changes && !string.IsNullOrEmpty(_luaList.Filename)) + if (LuaImp.ScriptList.Changes && !string.IsNullOrEmpty(LuaImp.ScriptList.Filename)) { GlobalWin.Sound.StopSound(); var result = MessageBox.Show("Save changes to session?", "Lua Console", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3); GlobalWin.Sound.StartSound(); if (result == DialogResult.Yes) { - if (!string.IsNullOrWhiteSpace(_luaList.Filename)) + if (!string.IsNullOrWhiteSpace(LuaImp.ScriptList.Filename)) { - _luaList.SaveSession(); + LuaImp.ScriptList.SaveSession(); } else { @@ -666,7 +663,7 @@ namespace BizHawk.Client.EmuHawk if (result == DialogResult.No) { - _luaList.Changes = false; + LuaImp.ScriptList.Changes = false; return true; } @@ -693,7 +690,7 @@ namespace BizHawk.Client.EmuHawk private void FileSubMenu_DropDownOpened(object sender, EventArgs e) { - SaveSessionMenuItem.Enabled = _luaList.Changes; + SaveSessionMenuItem.Enabled = LuaImp.ScriptList.Changes; } private void RecentSessionsSubMenu_DropDownOpened(object sender, EventArgs e) @@ -712,11 +709,11 @@ namespace BizHawk.Client.EmuHawk private void NewSessionMenuItem_Click(object sender, EventArgs e) { - var result = !_luaList.Changes || AskSaveChanges(); + var result = !LuaImp.ScriptList.Changes || AskSaveChanges(); if (result) { - _luaList.Clear(); + LuaImp.ScriptList.Clear(); ClearOutputWindow(); UpdateDialog(); } @@ -727,27 +724,27 @@ namespace BizHawk.Client.EmuHawk var file = GetFileFromUser("Lua Session Files (*.luases)|*.luases|All Files|*.*"); if (file != null) { - _luaList.LoadLuaSession(file.FullName); + LuaImp.ScriptList.LoadLuaSession(file.FullName); RunLuaScripts(); UpdateDialog(); - _luaList.Changes = false; + LuaImp.ScriptList.Changes = false; } } private void SaveSessionMenuItem_Click(object sender, EventArgs e) { - if (_luaList.Changes) + if (LuaImp.ScriptList.Changes) { - if (!string.IsNullOrWhiteSpace(_luaList.Filename)) + if (!string.IsNullOrWhiteSpace(LuaImp.ScriptList.Filename)) { - _luaList.SaveSession(); + LuaImp.ScriptList.SaveSession(); } else { SaveSessionAs(); } - OutputMessages.Text = Path.GetFileName(_luaList.Filename) + " saved."; + OutputMessages.Text = Path.GetFileName(LuaImp.ScriptList.Filename) + " saved."; } } @@ -778,8 +775,8 @@ namespace BizHawk.Client.EmuHawk MoveDownMenuItem.Enabled = LuaListView.SelectedIndices().Any(); - SelectAllMenuItem.Enabled = _luaList.Any(); - StopAllScriptsMenuItem.Enabled = _luaList.Any(script => script.Enabled); + SelectAllMenuItem.Enabled = LuaImp.ScriptList.Any(); + StopAllScriptsMenuItem.Enabled = LuaImp.ScriptList.Any(script => script.Enabled); RegisteredFunctionsMenuItem.Enabled = GlobalWin.Tools.LuaConsole.LuaImp.RegisteredFunctions.Any(); } @@ -787,12 +784,12 @@ namespace BizHawk.Client.EmuHawk { var sfd = new SaveFileDialog { - InitialDirectory = !string.IsNullOrWhiteSpace(_luaList.Filename) ? - Path.GetDirectoryName(_luaList.Filename) : + InitialDirectory = !string.IsNullOrWhiteSpace(LuaImp.ScriptList.Filename) ? + Path.GetDirectoryName(LuaImp.ScriptList.Filename) : PathManager.MakeAbsolutePath(Global.Config.PathEntries.LuaPathFragment, null), DefaultExt = ".lua", - FileName = !string.IsNullOrWhiteSpace(_luaList.Filename) ? - Path.GetFileNameWithoutExtension(_luaList.Filename) : + FileName = !string.IsNullOrWhiteSpace(LuaImp.ScriptList.Filename) ? + Path.GetFileNameWithoutExtension(LuaImp.ScriptList.Filename) : Path.GetFileNameWithoutExtension(Global.Game.Name), OverwritePrompt = true, Filter = "Lua Scripts (*.lua)|*.lua|All Files (*.*)|*.*" @@ -804,7 +801,7 @@ namespace BizHawk.Client.EmuHawk { string defaultTemplate = "while true do\n\temu.frameadvance();\nend"; File.WriteAllText(sfd.FileName, defaultTemplate); - _luaList.Add(new LuaFile(Path.GetFileNameWithoutExtension(sfd.FileName), sfd.FileName)); + LuaImp.ScriptList.Add(new LuaFile(Path.GetFileNameWithoutExtension(sfd.FileName), sfd.FileName)); UpdateDialog(); System.Diagnostics.Process.Start(sfd.FileName); } @@ -822,7 +819,7 @@ namespace BizHawk.Client.EmuHawk private void ToggleScriptMenuItem_Click(object sender, EventArgs e) { - var files = !SelectedFiles.Any() && Global.Config.ToggleAllIfNoneSelected ? _luaList : SelectedFiles; + var files = !SelectedFiles.Any() && Global.Config.ToggleAllIfNoneSelected ? LuaImp.ScriptList : SelectedFiles; foreach (var item in files) { item.Toggle(); @@ -927,7 +924,7 @@ namespace BizHawk.Client.EmuHawk LuaImp.RegisteredFunctions.Remove(function); } - _luaList.Remove(item); + LuaImp.ScriptList.Remove(item); } UpdateRegisteredFunctionsDialog(); @@ -954,7 +951,7 @@ namespace BizHawk.Client.EmuHawk { string text = File.ReadAllText(script.Path); File.WriteAllText(sfd.FileName, text); - _luaList.Add(new LuaFile(Path.GetFileNameWithoutExtension(sfd.FileName), sfd.FileName)); + LuaImp.ScriptList.Add(new LuaFile(Path.GetFileNameWithoutExtension(sfd.FileName), sfd.FileName)); UpdateDialog(); System.Diagnostics.Process.Start(sfd.FileName); } @@ -964,13 +961,13 @@ namespace BizHawk.Client.EmuHawk private void InsertSeparatorMenuItem_Click(object sender, EventArgs e) { var indices = LuaListView.SelectedIndices().ToList(); - if (indices.Any() && indices.Last() < _luaList.Count) + if (indices.Any() && indices.Last() < LuaImp.ScriptList.Count) { - _luaList.Insert(indices.Last(), LuaFile.SeparatorInstance); + LuaImp.ScriptList.Insert(indices.Last(), LuaFile.SeparatorInstance); } else { - _luaList.Add(LuaFile.SeparatorInstance); + LuaImp.ScriptList.Add(LuaFile.SeparatorInstance); } UpdateDialog(); @@ -986,9 +983,9 @@ namespace BizHawk.Client.EmuHawk foreach (var index in indices) { - var file = _luaList[index]; - _luaList.Remove(file); - _luaList.Insert(index - 1, file); + var file = LuaImp.ScriptList[index]; + LuaImp.ScriptList.Remove(file); + LuaImp.ScriptList.Insert(index - 1, file); } var newindices = indices.Select(t => t - 1).ToList(); @@ -1005,16 +1002,16 @@ namespace BizHawk.Client.EmuHawk private void MoveDownMenuItem_Click(object sender, EventArgs e) { var indices = LuaListView.SelectedIndices().ToList(); - if (indices.Count == 0 || indices.Last() == _luaList.Count - 1) + if (indices.Count == 0 || indices.Last() == LuaImp.ScriptList.Count - 1) { return; } for (var i = indices.Count - 1; i >= 0; i--) { - var file = _luaList[indices[i]]; - _luaList.Remove(file); - _luaList.Insert(indices[i] + 1, file); + var file = LuaImp.ScriptList[indices[i]]; + LuaImp.ScriptList.Remove(file); + LuaImp.ScriptList.Insert(indices[i] + 1, file); } var newindices = indices.Select(t => t + 1).ToList(); @@ -1035,7 +1032,7 @@ namespace BizHawk.Client.EmuHawk private void StopAllScriptsMenuItem_Click(object sender, EventArgs e) { - _luaList.StopAllScripts(); + LuaImp.ScriptList.StopAllScripts(); } private void RegisteredFunctionsMenuItem_Click(object sender, EventArgs e) @@ -1192,7 +1189,7 @@ namespace BizHawk.Client.EmuHawk StopAllScriptsContextItem.Visible = ScriptContextSeparator.Visible = - _luaList.Any(file => file.Enabled); + LuaImp.ScriptList.Any(file => file.Enabled); } private void ConsoleContextMenu_Opening(object sender, CancelEventArgs e) @@ -1223,10 +1220,10 @@ namespace BizHawk.Client.EmuHawk } else if (Path.GetExtension(path).ToLower() == ".luases") { - _luaList.LoadLuaSession(path); + LuaImp.ScriptList.LoadLuaSession(path); RunLuaScripts(); UpdateDialog(); - _luaList.Changes = false; + LuaImp.ScriptList.Changes = false; return; } } @@ -1280,12 +1277,12 @@ namespace BizHawk.Client.EmuHawk // For getting the name of the .lua file, for some reason this field is kept blank in LuaFile.cs? // The Name variable gets emptied again near the end just in case it would break something. - for (var i = 0; i < _luaList.Count; i++) + for (var i = 0; i < LuaImp.ScriptList.Count; i++) { - var words = Regex.Split(_luaList[i].Path, ".lua"); + var words = Regex.Split(LuaImp.ScriptList[i].Path, ".lua"); var split = words[0].Split(Path.DirectorySeparatorChar); - luaListTemp.Add(_luaList[i]); + luaListTemp.Add(LuaImp.ScriptList[i]); luaListTemp[i].Name = split[split.Length - 1]; } @@ -1304,10 +1301,10 @@ namespace BizHawk.Client.EmuHawk break; } - for (var i = 0; i < _luaList.Count; i++) + for (var i = 0; i < LuaImp.ScriptList.Count; i++) { - _luaList[i] = luaListTemp[i]; - _luaList[i].Name = ""; + LuaImp.ScriptList[i] = luaListTemp[i]; + LuaImp.ScriptList[i].Name = ""; } UpdateDialog();