diff options
Diffstat (limited to 'MediaBrowser.Common/Plugins/BasePlugin.cs')
| -rw-r--r-- | MediaBrowser.Common/Plugins/BasePlugin.cs | 123 |
1 files changed, 30 insertions, 93 deletions
diff --git a/MediaBrowser.Common/Plugins/BasePlugin.cs b/MediaBrowser.Common/Plugins/BasePlugin.cs index b75accf9b..95e076096 100644 --- a/MediaBrowser.Common/Plugins/BasePlugin.cs +++ b/MediaBrowser.Common/Plugins/BasePlugin.cs @@ -3,8 +3,6 @@ using MediaBrowser.Model.Plugins; using MediaBrowser.Model.Serialization; using System; using System.IO; -using System.Reflection; -using System.Runtime.InteropServices; namespace MediaBrowser.Common.Plugins { @@ -12,7 +10,7 @@ namespace MediaBrowser.Common.Plugins /// Provides a common base class for all plugins /// </summary> /// <typeparam name="TConfigurationType">The type of the T configuration type.</typeparam> - public abstract class BasePlugin<TConfigurationType> : IPlugin + public abstract class BasePlugin<TConfigurationType> : IPlugin, IPluginAssembly where TConfigurationType : BasePluginConfiguration { /// <summary> @@ -57,69 +55,42 @@ namespace MediaBrowser.Common.Plugins get { return typeof(TConfigurationType); } } - /// <summary> - /// The _assembly name - /// </summary> - private AssemblyName _assemblyName; - /// <summary> - /// Gets the name of the assembly. - /// </summary> - /// <value>The name of the assembly.</value> - protected AssemblyName AssemblyName + public void SetAttributes(string assemblyFilePath, string assemblyFileName, Version assemblyVersion, Guid assemblyId) { - get - { - return _assemblyName ?? (_assemblyName = GetType().Assembly.GetName()); - } + AssemblyFilePath = assemblyFilePath; + AssemblyFileName = assemblyFileName; + Version = assemblyVersion; + Id = assemblyId; } - /// <summary> - /// The _unique id - /// </summary> - private Guid? _uniqueId; + private Func<string, DateTime> _dateModifiedFn; + private Action<string> _directoryCreateFn; + public void SetStartupInfo(bool isFirstRun, Func<string, DateTime> dateModifiedFn, Action<string> directoryCreateFn) + { + IsFirstRun = isFirstRun; + + // hack alert, until the .net core transition is complete + _dateModifiedFn = dateModifiedFn; + _directoryCreateFn = directoryCreateFn; + } /// <summary> /// Gets the unique id. /// </summary> /// <value>The unique id.</value> - public Guid Id - { - get - { - - if (!_uniqueId.HasValue) - { - var attribute = (GuidAttribute)GetType().Assembly.GetCustomAttributes(typeof(GuidAttribute), true)[0]; - _uniqueId = new Guid(attribute.Value); - } - - return _uniqueId.Value; - } - } + public Guid Id { get; private set; } /// <summary> /// Gets the plugin version /// </summary> /// <value>The version.</value> - public Version Version - { - get - { - return AssemblyName.Version; - } - } + public Version Version { get; private set; } /// <summary> /// Gets the name the assembly file /// </summary> /// <value>The name of the assembly file.</value> - public string AssemblyFileName - { - get - { - return AssemblyName.Name + ".dll"; - } - } + protected string AssemblyFileName { get; private set; } /// <summary> /// Gets the last date modified of the configuration @@ -132,19 +103,7 @@ namespace MediaBrowser.Common.Plugins // Ensure it's been lazy loaded var config = Configuration; - return File.GetLastWriteTimeUtc(ConfigurationFilePath); - } - } - - /// <summary> - /// Gets the last date modified of the plugin - /// </summary> - /// <value>The assembly date last modified.</value> - public DateTime AssemblyDateLastModified - { - get - { - return File.GetLastWriteTimeUtc(AssemblyFilePath); + return _dateModifiedFn(ConfigurationFilePath); } } @@ -152,13 +111,7 @@ namespace MediaBrowser.Common.Plugins /// Gets the path to the assembly file /// </summary> /// <value>The assembly file path.</value> - public string AssemblyFilePath - { - get - { - return Path.Combine(ApplicationPaths.PluginsPath, AssemblyFileName); - } - } + public string AssemblyFilePath { get; private set; } /// <summary> /// The _configuration sync lock @@ -203,14 +156,6 @@ namespace MediaBrowser.Common.Plugins { return (TConfigurationType)XmlSerializer.DeserializeFromFile(typeof(TConfigurationType), path); } - catch (DirectoryNotFoundException) - { - return (TConfigurationType)Activator.CreateInstance(typeof(TConfigurationType)); - } - catch (FileNotFoundException) - { - return (TConfigurationType)Activator.CreateInstance(typeof(TConfigurationType)); - } catch { return (TConfigurationType)Activator.CreateInstance(typeof(TConfigurationType)); @@ -239,10 +184,6 @@ namespace MediaBrowser.Common.Plugins } /// <summary> - /// The _data folder path - /// </summary> - private string _dataFolderPath; - /// <summary> /// Gets the full path to the data folder, where the plugin can store any miscellaneous files needed /// </summary> /// <value>The data folder path.</value> @@ -250,16 +191,9 @@ namespace MediaBrowser.Common.Plugins { get { - if (_dataFolderPath == null) - { - // Give the folder name the same name as the config file name - // We can always make this configurable if/when needed - _dataFolderPath = Path.Combine(ApplicationPaths.PluginsPath, Path.GetFileNameWithoutExtension(ConfigurationFileName)); - - Directory.CreateDirectory(_dataFolderPath); - } - - return _dataFolderPath; + // Give the folder name the same name as the config file name + // We can always make this configurable if/when needed + return Path.Combine(ApplicationPaths.PluginsPath, Path.GetFileNameWithoutExtension(ConfigurationFileName)); } } @@ -272,8 +206,6 @@ namespace MediaBrowser.Common.Plugins { ApplicationPaths = applicationPaths; XmlSerializer = xmlSerializer; - - IsFirstRun = !File.Exists(ConfigurationFilePath); } /// <summary> @@ -288,7 +220,7 @@ namespace MediaBrowser.Common.Plugins { lock (_configurationSaveLock) { - Directory.CreateDirectory(Path.GetDirectoryName(ConfigurationFilePath)); + _directoryCreateFn(Path.GetDirectoryName(ConfigurationFilePath)); XmlSerializer.SerializeToFile(Configuration, ConfigurationFilePath); } @@ -349,4 +281,9 @@ namespace MediaBrowser.Common.Plugins get { return Configuration; } } } + + public interface IPluginAssembly + { + void SetAttributes(string assemblyFilePath, string assemblyFileName, Version assemblyVersion, Guid assemblyId); + } } |
