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:
Brett Hewitson 2021-12-09 17:10:07 +10:00
parent d373bf0866
commit 6c50f8be6f
6 changed files with 79 additions and 71 deletions

View file

@ -38,6 +38,8 @@ namespace ServerManagerTool.Plugin.Discord
public bool Enabled => true; public bool Enabled => true;
public string ConfigFile => Path.Combine(PluginHelper.PluginFolder, Config.Default.ConfigFile);
public string PluginCode => Config.Default.PluginCode; public string PluginCode => Config.Default.PluginCode;
public string PluginName => Config.Default.PluginName; public string PluginName => Config.Default.PluginName;
@ -227,56 +229,50 @@ namespace ServerManagerTool.Plugin.Discord
} }
} }
private void LoadConfig() internal void BackupConfig()
{ {
try if (!File.Exists(ConfigFile))
return;
var backupFile = Path.ChangeExtension(ConfigFile, "bak");
File.Copy(ConfigFile, backupFile, true);
}
internal void LoadConfig()
{ {
PluginConfig = null; PluginConfig = null;
var configFile = Path.Combine(PluginHelper.PluginFolder, Config.Default.ConfigFile); if (File.Exists(ConfigFile))
PluginConfig = JsonUtils.DeserializeFromFile<DiscordPluginConfig>(configFile); {
PluginConfig = JsonUtils.DeserializeFromFile<DiscordPluginConfig>(ConfigFile);
if ((PluginConfig?.ConfigProfiles?.Count ?? 0) == 0) }
else
{ {
PluginConfig = new DiscordPluginConfig(); PluginConfig = new DiscordPluginConfig();
SaveConfig(); SaveConfig();
} }
PluginConfig?.CommitChanges(); if (PluginConfig is null)
}
catch (Exception ex)
{ {
PluginConfig = new DiscordPluginConfig(); throw new Exception("Profile config file could not be loaded.");
Debug.WriteLine($"ERROR: {nameof(LoadConfig)}\r\n{ex.Message}");
} }
PluginConfig.CommitChanges();
} }
public void OpenConfigForm(Window owner) public void OpenConfigForm(Window owner)
{ {
var window = new ConfigWindow(this, this.PluginConfig); var window = new ConfigWindow(this, PluginConfig);
window.Owner = owner; window.Owner = owner;
window.ShowDialog();
var dialogResult = window.ShowDialog();
if (dialogResult.HasValue && dialogResult.Value)
{
SaveConfig();
LoadConfig(); LoadConfig();
} }
}
private void SaveConfig() internal void SaveConfig()
{ {
try JsonUtils.SerializeToFile(PluginConfig, ConfigFile);
{ PluginConfig.CommitChanges();
var configFile = Path.Combine(PluginHelper.PluginFolder, Config.Default.ConfigFile);
JsonUtils.SerializeToFile(PluginConfig, configFile);
PluginConfig?.CommitChanges();
}
catch (Exception ex)
{
Debug.WriteLine($"ERROR: {nameof(SaveConfig)}\r\n{ex.Message}");
}
} }
} }
} }

View file

@ -24,12 +24,9 @@ namespace ServerManagerTool.Plugin.Discord
public ObservableList<ConfigProfile> ConfigProfiles public ObservableList<ConfigProfile> ConfigProfiles
{ {
get { return Get<ObservableList<ConfigProfile>>(); } get { return Get<ObservableList<ConfigProfile>>(); }
set { Set(value); } private set { Set(value); }
} }
public override bool HasAnyChanges public override bool HasAnyChanges => base.HasChanges || ConfigProfiles.HasAnyChanges;
{
get => base.HasChanges || (ConfigProfiles?.HasAnyChanges ?? false);
}
} }
} }

View file

@ -27,6 +27,11 @@
<li>de-DE Translation file updated.</li> <li>de-DE Translation file updated.</li>
<li>pt-BR Translation file updated.</li> <li>pt-BR Translation file updated.</li>
</ul> </ul>
<u style="font-size: .9em;">CHANGE</u>
<br/>
<ul>
<li>Changed the plugin initialization so it throws an exception if unable to load config file, rather than creating a new config file.</li>
</ul>
</p> </p>
</div> </div>
</content> </content>

View file

@ -7,6 +7,29 @@
<link href="" /> <link href="" />
<updated>2021-12-09T00:00:00Z</updated> <updated>2021-12-09T00:00:00Z</updated>
<entry>
<id>urn:uuid:FD4F56C3-9E44-4422-A761-87E7F957454F</id>
<title>1.0.19 (1.0.19.2)</title>
<summary>1.0.19.2</summary>
<link href="" />
<updated>2021-12-09T00:00:00Z</updated>
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml" style="font-family: Arial, Verdana, Helvetica, Sans-Serif;font-size: .8em;">
<p>
<u style="font-size: .9em;">CHANGE</u>
<br/>
<ul>
<li>Changed the plugin initialization so it throws an exception if unable to load config file, rather than creating a new config file.</li>
</ul>
</p>
</div>
</content>
<author>
<name>bletch</name>
<email>bletch1971@hotmail.com</email>
</author>
</entry>
<entry> <entry>
<id>urn:uuid:A9EC2F32-E026-485F-BD7B-C657DBA95B54</id> <id>urn:uuid:A9EC2F32-E026-485F-BD7B-C657DBA95B54</id>
<title>1.0.19 (1.0.19.1)</title> <title>1.0.19 (1.0.19.1)</title>

