Configuration Changes

- Added a check for a valid configuration file when starting the server manager.
If config file is not valid, then it will delete the config file and shutdown the manager, with a message to restart the manager.
This commit is contained in:
Brett Hewitson 2022-07-16 13:50:46 +10:00
parent 9e52ccbaa1
commit 5fff2abc91
14 changed files with 244 additions and 76 deletions

View file

@ -1,5 +1,6 @@
using System;
using System.ComponentModel;
using System.Configuration;
using System.Diagnostics;
using System.Globalization;
using System.IO;
@ -50,43 +51,12 @@ namespace ServerManagerTool
public App()
{
if (string.IsNullOrWhiteSpace(Config.Default.ServerManagerUniqueKey))
{
Config.Default.ServerManagerUniqueKey = Guid.NewGuid().ToString();
}
if (!string.IsNullOrWhiteSpace(Config.Default.DataDir))
{
Config.Default.DataDir = IOUtils.NormalizeFolder(Config.Default.DataDir);
}
if (!string.IsNullOrWhiteSpace(Config.Default.ConfigDirectory))
{
Config.Default.ConfigDirectory = IOUtils.NormalizeFolder(Config.Default.ConfigDirectory);
}
if (!string.IsNullOrWhiteSpace(Config.Default.BackupPath))
{
Config.Default.BackupPath = IOUtils.NormalizeFolder(Config.Default.BackupPath);
}
if (!string.IsNullOrWhiteSpace(Config.Default.AutoUpdate_CacheDir))
{
Config.Default.AutoUpdate_CacheDir = IOUtils.NormalizeFolder(Config.Default.AutoUpdate_CacheDir);
}
App.Instance = this;
Instance = this;
ApplicationStarted = false;
Args = string.Empty;
BetaVersion = false;
Title = string.Empty;
Version = AppUtils.GetDeployedVersion(Assembly.GetEntryAssembly());
ServicePointManager.SecurityProtocol = SecurityUtils.GetSecurityProtocol(0xC00); // TLS12
AppDomain.CurrentDomain.UnhandledException += ErrorHandling.CurrentDomain_UnhandledException;
MigrateSettings();
ReconfigureLogging();
}
public bool ApplicationStarted
@ -195,6 +165,37 @@ namespace ServerManagerTool
}
}
private void CheckForValidSettings()
{
try
{
// test one property from both setting files
var upgradeConfigTest = Config.Default.UpgradeConfig;
upgradeConfigTest = CommonConfig.Default.UpgradeConfig;
// no issues fetching the setting values, must have valid files.
return;
}
catch (ConfigurationErrorsException ex)
{
// one or more issues occurred when fetching the setting values.
// we need to delete the setting files.
ConfigurationErrorsException exception = ex;
while (exception != null)
{
if (!string.IsNullOrWhiteSpace(exception.Filename) && exception.Filename.EndsWith("user.config"))
{
File.Delete(exception.Filename);
}
exception = exception.InnerException as ConfigurationErrorsException;
}
throw;
}
}
public static void DiscoverMachinePublicIP(bool forceOverride)
{
if (forceOverride || string.IsNullOrWhiteSpace(Config.Default.MachinePublicIP))
@ -309,6 +310,50 @@ namespace ServerManagerTool
{
base.OnStartup(e);
try
{
CheckForValidSettings();
}
catch (Exception ex)
{
var message = $"{ex.Message}\r\n\r\nTry restarting the server manager, if this keeps happening please report this crash to the Server Manager discord.";
var result = MessageBox.Show(message, "Server Manager crashed", MessageBoxButton.OK, MessageBoxImage.Exclamation);
Environment.Exit(1);
}
MigrateSettings();
if (string.IsNullOrWhiteSpace(Config.Default.ServerManagerUniqueKey))
{
Config.Default.ServerManagerUniqueKey = Guid.NewGuid().ToString();
}
if (!string.IsNullOrWhiteSpace(Config.Default.DataDir))
{
Config.Default.DataDir = IOUtils.NormalizeFolder(Config.Default.DataDir);
}
if (!string.IsNullOrWhiteSpace(Config.Default.ConfigDirectory))
{
Config.Default.ConfigDirectory = IOUtils.NormalizeFolder(Config.Default.ConfigDirectory);
}
if (!string.IsNullOrWhiteSpace(Config.Default.BackupPath))
{
Config.Default.BackupPath = IOUtils.NormalizeFolder(Config.Default.BackupPath);
}
if (!string.IsNullOrWhiteSpace(Config.Default.AutoUpdate_CacheDir))
{
Config.Default.AutoUpdate_CacheDir = IOUtils.NormalizeFolder(Config.Default.AutoUpdate_CacheDir);
}
ServicePointManager.SecurityProtocol = SecurityUtils.GetSecurityProtocol(Config.Default.ServicePointManager_SecurityProtocol);
AppDomain.CurrentDomain.UnhandledException += ErrorHandling.CurrentDomain_UnhandledException;
ReconfigureLogging();
_globalizer = GlobalizedApplication.Instance;
try
{