2018-12-22 18:40:30 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
2019-11-16 06:16:33 +00:00
|
|
|
|
using BizHawk.Client.Common;
|
|
|
|
|
|
2018-12-22 18:40:30 +00:00
|
|
|
|
namespace BizHawk.Client.ApiHawk
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A generic implementation of IExternalApi provider that provides
|
|
|
|
|
/// this functionality to any core.
|
|
|
|
|
/// The provider will scan an IExternal and register all IExternalApis
|
|
|
|
|
/// that the core object itself implements. In addition it provides
|
|
|
|
|
/// a Register() method to allow the core to pass in any additional Apis
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <seealso cref="IExternalApiProvider"/>
|
|
|
|
|
public class BasicApiProvider : IExternalApiProvider
|
|
|
|
|
{
|
2019-11-15 23:29:38 +00:00
|
|
|
|
private readonly Dictionary<Type, IExternalApi> _apis = new Dictionary<Type, IExternalApi>();
|
2018-12-22 18:40:30 +00:00
|
|
|
|
|
|
|
|
|
public BasicApiProvider(IApiContainer container)
|
|
|
|
|
{
|
|
|
|
|
// simplified logic here doesn't scan for possible Apis; just adds what it knows is implemented by the PluginApi
|
|
|
|
|
// this removes the possibility of automagically picking up a Api in a nested class, (find the type, then
|
|
|
|
|
// find the field), but we're going to keep such logic out of the basic provider. Anything the passed
|
|
|
|
|
// container doesn't implement directly needs to be added with Register()
|
|
|
|
|
// this also fully allows apis that are not IExternalApi
|
|
|
|
|
var libs = container.Libraries;
|
|
|
|
|
|
2019-11-15 23:29:38 +00:00
|
|
|
|
_apis = libs;
|
2018-12-22 18:40:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// the client can call this to register an additional Api
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T">The <seealso cref="IExternalApi"/> to register</typeparam>
|
|
|
|
|
public void Register<T>(T api)
|
|
|
|
|
where T : IExternalApi
|
|
|
|
|
{
|
|
|
|
|
if (api == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(api));
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-15 23:29:38 +00:00
|
|
|
|
_apis[typeof(T)] = api;
|
2018-12-22 18:40:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public T GetApi<T>()
|
|
|
|
|
where T : IExternalApi
|
|
|
|
|
{
|
|
|
|
|
return (T)GetApi(typeof(T));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public object GetApi(Type t)
|
|
|
|
|
{
|
2019-11-15 23:29:38 +00:00
|
|
|
|
KeyValuePair<Type, IExternalApi>[] k = _apis.Where(kvp => t.IsAssignableFrom(kvp.Key)).ToArray();
|
2018-12-22 18:40:30 +00:00
|
|
|
|
if (k.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
return k[0].Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool HasApi<T>()
|
|
|
|
|
where T : IExternalApi
|
|
|
|
|
{
|
|
|
|
|
return HasApi(typeof(T));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool HasApi(Type t)
|
|
|
|
|
{
|
2019-11-15 23:29:38 +00:00
|
|
|
|
return _apis.ContainsKey(t);
|
2018-12-22 18:40:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-15 23:29:38 +00:00
|
|
|
|
public IEnumerable<Type> AvailableApis => _apis.Select(d => d.Key);
|
2018-12-22 18:40:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|