mirror of
https://github.com/tribufu/ServerManagers
synced 2026-05-06 15:17:34 +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
|
|
@ -38,6 +38,8 @@ namespace ServerManagerTool.Plugin.Discord
|
|||
|
||||
public bool Enabled => true;
|
||||
|
||||
public string ConfigFile => Path.Combine(PluginHelper.PluginFolder, Config.Default.ConfigFile);
|
||||
|
||||
public string PluginCode => Config.Default.PluginCode;
|
||||
|
||||
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;
|
||||
|
||||
if (File.Exists(ConfigFile))
|
||||
{
|
||||
PluginConfig = null;
|
||||
|
||||
var configFile = Path.Combine(PluginHelper.PluginFolder, Config.Default.ConfigFile);
|
||||
PluginConfig = JsonUtils.DeserializeFromFile<DiscordPluginConfig>(configFile);
|
||||
|
||||
if ((PluginConfig?.ConfigProfiles?.Count ?? 0) == 0)
|
||||
{
|
||||
PluginConfig = new DiscordPluginConfig();
|
||||
|
||||
SaveConfig();
|
||||
}
|
||||
|
||||
PluginConfig?.CommitChanges();
|
||||
PluginConfig = JsonUtils.DeserializeFromFile<DiscordPluginConfig>(ConfigFile);
|
||||
}
|
||||
catch (Exception ex)
|
||||
else
|
||||
{
|
||||
PluginConfig = new DiscordPluginConfig();
|
||||
Debug.WriteLine($"ERROR: {nameof(LoadConfig)}\r\n{ex.Message}");
|
||||
SaveConfig();
|
||||
}
|
||||
|
||||
if (PluginConfig is null)
|
||||
{
|
||||
throw new Exception("Profile config file could not be loaded.");
|
||||
}
|
||||
|
||||
PluginConfig.CommitChanges();
|
||||
}
|
||||
|
||||
public void OpenConfigForm(Window owner)
|
||||
{
|
||||
var window = new ConfigWindow(this, this.PluginConfig);
|
||||
var window = new ConfigWindow(this, PluginConfig);
|
||||
window.Owner = owner;
|
||||
window.ShowDialog();
|
||||
|
||||
var dialogResult = window.ShowDialog();
|
||||
if (dialogResult.HasValue && dialogResult.Value)
|
||||
{
|
||||
SaveConfig();
|
||||
LoadConfig();
|
||||
}
|
||||
LoadConfig();
|
||||
}
|
||||
|
||||
private void SaveConfig()
|
||||
internal void SaveConfig()
|
||||
{
|
||||
try
|
||||
{
|
||||
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}");
|
||||
}
|
||||
JsonUtils.SerializeToFile(PluginConfig, ConfigFile);
|
||||
PluginConfig.CommitChanges();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,12 +24,9 @@ namespace ServerManagerTool.Plugin.Discord
|
|||
public ObservableList<ConfigProfile> ConfigProfiles
|
||||
{
|
||||
get { return Get<ObservableList<ConfigProfile>>(); }
|
||||
set { Set(value); }
|
||||
private set { Set(value); }
|
||||
}
|
||||
|
||||
public override bool HasAnyChanges
|
||||
{
|
||||
get => base.HasChanges || (ConfigProfiles?.HasAnyChanges ?? false);
|
||||
}
|
||||
public override bool HasAnyChanges => base.HasChanges || ConfigProfiles.HasAnyChanges;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,6 +27,11 @@
|
|||
<li>de-DE Translation file updated.</li>
|
||||
<li>pt-BR Translation file updated.</li>
|
||||
</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>
|
||||
</div>
|
||||
</content>
|
||||
|
|
|
|||
|
|
@ -7,6 +7,29 @@
|
|||
<link href="" />
|
||||
<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>
|
||||
<id>urn:uuid:A9EC2F32-E026-485F-BD7B-C657DBA95B54</id>
|
||||
<title>1.0.19 (1.0.19.1)</title>
|
||||
|
|
|
|||
|
|
@ -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