This commit is contained in:
2022-10-22 14:44:55 +03:00
parent cad3bb5b75
commit 725d02d152
9 changed files with 118 additions and 148 deletions

View File

@@ -271,7 +271,7 @@ public class Program
if (len > 0 && args[0] == "/remplug")
{
var plugName = args.MergeStrings(1);
var plugName = string.Join(' ', args, 1, args.Length - 1);
Console.WriteLine("Starting to remove " + plugName);
await ConsoleCommandsHandler.ExecuteCommad("remplug " + plugName);
loadPluginsOnStartup = true;

View File

@@ -4,6 +4,7 @@ using System.IO;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using PluginManager.Loaders;
using PluginManager.Others;
@@ -23,8 +24,8 @@ public static class Config
public static string UpdaterVersion
{
get => appConfig.UpdaterVersion;
set => appConfig.UpdaterVersion = value;
get => appConfig!.UpdaterVersion!;
set => appConfig!.UpdaterVersion = value;
}
public static string GetPluginVersion(string pluginName)
@@ -133,6 +134,25 @@ public static class Config
SaveConfig(SaveType.NORMAL);
}
public static bool TrySetValue<T>(string key, T value)
{
if (Config.ContainsKey(key))
{
try
{
Config.SetValue(key, value);
return true;
}
catch
{
return false;
}
}
Config.AddValueToVariables(key, value, false);
return true;
}
public static void RemoveKey(string key)
{
if (key == "Version" || key == "token" || key == "prefix")
@@ -205,8 +225,10 @@ public static class Config
appConfig = new AppConfig
{
ApplicationVariables = new Dictionary<string, object>(), ProtectedKeyWords = new List<string>(),
PluginVersions = new Dictionary<string, string>(), UpdaterVersion = "-1"
ApplicationVariables = new Dictionary<string, object>(),
ProtectedKeyWords = new List<string>(),
PluginVersions = new Dictionary<string, string>(),
UpdaterVersion = "-1"
};
}
@@ -220,9 +242,9 @@ public static class Config
return appConfig!.ApplicationVariables!.ContainsKey(key);
}
public static IDictionary<string, object> GetAllVariables()
public static IDictionary<string, object>? GetAllVariables()
{
return appConfig.ApplicationVariables;
return appConfig?.ApplicationVariables;
}
public static class PluginConfig

View File

@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using Discord.WebSocket;
using PluginManager.Others;
namespace PluginManager.Items;
@@ -20,7 +20,7 @@ public class Command
{
Author = message.Author;
var data = message.Content.Split(' ');
Arguments = data.Length > 1 ? new List<string>(data.MergeStrings(1).Split(' ')) : new List<string>();
Arguments = data.Length > 1 ? new List<string>(string.Join(' ', data, 1, data.Length - 1).Split(' ')) : new List<string>();
CommandName = data[0].Substring(1);
PrefixUsed = data[0][0];
}

View File

