Pass MovieSession to APIs via ctor

This commit is contained in:
YoshiRulz 2020-11-28 22:48:02 +10:00
parent c203edba17
commit 3de87af5de
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
6 changed files with 67 additions and 42 deletions

View File

@ -24,7 +24,9 @@ namespace BizHawk.Client.EmuHawk
private static readonly Type[] _ctorParamTypesB = { typeof(Action<string>), typeof(IMainFormForApi) }; private static readonly Type[] _ctorParamTypesB = { typeof(Action<string>), typeof(IMainFormForApi) };
private static readonly Type[] _ctorParamTypesC = { typeof(Action<string>), typeof(IMainFormForApi), typeof(DisplayManager), typeof(InputManager), typeof(Config), typeof(IEmulator), typeof(IGameInfo) }; private static readonly Type[] _ctorParamTypesC = { typeof(Action<string>), typeof(IMovieSession) };
private static readonly Type[] _ctorParamTypesD = { typeof(Action<string>), typeof(IMainFormForApi), typeof(DisplayManager), typeof(InputManager), typeof(Config), typeof(IEmulator), typeof(IGameInfo) };
private static readonly Type[] _ctorParamTypesTools = { typeof(ToolManager) }; private static readonly Type[] _ctorParamTypesTools = { typeof(ToolManager) };
@ -39,6 +41,7 @@ namespace BizHawk.Client.EmuHawk
IMainFormForApi mainForm, IMainFormForApi mainForm,
DisplayManager displayManager, DisplayManager displayManager,
InputManager inputManager, InputManager inputManager,
IMovieSession movieSession,
ToolManager toolManager, ToolManager toolManager,
Config config, Config config,
IEmulator emulator, IEmulator emulator,
@ -48,7 +51,8 @@ namespace BizHawk.Client.EmuHawk
.ToDictionary( .ToDictionary(
t => _apiTypes[t], t => _apiTypes[t],
t => (IExternalApi) ( 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(_ctorParamTypesB)?.Invoke(new object[] { logCallback, mainForm })
?? t.GetConstructor(_ctorParamTypesA)?.Invoke(new object[] { logCallback }) ?? t.GetConstructor(_ctorParamTypesA)?.Invoke(new object[] { logCallback })
?? t.GetConstructor(_ctorParamTypesTools)?.Invoke(new object[] { toolManager }) ?? t.GetConstructor(_ctorParamTypesTools)?.Invoke(new object[] { toolManager })
@ -64,12 +68,13 @@ namespace BizHawk.Client.EmuHawk
IMainFormForApi mainForm, IMainFormForApi mainForm,
DisplayManager displayManager, DisplayManager displayManager,
InputManager inputManager, InputManager inputManager,
IMovieSession movieSession,
ToolManager toolManager, ToolManager toolManager,
Config config, Config config,
IEmulator emulator, IEmulator emulator,
IGameInfo game) 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; ClientApi.EmuClient = _container.EmuClient;
return new BasicApiProvider(_container); return new BasicApiProvider(_container);
} }
@ -80,10 +85,11 @@ namespace BizHawk.Client.EmuHawk
IMainFormForApi mainForm, IMainFormForApi mainForm,
DisplayManager displayManager, DisplayManager displayManager,
InputManager inputManager, InputManager inputManager,
IMovieSession movieSession,
ToolManager toolManager, ToolManager toolManager,
Config config, Config config,
IEmulator emulator, IEmulator emulator,
IGameInfo game 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);
} }
} }

View File

