factor out serviceinjector to emulation.common

This commit is contained in:
goyuken 2014-12-17 18:17:16 +00:00
parent 844f66f9db
commit ecf8728de6
18 changed files with 93 additions and 60 deletions

View File

@ -1216,7 +1216,7 @@ namespace BizHawk.Client.EmuHawk
NesControllerSettingsMenuItem.Enabled = (Global.Emulator is NES || Global.Emulator is QuickNES) && !Global.MovieSession.Movie.IsActive; NesControllerSettingsMenuItem.Enabled = (Global.Emulator is NES || Global.Emulator is QuickNES) && !Global.MovieSession.Movie.IsActive;
MovieSettingsMenuItem.Enabled = Global.Emulator is NES && !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) private void FdsControlsMenuItem_DropDownOpened(object sender, EventArgs e)

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Windows.Forms; using System.Windows.Forms;
using BizHawk.Client.Common; using BizHawk.Client.Common;
using BizHawk.Emulation.Cores.Nintendo.NES; using BizHawk.Emulation.Cores.Nintendo.NES;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.EmuHawk namespace BizHawk.Client.EmuHawk
{ {

View File

@ -8,6 +8,7 @@ using BizHawk.Client.Common;
using BizHawk.Emulation.Cores.Nintendo.Gameboy; using BizHawk.Emulation.Cores.Nintendo.Gameboy;
using BizHawk.Client.EmuHawk.WinFormExtensions; using BizHawk.Client.EmuHawk.WinFormExtensions;
using System.Collections.Generic; using System.Collections.Generic;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.EmuHawk namespace BizHawk.Client.EmuHawk
{ {

View File

@ -7,6 +7,7 @@ using BizHawk.Client.Common;
using BizHawk.Emulation.Cores.Nintendo.Gameboy; using BizHawk.Emulation.Cores.Nintendo.Gameboy;
using BizHawk.Emulation.Cores.Nintendo.GBA; using BizHawk.Emulation.Cores.Nintendo.GBA;
using System.Collections.Generic; using System.Collections.Generic;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.EmuHawk namespace BizHawk.Client.EmuHawk
{ {

View File

@ -9,6 +9,7 @@ using System.Windows.Forms;
using BizHawk.Client.Common; using BizHawk.Client.Common;
using BizHawk.Emulation.Cores.Consoles.Sega.gpgx; using BizHawk.Emulation.Cores.Consoles.Sega.gpgx;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.EmuHawk namespace BizHawk.Client.EmuHawk
{ {

View File

@ -46,14 +46,4 @@ namespace BizHawk.Client.EmuHawk
bool IsDisposed { get; } bool IsDisposed { get; }
bool IsHandleCreated { get; } bool IsHandleCreated { get; }
} }
[AttributeUsage(AttributeTargets.Property)]
public class RequiredService : Attribute
{
}
[AttributeUsage(AttributeTargets.Property)]
public class OptionalService : Attribute
{
}
} }

View File

@ -49,12 +49,14 @@ namespace BizHawk.Client.EmuHawk
Docs.Clear(); Docs.Clear();
_caller = passed.Get(); _caller = passed.Get();
// what was this?
/*
var tt = typeof(TastudioLuaLibrary); var tt = typeof(TastudioLuaLibrary);
var mm = typeof(MainMemoryLuaLibrary); var mm = typeof(MainMemoryLuaLibrary);
var tatt = tt.GetCustomAttributes(typeof(LuaLibraryAttributes), false); var tatt = tt.GetCustomAttributes(typeof(LuaLibraryAttributes), false);
var matt = mm.GetCustomAttributes(typeof(LuaLibraryAttributes), false); var matt = mm.GetCustomAttributes(typeof(LuaLibraryAttributes), false);
*/
// Register lua libraries // Register lua libraries
var libs = Assembly var libs = Assembly

View File

@ -9,6 +9,7 @@ using System.Windows.Forms;
using BizHawk.Emulation.Cores.Nintendo.NES; using BizHawk.Emulation.Cores.Nintendo.NES;
using BizHawk.Common; using BizHawk.Common;
using BizHawk.Client.Common; using BizHawk.Client.Common;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.EmuHawk namespace BizHawk.Client.EmuHawk
{ {

View File

@ -5,6 +5,7 @@ using System.Drawing.Imaging;
using System.Windows.Forms; using System.Windows.Forms;
using BizHawk.Client.Common; using BizHawk.Client.Common;
using BizHawk.Emulation.Cores.PCEngine; using BizHawk.Emulation.Cores.PCEngine;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.EmuHawk namespace BizHawk.Client.EmuHawk
{ {

View File

@ -13,6 +13,7 @@ using BizHawk.Client.Common;
using BizHawk.Emulation.Cores.PCEngine; using BizHawk.Emulation.Cores.PCEngine;
using BizHawk.Emulation.Common.Components; using BizHawk.Emulation.Common.Components;
using BizHawk.Emulation.Common;
using ICSharpCode.SharpZipLib.Zip; using ICSharpCode.SharpZipLib.Zip;

View File

@ -10,6 +10,7 @@ using System.Windows.Forms;
using BizHawk.Client.Common; using BizHawk.Client.Common;
using BizHawk.Emulation.Cores.PCEngine; using BizHawk.Emulation.Cores.PCEngine;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.EmuHawk namespace BizHawk.Client.EmuHawk
{ {

View File

@ -10,6 +10,7 @@ using BizHawk.Emulation.Cores.Sega.MasterSystem;
using BizHawk.Common; using BizHawk.Common;
using BizHawk.Client.Common; using BizHawk.Client.Common;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.EmuHawk namespace BizHawk.Client.EmuHawk
{ {

View File

@ -33,6 +33,7 @@ using BizHawk.Common.NumberExtensions;
using BizHawk.Client.Common; using BizHawk.Client.Common;
using BizHawk.Emulation.Cores.Nintendo.SNES; using BizHawk.Emulation.Cores.Nintendo.SNES;
using BizHawk.Client.EmuHawk; //TODO: What?? using BizHawk.Client.EmuHawk; //TODO: What??
using BizHawk.Emulation.Common;
namespace BizHawk.Client.EmuHawk namespace BizHawk.Client.EmuHawk
{ {

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Windows.Forms; using System.Windows.Forms;
using BizHawk.Client.Common; using BizHawk.Client.Common;
using BizHawk.Emulation.Cores.Calculators; using BizHawk.Emulation.Cores.Calculators;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.EmuHawk namespace BizHawk.Client.EmuHawk
{ {

View File

@ -62,7 +62,7 @@ namespace BizHawk.Client.EmuHawk
.Where(t => VersionInfo.DeveloperBuild ? true : !(t.GetCustomAttributes(false) .Where(t => VersionInfo.DeveloperBuild ? true : !(t.GetCustomAttributes(false)
.OfType<ToolAttributes>().Any(a => !a.Released))) .OfType<ToolAttributes>().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 => !(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(t => Activator.CreateInstance(t))
.Select(instance => new .Select(instance => new
{ {

View File

@ -6,6 +6,8 @@ using System.Reflection;
using BizHawk.Emulation.Common.IEmulatorExtensions; using BizHawk.Emulation.Common.IEmulatorExtensions;
using BizHawk.Client.Common; using BizHawk.Client.Common;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.EmuHawk namespace BizHawk.Client.EmuHawk
{ {
@ -33,7 +35,7 @@ namespace BizHawk.Client.EmuHawk
if (!typeof(IToolForm).IsAssignableFrom(toolType)) if (!typeof(IToolForm).IsAssignableFrom(toolType))
throw new ArgumentException(String.Format("Type {0} does not implement IToolForm.", toolType.Name)); 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; return null;
var existingTool = _tools.FirstOrDefault(x => toolType.IsAssignableFrom(x.GetType())); var existingTool = _tools.FirstOrDefault(x => toolType.IsAssignableFrom(x.GetType()));
@ -54,53 +56,13 @@ namespace BizHawk.Client.EmuHawk
var newTool = CreateInstance(toolType); var newTool = CreateInstance(toolType);
UpdateServices(newTool); ServiceInjector.UpdateServices(Global.Emulator.ServiceProvider, newTool);
newTool.Restart(); newTool.Restart();
newTool.Show(); newTool.Show();
return newTool; return newTool;
} }
private static IEnumerable<PropertyInfo> GetPropertiesWithAttr(Type type, Type attributeType)
{
return type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic)
.Where(p => p.GetCustomAttributes(attributeType, false).Length > 0);
}
/// <summary>
/// Feeds the tool its required services.
/// </summary>
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 });
}
}
/// <summary>
/// Determines whether a tool is available, considering its dependencies
/// and the services provided by the emulator core.
/// </summary>
public bool IsAvailable(Type toolType)
{
return GetPropertiesWithAttr(toolType, typeof(RequiredService))
.Select(pi => pi.PropertyType)
.All(t => Global.Emulator.ServiceProvider.HasService(t));
}
/// <summary> /// <summary>
/// Determines whether a given IToolForm is already loaded /// Determines whether a given IToolForm is already loaded
/// </summary> /// </summary>
@ -198,9 +160,9 @@ namespace BizHawk.Client.EmuHawk
foreach (var tool in _tools) 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 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(); tool.Restart();

View File

@ -90,6 +90,7 @@
<Compile Include="Interfaces\IVideoProvider.cs" /> <Compile Include="Interfaces\IVideoProvider.cs" />
<Compile Include="MemoryDomain.cs" /> <Compile Include="MemoryDomain.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ServiceInjector.cs" />
<Compile Include="Sound\HuC6280PSG.cs" /> <Compile Include="Sound\HuC6280PSG.cs" />
<Compile Include="Sound\MMC5Audio.cs" /> <Compile Include="Sound\MMC5Audio.cs" />
<Compile Include="Sound\SN76489.cs" /> <Compile Include="Sound\SN76489.cs" />

View File

@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace BizHawk.Emulation.Common
{
/// <summary>
/// injects services into other classes
/// </summary>
public static class ServiceInjector
{
private static IEnumerable<PropertyInfo> GetPropertiesWithAttr(Type type, Type attributeType)
{
return type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic)
.Where(p => p.GetCustomAttributes(attributeType, false).Length > 0);
}
/// <summary>
/// Feeds the target its required services.
/// </summary>
/// <returns>false if update failed</returns>
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;
}
/// <summary>
/// Determines whether a target is available, considering its dependencies
/// and the services provided by the emulator core.
/// </summary>
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
{
}
}