mirror of
https://github.com/SubtitleEdit/subtitleedit.git
synced 2024-11-26 05:02:36 +01:00
Fixes #7186
This commit is contained in:
parent
35b2a21d17
commit
40d839d32f
25
src/Test/Core/PluginUpdateCheckerTest.cs
Normal file
25
src/Test/Core/PluginUpdateCheckerTest.cs
Normal file
@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Nikse.SubtitleEdit.Plugins;
|
||||
|
||||
namespace Test.Core
|
||||
{
|
||||
[TestClass]
|
||||
public class PluginUpdateCheckerTest
|
||||
{
|
||||
[TestMethod]
|
||||
public async Task CheckAsyncTest()
|
||||
{
|
||||
var pluginPath = Environment.GetEnvironmentVariable("plugin_directory", EnvironmentVariableTarget.User);
|
||||
const string githubUrl = "https://raw.githubusercontent.com/SubtitleEdit/plugins/master/Plugins4.xml";
|
||||
var sut = new PluginUpdateChecker(new PluginUpdateCheckerOptions()
|
||||
{
|
||||
GithubUrl = githubUrl, PluginDirectory = pluginPath
|
||||
});
|
||||
var updateCheckResult = await sut.CheckAsync();
|
||||
|
||||
Assert.AreEqual(false, updateCheckResult.Available);
|
||||
}
|
||||
}
|
||||
}
|
@ -64,6 +64,7 @@
|
||||
<Compile Include="Assa\AssaTimeCodes.cs" />
|
||||
<Compile Include="Core\AudioToTextTest.cs" />
|
||||
<Compile Include="Core\CsvUtilTest.cs" />
|
||||
<Compile Include="Core\PluginUpdateCheckerTest.cs" />
|
||||
<Compile Include="Core\WebVttHelperTest.cs" />
|
||||
<Compile Include="Core\WebVttToAssaTest.cs" />
|
||||
<Compile Include="Dictionaries\StringWithoutSpaceSplitToWordsTest.cs" />
|
||||
|
@ -46,6 +46,7 @@ using System.Windows.Forms;
|
||||
using Nikse.SubtitleEdit.Core.AudioToText;
|
||||
using Nikse.SubtitleEdit.Forms.AudioToText;
|
||||
using Nikse.SubtitleEdit.Forms.VTT;
|
||||
using Nikse.SubtitleEdit.Plugins;
|
||||
using Timer = System.Windows.Forms.Timer;
|
||||
using MessageBox = Nikse.SubtitleEdit.Forms.SeMsgBox.MessageBox;
|
||||
|
||||
@ -32906,7 +32907,7 @@ namespace Nikse.SubtitleEdit.Forms
|
||||
return _subtitle;
|
||||
}
|
||||
|
||||
private void checkForUpdatesToolStripMenuItem_Click(object sender, EventArgs e)
|
||||
private async void checkForUpdatesToolStripMenuItem_Click(object sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
@ -32924,6 +32925,19 @@ namespace Nikse.SubtitleEdit.Forms
|
||||
form.ShowDialog(this);
|
||||
}
|
||||
|
||||
var pluginUpdateChecker = new PluginUpdateChecker(new PluginUpdateCheckerOptions()
|
||||
{
|
||||
GithubUrl = "https://raw.githubusercontent.com/SubtitleEdit/plugins/master/Plugins4.xml",
|
||||
PluginDirectory = Configuration.PluginsDirectory
|
||||
});
|
||||
|
||||
var updateCheckResult = await pluginUpdateChecker.CheckAsync();
|
||||
if (updateCheckResult.Available)
|
||||
{
|
||||
MessageBox.Show(this, string.Join(Environment.NewLine, updateCheckResult.PluginUpdates),
|
||||
"Updates Available For Plugins", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
}
|
||||
|
||||
Configuration.Settings.General.LastCheckForUpdates = DateTime.Now;
|
||||
}
|
||||
|
||||
|
103
src/ui/Plugins/LocalPluginMetadataProvider.cs
Normal file
103
src/ui/Plugins/LocalPluginMetadataProvider.cs
Normal file
@ -0,0 +1,103 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Nikse.SubtitleEdit.Plugins
|
||||
{
|
||||
public class LocalPluginMetadataProvider
|
||||
{
|
||||
private readonly string _pluginFolder;
|
||||
|
||||
public LocalPluginMetadataProvider(string pluginFolder)
|
||||
{
|
||||
_pluginFolder = pluginFolder ?? throw new ArgumentNullException(nameof(pluginFolder));
|
||||
}
|
||||
|
||||
public IReadOnlyCollection<LocalPlugin> GetPlugins()
|
||||
{
|
||||
const string plugin = "IPlugin";
|
||||
|
||||
var pluginFiles = Directory.GetFiles(_pluginFolder, "*.dll");
|
||||
|
||||
// no plugin installed
|
||||
if (pluginFiles.Length == 0)
|
||||
{
|
||||
return new List<LocalPlugin>();
|
||||
}
|
||||
|
||||
var installedPlugins = new List<LocalPlugin>();
|
||||
|
||||
foreach (var pluginFile in pluginFiles)
|
||||
{
|
||||
try
|
||||
{
|
||||
var assembly = Assembly.Load(File.ReadAllBytes(pluginFile));
|
||||
|
||||
// all plugins must implement the "IPlugin" interface
|
||||
var pluginType = assembly.GetTypes().FirstOrDefault(type => type.GetInterface(plugin) != null);
|
||||
|
||||
var pluginInfo = ParseFromType(pluginType);
|
||||
|
||||
// invalid plugin info
|
||||
if (pluginInfo is null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
installedPlugins.Add(pluginInfo);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
return installedPlugins;
|
||||
}
|
||||
|
||||
private static string[] GetKnownProperties(Type pluginType)
|
||||
{
|
||||
return pluginType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
|
||||
// .Where(prop => _contextPropMap.Contains(prop.Name))
|
||||
.Select(prop => prop.Name)
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
private static LocalPlugin ParseFromType(Type pluginType)
|
||||
{
|
||||
if (pluginType is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var knownProperties = GetKnownProperties(pluginType);
|
||||
if (knownProperties.Length < 3)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// var instance = assembly.CreateInstance(pluginType.FullName);
|
||||
var instance = Activator.CreateInstance(pluginType);
|
||||
|
||||
// read name
|
||||
var name = (string)pluginType
|
||||
.GetProperty("Text", BindingFlags.Instance | BindingFlags.Public)
|
||||
?.GetValue(instance, null);
|
||||
|
||||
// read description
|
||||
var description = (string)pluginType
|
||||
.GetProperty(nameof(PluginInfo.Description), BindingFlags.Instance | BindingFlags.Public)
|
||||
?.GetValue(instance, null);
|
||||
|
||||
// read version
|
||||
// ReSharper disable once PossibleNullReferenceException
|
||||
var version = (decimal)pluginType
|
||||
.GetProperty(nameof(PluginInfo.Version), BindingFlags.Instance | BindingFlags.Public)
|
||||
.GetValue(instance, null);
|
||||
|
||||
return new LocalPlugin(name, description, version);
|
||||
}
|
||||
}
|
||||
}
|
54
src/ui/Plugins/OnlinePluginMetadataProvider.cs
Normal file
54
src/ui/Plugins/OnlinePluginMetadataProvider.cs
Normal file
@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace Nikse.SubtitleEdit.Plugins
|
||||
{
|
||||
public class OnlinePluginMetadataProvider
|
||||
{
|
||||
private readonly string _githubUrl;
|
||||
|
||||
public OnlinePluginMetadataProvider(string githubUrl)
|
||||
{
|
||||
_githubUrl = githubUrl ?? throw new ArgumentNullException(nameof(githubUrl));
|
||||
}
|
||||
|
||||
public Task<IReadOnlyCollection<PluginInfo>> GetPluginsAsync()
|
||||
{
|
||||
// get from metadata file in github
|
||||
var xdoc = XDocument.Load(_githubUrl);
|
||||
|
||||
var pluginInfos = new List<PluginInfo>();
|
||||
foreach (var xElement in xdoc.Root.Elements("Plugin"))
|
||||
{
|
||||
var pluginInfo = ReadFormXElement(xElement);
|
||||
if (pluginInfo is null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
pluginInfos.Add(pluginInfo);
|
||||
}
|
||||
|
||||
return Task.FromResult<IReadOnlyCollection<PluginInfo>>(pluginInfos);
|
||||
}
|
||||
|
||||
private PluginInfo ReadFormXElement(XElement element)
|
||||
{
|
||||
// try parse version
|
||||
if(!decimal.TryParse(element.Element(nameof(PluginInfo.Version))?.Value, out var version))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// read name
|
||||
var name = element.Element(nameof(PluginInfo.Name))?.Value;
|
||||
|
||||
// read description
|
||||
var description = element.Element(nameof(PluginInfo.Description))?.Value;
|
||||
|
||||
return new PluginInfo(name, description, version);
|
||||
}
|
||||
}
|
||||
}
|
67
src/ui/Plugins/PluginInfo.cs
Normal file
67
src/ui/Plugins/PluginInfo.cs
Normal file
@ -0,0 +1,67 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Nikse.SubtitleEdit.Plugins
|
||||
{
|
||||
public class PluginInfo
|
||||
{
|
||||
public PluginInfo(string name, string description, decimal version)
|
||||
{
|
||||
Name = name;
|
||||
Version = version;
|
||||
Description = description;
|
||||
}
|
||||
|
||||
public string Name { get; }
|
||||
public decimal Version { get; }
|
||||
public string Description { get; }
|
||||
}
|
||||
|
||||
public class LocalPlugin : PluginInfo
|
||||
{
|
||||
public LocalPlugin(string name, string description, decimal version)
|
||||
: base(name, description, version)
|
||||
{
|
||||
}
|
||||
|
||||
public PluginUpdate CheckUpdate(IEnumerable<PluginInfo> onlinePlugins)
|
||||
{
|
||||
foreach (var onlinePlugin in onlinePlugins)
|
||||
{
|
||||
if (onlinePlugin == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!Name.Equals(onlinePlugin.Name))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
return new PluginUpdate()
|
||||
{
|
||||
Name = Name,
|
||||
OldVersion = Version,
|
||||
NewVersion = onlinePlugin.Version,
|
||||
};
|
||||
}
|
||||
|
||||
return new PluginUpdate()
|
||||
{
|
||||
Name = Name,
|
||||
OldVersion = Version,
|
||||
NewVersion = Version,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public class PluginUpdate
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public decimal OldVersion { get; set; }
|
||||
public decimal NewVersion { get; set; }
|
||||
|
||||
public bool IsNewUpdateAvailable() => OldVersion < NewVersion;
|
||||
|
||||
public override string ToString() => $"{Name} - {OldVersion} -> {NewVersion}*";
|
||||
}
|
||||
}
|
90
src/ui/Plugins/PluginUpdateChecker.cs
Normal file
90
src/ui/Plugins/PluginUpdateChecker.cs
Normal file
@ -0,0 +1,90 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Nikse.SubtitleEdit.Plugins
|
||||
{
|
||||
public class PluginUpdateChecker
|
||||
{
|
||||
private readonly LocalPluginMetadataProvider _localPluginMetadataProvider;
|
||||
private readonly OnlinePluginMetadataProvider _onlinePluginMetadataProvider;
|
||||
private DateTime _lastCheckDate;
|
||||
|
||||
/// <summary>
|
||||
/// Stores the timestamp of last time check update check was made.
|
||||
/// </summary>
|
||||
public DateTime LastCheckDate => _lastCheckDate;
|
||||
|
||||
public PluginUpdateChecker(PluginUpdateCheckerOptions options)
|
||||
{
|
||||
_localPluginMetadataProvider = new LocalPluginMetadataProvider(options.PluginDirectory);
|
||||
_onlinePluginMetadataProvider = new OnlinePluginMetadataProvider(options.GithubUrl);
|
||||
}
|
||||
|
||||
public async Task<PluginUpdateResult> CheckAsync()
|
||||
{
|
||||
_lastCheckDate = DateTime.Now;
|
||||
|
||||
var installedPlugins = _localPluginMetadataProvider.GetPlugins();
|
||||
// no plugin is currently installed
|
||||
if (!installedPlugins.Any())
|
||||
{
|
||||
return new PluginUpdateResult();
|
||||
}
|
||||
|
||||
// plugin repository
|
||||
var onlinePlugins = await _onlinePluginMetadataProvider.GetPluginsAsync();
|
||||
if (!onlinePlugins.Any())
|
||||
{
|
||||
return new PluginUpdateResult();
|
||||
}
|
||||
|
||||
var pluginUpdateResult = new PluginUpdateResult();
|
||||
foreach (var installedPlugin in installedPlugins)
|
||||
{
|
||||
var updateCheckInfo = installedPlugin.CheckUpdate(onlinePlugins);
|
||||
if (updateCheckInfo.IsNewUpdateAvailable())
|
||||
{
|
||||
pluginUpdateResult.Add(updateCheckInfo);
|
||||
}
|
||||
}
|
||||
|
||||
return pluginUpdateResult;
|
||||
}
|
||||
}
|
||||
|
||||
public class PluginUpdateResult
|
||||
{
|
||||
private ICollection<PluginUpdate> _pluginUpdates;
|
||||
|
||||
public bool Available => PluginUpdates.Any();
|
||||
|
||||
public IEnumerable<PluginUpdate> PluginUpdates => _pluginUpdates ?? Array.Empty<PluginUpdate>();
|
||||
|
||||
/// <summary>
|
||||
/// Adds available update information
|
||||
/// </summary>
|
||||
public void Add(PluginUpdate pluginUpdate)
|
||||
{
|
||||
_pluginUpdates = _pluginUpdates ?? new List<PluginUpdate>();
|
||||
_pluginUpdates.Add(pluginUpdate);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents the update checker options for a plugin.
|
||||
/// </summary>
|
||||
public class PluginUpdateCheckerOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the directory path where the plugin is located.
|
||||
/// </summary>
|
||||
public string PluginDirectory { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the GitHub URL where the plugin's updates are being tracked.
|
||||
/// </summary>
|
||||
public string GithubUrl { get; set; }
|
||||
}
|
||||
}
|
@ -101,6 +101,7 @@
|
||||
<Reference Include="System.Web.Services" />
|
||||
<Reference Include="System.Windows.Forms" />
|
||||
<Reference Include="System.Xml" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="Vosk, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\..\packages\Vosk.0.3.38\lib\netstandard2.0\Vosk.dll</HintPath>
|
||||
</Reference>
|
||||
@ -1587,6 +1588,10 @@
|
||||
<Compile Include="Logic\VideoPreviewGenerator.cs" />
|
||||
<Compile Include="Logic\WindowsHelper.cs" />
|
||||
<Compile Include="Logic\WordSpellChecker.cs" />
|
||||
<Compile Include="Plugins\LocalPluginMetadataProvider.cs" />
|
||||
<Compile Include="Plugins\OnlinePluginMetadataProvider.cs" />
|
||||
<Compile Include="Plugins\PluginInfo.cs" />
|
||||
<Compile Include="Plugins\PluginUpdateChecker.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Forms\SyncPointsSync.cs">
|
||||
@ -2585,7 +2590,6 @@
|
||||
<Visible>false</Visible>
|
||||
</HunspellAssemblies>
|
||||
</ItemGroup>
|
||||
<ItemGroup />
|
||||
<Target Name="BeforeCompile">
|
||||
<XmlPeek XmlInputPath="$(SolutionDir)\src\ui\packages.config" Query="//package[@id='NHunspell']/@version">
|
||||
<Output TaskParameter="Result" PropertyName="HunspellVersion" />
|
||||
|
Loading…
Reference in New Issue
Block a user