aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/ApplicationHost.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Emby.Server.Implementations/ApplicationHost.cs')
-rw-r--r--Emby.Server.Implementations/ApplicationHost.cs79
1 files changed, 78 insertions, 1 deletions
diff --git a/Emby.Server.Implementations/ApplicationHost.cs b/Emby.Server.Implementations/ApplicationHost.cs
index c37e87d96..d579beb2f 100644
--- a/Emby.Server.Implementations/ApplicationHost.cs
+++ b/Emby.Server.Implementations/ApplicationHost.cs
@@ -1016,6 +1016,83 @@ namespace Emby.Server.Implementations
protected abstract void RestartInternal();
/// <summary>
+ /// Comparison function used in <see cref="GetLatestDLLVersion" />.
+ /// </summary>
+ private static int VersionCompare(
+ (Version PluginVersion, string Name, string Path) a,
+ (Version PluginVersion, string Name, string Path) b)
+ {
+ int compare = string.Compare(a.Name, b.Name, true, CultureInfo.InvariantCulture);
+
+ if (compare == 0)
+ {
+ return a.PluginVersion.CompareTo(b.PluginVersion);
+ }
+
+ return compare;
+ }
+
+ /// <summary>
+ /// Only loads the latest version of each assembly based upon the folder name.
+ /// eg. MyAssembly_11.9.3.6 - will be ignored.
+ /// MyAssembly_12.2.3.6 - will be loaded.
+ /// </summary>
+ /// <param name="path">Path to enumerate.</param>
+ /// <param name="cleanup">Set to true, to try and clean up earlier versions.</param>
+ /// <returns>IEnumerable{string} of filenames.</returns>
+ protected IEnumerable<string> GetLatestDLLVersion(string path, bool cleanup = true)
+ {
+ var dllList = new List<string>();
+ var versions = new List<(Version PluginVersion, string Name, string Path)>();
+ var directories = Directory.EnumerateDirectories(path, "*.*", SearchOption.TopDirectoryOnly);
+
+ foreach (var dir in directories)
+ {
+ int p = dir.LastIndexOf('_');
+ if (p != -1 && Version.TryParse(dir.Substring(p + 1), out Version ver))
+ {
+ // Versioned folder.
+ versions.Add((ver, dir.Substring(0, p), dir));
+ }
+ else
+ {
+ // Un-versioned folder.
+ versions.Add((new Version(), dir, dir));
+ }
+ }
+
+ string lastName = string.Empty;
+ versions.Sort(VersionCompare);
+ // Traverse backwards through the list.
+ // The first item will be the latest version.
+ for (int x = versions.Count - 1; x >= 0; x--)
+ {
+ if (!string.Equals(lastName, versions[x].Name, StringComparison.OrdinalIgnoreCase))
+ {
+ dllList.AddRange(Directory.EnumerateFiles(versions[x].Path, "*.dll", SearchOption.AllDirectories));
+ lastName = versions[x].Name;
+ continue;
+ }
+
+ if (!string.IsNullOrEmpty(lastName) && cleanup)
+ {
+ // Attempt a cleanup of old folders.
+ try
+ {
+ Logger.LogDebug("Attempting to delete {0}", versions[x].Path);
+ Directory.Delete(versions[x].Path, true);
+ }
+ catch
+ {
+ // Ignore errors.
+ }
+ }
+ }
+
+ return dllList;
+ }
+
+ /// <summary>
/// Gets the composable part assemblies.
/// </summary>
/// <returns>IEnumerable{Assembly}.</returns>
@@ -1023,7 +1100,7 @@ namespace Emby.Server.Implementations
{
if (Directory.Exists(ApplicationPaths.PluginsPath))
{
- foreach (var file in Directory.EnumerateFiles(ApplicationPaths.PluginsPath, "*.dll", SearchOption.AllDirectories))
+ foreach (var file in GetLatestDLLVersion(ApplicationPaths.PluginsPath))
{
Assembly plugAss;
try