static App() { XmlConfigurator.Configure(); Logger = LogManager.GetLogger("default"); ServiceInjector.InjectServices(); }
public bool IsAvailable(Type t) { if (!ServiceInjector.IsAvailable(Global.Emulator.ServiceProvider, t)) { return(false); } var tool = Assembly .GetExecutingAssembly() .GetTypes() .FirstOrDefault(type => type == t); if (tool == null) // This isn't a tool, must not be available { return(false); } var attr = tool.GetCustomAttributes(false) .OfType <ToolAttributes>() .FirstOrDefault(); if (attr == null) // If no attributes there is no supported systems documented so assume all { return(true); } // If no supported systems mentioned assume all if (attr.SupportedSystems != null && attr.SupportedSystems.Any()) { return(attr.SupportedSystems.Contains(Global.Emulator.SystemId)); } return(true); }
public T CustomLoad <T>(bool focus = true) where T : class, IToolForm { IToolForm newTool = CreateInstance <T>(""); if (newTool == null) { return(null); } if (newTool is Form) { (newTool as Form).Owner = GlobalWin.MainForm; } ServiceInjector.UpdateServices(Global.Emulator.ServiceProvider, newTool); string toolType = typeof(T).ToString(); // auto settings if (newTool is IToolFormAutoConfig) { ToolDialogSettings settings; if (!Global.Config.CommonToolSettings.TryGetValue(toolType, out settings)) { settings = new ToolDialogSettings(); Global.Config.CommonToolSettings[toolType] = settings; } AttachSettingHooks(newTool as IToolFormAutoConfig, settings); } return((T)newTool); }
public static object getInterfaces() { Console.WriteLine($"{RTC_Core.RemoteRTC?.expectedSide.ToString()} -> getInterfaces()"); MemoryInterfaces.Clear(); ServiceInjector.UpdateServices(Global.Emulator.ServiceProvider, MDRI); foreach (MemoryDomain _domain in MDRI.MemoryDomains) { if (!MemoryInterfaces.ContainsKey(_domain.ToString())) { MemoryInterfaces.Add(_domain.ToString(), new MemoryDomainProxy(_domain)); } } MainDomain = MDRI.MemoryDomains.MainMemory.ToString(); DataSize = (MemoryInterfaces[MainDomain] as MemoryDomainProxy).md.WordSize; BigEndian = (MemoryInterfaces[MainDomain] as MemoryDomainProxy).md.EndianType == MemoryDomain.Endian.Big; //RefreshDomains(); /* * if(VmdPool.Count > 0) * foreach (string VmdKey in VmdPool.Keys) * MemoryInterfaces.Add(VmdKey, VmdPool[VmdKey]); */ return(new object[] { MemoryInterfaces.Values.ToArray(), MainDomain }); }
private static ApiContainer Register( IMainFormForApi mainForm, IEmulatorServiceProvider serviceProvider, Action <string> logCallback) { var libDict = new Dictionary <Type, IExternalApi>(); foreach (var api in Assembly.GetAssembly(typeof(ApiSubsetContainer)).GetTypes() .Concat(Assembly.GetAssembly(typeof(ApiContainer)).GetTypes()) .Where(t => /*t.IsClass && */ t.IsSealed && typeof(IExternalApi).IsAssignableFrom(t) && ServiceInjector.IsAvailable(serviceProvider, t))) { //TODO if extra params are ignored, we can use the same array for every ConstructorInfo.Invoke call --yoshi object instance; if (typeof(IEmuClientApi).IsAssignableFrom(api)) { instance = (api.GetConstructor(CtorParamTypesEmuClientApi) ?? throw new Exception("failed to call EmuClientApi's hack-filled ctor")) .Invoke(new object[] { logCallback, GlobalWin.DisplayManager, GlobalWin.InputManager, mainForm, GlobalWin.Config, GlobalWin.Emulator, GlobalWin.Game }); } else { instance = api.GetConstructor(CtorParamTypesA)?.Invoke(new object[] { logCallback, GlobalWin.DisplayManager, GlobalWin.InputManager, mainForm }) ?? api.GetConstructor(CtorParamTypesB)?.Invoke(new object[] { logCallback }) ?? Activator.CreateInstance(api); } ServiceInjector.UpdateServices(serviceProvider, instance); libDict.Add( api.GetInterfaces().First(intf => typeof(IExternalApi).IsAssignableFrom(intf) && intf != typeof(IExternalApi)), (IExternalApi)instance ); } return(new ApiContainer(libDict)); }
private static void Register(IEmulatorServiceProvider serviceProvider) { // Register external apis var apis = Assembly .Load("BizHawk.Client.ApiHawk") .GetTypes() .Where(t => typeof(IExternalApi).IsAssignableFrom(t)) .Where(t => t.IsSealed) .Where(t => ServiceInjector.IsAvailable(serviceProvider, t)) .ToList(); apis.AddRange( Assembly .GetAssembly(typeof(ApiContainer)) .GetTypes() .Where(t => typeof(IExternalApi).IsAssignableFrom(t)) .Where(t => t.IsSealed) .Where(t => ServiceInjector.IsAvailable(serviceProvider, t))); foreach (var api in apis) { var instance = (IExternalApi)Activator.CreateInstance(api); ServiceInjector.UpdateServices(serviceProvider, instance); Libraries.Add(api, instance); } container = new ApiContainer(Libraries); GlobalWin.ApiProvider = new BasicApiProvider(container); }
static void Autofac(int loop) { var builder = new ContainerBuilder(); // Register individual components var injector = new ServiceInjector(); injector.Register(new[] { typeof(Program).Assembly }); builder.RegisterInstance <IServiceInjector>(injector); builder.RegisterType <Logger>(); builder.RegisterType <Service>().PropertiesAutowired(); builder.RegisterType <TestDispose>().Keyed <IDisposable>(1).PropertiesAutowired(); builder.RegisterType <Dispose2>().Keyed <IDisposable>(2).PropertiesAutowired(); var container = builder.Build(); Console.WriteLine(nameof(Autofac)); var sw = Stopwatch.StartNew(); using var scope = container.BeginLifetimeScope(); for (var i = 0; i < loop; i++) { var test = scope.ResolveKeyed <IDisposable>(1); var test2 = scope.ResolveKeyed <IDisposable>(2); } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); }
public override void Restart(IEmulatorServiceProvider newServiceProvider) { foreach (var lib in Libraries) { ServiceInjector.UpdateServices(newServiceProvider, lib.Value); } }
private static void InjectServices() { _userService = ServiceInjector.Retrieve <IUserService>(); _apiFactory = ServiceInjector.Retrieve <IVkApiFactory>(); _configurationProvider = ServiceInjector.Retrieve <IConfigurationProvider>(); _instApiFactory = ServiceInjector.Retrieve <IInstApiFactory>(); }
public static MemoryDomainProxy[] GetInterfaces() { try { Console.WriteLine($" getInterfaces()"); List<MemoryDomainProxy> interfaces = new List<MemoryDomainProxy>(); if (Global.Emulator?.ServiceProvider == null || Global.Emulator is NullEmulator) return new MemoryDomainProxy[] { }; ServiceInjector.UpdateServices(Global.Emulator.ServiceProvider, MDRI); foreach (MemoryDomain _domain in MDRI.MemoryDomains) if(!_domain.Name.Contains("Waterbox")) //Waterbox domains are banned interfaces.Add(new MemoryDomainProxy(new VanguardImplementation.BizhawkMemoryDomain(_domain))); return interfaces.ToArray(); } catch (Exception ex) { if (VanguardCore.ShowErrorDialog(ex, true) == DialogResult.Abort) throw new AbortEverythingException(); return new MemoryDomainProxy[] { }; } }
public void Restart() { foreach (var lib in Libraries) { ServiceInjector.UpdateServices(Global.Emulator.ServiceProvider, lib.Value); } }
public CoFactorFacadeTestClassBuilder() { Bootstrapper.Init(); _util = ServiceInjector.Retrieve <IUtil>(); _inputService = ServiceInjector.Retrieve <IInputService>(); _factorService = ServiceInjector.Retrieve <IFactorService>(); }
public void Restart() { // If Cheat tool is loaded, restarting will restart the list too anyway if (!GlobalWin.Tools.Has <Cheats>()) { Global.CheatList.NewList(GenerateDefaultCheatFilename(), autosave: true); } var unavailable = new List <IToolForm>(); foreach (var tool in _tools) { if (ServiceInjector.IsAvailable(Global.Emulator.ServiceProvider, tool.GetType())) { 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(); } } else { unavailable.Add(tool); ServiceInjector.ClearServices(tool); // the services of the old emulator core are no longer valid on the tool } } foreach (var tool in unavailable) { tool.Close(); _tools.Remove(tool); } }
private void SetTools() { ToolBoxStrip.Items.Clear(); var tools = EmuHawk.ReflectionCache.Types .Where(t => typeof(IToolForm).IsAssignableFrom(t)) .Where(t => typeof(Form).IsAssignableFrom(t)) .Where(t => !typeof(ToolBox).IsAssignableFrom(t)) .Where(t => ServiceInjector.IsAvailable(Emulator.ServiceProvider, t)) .Where(t => VersionInfo.DeveloperBuild || !t.GetCustomAttributes(false).OfType <ToolAttribute>().Any(a => !a.Released)); foreach (var t in tools) { var instance = Activator.CreateInstance(t); var tsb = new ToolStripButton { Image = ((Form)instance).Icon.ToBitmap(), Text = ((Form)instance).Text, DisplayStyle = ((Form)instance).ShowIcon ? ToolStripItemDisplayStyle.Image : ToolStripItemDisplayStyle.Text }; tsb.Click += (o, e) => { Tools.Load(t); Close(); }; ToolBoxStrip.Items.Add(tsb); } }
public EmuLuaLibrary(IEmulatorServiceProvider serviceProvider) : this() { LuaWait = new AutoResetEvent(false); Docs.Clear(); // Register lua libraries var libs = Assembly .Load("BizHawk.Client.Common") .GetTypes() .Where(t => typeof(LuaLibraryBase).IsAssignableFrom(t)) .Where(t => t.IsSealed) .Where(t => ServiceInjector.IsAvailable(serviceProvider, t)) .ToList(); libs.AddRange( Assembly .GetAssembly(typeof(EmuLuaLibrary)) .GetTypes() .Where(t => typeof(LuaLibraryBase).IsAssignableFrom(t)) .Where(t => t.IsSealed) .Where(t => ServiceInjector.IsAvailable(serviceProvider, t))); foreach (var lib in libs) { bool addLibrary = true; var attributes = lib.GetCustomAttributes(typeof(LuaLibraryAttributes), false); if (attributes.Any()) { addLibrary = VersionInfo.DeveloperBuild || (attributes.First() as LuaLibraryAttributes).Released; } if (addLibrary) { var instance = (LuaLibraryBase)Activator.CreateInstance(lib, _lua); instance.LuaRegister(lib, Docs); instance.LogOutputCallback = ConsoleLuaLibrary.LogOutput; ServiceInjector.UpdateServices(serviceProvider, instance); Libraries.Add(lib, instance); } } _lua.RegisterFunction("print", this, GetType().GetMethod("Print")); EmulatorLuaLibrary.FrameAdvanceCallback = Frameadvance; EmulatorLuaLibrary.YieldCallback = EmuYield; // Add LuaCanvas to Docs Type luaCanvas = typeof(LuaCanvas); var methods = luaCanvas .GetMethods() .Where(m => m.GetCustomAttributes(typeof(LuaMethodAttributes), false).Any()); foreach (var method in methods) { Docs.Add(new LibraryFunction(nameof(LuaCanvas), luaCanvas.Description(), method)); } }
private void Initialize() { // Show the window in normal state WindowState = WindowState.Normal; ServiceInjector.InjectServices(); _coreViewModel = new CoreViewModel(); DataContext = _coreViewModel; _coreViewModel.InitializeEnvironmentVariables(); switch (_coreViewModel.InitResult) { case InitResultType.InitOk: break; case InitResultType.AccessDenied: MessageBox.Show("Cannot access Environment variables! Please restart NVM# as an Administrator.", "NVM#"); Application.Current.Shutdown(); break; case InitResultType.OtherError: MessageBox.Show("NVM# encountered an error and needs to close!", "NVM#"); Application.Current.Shutdown(); break; } UserButton.IsChecked = true; SizeChanged += (o, a) => { switch (WindowState) { case WindowState.Maximized: SplitViewMenu.Width = (int)SplitViewMenuWidth.Wide; break; case WindowState.Normal: SplitViewMenu.Width = (int)SplitViewMenuWidth.Narrow; break; } RootGrid.ColumnDefinitions[0] = new ColumnDefinition { Width = new GridLength(SplitViewMenu.Width) }; RootGrid.InvalidateVisual(); }; // Enable the tooltip for SplitView menu buttons only if the SplitView width is narrow SplitViewMenu.SizeChanged += (o, a) => { var isNarrowMenu = (int)SplitViewMenu.Width == (int)SplitViewMenuWidth.Narrow; ToolTipService.SetIsEnabled(UserButton, isNarrowMenu); ToolTipService.SetIsEnabled(SystemButton, isNarrowMenu); ToolTipService.SetIsEnabled(ImportButton, isNarrowMenu); ToolTipService.SetIsEnabled(ExportButton, isNarrowMenu); ToolTipService.SetIsEnabled(AboutButton, isNarrowMenu); }; }
static App() { XmlConfigurator.Configure(); Logger = LogManager.GetLogger("default"); // Create service model to ensure available services ServiceInjector.InjectServices(); }
public void GettingUninitialisedService_ThrowsException() { var serviceInjector = new ServiceInjector <IMockService>(); var e = Assert.Throws <InvalidOperationException>(() => serviceInjector.Service); Assert.Equal("The service 'IMockService' has not been injected into the container.", e.Message); }
public MainWindow() { InitializeComponent(); ServiceInjector.InjectServices(); // Start-up services Loaded += MainWindow_LoadedAsync; }
public void Update(IEmulatorServiceProvider newServiceProvider) { ServiceInjector.UpdateServices(newServiceProvider, this); foreach (var provider in apiProviders) { ServiceInjector.UpdateServices(newServiceProvider, provider); provider.Update(); } }
public void GettingService_ReturnsSpecifiedService() { var serviceInjector = new ServiceInjector <IMockService>(); var service = new MockService(); serviceInjector.Service = service; Assert.Equal(service, serviceInjector.Service); }
public void HasValue_IsTrue_WhenServiceIsInjected() { var serviceInjector = new ServiceInjector <IMockService>(); var service = new MockService(); serviceInjector.Service = service; Assert.True(serviceInjector.HasValue); }
/// <summary>Loads the external tool's entry form.</summary> public IExternalToolForm LoadExternalToolForm(string toolPath, string customFormTypeName, bool focus = true) { var existingTool = _tools.OfType <IExternalToolForm>().FirstOrDefault(t => t.GetType().Assembly.Location == toolPath); if (existingTool != null) { if (!existingTool.IsDisposed) { if (focus) { existingTool.Show(); existingTool.Focus(); } return(existingTool); } _tools.Remove(existingTool); } var newTool = (IExternalToolForm)CreateInstance(typeof(IExternalToolForm), toolPath, customFormTypeName); if (newTool == null) { return(null); } if (newTool is Form form) { form.Owner = _owner; } ApiInjector.UpdateApis(_apiProvider, newTool); ServiceInjector.UpdateServices(_emulator.ServiceProvider, newTool); SetBaseProperties(newTool); // auto settings if (newTool is IToolFormAutoConfig autoConfigTool) { AttachSettingHooks( autoConfigTool, _config.CommonToolSettings.TryGetValue(customFormTypeName, out var settings) ? settings : (_config.CommonToolSettings[customFormTypeName] = new ToolDialogSettings()) ); } // custom settings if (HasCustomConfig(newTool)) { InstallCustomConfig( newTool, _config.CustomToolSettings.TryGetValue(customFormTypeName, out var settings) ? settings : (_config.CustomToolSettings[customFormTypeName] = new Dictionary <string, object>()) ); } newTool.Restart(); newTool.Show(); return(newTool); }
private static void InjectServices() { _likesService = ServiceInjector.Retrieve <ILikesService>(); _apiFactory = ServiceInjector.Retrieve <IVkApiFactory>(); _photoService = ServiceInjector.Retrieve <IPhotosService>(); _configurationProvider = ServiceInjector.Retrieve <IConfigurationProvider>(); _likeClickerService = ServiceInjector.Retrieve <ILikeClickerService>(); _userService = ServiceInjector.Retrieve <UserService>(); }
public bool IsAvailable(Type t) { if (!ServiceInjector.IsAvailable(Global.Emulator.ServiceProvider, t)) { return(false); } if (t == typeof(LuaConsole) && PlatformLinkedLibSingleton.RunningOnUnix) { return(false); } var tool = Assembly .GetExecutingAssembly() .GetTypes() .FirstOrDefault(type => type == t); if (tool == null) // This isn't a tool, must not be available { return(false); } var attr = tool.GetCustomAttributes(false) .OfType <ToolAttribute>() .FirstOrDefault(); // start with the assumption that if no supported systems are mentioned and no unsupported cores are mentioned // then this is available for all bool supported = true; if (attr?.SupportedSystems != null && attr.SupportedSystems.Any()) { // supported systems are available supported = attr.SupportedSystems.Contains(Global.Emulator.SystemId); if (supported) { // check for a core not supported override if (attr.UnsupportedCores.Contains(Global.Emulator.DisplayName())) { supported = false; } } } else if (attr?.UnsupportedCores != null && attr.UnsupportedCores.Any()) { // no supported system specified, but unsupported cores are if (attr.UnsupportedCores.Contains(Global.Emulator.DisplayName())) { supported = false; } } return(supported); }
private void SetTools() { ToolBoxStrip.Items.Clear(); foreach (var t in Assembly.GetAssembly(GetType()).GetTypes()) { if (!typeof(IToolForm).IsAssignableFrom(t)) { continue; } if (!typeof(Form).IsAssignableFrom(t)) { continue; } if (typeof(ToolBox).IsAssignableFrom(t)) //yo dawg i head you like toolboxes { continue; } if (VersionInfo.DeveloperBuild && t.GetCustomAttributes(false).OfType <ToolAttribute>().Any(a => !a.Released)) { continue; } if (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 { continue; } if (!ServiceInjector.IsAvailable(Emulator.ServiceProvider, t)) { continue; } // Skip this tool on linux. It isnt finished and it causes exceptions if (t == typeof(HexView) && BizHawk.Common.PlatformLinkedLibSingleton.RunningOnUnix) { continue; } var instance = Activator.CreateInstance(t); var tsb = new ToolStripButton { Image = (instance as Form).Icon.ToBitmap(), Text = (instance as Form).Text, DisplayStyle = (instance as Form).ShowIcon ? ToolStripItemDisplayStyle.Image : ToolStripItemDisplayStyle.Text }; tsb.Click += (o, e) => { GlobalWin.Tools.Load(t); Close(); }; ToolBoxStrip.Items.Add(tsb); } }
public static IServiceCollection AddServiceInjector(this IServiceCollection services, IEnumerable <Assembly> assemblies, out IServiceInjector injector) { var concreteInjector = new ServiceInjector(); injector = concreteInjector; concreteInjector.Register(assemblies); return(services.AddSingleton(injector)); }
private static void InjectServices() { _groupService = ServiceInjector.Retrieve <IGroupSerice>(); _userService = ServiceInjector.Retrieve <IUserService>(); _apiFactory = ServiceInjector.Retrieve <IVkApiFactory>(); _messagesService = ServiceInjector.Retrieve <IMessagesService>(); _citiesService = ServiceInjector.Retrieve <ICitiesService>(); _configurationProvider = ServiceInjector.Retrieve <IConfigurationProvider>(); _likeClickerService = ServiceInjector.Retrieve <ILikeClickerService>(); _cacheService = ServiceInjector.Retrieve <ICacheService>(); }
protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); MainWindow window = new MainWindow(); ServiceInjector.InjectServices(); ViewModelBase mainViewModel = new MainWindowViewModel(); window.DataContext = mainViewModel; window.Show(); }
public void SettingServiceMultipleTimes_ThrowsException() { var serviceInjector = new ServiceInjector <IMockService>(); var service1 = new MockService(); var service2 = new MockService(); serviceInjector.Service = service1; var e = Assert.Throws <InvalidOperationException>(() => serviceInjector.Service = service2); Assert.Equal("The 'IMockService' service has already been injected into the container.", e.Message); }