View file

@ -25,7 +25,7 @@ namespace ServerManagerTool.Plugin.Discord.Windows
InitializeComponent(); InitializeComponent();
WindowUtils.UpdateResourceDictionary(this, plugin.LanguageCode); WindowUtils.UpdateResourceDictionary(this, plugin.LanguageCode);
this.Plugin = plugin ?? new DiscordPlugin(); this.Plugin = plugin;
this.OriginalProfile = profile; this.OriginalProfile = profile;
this.Profile = profile.Clone(); this.Profile = profile.Clone();
this.Profile.CommitChanges(); this.Profile.CommitChanges();
@ -77,7 +77,10 @@ namespace ServerManagerTool.Plugin.Discord.Windows
if (this.Profile.HasAnyChanges) 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) if (MessageBox.Show(ResourceUtils.GetResourceString(this.Resources, "ConfigProfileWindow_CloseLabel"), ResourceUtils.GetResourceString(this.Resources, "ConfigProfileWindow_CloseTitle"), MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
{
e.Cancel = true; e.Cancel = true;
return;
}
} }
} }

View file

@ -23,8 +23,8 @@ namespace ServerManagerTool.Plugin.Discord.Windows
InitializeComponent(); InitializeComponent();
WindowUtils.UpdateResourceDictionary(this, plugin.LanguageCode); WindowUtils.UpdateResourceDictionary(this, plugin.LanguageCode);
this.Plugin = plugin ?? new DiscordPlugin(); this.Plugin = plugin;
this.PluginConfig = pluginConfig ?? new DiscordPluginConfig(); this.PluginConfig = pluginConfig;
if (plugin.BetaEnabled) if (plugin.BetaEnabled)
Title = $"{Title} {ResourceUtils.GetResourceString(this.Resources, "Global_BetaModeLabel")}"; Title = $"{Title} {ResourceUtils.GetResourceString(this.Resources, "Global_BetaModeLabel")}";
@ -64,7 +64,10 @@ namespace ServerManagerTool.Plugin.Discord.Windows
if (PluginConfig.HasAnyChanges) if (PluginConfig.HasAnyChanges)
{ {
if (MessageBox.Show(ResourceUtils.GetResourceString(this.Resources, "ConfigWindow_CloseLabel"), ResourceUtils.GetResourceString(this.Resources, "ConfigWindow_CloseTitle"), MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes) if (MessageBox.Show(ResourceUtils.GetResourceString(this.Resources, "ConfigWindow_CloseLabel"), ResourceUtils.GetResourceString(this.Resources, "ConfigWindow_CloseTitle"), MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
{
e.Cancel = true; e.Cancel = true;
return;
}
} }
} }
@ -91,8 +94,7 @@ namespace ServerManagerTool.Plugin.Discord.Windows
try try
{ {
var profile = new ConfigProfile(); var profile = new ConfigProfile();
if (OpenConfigProfile(profile))
if (EditProfile(profile))
PluginConfig.ConfigProfiles.Add(profile); PluginConfig.ConfigProfiles.Add(profile);
} }
catch (Exception ex) catch (Exception ex)
@ -143,7 +145,7 @@ namespace ServerManagerTool.Plugin.Discord.Windows
try try
{ {
var profile = ((ConfigProfile)((Button)e.Source).DataContext); var profile = ((ConfigProfile)((Button)e.Source).DataContext);
EditProfile(profile); OpenConfigProfile(profile);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -173,32 +175,21 @@ namespace ServerManagerTool.Plugin.Discord.Windows
{ {
try try
{ {
BackupExistingConfig(); Plugin.BackupConfig();
SaveConfig();
} }
catch (Exception ex) catch (Exception ex)
{ {
Debug.WriteLine($"ERROR: {nameof(Save_Click)}\r\n{ex.Message}"); Debug.WriteLine($"ERROR: {nameof(Save_Click)} - Backup\r\n{ex.Message}");
MessageBox.Show(ResourceUtils.GetResourceString(this.Resources, "ConfigWindow_SaveErrorLabel"), ResourceUtils.GetResourceString(this.Resources, "ConfigWindow_SaveErrorTitle"), MessageBoxButton.OK, MessageBoxImage.Error);
} }
}
private void BackupExistingConfig()
{
var configFile = Path.Combine(PluginHelper.PluginFolder, Config.Default.ConfigFile);
if (!File.Exists(configFile))
return;
var backupFile = Path.ChangeExtension(configFile, "bak");
try try
{ {
File.Copy(configFile, backupFile, true); Plugin.SaveConfig();
} }
catch catch (Exception ex)
{ {
// do nothing, just exit if cannot backup existing config file Debug.WriteLine($"ERROR: {nameof(Save_Click)} - Save\r\n{ex.Message}");
throw; 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) if (profile == null)
return false; return false;
@ -262,13 +253,6 @@ namespace ServerManagerTool.Plugin.Discord.Windows
return dialogResult.HasValue && dialogResult.Value; 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 #region Drag and Drop
private static readonly DependencyProperty DraggedItemProperty = DependencyProperty.Register(nameof(DraggedItem), typeof(ConfigProfile), typeof(ConfigWindow), new PropertyMetadata(null)); private static readonly DependencyProperty DraggedItemProperty = DependencyProperty.Register(nameof(DraggedItem), typeof(ConfigProfile), typeof(ConfigWindow), new PropertyMetadata(null));