From 55bb627550bd603b007900b7de42c44c6aa331b5 Mon Sep 17 00:00:00 2001 From: adelikat Date: Fri, 5 Jun 2020 21:09:03 -0500 Subject: [PATCH] Move MovieService.Get to IMovieSession, and require an IMovieSession in Bk2Movie and TasMovie ctors, refactor accordingly --- .../movie/MovieConversionExtensions.cs | 8 ++++---- src/BizHawk.Client.Common/movie/MovieService.cs | 12 ------------ src/BizHawk.Client.Common/movie/MovieSession.cs | 14 +++++++++++++- src/BizHawk.Client.Common/movie/bk2/Bk2Movie.IO.cs | 2 +- src/BizHawk.Client.Common/movie/bk2/Bk2Movie.cs | 8 ++++---- .../movie/import/IMovieImport.cs | 6 +++--- .../movie/import/MovieImport.cs | 4 ++-- .../movie/interfaces/IMovie.cs | 7 ++++++- .../movie/interfaces/IMovieSession.cs | 4 +++- .../movie/tasproj/TasMovie.cs | 6 +++--- src/BizHawk.Client.EmuHawk/MainForm.FileLoader.cs | 2 +- src/BizHawk.Client.EmuHawk/MainForm.cs | 8 ++++---- src/BizHawk.Client.EmuHawk/movie/PlayMovie.cs | 2 +- src/BizHawk.Client.EmuHawk/movie/RecordMovie.cs | 2 +- .../tools/TAStudio/TAStudio.MenuItems.cs | 2 +- .../tools/TAStudio/TAStudio.cs | 4 ++-- 16 files changed, 49 insertions(+), 42 deletions(-) diff --git a/src/BizHawk.Client.Common/movie/MovieConversionExtensions.cs b/src/BizHawk.Client.Common/movie/MovieConversionExtensions.cs index e596674d28..6ec30f56c5 100644 --- a/src/BizHawk.Client.Common/movie/MovieConversionExtensions.cs +++ b/src/BizHawk.Client.Common/movie/MovieConversionExtensions.cs @@ -18,7 +18,7 @@ namespace BizHawk.Client.Common.MovieConversionExtensions public static ITasMovie ToTasMovie(this IMovie old) { string newFilename = ConvertFileNameToTasMovie(old.Filename); - var tas = (ITasMovie)MovieService.Get(newFilename); + var tas = (ITasMovie)old.Session.Get(newFilename); tas.CopyLog(old.GetLogEntries()); old.Truncate(0); // Trying to minimize ram usage @@ -53,7 +53,7 @@ namespace BizHawk.Client.Common.MovieConversionExtensions public static IMovie ToBk2(this IMovie old) { - var bk2 = MovieService.Get(old.Filename.Replace(old.PreferredExtension, Bk2Movie.Extension)); + var bk2 = old.Session.Get(old.Filename.Replace(old.PreferredExtension, Bk2Movie.Extension)); bk2.CopyLog(old.GetLogEntries()); bk2.HeaderEntries.Clear(); @@ -87,7 +87,7 @@ namespace BizHawk.Client.Common.MovieConversionExtensions { string newFilename = ConvertFileNameToTasMovie(old.Filename); - var tas = (ITasMovie)MovieService.Get(newFilename); + var tas = (ITasMovie)old.Session.Get(newFilename); tas.BinarySavestate = savestate; tas.LagLog.Clear(); @@ -144,7 +144,7 @@ namespace BizHawk.Client.Common.MovieConversionExtensions { string newFilename = ConvertFileNameToTasMovie(old.Filename); - var tas = (ITasMovie) MovieService.Get(newFilename); + var tas = (ITasMovie)old.Session.Get(newFilename); tas.SaveRam = saveRam; tas.TasStateManager.Clear(); tas.LagLog.Clear(); diff --git a/src/BizHawk.Client.Common/movie/MovieService.cs b/src/BizHawk.Client.Common/movie/MovieService.cs index 74f38d5aea..c24892cc56 100644 --- a/src/BizHawk.Client.Common/movie/MovieService.cs +++ b/src/BizHawk.Client.Common/movie/MovieService.cs @@ -1,22 +1,10 @@ using System.Collections.Generic; -using System.IO; using System.Linq; namespace BizHawk.Client.Common { public static class MovieService { - public static IMovie Get(string path) - { - // TODO: change IMovies to take HawkFiles only and not path - if (Path.GetExtension(path)?.EndsWith("tasproj") ?? false) - { - return new TasMovie(path); - } - - return new Bk2Movie(path); - } - public static string StandardMovieExtension => Bk2Movie.Extension; public static string TasMovieExtension => TasMovie.Extension; diff --git a/src/BizHawk.Client.Common/movie/MovieSession.cs b/src/BizHawk.Client.Common/movie/MovieSession.cs index 15ae1a5f7a..9047d9d54a 100644 --- a/src/BizHawk.Client.Common/movie/MovieSession.cs +++ b/src/BizHawk.Client.Common/movie/MovieSession.cs @@ -245,7 +245,7 @@ namespace BizHawk.Client.Common public void RunQueuedMovie(bool recordMode, IEmulator emulator, IDictionary preferredCores) { - _queuedMovie.Attach(this, emulator); + _queuedMovie.Attach(emulator); foreach (var previousPref in _preferredCores) { preferredCores[previousPref.Key] = previousPref.Value; @@ -339,6 +339,18 @@ namespace BizHawk.Client.Common Movie.SwitchToPlay(); } + public IMovie Get(string path) + { + // TODO: change IMovies to take HawkFiles only and not path + if (Path.GetExtension(path)?.EndsWith("tasproj") ?? false) + { + return new TasMovie(this, path); + } + + return new Bk2Movie(this, path); + } + + private void ClearFrame() { if (Movie.IsPlayingOrFinished()) diff --git a/src/BizHawk.Client.Common/movie/bk2/Bk2Movie.IO.cs b/src/BizHawk.Client.Common/movie/bk2/Bk2Movie.IO.cs index 9c59115737..6a17cb2bac 100644 --- a/src/BizHawk.Client.Common/movie/bk2/Bk2Movie.IO.cs +++ b/src/BizHawk.Client.Common/movie/bk2/Bk2Movie.IO.cs @@ -160,7 +160,7 @@ namespace BizHawk.Client.Common SetCycleValues(); // We are pretending these only need to be set on save CreateDirectoryIfNotExists(fn); - using var bs = new ZipStateSaver(fn, Global.MovieSession.Settings.MovieCompressionLevel); + using var bs = new ZipStateSaver(fn, Session.Settings.MovieCompressionLevel); AddLumps(bs); if (!isBackup) diff --git a/src/BizHawk.Client.Common/movie/bk2/Bk2Movie.cs b/src/BizHawk.Client.Common/movie/bk2/Bk2Movie.cs index 4d358172b8..3e2fe2e632 100644 --- a/src/BizHawk.Client.Common/movie/bk2/Bk2Movie.cs +++ b/src/BizHawk.Client.Common/movie/bk2/Bk2Movie.cs @@ -9,18 +9,19 @@ namespace BizHawk.Client.Common { private Bk2Controller _adapter; - internal Bk2Movie(string filename) + internal Bk2Movie(IMovieSession session, string filename) { if (string.IsNullOrWhiteSpace(filename)) { throw new ArgumentNullException($"{nameof(filename)} can not be null."); } + Session = session; Filename = filename; Header[HeaderKeys.MovieVersion] = "BizHawk v2.0.0"; } - public virtual void Attach(IMovieSession session, IEmulator emulator) + public virtual void Attach(IEmulator emulator) { // TODO: this check would ideally happen // but is disabled for now because restarting a movie doesn't new one up @@ -32,11 +33,10 @@ namespace BizHawk.Client.Common //} Emulator = emulator; - Session = session; } public IEmulator Emulator { get; private set; } - public IMovieSession Session { get; private set; } + public IMovieSession Session { get; } protected bool MakeBackup { get; set; } = true; diff --git a/src/BizHawk.Client.Common/movie/import/IMovieImport.cs b/src/BizHawk.Client.Common/movie/import/IMovieImport.cs index 91b021336d..5d65aa6ee4 100644 --- a/src/BizHawk.Client.Common/movie/import/IMovieImport.cs +++ b/src/BizHawk.Client.Common/movie/import/IMovieImport.cs @@ -7,7 +7,7 @@ namespace BizHawk.Client.Common { public interface IMovieImport { - ImportResult Import(string path, Config config); + ImportResult Import(IMovieSession session, string path, Config config); } internal abstract class MovieImporter : IMovieImport @@ -16,7 +16,7 @@ namespace BizHawk.Client.Common protected const string Md5 = "MD5"; protected const string MovieOrigin = "MovieOrigin"; - public ImportResult Import(string path, Config config) + public ImportResult Import(IMovieSession session, string path, Config config) { SourceFile = new FileInfo(path); Config = config; @@ -28,7 +28,7 @@ namespace BizHawk.Client.Common } var newFileName = $"{SourceFile.FullName}.{Bk2Movie.Extension}"; - Result.Movie = MovieService.Get(newFileName); + Result.Movie = session.Get(newFileName); RunImport(); diff --git a/src/BizHawk.Client.Common/movie/import/MovieImport.cs b/src/BizHawk.Client.Common/movie/import/MovieImport.cs index c9696938fb..5c0ad2ebc2 100644 --- a/src/BizHawk.Client.Common/movie/import/MovieImport.cs +++ b/src/BizHawk.Client.Common/movie/import/MovieImport.cs @@ -30,7 +30,7 @@ namespace BizHawk.Client.Common ); // Attempt to import another type of movie file into a movie object. - public static ImportResult ImportFile(string path, Config config) + public static ImportResult ImportFile(IMovieSession session, string path, Config config) { string ext = Path.GetExtension(path) ?? ""; var importerType = ImporterForExtension(ext); @@ -47,7 +47,7 @@ namespace BizHawk.Client.Common return importer == null ? ImportResult.Error($"No importer found for file type {ext}") - : importer.Import(path, config); + : importer.Import(session, path, config); } private static Type ImporterForExtension(string ext) diff --git a/src/BizHawk.Client.Common/movie/interfaces/IMovie.cs b/src/BizHawk.Client.Common/movie/interfaces/IMovie.cs index b3436f49a4..15ae4a25d2 100644 --- a/src/BizHawk.Client.Common/movie/interfaces/IMovie.cs +++ b/src/BizHawk.Client.Common/movie/interfaces/IMovie.cs @@ -234,13 +234,18 @@ namespace BizHawk.Client.Common /// Thrown if attempting to attach a core when one is already attached /// or if the given core does not meet all required dependencies /// - void Attach(IMovieSession session, IEmulator emulator); + void Attach(IEmulator emulator); /// /// The currently attached core or null if not yet attached /// IEmulator Emulator { get; } + /// + /// The current movie session + /// + IMovieSession Session { get; } + IStringLog GetLogEntries(); void CopyLog(IEnumerable log); } diff --git a/src/BizHawk.Client.Common/movie/interfaces/IMovieSession.cs b/src/BizHawk.Client.Common/movie/interfaces/IMovieSession.cs index 95b5763bb6..c854d49101 100644 --- a/src/BizHawk.Client.Common/movie/interfaces/IMovieSession.cs +++ b/src/BizHawk.Client.Common/movie/interfaces/IMovieSession.cs @@ -61,8 +61,10 @@ namespace BizHawk.Client.Common void StopMovie(bool saveChanges = true); /// - /// If a movie is active, it will be converted to a + /// If a movie is active, it will be converted to a /// void ConvertToTasProj(); + + IMovie Get(string path); } } diff --git a/src/BizHawk.Client.Common/movie/tasproj/TasMovie.cs b/src/BizHawk.Client.Common/movie/tasproj/TasMovie.cs index aebe6a693e..f988f4556a 100644 --- a/src/BizHawk.Client.Common/movie/tasproj/TasMovie.cs +++ b/src/BizHawk.Client.Common/movie/tasproj/TasMovie.cs @@ -13,7 +13,7 @@ namespace BizHawk.Client.Common private IInputPollable _inputPollable; /// loaded core does not implement - internal TasMovie(string path) : base(path) + internal TasMovie(IMovieSession session, string path) : base(session, path) { Branches = new TasBranchCollection(this); ChangeLog = new TasMovieChangeLog(this); @@ -24,7 +24,7 @@ namespace BizHawk.Client.Common Markers.Add(0, "Power on"); } - public override void Attach(IMovieSession session, IEmulator emulator) + public override void Attach(IEmulator emulator) { if (!emulator.HasSavestates()) { @@ -39,7 +39,7 @@ namespace BizHawk.Client.Common _inputPollable = emulator.AsInputPollable(); TasStateManager.Attach(emulator); - base.Attach(session, emulator); + base.Attach(emulator); } public override bool StartsFromSavestate diff --git a/src/BizHawk.Client.EmuHawk/MainForm.FileLoader.cs b/src/BizHawk.Client.EmuHawk/MainForm.FileLoader.cs index bf4fc5098d..34b5991c35 100644 --- a/src/BizHawk.Client.EmuHawk/MainForm.FileLoader.cs +++ b/src/BizHawk.Client.EmuHawk/MainForm.FileLoader.cs @@ -102,7 +102,7 @@ namespace BizHawk.Client.EmuHawk return; } - StartNewMovie(MovieService.Get(filename), false); + StartNewMovie(MovieSession.Get(filename), false); } private void LoadRom(string filename, string archive = null) diff --git a/src/BizHawk.Client.EmuHawk/MainForm.cs b/src/BizHawk.Client.EmuHawk/MainForm.cs index 894428caf7..0e28759ad5 100644 --- a/src/BizHawk.Client.EmuHawk/MainForm.cs +++ b/src/BizHawk.Client.EmuHawk/MainForm.cs @@ -496,7 +496,7 @@ namespace BizHawk.Client.EmuHawk // If user picked a game, then do the commandline logic if (!Game.IsNullInstance()) { - var movie = MovieService.Get(_argParser.cmdMovie); + var movie = MovieSession.Get(_argParser.cmdMovie); MovieSession.ReadOnly = true; // if user is dumping and didn't supply dump length, make it as long as the loaded movie @@ -531,7 +531,7 @@ namespace BizHawk.Client.EmuHawk { if (File.Exists(Config.RecentMovies.MostRecent)) { - StartNewMovie(MovieService.Get(Config.RecentMovies.MostRecent), false); + StartNewMovie(MovieSession.Get(Config.RecentMovies.MostRecent), false); } else { @@ -2065,7 +2065,7 @@ namespace BizHawk.Client.EmuHawk { if (File.Exists(path)) { - var movie = MovieService.Get(path); + var movie = MovieSession.Get(path); MovieSession.ReadOnly = true; StartNewMovie(movie, false); } @@ -4015,7 +4015,7 @@ namespace BizHawk.Client.EmuHawk private void ProcessMovieImport(string fn, bool start) { - var result = MovieImport.ImportFile(fn, Config); + var result = MovieImport.ImportFile(MovieSession, fn, Config); if (result.Errors.Any()) { diff --git a/src/BizHawk.Client.EmuHawk/movie/PlayMovie.cs b/src/BizHawk.Client.EmuHawk/movie/PlayMovie.cs index ee46cf52c0..e541188967 100644 --- a/src/BizHawk.Client.EmuHawk/movie/PlayMovie.cs +++ b/src/BizHawk.Client.EmuHawk/movie/PlayMovie.cs @@ -128,7 +128,7 @@ namespace BizHawk.Client.EmuHawk private IMovie PreLoadMovieFile(HawkFile hf, bool force) { - var movie = MovieService.Get(hf.CanonicalFullPath); + var movie = _movieSession.Get(hf.CanonicalFullPath); try { diff --git a/src/BizHawk.Client.EmuHawk/movie/RecordMovie.cs b/src/BizHawk.Client.EmuHawk/movie/RecordMovie.cs index 15b7733e1c..879b0014de 100644 --- a/src/BizHawk.Client.EmuHawk/movie/RecordMovie.cs +++ b/src/BizHawk.Client.EmuHawk/movie/RecordMovie.cs @@ -92,7 +92,7 @@ namespace BizHawk.Client.EmuHawk } } - var movieToRecord = MovieService.Get(path); + var movieToRecord = _movieSession.Get(path); var fileInfo = new FileInfo(path); if (!fileInfo.Exists) diff --git a/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.MenuItems.cs b/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.MenuItems.cs index a104a9262f..bf3dd32f4a 100644 --- a/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.MenuItems.cs +++ b/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.MenuItems.cs @@ -121,7 +121,7 @@ namespace BizHawk.Client.EmuHawk if (result1.IsOk()) { _initializing = true; // Starting a new movie causes a core reboot - MainForm.StartNewMovie(MovieService.Get(ofd.FileName), false); + MainForm.StartNewMovie(MovieSession.Get(ofd.FileName), false); ConvertCurrentMovieToTasproj(); _initialized = false; StartNewMovieWrapper(CurrentTasMovie); diff --git a/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.cs b/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.cs index 89f1e20f14..b84fecd4ea 100644 --- a/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.cs +++ b/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.cs @@ -622,7 +622,7 @@ namespace BizHawk.Client.EmuHawk return false; } - var newMovie = (ITasMovie)MovieService.Get(file.FullName); + var newMovie = (ITasMovie)MovieSession.Get(file.FullName); newMovie.BindMarkersToInput = Settings.BindMarkersToInput; newMovie.TasStateManager.InvalidateCallback = GreenzoneInvalidated; @@ -677,7 +677,7 @@ namespace BizHawk.Client.EmuHawk } var filename = DefaultTasProjName(); // TODO don't do this, take over any mainform actions that can crash without a filename - var tasMovie = (ITasMovie)MovieService.Get(filename); + var tasMovie = (ITasMovie)MovieSession.Get(filename); tasMovie.BindMarkersToInput = Settings.BindMarkersToInput;