Refactor how movie loading handles preferredCores

First of all, use a try..finally pair around the mutating calls to make it clear that Config.PreferredCores will always be rolled back, and to the correct value, after the operation is done.
Then, assume that when Movie.Core is set, we always want to prefer that core... no matter what system comes up.  Seems to fix #2259
This commit is contained in:
nattthebear 2020-08-09 17:11:31 -04:00
parent 5a0b49726e
commit 56e3642d5c
3 changed files with 34 additions and 37 deletions

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using BizHawk.Emulation.Common;
using BizHawk.Emulation.Cores;
using BizHawk.Emulation.Cores.Nintendo.Gameboy;
@ -18,10 +19,6 @@ namespace BizHawk.Client.Common
private IMovie _queuedMovie;
// Previous saved core preferences. Stored here so that when a movie
// overrides the values, they can be restored to user preferences
private readonly IDictionary<string, string> _preferredCores = new Dictionary<string, string>();
public MovieSession(
IMovieConfig settings,
string backDirectory,
@ -206,20 +203,15 @@ namespace BizHawk.Client.Common
if (!record)
{
if (preferredCores.ContainsKey(systemId))
if (string.IsNullOrWhiteSpace(movie.Core))
{
string movieCore = preferredCores[systemId];
if (string.IsNullOrWhiteSpace(movie.Core))
{
PopupMessage($"No core specified in the movie file, using the preferred core {preferredCores[systemId]} instead.");
}
else
{
movieCore = movie.Core;
}
_preferredCores[systemId] = preferredCores[systemId];
preferredCores[systemId] = movieCore;
PopupMessage($"No core specified in the movie file, using the preferred core {preferredCores[systemId]} instead.");
}
else
{
var keys = preferredCores.Keys.ToList();
foreach (var k in keys)
preferredCores[k] = movie.Core;
}
}
@ -235,13 +227,9 @@ namespace BizHawk.Client.Common
_queuedMovie = movie;
}
public void RunQueuedMovie(bool recordMode, IEmulator emulator, IDictionary<string, string> preferredCores)
public void RunQueuedMovie(bool recordMode, IEmulator emulator)
{
MovieController = new Bk2Controller(emulator.ControllerDefinition);
foreach (var previousPref in _preferredCores)
{
preferredCores[previousPref.Key] = previousPref.Value;
}
Movie = _queuedMovie;
Movie.Attach(emulator);

View File

@ -66,7 +66,7 @@ namespace BizHawk.Client.Common
/// <summary>
/// Sets the Movie property with the QueuedMovie, clears the queued movie, and starts the new movie
/// </summary>
void RunQueuedMovie(bool recordMode, IEmulator emulator, IDictionary<string, string> preferredCores);
void RunQueuedMovie(bool recordMode, IEmulator emulator);
void StopMovie(bool saveChanges = true);

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using BizHawk.Client.Common;
@ -15,26 +16,34 @@ namespace BizHawk.Client.EmuHawk
throw new ArgumentNullException($"{nameof(movie)} cannot be null.");
}
var oldPreferredCores = new Dictionary<string, string>(Config.PreferredCores);
try
{
MovieSession.QueueNewMovie(movie, record, Emulator.SystemId, Config.PreferredCores);
try
{
MovieSession.QueueNewMovie(movie, record, Emulator.SystemId, Config.PreferredCores);
}
catch (MoviePlatformMismatchException ex)
{
using var ownerForm = new Form { TopMost = true };
MessageBox.Show(ownerForm, ex.Message, "Movie/Platform Mismatch", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
if (!_isLoadingRom)
{
RebootCore();
}
Config.RecentMovies.Add(movie.Filename);
MovieSession.RunQueuedMovie(record, Emulator);
}
catch (MoviePlatformMismatchException ex)
finally
{
using var ownerForm = new Form { TopMost = true };
MessageBox.Show(ownerForm, ex.Message, "Movie/Platform Mismatch", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
Config.PreferredCores = oldPreferredCores;
}
if (!_isLoadingRom)
{
RebootCore();
}
Config.RecentMovies.Add(movie.Filename);
MovieSession.RunQueuedMovie(record, Emulator, Config.PreferredCores);
SetMainformMovieInfo();
if (MovieSession.Movie.Hash != Game.Hash)