Fix movie importer crashing on files with unrecognised extentions

This commit is contained in:
James Groom 2024-03-22 17:00:43 +00:00 committed by GitHub
parent d17352a204
commit 85caed48cc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 16 deletions

View File

@ -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<Type>())?.Invoke(Array.Empty<object>()) 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<Type>())?.Invoke(Array.Empty<object>()) is IMovieImport importer
? importer.Import(dialogParent, session, path, config)
: ImportResult.Error($"No importer found for file type {ext}");
}
}
}

View File

@ -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())