mirror of
https://github.com/Tribufu/ServerManagers
synced 2026-08-09 04:31:06 +00:00
Changed the plugin initialization so it throws an exception if unable to load config file, rather than creating a new config file.
This commit is contained in:
parent
d373bf0866
commit
6c50f8be6f
6 changed files with 79 additions and 71 deletions
|
|
@ -25,7 +25,7 @@ namespace ServerManagerTool.Plugin.Discord.Windows
|
|||
InitializeComponent();
|
||||
WindowUtils.UpdateResourceDictionary(this, plugin.LanguageCode);
|
||||
|
||||
this.Plugin = plugin ?? new DiscordPlugin();
|
||||
this.Plugin = plugin;
|
||||
this.OriginalProfile = profile;
|
||||
this.Profile = profile.Clone();
|
||||
this.Profile.CommitChanges();
|
||||
|
|
@ -77,7 +77,10 @@ namespace ServerManagerTool.Plugin.Discord.Windows
|
|||
if (this.Profile.HasAnyChanges)
|
||||
{
|
||||
if (MessageBox.Show(ResourceUtils.GetResourceString(this.Resources, "ConfigProfileWindow_CloseLabel"), ResourceUtils.GetResourceString(this.Resources, "ConfigProfileWindow_CloseTitle"), MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
|
||||
{
|
||||
e.Cancel = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -23,8 +23,8 @@ namespace ServerManagerTool.Plugin.Discord.Windows
|
|||
InitializeComponent();
|
||||
WindowUtils.UpdateResourceDictionary(this, plugin.LanguageCode);
|
||||
|
||||
this.Plugin = plugin ?? new DiscordPlugin();
|
||||
this.PluginConfig = pluginConfig ?? new DiscordPluginConfig();
|
||||
this.Plugin = plugin;
|
||||
this.PluginConfig = pluginConfig;
|
||||
|
||||
if (plugin.BetaEnabled)
|
||||
Title = $"{Title} {ResourceUtils.GetResourceString(this.Resources, "Global_BetaModeLabel")}";
|
||||
|
|
@ -64,7 +64,10 @@ namespace ServerManagerTool.Plugin.Discord.Windows
|
|||
if (PluginConfig.HasAnyChanges)
|
||||
{
|
||||
if (MessageBox.Show(ResourceUtils.GetResourceString(this.Resources, "ConfigWindow_CloseLabel"), ResourceUtils.GetResourceString(this.Resources, "ConfigWindow_CloseTitle"), MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
|
||||
{
|
||||
e.Cancel = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -91,8 +94,7 @@ namespace ServerManagerTool.Plugin.Discord.Windows
|
|||
try
|
||||
{
|
||||
var profile = new ConfigProfile();
|
||||
|
||||
if (EditProfile(profile))
|
||||
if (OpenConfigProfile(profile))
|
||||
PluginConfig.ConfigProfiles.Add(profile);
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
|
@ -143,7 +145,7 @@ namespace ServerManagerTool.Plugin.Discord.Windows
|
|||
try
|
||||
{
|
||||
var profile = ((ConfigProfile)((Button)e.Source).DataContext);
|
||||
EditProfile(profile);
|
||||
OpenConfigProfile(profile);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
|
@ -173,32 +175,21 @@ namespace ServerManagerTool.Plugin.Discord.Windows
|
|||
{
|
||||
try
|
||||
{
|
||||
BackupExistingConfig();
|
||||
SaveConfig();
|
||||
Plugin.BackupConfig();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine($"ERROR: {nameof(Save_Click)}\r\n{ex.Message}");
|
||||
MessageBox.Show(ResourceUtils.GetResourceString(this.Resources, "ConfigWindow_SaveErrorLabel"), ResourceUtils.GetResourceString(this.Resources, "ConfigWindow_SaveErrorTitle"), MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
Debug.WriteLine($"ERROR: {nameof(Save_Click)} - Backup\r\n{ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private void BackupExistingConfig()
|
||||
{
|
||||
var configFile = Path.Combine(PluginHelper.PluginFolder, Config.Default.ConfigFile);
|
||||
if (!File.Exists(configFile))
|
||||
return;
|
||||
|
||||
var backupFile = Path.ChangeExtension(configFile, "bak");
|
||||
|
||||
try
|
||||
{
|
||||
File.Copy(configFile, backupFile, true);
|
||||
Plugin.SaveConfig();
|
||||
}
|
||||
catch
|
||||
catch (Exception ex)
|
||||
{
|
||||
// do nothing, just exit if cannot backup existing config file
|
||||
throw;
|
||||
Debug.WriteLine($"ERROR: {nameof(Save_Click)} - Save\r\n{ex.Message}");
|
||||
MessageBox.Show(ResourceUtils.GetResourceString(this.Resources, "ConfigWindow_SaveErrorLabel"), ResourceUtils.GetResourceString(this.Resources, "ConfigWindow_SaveErrorTitle"), MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -248,7 +239,7 @@ namespace ServerManagerTool.Plugin.Discord.Windows
|
|||
}
|
||||
}
|
||||
|
||||
private bool EditProfile(ConfigProfile profile)
|
||||
private bool OpenConfigProfile(ConfigProfile profile)
|
||||
{
|
||||
if (profile == null)
|
||||
return false;
|
||||
|
|
@ -262,13 +253,6 @@ namespace ServerManagerTool.Plugin.Discord.Windows
|
|||
return dialogResult.HasValue && dialogResult.Value;
|
||||
}
|
||||
|
||||
private void SaveConfig()
|
||||
{
|
||||
var configFile = Path.Combine(PluginHelper.PluginFolder, Config.Default.ConfigFile);
|
||||
JsonUtils.SerializeToFile(PluginConfig, configFile);
|
||||
PluginConfig?.CommitChanges();
|
||||
}
|
||||
|
||||
#region Drag and Drop
|
||||
|
||||
private static readonly DependencyProperty DraggedItemProperty = DependencyProperty.Register(nameof(DraggedItem), typeof(ConfigProfile), typeof(ConfigWindow), new PropertyMetadata(null));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue