mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-05-03 23:36:38 +01:00
More DI
This commit is contained in:
@@ -1,304 +0,0 @@
|
||||
using System;
|
||||
using System.Configuration;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
|
||||
namespace MediaBrowser.Common.Kernel
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides a base class to hold common application paths used by both the Ui and Server.
|
||||
/// This can be subclassed to add application-specific paths.
|
||||
/// </summary>
|
||||
public abstract class BaseApplicationPaths
|
||||
{
|
||||
/// <summary>
|
||||
/// The _program data path
|
||||
/// </summary>
|
||||
private string _programDataPath;
|
||||
/// <summary>
|
||||
/// Gets the path to the program data folder
|
||||
/// </summary>
|
||||
/// <value>The program data path.</value>
|
||||
public string ProgramDataPath
|
||||
{
|
||||
get
|
||||
{
|
||||
return _programDataPath ?? (_programDataPath = GetProgramDataPath());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The _data directory
|
||||
/// </summary>
|
||||
private string _dataDirectory;
|
||||
/// <summary>
|
||||
/// Gets the folder path to the data directory
|
||||
/// </summary>
|
||||
/// <value>The data directory.</value>
|
||||
public string DataPath
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_dataDirectory == null)
|
||||
{
|
||||
_dataDirectory = Path.Combine(ProgramDataPath, "data");
|
||||
|
||||
if (!Directory.Exists(_dataDirectory))
|
||||
{
|
||||
Directory.CreateDirectory(_dataDirectory);
|
||||
}
|
||||
}
|
||||
|
||||
return _dataDirectory;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The _image cache path
|
||||
/// </summary>
|
||||
private string _imageCachePath;
|
||||
/// <summary>
|
||||
/// Gets the image cache path.
|
||||
/// </summary>
|
||||
/// <value>The image cache path.</value>
|
||||
public string ImageCachePath
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_imageCachePath == null)
|
||||
{
|
||||
_imageCachePath = Path.Combine(CachePath, "images");
|
||||
|
||||
if (!Directory.Exists(_imageCachePath))
|
||||
{
|
||||
Directory.CreateDirectory(_imageCachePath);
|
||||
}
|
||||
}
|
||||
|
||||
return _imageCachePath;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The _plugins path
|
||||
/// </summary>
|
||||
private string _pluginsPath;
|
||||
/// <summary>
|
||||
/// Gets the path to the plugin directory
|
||||
/// </summary>
|
||||
/// <value>The plugins path.</value>
|
||||
public string PluginsPath
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_pluginsPath == null)
|
||||
{
|
||||
_pluginsPath = Path.Combine(ProgramDataPath, "plugins");
|
||||
if (!Directory.Exists(_pluginsPath))
|
||||
{
|
||||
Directory.CreateDirectory(_pluginsPath);
|
||||
}
|
||||
}
|
||||
|
||||
return _pluginsPath;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The _plugin configurations path
|
||||
/// </summary>
|
||||
private string _pluginConfigurationsPath;
|
||||
/// <summary>
|
||||
/// Gets the path to the plugin configurations directory
|
||||
/// </summary>
|
||||
/// <value>The plugin configurations path.</value>
|
||||
public string PluginConfigurationsPath
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_pluginConfigurationsPath == null)
|
||||
{
|
||||
_pluginConfigurationsPath = Path.Combine(PluginsPath, "configurations");
|
||||
if (!Directory.Exists(_pluginConfigurationsPath))
|
||||
{
|
||||
Directory.CreateDirectory(_pluginConfigurationsPath);
|
||||
}
|
||||
}
|
||||
|
||||
return _pluginConfigurationsPath;
|
||||
}
|
||||
}
|
||||
|
||||
private string _tempUpdatePath;
|
||||
/// <summary>
|
||||
/// Gets the path to where temporary update files will be stored
|
||||
/// </summary>
|
||||
/// <value>The plugin configurations path.</value>
|
||||
public string TempUpdatePath
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_tempUpdatePath == null)
|
||||
{
|
||||
_tempUpdatePath = Path.Combine(ProgramDataPath, "Updates");
|
||||
if (!Directory.Exists(_tempUpdatePath))
|
||||
{
|
||||
Directory.CreateDirectory(_tempUpdatePath);
|
||||
}
|
||||
}
|
||||
|
||||
return _tempUpdatePath;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The _log directory path
|
||||
/// </summary>
|
||||
private string _logDirectoryPath;
|
||||
/// <summary>
|
||||
/// Gets the path to the log directory
|
||||
/// </summary>
|
||||
/// <value>The log directory path.</value>
|
||||
public string LogDirectoryPath
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_logDirectoryPath == null)
|
||||
{
|
||||
_logDirectoryPath = Path.Combine(ProgramDataPath, "logs");
|
||||
if (!Directory.Exists(_logDirectoryPath))
|
||||
{
|
||||
Directory.CreateDirectory(_logDirectoryPath);
|
||||
}
|
||||
}
|
||||
return _logDirectoryPath;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The _configuration directory path
|
||||
/// </summary>
|
||||
private string _configurationDirectoryPath;
|
||||
/// <summary>
|
||||
/// Gets the path to the application configuration root directory
|
||||
/// </summary>
|
||||
/// <value>The configuration directory path.</value>
|
||||
public string ConfigurationDirectoryPath
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_configurationDirectoryPath == null)
|
||||
{
|
||||
_configurationDirectoryPath = Path.Combine(ProgramDataPath, "config");
|
||||
if (!Directory.Exists(_configurationDirectoryPath))
|
||||
{
|
||||
Directory.CreateDirectory(_configurationDirectoryPath);
|
||||
}
|
||||
}
|
||||
return _configurationDirectoryPath;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The _system configuration file path
|
||||
/// </summary>
|
||||
private string _systemConfigurationFilePath;
|
||||
/// <summary>
|
||||
/// Gets the path to the system configuration file
|
||||
/// </summary>
|
||||
/// <value>The system configuration file path.</value>
|
||||
public string SystemConfigurationFilePath
|
||||
{
|
||||
get
|
||||
{
|
||||
return _systemConfigurationFilePath ?? (_systemConfigurationFilePath = Path.Combine(ConfigurationDirectoryPath, "system.xml"));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The _cache directory
|
||||
/// </summary>
|
||||
private string _cachePath;
|
||||
/// <summary>
|
||||
/// Gets the folder path to the cache directory
|
||||
/// </summary>
|
||||
/// <value>The cache directory.</value>
|
||||
public string CachePath
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_cachePath == null)
|
||||
{
|
||||
_cachePath = Path.Combine(ProgramDataPath, "cache");
|
||||
|
||||
if (!Directory.Exists(_cachePath))
|
||||
{
|
||||
Directory.CreateDirectory(_cachePath);
|
||||
}
|
||||
}
|
||||
|
||||
return _cachePath;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The _temp directory
|
||||
/// </summary>
|
||||
private string _tempDirectory;
|
||||
/// <summary>
|
||||
/// Gets the folder path to the temp directory within the cache folder
|
||||
/// </summary>
|
||||
/// <value>The temp directory.</value>
|
||||
public string TempDirectory
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_tempDirectory == null)
|
||||
{
|
||||
_tempDirectory = Path.Combine(CachePath, "temp");
|
||||
|
||||
if (!Directory.Exists(_tempDirectory))
|
||||
{
|
||||
Directory.CreateDirectory(_tempDirectory);
|
||||
}
|
||||
}
|
||||
|
||||
return _tempDirectory;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the path to the application's ProgramDataFolder
|
||||
/// </summary>
|
||||
/// <returns>System.String.</returns>
|
||||
public static string GetProgramDataPath()
|
||||
{
|
||||
#if DEBUG
|
||||
string programDataPath = ConfigurationManager.AppSettings["DebugProgramDataPath"];
|
||||
|
||||
#else
|
||||
string programDataPath = Path.Combine(ConfigurationManager.AppSettings["ReleaseProgramDataPath"], ConfigurationManager.AppSettings["ProgramDataFolderName"]);
|
||||
#endif
|
||||
|
||||
programDataPath = programDataPath.Replace("%CommonApplicationData%", Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData));
|
||||
|
||||
// If it's a relative path, e.g. "..\"
|
||||
if (!Path.IsPathRooted(programDataPath))
|
||||
{
|
||||
var path = Assembly.GetExecutingAssembly().Location;
|
||||
path = Path.GetDirectoryName(path);
|
||||
|
||||
programDataPath = Path.Combine(path, programDataPath);
|
||||
|
||||
programDataPath = Path.GetFullPath(programDataPath);
|
||||
}
|
||||
|
||||
if (!Directory.Exists(programDataPath))
|
||||
{
|
||||
Directory.CreateDirectory(programDataPath);
|
||||
}
|
||||
|
||||
return programDataPath;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,16 +2,14 @@
|
||||
using MediaBrowser.Common.Net;
|
||||
using MediaBrowser.Common.Plugins;
|
||||
using MediaBrowser.Common.ScheduledTasks;
|
||||
using MediaBrowser.Common.Serialization;
|
||||
using MediaBrowser.Model.Configuration;
|
||||
using MediaBrowser.Model.Logging;
|
||||
using MediaBrowser.Model.Serialization;
|
||||
using MediaBrowser.Model.System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -24,7 +22,7 @@ namespace MediaBrowser.Common.Kernel
|
||||
/// <typeparam name="TApplicationPathsType">The type of the T application paths type.</typeparam>
|
||||
public abstract class BaseKernel<TConfigurationType, TApplicationPathsType> : IDisposable, IKernel
|
||||
where TConfigurationType : BaseApplicationConfiguration, new()
|
||||
where TApplicationPathsType : BaseApplicationPaths, new()
|
||||
where TApplicationPathsType : IApplicationPaths
|
||||
{
|
||||
/// <summary>
|
||||
/// Occurs when [has pending restart changed].
|
||||
@@ -129,7 +127,7 @@ namespace MediaBrowser.Common.Kernel
|
||||
get
|
||||
{
|
||||
// Lazy load
|
||||
LazyInitializer.EnsureInitialized(ref _configuration, ref _configurationLoaded, ref _configurationSyncLock, () => XmlSerializer.GetXmlConfiguration<TConfigurationType>(ApplicationPaths.SystemConfigurationFilePath, Logger));
|
||||
LazyInitializer.EnsureInitialized(ref _configuration, ref _configurationLoaded, ref _configurationSyncLock, () => GetXmlConfiguration<TConfigurationType>(ApplicationPaths.SystemConfigurationFilePath));
|
||||
return _configuration;
|
||||
}
|
||||
protected set
|
||||
@@ -161,19 +159,6 @@ namespace MediaBrowser.Common.Kernel
|
||||
/// <value>The application paths.</value>
|
||||
public TApplicationPathsType ApplicationPaths { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// The _failed assembly loads
|
||||
/// </summary>
|
||||
private readonly List<string> _failedPluginAssemblies = new List<string>();
|
||||
/// <summary>
|
||||
/// Gets the plugin assemblies that failed to load.
|
||||
/// </summary>
|
||||
/// <value>The failed assembly loads.</value>
|
||||
public IEnumerable<string> FailedPluginAssemblies
|
||||
{
|
||||
get { return _failedPluginAssemblies; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the list of currently loaded plugins
|
||||
/// </summary>
|
||||
@@ -204,46 +189,6 @@ namespace MediaBrowser.Common.Kernel
|
||||
/// <value>The rest services.</value>
|
||||
public IEnumerable<IRestfulService> RestServices { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// The disposable parts
|
||||
/// </summary>
|
||||
private readonly List<IDisposable> _disposableParts = new List<IDisposable>();
|
||||
|
||||
/// <summary>
|
||||
/// The _protobuf serializer initialized
|
||||
/// </summary>
|
||||
private bool _protobufSerializerInitialized;
|
||||
/// <summary>
|
||||
/// The _protobuf serializer sync lock
|
||||
/// </summary>
|
||||
private object _protobufSerializerSyncLock = new object();
|
||||
/// <summary>
|
||||
/// Gets a dynamically compiled generated serializer that can serialize protocontracts without reflection
|
||||
/// </summary>
|
||||
private DynamicProtobufSerializer _protobufSerializer;
|
||||
/// <summary>
|
||||
/// Gets the protobuf serializer.
|
||||
/// </summary>
|
||||
/// <value>The protobuf serializer.</value>
|
||||
public DynamicProtobufSerializer ProtobufSerializer
|
||||
{
|
||||
get
|
||||
{
|
||||
// Lazy load
|
||||
LazyInitializer.EnsureInitialized(ref _protobufSerializer, ref _protobufSerializerInitialized, ref _protobufSerializerSyncLock, () => DynamicProtobufSerializer.Create(AllTypes));
|
||||
return _protobufSerializer;
|
||||
}
|
||||
private set
|
||||
{
|
||||
_protobufSerializer = value;
|
||||
|
||||
if (value == null)
|
||||
{
|
||||
_protobufSerializerInitialized = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the UDP server port number.
|
||||
/// This can't be configurable because then the user would have to configure their client to discover the server.
|
||||
@@ -301,42 +246,40 @@ namespace MediaBrowser.Common.Kernel
|
||||
protected IApplicationHost ApplicationHost { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the task manager.
|
||||
/// The _XML serializer
|
||||
/// </summary>
|
||||
/// <value>The task manager.</value>
|
||||
protected ITaskManager TaskManager { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the assemblies.
|
||||
/// </summary>
|
||||
/// <value>The assemblies.</value>
|
||||
protected Assembly[] Assemblies { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets all types.
|
||||
/// </summary>
|
||||
/// <value>All types.</value>
|
||||
public Type[] AllTypes { get; private set; }
|
||||
private readonly IXmlSerializer _xmlSerializer;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="BaseKernel{TApplicationPathsType}" /> class.
|
||||
/// </summary>
|
||||
/// <param name="appHost">The app host.</param>
|
||||
/// <param name="appPaths">The app paths.</param>
|
||||
/// <param name="xmlSerializer">The XML serializer.</param>
|
||||
/// <param name="logger">The logger.</param>
|
||||
/// <exception cref="System.ArgumentNullException">isoManager</exception>
|
||||
protected BaseKernel(IApplicationHost appHost, ILogger logger)
|
||||
protected BaseKernel(IApplicationHost appHost, TApplicationPathsType appPaths, IXmlSerializer xmlSerializer, ILogger logger)
|
||||
{
|
||||
if (appHost == null)
|
||||
{
|
||||
throw new ArgumentNullException("appHost");
|
||||
}
|
||||
|
||||
if (appPaths == null)
|
||||
{
|
||||
throw new ArgumentNullException("appPaths");
|
||||
}
|
||||
if (xmlSerializer == null)
|
||||
{
|
||||
throw new ArgumentNullException("xmlSerializer");
|
||||
}
|
||||
if (logger == null)
|
||||
{
|
||||
throw new ArgumentNullException("logger");
|
||||
}
|
||||
|
||||
ApplicationPaths = appPaths;
|
||||
ApplicationHost = appHost;
|
||||
_xmlSerializer = xmlSerializer;
|
||||
Logger = logger;
|
||||
}
|
||||
|
||||
@@ -344,14 +287,12 @@ namespace MediaBrowser.Common.Kernel
|
||||
/// Initializes the Kernel
|
||||
/// </summary>
|
||||
/// <returns>Task.</returns>
|
||||
public async Task Init()
|
||||
public Task Init()
|
||||
{
|
||||
ApplicationPaths = new TApplicationPathsType();
|
||||
|
||||
IsFirstRun = !File.Exists(ApplicationPaths.SystemConfigurationFilePath);
|
||||
|
||||
// Performs initializations that can be reloaded at anytime
|
||||
await Reload().ConfigureAwait(false);
|
||||
return Reload();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -377,7 +318,6 @@ namespace MediaBrowser.Common.Kernel
|
||||
{
|
||||
// Set these to null so that they can be lazy loaded again
|
||||
Configuration = null;
|
||||
ProtobufSerializer = null;
|
||||
|
||||
ReloadLogger();
|
||||
|
||||
@@ -388,14 +328,12 @@ namespace MediaBrowser.Common.Kernel
|
||||
|
||||
await OnConfigurationLoaded().ConfigureAwait(false);
|
||||
|
||||
DisposeTaskManager();
|
||||
TaskManager = new TaskManager(Logger);
|
||||
FindParts();
|
||||
|
||||
Logger.Info("Loading Plugins");
|
||||
await ReloadComposableParts().ConfigureAwait(false);
|
||||
await OnComposablePartsLoaded().ConfigureAwait(false);
|
||||
|
||||
DisposeTcpManager();
|
||||
TcpManager = new TcpManager(ApplicationHost, this, ApplicationHost.Resolve<INetworkManager>(), Logger);
|
||||
TcpManager = (TcpManager)ApplicationHost.CreateInstance(typeof(TcpManager));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -417,184 +355,14 @@ namespace MediaBrowser.Common.Kernel
|
||||
OnLoggerLoaded();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Uses MEF to locate plugins
|
||||
/// Subclasses can use this to locate types within plugins
|
||||
/// </summary>
|
||||
/// <returns>Task.</returns>
|
||||
private async Task ReloadComposableParts()
|
||||
{
|
||||
_failedPluginAssemblies.Clear();
|
||||
|
||||
DisposeComposableParts();
|
||||
|
||||
Assemblies = GetComposablePartAssemblies().ToArray();
|
||||
|
||||
AllTypes = Assemblies.SelectMany(GetTypes).ToArray();
|
||||
|
||||
ComposeParts(AllTypes);
|
||||
|
||||
await OnComposablePartsLoaded().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Composes the parts.
|
||||
/// </summary>
|
||||
/// <param name="allTypes">All types.</param>
|
||||
private void ComposeParts(IEnumerable<Type> allTypes)
|
||||
{
|
||||
var concreteTypes = allTypes.Where(t => t.IsClass && !t.IsAbstract && !t.IsInterface && !t.IsGenericType).ToArray();
|
||||
|
||||
RegisterExportedValues();
|
||||
|
||||
FindParts(concreteTypes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Composes the parts with ioc container.
|
||||
/// </summary>
|
||||
/// <param name="allTypes">All types.</param>
|
||||
protected virtual void FindParts(Type[] allTypes)
|
||||
protected virtual void FindParts()
|
||||
{
|
||||
RestServices = GetExports<IRestfulService>(allTypes);
|
||||
WebSocketListeners = GetExports<IWebSocketListener>(allTypes);
|
||||
Plugins = GetExports<IPlugin>(allTypes);
|
||||
|
||||
var tasks = GetExports<IScheduledTask>(allTypes, false);
|
||||
|
||||
TaskManager.AddTasks(tasks);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the exports.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="allTypes">All types.</param>
|
||||
/// <param name="manageLiftime">if set to <c>true</c> [manage liftime].</param>
|
||||
/// <returns>IEnumerable{``0}.</returns>
|
||||
protected IEnumerable<T> GetExports<T>(Type[] allTypes, bool manageLiftime = true)
|
||||
{
|
||||
var currentType = typeof(T);
|
||||
|
||||
Logger.Info("Composing instances of " + currentType.Name);
|
||||
|
||||
var parts = allTypes.Where(currentType.IsAssignableFrom).Select(Instantiate).Cast<T>().ToArray();
|
||||
|
||||
if (manageLiftime)
|
||||
{
|
||||
_disposableParts.AddRange(parts.OfType<IDisposable>());
|
||||
}
|
||||
|
||||
return parts;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Instantiates the specified type.
|
||||
/// </summary>
|
||||
/// <param name="type">The type.</param>
|
||||
/// <returns>System.Object.</returns>
|
||||
private object Instantiate(Type type)
|
||||
{
|
||||
return ApplicationHost.CreateInstance(type);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Composes the exported values.
|
||||
/// </summary>
|
||||
/// <param name="container">The container.</param>
|
||||
protected virtual void RegisterExportedValues()
|
||||
{
|
||||
ApplicationHost.RegisterSingleInstance<IKernel>(this);
|
||||
ApplicationHost.RegisterSingleInstance(TaskManager);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the composable part assemblies.
|
||||
/// </summary>
|
||||
/// <returns>IEnumerable{Assembly}.</returns>
|
||||
protected virtual IEnumerable<Assembly> GetComposablePartAssemblies()
|
||||
{
|
||||
// Gets all plugin assemblies by first reading all bytes of the .dll and calling Assembly.Load against that
|
||||
// This will prevent the .dll file from getting locked, and allow us to replace it when needed
|
||||
var pluginAssemblies = Directory.EnumerateFiles(ApplicationPaths.PluginsPath, "*.dll", SearchOption.TopDirectoryOnly)
|
||||
.Select(file =>
|
||||
{
|
||||
try
|
||||
{
|
||||
return Assembly.Load(File.ReadAllBytes((file)));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_failedPluginAssemblies.Add(file);
|
||||
Logger.ErrorException("Error loading {0}", ex, file);
|
||||
return null;
|
||||
}
|
||||
|
||||
}).Where(a => a != null);
|
||||
|
||||
foreach (var pluginAssembly in pluginAssemblies)
|
||||
{
|
||||
yield return pluginAssembly;
|
||||
}
|
||||
|
||||
var runningDirectory = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
|
||||
var corePluginDirectory = Path.Combine(runningDirectory, "CorePlugins");
|
||||
|
||||
// This will prevent the .dll file from getting locked, and allow us to replace it when needed
|
||||
pluginAssemblies = Directory.EnumerateFiles(corePluginDirectory, "*.dll", SearchOption.TopDirectoryOnly)
|
||||
.Select(file =>
|
||||
{
|
||||
try
|
||||
{
|
||||
return Assembly.Load(File.ReadAllBytes((file)));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_failedPluginAssemblies.Add(file);
|
||||
Logger.ErrorException("Error loading {0}", ex, file);
|
||||
return null;
|
||||
}
|
||||
|
||||
}).Where(a => a != null);
|
||||
|
||||
foreach (var pluginAssembly in pluginAssemblies)
|
||||
{
|
||||
yield return pluginAssembly;
|
||||
}
|
||||
|
||||
// Include composable parts in the Model assembly
|
||||
yield return typeof(SystemInfo).Assembly;
|
||||
|
||||
// Include composable parts in the Common assembly
|
||||
yield return Assembly.GetExecutingAssembly();
|
||||
|
||||
// Include composable parts in the subclass assembly
|
||||
yield return GetType().Assembly;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a list of types within an assembly
|
||||
/// This will handle situations that would normally throw an exception - such as a type within the assembly that depends on some other non-existant reference
|
||||
/// </summary>
|
||||
/// <param name="assembly">The assembly.</param>
|
||||
/// <returns>IEnumerable{Type}.</returns>
|
||||
/// <exception cref="System.ArgumentNullException">assembly</exception>
|
||||
private static IEnumerable<Type> GetTypes(Assembly assembly)
|
||||
{
|
||||
if (assembly == null)
|
||||
{
|
||||
throw new ArgumentNullException("assembly");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return assembly.GetTypes();
|
||||
}
|
||||
catch (ReflectionTypeLoadException ex)
|
||||
{
|
||||
// If it fails we can still get a list of the Types it was able to resolve
|
||||
return ex.Types.Where(t => t != null);
|
||||
}
|
||||
RestServices = ApplicationHost.GetExports<IRestfulService>();
|
||||
WebSocketListeners = ApplicationHost.GetExports<IWebSocketListener>();
|
||||
Plugins = ApplicationHost.GetExports<IPlugin>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -612,7 +380,7 @@ namespace MediaBrowser.Common.Kernel
|
||||
|
||||
try
|
||||
{
|
||||
plugin.Initialize(this, Logger);
|
||||
plugin.Initialize(this, _xmlSerializer, Logger);
|
||||
|
||||
Logger.Info("{0} {1} initialized.", plugin.Name, plugin.Version);
|
||||
}
|
||||
@@ -654,12 +422,7 @@ namespace MediaBrowser.Common.Kernel
|
||||
if (dispose)
|
||||
{
|
||||
DisposeTcpManager();
|
||||
DisposeTaskManager();
|
||||
DisposeHttpManager();
|
||||
|
||||
DisposeComposableParts();
|
||||
|
||||
_disposableParts.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -675,18 +438,6 @@ namespace MediaBrowser.Common.Kernel
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disposes the task manager.
|
||||
/// </summary>
|
||||
private void DisposeTaskManager()
|
||||
{
|
||||
if (TaskManager != null)
|
||||
{
|
||||
TaskManager.Dispose();
|
||||
TaskManager = null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disposes the HTTP manager.
|
||||
/// </summary>
|
||||
@@ -699,17 +450,6 @@ namespace MediaBrowser.Common.Kernel
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disposes all objects gathered through MEF composable parts
|
||||
/// </summary>
|
||||
protected virtual void DisposeComposableParts()
|
||||
{
|
||||
foreach (var part in _disposableParts)
|
||||
{
|
||||
part.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the current application version
|
||||
/// </summary>
|
||||
@@ -761,7 +501,7 @@ namespace MediaBrowser.Common.Kernel
|
||||
IsNetworkDeployed = ApplicationHost.CanSelfUpdate,
|
||||
WebSocketPortNumber = TcpManager.WebSocketPortNumber,
|
||||
SupportsNativeWebSocket = TcpManager.SupportsNativeWebSocket,
|
||||
FailedPluginAssemblies = FailedPluginAssemblies.ToArray()
|
||||
FailedPluginAssemblies = ApplicationHost.FailedAssemblies.ToArray()
|
||||
};
|
||||
}
|
||||
|
||||
@@ -777,7 +517,7 @@ namespace MediaBrowser.Common.Kernel
|
||||
{
|
||||
lock (_configurationSaveLock)
|
||||
{
|
||||
XmlSerializer.SerializeToFile(Configuration, ApplicationPaths.SystemConfigurationFilePath);
|
||||
_xmlSerializer.SerializeToFile(Configuration, ApplicationPaths.SystemConfigurationFilePath);
|
||||
}
|
||||
|
||||
OnConfigurationUpdated();
|
||||
@@ -787,7 +527,7 @@ namespace MediaBrowser.Common.Kernel
|
||||
/// Gets the application paths.
|
||||
/// </summary>
|
||||
/// <value>The application paths.</value>
|
||||
BaseApplicationPaths IKernel.ApplicationPaths
|
||||
IApplicationPaths IKernel.ApplicationPaths
|
||||
{
|
||||
get { return ApplicationPaths; }
|
||||
}
|
||||
@@ -798,6 +538,63 @@ namespace MediaBrowser.Common.Kernel
|
||||
BaseApplicationConfiguration IKernel.Configuration
|
||||
{
|
||||
get { return Configuration; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads an xml configuration file from the file system
|
||||
/// It will immediately re-serialize and save if new serialization data is available due to property changes
|
||||
/// </summary>
|
||||
/// <param name="type">The type.</param>
|
||||
/// <param name="path">The path.</param>
|
||||
/// <returns>System.Object.</returns>
|
||||
public object GetXmlConfiguration(Type type, string path)
|
||||
{
|
||||
Logger.Info("Loading {0} at {1}", type.Name, path);
|
||||
|
||||
object configuration;
|
||||
|
||||
byte[] buffer = null;
|
||||
|
||||
// Use try/catch to avoid the extra file system lookup using File.Exists
|
||||
try
|
||||
{
|
||||
buffer = File.ReadAllBytes(path);
|
||||
|
||||
configuration = _xmlSerializer.DeserializeFromBytes(type, buffer);
|
||||
}
|
||||
catch (FileNotFoundException)
|
||||
{
|
||||
configuration = ApplicationHost.CreateInstance(type);
|
||||
}
|
||||
|
||||
// Take the object we just got and serialize it back to bytes
|
||||
var newBytes = _xmlSerializer.SerializeToBytes(configuration);
|
||||
|
||||
// If the file didn't exist before, or if something has changed, re-save
|
||||
if (buffer == null || !buffer.SequenceEqual(newBytes))
|
||||
{
|
||||
Logger.Info("Saving {0} to {1}", type.Name, path);
|
||||
|
||||
// Save it after load in case we got new items
|
||||
File.WriteAllBytes(path, newBytes);
|
||||
}
|
||||
|
||||
return configuration;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Reads an xml configuration file from the file system
|
||||
/// It will immediately save the configuration after loading it, just
|
||||
/// in case there are new serializable properties
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="path">The path.</param>
|
||||
/// <returns>``0.</returns>
|
||||
private T GetXmlConfiguration<T>(string path)
|
||||
where T : class
|
||||
{
|
||||
return GetXmlConfiguration(typeof(T), path) as T;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using MediaBrowser.Model.Updates;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -32,6 +33,26 @@ namespace MediaBrowser.Common.Kernel
|
||||
/// <value><c>true</c> if this instance can self update; otherwise, <c>false</c>.</value>
|
||||
bool CanSelfUpdate { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the failed assemblies.
|
||||
/// </summary>
|
||||
/// <value>The failed assemblies.</value>
|
||||
IEnumerable<string> FailedAssemblies { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets all concrete types.
|
||||
/// </summary>
|
||||
/// <value>All concrete types.</value>
|
||||
Type[] AllConcreteTypes { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the exports.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="manageLiftime">if set to <c>true</c> [manage liftime].</param>
|
||||
/// <returns>IEnumerable{``0}.</returns>
|
||||
IEnumerable<T> GetExports<T>(bool manageLiftime = true);
|
||||
|
||||
/// <summary>
|
||||
/// Checks for update.
|
||||
/// </summary>
|
||||
|
||||
76
MediaBrowser.Common/Kernel/IApplicationPaths.cs
Normal file
76
MediaBrowser.Common/Kernel/IApplicationPaths.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
|
||||
namespace MediaBrowser.Common.Kernel
|
||||
{
|
||||
/// <summary>
|
||||
/// Interface IApplicationPaths
|
||||
/// </summary>
|
||||
public interface IApplicationPaths
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the path to the program data folder
|
||||
/// </summary>
|
||||
/// <value>The program data path.</value>
|
||||
string ProgramDataPath { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the folder path to the data directory
|
||||
/// </summary>
|
||||
/// <value>The data directory.</value>
|
||||
string DataPath { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the image cache path.
|
||||
/// </summary>
|
||||
/// <value>The image cache path.</value>
|
||||
string ImageCachePath { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the path to the plugin directory
|
||||
/// </summary>
|
||||
/// <value>The plugins path.</value>
|
||||
string PluginsPath { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the path to the plugin configurations directory
|
||||
/// </summary>
|
||||
/// <value>The plugin configurations path.</value>
|
||||
string PluginConfigurationsPath { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the path to where temporary update files will be stored
|
||||
/// </summary>
|
||||
/// <value>The plugin configurations path.</value>
|
||||
string TempUpdatePath { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the path to the log directory
|
||||
/// </summary>
|
||||
/// <value>The log directory path.</value>
|
||||
string LogDirectoryPath { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the path to the application configuration root directory
|
||||
/// </summary>
|
||||
/// <value>The configuration directory path.</value>
|
||||
string ConfigurationDirectoryPath { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the path to the system configuration file
|
||||
/// </summary>
|
||||
/// <value>The system configuration file path.</value>
|
||||
string SystemConfigurationFilePath { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the folder path to the cache directory
|
||||
/// </summary>
|
||||
/// <value>The cache directory.</value>
|
||||
string CachePath { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the folder path to the temp directory within the cache folder
|
||||
/// </summary>
|
||||
/// <value>The temp directory.</value>
|
||||
string TempDirectory { get; }
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
using MediaBrowser.Common.Net;
|
||||
using MediaBrowser.Common.Plugins;
|
||||
using MediaBrowser.Common.Serialization;
|
||||
using MediaBrowser.Model.Configuration;
|
||||
using MediaBrowser.Model.System;
|
||||
using System;
|
||||
@@ -18,7 +17,7 @@ namespace MediaBrowser.Common.Kernel
|
||||
/// Gets the application paths.
|
||||
/// </summary>
|
||||
/// <value>The application paths.</value>
|
||||
BaseApplicationPaths ApplicationPaths { get; }
|
||||
IApplicationPaths ApplicationPaths { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the configuration.
|
||||
@@ -32,12 +31,6 @@ namespace MediaBrowser.Common.Kernel
|
||||
/// <value>The kernel context.</value>
|
||||
KernelContext KernelContext { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the protobuf serializer.
|
||||
/// </summary>
|
||||
/// <value>The protobuf serializer.</value>
|
||||
DynamicProtobufSerializer ProtobufSerializer { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Inits this instance.
|
||||
/// </summary>
|
||||
@@ -156,5 +149,13 @@ namespace MediaBrowser.Common.Kernel
|
||||
/// Notifies the pending restart.
|
||||
/// </summary>
|
||||
void NotifyPendingRestart();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the XML configuration.
|
||||
/// </summary>
|
||||
/// <param name="type">The type.</param>
|
||||
/// <param name="path">The path.</param>
|
||||
/// <returns>System.Object.</returns>
|
||||
object GetXmlConfiguration(Type type, string path);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using MediaBrowser.Common.Net;
|
||||
using MediaBrowser.Common.Serialization;
|
||||
using MediaBrowser.Model.Configuration;
|
||||
using MediaBrowser.Model.Logging;
|
||||
using System;
|
||||
@@ -14,6 +13,7 @@ using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Model.Serialization;
|
||||
|
||||
namespace MediaBrowser.Common.Kernel
|
||||
{
|
||||
@@ -35,6 +35,12 @@ namespace MediaBrowser.Common.Kernel
|
||||
/// <value>The HTTP server.</value>
|
||||
private IHttpServer HttpServer { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the json serializer.
|
||||
/// </summary>
|
||||
/// <value>The json serializer.</value>
|
||||
private IJsonSerializer _jsonSerializer;
|
||||
|
||||
/// <summary>
|
||||
/// This subscribes to HttpListener requests and finds the appropriate BaseHandler to process it
|
||||
/// </summary>
|
||||
@@ -96,8 +102,10 @@ namespace MediaBrowser.Common.Kernel
|
||||
/// <param name="applicationHost">The application host.</param>
|
||||
/// <param name="kernel">The kernel.</param>
|
||||
/// <param name="networkManager">The network manager.</param>
|
||||
/// <param name="jsonSerializer">The json serializer.</param>
|
||||
/// <param name="logger">The logger.</param>
|
||||
public TcpManager(IApplicationHost applicationHost, IKernel kernel, INetworkManager networkManager, ILogger logger)
|
||||
/// <exception cref="System.ArgumentNullException">applicationHost</exception>
|
||||
public TcpManager(IApplicationHost applicationHost, IKernel kernel, INetworkManager networkManager, IJsonSerializer jsonSerializer, ILogger logger)
|
||||
{
|
||||
if (applicationHost == null)
|
||||
{
|
||||
@@ -111,12 +119,17 @@ namespace MediaBrowser.Common.Kernel
|
||||
{
|
||||
throw new ArgumentNullException("networkManager");
|
||||
}
|
||||
if (jsonSerializer == null)
|
||||
{
|
||||
throw new ArgumentNullException("jsonSerializer");
|
||||
}
|
||||
if (logger == null)
|
||||
{
|
||||
throw new ArgumentNullException("logger");
|
||||
}
|
||||
|
||||
_logger = logger;
|
||||
_jsonSerializer = jsonSerializer;
|
||||
_kernel = kernel;
|
||||
_applicationHost = applicationHost;
|
||||
_networkManager = networkManager;
|
||||
@@ -203,7 +216,7 @@ namespace MediaBrowser.Common.Kernel
|
||||
/// <param name="e">The <see cref="WebSocketConnectEventArgs" /> instance containing the event data.</param>
|
||||
void HttpServer_WebSocketConnected(object sender, WebSocketConnectEventArgs e)
|
||||
{
|
||||
var connection = new WebSocketConnection(e.WebSocket, e.Endpoint, ProcessWebSocketMessageReceived, _logger);
|
||||
var connection = new WebSocketConnection(e.WebSocket, e.Endpoint, ProcessWebSocketMessageReceived, _jsonSerializer, _logger);
|
||||
|
||||
_webSocketConnections.Add(connection);
|
||||
}
|
||||
@@ -342,7 +355,7 @@ namespace MediaBrowser.Common.Kernel
|
||||
_logger.Info("Sending web socket message {0}", messageType);
|
||||
|
||||
var message = new WebSocketMessage<T> { MessageType = messageType, Data = dataFunction() };
|
||||
var bytes = JsonSerializer.SerializeToBytes(message);
|
||||
var bytes = _jsonSerializer.SerializeToBytes(message);
|
||||
|
||||
var tasks = connections.Select(s => Task.Run(() =>
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user