mirror of
https://github.com/chylex/TweetDuck.git
synced 2025-05-11 20:34:07 +02:00
Rewrite unknown property handling in FileSerializer
This commit is contained in:
parent
886eabe26c
commit
ca014f881c
@ -6,7 +6,7 @@
|
||||
namespace TweetDuck.Configuration{
|
||||
sealed class SystemConfig{
|
||||
private static readonly FileSerializer<SystemConfig> Serializer = new FileSerializer<SystemConfig>{
|
||||
OnReadUnknownProperty = (obj, property, value) => false
|
||||
// HandleUnknownProperties = (obj, data) => {}
|
||||
};
|
||||
|
||||
public static readonly bool IsHardwareAccelerationSupported = File.Exists(Path.Combine(Program.ProgramPath, "libEGL.dll")) &&
|
||||
|
@ -10,9 +10,10 @@
|
||||
|
||||
namespace TweetDuck.Configuration{
|
||||
sealed class UserConfig{
|
||||
private static readonly FileSerializer<UserConfig> Serializer = new FileSerializer<UserConfig>{
|
||||
OnReadUnknownProperty = (obj, property, value) => false
|
||||
};
|
||||
private static readonly FileSerializer<UserConfig> Serializer = new FileSerializer<UserConfig>{ HandleUnknownProperties = HandleUnknownProperties };
|
||||
|
||||
private static void HandleUnknownProperties(UserConfig obj, Dictionary<string, string> data){
|
||||
}
|
||||
|
||||
static UserConfig(){
|
||||
Serializer.RegisterTypeConverter(typeof(WindowState), WindowState.Converter);
|
||||
|
@ -12,8 +12,8 @@ sealed class FileSerializer<T>{
|
||||
|
||||
private static readonly ITypeConverter BasicSerializerObj = new BasicTypeConverter();
|
||||
|
||||
public delegate bool OnReadUnknownPropertyHandler(T obj, string property, string value);
|
||||
public OnReadUnknownPropertyHandler OnReadUnknownProperty { get; set; }
|
||||
public delegate void HandleUnknownPropertiesHandler(T obj, Dictionary<string, string> data);
|
||||
public HandleUnknownPropertiesHandler HandleUnknownProperties { get; set; }
|
||||
|
||||
private readonly Dictionary<string, PropertyInfo> props;
|
||||
private readonly Dictionary<Type, ITypeConverter> converters;
|
||||
@ -51,6 +51,8 @@ public void Write(string file, T obj){
|
||||
}
|
||||
|
||||
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.");
|
||||
@ -78,11 +80,19 @@ public void Read(string file, T obj){
|
||||
throw new SerializationException($"Invalid file format, cannot convert value: {value} (property: {property})");
|
||||
}
|
||||
}
|
||||
else if (!(OnReadUnknownProperty?.Invoke(obj, property, value) ?? false)){
|
||||
throw new SerializationException($"Invalid file format, unknown property: {property}+");
|
||||
else{
|
||||
unknownProperties[property] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (unknownProperties.Count > 0){
|
||||
HandleUnknownProperties?.Invoke(obj, unknownProperties);
|
||||
|
||||
if (unknownProperties.Count > 0){
|
||||
throw new SerializationException($"Invalid file format, unknown properties: {string.Join(", ", unknownProperties.Keys)}+");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class BasicTypeConverter : ITypeConverter{
|
||||
|
Loading…
Reference in New Issue
Block a user