Skip to content

Commit

Permalink
Make ApiContainer props return null if req. services unavailable
Browse files Browse the repository at this point in the history
specifically `MemoryEvents` and `MemorySaveState`
now they behave like `[OptionalApi]` rather than `[RequiredApi]`
  • Loading branch information
YoshiRulz committed Mar 9, 2023
1 parent 6387291 commit 2fca428
Showing 1 changed file with 51 additions and 16 deletions.
67 changes: 51 additions & 16 deletions src/BizHawk.Client.Common/Api/ApiContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,65 @@ public sealed class ApiContainer : IDisposable
{
public readonly IReadOnlyDictionary<Type, IExternalApi> 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<ICommApi>();

public IEmuClientApi EmuClient
=> Get<IEmuClientApi>();

public IEmulationApi Emulation
=> Get<IEmulationApi>(); // 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<IGameInfoApi>();

public IGuiApi Gui
=> Get<IGuiApi>(); // requires IEmulator

public IInputApi Input
=> Get<IInputApi>();

public IJoypadApi Joypad
=> Get<IJoypadApi>();

public IMemoryApi Memory
=> Get<IMemoryApi>(); // requires IEmulator

public IMemoryEventsApi? MemoryEvents
=> TryGet<IMemoryEventsApi>(); // requires IDebuggable

public IMemorySaveStateApi? MemorySaveState
=> TryGet<IMemorySaveStateApi>(); // requires IStatable

public IMovieApi Movie
=> Get<IMovieApi>();

public ISaveStateApi SaveState
=> Get<ISaveStateApi>();

public ISQLiteApi SQLite
=> Get<ISQLiteApi>();

public IUserDataApi UserData
=> Get<IUserDataApi>();

public IToolApi Tool
=> Get<IToolApi>();

public ApiContainer(IReadOnlyDictionary<Type, IExternalApi> libs) => Libraries = libs;

public void Dispose()
{
foreach (var lib in Libraries.Values) if (lib is IDisposable disposableLib) disposableLib.Dispose();
}

public T Get<T>()
where T : class, IExternalApi
=> (T) Libraries[typeof(T)];

public T? TryGet<T>()
where T : class, IExternalApi
=> Libraries.TryGetValue(typeof(T), out var inst) ? (T) inst : null;
}
}

0 comments on commit 2fca428

Please sign in to comment.