From 2fca4282a9332da32eaa267d3e1bd9f22a1cd62d Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Fri, 10 Mar 2023 04:30:52 +1000 Subject: [PATCH] Make `ApiContainer` props return `null` if req. services unavailable specifically `MemoryEvents` and `MemorySaveState` now they behave like `[OptionalApi]` rather than `[RequiredApi]` --- src/BizHawk.Client.Common/Api/ApiContainer.cs | 67 ++++++++++++++----- 1 file changed, 51 insertions(+), 16 deletions(-) diff --git a/src/BizHawk.Client.Common/Api/ApiContainer.cs b/src/BizHawk.Client.Common/Api/ApiContainer.cs index c9f6f0536bc..065a0e122c3 100644 --- a/src/BizHawk.Client.Common/Api/ApiContainer.cs +++ b/src/BizHawk.Client.Common/Api/ApiContainer.cs @@ -9,24 +9,51 @@ public sealed class ApiContainer : IDisposable { public readonly IReadOnlyDictionary Libraries; - public ICommApi Comm => (ICommApi) Libraries[typeof(ICommApi)]; - public IEmuClientApi EmuClient => (IEmuClientApi) Libraries[typeof(IEmuClientApi)]; - public IEmulationApi Emulation => (IEmulationApi) Libraries[typeof(IEmulationApi)]; + public ICommApi Comm + => Get(); + + public IEmuClientApi EmuClient + => Get(); + + public IEmulationApi Emulation + => Get(); // requires IEmulator [Obsolete("use Emulation")] - public IGameInfoApi GameInfo => (IGameInfoApi) Libraries[typeof(IGameInfoApi)]; - - public IGuiApi Gui => (IGuiApi) Libraries[typeof(IGuiApi)]; - public IInputApi Input => (IInputApi) Libraries[typeof(IInputApi)]; - public IJoypadApi Joypad => (IJoypadApi) Libraries[typeof(IJoypadApi)]; - public IMemoryApi Memory => (IMemoryApi) Libraries[typeof(IMemoryApi)]; - public IMemoryEventsApi MemoryEvents => (IMemoryEventsApi) Libraries[typeof(IMemoryEventsApi)]; - public IMemorySaveStateApi MemorySaveState => (IMemorySaveStateApi) Libraries[typeof(IMemorySaveStateApi)]; - public IMovieApi Movie => (IMovieApi) Libraries[typeof(IMovieApi)]; - public ISaveStateApi SaveState => (ISaveStateApi) Libraries[typeof(ISaveStateApi)]; - public ISQLiteApi SQLite => (ISQLiteApi) Libraries[typeof(ISQLiteApi)]; - public IUserDataApi UserData => (IUserDataApi) Libraries[typeof(IUserDataApi)]; - public IToolApi Tool => (IToolApi) Libraries[typeof(IToolApi)]; + public IGameInfoApi GameInfo + => Get(); + + public IGuiApi Gui + => Get(); // requires IEmulator + + public IInputApi Input + => Get(); + + public IJoypadApi Joypad + => Get(); + + public IMemoryApi Memory + => Get(); // requires IEmulator + + public IMemoryEventsApi? MemoryEvents + => TryGet(); // requires IDebuggable + + public IMemorySaveStateApi? MemorySaveState + => TryGet(); // requires IStatable + + public IMovieApi Movie + => Get(); + + public ISaveStateApi SaveState + => Get(); + + public ISQLiteApi SQLite + => Get(); + + public IUserDataApi UserData + => Get(); + + public IToolApi Tool + => Get(); public ApiContainer(IReadOnlyDictionary libs) => Libraries = libs; @@ -34,5 +61,13 @@ public void Dispose() { foreach (var lib in Libraries.Values) if (lib is IDisposable disposableLib) disposableLib.Dispose(); } + + public T Get() + where T : class, IExternalApi + => (T) Libraries[typeof(T)]; + + public T? TryGet() + where T : class, IExternalApi + => Libraries.TryGetValue(typeof(T), out var inst) ? (T) inst : null; } }