diff --git a/BizHawk.Client.Common/movie/import/IMovieImport.cs b/BizHawk.Client.Common/movie/import/IMovieImport.cs index 8f59f6874c..42d71e21fc 100644 --- a/BizHawk.Client.Common/movie/import/IMovieImport.cs +++ b/BizHawk.Client.Common/movie/import/IMovieImport.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; namespace BizHawk.Client.Common { @@ -30,6 +31,11 @@ namespace BizHawk.Client.Common RunImport(); + if (!Result.Errors.Any()) + { + Result.Movie.Save(); + } + return Result; } @@ -85,6 +91,13 @@ namespace BizHawk.Client.Common public IList Errors { get; } = new List(); public IMovie Movie { get; set; } + + public static ImportResult Error(string errorMsg) + { + var result = new ImportResult(); + result.Errors.Add(errorMsg); + return result; + } } [AttributeUsage(AttributeTargets.Class)] diff --git a/BizHawk.Client.Common/movie/import/MovieImport.cs b/BizHawk.Client.Common/movie/import/MovieImport.cs index 7d18ea944c..d0f7a5ae8f 100644 --- a/BizHawk.Client.Common/movie/import/MovieImport.cs +++ b/BizHawk.Client.Common/movie/import/MovieImport.cs @@ -19,18 +19,14 @@ namespace BizHawk.Client.Common } // Attempt to import another type of movie file into a movie object. - public static IMovie ImportFile(string path, out string errorMsg, out string warningMsg) + public static ImportResult ImportFile(string path) { - errorMsg = ""; - warningMsg = ""; - string ext = path != null ? Path.GetExtension(path).ToUpper() : ""; - + string ext = Path.GetExtension(path) ?? ""; var importerType = ImporterForExtension(ext); if (importerType == default) { - errorMsg = $"No importer found for file type {ext}"; - return null; + return ImportResult.Error($"No importer found for file type {ext}"); } // Create a new instance of the importer class using the no-argument constructor @@ -40,34 +36,10 @@ namespace BizHawk.Client.Common if (importer == null) { - errorMsg = $"No importer found for type {ext}"; - return null; + return ImportResult.Error($"No importer found for file type {ext}"); } - IMovie movie = null; - - try - { - var result = importer.Import(path); - if (result.Errors.Count > 0) - { - errorMsg = result.Errors.First(); - } - - if (result.Warnings.Count > 0) - { - warningMsg = result.Warnings.First(); - } - - movie = result.Movie; - } - catch (Exception ex) - { - errorMsg = ex.ToString(); - } - - movie?.Save(); - return movie; + return importer.Import(path); } private static Type ImporterForExtension(string ext) diff --git a/BizHawk.Client.EmuHawk/FileLoader.cs b/BizHawk.Client.EmuHawk/FileLoader.cs index 74b39bd265..9333983890 100644 --- a/BizHawk.Client.EmuHawk/FileLoader.cs +++ b/BizHawk.Client.EmuHawk/FileLoader.cs @@ -73,21 +73,7 @@ namespace BizHawk.Client.EmuHawk return; } - // tries to open a legacy movie format by importing it - var movie = MovieImport.ImportFile(filename, out var errorMsg, out var warningMsg); - if (!string.IsNullOrEmpty(errorMsg)) - { - MessageBox.Show(errorMsg, "Conversion error", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - else - { - // Fix movie extension to something palatable for these purposes. - // For instance, something which doesn't clobber movies you already may have had. - // I'm evenly torn between this, and a file in %TEMP%, but since we don't really have a way to clean up this tempfile, I choose this: - StartNewMovie(movie, false); - } - - GlobalWin.OSD.AddMessage(warningMsg); + ProcessMovieImport(filename, true); } private void LoadLuaFile(string filename, string archive = null) diff --git a/BizHawk.Client.EmuHawk/MainForm.Events.cs b/BizHawk.Client.EmuHawk/MainForm.Events.cs index 71ff7bf7fd..2774472387 100644 --- a/BizHawk.Client.EmuHawk/MainForm.Events.cs +++ b/BizHawk.Client.EmuHawk/MainForm.Events.cs @@ -542,7 +542,7 @@ namespace BizHawk.Client.EmuHawk { foreach (var fn in ofd.FileNames) { - ProcessMovieImport(fn); + ProcessMovieImport(fn, false); } } } diff --git a/BizHawk.Client.EmuHawk/MainForm.cs b/BizHawk.Client.EmuHawk/MainForm.cs index 97de293b91..6387defc76 100644 --- a/BizHawk.Client.EmuHawk/MainForm.cs +++ b/BizHawk.Client.EmuHawk/MainForm.cs @@ -322,20 +322,7 @@ namespace BizHawk.Client.EmuHawk // Copy pasta from drag & drop if (MovieImport.IsValidMovieExtension(Path.GetExtension(_argParser.cmdMovie))) { - var imported = MovieImport.ImportFile(_argParser.cmdMovie, out var errorMsg, out var warningMsg); - if (!string.IsNullOrEmpty(errorMsg)) - { - MessageBox.Show(errorMsg, "Conversion error", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - else - { - // fix movie extension to something palatable for these purposes. - // for instance, something which doesnt clobber movies you already may have had. - // i'm evenly torn between this, and a file in %TEMP%, but since we dont really have a way to clean up this tempfile, i choose this: - StartNewMovie(imported, false); - } - - GlobalWin.OSD.AddMessage(warningMsg); + ProcessMovieImport(_argParser.cmdMovie, true); } else { @@ -3852,23 +3839,26 @@ namespace BizHawk.Client.EmuHawk } } - private static void ProcessMovieImport(string fn) + private void ProcessMovieImport(string fn, bool start) { - var directory = PathManager.MakeAbsolutePath(Global.Config.PathEntries.MoviesPathFragment, null); - var movie = MovieImport.ImportFile(fn, out var errorMsg, out var warningMsg); + var result = MovieImport.ImportFile(fn); - if (!string.IsNullOrWhiteSpace(errorMsg)) + if (result.Errors.Any()) { - MessageBox.Show(errorMsg, "Conversion error", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show(string.Join("\n", result.Errors), "Conversion error", MessageBoxButtons.OK, MessageBoxIcon.Error); } - GlobalWin.OSD.AddMessage(!string.IsNullOrWhiteSpace(warningMsg) - ? warningMsg - : $"{Path.GetFileName(fn)} imported as {movie.Filename}"); - - if (!Directory.Exists(directory)) + if (result.Warnings.Any()) { - Directory.CreateDirectory(directory); + GlobalWin.OSD.AddMessage(result.Warnings.First()); // For now, just show the first warning + } + + GlobalWin.OSD.AddMessage($"{Path.GetFileName(fn)} imported as {result.Movie.Filename}"); + + if (start) + { + StartNewMovie(result.Movie, false); + Global.Config.RecentMovies.Add(result.Movie.Filename); } }