diff --git a/src/ARKServerManager/App.xaml.cs b/src/ARKServerManager/App.xaml.cs index d4ae0ef4..74642a34 100644 --- a/src/ARKServerManager/App.xaml.cs +++ b/src/ARKServerManager/App.xaml.cs @@ -39,7 +39,7 @@ namespace ServerManagerTool public event PropertyChangedEventHandler PropertyChanged; - private CancellationTokenSource _tokenSource; + private CancellationTokenSource _tokenSourceDiscordBot; private GlobalizedApplication _globalizer; private bool _applicationStarted; private string _args; @@ -151,6 +151,18 @@ namespace ServerManagerTool } } + public bool DiscordBotStarted + { + get + { + return _tokenSourceDiscordBot != null; + } + set + { + OnPropertyChanged(); + } + } + public string Title { get @@ -498,27 +510,7 @@ namespace ServerManagerTool if (Config.Default.DiscordBotEnabled) { - _tokenSource = new CancellationTokenSource(); - - Task discordTask = Task.Run(async () => - { - var discordWhiteList = new List(); - if (Config.Default.DiscordBotWhitelist != null) - { - discordWhiteList.AddRange(Config.Default.DiscordBotWhitelist.Cast()); - } - - await ServerManagerBotFactory.GetServerManagerBot()?.StartAsync(Config.Default.DiscordBotLogLevel, Config.Default.DiscordBotToken, Config.Default.DiscordBotPrefix, Config.Default.DataDir, discordWhiteList, DiscordBotHelper.HandleDiscordCommand, DiscordBotHelper.HandleTranslation, _tokenSource.Token); - }, _tokenSource.Token) - .ContinueWith(t => { - var message = t.Exception.InnerException is null ? t.Exception.Message : t.Exception.InnerException.Message; - if (message.StartsWith("#")) - { - message = _globalizer.GetResourceString(message.Substring(1)) ?? message.Substring(1); - } - - MessageBox.Show(message, _globalizer.GetResourceString("DiscordBot_ErrorTitle"), MessageBoxButton.OK, MessageBoxImage.Error); - }, TaskContinuationOptions.OnlyOnFaulted); + StartDiscordBot(); } } @@ -560,11 +552,7 @@ namespace ServerManagerTool private void ShutDownApplication() { - if (!(_tokenSource is null)) - { - _tokenSource.Cancel(); - _tokenSource.Dispose(); - } + StopDiscordBot(); if (ApplicationStarted) { @@ -605,5 +593,60 @@ namespace ServerManagerTool SettingsUtils.BackupUserConfigSettings(Config.Default, "userconfig.json", installFolder, backupFolder); SettingsUtils.BackupUserConfigSettings(CommonConfig.Default, "commonconfig.json", installFolder, backupFolder); } + + public void StartDiscordBot() + { + if (_tokenSourceDiscordBot != null) + { + return; + } + + _tokenSourceDiscordBot = new CancellationTokenSource(); + DiscordBotStarted = true; + + Task discordTask = Task.Run(async () => + { + var discordWhiteList = new List(); + if (Config.Default.DiscordBotWhitelist != null) + { + discordWhiteList.AddRange(Config.Default.DiscordBotWhitelist.Cast()); + } + + await ServerManagerBotFactory.GetServerManagerBot()?.StartAsync(Config.Default.DiscordBotLogLevel, Config.Default.DiscordBotToken, Config.Default.DiscordBotPrefix, Config.Default.DataDir, discordWhiteList, DiscordBotHelper.HandleDiscordCommand, DiscordBotHelper.HandleTranslation, _tokenSourceDiscordBot.Token); + + if (_tokenSourceDiscordBot != null) + { + // cleanup the token + _tokenSourceDiscordBot.Dispose(); + _tokenSourceDiscordBot = null; + } + DiscordBotStarted = false; + }, _tokenSourceDiscordBot.Token) + .ContinueWith(t => { + var message = t.Exception.InnerException is null ? t.Exception.Message : t.Exception.InnerException.Message; + if (message.StartsWith("#")) + { + message = _globalizer.GetResourceString(message.Substring(1)) ?? message.Substring(1); + } + + MessageBox.Show(message, _globalizer.GetResourceString("DiscordBot_ErrorTitle"), MessageBoxButton.OK, MessageBoxImage.Error); + + if (_tokenSourceDiscordBot != null) + { + // cleanup the token + _tokenSourceDiscordBot.Dispose(); + _tokenSourceDiscordBot = null; + } + DiscordBotStarted = false; + }, TaskContinuationOptions.OnlyOnFaulted); + } + + public void StopDiscordBot() + { + if (!(_tokenSourceDiscordBot is null)) + { + _tokenSourceDiscordBot.Cancel(); + } + } } } diff --git a/src/ARKServerManager/UserControls/GlobalSettingsControl.xaml.cs b/src/ARKServerManager/UserControls/GlobalSettingsControl.xaml.cs index 3a2f2986..87297eea 100644 --- a/src/ARKServerManager/UserControls/GlobalSettingsControl.xaml.cs +++ b/src/ARKServerManager/UserControls/GlobalSettingsControl.xaml.cs @@ -29,6 +29,7 @@ namespace ServerManagerTool private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); private readonly GlobalizedApplication _globalizer = GlobalizedApplication.Instance; + public static readonly DependencyProperty AppInstanceProperty = DependencyProperty.Register(nameof(AppInstance), typeof(App), typeof(GlobalSettingsControl), new PropertyMetadata(null)); public static readonly DependencyProperty IsAdministratorProperty = DependencyProperty.Register(nameof(IsAdministrator), typeof(bool), typeof(GlobalSettingsControl), new PropertyMetadata(false)); public static readonly DependencyProperty ConfigProperty = DependencyProperty.Register(nameof(Config), typeof(Config), typeof(GlobalSettingsControl), new PropertyMetadata(null)); public static readonly DependencyProperty CommonConfigProperty = DependencyProperty.Register(nameof(CommonConfig), typeof(CommonConfig), typeof(GlobalSettingsControl), new PropertyMetadata(null)); @@ -38,11 +39,11 @@ namespace ServerManagerTool public GlobalSettingsControl() { - this.Version = GetDeployedVersion(); - + this.AppInstance = App.Instance; this.Config = Config.Default; this.CommonConfig = CommonConfig.Default; - this.DataContext = this; + this.IsAdministrator = SecurityUtils.IsAdministrator(); + this.Version = GetDeployedVersion(); InitializeComponent(); WindowUtils.RemoveDefaultResourceDictionary(this, Config.Default.DefaultGlobalizationFile); @@ -59,7 +60,13 @@ namespace ServerManagerTool } } - this.IsAdministrator = SecurityUtils.IsAdministrator(); + this.DataContext = this; + } + + public App AppInstance + { + get { return GetValue(AppInstanceProperty) as App; } + set { SetValue(AppInstanceProperty, value); } } public string Version diff --git a/src/ConanServerManager/App.xaml.cs b/src/ConanServerManager/App.xaml.cs index 761109f7..6096007d 100644 --- a/src/ConanServerManager/App.xaml.cs +++ b/src/ConanServerManager/App.xaml.cs @@ -38,7 +38,7 @@ namespace ServerManagerTool public event PropertyChangedEventHandler PropertyChanged; - private CancellationTokenSource _tokenSource; + private CancellationTokenSource _tokenSourceDiscordBot; private GlobalizedApplication _globalizer; private bool _applicationStarted; private string _args; @@ -150,6 +150,18 @@ namespace ServerManagerTool } } + public bool DiscordBotStarted + { + get + { + return _tokenSourceDiscordBot != null; + } + set + { + OnPropertyChanged(); + } + } + public string Title { get @@ -479,27 +491,7 @@ namespace ServerManagerTool if (Config.Default.DiscordBotEnabled) { - _tokenSource = new CancellationTokenSource(); - - Task discordTask = Task.Run(async () => - { - var discordWhiteList = new List(); - if (Config.Default.DiscordBotWhitelist != null) - { - discordWhiteList.AddRange(Config.Default.DiscordBotWhitelist.Cast()); - } - - await ServerManagerBotFactory.GetServerManagerBot()?.StartAsync(Config.Default.DiscordBotLogLevel, Config.Default.DiscordBotToken,Config.Default.DiscordBotPrefix, Config.Default.DataPath, discordWhiteList, DiscordBotHelper.HandleDiscordCommand, DiscordBotHelper.HandleTranslation, _tokenSource.Token); - }, _tokenSource.Token) - .ContinueWith(t => { - var message = t.Exception.InnerException is null ? t.Exception.Message : t.Exception.InnerException.Message; - if (message.StartsWith("#")) - { - message = _globalizer.GetResourceString(message.Substring(1)) ?? message.Substring(1); - } - - MessageBox.Show(message, _globalizer.GetResourceString("DiscordBot_ErrorTitle"), MessageBoxButton.OK, MessageBoxImage.Error); - }, TaskContinuationOptions.OnlyOnFaulted); + StartDiscordBot(); } } @@ -541,11 +533,7 @@ namespace ServerManagerTool private void ShutDownApplication() { - if (!(_tokenSource is null)) - { - _tokenSource.Cancel(); - _tokenSource.Dispose(); - } + StopDiscordBot(); if (this.ApplicationStarted) { @@ -586,5 +574,60 @@ namespace ServerManagerTool SettingsUtils.BackupUserConfigSettings(Config.Default, "userconfig.json", installFolder, backupFolder); SettingsUtils.BackupUserConfigSettings(CommonConfig.Default, "commonconfig.json", installFolder, backupFolder); } + + public void StartDiscordBot() + { + if (_tokenSourceDiscordBot != null) + { + return; + } + + _tokenSourceDiscordBot = new CancellationTokenSource(); + DiscordBotStarted = true; + + Task discordTask = Task.Run(async () => + { + var discordWhiteList = new List(); + if (Config.Default.DiscordBotWhitelist != null) + { + discordWhiteList.AddRange(Config.Default.DiscordBotWhitelist.Cast()); + } + + await ServerManagerBotFactory.GetServerManagerBot()?.StartAsync(Config.Default.DiscordBotLogLevel, Config.Default.DiscordBotToken, Config.Default.DiscordBotPrefix, Config.Default.DataPath, discordWhiteList, DiscordBotHelper.HandleDiscordCommand, DiscordBotHelper.HandleTranslation, _tokenSourceDiscordBot.Token); + + if (_tokenSourceDiscordBot != null) + { + // cleanup the token + _tokenSourceDiscordBot.Dispose(); + _tokenSourceDiscordBot = null; + } + DiscordBotStarted = false; + }, _tokenSourceDiscordBot.Token) + .ContinueWith(t => { + var message = t.Exception.InnerException is null ? t.Exception.Message : t.Exception.InnerException.Message; + if (message.StartsWith("#")) + { + message = _globalizer.GetResourceString(message.Substring(1)) ?? message.Substring(1); + } + + MessageBox.Show(message, _globalizer.GetResourceString("DiscordBot_ErrorTitle"), MessageBoxButton.OK, MessageBoxImage.Error); + + if (_tokenSourceDiscordBot != null) + { + // cleanup the token + _tokenSourceDiscordBot.Dispose(); + _tokenSourceDiscordBot = null; + } + DiscordBotStarted = false; + }, TaskContinuationOptions.OnlyOnFaulted); + } + + public void StopDiscordBot() + { + if (!(_tokenSourceDiscordBot is null)) + { + _tokenSourceDiscordBot.Cancel(); + } + } } }