1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00
Robert d009b11e97 Core - Ensure application data folder exists
UI - Properly catch exceptions during exception display
UI - Display fatal exceptions during Core creation, even before Core initialisation
2020-03-12 19:34:53 +01:00

79 lines
2.7 KiB
C#

using System.IO;
using Artemis.Core.Exceptions;
using Artemis.Core.Models.Profile.KeyframeEngines;
using Artemis.Core.Plugins.Models;
using Artemis.Core.Services.Interfaces;
using Artemis.Storage.Repositories.Interfaces;
using LiteDB;
using Ninject.Activation;
using Ninject.Extensions.Conventions;
using Ninject.Modules;
using Serilog;
namespace Artemis.Core.Ninject
{
public class CoreModule : NinjectModule
{
public override void Load()
{
if (Kernel == null)
throw new ArtemisCoreException("Failed to bind Ninject Core module, kernel is null.");
// Bind all services as singletons
Kernel.Bind(x =>
{
x.FromThisAssembly()
.SelectAllClasses()
.InheritedFrom<IArtemisService>()
.BindAllInterfaces()
.Configure(c => c.InSingletonScope());
});
// Bind all protected services as singletons
Kernel.Bind(x =>
{
x.FromThisAssembly()
.SelectAllClasses()
.InheritedFrom<IProtectedArtemisService>()
.BindAllInterfaces()
.Configure(c => c.When(HasAccessToProtectedService).InSingletonScope());
});
Kernel.Bind<LiteRepository>().ToMethod(t =>
{
// Ensure the data folder exists
if (!Directory.Exists(Constants.DataFolder))
Directory.CreateDirectory(Constants.DataFolder);
return new LiteRepository(Constants.ConnectionString);
}).InSingletonScope();
// Bind all repositories as singletons
Kernel.Bind(x =>
{
x.FromAssemblyContaining<IRepository>()
.SelectAllClasses()
.InheritedFrom<IRepository>()
.BindAllInterfaces()
.Configure(c => c.InSingletonScope());
});
// Bind all keyframe engines
Kernel.Bind(x =>
{
x.FromAssemblyContaining<KeyframeEngine>()
.SelectAllClasses()
.InheritedFrom<KeyframeEngine>()
.BindAllBaseClasses();
});
Kernel.Bind<PluginSettings>().ToProvider<PluginSettingsProvider>();
Kernel.Bind<ILogger>().ToProvider<LoggerProvider>();
}
private bool HasAccessToProtectedService(IRequest r)
{
return r.ParentRequest != null && !r.ParentRequest.Service.Assembly.Location.StartsWith(Path.Combine(Constants.DataFolder, "plugins"));
}
}
}