@@ -1,11 +1,4 @@
using Discord.WebSocket;
using PluginManager.Interfaces;
using PluginManager.Loaders;
using PluginManager.Online;
using PluginManager.Others;
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
@@ -14,6 +7,13 @@ using System.Net.Http;
using System.Reflection;
using System.Threading.Tasks;
using Discord.WebSocket;
using PluginManager.Interfaces;
using PluginManager.Loaders;
using PluginManager.Online;
using PluginManager.Others;
using OperatingSystem = PluginManager.Others.OperatingSystem;
namespace PluginManager.Items;
@@ -143,7 +143,7 @@ public class ConsoleCommandsHandler
return;
}
var name = args.MergeStrings(1);
var name = string.Join(' ', args, 1, args.Length - 1);
// info[0] = plugin type
// info[1] = plugin link
// info[2] = if others are required, or string.Empty if none
@@ -313,7 +313,7 @@ public class ConsoleCommandsHandler
if (args.Length <= 1) return;
try
{
var pName = args.MergeStrings(1);
var pName = string.Join(' ', args, 1, args.Length - 1);
var client = new HttpClient();
var url = (await manager.GetPluginLinkByName(pName))[1];
if (url is null) throw new Exception($"Invalid plugin name {pName}.");
@@ -347,7 +347,7 @@ public class ConsoleCommandsHandler
if (args.Length <= 1) return;
isDownloading = true;
var plugName = args.MergeStrings(1);
var plugName = string.Join(' ', args, 1, args.Length - 1);
if (pluginsLoaded)
{
if (Functions.GetOperatingSystem() == OperatingSystem.WINDOWS)

View File

@@ -2,7 +2,9 @@
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Discord.WebSocket;
using PluginManager.Interfaces;
using PluginManager.Online;
using PluginManager.Online.Updates;
@@ -64,9 +66,12 @@ public class PluginLoader
await Task.Run(async () =>
{
var name = new FileInfo(file).Name.Split('.')[0];
var version = await ServerCom.GetVersionOfPackageFromWeb(name);
if (version is null)
return;
if (!Config.PluginVersionsContainsKey(name))
Config.SetPluginVersion(
name, (await ServerCom.GetVersionOfPackageFromWeb(name))?.PackageVersionID + ".0.0");
name, (version.PackageVersionID + ".0.0"));
if (await PluginUpdater.CheckForUpdates(name))
await PluginUpdater.Download(name);
@@ -78,9 +83,12 @@ public class PluginLoader
await Task.Run(async () =>
{
var name = new FileInfo(file).Name.Split('.')[0];
var version = await ServerCom.GetVersionOfPackageFromWeb(name);
if (version is null)
return;
if (!Config.PluginVersionsContainsKey(name))
Config.SetPluginVersion(
name, (await ServerCom.GetVersionOfPackageFromWeb(name))?.PackageVersionID + ".0.0");
name, (version.PackageVersionID + ".0.0"));
if (await PluginUpdater.CheckForUpdates(name))
await PluginUpdater.Download(name);
@@ -102,6 +110,7 @@ public class PluginLoader
var commandsLoader = new Loader<DBCommand>(pluginCMDFolder, pluginCMDExtension);
var eventsLoader = new Loader<DBEvent>(pluginEVEFolder, pluginEVEExtension);
commandsLoader.FileLoaded += OnCommandFileLoaded;
commandsLoader.PluginLoaded += OnCommandLoaded;
@@ -110,6 +119,7 @@ public class PluginLoader
Commands = commandsLoader.Load();
Events = eventsLoader.Load();
}
private void EventFileLoaded(LoaderArgs e)

View File

@@ -19,6 +19,7 @@ public class VersionString
}
catch (Exception ex)
{
Console.WriteLine(version);
throw new Exception("Failed to write Version", ex);
}
}

View File

@@ -1,7 +1,4 @@
using PluginManager.Online.Helpers;
using PluginManager.Others;
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
@@ -9,6 +6,9 @@ using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using PluginManager.Online.Helpers;
using PluginManager.Others;
namespace PluginManager.Online;
public static class ServerCom
@@ -100,10 +100,15 @@ public static class ServerCom
{
var url = "https://raw.githubusercontent.com/Wizzy69/installer/discord-bot-files/Versions";
var data = await ReadTextFromURL(url);
var version = (from item in data
where !item.StartsWith("#") && item.StartsWith(pakName)
select item.Split(',')[1]).FirstOrDefault();
if (version == default || version == null) return null;
return new VersionString(version);
foreach (var item in data)
{
if (item.StartsWith("#"))
continue;
string[] split = item.Split(',');
if (split[0] == pakName)
return new VersionString(split[1]);
}
return null;
}
}

View File

