1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2025-05-03 14:34:08 +02:00

Refactor method order and return types in config file classes

This commit is contained in:
chylex 2017-11-07 18:36:52 +01:00
parent 9a6b615174
commit 5b2daf9746
5 changed files with 20 additions and 30 deletions
Configuration
Core/Other/Analytics
Plugins
tests/Configuration

View File

@ -31,13 +31,11 @@ private SystemConfig(string file){
this.file = file; this.file = file;
} }
public bool Save(){ public void Save(){
try{ try{
Serializer.Write(file, this); Serializer.Write(file, this);
return true;
}catch(Exception e){ }catch(Exception e){
Program.Reporter.HandleException("Configuration Error", "Could not save the system configuration file.", true, e); Program.Reporter.HandleException("Configuration Error", "Could not save the system configuration file.", true, e);
return false;
} }
} }

View File

@ -140,7 +140,7 @@ private UserConfig(string file){
this.file = file; this.file = file;
} }
public bool Save(){ public void Save(){
try{ try{
if (File.Exists(file)){ if (File.Exists(file)){
string backupFile = GetBackupFile(file); string backupFile = GetBackupFile(file);
@ -149,29 +149,23 @@ public bool Save(){
} }
Serializer.Write(file, this); Serializer.Write(file, this);
return true;
}catch(Exception e){ }catch(Exception e){
Program.Reporter.HandleException("Configuration Error", "Could not save the configuration file.", true, e); Program.Reporter.HandleException("Configuration Error", "Could not save the configuration file.", true, e);
return false;
} }
} }
public bool Reload(){ public void Reload(){
try{ try{
LoadInternal(false); LoadInternal(false);
return true;
}catch(FileNotFoundException){ }catch(FileNotFoundException){
try{ try{
Serializer.Write(file, new UserConfig(file)); Serializer.Write(file, new UserConfig(file));
LoadInternal(false); LoadInternal(false);
return true;
}catch(Exception e){ }catch(Exception e){
Program.Reporter.HandleException("Configuration Error", "Could not regenerate configuration file.", true, e); Program.Reporter.HandleException("Configuration Error", "Could not regenerate configuration file.", true, e);
return false;
} }
}catch(Exception e){ }catch(Exception e){
Program.Reporter.HandleException("Configuration Error", "Could not reload configuration file.", true, e); Program.Reporter.HandleException("Configuration Error", "Could not reload configuration file.", true, e);
return false;
} }
} }

View File

@ -26,13 +26,11 @@ private AnalyticsFile(string file){
this.file = file; this.file = file;
} }
public bool Save(){ public void Save(){
try{ try{
Serializer.Write(file, this); Serializer.Write(file, this);
return true;
}catch(Exception e){ }catch(Exception e){
Program.Reporter.HandleException("Analytics File Error", "Could not save the analytics file.", true, e); Program.Reporter.HandleException("Analytics File Error", "Could not save the analytics file.", true, e);
return false;
} }
} }

View File

@ -32,6 +32,20 @@ public bool IsEnabled(Plugin plugin){
return !disabled.Contains(plugin.Identifier); return !disabled.Contains(plugin.Identifier);
} }
public void Save(string file){
try{
using(StreamWriter writer = new StreamWriter(new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.None), Encoding.UTF8)){
writer.WriteLine("#Disabled");
foreach(string identifier in disabled){
writer.WriteLine(identifier);
}
}
}catch(Exception e){
Program.Reporter.HandleException("Plugin Configuration Error", "Could not save the plugin configuration file.", true, e);
}
}
public void Load(string file){ public void Load(string file){
try{ try{
using(StreamReader reader = new StreamReader(new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read), Encoding.UTF8)){ using(StreamReader reader = new StreamReader(new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read), Encoding.UTF8)){
@ -54,19 +68,5 @@ public void Load(string file){
Program.Reporter.HandleException("Plugin Configuration Error", "Could not read the plugin configuration file. If you continue, the list of disabled plugins will be reset to default.", true, e); Program.Reporter.HandleException("Plugin Configuration Error", "Could not read the plugin configuration file. If you continue, the list of disabled plugins will be reset to default.", true, e);
} }
} }
public void Save(string file){
try{
using(StreamWriter writer = new StreamWriter(new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.None), Encoding.UTF8)){
writer.WriteLine("#Disabled");
foreach(string identifier in disabled){
writer.WriteLine(identifier);
}
}
}catch(Exception e){
Program.Reporter.HandleException("Plugin Configuration Error", "Could not save the plugin configuration file.", true, e);
}
}
} }
} }

View File

@ -76,7 +76,7 @@ public void TestReload(){
cfg.Save(); cfg.Save();
cfg.ZoomLevel = 200; cfg.ZoomLevel = 200;
Assert.IsTrue(cfg.Reload()); cfg.Reload();
Assert.AreEqual(123, cfg.ZoomLevel); Assert.AreEqual(123, cfg.ZoomLevel);
} }
@ -87,7 +87,7 @@ public void TestReset(){
cfg.Save(); cfg.Save();
File.Delete("reset"); File.Delete("reset");
Assert.IsTrue(cfg.Reload()); cfg.Reload();
Assert.AreEqual(100, cfg.ZoomLevel); Assert.AreEqual(100, cfg.ZoomLevel);
Assert.IsTrue(File.Exists("reset")); Assert.IsTrue(File.Exists("reset"));
} }