Updated display features

This commit is contained in:
2022-07-17 14:21:16 +03:00
parent b8ec6f42df
commit c415fa1c0c
16 changed files with 242 additions and 177 deletions

View File

@@ -18,32 +18,32 @@ namespace PluginManager.Online.Helpers
/// <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, CancellationToken cancellation = default)
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))
using (var response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead, cancellation))
{
var contentLength = response.Content.Headers.ContentLength;
using (var download = await response.Content.ReadAsStreamAsync())
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);
await download.CopyToAsync(destination, cancellation);
return;
}
// Convert absolute progress (bytes downloaded) into relative progress (0% - 100%)
var relativeProgress = new Progress<long>(totalBytes =>
{
progress.Report((float)totalBytes / contentLength.Value * 100);
downloadedBytes?.Report(totalBytes);
});
{
progress.Report((float)totalBytes / contentLength.Value * 100);
downloadedBytes?.Report(totalBytes);
}
);
// Use extension method to report progress while downloading
await download.CopyToOtherStreamAsync(destination, 81920, relativeProgress, cancellation);
await download.CopyToOtherStreamAsync(destination, bufferSize, relativeProgress, cancellation);
progress.Report(1);
}
}
@@ -57,10 +57,8 @@ namespace PluginManager.Online.Helpers
/// <returns></returns>
internal static async Task<string> DownloadStringAsync(string url, CancellationToken cancellation = default)
{
using (var client = new HttpClient())
{
return await client.GetStringAsync(url);
}
using var client = new HttpClient();
return await client.GetStringAsync(url, cancellation);
}

View File

@@ -30,7 +30,7 @@ public class PluginsManager
{
try
{
var list = await ServerCom.ReadTextFromFile(PluginsLink);
var list = await ServerCom.ReadTextFromURL(PluginsLink);
var lines = list.ToArray();
var data = new List<string[]>();
@@ -43,7 +43,8 @@ public class PluginsManager
data.Add(new[] { "-", "-", "-", "-", "-" });
for (var i = 0; i < len; i++)
{
if (lines[i].Length <= 2) continue;
if (lines[i].Length <= 2)
continue;
var content = lines[i].Split(',');
var display = new string[titles.Length];
if (op == OperatingSystem.WINDOWS)
@@ -54,7 +55,7 @@ public class PluginsManager
display[1] = content[1];
display[2] = content[2];
if (content.Length == 6 && (content[5] != null || content[5].Length > 2))
display[3] = ((await ServerCom.ReadTextFromFile(content[5])).Count + 1).ToString();
display[3] = ((await ServerCom.ReadTextFromURL(content[5])).Count + 1).ToString();
else
display[3] = "1";
@@ -72,7 +73,8 @@ public class PluginsManager
display[0] = content[0];
display[1] = content[1];
display[2] = content[2];
if (content.Length == 6 && (content[5] != null || content[5].Length > 2)) display[3] = ((await ServerCom.ReadTextFromFile(content[5])).Count + 1).ToString();
if (content.Length == 6 && (content[5] != null || content[5].Length > 2))
display[3] = ((await ServerCom.ReadTextFromURL(content[5])).Count + 1).ToString();
if (Config.PluginConfig.Contains(content[0]) || Config.PluginConfig.Contains(content[0]))
display[4] = "✓";
else
@@ -88,7 +90,7 @@ public class PluginsManager
}
catch (Exception exception)
{
Console.WriteLine("Failed to execute command: listlang\nReason: " + exception.Message);
Console.WriteLine("Failed to execute command: listplugs\nReason: " + exception.Message);
Functions.WriteErrFile(exception.ToString());
}
}
@@ -102,7 +104,7 @@ public class PluginsManager
{
try
{
var list = await ServerCom.ReadTextFromFile(PluginsLink);
var list = await ServerCom.ReadTextFromURL(PluginsLink);
var lines = list.ToArray();
var len = lines.Length;
for (var i = 0; i < len; i++)
@@ -110,8 +112,10 @@ public class PluginsManager
var contents = lines[i].Split(',');
if (contents[0] == name)
{
if (contents.Length == 6) return new[] { contents[2], contents[3], contents[5] };
if (contents.Length == 5) return new[] { contents[2], contents[3], string.Empty };
if (contents.Length == 6)
return new[] { contents[2], contents[3], contents[5] };
if (contents.Length == 5)
return new[] { contents[2], contents[3], string.Empty };
throw new Exception("Failed to download plugin. Invalid Argument Length");
}
}

View File

@@ -9,15 +9,14 @@ using PluginManager.Others;
namespace PluginManager.Online
{
public class ServerCom
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>> ReadTextFromFile(string link)
public static async Task<List<string>> ReadTextFromURL(string link)
{
string response = await OnlineFunctions.DownloadStringAsync(link);
string[] lines = response.Split('\n');
@@ -52,15 +51,12 @@ namespace PluginManager.Online
/// <returns></returns>
public static async Task DownloadFileAsync(string URL, string location)
{
bool isDownloading = true;
int c_progress = 0;
bool isDownloading = true;
float c_progress = 0;
Console_Utilities.ProgressBar pbar = new Console_Utilities.ProgressBar { Max = 100, NoColor = true };
Console_Utilities.ProgressBar pbar = new Console_Utilities.ProgressBar { Max = 100f, NoColor = true };
IProgress<float> progress = new Progress<float>(percent =>
{
c_progress = (int)percent;
});
IProgress<float> progress = new Progress<float>(percent => { c_progress = percent; });
Task updateProgressBarTask = new Task(() =>
@@ -68,7 +64,8 @@ namespace PluginManager.Online
while (isDownloading)
{
pbar.Update(c_progress);
if (c_progress == 100) break;
if (c_progress == 100f)
break;
Thread.Sleep(500);
}
}
@@ -78,8 +75,8 @@ namespace PluginManager.Online
await DownloadFileAsync(URL, location, progress);
c_progress = 100;
pbar.Update(100);
c_progress = pbar.Max;
pbar.Update(100f);
isDownloading = false;
}
}