Renamed PluginManager to DiscordBotCore.
Revamped the Logger
This commit is contained in:
72
DiscordBotCore/Online/Helpers/OnlineFunctions.cs
Normal file
72
DiscordBotCore/Online/Helpers/OnlineFunctions.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using DiscordBotCore.Others;
|
||||
|
||||
namespace DiscordBotCore.Online.Helpers;
|
||||
|
||||
internal static class OnlineFunctions
|
||||
{
|
||||
/// <summary>
|
||||
/// Downloads a <see cref="Stream" /> and saves it to another <see cref="Stream" />.
|
||||
/// </summary>
|
||||
/// <param name="client">The <see cref="HttpClient" /> that is used to download the file</param>
|
||||
/// <param name="url">The url to the file</param>
|
||||
/// <param name="destination">The <see cref="Stream" /> to save the downloaded data</param>
|
||||
/// <param name="progress">The <see cref="IProgress{T}" /> that is used to track the download progress</param>
|
||||
/// <param name="cancellation">The cancellation token</param>
|
||||
/// <returns></returns>
|
||||
internal static async Task DownloadFileAsync(
|
||||
this HttpClient client, string url, Stream destination,
|
||||
IProgress<float>? progress = null,
|
||||
IProgress<long>? downloadedBytes = null, int bufferSize = 81920,
|
||||
CancellationToken cancellation = default)
|
||||
{
|
||||
using (var response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead, cancellation))
|
||||
{
|
||||
var contentLength = response.Content.Headers.ContentLength;
|
||||
|
||||
using (var download = await response.Content.ReadAsStreamAsync(cancellation))
|
||||
{
|
||||
// Ignore progress reporting when no progress reporter was
|
||||
// passed or when the content length is unknown
|
||||
if (progress == null || !contentLength.HasValue)
|
||||
{
|
||||
await download.CopyToAsync(destination, cancellation);
|
||||
if (!contentLength.HasValue)
|
||||
progress?.Report(100f);
|
||||
return;
|
||||
}
|
||||
|
||||
// Convert absolute progress (bytes downloaded) into relative progress (0% - 100%)
|
||||
// total ... 100%
|
||||
// downloaded ... x%
|
||||
// x = downloaded * 100 / total => x = downloaded / total * 100
|
||||
var relativeProgress = new Progress<long>(totalBytesDownloaded =>
|
||||
{
|
||||
progress?.Report(totalBytesDownloaded / (float)contentLength.Value * 100);
|
||||
downloadedBytes?.Report(totalBytesDownloaded);
|
||||
}
|
||||
);
|
||||
|
||||
// Use extension method to report progress while downloading
|
||||
await download.CopyToOtherStreamAsync(destination, bufferSize, relativeProgress, cancellation);
|
||||
progress.Report(100f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read contents of a file as string from specified URL
|
||||
/// </summary>
|
||||
/// <param name="url">The URL to read from</param>
|
||||
/// <param name="cancellation">The cancellation token</param>
|
||||
/// <returns></returns>
|
||||
internal static async Task<string> DownloadStringAsync(string url, CancellationToken cancellation = default)
|
||||
{
|
||||
using var client = new HttpClient();
|
||||
return await client.GetStringAsync(url, cancellation);
|
||||
}
|
||||
}
|
||||
20
DiscordBotCore/Online/Helpers/PluginVersion.cs
Normal file
20
DiscordBotCore/Online/Helpers/PluginVersion.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System.Text.Json.Serialization;
|
||||
using DiscordBotCore.Interfaces.Updater;
|
||||
|
||||
namespace DiscordBotCore.Online.Helpers;
|
||||
|
||||
public class PluginVersion: Version
|
||||
{
|
||||
[JsonConstructor]
|
||||
public PluginVersion(int major, int minor, int patch): base(major, minor, patch)
|
||||
{
|
||||
}
|
||||
public PluginVersion(string versionAsString): base(versionAsString)
|
||||
{
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return ToShortString();
|
||||
}
|
||||
}
|
||||
172
DiscordBotCore/Online/PluginManager.cs
Normal file
172
DiscordBotCore/Online/PluginManager.cs
Normal file
@@ -0,0 +1,172 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using DiscordBotCore.Others;
|
||||
using DiscordBotCore.Plugin;
|
||||
using DiscordBotCore.Updater.Plugins;
|
||||
|
||||
namespace DiscordBotCore.Online;
|
||||
|
||||
public class PluginManager
|
||||
{
|
||||
private static readonly string _DefaultBranch = "releases";
|
||||
private static readonly string _DefaultBaseUrl = "https://raw.githubusercontent.com/andreitdr/SethPlugins";
|
||||
|
||||
private static readonly string _DefaultPluginsLink = "PluginsList.json";
|
||||
|
||||
|
||||
public string Branch { get; init; }
|
||||
public string BaseUrl { get; init; }
|
||||
|
||||
|
||||
private string PluginsLink => $"{BaseUrl}/{Branch}/{_DefaultPluginsLink}";
|
||||
|
||||
public PluginManager(Uri baseUrl, string branch)
|
||||
{
|
||||
BaseUrl = baseUrl.ToString();
|
||||
Branch = branch;
|
||||
}
|
||||
|
||||
public PluginManager(string branch)
|
||||
{
|
||||
BaseUrl = _DefaultBaseUrl;
|
||||
Branch = branch;
|
||||
}
|
||||
|
||||
public PluginManager()
|
||||
{
|
||||
BaseUrl = _DefaultBaseUrl;
|
||||
Branch = _DefaultBranch;
|
||||
}
|
||||
|
||||
public async Task<List<PluginOnlineInfo>?> GetPluginsList()
|
||||
{
|
||||
var jsonText = await ServerCom.GetAllTextFromUrl(PluginsLink);
|
||||
List<PluginOnlineInfo> result = await JsonManager.ConvertFromJson<List<PluginOnlineInfo>>(jsonText);
|
||||
|
||||
var currentOS = OperatingSystem.IsWindows() ? OSType.WINDOWS :
|
||||
OperatingSystem.IsLinux() ? OSType.LINUX :
|
||||
OperatingSystem.IsMacOS() ? OSType.MACOSX : OSType.NONE;
|
||||
|
||||
return result.FindAll(pl => (pl.SupportedOS & currentOS) != 0);
|
||||
|
||||
|
||||
}
|
||||
|
||||
public async Task<PluginOnlineInfo?> GetPluginDataByName(string pluginName)
|
||||
{
|
||||
List<PluginOnlineInfo>? plugins = await GetPluginsList();
|
||||
var result = plugins?.Find(p => p.Name == pluginName);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task RemovePluginFromDatabase(string pluginName)
|
||||
{
|
||||
List<PluginInfo> installedPlugins = await JsonManager.ConvertFromJson<List<PluginInfo>>(await File.ReadAllTextAsync(Application.CurrentApplication.PluginDatabase));
|
||||
|
||||
installedPlugins.RemoveAll(p => p.PluginName == pluginName);
|
||||
await JsonManager.SaveToJsonFile(Application.CurrentApplication.PluginDatabase,installedPlugins);
|
||||
}
|
||||
|
||||
public async Task AppendPluginToDatabase(PluginInfo pluginData)
|
||||
{
|
||||
List<PluginInfo> installedPlugins = await JsonManager.ConvertFromJson<List<PluginInfo>>(await File.ReadAllTextAsync(Application.CurrentApplication.PluginDatabase));
|
||||
|
||||
installedPlugins.Add(pluginData);
|
||||
await JsonManager.SaveToJsonFile(Application.CurrentApplication.PluginDatabase, installedPlugins);
|
||||
}
|
||||
|
||||
public async Task<List<PluginInfo>> GetInstalledPlugins()
|
||||
{
|
||||
return await JsonManager.ConvertFromJson<List<PluginInfo>>(await File.ReadAllTextAsync(Application.CurrentApplication.PluginDatabase));
|
||||
}
|
||||
|
||||
public async Task<bool> IsPluginInstalled(string pluginName)
|
||||
{
|
||||
List<PluginInfo> installedPlugins = await JsonManager.ConvertFromJson<List<PluginInfo>>(await File.ReadAllTextAsync(Application.CurrentApplication.PluginDatabase));
|
||||
|
||||
return installedPlugins.Any(plugin => plugin.PluginName == pluginName);
|
||||
}
|
||||
|
||||
public async Task CheckForUpdates()
|
||||
{
|
||||
var pluginUpdater = new PluginUpdater(this);
|
||||
|
||||
List<PluginInfo> installedPlugins = await GetInstalledPlugins();
|
||||
|
||||
foreach (var plugin in installedPlugins)
|
||||
{
|
||||
if (await pluginUpdater.HasUpdate(plugin.PluginName))
|
||||
{
|
||||
Application.CurrentApplication.Logger.Log("Updating plugin: " + plugin.PluginName, typeof(PluginManager), LogType.INFO);
|
||||
await pluginUpdater.UpdatePlugin(plugin.PluginName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> MarkPluginToUninstall(string pluginName)
|
||||
{
|
||||
List<PluginInfo> installedPlugins = await GetInstalledPlugins();
|
||||
PluginInfo? info = installedPlugins.Find(info => info.PluginName == pluginName);
|
||||
|
||||
if(info == null)
|
||||
return false;
|
||||
|
||||
await RemovePluginFromDatabase(pluginName);
|
||||
info.IsMarkedToUninstall = true;
|
||||
await AppendPluginToDatabase(info);
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
public async Task UninstallMarkedPlugins()
|
||||
{
|
||||
List<PluginInfo> installedPlugins = await GetInstalledPlugins();
|
||||
foreach(PluginInfo plugin in installedPlugins)
|
||||
{
|
||||
if(!plugin.IsMarkedToUninstall) continue;
|
||||
|
||||
await UninstallPlugin(plugin);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task UninstallPlugin(PluginInfo pluginInfo)
|
||||
{
|
||||
File.Delete(pluginInfo.FilePath);
|
||||
|
||||
foreach(string dependency in pluginInfo.ListOfDependancies)
|
||||
File.Delete(dependency);
|
||||
|
||||
await RemovePluginFromDatabase(pluginInfo.PluginName);
|
||||
}
|
||||
|
||||
public async Task InstallPlugin(PluginOnlineInfo pluginData, IProgress<float>? installProgress)
|
||||
{
|
||||
installProgress?.Report(0f);
|
||||
|
||||
int totalSteps = pluginData.HasDependencies ? pluginData.Dependencies.Count + 1 : 1;
|
||||
|
||||
float stepProgress = 1f / totalSteps;
|
||||
|
||||
float currentProgress = 0f;
|
||||
|
||||
IProgress<float> progress = new Progress<float>((p) => {
|
||||
installProgress?.Report(currentProgress + stepProgress * p);
|
||||
});
|
||||
|
||||
await ServerCom.DownloadFileAsync(pluginData.DownLoadLink, $"{Application.CurrentApplication.ApplicationEnvironmentVariables["PluginFolder"]}/{pluginData.Name}.dll", progress);
|
||||
|
||||
foreach (var dependency in pluginData.Dependencies)
|
||||
{
|
||||
await ServerCom.DownloadFileAsync(dependency.DownloadLink, dependency.DownloadLocation, progress);
|
||||
currentProgress += stepProgress;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
78
DiscordBotCore/Online/ServerCom.cs
Normal file
78
DiscordBotCore/Online/ServerCom.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using DiscordBotCore.Online.Helpers;
|
||||
|
||||
namespace DiscordBotCore.Online;
|
||||
|
||||
public static class ServerCom
|
||||
{
|
||||
/// <summary>
|
||||
/// Read all lines from a file async
|
||||
/// </summary>
|
||||
/// <param name="link">The link of the file</param>
|
||||
/// <returns></returns>
|
||||
public static async Task<List<string>> ReadTextFromURL(string link)
|
||||
{
|
||||
var response = await OnlineFunctions.DownloadStringAsync(link);
|
||||
var lines = response.Split('\n');
|
||||
return lines.ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get all text from a file async
|
||||
/// </summary>
|
||||
/// <param name="link">The link of the file</param>
|
||||
/// <returns></returns>
|
||||
public static async Task<string> GetAllTextFromUrl(string link)
|
||||
{
|
||||
var response = await OnlineFunctions.DownloadStringAsync(link);
|
||||
return response;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Download file from url
|
||||
/// </summary>
|
||||
/// <param name="URL">The url to the file</param>
|
||||
/// <param name="location">The location where to store the downloaded data</param>
|
||||
/// <param name="progress">The <see cref="IProgress{T}" /> to track the download</param>
|
||||
/// <returns></returns>
|
||||
public static async Task DownloadFileAsync(
|
||||
string URL, string location, IProgress<float>? progress,
|
||||
IProgress<long>? downloadedBytes)
|
||||
{
|
||||
using (var client = new HttpClient())
|
||||
{
|
||||
client.Timeout = TimeSpan.FromMinutes(5);
|
||||
|
||||
using (var file = new FileStream(location, FileMode.Create, FileAccess.Write, FileShare.None))
|
||||
{
|
||||
await client.DownloadFileAsync(URL, file, progress, downloadedBytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task DownloadFileAsync(string URl, string location, IProgress<float> progress)
|
||||
{
|
||||
await DownloadFileAsync(URl, location, progress, null);
|
||||
}
|
||||
|
||||
public static async Task DownloadFileAsync(string url, string location)
|
||||
{
|
||||
await DownloadFileAsync(url, location, null, null);
|
||||
}
|
||||
|
||||
public static Task CreateDownloadTask(string URl, string location)
|
||||
{
|
||||
return DownloadFileAsync(URl, location, null, null);
|
||||
}
|
||||
|
||||
public static Task CreateDownloadTask(string URl, string location, IProgress<float> progress)
|
||||
{
|
||||
return DownloadFileAsync(URl, location, progress, null);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user