Bot automatically downloads requirements for plugins
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -361,4 +361,5 @@ MigrationBackup/
|
|||||||
.ionide/
|
.ionide/
|
||||||
|
|
||||||
# Fody - auto-generated XML schema
|
# Fody - auto-generated XML schema
|
||||||
FodyWeavers.xsd
|
FodyWeavers.xsd
|
||||||
|
/BUILDS/DLL
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ using PluginManager.Loaders;
|
|||||||
using PluginManager.LanguageSystem;
|
using PluginManager.LanguageSystem;
|
||||||
using PluginManager.Online;
|
using PluginManager.Online;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace DiscordBot
|
namespace DiscordBot
|
||||||
{
|
{
|
||||||
@@ -106,6 +107,9 @@ namespace DiscordBot
|
|||||||
|
|
||||||
case "dwplug":
|
case "dwplug":
|
||||||
string name = data.MergeStrings(1);
|
string name = data.MergeStrings(1);
|
||||||
|
// info[0] = plugin type
|
||||||
|
// info[1] = plugin link
|
||||||
|
// info[2] = if others are required, or string.Empty if none
|
||||||
string[] info = await manager.GetPluginLinkByName(name);
|
string[] info = await manager.GetPluginLinkByName(name);
|
||||||
if (info[1] == null) // link is null
|
if (info[1] == null) // link is null
|
||||||
{
|
{
|
||||||
@@ -120,6 +124,27 @@ namespace DiscordBot
|
|||||||
}
|
}
|
||||||
Downloader dw = new Downloader(name + ".dll", info[1]);
|
Downloader dw = new Downloader(name + ".dll", info[1]);
|
||||||
await dw.DownloadFileAsync("./Data/Plugins/", info[0]);
|
await dw.DownloadFileAsync("./Data/Plugins/", info[0]);
|
||||||
|
|
||||||
|
// check requirements if any
|
||||||
|
|
||||||
|
if (info.Length == 3 && info[2] != string.Empty && info[2] != null)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Downloading requirements for plugin : {name}");
|
||||||
|
//
|
||||||
|
List<string> lines = await ServerCom.ReadTextFromFile(info[2]);
|
||||||
|
int i = 1;
|
||||||
|
foreach (var line in lines)
|
||||||
|
{
|
||||||
|
string[] split = line.Split(',');
|
||||||
|
Console.WriteLine($"Downloading item: {split[1]}");
|
||||||
|
await ServerCom.DownloadFileAsync(split[0], split[1], i, lines.Count);
|
||||||
|
Functions.WriteColorText($"Downloaded item {split[1]}");
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
Console.WriteLine();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case "setlang":
|
case "setlang":
|
||||||
if (data.Length == 2)
|
if (data.Length == 2)
|
||||||
|
|||||||
@@ -22,8 +22,11 @@ namespace PluginManager.Online
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public async Task DownloadFileAsync(string location = @"./Downloads/", string? pluginType = null)
|
public async Task DownloadFileAsync(string location = @"./Downloads/", string? pluginType = null, string customMessage = null)
|
||||||
{
|
{
|
||||||
|
if (customMessage != null)
|
||||||
|
Console.WriteLine(customMessage);
|
||||||
|
|
||||||
Directory.CreateDirectory(location);
|
Directory.CreateDirectory(location);
|
||||||
if (isWorking) return;
|
if (isWorking) return;
|
||||||
isWorking = true;
|
isWorking = true;
|
||||||
@@ -73,9 +76,6 @@ namespace PluginManager.Online
|
|||||||
Console.ForegroundColor = c;
|
Console.ForegroundColor = c;
|
||||||
Console.Write(" !\n");
|
Console.Write(" !\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -93,16 +93,25 @@ namespace PluginManager.Online
|
|||||||
{
|
{
|
||||||
string[] contents = lines[i].Split(',');
|
string[] contents = lines[i].Split(',');
|
||||||
if (contents[0] == name)
|
if (contents[0] == name)
|
||||||
return new string[] { contents[2], contents[3] };
|
{
|
||||||
|
if (contents.Length == 6)
|
||||||
|
return new string[] { contents[2], contents[3], contents[5] };
|
||||||
|
else if (contents.Length == 5)
|
||||||
|
return new string[] { contents[2], contents[3], string.Empty };
|
||||||
|
else throw new Exception("Failed to download plugin. Invalid Argument Length");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Failed to execute command: listlang\nReason: " + exception.Message);
|
Console.WriteLine("Failed to execute command: listplugs\nReason: " + exception.Message);
|
||||||
Others.Functions.WriteErrFile(exception.ToString());
|
Others.Functions.WriteErrFile(exception.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
return new string[] { null!, null! };
|
return new string[] { null!, null!, null! };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
58
PluginManager/Online/ServerCom.cs
Normal file
58
PluginManager/Online/ServerCom.cs
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
using PluginManager.Items;
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace PluginManager.Online
|
||||||
|
{
|
||||||
|
public class ServerCom
|
||||||
|
{
|
||||||
|
public static async Task<List<string>> ReadTextFromFile(string link)
|
||||||
|
{
|
||||||
|
List<string> s = new List<string>();
|
||||||
|
WebClient webClient = new WebClient();
|
||||||
|
var data = await webClient.OpenReadTaskAsync(link);
|
||||||
|
var response = await new StreamReader(data).ReadToEndAsync();
|
||||||
|
s.AddRange(from a in response.Split('\n')
|
||||||
|
where !a.StartsWith("$")
|
||||||
|
select a);
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task DownloadFileAsync(string url, string location, int downloadNumber, int totalToDownload)
|
||||||
|
{
|
||||||
|
WebClient client = new WebClient();
|
||||||
|
Spinner spinner = new Spinner();
|
||||||
|
Console.Write("Downloading ");
|
||||||
|
spinner.Start();
|
||||||
|
string oldTitle = Console.Title ?? "";
|
||||||
|
client.DownloadProgressChanged += (sender, e) =>
|
||||||
|
{
|
||||||
|
Console.Title = e.BytesReceived / 1024 + "/" + e.TotalBytesToReceive / 1024 + " (" + e.ProgressPercentage + "%) (" + downloadNumber + " / " + totalToDownload + ")";
|
||||||
|
};
|
||||||
|
client.DownloadFileCompleted += (sender, e) =>
|
||||||
|
{
|
||||||
|
spinner.Stop(); Console.WriteLine();
|
||||||
|
|
||||||
|
};
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await client.DownloadFileTaskAsync(new Uri(url), location);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.ToString());
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
Console.Title = oldTitle;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user