mirror of
https://github.com/chylex/TweetDuck.git
synced 2025-04-24 15:15:49 +02:00
Cleanup FileSerializer Write/Read calls & change exception for empty files
This commit is contained in:
parent
18f8d5b269
commit
9a6b615174
Configuration
Core/Other/Analytics
Data/Serialization
@ -1,6 +1,5 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using TweetDuck.Core.Utils;
|
||||
using TweetDuck.Data.Serialization;
|
||||
|
||||
namespace TweetDuck.Configuration{
|
||||
@ -34,7 +33,6 @@ private SystemConfig(string file){
|
||||
|
||||
public bool Save(){
|
||||
try{
|
||||
WindowsUtils.CreateDirectoryForFile(file);
|
||||
Serializer.Write(file, this);
|
||||
return true;
|
||||
}catch(Exception e){
|
||||
@ -47,9 +45,7 @@ public static SystemConfig Load(string file){
|
||||
SystemConfig config = new SystemConfig(file);
|
||||
|
||||
try{
|
||||
Serializer.Read(file, config);
|
||||
}catch(FileNotFoundException){
|
||||
}catch(DirectoryNotFoundException){
|
||||
Serializer.ReadIfExists(file, config);
|
||||
}catch(Exception e){
|
||||
Program.Reporter.HandleException("Configuration Error", "Could not open the system configuration file. If you continue, you will lose system specific configuration such as Hardware Acceleration.", true, e);
|
||||
}
|
||||
|
@ -142,8 +142,6 @@ private UserConfig(string file){
|
||||
|
||||
public bool Save(){
|
||||
try{
|
||||
WindowsUtils.CreateDirectoryForFile(file);
|
||||
|
||||
if (File.Exists(file)){
|
||||
string backupFile = GetBackupFile(file);
|
||||
File.Delete(backupFile);
|
||||
|
@ -1,6 +1,4 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using TweetDuck.Core.Utils;
|
||||
using TweetDuck.Data.Serialization;
|
||||
|
||||
namespace TweetDuck.Core.Other.Analytics{
|
||||
@ -30,7 +28,6 @@ private AnalyticsFile(string file){
|
||||
|
||||
public bool Save(){
|
||||
try{
|
||||
WindowsUtils.CreateDirectoryForFile(file);
|
||||
Serializer.Write(file, this);
|
||||
return true;
|
||||
}catch(Exception e){
|
||||
@ -43,9 +40,7 @@ public static AnalyticsFile Load(string file){
|
||||
AnalyticsFile config = new AnalyticsFile(file);
|
||||
|
||||
try{
|
||||
Serializer.Read(file, config);
|
||||
}catch(FileNotFoundException){
|
||||
}catch(DirectoryNotFoundException){
|
||||
Serializer.ReadIfExists(file, config);
|
||||
}catch(Exception e){
|
||||
Program.Reporter.HandleException("Analytics File Error", "Could not open the analytics file.", true, e);
|
||||
}
|
||||
|
@ -4,6 +4,7 @@
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.Serialization;
|
||||
using TweetDuck.Core.Utils;
|
||||
|
||||
namespace TweetDuck.Data.Serialization{
|
||||
sealed class FileSerializer<T>{
|
||||
@ -28,6 +29,8 @@ public void RegisterTypeConverter(Type type, ITypeConverter converter){
|
||||
}
|
||||
|
||||
public void Write(string file, T obj){
|
||||
WindowsUtils.CreateDirectoryForFile(file);
|
||||
|
||||
using(StreamWriter writer = new StreamWriter(new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.None))){
|
||||
foreach(KeyValuePair<string, PropertyInfo> prop in props){
|
||||
Type type = prop.Value.PropertyType;
|
||||
@ -54,8 +57,12 @@ public void Read(string file, T obj){
|
||||
Dictionary<string, string> unknownProperties = new Dictionary<string, string>(4);
|
||||
|
||||
using(StreamReader reader = new StreamReader(new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read))){
|
||||
if (reader.Peek() <= 1){
|
||||
throw new FormatException("Input appears to be a binary file.");
|
||||
switch(reader.Peek()){
|
||||
case -1:
|
||||
throw new FormatException("File is empty.");
|
||||
case 0:
|
||||
case 1:
|
||||
throw new FormatException("Input appears to be a binary file.");
|
||||
}
|
||||
|
||||
foreach(string line in reader.ReadToEnd().Split(new string[]{ NewLineReal }, StringSplitOptions.RemoveEmptyEntries)){
|
||||
@ -95,6 +102,13 @@ public void Read(string file, T obj){
|
||||
}
|
||||
}
|
||||
|
||||
public void ReadIfExists(string file, T obj){
|
||||
try{
|
||||
Read(file, obj);
|
||||
}catch(FileNotFoundException){
|
||||
}catch(DirectoryNotFoundException){}
|
||||
}
|
||||
|
||||
private sealed class BasicTypeConverter : ITypeConverter{
|
||||
bool ITypeConverter.TryWriteType(Type type, object value, out string converted){
|
||||
switch(Type.GetTypeCode(type)){
|
||||
|
Loading…
Reference in New Issue
Block a user