Added support for OptionalServices and cleaned up ToolManager code.
This commit is contained in:
parent
7925b6f939
commit
7f0229d9e6
|
@ -67,4 +67,19 @@ namespace BizHawk.Client.EmuHawk
|
||||||
this.Dependencies = requiredServices;
|
this.Dependencies = requiredServices;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Attribute used for IToolForms to indicate which IEmulatorServices they
|
||||||
|
/// could use, but that aren't critical for their functioning.
|
||||||
|
/// </summary>
|
||||||
|
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = true)]
|
||||||
|
sealed class OptionalServices : Attribute
|
||||||
|
{
|
||||||
|
public readonly Type[] Dependencies;
|
||||||
|
|
||||||
|
public OptionalServices(params Type[] optionalServices)
|
||||||
|
{
|
||||||
|
this.Dependencies = optionalServices;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,7 +37,7 @@ namespace BizHawk.Client.EmuHawk
|
||||||
|
|
||||||
var result = Get<T>();
|
var result = Get<T>();
|
||||||
|
|
||||||
UpdateDependencies(result);
|
UpdateServices(result);
|
||||||
result.Restart();
|
result.Restart();
|
||||||
|
|
||||||
result.Show();
|
result.Show();
|
||||||
|
@ -49,18 +49,7 @@ namespace BizHawk.Client.EmuHawk
|
||||||
/// RequiredServices attribute. If no attribute is defined, returns an
|
/// RequiredServices attribute. If no attribute is defined, returns an
|
||||||
/// empty array.
|
/// empty array.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Type[] GetDependencies<T>()
|
public Type[] GetRequiredServices(Type toolType)
|
||||||
where T : IToolForm
|
|
||||||
{
|
|
||||||
return GetDependencies(typeof(T));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets all the IEmulatorServices the tool needs, as defined in its
|
|
||||||
/// RequiredServices attribute. If no attribute is defined, returns an
|
|
||||||
/// empty array.
|
|
||||||
/// </summary>
|
|
||||||
public Type[] GetDependencies(Type toolType)
|
|
||||||
{
|
{
|
||||||
var attribute = (RequiredServices)toolType
|
var attribute = (RequiredServices)toolType
|
||||||
.GetCustomAttributes(typeof(RequiredServices), false)
|
.GetCustomAttributes(typeof(RequiredServices), false)
|
||||||
|
@ -72,36 +61,53 @@ namespace BizHawk.Client.EmuHawk
|
||||||
return attribute.Dependencies;
|
return attribute.Dependencies;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets all the IEmulatorServices the tool could use, but does not
|
||||||
|
/// need, as defined in its OptionalServices attribute. If no attribute
|
||||||
|
/// is defined, returns an empty array.
|
||||||
|
/// </summary>
|
||||||
|
public Type[] GetOptionalServices(Type toolType)
|
||||||
|
{
|
||||||
|
var attribute = (OptionalServices)toolType
|
||||||
|
.GetCustomAttributes(typeof(OptionalServices), false)
|
||||||
|
.FirstOrDefault();
|
||||||
|
|
||||||
|
if (attribute == null)
|
||||||
|
return new Type[0];
|
||||||
|
else
|
||||||
|
return attribute.Dependencies;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Feeds the tool its required services.
|
/// Feeds the tool its required services.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void UpdateDependencies(IToolForm tool)
|
private void UpdateServices(IToolForm tool)
|
||||||
{
|
{
|
||||||
var serviceSet = new Dictionary<Type, object>();
|
var serviceSet = new Dictionary<Type, object>();
|
||||||
|
|
||||||
foreach (var service in GetDependencies(tool.GetType()))
|
// Populate the required services
|
||||||
|
foreach (var service in GetRequiredServices(tool.GetType()))
|
||||||
serviceSet[service] = Global.Emulator.ServiceProvider.GetService(service);
|
serviceSet[service] = Global.Emulator.ServiceProvider.GetService(service);
|
||||||
|
|
||||||
|
// Populate the optional services if they exist, otherwise insert null
|
||||||
|
foreach(var service in GetOptionalServices(tool.GetType()))
|
||||||
|
{
|
||||||
|
if (Global.Emulator.ServiceProvider.HasService(service))
|
||||||
|
serviceSet[service] = Global.Emulator.ServiceProvider.GetService(service);
|
||||||
|
else
|
||||||
|
serviceSet[service] = null;
|
||||||
|
}
|
||||||
|
|
||||||
tool.EmulatorServices = serviceSet;
|
tool.EmulatorServices = serviceSet;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Determines whether a tool is available, considering its dependencies
|
|
||||||
/// and the services provided by the emulator core.
|
|
||||||
/// </summary>
|
|
||||||
public bool IsAvailable<T>()
|
|
||||||
where T : IToolForm
|
|
||||||
{
|
|
||||||
return IsAvailable(typeof(T));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Determines whether a tool is available, considering its dependencies
|
/// Determines whether a tool is available, considering its dependencies
|
||||||
/// and the services provided by the emulator core.
|
/// and the services provided by the emulator core.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool IsAvailable(Type toolType)
|
public bool IsAvailable(Type toolType)
|
||||||
{
|
{
|
||||||
return GetDependencies(toolType)
|
return GetRequiredServices(toolType)
|
||||||
.All(t => Global.Emulator.ServiceProvider.HasService(t));
|
.All(t => Global.Emulator.ServiceProvider.HasService(t));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -204,7 +210,7 @@ namespace BizHawk.Client.EmuHawk
|
||||||
{
|
{
|
||||||
if (IsAvailable(tool.GetType()))
|
if (IsAvailable(tool.GetType()))
|
||||||
{
|
{
|
||||||
UpdateDependencies(tool);
|
UpdateServices(tool);
|
||||||
tool.Restart();
|
tool.Restart();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
Loading…
Reference in New Issue