@@ -9,7 +9,9 @@ using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Discord.WebSocket;
using PluginManager.Items;
namespace PluginManager.Others;
@@ -113,31 +115,6 @@ public static class Functions
WriteErrFile(ex.ToString());
}
/// <summary>
/// Merge one array of strings into one string
/// </summary>
/// <param name="s">The array of strings</param>
/// <param name="indexToStart">The index from where the merge should start (included)</param>
/// <returns>A string built based on the array</returns>
public static string MergeStrings(this string[] s, int indexToStart)
{
return string.Join(' ', s, indexToStart, s.Length - 1);
/* string r = "";
int len = s.Length;
if (len <= indexToStart) return "";
for (int i = indexToStart; i < len - 1; ++i)
{
r += s[i] + " ";
}
r += s[len - 1];
return r;
*/
}
/// <summary>
/// Get the Operating system you are runnin on
/// </summary>
@@ -182,8 +159,7 @@ public static class Functions
var buffer = new byte[bufferSize];
long totalBytesRead = 0;
int bytesRead;
while ((bytesRead =
await stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0)
while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0)
{
await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false);
totalBytesRead += bytesRead;
@@ -264,32 +240,6 @@ public static class Functions
}
}
/// <summary>
/// Convert Bytes to highest measurement unit possible
/// </summary>
/// <param name="bytes">The amount of bytes</param>
/// <returns></returns>
public static (double, string) ConvertBytes(long bytes)
{
var units = new List<string>
{
"B",
"KB",
"MB",
"GB",
"TB"
};
var i = 0;
while (bytes >= 1024)
{
i++;
bytes /= 1024;
}
return (bytes, units[i]);
}
/// <summary>
/// Save to JSON file
/// </summary>
@@ -323,36 +273,6 @@ public static class Functions
return (obj ?? default)!;
}
/// <summary>
/// Check if all words from <paramref name="str" /> are in <paramref name="baseString" /><br />
/// This function returns true if<br />
/// 1. The <paramref name="str" /> is part of <paramref name="baseString" /><br />
/// 2. The words (split by a space) of <paramref name="str" /> are located (separately) in
/// <paramref name="baseString" /> <br />
/// <example>
/// The following example will return <see langword="TRUE" /><br />
/// <c>STRContains("Hello World !", "I type word Hello and then i typed word World !")</c><br />
/// The following example will return <see langword="TRUE" /><br />
/// <c>STRContains("Hello World !", "I typed Hello World !" </c><br />
/// The following example will return <see langword="TRUE" /><br />
/// <c>STRContains("Hello World", "I type World then Hello")</c><br />
/// The following example will return <see langword="FALSE" /><br />
/// <c>STRContains("Hello World !", "I typed Hello World")</c><br />
/// </example>
/// </summary>
/// <param name="str">The string you are checking</param>
/// <param name="baseString">The main string that should contain <paramref name="str" /></param>
/// <returns></returns>
public static bool STRContains(this string str, string baseString)
{
if (baseString.Contains(str)) return true;
var array = str.Split(' ');
foreach (var s in array)
if (!baseString.Contains(s))
return false;
return true;
}
public static bool TryReadValueFromJson(string input, string codeName, out JsonElement element)
{
Stream text;

View File

@@ -7,6 +7,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DiscordBot", "DiscordBot\Di
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PluginManager", "PluginManager\PluginManager.csproj", "{EDD4D9B3-98DD-4367-A09F-D1C5ACB61132}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MusicLibrary", "..\DiscordBotItems\Plugins\MusicLibrary\MusicLibrary.csproj", "{878DFE01-4596-4EBC-9651-0679598CE794}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MusicLibraryEvent", "..\DiscordBotItems\Plugins\MusicLibraryEvent\MusicLibraryEvent.csproj", "{3EE0C8B4-5625-48A8-8246-5AD54A38A9B1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -21,6 +25,14 @@ Global
{EDD4D9B3-98DD-4367-A09F-D1C5ACB61132}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EDD4D9B3-98DD-4367-A09F-D1C5ACB61132}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EDD4D9B3-98DD-4367-A09F-D1C5ACB61132}.Release|Any CPU.Build.0 = Release|Any CPU
{878DFE01-4596-4EBC-9651-0679598CE794}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{878DFE01-4596-4EBC-9651-0679598CE794}.Debug|Any CPU.Build.0 = Debug|Any CPU
{878DFE01-4596-4EBC-9651-0679598CE794}.Release|Any CPU.ActiveCfg = Release|Any CPU
{878DFE01-4596-4EBC-9651-0679598CE794}.Release|Any CPU.Build.0 = Release|Any CPU
{3EE0C8B4-5625-48A8-8246-5AD54A38A9B1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3EE0C8B4-5625-48A8-8246-5AD54A38A9B1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3EE0C8B4-5625-48A8-8246-5AD54A38A9B1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3EE0C8B4-5625-48A8-8246-5AD54A38A9B1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE