From 3de87af5dedd4a2ff7eb5bbb8df1c1c28a14e75b Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Sat, 28 Nov 2020 22:48:02 +1000 Subject: [PATCH] Pass MovieSession to APIs via ctor --- src/BizHawk.Client.EmuHawk/Api/ApiManager.cs | 14 +++-- .../Api/Libraries/JoypadApi.cs | 10 ++- .../Api/Libraries/MovieApi.cs | 62 ++++++++++--------- src/BizHawk.Client.EmuHawk/Api/UserDataApi.cs | 17 +++-- .../tools/Lua/Win32LuaLibraries.cs | 2 +- .../tools/ToolManager.cs | 4 +- 6 files changed, 67 insertions(+), 42 deletions(-) diff --git a/src/BizHawk.Client.EmuHawk/Api/ApiManager.cs b/src/BizHawk.Client.EmuHawk/Api/ApiManager.cs index d0aeeaa621..d72c11fd20 100644 --- a/src/BizHawk.Client.EmuHawk/Api/ApiManager.cs +++ b/src/BizHawk.Client.EmuHawk/Api/ApiManager.cs @@ -24,7 +24,9 @@ namespace BizHawk.Client.EmuHawk private static readonly Type[] _ctorParamTypesB = { typeof(Action), typeof(IMainFormForApi) }; - private static readonly Type[] _ctorParamTypesC = { typeof(Action), typeof(IMainFormForApi), typeof(DisplayManager), typeof(InputManager), typeof(Config), typeof(IEmulator), typeof(IGameInfo) }; + private static readonly Type[] _ctorParamTypesC = { typeof(Action), typeof(IMovieSession) }; + + private static readonly Type[] _ctorParamTypesD = { typeof(Action), typeof(IMainFormForApi), typeof(DisplayManager), typeof(InputManager), typeof(Config), typeof(IEmulator), typeof(IGameInfo) }; private static readonly Type[] _ctorParamTypesTools = { typeof(ToolManager) }; @@ -39,6 +41,7 @@ namespace BizHawk.Client.EmuHawk IMainFormForApi mainForm, DisplayManager displayManager, InputManager inputManager, + IMovieSession movieSession, ToolManager toolManager, Config config, IEmulator emulator, @@ -48,7 +51,8 @@ namespace BizHawk.Client.EmuHawk .ToDictionary( t => _apiTypes[t], t => (IExternalApi) ( - t.GetConstructor(_ctorParamTypesC)?.Invoke(new object[] { logCallback, mainForm, displayManager, inputManager, config, emulator, game }) + t.GetConstructor(_ctorParamTypesD)?.Invoke(new object[] { logCallback, mainForm, displayManager, inputManager, config, emulator, game }) + ?? t.GetConstructor(_ctorParamTypesC)?.Invoke(new object[] { logCallback, movieSession }) ?? t.GetConstructor(_ctorParamTypesB)?.Invoke(new object[] { logCallback, mainForm }) ?? t.GetConstructor(_ctorParamTypesA)?.Invoke(new object[] { logCallback }) ?? t.GetConstructor(_ctorParamTypesTools)?.Invoke(new object[] { toolManager }) @@ -64,12 +68,13 @@ namespace BizHawk.Client.EmuHawk IMainFormForApi mainForm, DisplayManager displayManager, InputManager inputManager, + IMovieSession movieSession, ToolManager toolManager, Config config, IEmulator emulator, IGameInfo game) { - _container = Register(serviceProvider, Console.WriteLine, mainForm, displayManager, inputManager, toolManager, config, emulator, game); + _container = Register(serviceProvider, Console.WriteLine, mainForm, displayManager, inputManager, movieSession, toolManager, config, emulator, game); ClientApi.EmuClient = _container.EmuClient; return new BasicApiProvider(_container); } @@ -80,10 +85,11 @@ namespace BizHawk.Client.EmuHawk IMainFormForApi mainForm, DisplayManager displayManager, InputManager inputManager, + IMovieSession movieSession, ToolManager toolManager, Config config, IEmulator emulator, IGameInfo game - ) => _luaContainer = Register(serviceProvider, logCallback, mainForm, displayManager, inputManager, toolManager, config, emulator, game); + ) => _luaContainer = Register(serviceProvider, logCallback, mainForm, displayManager, inputManager, movieSession, toolManager, config, emulator, game); } } diff --git a/src/BizHawk.Client.EmuHawk/Api/Libraries/JoypadApi.cs b/src/BizHawk.Client.EmuHawk/Api/Libraries/JoypadApi.cs index 09748fa5a9..cd45bd9abb 100644 --- a/src/BizHawk.Client.EmuHawk/Api/Libraries/JoypadApi.cs +++ b/src/BizHawk.Client.EmuHawk/Api/Libraries/JoypadApi.cs @@ -7,9 +7,15 @@ namespace BizHawk.Client.EmuHawk { public sealed class JoypadApi : IJoypadApi { + private readonly IMovieSession _movieSession; + private readonly Action LogCallback; - public JoypadApi(Action logCallback) => LogCallback = logCallback; + public JoypadApi(Action logCallback, IMovieSession movieSession) + { + LogCallback = logCallback; + _movieSession = movieSession; + } public IDictionary Get(int? controller = null) { @@ -23,7 +29,7 @@ namespace BizHawk.Client.EmuHawk public void SetFromMnemonicStr(string inputLogEntry) { - var controller = GlobalWin.MovieSession.GenerateMovieController(); + var controller = _movieSession.GenerateMovieController(); try { controller.SetFromMnemonic(inputLogEntry); diff --git a/src/BizHawk.Client.EmuHawk/Api/Libraries/MovieApi.cs b/src/BizHawk.Client.EmuHawk/Api/Libraries/MovieApi.cs index a1ce887684..049ca60373 100644 --- a/src/BizHawk.Client.EmuHawk/Api/Libraries/MovieApi.cs +++ b/src/BizHawk.Client.EmuHawk/Api/Libraries/MovieApi.cs @@ -9,22 +9,28 @@ namespace BizHawk.Client.EmuHawk { public sealed class MovieApi : IMovieApi { + private readonly IMovieSession _movieSession; + private readonly Action LogCallback; - public MovieApi(Action logCallback) => LogCallback = logCallback; + public MovieApi(Action logCallback, IMovieSession movieSession) + { + LogCallback = logCallback; + _movieSession = movieSession; + } - public bool StartsFromSavestate() => GlobalWin.MovieSession.Movie.IsActive() && GlobalWin.MovieSession.Movie.StartsFromSavestate; + public bool StartsFromSavestate() => _movieSession.Movie.IsActive() && _movieSession.Movie.StartsFromSavestate; - public bool StartsFromSaveram() => GlobalWin.MovieSession.Movie.IsActive() && GlobalWin.MovieSession.Movie.StartsFromSaveRam; + public bool StartsFromSaveram() => _movieSession.Movie.IsActive() && _movieSession.Movie.StartsFromSaveRam; public IDictionary GetInput(int frame, int? controller = null) { - if (GlobalWin.MovieSession.Movie.NotActive()) + if (_movieSession.Movie.NotActive()) { LogCallback("No movie loaded"); return null; } - var adapter = GlobalWin.MovieSession.Movie.GetInputState(frame); + var adapter = _movieSession.Movie.GetInputState(frame); if (adapter == null) { LogCallback("Can't get input of the last frame of the movie. Use the previous frame"); @@ -36,79 +42,79 @@ namespace BizHawk.Client.EmuHawk public string GetInputAsMnemonic(int frame) { - if (GlobalWin.MovieSession.Movie.NotActive() || frame >= GlobalWin.MovieSession.Movie.InputLogLength) + if (_movieSession.Movie.NotActive() || frame >= _movieSession.Movie.InputLogLength) { return string.Empty; } - var lg = GlobalWin.MovieSession.Movie.LogGeneratorInstance( - GlobalWin.MovieSession.Movie.GetInputState(frame)); + var lg = _movieSession.Movie.LogGeneratorInstance( + _movieSession.Movie.GetInputState(frame)); return lg.GenerateLogEntry(); } public void Save(string filename = null) { - if (GlobalWin.MovieSession.Movie.NotActive()) + if (_movieSession.Movie.NotActive()) { return; } if (!string.IsNullOrEmpty(filename)) { - filename += $".{GlobalWin.MovieSession.Movie.PreferredExtension}"; + filename += $".{_movieSession.Movie.PreferredExtension}"; if (new FileInfo(filename).Exists) { LogCallback($"File {filename} already exists, will not overwrite"); return; } - GlobalWin.MovieSession.Movie.Filename = filename; + _movieSession.Movie.Filename = filename; } - GlobalWin.MovieSession.Movie.Save(); + _movieSession.Movie.Save(); } public Dictionary GetHeader() { var table = new Dictionary(); - if (GlobalWin.MovieSession.Movie.NotActive()) + if (_movieSession.Movie.NotActive()) { return table; } - foreach (var kvp in GlobalWin.MovieSession.Movie.HeaderEntries) table[kvp.Key] = kvp.Value; + foreach (var kvp in _movieSession.Movie.HeaderEntries) table[kvp.Key] = kvp.Value; return table; } - public List GetComments() => GlobalWin.MovieSession.Movie.Comments.ToList(); + public List GetComments() => _movieSession.Movie.Comments.ToList(); public List GetSubtitles() => - GlobalWin.MovieSession.Movie.Subtitles + _movieSession.Movie.Subtitles .Select(s => s.ToString()) .ToList(); - public string Filename() => GlobalWin.MovieSession.Movie.Filename; + public string Filename() => _movieSession.Movie.Filename; - public bool GetReadOnly() => GlobalWin.MovieSession.ReadOnly; + public bool GetReadOnly() => _movieSession.ReadOnly; - public ulong GetRerecordCount() => GlobalWin.MovieSession.Movie.Rerecords; + public ulong GetRerecordCount() => _movieSession.Movie.Rerecords; - public bool GetRerecordCounting() => GlobalWin.MovieSession.Movie.IsCountingRerecords; + public bool GetRerecordCounting() => _movieSession.Movie.IsCountingRerecords; - public bool IsLoaded() => GlobalWin.MovieSession.Movie.IsActive(); + public bool IsLoaded() => _movieSession.Movie.IsActive(); - public int Length() => GlobalWin.MovieSession.Movie.FrameCount; + public int Length() => _movieSession.Movie.FrameCount; - public string Mode() => (GlobalWin.MovieSession.Movie?.Mode ?? MovieMode.Inactive).ToString().ToUpper(); + public string Mode() => (_movieSession.Movie?.Mode ?? MovieMode.Inactive).ToString().ToUpper(); - public void SetReadOnly(bool readOnly) => GlobalWin.MovieSession.ReadOnly = readOnly; + public void SetReadOnly(bool readOnly) => _movieSession.ReadOnly = readOnly; - public void SetRerecordCount(ulong count) => GlobalWin.MovieSession.Movie.Rerecords = count; + public void SetRerecordCount(ulong count) => _movieSession.Movie.Rerecords = count; - public void SetRerecordCounting(bool counting) => GlobalWin.MovieSession.Movie.IsCountingRerecords = counting; + public void SetRerecordCounting(bool counting) => _movieSession.Movie.IsCountingRerecords = counting; - public void Stop() => GlobalWin.MovieSession.StopMovie(); + public void Stop() => _movieSession.StopMovie(); public double GetFps() { - var movie = GlobalWin.MovieSession.Movie; + var movie = _movieSession.Movie; // Why does it need the movie to be active to know the frame rate? if (movie.NotActive()) { diff --git a/src/BizHawk.Client.EmuHawk/Api/UserDataApi.cs b/src/BizHawk.Client.EmuHawk/Api/UserDataApi.cs index 8943bff373..0b231e62dc 100644 --- a/src/BizHawk.Client.EmuHawk/Api/UserDataApi.cs +++ b/src/BizHawk.Client.EmuHawk/Api/UserDataApi.cs @@ -5,6 +5,13 @@ namespace BizHawk.Client.EmuHawk { public sealed class UserDataApi : IUserDataApi { + private readonly IMovieSession _movieSession; + + public UserDataApi(Action logCallback, IMovieSession movieSession) + { + _movieSession = movieSession; + } + /// type of cannot be used in userdata public void Set(string name, object value) { @@ -13,15 +20,15 @@ namespace BizHawk.Client.EmuHawk var t = value.GetType(); if (!t.IsPrimitive && t != typeof(string)) throw new InvalidOperationException("Invalid type for userdata"); } - GlobalWin.MovieSession.UserBag[name] = value; + _movieSession.UserBag[name] = value; } - public object Get(string key) => GlobalWin.MovieSession.UserBag.TryGetValue(key, out var value) ? value : null; + public object Get(string key) => _movieSession.UserBag.TryGetValue(key, out var value) ? value : null; - public void Clear() => GlobalWin.MovieSession.UserBag.Clear(); + public void Clear() => _movieSession.UserBag.Clear(); - public bool Remove(string key) => GlobalWin.MovieSession.UserBag.Remove(key); + public bool Remove(string key) => _movieSession.UserBag.Remove(key); - public bool ContainsKey(string key) => GlobalWin.MovieSession.UserBag.ContainsKey(key); + public bool ContainsKey(string key) => _movieSession.UserBag.ContainsKey(key); } } diff --git a/src/BizHawk.Client.EmuHawk/tools/Lua/Win32LuaLibraries.cs b/src/BizHawk.Client.EmuHawk/tools/Lua/Win32LuaLibraries.cs index d1ca858e05..8c1c1edaf5 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Lua/Win32LuaLibraries.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Lua/Win32LuaLibraries.cs @@ -49,7 +49,7 @@ namespace BizHawk.Client.EmuHawk LuaWait = new AutoResetEvent(false); Docs.Clear(); - var apiContainer = ApiManager.RestartLua(serviceProvider, LogToLuaConsole, mainForm, displayManager, inputManager, mainForm.Tools, config, emulator, game); + var apiContainer = ApiManager.RestartLua(serviceProvider, LogToLuaConsole, mainForm, displayManager, inputManager, mainForm.MovieSession, mainForm.Tools, config, emulator, game); // Register lua libraries foreach (var lib in Client.Common.ReflectionCache.Types.Concat(EmuHawk.ReflectionCache.Types) diff --git a/src/BizHawk.Client.EmuHawk/tools/ToolManager.cs b/src/BizHawk.Client.EmuHawk/tools/ToolManager.cs index 843494268c..fe0c53a3f0 100644 --- a/src/BizHawk.Client.EmuHawk/tools/ToolManager.cs +++ b/src/BizHawk.Client.EmuHawk/tools/ToolManager.cs @@ -57,7 +57,7 @@ namespace BizHawk.Client.EmuHawk _emulator = emulator; _movieSession = movieSession; _game = game; - ApiProvider = ApiManager.Restart(_emulator.ServiceProvider, _owner, _displayManager, _inputManager, this, _config, _emulator, _game); + ApiProvider = ApiManager.Restart(_emulator.ServiceProvider, _owner, _displayManager, _inputManager, _movieSession, this, _config, _emulator, _game); } /// @@ -509,7 +509,7 @@ namespace BizHawk.Client.EmuHawk { _emulator = emulator; _game = game; - ApiProvider = ApiManager.Restart(_emulator.ServiceProvider, _owner, _displayManager, _inputManager, this, _config, _emulator, _game); + ApiProvider = ApiManager.Restart(_emulator.ServiceProvider, _owner, _displayManager, _inputManager, _movieSession, this, _config, _emulator, _game); // If Cheat tool is loaded, restarting will restart the list too anyway if (!Has()) {