Rewrite plugin loader/updater to support loading from .zip and more complex versions.
This commit is contained in:
@@ -14,12 +14,12 @@ namespace Torch.API.Managers
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Fired when plugins are loaded.
|
/// Fired when plugins are loaded.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
event Action<IList<ITorchPlugin>> PluginsLoaded;
|
event Action<ICollection<ITorchPlugin>> PluginsLoaded;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Collection of loaded plugins.
|
/// Collection of loaded plugins.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
IList<ITorchPlugin> Plugins { get; }
|
IDictionary<Guid, ITorchPlugin> Plugins { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Updates all loaded plugins.
|
/// Updates all loaded plugins.
|
||||||
|
@@ -17,7 +17,7 @@ namespace Torch.API.Plugins
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// The version of the plugin.
|
/// The version of the plugin.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
Version Version { get; }
|
string Version { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The name of the plugin.
|
/// The name of the plugin.
|
||||||
|
@@ -10,6 +10,7 @@ namespace Torch.API.Plugins
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Indicates that the given type should be loaded by the plugin manager as a plugin.
|
/// Indicates that the given type should be loaded by the plugin manager as a plugin.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[Obsolete]
|
||||||
[AttributeUsage(AttributeTargets.Class)]
|
[AttributeUsage(AttributeTargets.Class)]
|
||||||
public class PluginAttribute : Attribute
|
public class PluginAttribute : Attribute
|
||||||
{
|
{
|
||||||
|
@@ -29,7 +29,7 @@ namespace Torch.Server.ViewModels
|
|||||||
pluginManager.PluginsLoaded += PluginManager_PluginsLoaded;
|
pluginManager.PluginsLoaded += PluginManager_PluginsLoaded;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void PluginManager_PluginsLoaded(IList<ITorchPlugin> obj)
|
private void PluginManager_PluginsLoaded(ICollection<ITorchPlugin> obj)
|
||||||
{
|
{
|
||||||
Plugins.Clear();
|
Plugins.Clear();
|
||||||
foreach (var plugin in obj)
|
foreach (var plugin in obj)
|
||||||
|
@@ -2,51 +2,21 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Torch
|
namespace Torch
|
||||||
{
|
{
|
||||||
public static class StringExtensions
|
public static class StringExtensions
|
||||||
{
|
{
|
||||||
public static string Truncate(this string s, int maxLength)
|
/// <summary>
|
||||||
|
/// Try to extract a 3 component version from the string. Format: #.#.#
|
||||||
|
/// </summary>
|
||||||
|
public static bool TryExtractVersion(this string version, out Version result)
|
||||||
{
|
{
|
||||||
return s.Length <= maxLength ? s : s.Substring(0, maxLength);
|
result = null;
|
||||||
}
|
var match = Regex.Match(version, @"(\d+\.)?(\d+\.)?(\d+)");
|
||||||
|
return match.Success && Version.TryParse(match.Value, out result);
|
||||||
public static IEnumerable<string> ReadLines(this string s, int max, bool skipEmpty = false, char delim = '\n')
|
|
||||||
{
|
|
||||||
var lines = s.Split(delim);
|
|
||||||
|
|
||||||
for (var i = 0; i < lines.Length && i < max; i++)
|
|
||||||
{
|
|
||||||
var l = lines[i];
|
|
||||||
if (skipEmpty && string.IsNullOrWhiteSpace(l))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
yield return l;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static string Wrap(this string s, int lineLength)
|
|
||||||
{
|
|
||||||
if (s.Length <= lineLength)
|
|
||||||
return s;
|
|
||||||
|
|
||||||
var result = new StringBuilder();
|
|
||||||
for (var i = 0; i < s.Length;)
|
|
||||||
{
|
|
||||||
var next = i + lineLength;
|
|
||||||
if (s.Length - 1 < next)
|
|
||||||
{
|
|
||||||
result.AppendLine(s.Substring(i));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
result.AppendLine(s.Substring(i, next));
|
|
||||||
i = next;
|
|
||||||
}
|
|
||||||
|
|
||||||
return result.ToString();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,34 +1,37 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.IO.Compression;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using System.Xml.Serialization;
|
||||||
using NLog;
|
using NLog;
|
||||||
|
using Octokit;
|
||||||
using Torch.API;
|
using Torch.API;
|
||||||
using Torch.API.Managers;
|
using Torch.API.Managers;
|
||||||
using Torch.API.Plugins;
|
using Torch.API.Plugins;
|
||||||
|
using Torch.Collections;
|
||||||
using Torch.Commands;
|
using Torch.Commands;
|
||||||
using VRage.Collections;
|
|
||||||
|
|
||||||
namespace Torch.Managers
|
namespace Torch.Managers
|
||||||
{
|
{
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public class PluginManager : Manager, IPluginManager
|
public class PluginManager : Manager, IPluginManager
|
||||||
{
|
{
|
||||||
|
private GitHubClient _gitClient = new GitHubClient(new ProductHeaderValue("Torch"));
|
||||||
private static Logger _log = LogManager.GetLogger(nameof(PluginManager));
|
private static Logger _log = LogManager.GetLogger(nameof(PluginManager));
|
||||||
|
private const string MANIFEST_NAME = "manifest.xml";
|
||||||
public readonly string PluginDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Plugins");
|
public readonly string PluginDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Plugins");
|
||||||
[Dependency]
|
[Dependency]
|
||||||
private UpdateManager _updateManager;
|
|
||||||
[Dependency]
|
|
||||||
private CommandManager _commandManager;
|
private CommandManager _commandManager;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public IList<ITorchPlugin> Plugins { get; } = new ObservableList<ITorchPlugin>();
|
public IDictionary<Guid, ITorchPlugin> Plugins { get; } = new ObservableDictionary<Guid, ITorchPlugin>();
|
||||||
|
|
||||||
public event Action<IList<ITorchPlugin>> PluginsLoaded;
|
public event Action<ICollection<ITorchPlugin>> PluginsLoaded;
|
||||||
|
|
||||||
public PluginManager(ITorchBase torchInstance) : base(torchInstance)
|
public PluginManager(ITorchBase torchInstance) : base(torchInstance)
|
||||||
{
|
{
|
||||||
@@ -41,7 +44,7 @@ namespace Torch.Managers
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public void UpdatePlugins()
|
public void UpdatePlugins()
|
||||||
{
|
{
|
||||||
foreach (var plugin in Plugins)
|
foreach (var plugin in Plugins.Values)
|
||||||
plugin.Update();
|
plugin.Update();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,94 +53,257 @@ namespace Torch.Managers
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public override void Detach()
|
public override void Detach()
|
||||||
{
|
{
|
||||||
foreach (var plugin in Plugins)
|
foreach (var plugin in Plugins.Values)
|
||||||
plugin.Dispose();
|
plugin.Dispose();
|
||||||
|
|
||||||
Plugins.Clear();
|
Plugins.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void DownloadPlugins()
|
|
||||||
{
|
|
||||||
var folders = Directory.GetDirectories(PluginDir);
|
|
||||||
var taskList = new List<Task>();
|
|
||||||
|
|
||||||
//Copy list because we don't want to modify the config.
|
|
||||||
var toDownload = Torch.Config.Plugins.ToList();
|
|
||||||
|
|
||||||
foreach (var folder in folders)
|
|
||||||
{
|
|
||||||
var manifestPath = Path.Combine(folder, "manifest.xml");
|
|
||||||
if (!File.Exists(manifestPath))
|
|
||||||
{
|
|
||||||
_log.Debug($"No manifest in {folder}, skipping");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var manifest = PluginManifest.Load(manifestPath);
|
|
||||||
toDownload.RemoveAll(x => string.Compare(manifest.Repository, x, StringComparison.InvariantCultureIgnoreCase) == 0);
|
|
||||||
taskList.Add(_updateManager.CheckAndUpdatePlugin(manifest));
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var repository in toDownload)
|
|
||||||
{
|
|
||||||
var manifest = new PluginManifest {Repository = repository, Version = "0.0"};
|
|
||||||
taskList.Add(_updateManager.CheckAndUpdatePlugin(manifest));
|
|
||||||
}
|
|
||||||
|
|
||||||
Task.WaitAll(taskList.ToArray());
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public void LoadPlugins()
|
public void LoadPlugins()
|
||||||
{
|
{
|
||||||
if (Torch.Config.ShouldUpdatePlugins)
|
DownloadPluginUpdates();
|
||||||
DownloadPlugins();
|
_log.Info("Loading plugins...");
|
||||||
else
|
var pluginItems = Directory.EnumerateFiles(PluginDir, "*.zip").Union(Directory.EnumerateDirectories(PluginDir));
|
||||||
_log.Warn("Automatic plugin updates are disabled.");
|
foreach (var item in pluginItems)
|
||||||
|
|
||||||
_log.Info("Loading plugins");
|
|
||||||
var dlls = Directory.GetFiles(PluginDir, "*.dll", SearchOption.AllDirectories);
|
|
||||||
foreach (var dllPath in dlls)
|
|
||||||
{
|
{
|
||||||
_log.Debug($"Loading plugin {dllPath}");
|
var path = Path.Combine(PluginDir, item);
|
||||||
var asm = Assembly.UnsafeLoadFrom(dllPath);
|
var isZip = item.EndsWith(".zip", StringComparison.CurrentCultureIgnoreCase);
|
||||||
|
var manifest = isZip ? GetManifestFromZip(path) : GetManifestFromDirectory(path);
|
||||||
foreach (var type in asm.GetExportedTypes())
|
if (manifest == null)
|
||||||
{
|
{
|
||||||
if (type.GetInterfaces().Contains(typeof(ITorchPlugin)))
|
_log.Warn($"Item '{item}' is missing a manifest, skipping.");
|
||||||
{
|
|
||||||
if (type.GetCustomAttribute<PluginAttribute>() == null)
|
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Plugins.ContainsKey(manifest.Guid))
|
||||||
|
{
|
||||||
|
_log.Error($"The GUID provided by {manifest.Name} ({item}) is already in use by {Plugins[manifest.Guid].Name}");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isZip)
|
||||||
|
LoadPluginFromZip(path);
|
||||||
|
else
|
||||||
|
LoadPluginFromFolder(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
Plugins.ForEach(x => x.Value.Init(Torch));
|
||||||
|
_log.Info($"Loaded {Plugins.Count} plugins.");
|
||||||
|
PluginsLoaded?.Invoke(Plugins.Values);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void DownloadPluginUpdates()
|
||||||
|
{
|
||||||
|
_log.Info("Checking for plugin updates...");
|
||||||
|
var count = 0;
|
||||||
|
var pluginItems = Directory.EnumerateFiles(PluginDir, "*.zip").Union(Directory.EnumerateDirectories(PluginDir));
|
||||||
|
Parallel.ForEach(pluginItems, async item =>
|
||||||
|
{
|
||||||
|
var path = Path.Combine(PluginDir, item);
|
||||||
|
var isZip = item.EndsWith(".zip", StringComparison.CurrentCultureIgnoreCase);
|
||||||
|
var manifest = isZip ? GetManifestFromZip(path) : GetManifestFromDirectory(path);
|
||||||
|
if (manifest == null)
|
||||||
|
{
|
||||||
|
_log.Warn($"Item '{item}' is missing a manifest, skipping update check.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
manifest.Version.TryExtractVersion(out Version currentVersion);
|
||||||
|
var latest = await GetLatestArchiveAsync(manifest.Repository).ConfigureAwait(false);
|
||||||
|
|
||||||
|
if (currentVersion == null || latest.Item1 == null)
|
||||||
|
{
|
||||||
|
_log.Error($"Error parsing version from manifest or GitHub for plugin '{manifest.Name}.'");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (latest.Item1 <= currentVersion)
|
||||||
|
return;
|
||||||
|
|
||||||
|
_log.Info($"Updating plugin '{manifest.Name}' from {currentVersion} to {latest.Item1}.");
|
||||||
|
await UpdatePlugin(path, latest.Item2).ConfigureAwait(false);
|
||||||
|
count++;
|
||||||
|
});
|
||||||
|
|
||||||
|
_log.Info($"Updated {count} plugins.");
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<Tuple<Version, string>> GetLatestArchiveAsync(string repository)
|
||||||
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var plugin = (TorchPluginBase)Activator.CreateInstance(type);
|
var split = repository.Split('/');
|
||||||
if (plugin.Id == default(Guid))
|
var latest = await _gitClient.Repository.Release.GetLatest(split[0], split[1]).ConfigureAwait(false);
|
||||||
throw new TypeLoadException($"Plugin '{type.FullName}' is missing a {nameof(PluginAttribute)}");
|
if (!latest.TagName.TryExtractVersion(out Version latestVersion))
|
||||||
|
{
|
||||||
|
_log.Error($"Unable to parse version tag for the latest release of '{repository}.'");
|
||||||
|
}
|
||||||
|
|
||||||
_log.Info($"Loading plugin {plugin.Name} ({plugin.Version})");
|
var zipAsset = latest.Assets.FirstOrDefault(x => x.Name.Contains(".zip", StringComparison.CurrentCultureIgnoreCase));
|
||||||
plugin.StoragePath = Torch.Config.InstancePath;
|
if (zipAsset == null)
|
||||||
Plugins.Add(plugin);
|
{
|
||||||
|
_log.Error($"Unable to find archive for the latest release of '{repository}.'");
|
||||||
|
}
|
||||||
|
|
||||||
_commandManager.RegisterPluginCommands(plugin);
|
return new Tuple<Version, string>(latestVersion, zipAsset?.BrowserDownloadUrl);
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
_log.Error($"Error loading plugin '{type.FullName}'");
|
_log.Error($"Unable to get the latest release of '{repository}.'");
|
||||||
_log.Error(e);
|
_log.Error(e);
|
||||||
throw;
|
return default(Tuple<Version, string>);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Task UpdatePlugin(string localPath, string downloadUrl)
|
||||||
|
{
|
||||||
|
if (File.Exists(localPath))
|
||||||
|
File.Delete(localPath);
|
||||||
|
|
||||||
|
if (Directory.Exists(localPath))
|
||||||
|
Directory.Delete(localPath, true);
|
||||||
|
|
||||||
|
var fileName = downloadUrl.Split('/').Last();
|
||||||
|
var filePath = Path.Combine(PluginDir, fileName);
|
||||||
|
|
||||||
|
return new WebClient().DownloadFileTaskAsync(downloadUrl, filePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void LoadPluginFromFolder(string directory)
|
||||||
|
{
|
||||||
|
var assemblies = new List<Assembly>();
|
||||||
|
var files = Directory.EnumerateFiles(directory, "*.*", SearchOption.AllDirectories).ToList();
|
||||||
|
|
||||||
|
var manifest = GetManifestFromDirectory(directory);
|
||||||
|
if (manifest == null)
|
||||||
|
{
|
||||||
|
_log.Warn($"Directory {directory} is missing a manifest, skipping load.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var file in files)
|
||||||
|
{
|
||||||
|
if (!file.Contains(".dll", StringComparison.CurrentCultureIgnoreCase))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
using (var stream = File.OpenRead(file))
|
||||||
|
{
|
||||||
|
var data = new byte[stream.Length];
|
||||||
|
stream.Read(data, 0, data.Length);
|
||||||
|
assemblies.Add(Assembly.Load(data));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
InstantiatePlugin(manifest, assemblies);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void LoadPluginFromZip(string path)
|
||||||
|
{
|
||||||
|
PluginManifest manifest;
|
||||||
|
var assemblies = new List<Assembly>();
|
||||||
|
using (var zipFile = ZipFile.OpenRead(path))
|
||||||
|
{
|
||||||
|
manifest = GetManifestFromZip(path);
|
||||||
|
if (manifest == null)
|
||||||
|
{
|
||||||
|
_log.Warn($"Zip file {path} is missing a manifest, skipping.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var entry in zipFile.Entries)
|
||||||
|
{
|
||||||
|
if (!entry.Name.Contains(".dll", StringComparison.CurrentCultureIgnoreCase))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
using (var stream = entry.Open())
|
||||||
|
{
|
||||||
|
var data = new byte[entry.Length];
|
||||||
|
stream.Read(data, 0, data.Length);
|
||||||
|
assemblies.Add(Assembly.Load(data));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Plugins.ForEach(p => p.Init(Torch));
|
InstantiatePlugin(manifest, assemblies);
|
||||||
PluginsLoaded?.Invoke(Plugins.ToList());
|
}
|
||||||
|
|
||||||
|
private PluginManifest GetManifestFromZip(string path)
|
||||||
|
{
|
||||||
|
using (var zipFile = ZipFile.OpenRead(path))
|
||||||
|
{
|
||||||
|
foreach (var entry in zipFile.Entries)
|
||||||
|
{
|
||||||
|
if (!entry.Name.Equals(MANIFEST_NAME, StringComparison.CurrentCultureIgnoreCase))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
using (var stream = new StreamReader(entry.Open()))
|
||||||
|
{
|
||||||
|
var ser = new XmlSerializer(typeof(PluginManifest));
|
||||||
|
var manifest = (PluginManifest)ser.Deserialize(stream);
|
||||||
|
return manifest;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private PluginManifest GetManifestFromDirectory(string directory)
|
||||||
|
{
|
||||||
|
var path = Path.Combine(directory, MANIFEST_NAME);
|
||||||
|
return !File.Exists(path) ? null : PluginManifest.Load(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void InstantiatePlugin(PluginManifest manifest, IEnumerable<Assembly> assemblies)
|
||||||
|
{
|
||||||
|
Type pluginType = null;
|
||||||
|
foreach (var asm in assemblies)
|
||||||
|
{
|
||||||
|
foreach (var type in asm.GetExportedTypes())
|
||||||
|
{
|
||||||
|
if (!type.GetInterfaces().Contains(typeof(ITorchPlugin)))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (pluginType != null)
|
||||||
|
{
|
||||||
|
_log.Error($"The plugin '{manifest.Name}' has multiple implementations of {nameof(ITorchPlugin)}.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
pluginType = type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pluginType == null)
|
||||||
|
{
|
||||||
|
_log.Error($"The plugin '{manifest.Name}' does not have an implementation of {nameof(ITorchPlugin)}.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Backwards compatibility for PluginAttribute.
|
||||||
|
var pluginAttr = pluginType.GetCustomAttribute<PluginAttribute>();
|
||||||
|
if (pluginAttr != null)
|
||||||
|
{
|
||||||
|
_log.Warn($"Plugin '{manifest.Name}' is using the obsolete {nameof(PluginAttribute)}, using info from attribute if necessary.");
|
||||||
|
manifest.Version = manifest.Version ?? pluginAttr.Version.ToString();
|
||||||
|
manifest.Name = manifest.Name ?? pluginAttr.Name;
|
||||||
|
if (manifest.Guid == default(Guid))
|
||||||
|
manifest.Guid = pluginAttr.Guid;
|
||||||
|
}
|
||||||
|
|
||||||
|
_log.Info($"Loading plugin '{manifest.Name}' ({manifest.Version})");
|
||||||
|
var plugin = (TorchPluginBase)Activator.CreateInstance(pluginType);
|
||||||
|
|
||||||
|
plugin.Manifest = manifest;
|
||||||
|
plugin.StoragePath = Torch.Config.InstancePath;
|
||||||
|
plugin.Torch = Torch;
|
||||||
|
Plugins.Add(manifest.Guid, plugin);
|
||||||
|
_commandManager.RegisterPluginCommands(plugin);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerator<ITorchPlugin> GetEnumerator()
|
public IEnumerator<ITorchPlugin> GetEnumerator()
|
||||||
{
|
{
|
||||||
return Plugins.GetEnumerator();
|
return Plugins.Values.GetEnumerator();
|
||||||
}
|
}
|
||||||
|
|
||||||
IEnumerator IEnumerable.GetEnumerator()
|
IEnumerator IEnumerable.GetEnumerator()
|
||||||
|
@@ -44,7 +44,7 @@ namespace Torch.Managers
|
|||||||
CheckAndUpdateTorch();
|
CheckAndUpdateTorch();
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<Tuple<Version, string>> GetLatestRelease(string owner, string name)
|
private async Task<Tuple<Version, string>> TryGetLatestArchiveUrl(string owner, string name)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -53,56 +53,17 @@ namespace Torch.Managers
|
|||||||
return new Tuple<Version, string>(new Version(), null);
|
return new Tuple<Version, string>(new Version(), null);
|
||||||
|
|
||||||
var zip = latest.Assets.FirstOrDefault(x => x.Name.Contains(".zip"));
|
var zip = latest.Assets.FirstOrDefault(x => x.Name.Contains(".zip"));
|
||||||
var versionName = Regex.Match(latest.TagName, "(\\d+\\.)+\\d+").ToString();
|
if (zip == null)
|
||||||
if (string.IsNullOrWhiteSpace(versionName))
|
_log.Error($"Latest release of {owner}/{name} does not contain a zip archive.");
|
||||||
{
|
if (!latest.TagName.TryExtractVersion(out Version version))
|
||||||
_log.Warn("Unable to parse tag {0} for {1}/{2}", latest.TagName, owner, name);
|
_log.Error($"Unable to parse version tag for {owner}/{name}");
|
||||||
versionName = "0.0";
|
return new Tuple<Version, string>(version, zip?.BrowserDownloadUrl);
|
||||||
}
|
|
||||||
return new Tuple<Version, string>(new Version(versionName), zip?.BrowserDownloadUrl);
|
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
_log.Error($"An error occurred getting release information for '{owner}/{name}'");
|
_log.Error($"An error occurred getting release information for '{owner}/{name}'");
|
||||||
_log.Error(e);
|
_log.Error(e);
|
||||||
return new Tuple<Version, string>(new Version(), null);
|
return default(Tuple<Version, string>);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task CheckAndUpdatePlugin(PluginManifest manifest)
|
|
||||||
{
|
|
||||||
if (!Torch.Config.GetPluginUpdates)
|
|
||||||
return;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var name = manifest.Repository.Split('/');
|
|
||||||
if (name.Length != 2)
|
|
||||||
{
|
|
||||||
_log.Error($"'{manifest.Repository}' is not a valid GitHub repository.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var currentVersion = new Version(manifest.Version);
|
|
||||||
var releaseInfo = await GetLatestRelease(name[0], name[1]).ConfigureAwait(false);
|
|
||||||
if (releaseInfo.Item1 > currentVersion)
|
|
||||||
{
|
|
||||||
_log.Warn($"Updating {manifest.Repository} from version {currentVersion} to version {releaseInfo.Item1}");
|
|
||||||
var updateName = Path.Combine(_fsManager.TempDirectory, $"{name[0]}_{name[1]}.zip");
|
|
||||||
var updatePath = Path.Combine(_torchDir, "Plugins");
|
|
||||||
await new WebClient().DownloadFileTaskAsync(new Uri(releaseInfo.Item2), updateName).ConfigureAwait(false);
|
|
||||||
UpdateFromZip(updateName, updatePath);
|
|
||||||
File.Delete(updateName);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_log.Info($"{manifest.Repository} is up to date. ({currentVersion})");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
_log.Error($"An error occured downloading the plugin update for {manifest.Repository}.");
|
|
||||||
_log.Error(e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,7 +77,7 @@ namespace Torch.Managers
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var releaseInfo = await GetLatestRelease("TorchAPI", "Torch").ConfigureAwait(false);
|
var releaseInfo = await TryGetLatestArchiveUrl("TorchAPI", "Torch").ConfigureAwait(false);
|
||||||
if (releaseInfo.Item1 > Torch.TorchVersion)
|
if (releaseInfo.Item1 > Torch.TorchVersion)
|
||||||
{
|
{
|
||||||
_log.Warn($"Updating Torch from version {Torch.TorchVersion} to version {releaseInfo.Item1}");
|
_log.Warn($"Updating Torch from version {Torch.TorchVersion} to version {releaseInfo.Item1}");
|
||||||
|
@@ -10,8 +10,11 @@ namespace Torch
|
|||||||
{
|
{
|
||||||
public class PluginManifest
|
public class PluginManifest
|
||||||
{
|
{
|
||||||
public string Repository { get; set; } = "Jimmacle/notarealrepo";
|
public string Name { get; set; }
|
||||||
public string Version { get; set; } = "1.0";
|
public Guid Guid { get; set; }
|
||||||
|
public string Repository { get; set; }
|
||||||
|
public string Version { get; set; }
|
||||||
|
public List<Guid> Dependencies { get; } = new List<Guid>();
|
||||||
|
|
||||||
public void Save(string path)
|
public void Save(string path)
|
||||||
{
|
{
|
||||||
@@ -26,9 +29,20 @@ namespace Torch
|
|||||||
{
|
{
|
||||||
using (var f = File.OpenRead(path))
|
using (var f = File.OpenRead(path))
|
||||||
{
|
{
|
||||||
var ser = new XmlSerializer(typeof(PluginManifest));
|
return Load(f);
|
||||||
return (PluginManifest)ser.Deserialize(f);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static PluginManifest Load(Stream stream)
|
||||||
|
{
|
||||||
|
var ser = new XmlSerializer(typeof(PluginManifest));
|
||||||
|
return (PluginManifest)ser.Deserialize(stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static PluginManifest Load(TextReader reader)
|
||||||
|
{
|
||||||
|
var ser = new XmlSerializer(typeof(PluginManifest));
|
||||||
|
return (PluginManifest)ser.Deserialize(reader);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -154,7 +154,7 @@
|
|||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="ChatMessage.cs" />
|
<Compile Include="ChatMessage.cs" />
|
||||||
<Compile Include="Collections\ObservableList.cs" />
|
<Compile Include="Collections\ObservableList.cs" />
|
||||||
<Compile Include="DispatcherExtensions.cs" />
|
<Compile Include="Extensions\DispatcherExtensions.cs" />
|
||||||
<Compile Include="Managers\DependencyManager.cs" />
|
<Compile Include="Managers\DependencyManager.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="SaveGameStatus.cs" />
|
<Compile Include="SaveGameStatus.cs" />
|
||||||
|
@@ -257,7 +257,7 @@ namespace Torch
|
|||||||
try { Console.Title = $"{Config.InstanceName} - Torch {TorchVersion}, SE {GameVersion}"; }
|
try { Console.Title = $"{Config.InstanceName} - Torch {TorchVersion}, SE {GameVersion}"; }
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
///Running as service
|
//Running as service
|
||||||
}
|
}
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
|
@@ -16,30 +16,11 @@ namespace Torch
|
|||||||
public abstract class TorchPluginBase : ITorchPlugin
|
public abstract class TorchPluginBase : ITorchPlugin
|
||||||
{
|
{
|
||||||
public string StoragePath { get; internal set; }
|
public string StoragePath { get; internal set; }
|
||||||
public Guid Id { get; }
|
public PluginManifest Manifest { get; internal set; }
|
||||||
public Version Version { get; }
|
public Guid Id => Manifest.Guid;
|
||||||
public string Name { get; }
|
public string Version => Manifest.Version;
|
||||||
public ITorchBase Torch { get; private set; }
|
public string Name => Manifest.Name;
|
||||||
private static readonly Logger _log = LogManager.GetLogger(nameof(TorchPluginBase));
|
public ITorchBase Torch { get; internal set; }
|
||||||
|
|
||||||
protected TorchPluginBase()
|
|
||||||
{
|
|
||||||
var type = GetType();
|
|
||||||
var pluginInfo = type.GetCustomAttribute<PluginAttribute>();
|
|
||||||
if (pluginInfo == null)
|
|
||||||
{
|
|
||||||
_log.Warn($"Plugin {type.FullName} has no PluginAttribute");
|
|
||||||
Name = type.FullName;
|
|
||||||
Version = new Version(0, 0, 0, 0);
|
|
||||||
Id = default(Guid);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Name = pluginInfo.Name;
|
|
||||||
Version = pluginInfo.Version;
|
|
||||||
Id = pluginInfo.Guid;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public virtual void Init(ITorchBase torch)
|
public virtual void Init(ITorchBase torch)
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user