From 56e3642d5cae8e3caaae64f6b3933b1fb08efa9b Mon Sep 17 00:00:00 2001 From: nattthebear Date: Sun, 9 Aug 2020 17:11:31 -0400 Subject: [PATCH] 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 --- .../movie/MovieSession.cs | 32 +++++----------- .../movie/interfaces/IMovieSession.cs | 2 +- src/BizHawk.Client.EmuHawk/MainForm.Movie.cs | 37 ++++++++++++------- 3 files changed, 34 insertions(+), 37 deletions(-) diff --git a/src/BizHawk.Client.Common/movie/MovieSession.cs b/src/BizHawk.Client.Common/movie/MovieSession.cs index e53517c59e..452c3b0306 100644 --- a/src/BizHawk.Client.Common/movie/MovieSession.cs +++ b/src/BizHawk.Client.Common/movie/MovieSession.cs @@ -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 _preferredCores = new Dictionary(); - 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 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); diff --git a/src/BizHawk.Client.Common/movie/interfaces/IMovieSession.cs b/src/BizHawk.Client.Common/movie/interfaces/IMovieSession.cs index 8739553da6..41b3ccaa8a 100644 --- a/src/BizHawk.Client.Common/movie/interfaces/IMovieSession.cs +++ b/src/BizHawk.Client.Common/movie/interfaces/IMovieSession.cs @@ -66,7 +66,7 @@ namespace BizHawk.Client.Common /// /// Sets the Movie property with the QueuedMovie, clears the queued movie, and starts the new movie /// - void RunQueuedMovie(bool recordMode, IEmulator emulator, IDictionary preferredCores); + void RunQueuedMovie(bool recordMode, IEmulator emulator); void StopMovie(bool saveChanges = true); diff --git a/src/BizHawk.Client.EmuHawk/MainForm.Movie.cs b/src/BizHawk.Client.EmuHawk/MainForm.Movie.cs index b200f6cfc6..795f9105fc 100644 --- a/src/BizHawk.Client.EmuHawk/MainForm.Movie.cs +++ b/src/BizHawk.Client.EmuHawk/MainForm.Movie.cs @@ -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(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)