simplify movie import code more

This commit is contained in:
adelikat 2019-11-14 17:00:47 -06:00
parent f93766e5bb
commit af50d74b98
5 changed files with 35 additions and 74 deletions

View File

@ -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<string> Errors { get; } = new List<string>();
public IMovie Movie { get; set; }
public static ImportResult Error(string errorMsg)
{
var result = new ImportResult();
result.Errors.Add(errorMsg);
return result;
}
}
[AttributeUsage(AttributeTargets.Class)]

View File

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

View File

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

View File

@ -542,7 +542,7 @@ namespace BizHawk.Client.EmuHawk
{
foreach (var fn in ofd.FileNames)
{
ProcessMovieImport(fn);
ProcessMovieImport(fn, false);
}
}
}

View File

@ -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);
}
}