mirror of
https://github.com/chylex/TweetDuck.git
synced 2025-05-01 17:34:10 +02:00
Refactor FileSerializer to attempt error recovery & tweak StreamWriter usage
This commit is contained in:
parent
73549515eb
commit
4aec2f3260
@ -3,7 +3,6 @@
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Runtime.Serialization;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using TweetDuck.Core.Utils;
|
using TweetDuck.Core.Utils;
|
||||||
|
|
||||||
@ -65,6 +64,8 @@ public void RegisterTypeConverter(Type type, ITypeConverter converter){
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void Write(string file, T obj){
|
public void Write(string file, T obj){
|
||||||
|
LinkedList<string> errors = new LinkedList<string>();
|
||||||
|
|
||||||
WindowsUtils.CreateDirectoryForFile(file);
|
WindowsUtils.CreateDirectoryForFile(file);
|
||||||
|
|
||||||
using(StreamWriter writer = new StreamWriter(new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.None))){
|
using(StreamWriter writer = new StreamWriter(new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.None))){
|
||||||
@ -78,15 +79,21 @@ public void Write(string file, T obj){
|
|||||||
|
|
||||||
if (serializer.TryWriteType(type, value, out string converted)){
|
if (serializer.TryWriteType(type, value, out string converted)){
|
||||||
if (converted != null){
|
if (converted != null){
|
||||||
writer.Write($"{prop.Key} {EscapeLine(converted)}");
|
writer.Write(prop.Key);
|
||||||
|
writer.Write(' ');
|
||||||
|
writer.Write(EscapeLine(converted));
|
||||||
writer.Write(NewLineReal);
|
writer.Write(NewLineReal);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
throw new SerializationException($"Invalid serialization type, conversion failed for: {type}");
|
errors.AddLast($"Missing converter for type: {type}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (errors.First != null){
|
||||||
|
throw new SerializationSoftException(errors.ToArray());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Read(string file, T obj){
|
public void Read(string file, T obj){
|
||||||
@ -103,6 +110,7 @@ public void Read(string file, T obj){
|
|||||||
throw new FormatException("Input appears to be a binary file.");
|
throw new FormatException("Input appears to be a binary file.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LinkedList<string> errors = new LinkedList<string>();
|
||||||
int currentPos = 0;
|
int currentPos = 0;
|
||||||
|
|
||||||
do{
|
do{
|
||||||
@ -125,7 +133,8 @@ public void Read(string file, T obj){
|
|||||||
int space = line.IndexOf(' ');
|
int space = line.IndexOf(' ');
|
||||||
|
|
||||||
if (space == -1){
|
if (space == -1){
|
||||||
throw new SerializationException($"Invalid file format, missing separator: {line}");
|
errors.AddLast($"Missing separator on line: {line}");
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
string property = line.Substring(0, space);
|
string property = line.Substring(0, space);
|
||||||
@ -140,10 +149,14 @@ public void Read(string file, T obj){
|
|||||||
info.SetValue(obj, converted);
|
info.SetValue(obj, converted);
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
throw new SerializationException($"Invalid file format, cannot convert value: {value} (property: {property})");
|
errors.AddLast($"Failed reading property {property} with value: {value}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}while(currentPos != -1);
|
}while(currentPos != -1);
|
||||||
|
|
||||||
|
if (errors.First != null){
|
||||||
|
throw new SerializationSoftException(errors.ToArray());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ReadIfExists(string file, T obj){
|
public void ReadIfExists(string file, T obj){
|
||||||
|
12
Data/Serialization/SerializationSoftException.cs
Normal file
12
Data/Serialization/SerializationSoftException.cs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace TweetDuck.Data.Serialization{
|
||||||
|
sealed class SerializationSoftException : Exception{
|
||||||
|
public IList<string> Errors { get; }
|
||||||
|
|
||||||
|
public SerializationSoftException(IList<string> errors) : base(string.Join(Environment.NewLine, errors)){
|
||||||
|
this.Errors = errors;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -247,6 +247,7 @@
|
|||||||
<Compile Include="Data\Serialization\FileSerializer.cs" />
|
<Compile Include="Data\Serialization\FileSerializer.cs" />
|
||||||
<Compile Include="Data\InjectedHTML.cs" />
|
<Compile Include="Data\InjectedHTML.cs" />
|
||||||
<Compile Include="Data\Serialization\ITypeConverter.cs" />
|
<Compile Include="Data\Serialization\ITypeConverter.cs" />
|
||||||
|
<Compile Include="Data\Serialization\SerializationSoftException.cs" />
|
||||||
<Compile Include="Data\Serialization\SingleTypeConverter.cs" />
|
<Compile Include="Data\Serialization\SingleTypeConverter.cs" />
|
||||||
<Compile Include="Data\TwoKeyDictionary.cs" />
|
<Compile Include="Data\TwoKeyDictionary.cs" />
|
||||||
<Compile Include="Data\WindowState.cs" />
|
<Compile Include="Data\WindowState.cs" />
|
||||||
|
Loading…
Reference in New Issue
Block a user