From 4d06fb1fded8cfdc2f5fb3f48e79af7b75464a78 Mon Sep 17 00:00:00 2001 From: SuuperW Date: Sat, 16 Jan 2021 08:56:11 -0600 Subject: [PATCH] TAStudio: don't rely on a broken instance of a tool form for macro saving/loading --- .../tools/Macros/MacroInput.cs | 23 +++++---- .../tools/TAStudio/TAStudio.MenuItems.cs | 50 +++++++++++++------ .../tools/ToolFormBase.cs | 15 ++++-- 3 files changed, 58 insertions(+), 30 deletions(-) diff --git a/src/BizHawk.Client.EmuHawk/tools/Macros/MacroInput.cs b/src/BizHawk.Client.EmuHawk/tools/Macros/MacroInput.cs index 91d03f9860..bb52076458 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Macros/MacroInput.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Macros/MacroInput.cs @@ -15,7 +15,7 @@ namespace BizHawk.Client.EmuHawk [RequiredService] private IEmulator Emulator { get; set; } - private static readonly FilesystemFilterSet MacrosFSFilterSet = new FilesystemFilterSet(new FilesystemFilter("Movie Macros", new[] { "bk2m" })); + public static readonly FilesystemFilterSet MacrosFSFilterSet = new FilesystemFilterSet(new FilesystemFilter("Movie Macros", new[] { "bk2m" })); private readonly List _zones = new List(); private readonly List _unsavedZones = new List(); @@ -255,27 +255,28 @@ namespace BizHawk.Client.EmuHawk ZonesList.Items.Add($"{loadZone.Name} - length: {loadZone.Length}"); } - private string SuggestedFolder() + public static string SuggestedFolder(Config config, IGameInfo game = null) { - return Config.PathEntries.AbsolutePathFor(Path.Combine( - Config.PathEntries["Global", "Macros"].Path, - Game.FilesystemSafeName()), null); + return config.PathEntries.AbsolutePathFor(Path.Combine( + config.PathEntries["Global", "Macros"].Path, + game?.FilesystemSafeName()), null); } - public bool SaveMacroAs(MovieZone macro) + private bool SaveMacroAs(MovieZone macro) { + string suggestedFolder = SuggestedFolder(Config, Game); using var dialog = new SaveFileDialog { - InitialDirectory = SuggestedFolder(), + InitialDirectory = suggestedFolder, FileName = macro.Name, Filter = MacrosFSFilterSet.ToString() }; // Create directory? bool create = false; - if (!Directory.Exists(SuggestedFolder())) + if (!Directory.Exists(suggestedFolder)) { - Directory.CreateDirectory(SuggestedFolder()); + Directory.CreateDirectory(suggestedFolder); create = true; } @@ -295,11 +296,11 @@ namespace BizHawk.Client.EmuHawk return true; } - public MovieZone LoadMacro(IEmulator emulator = null, ToolManager tools = null) + private MovieZone LoadMacro(IEmulator emulator = null, ToolManager tools = null) { using var dialog = new OpenFileDialog { - InitialDirectory = SuggestedFolder(), + InitialDirectory = SuggestedFolder(Config), Filter = MacrosFSFilterSet.ToString() }; diff --git a/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.MenuItems.cs b/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.MenuItems.cs index 71b5543b63..34501d6f95 100644 --- a/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.MenuItems.cs +++ b/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.MenuItems.cs @@ -228,17 +228,26 @@ namespace BizHawk.Client.EmuHawk TasView.SelectRow(CurrentTasMovie.InputLogLength, false); } - var macro = new MovieZone( - CurrentTasMovie, - Emulator, - Tools, - MovieSession, - TasView.FirstSelectedIndex ?? 0, - TasView.LastSelectedIndex ?? 0 - TasView.FirstSelectedIndex ?? 0 + 1); + var file = SaveFileDialog( + null, + MacroInputTool.SuggestedFolder(Config, Game), + MacroInputTool.MacrosFSFilterSet, + this + ); - //var macroTool = Tools.Load(false); - var macroTool = new MacroInputTool { Config = Config }; - macroTool.SaveMacroAs(macro); + if (file != null) + { + new MovieZone( + CurrentTasMovie, + Emulator, + Tools, + MovieSession, + TasView.FirstSelectedIndex ?? 0, + TasView.LastSelectedIndex ?? 0 - TasView.FirstSelectedIndex ?? 0 + 1) + .Save(file.FullName); + + Config.RecentMacros.Add(file.FullName); + } } private void PlaceMacroAtSelectionMenuItem_Click(object sender, EventArgs e) @@ -248,13 +257,22 @@ namespace BizHawk.Client.EmuHawk return; } - //var macroTool = Tools.Load(false); - var macroTool = new MacroInputTool { Config = Config }; - var macro = macroTool.LoadMacro(Emulator, Tools); - if (macro != null) + var file = OpenFileDialog( + null, + MacroInputTool.SuggestedFolder(Config, Game), + MacroInputTool.MacrosFSFilterSet + ); + + if (file != null) { - macro.Start = TasView.FirstSelectedIndex ?? 0; - macro.PlaceZone(CurrentTasMovie, Config); + var macro = new MovieZone(file.FullName, Emulator, MovieSession, Tools); + if (macro != null) + { + macro.Start = TasView.FirstSelectedIndex ?? 0; + macro.PlaceZone(CurrentTasMovie, Config); + + Config.RecentMacros.Add(file.FullName); + } } } diff --git a/src/BizHawk.Client.EmuHawk/tools/ToolFormBase.cs b/src/BizHawk.Client.EmuHawk/tools/ToolFormBase.cs index e7911a9ae0..8bc049f796 100644 --- a/src/BizHawk.Client.EmuHawk/tools/ToolFormBase.cs +++ b/src/BizHawk.Client.EmuHawk/tools/ToolFormBase.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Windows.Forms; using BizHawk.Client.Common; @@ -59,6 +60,10 @@ namespace BizHawk.Client.EmuHawk protected virtual void FastUpdateAfter() { } public FileInfo OpenFileDialog(string currentFile, string path, string fileType, string fileExt) + { + return OpenFileDialog(currentFile, path, new FilesystemFilterSet(new FilesystemFilter(fileType, new[] { fileExt }))); + } + public FileInfo OpenFileDialog(string currentFile, string path, FilesystemFilterSet filterSet) { if (!Directory.Exists(path)) { @@ -69,9 +74,9 @@ namespace BizHawk.Client.EmuHawk { FileName = !string.IsNullOrWhiteSpace(currentFile) ? Path.GetFileName(currentFile) - : $"{Game.FilesystemSafeName()}.{fileExt}", + : $"{Game.FilesystemSafeName()}.{filterSet.Filters.FirstOrDefault()?.Extensions.FirstOrDefault()}", InitialDirectory = path, - Filter = new FilesystemFilterSet(new FilesystemFilter(fileType, new[] { fileExt })).ToString(), + Filter = filterSet.ToString(), RestoreDirectory = true }; @@ -80,6 +85,10 @@ namespace BizHawk.Client.EmuHawk } public static FileInfo SaveFileDialog(string currentFile, string path, string fileType, string fileExt, IDialogParent parent) + { + return SaveFileDialog(currentFile, path, new FilesystemFilterSet(new FilesystemFilter(fileType, new[] { fileExt })), parent); + } + public static FileInfo SaveFileDialog(string currentFile, string path, FilesystemFilterSet filterSet, IDialogParent parent) { if (!Directory.Exists(path)) { @@ -90,7 +99,7 @@ namespace BizHawk.Client.EmuHawk { FileName = Path.GetFileName(currentFile), InitialDirectory = path, - Filter = new FilesystemFilterSet(new FilesystemFilter(fileType, new[] { fileExt })).ToString(), + Filter = filterSet.ToString(), RestoreDirectory = true };