From 7dd4201971f1bb19ea380f2ca83aed11206cfe97 Mon Sep 17 00:00:00 2001 From: AmbulantRex <21176662+AmbulantRex@users.noreply.github.com> Date: Sun, 9 Apr 2023 10:53:09 -0600 Subject: Reconcile pre-packaged meta.json against manifest on install --- .../Plugins/PluginManager.cs | 74 ++++++++++++++++++++-- .../Updates/InstallationManager.cs | 7 +- 2 files changed, 74 insertions(+), 7 deletions(-) (limited to 'Emby.Server.Implementations') diff --git a/Emby.Server.Implementations/Plugins/PluginManager.cs b/Emby.Server.Implementations/Plugins/PluginManager.cs index 0a7c144ed..c6a7f4546 100644 --- a/Emby.Server.Implementations/Plugins/PluginManager.cs +++ b/Emby.Server.Implementations/Plugins/PluginManager.cs @@ -32,6 +32,8 @@ namespace Emby.Server.Implementations.Plugins /// public class PluginManager : IPluginManager { + private const string MetafileName = "meta.json"; + private readonly string _pluginsPath; private readonly Version _appVersion; private readonly List _assemblyLoadContexts; @@ -374,7 +376,7 @@ namespace Emby.Server.Implementations.Plugins try { var data = JsonSerializer.Serialize(manifest, _jsonOptions); - File.WriteAllText(Path.Combine(path, "meta.json"), data); + File.WriteAllText(Path.Combine(path, MetafileName), data); return true; } catch (ArgumentException e) @@ -385,7 +387,7 @@ namespace Emby.Server.Implementations.Plugins } /// - public async Task GenerateManifest(PackageInfo packageInfo, Version version, string path, PluginStatus status) + public async Task PopulateManifest(PackageInfo packageInfo, Version version, string path, PluginStatus status) { var versionInfo = packageInfo.Versions.First(v => v.Version == version.ToString()); var imagePath = string.Empty; @@ -427,13 +429,75 @@ namespace Emby.Server.Implementations.Plugins Version = versionInfo.Version, Status = status == PluginStatus.Disabled ? PluginStatus.Disabled : PluginStatus.Active, // Keep disabled state. AutoUpdate = true, - ImagePath = imagePath, - Assemblies = versionInfo.Assemblies + ImagePath = imagePath }; + var metafile = Path.Combine(Path.Combine(path, MetafileName)); + if (File.Exists(metafile)) + { + var data = File.ReadAllBytes(metafile); + var localManifest = JsonSerializer.Deserialize(data, _jsonOptions) ?? new PluginManifest(); + + // Plugin installation is the typical cause for populating a manifest. Activate. + localManifest.Status = status == PluginStatus.Disabled ? PluginStatus.Disabled : PluginStatus.Active; + + if (!Equals(localManifest.Id, manifest.Id)) + { + _logger.LogError("The manifest ID {LocalUUID} did not match the package info ID {PackageUUID}.", localManifest.Id, manifest.Id); + localManifest.Status = PluginStatus.Malfunctioned; + } + + if (localManifest.Version != manifest.Version) + { + _logger.LogWarning("The version of the local manifest was {LocalVersion}, but {PackageVersion} was expected. The value will be replaced.", localManifest.Version, manifest.Version); + + // Correct the local version. + localManifest.Version = manifest.Version; + } + + // Reconcile missing data against repository manifest. + ReconcileManifest(localManifest, manifest); + + manifest = localManifest; + } + else + { + _logger.LogInformation("No local manifest exists for plugin {Plugin}. Populating from repository manifest.", manifest.Name); + } + return SaveManifest(manifest, path); } + /// + /// Resolve the target plugin manifest against the source. Values are mapped onto the + /// target only if they are default values or empty strings. ID and status fields are ignored. + /// + /// The base to be reconciled. + /// The to reconcile against. + private void ReconcileManifest(PluginManifest baseManifest, PluginManifest projector) + { + var ignoredFields = new string[] + { + nameof(baseManifest.Id), + nameof(baseManifest.Status) + }; + + foreach (var property in baseManifest.GetType().GetProperties()) + { + var localValue = property.GetValue(baseManifest); + + if (property.PropertyType == typeof(bool) || ignoredFields.Any(s => Equals(s, property.Name))) + { + continue; + } + + if (property.PropertyType.IsNullOrDefault(localValue) || (property.PropertyType == typeof(string) && (string)localValue! == string.Empty)) + { + property.SetValue(baseManifest, property.GetValue(projector)); + } + } + } + /// /// Changes a plugin's load status. /// @@ -598,7 +662,7 @@ namespace Emby.Server.Implementations.Plugins { Version? version; PluginManifest? manifest = null; - var metafile = Path.Combine(dir, "meta.json"); + var metafile = Path.Combine(dir, MetafileName); if (File.Exists(metafile)) { // Only path where this stays null is when File.ReadAllBytes throws an IOException diff --git a/Emby.Server.Implementations/Updates/InstallationManager.cs b/Emby.Server.Implementations/Updates/InstallationManager.cs index 5e897833e..6c198b6f9 100644 --- a/Emby.Server.Implementations/Updates/InstallationManager.cs +++ b/Emby.Server.Implementations/Updates/InstallationManager.cs @@ -183,7 +183,7 @@ namespace Emby.Server.Implementations.Updates var plugin = _pluginManager.GetPlugin(package.Id, version.VersionNumber); if (plugin is not null) { - await _pluginManager.GenerateManifest(package, version.VersionNumber, plugin.Path, plugin.Manifest.Status).ConfigureAwait(false); + await _pluginManager.PopulateManifest(package, version.VersionNumber, plugin.Path, plugin.Manifest.Status).ConfigureAwait(false); } // Remove versions with a target ABI greater then the current application version. @@ -555,7 +555,10 @@ namespace Emby.Server.Implementations.Updates stream.Position = 0; using var reader = new ZipArchive(stream); reader.ExtractToDirectory(targetDir, true); - await _pluginManager.GenerateManifest(package.PackageInfo, package.Version, targetDir, status).ConfigureAwait(false); + + // Ensure we create one or populate existing ones with missing data. + await _pluginManager.PopulateManifest(package.PackageInfo, package.Version, targetDir, status); + _pluginManager.ImportPluginFrom(targetDir); } -- cgit v1.2.3