From 258289426e4db56d07a6e9732e272b7f9736d0b9 Mon Sep 17 00:00:00 2001 From: SuuperW Date: Tue, 19 Sep 2023 02:28:54 -0500 Subject: [PATCH] Move ApiManager to BizHawk.Client.Common, as part of moving LuaLibraries to BizHawk.Client.Common (to support testing). --- .../Api/ApiManager.cs | 15 +++++++++------ src/BizHawk.Client.EmuHawk/Program.cs | 3 +++ 2 files changed, 12 insertions(+), 6 deletions(-) rename src/{BizHawk.Client.EmuHawk => BizHawk.Client.Common}/Api/ApiManager.cs (85%) diff --git a/src/BizHawk.Client.EmuHawk/Api/ApiManager.cs b/src/BizHawk.Client.Common/Api/ApiManager.cs similarity index 85% rename from src/BizHawk.Client.EmuHawk/Api/ApiManager.cs rename to src/BizHawk.Client.Common/Api/ApiManager.cs index 4493c801fa..c987e402fa 100644 --- a/src/BizHawk.Client.EmuHawk/Api/ApiManager.cs +++ b/src/BizHawk.Client.Common/Api/ApiManager.cs @@ -5,19 +5,19 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; -using BizHawk.Client.Common; using BizHawk.Emulation.Common; -namespace BizHawk.Client.EmuHawk +namespace BizHawk.Client.Common { public static class ApiManager { - private static readonly IReadOnlyList<(Type ImplType, Type InterfaceType, ConstructorInfo Ctor, Type[] CtorTypes)> _apiTypes; + private static IReadOnlyList<(Type ImplType, Type InterfaceType, ConstructorInfo Ctor, Type[] CtorTypes)> _apiTypes + = Array.Empty<(Type ImplType, Type InterfaceType, ConstructorInfo Ctor, Type[] CtorTypes)>(); - static ApiManager() + public static void FindApis(IEnumerable typesToSearch) { var list = new List<(Type, Type, ConstructorInfo, Type[])>(); - foreach (var implType in Common.ReflectionCache.Types.Concat(ReflectionCache.Types) + foreach (var implType in typesToSearch .Where(t => /*t.IsClass &&*/t.IsSealed)) // small optimisation; api impl. types are all sealed classes { var interfaceType = implType.GetInterfaces().FirstOrDefault(t => typeof(IExternalApi).IsAssignableFrom(t) && t != typeof(IExternalApi)); @@ -25,7 +25,7 @@ namespace BizHawk.Client.EmuHawk var ctor = implType.GetConstructors().Single(); list.Add((implType, interfaceType, ctor, ctor.GetParameters().Select(pi => pi.ParameterType).ToArray())); } - _apiTypes = list.ToArray(); + _apiTypes = _apiTypes.Concat(list).ToArray(); } private static ApiContainer? _container; @@ -44,6 +44,9 @@ namespace BizHawk.Client.EmuHawk IEmulator emulator, IGameInfo game) { + if (_apiTypes.Count == 0) + throw new InvalidOperationException("Attempted to use ApiManager before calling ApiManager.FindApis."); + var avail = new Dictionary { [typeof(Action)] = logCallback, diff --git a/src/BizHawk.Client.EmuHawk/Program.cs b/src/BizHawk.Client.EmuHawk/Program.cs index 68c6031fee..987960d47d 100644 --- a/src/BizHawk.Client.EmuHawk/Program.cs +++ b/src/BizHawk.Client.EmuHawk/Program.cs @@ -129,6 +129,9 @@ namespace BizHawk.Client.EmuHawk } } + ApiManager.FindApis(Common.ReflectionCache.Types); + ApiManager.FindApis(ReflectionCache.Types); + typeof(Form).GetField(OSTC.IsUnixHost ? "default_icon" : "defaultIcon", BindingFlags.NonPublic | BindingFlags.Static) .SetValue(null, Properties.Resources.Logo);