mirror of
https://github.com/chylex/TweetDuck.git
synced 2025-04-28 00:15:47 +02:00
* Switch to .NET Framework 4.7.2 & C# 8.0, update libraries * Add TweetLib.Core project targeting .NET Standard 2.0 * Enable reference nullability checks for TweetLib.Core * Move a bunch of utility classes into TweetLib.Core & refactor * Partially move TweetDuck plugin & update system to TweetLib.Core * Move some constants and CultureInfo setup to TweetLib.Core * Move some configuration classes to TweetLib.Core * Minor refactoring and warning suppression * Add App to TweetLib.Core * Add IAppErrorHandler w/ implementation * Continue moving config, plugin, and update classes to TweetLib.Core * Fix a few nullability checks * Update installers to check for .NET Framework 4.7.2
107 lines
4.1 KiB
C#
107 lines
4.1 KiB
C#
using System;
|
|
using System.IO;
|
|
using TweetLib.Core.Serialization;
|
|
|
|
namespace TweetLib.Core.Features.Configuration{
|
|
public sealed class FileConfigInstance<T> : IConfigInstance<T> where T : BaseConfig{
|
|
public T Instance { get; }
|
|
public FileSerializer<T> Serializer { get; }
|
|
|
|
private readonly string filenameMain;
|
|
private readonly string filenameBackup;
|
|
private readonly string identifier;
|
|
|
|
public FileConfigInstance(string filename, T instance, string identifier){
|
|
this.filenameMain = filename;
|
|
this.filenameBackup = filename + ".bak";
|
|
this.identifier = identifier;
|
|
|
|
this.Instance = instance;
|
|
this.Serializer = new FileSerializer<T>();
|
|
}
|
|
|
|
private void LoadInternal(bool backup){
|
|
Serializer.Read(backup ? filenameBackup : filenameMain, Instance);
|
|
}
|
|
|
|
public void Load(){
|
|
Exception? firstException = null;
|
|
|
|
for(int attempt = 0; attempt < 2; attempt++){
|
|
try{
|
|
LoadInternal(attempt > 0);
|
|
|
|
if (firstException != null){ // silently log exception that caused a backup restore
|
|
App.ErrorHandler.Log(firstException.ToString());
|
|
}
|
|
|
|
return;
|
|
}catch(FileNotFoundException){
|
|
}catch(DirectoryNotFoundException){
|
|
break;
|
|
}catch(Exception e){
|
|
if (firstException == null){
|
|
firstException = e;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (firstException is FormatException){
|
|
OnException($"The configuration file for {identifier} is outdated or corrupted. If you continue, your {identifier} will be reset.", firstException);
|
|
}
|
|
else if (firstException is SerializationSoftException sse){
|
|
OnException($"{sse.Errors.Count} error{(sse.Errors.Count == 1 ? " was" : "s were")} encountered while loading the configuration file for {identifier}. If you continue, some of your {identifier} will be reset.", firstException);
|
|
}
|
|
else if (firstException != null){
|
|
OnException($"Could not open the configuration file for {identifier}. If you continue, your {identifier} will be reset.", firstException);
|
|
}
|
|
}
|
|
|
|
public void Save(){
|
|
try{
|
|
if (File.Exists(filenameMain)){
|
|
File.Delete(filenameBackup);
|
|
File.Move(filenameMain, filenameBackup);
|
|
}
|
|
|
|
Serializer.Write(filenameMain, Instance);
|
|
}catch(SerializationSoftException e){
|
|
OnException($"{e.Errors.Count} error{(e.Errors.Count == 1 ? " was" : "s were")} encountered while saving the configuration file for {identifier}.", e);
|
|
}catch(Exception e){
|
|
OnException($"Could not save the configuration file for {identifier}.", e);
|
|
}
|
|
}
|
|
|
|
public void Reload(){
|
|
try{
|
|
LoadInternal(false);
|
|
}catch(FileNotFoundException){
|
|
try{
|
|
Serializer.Write(filenameMain, Instance.ConstructWithDefaults<T>());
|
|
LoadInternal(false);
|
|
}catch(Exception e){
|
|
OnException($"Could not regenerate the configuration file for {identifier}.", e);
|
|
}
|
|
}catch(Exception e){
|
|
OnException($"Could not reload the configuration file for {identifier}.", e);
|
|
}
|
|
}
|
|
|
|
public void Reset(){
|
|
try{
|
|
File.Delete(filenameMain);
|
|
File.Delete(filenameBackup);
|
|
}catch(Exception e){
|
|
OnException($"Could not delete configuration files to reset {identifier}.", e);
|
|
return;
|
|
}
|
|
|
|
Reload();
|
|
}
|
|
|
|
private static void OnException(string message, Exception e){
|
|
App.ErrorHandler.HandleException("Configuration Error", message, true, e);
|
|
}
|
|
}
|
|
}
|