improved save method
This commit is contained in:
@@ -115,10 +115,10 @@ public class Program
|
|||||||
if (loadPluginsOnStartup) consoleCommandsHandler.HandleCommand("lp");
|
if (loadPluginsOnStartup) consoleCommandsHandler.HandleCommand("lp");
|
||||||
if (listPluginsAtStartup) consoleCommandsHandler.HandleCommand("listplugs");
|
if (listPluginsAtStartup) consoleCommandsHandler.HandleCommand("listplugs");
|
||||||
#endif
|
#endif
|
||||||
Config.SaveConfig();
|
Config.SaveConfig(SaveType.NORMAL);
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
//Console_Utilities.WriteColorText("&rSethBot (&yDEBUG&r) &c> ", false);
|
// Console_Utilities.WriteColorText("&rSethBot (&yDEBUG&r) &c> ", false);
|
||||||
var cmd = Console.ReadLine();
|
var cmd = Console.ReadLine();
|
||||||
if (!consoleCommandsHandler.HandleCommand(cmd!
|
if (!consoleCommandsHandler.HandleCommand(cmd!
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
@@ -149,6 +149,14 @@ public class Program
|
|||||||
|
|
||||||
Console_Utilities.WriteColorText("&rRemember to close the bot using the ShutDown command (&ysd&r) or some settings won't be saved\n");
|
Console_Utilities.WriteColorText("&rRemember to close the bot using the ShutDown command (&ysd&r) or some settings won't be saved\n");
|
||||||
Console.ForegroundColor = ConsoleColor.White;
|
Console.ForegroundColor = ConsoleColor.White;
|
||||||
|
|
||||||
|
if (Config.ContainsKey("LaunchMessage"))
|
||||||
|
{
|
||||||
|
Console_Utilities.WriteColorText(Config.GetValue<string>("LaunchMessage"));
|
||||||
|
Config.RemoveKey("LaunchMessage");
|
||||||
|
}
|
||||||
|
|
||||||
|
Console_Utilities.WriteColorText("Please note that the bot saves a backup save file every time you are using the shudown command (&ysd&c)");
|
||||||
Console.WriteLine($"============================ LOG ============================");
|
Console.WriteLine($"============================ LOG ============================");
|
||||||
|
|
||||||
try
|
try
|
||||||
@@ -243,7 +251,26 @@ public class Program
|
|||||||
|
|
||||||
if (len == 0 || (args[0] != "--exec" && args[0] != "--execute"))
|
if (len == 0 || (args[0] != "--exec" && args[0] != "--execute"))
|
||||||
{
|
{
|
||||||
Thread mainThread = new Thread(() => NoGUI(b));
|
|
||||||
|
Thread mainThread = new Thread(() =>
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
NoGUI(b);
|
||||||
|
}
|
||||||
|
catch (IOException ex)
|
||||||
|
{
|
||||||
|
if (ex.Message == "No process is on the other end of the pipe." || (uint)ex.HResult == 0x800700E9)
|
||||||
|
{
|
||||||
|
if (!Config.ContainsKey("LaunchMessage"))
|
||||||
|
Config.AddValueToVariables("LaunchMessage", "An error occured while closing the bot last time. Please consider closing the bot using the &rsd&c method !\nThere is a risk of losing all data or corruption of the save file, which in some cases requires to reinstall the bot !", false);
|
||||||
|
Functions.WriteErrFile(ex.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
});
|
||||||
mainThread.Start();
|
mainThread.Start();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -377,6 +404,6 @@ public class Program
|
|||||||
|
|
||||||
Console_Utilities.Initialize();
|
Console_Utilities.Initialize();
|
||||||
|
|
||||||
Config.SaveConfig();
|
Config.SaveConfig(SaveType.NORMAL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,8 +13,7 @@ namespace PluginManager
|
|||||||
{
|
{
|
||||||
public Dictionary<string, object>? ApplicationVariables { get; init; }
|
public Dictionary<string, object>? ApplicationVariables { get; init; }
|
||||||
public List<string>? ProtectedKeyWords { get; init; }
|
public List<string>? ProtectedKeyWords { get; init; }
|
||||||
|
public Dictionary<string, string>? PluginVersions { get; init; }
|
||||||
public Dictionary<string, string> PluginVersions { get; init; }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Config
|
public static class Config
|
||||||
@@ -59,9 +58,8 @@ namespace PluginManager
|
|||||||
public static bool Contains(string pluginName)
|
public static bool Contains(string pluginName)
|
||||||
{
|
{
|
||||||
foreach (var tuple in InstalledPlugins)
|
foreach (var tuple in InstalledPlugins)
|
||||||
{
|
if (tuple.Item1 == pluginName)
|
||||||
if (tuple.Item1 == pluginName) return true;
|
return true;
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -79,18 +77,18 @@ namespace PluginManager
|
|||||||
|
|
||||||
private static AppConfig? appConfig { get; set; }
|
private static AppConfig? appConfig { get; set; }
|
||||||
|
|
||||||
public static string GetPluginVersion(string pluginName) => appConfig.PluginVersions[pluginName];
|
public static string GetPluginVersion(string pluginName) => appConfig!.PluginVersions![pluginName];
|
||||||
public static void SetPluginVersion(string pluginName, string newVersion)
|
public static void SetPluginVersion(string pluginName, string newVersion)
|
||||||
{
|
{
|
||||||
if (appConfig.PluginVersions.ContainsKey(pluginName))
|
if (appConfig!.PluginVersions!.ContainsKey(pluginName))
|
||||||
appConfig.PluginVersions[pluginName] = newVersion;
|
appConfig.PluginVersions[pluginName] = newVersion;
|
||||||
else appConfig.PluginVersions.Add(pluginName, newVersion);
|
else appConfig.PluginVersions.Add(pluginName, newVersion);
|
||||||
|
|
||||||
// SaveConfig();
|
// SaveConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void RemovePluginVersion(string pluginName) => appConfig.PluginVersions.Remove(pluginName);
|
public static void RemovePluginVersion(string pluginName) => appConfig!.PluginVersions!.Remove(pluginName);
|
||||||
public static bool PluginVersionsContainsKey(string pluginName) => appConfig.PluginVersions.ContainsKey(pluginName);
|
public static bool PluginVersionsContainsKey(string pluginName) => appConfig!.PluginVersions!.ContainsKey(pluginName);
|
||||||
|
|
||||||
public static void AddValueToVariables<T>(string key, T value, bool isProtected)
|
public static void AddValueToVariables<T>(string key, T value, bool isProtected)
|
||||||
{
|
{
|
||||||
@@ -103,7 +101,7 @@ namespace PluginManager
|
|||||||
if (isProtected && key != "Version")
|
if (isProtected && key != "Version")
|
||||||
appConfig.ProtectedKeyWords!.Add(key);
|
appConfig.ProtectedKeyWords!.Add(key);
|
||||||
|
|
||||||
SaveConfig();
|
SaveConfig(SaveType.NORMAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Type GetVariableType(string value)
|
public static Type GetVariableType(string value)
|
||||||
@@ -171,7 +169,7 @@ namespace PluginManager
|
|||||||
throw new Exception("Key is protected");
|
throw new Exception("Key is protected");
|
||||||
|
|
||||||
appConfig.ApplicationVariables[key] = JsonSerializer.SerializeToElement(value);
|
appConfig.ApplicationVariables[key] = JsonSerializer.SerializeToElement(value);
|
||||||
SaveConfig();
|
SaveConfig(SaveType.NORMAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void RemoveKey(string key)
|
public static void RemoveKey(string key)
|
||||||
@@ -180,13 +178,24 @@ namespace PluginManager
|
|||||||
throw new Exception("Key is protected");
|
throw new Exception("Key is protected");
|
||||||
appConfig!.ApplicationVariables!.Remove(key);
|
appConfig!.ApplicationVariables!.Remove(key);
|
||||||
appConfig.ProtectedKeyWords!.Remove(key);
|
appConfig.ProtectedKeyWords!.Remove(key);
|
||||||
SaveConfig();
|
SaveConfig(SaveType.NORMAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async void SaveConfig()
|
public static async void SaveConfig(SaveType type)
|
||||||
{
|
{
|
||||||
string path = Functions.dataFolder + "config.json";
|
if (type == SaveType.NORMAL)
|
||||||
await Functions.SaveToJsonFile<AppConfig>(path, appConfig!);
|
{
|
||||||
|
string path = Functions.dataFolder + "config.json";
|
||||||
|
await Functions.SaveToJsonFile<AppConfig>(path, appConfig!);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (type == SaveType.BACKUP)
|
||||||
|
{
|
||||||
|
string path = Functions.dataFolder + "config.json.bak";
|
||||||
|
await Functions.SaveToJsonFile<AppConfig>(path, appConfig!);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task LoadConfig()
|
public static async Task LoadConfig()
|
||||||
@@ -194,11 +203,24 @@ namespace PluginManager
|
|||||||
string path = Functions.dataFolder + "config.json";
|
string path = Functions.dataFolder + "config.json";
|
||||||
if (File.Exists(path))
|
if (File.Exists(path))
|
||||||
{
|
{
|
||||||
appConfig = await Functions.ConvertFromJson<AppConfig>(path);
|
try
|
||||||
|
{
|
||||||
|
appConfig = await Functions.ConvertFromJson<AppConfig>(path);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
File.Delete(path);
|
||||||
|
Console.WriteLine("An error occured while loading the settings. Importing from backup file...");
|
||||||
|
path = Functions.dataFolder + "config.json.bak";
|
||||||
|
appConfig = await Functions.ConvertFromJson<AppConfig>(path);
|
||||||
|
Functions.WriteErrFile(ex.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Functions.WriteLogFile($"Loaded {appConfig.ApplicationVariables!.Keys.Count} application variables.\nLoaded {appConfig.ProtectedKeyWords!.Count} readonly variables.");
|
Functions.WriteLogFile($"Loaded {appConfig.ApplicationVariables!.Keys.Count} application variables.\nLoaded {appConfig.ProtectedKeyWords!.Count} readonly variables.");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
else
|
appConfig = new() { ApplicationVariables = new Dictionary<string, object>(), ProtectedKeyWords = new List<string>(), PluginVersions = new Dictionary<string, string>() };
|
||||||
appConfig = new() { ApplicationVariables = new Dictionary<string, object>(), ProtectedKeyWords = new List<string>(), PluginVersions = new Dictionary<string, string>() };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool ContainsValue<T>(T value) => appConfig!.ApplicationVariables!.ContainsValue(value!);
|
public static bool ContainsValue<T>(T value) => appConfig!.ApplicationVariables!.ContainsValue(value!);
|
||||||
|
|||||||
@@ -261,7 +261,8 @@ public class ConsoleCommandsHandler
|
|||||||
return;
|
return;
|
||||||
client.StopAsync();
|
client.StopAsync();
|
||||||
client.DisposeAsync();
|
client.DisposeAsync();
|
||||||
Config.SaveConfig();
|
Config.SaveConfig(SaveType.NORMAL);
|
||||||
|
Config.SaveConfig(SaveType.BACKUP);
|
||||||
Console.WriteLine("Bot is closing in 2 seconds ! Please wait to save data !");
|
Console.WriteLine("Bot is closing in 2 seconds ! Please wait to save data !");
|
||||||
Thread.Sleep(2000);
|
Thread.Sleep(2000);
|
||||||
Environment.Exit(0);
|
Environment.Exit(0);
|
||||||
@@ -307,7 +308,7 @@ public class ConsoleCommandsHandler
|
|||||||
{
|
{
|
||||||
if (Functions.GetOperatingSystem() == Others.OperatingSystem.WINDOWS)
|
if (Functions.GetOperatingSystem() == Others.OperatingSystem.WINDOWS)
|
||||||
{
|
{
|
||||||
Process.Start("DiscordBot.exe ", $"/remplug {plugName}");
|
Process.Start("DiscordBot.exe", $"/remplug {plugName}");
|
||||||
await Task.Delay(100);
|
await Task.Delay(100);
|
||||||
Environment.Exit(0);
|
Environment.Exit(0);
|
||||||
}
|
}
|
||||||
@@ -345,7 +346,7 @@ public class ConsoleCommandsHandler
|
|||||||
Console.WriteLine("Found: " + tuple.ToString());
|
Console.WriteLine("Found: " + tuple.ToString());
|
||||||
Config.PluginConfig.InstalledPlugins.Remove(tuple);
|
Config.PluginConfig.InstalledPlugins.Remove(tuple);
|
||||||
Config.RemovePluginVersion(plugName);
|
Config.RemovePluginVersion(plugName);
|
||||||
Config.SaveConfig();
|
Config.SaveConfig(SaveType.NORMAL);
|
||||||
}
|
}
|
||||||
Console.WriteLine("Removed the plugin DLL. Checking for other files ...");
|
Console.WriteLine("Removed the plugin DLL. Checking for other files ...");
|
||||||
|
|
||||||
@@ -374,6 +375,21 @@ public class ConsoleCommandsHandler
|
|||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
AddCommand("reload", "Reload the bot with all plugins", () =>
|
||||||
|
{
|
||||||
|
if (Functions.GetOperatingSystem() == Others.OperatingSystem.WINDOWS)
|
||||||
|
{
|
||||||
|
Process.Start("DiscordBot.exe", $"lp");
|
||||||
|
HandleCommand("sd");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
|
||||||
|
Process.Start("./DiscordBot", $"lp");
|
||||||
|
HandleCommand("sd");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
//Sort the commands by name
|
//Sort the commands by name
|
||||||
commandList.Sort((x, y) => x.CommandName.CompareTo(y.CommandName));
|
commandList.Sort((x, y) => x.CommandName.CompareTo(y.CommandName));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -94,7 +94,7 @@ public class PluginLoader
|
|||||||
|
|
||||||
|
|
||||||
//Save the new config file (after the updates)
|
//Save the new config file (after the updates)
|
||||||
Config.SaveConfig();
|
Config.SaveConfig(SaveType.NORMAL);
|
||||||
|
|
||||||
|
|
||||||
//Load all plugins
|
//Load all plugins
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ namespace PluginManager.Online.Helpers
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#region operators
|
||||||
public static bool operator <(VersionString s1, VersionString s2) => !(s1 > s2) && s1 != s2;
|
public static bool operator <(VersionString s1, VersionString s2) => !(s1 > s2) && s1 != s2;
|
||||||
|
|
||||||
public static bool operator ==(VersionString s1, VersionString s2)
|
public static bool operator ==(VersionString s1, VersionString s2)
|
||||||
@@ -50,6 +51,8 @@ namespace PluginManager.Online.Helpers
|
|||||||
public static bool operator <=(VersionString s1, VersionString s2) => (s1 < s2 || s1 == s2);
|
public static bool operator <=(VersionString s1, VersionString s2) => (s1 < s2 || s1 == s2);
|
||||||
public static bool operator >=(VersionString s1, VersionString s2) => (s1 > s2 || s1 == s2);
|
public static bool operator >=(VersionString s1, VersionString s2) => (s1 > s2 || s1 == s2);
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
return "{PackageID: " + PackageID + ", PackageVersion: " + PackageMainVersion + ", PackageCheckVersion: " + PackageCheckVersion + "}";
|
return "{PackageID: " + PackageID + ", PackageVersion: " + PackageMainVersion + ", PackageCheckVersion: " + PackageCheckVersion + "}";
|
||||||
|
|||||||
@@ -30,4 +30,6 @@ public enum PluginType { Command, Event, Unknown }
|
|||||||
|
|
||||||
public enum UnzipProgressType { PercentageFromNumberOfFiles, PercentageFromTotalSize }
|
public enum UnzipProgressType { PercentageFromNumberOfFiles, PercentageFromTotalSize }
|
||||||
|
|
||||||
public enum TableFormat { CENTER_EACH_COLUMN_BASED, CENTER_OVERALL_LENGTH, DEFAULT }
|
public enum TableFormat { CENTER_EACH_COLUMN_BASED, CENTER_OVERALL_LENGTH, DEFAULT }
|
||||||
|
|
||||||
|
public enum SaveType { NORMAL, BACKUP }
|
||||||
@@ -261,10 +261,9 @@ namespace PluginManager.Others
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static async Task SaveToJsonFile<T>(string file, T Data)
|
public static async Task SaveToJsonFile<T>(string file, T Data)
|
||||||
{
|
{
|
||||||
File.Delete(file);
|
MemoryStream str = new MemoryStream();
|
||||||
var s = File.Open(file, FileMode.OpenOrCreate);
|
await JsonSerializer.SerializeAsync(str, Data, typeof(T), new JsonSerializerOptions { WriteIndented = true });
|
||||||
await JsonSerializer.SerializeAsync(s, Data, typeof(T), new JsonSerializerOptions { WriteIndented = true });
|
await File.WriteAllBytesAsync(file, str.ToArray());
|
||||||
s.Close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -277,8 +276,7 @@ namespace PluginManager.Others
|
|||||||
{
|
{
|
||||||
Stream text;
|
Stream text;
|
||||||
if (File.Exists(input))
|
if (File.Exists(input))
|
||||||
text = File.OpenRead(input);
|
text = new MemoryStream(await File.ReadAllBytesAsync(input));
|
||||||
|
|
||||||
else
|
else
|
||||||
text = new MemoryStream(Encoding.ASCII.GetBytes(input));
|
text = new MemoryStream(Encoding.ASCII.GetBytes(input));
|
||||||
text.Position = 0;
|
text.Position = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user