From 7e9b13f5c33d72b874a5c9a436fbe6bb1ff9990f Mon Sep 17 00:00:00 2001 From: adelikat Date: Sun, 8 Jun 2014 19:36:33 +0000 Subject: [PATCH] Some movie refactoring - moving a function into the importer class, remove MovieExtension from the config file and instead add it as a movie property, add some todo comments in places regarding movie 2.0 --- BizHawk.Client.Common/config/Config.cs | 1 - BizHawk.Client.Common/movie/IMovie.cs | 5 ++ BizHawk.Client.Common/movie/Movie.cs | 6 +- BizHawk.Client.Common/movie/MovieImport.cs | 63 ++++++++++++++++----- BizHawk.Client.Common/movie/MovieSession.cs | 3 +- BizHawk.Client.Common/movie/TasMovie.cs | 2 + BizHawk.Client.EmuHawk/MainForm.Events.cs | 4 +- BizHawk.Client.EmuHawk/MainForm.cs | 33 ++--------- BizHawk.Client.EmuHawk/movie/PlayMovie.cs | 9 +-- BizHawk.Client.EmuHawk/movie/RecordMovie.cs | 6 +- 10 files changed, 79 insertions(+), 53 deletions(-) diff --git a/BizHawk.Client.Common/config/Config.cs b/BizHawk.Client.Common/config/Config.cs index e71a8ed4da..c18ed6019e 100644 --- a/BizHawk.Client.Common/config/Config.cs +++ b/BizHawk.Client.Common/config/Config.cs @@ -89,7 +89,6 @@ namespace BizHawk.Client.Common public bool AutoLoadLastSaveSlot = false; public bool WIN32_CONSOLE = true; public bool SkipLagFrame = false; - public string MovieExtension = "bkm"; public bool SupressAskSave = false; public bool AVI_CaptureOSD = false; public bool Screenshot_CaptureOSD = false; diff --git a/BizHawk.Client.Common/movie/IMovie.cs b/BizHawk.Client.Common/movie/IMovie.cs index eebf1daa8c..4567af8ef9 100644 --- a/BizHawk.Client.Common/movie/IMovie.cs +++ b/BizHawk.Client.Common/movie/IMovie.cs @@ -43,6 +43,11 @@ namespace BizHawk.Client.Common /// int InputLogLength { get; } + /// + /// Returns the file extension for this implementation + /// + string PreferredExtension { get; } + IMovieHeader Header { get; } #endregion diff --git a/BizHawk.Client.Common/movie/Movie.cs b/BizHawk.Client.Common/movie/Movie.cs index 4aa615ed61..2cc6e75b4e 100644 --- a/BizHawk.Client.Common/movie/Movie.cs +++ b/BizHawk.Client.Common/movie/Movie.cs @@ -2,8 +2,8 @@ using System.Globalization; using System.IO; using System.Text; -using BizHawk.Common; +using BizHawk.Common; using BizHawk.Emulation.Common; namespace BizHawk.Client.Common @@ -42,6 +42,10 @@ namespace BizHawk.Client.Common #region Properties + public string PreferredExtension { get { return "bkm"; } } + + public static string Extension { get { return "bkm"; } } + public IMovieHeader Header { get; private set; } public bool MakeBackup { get; set; } diff --git a/BizHawk.Client.Common/movie/MovieImport.cs b/BizHawk.Client.Common/movie/MovieImport.cs index 5ca3746004..a48ff5b1b2 100644 --- a/BizHawk.Client.Common/movie/MovieImport.cs +++ b/BizHawk.Client.Common/movie/MovieImport.cs @@ -11,6 +11,7 @@ namespace BizHawk.Client.Common { public static class MovieImport { + // Movies 2.0 TODO: this is Movie.cs specific, can it be IMovie based? If not, needs to be refactored to a hardcoded 2.0 implementation, client needs to know what kind of type it imported to, or the mainform method needs to be moved here public const string COMMENT = "comment"; public const string COREORIGIN = "CoreOrigin"; public const string CRC16 = "CRC16"; @@ -31,14 +32,46 @@ namespace BizHawk.Client.Common public const string SYNCHACK = "SyncHack"; public const string UNITCODE = "UnitCode"; + public static void ProcessMovieImport(string fn, Action conversionErrorCallback, Action messageCallback) + { + var d = PathManager.MakeAbsolutePath(Global.Config.PathEntries.MoviesPathFragment, null); + string errorMsg; + string warningMsg; + var m = ImportFile(fn, out errorMsg, out warningMsg); + + if (!string.IsNullOrWhiteSpace(errorMsg)) + { + conversionErrorCallback(errorMsg); + } + + if (!string.IsNullOrWhiteSpace(warningMsg)) + { + messageCallback(warningMsg); + + } + else + { + messageCallback(Path.GetFileName(fn) + " imported as " + "Movies\\" + + Path.GetFileName(fn) + "." + Global.MovieSession.Movie.PreferredExtension); + } + + if (!Directory.Exists(d)) + { + Directory.CreateDirectory(d); + } + + var outPath = Path.Combine(d, Path.GetFileName(fn) + "." + Global.MovieSession.Movie.PreferredExtension); + m.SaveAs(outPath); + } + // Attempt to import another type of movie file into a movie object. public static Movie ImportFile(string path, out string errorMsg, out string warningMsg) { Movie m = new Movie(); - errorMsg = String.Empty; - warningMsg = String.Empty; + errorMsg = string.Empty; + warningMsg = string.Empty; - string ext = path != null ? Path.GetExtension(path).ToUpper() : String.Empty; + string ext = path != null ? Path.GetExtension(path).ToUpper() : string.Empty; try { switch (ext) @@ -293,7 +326,7 @@ namespace BizHawk.Client.Common private static Movie ImportText(string path, out string errorMsg, out string warningMsg) { errorMsg = warningMsg = String.Empty; - Movie m = new Movie(path + "." + Global.Config.MovieExtension); + Movie m = new Movie(path + "." + Movie.Extension); FileInfo file = new FileInfo(path); StreamReader sr = file.OpenText(); string emulator = String.Empty; @@ -489,7 +522,7 @@ namespace BizHawk.Client.Common private static Movie ImportFCM(string path, out string errorMsg, out string warningMsg) { errorMsg = warningMsg = String.Empty; - Movie m = new Movie(path + "." + Global.Config.MovieExtension); + Movie m = new Movie(path + "." + Movie.Extension); FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); BinaryReader r = new BinaryReader(fs); // 000 4-byte signature: 46 43 4D 1A "FCM\x1A" @@ -741,7 +774,7 @@ namespace BizHawk.Client.Common private static Movie ImportFMV(string path, out string errorMsg, out string warningMsg) { errorMsg = warningMsg = String.Empty; - Movie m = new Movie(path + "." + Global.Config.MovieExtension); + Movie m = new Movie(path + "." + Movie.Extension); FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); BinaryReader r = new BinaryReader(fs); // 000 4-byte signature: 46 4D 56 1A "FMV\x1A" @@ -881,7 +914,7 @@ namespace BizHawk.Client.Common private static Movie ImportGMV(string path, out string errorMsg, out string warningMsg) { errorMsg = warningMsg = String.Empty; - Movie m = new Movie(path + "." + Global.Config.MovieExtension); + Movie m = new Movie(path + "." + Movie.Extension); FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); BinaryReader r = new BinaryReader(fs); // 000 16-byte signature and format version: "Gens Movie TEST9" @@ -1005,7 +1038,7 @@ namespace BizHawk.Client.Common private static Movie ImportLSMV(string path, out string errorMsg, out string warningMsg) { errorMsg = warningMsg = String.Empty; - Movie m = new Movie(path + "." + Global.Config.MovieExtension); + Movie m = new Movie(path + "." + Movie.Extension); HawkFile hf = new HawkFile(path); // .LSMV movies are .zip files containing data files. if (!hf.IsArchive) @@ -1237,7 +1270,7 @@ namespace BizHawk.Client.Common private static Movie ImportMCM(string path, out string errorMsg, out string warningMsg) { errorMsg = warningMsg = String.Empty; - Movie m = new Movie(path + "." + Global.Config.MovieExtension); + Movie m = new Movie(path + "." + Movie.Extension); FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); BinaryReader r = new BinaryReader(fs); // 000 8-byte "MDFNMOVI" signature @@ -1358,7 +1391,7 @@ namespace BizHawk.Client.Common private static Movie ImportMMV(string path, out string errorMsg, out string warningMsg) { errorMsg = warningMsg = String.Empty; - Movie m = new Movie(path + "." + Global.Config.MovieExtension); + Movie m = new Movie(path + "." + Movie.Extension); FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); BinaryReader r = new BinaryReader(fs); // 0000: 4-byte signature: "MMV\0" @@ -1472,7 +1505,7 @@ namespace BizHawk.Client.Common private static Movie ImportNMV(string path, out string errorMsg, out string warningMsg) { errorMsg = warningMsg = String.Empty; - Movie m = new Movie(path + "." + Global.Config.MovieExtension); + Movie m = new Movie(path + "." + Movie.Extension); FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); BinaryReader r = new BinaryReader(fs); // 000 4-byte signature: 4E 53 53 1A "NSS\x1A" @@ -1699,7 +1732,7 @@ namespace BizHawk.Client.Common private static Movie ImportSMV(string path, out string errorMsg, out string warningMsg) { errorMsg = warningMsg = String.Empty; - Movie m = new Movie(path + "." + Global.Config.MovieExtension); + Movie m = new Movie(path + "." + Movie.Extension); FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); BinaryReader r = new BinaryReader(fs); // 000 4-byte signature: 53 4D 56 1A "SMV\x1A" @@ -1970,7 +2003,7 @@ namespace BizHawk.Client.Common private static Movie ImportVBM(string path, out string errorMsg, out string warningMsg) { errorMsg = warningMsg = String.Empty; - Movie m = new Movie(path + "." + Global.Config.MovieExtension); + Movie m = new Movie(path + "." + Movie.Extension); FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); BinaryReader r = new BinaryReader(fs); // 000 4-byte signature: 56 42 4D 1A "VBM\x1A" @@ -2240,7 +2273,7 @@ namespace BizHawk.Client.Common private static Movie ImportVMV(string path, out string errorMsg, out string warningMsg) { errorMsg = warningMsg = String.Empty; - Movie m = new Movie(path + "." + Global.Config.MovieExtension); + Movie m = new Movie(path + "." + Movie.Extension); FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); BinaryReader r = new BinaryReader(fs); // 000 12-byte signature: "VirtuaNES MV" @@ -2464,7 +2497,7 @@ namespace BizHawk.Client.Common private static Movie ImportZMV(string path, out string errorMsg, out string warningMsg) { errorMsg = warningMsg = String.Empty; - Movie m = new Movie(path + "." + Global.Config.MovieExtension); + Movie m = new Movie(path + "." + Movie.Extension); FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); BinaryReader r = new BinaryReader(fs); // 000 3-byte signature: 5A 4D 56 "ZMV" diff --git a/BizHawk.Client.Common/movie/MovieSession.cs b/BizHawk.Client.Common/movie/MovieSession.cs index ea07648101..c4c6eb17cc 100644 --- a/BizHawk.Client.Common/movie/MovieSession.cs +++ b/BizHawk.Client.Common/movie/MovieSession.cs @@ -33,7 +33,8 @@ namespace BizHawk.Client.Common public static bool IsValidMovieExtension(string ext) { - if (ext.ToUpper() == "." + Global.Config.MovieExtension) + // Movies 2.0 TODO + if (ext.ToUpper() == "." + Global.MovieSession.Movie.PreferredExtension) { return true; } diff --git a/BizHawk.Client.Common/movie/TasMovie.cs b/BizHawk.Client.Common/movie/TasMovie.cs index 25ea42c9c4..18df417eb4 100644 --- a/BizHawk.Client.Common/movie/TasMovie.cs +++ b/BizHawk.Client.Common/movie/TasMovie.cs @@ -31,6 +31,8 @@ namespace BizHawk.Client.Common } } + public string PreferredExtension { get { return "tasproj"; } } + public void ToggleButton(int frame, string buttonName) { InvalidateGreenzone(frame); diff --git a/BizHawk.Client.EmuHawk/MainForm.Events.cs b/BizHawk.Client.EmuHawk/MainForm.Events.cs index 0387e36b13..312ee20bbf 100644 --- a/BizHawk.Client.EmuHawk/MainForm.Events.cs +++ b/BizHawk.Client.EmuHawk/MainForm.Events.cs @@ -2209,6 +2209,8 @@ namespace BizHawk.Client.EmuHawk else if (MovieImport.IsValidMovieExtension(Path.GetExtension(filePaths[0]))) { + // Movies 2.0 TODO: rethink this method + //tries to open a legacy movie format as if it were a BKM, by importing it if (CurrentlyOpenRom == null) { @@ -2231,7 +2233,7 @@ namespace BizHawk.Client.EmuHawk //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: - movie.Filename += ".autoimported." + Global.Config.MovieExtension; + movie.Filename += ".autoimported." + Movie.Extension; movie.Save(); StartNewMovie(movie, false); } diff --git a/BizHawk.Client.EmuHawk/MainForm.cs b/BizHawk.Client.EmuHawk/MainForm.cs index a20a5d350f..0c9606f532 100644 --- a/BizHawk.Client.EmuHawk/MainForm.cs +++ b/BizHawk.Client.EmuHawk/MainForm.cs @@ -3184,35 +3184,14 @@ namespace BizHawk.Client.EmuHawk } } + private static void ShowConversionError(string errorMsg) + { + MessageBox.Show(errorMsg, "Conversion error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + private static void ProcessMovieImport(string fn) // Nothing Winform Specific here, move to Movie import { - var d = PathManager.MakeAbsolutePath(Global.Config.PathEntries.MoviesPathFragment, null); - string errorMsg; - string warningMsg; - var m = MovieImport.ImportFile(fn, out errorMsg, out warningMsg); - - if (!String.IsNullOrWhiteSpace(errorMsg)) - { - MessageBox.Show(errorMsg, "Conversion error", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - - if (!String.IsNullOrWhiteSpace(warningMsg)) - { - GlobalWin.OSD.AddMessage(warningMsg); - } - else - { - GlobalWin.OSD.AddMessage(Path.GetFileName(fn) + " imported as " + "Movies\\" + - Path.GetFileName(fn) + "." + Global.Config.MovieExtension); - } - - if (!Directory.Exists(d)) - { - Directory.CreateDirectory(d); - } - - var outPath = Path.Combine(d, Path.GetFileName(fn) + "." + Global.Config.MovieExtension); - m.SaveAs(outPath); + MovieImport.ProcessMovieImport(fn, ShowConversionError, GlobalWin.OSD.AddMessage); } #endregion diff --git a/BizHawk.Client.EmuHawk/movie/PlayMovie.cs b/BizHawk.Client.EmuHawk/movie/PlayMovie.cs index 1da0b0b7af..cebd54a68b 100644 --- a/BizHawk.Client.EmuHawk/movie/PlayMovie.cs +++ b/BizHawk.Client.EmuHawk/movie/PlayMovie.cs @@ -14,6 +14,7 @@ namespace BizHawk.Client.EmuHawk { public partial class PlayMovie : Form { + // Movies 2.0 TODO: this is hopelessly Movie.cs specific, to make it generic, make a MovieLoader class that receives a path and returns an IMovie, that does the logic of determining bkm, bk2, or tasproj private List _movieList = new List(); private bool _sortReverse; private string _sortedCol; @@ -203,7 +204,7 @@ namespace BizHawk.Client.EmuHawk var tas = new List(); for (var i = 0; i < indices.Count; i++) { - if (Path.GetExtension(_movieList[indices[i]].Filename).ToUpper() == "." + Global.Config.MovieExtension) + if (Path.GetExtension(_movieList[indices[i]].Filename).ToUpper() == "." + Movie.Extension) { tas.Add(i); } @@ -272,7 +273,7 @@ namespace BizHawk.Client.EmuHawk dpTodo.Enqueue(subdir); //add movies - fpTodo.AddRange(Directory.GetFiles(dp, "*." + Global.Config.MovieExtension)); + fpTodo.AddRange(Directory.GetFiles(dp, "*." + Movie.Extension)); //add states if requested if (Global.Config.PlayMovie_ShowStateFiles) @@ -315,7 +316,7 @@ namespace BizHawk.Client.EmuHawk var filePaths = (string[])e.Data.GetData(DataFormats.FileDrop); filePaths - .Where(path => Path.GetExtension(path) == "." + Global.Config.MovieExtension) + .Where(path => Path.GetExtension(path) == "." + Movie.Extension) .ToList() .ForEach(path => AddMovieToList(path, force: true)); @@ -623,7 +624,7 @@ namespace BizHawk.Client.EmuHawk { var ofd = new OpenFileDialog { - Filter = "Movie Files (*." + Global.Config.MovieExtension + ")|*." + Global.Config.MovieExtension + "|Savestates|*.state|All Files|*.*", + Filter = "Movie Files (*." + Movie.Extension + ")|*." + Movie.Extension + "|Savestates|*.state|All Files|*.*", InitialDirectory = PathManager.MakeAbsolutePath(Global.Config.PathEntries.MoviesPathFragment, null) }; diff --git a/BizHawk.Client.EmuHawk/movie/RecordMovie.cs b/BizHawk.Client.EmuHawk/movie/RecordMovie.cs index 269baa38fe..509004b02d 100644 --- a/BizHawk.Client.EmuHawk/movie/RecordMovie.cs +++ b/BizHawk.Client.EmuHawk/movie/RecordMovie.cs @@ -47,7 +47,7 @@ namespace BizHawk.Client.EmuHawk if (path[path.Length - 4] != '.') // If no file extension, add movie extension { - path += "." + Global.Config.MovieExtension; + path += "." + Global.MovieSession.Movie.PreferredExtension; } return path; @@ -169,11 +169,11 @@ namespace BizHawk.Client.EmuHawk var sfd = new SaveFileDialog { InitialDirectory = PathManager.MakeAbsolutePath(Global.Config.PathEntries.MoviesPathFragment, null), - DefaultExt = "." + Global.Config.MovieExtension, + DefaultExt = "." + Global.MovieSession.Movie.PreferredExtension, FileName = RecordBox.Text, OverwritePrompt = false }; - var filter = "Movie Files (*." + Global.Config.MovieExtension + ")|*." + Global.Config.MovieExtension + "|Savestates|*.state|All Files|*.*"; + var filter = "Movie Files (*." + Global.MovieSession.Movie.PreferredExtension + ")|*." + Global.MovieSession.Movie.PreferredExtension + "|Savestates|*.state|All Files|*.*"; sfd.Filter = filter; var result = sfd.ShowHawkDialog();