From 7f0229d9e6124d30c5a2462df4c4d6945a119f20 Mon Sep 17 00:00:00 2001 From: scepheo Date: Sat, 13 Dec 2014 23:36:49 +0000 Subject: [PATCH] Added support for OptionalServices and cleaned up ToolManager code. --- BizHawk.Client.EmuHawk/tools/IToolForm.cs | 15 ++++++ BizHawk.Client.EmuHawk/tools/ToolManager.cs | 60 +++++++++++---------- 2 files changed, 48 insertions(+), 27 deletions(-) diff --git a/BizHawk.Client.EmuHawk/tools/IToolForm.cs b/BizHawk.Client.EmuHawk/tools/IToolForm.cs index 3323098227..bcbfc519fa 100644 --- a/BizHawk.Client.EmuHawk/tools/IToolForm.cs +++ b/BizHawk.Client.EmuHawk/tools/IToolForm.cs @@ -67,4 +67,19 @@ namespace BizHawk.Client.EmuHawk this.Dependencies = requiredServices; } } + + /// + /// Attribute used for IToolForms to indicate which IEmulatorServices they + /// could use, but that aren't critical for their functioning. + /// + [AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = true)] + sealed class OptionalServices : Attribute + { + public readonly Type[] Dependencies; + + public OptionalServices(params Type[] optionalServices) + { + this.Dependencies = optionalServices; + } + } } diff --git a/BizHawk.Client.EmuHawk/tools/ToolManager.cs b/BizHawk.Client.EmuHawk/tools/ToolManager.cs index f7f7077eec..98aceb2c57 100644 --- a/BizHawk.Client.EmuHawk/tools/ToolManager.cs +++ b/BizHawk.Client.EmuHawk/tools/ToolManager.cs @@ -37,7 +37,7 @@ namespace BizHawk.Client.EmuHawk var result = Get(); - UpdateDependencies(result); + UpdateServices(result); result.Restart(); result.Show(); @@ -49,18 +49,7 @@ namespace BizHawk.Client.EmuHawk /// RequiredServices attribute. If no attribute is defined, returns an /// empty array. /// - public Type[] GetDependencies() - where T : IToolForm - { - return GetDependencies(typeof(T)); - } - - /// - /// Gets all the IEmulatorServices the tool needs, as defined in its - /// RequiredServices attribute. If no attribute is defined, returns an - /// empty array. - /// - public Type[] GetDependencies(Type toolType) + public Type[] GetRequiredServices(Type toolType) { var attribute = (RequiredServices)toolType .GetCustomAttributes(typeof(RequiredServices), false) @@ -72,36 +61,53 @@ namespace BizHawk.Client.EmuHawk return attribute.Dependencies; } + /// + /// 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. + /// + 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; + } + /// /// Feeds the tool its required services. /// - private void UpdateDependencies(IToolForm tool) + private void UpdateServices(IToolForm tool) { var serviceSet = new Dictionary(); - 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); + // 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; } - /// - /// Determines whether a tool is available, considering its dependencies - /// and the services provided by the emulator core. - /// - public bool IsAvailable() - where T : IToolForm - { - return IsAvailable(typeof(T)); - } - /// /// Determines whether a tool is available, considering its dependencies /// and the services provided by the emulator core. /// public bool IsAvailable(Type toolType) { - return GetDependencies(toolType) + return GetRequiredServices(toolType) .All(t => Global.Emulator.ServiceProvider.HasService(t)); } @@ -204,7 +210,7 @@ namespace BizHawk.Client.EmuHawk { if (IsAvailable(tool.GetType())) { - UpdateDependencies(tool); + UpdateServices(tool); tool.Restart(); } else