@ -7,9 +7,15 @@ namespace BizHawk.Client.EmuHawk
{ {
public sealed class JoypadApi : IJoypadApi public sealed class JoypadApi : IJoypadApi
{ {
private readonly IMovieSession _movieSession;
private readonly Action<string> LogCallback; private readonly Action<string> LogCallback;
public JoypadApi(Action<string> logCallback) => LogCallback = logCallback; public JoypadApi(Action<string> logCallback, IMovieSession movieSession)
{
LogCallback = logCallback;
_movieSession = movieSession;
}
public IDictionary<string, object> Get(int? controller = null) public IDictionary<string, object> Get(int? controller = null)
{ {
@ -23,7 +29,7 @@ namespace BizHawk.Client.EmuHawk
public void SetFromMnemonicStr(string inputLogEntry) public void SetFromMnemonicStr(string inputLogEntry)
{ {
var controller = GlobalWin.MovieSession.GenerateMovieController(); var controller = _movieSession.GenerateMovieController();
try try
{ {
controller.SetFromMnemonic(inputLogEntry); controller.SetFromMnemonic(inputLogEntry);

View File

@ -9,22 +9,28 @@ namespace BizHawk.Client.EmuHawk
{ {
public sealed class MovieApi : IMovieApi public sealed class MovieApi : IMovieApi
{ {
private readonly IMovieSession _movieSession;
private readonly Action<string> LogCallback; private readonly Action<string> LogCallback;
public MovieApi(Action<string> logCallback) => LogCallback = logCallback; public MovieApi(Action<string> 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<string, object> GetInput(int frame, int? controller = null) public IDictionary<string, object> GetInput(int frame, int? controller = null)
{ {
if (GlobalWin.MovieSession.Movie.NotActive()) if (_movieSession.Movie.NotActive())
{ {
LogCallback("No movie loaded"); LogCallback("No movie loaded");
return null; return null;
} }
var adapter = GlobalWin.MovieSession.Movie.GetInputState(frame); var adapter = _movieSession.Movie.GetInputState(frame);
if (adapter == null) if (adapter == null)
{ {
LogCallback("Can't get input of the last frame of the movie. Use the previous frame"); 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) 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; return string.Empty;
} }
var lg = GlobalWin.MovieSession.Movie.LogGeneratorInstance( var lg = _movieSession.Movie.LogGeneratorInstance(
GlobalWin.MovieSession.Movie.GetInputState(frame)); _movieSession.Movie.GetInputState(frame));
return lg.GenerateLogEntry(); return lg.GenerateLogEntry();
} }
public void Save(string filename = null) public void Save(string filename = null)
{ {
if (GlobalWin.MovieSession.Movie.NotActive()) if (_movieSession.Movie.NotActive())
{ {
return; return;
} }
if (!string.IsNullOrEmpty(filename)) if (!string.IsNullOrEmpty(filename))
{ {
filename += $".{GlobalWin.MovieSession.Movie.PreferredExtension}"; filename += $".{_movieSession.Movie.PreferredExtension}";
if (new FileInfo(filename).Exists) if (new FileInfo(filename).Exists)
{ {
LogCallback($"File {filename} already exists, will not overwrite"); LogCallback($"File {filename} already exists, will not overwrite");
return; return;
} }
GlobalWin.MovieSession.Movie.Filename = filename; _movieSession.Movie.Filename = filename;
} }
GlobalWin.MovieSession.Movie.Save(); _movieSession.Movie.Save();
} }
public Dictionary<string, string> GetHeader() public Dictionary<string, string> GetHeader()
{ {
var table = new Dictionary<string, string>(); var table = new Dictionary<string, string>();
if (GlobalWin.MovieSession.Movie.NotActive()) if (_movieSession.Movie.NotActive())
{ {
return table; 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; return table;
} }
public List<string> GetComments() => GlobalWin.MovieSession.Movie.Comments.ToList(); public List<string> GetComments() => _movieSession.Movie.Comments.ToList();
public List<string> GetSubtitles() => public List<string> GetSubtitles() =>
GlobalWin.MovieSession.Movie.Subtitles _movieSession.Movie.Subtitles
.Select(s => s.ToString()) .Select(s => s.ToString())
.ToList(); .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() 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? // Why does it need the movie to be active to know the frame rate?
if (movie.NotActive()) if (movie.NotActive())
{ {

View File

@ -5,6 +5,13 @@ namespace BizHawk.Client.EmuHawk
{ {
public sealed class UserDataApi : IUserDataApi public sealed class UserDataApi : IUserDataApi
{ {
private readonly IMovieSession _movieSession;
public UserDataApi(Action<string> logCallback, IMovieSession movieSession)
{
_movieSession = movieSession;
}
/// <exception cref="InvalidOperationException">type of <paramref name="value"/> cannot be used in userdata</exception> /// <exception cref="InvalidOperationException">type of <paramref name="value"/> cannot be used in userdata</exception>
public void Set(string name, object value) public void Set(string name, object value)
{ {
@ -13,15 +20,15 @@ namespace BizHawk.Client.EmuHawk
var t = value.GetType(); var t = value.GetType();
if (!t.IsPrimitive && t != typeof(string)) throw new InvalidOperationException("Invalid type for userdata"); 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);
} }
} }

View File

@ -49,7 +49,7 @@ namespace BizHawk.Client.EmuHawk
LuaWait = new AutoResetEvent(false); LuaWait = new AutoResetEvent(false);
Docs.Clear(); 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 // Register lua libraries
foreach (var lib in Client.Common.ReflectionCache.Types.Concat(EmuHawk.ReflectionCache.Types) foreach (var lib in Client.Common.ReflectionCache.Types.Concat(EmuHawk.ReflectionCache.Types)

View File

@ -57,7 +57,7 @@ namespace BizHawk.Client.EmuHawk
_emulator = emulator; _emulator = emulator;
_movieSession = movieSession; _movieSession = movieSession;
_game = game; _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);
} }
/// <summary> /// <summary>
@ -509,7 +509,7 @@ namespace BizHawk.Client.EmuHawk
{ {
_emulator = emulator; _emulator = emulator;
_game = game; _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 Cheat tool is loaded, restarting will restart the list too anyway
if (!Has<Cheats>()) if (!Has<Cheats>())
{ {