From 85caed48cc5a1072d5019ef7cab45ddc385d4db4 Mon Sep 17 00:00:00 2001 From: James Groom Date: Fri, 22 Mar 2024 17:00:43 +0000 Subject: [PATCH] Fix movie importer crashing on files with unrecognised extentions --- .../movie/import/MovieImport.cs | 23 ++++++------------- src/BizHawk.Client.EmuHawk/MainForm.cs | 1 + 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/src/BizHawk.Client.Common/movie/import/MovieImport.cs b/src/BizHawk.Client.Common/movie/import/MovieImport.cs index 72310e478e..1b0fe10c18 100644 --- a/src/BizHawk.Client.Common/movie/import/MovieImport.cs +++ b/src/BizHawk.Client.Common/movie/import/MovieImport.cs @@ -4,6 +4,8 @@ using System.Linq; using System.IO; using System.Reflection; +using BizHawk.Common.CollectionExtensions; + namespace BizHawk.Client.Common { public static class MovieImport @@ -39,23 +41,12 @@ namespace BizHawk.Client.Common Config config) { string ext = Path.GetExtension(path) ?? ""; - var importerType = ImporterForExtension(ext); - - if (importerType == default) - { - return ImportResult.Error($"No importer found for file type {ext}"); - } - + var result = Importers.FirstOrNull(kvp => string.Equals(kvp.Value.Extension, ext, StringComparison.OrdinalIgnoreCase)); // Create a new instance of the importer class using the no-argument constructor - - return importerType.GetConstructor(Array.Empty())?.Invoke(Array.Empty()) is IMovieImport importer - ? importer.Import(dialogParent, session, path, config) - : ImportResult.Error($"No importer found for file type {ext}"); - } - - private static Type ImporterForExtension(string ext) - { - return Importers.First(i => string.Equals(i.Value.Extension, ext, StringComparison.OrdinalIgnoreCase)).Key; + return result is { Key: var importerType } + && importerType.GetConstructor(Array.Empty())?.Invoke(Array.Empty()) is IMovieImport importer + ? importer.Import(dialogParent, session, path, config) + : ImportResult.Error($"No importer found for file type {ext}"); } } } diff --git a/src/BizHawk.Client.EmuHawk/MainForm.cs b/src/BizHawk.Client.EmuHawk/MainForm.cs index 95cd50396a..d5379ba676 100644 --- a/src/BizHawk.Client.EmuHawk/MainForm.cs +++ b/src/BizHawk.Client.EmuHawk/MainForm.cs @@ -4162,6 +4162,7 @@ namespace BizHawk.Client.EmuHawk if (result.Errors.Any()) { ShowMessageBox(owner: null, string.Join("\n", result.Errors), "Conversion error", EMsgBoxIcon.Error); + return; } if (result.Warnings.Any())