diff --git a/BizHawk.Client.EmuHawk/MainForm.Events.cs b/BizHawk.Client.EmuHawk/MainForm.Events.cs index 27f2d402cb..55dfa3db5e 100644 --- a/BizHawk.Client.EmuHawk/MainForm.Events.cs +++ b/BizHawk.Client.EmuHawk/MainForm.Events.cs @@ -1209,14 +1209,14 @@ namespace BizHawk.Client.EmuHawk { FDSControlsMenuItem.Enabled = Global.Emulator.BoardName == "FDS"; - NESSoundChannelsMenuItem.Enabled = - MovieSettingsMenuItem.Enabled = - Global.Emulator is NES; + NESSoundChannelsMenuItem.Enabled = + MovieSettingsMenuItem.Enabled = + Global.Emulator is NES; NesControllerSettingsMenuItem.Enabled = (Global.Emulator is NES || Global.Emulator is QuickNES) && !Global.MovieSession.Movie.IsActive; MovieSettingsMenuItem.Enabled = Global.Emulator is NES && !Global.MovieSession.Movie.IsActive; - barcodeReaderToolStripMenuItem.Enabled = GlobalWin.Tools.IsAvailable(typeof(BarcodeEntry)); + barcodeReaderToolStripMenuItem.Enabled = ServiceInjector.IsAvailable(Global.Emulator.ServiceProvider, typeof(BarcodeEntry)); } private void FdsControlsMenuItem_DropDownOpened(object sender, EventArgs e) diff --git a/BizHawk.Client.EmuHawk/config/NES/NESSoundConfig.cs b/BizHawk.Client.EmuHawk/config/NES/NESSoundConfig.cs index efec5fd7ae..b20866231b 100644 --- a/BizHawk.Client.EmuHawk/config/NES/NESSoundConfig.cs +++ b/BizHawk.Client.EmuHawk/config/NES/NESSoundConfig.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Windows.Forms; using BizHawk.Client.Common; using BizHawk.Emulation.Cores.Nintendo.NES; +using BizHawk.Emulation.Common; namespace BizHawk.Client.EmuHawk { diff --git a/BizHawk.Client.EmuHawk/tools/GB/GBGPUView.cs b/BizHawk.Client.EmuHawk/tools/GB/GBGPUView.cs index ee5afc9760..06dc5b9636 100644 --- a/BizHawk.Client.EmuHawk/tools/GB/GBGPUView.cs +++ b/BizHawk.Client.EmuHawk/tools/GB/GBGPUView.cs @@ -8,6 +8,7 @@ using BizHawk.Client.Common; using BizHawk.Emulation.Cores.Nintendo.Gameboy; using BizHawk.Client.EmuHawk.WinFormExtensions; using System.Collections.Generic; +using BizHawk.Emulation.Common; namespace BizHawk.Client.EmuHawk { diff --git a/BizHawk.Client.EmuHawk/tools/GBA/GBAGPUView.cs b/BizHawk.Client.EmuHawk/tools/GBA/GBAGPUView.cs index 9e7d667b55..9c3efb7710 100644 --- a/BizHawk.Client.EmuHawk/tools/GBA/GBAGPUView.cs +++ b/BizHawk.Client.EmuHawk/tools/GBA/GBAGPUView.cs @@ -7,6 +7,7 @@ using BizHawk.Client.Common; using BizHawk.Emulation.Cores.Nintendo.Gameboy; using BizHawk.Emulation.Cores.Nintendo.GBA; using System.Collections.Generic; +using BizHawk.Emulation.Common; namespace BizHawk.Client.EmuHawk { diff --git a/BizHawk.Client.EmuHawk/tools/Genesis/VDPViewer.cs b/BizHawk.Client.EmuHawk/tools/Genesis/VDPViewer.cs index d2f113a056..820a457f29 100644 --- a/BizHawk.Client.EmuHawk/tools/Genesis/VDPViewer.cs +++ b/BizHawk.Client.EmuHawk/tools/Genesis/VDPViewer.cs @@ -9,6 +9,7 @@ using System.Windows.Forms; using BizHawk.Client.Common; using BizHawk.Emulation.Cores.Consoles.Sega.gpgx; using System.Drawing.Imaging; +using BizHawk.Emulation.Common; namespace BizHawk.Client.EmuHawk { diff --git a/BizHawk.Client.EmuHawk/tools/IToolForm.cs b/BizHawk.Client.EmuHawk/tools/IToolForm.cs index faa16bbb87..cb781f6deb 100644 --- a/BizHawk.Client.EmuHawk/tools/IToolForm.cs +++ b/BizHawk.Client.EmuHawk/tools/IToolForm.cs @@ -46,14 +46,4 @@ namespace BizHawk.Client.EmuHawk bool IsDisposed { get; } bool IsHandleCreated { get; } } - - [AttributeUsage(AttributeTargets.Property)] - public class RequiredService : Attribute - { - } - - [AttributeUsage(AttributeTargets.Property)] - public class OptionalService : Attribute - { - } } diff --git a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.cs b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.cs index 9799cdda3c..b0a3040692 100644 --- a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.cs +++ b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.cs @@ -49,12 +49,14 @@ namespace BizHawk.Client.EmuHawk Docs.Clear(); _caller = passed.Get(); - + // what was this? + /* var tt = typeof(TastudioLuaLibrary); var mm = typeof(MainMemoryLuaLibrary); var tatt = tt.GetCustomAttributes(typeof(LuaLibraryAttributes), false); var matt = mm.GetCustomAttributes(typeof(LuaLibraryAttributes), false); + */ // Register lua libraries var libs = Assembly diff --git a/BizHawk.Client.EmuHawk/tools/NES/BarcodeEntry.cs b/BizHawk.Client.EmuHawk/tools/NES/BarcodeEntry.cs index a6ebfe3cf8..aad9193f69 100644 --- a/BizHawk.Client.EmuHawk/tools/NES/BarcodeEntry.cs +++ b/BizHawk.Client.EmuHawk/tools/NES/BarcodeEntry.cs @@ -9,6 +9,7 @@ using System.Windows.Forms; using BizHawk.Emulation.Cores.Nintendo.NES; using BizHawk.Common; using BizHawk.Client.Common; +using BizHawk.Emulation.Common; namespace BizHawk.Client.EmuHawk { diff --git a/BizHawk.Client.EmuHawk/tools/PCE/PCEBGViewer.cs b/BizHawk.Client.EmuHawk/tools/PCE/PCEBGViewer.cs index d193225ef7..fd26976f1c 100644 --- a/BizHawk.Client.EmuHawk/tools/PCE/PCEBGViewer.cs +++ b/BizHawk.Client.EmuHawk/tools/PCE/PCEBGViewer.cs @@ -5,6 +5,7 @@ using System.Drawing.Imaging; using System.Windows.Forms; using BizHawk.Client.Common; using BizHawk.Emulation.Cores.PCEngine; +using BizHawk.Emulation.Common; namespace BizHawk.Client.EmuHawk { diff --git a/BizHawk.Client.EmuHawk/tools/PCE/PCESoundDebugger.cs b/BizHawk.Client.EmuHawk/tools/PCE/PCESoundDebugger.cs index dd0a58fe1f..5e4f723077 100644 --- a/BizHawk.Client.EmuHawk/tools/PCE/PCESoundDebugger.cs +++ b/BizHawk.Client.EmuHawk/tools/PCE/PCESoundDebugger.cs @@ -13,6 +13,7 @@ using BizHawk.Client.Common; using BizHawk.Emulation.Cores.PCEngine; using BizHawk.Emulation.Common.Components; +using BizHawk.Emulation.Common; using ICSharpCode.SharpZipLib.Zip; diff --git a/BizHawk.Client.EmuHawk/tools/PCE/PCETileViewer.cs b/BizHawk.Client.EmuHawk/tools/PCE/PCETileViewer.cs index 27f8269476..8d11f3285b 100644 --- a/BizHawk.Client.EmuHawk/tools/PCE/PCETileViewer.cs +++ b/BizHawk.Client.EmuHawk/tools/PCE/PCETileViewer.cs @@ -10,6 +10,7 @@ using System.Windows.Forms; using BizHawk.Client.Common; using BizHawk.Emulation.Cores.PCEngine; using System.Drawing.Imaging; +using BizHawk.Emulation.Common; namespace BizHawk.Client.EmuHawk { diff --git a/BizHawk.Client.EmuHawk/tools/SMS/VDPViewer.cs b/BizHawk.Client.EmuHawk/tools/SMS/VDPViewer.cs index 1024ba79cf..d9a7f33f1a 100644 --- a/BizHawk.Client.EmuHawk/tools/SMS/VDPViewer.cs +++ b/BizHawk.Client.EmuHawk/tools/SMS/VDPViewer.cs @@ -10,6 +10,7 @@ using BizHawk.Emulation.Cores.Sega.MasterSystem; using BizHawk.Common; using BizHawk.Client.Common; using System.Drawing.Imaging; +using BizHawk.Emulation.Common; namespace BizHawk.Client.EmuHawk { diff --git a/BizHawk.Client.EmuHawk/tools/SNES/SNESGraphicsDebugger.cs b/BizHawk.Client.EmuHawk/tools/SNES/SNESGraphicsDebugger.cs index 916d25c0e4..f3316d44b8 100644 --- a/BizHawk.Client.EmuHawk/tools/SNES/SNESGraphicsDebugger.cs +++ b/BizHawk.Client.EmuHawk/tools/SNES/SNESGraphicsDebugger.cs @@ -33,6 +33,7 @@ using BizHawk.Common.NumberExtensions; using BizHawk.Client.Common; using BizHawk.Emulation.Cores.Nintendo.SNES; using BizHawk.Client.EmuHawk; //TODO: What?? +using BizHawk.Emulation.Common; namespace BizHawk.Client.EmuHawk { diff --git a/BizHawk.Client.EmuHawk/tools/TI83/TI83KeyPad.cs b/BizHawk.Client.EmuHawk/tools/TI83/TI83KeyPad.cs index 289c631a10..7b6062edb0 100644 --- a/BizHawk.Client.EmuHawk/tools/TI83/TI83KeyPad.cs +++ b/BizHawk.Client.EmuHawk/tools/TI83/TI83KeyPad.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Windows.Forms; using BizHawk.Client.Common; using BizHawk.Emulation.Cores.Calculators; +using BizHawk.Emulation.Common; namespace BizHawk.Client.EmuHawk { diff --git a/BizHawk.Client.EmuHawk/tools/ToolBox.cs b/BizHawk.Client.EmuHawk/tools/ToolBox.cs index 1be1e0da42..4882d28b83 100644 --- a/BizHawk.Client.EmuHawk/tools/ToolBox.cs +++ b/BizHawk.Client.EmuHawk/tools/ToolBox.cs @@ -62,7 +62,7 @@ namespace BizHawk.Client.EmuHawk .Where(t => VersionInfo.DeveloperBuild ? true : !(t.GetCustomAttributes(false) .OfType().Any(a => !a.Released))) .Where(t => !(t == typeof(GBGameGenie))) // Hack, this tool is specific to a system id and a sub-system (gb and gg) we have no reasonable way to declare a dependency like that - .Where(t => GlobalWin.Tools.IsAvailable(t)) + .Where(t => BizHawk.Emulation.Common.ServiceInjector.IsAvailable(Global.Emulator.ServiceProvider, t)) .Select(t => Activator.CreateInstance(t)) .Select(instance => new { diff --git a/BizHawk.Client.EmuHawk/tools/ToolManager.cs b/BizHawk.Client.EmuHawk/tools/ToolManager.cs index e237e6392a..0b65701cc7 100644 --- a/BizHawk.Client.EmuHawk/tools/ToolManager.cs +++ b/BizHawk.Client.EmuHawk/tools/ToolManager.cs @@ -6,6 +6,8 @@ using System.Reflection; using BizHawk.Emulation.Common.IEmulatorExtensions; using BizHawk.Client.Common; +using BizHawk.Emulation.Common; + namespace BizHawk.Client.EmuHawk { @@ -33,7 +35,7 @@ namespace BizHawk.Client.EmuHawk if (!typeof(IToolForm).IsAssignableFrom(toolType)) throw new ArgumentException(String.Format("Type {0} does not implement IToolForm.", toolType.Name)); - if (!IsAvailable(toolType)) + if (!ServiceInjector.IsAvailable(Global.Emulator.ServiceProvider, toolType)) return null; var existingTool = _tools.FirstOrDefault(x => toolType.IsAssignableFrom(x.GetType())); @@ -54,53 +56,13 @@ namespace BizHawk.Client.EmuHawk var newTool = CreateInstance(toolType); - UpdateServices(newTool); + ServiceInjector.UpdateServices(Global.Emulator.ServiceProvider, newTool); newTool.Restart(); newTool.Show(); return newTool; } - - - private static IEnumerable GetPropertiesWithAttr(Type type, Type attributeType) - { - return type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic) - .Where(p => p.GetCustomAttributes(attributeType, false).Length > 0); - } - - /// - /// Feeds the tool its required services. - /// - private void UpdateServices(IToolForm tool) - { - var toolType = tool.GetType(); - - foreach (var propinfo in GetPropertiesWithAttr(toolType, typeof(RequiredService))) - { - var service = Global.Emulator.ServiceProvider.GetService(propinfo.PropertyType); - propinfo.GetSetMethod(true).Invoke(tool, new[] { service }); - } - - foreach (var propinfo in GetPropertiesWithAttr(toolType, typeof(OptionalService))) - { - var service = Global.Emulator.ServiceProvider.HasService(propinfo.PropertyType) - ? Global.Emulator.ServiceProvider.GetService(propinfo.PropertyType) : null; - propinfo.GetSetMethod(true).Invoke(tool, new[] { service }); - } - } - - /// - /// Determines whether a tool is available, considering its dependencies - /// and the services provided by the emulator core. - /// - public bool IsAvailable(Type toolType) - { - return GetPropertiesWithAttr(toolType, typeof(RequiredService)) - .Select(pi => pi.PropertyType) - .All(t => Global.Emulator.ServiceProvider.HasService(t)); - } - /// /// Determines whether a given IToolForm is already loaded /// @@ -198,9 +160,9 @@ namespace BizHawk.Client.EmuHawk foreach (var tool in _tools) { - if (IsAvailable(tool.GetType())) + if (ServiceInjector.IsAvailable(Global.Emulator.ServiceProvider, tool.GetType())) { - UpdateServices(tool); + ServiceInjector.UpdateServices(Global.Emulator.ServiceProvider, tool); if ((tool.IsHandleCreated && !tool.IsDisposed) || tool is RamWatch) // Hack for Ram Watch - in display watches mode it wants to keep running even closed, it will handle disposed logic { tool.Restart(); diff --git a/BizHawk.Emulation.Common/BizHawk.Emulation.Common.csproj b/BizHawk.Emulation.Common/BizHawk.Emulation.Common.csproj index ccf03881ab..4c7ff0f34a 100644 --- a/BizHawk.Emulation.Common/BizHawk.Emulation.Common.csproj +++ b/BizHawk.Emulation.Common/BizHawk.Emulation.Common.csproj @@ -90,6 +90,7 @@ + diff --git a/BizHawk.Emulation.Common/ServiceInjector.cs b/BizHawk.Emulation.Common/ServiceInjector.cs new file mode 100644 index 0000000000..27bff7c8e2 --- /dev/null +++ b/BizHawk.Emulation.Common/ServiceInjector.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Reflection; + +namespace BizHawk.Emulation.Common +{ + /// + /// injects services into other classes + /// + public static class ServiceInjector + { + private static IEnumerable GetPropertiesWithAttr(Type type, Type attributeType) + { + return type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic) + .Where(p => p.GetCustomAttributes(attributeType, false).Length > 0); + } + + /// + /// Feeds the target its required services. + /// + /// false if update failed + public static bool UpdateServices(IEmulatorServiceProvider source, object target) + { + Type toolType = target.GetType(); + object[] tmp = new object[1]; + + foreach (var propinfo in GetPropertiesWithAttr(toolType, typeof(RequiredService))) + { + tmp[0] = source.GetService(propinfo.PropertyType); + if (tmp[0] == null) + return false; + propinfo.GetSetMethod(true).Invoke(target, tmp); + } + + foreach (var propinfo in GetPropertiesWithAttr(toolType, typeof(OptionalService))) + { + tmp[0] = source.GetService(propinfo.PropertyType); + propinfo.GetSetMethod(true).Invoke(target, tmp); + } + return true; + } + + /// + /// Determines whether a target is available, considering its dependencies + /// and the services provided by the emulator core. + /// + public static bool IsAvailable(IEmulatorServiceProvider source, Type targetType) + { + return GetPropertiesWithAttr(targetType, typeof(RequiredService)) + .Select(pi => pi.PropertyType) + .All(t => source.HasService(t)); + } + } + + + [AttributeUsage(AttributeTargets.Property)] + public class RequiredService : Attribute + { + } + + [AttributeUsage(AttributeTargets.Property)] + public class OptionalService : Attribute + { + } +}