Reworked the Config system

This commit is contained in:
2024-08-06 19:07:08 +03:00
parent 8366de28cc
commit 721c28c283
28 changed files with 740 additions and 318 deletions

View File

@@ -131,7 +131,9 @@ internal static class PluginMethods
IProgress<float> progress = new Progress<float>(p => { downloadTask.Value = p; });
await ServerCom.DownloadFileAsync(pluginLink, $"{Application.CurrentApplication.ApplicationEnvironmentVariables["PluginFolder"]}/{pluginData.Name}.dll", progress);
string baseFolder = Application.CurrentApplication.ApplicationEnvironmentVariables.Get<string>("PluginFolder");
await ServerCom.DownloadFileAsync(pluginLink, $"{baseFolder}/{pluginData.Name}.dll", progress);
downloadTask.Increment(100);
@@ -177,12 +179,9 @@ internal static class PluginMethods
task.IsIndeterminate = true;
downloadTasks.Add(new Tuple<ProgressTask, IProgress<float>, string, string>(task, progress, dependency.DownloadLink, dependency.DownloadLocation));
}
int maxParallelDownloads = 5;
if (Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey("MaxParallelDownloads"))
maxParallelDownloads = int.Parse(Application.CurrentApplication.ApplicationEnvironmentVariables["MaxParallelDownloads"]);
int maxParallelDownloads = Application.CurrentApplication.ApplicationEnvironmentVariables.Get("MaxParallelDownloads", 5);
var options = new ParallelOptions()
{
MaxDegreeOfParallelism = maxParallelDownloads,

View File

@@ -15,8 +15,7 @@ internal static class SettingsConfigExtra
if (!Application.CurrentApplication.ApplicationEnvironmentVariables.ContainsKey(key))
return;
Application.CurrentApplication.ApplicationEnvironmentVariables[key] = string.Join(' ', value);
// Config.Application.CurrentApplication.ApplicationEnvironmentVariables.SaveToFile().Wait();
Application.CurrentApplication.ApplicationEnvironmentVariables.Add(key, string.Join(' ', value));
}
internal static void RemoveSettings(string key)

View File

@@ -14,9 +14,11 @@ namespace DiscordBot.Bot.Actions
public string Description => "Access module commands";
public string Usage => "module <name>";
public string Usage => "module <command>";
public IEnumerable<InternalActionOption> ListOfOptions => [];
public IEnumerable<InternalActionOption> ListOfOptions => [
new InternalActionOption("list", "List all loaded modules")
];
public InternalActionRunType RunType => InternalActionRunType.OnCall;
@@ -24,7 +26,7 @@ namespace DiscordBot.Bot.Actions
public Task Execute(string[] args)
{
string command = args[0];
string command = args?[0];
switch(command)
{
case "list":

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DiscordBot.Bot.Actions.Extra;
using DiscordBotCore;
@@ -14,6 +15,7 @@ public class SettingsConfig: ICommandAction
public string ActionName => "config";
public string Description => "Change the settings of the bot";
public string Usage => "config <options!>";
public IEnumerable<InternalActionOption> ListOfOptions => new List<InternalActionOption>
{
new InternalActionOption("help", "Displays this message"),
@@ -21,16 +23,16 @@ public class SettingsConfig: ICommandAction
new InternalActionOption("remove", "Remove a setting"),
new InternalActionOption("add", "Add a setting")
};
public InternalActionRunType RunType => InternalActionRunType.OnCall;
public bool RequireOtherThread => false;
public Task Execute(string[] args)
{
if (args is null)
{
foreach (var settings in Application.CurrentApplication.ApplicationEnvironmentVariables)
Console.WriteLine(settings.Key + ": " + settings.Value);
PrintAllSettings();
return Task.CompletedTask;
}
@@ -59,7 +61,7 @@ public class SettingsConfig: ICommandAction
break;
case "-h":
case "-help":
case "help":
Console.WriteLine("Options:");
Console.WriteLine("-s <settingName> <newValue>: Set a setting");
Console.WriteLine("-r <settingName>: Remove a setting");
@@ -76,4 +78,85 @@ public class SettingsConfig: ICommandAction
return Task.CompletedTask;
}
private void PrintList(IList<object> list, int indentLevel)
{
bool isListOfDictionaries = list.All(item => item is IDictionary<string, object>);
if (isListOfDictionaries)
{
foreach (var item in list)
{
if (item is IDictionary<string, object> dict)
{
PrintDictionary(dict, indentLevel + 1);
}
}
}
else
{
PrintIndent(indentLevel);
Console.WriteLine(string.Join(",", list));
}
}
private void PrintDictionary(IDictionary<string, object> dictionary, int indentLevel)
{
foreach (var kvp in dictionary)
{
PrintIndent(indentLevel);
Console.Write(kvp.Key + ": ");
var value = kvp.Value;
if (value is IDictionary<string, object> dict)
{
Console.WriteLine();
PrintDictionary(dict, indentLevel + 1);
}
else if (value is IList<object> list)
{
if (list.All(item => item is IDictionary<string, object>))
{
Console.WriteLine();
PrintList(list, indentLevel + 1);
}
else
{
PrintList(list, indentLevel);
}
}
else
{
Console.WriteLine(value);
}
}
}
private void PrintIndent(int indentLevel)
{
for (int i = 0; i < indentLevel; i++)
{
Console.Write(" "); // Two spaces for each indentation level
}
}
private void PrintAllSettings()
{
var settings = Application.CurrentApplication.ApplicationEnvironmentVariables;
foreach (var setting in settings)
{
Console.WriteLine("Setting: " + setting.Key);
if (setting.Value is IDictionary<string, object> dict)
{
PrintDictionary(dict, 1);
}
else if (setting.Value is IList<object> list)
{
PrintList(list, 1);
}
else
{
Console.WriteLine(setting.Value);
}
}
}
}